Skip to content

Commit b20e3ff

Browse files
committed
Auto merge of #86911 - bjorn3:crate_info_refactor, r=petrochenkov
Refactor linker code This merges `LinkerInfo` into `CrateInfo` as there is no reason to keep them separate. `LinkerInfo::to_linker` is merged into `get_linker` as both have different logic for each linker type and `to_linker` is directly called after `get_linker`. Also contains a couple of small cleanups. See the individual commits for all changes.
2 parents 8853999 + 25e45ba commit b20e3ff

File tree

10 files changed

+166
-201
lines changed

10 files changed

+166
-201
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::path::PathBuf;
55

66
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
7-
use rustc_codegen_ssa::back::linker::LinkerInfo;
87
use rustc_codegen_ssa::{CodegenResults, CompiledModule, CrateInfo, ModuleKind};
98
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
109
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
@@ -296,8 +295,7 @@ pub(crate) fn run_aot(
296295
allocator_module,
297296
metadata_module,
298297
metadata,
299-
linker_info: LinkerInfo::new(tcx, crate::target_triple(tcx.sess).to_string()),
300-
crate_info: CrateInfo::new(tcx),
298+
crate_info: CrateInfo::new(tcx, crate::target_triple(tcx.sess).to_string()),
301299
},
302300
work_products,
303301
))

compiler/rustc_codegen_cranelift/src/driver/jit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
178178

179179
let mut dylib_paths = Vec::new();
180180

181-
let crate_info = CrateInfo::new(tcx);
181+
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
182182
let formats = tcx.dependency_formats(());
183183
let data = &formats
184184
.iter()

compiler/rustc_codegen_cranelift/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ impl CodegenBackend for CraneliftCodegenBackend {
219219
sess,
220220
&codegen_results,
221221
outputs,
222-
&codegen_results.crate_info.local_crate_name.as_str(),
223222
);
224223

225224
Ok(())

compiler/rustc_codegen_llvm/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,7 @@ impl CodegenBackend for LlvmCodegenBackend {
292292

293293
// Run the linker on any artifacts that resulted from the LLVM run.
294294
// This should produce either a finished executable or library.
295-
link_binary::<LlvmArchiveBuilder<'_>>(
296-
sess,
297-
&codegen_results,
298-
outputs,
299-
&codegen_results.crate_info.local_crate_name.as_str(),
300-
);
295+
link_binary::<LlvmArchiveBuilder<'_>>(sess, &codegen_results, outputs);
301296

302297
Ok(())
303298
}

compiler/rustc_codegen_ssa/src/back/link.rs

