使用 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 模型:构建和宿主 profiles¶
即使您在调用 Conan 时只指定了一个 --profile 参数,Conan 也会在内部使用两个 profiles。一个用于 构建 二进制文件的机器(称为 构建 profile),另一个用于 运行 这些二进制文件的机器(称为 宿主 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 等同于 --profile:host。如果您不指定 --profile:build 参数,Conan 将在内部使用 default profile。
因此,如果我们想在 Ubuntu Linux 机器上构建压缩器应用程序,但将其运行在 Raspberry Pi 上,我们应该使用两个不同的 profiles。对于 构建 机器,我们可以使用默认 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
重要
请注意,为了成功构建此示例,您应该安装一个包含编译器以及构建应用程序以获得正确体系结构的工具的工具链。在这种情况下,宿主机器是运行 armv7hf 体系结构操作系统的 Raspberry Pi 3,并且我们在 Ubuntu 机器上安装了 arm-linux-gnueabihf 工具链。
如果您查看 raspberry profile,会有一个名为 [buildenv] 的部分。此部分用于设置构建应用程序所需的 environment variables。在这种情况下,我们声明了 CC、CXX 和 LD 变量,分别指向交叉构建工具链的编译器和链接器。将此部分添加到 profile 中将在我们进行 conan install 时,每次调用 VirtualBuildEnv 生成器。此生成器会将该环境信息添加到我们将要在使用 CMake 构建之前 sourced 的 conanbuild.sh 脚本中,以便它可以利用交叉构建工具链。
注意
在某些情况下,您的构建平台没有可用的工具链。对于这些情况,您可以使用交叉编译器的 Conan 包,并将其添加到 profile 的 [tool_requires] 部分。有关使用工具链包进行交叉构建的示例,请查看 此示例。
构建和宿主上下文¶
现在我们已经准备好了两个 profiles,让我们来看看我们的 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 依赖项。
现在,让我们构建应用程序。首先,使用构建和宿主平台的 profiles 调用 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 来激活 构建环境。这将设置定位交叉构建工具链并构建应用程序所需的 environment variables。
$ 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 构建