1- use std:: {
2- env,
3- path:: { Path , PathBuf } ,
4- } ;
1+ use std:: { env, path:: PathBuf } ;
52
3+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
4+ pub enum MemorySelect {
5+ CortexM0 ,
6+ CortexM3 ,
7+ }
68fn main ( ) {
79 let target = std:: env:: var ( "TARGET" ) . unwrap ( ) ;
810
@@ -13,11 +15,15 @@ fn main() {
1315 println ! ( "cargo:rustc-check-cfg=cfg(armv8m_base)" ) ;
1416 println ! ( "cargo:rustc-check-cfg=cfg(armv8m_main)" ) ;
1517
18+ let mut memory_select = None ;
1619 if target. starts_with ( "thumbv6m-" ) {
20+ memory_select = Some ( MemorySelect :: CortexM0 ) ;
1721 println ! ( "cargo:rustc-cfg=armv6m" ) ;
1822 } else if target. starts_with ( "thumbv7m-" ) {
23+ memory_select = Some ( MemorySelect :: CortexM3 ) ;
1924 println ! ( "cargo:rustc-cfg=armv7m" ) ;
2025 } else if target. starts_with ( "thumbv7em-" ) {
26+ memory_select = Some ( MemorySelect :: CortexM3 ) ;
2127 println ! ( "cargo:rustc-cfg=armv7m" ) ;
2228 println ! ( "cargo:rustc-cfg=armv7em" ) ; // (not currently used)
2329 } else if target. starts_with ( "thumbv8m.base" ) {
@@ -28,26 +34,33 @@ fn main() {
2834 println ! ( "cargo:rustc-cfg=armv8m_main" ) ;
2935 }
3036
31- // Put `memory.x` in our output directory and ensure it's
32- // on the linker search path.
3337 let out = PathBuf :: from ( env:: var_os ( "OUT_DIR" ) . unwrap ( ) ) ;
34- let memory_x_path = out. join ( "memory.x" ) ;
38+ let memory_x_file = match memory_select {
39+ Some ( MemorySelect :: CortexM0 ) => {
40+ // Put `memory.x` in our output directory and ensure it's
41+ // on the linker search path.
42+ "memory_microbit.x"
43+ }
44+ Some ( MemorySelect :: CortexM3 ) => {
45+ // Put `memory.x` in our output directory and ensure it's
46+ // on the linker search path.
47+ "memory_m3.x"
48+ }
49+ // TODO: Copy memory.x if it exists?
50+ None => panic ! ( "Unsupported target architecture: {}" , target) ,
51+ } ;
52+ let target_path = out. join ( "memory.x" ) ;
3553
3654 // Copy memory.x from parent directory only if it doesn't exist locally
37- if !Path :: new ( "memory.x" ) . exists ( ) {
38- std:: fs:: copy ( "../memory.x" , & memory_x_path)
39- . unwrap_or_else ( |e| panic ! ( "Failed to copy memory.x from parent: {}" , e) ) ;
40- } else {
41- std:: fs:: copy ( "memory.x" , & memory_x_path)
42- . unwrap_or_else ( |e| panic ! ( "Failed to copy memory.x: {}" , e) ) ;
43- }
55+ std:: fs:: copy ( memory_x_file, & target_path)
56+ . unwrap_or_else ( |e| panic ! ( "Failed to copy memory.x: {}" , e) ) ;
4457
4558 println ! ( "cargo:rustc-link-search={}" , out. display( ) ) ;
4659
4760 // By default, Cargo will re-run a build script whenever
4861 // any file in the project changes. By specifying `memory.x`
4962 // here, we ensure the build script is only re-run when
5063 // `memory.x` is changed.
51- println ! ( "cargo:rerun-if-changed=memory .x" ) ;
52- println ! ( "cargo:rerun-if-changed=../memory .x" ) ;
64+ println ! ( "cargo:rerun-if-changed=memory_m3 .x" ) ;
65+ println ! ( "cargo:rerun-if-changed=memory_microbit .x" ) ;
5366}
0 commit comments