Skip to content

Commit 30abdea

Browse files
authored
Significantly simplify sysroot configuration (#342)
1 parent 9d74dba commit 30abdea

File tree

2 files changed

+6
-122
lines changed

2 files changed

+6
-122
lines changed

plrustc/README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,3 @@ The use of `RUSTC_BOOTSTRAP` here is unfortunate, but at the moment things are t
3333

3434
Similar to `rustc`, `plrustc` is usually not invoked directly, but instead through `cargo`.
3535

36-
## Details
37-
38-
Some additional details are provided for users who intend to run PL/Rust and plrustc under restricted environments via seccomp and/or SELinux. These details are subject to change, although if that occurs it will be noted in the changelog.
39-
40-
### Sysroot configuration
41-
42-
To locate the Rust sysroot (which should have the installation of `postgrestd`), the following algorithm is used. It is very similar to the algorithm used by clippy, miri, etc. We stop at the first of these that provides a value.
43-
44-
1. If a `--sysroot` argument is provided via normal program arguments, then that value is used.
45-
2. The runtime environment is checked.
46-
1. First, for `PLRUSTC_SYSROOT` and `SYSROOT` in that order of preference.
47-
2. Then, for rustup: If both `RUSTUP_HOME` and `RUSTUP_TOOLCHAIN` are set, then we will use the path `$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN` as the sysroot.
48-
3. If `rustc` is on the path, then `rustc --print sysroot` is invoked and that value is used.
49-
4. The compile-time environment is checked.
50-
1. First, for `PLRUSTC_SYSROOT` and `SYSROOT` in that order of preference.
51-
2. Then, for rustup, if both `RUSTUP_HOME` and `RUSTUP_TOOLCHAIN` are set in the environment at runtime, then we will use the path `$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN` as the sysroot.
52-
5. If none of these were successful, an error is emitted and compilation will fail.
53-
54-
It's likely that a future version of plrustc will refine this to allow more control. In the short term this is impossible, howsever.

plrustc/plrustc/src/main.rs

Lines changed: 6 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use rustc_session::early_error;
1919
use rustc_session::parse::ParseSess;
2020
use rustc_span::source_map::FileLoader;
2121
use rustc_span::Symbol;
22-
use std::ffi::OsStr;
23-
use std::path::{Path, PathBuf};
22+
use std::path::Path;
2423

2524
const PLRUSTC_USER_CRATE_NAME: &str = "PLRUSTC_USER_CRATE_NAME";
2625
const PLRUSTC_USER_CRATE_ALLOWED_SOURCE_PATHS: &str = "PLRUSTC_USER_CRATE_ALLOWED_SOURCE_PATHS";
@@ -52,6 +51,8 @@ impl Callbacks for PlrustcCallbacks {
5251
}
5352
}
5453

