I have a C++ project with multiple targets. I redistribute the project via .deb packages which we build with CPack. Some of my CMakeLists.txt has include(CPack) line. It adds convenient package target to the build system, which I can call with ninja package to generate the .deb packages.
It works fine, but it builds ALL targets in the project, while only few of them actually make their way to the packages. Those targets are unsurprisingly marked with install(TARGET target_to_be_packaged) commands. Hence, I only want to build these targets when packaging to save time.
I am aware that I can specify targets manually and then run cpack manually:
$ ninja <the targets to be packaged>
$ cpack .
However, for this I need to compose the list of targets. While CMake already knows this list of targets! These are the ones mentioned in install commands. Is there a way to force CMake to only build necessary targets for me?
Of course, I can substitute the install command with some install_and_add_to_my_package_target command and then build my_package target, but I am looking for a cleaner solution.
Related: https://discourse.cmake.org/t/selectively-build-and-pack-targets/657