target_sources.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 scope of the source file paths (``<items>``) that follow
  19. them. ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`SOURCES`
  20. property of ``<target>``, which are used when building the target itself.
  21. ``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 "$<$<CONFIG:Debug>:dbgsrc.cpp>")
  42. # CORRECT: absolute path used inside the generator expression
  43. target_sources(MyTarget "$<$<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. ^^^^^^^^^
  48. .. versionadded:: 3.23
  49. .. code-block:: cmake
  50. target_sources(<target>
  51. [<INTERFACE|PUBLIC|PRIVATE>
  52. [FILE_SET <set> [TYPE <type>] [BASE_DIRS <dirs>...] [FILES <files>...]]...
  53. ]...)
  54. Adds a file set to a target, or adds files to an existing file set. Targets
  55. have zero or more named file sets. Each file set has a name, a type, a scope of
  56. ``INTERFACE``, ``PUBLIC``, or ``PRIVATE``, one or more base directories, and
  57. files within those directories. The only acceptable type is ``HEADERS``. The
  58. optional default file sets are named after their type. The target may not be a
  59. custom target or :prop_tgt:`FRAMEWORK` target.
  60. Files in a ``PRIVATE`` or ``PUBLIC`` file set are marked as source files for
  61. the purposes of IDE integration. Additionally, files in ``HEADERS`` file sets
  62. have their :prop_sf:`HEADER_FILE_ONLY` property set to ``TRUE``. Files in an
  63. ``INTERFACE`` or ``PUBLIC`` file set can be installed with the
  64. :command:`install(TARGETS)` command, and exported with the
  65. :command:`install(EXPORT)` and :command:`export` commands.
  66. Each ``target_sources(FILE_SET)`` entry starts with ``INTERFACE``, ``PUBLIC``, or
  67. ``PRIVATE`` and accepts the following arguments:
  68. ``FILE_SET <set>``
  69. The name of the file set to create or add to. It must contain only letters,
  70. numbers and underscores. Names starting with a capital letter are reserved
  71. for built-in file sets predefined by CMake. The only predefined set name is
  72. ``HEADERS``. All other set names must not start with a capital letter or
  73. underscore.
  74. ``TYPE <type>``
  75. Every file set is associated with a particular type of file. ``HEADERS``
  76. is currently the only defined type and it is an error to specify anything
  77. else. As a special case, if the name of the file set is ``HEADERS``, the
  78. type does not need to be specified and the ``TYPE <type>`` arguments can be
  79. omitted. For all other file set names, ``TYPE`` is required.
  80. ``BASE_DIRS <dirs>...``
  81. An optional list of base directories of the file set. Any relative path
  82. is treated as relative to the current source directory
  83. (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`). If no ``BASE_DIRS`` are
  84. specified when the file set is first created, the value of
  85. :variable:`CMAKE_CURRENT_SOURCE_DIR` is added. This argument supports
  86. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  87. No two base directories for a file set may be sub-directories of each other.
  88. This requirement must be met across all base directories added to a file set,
  89. not just those within a single call to ``target_sources()``.
  90. ``FILES <files>...``
  91. An optional list of files to add to the file set. Each file must be in
  92. one of the base directories, or a subdirectory of one of the base
  93. directories. This argument supports
  94. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  95. If relative paths are specified, they are considered relative to
  96. :variable:`CMAKE_CURRENT_SOURCE_DIR` at the time ``target_sources()`` is
  97. called. An exception to this is a path starting with ``$<``. Such paths
  98. are treated as relative to the target's source directory after evaluation
  99. of generator expressions.
  100. The following target properties are set by ``target_sources(FILE_SET)``,
  101. but they should not generally be manipulated directly:
  102. * :prop_tgt:`HEADER_SETS`
  103. * :prop_tgt:`INTERFACE_HEADER_SETS`
  104. * :prop_tgt:`HEADER_SET`
  105. * :prop_tgt:`HEADER_SET_<NAME>`
  106. * :prop_tgt:`HEADER_DIRS`
  107. * :prop_tgt:`HEADER_DIRS_<NAME>`
  108. Target properties related to include directories are also modified by
  109. ``target_sources(FILE_SET)`` as follows:
  110. :prop_tgt:`INCLUDE_DIRECTORIES`
  111. If the ``TYPE`` is ``HEADERS``, and the scope of the file set is ``PRIVATE``
  112. or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are wrapped in
  113. :genex:`$<BUILD_INTERFACE>` and appended to this property.
  114. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  115. If the ``TYPE`` is ``HEADERS``, and the scope of the file set is
  116. ``INTERFACE`` or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are
  117. wrapped in :genex:`$<BUILD_INTERFACE>` and appended to this property.