I improved the last solution to this:
# macros
macro(add_configurable_file in_path configured_path)
file(APPEND "${configure_script_path}" "
configure_file(
\"${in_path}\"
\"${configured_path}\"
@ONLY
)
")
endmacro()
# variables
set(configure_script_path "${VisualT_BINARY_DIR}/conf/configureScript.cmake")
string(TIMESTAMP VisualT_BUILD_DATE "%d %B %Y" UTC)
# register configurable files
file(REMOVE "${configure_script_path}")
add_configurable_file("${VisualT_SOURCE_DIR}/src/version.h.in" "${VisualT_BINARY_DIR}/src/version.h")
add_configurable_file("${VisualT_SOURCE_DIR}/doxyfile.in" "${VisualT_BINARY_DIR}/doxyfile")
# build-time configuration if main project
if("${CMAKE_PROJECT_NAME}" STREQUAL "VisualT")
add_custom_target(configured_files ALL
COMMAND "${CMAKE_COMMAND}" -P "${configure_script_path}"
COMMENT "running configure script")
else()
add_custom_target(configured_files ALL)
endif()
# configure-time configuration to avoid missing file warnings
execute_process(
COMMAND "${CMAKE_COMMAND}" -P "${configure_script_path}")
It still has some ugly “code generation” but I currently don’t know a proper way to avoid that:
For example, I could create a configurator.cmake.in
with only
configure_file(
@configurable@
@configured@)
And create a new configurator-xxx.cmake
for every configurable file. Then run all of them as separate scripts. That would get rid of the “code-in-a-string”, but it would cost too much in performance
Edit 1
goddammit I didn’t know include()
could include standard scripts too. Now I can replace
simply with
include("${configure_script_path}")
much better.