2020-03-10 04:04:17 +00:00
// NOTE: Adapted from cortex-m/build.rs
use std ::env ;
use std ::fs ;
use std ::io ::Write ;
use std ::path ::PathBuf ;
fn main ( ) {
let out_dir = PathBuf ::from ( env ::var ( " OUT_DIR " ) . unwrap ( ) ) ;
let target = env ::var ( " TARGET " ) . unwrap ( ) ;
2023-03-09 16:13:28 +00:00
let linker_file_path = if target . starts_with ( " riscv " ) {
2020-03-10 04:04:17 +00:00
println! ( " cargo:rustc-link-search= {} " , out_dir . display ( ) ) ;
2023-12-27 02:41:48 +00:00
#[ cfg(feature = " cramium-soc " ) ]
2023-07-17 10:23:17 +00:00
let p = PathBuf ::from ( " src/platform/cramium/link-soc.x " ) ;
2023-12-27 02:41:48 +00:00
#[ cfg(feature = " cramium-fpga " ) ]
let p = PathBuf ::from ( " src/platform/cramium/link-soc.x " ) ; // note deviation -- this is because we are still simulating the FPGA model and offsets are not yet settled for that
#[ cfg(not(any(feature = " cramium-soc " , feature = " cramium-fpga " ))) ]
2023-07-17 19:56:09 +00:00
let p = PathBuf ::from ( " src/platform/precursor/link.x " ) ;
2023-07-17 10:23:17 +00:00
2024-01-14 14:02:55 +00:00
println! ( " cargo:rerun-if-changed= {} " , p . clone ( ) . into_os_string ( ) . into_string ( ) . unwrap ( ) ) ;
2022-01-14 09:32:04 +00:00
println! ( " cargo:rustc-link-arg=-Tlink.x " ) ;
2023-03-09 16:13:28 +00:00
2023-07-17 10:23:17 +00:00
p
2023-03-09 16:13:28 +00:00
} else if target . starts_with ( " armv7a " ) {
let name = env ::var ( " CARGO_PKG_NAME " ) . unwrap ( ) ;
2024-01-14 14:02:55 +00:00
fs ::copy ( format! ( " bin/ {} .a " , target ) , out_dir . join ( format! ( " lib {} .a " , name ) ) )
. expect ( " copy arm assembly binary " ) ;
2023-03-09 16:13:28 +00:00
println! ( " cargo:rustc-link-lib=static= {} " , name ) ;
println! ( " cargo:rustc-link-search= {} " , out_dir . display ( ) ) ;
println! ( " cargo:rerun-if-changed=bin/ {} .a " , target ) ;
println! ( " cargo:rustc-link-arg=-Tlink.x " ) ;
PathBuf ::from ( " src/platform/atsama5d27/link.x " )
} else {
unreachable! ( " unsupported target " ) ;
} ;
2020-03-10 04:04:17 +00:00
2023-07-17 19:56:09 +00:00
println! ( " {} " , out_dir . join ( " link.x " ) . display ( ) ) ;
2020-03-10 04:04:17 +00:00
// Put the linker script somewhere the linker can find it
2023-12-27 02:41:48 +00:00
std ::fs ::OpenOptions ::new ( )
. create ( true )
. write ( true )
. truncate ( true )
. open ( out_dir . join ( " link.x " ) )
2020-03-10 04:04:17 +00:00
. unwrap ( )
2024-01-14 14:02:55 +00:00
. write_all ( fs ::read_to_string ( linker_file_path ) . expect ( " linker file read " ) . as_bytes ( ) )
2020-03-10 04:04:17 +00:00
. unwrap ( ) ;
println! ( " cargo:rustc-link-search= {} " , out_dir . display ( ) ) ;
println! ( " cargo:rerun-if-changed=build.rs " ) ;
println! ( " cargo:rerun-if-changed=link.x " ) ;
}