Skip to content

Commit

Permalink
Auto merge of #114750 - Enselic:metadata-dep-info, r=compiler-errors
Browse files Browse the repository at this point in the history
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.

Without this fix, the `.d` file contained

    dash-separated_something-extra.rmeta: dash-separated.rs

instead of

    libdash_separated_something-extra.rmeta: dash-separated.rs

which is the name of the file that is actually written by the compiler.

Worth noting: It took me several iterations to get all tests to pass, so I am relatively confident that this PR does not break anything.

Closes #68839
  • Loading branch information
bors committed Sep 17, 2023
2 parents 327e6cf + 04d81ba commit 8ed1d4a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 28 deletions.
20 changes: 13 additions & 7 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_metadata/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutFileName>,
pub temps_directory: Option<PathBuf>,
Expand All @@ -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<OutFileName>,
temps_directory: Option<PathBuf>,
Expand All @@ -922,6 +926,7 @@ impl OutputFilenames {
single_output_file,
temps_directory,
outputs,
crate_stem: format!("{out_crate_name}{extra}"),
filestem: format!("{out_filestem}{extra}"),
}
}
Expand All @@ -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
Expand Down
19 changes: 2 additions & 17 deletions compiler/rustc_session/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,11 @@ pub fn validate_crate_name(sess: &Session, s: Symbol, sp: Option<Span>) {
}
}

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
}

Expand Down
13 changes: 13 additions & 0 deletions tests/run-make/metadata-dep-info/Makefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/run-make/metadata-dep-info/dash-separated.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
libdash_separated_something-extra.rmeta: dash-separated.rs

dash-separated_something-extra.d: dash-separated.rs

dash-separated.rs:

0 comments on commit 8ed1d4a

Please sign in to comment.