cmake-toolchains.7.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. .. cmake-manual-description: CMake Toolchains Reference
  2. cmake-toolchains(7)
  3. *******************
  4. .. only:: html or latex
  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. Cross Compiling for Linux
  77. -------------------------
  78. A typical cross-compiling toolchain for Linux has content such
  79. as:
  80. .. code-block:: cmake
  81. set(CMAKE_SYSTEM_NAME Linux)
  82. set(CMAKE_SYSTEM_PROCESSOR arm)
  83. set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
  84. set(CMAKE_STAGING_PREFIX /home/devel/stage)
  85. set(CMAKE_C_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc)
  86. set(CMAKE_CXX_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++)
  87. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  88. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  89. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  90. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  91. The :variable:`CMAKE_SYSTEM_NAME` is the CMake-identifier of the target platform
  92. to build for.
  93. The :variable:`CMAKE_SYSTEM_PROCESSOR` is the CMake-identifier of the target architecture
  94. to build for.
  95. The :variable:`CMAKE_SYSROOT` is optional, and may be specified if a sysroot
  96. is available.
  97. The :variable:`CMAKE_STAGING_PREFIX` is also optional. It may be used to specify
  98. a path on the host to install to. The :variable:`CMAKE_INSTALL_PREFIX` is always
  99. the runtime installation location, even when cross-compiling.
  100. The :variable:`CMAKE_<LANG>_COMPILER` variables may be set to full paths, or to
  101. names of compilers to search for in standard locations. In cases where CMake does
  102. not have enough information to extract information from the compiler, the
  103. :module:`CMakeForceCompiler` module can be used to bypass some of the checks.
  104. CMake ``find_*`` commands will look in the sysroot, and the :variable:`CMAKE_FIND_ROOT_PATH`
  105. entries by default in all cases, as well as looking in the host system root prefix.
  106. Although this can be controlled on a case-by-case basis, when cross-compiling, it
  107. can be useful to exclude looking in either the host or the target for particular
  108. artifacts. Generally, includes, libraries and packages should be found in the
  109. target system prefixes, whereas executables which must be run as part of the build
  110. should be found only on the host and not on the target. This is the purpose of
  111. the ``CMAKE_FIND_ROOT_PATH_MODE_*`` variables.
  112. Cross Compiling using Clang
  113. ---------------------------
  114. Some compilers such as Clang are inherently cross compilers.
  115. The :variable:`CMAKE_<LANG>_COMPILER_TARGET` can be set to pass a
  116. value to those supported compilers when compiling:
  117. .. code-block:: cmake
  118. set(CMAKE_SYSTEM_NAME Linux)
  119. set(CMAKE_SYSTEM_PROCESSOR arm)
  120. set(triple arm-linux-gnueabihf)
  121. set(CMAKE_C_COMPILER clang)
  122. set(CMAKE_C_COMPILER_TARGET ${triple})
  123. set(CMAKE_CXX_COMPILER clang++)
  124. set(CMAKE_CXX_COMPILER_TARGET ${triple})
  125. Similarly, some compilers do not ship their own supplementary utilities
  126. such as linkers, but provide a way to specify the location of the external
  127. toolchain which will be used by the compiler driver. The
  128. :variable:`CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN` variable can be set in a
  129. toolchain file to pass the path to the compiler driver.
  130. Cross Compiling for QNX
  131. -----------------------
  132. As the Clang compiler the QNX QCC compile is inherently a cross compiler.
  133. And the :variable:`CMAKE_<LANG>_COMPILER_TARGET` can be set to pass a
  134. value to those supported compilers when compiling:
  135. .. code-block:: cmake
  136. set(CMAKE_SYSTEM_NAME QNX)
  137. set(arch gcc_ntoarmv7le)
  138. set(CMAKE_C_COMPILER qcc)
  139. set(CMAKE_C_COMPILER_TARGET ${arch})
  140. set(CMAKE_CXX_COMPILER QCC)
  141. set(CMAKE_CXX_COMPILER_TARGET ${arch})
  142. Cross Compiling for Windows CE
  143. ------------------------------
  144. Cross compiling for Windows CE requires the corresponding SDK being
  145. installed on your system. These SDKs are usually installed under
  146. ``C:/Program Files (x86)/Windows CE Tools/SDKs``.
  147. A toolchain file to configure a Visual Studio generator for
  148. Windows CE may look like this:
  149. .. code-block:: cmake
  150. set(CMAKE_SYSTEM_NAME WindowsCE)
  151. set(CMAKE_SYSTEM_VERSION 8.0)
  152. set(CMAKE_SYSTEM_PROCESSOR arm)
  153. set(CMAKE_GENERATOR_TOOLSET CE800) # Can be omitted for 8.0
  154. set(CMAKE_GENERATOR_PLATFORM SDK_AM335X_SK_WEC2013_V310)
  155. The :variable:`CMAKE_GENERATOR_PLATFORM` tells the generator which SDK to use.
  156. Further :variable:`CMAKE_SYSTEM_VERSION` tells the generator what version of
  157. Windows CE to use. Currently version 8.0 (Windows Embedded Compact 2013) is
  158. supported out of the box. Other versions may require one to set
  159. :variable:`CMAKE_GENERATOR_TOOLSET` to the correct value.
  160. Cross Compiling for Windows Phone
  161. ---------------------------------
  162. A toolchain file to configure a Visual Studio generator for
  163. Windows Phone may look like this:
  164. .. code-block:: cmake
  165. set(CMAKE_SYSTEM_NAME WindowsPhone)
  166. set(CMAKE_SYSTEM_VERSION 8.1)
  167. Cross Compiling for Windows Store
  168. ---------------------------------
  169. A toolchain file to configure a Visual Studio generator for
  170. Windows Store may look like this:
  171. .. code-block:: cmake
  172. set(CMAKE_SYSTEM_NAME WindowsStore)
  173. set(CMAKE_SYSTEM_VERSION 8.1)