Skip to content

Greatly simplify the driver #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 3 additions & 84 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use rustc_driver::Callbacks;
use rustc_hir::def_id::LocalDefId;
use rustc_interface::interface;
use rustc_middle::mir::BorrowCheckResult;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::query::{ExternProviders, Providers};
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

struct UbrustcCallbacks;

Expand All @@ -37,93 +35,14 @@ fn not_a_borrowchecker(cx: TyCtxt<'_>, _: LocalDefId) -> &'_ BorrowCheckResult<'
}

fn main() {
rustc_driver::install_ice_hook();
rustc_driver::install_ice_hook("https://github.com/thomcc/ubrustc/issues/new", |_| ());
rustc_driver::init_rustc_env_logger();
std::process::exit(rustc_driver::catch_with_exit_code(move || {
let orig_args: Vec<String> = std::env::args().collect();

let sysroot_arg = arg_value(&orig_args, "--sysroot");
let have_sysroot_arg = sysroot_arg.is_some();
let sysroot = sysroot_arg
.map(ToString::to_string)
.or_else(|| sysroot().map(|p| p.display().to_string()))
.expect("Failed to find sysroot");

let mut args: Vec<String> = orig_args.clone();

if !have_sysroot_arg {
args.extend(["--sysroot".to_string(), sysroot.to_string()]);
}

let our_exe_filename = std::env::current_exe()
.ok()
.and_then(|p| p.file_stem().map(ToOwned::to_owned))
.unwrap_or_else(|| "ubrustc".into());

// This is probably wrong.
let wrapper_mode = orig_args
.get(1)
.map(std::path::Path::new)
.and_then(std::path::Path::file_stem)
.map_or(false, |name| {
name == our_exe_filename || name == "ubrustc" || name == "rustc"
});

if wrapper_mode {
args.remove(1);
}
let args: Vec<String> = std::env::args().collect();
run_compiler(args, &mut UbrustcCallbacks);
}))
}

fn arg_value<'a, T: AsRef<str>>(args: &'a [T], find_arg: &str) -> Option<&'a str> {
let mut args = args.iter().map(|s| s.as_ref());
while let Some(arg) = args.next() {
let mut arg = arg.splitn(2, '=');
if arg.next() != Some(find_arg) {
continue;
}

if let Some(a) = arg.next().or_else(|| args.next()) {
return Some(a);
}
}
None
}

fn sysroot() -> Option<PathBuf> {
fn rustup_sysroot<H: ?Sized + AsRef<OsStr>, T: ?Sized + AsRef<Path>>(
home: &H,
toolchain: &T,
) -> PathBuf {
let mut path = PathBuf::from(home);
path.push("toolchains");
path.push(toolchain);
path
}
fn runtime_rustup_sysroot() -> Option<PathBuf> {
let home = std::env::var_os("RUSTUP_HOME")?;
let toolchain = std::env::var_os("RUSTUP_TOOLCHAIN")?;
Some(rustup_sysroot(&home, &toolchain))
}
fn compiletime_rustup_sysroot() -> Option<PathBuf> {
let home: &str = option_env!("RUSTUP_HOME")?;
let toolchain: &str = option_env!("RUSTUP_TOOLCHAIN")?;
Some(rustup_sysroot(&home, &toolchain))
}
fn rustc_on_path_sysroot() -> Option<PathBuf> {
std::process::Command::new("rustc")
.arg("--print=sysroot")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.map(|s| PathBuf::from(s.trim()))
}
runtime_rustup_sysroot()
.or_else(rustc_on_path_sysroot)
.or_else(compiletime_rustup_sysroot)
}

fn run_compiler<CB: Callbacks + Send>(mut args: Vec<String>, callbacks: &mut CB) -> ! {
args.splice(1..1, std::iter::once("--cfg=ubrustc".to_string()));
std::process::exit(rustc_driver::catch_with_exit_code(move || {
Expand Down