Sunday, October 30, 2022

[SOLVED] Avoid rebuilding of unselected and restored cmake subdirectories

Issue

I have a CMake project with multiple targets which are added via add_subdirectory, and add_subdirectory calls are guarded by options. Using Ninja as build runner.

Now when I turn some specific option off and reconfigure, all object files for the now missing subdirectory are apparently invalidated. When I turn the option back on, add_subdirectory is run again but it wants to recompile everything from scratch.

Obviously ccache helps here a lot because the input and output are perfectly identical. But copying objects around is still much slower than not copying anything at all, and the linker runtime comes on top.

Is there any way to tell cmake: if subdirectory is gone, please consider this temporarily and keep the files and their information valid, just not use them?


Solution

if subdirectory is gone, please consider this temporarily and keep the files and their information valid, just not use them?

Exactly, so do not use them. Do not modify add_subdirectory, change the dependency of projects not to depend on the targets exported by subdirectory.

add_subdirectory(A EXCLUDE_FROM_ALL)
if(option)
   target_link_libraries(your_super_target A)
endif()


Answered By - KamilCuk
Answer Checked By - Mildred Charles (WPSolving Admin)