命令
project()
enable_language()
try_compile()

变量
CMAKE_<LANG>_COMPILER
CMAKE_<LANG>_COMPILER_ID
CMAKE_<LANG>_COMPILER_VERSION
CMAKE_<LANG>_FLAGS
CMAKE_TOOLCHAIN_FILE
CMAKE_CROSSCOMPILING
CMAKE_SYSTEM_NAME
CMAKE_SYSTEM_PROCESSOR
CMAKE_SYSROOT
CMAKE_STAGING_PREFIX
CMAKE_FIND_ROOT_PATH
find_*
CMAKE_FIND_ROOT_PATH_MODE_*
CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN
CMAKE_<LANG>_COMPILER_TARGET
CMAKE_GENERATOR_PLATFORM
CMAKE_SYSTEM_VERSION
CMAKE_GENERATOR_TOOLSET

属性
ENABLED_LANGUAGES
LINKER_LANGUAGE
POSITION_INDEPENDENT_CODE
<LANG>_VISIBILITY_PRESET
VISIBILITY_INLINES_HIDDEN


CheckCXXSourceCompiles
CheckCXXSymbolExists
CheckIncludeFile

一些参考
CMAKE_CXX_COMPILER
CMAKE_CXX_COMPILER_ID
CMAKE_CXX_COMPILER_TARGET
CMAKE_C_COMPILER
CMAKE_CXX_COMPILER_ID
CMAKE_CXX_COMPILER_TARGET
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE

Introduction

CMake uses a toolchain of utilities to compile, link libraries and create archives, and other tasks to drive the build. The toolchain utilities available are determined by the languages enabled. In normal builds, CMake automatically determines the toolchain for host builds based on system introspection and defaults. In cross-compiling scenarios, a toolchain file may be specified with information about compiler and utility paths.

CMake使用(实用工具)工具链编译、链接库和创建归档,和其它任务去驱动构建。启用的(编程)语言决定工具链工具。在常规构建中,CMake基于系统自检和缺省自动为宿主构建决定工具链。在交叉编译的情况下,可以通过相关的编译器和工具路径来指定工具链文件。

Languages

Languages are enabled by the project() command. Language-specific built-in variables, such as CMAKE_CXX_COMPILER, CMAKE_CXX_COMPILER_ID etc are set by invoking the project() command. If no project command is in the top-level CMakeLists file, one will be implicitly generated. By default the enabled languages are C and CXX:

语言是由project()命令启用的。指定语言的内置变量(如CMAKE_CXX_COMPILER\CMAKE_CXX_COMPILER_ID等)在调用project()命令时被设置。如果在顶层的CMakeLists文件中没有project()命令,将生成一个隐式的。缺省启用的语言是C和CXX:

project(C_Only C)

A special value of NONE can also be used with the project() command to enable no languages:
在 project() 命令中指定 NONE 将不启用任何语言:

project(MyProject NONE)

The enable_language() command can be used to enable languages after the project() command:
在 project() 命令之后,可以使用 enable_language() 命令来启用语言:

enable_language(CXX)

When a language is enabled, CMake finds a compiler for that language, and determines some information, such as the vendor and version of the compiler, the target architecture and bitwidth, the location of corresponding utilities etc.

当一个语言被启用,CMake为该语言查找编译器,决定一些信息,如编译器的供应商和版本,目标架构和位宽,相应工具的位置等。

The ENABLED_LANGUAGES global property contains the languages which are currently enabled.

全局属性ENABLED_LANGUAGES包含当前启用的语言。

Variables and Properties

Several variables relate to the language components of a toolchain which are enabled. CMAKE_<LANG>_COMPILER is the full path to the compiler used for <LANG>. CMAKE_<LANG>_COMPILER_ID is the identifier used by CMake for the compiler and CMAKE_<LANG>_COMPILER_VERSION is the version of the compiler.

几个关于工具链条的语言组件的变量被启用。CMAKE_<LANG>_COMPILER 是用于 <LANG> 编译器的全路径。CMake使用 CMAKE_<LANG>_COMPILER_ID 作为编译器识别,CMAKE_<LANG>_COMPILER_VERSION 是编译器的版本。

The CMAKE_<LANG>_FLAGS variables and the configuration-specific equivalents contain flags that will be added to the compile command when compiling a file of a particular language.

CMAKE_<LANG>_FLAGS 变量和对应的具体配置包含将被添加到编译命令的标志,在特定语言编译文件时。

As the linker is invoked by the compiler driver, CMake needs a way to determine which compiler to use to invoke the linker. This is calculated by the LANGUAGE of source files in the target, and in the case of static libraries, the language of the dependent libraries. The choice CMake makes may be overridden with the LINKER_LANGUAGE target property.

