Let's say I have a C++ unit-test project using cmake like this:
$ tree
.
├── C-API-ConditionVariable-unit-test
│ ├── C-API-ConditionVariable-compile-link-test.c
│ ├── C-API-ConditionVariable-unit-test-0.cpp
│ └── C-API-ConditionVariable-unit-test-1.cpp
├── C-API-Mutex-unit-test
│ ├── C-API-Mutex-compile-link-test.c
│ ├── C-API-Mutex-unit-test-0.cpp
│ └── C-API-Mutex-unit-test-1.cpp
├── some
│ └── deeply
│ └── nested
│ └── path
│ └── SomeFeature-unit-test
│ ├── SomeFeature-compile-link-test.c
│ ├── SomeFeature-unit-test-0.cpp
│ └── SomeFeature-unit-test-1.cpp
└── CMakeLists.txt
Each source file from the subfolders creates a separate executable. I would like to have each subfolder in the project to be a non-standalone module - that is each subfolder is (more-or-less) self-contained, but is NOT a standalone cmake project which can be compiled separately. I would like to be able to only build everything or nothing. For example I don't want to make an impression that you could run cmake only from some/deeply/nested/path/SomeFeature-unit-test
to build only that.
Which option should I choose?
CMakeLists.txt
file in each subfolder +add_subdirectory()
in top-levelCmakeLists.txt
;some-random-name.cmake
in each subfolder +include()
in top-levelCmakeLists.txt
;- ignore my idea for each subfolder to be self-contained and put all relevant build info in the top-level
CMakeLists.txt
, additional cmake files only as helpers (macros, functions, ...);
The first option is the most convenient, but it suggests that each subfolder is actually a standalone project which could be compiled separately.
The second option seems to clearly suggest that there is only one cmake project here. But then in each some-random-name.cmake
in subfolders I have to use full path to source files, which is against my desire for each of them to be self contained. If there's only one level of nesting (like for first two example subfolders) it is ok, but for some/deeply/nested/path/SomeFeature-unit-test
this is not so nice. I would also have to prefix the names of output files.
The third option seems like an easy way to quickly create a CMakeLists.txt
file with a length of a spaghetti, so it possible I would prefer something else.
I would like know which is the "preferred" way in a "modern" cmake project. All the tutorials I could find for multi-directory project deal with the case when there's a standalone library in one folder, standalone application using that library in another folder and standalone tests using the same library in yet another directory. Looking at projects using cmake I see no consistency, so this doesn't help much.
(I'm a cmake noob, trying to convert a relatively large project to use cmake)