如何从单独的文件下载一个工具链的交叉编译在cmake? [英] How to download a toolchain for cross compilation in cmake from separate file?

查看:661
本文介绍了如何从单独的文件下载一个工具链的交叉编译在cmake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目与CMakeLists.txt文件在根和项目编译良好在Linux和OSX。现在我想交叉编译为MIPS OpenWRT。
我想尽可能自动化,所以我将使用下面的代码下载工具链并设置编译器变量:

I have a project with a CMakeLists.txt files in the root and the project compiles fine on Linux and OSX. Now I want to cross compile it for MIPS OpenWRT. I would like to automate it as much as possible, so I would use following code to download the toolchain and set the compiler variables:

ExternalProject_Add(ar71xx-toolchain
    PREFIX "${PROJECT_BINARY_DIR}/external/openwrt"
    URL "http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2"
    UPDATE_COMMAND ""
    PATCH_COMMAND ""
    BUILD_COMMAND ""
    CONFIGURE_COMMAND ""
    INSTALL_COMMAND ""
)
ExternalProject_Get_Property(ar71xx-toolchain SOURCE_DIR)
SET(CMAKE_C_COMPILER ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc)
SET(CMAKE_CXX_COMPILER ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-g++)
SET(CMAKE_STRIP ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-strip)

我认为我可以把它放在一个单独的工具链文件,并传递它与-DCMAKE_TOOLCHAIN_FILE,但似乎ExternalProject_Add不是在工具链文件内执行。
我想避免将工具链下载步骤放入主要的CMakeLists.txt,因为它实际上不是项目本身的必要,并需要为每个目标平台做同样的
所以有一个方法来定义当前交叉编译构建的可选步骤,并以某种方式将其作为在主项目构建之前执行的命令行参数传递?

I thought that I can put it in a separate toolchain file and pass it with -DCMAKE_TOOLCHAIN_FILE, but it seems that ExternalProject_Add is not executed inside the toolchain file. I would like to avoid putting the toolchain download step into the main CMakeLists.txt since it's actually not essential for the project itself and would require doing the same for each target platform... So is there a way to define optional steps for a current cross compile build and pass it somehow as command line parameter to be executed before the main project build?

UPDATE: strong>
基于Tsyvarev在工具链文件中的答案:

UPDATE: Based on Tsyvarev's answer that works for me in the toolchain file:

set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_DIR ${PROJECT_BINARY_DIR}/external/openwrt/toolchain)

if(NOT EXISTS ${TOOLCHAIN_DIR})
    file(DOWNLOAD http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 ${TOOLCHAIN_DIR}/toolchain.tar.bz2 SHOW_PROGRESS)
    execute_process(COMMAND tar --strip-components=2 -xjf ${TOOLCHAIN_DIR}/toolchain.tar.bz2 WORKING_DIRECTORY ${TOOLCHAIN_DIR})
    execute_process(COMMAND rm ${TOOLCHAIN_DIR}/toolchain.tar.bz2)
endif()

SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-g++)
SET(CMAKE_STRIP ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-strip)
SET(CMAKE_FIND_ROOT_PATH  ${TOOLCHAIN_DIR})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

将-DCMAKE_TOOLCHAIN_FILE传递为CMAKE时,参数添加到ExternalProject_Add的其他项目。因为它自己的$ {PROJECT_BINARY_DIR},它会再次下载工具链。但这是另一个问题...

There is one issue when passing -DCMAKE_TOOLCHAIN_FILE as CMAKE parameter to other projects added with ExternalProject_Add. Because of it's own ${PROJECT_BINARY_DIR} it will download the toolchain again. But this is another problem...

推荐答案

ExternalProject_add 执行所有步骤时间

ExternalProject_add executes all steps at build time, not at configuration time.

对于下载文件,您可以使用档案 (DOWNLOAD ...)命令。对于来自arhive的提取文件,只需使用适当的命令 execute_process

For download file you can use file(DOWNLOAD ...) command. For extract files from arhive just use execute_process with appropriate command.

这篇关于如何从单独的文件下载一个工具链的交叉编译在cmake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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