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" ) ]
54use std:: env;
6- #[ cfg( feature = "assemble" ) ]
75use std:: fs;
86#[ cfg( feature = "assemble" ) ]
97use 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" ) ]
1312use 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" ) ]
103125fn 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" ) ) ]
120143fn 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}
0 commit comments