Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid name clashes in cmake subprojects?

Tags:

cmake

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?

like image 630
Abstract-Sky Avatar asked Jun 05 '15 13:06

Abstract-Sky


1 Answers

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.

like image 125
Fraser Avatar answered Nov 07 '22 09:11

Fraser