RFC / Proposal / Feature Request
I’d like to be able to optionally specify a file to the add_subdirectory()
command, which it will use as CMakeLists.txt
, as if it were overlaid into the subdirectory. The file can have any path and name (not necessarily CMakeLists.txt
).
Like an “out-of-source build” (such as described in Professional CMake , section 2.2), this capability would introduce the notion of an “out-of-source configuration ”, where a CMakeLists.txt
file does not necessarily live next to or above the source files it configures.
My use case:
I have a CMake project using the Ninja generator and GNU Arm Embedded Toolchain, with libraries in Git submodules. I’d like to be able to add the libraries using add_subdirectory()
or similar, with its scoping features, without needing a CMakeLists.txt
in the subdirs. This would allow me to use the repos unmodified from upstream (since they don’t have CMakeLists.txt
files), and maintain my own CMakeLists.txt
files in the main project.
To date, I’ve been using add_subdirectory()
with forks of the library repos, with CMakeLists.txt
added in each. Existing project structure is like this:
MyCMakeProject/
├── CMakeLists.txt
├── src/
│ ├── CMakeLists.txt
│ └── main.cpp
├── libs/
│ ├── CMakeLists.txt
│ ├── library1/
│ │ ├── CMakeLists.txt
│ │ └── library1.cpp
│ └── library2/
│ ├── CMakeLists.txt
│ └── library2.cpp
Using the upstream repos means libs/library[12]/CMakeLists.txt
files will go away.
From what I’ve read, it seems like my options are:
Use include()
instead of add_subdirectory()
. This means no separate CMake scopes, and everything gets built directly into the main project.
Add extra subdirs for the upstream repos, such as shown below, to be able to continue to use add_subdirectory()
.
MyCMakeProject/
├── CMakeLists.txt
├── src/
│ ├── CMakeLists.txt
│ └── main.cpp
├── libs/
│ ├── CMakeLists.txt
│ ├── library1/
│ │ ├── CMakeLists.txt
│ │ └── upstream-repo/
│ │ └── library1.cpp
│ └── library2/
│ ├── CMakeLists.txt
│ └── upstream-repo/
│ └── library2.cpp
I believe a feature such as I’m suggesting here would allow a flatter, more straight-forward file/dir layout, like this:
MyCMakeProject/
├── CMakeLists.txt
├── src/
│ ├── CMakeLists.txt
│ └── main.cpp
├── libs/
│ ├── CMakeLists.txt
│ ├── cmake/
│ │ ├── CMakeLists_lib1.txt
│ │ └── CMakeLists_lib2.txt
│ ├── library1/
│ │ └── library1.cpp
│ └── library2/
│ └── library2.cpp
libs/CMakeLists.txt
would contain something like this:
add_subdirectory(library1 LISTS_FILE cmake/CMakeLists_lib1.txt)
add_subdirectory(library2 LISTS_FILE cmake/CMakeLists_lib2.txt)
libs/cmake/CMakeLists_lib1.txt
would then be treated by CMake as if it were libs/library1/CMakeLists.txt
, so it contains e.g.
add_library(library1 STATIC)
target_sources(library1 PRIVATE "library1.cpp")