Is there a generic way for disabling executable ta

2019-02-16 13:10发布

问题:

With our CMake build system I build some libraries and some executables. The build products are all output to a specific folder.

Now the problem is I have a VS2010 and a VS2008 toolchain but I only need the VS2008 toolchain for VS2008 libraries. The output executables are win32 target for both toolchains so I only need to build the executables once via the VS2010 toolchain while the VS2008 toolchain should just skip the executables and build only the desired libraries (which saves build time).

The CMake base scripts and overall setup may also be delivered to customers in the future so it would be very nice if there is a way in CMake to disable certain targets, like all executables in a generic way. Otherwise i have to write many big IF( BUILD_EXECUTABLES ) ... ENDIF() constructs around my executables setup in my CMakeLists.txt, without CMake giving me errors when I forget them.

The build is triggered via some batch files. Ideally i want to pass a variable to cmake via the -D option (e.g. "-D BUILD_EXECUTABLES=false")

I tried to wrap the ADD_EXECUTABLE macros but that does not work since I have calls like TARGET_LINK_LIBRARIES which then complain about the nonexistent target.

I could also set the output directory to some garbage folder which could be deleted afterwards, but that (as already mentioned) would not save build time (We have a quite huge project)

Any ideas how to accomplish that in clean and generic way?

回答1:

CMake targets have two properties which control if a target is built by default. The first one is EXCLUDE_FROM_ALL. It indicates if the target is excluded from the default build target. For Makefile generators, typing make will not trigger a build of a target whose EXCLUDE_FROM_ALL property is set to 1.

The other one is EXCLUDE_FROM_DEFAULT_BUILD and only applies to Visual Studio generators. If it is set to 1, the target will not be part of the default build when you invoke the "Build Solution" menu command.

You could set the values of both properties for executable targets depending on the option BUILD_EXECUTABLES:

if (NOT BUILD_EXECUTABLES)
   set_target_properties(exe1 exe2 PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
endif()


标签: cmake