Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a generic way for disabling executable targets in cmake

Tags:

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 for how to accomplish that in clean and generic way?

like image 258
ecreif Avatar asked Feb 14 '13 12:02

ecreif


People also ask

What are targets in CMake?

Introduction. A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.

What is a CMake executable?

The cmake executable is the command-line interface of the cross-platform buildsystem generator CMake. The above Synopsis lists various actions the tool can perform as described in sections below. To build a software project with CMake, Generate a Project Buildsystem.

Where can I find CMake executable?

Run cmake-gui.exe, which should be in your Start menu under Program Files, there may also be a shortcut on your desktop, or if you built from source, it will be in the build directory.


1 Answers

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()
like image 159
sakra Avatar answered Oct 13 '22 10:10

sakra