Skip to content

Commit

Permalink
Rollup merge of #72309 - petrochenkov:linkunspec, r=matthewjasper
Browse files Browse the repository at this point in the history
Some renaming and minor refactoring for `NativeLibraryKind`
  • Loading branch information
RalfJung authored May 22, 2020
2 parents 0eba152 + 8dbe4d9 commit afbbb86
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 125 deletions.
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ pub fn provide(providers: &mut Providers<'_>) {

pub fn provide_extern(providers: &mut Providers<'_>) {
providers.wasm_import_module_map = |tcx, cnum| {
// Build up a map from DefId to a `NativeLibrary` structure, where
// `NativeLibrary` internally contains information about
// Build up a map from DefId to a `NativeLib` structure, where
// `NativeLib` internally contains information about
// `#[link(wasm_import_module = "...")]` for example.
let native_libs = tcx.native_libraries(cnum);

Expand Down
50 changes: 27 additions & 23 deletions src/librustc_codegen_ssa/back/link.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_fs_util::fix_windows_verbatim_for_gcc;
use rustc_hir::def_id::CrateNum;
use rustc_middle::middle::cstore::{EncodedMetadata, LibSource, NativeLibrary, NativeLibraryKind};
use rustc_middle::middle::cstore::{EncodedMetadata, LibSource, NativeLib};
use rustc_middle::middle::dependency_format::Linkage;
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo};
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest, Sanitizer};
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_session::search_paths::PathKind;
use rustc_session::utils::NativeLibKind;
/// For all the linkers we support, and information they might
/// need out of the shared crate context before we get rid of it.
use rustc_session::{filesearch, Session};
Expand Down Expand Up @@ -328,11 +329,12 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
// metadata of the rlib we're generating somehow.
for lib in codegen_results.crate_info.used_libraries.iter() {
match lib.kind {
NativeLibraryKind::NativeStatic => {}
NativeLibraryKind::NativeStaticNobundle
| NativeLibraryKind::NativeFramework
| NativeLibraryKind::NativeRawDylib
| NativeLibraryKind::NativeUnknown => continue,
NativeLibKind::StaticBundle => {}
NativeLibKind::StaticNoBundle
| NativeLibKind::Dylib
| NativeLibKind::Framework
| NativeLibKind::RawDylib
| NativeLibKind::Unspecified => continue,
}
if let Some(name) = lib.name {
ab.add_native_library(name);
Expand Down Expand Up @@ -431,7 +433,7 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
// object files come from where and selectively skip them.
let skip_object_files = native_libs
.iter()
.any(|lib| lib.kind == NativeLibraryKind::NativeStatic && !relevant_lib(sess, lib));
.any(|lib| lib.kind == NativeLibKind::StaticBundle && !relevant_lib(sess, lib));
ab.add_rlib(
path,
&name.as_str(),
Expand Down Expand Up @@ -908,26 +910,28 @@ enum RlibFlavor {
StaticlibBase,
}

fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLibrary]) {
fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
let lib_args: Vec<_> = all_native_libs
.iter()
.filter(|l| relevant_lib(sess, l))
.filter_map(|lib| {
let name = lib.name?;
match lib.kind {
NativeLibraryKind::NativeStaticNobundle | NativeLibraryKind::NativeUnknown => {
NativeLibKind::StaticNoBundle
| NativeLibKind::Dylib
| NativeLibKind::Unspecified => {
if sess.target.target.options.is_like_msvc {
Some(format!("{}.lib", name))
} else {
Some(format!("-l{}", name))
}
}
NativeLibraryKind::NativeFramework => {
NativeLibKind::Framework => {
// ld-only syntax, since there are no frameworks in MSVC
Some(format!("-framework {}", name))
}
// These are included, no need to print them
NativeLibraryKind::NativeStatic | NativeLibraryKind::NativeRawDylib => None,
NativeLibKind::StaticBundle | NativeLibKind::RawDylib => None,
}
})
.collect();
Expand Down Expand Up @@ -1697,11 +1701,11 @@ fn add_local_native_libraries(
None => continue,
};
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(name),
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path),
NativeLibraryKind::NativeRawDylib => {
NativeLibKind::Dylib | NativeLibKind::Unspecified => cmd.link_dylib(name),
NativeLibKind::Framework => cmd.link_framework(name),
NativeLibKind::StaticNoBundle => cmd.link_staticlib(name),
NativeLibKind::StaticBundle => cmd.link_whole_staticlib(name, &search_path),
NativeLibKind::RawDylib => {
// FIXME(#58713): Proper handling for raw dylibs.
bug!("raw_dylib feature not yet implemented");
}
Expand Down Expand Up @@ -1891,7 +1895,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
let skip_native = native_libs
.iter()
.any(|lib| lib.kind == NativeLibraryKind::NativeStatic && !relevant_lib(sess, lib));
.any(|lib| lib.kind == NativeLibKind::StaticBundle && !relevant_lib(sess, lib));

if (!are_upstream_rust_objects_already_included(sess)
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum))
Expand Down Expand Up @@ -2033,9 +2037,9 @@ fn add_upstream_native_libraries(
continue;
}
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
NativeLibraryKind::NativeStaticNobundle => {
NativeLibKind::Dylib | NativeLibKind::Unspecified => cmd.link_dylib(name),
NativeLibKind::Framework => cmd.link_framework(name),
NativeLibKind::StaticNoBundle => {
// Link "static-nobundle" native libs only if the crate they originate from
// is being linked statically to the current crate. If it's linked dynamically
// or is an rlib already included via some other dylib crate, the symbols from
Expand All @@ -2047,8 +2051,8 @@ fn add_upstream_native_libraries(
// ignore statically included native libraries here as we've
// already included them when we included the rust library
// previously
NativeLibraryKind::NativeStatic => {}
NativeLibraryKind::NativeRawDylib => {
NativeLibKind::StaticBundle => {}
NativeLibKind::RawDylib => {
// FIXME(#58713): Proper handling for raw dylibs.
bug!("raw_dylib feature not yet implemented");
}
Expand All @@ -2057,7 +2061,7 @@ fn add_upstream_native_libraries(
}
}

fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool {
match lib.cfg {
Some(ref cfg) => rustc_attr::cfg_matches(cfg, &sess.parse_sess, None),
None => true,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_ssa/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
use rustc_session::cgu_reuse_tracker::CguReuse;
use rustc_session::config::{self, EntryFnType};
use rustc_session::utils::NativeLibKind;
use rustc_session::Session;
use rustc_span::Span;
use rustc_symbol_mangling::test as symbol_names_test;
Expand Down Expand Up @@ -895,7 +896,7 @@ pub fn provide_both(providers: &mut Providers<'_>) {
.native_libraries(krate)
.iter()
.filter(|lib| {
if lib.kind != cstore::NativeLibraryKind::NativeUnknown {
if !matches!(lib.kind, NativeLibKind::Dylib | NativeLibKind::Unspecified) {
return false;
}
let cfg = match lib.cfg {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_codegen_ssa/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_hir::def_id::CrateNum;
use rustc_hir::LangItem;
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::cstore::{CrateSource, LibSource, NativeLibrary};
use rustc_middle::middle::cstore::{CrateSource, LibSource, NativeLib};
use rustc_middle::middle::dependency_format::Dependencies;
use rustc_middle::ty::query::Providers;
use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT};
Expand Down Expand Up @@ -112,9 +112,9 @@ pub struct CrateInfo {
pub compiler_builtins: Option<CrateNum>,
pub profiler_runtime: Option<CrateNum>,
pub is_no_builtins: FxHashSet<CrateNum>,
pub native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLibrary>>>,
pub native_libraries: FxHashMap<CrateNum, Lrc<Vec<NativeLib>>>,
pub crate_name: FxHashMap<CrateNum, String>,
pub used_libraries: Lrc<Vec<NativeLibrary>>,
pub used_libraries: Lrc<Vec<NativeLib>>,
pub link_args: Lrc<Vec<String>>,
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
pub used_crates_static: Vec<(CrateNum, LibSource)>,
Expand Down
44 changes: 22 additions & 22 deletions src/librustc_interface/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::interface::parse_cfgspecs;

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
use rustc_middle::middle::cstore;
use rustc_session::config::Strip;
use rustc_session::config::{build_configuration, build_session_options, to_crate_config};
use rustc_session::config::{rustc_optgroups, ErrorOutputType, ExternLocation, Options, Passes};
Expand All @@ -11,6 +10,7 @@ use rustc_session::config::{Externs, OutputType, OutputTypes, Sanitizer, SymbolM
use rustc_session::getopts;
use rustc_session::lint::Level;
use rustc_session::search_paths::SearchPath;
use rustc_session::utils::NativeLibKind;
use rustc_session::{build_session, Session};
use rustc_span::edition::{Edition, DEFAULT_EDITION};
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -300,30 +300,30 @@ fn test_native_libs_tracking_hash_different_values() {

// Reference
v1.libs = vec![
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("b"), None, Some(cstore::NativeFramework)),
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("b"), None, NativeLibKind::Framework),
(String::from("c"), None, NativeLibKind::Unspecified),
];

// Change label
v2.libs = vec![
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("X"), None, Some(cstore::NativeFramework)),
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("X"), None, NativeLibKind::Framework),
(String::from("c"), None, NativeLibKind::Unspecified),
];

// Change kind
v3.libs = vec![
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("b"), None, Some(cstore::NativeStatic)),
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("b"), None, NativeLibKind::StaticBundle),
(String::from("c"), None, NativeLibKind::Unspecified),
];

// Change new-name
v4.libs = vec![
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("b"), Some(String::from("X")), Some(cstore::NativeFramework)),
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("b"), Some(String::from("X")), NativeLibKind::Framework),
(String::from("c"), None, NativeLibKind::Unspecified),
];

assert!(v1.dep_tracking_hash() != v2.dep_tracking_hash());
Expand All @@ -345,21 +345,21 @@ fn test_native_libs_tracking_hash_different_order() {

// Reference
v1.libs = vec![
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("b"), None, Some(cstore::NativeFramework)),
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("b"), None, NativeLibKind::Framework),
(String::from("c"), None, NativeLibKind::Unspecified),
];

v2.libs = vec![
(String::from("b"), None, Some(cstore::NativeFramework)),
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("b"), None, NativeLibKind::Framework),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("c"), None, NativeLibKind::Unspecified),
];

v3.libs = vec![
(String::from("c"), None, Some(cstore::NativeUnknown)),
(String::from("a"), None, Some(cstore::NativeStatic)),
(String::from("b"), None, Some(cstore::NativeFramework)),
(String::from("c"), None, NativeLibKind::Unspecified),
(String::from("a"), None, NativeLibKind::StaticBundle),
(String::from("b"), None, NativeLibKind::Framework),
];

assert!(v1.dep_tracking_hash() == v2.dep_tracking_hash());
Expand Down
Loading

0 comments on commit afbbb86

Please sign in to comment.