1 use crate::{encode_metadata, EncodedMetadata};
3 use rustc_data_structures::temp_dir::MaybeTempDir;
4 use rustc_hir::def_id::LOCAL_CRATE;
5 use rustc_middle::ty::TyCtxt;
6 use rustc_session::config::{CrateType, OutputFilenames, OutputType};
7 use rustc_session::output::filename_for_metadata;
8 use rustc_session::Session;
9 use tempfile::Builder as TempFileBuilder;
12 use std::path::{Path, PathBuf};
14 // FIXME(eddyb) maybe include the crate name in this?
15 pub const METADATA_FILENAME: &str = "lib.rmeta";
17 /// We use a temp directory here to avoid races between concurrent rustc processes,
18 /// such as builds in the same directory using the same filename for metadata while
19 /// building an `.rlib` (stomping over one another), or writing an `.rmeta` into a
20 /// directory being searched for `extern crate` (observing an incomplete file).
21 /// The returned path is the temporary file containing the complete metadata.
22 pub fn emit_metadata(sess: &Session, metadata: &[u8], tmpdir: &MaybeTempDir) -> PathBuf {
23 let out_filename = tmpdir.as_ref().join(METADATA_FILENAME);
24 let result = fs::write(&out_filename, metadata);
26 if let Err(e) = result {
27 sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
33 pub fn encode_and_write_metadata(
35 outputs: &OutputFilenames,
36 ) -> (EncodedMetadata, bool) {
37 #[derive(PartialEq, Eq, PartialOrd, Ord)]
44 let metadata_kind = tcx
49 CrateType::Executable | CrateType::Staticlib | CrateType::Cdylib => MetadataKind::None,
51 CrateType::Rlib => MetadataKind::Uncompressed,
53 CrateType::Dylib | CrateType::ProcMacro => MetadataKind::Compressed,
56 .unwrap_or(MetadataKind::None);
58 let crate_name = tcx.crate_name(LOCAL_CRATE);
59 let out_filename = filename_for_metadata(tcx.sess, crate_name.as_str(), outputs);
60 // To avoid races with another rustc process scanning the output directory,
61 // we need to write the file somewhere else and atomically move it to its
62 // final destination, with an `fs::rename` call. In order for the rename to
63 // always succeed, the temporary file needs to be on the same filesystem,
64 // which is why we create it inside the output directory specifically.
65 let metadata_tmpdir = TempFileBuilder::new()
67 .tempdir_in(out_filename.parent().unwrap_or_else(|| Path::new("")))
68 .unwrap_or_else(|err| tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err)));
69 let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
70 let metadata_filename = metadata_tmpdir.as_ref().join(METADATA_FILENAME);
72 // Always create a file at `metadata_filename`, even if we have nothing to write to it.
73 // This simplifies the creation of the output `out_filename` when requested.
75 MetadataKind::None => {
76 std::fs::File::create(&metadata_filename).unwrap_or_else(|e| {
77 tcx.sess.fatal(&format!(
78 "failed to create the file {}: {}",
79 metadata_filename.display(),
84 MetadataKind::Uncompressed | MetadataKind::Compressed => {
85 encode_metadata(tcx, &metadata_filename);
89 let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
91 // If the user requests metadata as output, rename `metadata_filename`
92 // to the expected output `out_filename`. The match above should ensure
93 // this file always exists.
94 let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
95 let (metadata_filename, metadata_tmpdir) = if need_metadata_file {
96 if let Err(e) = non_durable_rename(&metadata_filename, &out_filename) {
97 tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
99 if tcx.sess.opts.json_artifact_notifications {
103 .emit_artifact_notification(&out_filename, "metadata");
107 (metadata_filename, Some(metadata_tmpdir))
110 // Load metadata back to memory: codegen may need to include it in object files.
112 EncodedMetadata::from_path(metadata_filename, metadata_tmpdir).unwrap_or_else(|e| {
113 tcx.sess.fatal(&format!("failed to create encoded metadata from file: {}", e))
116 let need_metadata_module = metadata_kind == MetadataKind::Compressed;
118 (metadata, need_metadata_module)
121 #[cfg(not(target_os = "linux"))]
122 pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
123 std::fs::rename(src, dst)
126 /// This function attempts to bypass the auto_da_alloc heuristic implemented by some filesystems
127 /// such as btrfs and ext4. When renaming over a file that already exists then they will "helpfully"
128 /// write back the source file before committing the rename in case a developer forgot some of
129 /// the fsyncs in the open/write/fsync(file)/rename/fsync(dir) dance for atomic file updates.
131 /// To avoid triggering this heuristic we delete the destination first, if it exists.
132 /// The cost of an extra syscall is much lower than getting descheduled for the sync IO.
133 #[cfg(target_os = "linux")]
134 pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
135 let _ = std::fs::remove_file(dst);
136 std::fs::rename(src, dst)