Skip to content

Commit 9154757

Browse files
committed
Implement -L builtin:$path
1 parent 35936c4 commit 9154757

File tree

4 files changed

+71
-31
lines changed

4 files changed

+71
-31
lines changed

compiler/rustc_interface/src/tests.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -315,30 +315,39 @@ fn test_search_paths_tracking_hash_different_order() {
315315
json_rendered: HumanReadableErrorType::Default(ColorConfig::Never),
316316
};
317317

318+
let push = |opts: &mut Options, search_path| {
319+
opts.search_paths.push(SearchPath::from_cli_opt(
320+
None,
321+
&opts.target_triple,
322+
&early_dcx,
323+
search_path,
324+
));
325+
};
326+
318327
// Reference
319-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
320-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
321-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
322-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
323-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
324-
325-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
326-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
327-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
328-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
329-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
330-
331-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
332-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
333-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
334-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
335-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
336-
337-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
338-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
339-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
340-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
341-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
328+
push(&mut v1, "native=abc");
329+
push(&mut v1, "crate=def");
330+
push(&mut v1, "dependency=ghi");
331+
push(&mut v1, "framework=jkl");
332+
push(&mut v1, "all=mno");
333+
334+
push(&mut v2, "native=abc");
335+
push(&mut v2, "dependency=ghi");
336+
push(&mut v2, "crate=def");
337+
push(&mut v2, "framework=jkl");
338+
push(&mut v2, "all=mno");
339+
340+
push(&mut v3, "crate=def");
341+
push(&mut v3, "framework=jkl");
342+
push(&mut v3, "native=abc");
343+
push(&mut v3, "dependency=ghi");
344+
push(&mut v3, "all=mno");
345+
346+
push(&mut v4, "all=mno");
347+
push(&mut v4, "native=abc");
348+
push(&mut v4, "crate=def");
349+
push(&mut v4, "dependency=ghi");
350+
push(&mut v4, "framework=jkl");
342351

343352
assert_same_hash(&v1, &v2);
344353
assert_same_hash(&v1, &v3);

compiler/rustc_session/src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,11 +2795,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27952795
let debuginfo = select_debuginfo(matches, &cg);
27962796
let debuginfo_compression = unstable_opts.debuginfo_compression;
27972797

2798-
let mut search_paths = vec![];
2799-
for s in &matches.opt_strs("L") {
2800-
search_paths.push(SearchPath::from_cli_opt(early_dcx, s));
2801-
}
2802-
28032798
let libs = parse_libs(early_dcx, matches);
28042799

28052800
let test = matches.opt_present("test");
@@ -2848,6 +2843,11 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
28482843
candidate.join("library/std/src/lib.rs").is_file().then_some(candidate)
28492844
};
28502845

2846+
let mut search_paths = vec![];
2847+
for s in &matches.opt_strs("L") {
2848+
search_paths.push(SearchPath::from_cli_opt(Some(&sysroot), &target_triple, early_dcx, s));
2849+
}
2850+
28512851
let working_dir = std::env::current_dir().unwrap_or_else(|e| {
28522852
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
28532853
});

compiler/rustc_session/src/search_paths.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::filesearch::make_target_lib_path;
22
use crate::EarlyDiagCtxt;
3+
use rustc_target::spec::TargetTriple;
34
use std::path::{Path, PathBuf};
45

56
#[derive(Clone, Debug)]
@@ -46,7 +47,12 @@ impl PathKind {
4647
}
4748

4849
impl SearchPath {
49-
pub fn from_cli_opt(early_dcx: &EarlyDiagCtxt, path: &str) -> Self {
50+
pub fn from_cli_opt(
51+
sysroot: Option<&Path>,
52+
triple: &TargetTriple,
53+
early_dcx: &EarlyDiagCtxt,
54+
path: &str,
55+
) -> Self {
5056
let (kind, path) = if let Some(stripped) = path.strip_prefix("native=") {
5157
(PathKind::Native, stripped)
5258
} else if let Some(stripped) = path.strip_prefix("crate=") {
@@ -57,6 +63,27 @@ impl SearchPath {
5763
(PathKind::Framework, stripped)
5864
} else if let Some(stripped) = path.strip_prefix("all=") {
5965
(PathKind::All, stripped)
66+
} else if let Some(stripped) = path.strip_prefix("builtin:") {
67+
let Some(sysroot) = sysroot else {
68+
early_dcx.early_fatal("`-L builtin:` is not supported without a sysroot present");
69+
};
70+
let triple = match triple {
71+
TargetTriple::TargetTriple(triple) => triple,
72+
TargetTriple::TargetJson { .. } => {
73+
early_dcx.early_fatal("`-L builtin:` is not supported with custom targets");
74+
}
75+
};
76+
77+
if stripped.contains(std::path::is_separator) {
78+
early_dcx.early_fatal("`-L builtin:` does not accept paths");
79+
}
80+
81+
let path = make_target_lib_path(sysroot, triple).join("builtin").join(stripped);
82+
if !path.is_dir() {
83+
early_dcx.early_fatal(format!("builtin:{stripped} does not exist"));
84+
}
85+
86+
return Self::new(PathKind::All, path);
6087
} else {
6188
(PathKind::All, path)
6289
};

src/librustdoc/config.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,6 @@ impl Options {
460460
&matches.free[0]
461461
});
462462

463-
let libs =
464-
matches.opt_strs("L").iter().map(|s| SearchPath::from_cli_opt(early_dcx, s)).collect();
465463
let externs = parse_externs(early_dcx, matches, &unstable_opts);
466464
let extern_html_root_urls = match parse_extern_html_roots(matches) {
467465
Ok(ex) => ex,
@@ -626,6 +624,12 @@ impl Options {
626624

627625
let target = parse_target_triple(early_dcx, matches);
628626

627+
let libs = matches
628+
.opt_strs("L")
629+
.iter()
630+
.map(|s| SearchPath::from_cli_opt(None, &target, early_dcx, s))
631+
.collect();
632+
629633
let show_coverage = matches.opt_present("show-coverage");
630634

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

0 commit comments

Comments
 (0)