target_sources.rst 6.5 KB

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