configure_file.rst 4.2 KB

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