cmake-buildsystem.7.rst 38 KB

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