自定义设置:创建您的 settings_user.yml

请首先克隆源代码以重新创建此项目。您可以在 GitHub 上的 examples2 存储库 中找到它们。

$ git clone https://github.com/conan-io/examples2.git
$ cd examples2/examples/config_files/settings_user

在此示例中,我们将了解如何自定义您的设置,而无需覆盖原始的 settings.yml 文件。

注意

为了更好地理解此示例,强烈建议您事先阅读有关 settings.yml 的参考信息。

找到 settings_user.yml

首先,让我们看一下建议的 source/settings_user.yml

settings_user.yml
os:
    webOS:
        sdk_version: [null, "7.0.0", "6.0.1", "6.0.0"]
arch: ["cortexa15t2hf"]
compiler:
    gcc:
        version: ["13.0-rc"]

如您所见,我们不必重写所有设置,因为它们将与已在 settings.yml 中定义的设置合并。

那么,我们通过 settings_user.yml 文件添加了什么内容呢?

  • 新的操作系统:webOS,及其子设置:sdk_version

  • 新的 arch 可用:cortexa15t2hf

  • 新的 gcc 版本:13.0-rc

现在,是时候将文件 source/settings_user.yml 复制到您的 [CONAN_HOME]/ 文件夹中。

$ conan config install sources/settings_user.yml
Copying file settings_user.yml to /Users/myuser/.conan2/.

使用您的新设置

复制了 settings_user.yml 之后,您应该能够在您的配方中使用它们。将此简单的配方添加到您的本地文件夹中。

conanfile.py
from conan import ConanFile

class PkgConan(ConanFile):
    name = "pkg"
    version = "1.0"
    settings = "os", "compiler", "build_type", "arch"

然后,创建几个 Conan 软件包(不是二进制文件,因为它肯定没有任何源文件)以查看其是否正常工作。

