Make .rmeta file in dep-info have correct name (lib prefix)

Since `filename_for_metadata()` and
`OutputFilenames::path(OutputType::Metadata)` had different logic for
the name of the metadata file, the `.d` file contained a file name
different from the actual name used. Share the logic to fix the
out-of-sync name.

Closes 68839.
This commit is contained in:
Martin Nordholts 2023-08-11 06:20:35 +02:00
parent e286f25ec0
commit 04d81ba153
7 changed files with 49 additions and 28 deletions

View File

@ -546,6 +546,13 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
) { ) {
sess.emit_fatal(errors::MultipleOutputTypesToStdout); 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 { match sess.io.output_file {
None => { None => {
// "-" as input file will cause the parser to read from stdin so we // "-" as input file will cause the parser to read from stdin so we
@ -554,15 +561,11 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
let dirpath = sess.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 // If a crate name is present, we use it as the link name
let stem = sess let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
.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());
OutputFilenames::new( OutputFilenames::new(
dirpath, dirpath,
crate_name.unwrap_or_else(|| stem.replace('-', "_")),
stem, stem,
None, None,
sess.io.temps_dir.clone(), sess.io.temps_dir.clone(),
@ -587,9 +590,12 @@ pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> Outpu
sess.emit_warning(errors::IgnoringOutDir); sess.emit_warning(errors::IgnoringOutDir);
} }
let out_filestem =
out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
OutputFilenames::new( OutputFilenames::new(
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(), 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, ofile,
sess.io.temps_dir.clone(), sess.io.temps_dir.clone(),
sess.opts.cg.extra_filename.clone(), sess.opts.cg.extra_filename.clone(),

View File

@ -5,7 +5,6 @@ use crate::errors::{
use crate::{encode_metadata, EncodedMetadata}; use crate::{encode_metadata, EncodedMetadata};
use rustc_data_structures::temp_dir::MaybeTempDir; use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OutFileName, OutputType}; use rustc_session::config::{OutFileName, OutputType};
use rustc_session::output::filename_for_metadata; 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) { 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, tcx.output_filenames(()));
let out_filename = filename_for_metadata(tcx.sess, crate_name, tcx.output_filenames(()));
// To avoid races with another rustc process scanning the output directory, // 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 // 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 // final destination, with an `fs::rename` call. In order for the rename to

View File

@ -880,6 +880,9 @@ impl OutFileName {
#[derive(Clone, Hash, Debug, HashStable_Generic)] #[derive(Clone, Hash, Debug, HashStable_Generic)]
pub struct OutputFilenames { pub struct OutputFilenames {
pub out_directory: PathBuf, pub out_directory: PathBuf,
/// Crate name. Never contains '-'.
crate_stem: String,
/// Typically based on `.rs` input file name. Any '-' is preserved.
filestem: String, filestem: String,
pub single_output_file: Option<OutFileName>, pub single_output_file: Option<OutFileName>,
pub temps_directory: Option<PathBuf>, pub temps_directory: Option<PathBuf>,
@ -893,6 +896,7 @@ pub const DWARF_OBJECT_EXT: &str = "dwo";
impl OutputFilenames { impl OutputFilenames {
pub fn new( pub fn new(
out_directory: PathBuf, out_directory: PathBuf,
out_crate_name: String,
out_filestem: String, out_filestem: String,
single_output_file: Option<OutFileName>, single_output_file: Option<OutFileName>,
temps_directory: Option<PathBuf>, temps_directory: Option<PathBuf>,
@ -904,6 +908,7 @@ impl OutputFilenames {
single_output_file, single_output_file,
temps_directory, temps_directory,
outputs, outputs,
crate_stem: format!("{out_crate_name}{extra}"),
filestem: format!("{out_filestem}{extra}"), filestem: format!("{out_filestem}{extra}"),
} }
} }
@ -920,7 +925,12 @@ impl OutputFilenames {
/// should be placed on disk. /// should be placed on disk.
pub fn output_path(&self, flavor: OutputType) -> PathBuf { pub fn output_path(&self, flavor: OutputType) -> PathBuf {
let extension = flavor.extension(); 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 /// Gets the path where a compilation artifact of the given type for the

View File

@ -119,26 +119,11 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
} }
} }
pub fn filename_for_metadata( pub fn filename_for_metadata(sess: &Session, outputs: &OutputFilenames) -> OutFileName {
sess: &Session, let out_filename = outputs.path(OutputType::Metadata);
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")))
});
if let OutFileName::Real(ref path) = out_filename { if let OutFileName::Real(ref path) = out_filename {
check_file_is_writeable(path, sess); check_file_is_writeable(path, sess);
} }
out_filename out_filename
} }

View File

@ -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

View File

@ -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;

View File

@ -0,0 +1,5 @@
libdash_separated_something-extra.rmeta: dash-separated.rs
dash-separated_something-extra.d: dash-separated.rs
dash-separated.rs: