configure_file.rst 3.7 KB

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