Skip to content

One precedence rule for build configuration: environment, flags, pyproject, cache #201664

Open

Description

@zklaus

🚀 The feature, motivation and pitch

A build option such as USE_CUDA, BLAS or TORCH_CUDA_ARCH_LIST can reach CMake through several channels: an environment variable, a -C cmake.define.X=Y or -C cmake.args=-DX=Y flag, CMAKE_ARGS, a value in pyproject.toml, or whatever build/CMakeCache.txt remembers from the last configure. Today the order in which these win is not written down anywhere, and it is not even the same for every option: since #188242 an exported USE_*/BUILD_*/CMAKE_* variable overrides the cache on every configure, while the names cmake/EnvVarForwarding.cmake forwards by list (BLAS, TORCH_CUDA_ARCH_LIST, CUDNN_*, MKL_THREADING, ...) only seed the cache once and are then ignored. -D beats the environment for the second group and loses to it for the first.

This issue proposes to adopt one rule for every option, document it, and bring the remaining channels in line with it:

Nearest to the process wins, and the environment counts as nearest.

tier channel re-applied every configure?
1 environment variables (USE_CUDA=0, BLAS=OpenBLAS, DEBUG=1, SKBUILD_*; -D inside CMAKE_ARGS) yes
2 per-invocation flags (-C torch.cuda=false, -C cmake.define.USE_CUDA=OFF, -C cmake.args=-D...) yes
3 pyproject.toml (static cmake.define, overrides, declared-setting defaults) yes
4 persisted CMakeCache.txt only where 1-3 are silent
5 CMake defaults (option(), set(... CACHE ...)) only where 1-4 are silent

In one sentence for the docs: environment variables win over -C flags, -C flags win over pyproject.toml, and all three win over what the build directory remembers from last time; to change one setting for a single command, put the variable in front of the command.

This is the order scikit-build-core itself implements for its own settings and for the declared config-settings of 1.1 (environment > -C > pyproject > default), and the order #188242 already gave the prefixed names. The proposal makes it universal rather than inventing a new one.

Steps, each its own PR:

  • EnvVarForwarding.cmake: forward the list-based names and aliases with the same override semantics as the prefixed names, with a test asserting tiers 1-4 for one name of each kind. Apply one precedence rule to all forwarded build env vars #201666
  • Documentation: README "Adjust Build Options" and CONTRIBUTING state the rule (part of Apply one precedence rule to all forwarded build env vars #201666); the env-var reference moving to CONTRIBUTING in Move the build environment variable reference into CONTRIBUTING.md #194745 drops the per-name "seeds the cache only" annotations, which no longer describe a difference.
  • Declared config-settings ([build] Expose common build options as PEP 517 config-settings #200651): the sentence "the bound environment variable takes precedence over the setting" is true under this rule; the review there asks for a settings test.
  • Options read straight from the environment in CMake code. A few dozen $ENV{} reads outside third_party/ bypass the cache, so they honour tier 1 only and a -D or cmake.define of the same name is silently ignored. Normalize the ones with a cache variable first, in one PR:
    • Switches that test $ENV{} instead of their cache variable: USE_LAYERNORM_FAST_RECIPROCAL (cmake/Dependencies.cmake:1142), USE_CPU_VECTORIZATION (cmake/Codegen.cmake:389), ATEN_STATIC_CUDA (aten/src/ATen/CMakeLists.txt:875), BUILD_IGNORE_SVE_UNAVAILABLE (cmake/Modules/FindARM.cmake:30), TORCH_XPU_ARCH_LIST (cmake/public/utils.cmake:324).
    • TORCH_CUDA_ARCH_LIST: the USE_MSLK default (CMakeLists.txt:1186) reads $ENV{} and misses a -D value (found reviewing [build] Expose common build options as PEP 517 config-settings #200651); cmake/Dependencies.cmake:1495, cmake/public/utils.cmake:337 and cmake/External/nccl_ep.cmake:34 re-read an environment the forwarding has already applied and can go (raised by the review on Apply one precedence rule to all forwarded build env vars #201666).
    • The rest, library location hints (MKLROOT, OpenBLAS_HOME, NCCL_ROOT, ROCM_PATH, ACL_ROOT_DIR, MIOPEN_PATH, ...) and the switches PYTORCH_ROCM_ARCH, ATEN_AVX512_256, TORCH_NVCC_FLAGS: document as environment-only until normalized. The CACHE seeds in FindCUDNN.cmake, FindCUDA.cmake and nnpack.cmake apply only to undefined entries and stay.
  • The three spellings of -D (cmake.define: per-key merge; cmake.args: replaced wholesale by a higher-priority source; CMAKE_ARGS: concatenated last) stay as they are and are documented as tie-breaks inside tier 2. The blessed flag spellings are a declared setting where one exists and cmake.define otherwise; cmake.args and CMAKE_ARGS are compatibility channels. Note that conda-style environments export CMAKE_ARGS with toolchain -D flags on activation, so tier 1 is not hypothetical.
  • Stickiness is tier 4 by design: a flag or variable dropped on the next build leaves its value in the cache. The documented reset is -C cmake.fresh=true (scikit-build-core 1.1, see Bump scikit-build-core to 1.1.1 and make use of its features #200913), not deleting build/.
  • CMake configure presets, when we adopt them: cacheVariables behave as a tier-3 file channel (re-applied each configure, lose to -D); a preset's environment block overrides the shell environment inside the cmake process and so ranks above tier 1 for CMake-side reads, the one place a file beats the environment. To be documented when presets land.

Alternatives

The common CLI convention is the reverse at the top: defaults < config files < environment < command-line flags, so that an ambient USE_CUDA=0 cannot defeat a deliberate -C cmake.define.USE_CUDA=ON. Restoring that for the prefixed names was the first draft of this proposal and was dropped for two reasons. scikit-build-core resolves its own settings and every declared config-setting as environment > -C > default and a project cannot invert that, so flags-over-environment could only ever apply to part of the surface, and a build with two orders is worse than a build with one unconventional order. And the objection is answered by the shell: USE_CUDA=0 pip install -e . is the per-invocation spelling of an environment variable, as near to the process as a flag, and it is the only spelling that reaches every consumer (the cache, $ENV{} reads in CMake code, child Python processes, spin develop); no flag reaches an environment-only consumer.

Doing nothing keeps two orders and leaves them undocumented; the inconsistency was found while reviewing #200651, whose documentation has to state one.

Additional context

The precedence rows were measured against scikit-build-core 1.0.3 and 1.1.1 (sources.py, builder.py) and cmake/EnvVarForwarding.cmake on main; the test in #201666 pins tiers 1-4 with a real configure. Related upstream: declared config-settings scikit-build/scikit-build-core#1489; configure-preset support scikit-build/scikit-build-core#790 and #994.

cc @malfet @Skylion007 @janeyx99 @albanD @atalman

Activity

  1. added
    module: build
    triaged
    enhancement
    bot-triaged
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bot-triagedenhancementmodule: buildtriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions