Conan2: profiles, the beginning interpretation of how Conan running

本文介绍了Conanprofiles的功能,如何创建、检测和使用配置文件来设置构建设置、选项、环境变量、工具要求等。用户可以自定义默认或自命名的profile,以确保项目的稳定性和一致性。
摘要由CSDN通过智能技术生成

Introduction to profiles

Conan profiles allow users to set a complete configuration set for settings, options, environment variables (for build time and runtime context), tool requirements, and configuration variables in a file.

an example of a profile structure

[settings]
arch=x86_64
build_type=Release
os=Macos

[options]
MyLib:shared=True
[tool_requires]
tool1/0.1@user/channel
*: tool4/0.1@user/channel

[buildenv]
VAR1=value

[runenv]
EnvironmentVar1=My Value

[conf]
tools.build:jobs=2

[replace_requires]
zlib/1.2.123: zlib/*

[replace_tool_requires]
7zip/*: 7zip/system

[platform_requires]
dlib/1.3.22

[platform_tool_requires]
cmake/3.24.2

Profiles can be created with the detect option in conan profile command, and edited later.
If you don’t specify a name, the command will create the default profile(the --force option will override the existed default profile):

>conan profile detect --force
detect_api: Found msvc 17

Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=193
os=Windows

WARN: This profile is a guess of your environment, please check it.
WARN: The output of this command is not guaranteed to be stable and can change in future Conan versions.
WARN: Use your own profile files for stability.
Saving detected profile to C:\Users\XXXXX\.conan2\profiles\default

One can create a self-named profile using --name option like this:

> conan profile detect --name myprofile
> detect_api: Found msvc 17

Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=193
os=Windows

WARN: This profile is a guess of your environment, please check it.
WARN: The output of this command is not guaranteed to be stable and can change in future Conan versions.
WARN: Use your own profile files for stability.
Saving detected profile to C:\Users\XXXXX\.conan2\profiles\myprofile

Conan will always set the default C++ standard as the one that the detected compiler version uses by default.
If you want to use a different C++ standard, you can edit the default profile file directly.

myprofile

...
compiler.cppstd=20
...

Profile files can be used with -pr/–profile option in many commands like conan install or conan create commands.
If you don’t specify any profile at all, the default profile will be always used:

> conan create .
> conan create . -pr=myprofile

Profiles can be located in different folders:

$ conan install . -pr /abs/path/to/myprofile # abs path
$ conan install . -pr ./relpath/to/myprofile # resolved to current dir
$ conan install . -pr ../relpath/to/myprofile # resolved to relative dir
$ conan install . -pr myprofile # resolved to [CONAN_HOME]/profiles/myprofile

Command conan profile list can be used to list existing profiles in the profiles folder

> conan profile list
Profiles found in the cache:
default
myprofile

Command conan profile show -pr myprofile can be used to show the profile’s content per context:

> conan profile show -pr myprofile
Host profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=20
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows

Build profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows

Available sections in profiles

[settings]

List of settings available from settings.yml

[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=20
compiler.runtime=dynamic
compiler.runtime_type=Release
compiler.version=193
os=Windows

[options]

List of options available from your recipe and its dependencies

[options]
my_pkg_option=True
shared=True

[tool_requires]

List of tool_requires required by your recipe or its dependencies

[tool_requires]
cmake/3.25.2

[buildenv]

List of environment variables that will be injected to the environment every time the ConanFile run(cmd, env=“conanbuild”) method is invoked (build time context is automatically run by VirtualBuildEnv).
• += == append: appends values at the end of the existing value.
• =+ == prepend: puts values at the beginning of the existing value.
• =! == unset: gets rid of any variable value.
Another essential point to mention is the possibility of defining variables as PATH ones by simply putting (path) as the prefix of the variable. It is useful to automatically get the append/prepend of the PATH in different systems (Windows uses ; as separation, and UNIX : ).

[buildenv]
# Define a variable "MyVar1"
MyVar1=My_Value; other
# Append another value to "MyVar1"
MyVar1+=MyValue12
# Define a PATH variable "MyPath1"
MyPath1=(path)/some/path11
# Prepend another PATH to "MyPath1"
MyPath1=+(path)/other_path/path12
# Unset the variable "MyPath1"
MyPath1=!

Then, the result of applying this profile is:
• MyVar1: My_Value; other MyValue12
• MyPath1:
  – Unix: /other_path/path12:/some/path11
  – Windows: /other_path/path12;/some/path11
• mypkg*:PATH: None

[runenv]

List of environment variables that will be injected to the environment every time the ConanFile run(cmd, env=“conanrun”) method is invoked (runtime context is automatically run by VirtualRunEnv). All the operators/patterns explained for [buildenv] applies to this one in the same way:

[runenv]
MyVar1=My_Value; other
MyVar1+=MyValue12
MyPath1=(path)/some/path11
MyPath1=+(path)/other_path/path12
MyPath1=!

[conf]

List of user/tools configurations:

[conf]
tools.build:verbosity=verbose
tools.microsoft.msbuild:max_cpu_count=2
tools.microsoft.msbuild:vs_version = 16
tools.build:jobs=10
# User conf variable
user.confvar:something=False

They can also be used in global.conf, but profiles values will have priority over globally defined ones in global.conf

References

Conan Documentation --Release 2.0.17

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值