Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 709a782

Browse files
committed
move emit_metadata to rustc_metadata::fs
1 parent aedf78e commit 709a782

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ use super::command::Command;
2828
use super::linker::{self, Linker};
2929
use super::metadata::{create_rmeta_file, MetadataPosition};
3030
use super::rpath::{self, RPathConfig};
31-
use crate::{
32-
looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib,
33-
METADATA_FILENAME,
34-
};
31+
use crate::{looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib};
3532

3633
use cc::windows_registry;
3734
use regex::Regex;
@@ -237,23 +234,7 @@ pub fn each_linked_rlib(
237234
Ok(())
238235
}
239236

240-
/// We use a temp directory here to avoid races between concurrent rustc processes,
241-
/// such as builds in the same directory using the same filename for metadata while
242-
/// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
243-
/// directory being searched for `extern crate` (observing an incomplete file).
244-
/// The returned path is the temporary file containing the complete metadata.
245-
pub fn emit_metadata(sess: &Session, metadata: &[u8], tmpdir: &MaybeTempDir) -> PathBuf {
246-
let out_filename = tmpdir.as_ref().join(METADATA_FILENAME);
247-
let result = fs::write(&out_filename, metadata);
248-
249-
if let Err(e) = result {
250-
sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
251-
}
252-
253-
out_filename
254-
}
255-
256-
/// Create an 'rlib'.
237+
/// Create an 'arlib'.
257238
///
258239
/// An rlib in its current incarnation is essentially a renamed .a file. The rlib primarily contains
259240
/// 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>>(
276257
RlibFlavor::Normal => {
277258
let (metadata, metadata_position) =
278259
create_rmeta_file(sess, codegen_results.metadata.raw_data());
279-
let metadata = emit_metadata(sess, &metadata, tmpdir);
260+
let metadata = rustc_metadata::fs::emit_metadata(sess, &metadata, tmpdir);
280261
match metadata_position {
281262
MetadataPosition::First => {
282263
// 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>>(
502483

503484
ab.add_archive(path, move |fname: &str| {
504485
// Ignore metadata files, no matter the name.
505-
if fname == METADATA_FILENAME {
486+
if fname == rustc_metadata::fs::METADATA_FILENAME {
506487
return true;
507488
}
508489

@@ -2474,7 +2455,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
24742455

24752456
let mut archive = <B as ArchiveBuilder>::new(sess, &dst);
24762457
if let Err(e) = archive.add_archive(cratepath, move |f| {
2477-
if f == METADATA_FILENAME {
2458+
if f == rustc_metadata::fs::METADATA_FILENAME {
24782459
return true;
24792460
}
24802461

compiler/rustc_codegen_ssa/src/back/metadata.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ use rustc_data_structures::memmap::Mmap;
1616
use rustc_data_structures::owning_ref::OwningRef;
1717
use rustc_data_structures::rustc_erase_owner;
1818
use rustc_data_structures::sync::MetadataRef;
19+
use rustc_metadata::fs::METADATA_FILENAME;
1920
use rustc_metadata::EncodedMetadata;
2021
use rustc_session::cstore::MetadataLoader;
2122
use rustc_session::Session;
2223
use rustc_target::abi::Endian;
2324
use rustc_target::spec::{RelocModel, Target};
2425

25-
use crate::METADATA_FILENAME;
26-
2726
/// The default metadata loader. This is used by cg_llvm and cg_clif.
2827
///
2928
/// # Metadata location

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ pub struct ModuleCodegen<M> {
6464
pub kind: ModuleKind,
6565
}
6666

67-
// FIXME(eddyb) maybe include the crate name in this?
68-
pub const METADATA_FILENAME: &str = "lib.rmeta";
69-
7067
impl<M> ModuleCodegen<M> {
7168
pub fn into_compiled_module(
7269
self,

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::util;
55
use ast::CRATE_NODE_ID;
66
use rustc_ast::{self as ast, visit};
77
use rustc_borrowck as mir_borrowck;
8-
use rustc_codegen_ssa::back::link::emit_metadata;
98
use rustc_codegen_ssa::traits::CodegenBackend;
109
use rustc_data_structures::parallel;
1110
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
@@ -17,6 +16,7 @@ use rustc_hir::definitions::Definitions;
1716
use rustc_hir::Crate;
1817
use rustc_lint::{EarlyCheckNode, LintStore};
1918
use rustc_metadata::creader::CStore;
19+
use rustc_metadata::fs::emit_metadata;
2020
use rustc_metadata::{encode_metadata, EncodedMetadata};
2121
use rustc_middle::arena::Arena;
2222
use rustc_middle::dep_graph::DepGraph;

compiler/rustc_metadata/src/fs.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use rustc_data_structures::temp_dir::MaybeTempDir;
2+
use rustc_session::Session;
3+
4+
use std::fs;
5+
use std::path::PathBuf;
6+
7+
// FIXME(eddyb) maybe include the crate name in this?
8+
pub const METADATA_FILENAME: &str = "lib.rmeta";
9+
10+
/// We use a temp directory here to avoid races between concurrent rustc processes,
11+
/// such as builds in the same directory using the same filename for metadata while
12+
/// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
13+
/// directory being searched for `extern crate` (observing an incomplete file).
14+
/// The returned path is the temporary file containing the complete metadata.
15+
pub fn emit_metadata(sess: &Session, metadata: &[u8], tmpdir: &MaybeTempDir) -> PathBuf {
16+
let out_filename = tmpdir.as_ref().join(METADATA_FILENAME);
17+
let result = fs::write(&out_filename, metadata);
18+
19+
if let Err(e) = result {
20+
sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
21+
}
22+
23+
out_filename
24+
}

compiler/rustc_metadata/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ mod native_libs;
3434
mod rmeta;
3535

3636
pub mod creader;
37+
pub mod fs;
3738
pub mod locator;
3839

40+
pub use fs::{emit_metadata, METADATA_FILENAME};
3941
pub use rmeta::{encode_metadata, EncodedMetadata, METADATA_HEADER};

0 commit comments

Comments
 (0)