Add check for opt-level="z" for esp32c3

This commit is contained in:
Seth Pellegrino 2022-09-26 11:01:25 -07:00
parent 7889a992d7
commit c6ac87f4dc
2 changed files with 39 additions and 8 deletions

View File

@ -41,13 +41,14 @@ smart-leds = "0.3.0"
ssd1306 = "0.7.1"
[features]
default = ["rt", "vectored"]
direct-boot = []
eh1 = ["esp-hal-common/eh1"]
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"]
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"

View File

@ -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());
@ -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"))
@ -71,3 +75,29 @@ fn add_defaults() {
println!("cargo:rustc-link-search={}", out.display());
}
const OPT_LEVEL_Z_MSG: &str = r#"opt-level=z will produce broken interrupt handling code for this architecture, causing an 'attempt to subtract with overflow' panic if an enabled interrupt is triggered.
Please use `opt-level="s"` in lieu of "z", or alternatively enable `features = ["allow-opt-level-z"]` to supress this error.
For more information, see: https://github.com/esp-rs/esp-hal/issues/196
"#;
// 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!("cargo:warning={}", OPT_LEVEL_Z_MSG);
exit(1);
}
}
_ => {}
}
}