1
Fork 0

Rollup merge of #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`
This commit is contained in:
Yuki Okushi 2021-09-17 14:09:47 +09:00 committed by GitHub
commit a84d39c7d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 48 deletions

View file

@ -637,7 +637,7 @@ fn link_dwarf_object<'a>(sess: &'a Session, executable_out_filename: &Path) {
cmd.arg("-o"); cmd.arg("-o");
cmd.arg(&dwp_out_filename); cmd.arg(&dwp_out_filename);
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths(false); let mut new_path = sess.get_tools_search_paths(false);
if let Some(path) = env::var_os("PATH") { if let Some(path) = env::var_os("PATH") {
new_path.extend(env::split_paths(&path)); new_path.extend(env::split_paths(&path));
} }
@ -2555,8 +2555,7 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
match ld_impl { match ld_impl {
LdImpl::Lld => { LdImpl::Lld => {
if sess.target.lld_flavor == LldFlavor::Ld64 { if sess.target.lld_flavor == LldFlavor::Ld64 {
let tools_path = let tools_path = sess.get_tools_search_paths(false);
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
let ld64_exe = tools_path let ld64_exe = tools_path
.into_iter() .into_iter()
.map(|p| p.join("gcc-ld")) .map(|p| p.join("gcc-ld"))
@ -2571,8 +2570,7 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
arg arg
}); });
} else { } else {
let tools_path = let tools_path = sess.get_tools_search_paths(false);
sess.host_filesearch(PathKind::All).get_tools_search_paths(false);
let lld_path = tools_path let lld_path = tools_path
.into_iter() .into_iter()
.map(|p| p.join("gcc-ld")) .map(|p| p.join("gcc-ld"))

View file

@ -15,7 +15,6 @@ use rustc_middle::middle::dependency_format::Linkage;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_serialize::{json, Encoder}; use rustc_serialize::{json, Encoder};
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip}; use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
use rustc_session::search_paths::PathKind;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use rustc_target::spec::{LinkOutputKind, LinkerFlavor, LldFlavor}; use rustc_target::spec::{LinkOutputKind, LinkerFlavor, LldFlavor};
@ -101,7 +100,7 @@ pub fn get_linker<'a>(
// The compiler's sysroot often has some bundled tools, so add it to the // The compiler's sysroot often has some bundled tools, so add it to the
// PATH for the child. // PATH for the child.
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths(self_contained); let mut new_path = sess.get_tools_search_paths(self_contained);
let mut msvc_changed_path = false; let mut msvc_changed_path = false;
if sess.target.is_like_msvc { if sess.target.is_like_msvc {
if let Some(ref tool) = msvc_tool { if let Some(ref tool) = msvc_tool {

View file

@ -677,10 +677,7 @@ impl RustcDefaultCalls {
println!("{}", targets.join("\n")); println!("{}", targets.join("\n"));
} }
Sysroot => println!("{}", sess.sysroot.display()), Sysroot => println!("{}", sess.sysroot.display()),
TargetLibdir => println!( TargetLibdir => println!("{}", sess.target_tlib_path.dir.display()),
"{}",
sess.target_tlib_path.as_ref().unwrap_or(&sess.host_tlib_path).dir.display()
),
TargetSpec => println!("{}", sess.target.to_json().pretty()), TargetSpec => println!("{}", sess.target.to_json().pretty()),
FileNames | CrateName => { FileNames | CrateName => {
let input = input.unwrap_or_else(|| { let input = input.unwrap_or_else(|| {

View file

@ -1,3 +1,5 @@
//! A module for searching for libraries
pub use self::FileMatch::*; pub use self::FileMatch::*;
use std::env; use std::env;
@ -14,8 +16,6 @@ pub enum FileMatch {
FileDoesntMatch, FileDoesntMatch,
} }
// A module for searching for libraries
#[derive(Clone)] #[derive(Clone)]
pub struct FileSearch<'a> { pub struct FileSearch<'a> {
sysroot: &'a Path, sysroot: &'a Path,
@ -83,22 +83,10 @@ impl<'a> FileSearch<'a> {
FileSearch { sysroot, triple, search_paths, tlib_path, kind } FileSearch { sysroot, triple, search_paths, tlib_path, kind }
} }
// Returns just the directories within the search paths. /// Returns just the directories within the search paths.
pub fn search_path_dirs(&self) -> Vec<PathBuf> { pub fn search_path_dirs(&self) -> Vec<PathBuf> {
self.search_paths().map(|sp| sp.dir.to_path_buf()).collect() self.search_paths().map(|sp| sp.dir.to_path_buf()).collect()
} }
// Returns a list of directories where target-specific tool binaries are located.
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
let rustlib_path = rustc_target::target_rustlib_path(self.sysroot, &self.triple);
let p = std::array::IntoIter::new([
Path::new(&self.sysroot),
Path::new(&rustlib_path),
Path::new("bin"),
])
.collect::<PathBuf>();
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
}
} }
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf { 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 {
.collect::<PathBuf>() .collect::<PathBuf>()
} }
// This function checks if sysroot is found using env::args().next(), and if it /// This function checks if sysroot is found using env::args().next(), and if it
// is not found, uses env::current_exe() to imply sysroot. /// is not found, uses env::current_exe() to imply sysroot.
pub fn get_or_default_sysroot() -> PathBuf { pub fn get_or_default_sysroot() -> PathBuf {
// Follow symlinks. If the resolved path is relative, make it absolute. // Follow symlinks. If the resolved path is relative, make it absolute.
fn canonicalize(path: PathBuf) -> PathBuf { fn canonicalize(path: PathBuf) -> PathBuf {

View file

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

View file

@ -36,7 +36,7 @@ use std::fmt;
use std::io::Write; use std::io::Write;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::ops::{Div, Mul}; use std::ops::{Div, Mul};
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -131,9 +131,8 @@ pub struct Session {
pub target: Target, pub target: Target,
pub host: Target, pub host: Target,
pub opts: config::Options, pub opts: config::Options,
pub host_tlib_path: SearchPath, pub host_tlib_path: Lrc<SearchPath>,
/// `None` if the host and target are the same. pub target_tlib_path: Lrc<SearchPath>,
pub target_tlib_path: Option<SearchPath>,
pub parse_sess: ParseSess, pub parse_sess: ParseSess,
pub sysroot: PathBuf, pub sysroot: PathBuf,
/// The name of the root source file of the crate, in the local file system. /// The name of the root source file of the crate, in the local file system.
@ -787,8 +786,7 @@ impl Session {
&self.sysroot, &self.sysroot,
self.opts.target_triple.triple(), self.opts.target_triple.triple(),
&self.opts.search_paths, &self.opts.search_paths,
// `target_tlib_path == None` means it's the same as `host_tlib_path`. &self.target_tlib_path,
self.target_tlib_path.as_ref().unwrap_or(&self.host_tlib_path),
kind, kind,
) )
} }
@ -802,6 +800,18 @@ impl Session {
) )
} }
/// Returns a list of directories where target-specific tool binaries are located.
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
let rustlib_path = rustc_target::target_rustlib_path(&self.sysroot, &config::host_triple());
let p = std::array::IntoIter::new([
Path::new(&self.sysroot),
Path::new(&rustlib_path),
Path::new("bin"),
])
.collect::<PathBuf>();
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
}
pub fn init_incr_comp_session( pub fn init_incr_comp_session(
&self, &self,
session_dir: PathBuf, session_dir: PathBuf,
@ -1245,11 +1255,13 @@ pub fn build_session(
let host_triple = config::host_triple(); let host_triple = config::host_triple();
let target_triple = sopts.target_triple.triple(); let target_triple = sopts.target_triple.triple();
let host_tlib_path = SearchPath::from_sysroot_and_triple(&sysroot, host_triple); let host_tlib_path = Lrc::new(SearchPath::from_sysroot_and_triple(&sysroot, host_triple));
let target_tlib_path = if host_triple == target_triple { let target_tlib_path = if host_triple == target_triple {
None // Use the same `SearchPath` if host and target triple are identical to avoid unnecessary
// rescanning of the target lib path and an unnecessary allocation.
host_tlib_path.clone()
} else { } else {
Some(SearchPath::from_sysroot_and_triple(&sysroot, target_triple)) Lrc::new(SearchPath::from_sysroot_and_triple(&sysroot, target_triple))
}; };
let file_path_mapping = sopts.file_path_mapping(); let file_path_mapping = sopts.file_path_mapping();