cmake-buildsystem.7.rst 33 KB

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