build.rs 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. //! This build script copies the `memory.x` file from the crate root into
  2. //! a directory where the linker can always find it at build time.
  3. //! For many projects this is optional, as the linker always searches the
  4. //! project root directory -- wherever `Cargo.toml` is. However, if you
  5. //! are using a workspace or have a more complicated build setup, this
  6. //! build script becomes required. Additionally, by requesting that
  7. //! Cargo re-run the build script whenever `memory.x` is changed,
  8. //! updating `memory.x` ensures a rebuild of the application with the
  9. //! new memory settings.
  10. use std::env;
  11. use std::fs::File;
  12. use std::io::Write;
  13. use std::path::PathBuf;
  14. fn main() {
  15. // Put `memory.x` in our output directory and ensure it's
  16. // on the linker search path.
  17. let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
  18. File::create(out.join("memory.x"))
  19. .unwrap()
  20. .write_all(include_bytes!("memory.x"))
  21. .unwrap();
  22. println!("cargo:rustc-link-search={}", out.display());
  23. // By default, Cargo will re-run a build script whenever
  24. // any file in the project changes. By specifying `memory.x`
  25. // here, we ensure the build script is only re-run when
  26. // `memory.x` is changed.
  27. println!("cargo:rerun-if-changed=memory.x");
  28. }