CPackIFWConfigureFile.cmake 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. # NOTE: This file used to himself packaging via CPack IFW generator and
  25. # should be compatible with minimal CMake version defined in
  26. # ../CMakeLists.txt file.
  27. if(NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED)
  28. set(CPackIFWConfigureFile_CMake_INCLUDED 1)
  29. macro(cpack_ifw_configure_file INPUT OUTPUT)
  30. file(READ "${INPUT}" _tmp)
  31. foreach(_tmp_regex "%{([^%}]+)}" "%([^%]+)%")
  32. string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
  33. while(_tmp_vars)
  34. foreach(_tmp_var ${_tmp_vars})
  35. string(REGEX REPLACE "${_tmp_regex}" "\\1"
  36. _tmp_var_name "${_tmp_var}")
  37. if(DEFINED ${_tmp_var_name})
  38. set(_tmp_var_value "${${_tmp_var_name}}")
  39. elseif(NOT "$ENV{${_tmp_var_name}}" STREQUAL "")
  40. set(_tmp_var_value "$ENV{${_tmp_var_name}}")
  41. else()
  42. set(_tmp_var_value "")
  43. endif()
  44. string(REPLACE "${_tmp_var}" "${_tmp_var_value}" _tmp "${_tmp}")
  45. endforeach()
  46. string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
  47. endwhile()
  48. endforeach()
  49. if(IS_ABSOLUTE "${OUTPUT}")
  50. file(WRITE "${OUTPUT}" "${_tmp}")
  51. else()
  52. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}" "${_tmp}")
  53. endif()
  54. endmacro()
  55. endif() # NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED