diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 37e242c6e4010..0634e44c5620b 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -568,6 +568,13 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu ) { sess.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 @@ -576,15 +583,11 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu 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 - .opts - .crate_name - .clone() - .or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string())) - .unwrap_or_else(|| sess.io.input.filestem().to_owned()); + let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned()); OutputFilenames::new( dirpath, + crate_name.unwrap_or_else(|| stem.replace('-', "_")), stem, None, sess.io.temps_dir.clone(), @@ -609,9 +612,12 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu sess.emit_warning(errors::IgnoringOutDir); } + let out_filestem = + out_file.filestem().unwrap_or_default().to_str().unwrap().to_string(); OutputFilenames::new( out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(), - out_file.filestem().unwrap_or_default().to_str().unwrap().to_string(), + crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")), + out_filestem, ofile, sess.io.temps_dir.clone(), sess.opts.cg.extra_filename.clone(), diff --git a/compiler/rustc_metadata/src/fs.rs b/compiler/rustc_metadata/src/fs.rs index 2a9662b809aef..7eb2a347db214 100644 --- a/compiler/rustc_metadata/src/fs.rs +++ b/compiler/rustc_metadata/src/fs.rs @@ -5,7 +5,6 @@ use crate::errors::{ use crate::{encode_metadata, EncodedMetadata}; use rustc_data_structures::temp_dir::MaybeTempDir; -use rustc_hir::def_id::LOCAL_CRATE; use rustc_middle::ty::TyCtxt; use rustc_session::config::{OutFileName, OutputType}; use rustc_session::output::filename_for_metadata; @@ -40,8 +39,7 @@ pub fn emit_wrapper_file( } pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> (EncodedMetadata, bool) { - let crate_name = tcx.crate_name(LOCAL_CRATE); - let out_filename = filename_for_metadata(tcx.sess, crate_name, tcx.output_filenames(())); + let out_filename = filename_for_metadata(tcx.sess, tcx.output_filenames(())); // To avoid races with another rustc process scanning the output directory, // we need to write the file somewhere else and atomically move it to its // final destination, with an `fs::rename` call. In order for the rename to diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index ba82ee95caa27..956ae149e5545 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -898,6 +898,9 @@ impl OutFileName { #[derive(Clone, Hash, Debug, HashStable_Generic)] pub struct OutputFilenames { pub out_directory: PathBuf, + /// Crate name. Never contains '-'. + crate_stem: String, + /// Typically based on `.rs` input file name. Any '-' is preserved. filestem: String, pub single_output_file: Option, pub temps_directory: Option, @@ -911,6 +914,7 @@ pub const DWARF_OBJECT_EXT: &str = "dwo"; impl OutputFilenames { pub fn new( out_directory: PathBuf, + out_crate_name: String, out_filestem: String, single_output_file: Option, temps_directory: Option, @@ -922,6 +926,7 @@ impl OutputFilenames { single_output_file, temps_directory, outputs, + crate_stem: format!("{out_crate_name}{extra}"), filestem: format!("{out_filestem}{extra}"), } } @@ -938,7 +943,12 @@ impl OutputFilenames { /// should be placed on disk. pub fn output_path(&self, flavor: OutputType) -> PathBuf { let extension = flavor.extension(); - self.with_directory_and_extension(&self.out_directory, extension) + match flavor { + OutputType::Metadata => { + self.out_directory.join(format!("lib{}.{}", self.crate_stem, extension)) + } + _ => self.with_directory_and_extension(&self.out_directory, extension), + } } /// Gets the path where a compilation artifact of the given type for the diff --git a/compiler/rustc_session/src/output.rs b/compiler/rustc_session/src/output.rs index c0884fb21cd18..7a57b0621cd47 100644 --- a/compiler/rustc_session/src/output.rs +++ b/compiler/rustc_session/src/output.rs @@ -119,26 +119,11 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option) { } } -pub fn filename_for_metadata( - sess: &Session, - crate_name: Symbol, - outputs: &OutputFilenames, -) -> OutFileName { - // If the command-line specified the path, use that directly. - if let Some(Some(out_filename)) = sess.opts.output_types.get(&OutputType::Metadata) { - return out_filename.clone(); - } - - let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename); - - let out_filename = outputs.single_output_file.clone().unwrap_or_else(|| { - OutFileName::Real(outputs.out_directory.join(&format!("lib{libname}.rmeta"))) - }); - +pub fn filename_for_metadata(sess: &Session, outputs: &OutputFilenames) -> OutFileName { + let out_filename = outputs.path(OutputType::Metadata); if let OutFileName::Real(ref path) = out_filename { check_file_is_writeable(path, sess); } - out_filename } diff --git a/tests/run-make/metadata-dep-info/Makefile b/tests/run-make/metadata-dep-info/Makefile new file mode 100644 index 0000000000000..f9043f214338c --- /dev/null +++ b/tests/run-make/metadata-dep-info/Makefile @@ -0,0 +1,13 @@ +include ../tools.mk + +ifdef RUSTC_BLESS_TEST + RUSTC_TEST_OP = cp +else + RUSTC_TEST_OP = $(DIFF) +endif + +all: + $(RUSTC) --emit=metadata,dep-info --crate-type lib dash-separated.rs -C extra-filename=_something-extra + # Strip TMPDIR since it is a machine specific absolute path + sed "s%.*[/\\]%%" "$(TMPDIR)"/dash-separated_something-extra.d > "$(TMPDIR)"/dash-separated_something-extra.normalized.d + $(RUSTC_TEST_OP) "$(TMPDIR)"/dash-separated_something-extra.normalized.d dash-separated_something-extra.normalized.d diff --git a/tests/run-make/metadata-dep-info/dash-separated.rs b/tests/run-make/metadata-dep-info/dash-separated.rs new file mode 100644 index 0000000000000..4202afd3d084f --- /dev/null +++ b/tests/run-make/metadata-dep-info/dash-separated.rs @@ -0,0 +1,4 @@ +//! It is important that this file has at least one `-` in the file name since +//! we want to test that it becomes a `_` when appropriate. + +pub struct Foo; diff --git a/tests/run-make/metadata-dep-info/dash-separated_something-extra.normalized.d b/tests/run-make/metadata-dep-info/dash-separated_something-extra.normalized.d new file mode 100644 index 0000000000000..497d76b4ea12e --- /dev/null +++ b/tests/run-make/metadata-dep-info/dash-separated_something-extra.normalized.d @@ -0,0 +1,5 @@ +libdash_separated_something-extra.rmeta: dash-separated.rs + +dash-separated_something-extra.d: dash-separated.rs + +dash-separated.rs: