CPackIFWConfigureFile.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 provides a command similar to :command:`configure_file` for
  8. configuring 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. This module provides the following command:
  15. .. command:: cpack_ifw_configure_file
  16. Copies a file template to output file and substitutes variable values
  17. referenced as ``%{VAR}`` or ``%VAR%`` from the input file template
  18. content:
  19. .. code-block:: cmake
  20. cpack_ifw_configure_file(<input> <output>)
  21. ``<input>``
  22. Input file template. If given as a relative path, it is interpreted as
  23. relative to the current source directory
  24. (:variable:`CMAKE_CURRENT_SOURCE_DIR`).
  25. ``<output>``
  26. Output file. If given as a relative path, it is interpreted as relative
  27. to the current binary directory (:variable:`CMAKE_CURRENT_BINARY_DIR`).
  28. Qt Installer Framework (QtIFW) uses ``@`` characters for embedding
  29. predefined variables (``TargetDir``, ``StartMenuDir``, etc.) in Qt
  30. installer scripts:
  31. .. code-block:: javascript
  32. :caption: ``example.qs``
  33. component.addOperation(
  34. "CreateShortcut",
  35. "@TargetDir@/example.com.html",
  36. "@StartMenuDir@/Example Web Site.lnk"
  37. );
  38. The purpose of this command is to preserve the QtIFW predefined variables
  39. containing the ``@`` characters (``@VAR@``), and instead use the ``%``
  40. characters for template placeholders (``%VAR%``, ``%{VAR}``) in
  41. Qt/IFW/SDK/Creator templates. The :command:`configure_file` command
  42. would otherwise replace all variable references containing the ``@``
  43. characters.
  44. Each variable reference will be replaced with the current value of the
  45. variable, or the empty string if the variable is not defined.
  46. Examples
  47. ^^^^^^^^
  48. In the following example this module is used to create an IFW component
  49. script from a given template file ``qt.tools.foo.qs.in``, where
  50. ``%FOO_DOC_DIR%`` variable reference will be replaced by the values of
  51. the ``FOO_DOC_DIR`` CMake variable.
  52. .. code-block:: cmake
  53. :caption: ``CMakeLists.txt``
  54. cmake_minimum_required(VERSION 3.8)
  55. project(Foo)
  56. # ...
  57. include(CPackIFWConfigureFile)
  58. set(FOO_DOC_DIR "doc/foo")
  59. cpack_ifw_configure_file(qt.tools.foo.qs.in qt.tools.foo.qs)
  60. .. code-block:: javascript
  61. :caption: ``qt.tools.foo.qs.in``
  62. function Component()
  63. {
  64. }
  65. Component.prototype.createOperations = function()
  66. {
  67. if (installer.value("os") === "win") {
  68. component.addOperation(
  69. "CreateShortcut",
  70. "@TargetDir@/%FOO_DOC_DIR%/example.com.html",
  71. "@StartMenuDir@/Example Web Site.lnk"
  72. );
  73. }
  74. component.createOperations();
  75. }
  76. // ...
  77. See Also
  78. ^^^^^^^^
  79. * The :cpack_gen:`CPack IFW Generator`.
  80. * The :module:`CPackIFW` module.
  81. #]=======================================================================]
  82. if(NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED)
  83. set(CPackIFWConfigureFile_CMake_INCLUDED 1)
  84. macro(cpack_ifw_configure_file INPUT OUTPUT)
  85. file(READ "${INPUT}" _tmp)
  86. foreach(_tmp_regex "%{([^%}]+)}" "%([^%]+)%")
  87. string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
  88. while(_tmp_vars)
  89. foreach(_tmp_var ${_tmp_vars})
  90. string(REGEX REPLACE "${_tmp_regex}" "\\1"
  91. _tmp_var_name "${_tmp_var}")
  92. if(DEFINED ${_tmp_var_name})
  93. set(_tmp_var_value "${${_tmp_var_name}}")
  94. elseif(NOT "$ENV{${_tmp_var_name}}" STREQUAL "")
  95. set(_tmp_var_value "$ENV{${_tmp_var_name}}")
  96. else()
  97. set(_tmp_var_value "")
  98. endif()
  99. string(REPLACE "${_tmp_var}" "${_tmp_var_value}" _tmp "${_tmp}")
  100. endforeach()
  101. string(REGEX MATCHALL "${_tmp_regex}" _tmp_vars "${_tmp}")
  102. endwhile()
  103. endforeach()
  104. if(IS_ABSOLUTE "${OUTPUT}")
  105. file(WRITE "${OUTPUT}" "${_tmp}")
  106. else()
  107. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}" "${_tmp}")
  108. endif()
  109. endmacro()
  110. endif() # NOT DEFINED CPackIFWConfigureFile_CMake_INCLUDED