Skip to content

Commit

Permalink
Merge pull request #26 from jannic/only-write-to-out-dir
Browse files Browse the repository at this point in the history
Write build results to OUT_DIR
  • Loading branch information
jannic authored Jan 26, 2023
2 parents 4c477f9 + f4623ab commit b46a77e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ In order to remove the need for GCC for users of this crate, we link against pre
If you wish to add or change an existing bootloader you should install GCC and build with the feature `assemble`

```
cargo build --features=assemble
UPDATE_PRECOMPILED_BINARIES=true cargo build --features=assemble
```

To add a new bootloader to the build you need to add it to `SOURCE_FILES` in `build.rs` and add an entry for it in `lib.rs`
Expand Down
58 changes: 49 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! Compiles boot2 bootloader from assembler source
//! Compiles the bootloader from assembly language source, and creates a binary file.
#[cfg(feature = "assemble")]
use std::env;
#[cfg(feature = "assemble")]
use std::fs;
#[cfg(feature = "assemble")]
use std::io::{self, Write};
use std::path::Path;
#[cfg(feature = "assemble")]
use std::path::{Path, PathBuf};
use std::path::PathBuf;
#[cfg(feature = "assemble")]
use std::process::Command;

Expand Down Expand Up @@ -70,7 +69,7 @@ fn make_bin<P: AsRef<Path>, Q: AsRef<Path>>(input_path: P, out_dir: Q) -> PathBu
}

#[cfg(feature = "assemble")]
fn make_padded_bin<P: AsRef<Path>, Q: AsRef<Path>>(input_path: P, out_dir: Q) {
fn make_padded_bin<P: AsRef<Path>, Q: AsRef<Path>>(input_path: P, out_dir: Q) -> PathBuf {
const BOOT2_OUTPUT_LEN: usize = 256;
const MAX_BOOT2_INPUT_LEN: usize = BOOT2_OUTPUT_LEN - 4;
let input_path: &Path = input_path.as_ref();
Expand All @@ -89,7 +88,8 @@ fn make_padded_bin<P: AsRef<Path>, Q: AsRef<Path>>(input_path: P, out_dir: Q) {
let mut result_file = PathBuf::from(input_path.file_name().unwrap());
result_file.set_extension("padded.bin");
let result_path = out_dir.as_ref().join(result_file);
fs::write(result_path, blob).expect("writing padded output file");
fs::write(&result_path, blob).expect("writing padded output file");
result_path
}

#[cfg(feature = "assemble")]
Expand All @@ -99,25 +99,65 @@ fn calc_crc(data: &[u8]) -> u32 {
engine.get_crc()
}

#[cfg(feature = "assemble")]
fn update_precompiled_bin<P: AsRef<Path>>(input_path: P) {
let input_path: &Path = input_path.as_ref();
// Abort if this crate is being built as a dependency.
// This check is crude, but CARGO_PRIMARY_PACKAGE is not
// available in build scripts.
if !env::var("OUT_DIR")
.unwrap()
.starts_with(&env::var("CARGO_MANIFEST_DIR").unwrap())
{
panic!(
"UPDATE_PRECOMPILED_BINARIES must only be used when compiling this package directly"
);
}
let precompiled_bin_dir = env::var("CARGO_MANIFEST_DIR").unwrap() + "/bin/";
std::fs::copy(
&input_path,
Path::new(&precompiled_bin_dir).join(input_path.file_name().unwrap()),
)
.unwrap();
}

#[cfg(feature = "assemble")]
fn main() -> Result<(), String> {
// Store temporary build files here
let out_dir = env::var("OUT_DIR").unwrap();
// And our final output here
let final_outdir = concat!(env!("CARGO_MANIFEST_DIR"), "/bin/");
for asm_file in SOURCE_FILES.iter() {
let elf = make_elf(asm_file, &out_dir);
let bin = make_bin(elf, &out_dir);
let _padded_bin = make_padded_bin(bin, &final_outdir);
let padded_bin = make_padded_bin(bin, &out_dir);
if env::var("UPDATE_PRECOMPILED_BINARIES").is_ok() {
update_precompiled_bin(padded_bin);
}
println!("cargo:rerun-if-changed={}", asm_file);
}
println!("cargo:rerun-if-changed=./build.rs");
println!("cargo:rerun-if-env-changed=UPDATE_PRECOMPILED_BINARIES");

Ok(())
}

#[cfg(not(feature = "assemble"))]
fn main() -> Result<(), String> {
let in_dir = env::var("CARGO_MANIFEST_DIR").unwrap() + "/bin/";
let out_dir = env::var("OUT_DIR").unwrap();

let paths: Vec<_> = fs::read_dir(in_dir)
.unwrap()
.map(|entry| entry.unwrap().path())
.collect();
for path in paths {
if path
.file_name()
.unwrap()
.to_string_lossy()
.ends_with(".padded.bin")
{
std::fs::copy(&path, Path::new(&out_dir).join(path.file_name().unwrap())).unwrap();
}
}
println!("cargo:warning=Using prebuilt boot2 files. use feature `assemble` to rebuild instead (requires GNU toolchain)");
Ok(())
}
2 changes: 1 addition & 1 deletion check-blobs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ for lib in bin/*.bin; do
arm-none-eabi-objdump -b binary -m armv6-m -M force-thumb -D "$lib" > "bin/${filename%.bin}.before"
done

cargo build --features=assemble
UPDATE_PRECOMPILED_BINARIES=true cargo build --features=assemble

for lib in bin/*.bin; do
filename=$(basename "$lib")
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

/// The bootloader to use if you have a W25Q080 flash device
pub static BOOT_LOADER_W25Q080: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_w25q080.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_w25q080.padded.bin"));

/// The bootloader to use if you want to copy code to RAM and then boot from RAM
pub static BOOT_LOADER_RAM_MEMCPY: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_ram_memcpy.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_ram_memcpy.padded.bin"));

/// The bootloader to use if you want to boot from an AT25SF128A flash device
pub static BOOT_LOADER_AT25SF128A: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_at25sf128a.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_at25sf128a.padded.bin"));

/// The bootloader to use if you want to boot from an GD25Q64CS flash device
pub static BOOT_LOADER_GD25Q64CS: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_gd25q64cs.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_gd25q64cs.padded.bin"));

/// The bootloader to use if you want to boot from an W25X10CL flash device
pub static BOOT_LOADER_W25X10CL: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_w25x10cl.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_w25x10cl.padded.bin"));

/// The bootloader to use if you want to boot from a generic flash device
pub static BOOT_LOADER_GENERIC_03H: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_generic_03h.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_generic_03h.padded.bin"));

/// The bootloader to use if you want to boot from an IS25LP080 flash device
pub static BOOT_LOADER_IS25LP080: [u8; 256] =
*include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/bin/boot2_is25lp080.padded.bin"));
*include_bytes!(concat!(env!("OUT_DIR"), "/boot2_is25lp080.padded.bin"));

0 comments on commit b46a77e

Please sign in to comment.