//! Implementation module //! //! **File:** `build.rs` //! **Author:** Kevin Thomas //! **Date:** 2025 //! //! MIT License //! //! Copyright (c) 2025 Kevin Thomas //! //! Permission is hereby granted, free of charge, to any person obtaining a copy //! of this software and associated documentation files (the "Software"), to deal //! in the Software without restriction, including without limitation the rights //! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //! copies of the Software, and to permit persons to whom the Software is //! furnished to do so, subject to the following conditions: //! //! The above copyright notice and this permission notice shall be included in //! all copies or substantial portions of the Software. //! //! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //! SOFTWARE. // Import dependencies from std::fs use std::fs::{read_to_string, File}; // Import std::io::Write use std::io::Write; // Import std::path::{Path, PathBuf} use std::path::{Path, PathBuf}; /// The main entry point. fn main() { let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); let c = read_to_string(".pico-rs").unwrap_or_default().trim().to_lowercase(); setup_target(&c, &out); write_riscv(&out); print_cfgs(&out); } /// Executes the setup target operation. /// /// # Arguments /// /// * `c` - Target string from config. /// * `out` - Output path. fn setup_target(c: &str, out: &Path) { if c == "rp2040" { write_rp2040(out); } else { write_rp2350(out); } } /// Executes the write rp2040 operation. /// /// # Arguments /// /// * `out` - Output path. fn write_rp2040(out: &Path) { let b = include_bytes!("rp2040.x"); File::create(out.join("memory.x")).unwrap().write_all(b).unwrap(); println!("cargo::rustc-cfg=rp2040"); println!("cargo:rerun-if-changed=rp2040.x"); } /// Executes the write rp2350 operation. /// /// # Arguments /// /// * `out` - Output path. fn write_rp2350(out: &Path) { let b = include_bytes!("rp2350.x"); File::create(out.join("memory.x")).unwrap().write_all(b).unwrap(); println!("cargo::rustc-cfg=rp2350"); println!("cargo:rerun-if-changed=rp2350.x"); } /// Executes the write riscv operation. /// /// # Arguments /// /// * `out` - Output path. fn write_riscv(out: &Path) { let b = include_bytes!("rp2350_riscv.x"); File::create(out.join("rp2350_riscv.x")).unwrap().write_all(b).unwrap(); } /// Executes the print cfgs operation. /// /// # Arguments /// /// * `out` - Output path. fn print_cfgs(out: &Path) { println!("cargo::rustc-check-cfg=cfg(rp2040)"); println!("cargo::rustc-check-cfg=cfg(rp2350)"); println!("cargo:rustc-link-search={}", out.display()); println!("cargo:rerun-if-changed=.pico-rs"); println!("cargo:rerun-if-changed=rp2350_riscv.x"); println!("cargo:rerun-if-changed=build.rs"); }