configure_file.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. configure_file
  2. --------------
  3. .. only:: html
  4. .. contents::
  5. Copy a file to another location and modify its contents.
  6. .. code-block:: cmake
  7. configure_file(<input> <output>
  8. [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
  9. FILE_PERMISSIONS <permissions>...]
  10. [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
  11. [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
  12. Copies an ``<input>`` file to an ``<output>`` file while performing
  13. `transformations`_ of the input file content.
  14. If the input file is modified the build system will re-run CMake to
  15. re-configure the file and generate the build system again.
  16. The generated file is modified and its timestamp updated on subsequent
  17. cmake runs only if its content is changed.
  18. Options
  19. ^^^^^^^
  20. The options are:
  21. ``<input>``
  22. Path to the input file. A relative path is treated with respect to
  23. the value of :variable:`CMAKE_CURRENT_SOURCE_DIR`. The input path
  24. must be a file, not a directory.
  25. ``<output>``
  26. Path to the output file or directory. A relative path is treated
  27. with respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  28. If the path names an existing directory the output file is placed
  29. in that directory with the same file name as the input file.
  30. If the path contains non-existent directories, they are created.
  31. ``NO_SOURCE_PERMISSIONS``
  32. .. versionadded:: 3.19
  33. Do not transfer the permissions of the input file to the output file.
  34. The copied file permissions default to the standard 644 value
  35. (-rw-r--r--).
  36. ``USE_SOURCE_PERMISSIONS``
  37. .. versionadded:: 3.20
  38. Transfer the permissions of the input file to the output file.
  39. This is already the default behavior if none of the three permissions-related
  40. keywords are given (``NO_SOURCE_PERMISSIONS``, ``USE_SOURCE_PERMISSIONS``
  41. or ``FILE_PERMISSIONS``). The ``USE_SOURCE_PERMISSIONS`` keyword mostly
  42. serves as a way of making the intended behavior clearer at the call site.
  43. ``FILE_PERMISSIONS <permissions>...``
  44. .. versionadded:: 3.20
  45. Ignore the input file's permissions and use the specified ``<permissions>``
  46. for the output file instead.
  47. ``COPYONLY``
  48. Copy the file without replacing any variable references or other
  49. content. This option may not be used with ``NEWLINE_STYLE``.
  50. ``ESCAPE_QUOTES``
  51. Escape any substituted quotes with backslashes (C-style).
  52. ``@ONLY``
  53. Restrict variable replacement to references of the form ``@VAR@``.
  54. This is useful for configuring scripts that use ``${VAR}`` syntax.
  55. ``NEWLINE_STYLE <style>``
  56. Specify the newline style for the output file. Specify
  57. ``UNIX`` or ``LF`` for ``\n`` newlines, or specify
  58. ``DOS``, ``WIN32``, or ``CRLF`` for ``\r\n`` newlines.
  59. This option may not be used with ``COPYONLY``.
  60. Transformations
  61. ^^^^^^^^^^^^^^^
  62. :ref:`Variables <CMake Language Variables>` referenced in the input
  63. file content as ``@VAR@``, ``${VAR}``, ``$CACHE{VAR}``, and
  64. :ref:`environment variables <CMake Language Environment Variables>`
  65. referenced as ``$ENV{VAR}``, will each be replaced with the current value
  66. of the variable, or the empty string if the variable is not defined.
  67. Furthermore, input lines of the form
  68. .. code-block:: c
  69. #cmakedefine VAR ...
  70. will be replaced with either
  71. .. code-block:: c
  72. #define VAR ...
  73. or
  74. .. code-block:: c
  75. /* #undef VAR */
  76. depending on whether ``VAR`` is set in CMake to any value not considered
  77. a false constant by the :command:`if` command. The "..." content on the
  78. line after the variable name, if any, is processed as above.
  79. Unlike lines of the form ``#cmakedefine VAR ...``, in lines of the form
  80. ``#cmakedefine01 VAR``, ``VAR`` itself will expand to ``VAR 0`` or ``VAR 1``
  81. rather than being assigned the value ``...``. Therefore, input lines of the form
  82. .. code-block:: c
  83. #cmakedefine01 VAR
  84. will be replaced with either
  85. .. code-block:: c
  86. #define VAR 0
  87. or
  88. .. code-block:: c
  89. #define VAR 1
  90. Input lines of the form ``#cmakedefine01 VAR ...`` will expand
  91. as ``#cmakedefine01 VAR ... 0`` or ``#cmakedefine01 VAR ... 1``,
  92. which may lead to undefined behavior.
  93. .. versionadded:: 3.10
  94. The result lines (with the exception of the ``#undef`` comments) can be
  95. indented using spaces and/or tabs between the ``#`` character
  96. and the ``cmakedefine`` or ``cmakedefine01`` words. This whitespace
  97. indentation will be preserved in the output lines:
  98. .. code-block:: c
  99. # cmakedefine VAR
  100. # cmakedefine01 VAR
  101. will be replaced, if ``VAR`` is defined, with
  102. .. code-block:: c
  103. # define VAR
  104. # define VAR 1
  105. Example
  106. ^^^^^^^
  107. Consider a source tree containing a ``foo.h.in`` file:
  108. .. code-block:: c
  109. #cmakedefine FOO_ENABLE
  110. #cmakedefine FOO_STRING "@FOO_STRING@"
  111. An adjacent ``CMakeLists.txt`` may use ``configure_file`` to
  112. configure the header:
  113. .. code-block:: cmake
  114. option(FOO_ENABLE "Enable Foo" ON)
  115. if(FOO_ENABLE)
  116. set(FOO_STRING "foo")
  117. endif()
  118. configure_file(foo.h.in foo.h @ONLY)
  119. This creates a ``foo.h`` in the build directory corresponding to
  120. this source directory. If the ``FOO_ENABLE`` option is on, the
  121. configured file will contain:
  122. .. code-block:: c
  123. #define FOO_ENABLE
  124. #define FOO_STRING "foo"
  125. Otherwise it will contain:
  126. .. code-block:: c
  127. /* #undef FOO_ENABLE */
  128. /* #undef FOO_STRING */
  129. One may then use the :command:`target_include_directories` command to
  130. specify the output directory as an include directory:
  131. .. code-block:: cmake
  132. target_include_directories(<target> [SYSTEM] <INTERFACE|PUBLIC|PRIVATE> "${CMAKE_CURRENT_BINARY_DIR}")
  133. so that sources may include the header as ``#include <foo.h>``.
  134. See Also
  135. ^^^^^^^^
  136. * :command:`file(GENERATE)`