Skip to content

Commit

Permalink
Merge pull request #198 from rustbox/opt-level-z-check-prototype
Browse files Browse the repository at this point in the history
Check for opt-level="z" in `build.rs`, provide feedback about dangers
  • Loading branch information
bjoernQ authored Oct 11, 2022
2 parents ed9a074 + 3832008 commit 82c3ca2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
15 changes: 8 additions & 7 deletions esp32c3-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ smart-leds = "0.3.0"
ssd1306 = "0.7.1"

[features]
default = ["rt", "vectored"]
direct-boot = []
eh1 = ["esp-hal-common/eh1", "dep:embedded-hal-1", "dep:embedded-hal-nb"]
rt = ["riscv-rt"]
smartled = ["esp-hal-common/smartled"]
ufmt = ["esp-hal-common/ufmt"]
vectored = ["esp-hal-common/vectored"]
default = ["rt", "vectored"]
direct-boot = []
eh1 = ["esp-hal-common/eh1", "dep:embedded-hal-1", "dep:embedded-hal-nb"]
rt = ["riscv-rt"]
smartled = ["esp-hal-common/smartled"]
ufmt = ["esp-hal-common/ufmt"]
vectored = ["esp-hal-common/vectored"]
allow-opt-level-z = []

[[example]]
name = "hello_rgb"
Expand Down
44 changes: 43 additions & 1 deletion esp32c3-hal/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{env, fs::File, io::Write, path::PathBuf};
use std::{env, fs::File, io::Write, path::PathBuf, process::exit};

#[cfg(feature = "direct-boot")]
fn main() {
check_opt_level();

// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());

Expand Down Expand Up @@ -36,6 +38,8 @@ fn main() {

#[cfg(not(feature = "direct-boot"))]
fn main() {
check_opt_level();

// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
Expand Down Expand Up @@ -71,3 +75,41 @@ fn add_defaults() {

println!("cargo:rustc-link-search={}", out.display());
}

const OPT_LEVEL_Z_MSG: &str = r#"opt-level=z will produce broken 128-bit shifts (i.e. `1u128 << i`). The hal's interrupt handling relies on that operation, causing an 'attempt to subtract with overflow' panic if an enabled interrupt is triggered while using that opt-level.
Please use `opt-level="s"` in lieu of "z", or alternatively enable `features = ["allow-opt-level-z"]` to suppress this error. The latter option is only recommended if you:
* Do not use interrupts, and
* Do not have any shifts of 128-bit integers (either u128 or i128) in your code
See also: https://github.com/esp-rs/esp-hal/issues/196
and: https://github.com/llvm/llvm-project/issues/57988
"#;

// Once a rust nightly has a fix for https://github.com/llvm/llvm-project/issues/57988 , consider:
// 1. Removing this check in favor of bumping the minimum rust verison, and/or
// 2. Augmenting this check to ensure that version for opt-level=z
fn check_opt_level() {
if cfg!(feature = "allow-opt-level-z") {
return;
}

match env::var_os("OPT_LEVEL") {
Some(ref opt_level) => {
if opt_level == "z" {
println!(
"{}",
OPT_LEVEL_Z_MSG
.lines()
.into_iter()
.map(|l| format!("cargo:warning={l}"))
.collect::<Vec<String>>()
.join("\n")
);
exit(1);
}
}
_ => {}
}
}

0 comments on commit 82c3ca2

Please sign in to comment.