连接器被编译驱动器调用。CMake需要一个方法去决定使用哪个编译器去调用链接器。这个通过目标中的源文件的语言来计算,在静态库的情况下,也通过依赖库的语言来计算。CMake做出的选择可能被目标属性 LINKER_LANGUAGE 重写。

Toolchain Features

CMake provides the try_compile() command and wrapper macros such as CheckCXXSourceCompiles, CheckCXXSymbolExists and CheckIncludeFile to test capability and availability of various toolchain features. These APIs test the toolchain in some way and cache the result so that the test does not have to be performed again the next time CMake runs.

CMake提供 try_compile() 命令和宏测试各种工具链功能的性能和有效性,比如 CheckCXXSourceCompiles, CheckCXXSymbolExists 和 CheckIncludeFile。这些 API 测试工具链使用一些方法和缓存结果,使测试不一定在下一次CMake运行时再次被执行。

Some toolchain features have built-in handling in CMake, and do not require compile-tests. For example, POSITION_INDEPENDENT_CODE allows specifying that a target should be built as position-independent code, if the compiler supports that feature. The <LANG>_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN target properties add flags for hidden visibility, if supported by the compiler.

一些工具链功能在CMake中有内置处理,不请求编译测试。例如,POSITION_INDEPENDENT_CODE 允许指定目标被构建作为位置无关代码,如果编译器支持此项特性。目标属性 <LANG>_VISIBILITY_PRESET 和 VISIBILITY_INLINES_HIDDEN 添加标志以隐藏可见性,如果编译器支持此功能。

Cross Compiling

If cmake(1) is invoked with the command line parameter -DCMAKE_TOOLCHAIN_FILE=path/to/file, the file will be loaded early to set values for the compilers. The CMAKE_CROSSCOMPILING variable is set to true when CMake is cross-compiling.

如果cmake被调用携带命令行参数 -DCMAKE_TOOLCHAIN_FILE=path/to/file,文件将被提前加载以为编译器设置值。在交叉编译时CMAKE_CROSSCOMPILING变量将被设置为真值。

Cross Compiling for Linux

A typical cross-compiling toolchain for Linux has content such as:

一个典型的linux下的交叉编译工具链内容如下:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(CMAKE_STAGING_PREFIX /home/devel/stage)

set(tools /home/devel/gcc-4.7-linaro-rpi-gnueabihf)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

The CMAKE_SYSTEM_NAME is the CMake-identifier of the target platform to build for.

CMAKE_SYSTEM_NAME 是将要构建的目标平台的CMake识别。

The CMAKE_SYSTEM_PROCESSOR is the CMake-identifier of the target architecture to build for.

CMAKE_SYSTEM_PROCESSOR 是将要构建的目标架构的CMake识别。

The CMAKE_SYSROOT is optional, and may be specified if a sysroot is available.

CMAKE_SYSROOT 是一个选项,在sysroot有效时被指定。

The CMAKE_STAGING_PREFIX is also optional. It may be used to specify a path on the host to install to. The CMAKE_INSTALL_PREFIX is always the runtime installation location, even when cross-compiling.

CMAKE_STAGING_PREFIX 也是一个选项。可以指定宿主机的安装路径。CMAKE_INSTALL_PREFIX指定运行时安装位置,即时在交叉编译环境下。

The CMAKE_<LANG>_COMPILER variables may be set to full paths, or to names of compilers to search for in standard locations. In cases where CMake does not have enough information to extract information from the compiler, the CMakeForceCompiler module can be used to bypass some of the checks.

CMAKE_<LANG>_COMPILER 变量可以是全路径,或者是编译器将要查找的标准位置。一旦CMake不能从编译器提取到足够的信息,CMakeForceCompiler 模块可以做一些旁路检查。

CMake find_* commands will look in the sysroot, and the CMAKE_FIND_ROOT_PATH entries by default in all cases, as well as looking in the host system root prefix. Although this can be controlled on a case-by-case basis, when cross-compiling, it can be useful to exclude looking in either the host or the target for particular artifacts. Generally, includes, libraries and packages should be found in the target system prefixes, whereas executables which must be run as part of the build should be found only on the host and not on the target. This is the purpose of the CMAKE_FIND_ROOT_PATH_MODE_* variables.

find_*命令将在sysroot中查找,CMAKE_FIND_ROOT_PATH 在所有情况下是缺省的条目,包括查找宿主系统的根前缀。虽然基于不同的情况这可以被控制,当交叉编译时,它在排除查找主机或者特定工件的目标时是有用的。通常,包含文件,库文件和包在目标系统预处理时被发现,而那些作为构建的一部分的必须运行的可执行程序仅仅在宿主和非目标上被发现。这是 CMAKE_FIND_ROOT_PATH_MODE_* 变量的目的。

