cmake-toolchains.7.rst 6.3 KB

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