I am using cmake as build generator. version : 3.18.5 I have 5 targets in my project. I want to compile only particular set of targets depends upon the option i give during make. How to do this?
for example, if i run
gmake -j4 foo=set2
I want target app1,app3,app5 should get compiled. if i run
gmake -j4 foo=3
i want target app2,app4 should get compiled.
i tried the above things and inside cmakelist.txt,
if(foo STREQUAL "set2")
add_subdirectory(../../app1)
add_subdirectory(../../app3)
add_subdirectory(../../app5)
elseif(foo STREQUAL "set3")
add_subdirectory(../../app2)
add_subdirectory(../../app4)
endif()
But i did not got the expected result.
Your if(foo STREQUAL ..)
construction doesn't work because it is evaluated at configuration time, not during the build. It won't be possible to make the exact syntax you envisioned work, but we can get close.
If you add two custom targets
add_custom_target(set2)
add_custom_target(set3)
and then manually specify the targets that belong to each set as dependencies
add_dependencies(set2 app1 app3 app5)
add_dependencies(set3 app2 app4)
after including all of the subdirectories, then calling gmake -j4 set2
(or cmake --build . --target set2 -- -j4
) should build just the 3 apps.