@@ -18,7 +18,7 @@ fn add_linker_script(arch_width: u32) -> io::Result<()> {
18
18
}
19
19
20
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 > ) {
21
+ fn parse_target ( target : & str , cargo_flags : & str ) -> ( u32 , HashSet < char > ) {
22
22
// isolate bit width and extensions from the rest of the target information
23
23
let arch = target
24
24
. trim_start_matches ( "riscv" )
@@ -43,30 +43,39 @@ fn parse_target(target: &str) -> (u32, HashSet<char>) {
43
43
extensions. insert ( 'd' ) ;
44
44
}
45
45
46
- ( bits, extensions)
47
- }
48
-
49
- fn main ( ) {
50
- let target = env:: var ( "TARGET" ) . unwrap ( ) ;
51
- let _name = env:: var ( "CARGO_PKG_NAME" ) . unwrap ( ) ;
52
-
53
- // This is required until target_feature risc-v work is stable and in-use (rust 1.75.0)
54
- let cargo_flags = env:: var ( "CARGO_ENCODED_RUSTFLAGS" ) . unwrap ( ) ;
55
46
let cargo_flags = cargo_flags
56
47
. split ( 0x1fu8 as char )
57
48
. filter ( |arg| !arg. is_empty ( ) ) ;
58
-
59
- let target_features = cargo_flags
49
+
50
+ cargo_flags
60
51
. filter ( |k| k. starts_with ( "target-feature=" ) ) . flat_map ( |str| {
61
52
let flags = str. split ( '=' ) . collect :: < Vec < & str > > ( ) [ 1 ] ;
62
53
flags. split ( ',' )
54
+ } )
55
+ . for_each ( |feature| {
56
+ let chars = feature. chars ( ) . collect :: < Vec < char > > ( ) ;
57
+ match chars[ 0 ] {
58
+ '+' => { extensions. insert ( chars[ 1 ] ) ; }
59
+ '-' => { extensions. remove ( & chars[ 1 ] ) ; }
60
+ _ => { panic ! ( "Unsupported target feature operation" ) ; }
61
+ }
63
62
} ) ;
64
63
64
+ ( bits, extensions)
65
+ }
66
+
67
+ fn main ( ) {
68
+ let target = env:: var ( "TARGET" ) . unwrap ( ) ;
69
+ let cargo_flags = env:: var ( "CARGO_ENCODED_RUSTFLAGS" ) . unwrap ( ) ;
70
+ let _name = env:: var ( "CARGO_PKG_NAME" ) . unwrap ( ) ;
71
+
65
72
// set configuration flags depending on the target
66
73
if target. starts_with ( "riscv" ) {
67
74
println ! ( "cargo:rustc-cfg=riscv" ) ;
68
75
69
- let ( bits, mut extensions) = parse_target ( & target) ;
76
+ // This is required until target_arch & target_feature risc-v work is
77
+ // stable and in-use (rust 1.75.0)
78
+ let ( bits, extensions) = parse_target ( & target, & cargo_flags) ;
70
79
71
80
// generate the linker script and expose the ISA width
72
81
let arch_width = match bits {
@@ -82,15 +91,6 @@ fn main() {
82
91
} ;
83
92
add_linker_script ( arch_width) . unwrap ( ) ;
84
93
85
- target_features. for_each ( |feature| {
86
- let chars = feature. chars ( ) . collect :: < Vec < char > > ( ) ;
87
- if chars[ 0 ] == '+' {
88
- extensions. insert ( chars[ 1 ] ) ;
89
- } else if chars[ 0 ] == '-' {
90
- extensions. remove ( & chars[ 1 ] ) ;
91
- }
92
- } ) ;
93
-
94
94
// expose the ISA extensions
95
95
for ext in & extensions {
96
96
println ! ( "cargo:rustc-cfg=riscv{}" , ext) ;
0 commit comments