cmake-buildsystem.7.rst 38 KB

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