Skip to content

Commit

Permalink
Auto merge of rust-lang#86877 - bjorn3:remove_lib_source, r=petrochenkov
Browse files Browse the repository at this point in the history
Remove LibSource

The information is stored in used_crate_source too anyway.

Split out of rust-lang#86105

r? `@petrochenkov`
  • Loading branch information
bors committed Jul 5, 2021
2 parents 952fdf2 + 5added3 commit 72b0c7d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 81 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
.find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
.unwrap()
.1;
for &(cnum, _) in &crate_info.used_crates_dynamic {
for &cnum in &crate_info.used_crates {
let src = &crate_info.used_crate_source[&cnum];
match data[cnum.as_usize() - 1] {
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
Expand Down
48 changes: 30 additions & 18 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::Handler;
use rustc_fs_util::fix_windows_verbatim_for_gcc;
use rustc_hir::def_id::CrateNum;
use rustc_middle::middle::cstore::{DllImport, LibSource};
use rustc_middle::middle::cstore::DllImport;
use rustc_middle::middle::dependency_format::Linkage;
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip};
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest};
Expand Down Expand Up @@ -238,7 +238,7 @@ pub fn each_linked_rlib(
info: &CrateInfo,
f: &mut dyn FnMut(CrateNum, &Path),
) -> Result<(), String> {
let crates = info.used_crates_static.iter();
let crates = info.used_crates.iter();
let mut fmts = None;
for (ty, list) in info.dependency_formats.iter() {
match ty {
Expand All @@ -256,22 +256,23 @@ pub fn each_linked_rlib(
Some(f) => f,
None => return Err("could not find formats for rlibs".to_string()),
};
for &(cnum, ref path) in crates {
for &cnum in crates {
match fmts.get(cnum.as_usize() - 1) {
Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue,
Some(_) => {}
None => return Err("could not find formats for rlibs".to_string()),
}
let name = &info.crate_name[&cnum];
let path = match *path {
LibSource::Some(ref p) => p,
LibSource::MetadataOnly => {
return Err(format!(
"could not find rlib for: `{}`, found rmeta (metadata) file",
name
));
}
LibSource::None => return Err(format!("could not find rlib for: `{}`", name)),
let used_crate_source = &info.used_crate_source[&cnum];
let path = if let Some((path, _)) = &used_crate_source.rlib {
path
} else if used_crate_source.rmeta.is_some() {
return Err(format!(
"could not find rlib for: `{}`, found rmeta (metadata) file",
name
));
} else {
return Err(format!("could not find rlib for: `{}`", name));
};
f(cnum, &path);
}
Expand Down Expand Up @@ -1759,8 +1760,19 @@ fn add_rpath_args(
// where extern libraries might live, based on the
// add_lib_search_paths
if sess.opts.cg.rpath {
let libs = codegen_results
.crate_info
.used_crates
.iter()
.filter_map(|cnum| {
codegen_results.crate_info.used_crate_source[cnum]
.dylib
.as_ref()
.map(|(path, _)| &**path)
})
.collect::<Vec<_>>();
let mut rpath_config = RPathConfig {
used_crates: &codegen_results.crate_info.used_crates_dynamic,
libs: &*libs,
out_filename: out_filename.to_path_buf(),
has_rpath: sess.target.has_rpath,
is_like_osx: sess.target.is_like_osx,
Expand Down Expand Up @@ -2121,7 +2133,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(

// Invoke get_used_crates to ensure that we get a topological sorting of
// crates.
let deps = &codegen_results.crate_info.used_crates_dynamic;
let deps = &codegen_results.crate_info.used_crates;

// There's a few internal crates in the standard library (aka libcore and
// libstd) which actually have a circular dependence upon one another. This
Expand Down Expand Up @@ -2149,7 +2161,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let mut required = FxHashSet::default();

let info = &codegen_results.crate_info;
for &(cnum, _) in deps.iter().rev() {
for &cnum in deps.iter().rev() {
if let Some(missing) = info.missing_lang_items.get(&cnum) {
let missing_crates = missing.iter().map(|i| info.lang_item_to_crate.get(i).copied());
required.extend(missing_crates);
Expand All @@ -2176,7 +2188,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(

let mut compiler_builtins = None;

for &(cnum, _) in deps.iter() {
for &cnum in deps.iter() {
if group_start == Some(cnum) {
cmd.group_start();
}
Expand Down Expand Up @@ -2388,9 +2400,9 @@ fn add_upstream_native_libraries(
.find(|(ty, _)| *ty == crate_type)
.expect("failed to find crate type in dependency format list");

let crates = &codegen_results.crate_info.used_crates_static;
let crates = &codegen_results.crate_info.used_crates;
let mut last = (NativeLibKind::Unspecified, None);
for &(cnum, _) in crates {
for &cnum in crates {
for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
let name = match lib.name {
Some(l) => l,
Expand Down
19 changes: 7 additions & 12 deletions compiler/rustc_codegen_ssa/src/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use rustc_hir::def_id::CrateNum;
use rustc_middle::middle::cstore::LibSource;

pub struct RPathConfig<'a> {
pub used_crates: &'a [(CrateNum, LibSource)],
pub libs: &'a [&'a Path],
pub out_filename: PathBuf,
pub is_like_osx: bool,
pub has_rpath: bool,
Expand All @@ -23,9 +20,7 @@ pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {

debug!("preparing the RPATH!");

let libs = config.used_crates;
let libs = libs.iter().filter_map(|&(_, ref l)| l.option()).collect::<Vec<_>>();
let rpaths = get_rpaths(config, &libs);
let rpaths = get_rpaths(config);
let mut flags = rpaths_to_flags(&rpaths);

// Use DT_RUNPATH instead of DT_RPATH if available
Expand All @@ -52,17 +47,17 @@ fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
ret
}

fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
debug!("output: {:?}", config.out_filename.display());
debug!("libs:");
for libpath in libs {
for libpath in config.libs {
debug!(" {:?}", libpath.display());
}

// Use relative paths to the libraries. Binaries can be moved
// as long as they maintain the relative relationship to the
// crates they depend on.
let rpaths = get_rpaths_relative_to_output(config, libs);
let rpaths = get_rpaths_relative_to_output(config);

debug!("rpaths:");
for rpath in &rpaths {
Expand All @@ -73,8 +68,8 @@ fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
minimize_rpaths(&rpaths)
}

fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<String> {
config.libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect()
}

fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_minimize2() {
fn test_rpath_relative() {
if cfg!(target_os = "macos") {
let config = &mut RPathConfig {
used_crates: &[],
libs: &[],
has_rpath: true,
is_like_osx: true,
linker_is_gnu: false,
Expand All @@ -45,7 +45,7 @@ fn test_rpath_relative() {
assert_eq!(res, "@loader_path/../lib");
} else {
let config = &mut RPathConfig {
used_crates: &[],
libs: &[],
out_filename: PathBuf::from("bin/rustc"),
has_rpath: true,
is_like_osx: false,
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem;
use rustc_index::vec::Idx;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::middle::cstore::EncodedMetadata;
use rustc_middle::middle::cstore::{self, LinkagePreference};
use rustc_middle::middle::cstore::{self, EncodedMetadata};
use rustc_middle::middle::lang_items;
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
Expand Down Expand Up @@ -779,8 +778,7 @@ impl CrateInfo {
native_libraries: Default::default(),
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
crate_name: Default::default(),
used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
used_crates: cstore::used_crates(tcx),
used_crate_source: Default::default(),
lang_item_to_crate: Default::default(),
missing_lang_items: Default::default(),
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_codegen_ssa/src/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::{self, CrateSource, LibSource};
use rustc_middle::middle::cstore::{self, CrateSource};
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 @@ -144,8 +144,7 @@ pub struct CrateInfo {
pub crate_name: FxHashMap<CrateNum, String>,
pub used_libraries: Vec<NativeLib>,
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
pub used_crates_static: Vec<(CrateNum, LibSource)>,
pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
pub used_crates: Vec<CrateNum>,
pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
pub dependency_formats: Lrc<Dependencies>,
Expand Down
16 changes: 13 additions & 3 deletions compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ use crate::creader::CStore;

use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::CrateNum;
use rustc_middle::middle::cstore::CrateDepKind;
use rustc_middle::middle::cstore::LinkagePreference::{self, RequireDynamic, RequireStatic};
use rustc_middle::middle::cstore::{self, CrateDepKind};
use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::CrateType;
Expand Down Expand Up @@ -274,8 +274,18 @@ fn add_library(
}

fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
let crates = cstore::used_crates(tcx, RequireStatic);
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) {
let all_crates_available_as_rlib = tcx
.crates(())
.iter()
.cloned()
.filter_map(|cnum| {
if tcx.dep_kind(cnum).macros_only() {
return None;
}
Some(tcx.used_crate_source(cnum).rlib.is_some())
})
.all(|is_rlib| is_rlib);
if !all_crates_available_as_rlib {
return None;
}

Expand Down
41 changes: 3 additions & 38 deletions compiler/rustc_middle/src/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ impl CrateDepKind {
}
}

#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
pub enum LibSource {
Some(PathBuf),
MetadataOnly,
None,
}

impl LibSource {
pub fn is_some(&self) -> bool {
matches!(self, LibSource::Some(_))
}

pub fn option(&self) -> Option<PathBuf> {
match *self {
LibSource::Some(ref p) => Some(p.clone()),
LibSource::MetadataOnly | LibSource::None => None,
}
}
}

#[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable)]
pub enum LinkagePreference {
RequireDynamic,
Expand Down Expand Up @@ -227,7 +207,7 @@ pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
// In order to get this left-to-right dependency ordering, we perform a
// topological sort of all crates putting the leaves at the right-most
// positions.
pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
pub fn used_crates(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
let mut libs = tcx
.crates(())
.iter()
Expand All @@ -236,26 +216,11 @@ pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum,
if tcx.dep_kind(cnum).macros_only() {
return None;
}
let source = tcx.used_crate_source(cnum);
let path = match prefer {
LinkagePreference::RequireDynamic => source.dylib.clone().map(|p| p.0),
LinkagePreference::RequireStatic => source.rlib.clone().map(|p| p.0),
};
let path = match path {
Some(p) => LibSource::Some(p),
None => {
if source.rmeta.is_some() {
LibSource::MetadataOnly
} else {
LibSource::None
}
}
};
Some((cnum, path))
Some(cnum)
})
.collect::<Vec<_>>();
let mut ordering = tcx.postorder_cnums(()).to_owned();
ordering.reverse();
libs.sort_by_cached_key(|&(a, _)| ordering.iter().position(|x| *x == a));
libs.sort_by_cached_key(|&a| ordering.iter().position(|x| *x == a));
libs
}

0 comments on commit 72b0c7d

Please sign in to comment.