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