RFC: inject CMakeLists.txt to add_subdirectory()

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 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")