Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cuda-oxide/crates/cargo-oxide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,39 @@ Relative backend paths are resolved from the `.cargo` directory containing the
config file. Each `extra-rustflags` array element remains one rustc argument,
including values that contain spaces.

Flags that belong to one host CPU, such as `-C target-cpu=...`, go in a
`[target.<triple>]` table, so one config serves several host architectures:

```toml
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"]
```

A build gets the top-level list, then the table of the triple it builds for.
That triple is, in order:

1. a `--target` among the Cargo arguments, which follow the first `--`
(`cargo oxide build -- --target aarch64-unknown-linux-gnu`); a later `--`
that starts test or program arguments ends the search;
2. `CARGO_BUILD_TARGET`, which also serves the commands that take no Cargo
arguments, such as `cargo oxide run <example>`;
3. the host triple of `rustc -vV`, run as the build would run it: `RUSTC`,
else `CARGO_BUILD_RUSTC`, else `rustc`, from the project directory.

`host-tuple` in 1 or 2 means the host triple of 3. A triple with no table gets
the top-level list alone. A config without `[target.*]` tables behaves as
before and runs no extra `rustc -vV`. `[build] target` and `[build] rustc` in
a Cargo config file are not consulted, and the probe in 3 does not go
through `RUSTC_WRAPPER`; pass `--target` when you cross-compile.
The only field of a table is `extra-rustflags`; `cfg(...)` keys and other
fields are refused, and so are two different `--target` values in one command,
since each triple's flags would apply to both.

Configuration values are defaults. Precedence is:

1. explicit `cargo oxide` flags and internal artifact paths;
Expand Down
148 changes: 145 additions & 3 deletions cuda-oxide/crates/cargo-oxide/src/commands/codegen_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

use crate::{backend, cutlass_toolchain, platform};
use std::ffi::OsStr;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::Command;

