5

How to configure/hack cmake to build particular executable added with add_executable() but do not install it?

The executable is a unit test and will eventually be handled with add_test but for now I just want to strip test binaries off the release touching as little as possible.

Thanks

bobah
  • 18,364
  • 2
  • 37
  • 70

2 Answers2

7

Since EXCLUDE_FROM_ALL has undefined behavior if combined with INSTALL (cmake tries to warn you, it doesn't matter if OPTIONAL is set), a solution guaranteed to work is more complicated.

You should:

Then (supposing you are using make, but will work also for ninja):

  • You can call make install, which triggers make all_but_tests, and installs once built is complete.

  • You can call make my_tests, and then make install, in this case it will install everything. You can concatenate the commands like this

    make my_tests && make install

    or, since there is not difference in this case:

    make [all] && make install

I was attracted by this question because I recently had to face a similar problem: Installing only one target and its dependencies

Edit:

add_dependency(install all_but_tests) will probably not work.

So, either you use an adequate workaround, or you call

make all_but_tests && make install

everytime you want to install "all_but_tests"

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
6

CMake will only install an executable target if you apply the install function to it, i.e.:

 install(TARGETS ExecutableTest RUNTIME DESTINATION "bin")

To prevent the installation of the ExecutableTest for a Release build, add a CONFIGURATIONS restriction:

 install(TARGETS ExecutableTest RUNTIME DESTINATION "bin" CONFIGURATIONS Debug)

Alternatively, you can make ExecutableTest an optional target, which is not built by default:

add_executable(ExecutableTest EXCLUDE_FROM_ALL ${ExecutableTestFiles})

and then optionally only install the ExecutableTest if it has been built explicitly:

 install(TARGETS ExecutableTest RUNTIME DESTINATION "bin" OPTIONAL)

All optional test targets can be pooled in a super target to allow for building them in one step:

add_custom_target(MyTests DEPENDS ExecutableTest ExecutableTest2 ExecutableTest3)
sakra
  • 62,199
  • 16
  • 168
  • 151
  • I need it for both Debug and Release, so first suggestion, although feasible won't work. the one with optional target seems exactly what I want, am going to give it a try in a sec and will give this answer a chevron soon after. – bobah Nov 14 '12 at 16:30
  • The #2 did prevent tests from being built, Is there a way to group all these optional targets to be able to say "make mytests"? – bobah Nov 15 '12 at 07:52
  • 1
    shouldn't it be `add_custom_target(MyTests DEPENDS ExecutableTest ExecutableTest2 ExecutableTest3)`, so adding **DEPENDS** keyword? – Antonio Jun 21 '13 at 09:24
  • @Antonio. Thanks for spotting the mistake. I have updated the answer. – sakra Jun 21 '13 at 17:08