]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_metadata/src/fs.rs
Rollup merge of #95005 - ssomers:btree_static_assert, r=thomcc
[rust.git] / compiler / rustc_metadata / src / fs.rs
1 use crate::{encode_metadata, EncodedMetadata};
2
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;
10
11 use std::fs;
12 use std::path::{Path, PathBuf};
13
14 // FIXME(eddyb) maybe include the crate name in this?
15 pub const METADATA_FILENAME: &str = "lib.rmeta";
16
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);
25
26     if let Err(e) = result {
27         sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
28     }
29
30     out_filename
31 }
32
33 pub fn encode_and_write_metadata(
34     tcx: TyCtxt<'_>,
35     outputs: &OutputFilenames,
36 ) -> (EncodedMetadata, bool) {
37     #[derive(PartialEq, Eq, PartialOrd, Ord)]
38     enum MetadataKind {
39         None,
40         Uncompressed,
41         Compressed,
42     }
43
44     let metadata_kind = tcx
45         .sess
46         .crate_types()
47         .iter()
48         .map(|ty| match *ty {
49             CrateType::Executable | CrateType::Staticlib | CrateType::Cdylib => MetadataKind::None,
50
51             CrateType::Rlib => MetadataKind::Uncompressed,
52
53             CrateType::Dylib | CrateType::ProcMacro => MetadataKind::Compressed,
54         })
55         .max()
56         .unwrap_or(MetadataKind::None);
57
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()
66         .prefix("rmeta")
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);
71
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.
74     match metadata_kind {
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(),
80                     e
81                 ))
82             });
83         }
84         MetadataKind::Uncompressed | MetadataKind::Compressed => {
85             encode_metadata(tcx, &metadata_filename);
86         }
87     };
88
89     let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
90
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));
98         }
99         if tcx.sess.opts.json_artifact_notifications {
100             tcx.sess
101                 .parse_sess
102                 .span_diagnostic
103                 .emit_artifact_notification(&out_filename, "metadata");
104         }
105         (out_filename, None)
106     } else {
107         (metadata_filename, Some(metadata_tmpdir))
108     };
109
110     // Load metadata back to memory: codegen may need to include it in object files.
111     let metadata =
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))
114         });
115
116     let need_metadata_module = metadata_kind == MetadataKind::Compressed;
117
118     (metadata, need_metadata_module)
119 }
120
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)
124 }
125
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.
130 ///
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)
137 }