Auto merge of #86877 - bjorn3:remove_lib_source, r=petrochenkov

Remove LibSource

The information is stored in used_crate_source too anyway.

Split out of #86105

r? `@petrochenkov`
This commit is contained in:
bors 2021-07-05 21:52:34 +00:00
commit 72b0c7dfe9
8 changed files with 60 additions and 81 deletions

View File

@ -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) .find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
.unwrap() .unwrap()
.1; .1;
for &(cnum, _) in &crate_info.used_crates_dynamic { for &cnum in &crate_info.used_crates {
let src = &crate_info.used_crate_source[&cnum]; let src = &crate_info.used_crate_source[&cnum];
match data[cnum.as_usize() - 1] { match data[cnum.as_usize() - 1] {
Linkage::NotLinked | Linkage::IncludedFromDylib => {} Linkage::NotLinked | Linkage::IncludedFromDylib => {}

View File

@ -3,7 +3,7 @@
use rustc_errors::Handler; use rustc_errors::Handler;
use rustc_fs_util::fix_windows_verbatim_for_gcc; use rustc_fs_util::fix_windows_verbatim_for_gcc;
use rustc_hir::def_id::CrateNum; 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_middle::middle::dependency_format::Linkage;
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip}; use rustc_session::config::{self, CFGuard, CrateType, DebugInfo, LdImpl, Strip};
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest}; use rustc_session::config::{OutputFilenames, OutputType, PrintRequest};
@ -238,7 +238,7 @@ pub fn each_linked_rlib(
info: &CrateInfo, info: &CrateInfo,
f: &mut dyn FnMut(CrateNum, &Path), f: &mut dyn FnMut(CrateNum, &Path),
) -> Result<(), String> { ) -> Result<(), String> {
let crates = info.used_crates_static.iter(); let crates = info.used_crates.iter();
let mut fmts = None; let mut fmts = None;
for (ty, list) in info.dependency_formats.iter() { for (ty, list) in info.dependency_formats.iter() {
match ty { match ty {
@ -256,22 +256,23 @@ pub fn each_linked_rlib(
Some(f) => f, Some(f) => f,
None => return Err("could not find formats for rlibs".to_string()), 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) { match fmts.get(cnum.as_usize() - 1) {
Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue, Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue,
Some(_) => {} Some(_) => {}
None => return Err("could not find formats for rlibs".to_string()), None => return Err("could not find formats for rlibs".to_string()),
} }
let name = &info.crate_name[&cnum]; let name = &info.crate_name[&cnum];
let path = match *path { let used_crate_source = &info.used_crate_source[&cnum];
LibSource::Some(ref p) => p, let path = if let Some((path, _)) = &used_crate_source.rlib {
LibSource::MetadataOnly => { path
return Err(format!( } else if used_crate_source.rmeta.is_some() {
"could not find rlib for: `{}`, found rmeta (metadata) file", return Err(format!(
name "could not find rlib for: `{}`, found rmeta (metadata) file",
)); name
} ));
LibSource::None => return Err(format!("could not find rlib for: `{}`", name)), } else {
return Err(format!("could not find rlib for: `{}`", name));
}; };
f(cnum, &path); f(cnum, &path);
} }
@ -1759,8 +1760,19 @@ fn add_rpath_args(
// where extern libraries might live, based on the // where extern libraries might live, based on the
// add_lib_search_paths // add_lib_search_paths
if sess.opts.cg.rpath { 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 { let mut rpath_config = RPathConfig {
used_crates: &codegen_results.crate_info.used_crates_dynamic, libs: &*libs,
out_filename: out_filename.to_path_buf(), out_filename: out_filename.to_path_buf(),
has_rpath: sess.target.has_rpath, has_rpath: sess.target.has_rpath,
is_like_osx: sess.target.is_like_osx, is_like_osx: sess.target.is_like_osx,
@ -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 // Invoke get_used_crates to ensure that we get a topological sorting of
// crates. // 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 // There's a few internal crates in the standard library (aka libcore and
// libstd) which actually have a circular dependence upon one another. This // libstd) which actually have a circular dependence upon one another. This
@ -2149,7 +2161,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let mut required = FxHashSet::default(); let mut required = FxHashSet::default();
let info = &codegen_results.crate_info; 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) { 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()); let missing_crates = missing.iter().map(|i| info.lang_item_to_crate.get(i).copied());
required.extend(missing_crates); required.extend(missing_crates);
@ -2176,7 +2188,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let mut compiler_builtins = None; let mut compiler_builtins = None;
for &(cnum, _) in deps.iter() { for &cnum in deps.iter() {
if group_start == Some(cnum) { if group_start == Some(cnum) {
cmd.group_start(); cmd.group_start();
} }
@ -2388,9 +2400,9 @@ fn add_upstream_native_libraries(
.find(|(ty, _)| *ty == crate_type) .find(|(ty, _)| *ty == crate_type)
.expect("failed to find crate type in dependency format list"); .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); 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() { for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
let name = match lib.name { let name = match lib.name {
Some(l) => l, Some(l) => l,

View File

@ -4,11 +4,8 @@
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use rustc_hir::def_id::CrateNum;
use rustc_middle::middle::cstore::LibSource;
pub struct RPathConfig<'a> { pub struct RPathConfig<'a> {
pub used_crates: &'a [(CrateNum, LibSource)], pub libs: &'a [&'a Path],
pub out_filename: PathBuf, pub out_filename: PathBuf,
pub is_like_osx: bool, pub is_like_osx: bool,
pub has_rpath: bool, pub has_rpath: bool,
@ -23,9 +20,7 @@ pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
debug!("preparing the RPATH!"); debug!("preparing the RPATH!");
let libs = config.used_crates; let rpaths = get_rpaths(config);
let libs = libs.iter().filter_map(|&(_, ref l)| l.option()).collect::<Vec<_>>();
let rpaths = get_rpaths(config, &libs);
let mut flags = rpaths_to_flags(&rpaths); let mut flags = rpaths_to_flags(&rpaths);
// Use DT_RUNPATH instead of DT_RPATH if available // Use DT_RUNPATH instead of DT_RPATH if available
@ -52,17 +47,17 @@ fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
ret 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!("output: {:?}", config.out_filename.display());
debug!("libs:"); debug!("libs:");
for libpath in libs { for libpath in config.libs {
debug!(" {:?}", libpath.display()); debug!(" {:?}", libpath.display());
} }
// Use relative paths to the libraries. Binaries can be moved // Use relative paths to the libraries. Binaries can be moved
// as long as they maintain the relative relationship to the // as long as they maintain the relative relationship to the
// crates they depend on. // crates they depend on.
let rpaths = get_rpaths_relative_to_output(config, libs); let rpaths = get_rpaths_relative_to_output(config);
debug!("rpaths:"); debug!("rpaths:");
for rpath in &rpaths { for rpath in &rpaths {
@ -73,8 +68,8 @@ fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
minimize_rpaths(&rpaths) minimize_rpaths(&rpaths)
} }
fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> { fn get_rpaths_relative_to_output(config: &mut RPathConfig<'_>) -> Vec<String> {
libs.iter().map(|a| get_rpath_relative_to_output(config, a)).collect() 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 { fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {

View File

@ -35,7 +35,7 @@ fn test_minimize2() {
fn test_rpath_relative() { fn test_rpath_relative() {
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
let config = &mut RPathConfig { let config = &mut RPathConfig {
used_crates: &[], libs: &[],
has_rpath: true, has_rpath: true,
is_like_osx: true, is_like_osx: true,
linker_is_gnu: false, linker_is_gnu: false,
@ -45,7 +45,7 @@ fn test_rpath_relative() {
assert_eq!(res, "@loader_path/../lib"); assert_eq!(res, "@loader_path/../lib");
} else { } else {
let config = &mut RPathConfig { let config = &mut RPathConfig {
used_crates: &[], libs: &[],
out_filename: PathBuf::from("bin/rustc"), out_filename: PathBuf::from("bin/rustc"),
has_rpath: true, has_rpath: true,
is_like_osx: false, is_like_osx: false,

View File

@ -19,8 +19,7 @@
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::middle::cstore::EncodedMetadata; use rustc_middle::middle::cstore::{self, EncodedMetadata};
use rustc_middle::middle::cstore::{self, LinkagePreference};
use rustc_middle::middle::lang_items; use rustc_middle::middle::lang_items;
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem}; use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout}; use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
@ -779,8 +778,7 @@ pub fn new(tcx: TyCtxt<'_>) -> CrateInfo {
native_libraries: Default::default(), native_libraries: Default::default(),
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(), used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
crate_name: Default::default(), crate_name: Default::default(),
used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic), used_crates: cstore::used_crates(tcx),
used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
used_crate_source: Default::default(), used_crate_source: Default::default(),
lang_item_to_crate: Default::default(), lang_item_to_crate: Default::default(),
missing_lang_items: Default::default(), missing_lang_items: Default::default(),

View File

@ -24,7 +24,7 @@
use rustc_hir::def_id::CrateNum; use rustc_hir::def_id::CrateNum;
use rustc_hir::LangItem; use rustc_hir::LangItem;
use rustc_middle::dep_graph::WorkProduct; 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::middle::dependency_format::Dependencies;
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT}; use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT};
@ -144,8 +144,7 @@ pub struct CrateInfo {
pub crate_name: FxHashMap<CrateNum, String>, pub crate_name: FxHashMap<CrateNum, String>,
pub used_libraries: Vec<NativeLib>, pub used_libraries: Vec<NativeLib>,
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>, pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
pub used_crates_static: Vec<(CrateNum, LibSource)>, pub used_crates: Vec<CrateNum>,
pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>, pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>, pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
pub dependency_formats: Lrc<Dependencies>, pub dependency_formats: Lrc<Dependencies>,

View File

@ -55,8 +55,8 @@
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::CrateNum; 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::LinkagePreference::{self, RequireDynamic, RequireStatic};
use rustc_middle::middle::cstore::{self, CrateDepKind};
use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage}; use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
@ -274,8 +274,18 @@ fn add_library(
} }
fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> { fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
let crates = cstore::used_crates(tcx, RequireStatic); let all_crates_available_as_rlib = tcx
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) { .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; return None;
} }

View File

@ -60,26 +60,6 @@ pub fn macros_only(self) -> bool {
} }
} }
#[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)] #[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable)]
pub enum LinkagePreference { pub enum LinkagePreference {
RequireDynamic, RequireDynamic,
@ -227,7 +207,7 @@ fn def_path_hash_to_def_id(
// In order to get this left-to-right dependency ordering, we perform a // 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 // topological sort of all crates putting the leaves at the right-most
// positions. // positions.
pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> { pub fn used_crates(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
let mut libs = tcx let mut libs = tcx
.crates(()) .crates(())
.iter() .iter()
@ -236,26 +216,11 @@ pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum,
if tcx.dep_kind(cnum).macros_only() { if tcx.dep_kind(cnum).macros_only() {
return None; return None;
} }
let source = tcx.used_crate_source(cnum); Some(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))
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut ordering = tcx.postorder_cnums(()).to_owned(); let mut ordering = tcx.postorder_cnums(()).to_owned();
ordering.reverse(); 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 libs
} }