target_sources.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. target_sources
  2. --------------
  3. .. versionadded:: 3.1
  4. Add sources to a target.
  5. .. code-block:: cmake
  6. target_sources(<target>
  7. <INTERFACE|PUBLIC|PRIVATE> [items1...]
  8. [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
  9. Specifies sources to use when building a target and/or its dependents.
  10. The named ``<target>`` must have been created by a command such as
  11. :command:`add_executable` or :command:`add_library` or
  12. :command:`add_custom_target` and must not be an
  13. :ref:`ALIAS target <Alias Targets>`. The ``<items>`` may use
  14. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  15. .. versionadded:: 3.20
  16. ``<target>`` can be a custom target.
  17. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
  18. specify the :ref:`scope <Target Usage Requirements>` of the source file paths
  19. (``<items>``) that follow them. ``PRIVATE`` and ``PUBLIC`` items will
  20. populate the :prop_tgt:`SOURCES` property of ``<target>``, which are used when
  21. building the target itself. ``PUBLIC`` and ``INTERFACE`` items will populate the
  22. :prop_tgt:`INTERFACE_SOURCES` property of ``<target>``, which are used
  23. when building dependents. A target created by :command:`add_custom_target`
  24. can only have ``PRIVATE`` scope.
  25. Repeated calls for the same ``<target>`` append items in the order called.
  26. .. versionadded:: 3.3
  27. Allow exporting targets with :prop_tgt:`INTERFACE_SOURCES`.
  28. .. versionadded:: 3.11
  29. Allow setting ``INTERFACE`` items on
  30. :ref:`IMPORTED targets <Imported Targets>`.
  31. .. versionchanged:: 3.13
  32. Relative source file paths are interpreted as being relative to the current
  33. source directory (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`).
  34. See policy :policy:`CMP0076`.
  35. A path that begins with a generator expression is left unmodified.
  36. When a target's :prop_tgt:`SOURCE_DIR` property differs from
  37. :variable:`CMAKE_CURRENT_SOURCE_DIR`, use absolute paths in generator
  38. expressions to ensure the sources are correctly assigned to the target.
  39. .. code-block:: cmake
  40. # WRONG: starts with generator expression, but relative path used
  41. target_sources(MyTarget PRIVATE "$<$<CONFIG:Debug>:dbgsrc.cpp>")
  42. # CORRECT: absolute path used inside the generator expression
  43. target_sources(MyTarget PRIVATE "$<$<CONFIG:Debug>:${CMAKE_CURRENT_SOURCE_DIR}/dbgsrc.cpp>")
  44. See the :manual:`cmake-buildsystem(7)` manual for more on defining
  45. buildsystem properties.
  46. .. _`File Sets`:
  47. File Sets
  48. ^^^^^^^^^
  49. .. versionadded:: 3.23
  50. .. code-block:: cmake
  51. target_sources(<target>
  52. [<INTERFACE|PUBLIC|PRIVATE>
  53. [FILE_SET <set> [TYPE <type>] [BASE_DIRS <dirs>...] [FILES <files>...]]...
  54. ]...)
  55. Adds a file set to a target, or adds files to an existing file set. Targets
  56. have zero or more named file sets. Each file set has a name, a type, a scope of
  57. ``INTERFACE``, ``PUBLIC``, or ``PRIVATE``, one or more base directories, and
  58. files within those directories. The acceptable types include:
  59. ``HEADERS``
  60. Sources intended to be used via a language's ``#include`` mechanism.
  61. ``CXX_MODULES``
  62. .. note ::
  63. Experimental. Gated by ``CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API``
  64. Sources which contain C++ interface module or partition units (i.e., those
  65. using the ``export`` keyword). This file set type may not have an
  66. ``INTERFACE`` scope except on ``IMPORTED`` targets.
  67. The optional default file sets are named after their type. The target may not
  68. be a custom target or :prop_tgt:`FRAMEWORK` target.
  69. Files in a ``PRIVATE`` or ``PUBLIC`` file set are marked as source files for
  70. the purposes of IDE integration. Additionally, files in ``HEADERS`` file sets
  71. have their :prop_sf:`HEADER_FILE_ONLY` property set to ``TRUE``. Files in an
  72. ``INTERFACE`` or ``PUBLIC`` file set can be installed with the
  73. :command:`install(TARGETS)` command, and exported with the
  74. :command:`install(EXPORT)` and :command:`export` commands.
  75. Each ``target_sources(FILE_SET)`` entry starts with ``INTERFACE``, ``PUBLIC``, or
  76. ``PRIVATE`` and accepts the following arguments:
  77. ``FILE_SET <set>``
  78. The name of the file set to create or add to. It must contain only letters,
  79. numbers and underscores. Names starting with a capital letter are reserved
  80. for built-in file sets predefined by CMake. The only predefined set names
  81. are those matching the acceptable types. All other set names must not start
  82. with a capital letter or
  83. underscore.
  84. ``TYPE <type>``
  85. Every file set is associated with a particular type of file. Only types
  86. specified above may be used and it is an error to specify anything else. As
  87. a special case, if the name of the file set is one of the types, the type
  88. does not need to be specified and the ``TYPE <type>`` arguments can be
  89. omitted. For all other file set names, ``TYPE`` is required.
  90. ``BASE_DIRS <dirs>...``
  91. An optional list of base directories of the file set. Any relative path
  92. is treated as relative to the current source directory
  93. (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`). If no ``BASE_DIRS`` are
  94. specified when the file set is first created, the value of
  95. :variable:`CMAKE_CURRENT_SOURCE_DIR` is added. This argument supports
  96. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  97. No two base directories for a file set may be sub-directories of each other.
  98. This requirement must be met across all base directories added to a file set,
  99. not just those within a single call to ``target_sources()``.
  100. ``FILES <files>...``
  101. An optional list of files to add to the file set. Each file must be in
  102. one of the base directories, or a subdirectory of one of the base
  103. directories. This argument supports
  104. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  105. If relative paths are specified, they are considered relative to
  106. :variable:`CMAKE_CURRENT_SOURCE_DIR` at the time ``target_sources()`` is
  107. called. An exception to this is a path starting with ``$<``. Such paths
  108. are treated as relative to the target's source directory after evaluation
  109. of generator expressions.
  110. The following target properties are set by ``target_sources(FILE_SET)``,
  111. but they should not generally be manipulated directly:
  112. For file sets of type ``HEADERS``:
  113. * :prop_tgt:`HEADER_SETS`
  114. * :prop_tgt:`INTERFACE_HEADER_SETS`
  115. * :prop_tgt:`HEADER_SET`
  116. * :prop_tgt:`HEADER_SET_<NAME>`
  117. * :prop_tgt:`HEADER_DIRS`
  118. * :prop_tgt:`HEADER_DIRS_<NAME>`
  119. For file sets of type ``CXX_MODULES``:
  120. * :prop_tgt:`CXX_MODULE_SETS`
  121. * :prop_tgt:`INTERFACE_CXX_MODULE_SETS`
  122. * :prop_tgt:`CXX_MODULE_SET`
  123. * :prop_tgt:`CXX_MODULE_SET_<NAME>`
  124. * :prop_tgt:`CXX_MODULE_DIRS`
  125. * :prop_tgt:`CXX_MODULE_DIRS_<NAME>`
  126. Target properties related to include directories are also modified by
  127. ``target_sources(FILE_SET)`` as follows:
  128. :prop_tgt:`INCLUDE_DIRECTORIES`
  129. If the ``TYPE`` is ``HEADERS``, and the scope of the file set is ``PRIVATE``
  130. or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are wrapped in
  131. :genex:`$<BUILD_INTERFACE>` and appended to this property.
  132. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  133. If the ``TYPE`` is ``HEADERS``, and the scope of the file set is
  134. ``INTERFACE`` or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are
  135. wrapped in :genex:`$<BUILD_INTERFACE>` and appended to this property.
  136. See Also
  137. ^^^^^^^^
  138. * :command:`add_executable`
  139. * :command:`add_library`
  140. * :command:`target_compile_definitions`
  141. * :command:`target_compile_features`
  142. * :command:`target_compile_options`
  143. * :command:`target_include_directories`
  144. * :command:`target_link_libraries`
  145. * :command:`target_link_directories`
  146. * :command:`target_link_options`
  147. * :command:`target_precompile_headers`