cmake-toolchains.7.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. .. cmake-manual-description: CMake Toolchains Reference
  2. cmake-toolchains(7)
  3. *******************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. CMake uses a toolchain of utilities to compile, link libraries and create
  9. archives, and other tasks to drive the build. The toolchain utilities available
  10. are determined by the languages enabled. In normal builds, CMake automatically
  11. determines the toolchain for host builds based on system introspection and
  12. defaults. In cross-compiling scenarios, a toolchain file may be specified
  13. with information about compiler and utility paths.
  14. Languages
  15. =========
  16. Languages are enabled by the :command:`project` command. Language-specific
  17. built-in variables, such as
  18. :variable:`CMAKE_CXX_COMPILER <CMAKE_<LANG>_COMPILER>`,
  19. :variable:`CMAKE_CXX_COMPILER_ID <CMAKE_<LANG>_COMPILER_ID>` etc are set by
  20. invoking the :command:`project` command. If no project command
  21. is in the top-level CMakeLists file, one will be implicitly generated. By default
  22. the enabled languages are ``C`` and ``CXX``:
  23. .. code-block:: cmake
  24. project(C_Only C)
  25. A special value of ``NONE`` can also be used with the :command:`project` command
  26. to enable no languages:
  27. .. code-block:: cmake
  28. project(MyProject NONE)
  29. The :command:`enable_language` command can be used to enable languages after the
  30. :command:`project` command:
  31. .. code-block:: cmake
  32. enable_language(CXX)
  33. When a language is enabled, CMake finds a compiler for that language, and
  34. determines some information, such as the vendor and version of the compiler,
  35. the target architecture and bitwidth, the location of corresponding utilities
  36. etc.
  37. The :prop_gbl:`ENABLED_LANGUAGES` global property contains the languages which
  38. are currently enabled.
  39. Variables and Properties
  40. ========================
  41. Several variables relate to the language components of a toolchain which are
  42. enabled. :variable:`CMAKE_<LANG>_COMPILER` is the full path to the compiler used
  43. for ``<LANG>``. :variable:`CMAKE_<LANG>_COMPILER_ID` is the identifier used
  44. by CMake for the compiler and :variable:`CMAKE_<LANG>_COMPILER_VERSION` is the
  45. version of the compiler.
  46. The :variable:`CMAKE_<LANG>_FLAGS` variables and the configuration-specific
  47. equivalents contain flags that will be added to the compile command when
  48. compiling a file of a particular language.
  49. As the linker is invoked by the compiler driver, CMake needs a way to determine
  50. which compiler to use to invoke the linker. This is calculated by the
  51. :prop_sf:`LANGUAGE` of source files in the target, and in the case of static
  52. libraries, the language of the dependent libraries. The choice CMake makes may
  53. be overridden with the :prop_tgt:`LINKER_LANGUAGE` target property.
  54. Toolchain Features
  55. ==================
  56. CMake provides the :command:`try_compile` command and wrapper macros such as
  57. :module:`CheckCXXSourceCompiles`, :module:`CheckCXXSymbolExists` and
  58. :module:`CheckIncludeFile` to test capability and availability of various
  59. toolchain features. These APIs test the toolchain in some way and cache the
  60. result so that the test does not have to be performed again the next time
  61. CMake runs.
  62. Some toolchain features have built-in handling in CMake, and do not require
  63. compile-tests. For example, :prop_tgt:`POSITION_INDEPENDENT_CODE` allows
  64. specifying that a target should be built as position-independent code, if
  65. the compiler supports that feature. The :prop_tgt:`<LANG>_VISIBILITY_PRESET`
  66. and :prop_tgt:`VISIBILITY_INLINES_HIDDEN` target properties add flags for
  67. hidden visibility, if supported by the compiler.
  68. .. _`Cross Compiling Toolchain`:
  69. Cross Compiling
  70. ===============
  71. If :manual:`cmake(1)` is invoked with the command line parameter
  72. ``-DCMAKE_TOOLCHAIN_FILE=path/to/file``, the file will be loaded early to set
  73. values for the compilers.
  74. The :variable:`CMAKE_CROSSCOMPILING` variable is set to true when CMake is
  75. cross-compiling.
  76. Note that using the :variable:`CMAKE_SOURCE_DIR` or :variable:`CMAKE_BINARY_DIR`
  77. variables inside a toolchain file is typically undesirable. The toolchain
  78. file is used in contexts where these variables have different values when used
  79. in different places (e.g. as part of a call to :command:`try_compile`). In most
  80. cases, where there is a need to evaluate paths inside a toolchain file, the more
  81. appropriate variable to use would be :variable:`CMAKE_CURRENT_LIST_DIR`, since
  82. it always has an unambiguous, predictable value.
  83. Cross Compiling for Linux
  84. -------------------------
  85. A typical cross-compiling toolchain for Linux has content such
  86. as:
  87. .. code-block:: cmake
  88. set(CMAKE_SYSTEM_NAME Linux)
  89. set(CMAKE_SYSTEM_PROCESSOR arm)
  90. set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
  91. set(CMAKE_STAGING_PREFIX /home/devel/stage)
  92. set(tools /home/devel/gcc-4.7-linaro-rpi-gnueabihf)
  93. set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
  94. set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)
  95. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  96. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  97. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  98. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  99. The :variable:`CMAKE_SYSTEM_NAME` is the CMake-identifier of the target platform
  100. to build for.
  101. The :variable:`CMAKE_SYSTEM_PROCESSOR` is the CMake-identifier of the target architecture
  102. to build for.
  103. The :variable:`CMAKE_SYSROOT` is optional, and may be specified if a sysroot
  104. is available.
  105. The :variable:`CMAKE_STAGING_PREFIX` is also optional. It may be used to specify
  106. a path on the host to install to. The :variable:`CMAKE_INSTALL_PREFIX` is always
  107. the runtime installation location, even when cross-compiling.
  108. The :variable:`CMAKE_<LANG>_COMPILER` variables may be set to full paths, or to
  109. names of compilers to search for in standard locations. For toolchains that
  110. do not support linking binaries without custom flags or scripts one may set
  111. the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to ``STATIC_LIBRARY``
  112. to tell CMake not to try to link executables during its checks.
  113. CMake ``find_*`` commands will look in the sysroot, and the :variable:`CMAKE_FIND_ROOT_PATH`
  114. entries by default in all cases, as well as looking in the host system root prefix.
  115. Although this can be controlled on a case-by-case basis, when cross-compiling, it
  116. can be useful to exclude looking in either the host or the target for particular
  117. artifacts. Generally, includes, libraries and packages should be found in the
  118. target system prefixes, whereas executables which must be run as part of the build
  119. should be found only on the host and not on the target. This is the purpose of
  120. the ``CMAKE_FIND_ROOT_PATH_MODE_*`` variables.
  121. .. _`Cray Cross-Compile`:
  122. Cross Compiling for the Cray Linux Environment
  123. ----------------------------------------------
  124. Cross compiling for compute nodes in the Cray Linux Environment can be done
  125. without needing a separate toolchain file. Specifying
  126. ``-DCMAKE_SYSTEM_NAME=CrayLinuxEnvironment`` on the CMake command line will
  127. ensure that the appropriate build settings and search paths are configured.
  128. The platform will pull its configuration from the current environment
  129. variables and will configure a project to use the compiler wrappers from the
  130. Cray Programming Environment's ``PrgEnv-*`` modules if present and loaded.
  131. The default configuration of the Cray Programming Environment is to only
  132. support static libraries. This can be overridden and shared libraries
  133. enabled by setting the ``CRAYPE_LINK_TYPE`` environment variable to
  134. ``dynamic``.
  135. Running CMake without specifying :variable:`CMAKE_SYSTEM_NAME` will
  136. run the configure step in host mode assuming a standard Linux environment.
  137. If not overridden, the ``PrgEnv-*`` compiler wrappers will end up getting used,
  138. which if targeting the either the login node or compute node, is likely not the
  139. desired behavior. The exception to this would be if you are building directly
  140. on a NID instead of cross-compiling from a login node. If trying to build
  141. software for a login node, you will need to either first unload the
  142. currently loaded ``PrgEnv-*`` module or explicitly tell CMake to use the
  143. system compilers in ``/usr/bin`` instead of the Cray wrappers. If instead
  144. targeting a compute node is desired, just specify the
  145. :variable:`CMAKE_SYSTEM_NAME` as mentioned above.
  146. Cross Compiling using Clang
  147. ---------------------------
  148. Some compilers such as Clang are inherently cross compilers.
  149. The :variable:`CMAKE_<LANG>_COMPILER_TARGET` can be set to pass a
  150. value to those supported compilers when compiling:
  151. .. code-block:: cmake
  152. set(CMAKE_SYSTEM_NAME Linux)
  153. set(CMAKE_SYSTEM_PROCESSOR arm)
  154. set(triple arm-linux-gnueabihf)
  155. set(CMAKE_C_COMPILER clang)
  156. set(CMAKE_C_COMPILER_TARGET ${triple})
  157. set(CMAKE_CXX_COMPILER clang++)
  158. set(CMAKE_CXX_COMPILER_TARGET ${triple})
  159. Similarly, some compilers do not ship their own supplementary utilities
  160. such as linkers, but provide a way to specify the location of the external
  161. toolchain which will be used by the compiler driver. The
  162. :variable:`CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN` variable can be set in a
  163. toolchain file to pass the path to the compiler driver.
  164. Cross Compiling for QNX
  165. -----------------------
  166. As the Clang compiler the QNX QCC compile is inherently a cross compiler.
  167. And the :variable:`CMAKE_<LANG>_COMPILER_TARGET` can be set to pass a
  168. value to those supported compilers when compiling:
  169. .. code-block:: cmake
  170. set(CMAKE_SYSTEM_NAME QNX)
  171. set(arch gcc_ntoarmv7le)
  172. set(CMAKE_C_COMPILER qcc)
  173. set(CMAKE_C_COMPILER_TARGET ${arch})
  174. set(CMAKE_CXX_COMPILER QCC)
  175. set(CMAKE_CXX_COMPILER_TARGET ${arch})
  176. set(CMAKE_SYSROOT $ENV{QNX_TARGET})
  177. Cross Compiling for Windows CE
  178. ------------------------------
  179. Cross compiling for Windows CE requires the corresponding SDK being
  180. installed on your system. These SDKs are usually installed under
  181. ``C:/Program Files (x86)/Windows CE Tools/SDKs``.
  182. A toolchain file to configure a Visual Studio generator for
  183. Windows CE may look like this:
  184. .. code-block:: cmake
  185. set(CMAKE_SYSTEM_NAME WindowsCE)
  186. set(CMAKE_SYSTEM_VERSION 8.0)
  187. set(CMAKE_SYSTEM_PROCESSOR arm)
  188. set(CMAKE_GENERATOR_TOOLSET CE800) # Can be omitted for 8.0
  189. set(CMAKE_GENERATOR_PLATFORM SDK_AM335X_SK_WEC2013_V310)
  190. The :variable:`CMAKE_GENERATOR_PLATFORM` tells the generator which SDK to use.
  191. Further :variable:`CMAKE_SYSTEM_VERSION` tells the generator what version of
  192. Windows CE to use. Currently version 8.0 (Windows Embedded Compact 2013) is
  193. supported out of the box. Other versions may require one to set
  194. :variable:`CMAKE_GENERATOR_TOOLSET` to the correct value.
  195. Cross Compiling for Windows 10 Universal Applications
  196. -----------------------------------------------------
  197. A toolchain file to configure a Visual Studio generator for a
  198. Windows 10 Universal Application may look like this:
  199. .. code-block:: cmake
  200. set(CMAKE_SYSTEM_NAME WindowsStore)
  201. set(CMAKE_SYSTEM_VERSION 10.0)
  202. A Windows 10 Universal Application targets both Windows Store and
  203. Windows Phone. Specify the :variable:`CMAKE_SYSTEM_VERSION` variable
  204. to be ``10.0`` to build with the latest available Windows 10 SDK.
  205. Specify a more specific version (e.g. ``10.0.10240.0`` for RTM)
  206. to build with the corresponding SDK.
  207. Cross Compiling for Windows Phone
  208. ---------------------------------
  209. A toolchain file to configure a Visual Studio generator for
  210. Windows Phone may look like this:
  211. .. code-block:: cmake
  212. set(CMAKE_SYSTEM_NAME WindowsPhone)
  213. set(CMAKE_SYSTEM_VERSION 8.1)
  214. Cross Compiling for Windows Store
  215. ---------------------------------
  216. A toolchain file to configure a Visual Studio generator for
  217. Windows Store may look like this:
  218. .. code-block:: cmake
  219. set(CMAKE_SYSTEM_NAME WindowsStore)
  220. set(CMAKE_SYSTEM_VERSION 8.1)
  221. .. _`Cross Compiling for Android`:
  222. Cross Compiling for Android
  223. ---------------------------
  224. A toolchain file may configure cross-compiling for Android by setting the
  225. :variable:`CMAKE_SYSTEM_NAME` variable to ``Android``. Further configuration
  226. is specific to the Android development environment to be used.
  227. For :ref:`Visual Studio Generators`, CMake expects :ref:`NVIDIA Nsight Tegra
  228. Visual Studio Edition <Cross Compiling for Android with NVIDIA Nsight Tegra
  229. Visual Studio Edition>` to be installed. See that section for further
  230. configuration details.
  231. For :ref:`Makefile Generators` and the :generator:`Ninja` generator,
  232. CMake expects one of these environments:
  233. * :ref:`NDK <Cross Compiling for Android with the NDK>`
  234. * :ref:`Standalone Toolchain <Cross Compiling for Android with a Standalone Toolchain>`
  235. CMake uses the following steps to select one of the environments:
  236. * If the :variable:`CMAKE_ANDROID_NDK` variable is set, the NDK at the
  237. specified location will be used.
  238. * Else, if the :variable:`CMAKE_ANDROID_STANDALONE_TOOLCHAIN` variable
  239. is set, the Standalone Toolchain at the specified location will be used.
  240. * Else, if the :variable:`CMAKE_SYSROOT` variable is set to a directory
  241. of the form ``<ndk>/platforms/android-<api>/arch-<arch>``, the ``<ndk>``
  242. part will be used as the value of :variable:`CMAKE_ANDROID_NDK` and the
  243. NDK will be used.
  244. * Else, if the :variable:`CMAKE_SYSROOT` variable is set to a directory of the
  245. form ``<standalone-toolchain>/sysroot``, the ``<standalone-toolchain>`` part
  246. will be used as the value of :variable:`CMAKE_ANDROID_STANDALONE_TOOLCHAIN`
  247. and the Standalone Toolchain will be used.
  248. * Else, if a cmake variable ``ANDROID_NDK`` is set it will be used
  249. as the value of :variable:`CMAKE_ANDROID_NDK`, and the NDK will be used.
  250. * Else, if a cmake variable ``ANDROID_STANDALONE_TOOLCHAIN`` is set, it will be
  251. used as the value of :variable:`CMAKE_ANDROID_STANDALONE_TOOLCHAIN`, and the
  252. Standalone Toolchain will be used.
  253. * Else, if an environment variable ``ANDROID_NDK_ROOT`` or
  254. ``ANDROID_NDK`` is set, it will be used as the value of
  255. :variable:`CMAKE_ANDROID_NDK`, and the NDK will be used.
  256. * Else, if an environment variable ``ANDROID_STANDALONE_TOOLCHAIN`` is
  257. set then it will be used as the value of
  258. :variable:`CMAKE_ANDROID_STANDALONE_TOOLCHAIN`, and the Standalone
  259. Toolchain will be used.
  260. * Else, an error diagnostic will be issued that neither the NDK or
  261. Standalone Toolchain can be found.
  262. .. _`Cross Compiling for Android with the NDK`:
  263. Cross Compiling for Android with the NDK
  264. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  265. A toolchain file may configure :ref:`Makefile Generators` or the
  266. :generator:`Ninja` generator to target Android for cross-compiling.
  267. Configure use of an Android NDK with the following variables:
  268. :variable:`CMAKE_SYSTEM_NAME`
  269. Set to ``Android``. Must be specified to enable cross compiling
  270. for Android.
  271. :variable:`CMAKE_SYSTEM_VERSION`
  272. Set to the Android API level. If not specified, the value is
  273. determined as follows:
  274. * If the :variable:`CMAKE_ANDROID_API` variable is set, its value
  275. is used as the API level.
  276. * If the :variable:`CMAKE_SYSROOT` variable is set, the API level is
  277. detected from the NDK directory structure containing the sysroot.
  278. * Otherwise, the latest API level available in the NDK is used.
  279. :variable:`CMAKE_ANDROID_ARCH_ABI`
  280. Set to the Android ABI (architecture). If not specified, this
  281. variable will default to ``armeabi``.
  282. The :variable:`CMAKE_ANDROID_ARCH` variable will be computed
  283. from ``CMAKE_ANDROID_ARCH_ABI`` automatically.
  284. Also see the :variable:`CMAKE_ANDROID_ARM_MODE` and
  285. :variable:`CMAKE_ANDROID_ARM_NEON` variables.
  286. :variable:`CMAKE_ANDROID_NDK`
  287. Set to the absolute path to the Android NDK root directory.
  288. A ``${CMAKE_ANDROID_NDK}/platforms`` directory must exist.
  289. If not specified, a default for this variable will be chosen
  290. as specified :ref:`above <Cross Compiling for Android>`.
  291. :variable:`CMAKE_ANDROID_NDK_DEPRECATED_HEADERS`
  292. Set to a true value to use the deprecated per-api-level headers
  293. instead of the unified headers. If not specified, the default will
  294. be false unless using a NDK that does not provide unified headers.
  295. :variable:`CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION`
  296. On NDK r19 or above, this variable must be unset or set to ``clang``.
  297. On NDK r18 or below, set this to the version of the NDK toolchain to
  298. be selected as the compiler. If not specified, the default will be
  299. the latest available GCC toolchain.
  300. :variable:`CMAKE_ANDROID_STL_TYPE`
  301. Set to specify which C++ standard library to use. If not specified,
  302. a default will be selected as described in the variable documentation.
  303. The following variables will be computed and provided automatically:
  304. :variable:`CMAKE_<LANG>_ANDROID_TOOLCHAIN_PREFIX`
  305. The absolute path prefix to the binutils in the NDK toolchain.
  306. :variable:`CMAKE_<LANG>_ANDROID_TOOLCHAIN_SUFFIX`
  307. The host platform suffix of the binutils in the NDK toolchain.
  308. For example, a toolchain file might contain:
  309. .. code-block:: cmake
  310. set(CMAKE_SYSTEM_NAME Android)
  311. set(CMAKE_SYSTEM_VERSION 21) # API level
  312. set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
  313. set(CMAKE_ANDROID_NDK /path/to/android-ndk)
  314. set(CMAKE_ANDROID_STL_TYPE gnustl_static)
  315. Alternatively one may specify the values without a toolchain file:
  316. .. code-block:: console
  317. $ cmake ../src \
  318. -DCMAKE_SYSTEM_NAME=Android \
  319. -DCMAKE_SYSTEM_VERSION=21 \
  320. -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
  321. -DCMAKE_ANDROID_NDK=/path/to/android-ndk \
  322. -DCMAKE_ANDROID_STL_TYPE=gnustl_static
  323. .. _`Cross Compiling for Android with a Standalone Toolchain`:
  324. Cross Compiling for Android with a Standalone Toolchain
  325. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  326. A toolchain file may configure :ref:`Makefile Generators` or the
  327. :generator:`Ninja` generator to target Android for cross-compiling
  328. using a standalone toolchain.
  329. Configure use of an Android standalone toolchain with the following variables:
  330. :variable:`CMAKE_SYSTEM_NAME`
  331. Set to ``Android``. Must be specified to enable cross compiling
  332. for Android.
  333. :variable:`CMAKE_ANDROID_STANDALONE_TOOLCHAIN`
  334. Set to the absolute path to the standalone toolchain root directory.
  335. A ``${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot`` directory
  336. must exist.
  337. If not specified, a default for this variable will be chosen
  338. as specified :ref:`above <Cross Compiling for Android>`.
  339. :variable:`CMAKE_ANDROID_ARM_MODE`
  340. When the standalone toolchain targets ARM, optionally set this to ``ON``
  341. to target 32-bit ARM instead of 16-bit Thumb.
  342. See variable documentation for details.
  343. :variable:`CMAKE_ANDROID_ARM_NEON`
  344. When the standalone toolchain targets ARM v7, optionally set thisto ``ON``
  345. to target ARM NEON devices. See variable documentation for details.
  346. The following variables will be computed and provided automatically:
  347. :variable:`CMAKE_SYSTEM_VERSION`
  348. The Android API level detected from the standalone toolchain.
  349. :variable:`CMAKE_ANDROID_ARCH_ABI`
  350. The Android ABI detected from the standalone toolchain.
  351. :variable:`CMAKE_<LANG>_ANDROID_TOOLCHAIN_PREFIX`
  352. The absolute path prefix to the ``binutils`` in the standalone toolchain.
  353. :variable:`CMAKE_<LANG>_ANDROID_TOOLCHAIN_SUFFIX`
  354. The host platform suffix of the ``binutils`` in the standalone toolchain.
  355. For example, a toolchain file might contain:
  356. .. code-block:: cmake
  357. set(CMAKE_SYSTEM_NAME Android)
  358. set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN /path/to/android-toolchain)
  359. Alternatively one may specify the values without a toolchain file:
  360. .. code-block:: console
  361. $ cmake ../src \
  362. -DCMAKE_SYSTEM_NAME=Android \
  363. -DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=/path/to/android-toolchain
  364. .. _`Cross Compiling for Android with NVIDIA Nsight Tegra Visual Studio Edition`:
  365. Cross Compiling for Android with NVIDIA Nsight Tegra Visual Studio Edition
  366. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  367. A toolchain file to configure one of the :ref:`Visual Studio Generators`
  368. to build using NVIDIA Nsight Tegra targeting Android may look like this:
  369. .. code-block:: cmake
  370. set(CMAKE_SYSTEM_NAME Android)
  371. The :variable:`CMAKE_GENERATOR_TOOLSET` may be set to select
  372. the Nsight Tegra "Toolchain Version" value.
  373. See also target properties:
  374. * :prop_tgt:`ANDROID_ANT_ADDITIONAL_OPTIONS`
  375. * :prop_tgt:`ANDROID_API_MIN`
  376. * :prop_tgt:`ANDROID_API`
  377. * :prop_tgt:`ANDROID_ARCH`
  378. * :prop_tgt:`ANDROID_ASSETS_DIRECTORIES`
  379. * :prop_tgt:`ANDROID_GUI`
  380. * :prop_tgt:`ANDROID_JAR_DEPENDENCIES`
  381. * :prop_tgt:`ANDROID_JAR_DIRECTORIES`
  382. * :prop_tgt:`ANDROID_JAVA_SOURCE_DIR`
  383. * :prop_tgt:`ANDROID_NATIVE_LIB_DEPENDENCIES`
  384. * :prop_tgt:`ANDROID_NATIVE_LIB_DIRECTORIES`
  385. * :prop_tgt:`ANDROID_PROCESS_MAX`
  386. * :prop_tgt:`ANDROID_PROGUARD_CONFIG_PATH`
  387. * :prop_tgt:`ANDROID_PROGUARD`
  388. * :prop_tgt:`ANDROID_SECURE_PROPS_PATH`
  389. * :prop_tgt:`ANDROID_SKIP_ANT_STEP`
  390. * :prop_tgt:`ANDROID_STL_TYPE`
  391. .. _`Cross Compiling for iOS, tvOS, or watchOS`:
  392. Cross Compiling for iOS, tvOS, or watchOS
  393. -----------------------------------------
  394. For cross-compiling to iOS, tvOS, or watchOS, the :generator:`Xcode`
  395. generator is recommended. The :generator:`Unix Makefiles` or
  396. :generator:`Ninja` generators can also be used, but they require the
  397. project to handle more areas like target CPU selection and code signing.
  398. Any of the three systems can be targeted by setting the
  399. :variable:`CMAKE_SYSTEM_NAME` variable to a value from the table below.
  400. By default, the latest Device SDK is chosen. As for all Apple platforms,
  401. a different SDK (e.g. a simulator) can be selected by setting the
  402. :variable:`CMAKE_OSX_SYSROOT` variable, although this should rarely be
  403. necessary (see :ref:`Switching Between Device and Simulator` below).
  404. A list of available SDKs can be obtained by running ``xcodebuild -showsdks``.
  405. ======= ================= ==================== ================
  406. OS CMAKE_SYSTEM_NAME Device SDK (default) Simulator SDK
  407. ======= ================= ==================== ================
  408. iOS iOS iphoneos iphonesimulator
  409. tvOS tvOS appletvos appletvsimulator
  410. watchOS watchOS watchos watchsimulator
  411. ======= ================= ==================== ================
  412. For example, to create a CMake configuration for iOS, the following
  413. command is sufficient:
  414. .. code-block:: console
  415. cmake .. -GXcode -DCMAKE_SYSTEM_NAME=iOS
  416. Variable :variable:`CMAKE_OSX_ARCHITECTURES` can be used to set architectures
  417. for both device and simulator. Variable :variable:`CMAKE_OSX_DEPLOYMENT_TARGET`
  418. can be used to set an iOS/tvOS/watchOS deployment target.
  419. Next configuration will install fat 5 architectures iOS library
  420. and add the ``-miphoneos-version-min=9.3``/``-mios-simulator-version-min=9.3``
  421. flags to the compiler:
  422. .. code-block:: console
  423. $ cmake -S. -B_builds -GXcode \
  424. -DCMAKE_SYSTEM_NAME=iOS \
  425. "-DCMAKE_OSX_ARCHITECTURES=armv7;armv7s;arm64;i386;x86_64" \
  426. -DCMAKE_OSX_DEPLOYMENT_TARGET=9.3 \
  427. -DCMAKE_INSTALL_PREFIX=`pwd`/_install \
  428. -DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO \
  429. -DCMAKE_IOS_INSTALL_COMBINED=YES
  430. Example:
  431. .. code-block:: cmake
  432. # CMakeLists.txt
  433. cmake_minimum_required(VERSION 3.14)
  434. project(foo)
  435. add_library(foo foo.cpp)
  436. install(TARGETS foo DESTINATION lib)
  437. Install:
  438. .. code-block:: console
  439. $ cmake --build _builds --config Release --target install
  440. Check library:
  441. .. code-block:: console
  442. $ lipo -info _install/lib/libfoo.a
  443. Architectures in the fat file: _install/lib/libfoo.a are: i386 armv7 armv7s x86_64 arm64
  444. .. code-block:: console
  445. $ otool -l _install/lib/libfoo.a | grep -A2 LC_VERSION_MIN_IPHONEOS
  446. cmd LC_VERSION_MIN_IPHONEOS
  447. cmdsize 16
  448. version 9.3
  449. Code Signing
  450. ^^^^^^^^^^^^
  451. Some build artifacts for the embedded Apple platforms require mandatory
  452. code signing. If the :generator:`Xcode` generator is being used and
  453. code signing is required or desired, the development team ID can be
  454. specified via the ``CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM`` CMake variable.
  455. This team ID will then be included in the generated Xcode project.
  456. By default, CMake avoids the need for code signing during the internal
  457. configuration phase (i.e compiler ID and feature detection).
  458. .. _`Switching Between Device and Simulator`:
  459. Switching Between Device and Simulator
  460. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  461. When configuring for any of the embedded platforms, one can target either
  462. real devices or the simulator. Both have their own separate SDK, but CMake
  463. only supports specifying a single SDK for the configuration phase. This
  464. means the developer must select one or the other at configuration time.
  465. When using the :generator:`Xcode` generator, this is less of a limitation
  466. because Xcode still allows you to build for either a device or a simulator,
  467. even though configuration was only performed for one of the two. From
  468. within the Xcode IDE, builds are performed for the selected "destination"
  469. platform. When building from the command line, the desired sdk can be
  470. specified directly by passing a ``-sdk`` option to the underlying build
  471. tool (``xcodebuild``). For example:
  472. .. code-block:: console
  473. $ cmake --build ... -- -sdk iphonesimulator
  474. Please note that checks made during configuration were performed against
  475. the configure-time SDK and might not hold true for other SDKs. Commands
  476. like :command:`find_package`, :command:`find_library`, etc. store and use
  477. details only for the configured SDK/platform, so they can be problematic
  478. if wanting to switch between device and simulator builds. You can follow
  479. the next rules to make device + simulator configuration work:
  480. - Use explicit ``-l`` linker flag,
  481. e.g. ``target_link_libraries(foo PUBLIC "-lz")``
  482. - Use explicit ``-framework`` linker flag,
  483. e.g. ``target_link_libraries(foo PUBLIC "-framework CoreFoundation")``
  484. - Use :command:`find_package` only for libraries installed with
  485. :variable:`CMAKE_IOS_INSTALL_COMBINED` feature