Skip to content

Commit c704aad

Browse files
committed
unify LinkSelfContained and LinkSelfContainedDefault
Removes the backwards-compatible `LinkSelfContainedDefault`, by incorporating the remaining specifics into `LinkSelfContained`. Then renames the modern options to keep the old name.
1 parent b816207 commit c704aad

File tree

8 files changed

+59
-88
lines changed

8 files changed

+59
-88
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_session::utils::NativeLibKind;
2323
use rustc_session::{filesearch, Session};
2424
use rustc_span::symbol::Symbol;
2525
use rustc_target::spec::crt_objects::CrtObjects;
26-
use rustc_target::spec::LinkSelfContained;
26+
use rustc_target::spec::LinkSelfContainedDefault;
2727
use rustc_target::spec::{Cc, LinkOutputKind, LinkerFlavor, Lld, PanicStrategy};
2828
use rustc_target::spec::{RelocModel, RelroLevel, SanitizerSet, SplitDebuginfo};
2929

@@ -1714,9 +1714,9 @@ fn self_contained(sess: &Session, crate_type: CrateType) -> bool {
17141714
}
17151715

17161716
match sess.target.link_self_contained {
1717-
LinkSelfContained::True => true,
1718-
LinkSelfContained::False => false,
1719-
LinkSelfContained::WithComponents(components) => {
1717+
LinkSelfContainedDefault::True => true,
1718+
LinkSelfContainedDefault::False => false,
1719+
LinkSelfContainedDefault::WithComponents(components) => {
17201720
if components.is_all() {
17211721
true
17221722
} else if components.is_empty() {
@@ -1733,8 +1733,8 @@ fn self_contained(sess: &Session, crate_type: CrateType) -> bool {
17331733
// FIXME: Find a better heuristic for "native musl toolchain is available",
17341734
// based on host and linker path, for example.
17351735
// (https://github.com/rust-lang/rust/pull/71769#issuecomment-626330237).
1736-
LinkSelfContained::InferredForMusl => sess.crt_static(Some(crate_type)),
1737-
LinkSelfContained::InferredForMingw => {
1736+
LinkSelfContainedDefault::InferredForMusl => sess.crt_static(Some(crate_type)),
1737+
LinkSelfContainedDefault::InferredForMingw => {
17381738
sess.host == sess.target
17391739
&& sess.target.vendor != "uwp"
17401740
&& detect_self_contained_mingw(&sess)

compiler/rustc_target/src/spec/crt_objects.rs

-38
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@
4040
//! but not gcc's. As a result rustc cannot link with C++ static libraries (#36710)
4141
//! when linking in self-contained mode.
4242
43-
use crate::json::{Json, ToJson};
4443
use crate::spec::LinkOutputKind;
4544
use std::borrow::Cow;
4645
use std::collections::BTreeMap;
47-
use std::str::FromStr;
4846

4947
pub type CrtObjects = BTreeMap<LinkOutputKind, Vec<Cow<'static, str>>>;
5048

@@ -123,39 +121,3 @@ pub(super) fn pre_wasi_self_contained() -> CrtObjects {
123121
pub(super) fn post_wasi_self_contained() -> CrtObjects {
124122
new(&[])
125123
}
126-
127-
/// Which logic to use to determine whether to use self-contained linking mode
128-
/// if `-Clink-self-contained` is not specified explicitly.
129-
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
130-
pub enum LinkSelfContainedDefault {
131-
False,
132-
True,
133-
Musl,
134-
Mingw,
135-
}
136-
137-
impl FromStr for LinkSelfContainedDefault {
138-
type Err = ();
139-
140-
fn from_str(s: &str) -> Result<LinkSelfContainedDefault, ()> {
141-
Ok(match s {
142-
"false" => LinkSelfContainedDefault::False,
143-
"true" | "wasm" => LinkSelfContainedDefault::True,
144-
"musl" => LinkSelfContainedDefault::Musl,
145-
"mingw" => LinkSelfContainedDefault::Mingw,
146-
_ => return Err(()),
147-
})
148-
}
149-
}
150-
151-
impl ToJson for LinkSelfContainedDefault {
152-
fn to_json(&self) -> Json {
153-
match *self {
154-
LinkSelfContainedDefault::False => "false",
155-
LinkSelfContainedDefault::True => "true",
156-
LinkSelfContainedDefault::Musl => "musl",
157-
LinkSelfContainedDefault::Mingw => "mingw",
158-
}
159-
.to_json()
160-
}
161-
}

compiler/rustc_target/src/spec/linux_musl_base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::spec::crt_objects;
2-
use crate::spec::LinkSelfContained;
2+
use crate::spec::LinkSelfContainedDefault;
33
use crate::spec::TargetOptions;
44

55
pub fn opts() -> TargetOptions {
@@ -8,7 +8,7 @@ pub fn opts() -> TargetOptions {
88
base.env = "musl".into();
99
base.pre_link_objects_self_contained = crt_objects::pre_musl_self_contained();
1010
base.post_link_objects_self_contained = crt_objects::post_musl_self_contained();
11-
base.link_self_contained = LinkSelfContained::InferredForMusl;
11+
base.link_self_contained = LinkSelfContainedDefault::InferredForMusl;
1212

1313
// These targets statically link libc by default
1414
base.crt_static_default = true;

compiler/rustc_target/src/spec/mod.rs

+43-34
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::abi::call::Conv;
3838
use crate::abi::{Endian, Integer, Size, TargetDataLayout, TargetDataLayoutErrors};
3939
use crate::json::{Json, ToJson};
4040
use crate::spec::abi::{lookup as lookup_abi, Abi};
41-
use crate::spec::crt_objects::{CrtObjects, LinkSelfContainedDefault};
41+
use crate::spec::crt_objects::CrtObjects;
4242
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4343
use rustc_fs_util::try_canonicalize;
4444
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
@@ -542,7 +542,7 @@ impl ToJson for LinkerFlavorCli {
542542
/// - explicitly enabling some of the self-contained linking components, e.g. the linker component
543543
/// to use `rust-lld`
544544
#[derive(Clone, Copy, PartialEq, Debug)]
545-
pub enum LinkSelfContained {
545+
pub enum LinkSelfContainedDefault {
546546
/// The target spec explicitly enables self-contained linking.
547547
True,
548548

@@ -560,10 +560,25 @@ pub enum LinkSelfContained {
560560
WithComponents(LinkSelfContainedComponents),
561561
}
562562

563-
impl ToJson for LinkSelfContained {
563+
/// Parses a backwards-compatible `-Clink-self-contained` option string, without components.
564+
impl FromStr for LinkSelfContainedDefault {
565+
type Err = ();
566+
567+
fn from_str(s: &str) -> Result<LinkSelfContainedDefault, ()> {
568+
Ok(match s {
569+
"false" => LinkSelfContainedDefault::False,
570+
"true" | "wasm" => LinkSelfContainedDefault::True,
571+
"musl" => LinkSelfContainedDefault::InferredForMusl,
572+
"mingw" => LinkSelfContainedDefault::InferredForMingw,
573+
_ => return Err(()),
574+
})
575+
}
576+
}
577+
578+
impl ToJson for LinkSelfContainedDefault {
564579
fn to_json(&self) -> Json {
565580
match *self {
566-
LinkSelfContained::WithComponents(components) => {
581+
LinkSelfContainedDefault::WithComponents(components) => {
567582
// Serialize the components in a json object's `components` field, to prepare for a
568583
// future where `crt-objects-fallback` is removed from the json specs and
569584
// incorporated as a field here.
@@ -572,29 +587,31 @@ impl ToJson for LinkSelfContained {
572587
map.to_json()
573588
}
574589

575-
// Stable values backwards-compatible with `LinkSelfContainedDefault`
576-
LinkSelfContained::True => "true".to_json(),
577-
LinkSelfContained::False => "false".to_json(),
578-
LinkSelfContained::InferredForMusl => "musl".to_json(),
579-
LinkSelfContained::InferredForMingw => "mingw".to_json(),
590+
// Stable backwards-compatible values
591+
LinkSelfContainedDefault::True => "true".to_json(),
592+
LinkSelfContainedDefault::False => "false".to_json(),
593+
LinkSelfContainedDefault::InferredForMusl => "musl".to_json(),
594+
LinkSelfContainedDefault::InferredForMingw => "mingw".to_json(),
580595
}
581596
}
582597
}
583598

584-
impl LinkSelfContained {
599+
impl LinkSelfContainedDefault {
585600
/// Returns whether the target spec has self-contained linking explicitly disabled. Used to emit
586601
/// errors if the user then enables it on the CLI.
587602
pub fn is_disabled(self) -> bool {
588-
self == LinkSelfContained::False
603+
self == LinkSelfContainedDefault::False
589604
}
590605

591606
/// Returns whether the target spec explictly requests self-contained linking, i.e. not via
592607
/// inference.
593608
pub fn is_linker_enabled(self) -> bool {
594609
match self {
595-
LinkSelfContained::True => true,
596-
LinkSelfContained::False => false,
597-
LinkSelfContained::WithComponents(c) => c.contains(LinkSelfContainedComponents::LINKER),
610+
LinkSelfContainedDefault::True => true,
611+
LinkSelfContainedDefault::False => false,
612+
LinkSelfContainedDefault::WithComponents(c) => {
613+
c.contains(LinkSelfContainedComponents::LINKER)
614+
}
598615
_ => false,
599616
}
600617
}
@@ -604,23 +621,12 @@ impl LinkSelfContained {
604621
/// - the other variants as a backwards-compatible `crt-objects-fallback` string
605622
fn json_key(self) -> &'static str {
606623
match self {
607-
LinkSelfContained::WithComponents(_) => "link-self-contained",
624+
LinkSelfContainedDefault::WithComponents(_) => "link-self-contained",
608625
_ => "crt-objects-fallback",
609626
}
610627
}
611628
}
612629

613-
impl From<LinkSelfContainedDefault> for LinkSelfContained {
614-
fn from(value: LinkSelfContainedDefault) -> Self {
615-
match value {
616-
LinkSelfContainedDefault::True => LinkSelfContained::True,
617-
LinkSelfContainedDefault::False => LinkSelfContained::False,
618-
LinkSelfContainedDefault::Musl => LinkSelfContained::InferredForMusl,
619-
LinkSelfContainedDefault::Mingw => LinkSelfContained::InferredForMingw,
620-
}
621-
}
622-
}
623-
624630
bitflags::bitflags! {
625631
#[derive(Default)]
626632
/// The `-C link-self-contained` components that can individually be enabled or disabled.
@@ -1888,7 +1894,7 @@ pub struct TargetOptions {
18881894
pub post_link_objects_self_contained: CrtObjects,
18891895
/// Behavior for the self-contained linking mode: inferred for some targets, or explicitly
18901896
/// enabled (in bulk, or with individual components).
1891-
pub link_self_contained: LinkSelfContained,
1897+
pub link_self_contained: LinkSelfContainedDefault,
18921898

18931899
/// Linker arguments that are passed *before* any user-defined libraries.
18941900
pub pre_link_args: LinkArgs,
@@ -2363,7 +2369,7 @@ impl Default for TargetOptions {
23632369
post_link_objects: Default::default(),
23642370
pre_link_objects_self_contained: Default::default(),
23652371
post_link_objects_self_contained: Default::default(),
2366-
link_self_contained: LinkSelfContained::False,
2372+
link_self_contained: LinkSelfContainedDefault::False,
23672373
pre_link_args: LinkArgs::new(),
23682374
pre_link_args_json: LinkArgsCli::new(),
23692375
late_link_args: LinkArgs::new(),
@@ -2844,7 +2850,7 @@ impl Target {
28442850
}
28452851
Ok::<(), String>(())
28462852
} );
2847-
($key_name:ident, LinkSelfContained) => ( {
2853+
($key_name:ident, link_self_contained_components) => ( {
28482854
// Skeleton of what needs to be parsed:
28492855
//
28502856
// ```
@@ -2873,18 +2879,18 @@ impl Target {
28732879
_ => return Err(format!("not a string: {:?}", s)),
28742880
};
28752881
}
2876-
base.$key_name = LinkSelfContained::WithComponents(components);
2882+
base.$key_name = LinkSelfContainedDefault::WithComponents(components);
28772883
} else {
28782884
incorrect_type.push(name)
28792885
}
28802886
}
28812887
Ok::<(), String>(())
28822888
} );
2883-
($key_name:ident = $json_name:expr, LinkSelfContainedDefault) => ( {
2889+
($key_name:ident = $json_name:expr, link_self_contained_backwards_compatible) => ( {
28842890
let name = $json_name;
28852891
obj.remove(name).and_then(|o| o.as_str().and_then(|s| {
28862892
match s.parse::<LinkSelfContainedDefault>() {
2887-
Ok(lsc_default) => base.$key_name = lsc_default.into(),
2893+
Ok(lsc_default) => base.$key_name = lsc_default,
28882894
_ => return Some(Err(format!("'{}' is not a valid `-Clink-self-contained` default. \
28892895
Use 'false', 'true', 'musl' or 'mingw'", s))),
28902896
}
@@ -3034,9 +3040,12 @@ impl Target {
30343040
key!(pre_link_objects_self_contained = "pre-link-objects-fallback", link_objects);
30353041
key!(post_link_objects_self_contained = "post-link-objects-fallback", link_objects);
30363042
// Deserializes the backwards-compatible variants of `-Clink-self-contained`
3037-
key!(link_self_contained = "crt-objects-fallback", LinkSelfContainedDefault)?;
3043+
key!(
3044+
link_self_contained = "crt-objects-fallback",
3045+
link_self_contained_backwards_compatible
3046+
)?;
30383047
// Deserializes the components variant of `-Clink-self-contained`
3039-
key!(link_self_contained, LinkSelfContained)?;
3048+
key!(link_self_contained, link_self_contained_components)?;
30403049
key!(pre_link_args_json = "pre-link-args", link_args);
30413050
key!(late_link_args_json = "late-link-args", link_args);
30423051
key!(late_link_args_dynamic_json = "late-link-args-dynamic", link_args);

compiler/rustc_target/src/spec/wasm32_wasi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
//! you know what you're getting in to!
7474
7575
use super::crt_objects;
76-
use super::LinkSelfContained;
76+
use super::LinkSelfContainedDefault;
7777
use super::{wasm_base, Cc, LinkerFlavor, Target};
7878

7979
pub fn target() -> Target {
@@ -86,7 +86,7 @@ pub fn target() -> Target {
8686
options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
8787

8888
// FIXME: Figure out cases in which WASM needs to link with a native toolchain.
89-
options.link_self_contained = LinkSelfContained::True;
89+
options.link_self_contained = LinkSelfContainedDefault::True;
9090

9191
// Right now this is a bit of a workaround but we're currently saying that
9292
// the target by default has a static crt which we're taking as a signal

compiler/rustc_target/src/spec/wasm32_wasi_preview1_threads.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
//! you know what you're getting in to!
7474
7575
use super::crt_objects;
76-
use super::LinkSelfContained;
76+
use super::LinkSelfContainedDefault;
7777
use super::{wasm_base, Cc, LinkerFlavor, Target};
7878

7979
pub fn target() -> Target {
@@ -99,7 +99,7 @@ pub fn target() -> Target {
9999
options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
100100

101101
// FIXME: Figure out cases in which WASM needs to link with a native toolchain.
102-
options.link_self_contained = LinkSelfContained::True;
102+
options.link_self_contained = LinkSelfContainedDefault::True;
103103

104104
// Right now this is a bit of a workaround but we're currently saying that
105105
// the target by default has a static crt which we're taking as a signal

compiler/rustc_target/src/spec/wasm_base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{cvs, Cc, LinkerFlavor, PanicStrategy, RelocModel, TargetOptions, TlsModel};
2-
use crate::spec::LinkSelfContained;
2+
use crate::spec::LinkSelfContainedDefault;
33

44
pub fn options() -> TargetOptions {
55
macro_rules! args {
@@ -100,7 +100,7 @@ pub fn options() -> TargetOptions {
100100
// rust-lang/rust#104137: cannot blindly remove this without putting in
101101
// some other way to compensate for lack of `-nostartfiles` in linker
102102
// invocation.
103-
link_self_contained: LinkSelfContained::True,
103+
link_self_contained: LinkSelfContainedDefault::True,
104104

105105
// This has no effect in LLVM 8 or prior, but in LLVM 9 and later when
106106
// PIC code is implemented this has quite a drastic effect if it stays

compiler/rustc_target/src/spec/windows_gnu_base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::spec::crt_objects;
2-
use crate::spec::LinkSelfContained;
2+
use crate::spec::LinkSelfContainedDefault;
33
use crate::spec::{cvs, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
44
use std::borrow::Cow;
55

@@ -91,7 +91,7 @@ pub fn opts() -> TargetOptions {
9191
post_link_objects: crt_objects::post_mingw(),
9292
pre_link_objects_self_contained: crt_objects::pre_mingw_self_contained(),
9393
post_link_objects_self_contained: crt_objects::post_mingw_self_contained(),
94-
link_self_contained: LinkSelfContained::InferredForMingw,
94+
link_self_contained: LinkSelfContainedDefault::InferredForMingw,
9595
late_link_args,
9696
late_link_args_dynamic,
9797
late_link_args_static,

0 commit comments

Comments
 (0)