Let’s say I have two files main.cpp
and generate_header.cpp
. generate_header.cpp
generates a header containing data that main.cpp
needs to run. based on this answer, I can use the following code to run generate_header.cpp
before building main.cpp
:
add_custom_target(run_generate_header
COMMAND generate_header
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "generating header"
)
add_dependencies(main_target run_generate_header)
However, as generating the header is slow, I would like to generate it only if it does not contain data.
To achieve this, I thought about using a preprocessor macro HEADER_GENERATED
, and check_cxx_symbol_exists
to generate the header only if the macro is not defined.
check_cxx_symbol_exists(HEADER_GENERATED ${HEADER_PATH} HEADER_GENERATED)
if (NOT HEADER_GENERATED)
add_dependencies(main_target run_generate_header)
endif()
The problem with this approach is that check_cxx_symbol_exists
runs during the configuration stage, not the build stage, so if the header is erased after configuration, it will not be regenerated and the build will fail. Is there a way to check if the header contains data during the build stage? Thanks in advance!