Skip to content

Commit b46a77e

Browse files
authored
Merge pull request #26 from jannic/only-write-to-out-dir
Write build results to OUT_DIR
2 parents 4c477f9 + f4623ab commit b46a77e

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ In order to remove the need for GCC for users of this crate, we link against pre
7272
If you wish to add or change an existing bootloader you should install GCC and build with the feature `assemble`
7373

7474
```
75-
cargo build --features=assemble
75+
UPDATE_PRECOMPILED_BINARIES=true cargo build --features=assemble
7676
```
7777

7878
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`

build.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
//! Compiles boot2 bootloader from assembler source
22
//! Compiles the bootloader from assembly language source, and creates a binary file.
33
4-
#[cfg(feature = "assemble")]
54
use std::env;
6-
#[cfg(feature = "assemble")]
75
use std::fs;
86
#[cfg(feature = "assemble")]
97
use std::io::{self, Write};
8+
use std::path::Path;
109
#[cfg(feature = "assemble")]
11-
use std::path::{Path, PathBuf};
10+
use std::path::PathBuf;
1211
#[cfg(feature = "assemble")]
1312
use std::process::Command;
1413

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

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

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

102+
#[cfg(feature = "assemble")]
103+
fn update_precompiled_bin<P: AsRef<Path>>(input_path: P) {
104+
let input_path: &Path = input_path.as_ref();
105+
// Abort if this crate is being built as a dependency.
106+
// This check is crude, but CARGO_PRIMARY_PACKAGE is not
107+
// available in build scripts.
108+
if !env::var("OUT_DIR")
109+
.unwrap()
110+
.starts_with(&env::var("CARGO_MANIFEST_DIR").unwrap())
111+
{
112+
panic!(
113+
"UPDATE_PRECOMPILED_BINARIES must only be used when compiling this package directly"
114+
);
115+
}
116+
let precompiled_bin_dir = env::var("CARGO_MANIFEST_DIR").unwrap() + "/bin/";
117+
std::fs::copy(
118+
&input_path,
119+
Path::new(&precompiled_bin_dir).join(input_path.file_name().unwrap()),
120+
)
121+
.unwrap();
122+
}
123+
102124
#[cfg(feature = "assemble")]
103125
fn main() -> Result<(), String> {
104-
// Store temporary build files here
105126
let out_dir = env::var("OUT_DIR").unwrap();
106-
// And our final output here
107-
let final_outdir = concat!(env!("CARGO_MANIFEST_DIR"), "/bin/");
108127
for asm_file in SOURCE_FILES.iter() {
109128
let elf = make_elf(asm_file, &out_dir);
110129
let bin = make_bin(elf, &out_dir);
111-
let _padded_bin = make_padded_bin(bin, &final_outdir);
130+
let padded_bin = make_padded_bin(bin, &out_dir);
131+
if env::var("UPDATE_PRECOMPILED_BINARIES").is_ok() {
132+
update_precompiled_bin(padded_bin);
133+
}
112134
println!("cargo:rerun-if-changed={}", asm_file);
113135
}
114136
println!("cargo:rerun-if-changed=./build.rs");
137+
println!("cargo:rerun-if-env-changed=UPDATE_PRECOMPILED_BINARIES");
115138

116139
Ok(())
117140
}
118141

119142
#[cfg(not(feature = "assemble"))]
120143
fn main() -> Result<(), String> {
144+
let in_dir = env::var("CARGO_MANIFEST_DIR").unwrap() + "/bin/";
145+
let out_dir = env::var("OUT_DIR").unwrap();
146+
147+
let paths: Vec<_> = fs::read_dir(in_dir)
148+
.unwrap()
149+
.map(|entry| entry.unwrap().path())
150+
.collect();
151+
for path in paths {
152+
if path
153+
.file_name()
154+
.unwrap()
155+
.to_string_lossy()
156+
.ends_with(".padded.bin")
157+
{
158+
std::fs::copy(&path, Path::new(&out_dir).join(path.file_name().unwrap())).unwrap();
159+
}
160+
}
121161
println!("cargo:warning=Using prebuilt boot2 files. use feature `assemble` to rebuild instead (requires GNU toolchain)");
122162
Ok(())
123163
}

check-blobs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ for lib in bin/*.bin; do
1818
arm-none-eabi-objdump -b binary -m armv6-m -M force-thumb -D "$lib" > "bin/${filename%.bin}.before"
1919
done
2020

21-
cargo build --features=assemble
21+
UPDATE_PRECOMPILED_BINARIES=true cargo build --features=assemble
2222

2323
for lib in bin/*.bin; do
2424
filename=$(basename "$lib")

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)