Skip to content

Commit 87248e1

Browse files
Auto merge of #146421 - mati865:experiments, r=<try>
[nothing to see here yet] Experiments
2 parents ddaf123 + ee49589 commit 87248e1

File tree

13 files changed

+1415
-9
lines changed

13 files changed

+1415
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ members = [
4545
"src/tools/unicode-table-generator",
4646
"src/tools/unstable-book-gen",
4747
"src/tools/wasm-component-ld",
48+
# "src/tools/wild-linker",
4849
"src/tools/x",
4950
# tidy-alphabetical-end
5051
]
@@ -53,6 +54,7 @@ exclude = [
5354
"build",
5455
"compiler/rustc_codegen_cranelift",
5556
"compiler/rustc_codegen_gcc",
57+
"src/tools/wild-linker",
5658
"src/bootstrap",
5759
"tests/rustdoc-gui",
5860
# HACK(eddyb) This hardcodes the fact that our CI uses `/checkout/obj`.
@@ -92,4 +94,3 @@ codegen-units = 1
9294
# If you want to use a crate with local modifications, you can set a path or git dependency here.
9395
# For git dependencies, also add your source to ALLOWED_SOURCES in src/tools/tidy/src/extdeps.rs.
9496
#[patch.crates-io]
95-

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,8 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
13261326
LinkerFlavor::Gnu(Cc::Yes, _)
13271327
| LinkerFlavor::Darwin(Cc::Yes, _)
13281328
| LinkerFlavor::WasmLld(Cc::Yes)
1329-
| LinkerFlavor::Unix(Cc::Yes) => {
1329+
| LinkerFlavor::Unix(Cc::Yes)
1330+
| LinkerFlavor::Wild => {
13301331
if cfg!(any(target_os = "solaris", target_os = "illumos")) {
13311332
// On historical Solaris systems, "cc" may have
13321333
// been Sun Studio, which is not flag-compatible
@@ -1366,6 +1367,8 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
13661367
});
13671368
let flavor = sess.target.linker_flavor.with_linker_hints(stem);
13681369
let flavor = adjust_flavor_to_features(flavor, features);
1370+
let linker =
1371+
if flavor == LinkerFlavor::Wild { PathBuf::from("cc") } else { linker };
13691372
Some((linker, flavor))
13701373
}
13711374
(None, None) => None,
@@ -2482,6 +2485,8 @@ fn add_order_independent_options(
24822485
// Take care of the flavors and CLI options requesting the `lld` linker.
24832486
add_lld_args(cmd, sess, flavor, self_contained_components);
24842487

2488+
add_wild_args(cmd, sess, flavor, self_contained_components);
2489+
24852490
add_apple_link_args(cmd, sess, flavor);
24862491

24872492
let apple_sdk_root = add_apple_sdk(cmd, sess, flavor);
@@ -3403,6 +3408,58 @@ fn add_lld_args(
34033408
}
34043409
}
34053410

3411+
fn add_wild_args(
3412+
cmd: &mut dyn Linker,
3413+
sess: &Session,
3414+
flavor: LinkerFlavor,
3415+
self_contained_components: LinkSelfContainedComponents,
3416+
) {
3417+
// Either Wild or LLD to make it work with CI
3418+
if flavor != LinkerFlavor::Wild || std::env::var_os("BUILDING_RUSTC").is_some() {
3419+
let self_contained_cli = sess.opts.cg.link_self_contained.is_linker_enabled();
3420+
let self_contained_target = self_contained_components.is_linker_enabled();
3421+
3422+
let self_contained_linker = self_contained_cli || self_contained_target;
3423+
if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() {
3424+
let mut linker_path_exists = false;
3425+
for path in sess.get_tools_search_paths(false) {
3426+
let linker_path = path.join("gcc-ld");
3427+
linker_path_exists |= linker_path.exists();
3428+
cmd.cc_arg({
3429+
let mut arg = OsString::from("-B");
3430+
arg.push(linker_path);
3431+
arg
3432+
});
3433+
}
3434+
if !linker_path_exists {
3435+
sess.dcx().emit_fatal(errors::SelfContainedLinkerMissing);
3436+
}
3437+
}
3438+
3439+
if !sess.target.is_like_wasm {
3440+
cmd.cc_arg("-fuse-ld=lld");
3441+
}
3442+
return ();
3443+
}
3444+
3445+
let mut linker_path_exists = false;
3446+
for path in sess.get_tools_search_paths(false) {
3447+
let linker_path = path.join("wild-gcc-ld");
3448+
linker_path_exists |= linker_path.exists();
3449+
cmd.cc_arg({
3450+
let mut arg = OsString::from("-B");
3451+
arg.push(linker_path);
3452+
arg
3453+
});
3454+
// cmd.cc_arg("-Wl,--no-fork");
3455+
}
3456+
if !linker_path_exists {
3457+
// As a sanity check, we emit an error if none of these paths exist: we want
3458+
// self-contained linking and have no linker.
3459+
sess.dcx().emit_fatal(errors::SelfContainedLinkerMissing);
3460+
}
3461+
}
3462+
34063463
// gold has been deprecated with binutils 2.44
34073464
// and is known to behave incorrectly around Rust programs.
34083465
// There have been reports of being unable to bootstrap with gold:

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ pub(crate) fn get_linker<'a>(
161161
LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
162162
LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
163163
LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
164+
LinkerFlavor::Wild => Box::new(GccLinker {
165+
cmd,
166+
sess,
167+
target_cpu,
168+
hinted_static: None,
169+
is_ld: false,
170+
is_gnu: true,
171+
uses_lld: flavor.uses_lld(),
172+
}),
164173
}
165174
}
166175

