configure_file.rst 5.1 KB

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