From 6594c420e2092f26fa050d897d69b5030e32f83d Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Sun, 2 Jun 2024 01:52:40 -0700 Subject: [PATCH] Add MetalShaderSupport CMake helper --- CMakeLists.txt | 1 + cmake/MetalShaderSupport.cmake | 41 +++++++++++++++++++++++++++++++ example/ExampleApp/CMakeLists.txt | 15 +++++------ example/ShaderBase/CMakeLists.txt | 10 +------- 4 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 cmake/MetalShaderSupport.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 0912dd5..cb36d05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.12) set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_SOURCE_DIR}/cmake") include(CheckLanguage) +include(MetalShaderSupport) project(MetalTest) diff --git a/cmake/MetalShaderSupport.cmake b/cmake/MetalShaderSupport.cmake new file mode 100644 index 0000000..7d7bb1a --- /dev/null +++ b/cmake/MetalShaderSupport.cmake @@ -0,0 +1,41 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file LICENCE.txt or https://cmake.org/licensing for details. + +function(add_metal_shader_library TARGET) + cmake_parse_arguments(PARSE_ARGV 1 _amsl + "" + "" #TODO: support "STANDARD" + "" + ) + + add_library(${TARGET} MODULE ${_amsl_UNPARSED_ARGUMENTS}) + + set_target_properties(${TARGET} PROPERTIES + XCODE_PRODUCT_TYPE com.apple.product-type.metal-library + XCODE_ATTRIBUTE_MTL_FAST_MATH "YES" + XCODE_ATTRIBUTE_MTL_ENABLE_DEBUG_INFO[variant=Debug] "INCLUDE_SOURCE" + XCODE_ATTRIBUTE_MTL_ENABLE_DEBUG_INFO[variant=RelWithDebInfo] "INCLUDE_SOURCE" + ) +endfunction() + +function(target_embed_metal_shader_libraries TARGET) + cmake_parse_arguments(PARSE_ARGV 1 _temsl + "" + "" + "" + ) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.28 AND ${CMAKE_GENERATOR} STREQUAL "Xcode") + set_target_properties(${TARGET} PROPERTIES + XCODE_EMBED_RESOURCES ${_temsl_UNPARSED_ARGUMENTS} + ) + else() + foreach(SHADERLIB IN LISTS _temsl_UNPARSED_ARGUMENTS) + add_custom_command(TARGET ${TARGET} POST_BUILD + DEPENDS ${SHADERLIB} + COMMAND ${CMAKE_COMMAND} -E copy "$" "$/Resources/$" + VERBATIM + ) + endforeach() + endif() +endfunction() diff --git a/example/ExampleApp/CMakeLists.txt b/example/ExampleApp/CMakeLists.txt index 128c696..2ee61c6 100644 --- a/example/ExampleApp/CMakeLists.txt +++ b/example/ExampleApp/CMakeLists.txt @@ -23,7 +23,6 @@ set(ExampleApp_RESOURCES add_executable(ExampleApp MACOSX_BUNDLE ${ExampleApp_SOURCES} ${ExampleApp_HEADERS} ${ExampleApp_RESOURCES} ) -add_dependencies(ExampleApp ShaderBase) set_target_properties(ExampleApp PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist" @@ -31,17 +30,15 @@ set_target_properties(ExampleApp PROPERTIES XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER ca.dpogue.metaltest.example ) -if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.28 AND ${CMAKE_GENERATOR} STREQUAL "Xcode") +target_embed_metal_shader_libraries(ExampleApp + ShaderBase +) + +if(${CMAKE_GENERATOR} STREQUAL "Xcode") set_target_properties(ExampleApp PROPERTIES - RESOURCE "${ExampleApp_RESOURCES}" - XCODE_EMBED_RESOURCES ShaderBase + RESOURCE ${ExampleApp_RESOURCES} ) else() - add_custom_command(TARGET ExampleApp POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy "$" "$/Resources/$" - VERBATIM - ) - find_program(IBTOOL ibtool HINTS "/usr/bin" "${OSX_DEVELOPER_ROOT}/usr/bin") add_custom_command(TARGET ExampleApp POST_BUILD COMMAND ${IBTOOL} --output-format human-readable-text --compile "$/Resources/Main.storyboardc" "${CMAKE_CURRENT_SOURCE_DIR}/Main.storyboard" diff --git a/example/ShaderBase/CMakeLists.txt b/example/ShaderBase/CMakeLists.txt index 55d4356..4337f43 100644 --- a/example/ShaderBase/CMakeLists.txt +++ b/example/ShaderBase/CMakeLists.txt @@ -9,15 +9,7 @@ set(ShaderBase_SOURCES AAPLShaders.metal ) -add_library(ShaderBase MODULE +add_metal_shader_library(ShaderBase ${ShaderBase_SOURCES} ${ShaderBase_HEADERS} ) - -set_target_properties(ShaderBase PROPERTIES - XCODE_PRODUCT_TYPE com.apple.product-type.metal-library - #XCODE_ATTRIBUTE_MTL_LANGUAGE_REVISION Metal21 - XCODE_ATTRIBUTE_MTL_FAST_MATH "YES" - XCODE_ATTRIBUTE_MTL_ENABLE_DEBUG_INFO[variant=Debug] "INCLUDE_SOURCE" - XCODE_ATTRIBUTE_MTL_ENABLE_DEBUG_INFO[variant=RelWithDebInfo] "INCLUDE_SOURCE" -)