configure_file.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. If the input file is modified the build system will re-run CMake to
  24. re-configure the file and generate the build system again.
  25. The arguments are:
  26. ``<input>``
  27. Path to the input file. A relative path is treated with respect to
  28. the value of :variable:`CMAKE_CURRENT_SOURCE_DIR`. The input path
  29. must be a file, not a directory.
  30. ``<output>``
  31. Path to the output file or directory. A relative path is treated
  32. with respect to the value of :variable:`CMAKE_CURRENT_BINARY_DIR`.
  33. If the path names an existing directory the output file is placed
  34. in that directory with the same file name as the input file.
  35. ``COPYONLY``
  36. Copy the file without replacing any variable references or other
  37. content. This option may not be used with ``NEWLINE_STYLE``.
  38. ``ESCAPE_QUOTES``
  39. Escape any substituted quotes with backslashes (C-style).
  40. ``@ONLY``
  41. Restrict variable replacement to references of the form ``@VAR@``.
  42. This is useful for configuring scripts that use ``${VAR}`` syntax.
  43. ``NEWLINE_STYLE <style>``
  44. Specify the newline style for the output file. Specify
  45. ``UNIX`` or ``LF`` for ``\n`` newlines, or specify
  46. ``DOS``, ``WIN32``, or ``CRLF`` for ``\r\n`` newlines.
  47. This option may not be used with ``COPYONLY``.
  48. Example
  49. ^^^^^^^
  50. Consider a source tree containing a ``foo.h.in`` file:
  51. .. code-block:: c
  52. #cmakedefine FOO_ENABLE
  53. #cmakedefine FOO_STRING "@FOO_STRING@"
  54. An adjacent ``CMakeLists.txt`` may use ``configure_file`` to
  55. configure the header:
  56. .. code-block:: cmake
  57. option(FOO_ENABLE "Enable Foo" ON)
  58. if(FOO_ENABLE)
  59. set(FOO_STRING "foo")
  60. endif()
  61. configure_file(foo.h.in foo.h @ONLY)
  62. This creates a ``foo.h`` in the build directory corresponding to
  63. this source directory. If the ``FOO_ENABLE`` option is on, the
  64. configured file will contain:
  65. .. code-block:: c
  66. #define FOO_ENABLE
  67. #define FOO_STRING "foo"
  68. Otherwise it will contain:
  69. .. code-block:: c
  70. /* #undef FOO_ENABLE */
  71. /* #undef FOO_STRING */
  72. One may then use the :command:`include_directories` command to
  73. specify the output directory as an include directory:
  74. .. code-block:: cmake
  75. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  76. so that sources may include the header as ``#include <foo.h>``.