54+
// TODO: eventually we can replace this with:
55+
// rustc_driver::install_ice_hook("https://github.com/tcdi/plrust/issues/new", |_| ());
5556
fn install_ice_hook() {
5657
fn report_plrustc_ice(info: &std::panic::PanicInfo<'_>, bug_report_url: &str) {
5758
// Invoke the default panic handler to print the message and (possibly) a back trace
@@ -108,45 +109,14 @@ fn main() {
108109
install_ice_hook();
109110
rustc_driver::init_rustc_env_logger();
110111
std::process::exit(rustc_driver::catch_with_exit_code(move || {
111-
let orig_args: Vec<String> = std::env::args().collect();
112-
let orig_args = rustc_driver::args::arg_expand_all(&orig_args);
113-
114-
let sysroot_arg = arg_value(&orig_args, "--sysroot");
115-
let have_sysroot_arg = sysroot_arg.is_some();
116-
let sysroot = sysroot_arg
117-
.map(ToString::to_string)
118-
.or_else(|| sysroot().map(|p| p.display().to_string()))
119-
.expect("Failed to find sysroot");
120-
121-
let mut args: Vec<String> = orig_args.clone();
122-
123-
if !have_sysroot_arg {
124-
args.extend(["--sysroot".to_string(), sysroot.to_string()]);
125-
}
126-
127-
let our_exe_filename = std::env::current_exe()
128-
.ok()
129-
.and_then(|p| p.file_stem().map(ToOwned::to_owned))
130-
.unwrap_or_else(|| "plrustc".into());
131-
132-
let wrapper_mode = orig_args
133-
.get(1)
134-
.map(std::path::Path::new)
135-
.and_then(std::path::Path::file_stem)
136-
.map_or(false, |name| {
137-
name == our_exe_filename || name == "plrustc" || name == "rustc"
138-
});
139-
140-
if wrapper_mode {
141-
args.remove(1);
142-
}
143-
112+
let args = rustc_driver::args::arg_expand_all(&std::env::args().collect::<Vec<_>>());
113+
let config = PlrustcConfig::from_env_and_args(&args);
144114
run_compiler(
145115
args,
146116
&mut PlrustcCallbacks {
147117
// FIXME SOMEDAY: check caplints?
148118
lints_enabled: true,
149-
config: PlrustcConfig::from_env_and_args(&orig_args),
119+
config,
150120
},
151121
);
152122
}))
@@ -332,73 +302,6 @@ impl FileLoader for PlrustcFileLoader {
332302
}
333303
}
334304

335-
/// Get the sysroot, looking from most specific to this invocation to the
336-
/// least.
337-
///
338-
/// - command line `--sysroot` arg (happens in caller)
339-
///
340-
/// - runtime environment
341-
/// - PLRUSTC_SYSROOT
342-
/// - SYSROOT
343-
/// - RUSTUP_HOME, RUSTUP_TOOLCHAIN
344-
///
345-
/// - sysroot from rustc in the path
346-
///
347-
/// - compile-time environment
348-
/// - PLRUSTC_SYSROOT
349-
/// - SYSROOT
350-
/// - RUSTUP_HOME, RUSTUP_TOOLCHAIN
351-
fn sysroot() -> Option<PathBuf> {
352-
fn rustup_sysroot<H: ?Sized + AsRef<OsStr>, T: ?Sized + AsRef<Path>>(
353-
home: &H,
354-
toolchain: &T,
355-
) -> PathBuf {
356-
let mut path = PathBuf::from(home);
357-
path.push("toolchains");
358-
path.push(toolchain);
359-
path
360-
}
361-
fn runtime_rustup_sysroot() -> Option<PathBuf> {
362-
let home = std::env::var_os("RUSTUP_HOME")?;
363-
let toolchain = std::env::var_os("RUSTUP_TOOLCHAIN")?;
364-
Some(rustup_sysroot(&home, &toolchain))
365-
}
366-
fn compiletime_rustup_sysroot() -> Option<PathBuf> {
367-
let home: &str = option_env!("RUSTUP_HOME")?;
368-
let toolchain: &str = option_env!("RUSTUP_TOOLCHAIN")?;
369-
Some(rustup_sysroot(&home, &toolchain))
370-
}
371-
fn rustc_on_path_sysroot() -> Option<PathBuf> {
372-
std::process::Command::new("rustc")
373-
.arg("--print=sysroot")
374-
.output()
375-
.ok()
376-
.and_then(|out| String::from_utf8(out.stdout).ok())
377-
.map(|s| PathBuf::from(s.trim()))
378-
}
379-
fn runtime_explicit_env() -> Option<PathBuf> {
380-
let sysroot =
381-
std::env::var_os("PLRUSTC_SYSROOT").or_else(|| std::env::var_os("SYSROOT"))?;
382-
Some(PathBuf::from(sysroot))
383-
}
384-
fn compiletime_explicit_env() -> Option<PathBuf> {
385-
let plrustc_sysroot: Option<&str> = option_env!("PLRUSTC_SYSROOT");
386-
if let Some(plrustc_sysroot) = plrustc_sysroot {
387-
return Some(plrustc_sysroot.into());
388-
}
389-
let sysroot: Option<&str> = option_env!("SYSROOT");
390-
if let Some(sysroot) = sysroot {
391-
return Some(sysroot.into());
392-
}
393-
None
394-
}
395-
runtime_explicit_env()
396-
.or_else(runtime_rustup_sysroot)
397-
.or_else(rustc_on_path_sysroot)
398-
.or_else(compiletime_explicit_env)
399-
.or_else(compiletime_rustup_sysroot)
400-
}
401-
402305
fn run_compiler(mut args: Vec<String>, callbacks: &mut PlrustcCallbacks) -> ! {
403306
args.splice(1..1, std::iter::once("--cfg=plrustc".to_string()));
404307

0 commit comments

Comments
 (0)