Reputation: 10556
I want to generate some compile time constants. The first answer to another question gets me quite close. From my CMakeLists.txt:
add_library(${PROJECT_NAME} STATIC ${CXX_SRCS} compile_time.hpp)
add_custom_command(OUTPUT compile_time.hpp
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/compile_time.cmake)
This works in the sense that the first time I run make
, it generates compile_time.hpp
, so that the values of the variables are defined when I run make
and not cmake
. But compile_time.hpp
is not remade when I rerun make
or even cmake
to redo the makefiles.
How can I make the target compile_time.cpp
be marked as phony
so that it is always remade? I tried
add_custom_target(compile_time.hpp)
to no effect.
Upvotes: 10
Views: 7507
Reputation: 66288
add_custom_target creates a "phony" target: It has no output and is always built. For make some target depended from the "phony" one, use add_dependencies()
call:
add_custom_target(compile_time
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/compile_time.cmake
)
# Because we use *target-level* dependency, there is no needs in specifying
# header file for 'add_library()' call.
add_library(${PROJECT_NAME} STATIC ${CXX_SRCS})
add_dependencies(${PROJECT_NAME} compile_time)
Library's dependency from the header compile_time.h will be detected automatically by headers scanning. Because script compile_time.cmake
updates this header unconditionally, the library will be rebuilt every time.
Upvotes: 9