Skip to content

Commit b342215

Browse files
committed
Remove LibSource
The information is stored in used_crate_source too anyway
1 parent 479f1b0 commit b342215

File tree

8 files changed

+59
-81
lines changed

8 files changed

+59
-81
lines changed

compiler/rustc_codegen_cranelift/src/driver/jit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
185185
.find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
186186
.unwrap()
187187
.1;
188-
for &(cnum, _) in &crate_info.used_crates_dynamic {
188+
for &cnum in &crate_info.used_crates {
189189
let src = &crate_info.used_crate_source[&cnum];
190190
match data[cnum.as_usize() - 1] {
191191
Linkage::NotLinked | Linkage::IncludedFromDylib => {}

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::temp_dir::MaybeTempDir;
33
use rustc_errors::Handler;
44
use rustc_fs_util::fix_windows_verbatim_for_gcc;
55
use rustc_hir::def_id::CrateNum;
6-
use rustc_middle::middle::cstore::{DllImport, LibSource};
6+
use rustc_middle::middle::cstore::DllImport;
77
use rustc_middle::middle::dependency_format::Linkage;
88
use rustc_session::config::{self, CFGuard, CrateType, DebugInfo};
99
use rustc_session::config::{OutputFilenames, OutputType, PrintRequest};
@@ -238,7 +238,7 @@ pub fn each_linked_rlib(
238238
info: &CrateInfo,
239239
f: &mut dyn FnMut(CrateNum, &Path),
240240
) -> Result<(), String> {
241-
let crates = info.used_crates_static.iter();
241+
let crates = info.used_crates.iter();
242242
let mut fmts = None;
243243
for (ty, list) in info.dependency_formats.iter() {
244244
match ty {
@@ -256,22 +256,22 @@ pub fn each_linked_rlib(
256256
Some(f) => f,
257257
None => return Err("could not find formats for rlibs".to_string()),
258258
};
259-
for &(cnum, ref path) in crates {
259+
for &cnum in crates {
260260
match fmts.get(cnum.as_usize() - 1) {
261261
Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue,
262262
Some(_) => {}
263263
None => return Err("could not find formats for rlibs".to_string()),
264264
}
265265
let name = &info.crate_name[&cnum];
266-
let path = match *path {
267-
LibSource::Some(ref p) => p,
268-
LibSource::MetadataOnly => {
269-
return Err(format!(
270-
"could not find rlib for: `{}`, found rmeta (metadata) file",
271-
name
272-
));
273-
}
274-
LibSource::None => return Err(format!("could not find rlib for: `{}`", name)),
266+
let path = if let Some((path, _)) = &info.used_crate_source[&cnum].rlib {
267+
path
268+
} else if info.used_crate_source[&cnum].rmeta.is_some() {
269+
return Err(format!(
270+
"could not find rlib for: `{}`, found rmeta (metadata) file",
271+
name
272+
));
273+
} else {
274+
return Err(format!("could not find rlib for: `{}`", name));
275275
};
276276
f(cnum, &path);
277277
}
@@ -1725,8 +1725,19 @@ fn add_rpath_args(
17251725

17261726
path
17271727
};
1728+
let libs = codegen_results
1729+
.crate_info
1730+
.used_crates
1731+
.iter()
1732+
.filter_map(|cnum| {
1733+
codegen_results.crate_info.used_crate_source[cnum]
1734+
.dylib
1735+
.as_ref()
1736+
.map(|(path, _)| &**path)
1737+
})
1738+
.collect::<Vec<_>>();
17281739
let mut rpath_config = RPathConfig {
1729-
used_crates: &codegen_results.crate_info.used_crates_dynamic,
1740+
libs: &*libs,
17301741
out_filename: out_filename.to_path_buf(),
17311742
has_rpath: sess.target.has_rpath,
17321743
is_like_osx: sess.target.is_like_osx,
@@ -2086,7 +2097,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
20862097

20872098
// Invoke get_used_crates to ensure that we get a topological sorting of
20882099
// crates.
2089-
let deps = &codegen_results.crate_info.used_crates_dynamic;
2100+
let deps = &codegen_results.crate_info.used_crates;
20902101

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

21162127
let info = &codegen_results.crate_info;
2117-
for &(cnum, _) in deps.iter().rev() {
2128+
for &cnum in deps.iter().rev() {
21182129
if let Some(missing) = info.missing_lang_items.get(&cnum) {
21192130
let missing_crates = missing.iter().map(|i| info.lang_item_to_crate.get(i).copied());
21202131
required.extend(missing_crates);
@@ -2141,7 +2152,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21412152

21422153
let mut compiler_builtins = None;
21432154

2144-
for &(cnum, _) in deps.iter() {
2155+
for &cnum in deps.iter() {
21452156
if group_start == Some(cnum) {
21462157
cmd.group_start();
21472158
}
@@ -2353,9 +2364,9 @@ fn add_upstream_native_libraries(
23532364
.find(|(ty, _)| *ty == crate_type)
23542365
.expect("failed to find crate type in dependency format list");
23552366

2356-
let crates = &codegen_results.crate_info.used_crates_static;
2367+
let crates = &codegen_results.crate_info.used_crates;
23572368
let mut last = (NativeLibKind::Unspecified, None);
2358-
for &(cnum, _) in crates {
2369+
for &cnum in crates {
23592370
for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
23602371
let name = match lib.name {
23612372
Some(l) => l,

compiler/rustc_codegen_ssa/src/back/rpath.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ use std::env;
44
use std::fs;
55
use std::path::{Path, PathBuf};
66

7-
use rustc_hir::def_id::CrateNum;
8-
use rustc_middle::middle::cstore::LibSource;
9-
107
pub struct RPathConfig<'a> {
11-
pub used_crates: &'a [(CrateNum, LibSource)],
8+
pub libs: &'a [&'a Path],
129
pub out_filename: PathBuf,
1310
pub is_like_osx: bool,
1411
pub has_rpath: bool,
@@ -24,9 +21,7 @@ pub fn get_rpath_flags(config: &mut RPathConfig<'_>) -> Vec<String> {
2421

2522
debug!("preparing the RPATH!");
2623

27-
let libs = config.used_crates;
28-
let libs = libs.iter().filter_map(|&(_, ref l)| l.option()).collect::<Vec<_>>();
29-
let rpaths = get_rpaths(config, &libs);
24+
let rpaths = get_rpaths(config);
3025
let mut flags = rpaths_to_flags(&rpaths);
3126

3227
// Use DT_RUNPATH instead of DT_RPATH if available
@@ -53,17 +48,17 @@ fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
5348
ret
5449
}
5550

56-
fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
51+
fn get_rpaths(config: &mut RPathConfig<'_>) -> Vec<String> {
5752
debug!("output: {:?}", config.out_filename.display());
5853
debug!("libs:");
59-
for libpath in libs {
54+
for libpath in config.libs {
6055
debug!(" {:?}", libpath.display());
6156
}
6257

6358
// Use relative paths to the libraries. Binaries can be moved
6459
// as long as they maintain the relative relationship to the
6560
// crates they depend on.
66-
let rel_rpaths = get_rpaths_relative_to_output(config, libs);
61+
let rel_rpaths = get_rpaths_relative_to_output(config);
6762

6863
// And a final backup rpath to the global library location.
6964
let fallback_rpaths = vec![get_install_prefix_rpath(config)];
@@ -85,8 +80,8 @@ fn get_rpaths(config: &mut RPathConfig<'_>, libs: &[PathBuf]) -> Vec<String> {
8580
minimize_rpaths(&rpaths)
8681
}
8782

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

9287
fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> String {

compiler/rustc_codegen_ssa/src/back/rpath/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn test_minimize2() {
3535
fn test_rpath_relative() {
3636
if cfg!(target_os = "macos") {
3737
let config = &mut RPathConfig {
38-
used_crates: &[],
38+
libs: &[],
3939
has_rpath: true,
4040
is_like_osx: true,
4141
linker_is_gnu: false,
@@ -46,7 +46,7 @@ fn test_rpath_relative() {
4646
assert_eq!(res, "@loader_path/../lib");
4747
} else {
4848
let config = &mut RPathConfig {
49-
used_crates: &[],
49+
libs: &[],
5050
out_filename: PathBuf::from("bin/rustc"),
5151
get_install_prefix_lib_path: &mut || panic!(),
5252
has_rpath: true,

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1919
use rustc_hir::lang_items::LangItem;
2020
use rustc_index::vec::Idx;
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
22-
use rustc_middle::middle::cstore::EncodedMetadata;
23-
use rustc_middle::middle::cstore::{self, LinkagePreference};
22+
use rustc_middle::middle::cstore::{self, EncodedMetadata};
2423
use rustc_middle::middle::lang_items;
2524
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
2625
use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
@@ -779,8 +778,7 @@ impl CrateInfo {
779778
native_libraries: Default::default(),
780779
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
781780
crate_name: Default::default(),
782-
used_crates_dynamic: cstore::used_crates(tcx, LinkagePreference::RequireDynamic),
783-
used_crates_static: cstore::used_crates(tcx, LinkagePreference::RequireStatic),
781+
used_crates: cstore::used_crates(tcx),
784782
used_crate_source: Default::default(),
785783
lang_item_to_crate: Default::default(),
786784
missing_lang_items: Default::default(),

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_data_structures::sync::Lrc;
2424
use rustc_hir::def_id::CrateNum;
2525
use rustc_hir::LangItem;
2626
use rustc_middle::dep_graph::WorkProduct;
27-
use rustc_middle::middle::cstore::{self, CrateSource, LibSource};
27+
use rustc_middle::middle::cstore::{self, CrateSource};
2828
use rustc_middle::middle::dependency_format::Dependencies;
2929
use rustc_middle::ty::query::Providers;
3030
use rustc_session::config::{OutputFilenames, OutputType, RUST_CGU_EXT};
@@ -144,8 +144,7 @@ pub struct CrateInfo {
144144
pub crate_name: FxHashMap<CrateNum, String>,
145145
pub used_libraries: Vec<NativeLib>,
146146
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
147-
pub used_crates_static: Vec<(CrateNum, LibSource)>,
148-
pub used_crates_dynamic: Vec<(CrateNum, LibSource)>,
147+
pub used_crates: Vec<CrateNum>,
149148
pub lang_item_to_crate: FxHashMap<LangItem, CrateNum>,
150149
pub missing_lang_items: FxHashMap<CrateNum, Vec<LangItem>>,
151150
pub dependency_formats: Lrc<Dependencies>,

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ use crate::creader::CStore;
5555

5656
use rustc_data_structures::fx::FxHashMap;
5757
use rustc_hir::def_id::CrateNum;
58+
use rustc_middle::middle::cstore::CrateDepKind;
5859
use rustc_middle::middle::cstore::LinkagePreference::{self, RequireDynamic, RequireStatic};
59-
use rustc_middle::middle::cstore::{self, CrateDepKind};
6060
use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage};
6161
use rustc_middle::ty::TyCtxt;
6262
use rustc_session::config::CrateType;
@@ -274,8 +274,18 @@ fn add_library(
274274
}
275275

276276
fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
277-
let crates = cstore::used_crates(tcx, RequireStatic);
278-
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) {
277+
let all_crates_available_as_rlib = tcx
278+
.crates(())
279+
.iter()
280+
.cloned()
281+
.filter_map(|cnum| {
282+
if tcx.dep_kind(cnum).macros_only() {
283+
return None;
284+
}
285+
Some(tcx.used_crate_source(cnum).rlib.is_some())
286+
})
287+
.all(|is_rlib| is_rlib);
288+
if !all_crates_available_as_rlib {
279289
return None;
280290
}
281291

compiler/rustc_middle/src/middle/cstore.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,6 @@ impl CrateDepKind {
6161
}
6262
}
6363

64-
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
65-
pub enum LibSource {
66-
Some(PathBuf),
67-
MetadataOnly,
68-
None,
69-
}
70-
71-
impl LibSource {
72-
pub fn is_some(&self) -> bool {
73-
matches!(self, LibSource::Some(_))
74-
}
75-
76-
pub fn option(&self) -> Option<PathBuf> {
77-
match *self {
78-
LibSource::Some(ref p) => Some(p.clone()),
79-
LibSource::MetadataOnly | LibSource::None => None,
80-
}
81-
}
82-
}
83-
8464
#[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable)]
8565
pub enum LinkagePreference {
8666
RequireDynamic,
@@ -230,7 +210,7 @@ pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
230210
// In order to get this left-to-right dependency ordering, we perform a
231211
// topological sort of all crates putting the leaves at the right-most
232212
// positions.
233-
pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
213+
pub fn used_crates(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
234214
let mut libs = tcx
235215
.crates(())
236216
.iter()
@@ -239,26 +219,11 @@ pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum,
239219
if tcx.dep_kind(cnum).macros_only() {
240220
return None;
241221
}
242-
let source = tcx.used_crate_source(cnum);
243-
let path = match prefer {
244-
LinkagePreference::RequireDynamic => source.dylib.clone().map(|p| p.0),
245-
LinkagePreference::RequireStatic => source.rlib.clone().map(|p| p.0),
246-
};
247-
let path = match path {
248-
Some(p) => LibSource::Some(p),
249-
None => {
250-
if source.rmeta.is_some() {
251-
LibSource::MetadataOnly
252-
} else {
253-
LibSource::None
254-
}
255-
}
256-
};
257-
Some((cnum, path))
222+
Some(cnum)
258223
})
259224
.collect::<Vec<_>>();
260225
let mut ordering = tcx.postorder_cnums(()).to_owned();
261226
ordering.reverse();
262-
libs.sort_by_cached_key(|&(a, _)| ordering.iter().position(|x| *x == a));
227+
libs.sort_by_cached_key(|&a| ordering.iter().position(|x| *x == a));
263228
libs
264229
}

0 commit comments

Comments
 (0)