build.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. // NOTE: Adapted from cortex-m/build.rs
  2. extern crate riscv_target;
  3. use riscv_target::Target;
  4. use std::env;
  5. use std::fs;
  6. use std::path::PathBuf;
  7. fn main() {
  8. let target = env::var("TARGET").unwrap();
  9. let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
  10. let name = env::var("CARGO_PKG_NAME").unwrap();
  11. if target.starts_with("riscv") {
  12. let mut target = Target::from_target_str(&target);
  13. target.retain_extensions("imfdc");
  14. let archive: String;
  15. if cfg!(feature = "sbi") {
  16. println!("======== compiling riscv-rt for sbi");
  17. archive = format!("bin/{}-sbi.a", target.to_string());
  18. } else {
  19. archive = format!("bin/{}.a", target.to_string());
  20. }
  21. fs::copy(&archive, out_dir.join(format!("lib{}.a", name))).unwrap();
  22. println!("cargo:rerun-if-changed={}", archive);
  23. println!("cargo:rustc-link-lib=static={}", name);
  24. }
  25. // Put the linker script somewhere the linker can find it
  26. fs::write(out_dir.join("link.x"), include_bytes!("link.x")).unwrap();
  27. println!("cargo:rustc-link-search={}", out_dir.display());
  28. println!("cargo:rerun-if-changed=link.x");
  29. }