move emit_metadata to rustc_metadata::fs

This commit is contained in:
Yoshiki Matsuda 2022-04-24 19:34:35 +09:00
parent aedf78e56b
commit 709a78226b
6 changed files with 33 additions and 30 deletions

View file

@ -28,10 +28,7 @@ use super::command::Command;
use super::linker::{self, Linker};
use super::metadata::{create_rmeta_file, MetadataPosition};
use super::rpath::{self, RPathConfig};
use crate::{
looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib,
METADATA_FILENAME,
};
use crate::{looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib};
use cc::windows_registry;
use regex::Regex;
@ -237,23 +234,7 @@ pub fn each_linked_rlib(
Ok(())
}
/// We use a temp directory here to avoid races between concurrent rustc processes,
/// such as builds in the same directory using the same filename for metadata while
/// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
/// directory being searched for `extern crate` (observing an incomplete file).
/// The returned path is the temporary file containing the complete metadata.
pub fn emit_metadata(sess: &Session, metadata: &[u8], tmpdir: &MaybeTempDir) -> PathBuf {
let out_filename = tmpdir.as_ref().join(METADATA_FILENAME);
let result = fs::write(&out_filename, metadata);
if let Err(e) = result {
sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
}
out_filename
}
/// Create an 'rlib'.
/// Create an 'arlib'.
///
/// An rlib in its current incarnation is essentially a renamed .a file. The rlib primarily contains
/// the object file of the crate, but it also contains all of the object files from native
@ -276,7 +257,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
RlibFlavor::Normal => {
let (metadata, metadata_position) =
create_rmeta_file(sess, codegen_results.metadata.raw_data());
let metadata = emit_metadata(sess, &metadata, tmpdir);
let metadata = rustc_metadata::fs::emit_metadata(sess, &metadata, tmpdir);
match metadata_position {
MetadataPosition::First => {
// Most of the time metadata in rlib files is wrapped in a "dummy" object
@ -502,7 +483,7 @@ fn link_staticlib<'a, B: ArchiveBuilder<'a>>(
ab.add_archive(path, move |fname: &str| {
// Ignore metadata files, no matter the name.
if fname == METADATA_FILENAME {
if fname == rustc_metadata::fs::METADATA_FILENAME {
return true;
}
@ -2474,7 +2455,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let mut archive = <B as ArchiveBuilder>::new(sess, &dst);
if let Err(e) = archive.add_archive(cratepath, move |f| {
if f == METADATA_FILENAME {
if f == rustc_metadata::fs::METADATA_FILENAME {
return true;
}

View file

@ -16,14 +16,13 @@ use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::rustc_erase_owner;
use rustc_data_structures::sync::MetadataRef;
use rustc_metadata::fs::METADATA_FILENAME;
use rustc_metadata::EncodedMetadata;
use rustc_session::cstore::MetadataLoader;
use rustc_session::Session;
use rustc_target::abi::Endian;
use rustc_target::spec::{RelocModel, Target};
use crate::METADATA_FILENAME;
/// The default metadata loader. This is used by cg_llvm and cg_clif.
///
/// # Metadata location

View file

@ -64,9 +64,6 @@ pub struct ModuleCodegen<M> {
pub kind: ModuleKind,
}
// FIXME(eddyb) maybe include the crate name in this?
pub const METADATA_FILENAME: &str = "lib.rmeta";
impl<M> ModuleCodegen<M> {
pub fn into_compiled_module(
self,

View file

@ -5,7 +5,6 @@ use crate::util;
use ast::CRATE_NODE_ID;
use rustc_ast::{self as ast, visit};
use rustc_borrowck as mir_borrowck;
use rustc_codegen_ssa::back::link::emit_metadata;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::parallel;
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
@ -17,6 +16,7 @@ use rustc_hir::definitions::Definitions;
use rustc_hir::Crate;
use rustc_lint::{EarlyCheckNode, LintStore};
use rustc_metadata::creader::CStore;
use rustc_metadata::fs::emit_metadata;
use rustc_metadata::{encode_metadata, EncodedMetadata};
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;

View file

@ -0,0 +1,24 @@
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_session::Session;
use std::fs;
use std::path::PathBuf;
// FIXME(eddyb) maybe include the crate name in this?
pub const METADATA_FILENAME: &str = "lib.rmeta";
/// We use a temp directory here to avoid races between concurrent rustc processes,
/// such as builds in the same directory using the same filename for metadata while
/// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
/// directory being searched for `extern crate` (observing an incomplete file).
/// The returned path is the temporary file containing the complete metadata.
pub fn emit_metadata(sess: &Session, metadata: &[u8], tmpdir: &MaybeTempDir) -> PathBuf {
let out_filename = tmpdir.as_ref().join(METADATA_FILENAME);
let result = fs::write(&out_filename, metadata);
if let Err(e) = result {
sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
}
out_filename
}

View file

@ -34,6 +34,8 @@ mod native_libs;
mod rmeta;
pub mod creader;
pub mod fs;
pub mod locator;
pub use fs::{emit_metadata, METADATA_FILENAME};
pub use rmeta::{encode_metadata, EncodedMetadata, METADATA_HEADER};