Skip to content

Avoid RUSTFLAGS as much as possible #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion crates/cli-tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## 0.1.0-git

<!-- Increment to skip CHANGELOG.md test: 8 -->
<!-- Increment to skip CHANGELOG.md test: 11 -->
34 changes: 19 additions & 15 deletions crates/cli-tools/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ impl RustAppletBuild {
let target_dir = fs::try_relative(std::env::current_dir()?, &metadata.target_directory)?;
let name = package.name.replace('-', "_");
let mut cargo = Command::new("cargo");
let mut rustflags = vec![
"-C panic=abort".to_string(),
"-C codegen-units=1".to_string(),
"-C embed-bitcode=yes".to_string(),
"-C lto=fat".to_string(),
];
let mut rustflags = Vec::new();
cargo.args(["rustc", "--lib"]);
// We deliberately don't use the provided profile for those configs because they don't
// depend on user-provided options (as opposed to opt-level).
cargo.arg("--config=profile.release.codegen-units=1");
cargo.arg("--config=profile.release.lto=true");
cargo.arg("--config=profile.release.panic=\"abort\"");
match &self.native {
None => {
rustflags.push(format!("-C link-arg=-zstack-size={}", self.stack_size));
Expand All @@ -116,12 +116,10 @@ impl RustAppletBuild {
wasefire_feature(package, "native", &mut cargo)?;
}
}
match &self.profile {
Some(profile) => drop(cargo.arg(format!("--profile={profile}"))),
None => drop(cargo.arg("--release")),
}
let profile = self.profile.as_deref().unwrap_or("release");
cargo.arg(format!("--profile={profile}"));
if let Some(level) = self.opt_level {
rustflags.push(format!("-C opt-level={level}"));
cargo.arg(format!("--config=profile.{profile}.opt-level={level}"));
}
cargo.args(&self.cargo);
if self.prod {
Expand All @@ -137,8 +135,8 @@ impl RustAppletBuild {
None => "target/wasefire".into(),
};
let (src, dst) = match &self.native {
None => (format!("wasm32-unknown-unknown/release/{name}.wasm"), "applet.wasm"),
Some(target) => (format!("{target}/release/lib{name}.a"), "libapplet.a"),
None => (format!("wasm32-unknown-unknown/{profile}/{name}.wasm"), "applet.wasm"),
Some(target) => (format!("{target}/{profile}/lib{name}.a"), "libapplet.a"),
};
let applet = out_dir.join(dst);
if fs::copy_if_changed(target_dir.join(src), &applet)? && dst.ends_with(".wasm") {
Expand Down Expand Up @@ -186,7 +184,13 @@ pub enum OptLevel {

impl Display for OptLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_possible_value().unwrap().get_name())
let value = self.to_possible_value().unwrap();
let name = value.get_name();
if f.alternate() || !matches!(self, OptLevel::Os | OptLevel::Oz) {
write!(f, "{name}")
} else {
write!(f, "{name:?}")
}
}
}

Expand All @@ -198,7 +202,7 @@ pub fn optimize_wasm(applet: impl AsRef<Path>, opt_level: Option<OptLevel>) -> R
let mut opt = Command::new("wasm-opt");
opt.args(["--enable-bulk-memory", "--enable-sign-ext", "--enable-mutable-globals"]);
match opt_level {
Some(level) => drop(opt.arg(format!("-O{level}"))),
Some(level) => drop(opt.arg(format!("-O{level:#}"))),
None => drop(opt.arg("-O")),
}
opt.arg(applet.as_ref());
Expand Down
1 change: 0 additions & 1 deletion crates/xtask/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ probe-rs = "0.24.0"
rustc-demangle = "0.1.24"
serde = { version = "1.0.202", features = ["derive"] }
stack-sizes = "0.5.0"
strum = { version = "0.26.2", features = ["derive"] }
wasefire-cli-tools = { version = "0.1.0-git", path = "../cli-tools" }

[lints]
Expand Down
29 changes: 5 additions & 24 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use lazy_static::lazy_static;
use probe_rs::config::TargetSelector;
use probe_rs::{flashing, Permissions, Session};
use rustc_demangle::demangle;
use strum::{Display, EnumString};
use wasefire_cli_tools::{action, cmd, fs};

mod footprint;
Expand Down Expand Up @@ -167,7 +166,7 @@ struct RunnerOptions {

/// Optimization level (0, 1, 2, 3, s, z).
#[clap(long, short = 'O')]
opt_level: Option<OptLevel>,
opt_level: Option<action::OptLevel>,

/// Produces target/wasefire/platform_{side}.bin files instead of flashing.
#[clap(long)]
Expand Down Expand Up @@ -218,22 +217,6 @@ struct RunnerOptions {
memory_page_count: Option<usize>,
}

#[derive(Copy, Clone, EnumString, Display)]
enum OptLevel {
#[strum(serialize = "0")]
O0,
#[strum(serialize = "1")]
O1,
#[strum(serialize = "2")]
O2,
#[strum(serialize = "3")]
O3,
#[strum(serialize = "s")]
Os,
#[strum(serialize = "z")]
Oz,
}

impl Flags {
fn execute(self) -> Result<()> {
match self.command {
Expand Down Expand Up @@ -413,17 +396,15 @@ impl RunnerOptions {
if main.release {
cargo.arg("-Zbuild-std=core,alloc");
cargo.arg("-Zbuild-std-features=panic_immediate_abort");
rustflags.push("-C lto=fat".to_string());
rustflags.push("-C codegen-units=1".to_string());
rustflags.push("-C embed-bitcode=yes".to_string());
cargo.arg("--config=profile.release.codegen-units=1");
cargo.arg("--config=profile.release.lto=true");
} else {
cargo.arg("--config=profile.release.debug=2");
rustflags.push("-C link-arg=-Tdefmt.x".to_string());
rustflags.push("-C debuginfo=2".to_string());
rustflags.push("-C strip=none".to_string());
}
}
if let Some(level) = self.opt_level {
rustflags.push(format!("-C opt-level={level}"));
cargo.arg(format!("--config=profile.release.opt-level={level}"));
}
if main.release {
features.push("release".to_string());
Expand Down
Loading