First off, you should use add_subdirectory()
instead of subdirs()
, which is deprecated. You get more precise control of the order of processing that way.
Second, because everything depends on tool2, you should build tool2 first.
main-tool/CMakeLists.txt:
add_subdirectory{"tool2"}
add_subdirectory{"tool1"}
Don't add another add_subdirectory
command in the tool1 directory. There are two approaches you can use to access tool2 from the tool1 build.
- First, you could define some cmake variables in the top level main CMakeLists.txt related to tool2, then populate those variables in the tool2 build. You might need to use the PARENT_SCOPE directive to
set
those variables from the tool2 subdirectory. Then use those variables to access tool2 from the tool1 subdirectory.
- Alternatively, you could build all three tools (tool2, tool1, and main tool, in that order) from the top level CMakeLists file. The build file can get long, but it saves headaches trying to manage the scope of cmake variables. This is the approach I usually take and I recommend it.
To summarize, my recommendation is "one big CMakeLists.txt file". Unless it gets really, really big.