conan2 - 如何配置交叉编译

正常当你的项目是通过交叉编译进行开发的,且需要支持多个平台编译,那么往往需要提供多个conan的profile文件。
conan2支持两种方式配置交叉编译器,第一种是在profile里的[buildenv]节点下定义CC、CXX等;第二种是在conan的profile里引入现有的toolchain.cmake文件:

第一种:在profile里的[buildenv]节点下定义CC、CXX

[settings]
os=Linux
arch=armv8
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.cppstd=gnu17
compiler.version=9

[tool_requires]
cmake/3.27.7

[conf]
tools.cmake.cmaketoolchain:generator=Ninja
tools.build:jobs={{os.cpu_count() - 2}}
tools.build:sysroot=/xxx/xxx/aarch64-poky-linux/sysroot

[buildenv]
CC=/xxx/xxx/aarch64-poky-linux/toolchain/bin/aarch64-poky-linux-gcc
CXX=/xxx/xxx/aarch64-poky-linux/toolchain/bin/aarch64-poky-linux-g++

PKG_CONFIG_EXECUTABLE=/usr/bin/pkg-config
PKG_CONFIG_SYSROOT_DIR=/xxx/xxx/aarch64-poky-linux/sysroot
PKG_CONFIG_PATH=/xxx/xxx/aarch64-poky-linux/sysroot/usr/lib/pkgconfig

KEY_1=VALUE_1
KEY_2=VALUE_2
KEY_3=VALUE_3

[conf] 配置了sysroot
[buildenv] 配置了交叉编译的CC,CXX
[buildenv] 配置了PKG_CONFIG_XXX,为的是能方便通过pkg-config找到linux的基础库;
[buildenv] 配置了KEY_X=VALUE_X,为的是可以在conanfile.py里可以读到,并可以统一定义为CMake变量和C/C++宏

第二种:在conan的profile里引入现有的toolchain.cmake文件

[settings]
os=Linux
arch=armv8
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.cppstd=gnu17
compiler.version=9

[tool_requires]
cmake/3.27.7

[conf]
tools.build.cross_building:cross_build=true
tools.cmake.cmaketoolchain:generator=Ninja
tools.build:jobs={{os.cpu_count()}}
tools.cmake.cmaketoolchain:user_toolchain=/xxx/xxx/aarch64-poky-linux/toolchain.cmake

[buildenv]
KEY_1=VALUE_1
KEY_2=VALUE_2
KEY_3=VALUE_3

如上conan的profile看起来简化了很多,完全靠[conf]里的tools.cmake.cmaketoolchain:user_toolchain引入了交叉编译的所有配置,这种方式也方便非conan的项目复用现有的toolchain.cmake文件,conan本身只是包裹了cmake,最好不要因为conan的引入增加维护的复杂度。

统一定义为CMake变量和C/C++宏

在C++工程里,经常会遇到一种情况:在cmake里判断的条件需要在C/C++里也能支持判断,本身它们就不能相互访问,常规办法就是cmake里判断变量定义就同时定义一个同名的C/C++宏,但这种手动判断和定义很琐碎,于是可以借助conanfile.py和profile来做,如下:

from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.build import check_min_cppstd

class ApplicationRecipe(ConanFile):
    name = "myapp"
    version = "1.0.0"

    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps"

    cli_args = []

    def validate(self):
        check_min_cppstd(self, "17")

    def requirements(self):
       self.requires("hailort/4.16.0@isw/stable")

    def layout(self):
        cmake_layout(self)

    def generate(self):
        # Customize the toolchain.
        toolchain = CMakeToolchain(self)

        # Add extra cmake vars and micros into toolchain.
        buildenv = self.buildenv.vars(self)
        for key, value in buildenv.items():
            if value == 'none':
                toolchain.preprocessor_definitions[key] = None
            else:
                toolchain.preprocessor_definitions[key] = value
                self.cli_args.extend(['-D{}="{}"'.format(key, value)])

        # Generate toolchain file.
        toolchain.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure(cli_args = self.cli_args)
        cmake.build()

在def generate(self)里我们通过读取[buildenv]里来自profile里定义的变量来重新定义cmake变量和C/C++宏,并且如果发现profile里定义的变量的value为none, 就不给予定义cmake变量了,因为没有value的cmake变量没有意义。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容