Skip to content

Commit e21b367

Browse files
authored
Rollup merge of rust-lang#88751 - bjorn3:move_filesearch, r=oli-obk
Couple of changes to FileSearch and SearchPath * Turn a couple of regular comments into doc comments * Move `get_tools_search_paths` from `FileSearch` to `Session` * Use Lrc instead of Option to avoid duplication of a `SearchPath`
2 parents 8684a4d + d7ef0b3 commit e21b367

File tree

6 files changed

+42
-48
lines changed

6 files changed

+42
-48
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ fn link_dwarf_object<'a>(sess: &'a Session, executable_out_filename: &Path) {
637637
cmd.arg("-o");
638638
cmd.arg(&dwp_out_filename);
639639

640-
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
640+
let mut new_path = sess.get_tools_search_paths(false);
641641
if let Some(path) = env::var_os("PATH") {
642642
new_path.extend(env::split_paths(&path));
643643
}
@@ -2555,8 +2555,7 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
25552555
match ld_impl {
25562556
LdImpl::Lld => {
25572557
if sess.target.lld_flavor == LldFlavor::Ld64 {
2558-
let tools_path =
2559-
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
2558+
let tools_path = sess.get_tools_search_paths(false);
25602559
let ld64_exe = tools_path
25612560
.into_iter()
25622561
.map(|p| p.join("gcc-ld"))
@@ -2571,8 +2570,7 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
25712570
arg
25722571
});
25732572
} else {
2574-
let tools_path =
2575-
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
2573+
let tools_path = sess.get_tools_search_paths(false);
25762574
let lld_path = tools_path
25772575
.into_iter()
25782576
.map(|p| p.join("gcc-ld"))

compiler/rustc_codegen_ssa/src/back/linker.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_middle::middle::dependency_format::Linkage;
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_serialize::{json, Encoder};
1717
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
18-
use rustc_session::search_paths::PathKind;
1918
use rustc_session::Session;
2019
use rustc_span::symbol::Symbol;
2120
use rustc_target::spec::{LinkOutputKind, LinkerFlavor, LldFlavor};
@@ -101,7 +100,7 @@ pub fn get_linker<'a>(
101100

102101
// The compiler's sysroot often has some bundled tools, so add it to the
103102
// PATH for the child.
104-
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths(self_contained);
103+
let mut new_path = sess.get_tools_search_paths(self_contained);
105104
let mut msvc_changed_path = false;
106105
if sess.target.is_like_msvc {
107106
if let Some(ref tool) = msvc_tool {

compiler/rustc_driver/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -677,10 +677,7 @@ impl RustcDefaultCalls {
677677
println!("{}", targets.join("\n"));
678678
}
679679
Sysroot => println!("{}", sess.sysroot.display()),
680-
TargetLibdir => println!(
681-
"{}",
682-
sess.target_tlib_path.as_ref().unwrap_or(&sess.host_tlib_path).dir.display()
683-
),
680+
TargetLibdir => println!("{}", sess.target_tlib_path.dir.display()),
684681
TargetSpec => println!("{}", sess.target.to_json().pretty()),
685682
FileNames | CrateName => {
686683
let input = input.unwrap_or_else(|| {

compiler/rustc_session/src/filesearch.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! A module for searching for libraries
2+
13
pub use self::FileMatch::*;
24

35
use std::env;
@@ -14,8 +16,6 @@ pub enum FileMatch {
1416
FileDoesntMatch,
1517
}
1618

17-
// A module for searching for libraries
18-
1919
#[derive(Clone)]
2020
pub struct FileSearch<'a> {
2121
sysroot: &'a Path,
@@ -83,22 +83,10 @@ impl<'a> FileSearch<'a> {
8383
FileSearch { sysroot, triple, search_paths, tlib_path, kind }
8484
}
8585

86-
// Returns just the directories within the search paths.
86+
/// Returns just the directories within the search paths.
8787
pub fn search_path_dirs(&self) -> Vec<PathBuf> {
8888
self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
8989
}
90-
91-
// Returns a list of directories where target-specific tool binaries are located.
92-
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
93-
let rustlib_path = rustc_target::target_rustlib_path(self.sysroot, &self.triple);
94-
let p = std::array::IntoIter::new([
95-
Path::new(&self.sysroot),
96-
Path::new(&rustlib_path),
97-
Path::new("bin"),
98-
])
99-
.collect::<PathBuf>();
100-
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
101-
}
10290
}
10391

10492
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
@@ -107,8 +95,8 @@ pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
10795
.collect::<PathBuf>()
10896
}
10997

110-
// This function checks if sysroot is found using env::args().next(), and if it
111-
// is not found, uses env::current_exe() to imply sysroot.
98+
/// This function checks if sysroot is found using env::args().next(), and if it
99+
/// is not found, uses env::current_exe() to imply sysroot.
112100
pub fn get_or_default_sysroot() -> PathBuf {
113101
// Follow symlinks. If the resolved path is relative, make it absolute.
114102
fn canonicalize(path: PathBuf) -> PathBuf {

compiler/rustc_session/src/search_paths.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ pub struct SearchPath {
99
pub files: Vec<SearchPathFile>,
1010
}
1111

12-
// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
13-
// it is searched repeatedly by `find_library_crate`, and the searches involve
14-
// checking the prefix and suffix of the filename of each `PathBuf`. This is
15-
// doable, but very slow, because it involves calls to `file_name` and
16-
// `extension` that are themselves slow.
17-
//
18-
// This type augments the `PathBuf` with an `Option<String>` containing the
19-
// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
20-
// `Option<String>` than the `PathBuf`. (It's an `Option` because
21-
// `Path::file_name` can fail; if that happens then all subsequent checking
22-
// will also fail, which is fine.)
12+
/// The obvious implementation of `SearchPath::files` is a `Vec<PathBuf>`. But
13+
/// it is searched repeatedly by `find_library_crate`, and the searches involve
14+
/// checking the prefix and suffix of the filename of each `PathBuf`. This is
15+
/// doable, but very slow, because it involves calls to `file_name` and
16+
/// `extension` that are themselves slow.
17+
///
18+
/// This type augments the `PathBuf` with an `Option<String>` containing the
19+
/// `PathBuf`'s filename. The prefix and suffix checking is much faster on the
20+
/// `Option<String>` than the `PathBuf`. (It's an `Option` because
21+
/// `Path::file_name` can fail; if that happens then all subsequent checking
22+
/// will also fail, which is fine.)
2323
#[derive(Clone, Debug)]
2424
pub struct SearchPathFile {
2525
pub path: PathBuf,

compiler/rustc_session/src/session.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::fmt;
3636
use std::io::Write;
3737
use std::num::NonZeroU32;
3838
use std::ops::{Div, Mul};
39-
use std::path::PathBuf;
39+
use std::path::{Path, PathBuf};
4040
use std::str::FromStr;
4141
use std::sync::Arc;
4242
use std::time::Duration;
@@ -131,9 +131,8 @@ pub struct Session {
131131
pub target: Target,
132132
pub host: Target,
133133
pub opts: config::Options,
134-
pub host_tlib_path: SearchPath,
135-
/// `None` if the host and target are the same.
136-
pub target_tlib_path: Option<SearchPath>,
134+
pub host_tlib_path: Lrc<SearchPath>,
135+
pub target_tlib_path: Lrc<SearchPath>,
137136
pub parse_sess: ParseSess,
138137
pub sysroot: PathBuf,
139138
/// The name of the root source file of the crate, in the local file system.
@@ -787,8 +786,7 @@ impl Session {
787786
&self.sysroot,
788787
self.opts.target_triple.triple(),
789788
&self.opts.search_paths,
790-
// `target_tlib_path == None` means it's the same as `host_tlib_path`.
791-
self.target_tlib_path.as_ref().unwrap_or(&self.host_tlib_path),
789+
&self.target_tlib_path,
792790
kind,
793791
)
794792
}
@@ -802,6 +800,18 @@ impl Session {
802800
)
803801
}
804802

803+
/// Returns a list of directories where target-specific tool binaries are located.
804+
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
805+
let rustlib_path = rustc_target::target_rustlib_path(&self.sysroot, &config::host_triple());
806+
let p = std::array::IntoIter::new([
807+
Path::new(&self.sysroot),
808+
Path::new(&rustlib_path),
809+
Path::new("bin"),
810+
])
811+
.collect::<PathBuf>();
812+
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
813+
}
814+
805815
pub fn init_incr_comp_session(
806816
&self,
807817
session_dir: PathBuf,
@@ -1245,11 +1255,13 @@ pub fn build_session(
12451255

12461256
let host_triple = config::host_triple();
12471257
let target_triple = sopts.target_triple.triple();
1248-
let host_tlib_path = SearchPath::from_sysroot_and_triple(&sysroot, host_triple);
1258+
let host_tlib_path = Lrc::new(SearchPath::from_sysroot_and_triple(&sysroot, host_triple));
12491259
let target_tlib_path = if host_triple == target_triple {
1250-
None
1260+
// Use the same `SearchPath` if host and target triple are identical to avoid unnecessary
1261+
// rescanning of the target lib path and an unnecessary allocation.
1262+
host_tlib_path.clone()
12511263
} else {
1252-
Some(SearchPath::from_sysroot_and_triple(&sysroot, target_triple))
1264+
Lrc::new(SearchPath::from_sysroot_and_triple(&sysroot, target_triple))
12531265
};
12541266

12551267
let file_path_mapping = sopts.file_path_mapping();

0 commit comments

Comments
 (0)