Skip to content

Commit 848c2b7

Browse files
authored
Rollup merge of rust-lang#55225 - bjorn3:rustc_link, r=cramertj
Move cg_llvm::back::linker to cg_utils This allows it to be reused by alternative codegen backends.
2 parents 942a162 + 655f9d8 commit 848c2b7

File tree

11 files changed

+82
-67
lines changed

11 files changed

+82
-67
lines changed

src/Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,11 +2135,13 @@ dependencies = [
21352135
"flate2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
21362136
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
21372137
"rustc 0.0.0",
2138+
"rustc_allocator 0.0.0",
21382139
"rustc_data_structures 0.0.0",
21392140
"rustc_incremental 0.0.0",
21402141
"rustc_metadata_utils 0.0.0",
21412142
"rustc_mir 0.0.0",
21422143
"rustc_target 0.0.0",
2144+
"serialize 0.0.0",
21432145
"syntax 0.0.0",
21442146
"syntax_pos 0.0.0",
21452147
]

src/librustc_codegen_llvm/back/archive.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ enum Addition {
5252
},
5353
}
5454

55-
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
56-
-> PathBuf {
57-
// On Windows, static libraries sometimes show up as libfoo.a and other
58-
// times show up as foo.lib
59-
let oslibname = format!("{}{}{}",
60-
sess.target.target.options.staticlib_prefix,
61-
name,
62-
sess.target.target.options.staticlib_suffix);
63-
let unixlibname = format!("lib{}.a", name);
64-
65-
for path in search_paths {
66-
debug!("looking for {} inside {:?}", name, path);
67-
let test = path.join(&oslibname);
68-
if test.exists() { return test }
69-
if oslibname != unixlibname {
70-
let test = path.join(&unixlibname);
71-
if test.exists() { return test }
72-
}
73-
}
74-
sess.fatal(&format!("could not find native static library `{}`, \
75-
perhaps an -L flag is missing?", name));
76-
}
7755

7856
fn is_relevant_child(c: &Child) -> bool {
7957
match c.name() {
@@ -128,7 +106,7 @@ impl<'a> ArchiveBuilder<'a> {
128106
/// Adds all of the contents of a native library to this archive. This will
129107
/// search in the relevant locations for a library named `name`.
130108
pub fn add_native_library(&mut self, name: &str) {
131-
let location = find_library(name, &self.config.lib_search_paths,
109+
let location = ::rustc_codegen_utils::find_library(name, &self.config.lib_search_paths,
132110
self.config.sess);
133111
self.add_archive(&location, |_| false).unwrap_or_else(|e| {
134112
self.config.sess.fatal(&format!("failed to add native library {}: {}",

src/librustc_codegen_llvm/back/link.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use back::wasm;
1212
use cc::windows_registry;
1313
use super::archive::{ArchiveBuilder, ArchiveConfig};
1414
use super::bytecode::RLIB_BYTECODE_EXTENSION;
15-
use super::linker::Linker;
16-
use super::command::Command;
1715
use super::rpath::RPathConfig;
1816
use super::rpath;
1917
use metadata::METADATA_FILENAME;
@@ -31,6 +29,8 @@ use rustc::hir::def_id::CrateNum;
3129
use tempfile::{Builder as TempFileBuilder, TempDir};
3230
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
3331
use rustc_data_structures::fx::FxHashSet;
32+
use rustc_codegen_utils::linker::Linker;
33+
use rustc_codegen_utils::command::Command;
3434
use context::get_reloc_model;
3535
use llvm;
3636

@@ -701,7 +701,8 @@ fn link_natively(sess: &Session,
701701
}
702702

703703
{
704-
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor);
704+
let target_cpu = ::llvm_util::target_cpu(sess);
705+
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor, target_cpu);
705706
link_args(&mut *linker, flavor, sess, crate_type, tmpdir,
706707
out_filename, codegen_results);
707708
cmd = linker.finalize();

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use back::bytecode::{DecodedBytecode, RLIB_BYTECODE_EXTENSION};
12-
use back::symbol_export;
1312
use back::write::{ModuleConfig, with_llvm_pmb, CodegenContext};
1413
use back::write::{self, DiagnosticHandlers, pre_lto_bitcode_filename};
1514
use errors::{FatalError, Handler};
@@ -24,6 +23,7 @@ use rustc::middle::exported_symbols::SymbolExportLevel;
2423
use rustc::session::config::{self, Lto};
2524
use rustc::util::common::time_ext;
2625
use rustc_data_structures::fx::FxHashMap;
26+
use rustc_codegen_utils::symbol_export;
2727
use time_graph::Timeline;
2828
use {ModuleCodegen, ModuleLlvm, ModuleKind};
2929

src/librustc_codegen_llvm/back/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ use attributes;
1212
use back::bytecode::{self, RLIB_BYTECODE_EXTENSION};
1313
use back::lto::{self, ModuleBuffer, ThinBuffer, SerializedModule};
1414
use back::link::{self, get_linker, remove};
15-
use back::command::Command;
16-
use back::linker::LinkerInfo;
17-
use back::symbol_export::ExportedSymbols;
1815
use base;
1916
use consts;
2017
use memmap;
@@ -38,6 +35,9 @@ use rustc::util::common::{time_ext, time_depth, set_time_depth, print_time_passe
3835
use rustc_fs_util::{path2cstr, link_or_copy};
3936
use rustc_data_structures::small_c_str::SmallCStr;
4037
use rustc_data_structures::svh::Svh;
38+
use rustc_codegen_utils::command::Command;
39+
use rustc_codegen_utils::linker::LinkerInfo;
40+
use rustc_codegen_utils::symbol_export::ExportedSymbols;
4141
use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
4242
use errors::emitter::{Emitter};
4343
use syntax::attr;

src/librustc_codegen_llvm/lib.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ use back::bytecode::RLIB_BYTECODE_EXTENSION;
7171

7272
pub use llvm_util::target_features;
7373
use std::any::Any;
74-
use std::path::{PathBuf};
7574
use std::sync::mpsc;
7675
use rustc_data_structures::sync::Lrc;
7776

@@ -87,20 +86,17 @@ use rustc::util::time_graph;
8786
use rustc::util::nodemap::{FxHashSet, FxHashMap};
8887
use rustc::util::profiling::ProfileCategory;
8988
use rustc_mir::monomorphize;
89+
use rustc_codegen_utils::{CompiledModule, ModuleKind};
9090
use rustc_codegen_utils::codegen_backend::CodegenBackend;
9191
use rustc_data_structures::svh::Svh;
9292

9393
mod diagnostics;
9494

9595
mod back {
96-
pub use rustc_codegen_utils::symbol_names;
9796
mod archive;
9897
pub mod bytecode;
99-
mod command;
100-
pub mod linker;
10198
pub mod link;
10299
pub mod lto;
103-
pub mod symbol_export;
104100
pub mod write;
105101
mod rpath;
106102
pub mod wasm;
@@ -194,14 +190,14 @@ impl CodegenBackend for LlvmCodegenBackend {
194190
}
195191

196192
fn provide(&self, providers: &mut ty::query::Providers) {
197-
back::symbol_names::provide(providers);
198-
back::symbol_export::provide(providers);
193+
rustc_codegen_utils::symbol_export::provide(providers);
194+
rustc_codegen_utils::symbol_names::provide(providers);
199195
base::provide(providers);
200196
attributes::provide(providers);
201197
}
202198

203199
fn provide_extern(&self, providers: &mut ty::query::Providers) {
204-
back::symbol_export::provide_extern(providers);
200+
rustc_codegen_utils::symbol_export::provide_extern(providers);
205201
base::provide_extern(providers);
206202
attributes::provide_extern(providers);
207203
}
@@ -281,13 +277,6 @@ struct CachedModuleCodegen {
281277
source: WorkProduct,
282278
}
283279

284-
#[derive(Copy, Clone, Debug, PartialEq)]
285-
enum ModuleKind {
286-
Regular,
287-
Metadata,
288-
Allocator,
289-
}
290-
291280
impl ModuleCodegen {
292281
fn into_compiled_module(self,
293282
emit_obj: bool,
@@ -321,15 +310,6 @@ impl ModuleCodegen {
321310
}
322311
}
323312

324-
#[derive(Debug)]
325-
struct CompiledModule {
326-
name: String,
327-
kind: ModuleKind,
328-
object: Option<PathBuf>,
329-
bytecode: Option<PathBuf>,
330-
bytecode_compressed: Option<PathBuf>,
331-
}
332-
333313
struct ModuleLlvm {
334314
llcx: &'static mut llvm::Context,
335315
llmod_raw: *const llvm::Module,
@@ -377,7 +357,7 @@ struct CodegenResults {
377357
crate_hash: Svh,
378358
metadata: rustc::middle::cstore::EncodedMetadata,
379359
windows_subsystem: Option<String>,
380-
linker_info: back::linker::LinkerInfo,
360+
linker_info: rustc_codegen_utils::linker::LinkerInfo,
381361
crate_info: CrateInfo,
382362
}
383363

src/librustc_codegen_utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ test = false
1313
flate2 = "1.0"
1414
log = "0.4"
1515

16+
serialize = { path = "../libserialize" }
1617
syntax = { path = "../libsyntax" }
1718
syntax_pos = { path = "../libsyntax_pos" }
1819
rustc = { path = "../librustc" }
20+
rustc_allocator = { path = "../librustc_allocator" }
1921
rustc_target = { path = "../librustc_target" }
2022
rustc_data_structures = { path = "../librustc_data_structures" }
2123
rustc_mir = { path = "../librustc_mir" }

src/librustc_codegen_utils/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ extern crate flate2;
3030
#[macro_use]
3131
extern crate log;
3232

33+
extern crate serialize;
3334
#[macro_use]
3435
extern crate rustc;
36+
extern crate rustc_allocator;
3537
extern crate rustc_target;
3638
extern crate rustc_mir;
3739
extern crate rustc_incremental;
@@ -40,10 +42,16 @@ extern crate syntax_pos;
4042
#[macro_use] extern crate rustc_data_structures;
4143
extern crate rustc_metadata_utils;
4244

45+
use std::path::PathBuf;
46+
47+
use rustc::session::Session;
4348
use rustc::ty::TyCtxt;
4449

50+
pub mod command;
4551
pub mod link;
52+
pub mod linker;
4653
pub mod codegen_backend;
54+
pub mod symbol_export;
4755
pub mod symbol_names;
4856
pub mod symbol_names_test;
4957

@@ -61,4 +69,43 @@ pub fn check_for_rustc_errors_attr(tcx: TyCtxt) {
6169
}
6270
}
6371

72+
#[derive(Copy, Clone, Debug, PartialEq)]
73+
pub enum ModuleKind {
74+
Regular,
75+
Metadata,
76+
Allocator,
77+
}
78+
79+
#[derive(Debug)]
80+
pub struct CompiledModule {
81+
pub name: String,
82+
pub kind: ModuleKind,
83+
pub object: Option<PathBuf>,
84+
pub bytecode: Option<PathBuf>,
85+
pub bytecode_compressed: Option<PathBuf>,
86+
}
87+
88+
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
89+
-> PathBuf {
90+
// On Windows, static libraries sometimes show up as libfoo.a and other
91+
// times show up as foo.lib
92+
let oslibname = format!("{}{}{}",
93+
sess.target.target.options.staticlib_prefix,
94+
name,
95+
sess.target.target.options.staticlib_suffix);
96+
let unixlibname = format!("lib{}.a", name);
97+
98+
for path in search_paths {
99+
debug!("looking for {} inside {:?}", name, path);
100+
let test = path.join(&oslibname);
101+
if test.exists() { return test }
102+
if oslibname != unixlibname {
103+
let test = path.join(&unixlibname);
104+
if test.exists() { return test }
105+
}
106+
}
107+
sess.fatal(&format!("could not find native static library `{}`, \
108+
perhaps an -L flag is missing?", name));
109+
}
110+
64111
__build_diagnostic_array! { librustc_codegen_utils, DIAGNOSTICS }

src/librustc_codegen_llvm/back/linker.rs renamed to src/librustc_codegen_utils/linker.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use std::io::prelude::*;
1515
use std::io::{self, BufWriter};
1616
use std::path::{Path, PathBuf};
1717

18-
use back::archive;
19-
use back::command::Command;
20-
use back::symbol_export;
18+
use command::Command;
2119
use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
2220
use rustc::middle::dependency_format::Linkage;
2321
use rustc::session::Session;
@@ -26,7 +24,6 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
2624
use rustc::ty::TyCtxt;
2725
use rustc_target::spec::{LinkerFlavor, LldFlavor};
2826
use serialize::{json, Encoder};
29-
use llvm_util;
3027

3128
/// For all the linkers we support, and information they might
3229
/// need out of the shared crate context before we get rid of it.
@@ -43,10 +40,13 @@ impl LinkerInfo {
4340
}
4441
}
4542

46-
pub fn to_linker<'a>(&'a self,
47-
cmd: Command,
48-
sess: &'a Session,
49-
flavor: LinkerFlavor) -> Box<dyn Linker+'a> {
43+
pub fn to_linker<'a>(
44+
&'a self,
45+
cmd: Command,
46+
sess: &'a Session,
47+
flavor: LinkerFlavor,
48+
target_cpu: &'a str,
49+
) -> Box<dyn Linker+'a> {
5050
match flavor {
5151
LinkerFlavor::Lld(LldFlavor::Link) |
5252
LinkerFlavor::Msvc => {
@@ -70,6 +70,7 @@ impl LinkerInfo {
7070
info: self,
7171
hinted_static: false,
7272
is_ld: false,
73+
target_cpu,
7374
}) as Box<dyn Linker>
7475
}
7576

@@ -82,6 +83,7 @@ impl LinkerInfo {
8283
info: self,
8384
hinted_static: false,
8485
is_ld: true,
86+
target_cpu,
8587
}) as Box<dyn Linker>
8688
}
8789

@@ -144,6 +146,7 @@ pub struct GccLinker<'a> {
144146
hinted_static: bool, // Keeps track of the current hinting mode.
145147
// Link as ld
146148
is_ld: bool,
149+
target_cpu: &'a str,
147150
}
148151

149152
impl<'a> GccLinker<'a> {
@@ -204,7 +207,8 @@ impl<'a> GccLinker<'a> {
204207
};
205208

206209
self.linker_arg(&format!("-plugin-opt={}", opt_level));
207-
self.linker_arg(&format!("-plugin-opt=mcpu={}", llvm_util::target_cpu(self.sess)));
210+
let target_cpu = self.target_cpu;
211+
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
208212

209213
match self.sess.lto() {
210214
config::Lto::Thin |
@@ -263,7 +267,7 @@ impl<'a> Linker for GccLinker<'a> {
263267
// -force_load is the macOS equivalent of --whole-archive, but it
264268
// involves passing the full path to the library to link.
265269
self.linker_arg("-force_load");
266-
let lib = archive::find_library(lib, search_path, &self.sess);
270+
let lib = ::find_library(lib, search_path, &self.sess);
267271
self.linker_arg(&lib);
268272
}
269273
}
@@ -898,7 +902,8 @@ impl<'a> Linker for EmLinker<'a> {
898902
fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec<String> {
899903
let mut symbols = Vec::new();
900904

901-
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
905+
let export_threshold =
906+
::symbol_export::crates_export_threshold(&[crate_type]);
902907
for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
903908
if level.is_below_threshold(export_threshold) {
904909
symbols.push(symbol.symbol_name(tcx).to_string());

src/librustc_codegen_llvm/back/symbol_export.rs renamed to src/librustc_codegen_utils/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use rustc_data_structures::sync::Lrc;
1212
use std::sync::Arc;
1313

14-
use monomorphize::Instance;
14+
use rustc::ty::Instance;
1515
use rustc::hir;
1616
use rustc::hir::Node;
1717
use rustc::hir::CodegenFnAttrFlags;

0 commit comments

Comments
 (0)