visual-studiovisual-studio-2015cmakehlsl

CMAKE avoid compilation of HLSL shader files on Visual Studio


I have a DirectX11 project with CMake on Windows. The project contains (at least) three sub-projects - one is for the core 3D engine library - a common asset directory which contains all the binary data used by the test or sample apps that builds on top of the engine.

./ 
+-- src/
|   +-- api/
|       +-- CmakeLists.txt
+-- tests/
|   +-- assets/
|   |   +-- shaders/
|   |   |   +-- 1.hlsl
|   |   |   +-- 2.hlsl
|   |   |       ...
|   |   +-- images/
|   |   |       ...
|   |   +-- models/
|   |   |       ...
|   |   +-- CmakeLists.txt
|   +-- sampleApp1/
|   |   +-- CmakeLists.txt
|   |        ...
|   +-- sampleApp2 
|   |   +-- CmakeLists.txt
|   |   +-- ...
|   |   ... 
|   +-- CmakeLists.txt
+-- CmakeLists.txt

The asset files are just being copied in relative to the test projects binary dir (${CMAKE_CURRENT_BINARY_DIR}) to make them available in relative to their work directory (../assets).

I have hlsl shader files in assets as well. When such files are being added to the project with CMake, VS will treat them as compilable source - obviously - which I wish to avoid, because I want such files to be edited with the IDE and just copy them to the binary folder and use the engine to compile, rather than let VS to do it.

I have tried to set as header file, like another source files that I wish to avoid building, but it seems that something sets them as shader file and overrides this for some reason.

Here is my setup I'm attempting with:

cmake_minimum_required (VERSION 2.6)
project (Project)
subdirs(assets)

In the asset folder I have the following:

file(GLOB_RECURSE FILES *.*)

add_custom_target(assets SOURCES ${FILES})
set_target_properties(assets PROPERTIES FOLDER tests)

# copy static sources 
foreach(_source IN ITEMS ${FILES})

    # this does not work for some reason
    set_source_files_properties(${_source} PROPERTIES HEADER_FILE_ONLY TRUE)

    if (IS_ABSOLUTE "${_source}")
        file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_source}")
    else()
        set(source_rel "${_source}")
    endif()

    get_filename_component(_source_path "${_source_rel}" PATH)
    get_filename_component(_source_name "${_source_rel}" NAME)    
    set(dst_path "${CMAKE_CURRENT_BINARY_DIR}/${_source_path}")

    file(MAKE_DIRECTORY ${dst_path})

    # -- win32 only
    if (WIN32)
        string(REPLACE "/" "\\" _source "${_source}")
        string(REPLACE "/" "\\" _dest "${dst_path}/${_source_name}")
    else()
        message("win32 required")
    endif(WIN32)

    add_custom_command(
        TARGET assets
        COMMAND copy ${_source} ${_dest}
        DEPENDS ${_source}
    )

endforeach()

I have tried the following method described in this answer, but didn't succeed.


Solution

  • You can exclude the shaders from the build as shown in this answer, which will prevent the fxc/dxc shader compilers from trying to compile them.

    set_source_files_properties(SOURCE ${MY_SHADERS} PROPERTIES VS_SETTINGS "ExcludedFromBuild=true")