Project dependencies across different directories

2019-09-04 18:19发布

问题:

Suppose I have such project structure:

Root
  + ProjA
     + SubA.1
     + SubA.2
  + ProjB
     + SubB.1
     + SubB.2

And the dependencies I setup is:

SubA.2 depends on SubA.1
ProjA depends on SubA.1 and SubA.2 
ProjB depends on ProjA

I want to make the build order looks like this when start with make -jX:

1. SubA.1
2. SubA.2
3. SubB.1 SubB.2

But the fact is:

1. SubA.1 SubB.1 SubB.2
2. SubA.2

It seems the ProjB => ProjA can't be used to make all the SubBs build after SubAs.

How can I make all the SubBs build after all the SubAs finishes?

回答1:

In CMake dependencies can be set only on per-target basis.

You should decide, whether ProjB depends on internals of ProjA or not. By "internals" I mean names of targets, values of CMake variables and so on.

  1. If ProjB actually require internals of ProjA, you may carefully determine targets of ProjA, which are required for one or another target of ProjB, and set only needed dependencies. Such a way building of ProjA may interleave with ProjB with make -j, but building will be correct.

E.g., if SubB.1 is executable, which requires SubA.1 library for link with, then

target_link_libraries(SubB.1 SubA.1)

automatically sets needed dependencies.

If SubB.2 is generated using executable SubA.2, then usage

add_custom_command(OUTPUT <subB.2_file>
    COMMAND SubA.2 <args>
)

will automatically set needed dependencies.

  1. If ProjB doesn't aware about internal of ProjA and it is only known that ProjB can be built only when ProjA is already built, then you may use ExternalProject_Add for build both projects and set dependencies between created targets:

CMakeLists.txt:

ExternalProject_Add(ProjA ...)
ExternalProject_Add(ProjB ...)
# Now `ProjA` and `ProjB` are targets in top-level project.
add_dependencies(ProjB ProjA)


标签: cmake