Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake exclude executable during install

Tags:

cmake

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

like image 234
bobah Avatar asked Nov 14 '12 15:11

bobah


1 Answers

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:

  • Remove dependency of "install" target to "all" target (once, in the main CMakeLists.txt):

    set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)

  • Add OPTIONAL to the INSTALL statements in your test libraries. Be aware of the possible bug of CMake I reported here

  • Collect separately all the targets you want to include in a custom "all_but_tests" CMake add_custom_target depending on whole project being built (most difficult step)

  • Create a custom all_but_tests target:

    add_custom_target(all_but_tests DEPENDS <<list of targets>>)

  • Add a dependency of the target install to all_but_tests

    add_dependency(install all_but_tests)

    (Sorry, this I have never tried, feedback is welcome)

  • Create a custom tests target:

    add_custom_target(my_tests DEPENDS <<list of tests>>)

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"

like image 153
Antonio Avatar answered Sep 30 '22 11:09

Antonio