本地编译并交叉编译相同的代码

本地编译并交叉编译相同的代码

本文介绍了CMake-本地编译并交叉编译相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为嵌入式ARM / Linux设备编写应用程序。使用交叉编译器Eclipse和Ninja在Windows PC上进行开发。 CMake当前可以创建可以很好地满足预期目的的构建脚本。

We're writing an application for an embedded ARM/Linux device. Development is performed on a Windows PC, using a crosscompiler, Eclipse and Ninja. CMake currently can create the build scripts that work well for the intended purpose.

一旦项目被推送,我们就可以在连接到网络的嵌入式设备上运行单元测试。 (通过Git)到服务器。

We have unit tests that run on the embedded device attached to the net, once the project is pushed (over Git) to the server.

我们正在尝试在PC上实施单元测试,然后再在设备上进行测试。这意味着使用MinGW GCC进行本地构建-当然,我们无法在PC上启动ARM Linux可执行文件。

We're trying to implement unit tests, that would run on the PC, before we try them on the device. That means building natively, using MinGW GCC - of course we can't launch the ARM Linux executables on the PC.

即使我们切换工具链,也可以启动CMake进行重建Ninja的规则集,或创建两个构建目录,一个用于PC,一个用于ARM,问题仍然在于CMake将尝试运行测试可执行文件,然后在构建过程中,将在ARM构建中尝试单元测试。

Even if we switch the toolchain, launching CMake to rebuild the ruleset for Ninja, or create two build directories, one for PC, one for ARM, the problem remains that CMake will try to run a test executable, and later during build, unit tests will be attempted on the ARM build.

我们如何配置版本(通过CMake)创建两者-而不是尝试在PC上运行交叉编译的版本?

How can we configure the builds (through CMake) to create both - and not attempt to run the crosscompiled ones on the PC?

推荐答案

我在我的项目中进行了类似的设置(从相同的来源构建模拟器,单元测试和目标二进制文件),并且您可以检查来区分两个用例。只是放

I have a similar setup in my projects (building from the same sources a simulator, unit tests, and the target binary), and you can check for CMAKE_CROSSCOMPILING to differentiate your two use cases. Just putting

if (NOT CMAKE_CROSSCOMPILING)
    ....
endif()

应该可以解决问题。

您需要有两个二进制输出目录。 CMake不允许在一个目录中混合使用工具链。

And you need to have two binary output directories. CMake does not allow to mix toolchains in one directory.

但是您不需要两个IDE项目。在我的项目中:

But you don't need to have two IDE projects. In my projects:


  • 我添加了所有来源-包括仅交叉编译文件-进入库/可执行目标


    • 我将它们标记为PC专用版本的从构建中排除

    • 因此所有源都将显示在一个IDE项目中(例如,用于搜索代码)


    • 我已将其从默认版本中删除,但是您可以从您的IDE

    • 对于我来说,这是一个外部脚本,但是您也可以直接在 COMMAND 参数中传递必要的调用

    • 您甚至可以使用另一个 ExternalProject_Add()包括自己的项目以进行交叉编译

    • I've removed it from the default build, but you can explicitly start it from your IDE
    • In my case it's an external script, but you can also pass the necessary calls directly in COMMAND parameters
    • You could even use another ExternalProject_Add() to include your own project for cross-compiling

    以下是这种方法的一些代码段:

    Here are some code snippets for this kind of approach:

    if (NOT CMAKE_CROSSCOMPILING)
        set(PC "ON" CACHE INTERNAL "hw-platform PC")
        unset(MCU CACHE)
    else()
        set(MCU "ON" CACHE INTERNAL "hw-platform MCU")
        unset(PC CACHE)
    endif()
    
    ...
    
    if (PC)
        # Exclude files that only compile/work on MCU
        set_source_files_properties(... PROPERTIES HEADER_FILE_ONLY 1)
    endif()
    
    ...
    
    if (PC)
        add_test(...)
    endif()
    
    ...
    
    if (PC AND EXISTS "${CMAKE_SOURCE_DIR}/buildMcu.cmd")
        add_custom_target(
            BUILD_MCU
            COMMAND buildMcu.cmd
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        )
        set_target_properties(BUILD_MCU PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD 1)
    endif()
    

    参考



    这篇关于CMake-本地编译并交叉编译相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:33