1
1
// NOTE: Adapted from cortex-m/build.rs
2
2
3
- use riscv_target:: Target ;
4
- use std:: { env, fs, io, path:: PathBuf } ;
3
+ use std:: { collections:: HashSet , env, fs, io, path:: PathBuf } ;
5
4
6
5
fn add_linker_script ( arch_width : u32 ) -> io:: Result < ( ) > {
7
6
// Read the file to a string and replace all occurrences of ${ARCH_WIDTH} with the arch width
@@ -18,17 +17,47 @@ fn add_linker_script(arch_width: u32) -> io::Result<()> {
18
17
Ok ( ( ) )
19
18
}
20
19
20
+ /// Parse the target RISC-V architecture and returns its bit width and the extension set
21
+ fn parse_target ( target : & str ) -> ( u32 , HashSet < char > ) {
22
+ // isolate bit width and extensions from the rest of the target information
23
+ let arch = target
24
+ . trim_start_matches ( "riscv" )
25
+ . split ( '-' )
26
+ . next ( )
27
+ . unwrap ( ) ;
28
+
29
+ let bits = arch
30
+ . chars ( )
31
+ . take_while ( |c| c. is_ascii_digit ( ) )
32
+ . collect :: < String > ( )
33
+ . parse :: < u32 > ( )
34
+ . unwrap ( ) ;
35
+
36
+ let mut extensions: HashSet < char > = arch. chars ( ) . skip_while ( |c| c. is_ascii_digit ( ) ) . collect ( ) ;
37
+ // get rid of the 'g' shorthand extension
38
+ if extensions. remove ( & 'g' ) {
39
+ extensions. insert ( 'i' ) ;
40
+ extensions. insert ( 'm' ) ;
41
+ extensions. insert ( 'a' ) ;
42
+ extensions. insert ( 'f' ) ;
43
+ extensions. insert ( 'd' ) ;
44
+ }
45
+
46
+ ( bits, extensions)
47
+ }
48
+
21
49
fn main ( ) {
22
50
let target = env:: var ( "TARGET" ) . unwrap ( ) ;
23
51
let _name = env:: var ( "CARGO_PKG_NAME" ) . unwrap ( ) ;
24
52
25
53
// set configuration flags depending on the target
26
54
if target. starts_with ( "riscv" ) {
27
55
println ! ( "cargo:rustc-cfg=riscv" ) ;
28
- let target = Target :: from_target_str ( & target) ;
29
56
30
- // generate the linker script
31
- let arch_width = match target. bits {
57
+ let ( bits, extensions) = parse_target ( & target) ;
58
+
59
+ // generate the linker script and expose the ISA width
60
+ let arch_width = match bits {
32
61
32 => {
33
62
println ! ( "cargo:rustc-cfg=riscv32" ) ;
34
63
4
@@ -42,8 +71,8 @@ fn main() {
42
71
add_linker_script ( arch_width) . unwrap ( ) ;
43
72
44
73
// expose the ISA extensions
45
- if target . has_extension ( 'm' ) {
46
- println ! ( "cargo:rustc-cfg=riscvm" ) ;
74
+ for ext in & extensions {
75
+ println ! ( "cargo:rustc-cfg=riscv{}" , ext ) ;
47
76
}
48
77
}
49
78
}
0 commit comments