build.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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::io::Write;
  7. use std::path::PathBuf;
  8. fn main() {
  9. let target = env::var("TARGET").unwrap();
  10. let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
  11. let name = env::var("CARGO_PKG_NAME").unwrap();
  12. if target.starts_with("riscv") {
  13. let mut target = Target::from_target_str(&target);
  14. target.retain_extensions("imc");
  15. let target = target.to_string();
  16. fs::copy(
  17. format!("bin/{}.a", target),
  18. out_dir.join(format!("lib{}.a", name)),
  19. ).unwrap();
  20. println!("cargo:rustc-link-lib=static={}", name);
  21. println!("cargo:rustc-link-search={}", out_dir.display());
  22. }
  23. // Put the linker script somewhere the linker can find it
  24. fs::File::create(out_dir.join("link.x"))
  25. .unwrap()
  26. .write_all(include_bytes!("link.x"))
  27. .unwrap();
  28. println!("cargo:rustc-link-search={}", out_dir.display());
  29. println!("cargo:rerun-if-changed=build.rs");
  30. println!("cargo:rerun-if-changed=link.x");
  31. }