1
Fork 0

Move cg_llvm:🔙:linker to cg_utils

This commit is contained in:
bjorn3 2018-10-20 14:45:08 +02:00
parent b6e8f9dbdc
commit 942864a000
10 changed files with 80 additions and 67 deletions

View file

@ -52,28 +52,6 @@ enum Addition {
}, },
} }
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
-> PathBuf {
// On Windows, static libraries sometimes show up as libfoo.a and other
// times show up as foo.lib
let oslibname = format!("{}{}{}",
sess.target.target.options.staticlib_prefix,
name,
sess.target.target.options.staticlib_suffix);
let unixlibname = format!("lib{}.a", name);
for path in search_paths {
debug!("looking for {} inside {:?}", name, path);
let test = path.join(&oslibname);
if test.exists() { return test }
if oslibname != unixlibname {
let test = path.join(&unixlibname);
if test.exists() { return test }
}
}
sess.fatal(&format!("could not find native static library `{}`, \
perhaps an -L flag is missing?", name));
}
fn is_relevant_child(c: &Child) -> bool { fn is_relevant_child(c: &Child) -> bool {
match c.name() { match c.name() {
@ -128,7 +106,7 @@ impl<'a> ArchiveBuilder<'a> {
/// Adds all of the contents of a native library to this archive. This will /// Adds all of the contents of a native library to this archive. This will
/// search in the relevant locations for a library named `name`. /// search in the relevant locations for a library named `name`.
pub fn add_native_library(&mut self, name: &str) { pub fn add_native_library(&mut self, name: &str) {
let location = find_library(name, &self.config.lib_search_paths, let location = ::rustc_codegen_utils::find_library(name, &self.config.lib_search_paths,
self.config.sess); self.config.sess);
self.add_archive(&location, |_| false).unwrap_or_else(|e| { self.add_archive(&location, |_| false).unwrap_or_else(|e| {
self.config.sess.fatal(&format!("failed to add native library {}: {}", self.config.sess.fatal(&format!("failed to add native library {}: {}",

View file

@ -12,8 +12,6 @@ use back::wasm;
use cc::windows_registry; use cc::windows_registry;
use super::archive::{ArchiveBuilder, ArchiveConfig}; use super::archive::{ArchiveBuilder, ArchiveConfig};
use super::bytecode::RLIB_BYTECODE_EXTENSION; use super::bytecode::RLIB_BYTECODE_EXTENSION;
use super::linker::Linker;
use super::command::Command;
use super::rpath::RPathConfig; use super::rpath::RPathConfig;
use super::rpath; use super::rpath;
use metadata::METADATA_FILENAME; use metadata::METADATA_FILENAME;
@ -31,6 +29,8 @@ use rustc::hir::def_id::CrateNum;
use tempfile::{Builder as TempFileBuilder, TempDir}; use tempfile::{Builder as TempFileBuilder, TempDir};
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor}; use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_codegen_utils::linker::Linker;
use rustc_codegen_utils::command::Command;
use context::get_reloc_model; use context::get_reloc_model;
use llvm; use llvm;
@ -701,7 +701,8 @@ fn link_natively(sess: &Session,
} }
{ {
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor); let target_cpu = ::llvm_util::target_cpu(sess);
let mut linker = codegen_results.linker_info.to_linker(cmd, &sess, flavor, target_cpu);
link_args(&mut *linker, flavor, sess, crate_type, tmpdir, link_args(&mut *linker, flavor, sess, crate_type, tmpdir,
out_filename, codegen_results); out_filename, codegen_results);
cmd = linker.finalize(); cmd = linker.finalize();

View file

@ -9,7 +9,6 @@
// except according to those terms. // except according to those terms.
use back::bytecode::{DecodedBytecode, RLIB_BYTECODE_EXTENSION}; use back::bytecode::{DecodedBytecode, RLIB_BYTECODE_EXTENSION};
use back::symbol_export;
use back::write::{ModuleConfig, with_llvm_pmb, CodegenContext}; use back::write::{ModuleConfig, with_llvm_pmb, CodegenContext};
use back::write::{self, DiagnosticHandlers, pre_lto_bitcode_filename}; use back::write::{self, DiagnosticHandlers, pre_lto_bitcode_filename};
use errors::{FatalError, Handler}; use errors::{FatalError, Handler};
@ -24,6 +23,7 @@ use rustc::middle::exported_symbols::SymbolExportLevel;
use rustc::session::config::{self, Lto}; use rustc::session::config::{self, Lto};
use rustc::util::common::time_ext; use rustc::util::common::time_ext;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_codegen_utils::symbol_export;
use time_graph::Timeline; use time_graph::Timeline;
use {ModuleCodegen, ModuleLlvm, ModuleKind}; use {ModuleCodegen, ModuleLlvm, ModuleKind};

View file

@ -12,9 +12,6 @@ use attributes;
use back::bytecode::{self, RLIB_BYTECODE_EXTENSION}; use back::bytecode::{self, RLIB_BYTECODE_EXTENSION};
use back::lto::{self, ModuleBuffer, ThinBuffer, SerializedModule}; use back::lto::{self, ModuleBuffer, ThinBuffer, SerializedModule};
use back::link::{self, get_linker, remove}; use back::link::{self, get_linker, remove};
use back::command::Command;
use back::linker::LinkerInfo;
use back::symbol_export::ExportedSymbols;
use base; use base;
use consts; use consts;
use memmap; use memmap;
@ -38,6 +35,9 @@ use rustc::util::common::{time_ext, time_depth, set_time_depth, print_time_passe
use rustc_fs_util::{path2cstr, link_or_copy}; use rustc_fs_util::{path2cstr, link_or_copy};
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_codegen_utils::command::Command;
use rustc_codegen_utils::linker::LinkerInfo;
use rustc_codegen_utils::symbol_export::ExportedSymbols;
use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId}; use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
use errors::emitter::{Emitter}; use errors::emitter::{Emitter};
use syntax::attr; use syntax::attr;

View file

@ -71,7 +71,6 @@ use back::bytecode::RLIB_BYTECODE_EXTENSION;
pub use llvm_util::target_features; pub use llvm_util::target_features;
use std::any::Any; use std::any::Any;
use std::path::{PathBuf};
use std::sync::mpsc; use std::sync::mpsc;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
@ -87,20 +86,17 @@ use rustc::util::time_graph;
use rustc::util::nodemap::{FxHashSet, FxHashMap}; use rustc::util::nodemap::{FxHashSet, FxHashMap};
use rustc::util::profiling::ProfileCategory; use rustc::util::profiling::ProfileCategory;
use rustc_mir::monomorphize; use rustc_mir::monomorphize;
use rustc_codegen_utils::{CompiledModule, ModuleKind};
use rustc_codegen_utils::codegen_backend::CodegenBackend; use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
mod diagnostics; mod diagnostics;
mod back { mod back {
pub use rustc_codegen_utils::symbol_names;
mod archive; mod archive;
pub mod bytecode; pub mod bytecode;
mod command;
pub mod linker;
pub mod link; pub mod link;
pub mod lto; pub mod lto;
pub mod symbol_export;
pub mod write; pub mod write;
mod rpath; mod rpath;
pub mod wasm; pub mod wasm;
@ -194,14 +190,14 @@ impl CodegenBackend for LlvmCodegenBackend {
} }
fn provide(&self, providers: &mut ty::query::Providers) { fn provide(&self, providers: &mut ty::query::Providers) {
back::symbol_names::provide(providers); rustc_codegen_utils::symbol_export::provide(providers);
back::symbol_export::provide(providers); rustc_codegen_utils::symbol_names::provide(providers);
base::provide(providers); base::provide(providers);
attributes::provide(providers); attributes::provide(providers);
} }
fn provide_extern(&self, providers: &mut ty::query::Providers) { fn provide_extern(&self, providers: &mut ty::query::Providers) {
back::symbol_export::provide_extern(providers); rustc_codegen_utils::symbol_export::provide_extern(providers);
base::provide_extern(providers); base::provide_extern(providers);
attributes::provide_extern(providers); attributes::provide_extern(providers);
} }
@ -281,13 +277,6 @@ struct CachedModuleCodegen {
source: WorkProduct, source: WorkProduct,
} }
#[derive(Copy, Clone, Debug, PartialEq)]
enum ModuleKind {
Regular,
Metadata,
Allocator,
}
impl ModuleCodegen { impl ModuleCodegen {
fn into_compiled_module(self, fn into_compiled_module(self,
emit_obj: bool, emit_obj: bool,
@ -321,15 +310,6 @@ impl ModuleCodegen {
} }
} }
#[derive(Debug)]
struct CompiledModule {
name: String,
kind: ModuleKind,
object: Option<PathBuf>,
bytecode: Option<PathBuf>,
bytecode_compressed: Option<PathBuf>,
}
struct ModuleLlvm { struct ModuleLlvm {
llcx: &'static mut llvm::Context, llcx: &'static mut llvm::Context,
llmod_raw: *const llvm::Module, llmod_raw: *const llvm::Module,
@ -377,7 +357,7 @@ struct CodegenResults {
crate_hash: Svh, crate_hash: Svh,
metadata: rustc::middle::cstore::EncodedMetadata, metadata: rustc::middle::cstore::EncodedMetadata,
windows_subsystem: Option<String>, windows_subsystem: Option<String>,
linker_info: back::linker::LinkerInfo, linker_info: rustc_codegen_utils::linker::LinkerInfo,
crate_info: CrateInfo, crate_info: CrateInfo,
} }

View file

@ -13,9 +13,11 @@ test = false
flate2 = "1.0" flate2 = "1.0"
log = "0.4" log = "0.4"
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" } syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" } syntax_pos = { path = "../libsyntax_pos" }
rustc = { path = "../librustc" } rustc = { path = "../librustc" }
rustc_allocator = { path = "../librustc_allocator" }
rustc_target = { path = "../librustc_target" } rustc_target = { path = "../librustc_target" }
rustc_data_structures = { path = "../librustc_data_structures" } rustc_data_structures = { path = "../librustc_data_structures" }
rustc_mir = { path = "../librustc_mir" } rustc_mir = { path = "../librustc_mir" }

View file

@ -30,8 +30,10 @@ extern crate flate2;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate serialize;
#[macro_use] #[macro_use]
extern crate rustc; extern crate rustc;
extern crate rustc_allocator;
extern crate rustc_target; extern crate rustc_target;
extern crate rustc_mir; extern crate rustc_mir;
extern crate rustc_incremental; extern crate rustc_incremental;
@ -40,10 +42,16 @@ extern crate syntax_pos;
#[macro_use] extern crate rustc_data_structures; #[macro_use] extern crate rustc_data_structures;
extern crate rustc_metadata_utils; extern crate rustc_metadata_utils;
use std::path::PathBuf;
use rustc::session::Session;
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
pub mod command;
pub mod link; pub mod link;
pub mod linker;
pub mod codegen_backend; pub mod codegen_backend;
pub mod symbol_export;
pub mod symbol_names; pub mod symbol_names;
pub mod symbol_names_test; pub mod symbol_names_test;
@ -61,4 +69,43 @@ pub fn check_for_rustc_errors_attr(tcx: TyCtxt) {
} }
} }
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ModuleKind {
Regular,
Metadata,
Allocator,
}
#[derive(Debug)]
pub struct CompiledModule {
pub name: String,
pub kind: ModuleKind,
pub object: Option<PathBuf>,
pub bytecode: Option<PathBuf>,
pub bytecode_compressed: Option<PathBuf>,
}
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
-> PathBuf {
// On Windows, static libraries sometimes show up as libfoo.a and other
// times show up as foo.lib
let oslibname = format!("{}{}{}",
sess.target.target.options.staticlib_prefix,
name,
sess.target.target.options.staticlib_suffix);
let unixlibname = format!("lib{}.a", name);
for path in search_paths {
debug!("looking for {} inside {:?}", name, path);
let test = path.join(&oslibname);
if test.exists() { return test }
if oslibname != unixlibname {
let test = path.join(&unixlibname);
if test.exists() { return test }
}
}
sess.fatal(&format!("could not find native static library `{}`, \
perhaps an -L flag is missing?", name));
}
__build_diagnostic_array! { librustc_codegen_utils, DIAGNOSTICS } __build_diagnostic_array! { librustc_codegen_utils, DIAGNOSTICS }

View file

@ -15,9 +15,7 @@ use std::io::prelude::*;
use std::io::{self, BufWriter}; use std::io::{self, BufWriter};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use back::archive; use command::Command;
use back::command::Command;
use back::symbol_export;
use rustc::hir::def_id::{LOCAL_CRATE, CrateNum}; use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
use rustc::middle::dependency_format::Linkage; use rustc::middle::dependency_format::Linkage;
use rustc::session::Session; use rustc::session::Session;
@ -26,7 +24,6 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
use rustc_target::spec::{LinkerFlavor, LldFlavor}; use rustc_target::spec::{LinkerFlavor, LldFlavor};
use serialize::{json, Encoder}; use serialize::{json, Encoder};
use llvm_util;
/// For all the linkers we support, and information they might /// For all the linkers we support, and information they might
/// need out of the shared crate context before we get rid of it. /// need out of the shared crate context before we get rid of it.
@ -43,10 +40,13 @@ impl LinkerInfo {
} }
} }
pub fn to_linker<'a>(&'a self, pub fn to_linker<'a>(
cmd: Command, &'a self,
sess: &'a Session, cmd: Command,
flavor: LinkerFlavor) -> Box<dyn Linker+'a> { sess: &'a Session,
flavor: LinkerFlavor,
target_cpu: &'a str,
) -> Box<dyn Linker+'a> {
match flavor { match flavor {
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Lld(LldFlavor::Link) |
LinkerFlavor::Msvc => { LinkerFlavor::Msvc => {
@ -70,6 +70,7 @@ impl LinkerInfo {
info: self, info: self,
hinted_static: false, hinted_static: false,
is_ld: false, is_ld: false,
target_cpu,
}) as Box<dyn Linker> }) as Box<dyn Linker>
} }
@ -82,6 +83,7 @@ impl LinkerInfo {
info: self, info: self,
hinted_static: false, hinted_static: false,
is_ld: true, is_ld: true,
target_cpu,
}) as Box<dyn Linker> }) as Box<dyn Linker>
} }
@ -144,6 +146,7 @@ pub struct GccLinker<'a> {
hinted_static: bool, // Keeps track of the current hinting mode. hinted_static: bool, // Keeps track of the current hinting mode.
// Link as ld // Link as ld
is_ld: bool, is_ld: bool,
target_cpu: &'a str,
} }
impl<'a> GccLinker<'a> { impl<'a> GccLinker<'a> {
@ -204,7 +207,8 @@ impl<'a> GccLinker<'a> {
}; };
self.linker_arg(&format!("-plugin-opt={}", opt_level)); self.linker_arg(&format!("-plugin-opt={}", opt_level));
self.linker_arg(&format!("-plugin-opt=mcpu={}", llvm_util::target_cpu(self.sess))); let target_cpu = self.target_cpu;
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
match self.sess.lto() { match self.sess.lto() {
config::Lto::Thin | config::Lto::Thin |
@ -263,7 +267,7 @@ impl<'a> Linker for GccLinker<'a> {
// -force_load is the macOS equivalent of --whole-archive, but it // -force_load is the macOS equivalent of --whole-archive, but it
// involves passing the full path to the library to link. // involves passing the full path to the library to link.
self.linker_arg("-force_load"); self.linker_arg("-force_load");
let lib = archive::find_library(lib, search_path, &self.sess); let lib = ::find_library(lib, search_path, &self.sess);
self.linker_arg(&lib); self.linker_arg(&lib);
} }
} }
@ -898,7 +902,8 @@ impl<'a> Linker for EmLinker<'a> {
fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec<String> { fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec<String> {
let mut symbols = Vec::new(); let mut symbols = Vec::new();
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); let export_threshold =
::symbol_export::crates_export_threshold(&[crate_type]);
for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() { for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
if level.is_below_threshold(export_threshold) { if level.is_below_threshold(export_threshold) {
symbols.push(symbol.symbol_name(tcx).to_string()); symbols.push(symbol.symbol_name(tcx).to_string());

View file

@ -11,7 +11,7 @@
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use std::sync::Arc; use std::sync::Arc;
use monomorphize::Instance; use rustc::ty::Instance;
use rustc::hir; use rustc::hir;
use rustc::hir::Node; use rustc::hir::Node;
use rustc::hir::CodegenFnAttrFlags; use rustc::hir::CodegenFnAttrFlags;