cmake-buildsystem.7.rst 37 KB

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