@@ -5,26 +5,26 @@ use std::ffi::OsString;
5
5
use std:: path:: Path ;
6
6
7
7
#[ derive( Debug , PartialEq , Default ) ]
8
- pub ( crate ) struct RustcCodegenFlags {
9
- branch_protection : Option < String > ,
10
- code_model : Option < String > ,
8
+ pub ( crate ) struct RustcCodegenFlags < ' a > {
9
+ branch_protection : Option < & ' a str > ,
10
+ code_model : Option < & ' a str > ,
11
11
no_vectorize_loops : bool ,
12
12
no_vectorize_slp : bool ,
13
- profile_generate : Option < String > ,
14
- profile_use : Option < String > ,
15
- control_flow_guard : Option < String > ,
16
- lto : Option < String > ,
17
- relocation_model : Option < String > ,
13
+ profile_generate : Option < & ' a str > ,
14
+ profile_use : Option < & ' a str > ,
15
+ control_flow_guard : Option < & ' a str > ,
16
+ lto : Option < & ' a str > ,
17
+ relocation_model : Option < & ' a str > ,
18
18
embed_bitcode : Option < bool > ,
19
19
force_frame_pointers : Option < bool > ,
20
20
link_dead_code : Option < bool > ,
21
21
no_redzone : Option < bool > ,
22
22
soft_float : Option < bool > ,
23
23
}
24
24
25
- impl RustcCodegenFlags {
25
+ impl < ' this > RustcCodegenFlags < ' this > {
26
26
// Parse flags obtained from CARGO_ENCODED_RUSTFLAGS
27
- pub ( crate ) fn parse ( rustflags_env : & str ) -> Result < RustcCodegenFlags , Error > {
27
+ pub ( crate ) fn parse ( rustflags_env : & ' this str ) -> Result < Self , Error > {
28
28
fn is_flag_prefix ( flag : & str ) -> bool {
29
29
[
30
30
"-Z" ,
@@ -45,19 +45,19 @@ impl RustcCodegenFlags {
45
45
. contains ( & flag)
46
46
}
47
47
48
- fn handle_flag_prefix < ' a > ( prev : & ' a str , curr : & ' a str ) -> Cow < ' a , str > {
48
+ fn handle_flag_prefix < ' a > ( prev : & ' a str , curr : & ' a str ) -> ( & ' a str , & ' a str ) {
49
49
match prev {
50
- "--codegen" | "-C" => Cow :: from ( format ! ( "-C{} " , curr) ) ,
50
+ "--codegen" | "-C" => ( "-C" , curr) ,
51
51
// Handle flags passed like --codegen=code-model=small
52
- _ if curr. starts_with ( "--codegen=" ) => Cow :: from ( format ! ( "-C{} " , & curr[ 10 ..] ) ) ,
53
- "-Z" => Cow :: from ( format ! ( "-Z{} " , curr) ) ,
54
- "-L" | "-l" | "-o" => Cow :: from ( format ! ( "{}{}" , prev, curr) ) ,
52
+ _ if curr. starts_with ( "--codegen=" ) => ( "-C" , & curr[ 10 ..] ) ,
53
+ "-Z" => ( "-Z" , curr) ,
54
+ "-L" | "-l" | "-o" => ( prev, curr) ,
55
55
// Handle lint flags
56
- "-W" | "--warn" => Cow :: from ( format ! ( "-W{} " , curr) ) ,
57
- "-A" | "--allow" => Cow :: from ( format ! ( "-A{} " , curr) ) ,
58
- "-D" | "--deny" => Cow :: from ( format ! ( "-D{} " , curr) ) ,
59
- "-F" | "--forbid" => Cow :: from ( format ! ( "-F{} " , curr) ) ,
60
- _ => Cow :: from ( curr) ,
56
+ "-W" | "--warn" => ( "-W" , curr) ,
57
+ "-A" | "--allow" => ( "-A" , curr) ,
58
+ "-D" | "--deny" => ( "-D" , curr) ,
59
+ "-F" | "--forbid" => ( "-F" , curr) ,
60
+ _ => ( "" , curr) ,
61
61
}
62
62
}
63
63
@@ -71,14 +71,14 @@ impl RustcCodegenFlags {
71
71
continue ;
72
72
}
73
73
74
- let rustc_flag = handle_flag_prefix ( prev, curr) ;
75
- codegen_flags. set_rustc_flag ( & rustc_flag) ?;
74
+ let ( prefix , rustc_flag) = handle_flag_prefix ( prev, curr) ;
75
+ codegen_flags. set_rustc_flag ( prefix , rustc_flag) ?;
76
76
}
77
77
78
78
Ok ( codegen_flags)
79
79
}
80
80
81
- fn set_rustc_flag ( & mut self , flag : & str ) -> Result < ( ) , Error > {
81
+ fn set_rustc_flag ( & mut self , prefix : & str , flag : & ' this str ) -> Result < ( ) , Error > {
82
82
// Convert a textual representation of a bool-like rustc flag argument into an actual bool
83
83
fn arg_to_bool ( arg : impl AsRef < str > ) -> Option < bool > {
84
84
match arg. as_ref ( ) {
@@ -89,16 +89,24 @@ impl RustcCodegenFlags {
89
89
}
90
90
91
91
let ( flag, value) = if let Some ( ( flag, value) ) = flag. split_once ( '=' ) {
92
- ( flag, Some ( value. to_owned ( ) ) )
92
+ ( flag, Some ( value) )
93
93
} else {
94
94
( flag, None )
95
95
} ;
96
+ let flag = if prefix. is_empty ( ) {
97
+ Cow :: Borrowed ( flag)
98
+ } else {
99
+ Cow :: Owned ( format ! ( "{prefix}{flag}" ) )
100
+ } ;
96
101
97
- fn flag_ok_or ( flag : Option < String > , msg : & ' static str ) -> Result < String , Error > {
102
+ fn flag_ok_or < ' flag > (
103
+ flag : Option < & ' flag str > ,
104
+ msg : & ' static str ,
105
+ ) -> Result < & ' flag str , Error > {
98
106
flag. ok_or ( Error :: new ( ErrorKind :: InvalidFlag , msg) )
99
107
}
100
108
101
- match flag {
109
+ match flag. as_ref ( ) {
102
110
// https://doc.rust-lang.org/rustc/codegen-options/index.html#code-model
103
111
"-Ccode-model" => {
104
112
self . code_model = Some ( flag_ok_or ( value, "-Ccode-model must have a value" ) ?) ;
@@ -117,9 +125,9 @@ impl RustcCodegenFlags {
117
125
self . profile_use = Some ( flag_ok_or ( value, "-Cprofile-use must have a value" ) ?) ;
118
126
}
119
127
// https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard
120
- "-Ccontrol-flow-guard" => self . control_flow_guard = value. or ( Some ( "true" . into ( ) ) ) ,
128
+ "-Ccontrol-flow-guard" => self . control_flow_guard = value. or ( Some ( "true" ) ) ,
121
129
// https://doc.rust-lang.org/rustc/codegen-options/index.html#lto
122
- "-Clto" => self . lto = value. or ( Some ( "true" . into ( ) ) ) ,
130
+ "-Clto" => self . lto = value. or ( Some ( "true" ) ) ,
123
131
// https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model
124
132
"-Crelocation-model" => {
125
133
self . relocation_model =
@@ -176,13 +184,13 @@ impl RustcCodegenFlags {
176
184
match family {
177
185
ToolFamily :: Clang { .. } | ToolFamily :: Gnu => {
178
186
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mbranch-protection
179
- if let Some ( value) = & self . branch_protection {
187
+ if let Some ( value) = self . branch_protection {
180
188
push_if_supported (
181
189
format ! ( "-mbranch-protection={}" , value. replace( "," , "+" ) ) . into ( ) ,
182
190
) ;
183
191
}
184
192
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mcmodel
185
- if let Some ( value) = & self . code_model {
193
+ if let Some ( value) = self . code_model {
186
194
push_if_supported ( format ! ( "-mcmodel={value}" ) . into ( ) ) ;
187
195
}
188
196
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-vectorize
@@ -194,16 +202,16 @@ impl RustcCodegenFlags {
194
202
push_if_supported ( "-fno-slp-vectorize" . into ( ) ) ;
195
203
}
196
204
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-generate
197
- if let Some ( value) = & self . profile_generate {
205
+ if let Some ( value) = self . profile_generate {
198
206
push_if_supported ( format ! ( "-fprofile-generate={value}" ) . into ( ) ) ;
199
207
}
200
208
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fprofile-use
201
- if let Some ( value) = & self . profile_use {
209
+ if let Some ( value) = self . profile_use {
202
210
push_if_supported ( format ! ( "-fprofile-use={value}" ) . into ( ) ) ;
203
211
}
204
212
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard
205
- if let Some ( value) = & self . control_flow_guard {
206
- let cc_val = match value. as_str ( ) {
213
+ if let Some ( value) = self . control_flow_guard {
214
+ let cc_val = match value {
207
215
"y" | "yes" | "on" | "true" | "checks" => Some ( "cf" ) ,
208
216
"nochecks" => Some ( "cf-nochecks" ) ,
209
217
"n" | "no" | "off" | "false" => Some ( "none" ) ,
@@ -214,8 +222,8 @@ impl RustcCodegenFlags {
214
222
}
215
223
}
216
224
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
217
- if let Some ( value) = & self . lto {
218
- let cc_val = match value. as_str ( ) {
225
+ if let Some ( value) = self . lto {
226
+ let cc_val = match value {
219
227
"y" | "yes" | "on" | "true" | "fat" => Some ( "full" ) ,
220
228
"thin" => Some ( "thin" ) ,
221
229
_ => None ,
@@ -227,8 +235,8 @@ impl RustcCodegenFlags {
227
235
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIC
228
236
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fPIE
229
237
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mdynamic-no-pic
230
- if let Some ( value) = & self . relocation_model {
231
- let cc_flag = match value. as_str ( ) {
238
+ if let Some ( value) = self . relocation_model {
239
+ let cc_flag = match value {
232
240
"pic" => Some ( "-fPIC" ) ,
233
241
"pie" => Some ( "-fPIE" ) ,
234
242
"dynamic-no-pic" => Some ( "-mdynamic-no-pic" ) ,
@@ -239,40 +247,34 @@ impl RustcCodegenFlags {
239
247
}
240
248
}
241
249
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fembed-bitcode
242
- if let Some ( value) = & self . embed_bitcode {
243
- let cc_val = if * value { "all" } else { "off" } ;
250
+ if let Some ( value) = self . embed_bitcode {
251
+ let cc_val = if value { "all" } else { "off" } ;
244
252
push_if_supported ( format ! ( "-fembed-bitcode={cc_val}" ) . into ( ) ) ;
245
253
}
246
254
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fno-omit-frame-pointer
247
255
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fomit-frame-pointer
248
- if let Some ( value) = & self . force_frame_pointers {
249
- let cc_flag = if * value {
256
+ if let Some ( value) = self . force_frame_pointers {
257
+ let cc_flag = if value {
250
258
"-fno-omit-frame-pointer"
251
259
} else {
252
260
"-fomit-frame-pointer"
253
261
} ;
254
262
push_if_supported ( cc_flag. into ( ) ) ;
255
263
}
256
264
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-dead_strip
257
- if let Some ( value) = & self . link_dead_code {
258
- if !value {
259
- push_if_supported ( "-dead_strip" . into ( ) ) ;
260
- }
265
+ if let Some ( false ) = self . link_dead_code {
266
+ push_if_supported ( "-dead_strip" . into ( ) ) ;
261
267
}
262
268
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-red-zone
263
269
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mred-zone
264
- if let Some ( value) = & self . no_redzone {
265
- let cc_flag = if * value {
266
- "-mno-red-zone"
267
- } else {
268
- "-mred-zone"
269
- } ;
270
+ if let Some ( value) = self . no_redzone {
271
+ let cc_flag = if value { "-mno-red-zone" } else { "-mred-zone" } ;
270
272
push_if_supported ( cc_flag. into ( ) ) ;
271
273
}
272
274
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-msoft-float
273
275
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mno-soft-float
274
- if let Some ( value) = & self . soft_float {
275
- let cc_flag = if * value {
276
+ if let Some ( value) = self . soft_float {
277
+ let cc_flag = if value {
276
278
"-msoft-float"
277
279
} else {
278
280
"-mno-soft-float"
@@ -282,8 +284,8 @@ impl RustcCodegenFlags {
282
284
}
283
285
ToolFamily :: Msvc { .. } => {
284
286
// https://learn.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard
285
- if let Some ( value) = & self . control_flow_guard {
286
- let cc_val = match value. as_str ( ) {
287
+ if let Some ( value) = self . control_flow_guard {
288
+ let cc_val = match value {
287
289
"y" | "yes" | "on" | "true" | "checks" => Some ( "cf" ) ,
288
290
"n" | "no" | "off" | "false" => Some ( "cf-" ) ,
289
291
_ => None ,
@@ -293,8 +295,8 @@ impl RustcCodegenFlags {
293
295
}
294
296
}
295
297
// https://learn.microsoft.com/en-us/cpp/build/reference/oy-frame-pointer-omission
296
- if let Some ( value) = & self . force_frame_pointers {
297
- let cc_flag = if * value { "/Oy-" } else { "/Oy" } ;
298
+ if let Some ( value) = self . force_frame_pointers {
299
+ let cc_flag = if value { "/Oy-" } else { "/Oy" } ;
298
300
push_if_supported ( cc_flag. into ( ) ) ;
299
301
}
300
302
}
0 commit comments