Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let CMake set the "Exclude From Build" option for a single source file in Visual Studio

In Visual Studio there is an "Exclude From Build" option in the properties page of each source file, that can be set to exclude the file from build, but keep it visible in the source tree:

Screenshot property page

Is there a way to set that specific property with CMake? I found a VS_DEPLOYMENT_CONTENT property and tried that but without success (it doesn't seem to do anything).

The reason for using that property is mainly to keep the file in the project to be able to open and edit it from within Visual Studio.

Thanks in advance!

like image 716
bender Avatar asked Aug 10 '18 06:08

bender


2 Answers

Unfortunately, I also can't find the answer how to set "Exclude From Build" for some files with cmake in the same project. For now, I have added all such files to the target created by add_custom_target.

if(MSVC)
  add_custom_target(myproj.additional SOURCES ${otherHeaders} ${otherSources})
endif()

This will create an extra project in the solution. The files in this project will not compile and I can still edit and search in them.

Another option is to set the HEADER_FILE_ONLY property for sources that should not be compile:

if(MSVC)
  set_source_files_properties(${otherSources} PROPERTIES
    HEADER_FILE_ONLY TRUE
  )
endif()

In this case, the files stay in the project but without any marks - such files will not differ in appearance from those that are compiled

like image 194
mr NAE Avatar answered Nov 04 '22 10:11

mr NAE


Since CMake 3.18 you can use VS_SETTINGS. That allows you to set any VS Project setting you like.

set_property(SOURCE ${SourceFiles} PROPERTY VS_SETTINGS "ExcludedFromBuild=true")

Note that you have to set the settings values with the name as it is written in the vcxproj file. To know what the setting is called you can set the setting to the correct value via the VS IDE, save the project and then open the vcxproj file in a text editor and search for the correct setting-value pair. For example, in case of Excluded from Build:

<FXCompile Include="C:\path\to\source\file\file.hlsl">
  <ExcludedFromBuild>true</ExcludedFromBuild>
</FXCompile>
like image 39
Krienie Avatar answered Nov 04 '22 09:11

Krienie