11// NOTE: Adapted from cortex-m/build.rs
22
3- use riscv_target_parser:: { Extension , RiscvTarget , Width } ;
3+ use riscv_target_parser:: RiscvTarget ;
44use std:: { env, fs, io, path:: PathBuf } ;
55
6+ // List of all possible RISC-V configurations to check for in risv-rt
7+ const RISCV_CFG : [ & str ; 5 ] = [ "riscvi" , "riscve" , "riscvm" , "riscvf" , "riscvd" ] ;
8+
69fn add_linker_script ( arch_width : u32 ) -> io:: Result < ( ) > {
710 // Read the file to a string and replace all occurrences of ${ARCH_WIDTH} with the arch width
811 let mut content = fs:: read_to_string ( "link.x.in" ) ?;
@@ -20,32 +23,36 @@ fn add_linker_script(arch_width: u32) -> io::Result<()> {
2023
2124fn main ( ) {
2225 // Required until target_feature risc-v is stable and in-use (rust 1.75)
23- for ext in [ 'i' , 'e' , 'm' , 'a' , 'f' , 'd' , 'g' , 'c' ] {
24- println ! ( "cargo:rustc-check-cfg=cfg(riscv{ })" , ext ) ;
26+ for ext in RISCV_CFG . iter ( ) {
27+ println ! ( "cargo:rustc-check-cfg=cfg({ext })" ) ;
2528 }
2629
2730 let target = env:: var ( "TARGET" ) . unwrap ( ) ;
2831 let cargo_flags = env:: var ( "CARGO_ENCODED_RUSTFLAGS" ) . unwrap ( ) ;
2932
3033 if let Ok ( target) = RiscvTarget :: build ( & target, & cargo_flags) {
3134 let width = target. width ( ) ;
32- let base = target. base_extension ( ) . expect ( "No base extension found" ) ;
33-
34- // set environmet variable RISCV_RT_BASE_ISA to the width of the target
35- // this is used in riscv_rt_macros to determine the base ISA
36- let env_var = match ( width, base) {
37- ( Width :: W32 , Extension :: I ) => "rv32i" ,
38- ( Width :: W32 , Extension :: E ) => "rv32e" ,
39- ( Width :: W64 , Extension :: I ) => "rv64i" ,
40- ( Width :: W64 , Extension :: E ) => "rv64e" ,
41- _ => panic ! ( "Unsupported target" ) ,
42- } ;
43- println ! ( "cargo:rustc-env=RISCV_RT_BASE_ISA={env_var}" ) ;
35+
36+ // set environmet variable RISCV_RT_BASE_ISA to the base ISA of the target.
37+ println ! (
38+ "cargo:rustc-env=RISCV_RT_BASE_ISA={}" ,
39+ target. llvm_base_isa( )
40+ ) ;
41+ // set environment variable RISCV_RT_LLVM_ARCH_PATCH to patch LLVM bug.
42+ // (this env variable is temporary and will be removed after LLVM being fixed)
43+ println ! (
44+ "cargo:rustc-env=RISCV_RT_LLVM_ARCH_PATCH={}" ,
45+ target. llvm_arch_patch( )
46+ ) ;
47+ // make sure that these env variables are not changed without notice.
48+ println ! ( "cargo:rerun-if-env-changed=RISCV_RT_BASE_ISA" ) ;
49+ println ! ( "cargo:rerun-if-env-changed=RISCV_RT_LLVM_ARCH_PATCH" ) ;
4450
4551 for flag in target. rustc_flags ( ) {
4652 // Required until target_feature risc-v is stable and in-use
47- println ! ( "cargo:rustc-check-cfg=cfg({flag})" ) ;
48- println ! ( "cargo:rustc-cfg={flag}" ) ;
53+ if RISCV_CFG . contains ( & flag. as_str ( ) ) {
54+ println ! ( "cargo:rustc-cfg={flag}" ) ;
55+ }
4956 }
5057 add_linker_script ( width. into ( ) ) . unwrap ( ) ;
5158 }
0 commit comments