@@ -1299,21 +1299,18 @@ impl<'a> Builder<'a> {
1299
1299
cargo
1300
1300
}
1301
1301
1302
- /// Prepares an invocation of `cargo` to be run.
1303
- ///
1304
1302
/// This will create a `Command` that represents a pending execution of
1305
1303
/// Cargo. This cargo will be configured to use `compiler` as the actual
1306
1304
/// rustc compiler, its output will be scoped by `mode`'s output directory,
1307
1305
/// it will pass the `--target` flag for the specified `target`, and will be
1308
1306
/// executing the Cargo command `cmd`.
1309
- pub fn cargo (
1307
+ fn cargo (
1310
1308
& self ,
1311
1309
compiler : Compiler ,
1312
1310
mode : Mode ,
1313
1311
source_type : SourceType ,
1314
1312
target : TargetSelection ,
1315
1313
cmd : & str ,
1316
- is_for_mir_opt_tests : bool ,
1317
1314
) -> Cargo {
1318
1315
let mut cargo = self . bare_cargo ( compiler, mode, target, cmd) ;
1319
1316
let out_dir = self . stage_out ( compiler, mode) ;
@@ -1873,17 +1870,6 @@ impl<'a> Builder<'a> {
1873
1870
rustflags. arg ( "-Wrustc::internal" ) ;
1874
1871
}
1875
1872
1876
- if !is_for_mir_opt_tests {
1877
- self . configure_linker (
1878
- compiler,
1879
- target,
1880
- & mut cargo,
1881
- & mut rustflags,
1882
- & mut rustdocflags,
1883
- & mut hostflags,
1884
- ) ;
1885
- }
1886
-
1887
1873
// If Control Flow Guard is enabled, pass the `control-flow-guard` flag to rustc
1888
1874
// when compiling the standard library, since this might be linked into the final outputs
1889
1875
// produced by rustc. Since this mitigation is only available on Windows, only enable it
@@ -2032,136 +2018,14 @@ impl<'a> Builder<'a> {
2032
2018
cargo. env ( "RUSTFLAGS" , & rustc_args. join ( " " ) ) ;
2033
2019
}
2034
2020
2035
- Cargo { command : cargo, rustflags, rustdocflags, hostflags, allow_features }
2036
- }
2037
-
2038
- fn configure_linker (
2039
- & self ,
2040
- compiler : Compiler ,
2041
- target : TargetSelection ,
2042
- cargo : & mut Command ,
2043
- rustflags : & mut Rustflags ,
2044
- rustdocflags : & mut Rustflags ,
2045
- hostflags : & mut HostFlags ,
2046
- ) {
2047
- // Dealing with rpath here is a little special, so let's go into some
2048
- // detail. First off, `-rpath` is a linker option on Unix platforms
2049
- // which adds to the runtime dynamic loader path when looking for
2050
- // dynamic libraries. We use this by default on Unix platforms to ensure
2051
- // that our nightlies behave the same on Windows, that is they work out
2052
- // of the box. This can be disabled by setting `rpath = false` in `[rust]`
2053
- // table of `config.toml`
2054
- //
2055
- // Ok, so the astute might be wondering "why isn't `-C rpath` used
2056
- // here?" and that is indeed a good question to ask. This codegen
2057
- // option is the compiler's current interface to generating an rpath.
2058
- // Unfortunately it doesn't quite suffice for us. The flag currently
2059
- // takes no value as an argument, so the compiler calculates what it
2060
- // should pass to the linker as `-rpath`. This unfortunately is based on
2061
- // the **compile time** directory structure which when building with
2062
- // Cargo will be very different than the runtime directory structure.
2063
- //
2064
- // All that's a really long winded way of saying that if we use
2065
- // `-Crpath` then the executables generated have the wrong rpath of
2066
- // something like `$ORIGIN/deps` when in fact the way we distribute
2067
- // rustc requires the rpath to be `$ORIGIN/../lib`.
2068
- //
2069
- // So, all in all, to set up the correct rpath we pass the linker
2070
- // argument manually via `-C link-args=-Wl,-rpath,...`. Plus isn't it
2071
- // fun to pass a flag to a tool to pass a flag to pass a flag to a tool
2072
- // to change a flag in a binary?
2073
- if self . config . rpath_enabled ( target) && helpers:: use_host_linker ( target) {
2074
- let libdir = self . sysroot_libdir_relative ( compiler) . to_str ( ) . unwrap ( ) ;
2075
- let rpath = if target. contains ( "apple" ) {
2076
- // Note that we need to take one extra step on macOS to also pass
2077
- // `-Wl,-instal_name,@rpath/...` to get things to work right. To
2078
- // do that we pass a weird flag to the compiler to get it to do
2079
- // so. Note that this is definitely a hack, and we should likely
2080
- // flesh out rpath support more fully in the future.
2081
- rustflags. arg ( "-Zosx-rpath-install-name" ) ;
2082
- Some ( format ! ( "-Wl,-rpath,@loader_path/../{libdir}" ) )
2083
- } else if !target. is_windows ( ) && !target. contains ( "aix" ) && !target. contains ( "xous" ) {
2084
- rustflags. arg ( "-Clink-args=-Wl,-z,origin" ) ;
2085
- Some ( format ! ( "-Wl,-rpath,$ORIGIN/../{libdir}" ) )
2086
- } else {
2087
- None
2088
- } ;
2089
- if let Some ( rpath) = rpath {
2090
- rustflags. arg ( & format ! ( "-Clink-args={rpath}" ) ) ;
2091
- }
2092
- }
2093
-
2094
- for arg in linker_args ( self , compiler. host , LldThreads :: Yes ) {
2095
- hostflags. arg ( & arg) ;
2096
- }
2097
-
2098
- if let Some ( target_linker) = self . linker ( target) {
2099
- let target = crate :: envify ( & target. triple ) ;
2100
- cargo. env ( & format ! ( "CARGO_TARGET_{target}_LINKER" ) , target_linker) ;
2101
- }
2102
- // We want to set -Clinker using Cargo, therefore we only call `linker_flags` and not
2103
- // `linker_args` here.
2104
- for flag in linker_flags ( self , target, LldThreads :: Yes ) {
2105
- rustflags. arg ( & flag) ;
2106
- }
2107
- for arg in linker_args ( self , target, LldThreads :: Yes ) {
2108
- rustdocflags. arg ( & arg) ;
2109
- }
2110
-
2111
- if !self . config . dry_run ( ) && self . cc . borrow ( ) [ & target] . args ( ) . iter ( ) . any ( |arg| arg == "-gz" )
2112
- {
2113
- rustflags. arg ( "-Clink-arg=-gz" ) ;
2114
- }
2115
-
2116
- // Throughout the build Cargo can execute a number of build scripts
2117
- // compiling C/C++ code and we need to pass compilers, archivers, flags, etc
2118
- // obtained previously to those build scripts.
2119
- // Build scripts use either the `cc` crate or `configure/make` so we pass
2120
- // the options through environment variables that are fetched and understood by both.
2121
- //
2122
- // FIXME: the guard against msvc shouldn't need to be here
2123
- if target. is_msvc ( ) {
2124
- if let Some ( ref cl) = self . config . llvm_clang_cl {
2125
- cargo. env ( "CC" , cl) . env ( "CXX" , cl) ;
2126
- }
2127
- } else {
2128
- let ccache = self . config . ccache . as_ref ( ) ;
2129
- let ccacheify = |s : & Path | {
2130
- let ccache = match ccache {
2131
- Some ( ref s) => s,
2132
- None => return s. display ( ) . to_string ( ) ,
2133
- } ;
2134
- // FIXME: the cc-rs crate only recognizes the literal strings
2135
- // `ccache` and `sccache` when doing caching compilations, so we
2136
- // mirror that here. It should probably be fixed upstream to
2137
- // accept a new env var or otherwise work with custom ccache
2138
- // vars.
2139
- match & ccache[ ..] {
2140
- "ccache" | "sccache" => format ! ( "{} {}" , ccache, s. display( ) ) ,
2141
- _ => s. display ( ) . to_string ( ) ,
2142
- }
2143
- } ;
2144
- let triple_underscored = target. triple . replace ( "-" , "_" ) ;
2145
- let cc = ccacheify ( & self . cc ( target) ) ;
2146
- cargo. env ( format ! ( "CC_{triple_underscored}" ) , & cc) ;
2147
-
2148
- let cflags = self . cflags ( target, GitRepo :: Rustc , CLang :: C ) . join ( " " ) ;
2149
- cargo. env ( format ! ( "CFLAGS_{triple_underscored}" ) , & cflags) ;
2150
-
2151
- if let Some ( ar) = self . ar ( target) {
2152
- let ranlib = format ! ( "{} s" , ar. display( ) ) ;
2153
- cargo
2154
- . env ( format ! ( "AR_{triple_underscored}" ) , ar)
2155
- . env ( format ! ( "RANLIB_{triple_underscored}" ) , ranlib) ;
2156
- }
2157
-
2158
- if let Ok ( cxx) = self . cxx ( target) {
2159
- let cxx = ccacheify ( & cxx) ;
2160
- let cxxflags = self . cflags ( target, GitRepo :: Rustc , CLang :: Cxx ) . join ( " " ) ;
2161
- cargo
2162
- . env ( format ! ( "CXX_{triple_underscored}" ) , & cxx)
2163
- . env ( format ! ( "CXXFLAGS_{triple_underscored}" ) , cxxflags) ;
2164
- }
2021
+ Cargo {
2022
+ command : cargo,
2023
+ compiler,
2024
+ target,
2025
+ rustflags,
2026
+ rustdocflags,
2027
+ hostflags,
2028
+ allow_features,
2165
2029
}
2166
2030
}
2167
2031
@@ -2364,17 +2228,46 @@ impl HostFlags {
2364
2228
#[ derive( Debug ) ]
2365
2229
pub struct Cargo {
2366
2230
command : Command ,
2231
+ compiler : Compiler ,
2232
+ target : TargetSelection ,
2367
2233
rustflags : Rustflags ,
2368
2234
rustdocflags : Rustflags ,
2369
2235
hostflags : HostFlags ,
2370
2236
allow_features : String ,
2371
2237
}
2372
2238
2373
2239
impl Cargo {
2240
+ /// Calls `Builder::cargo` and `Cargo::configure_linker` to prepare an invocation of `cargo` to be run.
2241
+ pub fn new (
2242
+ builder : & Builder < ' _ > ,
2243
+ compiler : Compiler ,
2244
+ mode : Mode ,
2245
+ source_type : SourceType ,
2246
+ target : TargetSelection ,
2247
+ cmd : & str ,
2248
+ ) -> Cargo {
2249
+ let mut cargo = builder. cargo ( compiler, mode, source_type, target, cmd) ;
2250
+ cargo. configure_linker ( builder) ;
2251
+ cargo
2252
+ }
2253
+
2254
+ /// Same as `Cargo::new` except this one doesn't configure the linker with `Cargo::configure_linker`
2255
+ pub fn new_for_mir_opt_tests (
2256
+ builder : & Builder < ' _ > ,
2257
+ compiler : Compiler ,
2258
+ mode : Mode ,
2259
+ source_type : SourceType ,
2260
+ target : TargetSelection ,
2261
+ cmd : & str ,
2262
+ ) -> Cargo {
2263
+ builder. cargo ( compiler, mode, source_type, target, cmd)
2264
+ }
2265
+
2374
2266
pub fn rustdocflag ( & mut self , arg : & str ) -> & mut Cargo {
2375
2267
self . rustdocflags . arg ( arg) ;
2376
2268
self
2377
2269
}
2270
+
2378
2271
pub fn rustflag ( & mut self , arg : & str ) -> & mut Cargo {
2379
2272
self . rustflags . arg ( arg) ;
2380
2273
self
@@ -2404,8 +2297,8 @@ impl Cargo {
2404
2297
self
2405
2298
}
2406
2299
2407
- pub fn add_rustc_lib_path ( & mut self , builder : & Builder < ' _ > , compiler : Compiler ) {
2408
- builder. add_rustc_lib_path ( compiler, & mut self . command ) ;
2300
+ pub fn add_rustc_lib_path ( & mut self , builder : & Builder < ' _ > ) {
2301
+ builder. add_rustc_lib_path ( self . compiler , & mut self . command ) ;
2409
2302
}
2410
2303
2411
2304
pub fn current_dir ( & mut self , dir : & Path ) -> & mut Cargo {
@@ -2424,6 +2317,134 @@ impl Cargo {
2424
2317
self . allow_features . push_str ( features) ;
2425
2318
self
2426
2319
}
2320
+
2321
+ fn configure_linker ( & mut self , builder : & Builder < ' _ > ) -> & mut Cargo {
2322
+ let target = self . target ;
2323
+ let compiler = self . compiler ;
2324
+
2325
+ // Dealing with rpath here is a little special, so let's go into some
2326
+ // detail. First off, `-rpath` is a linker option on Unix platforms
2327
+ // which adds to the runtime dynamic loader path when looking for
2328
+ // dynamic libraries. We use this by default on Unix platforms to ensure
2329
+ // that our nightlies behave the same on Windows, that is they work out
2330
+ // of the box. This can be disabled by setting `rpath = false` in `[rust]`
2331
+ // table of `config.toml`
2332
+ //
2333
+ // Ok, so the astute might be wondering "why isn't `-C rpath` used
2334
+ // here?" and that is indeed a good question to ask. This codegen
2335
+ // option is the compiler's current interface to generating an rpath.
2336
+ // Unfortunately it doesn't quite suffice for us. The flag currently
2337
+ // takes no value as an argument, so the compiler calculates what it
2338
+ // should pass to the linker as `-rpath`. This unfortunately is based on
2339
+ // the **compile time** directory structure which when building with
2340
+ // Cargo will be very different than the runtime directory structure.
2341
+ //
2342
+ // All that's a really long winded way of saying that if we use
2343
+ // `-Crpath` then the executables generated have the wrong rpath of
2344
+ // something like `$ORIGIN/deps` when in fact the way we distribute
2345
+ // rustc requires the rpath to be `$ORIGIN/../lib`.
2346
+ //
2347
+ // So, all in all, to set up the correct rpath we pass the linker
2348
+ // argument manually via `-C link-args=-Wl,-rpath,...`. Plus isn't it
2349
+ // fun to pass a flag to a tool to pass a flag to pass a flag to a tool
2350
+ // to change a flag in a binary?
2351
+ if builder. config . rpath_enabled ( target) && helpers:: use_host_linker ( target) {
2352
+ let libdir = builder. sysroot_libdir_relative ( compiler) . to_str ( ) . unwrap ( ) ;
2353
+ let rpath = if target. contains ( "apple" ) {
2354
+ // Note that we need to take one extra step on macOS to also pass
2355
+ // `-Wl,-instal_name,@rpath/...` to get things to work right. To
2356
+ // do that we pass a weird flag to the compiler to get it to do
2357
+ // so. Note that this is definitely a hack, and we should likely
2358
+ // flesh out rpath support more fully in the future.
2359
+ self . rustflags . arg ( "-Zosx-rpath-install-name" ) ;
2360
+ Some ( format ! ( "-Wl,-rpath,@loader_path/../{libdir}" ) )
2361
+ } else if !target. is_windows ( ) && !target. contains ( "aix" ) && !target. contains ( "xous" ) {
2362
+ self . rustflags . arg ( "-Clink-args=-Wl,-z,origin" ) ;
2363
+ Some ( format ! ( "-Wl,-rpath,$ORIGIN/../{libdir}" ) )
2364
+ } else {
2365
+ None
2366
+ } ;
2367
+ if let Some ( rpath) = rpath {
2368
+ self . rustflags . arg ( & format ! ( "-Clink-args={rpath}" ) ) ;
2369
+ }
2370
+ }
2371
+
2372
+ for arg in linker_args ( builder, compiler. host , LldThreads :: Yes ) {
2373
+ self . hostflags . arg ( & arg) ;
2374
+ }
2375
+
2376
+ if let Some ( target_linker) = builder. linker ( target) {
2377
+ let target = crate :: envify ( & target. triple ) ;
2378
+ self . command . env ( & format ! ( "CARGO_TARGET_{target}_LINKER" ) , target_linker) ;
2379
+ }
2380
+ // We want to set -Clinker using Cargo, therefore we only call `linker_flags` and not
2381
+ // `linker_args` here.
2382
+ for flag in linker_flags ( builder, target, LldThreads :: Yes ) {
2383
+ self . rustflags . arg ( & flag) ;
2384
+ }
2385
+ for arg in linker_args ( builder, target, LldThreads :: Yes ) {
2386
+ self . rustdocflags . arg ( & arg) ;
2387
+ }
2388
+
2389
+ if !builder. config . dry_run ( )
2390
+ && builder. cc . borrow ( ) [ & target] . args ( ) . iter ( ) . any ( |arg| arg == "-gz" )
2391
+ {
2392
+ self . rustflags . arg ( "-Clink-arg=-gz" ) ;
2393
+ }
2394
+
2395
+ // Throughout the build Cargo can execute a number of build scripts
2396
+ // compiling C/C++ code and we need to pass compilers, archivers, flags, etc
2397
+ // obtained previously to those build scripts.
2398
+ // Build scripts use either the `cc` crate or `configure/make` so we pass
2399
+ // the options through environment variables that are fetched and understood by both.
2400
+ //
2401
+ // FIXME: the guard against msvc shouldn't need to be here
2402
+ if target. is_msvc ( ) {
2403
+ if let Some ( ref cl) = builder. config . llvm_clang_cl {
2404
+ self . command . env ( "CC" , cl) . env ( "CXX" , cl) ;
2405
+ }
2406
+ } else {
2407
+ let ccache = builder. config . ccache . as_ref ( ) ;
2408
+ let ccacheify = |s : & Path | {
2409
+ let ccache = match ccache {
2410
+ Some ( ref s) => s,
2411
+ None => return s. display ( ) . to_string ( ) ,
2412
+ } ;
2413
+ // FIXME: the cc-rs crate only recognizes the literal strings
2414
+ // `ccache` and `sccache` when doing caching compilations, so we
2415
+ // mirror that here. It should probably be fixed upstream to
2416
+ // accept a new env var or otherwise work with custom ccache
2417
+ // vars.
2418
+ match & ccache[ ..] {
2419
+ "ccache" | "sccache" => format ! ( "{} {}" , ccache, s. display( ) ) ,
2420
+ _ => s. display ( ) . to_string ( ) ,
2421
+ }
2422
+ } ;
2423
+ let triple_underscored = target. triple . replace ( "-" , "_" ) ;
2424
+ let cc = ccacheify ( & builder. cc ( target) ) ;
2425
+ self . command . env ( format ! ( "CC_{triple_underscored}" ) , & cc) ;
2426
+
2427
+ let cflags = builder. cflags ( target, GitRepo :: Rustc , CLang :: C ) . join ( " " ) ;
2428
+ self . command . env ( format ! ( "CFLAGS_{triple_underscored}" ) , & cflags) ;
2429
+
2430
+ if let Some ( ar) = builder. ar ( target) {
2431
+ let ranlib = format ! ( "{} s" , ar. display( ) ) ;
2432
+ self . command
2433
+ . env ( format ! ( "AR_{triple_underscored}" ) , ar)
2434
+ . env ( format ! ( "RANLIB_{triple_underscored}" ) , ranlib) ;
2435
+ }
2436
+
2437
+ if let Ok ( cxx) = builder. cxx ( target) {
2438
+ let cxx = ccacheify ( & cxx) ;
2439
+ let cxxflags = builder. cflags ( target, GitRepo :: Rustc , CLang :: Cxx ) . join ( " " ) ;
2440
+ self . command
2441
+ . env ( format ! ( "CXX_{triple_underscored}" ) , & cxx)
2442
+ . env ( format ! ( "CXXFLAGS_{triple_underscored}" ) , cxxflags) ;
2443
+ }
2444
+ }
2445
+
2446
+ self
2447
+ }
2427
2448
}
2428
2449
2429
2450
impl From < Cargo > for Command {
0 commit comments