skip to content

In Conan 2, how do settings, options and conf differ, and which of them change a package's package_id?

level: middleimportance: must knowfreq 58%

answer

  1. project-wide versus per-package
  2. validated against settings.yml
  3. conf configures tools
  4. hash of the info object

basics

~20 s

Settings are project-wide values such as compiler and build_type; options are per-package switches such as shared; conf configures Conan and its tools. Settings and options a recipe declares enter its package_id, while conf stays out unless tools.info.package_id:confs lists it.

solid answer

~40 s

**Settings** are global configuration from the profile, validated against `settings.yml`: `os`, `arch`, `compiler`, `compiler.version`, `compiler.cppstd`, `compiler.libcxx`, `build_type`. A recipe declares which ones affect it, and an invalid value such as an unknown compiler version is rejected. **Options** are declared per recipe with allowed values and `default_options`, such as `shared` and `fPIC`, and set per package with `-o "fmt/*:shared=True"`. Both feed the recipe's `info` object, so changing either gives a new **package_id**: on a test run, switching `fmt/*:shared` to True moved fmt to a different ID with no binary. **Conf** items such as `tools.build:jobs`, `tools.build:cxxflags` or `tools.cmake.cmaketoolchain:generator` configure the tools and leave the ID alone, unless `tools.info.package_id:confs` names them. Dependencies also contribute, according to the package ID modes.

code

ini · 15 lines
ini
[settings]
os=Macos
arch=armv8
compiler=apple-clang
compiler.version=21
compiler.cppstd=gnu17
compiler.libcxx=libc++
build_type=Release

[options]
zlib/*:shared=True

[conf]
tools.build:jobs=8
tools.cmake.cmaketoolchain:generator=Ninja

go deeper

for a junior

Know the three flags: -s for settings, -o for options with a package pattern, -c for conf.

for a middle

Explain which families enter the package_id and show it with conan graph info by changing an option and a conf item.

for a senior

Use package_id(), package ID modes and tools.info.package_id:confs deliberately, and spot binaries that are wrongly shared or needlessly split.

for a principal

Decide which flags count as identity for your organisation's binaries, trading the number of builds against the risk of mixing incompatible ones.

## Three kinds of configuration Conan 2 separates configuration into three families that look alike on the command line (`-s`, `-o`, `-c`) but behave differently. | | Settings | Options | Conf | |---|---|---|---| | Scope | project-wide, same for every package in a context | per package | global, per package pattern possible | | Defined by | `settings.yml` in the Conan home | the recipe's `options` and `default_options` | Conan's list of known conf names | | Typical items | `os`, `arch`, `compiler.version`, `build_type` | `shared`, `fPIC`, `with_ssl` | `tools.build:jobs`, `tools.build:cxxflags` | | Set with | `[settings]`, `-s` | `[options]`, `-o "pkg/*:opt=val"` | `[conf]`, `-c` | | In the package_id | yes, those the recipe declares | yes | no, unless listed in `tools.info.package_id:confs` | ## Settings Settings describe the platform and the toolchain. The allowed values live in `settings.yml`, so a typo fails fast: asking for `compiler.version=20` with apple-clang in Conan 2.33 stops with `Invalid setting '20' is not a valid 'settings.compiler.version' value` and lists the valid ones. A recipe opts in with `settings = "os", "compiler", "build_type", "arch"`; a recipe that declares no settings does not see them and cannot vary by them. ## Options Options are features of one package. The recipe declares the possible values and the defaults, and consumers override them with a pattern so they hit the right package: `-o "zlib/*:shared=True"`, or `zlib/*:shared=True` under `[options]`. Recipes often prune options in `config_options()` or `configure()`, for example removing `fPIC` on Windows or when `shared` is True, so the package ID does not vary by an option that has no effect. ## Conf Conf items configure Conan and the build helpers, not the package's identity. Examples: - `tools.build:jobs`, the parallel job count; - `tools.cmake.cmaketoolchain:generator`, to choose Ninja instead of the default; - `tools.build:cxxflags`, extra compiler flags appended by the toolchain; - `tools.build:compiler_executables`, explicit compiler paths. Because these do not enter the package ID by default, a binary built with `-c tools.build:cxxflags=["-O1"]` has the **same** ID as one built without it; on a test run the ID did not move. When such a flag does change the binary in a way consumers care about, list it in `tools.info.package_id:confs` so it becomes part of the identity. ## How the package_id is computed For each package Conan builds an **info** object and hashes it into the `package_id`: 1. the declared **settings** values; 2. the **options** values after `configure()`; 3. the **requirements**, reduced according to the package ID modes: by default `minor_mode` for a non-embedded dependency, such as a shared library a shared library links, and `full_mode` (version, recipe revision and package ID) for an embedded one, such as a static library linked into a shared library or an application; 4. anything the recipe's `package_id()` method adds or removes. Some special cases follow from this: - a recipe with `package_type = "header-library"` gets its info cleared, so one package serves every configuration; - `test_requires` never affect the ID, and tool requirements only do if `core.package_id:default_build_mode` is set; - `package_id()` can relax the ID, for example deleting `compiler.cppstd` for a C library, or tighten it. ## Per-package settings Settings are project-wide by default, but a profile or the command line can scope a value to some packages with a pattern, for example `-s "zlib/*:build_type=Release"` while the rest of the graph is Debug. This is how a team debugs its own code against optimised third-party libraries. The scoped value is still a setting, so it still enters the package ID of the packages it applies to: the Release zlib binary is the same one a fully Release build uses. ## Why the distinction matters Mixing the families is a common source of surprise. Passing an optimisation flag as conf keeps one binary for two differently compiled builds; encoding a platform difference as an option makes consumers set it by hand; and changing a setting a recipe declares, such as `build_type`, always means another binary has to exist. Which binaries are then built or reused is decided by the `--build` policy and the compatibility rules, which are separate questions.

  • How do you make a compiler flag passed through conf part of the package identity?
    List it in `tools.info.package_id:confs`, for example `["tools.build:cxxflags"]`. Conan then copies those conf values into the info object, so builds with different flags get different package IDs.
  • Why do recipes delete fPIC when shared is True?
    A shared library is always position-independent, so `fPIC` has no effect. Removing it in `configure()` stops the option from splitting the package ID into two identical binaries.

A package_id works like a product's stock code: a different size or colour (settings and options) gets a different code, while which worker packed the box (conf) does not, unless the shop decides packing matters and adds it to the code.

  • Conf values such as tools.build:cxxflags always change the package_id.
  • Options are global, so shared=True applies to every package automatically.
  • Settings can take any string; Conan does not validate their values.
  • A header-library package needs one binary per compiler and build type.
  • test_requires are part of the package_id like any other requirement.