Autogen: Imported library's location become target's autogen_timestamp_deps?
The CMakeLists.txt attached below contains 1 imported static library ("built" by custom command's BYPRODUCTS
) and 2 executables. Both executables depend on the imported library, but only test2
executable have CMAKE_AUTOMOC
enabled. When building with make -j14
, following error occurred:
make[2]: *** No rule to make target 'libgtest.a', needed by 'CMakeFiles/test2_autogen_timestamp_deps'. Stop
make[1]: *** [CMakeFiles/Makefile2:168: CMakeFiles/test2_autogen_timestamp_deps.dir/all] Error 2
By deeper inspection, it's found that libgtest.a
is depended on by test2_autogen_timestamp_deps.dir/build.make
, but Makefile2
doesn't add a dependency from test2_autogen_timestamp_deps.dir/all
to GTest.dir/all
where the lib is actually "built".
Now, if I move the BYPRODUCTS
to the outer level custom target, CMake does add the dependency to GTest.dir/all
in Makefile2
. But in our project, these steps are actually added by ExternalProject
and I don't have any idea on how we can influence the BYPRODUCTS
of the top-level custom target (or any STEP_TARGETS
it creates).
And, if I read the doc correctly, autogen_timestamp_deps
is supposed to be used for moc
, but I don't think a static library have anything to do with that. So I don't think this is an intended behavior?
Tested with CMake 3.28.3 and 3.29.0-rc3.
cmake_minimum_required(VERSION 3.5.0)
project(automoc-and-imported-lib VERSION 0.0 LANGUAGES CXX)
find_package(Qt5 COMPONENTS Core)
# Can be any random static lib on your system
set(a_static_lib /usr/lib/x86_64-linux-gnu/libgtest.a)
# In our project, this is ExternalProject_Add(). Imagine this is the build step...
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fakeBuildGTest
COMMAND cp ${a_static_lib} ${CMAKE_CURRENT_BINARY_DIR}/libgtest.a
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/libgtest.a)
# ...which gets pulled in by the custom target.
add_custom_target(GTest DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/fakeBuildGTest)
add_library(gtest STATIC IMPORTED)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/libgtest.a)
add_dependencies(gtest GTest)
write_file(test.cpp "int main() { return 0; }")
add_executable(test1 test.cpp)
target_link_libraries(test1 gtest)
set(CMAKE_AUTOMOC ON)
add_executable(test2 test.cpp)
target_link_libraries(test2 gtest)