Skip to content

Commit

Permalink
Rollup merge of rust-lang#55225 - bjorn3:rustc_link, r=cramertj
Browse files Browse the repository at this point in the history
Move cg_llvm::back::linker to cg_utils

This allows it to be reused by alternative codegen backends.
  • Loading branch information
kennytm committed Oct 24, 2018
2 parents 942a162 + 655f9d8 commit 848c2b7
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 67 deletions.
2 changes: 2 additions & 0 deletions src/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2135,11 +2135,13 @@ dependencies = [
"flate2 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0",
"rustc_allocator 0.0.0",
"rustc_data_structures 0.0.0",
"rustc_incremental 0.0.0",
"rustc_metadata_utils 0.0.0",
"rustc_mir 0.0.0",
"rustc_target 0.0.0",
"serialize 0.0.0",
"syntax 0.0.0",
"syntax_pos 0.0.0",
]
Expand Down
24 changes: 1 addition & 23 deletions src/librustc_codegen_llvm/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
match c.name() {
Expand Down Expand Up @@ -128,7 +106,7 @@ impl<'a> ArchiveBuilder<'a> {
/// Adds all of the contents of a native library to this archive. This will
/// search in the relevant locations for a library named `name`.
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.add_archive(&location, |_| false).unwrap_or_else(|e| {
self.config.sess.fatal(&format!("failed to add native library {}: {}",
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_codegen_llvm/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use back::wasm;
use cc::windows_registry;
use super::archive::{ArchiveBuilder, ArchiveConfig};
use super::bytecode::RLIB_BYTECODE_EXTENSION;
use super::linker::Linker;
use super::command::Command;
use super::rpath::RPathConfig;
use super::rpath;
use metadata::METADATA_FILENAME;
Expand All @@ -31,6 +29,8 @@ use rustc::hir::def_id::CrateNum;
use tempfile::{Builder as TempFileBuilder, TempDir};
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
use rustc_data_structures::fx::FxHashSet;
use rustc_codegen_utils::linker::Linker;
use rustc_codegen_utils::command::Command;
use context::get_reloc_model;
use llvm;

Expand Down Expand Up @@ -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,
out_filename, codegen_results);
cmd = linker.finalize();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

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

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use attributes;
use back::bytecode::{self, RLIB_BYTECODE_EXTENSION};
use back::lto::{self, ModuleBuffer, ThinBuffer, SerializedModule};
use back::link::{self, get_linker, remove};
use back::command::Command;
use back::linker::LinkerInfo;
use back::symbol_export::ExportedSymbols;
use base;
use consts;
use memmap;
Expand All @@ -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_data_structures::small_c_str::SmallCStr;
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::emitter::{Emitter};
use syntax::attr;
Expand Down
30 changes: 5 additions & 25 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ use back::bytecode::RLIB_BYTECODE_EXTENSION;

pub use llvm_util::target_features;
use std::any::Any;
use std::path::{PathBuf};
use std::sync::mpsc;
use rustc_data_structures::sync::Lrc;

Expand All @@ -87,20 +86,17 @@ use rustc::util::time_graph;
use rustc::util::nodemap::{FxHashSet, FxHashMap};
use rustc::util::profiling::ProfileCategory;
use rustc_mir::monomorphize;
use rustc_codegen_utils::{CompiledModule, ModuleKind};
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::svh::Svh;

mod diagnostics;

mod back {
pub use rustc_codegen_utils::symbol_names;
mod archive;
pub mod bytecode;
mod command;
pub mod linker;
pub mod link;
pub mod lto;
pub mod symbol_export;
pub mod write;
mod rpath;
pub mod wasm;
Expand Down Expand Up @@ -194,14 +190,14 @@ impl CodegenBackend for LlvmCodegenBackend {
}

fn provide(&self, providers: &mut ty::query::Providers) {
back::symbol_names::provide(providers);
back::symbol_export::provide(providers);
rustc_codegen_utils::symbol_export::provide(providers);
rustc_codegen_utils::symbol_names::provide(providers);
base::provide(providers);
attributes::provide(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);
attributes::provide_extern(providers);
}
Expand Down Expand Up @@ -281,13 +277,6 @@ struct CachedModuleCodegen {
source: WorkProduct,
}

#[derive(Copy, Clone, Debug, PartialEq)]
enum ModuleKind {
Regular,
Metadata,
Allocator,
}

impl ModuleCodegen {
fn into_compiled_module(self,
emit_obj: bool,
Expand Down Expand Up @@ -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 {
llcx: &'static mut llvm::Context,
llmod_raw: *const llvm::Module,
Expand Down Expand Up @@ -377,7 +357,7 @@ struct CodegenResults {
crate_hash: Svh,
metadata: rustc::middle::cstore::EncodedMetadata,
windows_subsystem: Option<String>,
linker_info: back::linker::LinkerInfo,
linker_info: rustc_codegen_utils::linker::LinkerInfo,
crate_info: CrateInfo,
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ test = false
flate2 = "1.0"
log = "0.4"

serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
rustc = { path = "../librustc" }
rustc_allocator = { path = "../librustc_allocator" }
rustc_target = { path = "../librustc_target" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_mir = { path = "../librustc_mir" }
Expand Down
File renamed without changes.
47 changes: 47 additions & 0 deletions src/librustc_codegen_utils/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ extern crate flate2;
#[macro_use]
extern crate log;

extern crate serialize;
#[macro_use]
extern crate rustc;
extern crate rustc_allocator;
extern crate rustc_target;
extern crate rustc_mir;
extern crate rustc_incremental;
Expand All @@ -40,10 +42,16 @@ extern crate syntax_pos;
#[macro_use] extern crate rustc_data_structures;
extern crate rustc_metadata_utils;

use std::path::PathBuf;

use rustc::session::Session;
use rustc::ty::TyCtxt;

pub mod command;
pub mod link;
pub mod linker;
pub mod codegen_backend;
pub mod symbol_export;
pub mod symbol_names;
pub mod symbol_names_test;

Expand All @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use std::io::prelude::*;
use std::io::{self, BufWriter};
use std::path::{Path, PathBuf};

use back::archive;
use back::command::Command;
use back::symbol_export;
use command::Command;
use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
use rustc::middle::dependency_format::Linkage;
use rustc::session::Session;
Expand All @@ -26,7 +24,6 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
use rustc::ty::TyCtxt;
use rustc_target::spec::{LinkerFlavor, LldFlavor};
use serialize::{json, Encoder};
use llvm_util;

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

pub fn to_linker<'a>(&'a self,
cmd: Command,
sess: &'a Session,
flavor: LinkerFlavor) -> Box<dyn Linker+'a> {
pub fn to_linker<'a>(
&'a self,
cmd: Command,
sess: &'a Session,
flavor: LinkerFlavor,
target_cpu: &'a str,
) -> Box<dyn Linker+'a> {
match flavor {
LinkerFlavor::Lld(LldFlavor::Link) |
LinkerFlavor::Msvc => {
Expand All @@ -70,6 +70,7 @@ impl LinkerInfo {
info: self,
hinted_static: false,
is_ld: false,
target_cpu,
}) as Box<dyn Linker>
}

Expand All @@ -82,6 +83,7 @@ impl LinkerInfo {
info: self,
hinted_static: false,
is_ld: true,
target_cpu,
}) as Box<dyn Linker>
}

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

impl<'a> GccLinker<'a> {
Expand Down Expand Up @@ -204,7 +207,8 @@ impl<'a> GccLinker<'a> {
};

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() {
config::Lto::Thin |
Expand Down Expand Up @@ -263,7 +267,7 @@ impl<'a> Linker for GccLinker<'a> {
// -force_load is the macOS equivalent of --whole-archive, but it
// involves passing the full path to the library to link.
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);
}
}
Expand Down Expand Up @@ -898,7 +902,8 @@ impl<'a> Linker for EmLinker<'a> {
fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec<String> {
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() {
if level.is_below_threshold(export_threshold) {
symbols.push(symbol.symbol_name(tcx).to_string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use rustc_data_structures::sync::Lrc;
use std::sync::Arc;

use monomorphize::Instance;
use rustc::ty::Instance;
use rustc::hir;
use rustc::hir::Node;
use rustc::hir::CodegenFnAttrFlags;
Expand Down

0 comments on commit 848c2b7

Please sign in to comment.