2

Let say I have a library ProjectA where I defined a custom target build-tests in my CMakefile to build my tests such that I can do make test afterwards. (see CMake & CTest : make test doesn't build tests).

Then, I have a second library ProjectB that depends on ProjectA. I use git submodule such that I can do add_subdirectory(ProjectA) in ProjectB's CMakefile. The problem is that, I would like to do same thing: define a custom target build-tests to build the tests of ProjectB but I cannot because it has already been defined in ProjectA...

My problem is similar to How to handle a transitive dependency conflict using Git submodules and CMake? and CMake and using git-submodule for dependence projects but they can define one target to avoid this while I would like to be able to do make build-tests in ProjectA AND in ProjectB.

So, is there a way to define a custom target locally to a cmake project for example ? or is there a clever way to do what I would like to do ? (which seems quite natural)

Pierre Marchand
  • 619
  • 1
  • 7
  • 10
  • Possible duplicate of [In CMake how to create targets with identical names?](https://stackoverflow.com/questions/44691109/in-cmake-how-to-create-targets-with-identical-names) – Tsyvarev Jul 12 '17 at 20:38
  • @Tsyvarev Not exactly a duplicate. Here it's about git submodules. The other question was about cmake subprojects. They are slightly different things. – skypjack Jul 12 '17 at 20:53
  • @skypjack: This question just mention git submodules, but they are not main purpose of it. BTW, you have post very similar answer to the duplicate question. – Tsyvarev Jul 12 '17 at 21:37
  • @Tsyvarev Well, the same variable of cmake solves both the problems, even if they are part of different questions. So, yes, it makes perfectly sense to give similar answers indeed. – skypjack Jul 12 '17 at 21:39

2 Answers2

0

So, is there a way to define a custom target locally to a cmake project for example ? or is there a clever way to do what I would like to do ? (which seems quite natural)

For you are working with custom targets, probably ALLOW_DUPLICATE_CUSTOM_TARGETS can do the job.
The documentation is quite clear indeed:

Allow duplicate custom targets to be created.

If you go a bit further you find that:

The property allows multiple add_custom_target command calls in different directories to specify the same target name.

Unfortunately it has also some limitations:

[...] setting this property will cause non-Makefile generators to produce an error and refuse to generate the project.

Read carefully the documentation (see the link above) to know if it's suitable for your purposes.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • I have accepted this answer because it solves the problem I have submitted. But I have to admit that I used cmake also because it allows people to build projects for their IDE (Xcode for example). And using duplicate targets seems to forbid this so I ended up not using git submodules and I build and test my libraries separately. I just needed to define a FindProjectA.cmake to handle the dependency in 'ProjectB'. – Pierre Marchand Jul 23 '17 at 10:09
  • @PierreMarchand Yeah, to allow duplicate custom targets has some limitations that are mentioned in the documentation. Unfortunately it doesn't depend on cmake, instead it's due to the underlying tools used on the different platforms. There is no clean, up and running solution here but the handwritten workarounds like you did if you cannot deal with those limitations. – skypjack Jul 23 '17 at 10:18
0

I think I found a better solution. Let us assume:

  1. You have control of the CMakeLists.txt file in both projects.
  2. The shared target is expected to be configured identically.

Then you can do something like:

if(NOT TARGET build-tests)
    add_custom_target(build-tests ALL)
endif()

add_custom_target(build-tests.ProjectA ...)
add_dependencies(build-tests build-tests.ProjectA)

The build-tests target will essentially become the union of each project's build-tests target.

user1202136
  • 11,171
  • 4
  • 41
  • 62