cmake-toolchains.7.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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
  69. ===============
  70. If :manual:`cmake(1)` is invoked with the command line parameter
  71. ``-DCMAKE_TOOLCHAIN_FILE=path/to/file``, the file will be loaded early to set
  72. values for the compilers. A typical cross-compiling toolchain has content such
  73. as:
  74. .. code-block:: cmake
  75. set(CMAKE_SYSTEM_NAME Linux)
  76. set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
  77. set(CMAKE_STAGING_PREFIX /home/devel/stage)
  78. set(CMAKE_C_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc)
  79. set(CMAKE_CXX_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++)
  80. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  81. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  82. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  83. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  84. The :variable:`CMAKE_SYSTEM_NAME` is the CMake-identifier of the target platform
  85. to build for.
  86. The :variable:`CMAKE_SYSROOT` is optional, and may be specified if a sysroot
  87. is available.
  88. The :variable:`CMAKE_STAGING_PREFIX` is also optional. It may be used to specify
  89. a path on the host to install to. The :variable:`CMAKE_INSTALL_PREFIX` is always
  90. the runtime installation location, even when cross-compiling.
  91. The :variable:`CMAKE_<LANG>_COMPILER` variables may be set to full paths, or to
  92. names of compilers to search for in standard locations. In cases where CMake does
  93. not have enough information to extract information from the compiler, the
  94. :module:`CMakeForceCompiler` module can be used to bypass some of the checks.
  95. CMake ``find_*`` commands will look in the sysroot, and the :variable:`CMAKE_FIND_ROOT_PATH`
  96. entries by default in all cases, as well as looking in the host system root prefix.
  97. Although this can be controlled on a case-by-case basis, when cross-compiling, it
  98. can be useful to exclude looking in either the host or the target for particular
  99. artifacts. Generally, includes, libraries and packages should be found in the
  100. target system prefixes, whereas executables which must be run as part of the build
  101. should be found only on the host and not on the target. This is the purpose of
  102. the ``CMAKE_FIND_ROOT_PATH_MODE_*`` variables.
  103. Some compilers are inherently cross compilers, such as Clang and the QNX QCC
  104. compiler. The :variable:`CMAKE_<LANG>_COMPILER_TARGET` can be set to pass a
  105. value to those supported compilers when compiling:
  106. .. code-block:: cmake
  107. set(CMAKE_SYSTEM_NAME Linux)
  108. set(triple arm-linux-gnueabihf)
  109. set(CMAKE_C_COMPILER clang)
  110. set(CMAKE_C_COMPILER_TARGET ${triple})
  111. set(CMAKE_CXX_COMPILER clang++)
  112. set(CMAKE_CXX_COMPILER_TARGET ${triple})
  113. Or, for QCC:
  114. .. code-block:: cmake
  115. set(CMAKE_SYSTEM_NAME QNX)
  116. set(arch gcc_ntoarmv7le)
  117. set(CMAKE_C_COMPILER qcc)
  118. set(CMAKE_C_COMPILER_TARGET ${arch})
  119. set(CMAKE_CXX_COMPILER QCC)
  120. set(CMAKE_CXX_COMPILER_TARGET ${arch})
  121. Similarly, some compilers do not ship their own supplementary utilities
  122. such as linkers, but provide a way to specify the location of the external
  123. toolchain which will be used by the compiler driver. The
  124. :variable:`CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN` variable can be set in a
  125. toolchain file to pass the path to the compiler driver.
  126. The :variable:`CMAKE_CROSSCOMPILING` variable is set to true when CMake is
  127. cross-compiling.