cmake-toolchains.7.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. If no project command
  17. is in the top-level CMakeLists file, one will be implicitly generated. By default
  18. the enabled languages are C and CXX:
  19. .. code-block:: cmake
  20. project(C_Only C)
  21. A special value of NONE can also be used with the :command:`project` command
  22. to enable no languages:
  23. .. code-block:: cmake
  24. project(MyProject NONE)
  25. The :command:`enable_language` command can be used to enable languages after the
  26. :command:`project` command:
  27. .. code-block:: cmake
  28. enable_language(CXX)
  29. When a language is enabled, CMake finds a compiler for that language, and
  30. determines some information, such as the vendor and version of the compiler,
  31. the target architecture and bitwidth, the location of corresponding utilities
  32. etc.
  33. The :prop_gbl:`ENABLED_LANGUAGES` global property contains the languages which
  34. are currently enabled.
  35. Variables and Properties
  36. ========================
  37. Several variables relate to the language components of a toolchain which are
  38. enabled. :variable:`CMAKE_<LANG>_COMPILER` is the full path to the compiler used
  39. for ``<LANG>``. :variable:`CMAKE_<LANG>_COMPILER_ID` is the identifier used
  40. by CMake for the compiler and :variable:`CMAKE_<LANG>_COMPILER_VERSION` is the
  41. version of the compiler.
  42. The :variable:`CMAKE_<LANG>_FLAGS` variables and the configuration-specific
  43. equivalents contain flags that will be added to the compile command when
  44. compiling a file of a particular language.
  45. As the linker is invoked by the compiler driver, CMake needs a way to determine
  46. which compiler to use to invoke the linker. This is calculated by the
  47. :prop_sf:`LANGUAGE` of source files in the target, and in the case of static
  48. libraries, the language of the dependent libraries. The choice CMake makes may
  49. be overridden with the :prop_tgt:`LINKER_LANGUAGE` target property.
  50. Toolchain Features
  51. ==================
  52. CMake provides the :command:`try_compile` command and wrapper macros such as
  53. :module:`CheckCXXSourceCompiles`, :module:`CheckCXXSymbolExists` and
  54. :module:`CheckIncludeFile` to test capability and availability of various
  55. toolchain features. These APIs test the toolchain in some way and cache the
  56. result so that the test does not have to be performed again the next time
  57. CMake runs.
  58. Some toolchain features have built-in handling in CMake, and do not require
  59. compile-tests. For example, :prop_tgt:`POSITION_INDEPENDENT_CODE` allows
  60. specifying that a target should be built as position-independent code, if
  61. the compiler supports that feature. The :prop_tgt:`<LANG>_VISIBILITY_PRESET`
  62. and :prop_tgt:`VISIBILITY_INLINES_HIDDEN` target properties add flags for
  63. hidden visibility, if supported by the compiler.
  64. Cross Compiling
  65. ===============
  66. If :manual:`cmake(1)` is invoked with the command line parameter
  67. ``-DCMAKE_TOOLCHAIN_FILE=path/to/file``, the file will be loaded early to set
  68. values for the compilers. A typical cross-compiling toolchain has content such
  69. as:
  70. .. code-block:: cmake
  71. set(CMAKE_SYSTEM_NAME Linux)
  72. set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
  73. set(CMAKE_STAGING_PREFIX /home/devel/stage)
  74. set(CMAKE_C_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc)
  75. set(CMAKE_CXX_COMPILER /home/devel/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-g++)
  76. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  77. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  78. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  79. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  80. The :variable:`CMAKE_SYSTEM_NAME` is the CMake-identifier of the target platform
  81. to build for.
  82. The :variable:`CMAKE_SYSROOT` is optional, and may be specified if a sysroot
  83. is available.
  84. The :variable:`CMAKE_STAGING_PREFIX` is also optional. It may be used to specify
  85. a path on the host to install to. The :variable:`CMAKE_INSTALL_PREFIX` is always
  86. the runtime installation location, even when cross-compiling.
  87. The :variable:`CMAKE_<LANG>_COMPILER` variables may be set to full paths, or to
  88. names of compilers to search for in standard locations. In cases where CMake does
  89. not have enough information to extract information from the compiler, the
  90. :module:`CMakeForceCompiler` module can be used to bypass some of the checks.
  91. CMake ``find_*`` commands will look in the sysroot, and the :variable:`CMAKE_FIND_ROOT_PATH`
  92. entries by default in all cases, as well as looking in the host system root prefix.
  93. Although this can be controlled on a case-by-case basis, when cross-compiling, it
  94. can be useful to exclude looking in either the host or the target for particular
  95. artifacts. Generally, includes, libraries and packages should be found in the
  96. target system prefixes, whereas executables which must be run as part of the build
  97. should be found only on the host and not on the target. This is the purpose of
  98. the ``CMAKE_FIND_ROOT_PATH_MODE_*`` variables.
  99. Some compilers are inherently cross compilers, such as Clang and the QNX QCC
  100. compiler. The :variable:`CMAKE_<LANG>_COMPILER_TARGET` can be set to pass a
  101. value to those supported compilers when compiling:
  102. .. code-block:: cmake
  103. set(CMAKE_SYSTEM_NAME Linux)
  104. set(triple arm-linux-gnueabihf)
  105. set(CMAKE_C_COMPILER clang)
  106. set(CMAKE_C_COMPILER_TARGET ${triple})
  107. set(CMAKE_CXX_COMPILER clang++)
  108. set(CMAKE_CXX_COMPILER_TARGET ${triple})
  109. Or, for QCC:
  110. .. code-block:: cmake
  111. set(CMAKE_SYSTEM_NAME QNX)
  112. set(arch gcc_ntoarmv7le)
  113. set(CMAKE_C_COMPILER qcc)
  114. set(CMAKE_C_COMPILER_TARGET ${arch})
  115. set(CMAKE_CXX_COMPILER QCC)
  116. set(CMAKE_CXX_COMPILER_TARGET ${arch})
  117. Similarly, some compilers do not ship their own supplementary utilities
  118. such as linkers, but provide a way to specify the location of the external
  119. toolchain which will be used by the compiler driver. The
  120. :variable:`CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN` variable can be set in a
  121. toolchain file to pass the path to the compiler driver.
  122. The :variable:`CMAKE_CROSSCOMPILING` variable is set to true when CMake is
  123. cross-compiling.