Skip to content

feat(cargo-oxide): select extra-rustflags by target triple - #1825

Open
avlp12 wants to merge 2 commits into
NVIDIA:mainfrom
avlp12:feat/cargo-oxide-per-target-extra-rustflags
Open

avlp12 wants to merge 2 commits into
NVIDIA:mainfrom
avlp12:feat/cargo-oxide-per-target-extra-rustflags

Conversation

@avlp12

@avlp12 avlp12 commented Nov 15, 2026 •

Copy link
Copy Markdown

Summary

Closes #1481.

extra-rustflags in .cargo/cuda-oxide.toml is one flat list, and the CARGO_ENCODED_RUSTFLAGS that cargo-oxide exports hides Cargo's own target.<triple>.rustflags. A CPU flag meant for one host therefore reaches every host: a config carrying -C target-cpu=znver3 for an x86_64 workstation breaks the build on an aarch64 DGX Spark. This PR adds [target.<triple>] tables whose extra-rustflags are appended after the top-level list for the triple being built.

extra-rustflags = ["--cfg", "my_device_cfg"]   # every triple

[target.x86_64-unknown-linux-gnu]
extra-rustflags = ["-C", "target-cpu=znver3"]

[target.aarch64-unknown-linux-gnu]
extra-rustflags = ["-C", "target-cpu=native"]

Changes

  • commands/context.rs: parse [target.<triple>] tables into target_extra_rustflags. extra_rustflags_for(triple) returns the top-level list, then the table of that triple. cfg(...) keys, fields other than extra-rustflags, and non-table values are named errors.
  • commands/codegen_env.rs: find the triple a command builds for, in this order:
    1. a --target among the Cargo arguments (after the first --; a later -- ends the search);
    2. CARGO_BUILD_TARGET;
    3. the host triple of rustc -vV, run as the build would run it (RUSTC, else CARGO_BUILD_RUSTC, else rustc, with the command's directory and environment).
  • host-tuple resolves to the host triple. Two different targets in one command are refused.
  • A config without [target.*] tables behaves as before and runs no extra rustc -vV.
  • README.md: document the tables and the lookup order.
  • commands/tests.rs: 12 new tests for the selection rules, the host probe, and the config-shape errors.

Not covered, as noted in #1481: [build] target and [build] rustc in a Cargo config file are not read, and the host probe does not go through RUSTC_WRAPPER. I am happy to change either if maintainers prefer.

Testing

Run on x86_64 Linux (WSL2) with the toolchain pinned in cuda-oxide/rust-toolchain.toml (1.99.1), on top of main at 348895e. just is not installed on that machine, so I ran the commands of the check recipe directly.

cargo test -p cargo-oxide: 301 passed on main, 313 passed on this branch, 0 failed on both.

check step Result
fmt-check pass
clippy (workspace and rustc-codegen-cuda, -D warnings) pass
test pass
test-cuda pass
check-intrinsics (with base_ref=origin/main) pass
doc-check pass
check-guards the 17 check-*.sh scripts and check-spdx-headers.sh pass; four items did not run (below)

Four guard items could not run on that machine, and they fail the same way on main there: check-device-only-build.sh (offline, the fixture's crates were not available), the cargo deny steps and check-example-license-policy.sh (cargo-deny not installed), and check-dependency-licenses.sh (needs the root-workspace toolchain 1.98.0, not installed). The change touches no manifest or lock file.

The version before this rebase was also tested on DGX Spark (aarch64); those results are in #1481.

  • just -f cuda-oxide/Justfile check passes (the local mirror of CI: fmt, clippy, tests, guards, docs) — all steps pass except the four guard items above, which did not run locally
  • cargo oxide run <example> passes, or cuda-oxide/scripts/smoketest.sh -o '^<example>$' — not run on this branch
  • New example added (if applicable) — not applicable

Checklist

  • All commits signed off (git commit -s)
  • All commits cryptographically signed and shown as Verified on GitHub (git commit -S)
  • SPDX headers on new source files (no new source files)

Questions about the review? Ping us in #contributors on Discord.

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Nov 15, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

avlp12 and others added 2 commits November 15, 2026 22:23
`extra-rustflags` in `.cargo/cuda-oxide.toml` was one flat list, and the
`CARGO_ENCODED_RUSTFLAGS` cargo-oxide exports hides Cargo's own
`target.<triple>.rustflags`. A project whose host code wants a CPU flag
therefore gave it to every host: `-C target-cpu=znver3` written for an
x86_64 workstation reached an aarch64 build (DGX Spark), where rustc warns
that znver3 is not a processor on every crate and crashes inside LLVM on
some.

A `[target.<triple>]` table now holds an `extra-rustflags` list for one
triple. A build gets the top-level list, then the table of the triple it
builds for: the command's `--target` (before any `--`), else
`CARGO_BUILD_TARGET`, else the host triple of `rustc -vV` run where Cargo
runs. A triple with no table gets the top-level list alone. With tables
present and no triple known, or two different `--target` values in one
command, the build stops with a named error instead of guessing. A config
without tables resolves exactly as before and runs no extra `rustc -vV`.
`cfg(...)` keys and fields other than `extra-rustflags` are refused by
name, so a table cargo-oxide cannot honor never builds without its flags;
`doctor` reports them through the existing config check.

Tests: a per-target config built with `--target aarch64-unknown-linux-gnu`
gets `-C target-cpu=native` and no znver3, and with `--target
x86_64-unknown-linux-gnu` the x86 list (red before this change: the table
was ignored and aarch64 got no flags); a flat list still reaches every
triple in the same position; selection order, unlisted and unknown
triples, the host fallback, `--target` parsing and the refused shapes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Alis Volat Propriis <alisvolatprop12@proton.me>
Review of the per-target `extra-rustflags` selection found three gaps:

- `--target host-tuple` (or `CARGO_BUILD_TARGET=host-tuple`), which
  Cargo accepts as the host triple, was looked up as a table named
  `host-tuple`, so the host's table was silently skipped. It now resolves
  through the host probe, asked at most once, and counts as the same
  target as the host's own name.
- The host probe ran `rustc` from PATH with the process environment. It
  now runs the compiler Cargo would run for the command (`RUSTC`, else
  `CARGO_BUILD_RUSTC`, the command's own value before the inherited one,
  else `rustc`) with the command's directory and environment changes, so
  a project `RUSTUP_TOOLCHAIN` reaches it too. The probe lives next to
  its one caller, and `backend::active_host_triple_in` goes.
- The README placed `--target` before `--`; Cargo arguments follow the
  first `--`. It now says so, names `CARGO_BUILD_TARGET` for commands
  without Cargo arguments, and lists `host-tuple`, the unread
  `[build] target` / `[build] rustc`, and that the probe does not go
  through `RUSTC_WRAPPER`.

The error for an unknown triple now also covers a probe that could not
run.

Tests: `host-tuple` on the command line and in both env sources, its
dedup against the host's name, one probe for several mentions, and the
named error when the probe fails; the probe's compiler choice, directory
and environment; and a fake compiler whose `host:` line is read, and one
that fails.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Alis Volat Propriis <alisvolatprop12@proton.me>
@avlp12
avlp12 force-pushed the feat/cargo-oxide-per-target-extra-rustflags branch from a77f18a to 54eb964 Compare November 15, 2026 15:27
@avlp12
avlp12 marked this pull request as ready for review November 15, 2026 15:28
@avlp12
avlp12 requested a review from nihalpasham as a code owner November 15, 2026 15:28

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(cargo-oxide): select extra-rustflags by target triple

1 participant