Search code examples
visual-studiocmakehlsl

CMake set_property command with generator expressions using build type


I'm creating a CMake project using v3.14.4 and the "Visual Studio 15 2017 Win64" generator. During the build, a .hlsl file is also to be compiled and stored in the same directory as the .exe. Something is preventing my generator for the hlsl file's VS_SHADER_OBJECT_FILE_NAME property from correctly being processed.

This is my CMake statement:

set_property(SOURCE shader.hlsl PROPERTY VS_SHADER_OBJECT_FILE_NAME
  $<$<CONFIG:Release>:Release/shader.dxbc>
  $<$<CONFIG:Debug>:Debug/shader.dxbc>)

The result in VS shows the expression was pretty much passed through.

$<$<CONFIG:Release>:Release/shader.dxbc>;$<$<CONFIG:Debug>:Debug/shader.dxbc>

I've used generators successfully with the target_compile_definitions() statement and set_property() is to also be supported. Is my usage malformed or better written a different way?


Solution

  • As pointed out by Tsyvarev, the VS_SHADER_OBJECT_FILE_NAME property doesn't support generator expressions. One solution to this is to copy the compiled shader to the desired destination for each build type, in this case the same directory as the .exe. The below function copy_compiled_shader will copy SHADERBC_FILE to a new directory determined by using a generator function.

    set_property(SOURCE shader.hlsl PROPERTY VS_SHADER_OBJECT_FILE_NAME shader.dxbc)
    
    function(copy_compiled_shader SHADERBC_FILE)
      add_custom_command(
        TARGET TestCS
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Copying the shader ${SHADERBC_FILE}"
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SHADERBC_FILE} $<TARGET_FILE_DIR:TestCS>/${SHADERBC_FILE}
      )
    endfunction()
    
    copy_compiled_shader(shader.dxbc)