binary_dir silently ignored in add_subdirectory(sub_dir [binary_dir] ) unless sub_dir has target(s)
Longer version at https://cmake.org/pipermail/cmake/2019-June/069547.html
Expected behavior:
- Either document and reject binary_dir argument as unsupported in this particular case. Warn at least.
- Or actually use binary_dir
https://cmake.org/cmake/help/v3.14/command/add_subdirectory.html
add_subdirectory(source_dir [binary_dir] ) "The binary_dir specifies the directory in which to place the output files. [...] If binary_dir is not specified, the value of source_dir, before expanding any relative path, will be used (the typical usage)."
I found this part of the documentation to be correct ONLY when the subdirectory defines and uses its own "sub-"target(s). If the subdirectory refers to higher main targets instead then binary_dir/ is just a decoy: the subdirectory build happens elsewhere. binary_dir is a decoy whether it's explicit or default. It exists but there are only totally generic and unused files there.
Example:
mainsrc/
├── CMakeLists.txt
├── inmodule
│ ├── CMakeLists.txt
│ ├── infile.c
└── mainfile.c
# mainsrc/CMakeLists.txt
add_executable(mainexe mainfile.c)
add_subdirectory(inmodule [ decoy_binary_dir ] )
# mainsrc/inmodule/CMakeLists.txt
target_sources(mainexe PRIVATE infile.c) # <= direct reference, no
sub-target
cmake -GNinja -B build -S mainsrc && ninja -C build
build
├── CMakeFiles
│ ├──
│ ├── mainexe.dir
│ │ ├──
│ │ ├──
│ │ ├── inmodule <= ACTUAL binary_dir!
│ │ │ └── infile.c.o
│ │ ├── mainfile.c.o
│ │ ├──
│ │ :
│ ├──
│ :
├── inmodule <= decoy with unused, boilerplate files unless there are target(s)
│ ├── CMakeFiles/
│ ├── cmake_install.cmake