将构建环境打包为 build_requires 个配置文件,改为使用系统编译器

Packaged build environment as build_requires of profiles, system compiler is used instead

我将首先尝试描述我的设置:

如果我尝试构建一个包如下:

$ conan create . foo/version --build=missing -pr linux-x86_64-Debug -pr cmake-3.18.2 -pr gcc-10.1.0

生成的配置是:

Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Debug
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=10.1
os=Linux
os_build=Linux
[options]
[build_requires]
*: gcc/10.1.0, cmake/3.18.2
[env]

我觉得很合适...

然后 conan 开始为 gcc 10.1 x86_64 构建 cmake 包 x86_64 配置:

cmake/3.18.2: WARN: Build folder is dirty, removing it: /home/manuel/.conan/data/cmake/3.18.2/_/_/build/46c0026dddc0e0537a797652343e8e1e9d0e39e7
cmake/3.18.2: Copying sources to build folder
cmake/3.18.2: Building your package in /home/manuel/.conan/data/cmake/3.18.2/_/_/build/46c0026dddc0e0537a797652343e8e1e9d0e39e7
cmake/3.18.2: Generator cmake created conanbuildinfo.cmake
cmake/3.18.2: Calling build()

但随后构建失败,因为 cmake 包 cmake 配置 正在使用系统编译器:

-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: called by CMake conan helper
-- Conan: called inside local cache
-- Conan: Adjusting output directories
-- Conan: Using cmake global configuration
-- Conan: Adjusting default RPATHs Conan policies
-- Conan: Adjusting language standard
-- Conan: Compiler GCC>=5, checking major version 10.1
-- Conan: Checking correct version: 7.4
CMake Error at ../conanbuildinfo.cmake:510 (message):
  Detected a mismatch for the compiler version between your conan profile
  settings and CMake:

  Compiler version specified in your conan profile: 10.1

  Compiler version detected in CMake: 7.4

  Please check your conan profile settings (conan profile show
  [default|your_profile_name])

  P.S.  You may set CONAN_DISABLE_CHECK_COMPILER CMake variable in order to
  disable this check.
Call Stack (most recent call first):
  ../conanbuildinfo.cmake:592 (conan_error_compiler_version)
  ../conanbuildinfo.cmake:695 (check_compiler_version)
  ../conanbuildinfo.cmake:249 (conan_check_compiler)
  CMakeLists.txt:9 (conan_basic_setup)

这看起来与 https://github.com/conan-io/conan/issues/1842 非常相似,但那期是 3 年前的。

Conan 在之前的一些版本中了解了 hostbuild 上下文,并且能够为这些上下文管理不同的配置。这优于旧的 os_buildarch_build 设置,ConanCenter 中的软件包正在朝着这个新功能发展。

在您的场景中,您正在尝试使用包 gcccmake 作为 build-requires 构建 library。您正在为 host 机器构建 library,并使用包 gcccmake,它们提供的工具应该 运行 在 构建 机器。即使您不是 cross-compiling,我们也可以同意原生建筑只是一个特定的 use-case,其中 hostbuild是同一台机器,因此,它们使用相同的 Conan 配置。

回到你的例子。您需要 host 平台的配置文件,您要为其生成 library 的平台。这些是您在问题中列出的所有配置文件。但是您还需要 build 上下文的概要文件,Conan 将使用该概要文件来构建(或检索)gcccmake 包。例如,您可能希望使用配置文件 linux-x86_64-Debug 在调试模式下编译 library,而在发布模式下使用 gcccmake

通常,您的 build 上下文的配置文件可以是 Conan 检测到的默认配置文件。

试试这个命令而不是你的(注意 --profile:build=default):

conan create <library/conanfile.py> foo/version --build=missing --profile:host linux-x86_64-Debug --profile:host cmake-3.18.2 --profile:host gcc-10.1.0 --profile:build=default 

现在柯南将使用新功能并将包分配给相应的上下文:

  • library 及其所有依赖项将分配给 host 上下文,并将使用来自所有 --profile:host 配置文件的信息:您的 Debug 版本和 Conan将 cmakegcc 添加为 build-requires.
  • gcccmake,因为它们在此命令中是 build-requires,将被分配给 build 上下文并将使用来自 --profile:build 的信息。如果 Conan 需要构建这些包,它将使用该配置文件中的设置,而不是 host 配置文件的设置。

使用此设置,您可以在单个 运行 中构建 cross-building 场景中的所有包:包在 host 上下文将使用您的 build-requires gcc 进行构建,而 build 上下文中的包将使用 --profile:build 中定义的工具。

是的,在这种情况下,build 上下文中的包将使用您系统中的编译器....除非您在 [=33] 中声明 [build_requires] =].