Move compiler input and ouput paths into session
This commit is contained in:
parent
42f75f1e46
commit
9f5cd03153
15 changed files with 113 additions and 154 deletions
|
@ -14,10 +14,10 @@ use rustc_middle::ty;
|
|||
use rustc_parse::maybe_new_parser_from_source_str;
|
||||
use rustc_query_impl::QueryCtxt;
|
||||
use rustc_session::config::{self, CheckCfg, ErrorOutputType, Input, OutputFilenames};
|
||||
use rustc_session::early_error;
|
||||
use rustc_session::lint;
|
||||
use rustc_session::parse::{CrateConfig, ParseSess};
|
||||
use rustc_session::Session;
|
||||
use rustc_session::{early_error, CompilerIO};
|
||||
use rustc_span::source_map::{FileLoader, FileName};
|
||||
use rustc_span::symbol::sym;
|
||||
use std::path::PathBuf;
|
||||
|
@ -35,19 +35,11 @@ pub type Result<T> = result::Result<T, ErrorGuaranteed>;
|
|||
pub struct Compiler {
|
||||
pub(crate) sess: Lrc<Session>,
|
||||
codegen_backend: Lrc<Box<dyn CodegenBackend>>,
|
||||
pub(crate) io: CompilerIO,
|
||||
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
|
||||
pub(crate) override_queries:
|
||||
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::ExternProviders)>,
|
||||
}
|
||||
|
||||
pub struct CompilerIO {
|
||||
pub input: Input,
|
||||
pub output_dir: Option<PathBuf>,
|
||||
pub output_file: Option<PathBuf>,
|
||||
pub temps_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
pub fn session(&self) -> &Lrc<Session> {
|
||||
&self.sess
|
||||
|
@ -55,9 +47,6 @@ impl Compiler {
|
|||
pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
|
||||
&self.codegen_backend
|
||||
}
|
||||
pub fn io(&self) -> &CompilerIO {
|
||||
&self.io
|
||||
}
|
||||
pub fn register_lints(&self) -> &Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> {
|
||||
&self.register_lints
|
||||
}
|
||||
|
@ -66,7 +55,7 @@ impl Compiler {
|
|||
sess: &Session,
|
||||
attrs: &[ast::Attribute],
|
||||
) -> OutputFilenames {
|
||||
util::build_output_filenames(&self.io, attrs, sess)
|
||||
util::build_output_filenames(attrs, sess)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -273,12 +262,19 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
|
|||
crate::callbacks::setup_callbacks();
|
||||
|
||||
let registry = &config.registry;
|
||||
|
||||
let temps_dir = config.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
|
||||
let (mut sess, codegen_backend) = util::create_session(
|
||||
config.opts,
|
||||
config.crate_cfg,
|
||||
config.crate_check_cfg,
|
||||
config.file_loader,
|
||||
config.input.opt_path(),
|
||||
CompilerIO {
|
||||
input: config.input,
|
||||
output_dir: config.output_dir,
|
||||
output_file: config.output_file,
|
||||
temps_dir,
|
||||
},
|
||||
config.lint_caps,
|
||||
config.make_codegen_backend,
|
||||
registry.clone(),
|
||||
|
@ -288,17 +284,9 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
|
|||
parse_sess_created(&mut sess.parse_sess);
|
||||
}
|
||||
|
||||
let temps_dir = sess.opts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
|
||||
|
||||
let compiler = Compiler {
|
||||
sess: Lrc::new(sess),
|
||||
codegen_backend: Lrc::new(codegen_backend),
|
||||
io: CompilerIO {
|
||||
input: config.input,
|
||||
output_dir: config.output_dir,
|
||||
output_file: config.output_file,
|
||||
temps_dir,
|
||||
},
|
||||
register_lints: config.register_lints,
|
||||
override_queries: config.override_queries,
|
||||
};
|
||||
|
|
|
@ -50,8 +50,8 @@ use std::rc::Rc;
|
|||
use std::sync::LazyLock;
|
||||
use std::{env, fs, iter};
|
||||
|
||||
pub fn parse<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::Crate> {
|
||||
let krate = sess.time("parse_crate", || match input {
|
||||
pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> {
|
||||
let krate = sess.time("parse_crate", || match &sess.io.input {
|
||||
Input::File(file) => parse_crate_from_file(file, &sess.parse_sess),
|
||||
Input::Str { input, name } => {
|
||||
parse_crate_from_source_str(name.clone(), input.clone(), &sess.parse_sess)
|
||||
|
@ -665,7 +665,6 @@ fn write_out_deps(
|
|||
|
||||
pub fn prepare_outputs(
|
||||
sess: &Session,
|
||||
compiler: &Compiler,
|
||||
krate: &ast::Crate,
|
||||
boxed_resolver: &RefCell<BoxedResolver>,
|
||||
crate_name: Symbol,
|
||||
|
@ -673,13 +672,13 @@ pub fn prepare_outputs(
|
|||
let _timer = sess.timer("prepare_outputs");
|
||||
|
||||
// FIXME: rustdoc passes &[] instead of &krate.attrs here
|
||||
let outputs = util::build_output_filenames(&compiler.io, &krate.attrs, sess);
|
||||
let outputs = util::build_output_filenames(&krate.attrs, sess);
|
||||
|
||||
let output_paths =
|
||||
generated_output_paths(sess, &outputs, compiler.io.output_file.is_some(), crate_name);
|
||||
generated_output_paths(sess, &outputs, sess.io.output_file.is_some(), crate_name);
|
||||
|
||||
// Ensure the source file isn't accidentally overwritten during compilation.
|
||||
if let Some(ref input_path) = compiler.io.input.opt_path() {
|
||||
if let Some(ref input_path) = sess.io.input.opt_path() {
|
||||
if sess.opts.will_create_output_file() {
|
||||
if output_contains_path(&output_paths, input_path) {
|
||||
let reported = sess.emit_err(InputFileWouldBeOverWritten { path: input_path });
|
||||
|
@ -693,7 +692,7 @@ pub fn prepare_outputs(
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(ref dir) = compiler.io.temps_dir {
|
||||
if let Some(ref dir) = sess.io.temps_dir {
|
||||
if fs::create_dir_all(dir).is_err() {
|
||||
let reported = sess.emit_err(TempsDirError);
|
||||
return Err(reported);
|
||||
|
@ -706,7 +705,7 @@ pub fn prepare_outputs(
|
|||
&& sess.opts.output_types.len() == 1;
|
||||
|
||||
if !only_dep_info {
|
||||
if let Some(ref dir) = compiler.io.output_dir {
|
||||
if let Some(ref dir) = sess.io.output_dir {
|
||||
if fs::create_dir_all(dir).is_err() {
|
||||
let reported = sess.emit_err(OutDirError);
|
||||
return Err(reported);
|
||||
|
|
|
@ -128,10 +128,8 @@ impl<'tcx> Queries<'tcx> {
|
|||
}
|
||||
|
||||
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
|
||||
self.parse.compute(|| {
|
||||
passes::parse(self.session(), &self.compiler.io.input)
|
||||
.map_err(|mut parse_error| parse_error.emit())
|
||||
})
|
||||
self.parse
|
||||
.compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit()))
|
||||
}
|
||||
|
||||
pub fn register_plugins(&self) -> Result<QueryResult<'_, (ast::Crate, Lrc<LintStore>)>> {
|
||||
|
@ -165,7 +163,7 @@ impl<'tcx> Queries<'tcx> {
|
|||
let parse_result = self.parse()?;
|
||||
let krate = parse_result.borrow();
|
||||
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
|
||||
find_crate_name(self.session(), &krate.attrs, &self.compiler.io.input)
|
||||
find_crate_name(self.session(), &krate.attrs)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -214,13 +212,7 @@ impl<'tcx> Queries<'tcx> {
|
|||
let crate_name = *self.crate_name()?.borrow();
|
||||
let (krate, resolver, lint_store) = self.expansion()?.steal();
|
||||
|
||||
let outputs = passes::prepare_outputs(
|
||||
self.session(),
|
||||
self.compiler,
|
||||
&krate,
|
||||
&resolver,
|
||||
crate_name,
|
||||
)?;
|
||||
let outputs = passes::prepare_outputs(self.session(), &krate, &resolver, crate_name)?;
|
||||
|
||||
let ty::ResolverOutputs {
|
||||
untracked,
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::interface::parse_cfgspecs;
|
|||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
|
||||
use rustc_session::config::rustc_optgroups;
|
||||
use rustc_session::config::Input;
|
||||
use rustc_session::config::TraitSolver;
|
||||
use rustc_session::config::{build_configuration, build_session_options, to_crate_config};
|
||||
use rustc_session::config::{
|
||||
|
@ -17,9 +18,11 @@ use rustc_session::config::{InstrumentCoverage, Passes};
|
|||
use rustc_session::lint::Level;
|
||||
use rustc_session::search_paths::SearchPath;
|
||||
use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
|
||||
use rustc_session::CompilerIO;
|
||||
use rustc_session::{build_session, getopts, Session};
|
||||
use rustc_span::edition::{Edition, DEFAULT_EDITION};
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::FileName;
|
||||
use rustc_span::SourceFileHashAlgorithm;
|
||||
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
|
||||
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
|
||||
|
@ -39,7 +42,14 @@ fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options
|
|||
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
|
||||
let registry = registry::Registry::new(&[]);
|
||||
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
|
||||
let sess = build_session(sessopts, None, None, registry, Default::default(), None, None);
|
||||
let temps_dir = sessopts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
|
||||
let io = CompilerIO {
|
||||
input: Input::Str { name: FileName::Custom(String::new()), input: String::new() },
|
||||
output_dir: None,
|
||||
output_file: None,
|
||||
temps_dir,
|
||||
};
|
||||
let sess = build_session(sessopts, io, None, registry, Default::default(), None, None);
|
||||
(sess, cfg)
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ use rustc_span::edition::Edition;
|
|||
use rustc_span::lev_distance::find_best_match_for_name;
|
||||
use rustc_span::source_map::FileLoader;
|
||||
use rustc_span::symbol::{sym, Symbol};
|
||||
use session::CompilerIO;
|
||||
use std::env;
|
||||
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
|
||||
use std::mem;
|
||||
|
@ -25,8 +26,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
|||
use std::sync::OnceLock;
|
||||
use std::thread;
|
||||
|
||||
use crate::interface::CompilerIO;
|
||||
|
||||
/// Function pointer type that constructs a new CodegenBackend.
|
||||
pub type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
|
||||
|
||||
|
@ -60,7 +59,7 @@ pub fn create_session(
|
|||
cfg: FxHashSet<(String, Option<String>)>,
|
||||
check_cfg: CheckCfg,
|
||||
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
|
||||
input_path: Option<PathBuf>,
|
||||
io: CompilerIO,
|
||||
lint_caps: FxHashMap<lint::LintId, lint::Level>,
|
||||
make_codegen_backend: Option<
|
||||
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
|
||||
|
@ -91,7 +90,7 @@ pub fn create_session(
|
|||
|
||||
let mut sess = session::build_session(
|
||||
sopts,
|
||||
input_path,
|
||||
io,
|
||||
bundle,
|
||||
descriptions,
|
||||
lint_caps,
|
||||
|
@ -488,17 +487,13 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<C
|
|||
base
|
||||
}
|
||||
|
||||
pub fn build_output_filenames(
|
||||
io: &CompilerIO,
|
||||
attrs: &[ast::Attribute],
|
||||
sess: &Session,
|
||||
) -> OutputFilenames {
|
||||
match io.output_file {
|
||||
pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames {
|
||||
match sess.io.output_file {
|
||||
None => {
|
||||
// "-" as input file will cause the parser to read from stdin so we
|
||||
// have to make up a name
|
||||
// We want to toss everything after the final '.'
|
||||
let dirpath = io.output_dir.clone().unwrap_or_default();
|
||||
let dirpath = sess.io.output_dir.clone().unwrap_or_default();
|
||||
|
||||
// If a crate name is present, we use it as the link name
|
||||
let stem = sess
|
||||
|
@ -506,13 +501,13 @@ pub fn build_output_filenames(
|
|||
.crate_name
|
||||
.clone()
|
||||
.or_else(|| rustc_attr::find_crate_name(sess, attrs).map(|n| n.to_string()))
|
||||
.unwrap_or_else(|| io.input.filestem().to_owned());
|
||||
.unwrap_or_else(|| sess.io.input.filestem().to_owned());
|
||||
|
||||
OutputFilenames::new(
|
||||
dirpath,
|
||||
stem,
|
||||
None,
|
||||
io.temps_dir.clone(),
|
||||
sess.io.temps_dir.clone(),
|
||||
sess.opts.cg.extra_filename.clone(),
|
||||
sess.opts.output_types.clone(),
|
||||
)
|
||||
|
@ -533,7 +528,7 @@ pub fn build_output_filenames(
|
|||
}
|
||||
Some(out_file.clone())
|
||||
};
|
||||
if io.output_dir != None {
|
||||
if sess.io.output_dir != None {
|
||||
sess.warn("ignoring --out-dir flag due to -o flag");
|
||||
}
|
||||
|
||||
|
@ -541,7 +536,7 @@ pub fn build_output_filenames(
|
|||
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
|
||||
out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(),
|
||||
ofile,
|
||||
io.temps_dir.clone(),
|
||||
sess.io.temps_dir.clone(),
|
||||
sess.opts.cg.extra_filename.clone(),
|
||||
sess.opts.output_types.clone(),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue