1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00
Files
CMake/Modules/CPackIFWConfigureFile.cmake
Peter Kokot 7db6ff4863 CMake: Include CPackIFWConfigureFile normally
As minimum CMake version required to build CMake itself is set to 3.13,
including this module from the source directory isn't needed anymore,
as it was added in CMake 3.8. This also enables changing module code
as done in other modules - not dependent on CMake build system itself.
2025-08-23 11:48:36 +02:00

70 lines
2.2 KiB
CMake

# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
CPackIFWConfigureFile
---------------------
.. versionadded:: 3.8
This module defines :command:`configure_file` similar command to
configure file templates prepared in QtIFW/SDK/Creator style.
Load this module in a CMake project with:
.. code-block:: cmake
include(CPackIFWConfigureFile)
Commands
^^^^^^^^
The module defines the following commands:
.. command:: cpack_ifw_configure_file
Copy a file to another location and modify its contents.
.. code-block:: cmake
cpack_ifw_configure_file(<input> <output>)
Copies an ``<input>`` file to an ``<output>`` file and substitutes variable
values referenced as ``%{VAR}`` or ``%VAR%`` in the input file content.
Each variable reference will be replaced with the current value of the
variable, or the empty string if the variable is not defined.
#]=======================================================================]
if(NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED)
set(CPackIFWConfigureFile_CMake_INCLUDED 1)
macro(cpack_ifw_configure_file INPUT OUTPUT)
file(READ "${INPUT}" _tmp)
foreach(_tmp_regex "%{([^%}]+)}" "%([^%]+)%")
string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
while(_tmp_vars)
foreach(_tmp_var ${_tmp_vars})
string(REGEX REPLACE "${_tmp_regex}" "\\1"
_tmp_var_name "${_tmp_var}")
if(DEFINED ${_tmp_var_name})
set(_tmp_var_value "${${_tmp_var_name}}")
elseif(NOT "$ENV{${_tmp_var_name}}" STREQUAL "")
set(_tmp_var_value "$ENV{${_tmp_var_name}}")
else()
set(_tmp_var_value "")
endif()
string(REPLACE "${_tmp_var}" "${_tmp_var_value}" _tmp "${_tmp}")
endforeach()
string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
endwhile()
endforeach()
if(IS_ABSOLUTE "${OUTPUT}")
file(WRITE "${OUTPUT}" "${_tmp}")
else()
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}" "${_tmp}")
endif()
endmacro()
endif() # NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED