target_sources does not always check for relative sources in interface
Files:
# src/repository/CMakeLists.txt
add_library(repository INTERFACE)
target_link_libraries(repository INTERFACE ZLIB::ZLIB)
target_sources(repository INTERFACE Unzipper.cpp Unzipper.hpp)
# src/CMakeLists.txt
add_subdirectory(repository)
add_executable( repository_debugger main.cpp )
target_link_libraries(repository_debugger repository spdlog::spdlog Threads::Threads)
The error is first reported when the repository
target is used:
CMake Error at src/CMakeLists.txt:2 (add_executable): Cannot find source file:
Unzipper.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx
I believe the right error message should be:
CMake Error in src/repository/CMakeLists.txt: Target "repository" contains relative path in its INTERFACE_SOURCES:
"Unzipper.cpp"
If I do this instead:
# src/CMakeLists.txt
add_subdirectory(repository)
add_library( repository INTERFACE )
target_link_libraries( repository INTERFACE ZLIB::ZLIB )
target_sources( repository INTERFACE repository/Unzipper.cpp repository/Unzipper.hpp )
add_executable( repository_debugger main.cpp )
target_link_libraries(repository_debugger repository spdlog::spdlog Threads::Threads)
The error is the expected one:
CMake Error in src/CMakeLists.txt: Target "repository" contains relative path in its INTERFACE_SOURCES:
"repository/Unzipper.cpp"
The check has been introduced about 2 years ago with bb5905bb . It seems it needs more tests.
Or am I doing something I shouldn't be doing?