configure_file.rst 3.6 KB

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