Adding generated files in subdirectories fail
Adding generated files in subdirectories fail for object libraries and executable.
I had the problem on object libraries when upgrading from v3.15.
After a git bisect
: the error message on object libraries was introduced by commit 36ded610 (PCH: Generate sources during Compute step, 2019-10-05, v3.16.0-rc1~2^2)
During the creation of this example I found out, that it also doesn't work for executables. But this was already broken before. As I couldn't compile versions older than 3.8, I don't know if this ever worked.
Error message:
CMake Error at CMakeLists.txt:5 (add_executable):
Cannot find source file:
build/bar/bar_exe.c
...
CMake Error at bar/CMakeLists.txt:4 (add_library):
Cannot find source file:
build/bar/bar_obj.c
Conditions for failure:
- executable in toplevel dir
- generated files added in subdirectory
- file added to executable or object library (not static library)
- clean build directory (generated files from first call found otherwise)
Example to reproduce:
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(foo LANGUAGES C)
add_executable(foo)
target_link_libraries(foo PRIVATE bar bar_obj)
add_subdirectory(bar)
bar/CMakeLists.txt
target_sources(foo PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bar_exe.c")
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/bar_exe.c" CONTENT "")
add_library(bar_obj OBJECT)
target_sources(bar_obj PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bar_obj.c")
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/bar_obj.c" CONTENT "")
add_library(bar STATIC)
target_sources(bar PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/bar.c")
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/bar.c" CONTENT "")
Partial Workaround:
Setting the GENERATED
property manually helps for the object library,
but not for the executable.
set_source_files_properties(
"${CMAKE_CURRENT_BINARY_DIR}/bar_exe.c"
"${CMAKE_CURRENT_BINARY_DIR}/bar_obj.c"
PROPERTIES GENERATED TRUE
)