build.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. use std::path::PathBuf;
  2. use std::{env, fs};
  3. fn read_dependent_script(script: &str) -> Result<String, Box<dyn std::error::Error>> {
  4. let content = fs::read_to_string(script)?;
  5. println!("cargo:rerun-if-changed={}", script);
  6. Ok(content)
  7. }
  8. fn process_ldscript_x86(script: &mut String) -> Result<(), Box<dyn std::error::Error>> {
  9. // Otherwise `bootstrap.rs` might be ignored and not linked in.
  10. println!("cargo:extra-link-args=--undefined=move_mbr --no-check-sections");
  11. let memory = read_dependent_script("src/arch/x86_64/memory.x")?;
  12. let link = read_dependent_script("src/arch/x86_64/link.x")?;
  13. *script = memory + script;
  14. script.push_str(&link);
  15. Ok(())
  16. }
  17. fn process_ldscript_arch(
  18. script: &mut String,
  19. arch: &str,
  20. ) -> Result<(), Box<dyn std::error::Error>> {
  21. match arch {
  22. "x86_64" => {
  23. process_ldscript_x86(script)?;
  24. }
  25. _ => panic!("Unsupported architecture: {}", arch),
  26. }
  27. Ok(())
  28. }
  29. fn main() -> Result<(), Box<dyn std::error::Error>> {
  30. let out_dir = PathBuf::from(env::var("OUT_DIR")?);
  31. let out_script = out_dir.join("link.x");
  32. let in_script = "src/link.x.in";
  33. let mut script = read_dependent_script(in_script)?;
  34. process_ldscript_arch(&mut script, &env::var("CARGO_CFG_TARGET_ARCH")?)?;
  35. fs::write(out_script, script)?;
  36. println!("cargo:rustc-link-search={}", out_dir.display());
  37. Ok(())
  38. }