use crate::errors::{
BinaryOutputToTty, FailedCopyToStdout, FailedCreateEncodedMetadata, FailedCreateFile,
FailedCreateTempdir, FailedWriteError,
};
use crate::{encode_metadata, EncodedMetadata};
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{OutFileName, OutputType};
use rustc_session::output::filename_for_metadata;
use rustc_session::{MetadataKind, Session};
use tempfile::Builder as TempFileBuilder;
use std::path::{Path, PathBuf};
use std::{fs, io};
pub const METADATA_FILENAME: &str = "lib.rmeta";
pub fn emit_wrapper_file(
sess: &Session,
data: &[u8],
tmpdir: &MaybeTempDir,
name: &str,
) -> PathBuf {
let out_filename = tmpdir.as_ref().join(name);
let result = fs::write(&out_filename, data);
if let Err(err) = result {
sess.emit_fatal(FailedWriteError { filename: out_filename, err });
}
out_filename
}
pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> (EncodedMetadata, bool) {
let out_filename = filename_for_metadata(tcx.sess, tcx.output_filenames(()));
let metadata_tmpdir = TempFileBuilder::new()
.prefix("rmeta")
.tempdir_in(out_filename.parent().unwrap_or_else(|| Path::new("")))
.unwrap_or_else(|err| tcx.sess.emit_fatal(FailedCreateTempdir { err }));
let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
let metadata_filename = metadata_tmpdir.as_ref().join(METADATA_FILENAME);
let metadata_kind = tcx.metadata_kind();
match metadata_kind {
MetadataKind::None => {
std::fs::File::create(&metadata_filename).unwrap_or_else(|err| {
tcx.sess.emit_fatal(FailedCreateFile { filename: &metadata_filename, err });
});
}
MetadataKind::Uncompressed | MetadataKind::Compressed => {
encode_metadata(tcx, &metadata_filename);
}
};
let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
let (metadata_filename, metadata_tmpdir) = if need_metadata_file {
let filename = match out_filename {
OutFileName::Real(ref path) => {
if let Err(err) = non_durable_rename(&metadata_filename, path) {
tcx.sess.emit_fatal(FailedWriteError { filename: path.to_path_buf(), err });
}
path.clone()
}
OutFileName::Stdout => {
if out_filename.is_tty() {
tcx.sess.emit_err(BinaryOutputToTty);
} else if let Err(err) = copy_to_stdout(&metadata_filename) {
tcx.sess
.emit_err(FailedCopyToStdout { filename: metadata_filename.clone(), err });
}
metadata_filename
}
};
if tcx.sess.opts.json_artifact_notifications {
tcx.sess
.parse_sess
.span_diagnostic
.emit_artifact_notification(&out_filename.as_path(), "metadata");
}
(filename, None)
} else {
(metadata_filename, Some(metadata_tmpdir))
};
let metadata =
EncodedMetadata::from_path(metadata_filename, metadata_tmpdir).unwrap_or_else(|err| {
tcx.sess.emit_fatal(FailedCreateEncodedMetadata { err });
});
let need_metadata_module = metadata_kind == MetadataKind::Compressed;
(metadata, need_metadata_module)
}
#[cfg(not(target_os = "linux"))]
pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
std::fs::rename(src, dst)
}
#[cfg(target_os = "linux")]
pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> {
let _ = std::fs::remove_file(dst);
std::fs::rename(src, dst)
}
pub fn copy_to_stdout(from: &Path) -> io::Result<()> {
let file = fs::File::open(from)?;
let mut reader = io::BufReader::new(file);
let mut stdout = io::stdout();
io::copy(&mut reader, &mut stdout)?;
Ok(())
}