[CMake] How to handle a submodule existing twice in a project?

Nicolas Desprès nicolas.despres at gmail.com
Fri May 18 11:24:25 EDT 2012


On Fri, May 18, 2012 at 1:15 AM, David Doria <daviddoria at gmail.com> wrote:
>> Yeah. Well, in that case I'd simply check for the TestB target thats
>> defined in TestB/CMakeLists.txt as condition for the top-levels
>> add_subdirectory:
>>
>> if(NOT TARGET TestB)
>>   add_subdirectory(TestB)
>> endif()
>>
>> That should work.
>>
>> Andreas
>
>
> Awesome, that seems to do the trick. Thanks!
>

Althought this trick works, be aware (assuming you are using Git
submodule) that the submodule shared by TestA and TestB (let's call it
TestC) may not have the same version. In other word, TestA may refer
to TestC version X whereas TestB may refer to TestC version Y which
could lead to very wierd and hard to debug errors. For those reasons I
try to avoid "diamond" submodules relationship and keep them flat. I
don't know an ideal way to handle that problem but what I do is to
have:
Test with 3 submodules: TestC, TestA and TestB. When compiling TestA
alone I use find_package() to search for a binary version of TestC or
a config file with target to import. Something like

if(NOT TARGET TestC)
  find_package(TestC REQUIRED)
endif()

Also I find this approach less cumbersome than updating the TestC
submodule in TestA and TestB each time TestC change.

Cheers,
-Nico


More information about the CMake mailing list