# ConfigureVersionedFile.cmake — build-time template substitution
#
# Replaces configure_file() for templates that contain version placeholders,
# so that the version is always fresh from `git describe` rather than stale
# from the last CMake configure.
#
# Expected -D inputs:
#   VERSION_VARS_FILE — path to imager_version_vars.cmake (generated by GenerateVersion.cmake)
#   INPUT             — path to the .in template
#   OUTPUT            — path to write the configured file
#   EXTRA_VARS_FILE   — (optional) path to a CMake file with additional set() calls
#                        for non-version variables the template needs

# Load the build-time version variables
include("${VERSION_VARS_FILE}")

# Load any extra variables the template needs (e.g. CMAKE_BINARY_DIR, MACOSX_BUNDLE_* etc.)
if(EXTRA_VARS_FILE)
    include("${EXTRA_VARS_FILE}")
endif()

# Substitute into a temporary file, then copy_if_different so downstream
# build steps (resource compiler, linker) don't rebuild when nothing changed.
set(_tmp "${OUTPUT}.tmp")
configure_file("${INPUT}" "${_tmp}" @ONLY)

execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_tmp}" "${OUTPUT}")
file(REMOVE "${_tmp}")