Cross Compiling using Clang

Some compilers such as Clang are inherently cross compilers. The CMAKE_<LANG>_COMPILER_TARGET can be set to pass a value to those supported compilers when compiling:

一些编译器是交叉编译固有的,如 Clang。当编译时,可以设置 CMAKE_<LANG>_COMPILER_TARGET 去传递值到支持的编译器:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(triple arm-linux-gnueabihf)

set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${triple})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${triple})

Similarly, some compilers do not ship their own supplementary utilities such as linkers, but provide a way to specify the location of the external toolchain which will be used by the compiler driver. The CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN variable can be set in a toolchain file to pass the path to the compiler driver.
同样,有些编译器不传送他们自己的辅助工具,像连接器啊,但是提供方法来指定外部工具链的位置供编译驱动器使用。可以在工具链文件设置 CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN 变量去传递路径给编译驱动。

Cross Compiling for QNX

As the Clang compiler the QNX QCC compile is inherently a cross compiler. And the CMAKE_<LANG>_COMPILER_TARGET can be set to pass a value to those supported compilers when compiling:

像Clang编译器一样,QNX QCC 编译器也是一个跨平台编译器。当编译时,可以设置 CMAKE_<LANG>_COMPILER_TARGET 去传递值到支持的编译器:

set(CMAKE_SYSTEM_NAME QNX)

set(arch gcc_ntoarmv7le)

set(CMAKE_C_COMPILER qcc)
set(CMAKE_C_COMPILER_TARGET ${arch})
set(CMAKE_CXX_COMPILER QCC)
set(CMAKE_CXX_COMPILER_TARGET ${arch})

Cross Compiling for Windows CE

Cross compiling for Windows CE requires the corresponding SDK being installed on your system. These SDKs are usually installed under C:/Program Files (x86)/Windows CE Tools/SDKs.

Windows CE的跨平台编译需要系统上装有对应的SDK。这些SDK通常安装在 C:/Program Files (x86)/Windows CE Tools/SDKs 。

A toolchain file to configure a Visual Studio generator for Windows CE may look like this:

为 Windows CE 配置 Visual Studio 生成器的工具链文件,如下:

set(CMAKE_SYSTEM_NAME WindowsCE)

set(CMAKE_SYSTEM_VERSION 8.0)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_GENERATOR_TOOLSET CE800) # Can be omitted for 8.0
set(CMAKE_GENERATOR_PLATFORM SDK_AM335X_SK_WEC2013_V310)

The CMAKE_GENERATOR_PLATFORM tells the generator which SDK to use. Further CMAKE_SYSTEM_VERSION tells the generator what version of Windows CE to use. Currently version 8.0 (Windows Embedded Compact 2013) is supported out of the box. Other versions may require one to set CMAKE_GENERATOR_TOOLSET to the correct value.

CMAKE_GENERATOR_PLATFORM 告诉生成器使用哪一个SDK。此外CMAKE_SYSTEM_VERSION告诉生成器Windows CE的版本。目前版本8.0(Windows Embedded Compact 2013)支持即开即用。其它版本可能需要给 CMAKE_GENERATOR_TOOLSET 设置正确的值。

Cross Compiling for Windows Phone

A toolchain file to configure a Visual Studio generator for Windows Phone may look like this:

为 Windows Phone 配置 Visual Studio 生成器的工具链文件,如下:

set(CMAKE_SYSTEM_NAME WindowsPhone)
set(CMAKE_SYSTEM_VERSION 8.1)

Cross Compiling for Windows Store

A toolchain file to configure a Visual Studio generator for Windows Store may look like this:

为 Windows Store 配置 Visual Studio 生成器的工具链文件,如下:

set(CMAKE_SYSTEM_NAME WindowsStore)
set(CMAKE_SYSTEM_VERSION 8.1)

Cross Compiling using NVIDIA Nsight Tegra

A toolchain file to configure a Visual Studio generator to build using NVIDIA Nsight Tegra targeting Android may look like this:

使用 NVIDIA Nsight Tegra 的 Android,配置 Visual Studio 生成器的工具链文件如下:

set(CMAKE_SYSTEM_NAME Android)

The CMAKE_GENERATOR_TOOLSET may be set to select the Nsight Tegra “Toolchain Version” value.

可以设置 CMAKE_GENERATOR_TOOLSET 为 Nsight Tegra “Toolchain Version” 值。

See the ANDROID_API_MIN, ANDROID_API and ANDROID_GUI target properties to configure targets within the project.

参考 ANDROID_API_MIN,ANDROID_API 和 ANDROID_GUI 目标属性配置项目内的目标。

