@@ -23,6 +23,7 @@ use rustc_session::utils::NativeLibKind;
23
23
use rustc_session:: { filesearch, Session } ;
24
24
use rustc_span:: symbol:: Symbol ;
25
25
use rustc_target:: spec:: crt_objects:: CrtObjects ;
26
+ use rustc_target:: spec:: LinkSelfContainedComponents ;
26
27
use rustc_target:: spec:: LinkSelfContainedDefault ;
27
28
use rustc_target:: spec:: { Cc , LinkOutputKind , LinkerFlavor , Lld , PanicStrategy } ;
28
29
use rustc_target:: spec:: { RelocModel , RelroLevel , SanitizerSet , SplitDebuginfo } ;
@@ -721,6 +722,7 @@ fn link_natively<'a>(
721
722
) -> Result < ( ) , ErrorGuaranteed > {
722
723
info ! ( "preparing {:?} to {:?}" , crate_type, out_filename) ;
723
724
let ( linker_path, flavor) = linker_and_flavor ( sess) ;
725
+ let self_contained_components = self_contained_components ( sess, crate_type) ;
724
726
let mut cmd = linker_with_args (
725
727
& linker_path,
726
728
flavor,
@@ -730,6 +732,7 @@ fn link_natively<'a>(
730
732
tmpdir,
731
733
out_filename,
732
734
codegen_results,
735
+ self_contained_components,
733
736
) ?;
734
737
735
738
linker:: disable_localization ( & mut cmd) ;
@@ -805,14 +808,14 @@ fn link_natively<'a>(
805
808
"Linker does not support -static-pie command line option. Retrying with -static instead."
806
809
) ;
807
810
// Mirror `add_(pre,post)_link_objects` to replace CRT objects.
808
- let self_contained = self_contained ( sess , crate_type ) ;
811
+ let self_contained_crt_objects = self_contained_components . is_crt_objects_enabled ( ) ;
809
812
let opts = & sess. target ;
810
- let pre_objects = if self_contained {
813
+ let pre_objects = if self_contained_crt_objects {
811
814
& opts. pre_link_objects_self_contained
812
815
} else {
813
816
& opts. pre_link_objects
814
817
} ;
815
- let post_objects = if self_contained {
818
+ let post_objects = if self_contained_crt_objects {
816
819
& opts. post_link_objects_self_contained
817
820
} else {
818
821
& opts. post_link_objects
@@ -823,7 +826,9 @@ fn link_natively<'a>(
823
826
. iter ( )
824
827
. copied ( )
825
828
. flatten ( )
826
- . map ( |obj| get_object_file_path ( sess, obj, self_contained) . into_os_string ( ) )
829
+ . map ( |obj| {
830
+ get_object_file_path ( sess, obj, self_contained_crt_objects) . into_os_string ( )
831
+ } )
827
832
. collect :: < Vec < _ > > ( )
828
833
} ;
829
834
let pre_objects_static_pie = get_objects ( pre_objects, LinkOutputKind :: StaticPicExe ) ;
@@ -1703,42 +1708,43 @@ fn detect_self_contained_mingw(sess: &Session) -> bool {
1703
1708
/// Various toolchain components used during linking are used from rustc distribution
1704
1709
/// instead of being found somewhere on the host system.
1705
1710
/// We only provide such support for a very limited number of targets.
1706
- fn self_contained ( sess : & Session , crate_type : CrateType ) -> bool {
1707
- // Emit an error if the user requested self-contained mode on the CLI but the target explicitly
1708
- // refuses it.
1709
- if let Some ( self_contained) = sess. opts . cg . link_self_contained . explicitly_set {
1710
- if sess. target . link_self_contained . is_disabled ( ) {
1711
- sess. emit_err ( errors:: UnsupportedLinkSelfContained ) ;
1712
- }
1713
- return self_contained;
1714
- }
1715
-
1716
- match sess. target . link_self_contained {
1717
- LinkSelfContainedDefault :: False => false ,
1718
- LinkSelfContainedDefault :: True => true ,
1719
- LinkSelfContainedDefault :: WithComponents ( components) => {
1720
- if components. is_all ( ) {
1721
- true
1722
- } else if components. is_empty ( ) {
1723
- false
1724
- } else {
1725
- // FIXME: Currently no target makes use of individual components to mean
1726
- // self-contained linking is fully enabled, in the sense of what the code downstream
1727
- // from here expects. Until components are handled a bit more deeply, we can
1728
- // consider that it's disabled and remain backwards compatible.
1729
- false
1711
+ fn self_contained_components ( sess : & Session , crate_type : CrateType ) -> LinkSelfContainedComponents {
1712
+ // Turn the backwards compatible bool values for `self_contained` into fully inferred
1713
+ // `LinkSelfContainedComponents`.
1714
+ let self_contained =
1715
+ if let Some ( self_contained) = sess. opts . cg . link_self_contained . explicitly_set {
1716
+ // Emit an error if the user requested self-contained mode on the CLI but the target
1717
+ // explicitly refuses it.
1718
+ if sess. target . link_self_contained . is_disabled ( ) {
1719
+ sess. emit_err ( errors:: UnsupportedLinkSelfContained ) ;
1730
1720
}
1731
- }
1721
+ self_contained
1722
+ } else {
1723
+ match sess. target . link_self_contained {
1724
+ LinkSelfContainedDefault :: False => false ,
1725
+ LinkSelfContainedDefault :: True => true ,
1726
+
1727
+ LinkSelfContainedDefault :: WithComponents ( components) => {
1728
+ // For target specs with explicitly enabled components, we can return them
1729
+ // directly.
1730
+ return components;
1731
+ }
1732
1732
1733
- // FIXME: Find a better heuristic for "native musl toolchain is available",
1734
- // based on host and linker path, for example.
1735
- // (https://github.com/rust-lang/rust/pull/71769#issuecomment-626330237).
1736
- LinkSelfContainedDefault :: InferredForMusl => sess. crt_static ( Some ( crate_type) ) ,
1737
- LinkSelfContainedDefault :: InferredForMingw => {
1738
- sess. host == sess. target
1739
- && sess. target . vendor != "uwp"
1740
- && detect_self_contained_mingw ( & sess)
1741
- }
1733
+ // FIXME: Find a better heuristic for "native musl toolchain is available",
1734
+ // based on host and linker path, for example.
1735
+ // (https://github.com/rust-lang/rust/pull/71769#issuecomment-626330237).
1736
+ LinkSelfContainedDefault :: InferredForMusl => sess. crt_static ( Some ( crate_type) ) ,
1737
+ LinkSelfContainedDefault :: InferredForMingw => {
1738
+ sess. host == sess. target
1739
+ && sess. target . vendor != "uwp"
1740
+ && detect_self_contained_mingw ( & sess)
1741
+ }
1742
+ }
1743
+ } ;
1744
+ if self_contained {
1745
+ LinkSelfContainedComponents :: all ( )
1746
+ } else {
1747
+ LinkSelfContainedComponents :: empty ( )
1742
1748
}
1743
1749
}
1744
1750
@@ -2062,13 +2068,14 @@ fn linker_with_args<'a>(
2062
2068
tmpdir : & Path ,
2063
2069
out_filename : & Path ,
2064
2070
codegen_results : & CodegenResults ,
2071
+ self_contained_components : LinkSelfContainedComponents ,
2065
2072
) -> Result < Command , ErrorGuaranteed > {
2066
- let self_contained = self_contained ( sess , crate_type ) ;
2073
+ let self_contained_crt_objects = self_contained_components . is_crt_objects_enabled ( ) ;
2067
2074
let cmd = & mut * super :: linker:: get_linker (
2068
2075
sess,
2069
2076
path,
2070
2077
flavor,
2071
- self_contained ,
2078
+ self_contained_components . are_any_components_enabled ( ) ,
2072
2079
& codegen_results. crate_info . target_cpu ,
2073
2080
) ;
2074
2081
let link_output_kind = link_output_kind ( sess, crate_type) ;
@@ -2095,7 +2102,7 @@ fn linker_with_args<'a>(
2095
2102
// ------------ Object code and libraries, order-dependent ------------
2096
2103
2097
2104
// Pre-link CRT objects.
2098
- add_pre_link_objects ( cmd, sess, flavor, link_output_kind, self_contained ) ;
2105
+ add_pre_link_objects ( cmd, sess, flavor, link_output_kind, self_contained_crt_objects ) ;
2099
2106
2100
2107
add_linked_symbol_object (
2101
2108
cmd,
@@ -2238,7 +2245,7 @@ fn linker_with_args<'a>(
2238
2245
cmd,
2239
2246
sess,
2240
2247
link_output_kind,
2241
- self_contained ,
2248
+ self_contained_components ,
2242
2249
flavor,
2243
2250
crate_type,
2244
2251
codegen_results,
@@ -2254,7 +2261,7 @@ fn linker_with_args<'a>(
2254
2261
// ------------ Object code and libraries, order-dependent ------------
2255
2262
2256
2263
// Post-link CRT objects.
2257
- add_post_link_objects ( cmd, sess, link_output_kind, self_contained ) ;
2264
+ add_post_link_objects ( cmd, sess, link_output_kind, self_contained_crt_objects ) ;
2258
2265
2259
2266
// ------------ Late order-dependent options ------------
2260
2267
@@ -2271,15 +2278,15 @@ fn add_order_independent_options(
2271
2278
cmd : & mut dyn Linker ,
2272
2279
sess : & Session ,
2273
2280
link_output_kind : LinkOutputKind ,
2274
- self_contained : bool ,
2281
+ self_contained_components : LinkSelfContainedComponents ,
2275
2282
flavor : LinkerFlavor ,
2276
2283
crate_type : CrateType ,
2277
2284
codegen_results : & CodegenResults ,
2278
2285
out_filename : & Path ,
2279
2286
tmpdir : & Path ,
2280
2287
) {
2281
2288
// Take care of the flavors and CLI options requesting the `lld` linker.
2282
- add_lld_args ( cmd, sess, flavor) ;
2289
+ add_lld_args ( cmd, sess, flavor, self_contained_components ) ;
2283
2290
2284
2291
add_apple_sdk ( cmd, sess, flavor) ;
2285
2292
@@ -2304,7 +2311,7 @@ fn add_order_independent_options(
2304
2311
// Make the binary compatible with data execution prevention schemes.
2305
2312
cmd. add_no_exec ( ) ;
2306
2313
2307
- if self_contained {
2314
+ if self_contained_components . is_crt_objects_enabled ( ) {
2308
2315
cmd. no_crt_objects ( ) ;
2309
2316
}
2310
2317
@@ -2335,7 +2342,7 @@ fn add_order_independent_options(
2335
2342
2336
2343
cmd. linker_plugin_lto ( ) ;
2337
2344
2338
- add_library_search_dirs ( cmd, sess, self_contained ) ;
2345
+ add_library_search_dirs ( cmd, sess, self_contained_components . are_any_components_enabled ( ) ) ;
2339
2346
2340
2347
cmd. output_filename ( out_filename) ;
2341
2348
@@ -2985,8 +2992,16 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, errors::AppleSdkRootErro
2985
2992
/// invoke it:
2986
2993
/// - when the self-contained linker flag is active: the build of `lld` distributed with rustc,
2987
2994
/// - or any `lld` available to `cc`.
2988
- fn add_lld_args ( cmd : & mut dyn Linker , sess : & Session , flavor : LinkerFlavor ) {
2989
- debug ! ( "add_lld_args requested, flavor: '{flavor:?}'" ) ;
2995
+ fn add_lld_args (
2996
+ cmd : & mut dyn Linker ,
2997
+ sess : & Session ,
2998
+ flavor : LinkerFlavor ,
2999
+ self_contained_components : LinkSelfContainedComponents ,
3000
+ ) {
3001
+ debug ! (
3002
+ "add_lld_args requested, flavor: '{:?}', target self-contained components: {:?}" ,
3003
+ flavor, self_contained_components,
3004
+ ) ;
2990
3005
2991
3006
// If the flavor doesn't use a C/C++ compiler to invoke the linker, or doesn't opt in to `lld`,
2992
3007
// we don't need to do anything.
@@ -3000,7 +3015,7 @@ fn add_lld_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
3000
3015
// - if the self-contained linker is enabled on the CLI or by the target spec,
3001
3016
// - and if the self-contained linker is not disabled on the CLI.
3002
3017
let self_contained_cli = sess. opts . cg . link_self_contained . is_linker_enabled ( ) ;
3003
- let self_contained_target = sess . target . options . link_self_contained . is_linker_enabled ( ) ;
3018
+ let self_contained_target = self_contained_components . is_linker_enabled ( ) ;
3004
3019
3005
3020
// FIXME: in the future, codegen backends may need to have more control over this process: they
3006
3021
// don't always support all the features the linker expects here, and vice versa. For example,
0 commit comments