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