使用新的操作系统及其子设置
$ conan create . -s os=webOS -s os.sdk_version=7.0.0
...
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu98
compiler.libcxx=libc++
compiler.version=12.0
os=webOS
os.sdk_version=7.0.0

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu98
compiler.libcxx=libc++
compiler.version=12.0
os=Macos
...
-------- Installing (downloading, building) binaries... --------
pkg/1.0: Copying sources to build folder
pkg/1.0: Building your package in /Users/myuser/.conan2/p/t/pkg929d53a5f06b1/b
pkg/1.0: Generating aggregated env files
pkg/1.0: Package 'a0d37d10fdb83a0414d7f4a1fb73da2c210211c6' built
pkg/1.0: Build folder /Users/myuser/.conan2/p/t/pkg929d53a5f06b1/b
pkg/1.0: Generated conaninfo.txt
pkg/1.0: Generating the package
pkg/1.0: Temporary package folder /Users/myuser/.conan2/p/t/pkg929d53a5f06b1/p
pkg/1.0 package(): WARN: No files in this package!
pkg/1.0: Package 'a0d37d10fdb83a0414d7f4a1fb73da2c210211c6' created
pkg/1.0: Created package revision 6a947a7b5669d6fde1a35ce5ff987fc6
pkg/1.0: Full package reference: pkg/1.0#637fc1c7080faaa7e2cdccde1bcde118:a0d37d10fdb83a0414d7f4a1fb73da2c210211c6#6a947a7b5669d6fde1a35ce5ff987fc6
pkg/1.0: Package folder /Users/myuser/.conan2/p/pkgb3950b1043542/p
使用新的 gcc 编译器版本
$ conan create . -s compiler=gcc -s compiler.version=13.0-rc -s compiler.libcxx=libstdc++11
...
Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=13.0-rc
os=Macos

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu98
compiler.libcxx=libc++
compiler.version=12.0
os=Macos
...
-------- Installing (downloading, building) binaries... --------
pkg/1.0: Copying sources to build folder
pkg/1.0: Building your package in /Users/myuser/.conan2/p/t/pkg918904bbca9dc/b
pkg/1.0: Generating aggregated env files
pkg/1.0: Package '44a4588d3fe63ccc6e7480565d35be38d405718e' built
pkg/1.0: Build folder /Users/myuser/.conan2/p/t/pkg918904bbca9dc/b
pkg/1.0: Generated conaninfo.txt
pkg/1.0: Generating the package
pkg/1.0: Temporary package folder /Users/myuser/.conan2/p/t/pkg918904bbca9dc/p
pkg/1.0 package(): WARN: No files in this package!
pkg/1.0: Package '44a4588d3fe63ccc6e7480565d35be38d405718e' created
pkg/1.0: Created package revision d913ec060e71cc56b10768afb9620094
pkg/1.0: Full package reference: pkg/1.0#637fc1c7080faaa7e2cdccde1bcde118:44a4588d3fe63ccc6e7480565d35be38d405718e#d913ec060e71cc56b10768afb9620094
pkg/1.0: Package folder /Users/myuser/.conan2/p/pkg789b624c93fc0/p
使用新的操作系统和新的架构
$ conan create . -s os=webOS -s arch=cortexa15t2hf
...
Profile host:
[settings]
arch=cortexa15t2hf
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu98
compiler.libcxx=libc++
compiler.version=12.0
os=webOS

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=apple-clang
compiler.cppstd=gnu98
compiler.libcxx=libc++
compiler.version=12.0
os=Macos
...
-------- Installing (downloading, building) binaries... --------
pkg/1.0: Copying sources to build folder
pkg/1.0: Building your package in /Users/myuser/.conan2/p/t/pkgde9b63a6bed0a/b
pkg/1.0: Generating aggregated env files
pkg/1.0: Package '19cf3cb5842b18dc78e5b0c574c1e71e7b0e17fc' built
pkg/1.0: Build folder /Users/myuser/.conan2/p/t/pkgde9b63a6bed0a/b
pkg/1.0: Generated conaninfo.txt
pkg/1.0: Generating the package
pkg/1.0: Temporary package folder /Users/myuser/.conan2/p/t/pkgde9b63a6bed0a/p
pkg/1.0 package(): WARN: No files in this package!
pkg/1.0: Package '19cf3cb5842b18dc78e5b0c574c1e71e7b0e17fc' created
pkg/1.0: Created package revision f5739d5a25b3757254dead01b30d3af0
pkg/1.0: Full package reference: pkg/1.0#637fc1c7080faaa7e2cdccde1bcde118:19cf3cb5842b18dc78e5b0c574c1e71e7b0e17fc#f5739d5a25b3757254dead01b30d3af0
pkg/1.0: Package folder /Users/myuser/.conan2/p/pkgd154182aac59e/p

如您所见,每个命令都创建了一个不同的软件包。这是完全正确的,因为我们对每个软件包使用了不同的设置。如果要查看创建的所有软件包,可以使用 conan list 命令。

列出所有 pkg/1.0 的软件包
$ conan list pkg/1.0:*
Local Cache
  pkg
    pkg/1.0
      revisions
        637fc1c7080faaa7e2cdccde1bcde118 (2023-02-16 06:42:10 UTC)
          packages
            19cf3cb5842b18dc78e5b0c574c1e71e7b0e17fc
              info
                settings
                  arch: cortexa15t2hf
                  build_type: Release
                  compiler: apple-clang
                  compiler.cppstd: gnu98
                  compiler.libcxx: libc++
                  compiler.version: 12.0
                  os: webOS
            44a4588d3fe63ccc6e7480565d35be38d405718e
              info
                settings
                  arch: x86_64
                  build_type: Release
                  compiler: gcc
                  compiler.libcxx: libstdc++11
                  compiler.version: 13.0-rc
                  os: Macos
            a0d37d10fdb83a0414d7f4a1fb73da2c210211c6
              info
                settings
                  arch: x86_64
                  build_type: Release
                  compiler: apple-clang
                  compiler.cppstd: gnu98
                  compiler.libcxx: libc++
                  compiler.version: 12.0
                  os: webOS
                  os.sdk_version: 7.0.0

尝试任何其他自定义设置!