Skip to content

Commit 0402b46

Browse files
committed
Implement -L builtin:$path
1 parent d3d145e commit 0402b46

File tree

4 files changed

+71
-31
lines changed

4 files changed

+71
-31
lines changed

compiler/rustc_interface/src/tests.rs

+32-23
Original file line numberDiff line numberDiff line change
@@ -308,30 +308,39 @@ fn test_search_paths_tracking_hash_different_order() {
308308
json_rendered: HumanReadableErrorType::Default(ColorConfig::Never),
309309
};
310310

311+
let push = |opts: &mut Options, search_path| {
312+
opts.search_paths.push(SearchPath::from_cli_opt(
313+
None,
314+
&opts.target_triple,
315+
&early_dcx,
316+
search_path,
317+
));
318+
};
319+
311320
// Reference
312-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
313-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
314-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
315-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
316-
v1.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
317-
318-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
319-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
320-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
321-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
322-
v2.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
323-
324-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
325-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
326-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
327-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
328-
v3.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
329-
330-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "all=mno"));
331-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "native=abc"));
332-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "crate=def"));
333-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "dependency=ghi"));
334-
v4.search_paths.push(SearchPath::from_cli_opt(&early_dcx, "framework=jkl"));
321+
push(&mut v1, "native=abc");
322+
push(&mut v1, "crate=def");
323+
push(&mut v1, "dependency=ghi");
324+
push(&mut v1, "framework=jkl");
325+
push(&mut v1, "all=mno");
326+
327+
push(&mut v2, "native=abc");
328+
push(&mut v2, "dependency=ghi");
329+
push(&mut v2, "crate=def");
330+
push(&mut v2, "framework=jkl");
331+
push(&mut v2, "all=mno");
332+
333+
push(&mut v3, "crate=def");
334+
push(&mut v3, "framework=jkl");
335+
push(&mut v3, "native=abc");
336+
push(&mut v3, "dependency=ghi");
337+
push(&mut v3, "all=mno");
338+
339+
push(&mut v4, "all=mno");
340+
push(&mut v4, "native=abc");
341+
push(&mut v4, "crate=def");
342+
push(&mut v4, "dependency=ghi");
343+
push(&mut v4, "framework=jkl");
335344

336345
assert_same_hash(&v1, &v2);
337346
assert_same_hash(&v1, &v3);

compiler/rustc_session/src/config.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2830,11 +2830,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
28302830
let debuginfo = select_debuginfo(matches, &cg);
28312831
let debuginfo_compression = unstable_opts.debuginfo_compression;
28322832

2833-
let mut search_paths = vec![];
2834-
for s in &matches.opt_strs("L") {
2835-
search_paths.push(SearchPath::from_cli_opt(early_dcx, s));
2836-
}
2837-
28382833
let libs = parse_libs(early_dcx, matches);
28392834

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

2889+
let mut search_paths = vec![];
2890+
for s in &matches.opt_strs("L") {
2891+
search_paths.push(SearchPath::from_cli_opt(Some(&sysroot), &target_triple, early_dcx, s));
2892+
}
2893+
28942894
let working_dir = std::env::current_dir().unwrap_or_else(|e| {
28952895
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
28962896
});

compiler/rustc_session/src/search_paths.rs

+28-1
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,6 @@ impl Options {
456456
&matches.free[0]
457457
});
458458

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

621619
let target = parse_target_triple(early_dcx, matches);
622620

621+
let libs = matches
622+
.opt_strs("L")
623+
.iter()
624+
.map(|s| SearchPath::from_cli_opt(None, &target, early_dcx, s))
625+
.collect();
626+
623627
let show_coverage = matches.opt_present("show-coverage");
624628

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

0 commit comments

Comments
 (0)