CPackIFWConfigureFile.cmake 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CPackIFWConfigureFile
  5. ---------------------
  6. .. versionadded:: 3.8
  7. This module defines :command:`configure_file` similar command to
  8. configure file templates prepared in QtIFW/SDK/Creator style.
  9. Load this module in a CMake project with:
  10. .. code-block:: cmake
  11. include(CPackIFWConfigureFile)
  12. Commands
  13. ^^^^^^^^
  14. The module defines the following commands:
  15. .. command:: cpack_ifw_configure_file
  16. Copy a file to another location and modify its contents.
  17. .. code-block:: cmake
  18. cpack_ifw_configure_file(<input> <output>)
  19. Copies an ``<input>`` file to an ``<output>`` file and substitutes variable
  20. values referenced as ``%{VAR}`` or ``%VAR%`` in the input file content.
  21. Each variable reference will be replaced with the current value of the
  22. variable, or the empty string if the variable is not defined.
  23. #]=======================================================================]
  24. if(NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED)
  25. set(CPackIFWConfigureFile_CMake_INCLUDED 1)
  26. macro(cpack_ifw_configure_file INPUT OUTPUT)
  27. file(READ "${INPUT}" _tmp)
  28. foreach(_tmp_regex "%{([^%}]+)}" "%([^%]+)%")
  29. string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
  30. while(_tmp_vars)
  31. foreach(_tmp_var ${_tmp_vars})
  32. string(REGEX REPLACE "${_tmp_regex}" "\\1"
  33. _tmp_var_name "${_tmp_var}")
  34. if(DEFINED ${_tmp_var_name})
  35. set(_tmp_var_value "${${_tmp_var_name}}")
  36. elseif(NOT "$ENV{${_tmp_var_name}}" STREQUAL "")
  37. set(_tmp_var_value "$ENV{${_tmp_var_name}}")
  38. else()
  39. set(_tmp_var_value "")
  40. endif()
  41. string(REPLACE "${_tmp_var}" "${_tmp_var_value}" _tmp "${_tmp}")
  42. endforeach()
  43. string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
  44. endwhile()
  45. endforeach()
  46. if(IS_ABSOLUTE "${OUTPUT}")
  47. file(WRITE "${OUTPUT}" "${_tmp}")
  48. else()
  49. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}" "${_tmp}")
  50. endif()
  51. endmacro()
  52. endif() # NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED