Refactor out a repeating pattern with get_or_default_sysroot
This commit is contained in:
parent
a03d19ef63
commit
5441523f07
@ -888,9 +888,7 @@ pub fn version_at_macro_invocation(
|
|||||||
let debug_flags = matches.opt_strs("Z");
|
let debug_flags = matches.opt_strs("Z");
|
||||||
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
|
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
|
||||||
let opts = config::Options::default();
|
let opts = config::Options::default();
|
||||||
let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| {
|
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
|
||||||
filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
|
|
||||||
});
|
|
||||||
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
|
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
|
||||||
|
|
||||||
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version();
|
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version();
|
||||||
@ -1100,9 +1098,7 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->
|
|||||||
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
|
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
|
||||||
|
|
||||||
let opts = config::Options::default();
|
let opts = config::Options::default();
|
||||||
let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| {
|
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
|
||||||
filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
|
|
||||||
});
|
|
||||||
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
|
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
|
||||||
|
|
||||||
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes();
|
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes();
|
||||||
|
@ -336,10 +336,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
|
|||||||
|
|
||||||
let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
|
let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
|
||||||
|
|
||||||
let sysroot = match &config.opts.maybe_sysroot {
|
let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone());
|
||||||
Some(sysroot) => sysroot.clone(),
|
|
||||||
None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"),
|
|
||||||
};
|
|
||||||
|
|
||||||
let (codegen_backend, target_cfg) = match config.make_codegen_backend {
|
let (codegen_backend, target_cfg) = match config.make_codegen_backend {
|
||||||
None => {
|
None => {
|
||||||
|
@ -13,7 +13,7 @@ use rustc_session::config::{
|
|||||||
use rustc_session::lint::Level;
|
use rustc_session::lint::Level;
|
||||||
use rustc_session::search_paths::SearchPath;
|
use rustc_session::search_paths::SearchPath;
|
||||||
use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
|
use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
|
||||||
use rustc_session::{build_session, getopts, CompilerIO, EarlyDiagCtxt, Session};
|
use rustc_session::{build_session, filesearch, getopts, CompilerIO, EarlyDiagCtxt, Session};
|
||||||
use rustc_span::edition::{Edition, DEFAULT_EDITION};
|
use rustc_span::edition::{Edition, DEFAULT_EDITION};
|
||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::{FileName, SourceFileHashAlgorithm};
|
use rustc_span::{FileName, SourceFileHashAlgorithm};
|
||||||
@ -38,12 +38,7 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
|
|||||||
temps_dir,
|
temps_dir,
|
||||||
};
|
};
|
||||||
|
|
||||||
let sysroot = match &sessopts.maybe_sysroot {
|
let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());
|
||||||
Some(sysroot) => sysroot.clone(),
|
|
||||||
None => {
|
|
||||||
rustc_session::filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let target_cfg =
|
let target_cfg =
|
||||||
rustc_session::config::build_target_config(&early_dcx, &sessopts, None, &sysroot);
|
rustc_session::config::build_target_config(&early_dcx, &sessopts, None, &sysroot);
|
||||||
|
@ -6,7 +6,7 @@ pub use crate::options::*;
|
|||||||
use crate::errors::FileWriteFail;
|
use crate::errors::FileWriteFail;
|
||||||
use crate::search_paths::SearchPath;
|
use crate::search_paths::SearchPath;
|
||||||
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
|
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
|
||||||
use crate::{lint, HashStableContext};
|
use crate::{filesearch, lint, HashStableContext};
|
||||||
use crate::{EarlyDiagCtxt, Session};
|
use crate::{EarlyDiagCtxt, Session};
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
|
||||||
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
|
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
|
||||||
@ -2856,16 +2856,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
|
|||||||
|
|
||||||
let logical_env = parse_logical_env(early_dcx, matches);
|
let logical_env = parse_logical_env(early_dcx, matches);
|
||||||
|
|
||||||
// Try to find a directory containing the Rust `src`, for more details see
|
let sysroot = filesearch::materialize_sysroot(sysroot_opt);
|
||||||
// the doc comment on the `real_rust_source_base_dir` field.
|
|
||||||
let tmp_buf;
|
|
||||||
let sysroot = match &sysroot_opt {
|
|
||||||
Some(s) => s,
|
|
||||||
None => {
|
|
||||||
tmp_buf = crate::filesearch::get_or_default_sysroot().expect("Failed finding sysroot");
|
|
||||||
&tmp_buf
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let real_rust_source_base_dir = {
|
let real_rust_source_base_dir = {
|
||||||
// This is the location used by the `rust-src` `rustup` component.
|
// This is the location used by the `rust-src` `rustup` component.
|
||||||
let mut candidate = sysroot.join("lib/rustlib/src/rust");
|
let mut candidate = sysroot.join("lib/rustlib/src/rust");
|
||||||
@ -2909,7 +2901,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
|
|||||||
describe_lints,
|
describe_lints,
|
||||||
output_types,
|
output_types,
|
||||||
search_paths,
|
search_paths,
|
||||||
maybe_sysroot: sysroot_opt,
|
maybe_sysroot: Some(sysroot),
|
||||||
target_triple,
|
target_triple,
|
||||||
test,
|
test,
|
||||||
incremental,
|
incremental,
|
||||||
|
@ -194,6 +194,12 @@ pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
|
|||||||
return sysroot_candidates;
|
return sysroot_candidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the provided sysroot or calls [`get_or_default_sysroot`] if it's none.
|
||||||
|
/// Panics if [`get_or_default_sysroot`] returns an error.
|
||||||
|
pub fn materialize_sysroot(maybe_sysroot: Option<PathBuf>) -> PathBuf {
|
||||||
|
maybe_sysroot.unwrap_or_else(|| get_or_default_sysroot().expect("Failed finding sysroot"))
|
||||||
|
}
|
||||||
|
|
||||||
/// This function checks if sysroot is found using env::args().next(), and if it
|
/// This function checks if sysroot is found using env::args().next(), and if it
|
||||||
/// is not found, finds sysroot from current rustc_driver dll.
|
/// is not found, finds sysroot from current rustc_driver dll.
|
||||||
pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
|
pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user