Suppose the source tree is in this structure:
/
|- lib1
| |- src.cpp
| |- lib1.h
| |- CMakeLists.txt
|
|- lib2
| |- src.cpp
| |- lib2.h
| |- CMakeLists.txt
|
|- lib3
| |- src.cpp
| |- lib3.h
| |- CMakeLists.txt
|
|- app
| |- src.cpp
| |- CMakeLists.txt
|
|- CMakeLists.txt
Suppose:
- lib1 has function f();
- lib2 has function g() which uses f();
- app/src.cpp uses function g();
- Nobody uses lib3.
I want:
- in app/CMakeLists.txt, it only link to lib2. The logic here is, app/src.cpp only uses g(), so when writing app/src.cpp, we can't specify the dependency to lib1 because it is implementation detail of lib2. So according to this logic, in app/CMakeLists.txt, it can't have anything related to lib1, i.e. it neither include_directories of lib1, add_subdirectory of lib1, nor target_link_libraries of lib1, etc.
- Since nobody uses lib3, it won't even be built. This needs to be done automatically. So manually add_subdirectory for lib1 and lib2 but not lib3 is not a clever way. You can imagine if we have a very large source tree with complicated tree structure and dependencies and hundreds of executables in hundreds of different subdirectories. If I only want to build several of them, then I don't want to bother building unused libraries at all.
So my question is: is there a way to write the CMakeLists.txt files in a scalable way to satisfy the above requirements? If not, then is there some similar tools that can do this?
Thanks.