# Can add\_custom\_target(\<name\> SOURCES file1 file2) be used with an fxc compile?

**URL:** https://discourse.cmake.org/t/can-add-custom-target-name-sources-file1-file2-be-used-with-an-fxc-compile/2548
**Category:** Usage
**Tags:** os:windows
**Created:** [January 14, 2021, 10:48pm UTC](https://discourse.cmake.org/t/can-add-custom-target-name-sources-file1-file2-be-used-with-an-fxc-compile/2548 "2021-01-14T22:48:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dudge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/51bf81/32.png) [@dudge](https://discourse.cmake.org/u/dudge)
#### Post date: [January 14, 2021, 10:48pm UTC](https://discourse.cmake.org/t/can-add-custom-target-name-sources-file1-file2-be-used-with-an-fxc-compile/2548/1 "2021-01-14T22:48:21Z")

</div>

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,.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 19, 2021, 7:24pm UTC](https://discourse.cmake.org/t/can-add-custom-target-name-sources-file1-file2-be-used-with-an-fxc-compile/2548/2 "2021-01-19T19:24:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dudge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/51bf81/32.png) [@dudge](https://discourse.cmake.org/u/dudge)
#### Post date: [January 20, 2021, 2:55pm UTC](https://discourse.cmake.org/t/can-add-custom-target-name-sources-file1-file2-be-used-with-an-fxc-compile/2548/3 "2021-01-20T14:55:09Z")

</div>

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

Thank you.
