How to avoid name conflicts in cmake subprojects?

So, I have a metaproject, which means that I have one directory (root directory) containing several subdirectories, each of which is a cmake project (with valid cmakelists). Cmakelists in the root directory import subprojects using the cmake add_subdirectory command.

Each subproject defines its own goals (libraries and executables), as well as target β€œchecks” that run the test suite for this project and the β€œdocs” that doxygen runs.

My problem is that CMake does not understand that the verification targets and docs are local to each project and therefore complain about name conflicts.

So my question is: is there a way to get cmake to understand that these goals are local to each project?

+7
cmake
source share
1 answer

CMake does not allow duplicate target names. The rationale is given in the docs for the CMP0002 policy :

  • Unique names can be unambiguously specified both in CMake code and command line commands.
  • Logical names are used by the Xcode and VS IDE generators to create meaningful project names for purposes.

You might be trying to install CMP0002 on an OLD (made with cmake_policy(SET CMP0002 OLD) ), which would at least get rid of the CMake error, but I would recommend against this - it certainly will not work for MSVC / Xcode .

The only option that I can see, in addition to unique names for manual encoding, is to write a CMake function that generates a unique name for the "control" target - for example, it can add the name of the target to be verified, giving way to names such as check_my_lib_one , check_my_exe_one , etc. .d.

This function can also be used to collect a list of all registered instances of check_<Target> and add them to a single target named check , which each subordinate check_<Target> will call, so running make check launches them all.

+1
source share

All Articles