target_sources.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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> FILE_SET set1 [TYPE type1] [BASE_DIRS dirs1...] [FILES files1...]
  52. [<INTERFACE|PUBLIC|PRIVATE> FILE_SET set2 [TYPE type2] [BASE_DIRS dirs2...] [FILES files2...])
  53. Adds a file set to a target, or adds files to an existing file set. Targets
  54. have zero or more named file sets. Each file set has a name, a type, a scope of
  55. ``INTERFACE``, ``PUBLIC``, or ``PRIVATE``, one or more base directories, and
  56. files within those directories. The only acceptable type is ``HEADERS``. The
  57. optional default file sets are named after their type. The target may not be a
  58. custom target.
  59. Files in a ``PRIVATE`` or ``PUBLIC`` file set are marked as source files for
  60. the purposes of IDE integration. Additionally, files in ``HEADERS`` file sets
  61. have their :prop_sf:`HEADER_FILE_ONLY` property set to ``TRUE``. Files in an
  62. ``INTERFACE`` or ``PUBLIC`` file set can be installed with the
  63. :command:`install(TARGETS)` command, and exported with the
  64. :command:`install(EXPORT)` and :command:`export` commands.
  65. Each ``target_sources(FILE_SET)`` entry starts with ``INTERFACE``, ``PUBLIC``, or
  66. ``PRIVATE`` and accepts the following arguments:
  67. ``FILE_SET <set>``
  68. A string representing the name of the file set to create or add to. This must
  69. not start with a capital letter, unless its name is ``HEADERS``.
  70. ``TYPE <type>``
  71. A string representing the type of the file set. The only acceptable value is
  72. ``HEADERS``. This may be omitted if the name of the file set is ``HEADERS``.
  73. ``BASE_DIRS <dirs>``
  74. An optional list of strings representing the base directories of the file
  75. set. This argument supports
  76. :manual:`generator expressions <cmake-generator-expressions(7)>`. No two
  77. ``BASE_DIRS`` may be sub-directories of each other. If no ``BASE_DIRS`` are
  78. specified when the file set is first created, the value of
  79. :variable:`CMAKE_CURRENT_SOURCE_DIR` is added.
  80. ``FILES <files>``
  81. An optional list of strings representing files in the file set. Each file
  82. must be in one of the ``BASE_DIRS``. This argument supports
  83. :manual:`generator expressions <cmake-generator-expressions(7)>`. If relative
  84. paths are specified, they are considered relative to
  85. :variable:`CMAKE_CURRENT_SOURCE_DIR` at the time ``target_sources()`` is
  86. called, unless they start with ``$<``, in which case they are computed
  87. relative to the target's source directory after genex evaluation.
  88. The following target properties are set by ``target_sources(FILE_SET)``:
  89. :prop_tgt:`HEADER_SETS`
  90. List of ``PRIVATE`` and ``PUBLIC`` header sets associated with a target.
  91. Headers listed in these header sets are treated as source files for the
  92. purposes of IDE integration, and have their :prop_sf:`HEADER_FILE_ONLY`
  93. property set to ``TRUE``.
  94. :prop_tgt:`INTERFACE_HEADER_SETS`
  95. List of ``INTERFACE`` and ``PUBLIC`` header sets associated with a target.
  96. Headers listed in these header sets can be installed with
  97. :command:`install(TARGETS)` and exported with :command:`install(EXPORT)` and
  98. :command:`export`.
  99. :prop_tgt:`HEADER_SET`
  100. Headers in the default header set associated with a target. This property
  101. supports :manual:`generator expressions <cmake-generator-expressions(7)>`.
  102. :prop_tgt:`HEADER_SET_<NAME>`
  103. Headers in the named header set ``<NAME>`` associated with a target. This
  104. property supports
  105. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  106. :prop_tgt:`HEADER_DIRS`
  107. Base directories of the default header set associated with a target. This
  108. property supports
  109. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  110. :prop_tgt:`HEADER_DIRS_<NAME>`
  111. Base directories of the header set ``<NAME>`` associated with a target. This
  112. property supports
  113. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  114. :prop_tgt:`INCLUDE_DIRECTORIES`
  115. If the ``TYPE`` is ``HEADERS``, and the scope of the file set is ``PRIVATE``
  116. or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are wrapped in
  117. :genex:`$<BUILD_INTERFACE>` and appended to this property.
  118. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  119. If the ``TYPE`` is ``HEADERS``, and the scope of the file set is
  120. ``INTERFACE`` or ``PUBLIC``, all of the ``BASE_DIRS`` of the file set are
  121. wrapped in :genex:`$<BUILD_INTERFACE>` and appended to this property.