cmake-buildsystem.7.rst 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. .. cmake-manual-description: CMake Buildsystem Reference
  2. cmake-buildsystem(7)
  3. ********************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. A CMake-based buildsystem is organized as a set of high-level logical
  9. targets. Each target corresponds to an executable or library, or
  10. is a custom target containing custom commands. Dependencies between the
  11. targets are expressed in the buildsystem to determine the build order
  12. and the rules for regeneration in response to change.
  13. Binary Targets
  14. ==============
  15. Executables and libraries are defined using the :command:`add_executable`
  16. and :command:`add_library` commands. The resulting binary files have
  17. appropriate :prop_tgt:`PREFIX`, :prop_tgt:`SUFFIX` and extensions for the platform targeted.
  18. Dependencies between binary targets are expressed using the
  19. :command:`target_link_libraries` command:
  20. .. code-block:: cmake
  21. add_library(archive archive.cpp zip.cpp lzma.cpp)
  22. add_executable(zipapp zipapp.cpp)
  23. target_link_libraries(zipapp archive)
  24. ``archive`` is defined as a ``STATIC`` library -- an archive containing objects
  25. compiled from ``archive.cpp``, ``zip.cpp``, and ``lzma.cpp``. ``zipapp``
  26. is defined as an executable formed by compiling and linking ``zipapp.cpp``.
  27. When linking the ``zipapp`` executable, the ``archive`` static library is
  28. linked in.
  29. Binary Executables
  30. ------------------
  31. The :command:`add_executable` command defines an executable target:
  32. .. code-block:: cmake
  33. add_executable(mytool mytool.cpp)
  34. Commands such as :command:`add_custom_command`, which generates rules to be
  35. run at build time can transparently use an :prop_tgt:`EXECUTABLE <TYPE>`
  36. target as a ``COMMAND`` executable. The buildsystem rules will ensure that
  37. the executable is built before attempting to run the command.
  38. Binary Library Types
  39. --------------------
  40. .. _`Normal Libraries`:
  41. Normal Libraries
  42. ^^^^^^^^^^^^^^^^
  43. By default, the :command:`add_library` command defines a ``STATIC`` library,
  44. unless a type is specified. A type may be specified when using the command:
  45. .. code-block:: cmake
  46. add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
  47. .. code-block:: cmake
  48. add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)
  49. The :variable:`BUILD_SHARED_LIBS` variable may be enabled to change the
  50. behavior of :command:`add_library` to build shared libraries by default.
  51. In the context of the buildsystem definition as a whole, it is largely
  52. irrelevant whether particular libraries are ``SHARED`` or ``STATIC`` --
  53. the commands, dependency specifications and other APIs work similarly
  54. regardless of the library type. The ``MODULE`` library type is
  55. dissimilar in that it is generally not linked to -- it is not used in
  56. the right-hand-side of the :command:`target_link_libraries` command.
  57. It is a type which is loaded as a plugin using runtime techniques.
  58. If the library does not export any unmanaged symbols (e.g. Windows
  59. resource DLL, C++/CLI DLL), it is required that the library not be a
  60. ``SHARED`` library because CMake expects ``SHARED`` libraries to export
  61. at least one symbol.
  62. .. code-block:: cmake
  63. add_library(archive MODULE 7z.cpp)
  64. .. _`Apple Frameworks`:
  65. Apple Frameworks
  66. """"""""""""""""
  67. A ``SHARED`` library may be marked with the :prop_tgt:`FRAMEWORK`
  68. target property to create an macOS or iOS Framework Bundle.
  69. A library with the ``FRAMEWORK`` target property should also set the
  70. :prop_tgt:`FRAMEWORK_VERSION` target property. This property is typically
  71. set to the value of "A" by macOS conventions.
  72. The ``MACOSX_FRAMEWORK_IDENTIFIER`` sets ``CFBundleIdentifier`` key
  73. and it uniquely identifies the bundle.
  74. .. code-block:: cmake
  75. add_library(MyFramework SHARED MyFramework.cpp)
  76. set_target_properties(MyFramework PROPERTIES
  77. FRAMEWORK TRUE
  78. FRAMEWORK_VERSION A # Version "A" is macOS convention
  79. MACOSX_FRAMEWORK_IDENTIFIER org.cmake.MyFramework
  80. )
  81. .. _`Object Libraries`:
  82. Object Libraries
  83. ^^^^^^^^^^^^^^^^
  84. The ``OBJECT`` library type defines a non-archival collection of object files
  85. resulting from compiling the given source files. The object files collection
  86. may be used as source inputs to other targets by using the syntax
  87. ``$<TARGET_OBJECTS:name>``. This is a
  88. :manual:`generator expression <cmake-generator-expressions(7)>` that can be
  89. used to supply the ``OBJECT`` library content to other targets:
  90. .. code-block:: cmake
  91. add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)
  92. add_library(archiveExtras STATIC $<TARGET_OBJECTS:archive> extras.cpp)
  93. add_executable(test_exe $<TARGET_OBJECTS:archive> test.cpp)
  94. The link (or archiving) step of those other targets will use the object
  95. files collection in addition to those from their own sources.
  96. Alternatively, object libraries may be linked into other targets:
  97. .. code-block:: cmake
  98. add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)
  99. add_library(archiveExtras STATIC extras.cpp)
  100. target_link_libraries(archiveExtras PUBLIC archive)
  101. add_executable(test_exe test.cpp)
  102. target_link_libraries(test_exe archive)
  103. The link (or archiving) step of those other targets will use the object
  104. files from ``OBJECT`` libraries that are *directly* linked. Additionally,
  105. usage requirements of the ``OBJECT`` libraries will be honored when compiling
  106. sources in those other targets. Furthermore, those usage requirements
  107. will propagate transitively to dependents of those other targets.
  108. Object libraries may not be used as the ``TARGET`` in a use of the
  109. :command:`add_custom_command(TARGET)` command signature. However,
  110. the list of objects can be used by :command:`add_custom_command(OUTPUT)`
  111. or :command:`file(GENERATE)` by using ``$<TARGET_OBJECTS:objlib>``.
  112. Build Specification and Usage Requirements
  113. ==========================================
  114. The :command:`target_include_directories`, :command:`target_compile_definitions`
  115. and :command:`target_compile_options` commands specify the build specifications
  116. and the usage requirements of binary targets. The commands populate the
  117. :prop_tgt:`INCLUDE_DIRECTORIES`, :prop_tgt:`COMPILE_DEFINITIONS` and
  118. :prop_tgt:`COMPILE_OPTIONS` target properties respectively, and/or the
  119. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`, :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`
  120. and :prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties.
  121. Each of the commands has a ``PRIVATE``, ``PUBLIC`` and ``INTERFACE`` mode. The
  122. ``PRIVATE`` mode populates only the non-``INTERFACE_`` variant of the target
  123. property and the ``INTERFACE`` mode populates only the ``INTERFACE_`` variants.
  124. The ``PUBLIC`` mode populates both variants of the respective target property.
  125. Each command may be invoked with multiple uses of each keyword:
  126. .. code-block:: cmake
  127. target_compile_definitions(archive
  128. PRIVATE BUILDING_WITH_LZMA
  129. INTERFACE USING_ARCHIVE_LIB
  130. )
  131. Note that usage requirements are not designed as a way to make downstreams
  132. use particular :prop_tgt:`COMPILE_OPTIONS` or
  133. :prop_tgt:`COMPILE_DEFINITIONS` etc for convenience only. The contents of
  134. the properties must be **requirements**, not merely recommendations or
  135. convenience.
  136. See the :ref:`Creating Relocatable Packages` section of the
  137. :manual:`cmake-packages(7)` manual for discussion of additional care
  138. that must be taken when specifying usage requirements while creating
  139. packages for redistribution.
  140. Target Properties
  141. -----------------
  142. The contents of the :prop_tgt:`INCLUDE_DIRECTORIES`,
  143. :prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS` target
  144. properties are used appropriately when compiling the source files of a
  145. binary target.
  146. Entries in the :prop_tgt:`INCLUDE_DIRECTORIES` are added to the compile line
  147. with ``-I`` or ``-isystem`` prefixes and in the order of appearance in the
  148. property value.
  149. Entries in the :prop_tgt:`COMPILE_DEFINITIONS` are prefixed with ``-D`` or
  150. ``/D`` and added to the compile line in an unspecified order. The
  151. :prop_tgt:`DEFINE_SYMBOL` target property is also added as a compile
  152. definition as a special convenience case for ``SHARED`` and ``MODULE``
  153. library targets.
  154. Entries in the :prop_tgt:`COMPILE_OPTIONS` are escaped for the shell and added
  155. in the order of appearance in the property value. Several compile options have
  156. special separate handling, such as :prop_tgt:`POSITION_INDEPENDENT_CODE`.
  157. The contents of the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
  158. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` and
  159. :prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties are
  160. *Usage Requirements* -- they specify content which consumers
  161. must use to correctly compile and link with the target they appear on.
  162. For any binary target, the contents of each ``INTERFACE_`` property on
  163. each target specified in a :command:`target_link_libraries` command is
  164. consumed:
  165. .. code-block:: cmake
  166. set(srcs archive.cpp zip.cpp)
  167. if (LZMA_FOUND)
  168. list(APPEND srcs lzma.cpp)
  169. endif()
  170. add_library(archive SHARED ${srcs})
  171. if (LZMA_FOUND)
  172. # The archive library sources are compiled with -DBUILDING_WITH_LZMA
  173. target_compile_definitions(archive PRIVATE BUILDING_WITH_LZMA)
  174. endif()
  175. target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
  176. add_executable(consumer)
  177. # Link consumer to archive and consume its usage requirements. The consumer
  178. # executable sources are compiled with -DUSING_ARCHIVE_LIB.
  179. target_link_libraries(consumer archive)
  180. Because it is common to require that the source directory and corresponding
  181. build directory are added to the :prop_tgt:`INCLUDE_DIRECTORIES`, the
  182. :variable:`CMAKE_INCLUDE_CURRENT_DIR` variable can be enabled to conveniently
  183. add the corresponding directories to the :prop_tgt:`INCLUDE_DIRECTORIES` of
  184. all targets. The variable :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE`
  185. can be enabled to add the corresponding directories to the
  186. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of all targets. This makes use of
  187. targets in multiple different directories convenient through use of the
  188. :command:`target_link_libraries` command.
  189. .. _`Target Usage Requirements`:
  190. Transitive Usage Requirements
  191. -----------------------------
  192. The usage requirements of a target can transitively propagate to dependents.
  193. The :command:`target_link_libraries` command has ``PRIVATE``,
  194. ``INTERFACE`` and ``PUBLIC`` keywords to control the propagation.
  195. .. code-block:: cmake
  196. add_library(archive archive.cpp)
  197. target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
  198. add_library(serialization serialization.cpp)
  199. target_compile_definitions(serialization INTERFACE USING_SERIALIZATION_LIB)
  200. add_library(archiveExtras extras.cpp)
  201. target_link_libraries(archiveExtras PUBLIC archive)
  202. target_link_libraries(archiveExtras PRIVATE serialization)
  203. # archiveExtras is compiled with -DUSING_ARCHIVE_LIB
  204. # and -DUSING_SERIALIZATION_LIB
  205. add_executable(consumer consumer.cpp)
  206. # consumer is compiled with -DUSING_ARCHIVE_LIB
  207. target_link_libraries(consumer archiveExtras)
  208. Because ``archive`` is a ``PUBLIC`` dependency of ``archiveExtras``, the
  209. usage requirements of it are propagated to ``consumer`` too. Because
  210. ``serialization`` is a ``PRIVATE`` dependency of ``archiveExtras``, the usage
  211. requirements of it are not propagated to ``consumer``.
  212. Generally, a dependency should be specified in a use of
  213. :command:`target_link_libraries` with the ``PRIVATE`` keyword if it is used by
  214. only the implementation of a library, and not in the header files. If a
  215. dependency is additionally used in the header files of a library (e.g. for
  216. class inheritance), then it should be specified as a ``PUBLIC`` dependency.
  217. A dependency which is not used by the implementation of a library, but only by
  218. its headers should be specified as an ``INTERFACE`` dependency. The
  219. :command:`target_link_libraries` command may be invoked with multiple uses of
  220. each keyword:
  221. .. code-block:: cmake
  222. target_link_libraries(archiveExtras
  223. PUBLIC archive
  224. PRIVATE serialization
  225. )
  226. Usage requirements are propagated by reading the ``INTERFACE_`` variants
  227. of target properties from dependencies and appending the values to the
  228. non-``INTERFACE_`` variants of the operand. For example, the
  229. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of dependencies is read and
  230. appended to the :prop_tgt:`INCLUDE_DIRECTORIES` of the operand. In cases
  231. where order is relevant and maintained, and the order resulting from the
  232. :command:`target_link_libraries` calls does not allow correct compilation,
  233. use of an appropriate command to set the property directly may update the
  234. order.
  235. For example, if the linked libraries for a target must be specified
  236. in the order ``lib1`` ``lib2`` ``lib3`` , but the include directories must
  237. be specified in the order ``lib3`` ``lib1`` ``lib2``:
  238. .. code-block:: cmake
  239. target_link_libraries(myExe lib1 lib2 lib3)
  240. target_include_directories(myExe
  241. PRIVATE $<TARGET_PROPERTY:lib3,INTERFACE_INCLUDE_DIRECTORIES>)
  242. Note that care must be taken when specifying usage requirements for targets
  243. which will be exported for installation using the :command:`install(EXPORT)`
  244. command. See :ref:`Creating Packages` for more.
  245. .. _`Compatible Interface Properties`:
  246. Compatible Interface Properties
  247. -------------------------------
  248. Some target properties are required to be compatible between a target and
  249. the interface of each dependency. For example, the
  250. :prop_tgt:`POSITION_INDEPENDENT_CODE` target property may specify a
  251. boolean value of whether a target should be compiled as
  252. position-independent-code, which has platform-specific consequences.
  253. A target may also specify the usage requirement
  254. :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` to communicate that
  255. consumers must be compiled as position-independent-code.
  256. .. code-block:: cmake
  257. add_executable(exe1 exe1.cpp)
  258. set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE ON)
  259. add_library(lib1 SHARED lib1.cpp)
  260. set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  261. add_executable(exe2 exe2.cpp)
  262. target_link_libraries(exe2 lib1)
  263. Here, both ``exe1`` and ``exe2`` will be compiled as position-independent-code.
  264. ``lib1`` will also be compiled as position-independent-code because that is the
  265. default setting for ``SHARED`` libraries. If dependencies have conflicting,
  266. non-compatible requirements :manual:`cmake(1)` issues a diagnostic:
  267. .. code-block:: cmake
  268. add_library(lib1 SHARED lib1.cpp)
  269. set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  270. add_library(lib2 SHARED lib2.cpp)
  271. set_property(TARGET lib2 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)
  272. add_executable(exe1 exe1.cpp)
  273. target_link_libraries(exe1 lib1)
  274. set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE OFF)
  275. add_executable(exe2 exe2.cpp)
  276. target_link_libraries(exe2 lib1 lib2)
  277. The ``lib1`` requirement ``INTERFACE_POSITION_INDEPENDENT_CODE`` is not
  278. "compatible" with the :prop_tgt:`POSITION_INDEPENDENT_CODE` property of
  279. the ``exe1`` target. The library requires that consumers are built as
  280. position-independent-code, while the executable specifies to not built as
  281. position-independent-code, so a diagnostic is issued.
  282. The ``lib1`` and ``lib2`` requirements are not "compatible". One of them
  283. requires that consumers are built as position-independent-code, while
  284. the other requires that consumers are not built as position-independent-code.
  285. Because ``exe2`` links to both and they are in conflict, a CMake error message
  286. is issued::
  287. CMake Error: The INTERFACE_POSITION_INDEPENDENT_CODE property of "lib2" does
  288. not agree with the value of POSITION_INDEPENDENT_CODE already determined
  289. for "exe2".
  290. To be "compatible", the :prop_tgt:`POSITION_INDEPENDENT_CODE` property,
  291. if set must be either the same, in a boolean sense, as the
  292. :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` property of all transitively
  293. specified dependencies on which that property is set.
  294. This property of "compatible interface requirement" may be extended to other
  295. properties by specifying the property in the content of the
  296. :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` target property. Each specified property
  297. must be compatible between the consuming target and the corresponding property
  298. with an ``INTERFACE_`` prefix from each dependency:
  299. .. code-block:: cmake
  300. add_library(lib1Version2 SHARED lib1_v2.cpp)
  301. set_property(TARGET lib1Version2 PROPERTY INTERFACE_CUSTOM_PROP ON)
  302. set_property(TARGET lib1Version2 APPEND PROPERTY
  303. COMPATIBLE_INTERFACE_BOOL CUSTOM_PROP
  304. )
  305. add_library(lib1Version3 SHARED lib1_v3.cpp)
  306. set_property(TARGET lib1Version3 PROPERTY INTERFACE_CUSTOM_PROP OFF)
  307. add_executable(exe1 exe1.cpp)
  308. target_link_libraries(exe1 lib1Version2) # CUSTOM_PROP will be ON
  309. add_executable(exe2 exe2.cpp)
  310. target_link_libraries(exe2 lib1Version2 lib1Version3) # Diagnostic
  311. Non-boolean properties may also participate in "compatible interface"
  312. computations. Properties specified in the
  313. :prop_tgt:`COMPATIBLE_INTERFACE_STRING`
  314. property must be either unspecified or compare to the same string among
  315. all transitively specified dependencies. This can be useful to ensure
  316. that multiple incompatible versions of a library are not linked together
  317. through transitive requirements of a target:
  318. .. code-block:: cmake
  319. add_library(lib1Version2 SHARED lib1_v2.cpp)
  320. set_property(TARGET lib1Version2 PROPERTY INTERFACE_LIB_VERSION 2)
  321. set_property(TARGET lib1Version2 APPEND PROPERTY
  322. COMPATIBLE_INTERFACE_STRING LIB_VERSION
  323. )
  324. add_library(lib1Version3 SHARED lib1_v3.cpp)
  325. set_property(TARGET lib1Version3 PROPERTY INTERFACE_LIB_VERSION 3)
  326. add_executable(exe1 exe1.cpp)
  327. target_link_libraries(exe1 lib1Version2) # LIB_VERSION will be "2"
  328. add_executable(exe2 exe2.cpp)
  329. target_link_libraries(exe2 lib1Version2 lib1Version3) # Diagnostic
  330. The :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` target property specifies
  331. that content will be evaluated numerically and the maximum number among all
  332. specified will be calculated:
  333. .. code-block:: cmake
  334. add_library(lib1Version2 SHARED lib1_v2.cpp)
  335. set_property(TARGET lib1Version2 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 200)
  336. set_property(TARGET lib1Version2 APPEND PROPERTY
  337. COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED
  338. )
  339. add_library(lib1Version3 SHARED lib1_v3.cpp)
  340. set_property(TARGET lib1Version3 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 1000)
  341. add_executable(exe1 exe1.cpp)
  342. # CONTAINER_SIZE_REQUIRED will be "200"
  343. target_link_libraries(exe1 lib1Version2)
  344. add_executable(exe2 exe2.cpp)
  345. # CONTAINER_SIZE_REQUIRED will be "1000"
  346. target_link_libraries(exe2 lib1Version2 lib1Version3)
  347. Similarly, the :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN` may be used to
  348. calculate the numeric minimum value for a property from dependencies.
  349. Each calculated "compatible" property value may be read in the consumer at
  350. generate-time using generator expressions.
  351. Note that for each dependee, the set of properties specified in each
  352. compatible interface property must not intersect with the set specified in
  353. any of the other properties.
  354. Property Origin Debugging
  355. -------------------------
  356. Because build specifications can be determined by dependencies, the lack of
  357. locality of code which creates a target and code which is responsible for
  358. setting build specifications may make the code more difficult to reason about.
  359. :manual:`cmake(1)` provides a debugging facility to print the origin of the
  360. contents of properties which may be determined by dependencies. The properties
  361. which can be debugged are listed in the
  362. :variable:`CMAKE_DEBUG_TARGET_PROPERTIES` variable documentation:
  363. .. code-block:: cmake
  364. set(CMAKE_DEBUG_TARGET_PROPERTIES
  365. INCLUDE_DIRECTORIES
  366. COMPILE_DEFINITIONS
  367. POSITION_INDEPENDENT_CODE
  368. CONTAINER_SIZE_REQUIRED
  369. LIB_VERSION
  370. )
  371. add_executable(exe1 exe1.cpp)
  372. In the case of properties listed in :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` or
  373. :prop_tgt:`COMPATIBLE_INTERFACE_STRING`, the debug output shows which target
  374. was responsible for setting the property, and which other dependencies also
  375. defined the property. In the case of
  376. :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` and
  377. :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN`, the debug output shows the
  378. value of the property from each dependency, and whether the value determines
  379. the new extreme.
  380. Build Specification with Generator Expressions
  381. ----------------------------------------------
  382. Build specifications may use
  383. :manual:`generator expressions <cmake-generator-expressions(7)>` containing
  384. content which may be conditional or known only at generate-time. For example,
  385. the calculated "compatible" value of a property may be read with the
  386. ``TARGET_PROPERTY`` expression:
  387. .. code-block:: cmake
  388. add_library(lib1Version2 SHARED lib1_v2.cpp)
  389. set_property(TARGET lib1Version2 PROPERTY
  390. INTERFACE_CONTAINER_SIZE_REQUIRED 200)
  391. set_property(TARGET lib1Version2 APPEND PROPERTY
  392. COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED
  393. )
  394. add_executable(exe1 exe1.cpp)
  395. target_link_libraries(exe1 lib1Version2)
  396. target_compile_definitions(exe1 PRIVATE
  397. CONTAINER_SIZE=$<TARGET_PROPERTY:CONTAINER_SIZE_REQUIRED>
  398. )
  399. In this case, the ``exe1`` source files will be compiled with
  400. ``-DCONTAINER_SIZE=200``.
  401. Configuration determined build specifications may be conveniently set using
  402. the ``CONFIG`` generator expression.
  403. .. code-block:: cmake
  404. target_compile_definitions(exe1 PRIVATE
  405. $<$<CONFIG:Debug>:DEBUG_BUILD>
  406. )
  407. The ``CONFIG`` parameter is compared case-insensitively with the configuration
  408. being built. In the presence of :prop_tgt:`IMPORTED` targets, the content of
  409. :prop_tgt:`MAP_IMPORTED_CONFIG_DEBUG <MAP_IMPORTED_CONFIG_<CONFIG>>` is also
  410. accounted for by this expression.
  411. Some buildsystems generated by :manual:`cmake(1)` have a predetermined
  412. build-configuration set in the :variable:`CMAKE_BUILD_TYPE` variable. The
  413. buildsystem for the IDEs such as Visual Studio and Xcode are generated
  414. independent of the build-configuration, and the actual build configuration
  415. is not known until build-time. Therefore, code such as
  416. .. code-block:: cmake
  417. string(TOLOWER ${CMAKE_BUILD_TYPE} _type)
  418. if (_type STREQUAL debug)
  419. target_compile_definitions(exe1 PRIVATE DEBUG_BUILD)
  420. endif()
  421. may appear to work for :ref:`Makefile Generators` and :generator:`Ninja`
  422. generators, but is not portable to IDE generators. Additionally,
  423. the :prop_tgt:`IMPORTED` configuration-mappings are not accounted for
  424. with code like this, so it should be avoided.
  425. The unary ``TARGET_PROPERTY`` generator expression and the ``TARGET_POLICY``
  426. generator expression are evaluated with the consuming target context. This
  427. means that a usage requirement specification may be evaluated differently based
  428. on the consumer:
  429. .. code-block:: cmake
  430. add_library(lib1 lib1.cpp)
  431. target_compile_definitions(lib1 INTERFACE
  432. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:LIB1_WITH_EXE>
  433. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:LIB1_WITH_SHARED_LIB>
  434. $<$<TARGET_POLICY:CMP0041>:CONSUMER_CMP0041_NEW>
  435. )
  436. add_executable(exe1 exe1.cpp)
  437. target_link_libraries(exe1 lib1)
  438. cmake_policy(SET CMP0041 NEW)
  439. add_library(shared_lib shared_lib.cpp)
  440. target_link_libraries(shared_lib lib1)
  441. The ``exe1`` executable will be compiled with ``-DLIB1_WITH_EXE``, while the
  442. ``shared_lib`` shared library will be compiled with ``-DLIB1_WITH_SHARED_LIB``
  443. and ``-DCONSUMER_CMP0041_NEW``, because policy :policy:`CMP0041` is
  444. ``NEW`` at the point where the ``shared_lib`` target is created.
  445. The ``BUILD_INTERFACE`` expression wraps requirements which are only used when
  446. consumed from a target in the same buildsystem, or when consumed from a target
  447. exported to the build directory using the :command:`export` command. The
  448. ``INSTALL_INTERFACE`` expression wraps requirements which are only used when
  449. consumed from a target which has been installed and exported with the
  450. :command:`install(EXPORT)` command:
  451. .. code-block:: cmake
  452. add_library(ClimbingStats climbingstats.cpp)
  453. target_compile_definitions(ClimbingStats INTERFACE
  454. $<BUILD_INTERFACE:ClimbingStats_FROM_BUILD_LOCATION>
  455. $<INSTALL_INTERFACE:ClimbingStats_FROM_INSTALLED_LOCATION>
  456. )
  457. install(TARGETS ClimbingStats EXPORT libExport ${InstallArgs})
  458. install(EXPORT libExport NAMESPACE Upstream::
  459. DESTINATION lib/cmake/ClimbingStats)
  460. export(EXPORT libExport NAMESPACE Upstream::)
  461. add_executable(exe1 exe1.cpp)
  462. target_link_libraries(exe1 ClimbingStats)
  463. In this case, the ``exe1`` executable will be compiled with
  464. ``-DClimbingStats_FROM_BUILD_LOCATION``. The exporting commands generate
  465. :prop_tgt:`IMPORTED` targets with either the ``INSTALL_INTERFACE`` or the
  466. ``BUILD_INTERFACE`` omitted, and the ``*_INTERFACE`` marker stripped away.
  467. A separate project consuming the ``ClimbingStats`` package would contain:
  468. .. code-block:: cmake
  469. find_package(ClimbingStats REQUIRED)
  470. add_executable(Downstream main.cpp)
  471. target_link_libraries(Downstream Upstream::ClimbingStats)
  472. Depending on whether the ``ClimbingStats`` package was used from the build
  473. location or the install location, the ``Downstream`` target would be compiled
  474. with either ``-DClimbingStats_FROM_BUILD_LOCATION`` or
  475. ``-DClimbingStats_FROM_INSTALL_LOCATION``. For more about packages and
  476. exporting see the :manual:`cmake-packages(7)` manual.
  477. .. _`Include Directories and Usage Requirements`:
  478. Include Directories and Usage Requirements
  479. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  480. Include directories require some special consideration when specified as usage
  481. requirements and when used with generator expressions. The
  482. :command:`target_include_directories` command accepts both relative and
  483. absolute include directories:
  484. .. code-block:: cmake
  485. add_library(lib1 lib1.cpp)
  486. target_include_directories(lib1 PRIVATE
  487. /absolute/path
  488. relative/path
  489. )
  490. Relative paths are interpreted relative to the source directory where the
  491. command appears. Relative paths are not allowed in the
  492. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of :prop_tgt:`IMPORTED` targets.
  493. In cases where a non-trivial generator expression is used, the
  494. ``INSTALL_PREFIX`` expression may be used within the argument of an
  495. ``INSTALL_INTERFACE`` expression. It is a replacement marker which
  496. expands to the installation prefix when imported by a consuming project.
  497. Include directories usage requirements commonly differ between the build-tree
  498. and the install-tree. The ``BUILD_INTERFACE`` and ``INSTALL_INTERFACE``
  499. generator expressions can be used to describe separate usage requirements
  500. based on the usage location. Relative paths are allowed within the
  501. ``INSTALL_INTERFACE`` expression and are interpreted relative to the
  502. installation prefix. For example:
  503. .. code-block:: cmake
  504. add_library(ClimbingStats climbingstats.cpp)
  505. target_include_directories(ClimbingStats INTERFACE
  506. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>
  507. $<INSTALL_INTERFACE:/absolute/path>
  508. $<INSTALL_INTERFACE:relative/path>
  509. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/$<CONFIG>/generated>
  510. )
  511. Two convenience APIs are provided relating to include directories usage
  512. requirements. The :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE` variable
  513. may be enabled, with an equivalent effect to:
  514. .. code-block:: cmake
  515. set_property(TARGET tgt APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
  516. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_BINARY_DIR}>
  517. )
  518. for each target affected. The convenience for installed targets is
  519. an ``INCLUDES DESTINATION`` component with the :command:`install(TARGETS)`
  520. command:
  521. .. code-block:: cmake
  522. install(TARGETS foo bar bat EXPORT tgts ${dest_args}
  523. INCLUDES DESTINATION include
  524. )
  525. install(EXPORT tgts ${other_args})
  526. install(FILES ${headers} DESTINATION include)
  527. This is equivalent to appending ``${CMAKE_INSTALL_PREFIX}/include`` to the
  528. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of each of the installed
  529. :prop_tgt:`IMPORTED` targets when generated by :command:`install(EXPORT)`.
  530. When the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of an
  531. :ref:`imported target <Imported targets>` is consumed, the entries in the
  532. property are treated as ``SYSTEM`` include directories, as if they were
  533. listed in the :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` of the
  534. dependency. This can result in omission of compiler warnings for headers
  535. found in those directories. This behavior for :ref:`imported targets` may
  536. be controlled by setting the :prop_tgt:`NO_SYSTEM_FROM_IMPORTED` target
  537. property on the *consumers* of imported targets.
  538. If a binary target is linked transitively to a macOS :prop_tgt:`FRAMEWORK`, the
  539. ``Headers`` directory of the framework is also treated as a usage requirement.
  540. This has the same effect as passing the framework directory as an include
  541. directory.
  542. Link Libraries and Generator Expressions
  543. ----------------------------------------
  544. Like build specifications, :prop_tgt:`link libraries <LINK_LIBRARIES>` may be
  545. specified with generator expression conditions. However, as consumption of
  546. usage requirements is based on collection from linked dependencies, there is
  547. an additional limitation that the link dependencies must form a "directed
  548. acyclic graph". That is, if linking to a target is dependent on the value of
  549. a target property, that target property may not be dependent on the linked
  550. dependencies:
  551. .. code-block:: cmake
  552. add_library(lib1 lib1.cpp)
  553. add_library(lib2 lib2.cpp)
  554. target_link_libraries(lib1 PUBLIC
  555. $<$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>:lib2>
  556. )
  557. add_library(lib3 lib3.cpp)
  558. set_property(TARGET lib3 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  559. add_executable(exe1 exe1.cpp)
  560. target_link_libraries(exe1 lib1 lib3)
  561. As the value of the :prop_tgt:`POSITION_INDEPENDENT_CODE` property of
  562. the ``exe1`` target is dependent on the linked libraries (``lib3``), and the
  563. edge of linking ``exe1`` is determined by the same
  564. :prop_tgt:`POSITION_INDEPENDENT_CODE` property, the dependency graph above
  565. contains a cycle. :manual:`cmake(1)` issues an error message.
  566. .. _`Output Artifacts`:
  567. Output Artifacts
  568. ----------------
  569. The buildsystem targets created by the :command:`add_library` and
  570. :command:`add_executable` commands create rules to create binary outputs.
  571. The exact output location of the binaries can only be determined at
  572. generate-time because it can depend on the build-configuration and the
  573. link-language of linked dependencies etc. ``TARGET_FILE``,
  574. ``TARGET_LINKER_FILE`` and related expressions can be used to access the
  575. name and location of generated binaries. These expressions do not work
  576. for ``OBJECT`` libraries however, as there is no single file generated
  577. by such libraries which is relevant to the expressions.
  578. There are three kinds of output artifacts that may be build by targets
  579. as detailed in the following sections. Their classification differs
  580. between DLL platforms and non-DLL platforms. All Windows-based
  581. systems including Cygwin are DLL platforms.
  582. .. _`Runtime Output Artifacts`:
  583. Runtime Output Artifacts
  584. ^^^^^^^^^^^^^^^^^^^^^^^^
  585. A *runtime* output artifact of a buildsystem target may be:
  586. * The executable file (e.g. ``.exe``) of an executable target
  587. created by the :command:`add_executable` command.
  588. * On DLL platforms: the executable file (e.g. ``.dll``) of a shared
  589. library target created by the :command:`add_library` command
  590. with the ``SHARED`` option.
  591. The :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
  592. target properties may be used to control runtime output artifact locations
  593. and names in the build tree.
  594. .. _`Library Output Artifacts`:
  595. Library Output Artifacts
  596. ^^^^^^^^^^^^^^^^^^^^^^^^
  597. A *library* output artifact of a buildsystem target may be:
  598. * The loadable module file (e.g. ``.dll`` or ``.so``) of a module
  599. library target created by the :command:`add_library` command
  600. with the ``MODULE`` option.
  601. * On non-DLL platforms: the shared library file (e.g. ``.so`` or ``.dylib``)
  602. of a shared library target created by the :command:`add_library`
  603. command with the ``SHARED`` option.
  604. The :prop_tgt:`LIBRARY_OUTPUT_DIRECTORY` and :prop_tgt:`LIBRARY_OUTPUT_NAME`
  605. target properties may be used to control library output artifact locations
  606. and names in the build tree.
  607. .. _`Archive Output Artifacts`:
  608. Archive Output Artifacts
  609. ^^^^^^^^^^^^^^^^^^^^^^^^
  610. An *archive* output artifact of a buildsystem target may be:
  611. * The static library file (e.g. ``.lib`` or ``.a``) of a static
  612. library target created by the :command:`add_library` command
  613. with the ``STATIC`` option.
  614. * On DLL platforms: the import library file (e.g. ``.lib``) of a shared
  615. library target created by the :command:`add_library` command
  616. with the ``SHARED`` option. This file is only guaranteed to exist if
  617. the library exports at least one unmanaged symbol.
  618. * On DLL platforms: the import library file (e.g. ``.lib``) of an
  619. executable target created by the :command:`add_executable` command
  620. when its :prop_tgt:`ENABLE_EXPORTS` target property is set.
  621. * On AIX: the linker import file (e.g. ``.imp``) of an executable target
  622. created by the :command:`add_executable` command when its
  623. :prop_tgt:`ENABLE_EXPORTS` target property is set.
  624. The :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY` and :prop_tgt:`ARCHIVE_OUTPUT_NAME`
  625. target properties may be used to control archive output artifact locations
  626. and names in the build tree.
  627. Directory-Scoped Commands
  628. -------------------------
  629. The :command:`target_include_directories`,
  630. :command:`target_compile_definitions` and
  631. :command:`target_compile_options` commands have an effect on only one
  632. target at a time. The commands :command:`add_compile_definitions`,
  633. :command:`add_compile_options` and :command:`include_directories` have
  634. a similar function, but operate at directory scope instead of target
  635. scope for convenience.
  636. Pseudo Targets
  637. ==============
  638. Some target types do not represent outputs of the buildsystem, but only inputs
  639. such as external dependencies, aliases or other non-build artifacts. Pseudo
  640. targets are not represented in the generated buildsystem.
  641. .. _`Imported Targets`:
  642. Imported Targets
  643. ----------------
  644. An :prop_tgt:`IMPORTED` target represents a pre-existing dependency. Usually
  645. such targets are defined by an upstream package and should be treated as
  646. immutable. After declaring an :prop_tgt:`IMPORTED` target one can adjust its
  647. target properties by using the customary commands such as
  648. :command:`target_compile_definitions`, :command:`target_include_directories`,
  649. :command:`target_compile_options` or :command:`target_link_libraries` just like
  650. with any other regular target.
  651. :prop_tgt:`IMPORTED` targets may have the same usage requirement properties
  652. populated as binary targets, such as
  653. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
  654. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,
  655. :prop_tgt:`INTERFACE_COMPILE_OPTIONS`,
  656. :prop_tgt:`INTERFACE_LINK_LIBRARIES`, and
  657. :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.
  658. The :prop_tgt:`LOCATION` may also be read from an IMPORTED target, though there
  659. is rarely reason to do so. Commands such as :command:`add_custom_command` can
  660. transparently use an :prop_tgt:`IMPORTED` :prop_tgt:`EXECUTABLE <TYPE>` target
  661. as a ``COMMAND`` executable.
  662. The scope of the definition of an :prop_tgt:`IMPORTED` target is the directory
  663. where it was defined. It may be accessed and used from subdirectories, but
  664. not from parent directories or sibling directories. The scope is similar to
  665. the scope of a cmake variable.
  666. It is also possible to define a ``GLOBAL`` :prop_tgt:`IMPORTED` target which is
  667. accessible globally in the buildsystem.
  668. See the :manual:`cmake-packages(7)` manual for more on creating packages
  669. with :prop_tgt:`IMPORTED` targets.
  670. .. _`Alias Targets`:
  671. Alias Targets
  672. -------------
  673. An ``ALIAS`` target is a name which may be used interchangeably with
  674. a binary target name in read-only contexts. A primary use-case for ``ALIAS``
  675. targets is for example or unit test executables accompanying a library, which
  676. may be part of the same buildsystem or built separately based on user
  677. configuration.
  678. .. code-block:: cmake
  679. add_library(lib1 lib1.cpp)
  680. install(TARGETS lib1 EXPORT lib1Export ${dest_args})
  681. install(EXPORT lib1Export NAMESPACE Upstream:: ${other_args})
  682. add_library(Upstream::lib1 ALIAS lib1)
  683. In another directory, we can link unconditionally to the ``Upstream::lib1``
  684. target, which may be an :prop_tgt:`IMPORTED` target from a package, or an
  685. ``ALIAS`` target if built as part of the same buildsystem.
  686. .. code-block:: cmake
  687. if (NOT TARGET Upstream::lib1)
  688. find_package(lib1 REQUIRED)
  689. endif()
  690. add_executable(exe1 exe1.cpp)
  691. target_link_libraries(exe1 Upstream::lib1)
  692. ``ALIAS`` targets are not mutable, installable or exportable. They are
  693. entirely local to the buildsystem description. A name can be tested for
  694. whether it is an ``ALIAS`` name by reading the :prop_tgt:`ALIASED_TARGET`
  695. property from it:
  696. .. code-block:: cmake
  697. get_target_property(_aliased Upstream::lib1 ALIASED_TARGET)
  698. if(_aliased)
  699. message(STATUS "The name Upstream::lib1 is an ALIAS for ${_aliased}.")
  700. endif()
  701. .. _`Interface Libraries`:
  702. Interface Libraries
  703. -------------------
  704. An ``INTERFACE`` library target does not compile sources and does not
  705. produce a library artifact on disk, so it has no :prop_tgt:`LOCATION`.
  706. It may specify usage requirements such as
  707. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
  708. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,
  709. :prop_tgt:`INTERFACE_COMPILE_OPTIONS`,
  710. :prop_tgt:`INTERFACE_LINK_LIBRARIES`,
  711. :prop_tgt:`INTERFACE_SOURCES`,
  712. and :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.
  713. Only the ``INTERFACE`` modes of the :command:`target_include_directories`,
  714. :command:`target_compile_definitions`, :command:`target_compile_options`,
  715. :command:`target_sources`, and :command:`target_link_libraries` commands
  716. may be used with ``INTERFACE`` libraries.
  717. Since CMake 3.19, an ``INTERFACE`` library target may optionally contain
  718. source files. An interface library that contains source files will be
  719. included as a build target in the generated buildsystem. It does not
  720. compile sources, but may contain custom commands to generate other sources.
  721. Additionally, IDEs will show the source files as part of the target for
  722. interactive reading and editing.
  723. A primary use-case for ``INTERFACE`` libraries is header-only libraries.
  724. .. code-block:: cmake
  725. add_library(Eigen INTERFACE
  726. src/eigen.h
  727. src/vector.h
  728. src/matrix.h
  729. )
  730. target_include_directories(Eigen INTERFACE
  731. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
  732. $<INSTALL_INTERFACE:include/Eigen>
  733. )
  734. add_executable(exe1 exe1.cpp)
  735. target_link_libraries(exe1 Eigen)
  736. Here, the usage requirements from the ``Eigen`` target are consumed and used
  737. when compiling, but it has no effect on linking.
  738. Another use-case is to employ an entirely target-focussed design for usage
  739. requirements:
  740. .. code-block:: cmake
  741. add_library(pic_on INTERFACE)
  742. set_property(TARGET pic_on PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  743. add_library(pic_off INTERFACE)
  744. set_property(TARGET pic_off PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)
  745. add_library(enable_rtti INTERFACE)
  746. target_compile_options(enable_rtti INTERFACE
  747. $<$<OR:$<COMPILER_ID:GNU>,$<COMPILER_ID:Clang>>:-rtti>
  748. )
  749. add_executable(exe1 exe1.cpp)
  750. target_link_libraries(exe1 pic_on enable_rtti)
  751. This way, the build specification of ``exe1`` is expressed entirely as linked
  752. targets, and the complexity of compiler-specific flags is encapsulated in an
  753. ``INTERFACE`` library target.
  754. ``INTERFACE`` libraries may be installed and exported. Any content they refer
  755. to must be installed separately:
  756. .. code-block:: cmake
  757. set(Eigen_headers
  758. src/eigen.h
  759. src/vector.h
  760. src/matrix.h
  761. )
  762. add_library(Eigen INTERFACE ${Eigen_headers})
  763. target_include_directories(Eigen INTERFACE
  764. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
  765. $<INSTALL_INTERFACE:include/Eigen>
  766. )
  767. install(TARGETS Eigen EXPORT eigenExport)
  768. install(EXPORT eigenExport NAMESPACE Upstream::
  769. DESTINATION lib/cmake/Eigen
  770. )
  771. install(FILES ${Eigen_headers}
  772. DESTINATION include/Eigen
  773. )