CPackIFWConfigureFile.cmake 2.2 KB

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