Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,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 @@ -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();

// 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 @@ -587,9 +590,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 @@ -880,6 +880,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 @@ -893,6 +896,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 @@ -904,6 +908,7 @@ impl OutputFilenames {
single_output_file,
temps_directory,
outputs,
crate_stem: format!("{out_crate_name}{extra}"),
filestem: format!("{out_filestem}{extra}"),
}
}
Expand All @@ -920,7 +925,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: