Question

I am trying to migrate a project from a custom build script to cmake. The source structure looks roughly like this:

src
 |
 +-CMakeLists.txt
 |
 +-generated
 |  |
 |  +-CMakeLists.txt
 |  |
 |  +-database
 |  |
 |  +-...
 |
 +-main
    |
    +-CMakeLists.txt
    |
    +-database
    |
    +-...

The source files in generated/database get autogenerated. cmake seems to be capable of this (that's not part of the question), but I wonder how I can make it build stuff in the right order. main/database contains the framework that is used in the autogenerated files. However, there are other folders in main that depend on the generated sources. If I structure the top-level CMakeLists.txt like this:

add_subdirectory("generated")
add_subdirectory("main")

I cannot refer to main/database in generated/CMakeLists.txt as dependency.

Overall, I have the impression that cmake forces me to structure my files according to their dependencies, but I want to preserve the current layout - the dependencies in the project are far too complex to map them onto a file system hierarchy.

Should I just avoid add_subdirectory and write everything in the top-level CMakeLists.txt? It seems like this should be possible. Or is there another way to solve this?

Was it helpful?

Solution

Overall, I have the impression that cmake forces me to structure
my files according to their dependencies

If CMake has information that target A depends on target B (for example target_link_libraries(A B)) B will be build first, then A. If you use generated sources CMake can't get dependency information and you need to provide it using add_dependencies.

From documentation:

Adding dependencies with this command can be used to make sure one target
is built before another target.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top