72119f6a6b7648626974c30950c98dba9a4439b2
[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 scope of the source file paths (``<items>``) that follow
26 them.  ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`SOURCES`
27 property of ``<target>``, which are used when building the target itself.
28 ``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 only acceptable type is ``HEADERS``. The
79 optional default file sets are named after their type. The target may not be a
80 custom target or :prop_tgt:`FRAMEWORK` target.
81
82 Files in a ``PRIVATE`` or ``PUBLIC`` file set are marked as source files for
83 the purposes of IDE integration. Additionally, files in ``HEADERS`` file sets
84 have their :prop_sf:`HEADER_FILE_ONLY` property set to ``TRUE``. Files in an
85 ``INTERFACE`` or ``PUBLIC`` file set can be installed with the
86 :command:`install(TARGETS)` command, and exported with the
87 :command:`install(EXPORT)` and :command:`export` commands.
88
89 Each ``target_sources(FILE_SET)`` entry starts with ``INTERFACE``, ``PUBLIC``, or
90 ``PRIVATE`` and accepts the following arguments:
91
92 ``FILE_SET <set>``
93
94   The name of the file set to create or add to. It must contain only letters,
95   numbers and underscores. Names starting with a capital letter are reserved
96   for built-in file sets predefined by CMake. The only predefined set name is
97   ``HEADERS``. All other set names must not start with a capital letter or
98   underscore.
99
100 ``TYPE <type>``
101
102   Every file set is associated with a particular type of file. ``HEADERS``
103   is currently the only defined type and it is an error to specify anything
104   else. As a special case, if the name of the file set is ``HEADERS``, the
105   type does not need to be specified and the ``TYPE <type>`` arguments can be
106   omitted. For all other file set names, ``TYPE`` is required.
107
108 ``BASE_DIRS <dirs>...``
109
110   An optional list of base directories of the file set. Any relative path
111   is treated as relative to the current source directory
112   (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`). If no ``BASE_DIRS`` are
113   specified when the file set is first created, the value of
114   :variable:`CMAKE_CURRENT_SOURCE_DIR` is added. This argument supports
115   :manual:`generator expressions <cmake-generator-expressions(7)>`.
116
117   No two base directories for a file set may be sub-directories of each other.
118   This requirement must be met across all base directories added to a file set,
119   not just those within a single call to ``target_sources()``.
120
121 ``FILES <files>...``
122
123   An optional list of files to add to the file set. Each file must be in
124   one of the base directories, or a subdirectory of one of the base
125   directories. This argument supports
126   :manual:`generator expressions <cmake-generator-expressions(7)>`.
127
128   If relative paths are specified, they are considered relative to
129   :variable:`CMAKE_CURRENT_SOURCE_DIR` at the time ``target_sources()`` is
130   called. An exception to this is a path starting with ``$<``. Such paths
131   are treated as relative to the target's source directory after evaluation
132   of generator expressions.
133
134 The following target properties are set by ``target_sources(FILE_SET)``,
135 but they should not generally be manipulated directly:
136
137 * :prop_tgt:`HEADER_SETS`
138 * :prop_tgt:`INTERFACE_HEADER_SETS`
139 * :prop_tgt:`HEADER_SET`
140 * :prop_tgt:`HEADER_SET_<NAME>`
141 * :prop_tgt:`HEADER_DIRS`
142 * :prop_tgt:`HEADER_DIRS_<NAME>`
143
144 Target properties related to include directories are also modified by
145 ``target_sources(FILE_SET)`` as follows:
146
147 :prop_tgt:`INCLUDE_DIRECTORIES`
148
149   If the ``TYPE`` is ``HEADERS``, and the scope of the file set is ``PRIVATE``
150   or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are wrapped in
151   :genex:`$<BUILD_INTERFACE>` and appended to this property.
152
153 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
154
155   If the ``TYPE`` is ``HEADERS``, and the scope of the file set is
156   ``INTERFACE`` or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are
157   wrapped in :genex:`$<BUILD_INTERFACE>` and appended to this property.