Skip to content

Commit 08395ed

Browse files
committed
remove freeform component from LinkerFlavorCli
1 parent a4c8128 commit 08395ed

12 files changed

+93
-122
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+64-72
Original file line numberDiff line numberDiff line change
@@ -2696,28 +2696,28 @@ fn get_apple_sdk_root(sdk_name: &str) -> Result<String, String> {
26962696

26972697
/// This takes care of the various possible enrichments to the linking that can be requested on the
26982698
/// CLI (and emitting errors and warnings when applicable):
2699-
/// - shortcuts to `-fuse-ld` with the `gcc` flavor
2700-
/// - the unstable `-Zgcc-ld=lld` flag to use `rust-lld`, stabilized as the following item
2699+
/// - the unstable `-Zgcc-ld=lld` flag to use `rust-lld`
2700+
/// - the shortcut to `-fuse-ld=lld` with the `gcc` flavor, to eventually use `rust-lld` when
2701+
/// `-Clink-self-contained=linker` is introduced.
27012702
fn handle_cli_linker_flavors(
27022703
cmd: &mut dyn Linker,
27032704
sess: &Session,
27042705
flavor: LinkerFlavor,
27052706
crt_objects_fallback: bool,
27062707
) {
27072708
let unstable_gcc_lld = sess.opts.unstable_opts.gcc_ld == Some(LdImpl::Lld);
2708-
if unstable_gcc_lld {
2709-
// Sanity check: ensure `gcc` is the currently selected flavor.
2710-
if LinkerFlavor::Gcc != flavor {
2711-
sess.fatal("`-Zgcc-ld` is used even though the linker flavor is not `gcc`");
2712-
}
2709+
2710+
// Sanity check: ensure `gcc` is the currently selected flavor.
2711+
if unstable_gcc_lld && LinkerFlavor::Gcc != flavor {
2712+
sess.fatal("`-Zgcc-ld` is used even though the linker flavor is not `gcc`");
27132713
}
27142714

27152715
let cg = &sess.opts.cg;
27162716

27172717
// The `-C linker-flavor` CLI flag can optionally enrich linker-flavors. Check whether that's
27182718
// applicable, and emit errors if sanity checks fail. There's currently only one enrichment:
2719-
// adding an argument to the `cc` invocation to use the `use_ld` given linker.
2720-
let use_ld = match &cg.linker_flavor {
2719+
// adding an argument to the `cc` invocation to use `lld`.
2720+
match &cg.linker_flavor {
27212721
Some(LinkerFlavorCli::Gcc { use_ld }) => {
27222722
// Ensure `gcc` is the currently selected flavor.
27232723
//
@@ -2733,79 +2733,71 @@ fn handle_cli_linker_flavors(
27332733
);
27342734
}
27352735

2736-
use_ld
2736+
if *use_ld != LdImpl::Lld {
2737+
// We're not in a situation needing enrichments.
2738+
return;
2739+
}
27372740
}
27382741

2739-
// Note: exhaustive match arm here, to avoid fallthroughs if new linker-flavor enrichments
2740-
// are added in the future.
27412742
Some(LinkerFlavorCli::WellKnown(_)) | None => {
2742-
if unstable_gcc_lld {
2743-
"lld"
2744-
} else {
2743+
if !unstable_gcc_lld {
27452744
// We're not in a situation needing enrichments.
27462745
return;
27472746
}
27482747
}
2749-
};
2748+
}
27502749

2751-
// From now on in this function, we handle the `gcc` linker-flavor enrichment.
2750+
// From now on in this function, we handle the `gcc` linker-flavor enrichment to use `lld`.
27522751

2753-
// Except for `lld`, the given linker executable will be passed straight to `-fuse-ld`.
2754-
if use_ld == "lld" {
2755-
// Start by checking if we're in the context of a known issue that users might hit when
2756-
// using `lld`:
2757-
//
2758-
// 1. when requesting self-contained CRT linking (or on a target that does it
2759-
// automatically), and coverage/profile generation: point at #79555 "Coverage is not
2760-
// generated when using rust-lld as linker"
2761-
let instrument_coverage = cg.instrument_coverage.is_some()
2762-
&& cg.instrument_coverage != Some(InstrumentCoverage::Off);
2763-
let generate_profile = cg.profile_generate.enabled();
2764-
if crt_objects_fallback && (instrument_coverage || generate_profile) {
2765-
sess.warn(
2766-
"Using `lld`, self-contained linking, and coverage or profile generation has known \
2767-
issues. See issue #79555 for more details, at \
2768-
https://github.com/rust-lang/rust/issues/79555",
2769-
);
2770-
}
2752+
// Start by checking if we're in the context of a known issue that users might hit when
2753+
// using `lld`:
2754+
//
2755+
// 1. when requesting self-contained CRT linking (or on a target that does it
2756+
// automatically), and coverage/profile generation: point at #79555 "Coverage is not
2757+
// generated when using rust-lld as linker"
2758+
let instrument_coverage =
2759+
cg.instrument_coverage.is_some() && cg.instrument_coverage != Some(InstrumentCoverage::Off);
2760+
let generate_profile = cg.profile_generate.enabled();
2761+
if crt_objects_fallback && (instrument_coverage || generate_profile) {
2762+
sess.warn(
2763+
"Using `lld`, self-contained linking, and coverage or profile generation has known \
2764+
issues. See issue #79555 for more details, at \
2765+
https://github.com/rust-lang/rust/issues/79555",
2766+
);
2767+
}
27712768

2772-
// 2. Maybe point at https://github.com/flamegraph-rs/flamegraph/pull/157 or the
2773-
// corresponding rust/LLVM issue when/if it's tracked, depending on whether we use the
2774-
// workaround argument `--no-rosegment` by default when invoking `lld`.
2775-
//
2776-
// 3. If in the future, other linker flavors and targets are eligible to a `rust-lld`
2777-
// enrichment, maybe also point at target-specific issues like:
2778-
// - MSVC + ThinLTO blocker https://github.com/rust-lang/rust/issues/81408
2779-
// - the "lld on MSVC" tracking issue https://github.com/rust-lang/rust/issues/71520
2780-
// containing a list of blocking issues
2781-
2782-
// Now, handle `rust-lld`. If the `-Zgcc-ld=lld` flag was provided, we use `rust-lld`, the
2783-
// rustup-distributed version of `lld` (when applicable, i.e. not in distro-builds) by:
2784-
// - checking the `lld-wrapper`s exist in the sysroot
2785-
// - adding their folder as a search path, or requesting to use a wrapper directly
2786-
if unstable_gcc_lld {
2787-
// A `gcc-ld` folder (containing the `lld-wrapper`s that will run `rust-lld`) is present in
2788-
// the sysroot's target-specific tool binaries folder.
2789-
let tools_path = sess.get_tools_search_paths(false);
2790-
let gcc_ld_dir = tools_path
2791-
.into_iter()
2792-
.map(|p| p.join("gcc-ld"))
2793-
.find(|p| p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists())
2794-
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2795-
2796-
cmd.arg({
2797-
let mut arg = OsString::from("-B");
2798-
arg.push(gcc_ld_dir);
2799-
arg
2800-
});
2801-
cmd.arg(format!("-Wl,-rustc-lld-flavor={}", sess.target.lld_flavor.as_str()));
2802-
} else {
2803-
// We were asked to use `lld` but not `rust-lld`.
2804-
cmd.arg("-fuse-ld=lld");
2805-
}
2769+
// 2. Maybe point at https://github.com/flamegraph-rs/flamegraph/pull/157 or the
2770+
// corresponding rust/LLVM issue when/if it's tracked, depending on whether we use the
2771+
// workaround argument `--no-rosegment` by default when invoking `lld`.
2772+
//
2773+
// 3. If in the future, other linker flavors and targets are eligible to a `rust-lld`
2774+
// enrichment, maybe also point at target-specific issues like:
2775+
// - MSVC + ThinLTO blocker https://github.com/rust-lang/rust/issues/81408
2776+
// - the "lld on MSVC" tracking issue https://github.com/rust-lang/rust/issues/71520
2777+
// containing a list of blocking issues
2778+
2779+
// Now, handle `rust-lld`. If the `-Zgcc-ld=lld` flag was provided, we use `rust-lld`, the
2780+
// rustup-distributed version of `lld` (when applicable, i.e. not in distro-builds) by:
2781+
// - checking the `lld-wrapper`s exist in the sysroot
2782+
// - adding their folder as a search path, or requesting to use a wrapper directly
2783+
if unstable_gcc_lld {
2784+
// A `gcc-ld` folder (containing the `lld-wrapper`s that will run `rust-lld`) is present in
2785+
// the sysroot's target-specific tool binaries folder.
2786+
let tools_path = sess.get_tools_search_paths(false);
2787+
let gcc_ld_dir = tools_path
2788+
.into_iter()
2789+
.map(|p| p.join("gcc-ld"))
2790+
.find(|p| p.join(if sess.host.is_like_windows { "ld.exe" } else { "ld" }).exists())
2791+
.unwrap_or_else(|| sess.fatal("rust-lld (as ld) not found"));
2792+
2793+
cmd.arg({
2794+
let mut arg = OsString::from("-B");
2795+
arg.push(gcc_ld_dir);
2796+
arg
2797+
});
2798+
cmd.arg(format!("-Wl,-rustc-lld-flavor={}", sess.target.lld_flavor.as_str()));
28062799
} else {
2807-
// Otherwise, we were just asked to use a linker executable, and it's expected that `cc`
2808-
// will find it on the $PATH.
2809-
cmd.arg(format!("-fuse-ld={}", use_ld));
2800+
// Otherwise, we were asked to use `lld` but not `rust-lld`.
2801+
cmd.arg("-fuse-ld=lld");
28102802
}
28112803
}

compiler/rustc_session/src/config.rs

+8-24
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ impl LinkerPluginLto {
208208
/// `to_flavor` method, and never the enrichment variants.
209209
///
210210
/// Currently used to represent finer-grained uses of `gcc`, to improve ease-of-use for wrapping a
211-
/// given linker like `lld`, `gold` or `mold`.
212-
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
211+
/// given linker (currently: only `lld`).
212+
#[derive(Clone, Debug, PartialEq)]
213213
pub enum LinkerFlavorCli {
214214
/// When the CLI option is one of the well-known linker-flavors that don't need special
215215
/// handling.
216216
WellKnown(LinkerFlavor),
217217

218-
/// Enrichments to the `LinkerFlavor::Gcc` flavor, to specify the linker via `-fuse-ld`
219-
Gcc { use_ld: String },
218+
/// Enrichments to the `LinkerFlavor::Gcc` flavor, to specify the linker via `-fuse-ld`.
219+
Gcc { use_ld: LdImpl },
220220
}
221221

222222
impl LinkerFlavorCli {
@@ -241,17 +241,11 @@ impl FromStr for LinkerFlavorCli {
241241

242242
// Otherwise, it should be the enrichments to the gcc/cc flavor: wrapping a given linker
243243
// separated by a colon like `gcc:lld`.
244-
let parts: Vec<_> = s.split("gcc:").collect();
245-
if parts.len() != 2 {
244+
if s != "gcc:lld" {
246245
return Err(());
247246
}
248247

249-
let wrapped_linker = parts[1];
250-
if !wrapped_linker.is_empty() {
251-
Ok(LinkerFlavorCli::Gcc { use_ld: wrapped_linker.to_string() })
252-
} else {
253-
Err(())
254-
}
248+
Ok(LinkerFlavorCli::Gcc { use_ld: LdImpl::Lld })
255249
}
256250
}
257251

@@ -2442,26 +2436,16 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
24422436
);
24432437
}
24442438

2445-
if let Some(LinkerFlavorCli::Gcc { use_ld }) = &cg.linker_flavor {
2439+
if let Some(LinkerFlavorCli::Gcc { .. }) = &cg.linker_flavor {
24462440
// For testing purposes, until we have more feedback about these options: ensure `-Z
24472441
// unstable-options` is enabled when using the `gcc` linker flavor enrichments.
24482442
if !unstable_opts.unstable_options {
24492443
early_error(
24502444
error_format,
2451-
"the `gcc:*` linker flavor is unstable, the `-Z unstable-options` \
2445+
"the `gcc:lld` linker flavor is unstable, the `-Z unstable-options` \
24522446
flag must also be passed to use it",
24532447
);
24542448
}
2455-
2456-
// Until the unstable flag is removed, ensure `-Zgcc-ld=lld` and `-Clinker-flavor=gcc:lld`
2457-
// have a matching linker choice.
2458-
if use_ld != "lld" && unstable_opts.gcc_ld == Some(LdImpl::Lld) {
2459-
early_error(
2460-
error_format,
2461-
"`-Zgcc-ld=lld` and `-Clinker-flavor` differ in their \
2462-
linker choice. The latter should be `-Clinker-flavor=gcc:lld`",
2463-
);
2464-
}
24652449
}
24662450

24672451
let prints = collect_print_requests(&mut cg, &mut unstable_opts, matches, error_format);

compiler/rustc_session/src/options.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ mod desc {
384384
pub const parse_cfprotection: &str = "`none`|`no`|`n` (default), `branch`, `return`, or `full`|`yes`|`y` (equivalent to `branch` and `return`)";
385385
pub const parse_strip: &str = "either `none`, `debuginfo`, or `symbols`";
386386
pub const parse_linker_flavor: &str = "one of: `em`, `gcc`, `l4-bender`, `ld`, `msvc`, \
387-
`ptx-linker`, `bpf-linker`, `wasm-ld`, `ld64.lld`, `ld.lld`, `lld-link`, \
388-
or a `gcc:`-prefixed linker to use with the gcc flavor, like `gcc:lld` or `gcc:gold,`";
387+
`ptx-linker`, `bpf-linker`, `wasm-ld`, `ld64.lld`, `ld.lld`, `lld-link`, or `gcc:lld`";
389388
pub const parse_optimization_fuel: &str = "crate=integer";
390389
pub const parse_mir_spanview: &str = "`statement` (default), `terminator`, or `block`";
391390
pub const parse_instrument_coverage: &str =
@@ -1618,7 +1617,7 @@ pub enum WasiExecModel {
16181617
Reactor,
16191618
}
16201619

1621-
#[derive(Clone, Copy, Hash, PartialEq)]
1620+
#[derive(Clone, Copy, Hash, PartialEq, Debug)]
16221621
pub enum LdImpl {
16231622
Lld,
16241623
}

compiler/rustc_session/src/tests.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,18 @@ pub fn parse_well_known_linker_flavor() {
3232
assert_eq!(LinkerFlavorCli::from_str("unknown_linker"), Err(()));
3333
}
3434

35-
/// Enrichments can currently allow for the `gcc` flavor to specify for a given linker to be
36-
/// used, much like you'd use `-fuse-ld` as a link arg. When using `-C
37-
/// linker-flavor=gcc:$linker`, the `$linker` will be passed directly to `cc`.
35+
/// Enrichments can currently allow the `gcc` flavor to use `lld`, much like you'd use `-fuse-ld` as
36+
/// a link arg.
3837
#[test]
3938
pub fn parse_gcc_enrichment_linker_flavor() {
4039
assert_eq!(
4140
LinkerFlavorCli::from_str("gcc:lld"),
42-
Ok(LinkerFlavorCli::Gcc { use_ld: "lld".to_string() })
43-
);
44-
assert_eq!(
45-
LinkerFlavorCli::from_str("gcc:gold"),
46-
Ok(LinkerFlavorCli::Gcc { use_ld: "gold".to_string() })
41+
Ok(LinkerFlavorCli::Gcc { use_ld: LdImpl::Lld })
4742
);
4843

44+
// Only `gcc:lld` is supported for now
45+
assert_eq!(LinkerFlavorCli::from_str("gcc:gold"), Err(()));
46+
4947
// No linker actually mentioned
5048
assert_eq!(LinkerFlavorCli::from_str("gcc:"), Err(()));
5149

src/test/run-make/gcc-linker-flavor/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Ensure that the enriched `gcc` linker-flavor passes the requested linker to `cc`
44

55
-include ../../run-make-fulldeps/tools.mk
6-
RUSTC_FLAGS = -C linker-flavor=gcc:my_wrapped_linker -Z unstable-options --print link-args
6+
RUSTC_FLAGS = -C linker-flavor=gcc:lld -Z unstable-options --print link-args
77

88
all:
9-
$(RUSTC) $(RUSTC_FLAGS) empty.rs | $(CGREP) "fuse-ld=my_wrapped_linker"
9+
$(RUSTC) $(RUSTC_FLAGS) empty.rs | $(CGREP) "fuse-ld=lld"
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Test ensuring that the enriched `gcc` linker flavor requesting an arbitrary linker (`-C
2-
// linker-flavor=gcc:$linker`) is passed to `cc` as `-fuse-ld`
1+
// Test ensuring that the enriched `gcc` linker flavor requesting `lld` (`-C
2+
// linker-flavor=gcc:lld`) is passed to `cc` as `-fuse-ld=lld`
33

44
fn main() {}

src/test/ui/linkers/gcc_ld_mismatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// check-fail
2-
// compile-flags: -Zgcc-ld=lld -Clinker-flavor=gcc:not_lld -Zunstable-options
1+
// build-fail
2+
// compile-flags: -Zgcc-ld=lld -Clinker-flavor=em -Zunstable-options
33

44
// Test ensuring that until the unstable flag is removed (if ever), if both the linker-flavor and
55
// `gcc-ld` flags are used, they ask for the same linker.
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
error: `-Zgcc-ld=lld` and `-Clinker-flavor` differ in their linker choice. The latter should be `-Clinker-flavor=gcc:lld`
1+
error: `-Zgcc-ld` is used even though the linker flavor is not `gcc`
2+
3+
error: aborting due to previous error
24

src/test/ui/linkers/unstable_linker_flavor.lld.stderr

-2
This file was deleted.

src/test/ui/linkers/unstable_linker_flavor.other.stderr

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// check-fail
2-
// revisions: lld other
3-
// [lld] compile-flags: -C linker-flavor=gcc:lld
4-
// [other] compile-flags: -C linker-flavor=gcc:other
2+
// compile-flags: -C linker-flavor=gcc:lld
53

6-
// Test ensuring that the unstable `gcc:*` values of the stable `-C linker-flavor` flag require
4+
// Test ensuring that the unstable `gcc:lld` value of the stable `-C linker-flavor` flag requires
75
// using `-Z unstable options`
86

97
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `gcc:lld` linker flavor is unstable, the `-Z unstable-options` flag must also be passed to use it
2+

0 commit comments

Comments
 (0)