Is there any way to do the equivalent of add_custom_command (run an external script when a certain file changes), but for something that should be run during CMake script ex
Turning my comment into an answer
configure_file() is the right choice to retrigger the configuration process. And execute_process() is used to run commands during the configuration step.
Here is an abstract CMake snippet of the steps in your description:
function(my_add_library_from_cfg _target _cfg_file)
# Retrigger configuration process each time config file changes
get_filename_component(_cfg_name "${_cfg_file}" NAME)
configure_file("${_cfg_file}" "${_cfg_name}.tmp")
# Generating sources and file list
execute_process(
COMMAND ${CMAKE_COMMAND} -E echo "#define FOO"
OUTPUT_FILE foo.c
)
execute_process(
COMMAND ${CMAKE_COMMAND} -E echo "foo.c"
OUTPUT_FILE files.lst
)
# Reading file list and add library
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/files.lst" _sources_lst)
add_library(${_target} ${_sources_lst} "${_cfg_file}")
endfunction(my_add_library_from_cfg)
configure_file()
will retrigger CMake each time the given .cfg
file changes
.cfg
file as a command line parameter in execute_process()
you should use the generated .tmp
version in the current binary directoryexecute_process()
can do a lot of things
stdout
and write the output to filesWORKING_DIRECTORY
here is the current binary directoryfile()
reads in the list of source files