Selectively build and pack targets

Hi,
at our company we encountered the following problem. If we try to package our software using cpack, all targets in the CMakeLists.txt get build before the actual packaging. This however takes a very long time because unit tests and other services, which are not part of the package, get build as well.
How can we disable this behavior?

Plus, in any case, which of our executable gets part of the package? Only these with install instructions, or everything?

Greetings
Jan

2 Likes

You can run cpack rather than make package or ninja package, which will then skip the build.

Alternatively, you can set EXCLUDE_FROM_ALL target property or directory property on the targets you don’t want to build.

Okay. Running cpack alone might be an option. However, I cannot run it on multiple cores, can I?

1 Like

No, but you can’t when running make package either, so you won’t lose any performance benefit by doing that.

1 Like

make package -j works perfectly fine for me… Anyways. I’m not sure if CPack is doing anything as it doesn’t print progress. Maybe it’s building something, but on one core this might take several hours.
Any other ideas besides EXCLUDE_FROM_ALL?

make package -j is performing the build in parallel. The actual package step can’t be parallelized, because it uses the cmake_install.cmake file, which runs serially.

Ah okay. So what I would like then is a parallel build, but with only the targets which are needed for the package. For example, unit tests should be skipped. The result should then be packaged for release.

In that case I would run:

$ make -j10 <the targets you want>
$ cpack .

then you may add DISABLE_TESTS option to your main CMakeLists.txt and do something like:

if(DISABLE_TESTS)
   message(STATUS "tests disabled")  
else()
   enable_testing()
endif()

Then after that, I don’t know the layout of your project but in my usual layout sources for unit tests are in a separate directory so going in ‘tests’ subdir is only done if(NOT DISABLE_TESTS) i.e.

if(NOT DISABLE_TESTS)
  add_subdirectory(tests)
endif()

cpack internally runs install before building the package so if any target is not installed it won’t be packaged either. This is usually the case of unit test executable and any generator tool built & used in the build for example.

This is good to hear. But still annoying that everything gets build.

Yes, but cmake should be able to generate these targets…

Sounds good, but can cmake/cpack deduce these targets for me? I mostly call install with TARGETS option, so these dependencies should be clear for cmake/cpack.

Related on Stack Overflow: Build only necessary targets for CPack