Skip to content

Commit

Permalink
Implement -L builtin:$path
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Mar 1, 2024
1 parent d3d145e commit 726c960
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 34 deletions.
55 changes: 32 additions & 23 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,30 +308,39 @@ fn test_search_paths_tracking_hash_different_order() {
json_rendered: HumanReadableErrorType::Default(ColorConfig::Never),
};

let push = |opts: &mut Options, search_path| {
opts.search_paths.push(SearchPath::from_cli_opt(
None,
&opts.target_triple,
&early_dcx,
search_path,
));
};

// Reference
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));

v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));

v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));

v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
push(&mut v1, "native=abc");
push(&mut v1, "crate=def");
push(&mut v1, "dependency=ghi");
push(&mut v1, "framework=jkl");
push(&mut v1, "all=mno");

push(&mut v2, "native=abc");
push(&mut v2, "dependency=ghi");
push(&mut v2, "crate=def");
push(&mut v2, "framework=jkl");
push(&mut v2, "all=mno");

push(&mut v3, "crate=def");
push(&mut v3, "framework=jkl");
push(&mut v3, "native=abc");
push(&mut v3, "dependency=ghi");
push(&mut v3, "all=mno");

push(&mut v4, "all=mno");
push(&mut v4, "native=abc");
push(&mut v4, "crate=def");
push(&mut v4, "dependency=ghi");
push(&mut v4, "framework=jkl");

assert_same_hash(&v1, &v2);
assert_same_hash(&v1, &v3);
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2830,11 +2830,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
let debuginfo = select_debuginfo(matches, &cg);
let debuginfo_compression = unstable_opts.debuginfo_compression;

let mut search_paths = vec![];
for s in &matches.opt_strs("L") {
search_paths.push(SearchPath::from_cli_opt(early_dcx, s));
}

let libs = parse_libs(early_dcx, matches);

let test = matches.opt_present("test");
Expand Down Expand Up @@ -2891,6 +2886,11 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
candidate.join("library/std/src/lib.rs").is_file().then_some(candidate)
};

let mut search_paths = vec![];
for s in &matches.opt_strs("L") {
search_paths.push(SearchPath::from_cli_opt(Some(&sysroot), &target_triple, early_dcx, s));
}

let working_dir = std::env::current_dir().unwrap_or_else(|e| {
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
});
Expand Down
36 changes: 32 additions & 4 deletions compiler/rustc_session/src/search_paths.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::filesearch::make_target_lib_path;
use crate::EarlyDiagCtxt;
use rustc_target::spec::TargetTriple;
use std::path::{Path, PathBuf};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -46,7 +47,16 @@ impl PathKind {
}

impl SearchPath {
pub fn from_cli_opt(early_dcx: &EarlyDiagCtxt, path: &str) -> Self {
pub fn from_cli_opt(
sysroot: Option<&Path>,
triple: &TargetTriple,
early_dcx: &EarlyDiagCtxt,
path: &str,
) -> Self {
if path.is_empty() {
early_dcx.early_fatal("empty search path given via `-L`");
}

let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
(PathKind::Native, stripped)
} else if let Some(stripped) = path.strip_prefix("crate=") {
Expand All @@ -57,12 +67,30 @@ impl SearchPath {
(PathKind::Framework, stripped)
} else if let Some(stripped) = path.strip_prefix("all=") {
(PathKind::All, stripped)
} else if let Some(stripped) = path.strip_prefix("builtin:") {
let Some(sysroot) = sysroot else {
early_dcx.early_fatal("`-L builtin:` is not supported without a sysroot present");
};
let triple = match triple {
TargetTriple::TargetTriple(triple) => triple,
TargetTriple::TargetJson { .. } => {
early_dcx.early_fatal("`-L builtin:` is not supported with custom targets");
}
};

if stripped.contains(std::path::is_separator) {
early_dcx.early_fatal("`-L builtin:` does not accept paths");
}

let path = make_target_lib_path(sysroot, triple).join("builtin").join(stripped);
if !path.is_dir() {
early_dcx.early_fatal(format!("builtin:{stripped} does not exist"));
}

return Self::new(PathKind::All, path);
} else {
(PathKind::All, path)
};
if path.is_empty() {
early_dcx.early_fatal("empty search path given via `-L`");
}

let dir = PathBuf::from(path);
Self::new(kind, dir)
Expand Down
8 changes: 6 additions & 2 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ impl Options {
&matches.free[0]
});

let libs =
matches.opt_strs("L").iter().map(|s| SearchPath::from_cli_opt(early_dcx, s)).collect();
let externs = parse_externs(early_dcx, matches, &unstable_opts);
let extern_html_root_urls = match parse_extern_html_roots(matches) {
Ok(ex) => ex,
Expand Down Expand Up @@ -620,6 +618,12 @@ impl Options {

let target = parse_target_triple(early_dcx, matches);

let libs = matches
.opt_strs("L")
.iter()
.map(|s| SearchPath::from_cli_opt(None, &target, early_dcx, s))
.collect();

let show_coverage = matches.opt_present("show-coverage");

let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
Expand Down

0 comments on commit 726c960

Please sign in to comment.