compiler/rustc_session/src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ mod desc {
862862
pub(crate) const parse_link_self_contained: &str = "one of: `y`, `yes`, `on`, `n`, `no`, `off`, or a list of enabled (`+` prefix) and disabled (`-` prefix) \
863863
components: `crto`, `libc`, `unwind`, `linker`, `sanitizers`, `mingw`";
864864
pub(crate) const parse_linker_features: &str =
865-
"a list of enabled (`+` prefix) and disabled (`-` prefix) features: `lld`";
865+
"a list of enabled (`+` prefix) and disabled (`-` prefix) features: `lld`, `wild`";
866866
pub(crate) const parse_polonius: &str = "either no value or `legacy` (the default), or `next`";
867867
pub(crate) const parse_stack_protector: &str =
868868
"one of (`none` (default), `basic`, `strong`, or `all`)";

compiler/rustc_target/src/spec/mod.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub enum LinkerFlavor {
128128
/// Emscripten Compiler Frontend, a wrapper around `WasmLld(Cc::Yes)` that has a different
129129
/// interface and produces some additional JavaScript output.
130130
EmCc,
131+
// TODO: This needs some design on how to proceed
132+
Wild,
131133
// Below: other linker-like tools with unique interfaces for exotic targets.
132134
/// Linker tool for BPF.
133135
Bpf,
@@ -154,6 +156,7 @@ pub enum LinkerFlavorCli {
154156
Bpf,
155157
Ptx,
156158
Llbc,
159+
Wild,
157160

158161
// Legacy stable values
159162
Gcc,
@@ -179,7 +182,8 @@ impl LinkerFlavorCli {
179182
| LinkerFlavorCli::Ld
180183
| LinkerFlavorCli::Lld(..)
181184
| LinkerFlavorCli::Msvc(Lld::No)
182-
| LinkerFlavorCli::Em => false,
185+
| LinkerFlavorCli::Em
186+
| LinkerFlavorCli::Wild => false,
183187
}
184188
}
185189
}
@@ -211,6 +215,7 @@ impl LinkerFlavor {
211215
LinkerFlavorCli::Bpf => LinkerFlavor::Bpf,
212216
LinkerFlavorCli::Llbc => LinkerFlavor::Llbc,
213217
LinkerFlavorCli::Ptx => LinkerFlavor::Ptx,
218+
LinkerFlavorCli::Wild => LinkerFlavor::Wild,
214219

215220
// Below: legacy stable values
216221
LinkerFlavorCli::Gcc => match lld_flavor {
@@ -251,6 +256,7 @@ impl LinkerFlavor {
251256
LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,
252257
LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,
253258
LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,
259+
LinkerFlavor::Wild => LinkerFlavorCli::Wild,
254260
}
255261
}
256262

@@ -266,6 +272,7 @@ impl LinkerFlavor {
266272
LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,
267273
LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,
268274
LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,
275+
LinkerFlavor::Wild => LinkerFlavorCli::Wild,
269276
}
270277
}
271278

@@ -280,6 +287,7 @@ impl LinkerFlavor {
280287
LinkerFlavorCli::EmCc => (Some(Cc::Yes), Some(Lld::Yes)),
281288
LinkerFlavorCli::Bpf | LinkerFlavorCli::Ptx => (None, None),
282289
LinkerFlavorCli::Llbc => (None, None),
290+
LinkerFlavorCli::Wild => (None, None),
283291

284292
// Below: legacy stable values
285293
LinkerFlavorCli::Gcc => (Some(Cc::Yes), None),
@@ -298,6 +306,8 @@ impl LinkerFlavor {
298306

299307
if stem == "llvm-bitcode-linker" {
300308
Ok(Self::Llbc)
309+
} else if stem == "wild" {
310+
Ok(Self::Wild)
301311
} else if stem == "emcc" // GCC/Clang can have an optional target prefix.
302312
|| stem == "gcc"
303313
|| stem.ends_with("-gcc")
@@ -335,7 +345,11 @@ impl LinkerFlavor {
335345
LinkerFlavor::WasmLld(cc) => LinkerFlavor::WasmLld(cc_hint.unwrap_or(cc)),
336346
LinkerFlavor::Unix(cc) => LinkerFlavor::Unix(cc_hint.unwrap_or(cc)),
337347
LinkerFlavor::Msvc(lld) => LinkerFlavor::Msvc(lld_hint.unwrap_or(lld)),
338-
LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Llbc | LinkerFlavor::Ptx => self,
348+
LinkerFlavor::EmCc
349+
| LinkerFlavor::Bpf
350+
| LinkerFlavor::Llbc
351+
| LinkerFlavor::Ptx
352+
| LinkerFlavor::Wild => self,
339353
}
340354
}
341355

@@ -363,7 +377,8 @@ impl LinkerFlavor {
363377
| (LinkerFlavor::EmCc, LinkerFlavorCli::EmCc)
364378
| (LinkerFlavor::Bpf, LinkerFlavorCli::Bpf)
365379
| (LinkerFlavor::Llbc, LinkerFlavorCli::Llbc)
366-
| (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx) => return true,
380+
| (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx)
381+
| (LinkerFlavor::Wild, LinkerFlavorCli::Wild) => return true,
367382
// 2. The linker flavor is independent of target and compatible
368383
(LinkerFlavor::Ptx, LinkerFlavorCli::Llbc) => return true,
369384
_ => {}
@@ -393,6 +408,7 @@ impl LinkerFlavor {
393408
LinkerFlavor::Darwin(..) => LldFlavor::Ld64,
394409
LinkerFlavor::WasmLld(..) => LldFlavor::Wasm,
395410
LinkerFlavor::Msvc(..) => LldFlavor::Link,
411+
LinkerFlavor::Wild => todo!(),
396412
}
397413
}
398414

@@ -415,7 +431,8 @@ impl LinkerFlavor {
415431
| LinkerFlavor::Unix(_)
416432
| LinkerFlavor::Bpf
417433
| LinkerFlavor::Llbc
418-
| LinkerFlavor::Ptx => false,
434+
| LinkerFlavor::Ptx
435+
| LinkerFlavor::Wild => false,
419436
}
420437
}
421438

@@ -427,7 +444,8 @@ impl LinkerFlavor {
427444
| LinkerFlavor::Darwin(Cc::Yes, _)
428445
| LinkerFlavor::WasmLld(Cc::Yes)
429446
| LinkerFlavor::Unix(Cc::Yes)
430-
| LinkerFlavor::EmCc => true,
447+
| LinkerFlavor::EmCc
448+
| LinkerFlavor::Wild => true,
431449
LinkerFlavor::Gnu(..)
432450
| LinkerFlavor::Darwin(..)
433451
| LinkerFlavor::WasmLld(_)
@@ -512,6 +530,7 @@ linker_flavor_cli_impls! {
512530
(LinkerFlavorCli::Bpf) "bpf"
513531
(LinkerFlavorCli::Llbc) "llbc"
514532
(LinkerFlavorCli::Ptx) "ptx"
533+
(LinkerFlavorCli::Wild) "wild"
515534

516535
// Legacy stable flavors
517536
(LinkerFlavorCli::Gcc) "gcc"
@@ -2380,6 +2399,7 @@ fn add_link_args_iter(
23802399
assert_eq!(lld, Lld::No);
23812400
insert(LinkerFlavor::Msvc(Lld::Yes));
23822401
}
2402+
LinkerFlavor::Wild => insert(LinkerFlavor::Wild),
23832403
LinkerFlavor::WasmLld(..)
23842404
| LinkerFlavor::Unix(..)
23852405
| LinkerFlavor::EmCc
@@ -2770,6 +2790,7 @@ impl Target {
27702790
| LinkerFlavor::Llbc => {
27712791
check_eq!(flavor, self.linker_flavor, "mixing different linker flavors")
27722792
}
2793+
LinkerFlavor::Wild => todo!(),
27732794
}
27742795

27752796
// Check that link args for cc and non-cc versions of flavors are consistent.

compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_gnu.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub(crate) fn target() -> Target {
2727
base.link_self_contained = crate::spec::LinkSelfContainedDefault::with_linker();
2828
}
2929

30+
base.linker_flavor = LinkerFlavor::Wild;
31+
3032
Target {
3133
llvm_target: "x86_64-unknown-linux-gnu".into(),
3234
metadata: TargetMetadata {

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use serde_derive::Deserialize;
2020
use tracing::span;
2121

2222
use crate::core::build_steps::gcc::{Gcc, GccOutput, add_cg_gcc_cargo_flags};
23-
use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld_artifacts};
23+
use crate::core::build_steps::tool::{
24+
RustcPrivateCompilers, SourceType, copy_lld_artifacts, copy_wild_artifacts,
25+
};
2426
use crate::core::build_steps::{dist, llvm};
2527
use crate::core::builder;
2628
use crate::core::builder::{
@@ -2315,6 +2317,15 @@ impl Step for Assemble {
23152317
copy_lld_artifacts(builder, lld_wrapper, target_compiler);
23162318
}
23172319

2320+
if builder.host_target.triple == "x86_64-unknown-linux-gnu" {
2321+
let wild_wrapper =
2322+
builder.ensure(crate::core::build_steps::tool::WildLinker::for_use_by_compiler(
2323+
builder,
2324+
target_compiler,
2325+
));
2326+
copy_wild_artifacts(builder, wild_wrapper, target_compiler);
2327+
}
2328+
23182329
if builder.config.llvm_enabled(target_compiler.host) && builder.config.llvm_tools_enabled {
23192330
debug!(
23202331
"llvm and llvm tools enabled; copying `llvm-objcopy` as `rust-objcopy` to \

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,33 @@ impl Step for Rustc {
552552
}
553553
}
554554

555+
if builder.host_target.triple == "x86_64-unknown-linux-gnu" {
556+
let src_dir = builder.sysroot_target_bindir(target_compiler, target);
557+
let rust_wild = exe("rust-wild", target_compiler.host);
558+
builder.copy_link(
559+
&src_dir.join(&rust_wild),
560+
&dst_dir.join(&rust_wild),
561+
FileType::Executable,
562+
);
563+
let self_contained_wild_src_dir = src_dir.join("wild-gcc-ld");
564+
let self_contained_wild_dst_dir = dst_dir.join("wild-gcc-ld");
565+
t!(fs::create_dir(&self_contained_wild_dst_dir));
566+
let wild_name = "wild";
567+
let exe_name = exe(wild_name, target_compiler.host);
568+
builder.copy_link(
569+
&self_contained_wild_src_dir.join(&exe_name),
570+
&self_contained_wild_dst_dir.join(&exe_name),
571+
FileType::Executable,
572+
);
573+
// Pretend Wild is LD so the compiler can pick it up
574+
let exe_name = exe("ld", target_compiler.host);
575+
builder.copy_link(
576+
&self_contained_wild_src_dir.join(&exe_name),
577+
&self_contained_wild_dst_dir.join(&exe_name),
578+
FileType::Executable,
579+
);
580+
}
581+
555582
if builder.config.llvm_enabled(target_compiler.host)
556583
&& builder.config.llvm_tools_enabled
557584
{

0 commit comments

Comments
 (0)