cmake工具链的更多相关文章

  1. nRF5 SDK for Mesh(三) Installing the mesh toolchain 安装编译工具链

    Installing the mesh toolchain To build the example applications, a toolchain based on either CMake o ...

  2. 编译工具链,生成各个平台的ffmpeg版本的库

    1.在开始动手编译ffmpeg之前我们来梳理一下几个概念,gcc.g++.msvc.mingw.clang.cmake.make.qmake 作为一个windows软件工程师,以为长时间浸淫在各种强大 ...

  3. xmake v2.3.7 发布, 新增 tinyc 和 emscripten 工具链支持

    xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...

  4. xmake v2.5.2 发布, 支持自动拉取交叉工具链和依赖包集成

    xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...

  5. 字节跳动 DanceCC 工具链系列之Xcode LLDB耗时监控统计方案

    作者:李卓立 仲凯宁 背景介绍 在<字节跳动 DanceCC 工具链系列之Swift 调试性能的优化方案>[1]一文中,我们介绍了如何使用自定义的工具链,来针对性优化调试器的性能,解决大型 ...

  6. HK32F030MF4P6的Linux GCC工具链和VSCode开发环境

    HK32F030MF4P6简介 航顺的 HK32F030MF4P6, TSSOP20封装, Arm Cortex M0 内核, 内建32MHz时钟, 16K Flash, 2K RAM(实际上可用的有 ...

  7. X86上搭建交叉工具链,来给龙芯笔记本编译本地工具链(未完待续)

    故事的背景是,我买了一台龙芯2F的笔记本来装B. 为什么说是装B呢?因为不但操作系统是Linux,而且CPU还是龙芯的. 一般人有这么酷的装备吗?简直是装B大圣啊. 这里一定要申明一点,本人不是IT技 ...

  8. Atitit 项目管理(5)----------后勤管理与工具链支持管理

    Atitit 项目管理(5)----------后勤管理与工具链支持管理 1.1. keyword1 1.2. 15个辅助软件1 1.3. 公共模块管理(100个即可)2 1.4. 第三方类库表2 1 ...

  9. gcc工具链简述

    工具链软件包括BINUTILS.GCC.GLIBC.GDB等. BINUTILS是二进制程序处理工具,包括链接器.汇编器等目标程序处理的工具. GCC(GNU Compiler Collection) ...

随机推荐

  1. php --with-mysql=mysqlnd

    1.什么是mysqlnd驱动? PHP手册上的描述: MySQL Native Driver is a replacement for the MySQL Client Library (libmys ...

  2. iOS开发——获取当前屏幕显示的viewcontroller

    获取当前屏幕显示的viewcontroller,然后想怎么跳就怎么跳. - (UIViewController *)getCurrentVC { UIViewController *result = ...

  3. Laravel邮件发送问题小解

    在Laravel中已经有内置了发送邮件的功能,通过 Mail::send 可发送邮件,但要使用这个函数必须先进行相关配置. 在 /app/config/mail.php 中设置你的邮件参数,如下: 1 ...

  4. Android线程之异步消息处理机制(二)——Message、Handler、MessageQueue和Looper

    异步消息处理机制解析 Android中的异步消息处理主要有四个部分组成,Message.Handler.MessageQueue和Looper. 1.Message Message是在线程之间传递的消 ...

  5. ios 简单的plist文件读写操作(Document和NSUserDefaults)

    最近遇到ios上文件读写操作的有关知识,记录下来,以便以后查阅,同时分享与大家. 一,简单介绍一下常用的plist文件. 全名是:Property List,属性列表文件,它是一种用来存储串行化后的对 ...

  6. C++中string类的使用方法

    如果所比较的两个string 相等,则返回0: 操作string 大于参数string,返回 正数:操作string 小于参数string,返回负数. (1) 比较操作string 与 _Str 或C ...

  7. 基于STM32的USB枚举过程学习笔记

    源:基于STM32的USB枚举过程学习笔记 基于STM32的USB枚举过程学习笔记(一) 基于STM32的USB枚举过程学习笔记(二) 基于STM32的USB枚举过程学习笔记(三) 基于STM32的U ...

  8. UVa 124 - Following Orders

    题目大意:给一个变量列表和变量的大小关系,输出所有的满足约束的序列. 构建为有向图,然后就是拓扑排序,使用回溯输出所有的结果. #include <cstdio> #include < ...

  9. 递归添加 另一个ds 里的DataRow 时 报错:该行已经属于另一个表。

    public void create_tree(DataSet ds, int parentid)        { DataSet newds = new DataSet();            ...

  10. 2782: [HNOI2006]最短母串

    2782: [HNOI2006]最短母串 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 3  Solved: 2[Submit][Status][Web ...