use std::{env, fs::File, io::Write, path::PathBuf, process::Command}; fn main() { // 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")) .unwrap() .write_all(include_bytes!("memory.x")) .unwrap(); println!("cargo:rustc-link-search={}", out.display()); // Only re-run the build script when memory.x is changed, // instead of when any part of the source code changes. println!("cargo:rerun-if-changed=memory.x"); let revision = env::var("BUILD_REVISION"); match revision { Ok(r) => { // Skip if the environment variable is already set println!("cargo:rustc-env=BUILD_REVISION={}", r); } Err(_) => { let output = Command::new("git") .args(&["rev-parse", "--short=7", "HEAD"]) .output() .expect("Failed to execute git command"); let dirty = !Command::new("git") .args(&["status", "-s"]) .output() .expect("Failed to execute git command").stdout.is_empty(); let build_revision: String; if dirty { build_revision = String::from_utf8(output.stdout) .expect("Invalid UTF-8 sequence") + "-dirty"; } else { build_revision = String::from_utf8(output.stdout) .expect("Invalid UTF-8 sequence"); } println!("cargo:rustc-env=BUILD_REVISION={}", build_revision.trim()); } }; }