Skip to content

Commit

Permalink
[WIP] build_output_filenames: Use already-'resolved' crate name in fa…
Browse files Browse the repository at this point in the history
…vor of manually 'resolving' it
  • Loading branch information
fmease committed Jul 10, 2024
1 parent a401c2d commit e688f5e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
4 changes: 0 additions & 4 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,6 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
}
}

pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
attr::first_attr_value_str_by_name(attrs, sym::crate_name)
}

#[derive(Clone, Debug)]
pub struct Condition {
pub name: Symbol,
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,12 @@ fn print_crate_info(
// no crate attributes, print out an error and exit
return Compilation::Continue;
};
let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess);
let crate_name = passes::get_crate_name(sess, attrs);
let (crate_name, crate_name_origin) = passes::get_crate_name(sess, attrs);
let t_outputs = rustc_interface::util::build_output_filenames(
sess,
crate_name,
crate_name_origin,
);
let crate_types = collect_crate_types(sess, attrs);
for &style in &crate_types {
let fname = rustc_session::output::filename_for_input(
Expand All @@ -774,7 +778,8 @@ fn print_crate_info(
// no crate attributes, print out an error and exit
return Compilation::Continue;
};
println_info!("{}", passes::get_crate_name(sess, attrs));
let (crate_name, _) = passes::get_crate_name(sess, attrs);
println_info!("{crate_name}");
}
Cfg => {
let mut cfgs = sess
Expand Down
27 changes: 18 additions & 9 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,16 @@ pub(crate) fn create_global_ctxt<'tcx>(

let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);

let crate_name = get_crate_name(sess, &pre_configured_attrs);
let (crate_name, crate_name_origin) = get_crate_name(sess, &pre_configured_attrs);
let crate_types = collect_crate_types(sess, &pre_configured_attrs);
let stable_crate_id = StableCrateId::new(
crate_name,
crate_types.contains(&CrateType::Executable),
sess.opts.cg.metadata.clone(),
sess.cfg_version,
);
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
let outputs = util::build_output_filenames(sess, crate_name, crate_name_origin);

let dep_graph = setup_dep_graph(sess, crate_name)?;

let cstore =
Expand Down Expand Up @@ -1044,7 +1045,7 @@ pub(crate) fn start_codegen<'tcx>(
}

/// Compute and validate the crate name.
pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol {
pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> (Symbol, CrateNameOrigin) {
// We unconditionally validate all `#![crate_name]`s even if a crate name was
// set on the command line via `--crate-name` which we prioritize over the
// crate attributes. We perform the validation here instead of later to ensure
Expand All @@ -1053,9 +1054,9 @@ pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol
let attr_crate_name =
validate_and_find_value_str_builtin_attr(sym::crate_name, sess, krate_attrs);

let validate = |name, span| {
let validate = |name, span, origin| {
rustc_session::output::validate_crate_name(sess, name, span);
name
(name, origin)
};

if let Some(crate_name) = &sess.opts.crate_name {
Expand All @@ -1069,11 +1070,11 @@ pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol
attr_crate_name,
});
}
return validate(crate_name, None);
return validate(crate_name, None, CrateNameOrigin::CliOpt);
}

if let Some((crate_name, span)) = attr_crate_name {
return validate(crate_name, Some(span));
return validate(crate_name, Some(span), CrateNameOrigin::CrateAttr);
}

if let Input::File(ref path) = sess.io.input
Expand All @@ -1082,11 +1083,19 @@ pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol
if file_stem.starts_with('-') {
sess.dcx().emit_err(errors::CrateNameInvalid { crate_name: file_stem });
} else {
return validate(Symbol::intern(&file_stem.replace('-', "_")), None);
let crate_name = file_stem.replace('-', "_");
return validate(Symbol::intern(&crate_name), None, CrateNameOrigin::InputFile);
}
}

Symbol::intern("rust_out")
(Symbol::intern("rust_out"), CrateNameOrigin::Fallback)
}

pub enum CrateNameOrigin {
CliOpt,
CrateAttr,
InputFile,
Fallback,
}

fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit {
Expand Down
37 changes: 21 additions & 16 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errors;
use crate::passes::CrateNameOrigin;
use rustc_ast as ast;
use rustc_codegen_ssa::traits::CodegenBackend;
#[cfg(parallel_compiler)]
Expand All @@ -14,8 +15,9 @@ use rustc_session::{filesearch, EarlyDiagCtxt, Session};
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::edition::Edition;
use rustc_span::source_map::SourceMapInputs;
use rustc_span::symbol::sym;
use rustc_span::{sym, Symbol};
use rustc_target::spec::Target;

use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -453,37 +455,41 @@ fn multiple_output_types_to_stdout(
}
}

pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames {
pub fn build_output_filenames(
sess: &Session,
crate_name: Symbol,
crate_name_origin: CrateNameOrigin,
) -> OutputFilenames {
if multiple_output_types_to_stdout(
&sess.opts.output_types,
sess.io.output_file == Some(OutFileName::Stdout),
) {
sess.dcx().emit_fatal(errors::MultipleOutputTypesToStdout);
}

let crate_name = sess
.opts
.crate_name
.clone()
.or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string()));

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 = sess.io.output_dir.clone().unwrap_or_default();

// If a crate name is present, we use it as the link name
let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
// FIXME(fmease): Figure out a nicer way to do this. `stem` is almost
// identical to `crate_name` except when it was derived
// from a real(!) input file in which case they may differ
// by `-`/`_` only.
let stem = match crate_name_origin {
CrateNameOrigin::InputFile => sess.io.input.filestem(),
_ => crate_name.as_str(),
};

OutputFilenames::new(
dirpath,
crate_name.unwrap_or_else(|| stem.replace('-', "_")),
crate_name,
stem,
None,
sess.io.temps_dir.clone(),
sess.opts.cg.extra_filename.clone(),
&sess.opts.cg.extra_filename,
sess.opts.output_types.clone(),
)
}
Expand All @@ -504,15 +510,14 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
sess.dcx().emit_warn(errors::IgnoringOutDir);
}

let out_filestem =
out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
let out_filestem = out_file.filestem().unwrap_or_default().to_str().unwrap();
OutputFilenames::new(
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")),
crate_name,
out_filestem,
ofile,
sess.io.temps_dir.clone(),
sess.opts.cg.extra_filename.clone(),
&sess.opts.cg.extra_filename,
sess.opts.output_types.clone(),
)
}
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use rustc_feature::UnstableFeatures;
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
use rustc_span::edition::{Edition, DEFAULT_EDITION, EDITION_NAME_LIST, LATEST_STABLE_EDITION};
use rustc_span::source_map::FilePathMapping;
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm};
use rustc_span::{
FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm, Symbol,
};
use rustc_target::spec::{FramePointer, LinkSelfContainedComponents, LinkerFeatures};
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple};
use std::collections::btree_map::{
Expand Down Expand Up @@ -945,7 +947,7 @@ impl OutFileName {
#[derive(Clone, Hash, Debug, HashStable_Generic, Encodable, Decodable)]
pub struct OutputFilenames {
pub(crate) out_directory: PathBuf,
/// Crate name. Never contains '-'.
/// Crate name (must not contain `-`) plus extra (`-Cextra-filename`).
crate_stem: String,
/// Typically based on `.rs` input file name. Any '-' is preserved.
filestem: String,
Expand All @@ -961,19 +963,19 @@ pub const DWARF_OBJECT_EXT: &str = "dwo";
impl OutputFilenames {
pub fn new(
out_directory: PathBuf,
out_crate_name: String,
out_filestem: String,
crate_name: Symbol,
out_filestem: &str,
single_output_file: Option<OutFileName>,
temps_directory: Option<PathBuf>,
extra: String,
extra: &str,
outputs: OutputTypes,
) -> Self {
OutputFilenames {
out_directory,
single_output_file,
temps_directory,
outputs,
crate_stem: format!("{out_crate_name}{extra}"),
crate_stem: format!("{crate_name}{extra}"),
filestem: format!("{out_filestem}{extra}"),
}
}
Expand Down

0 comments on commit e688f5e

Please sign in to comment.