CPackIFWConfigureFile.cmake 2.1 KB

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