Skip to content

target_sources() doesn't recognise GENERATED property for sources added from different scope to target

target_sources doesn't work with GENERATED source files. Here is a simple example:

Top-level CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(foo)
add_library(foo foo.cpp) # foo.cpp is a normal pre-existing file
add_subdirectory(bar)

bar/CMakeLists.txt:

set(bar "int bar() { return 0; }")
add_custom_command(COMMAND ${CMAKE_COMMAND} -E echo "${bar}" > bar.cpp
                   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp
                   VERBATIM
                   )
add_custom_target(gen_bar DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/bar.cpp PROPERTIES GENERATED 1)

add_dependencies(foo gen_bar)
target_sources(foo PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/bar.cpp)

CMake error:

CMake Error at CMakeLists.txt:3 (add_library):
  Cannot find source file:

    /Users/paleozogt/Development/test/cmake-test/.build/bar/bar.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx
Edited by Craig Scott
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information