如何指示CMake使用构建体系结构编译器 [英] How to instruct CMake to use the build architecture compiler

查看:71
本文介绍了如何指示CMake使用构建体系结构编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CMake进行交叉编译时,通常通过 CMAKE_TOOLCHAIN_FILE 选项指定一个工具链文件。在 GNU术语中,可以使用以下方法指定主机体系结构工具集这个文件。但是,通常不能指望能够执行使用此工具链构建的任何内容。很多时候,需要为构建体系结构编译一些构建工具。

When using CMake for cross compiling, one generally specifies a toolchain file via the CMAKE_TOOLCHAIN_FILE option. In GNU terminology, one can specify the host architecture toolset using this file. However, one can generally not expect to be able to execute anything built with this toolchain. So often enough, some build tools need to be compiled for the build architecture.

请考虑以下设置。我有两个源文件 genfoo.c bar.c 。在构建期间,需要编译并运行 genfoo.c 。它的输出需要写入 foo.h 。然后,我可以编译 bar.c ,其中 #include foo.h 。由于CMake默认使用主机体系结构工具链,因此 bar.c 的说明很简单。但是我如何告诉它使用构建体系结构工具链来编译 genfoo.c ?简单地说 add_executable(genfoo genfoo.c)会导致使用错误的编译器。

Consider the following setup. I have two source files genfoo.c and bar.c. During build, genfoo.c needs to be compiled and run. Its output needs to be written to foo.h. Then I can compile bar.c, which #include "foo.h". Since CMake defaults to using the host architecture toolchain, the instructions for bar.c are easy. But how do I tell it to use the build architecture toolchain for compiling genfoo.c? Simply saying add_executable(genfoo genfoo.c) will result in using the wrong compiler.

推荐答案

CMake一次只能处理一个编译器。所以-如果您不花太多时间将另一个编译器设置为新语言-您将得到两个配置周期。

CMake can only handle one compiler at a time. So - if you don't go the long way to set up the other compiler as a new language - you will end up with two configuration cycles.

我看到以下内容自动执行此过程的方法:

I see the following approaches to automate this process:


  1. 以示例 CMake交叉编译-使用在生成期间创建的可执行文件吗? 从CMake页面开始,我会得到:

  1. Taking the example "CMake Cross Compiling - Using executables in the build created during the build?" from the CMake pages as a starting point I'll get:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(FooBarTest)

# When crosscompiling import the executable targets
if (CMAKE_CROSSCOMPILING)
    set(IMPORT_PATH "IMPORTFILE-NOTFOUND" CACHE FILEPATH "Point it to the export file path from a native build")
    file(TO_CMAKE_PATH "${IMPORT_PATH}" IMPORT_PATH_CMAKE)
    include(${IMPORT_PATH_CMAKE}/genfooTargets.cmake)

    # Then use the target name as COMMAND, CMake >= 2.6 knows how to handle this
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.h
        COMMAND genfoo
    )

    add_executable(bar bar.cpp ${CMAKE_CURRENT_BINARY_DIR}/foo.h)
    target_include_directories(bar PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
endif()

# Only build the generator if not crosscompiling
if (NOT CMAKE_CROSSCOMPILING)
    add_executable(genfoo genfoo.cpp)
    export(TARGETS genfoo FILE "${CMAKE_CURRENT_BINARY_DIR}/genfooTargets.cmake")
endif()

然后使用类似以下的脚本:

Then using a script like:

build.sh

#!/bin/bash

if [ ! -d hostBuild ]; then
    cmake -E make_directory hostBuild
    cmake -E chdir hostBuild cmake ..
fi
cmake --build hostBuild

if [ ! -d crossBuild ]; then
    cmake -E make_directory crossBuild
    cmake -E chdir crossBuild cmake .. -DIMPORT_PATH=${PWD}/hostBuild -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake
fi
cmake --build crossBuild

我将通过调用获得所需的结果。/build.sh

拆分 CMakeLists.txt 甚至替换 export() / include()和一些我知道构建工具输出路径的地方,例如通过使用 CMAKE_RUNTIME_OUTPUT_DIRECTORY 可以简化操作:

Splitting the CMakeLists.txt and maybe even replace the export()/include() with something where I know the output path of my build tools e.g. by using CMAKE_RUNTIME_OUTPUT_DIRECTORY would simplify things:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(FooBarTest)

# Then use the target name as COMMAND. CMake >= 2.6 knows how to handle this
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.h
    COMMAND genfoo
)

add_executable(bar bar.cpp ${CMAKE_CURRENT_BINARY_DIR}/foo.h)
target_include_directories(bar PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

buildTools / CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(BuildTools)

add_executable(genfoo genfoo.cpp)

build.sh

#!/bin/bash

if [ ! -d crossBuild ]; then
    cmake -E make_directory crossBuild
    cmake -E chdir crossBuild cmake .. -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake
fi
if [ ! -d hostBuild ]; then
    cmake -E make_directory hostBuild
    cmake -E chdir hostBuild cmake ../buildTools -DCMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH=${PWD}/crossBuild
fi
cmake --build hostBuild
cmake --build crossBuild


参考文献

  • Making a CMake library accessible by other CMake packages automatically
  • CMake build multiple targets in different build directories
  • How do I make CMake output into a 'bin' dir?

这篇关于如何指示CMake使用构建体系结构编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