@@ -24,6 +24,9 @@ use super::command::Command;
24
24
use super :: symbol_export;
25
25
use crate :: errors;
26
26
27
+ #[ cfg( test) ]
28
+ mod tests;
29
+
27
30
/// Disables non-English messages from localized linkers.
28
31
/// Such messages may cause issues with text encoding on Windows (#35785)
29
32
/// and prevent inspection of linker output in case of errors, which we occasionally do.
@@ -178,23 +181,53 @@ fn verbatim_args<L: Linker + ?Sized>(
178
181
}
179
182
l
180
183
}
184
+ /// Add underlying linker arguments to C compiler command, by wrapping them in
185
+ /// `-Wl` or `-Xlinker`.
186
+ fn convert_link_args_to_cc_args (
187
+ cmd : & mut Command ,
188
+ args : impl IntoIterator < Item : AsRef < OsStr > , IntoIter : ExactSizeIterator > ,
189
+ ) {
190
+ let args = args. into_iter ( ) ;
191
+ if args. len ( ) == 0 {
192
+ return ;
193
+ }
194
+
195
+ let mut combined_arg = OsString :: from ( "-Wl" ) ;
196
+ for arg in args {
197
+ // If the argument itself contains a comma, we need to emit it
198
+ // as `-Xlinker`, otherwise we can use `-Wl`.
199
+ if arg. as_ref ( ) . as_encoded_bytes ( ) . contains ( & b',' ) {
200
+ // Emit current `-Wl` argument, if any has been built.
201
+ if combined_arg != OsStr :: new ( "-Wl" ) {
202
+ cmd. arg ( combined_arg) ;
203
+ // Begin next `-Wl` argument.
204
+ combined_arg = OsString :: from ( "-Wl" ) ;
205
+ }
206
+
207
+ // Emit `-Xlinker` argument.
208
+ cmd. arg ( "-Xlinker" ) ;
209
+ cmd. arg ( arg) ;
210
+ } else {
211
+ // Append to `-Wl` argument.
212
+ combined_arg. push ( "," ) ;
213
+ combined_arg. push ( arg) ;
214
+ }
215
+ }
216
+ // Emit final `-Wl` argument.
217
+ if combined_arg != OsStr :: new ( "-Wl" ) {
218
+ cmd. arg ( combined_arg) ;
219
+ }
220
+ }
181
221
/// Arguments for the underlying linker.
182
222
/// Add options to pass them through cc wrapper if `Linker` is a cc wrapper.
183
223
fn link_args < L : Linker + ?Sized > (
184
224
l : & mut L ,
185
225
args : impl IntoIterator < Item : AsRef < OsStr > , IntoIter : ExactSizeIterator > ,
186
226
) -> & mut L {
187
- let args = args. into_iter ( ) ;
188
227
if !l. is_cc ( ) {
189
228
verbatim_args ( l, args) ;
190
- } else if args. len ( ) != 0 {
191
- // FIXME: Support arguments with commas, see `rpaths_to_flags` for the example.
192
- let mut combined_arg = OsString :: from ( "-Wl" ) ;
193
- for arg in args {
194
- combined_arg. push ( "," ) ;
195
- combined_arg. push ( arg) ;
196
- }
197
- l. cmd ( ) . arg ( combined_arg) ;
229
+ } else {
230
+ convert_link_args_to_cc_args ( l. cmd ( ) , args) ;
198
231
}
199
232
l
200
233
}
0 commit comments