configure_file.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. [FILE_PERMISSIONS <permissions>...]
  7. [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
  8. [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
  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. Input file lines of the form ``#cmakedefine01 VAR`` will be replaced with
  27. either ``#define VAR 1`` or ``#define VAR 0`` similarly.
  28. .. versionadded:: 3.10
  29. The result lines (with the exception of the ``#undef`` comments) can be
  30. indented using spaces and/or tabs between the ``#`` character
  31. and the ``cmakedefine`` or ``cmakedefine01`` words. This whitespace
  32. indentation will be preserved in the output lines:
  33. .. code-block:: c
  34. # cmakedefine VAR
  35. # cmakedefine01 VAR
  36. will be replaced, if ``VAR`` is defined, with
  37. .. code-block:: c
  38. # define VAR
  39. # define VAR 1
  40. If the input file is modified the build system will re-run CMake to
  41. re-configure the file and generate the build system again.
  42. The generated file is modified and its timestamp updated on subsequent
  43. cmake runs only if its content is changed.
  44. The arguments are:
  45. ``<input>``
  46. Path to the input file. A relative path is treated with respect to
  47. the value of :variable:`CMAKE_CURRENT_SOURCE_DIR`. The input path
  48. must be a file, not a directory.
  49. ``<output>``
  50. Path to the output file or directory. A relative path is treated
  51. with respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  52. If the path names an existing directory the output file is placed
  53. in that directory with the same file name as the input file.
  54. ``FILE_PERMISSIONS <permissions>...``
  55. Use user provided permissions for the output file.
  56. ``COPYONLY``
  57. Copy the file without replacing any variable references or other
  58. content. This option may not be used with ``NEWLINE_STYLE``.
  59. ``ESCAPE_QUOTES``
  60. Escape any substituted quotes with backslashes (C-style).
  61. ``@ONLY``
  62. Restrict variable replacement to references of the form ``@VAR@``.
  63. This is useful for configuring scripts that use ``${VAR}`` syntax.
  64. ``NO_SOURCE_PERMISSIONS``
  65. .. versionadded:: 3.19
  66. Does not transfer the file permissions of the original file to the copy.
  67. The copied file permissions default to the standard 644 value
  68. (-rw-r--r--).
  69. ``USE_SOURCE_PERMISSIONS``
  70. .. versionadded:: 3.20
  71. Transfer the file permissions of the original file to the output file.
  72. ``NEWLINE_STYLE <style>``
  73. Specify the newline style for the output file. Specify
  74. ``UNIX`` or ``LF`` for ``\n`` newlines, or specify
  75. ``DOS``, ``WIN32``, or ``CRLF`` for ``\r\n`` newlines.
  76. This option may not be used with ``COPYONLY``.
  77. Example
  78. ^^^^^^^
  79. Consider a source tree containing a ``foo.h.in`` file:
  80. .. code-block:: c
  81. #cmakedefine FOO_ENABLE
  82. #cmakedefine FOO_STRING "@FOO_STRING@"
  83. An adjacent ``CMakeLists.txt`` may use ``configure_file`` to
  84. configure the header:
  85. .. code-block:: cmake
  86. option(FOO_ENABLE "Enable Foo" ON)
  87. if(FOO_ENABLE)
  88. set(FOO_STRING "foo")
  89. endif()
  90. configure_file(foo.h.in foo.h @ONLY)
  91. This creates a ``foo.h`` in the build directory corresponding to
  92. this source directory. If the ``FOO_ENABLE`` option is on, the
  93. configured file will contain:
  94. .. code-block:: c
  95. #define FOO_ENABLE
  96. #define FOO_STRING "foo"
  97. Otherwise it will contain:
  98. .. code-block:: c
  99. /* #undef FOO_ENABLE */
  100. /* #undef FOO_STRING */
  101. One may then use the :command:`include_directories` command to
  102. specify the output directory as an include directory:
  103. .. code-block:: cmake
  104. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  105. so that sources may include the header as ``#include <foo.h>``.