repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
xtask/src/guest_build.rs
Raw//! Canonical Cargo invocation for release Wasm guests (`SMH-SPEC-SPEC0001`).
use std::{
env,
ffi::OsString,
path::{Path, PathBuf},
process::Command,
};
/// Canonical release policy for embedded Wasm guests, independent of host preferences.
pub(crate) const GUEST_RELEASE_PROFILE: &[(&str, &str)] = &[
("CARGO_PROFILE_RELEASE_OPT_LEVEL", "3"),
("CARGO_PROFILE_RELEASE_LTO", "thin"),
("CARGO_PROFILE_RELEASE_CODEGEN_UNITS", "1"),
("CARGO_PROFILE_RELEASE_STRIP", "symbols"),
("CARGO_PROFILE_RELEASE_PANIC", "abort"),
("CARGO_PROFILE_RELEASE_DEBUG", "false"),
("CARGO_PROFILE_RELEASE_INCREMENTAL", "false"),
("CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS", "false"),
("CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS", "false"),
("CARGO_PROFILE_RELEASE_RPATH", "false"),
];
/// Cargo invocation for a release guest whose bytes are embedded by a host.
///
/// Rust records panic paths in component code. Map workspace and Cargo home to
/// fixed prefixes so local and CI builds produce identical bytes.
pub(crate) fn canonical_guest_cargo_command(workspace: &Path) -> Result<Command, String> {
let workspace = workspace
.canonicalize()
.map_err(|error| format!("cannot resolve guest workspace directory: {error}"))?;
let cargo_home = cargo_home(&workspace)?;
let mut command = Command::new("cargo");
command
.current_dir(&workspace)
.env_remove("RUSTFLAGS")
.env_remove("CARGO_ENCODED_RUSTFLAGS")
.env(
"CARGO_ENCODED_RUSTFLAGS",
guest_rustflags(&workspace, &cargo_home)?,
)
.envs(GUEST_RELEASE_PROFILE.iter().copied());
Ok(command)
}
/// Resolve Cargo's home exactly as Cargo does for this workspace invocation.
fn cargo_home(workspace: &Path) -> Result<PathBuf, String> {
let home = match env::var_os("CARGO_HOME") {
Some(home) => PathBuf::from(home),
None => env::var_os("HOME")
.map(PathBuf::from)
.map(|home| home.join(".cargo"))
.ok_or("cannot determine Cargo home: CARGO_HOME and HOME are unset")?,
};
let home = if home.is_absolute() {
home
} else {
workspace.join(home)
};
home.canonicalize()
.map_err(|error| format!("cannot resolve Cargo home {}: {error}", home.display()))
}
/// Rust flags separated by Cargo's documented unit-separator encoding.
fn guest_rustflags(workspace: &Path, cargo_home: &Path) -> Result<OsString, String> {
let workspace = remap_path_flag(workspace, "/smith")?;
let cargo_home = remap_path_flag(cargo_home, "/cargo")?;
Ok([
OsString::from("-C"),
OsString::from("target-cpu=generic"),
workspace,
cargo_home,
]
.join(&OsString::from("\u{1f}")))
}
/// One lossless remap flag; Rust flags cannot encode the unit separator.
fn remap_path_flag(path: &Path, destination: &str) -> Result<OsString, String> {
let path = path
.to_str()
.ok_or_else(|| format!("guest path is not Unicode: {}", path.display()))?;
if path.contains('\u{1f}') {
return Err(format!("guest path contains Cargo flag separator: {path}"));
}
Ok(format!("--remap-path-prefix={path}={destination}").into())
}