使用 Conan 进行交叉编译应用程序:主机和构建上下文¶
请先克隆源代码以重新创建此项目。您可以在 GitHub 上的examples2 仓库中找到它们。
$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/tutorial/consuming_packages/cross_building
在之前的示例中,我们学习了如何使用 conanfile.py 或 conanfile.txt 来构建一个使用 Zlib 和 CMake Conan 包压缩字符串的应用程序。此外,我们还解释了您可以在名为 Conan profile 的文件中设置操作系统、编译器或构建配置等信息。您可以将该 profile 作为参数(--profile)传递给 conan install 命令。我们还解释了不指定该 profile 等同于使用 --profile=default 参数。
对于所有这些示例,我们使用相同的平台来构建和运行应用程序。但是,如果您想在运行 Ubuntu Linux 的机器上构建应用程序,然后在 Raspberry Pi 等其他平台上运行它,该怎么办?Conan 可以通过使用两个不同的 profile 来模拟这种情况:一个用于**构建**应用程序的机器(Ubuntu Linux),另一个用于**运行**应用程序的机器(Raspberry Pi)。我们将在下一节中解释这种“两个 profile”的方法。
Conan 双 profile 模型:构建和主机 profile¶
即使您在调用 Conan 时只指定了一个 --profile 参数,Conan 也会在内部使用两个 profile。一个用于**构建**二进制文件的机器(称为 **build** profile),另一个用于**运行**这些二进制文件的机器(称为 **host** profile)。调用此命令
$ conan install . --build=missing --profile=someprofile
等同于
$ conan install . --build=missing --profile:host=someprofile --profile:build=default
如您所见,我们使用了两个新参数
profile:host
:这是定义构建的二进制文件将在其上运行的平台的 profile。对于我们的字符串压缩器应用程序,此 profile 将应用于将在 **Raspberry Pi** 上运行的 Zlib 库。profile:build
:这是定义二进制文件将在其上构建的平台的 profile。对于我们的字符串压缩器应用程序,此 profile 将被 CMake 工具使用,该工具将在 **Ubuntu Linux** 机器上编译它。
请注意,当您只使用一个 --profile
参数时,它等同于 --profile:host
。如果您不指定 --profile:build
参数,Conan 将在内部使用 default profile。
因此,如果我们想在 Ubuntu Linux 机器上构建压缩器应用程序,但要在 Raspberry Pi 上运行它,我们应该使用两个不同的 profile。对于**构建**机器,我们可以使用默认 profile,在我们的例子中是这样的:
[settings]
os=Linux
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu14
compiler.libcxx=libstdc++11
compiler.version=9
以及 Raspberry Pi 的 profile,即**主机**机器:
[settings]
os=Linux
arch=armv7hf
compiler=gcc
build_type=Release
compiler.cppstd=gnu14
compiler.libcxx=libstdc++11
compiler.version=9
[buildenv]
CC=arm-linux-gnueabihf-gcc-9
CXX=arm-linux-gnueabihf-g++-9
LD=arm-linux-gnueabihf-ld
重要
请注意,为了成功构建此示例,您应该已安装一个包含编译器和用于构建适用于相应架构的应用程序的所有工具的工具链。在这种情况下,主机是 Raspberry Pi 3,带有 armv7hf 架构的操作系统,并且我们在 Ubuntu 机器上安装了 arm-linux-gnueabihf 工具链。
如果您查看 raspberry profile,会有一个名为 [buildenv]
的部分。此部分用于设置构建应用程序所需的环境变量。在这种情况下,我们声明了 CC
、CXX
和 LD
变量,分别指向交叉构建工具链的编译器和链接器。将此部分添加到 profile 将在每次我们执行 conan install 时调用 VirtualBuildEnv 生成器。此生成器会将该环境信息添加到 conanbuild.sh
脚本中,我们将在使用 CMake 进行构建之前 source 该脚本,以便它可以使用交叉构建工具链。
注意
在某些情况下,构建平台可能没有可用的工具链。对于这些情况,您可以使用 Conan 包作为交叉编译器,并将其添加到 profile 的 [tool_requires]
部分。有关使用工具链包进行交叉构建的示例,请参阅此示例。
构建和主机上下文¶
现在我们已经准备好了两个 profile,让我们来看看我们的 conanfile.py
from conan import ConanFile
from conan.tools.cmake import cmake_layout
class CompressorRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain", "CMakeDeps"
def requirements(self):
self.requires("zlib/1.3.1")
def build_requirements(self):
self.tool_requires("cmake/3.27.9")
def layout(self):
cmake_layout(self)
如您所见,这实际上与我们在上一个示例中使用的 conanfile.py 基本相同。我们将 zlib/1.3.1 作为常规依赖项,将 cmake/3.27.9 作为构建应用程序所需的工具。
我们需要应用程序为 Raspberry Pi 构建,并使用交叉构建工具链,同时还需要链接为同一平台构建的 zlib/1.3.1 库。另一方面,我们需要 cmake/3.27.9 二进制文件在 Ubuntu Linux 上运行。Conan 在依赖项图中通过区分我们称之为“构建上下文”和“主机上下文”来内部管理这些。
主机上下文由根包(在 conan install 或 conan create 命令中指定的包)及其通过
self.requires()
添加的所有依赖项填充。在这种情况下,这包括压缩器应用程序和 zlib/1.3.1 依赖项。构建上下文包含在构建机器上使用的工具依赖项。此类通常包括所有开发工具,如 CMake、编译器和链接器。在这种情况下,这包括 cmake/3.27.9 工具。
这些上下文定义了 Conan 如何管理每个依赖项。例如,由于 zlib/1.3.1 属于主机上下文,因此在 **raspberry** profile(主机 profile)中定义的 [buildenv]
构建环境仅适用于构建 zlib/1.3.1 库,而不会影响属于构建上下文的任何内容,例如 cmake/3.27.9 依赖项。
现在,让我们构建应用程序。首先,使用构建和主机平台的 profile 调用 conan install。这将安装为 armv7hf 架构构建的 zlib/1.3.1 依赖项,以及一个在 64 位架构上运行的 cmake/3.27.9 版本。
$ conan install . --build missing -pr:b=default -pr:h=./profiles/raspberry
然后,让我们调用 CMake 来构建应用程序。正如我们在上一个示例中所做的那样,我们必须通过运行 source Release/generators/conanbuild.sh
来激活**构建环境**。这将设置查找交叉构建工具链和构建应用程序所需的环境变量。
$ cd build
$ source Release/generators/conanbuild.sh
Capturing current environment in deactivate_conanbuildenv-release-armv7hf.sh
Configuring environment variables
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=Release/generators/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
...
-- Conan toolchain: C++ Standard 14 with extensions ON
-- The C compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/arm-linux-gnueabihf-gcc-9 - skipped
-- Detecting C compile features
-- Detecting C compile features - done [100%] Built target compressor
...
$ source Release/generators/deactivate_conanbuild.sh
您可以通过运行 file
Linux 工具来检查我们是否为正确的架构构建了应用程序。
$ file compressor
compressor: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically
linked, interpreter /lib/ld-linux-armhf.so.3,
BuildID[sha1]=2a216076864a1b1f30211debf297ac37a9195196, for GNU/Linux 3.2.0, not
stripped
另请参阅
使用 tool_requires 进行交叉构建
使用 Conan 为 iOS 构建