Skip to content

Commit

Permalink
rustc: Simplify getting sysroot library directory
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Aug 22, 2024
1 parent 0f6e1ae commit f62b9e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 45 deletions.
27 changes: 11 additions & 16 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,11 +1317,9 @@ fn link_sanitizer_runtime(
name: &str,
) {
fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf {
let session_tlib =
filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple());
let path = session_tlib.join(filename);
let path = sess.target_tlib_path.dir.join(filename);
if path.exists() {
return session_tlib;
return sess.target_tlib_path.dir.clone();
} else {
let default_sysroot =
filesearch::get_or_default_sysroot().expect("Failed finding sysroot");
Expand Down Expand Up @@ -1612,19 +1610,18 @@ fn print_native_static_libs(
}

fn get_object_file_path(sess: &Session, name: &str, self_contained: bool) -> PathBuf {
let fs = sess.target_filesearch(PathKind::Native);
let file_path = fs.get_lib_path().join(name);
let file_path = sess.target_tlib_path.dir.join(name);
if file_path.exists() {
return file_path;
}
// Special directory with objects used only in self-contained linkage mode
if self_contained {
let file_path = fs.get_self_contained_lib_path().join(name);
let file_path = sess.target_tlib_path.dir.join("self-contained").join(name);
if file_path.exists() {
return file_path;
}
}
for search_path in fs.search_paths() {
for search_path in sess.target_filesearch(PathKind::Native).search_paths() {
let file_path = search_path.dir.join(name);
if file_path.exists() {
return file_path;
Expand Down Expand Up @@ -2131,7 +2128,7 @@ fn add_library_search_dirs(
| LinkSelfContainedComponents::UNWIND
| LinkSelfContainedComponents::MINGW,
) {
let lib_path = sess.target_filesearch(PathKind::Native).get_self_contained_lib_path();
let lib_path = sess.target_tlib_path.dir.join("self-contained");
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
}

Expand All @@ -2146,8 +2143,7 @@ fn add_library_search_dirs(
|| sess.target.os == "fuchsia"
|| sess.target.is_like_osx && !sess.opts.unstable_opts.sanitizer.is_empty()
{
let lib_path = sess.target_filesearch(PathKind::Native).get_lib_path();
cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
cmd.include_path(&fix_windows_verbatim_for_gcc(&sess.target_tlib_path.dir));
}

// Mac Catalyst uses the macOS SDK, but to link to iOS-specific frameworks
Expand Down Expand Up @@ -2859,15 +2855,14 @@ fn add_upstream_native_libraries(
//
// The returned path will always have `fix_windows_verbatim_for_gcc()` applied to it.
fn rehome_sysroot_lib_dir(sess: &Session, lib_dir: &Path) -> PathBuf {
let sysroot_lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
let sysroot_lib_path = &sess.target_tlib_path.dir;
let canonical_sysroot_lib_path =
{ try_canonicalize(&sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };
{ try_canonicalize(sysroot_lib_path).unwrap_or_else(|_| sysroot_lib_path.clone()) };

let canonical_lib_dir = try_canonicalize(lib_dir).unwrap_or_else(|_| lib_dir.to_path_buf());
if canonical_lib_dir == canonical_sysroot_lib_path {
// This path, returned by `target_filesearch().get_lib_path()`, has
// already had `fix_windows_verbatim_for_gcc()` applied if needed.
sysroot_lib_path
// This path already had `fix_windows_verbatim_for_gcc()` applied if needed.
sysroot_lib_path.clone()
} else {
fix_windows_verbatim_for_gcc(lib_dir)
}
Expand Down
16 changes: 1 addition & 15 deletions compiler/rustc_session/src/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ use std::{env, fs};

use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
use smallvec::{smallvec, SmallVec};
use tracing::debug;

use crate::search_paths::{PathKind, SearchPath};

#[derive(Clone)]
pub struct FileSearch<'a> {
sysroot: &'a Path,
triple: &'a str,
cli_search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath,
kind: PathKind,
Expand All @@ -32,23 +29,12 @@ impl<'a> FileSearch<'a> {
.chain(std::iter::once(self.tlib_path))
}

pub fn get_lib_path(&self) -> PathBuf {
make_target_lib_path(self.sysroot, self.triple)
}

pub fn get_self_contained_lib_path(&self) -> PathBuf {
self.get_lib_path().join("self-contained")
}

pub fn new(
sysroot: &'a Path,
triple: &'a str,
cli_search_paths: &'a [SearchPath],
tlib_path: &'a SearchPath,
kind: PathKind,
) -> FileSearch<'a> {
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
FileSearch { sysroot, triple, cli_search_paths, tlib_path, kind }
FileSearch { cli_search_paths, tlib_path, kind }
}
}

Expand Down
16 changes: 2 additions & 14 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,22 +439,10 @@ impl Session {
}

pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
filesearch::FileSearch::new(
&self.sysroot,
self.opts.target_triple.triple(),
&self.opts.search_paths,
&self.target_tlib_path,
kind,
)
filesearch::FileSearch::new(&self.opts.search_paths, &self.target_tlib_path, kind)
}
pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch<'_> {
filesearch::FileSearch::new(
&self.sysroot,
config::host_triple(),
&self.opts.search_paths,
&self.host_tlib_path,
kind,
)
filesearch::FileSearch::new(&self.opts.search_paths, &self.host_tlib_path, kind)
}

/// Returns a list of directories where target-specific tool binaries are located. Some fallback
Expand Down

0 comments on commit f62b9e0

Please sign in to comment.