use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{env, ffi::OsStr};
fn main() {
let mut target = env::var("TARGET").unwrap();
let path = Path::new(&target);
if path.extension() == Some(OsStr::new("json")) {
target = path
.file_stem()
.map_or(target.clone(), |stem| stem.to_str().unwrap().to_string());
}
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
let link_x = include_bytes!("link.x.in");
let mut f = if env::var_os("CARGO_FEATURE_DEVICE").is_some() {
let mut f = File::create(out.join("link.x")).unwrap();
f.write_all(link_x).unwrap();
writeln!(
f,
r#"
/* Provides weak aliases (cf. PROVIDED) for device specific interrupt handlers */
/* This will usually be provided by a device crate generated using svd2rust (see `device.x`) */
INCLUDE device.x"#
)
.unwrap();
f
} else {
let mut f = File::create(out.join("link.x")).unwrap();
f.write_all(link_x).unwrap();
f
};
println!("cargo:rustc-check-cfg=cfg(armv6m)");
println!("cargo:rustc-check-cfg=cfg(armv7m)");
println!("cargo:rustc-check-cfg=cfg(armv8m)");
println!("cargo:rustc-check-cfg=cfg(cortex_m)");
println!("cargo:rustc-check-cfg=cfg(has_fpu)");
let max_int_handlers = if target.starts_with("thumbv6m-") {
println!("cargo:rustc-cfg=cortex_m");
println!("cargo:rustc-cfg=armv6m");
32
} else if target.starts_with("thumbv7m-") || target.starts_with("thumbv7em-") {
println!("cargo:rustc-cfg=cortex_m");
println!("cargo:rustc-cfg=armv7m");
240
} else if target.starts_with("thumbv8m") {
println!("cargo:rustc-cfg=cortex_m");
println!("cargo:rustc-cfg=armv8m");
496
} else {
240
};
if target.ends_with("-eabihf") {
println!("cargo:rustc-cfg=has_fpu");
}
writeln!(
f,
r#"
ASSERT(SIZEOF(.vector_table) <= 0x{:x}, "
There can't be more than {1} interrupt handlers. This may be a bug in
your device crate, or you may have registered more than {1} interrupt
handlers.");
"#,
max_int_handlers * 4 + 0x40,
max_int_handlers
)
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=link.x.in");
}