Can add_custom_target(<name> SOURCES file1 file2) be used with an fxc compile?

Directory ${CMAKE_SOURCE_DIR}/shaders contains the two files VSFont.hlsl and PSFont.hlsl. This folder also contains a CMAKELists.txt file with these lines:

set(VSSrcHLSL “VSFont.hlsl” “PSFont.hlsl”)
add_custom_target(hlslSubSystem SOURCES “${VSSrcHLSL}”)

When I run CMake on the parent project it lists the two file VSFont.hlsl and PSFont.hlsl i the Visual Studio 2019 IDE under hlslSubSystem. This is what I want.

If I replace the shaders/CMakeList.txt file with this:

set(VSSrcHLSL “VSFont.hlsl” “PSFont.hlsl”)

add_custom_target(hlslSubSystem)

get_filename_component(strippedFileName “VSFont.hlsl” NAME_WE)
add_custom_command(TARGET hlslSubSystem POST_BUILD
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/VSFont.hlsl
COMMAND ${FXC} /T “vs_4_0” /E"FontVtxShader"
/Fh “…/inc/VertexShader.h” /Od /Zi
/Fo “${CMAKE_BINARY_DIR}”/bin/"${strippedFileName}.cso"
“${CMAKE_CURRENT_SOURCE_DIR}”/“VSFont.hlsl”
BYPRODUCTS ${CMAKE_BINARY_DIR}/Shaders/${strippedFileName}.cso
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

and similarly for the PSFont.hlsl compilation.

The parent project CMakeLists.txt specifies:

add_dependencies("${VSProjName}" hlslSubSystem)

so that when I run CMake on the parent and then build the project in Visual Studio the fxc compile happens then the entire project compiles and runs successfully. That is what I want.

However the files VSFont.hlsl and PSFont.hlsl are not listed in the Visual Studio IDE because I have left the SOURCES clause out of the add_custom_target( ) command. That is not what I want.

When I then add the SOURCES "${VSSrcHLSL} clause back into the add_custom_target( ) command the source files show up in the interface but the compile fails. It fails because it is looking for main as an entry point. It seems to be attempting the compile by other means that that specified in the add_custom_command( ) statement.

Using source_group( ) also fails. It is ignored. I have read elsewhere that it only works on targets that have been named in an add_executable( ). or add_library( ) command. I have also read elsewhere that I cannot use either of these on an fxc compile and that I must use add_custom_command( ).

If all this is true it would appear that it is not possible to compile an hlsl subsystem and get the hlsl source files listed in the Visual Studio 2019 IDE at the same time.

Is that true? If so I can stop trying.

If that is not true, can someone suggest another way to do it?

Thank you,.

I think add_custom_command(OUTPUT) is what you want (rather than add_custom_command(TARGET)). You can make a custom target with add_custom_target(tgtname DEPENDS ${output1} ${output2}). I don’t know what it’d take to get the hlsl files show up in the source list. Maybe just listing them as sources to the library which consumes the OUTPUT of their custom commands?

I will give it a try and report back how it goes.

Thank you.