cmake-toolchains.7.rst 26 KB

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