target_sources.rst 6.5 KB

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