1
1
// NOTE: Adapted from cortex-m/build.rs
2
2
3
- use riscv_target_parser:: { Extension , RiscvTarget , Width } ;
3
+ use riscv_target_parser:: RiscvTarget ;
4
4
use std:: { env, fs, io, path:: PathBuf } ;
5
5
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
+
6
9
fn add_linker_script ( arch_width : u32 ) -> io:: Result < ( ) > {
7
10
// Read the file to a string and replace all occurrences of ${ARCH_WIDTH} with the arch width
8
11
let mut content = fs:: read_to_string ( "link.x.in" ) ?;
@@ -20,32 +23,36 @@ fn add_linker_script(arch_width: u32) -> io::Result<()> {
20
23
21
24
fn main ( ) {
22
25
// 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 })" ) ;
25
28
}
26
29
27
30
let target = env:: var ( "TARGET" ) . unwrap ( ) ;
28
31
let cargo_flags = env:: var ( "CARGO_ENCODED_RUSTFLAGS" ) . unwrap ( ) ;
29
32
30
33
if let Ok ( target) = RiscvTarget :: build ( & target, & cargo_flags) {
31
34
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" ) ;
44
50
45
51
for flag in target. rustc_flags ( ) {
46
52
// 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
+ }
49
56
}
50
57
add_linker_script ( width. into ( ) ) . unwrap ( ) ;
51
58
}
0 commit comments