skip to content

In a Conan 2 recipe, what changes when a code generator is declared with tool_requires instead of requires, especially when cross-building for an ARM target?

level: seniorimportance: should knowfreq 40%

answer

  1. host context versus build context
  2. which profile builds the tool
  3. headers and libs not propagated
  4. invisible to downstream consumers

basics

~20 s

tool_requires puts the generator in the build context, built for the build profile so it runs on the build machine, with headers and libraries not propagated, visible=False and no effect on package_id by default. requires would build it for the target.

solid answer

~50 s

`self.requires()` places a dependency in the **host context**: it is built with the host profile, here the ARM target, its headers and libraries are propagated to the consumer, and it can enter the consumer's package ID. `self.tool_requires()`, usually called in `build_requirements()` or set as the `tool_requires` attribute, places it in the **build context**: it is resolved and built with the build profile (`-pr:b`), so a code generator compiles for the machine that runs the build. Its default traits are `headers=False`, `libs=False`, `build=True`, `run=True` and `visible=False`, so nothing gets linked, its executables reach the build through the `conanbuild` environment, and consumers further down never see it or conflict with it. A tool requirement does not change the package ID unless `core.package_id:default_build_mode` is configured. Declaring the generator with `requires` would build an ARM binary the x86_64 build machine cannot run.

code

python · 19 lines
python
from conan import ConanFile
from conan.tools.cmake import cmake_layout


class MathcoreRecipe(ConanFile):
    name = "mathcore"
    version = "1.4.0"
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps", "CMakeToolchain"

    def requirements(self):
        self.requires("zlib/1.3.1")       # host: linked into mathcore

    def build_requirements(self):
        self.tool_requires("codegen/1.0")  # build: runs during the build
        self.test_requires("gtest/1.15.0") # host: tests only, not propagated

    def layout(self):
        cmake_layout(self)

go deeper

for a junior

Remember the split: requires is for libraries you link, tool_requires is for programs the build runs.

for a middle

Name the traits: build context, headers and libs off, run on, visible off, and show the contexts in conan graph info.

for a senior

Diagnose a cross build that tries to execute a target binary, and handle packages that need both a host library and a build tool.

for a principal

Decide whether tool versions should affect package IDs through default_build_mode, trading rebuild cost against traceability.

## Two contexts in every Conan 2 graph Conan 2 resolves every dependency graph in two **contexts**: - the **host context**, for code that ends up in, or runs on, the machine the product targets; it uses the **host profile** (`-pr:h`, or plain `-pr`); - the **build context**, for tools that run on the machine doing the build; it uses the **build profile** (`-pr:b`). On a native build both profiles are often the same `default` profile, which hides the difference. In a cross build, say an x86_64 Linux CI machine producing a library for an armv8 device, they differ, and the declaration decides which profile each dependency is built for. ## requires: a host dependency `self.requires("zlib/1.3.1")` creates a regular requirement. Its default traits make it a library you link: - it is built with the host profile, so for armv8; - `headers` and `libs` are propagated, so CMakeDeps writes targets for it; - it is `visible`, so two paths to different versions of it in one graph conflict; - its version, and when it is a static library embedded into the consumer also its revision and package ID, enter the consumer's package ID. ## tool_requires: a build tool `self.tool_requires("codegen/1.0")` is the declaration for an executable the build runs, such as a code generator, CMake itself or a protocol compiler. Conan 2.33's `tool_require` creates the requirement with these traits: | Trait | Value | Effect | |---|---|---| | `build` | `True` | resolved in the build context with the build profile | | `headers` | `False` | no include directories propagated | | `libs` | `False` | nothing linked into the consumer | | `run` | `True` | its `bin` folders go on `PATH` through the build environment | | `visible` | `False` | only the direct consumer sees it, so it cannot conflict downstream | In addition, `core.package_id:default_build_mode` defaults to `None`, so a tool requirement does not enter the consumer's package ID: upgrading the generator does not invalidate every binary that used it, unless the team opts in. Where to declare it: 1. the `tool_requires` attribute, for a fixed list; 2. `self.tool_requires(...)` inside `build_requirements()`, for conditional logic; 3. `[tool_requires]` in a conanfile.txt. ## Seeing the contexts `conan graph info . -s:h build_type=Debug` on a project with `fmt` as a requirement and `codegen` as a tool requirement shows `fmt` in context `host` with `build_type` Debug, while `codegen` sits in context `build` with the build profile's `Release`. The same split happens for `arch` in a cross build. ## When a package is both Some packages are needed in both roles. A serialization library that ships a runtime library and a compiler for its schema files is declared twice: once with `self.requires(...)` for the library the target links, and once with `self.tool_requires(...)` for the compiler executable that runs during the build. Conan resolves it once per context, and in a cross build that means two different binaries, one per profile. Getting only the `requires` half right is a classic cross-build failure: the build tries to execute an armv8 binary on x86_64. ## Reading a cross build that goes wrong The symptom of a misdeclared tool is usually not a Conan error. Conan resolves the graph happily, builds the generator for armv8 because the recipe said `requires`, and the failure appears later, when CMake's `add_custom_command` tries to execute it on the x86_64 runner and the armv8 binary cannot run there. Two checks find it fast: - `conan graph info` with the cross profiles: the tool should appear in context `build`, with the build machine's `arch`. - the generated `conanbuild.sh`: the tool's `bin` folder should be on `PATH` there, which is where Conan puts the executables of build-context requirements. ## The cost of getting it wrong - Declaring a generator with `requires`: in a cross build the executable is built for the target and cannot run, and the tool also leaks headers, libraries and version conflicts into consumers. - Declaring a runtime library with `tool_requires`: nothing is linked, and the build fails at link time or the binary misses a dependency. - `test_requires`, for GoogleTest or Catch2, is the third kind: host context, headers and libraries available to this package's tests, but never propagated to consumers and never part of the package ID. Which profile file holds which settings, and how `-pr:h` and `-pr:b` are composed, belongs to the material on profiles; here the point is the declaration.

  • How do the executables of a tool_requires reach the build?
    Its `run=True` trait makes the generator environment add the tool's `bin` folders to `PATH` in `conanbuild.sh` or `conanbuild.bat`, and the `CMake` helper runs commands inside that environment, so `add_custom_command` can call the tool by name.
  • Can a profile inject a tool requirement without editing the recipe?
    Yes, a profile's `[tool_requires]` section adds tool requirements to the matching packages, which is how teams pin a CMake or Ninja version for every build. Profile composition itself is covered with profiles.

Ingredients end up on the diner's plate, while the oven only has to work in the kitchen: requires are ingredients built for the target machine, tool_requires are appliances built for the machine that does the cooking, and nobody serves the oven.

  • tool_requires and requires differ only in when the dependency is downloaded.
  • A tool_requires is built with the host profile, like everything else.
  • Upgrading a tool_requires always changes every consumer's package ID.
  • A tool_requires propagates its headers to consumers of my package.
  • A package can never be both a requires and a tool_requires in one graph.