The “common wisdom” to prevent add_subdirectory() from running install() rules from inside it is to use
add_subdirectory(dir EXCLUDE_FROM_ALL). This works fine, except it also removes all targets from building as part of ALL (naturally), except when they are explicitly referenced.
We have sub-projects, added with add_subdirectory() and want to prevent install() rules from running in them, but we have to build the targets explicitly, because some are executables and modules (so not referenced anywhere), and install them in a custom way in the parent project. The trick we use is to create a dummy custom target part of ALL and make it depend on the subdir targets:
add_subdirectory(subproj EXCLUDE_FROM_ALL) # targets: subproj.target1, subproj.target2, etc. add_custom_target(do_build_subproj ALL) add_dependencies(do_build_subproj subproj.target1 subproj.target2)
Needless to say this is not ideal and just smells bad.
Is there any easier way to make this happen?
If not, would it make sense to make a feature request to support this use-case?
Well one other way to do it would be to set the EXCLUDE_FROM_ALL target property to false for the subproj targets, like:
set_target_properties(subproj.target1 subproj.target2 PROPERTIES EXCLUDE_FROM_ALL 0)
but it’s still tedious to list the targets individually.