Source for add_executable that is generated by add_custom_command will fail if it's not within the same CMakeLists
Please see below minimal example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(example)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/main.cpp
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/main.cpp.in ${CMAKE_BINARY_DIR}/main.cpp)
add_executable(${PROJECT_NAME} ${CMAKE_BINARY_DIR}/main.cpp)
This works as intended, but if I simply modify to the below:
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(example)
add_subdirectory(subdir)
add_executable(${PROJECT_NAME} ${CMAKE_BINARY_DIR}/main.cpp)
subdir/CMakeLists.txt
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/main.cpp
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/main.cpp.in ${CMAKE_BINARY_DIR}/main.cpp)
We will see cmake configuration error:
Cannot find source file:
/home/jasjuang/example/build/main.cpp
I think this is a bug because it is reasonable to assume that the second scenario should work as intended too.