configure_file.rst 4.0 KB

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