So I have setup a metaproject, which means I have one directory (the root directory) containing multiple subdirectories, each of which is a cmake project (with a valid cmakelists). The cmakelists in the root directory imports the subprojects using the cmake add_subdirectory command.
Each subproject defines it's own targets (libraries and executables) as well as targets "check" which executes the test suite for that project and "docs" which runs doxygen.
My problem is that CMake does not understand that the check and docs targets are local to each project, and therefore complains about name clashes.
So my question is, is there any way to get cmake to understand that these targets are local to each project?
CMake doesn't allow duplicate target names. The rationale is provided in the docs for policy CMP0002
:
- Unique names may be referenced unambiguously both in CMake code and on make tool command lines.
- Logical names are used by Xcode and VS IDE generators to produce meaningful project names for the targets.
Your could try setting CMP0002
to OLD
(done via cmake_policy(SET CMP0002 OLD)
) which would at least get rid of the CMake error, but I'd recommend against that - it certainly won't work for MSVC/Xcode.
The only option I can see short of hand-coding unique names would be to write a CMake function which generates a unique name per "check" target - for example it could append the name of the target which is being checked, yielding names like check_my_lib_one
, check_my_exe_one
, etc.
This function could also be used to collect a list of all registered check_<Target>
instances and add them to a single target called check
which would invoke each subordinate check_<Target>
, so running make check
runs them all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With