use {
rustc_version::version,
std::{
env::current_dir,
fs::{read_to_string, write}
}
};
fn main() {
let v = version().expect("failed to get rustc version for build");
let cwd = current_dir().expect("failed to get current directory");
let raw_src = cwd.join("src");
let src_path = cwd.join("src/direct.rs.src");
let dst_path = cwd.join("src/direct.rs");
println!("cargo:rerun-if-changed={}", raw_src.display());
println!("cargo:rerun-if-env-changed=RUSTC");
let src_contents = read_to_string(&src_path).expect("failed to read source file");
let generated = if v.minor > 81 {
src_contents
} else {
src_contents.as_str().replace(
"#[unsafe(link_section = \".init_array.00099\")]",
"#[link_section = \".init_array.00099\"]"
)
};
let need_write = match read_to_string(&dst_path) {
Ok(existing) => existing != generated,
Err(_) => true
};
if need_write {
write(&dst_path, generated.as_bytes()).expect("failed to write destination file");
}
}