Expand Down Expand Up @@ -352,6 +352,7 @@ pub(super) fn apply_host_linker_rustflags_or_exit(cmd: &mut Command, ctx: &Conte
/// every configured array element and `--device-cfg` value intact.
fn build_encoded_rustflags(
ctx: &Context,
configured_rustflags: &[String],
profile: CodegenProfilePolicy,
device_cfgs: &[String],
) -> Result<String, String> {
Expand All @@ -366,7 +367,7 @@ fn build_encoded_rustflags(
Ok(build_encoded_rustflags_with_existing(
&ctx.backend_so,
profile,
&ctx.config.extra_rustflags,
configured_rustflags,
&explicit_rustflags,
existing_encoded.as_deref(),
existing.as_deref(),
Expand Down Expand Up @@ -569,14 +570,155 @@ pub(super) fn append_full_debug_rustflags(
}
}

/// Cargo's name for the host triple in `--target` and `CARGO_BUILD_TARGET`.
const HOST_TUPLE: &str = "host-tuple";

/// The target triple a Cargo command builds for, as far as `extra-rustflags`
/// selection needs it: an explicit `--target` among the Cargo arguments,
/// before any `--` that starts the program's or test binary's arguments, else
/// `CARGO_BUILD_TARGET` (the command's own, then the inherited one). `None`
/// means the command names none, so Cargo builds for the host. `host-tuple`
/// resolves through `host_triple`, which is asked at most once.
///
/// `[build] target` in a Cargo config file is not read here; a project that
/// sets it and has `[target.<triple>]` tables passes `--target` instead.
pub(super) fn explicit_cargo_target(
cmd: &Command,
inherited_build_target: Option<&str>,
host_triple: &mut dyn FnMut() -> Option<String>,
) -> Result<Option<String>, String> {
let mut host: Option<Option<String>> = None;
let mut resolve = |value: String| -> Result<String, String> {
if value != HOST_TUPLE {
return Ok(value);
}
host.get_or_insert_with(&mut *host_triple)
.clone()
.ok_or_else(|| {
"cuda-oxide config `[target.<triple>] extra-rustflags`: the build's target is \
`host-tuple`, and `rustc -vV` could not be run or printed no host line"
.to_string()
})
};
let mut named: Vec<String> = Vec::new();
let mut args = cmd.get_args().map(|arg| arg.to_string_lossy().into_owned());
while let Some(arg) = args.next() {
if arg == "--" {
break;
}
if arg == "--target" {
if let Some(value) = args.next() {
named.push(resolve(value)?);
}
} else if let Some(value) = arg.strip_prefix("--target=") {
named.push(resolve(value.to_string())?);
}
}
named.sort();
named.dedup();
match named.as_slice() {
[] => {}
[one] => return Ok(Some(one.clone())),
several => {
return Err(format!(
"cuda-oxide config `[target.<triple>] extra-rustflags` selects one triple's \
flags, and this command names several targets: {}",
several.join(", ")
));
}
}
let own = cmd
.get_envs()
.find(|(name, _)| *name == OsStr::new("CARGO_BUILD_TARGET"))
.map(|(_, value)| value.map(|v| v.to_string_lossy().into_owned()));
match own {
Some(value) => value,
None => inherited_build_target.map(str::to_string),
}
.filter(|value| !value.is_empty())
.map(resolve)
.transpose()
}

/// `rustc -vV` as Cargo would run it for `cmd`: the compiler is `RUSTC`, else
/// `CARGO_BUILD_RUSTC` (each the command's own value before the inherited
/// one), else `rustc` from `PATH`; it runs in `cmd`'s directory with `cmd`'s
/// environment changes, so a toolchain file or a `RUSTUP_TOOLCHAIN` the build
/// sees applies to the probe too. `[build] rustc` in a Cargo config file is
/// not read, and `RUSTC_WRAPPER` / `RUSTC_WORKSPACE_WRAPPER` are not applied:
/// the probe asks the compiler itself.
pub(super) fn host_probe_command(
cmd: &Command,
inherited: &dyn Fn(&str) -> Option<OsString>,
) -> Command {
let pick = |name: &str| {
let own = cmd
.get_envs()
.find(|(key, _)| *key == OsStr::new(name))
.map(|(_, value)| value.map(OsStr::to_os_string));
match own {
Some(value) => value,
None => inherited(name),
}
.filter(|value| !value.is_empty())
};
let program = pick("RUSTC")
.or_else(|| pick("CARGO_BUILD_RUSTC"))
.unwrap_or_else(|| OsString::from("rustc"));
let mut probe = Command::new(program);
probe.arg("-vV");
if let Some(dir) = cmd.get_current_dir() {
probe.current_dir(dir);
}
for (key, value) in cmd.get_envs() {
match value {
Some(value) => probe.env(key, value),
None => probe.env_remove(key),
};
}
probe
}

/// The `host:` line of a `rustc -vV` run; `None` when it cannot run, fails,
/// or prints none.
pub(super) fn host_triple_of(mut probe: Command) -> Option<String> {
let output = probe.output().ok()?;
if !output.status.success() {
return None;
}
String::from_utf8_lossy(&output.stdout)
.lines()
.find_map(|line| line.strip_prefix("host: ").map(str::to_owned))
}

/// The project rustflags for `cmd`'s build: `ctx`'s top-level list, then the
/// table of the triple the command builds for. The host triple is asked of
/// the build's own `rustc -vV` ([`host_probe_command`]) only when the config
/// has `[target.<triple>]` tables and the command names no target or names
/// `host-tuple`.
fn configured_rustflags_for_command(ctx: &Context, cmd: &Command) -> Result<Vec<String>, String> {
if ctx.config.target_extra_rustflags.is_empty() {
return ctx.config.extra_rustflags_for(None);
}
let inherited = |name: &str| std::env::var_os(name);
let mut probe_host = || host_triple_of(host_probe_command(cmd, &inherited));
let inherited_target = std::env::var("CARGO_BUILD_TARGET").ok();
let triple = match explicit_cargo_target(cmd, inherited_target.as_deref(), &mut probe_host)? {
Some(triple) => Some(triple),
None => probe_host(),
};
ctx.config.extra_rustflags_for(triple.as_deref())
}

/// Set `CARGO_ENCODED_RUSTFLAGS`, `RUSTC_BOOTSTRAP`, and profile pins on `cmd`.
fn apply_codegen_rustflags(
cmd: &mut Command,
ctx: &Context,
profile: CodegenProfilePolicy,
device_cfgs: &[String],
) -> Result<(), String> {
let mut encoded = build_encoded_rustflags(ctx, profile, device_cfgs)?;
let configured = configured_rustflags_for_command(ctx, cmd)?;
let mut encoded = build_encoded_rustflags(ctx, &configured, profile, device_cfgs)?;
let inherited_debug = std::env::var("CUDA_OXIDE_DEBUG").ok();
append_full_debug_rustflags(&mut encoded, cmd, inherited_debug.as_deref());

Expand Down
100 changes: 99 additions & 1 deletion cuda-oxide/crates/cargo-oxide/src/commands/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

use crate::backend;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use super::*;
Expand All @@ -15,12 +16,49 @@ pub struct OxideConfig {
pub backend: Option<PathBuf>,
/// Default CUDA architecture for codegen commands.
pub default_arch: Option<String>,
/// Additional rustflags appended after cuda-oxide's required flags.
/// Additional rustflags appended after cuda-oxide's required flags, for
/// every target triple (top-level `extra-rustflags`).
pub extra_rustflags: Vec<String>,
/// `[target.<triple>] extra-rustflags`: appended after `extra_rustflags`
/// only when the build's target triple is `<triple>`. A triple with no
/// table gets `extra_rustflags` alone.
pub target_extra_rustflags: BTreeMap<String, Vec<String>>,
/// Environment variables applied to child Cargo invocations.
pub env: Vec<(String, String)>,
}

impl OxideConfig {
/// The project rustflags for a build of `triple`: the top-level list, then
/// the triple's own table. rustc keeps the last of a repeated `-C` option,
/// so a triple's `-C target-cpu` overrides a top-level one.
///
/// `triple` is only consulted when a `[target.<triple>]` table exists; with
/// tables present and no triple known the result is an error, never the
/// flags of some other triple.
pub fn extra_rustflags_for(&self, triple: Option<&str>) -> Result<Vec<String>, String> {
let mut flags = self.extra_rustflags.clone();
if self.target_extra_rustflags.is_empty() {
return Ok(flags);
}
let Some(triple) = triple else {
return Err(format!(
"cuda-oxide config has `[target.<triple>] extra-rustflags` for {}, but the \
build's target triple is unknown (no `--target`, no CARGO_BUILD_TARGET, and \
the build's `rustc -vV` could not be run or printed no host line)",
self.target_extra_rustflags
.keys()
.map(String::as_str)
.collect::<Vec<_>>()
.join(", ")
));
};
if let Some(own) = self.target_extra_rustflags.get(triple) {
flags.extend(own.iter().cloned());
}
Ok(flags)
}
}

/// Pre-resolved context shared across all commands.
///
/// Built once at startup by [`resolve_context`] and passed by reference to
Expand Down Expand Up @@ -342,6 +380,16 @@ pub(super) fn inspect_oxide_config(workspace_root: &Path) -> OxideConfigInspecti
Vec::new()
}
};
let target_extra_rustflags = match table.get("target") {
None => BTreeMap::new(),
Some(value) => match parse_config_targets(value, &config_path) {
Ok(targets) => targets,
Err(error) => {
errors.push(error);
BTreeMap::new()
}
},
};
let env = match table.get("env") {
None => Vec::new(),
Some(value) => match parse_config_env(value, &config_path) {
Expand Down Expand Up @@ -373,6 +421,7 @@ pub(super) fn inspect_oxide_config(workspace_root: &Path) -> OxideConfigInspecti
backend,
default_arch,
extra_rustflags,
target_extra_rustflags,
env,
},
warnings,
Expand Down Expand Up @@ -489,6 +538,55 @@ fn optional_config_string_array(
}
}

/// Parse `[target.<triple>]` tables. Each holds only `extra-rustflags`; a key
/// is a plain target triple. Cargo's `cfg(...)` keys and any other field are
/// refused by name rather than ignored, since an ignored table would build
/// without the flags its author wrote.
fn parse_config_targets(
value: &toml::Value,
config_path: &Path,
) -> Result<BTreeMap<String, Vec<String>>, String> {
let table = value.as_table().ok_or_else(|| {
format!(
"cuda-oxide config {} field `target` must be a table of `[target.<triple>]` tables",
config_path.display()
)
})?;
let mut targets = BTreeMap::new();
for (triple, entry) in table {
if triple.starts_with("cfg(") {
return Err(format!(
"cuda-oxide config {} `[target.'{triple}']`: cfg expressions are not \
supported; name the target triple",
config_path.display()
));
}
let entry = entry.as_table().ok_or_else(|| {
format!(
"cuda-oxide config {} `target.{triple}` must be a table",
config_path.display()
)
})?;
if let Some(key) = entry.keys().find(|key| key.as_str() != "extra-rustflags") {
return Err(format!(
"cuda-oxide config {} `[target.{triple}]` has unknown field `{key}`; \
the only field is `extra-rustflags`",
config_path.display()
));
}
let flags =
optional_config_string_array(entry, "extra-rustflags", config_path).map_err(|_| {
format!(
"cuda-oxide config {} `[target.{triple}]` field `extra-rustflags` must be \
an array of strings",
config_path.display()
)
})?;
targets.insert(triple.clone(), flags);
}
Ok(targets)
}

fn parse_config_env(
value: &toml::Value,
config_path: &Path,
Expand Down
Loading