+20-101
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
5454
sess: &'a Session,
5555
codegen_results: &CodegenResults,
5656
outputs: &OutputFilenames,
57-
crate_name: &str,
5857
) {
5958
let _timer = sess.timer("link_binary");
6059
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
@@ -87,7 +86,12 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
8786
.tempdir()
8887
.unwrap_or_else(|err| sess.fatal(&format!("couldn't create a temp dir: {}", err)));
8988
let path = MaybeTempDir::new(tmpdir, sess.opts.cg.save_temps);
90-
let out_filename = out_filename(sess, crate_type, outputs, crate_name);
89+
let out_filename = out_filename(
90+
sess,
91+
crate_type,
92+
outputs,
93+
&codegen_results.crate_info.local_crate_name.as_str(),
94+
);
9195
match crate_type {
9296
CrateType::Rlib => {
9397
let _timer = sess.timer("link_rlib");
@@ -143,97 +147,6 @@ pub fn link_binary<'a, B: ArchiveBuilder<'a>>(
143147
});
144148
}
145149

146-
// The third parameter is for env vars, used on windows to set up the
147-
// path for MSVC to find its DLLs, and gcc to find its bundled
148-
// toolchain
149-
fn get_linker(
150-
sess: &Session,
151-
linker: &Path,
152-
flavor: LinkerFlavor,
153-
self_contained: bool,
154-
) -> Command {
155-
let msvc_tool = windows_registry::find_tool(&sess.opts.target_triple.triple(), "link.exe");
156-
157-
// If our linker looks like a batch script on Windows then to execute this
158-
// we'll need to spawn `cmd` explicitly. This is primarily done to handle
159-
// emscripten where the linker is `emcc.bat` and needs to be spawned as
160-
// `cmd /c emcc.bat ...`.
161-
//
162-
// This worked historically but is needed manually since #42436 (regression
163-
// was tagged as #42791) and some more info can be found on #44443 for
164-
// emscripten itself.
165-
let mut cmd = match linker.to_str() {
166-
Some(linker) if cfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker),
167-
_ => match flavor {
168-
LinkerFlavor::Lld(f) => Command::lld(linker, f),
169-
LinkerFlavor::Msvc if sess.opts.cg.linker.is_none() && sess.target.linker.is_none() => {
170-
Command::new(msvc_tool.as_ref().map_or(linker, |t| t.path()))
171-
}
172-
_ => Command::new(linker),
173-
},
174-
};
175-
176-
// UWP apps have API restrictions enforced during Store submissions.
177-
// To comply with the Windows App Certification Kit,
178-
// MSVC needs to link with the Store versions of the runtime libraries (vcruntime, msvcrt, etc).
179-
let t = &sess.target;
180-
if (flavor == LinkerFlavor::Msvc || flavor == LinkerFlavor::Lld(LldFlavor::Link))
181-
&& t.vendor == "uwp"
182-
{
183-
if let Some(ref tool) = msvc_tool {
184-
let original_path = tool.path();
185-
if let Some(ref root_lib_path) = original_path.ancestors().nth(4) {
186-
let arch = match t.arch.as_str() {
187-
"x86_64" => Some("x64"),
188-
"x86" => Some("x86"),
189-
"aarch64" => Some("arm64"),
190-
"arm" => Some("arm"),
191-
_ => None,
192-
};
193-
if let Some(ref a) = arch {
194-
// FIXME: Move this to `fn linker_with_args`.
195-
let mut arg = OsString::from("/LIBPATH:");
196-
arg.push(format!("{}\\lib\\{}\\store", root_lib_path.display(), a));
197-
cmd.arg(&arg);
198-
} else {
199-
warn!("arch is not supported");
200-
}
201-
} else {
202-
warn!("MSVC root path lib location not found");
203-
}
204-
} else {
205-
warn!("link.exe not found");
206-
}
207-
}
208-
209-
// The compiler's sysroot often has some bundled tools, so add it to the
210-
// PATH for the child.
211-
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths(self_contained);
212-
let mut msvc_changed_path = false;
213-
if sess.target.is_like_msvc {
214-
if let Some(ref tool) = msvc_tool {
215-
cmd.args(tool.args());
216-
for &(ref k, ref v) in tool.env() {
217-
if k == "PATH" {
218-
new_path.extend(env::split_paths(v));
219-
msvc_changed_path = true;
220-
} else {
221-
cmd.env(k, v);
222-
}
223-
}
224-
}
225-
}
226-
227-
if !msvc_changed_path {
228-
if let Some(path) = env::var_os("PATH") {
229-
new_path.extend(env::split_paths(&path));
230-
}
231-
}
232-
cmd.env("PATH", env::join_paths(new_path).unwrap());
233-
234-
cmd
235-
}
236-
237150
pub fn each_linked_rlib(
238151
info: &CrateInfo,
239152
f: &mut dyn FnMut(CrateNum, &Path),
@@ -1800,11 +1713,13 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
18001713
codegen_results: &CodegenResults,
18011714
) -> Command {
18021715
let crt_objects_fallback = crt_objects_fallback(sess, crate_type);
1803-
let base_cmd = get_linker(sess, path, flavor, crt_objects_fallback);
1804-
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
1805-
// to the linker args construction.
1806-
assert!(base_cmd.get_args().is_empty() || sess.target.vendor == "uwp");
1807-
let cmd = &mut *codegen_results.linker_info.to_linker(base_cmd, &sess, flavor);
1716+
let cmd = &mut *super::linker::get_linker(
1717+
sess,
1718+
path,
1719+
flavor,
1720+
crt_objects_fallback,
1721+
&codegen_results.crate_info.target_cpu,
1722+
);
18081723
let link_output_kind = link_output_kind(sess, crate_type);
18091724

18101725
// ------------ Early order-dependent options ------------
@@ -1814,7 +1729,11 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
18141729
// dynamic library.
18151730
// Must be passed before any libraries to prevent the symbols to export from being thrown away,
18161731
// at least on some platforms (e.g. windows-gnu).
1817-
cmd.export_symbols(tmpdir, crate_type);
1732+
cmd.export_symbols(
1733+
tmpdir,
1734+
crate_type,
1735+
&codegen_results.crate_info.exported_symbols[&crate_type],
1736+
);
18181737

18191738
// Can be used for adding custom CRT objects or overriding order-dependent options above.
18201739
// FIXME: In practice built-in target specs use this for arbitrary order-independent options,
@@ -1986,10 +1905,10 @@ fn add_order_independent_options(
19861905
if flavor == LinkerFlavor::PtxLinker {
19871906
// Provide the linker with fallback to internal `target-cpu`.
19881907
cmd.arg("--fallback-arch");
1989-
cmd.arg(&codegen_results.linker_info.target_cpu);
1908+
cmd.arg(&codegen_results.crate_info.target_cpu);
19901909
} else if flavor == LinkerFlavor::BpfLinker {
19911910
cmd.arg("--cpu");
1992-
cmd.arg(&codegen_results.linker_info.target_cpu);
1911+
cmd.arg(&codegen_results.crate_info.target_cpu);
19931912
cmd.arg("--cpu-features");
19941913
cmd.arg(match &sess.opts.cg.target_feature {
19951914
feat if !feat.is_empty() => feat,

0 commit comments

Comments
 (0)