1 //! Functions for saving and removing intermediate [work products].
3 //! [work products]: WorkProduct
5 use crate::persist::fs::*;
6 use rustc_fs_util::link_or_copy;
7 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
8 use rustc_session::Session;
10 use std::path::PathBuf;
12 /// Copies a CGU work product to the incremental compilation directory, so next compilation can find and reuse it.
13 pub fn copy_cgu_workproduct_to_incr_comp_cache_dir(
16 path: &Option<PathBuf>,
17 ) -> Option<(WorkProductId, WorkProduct)> {
18 debug!("copy_cgu_workproduct_to_incr_comp_cache_dir({:?},{:?})", cgu_name, path);
19 sess.opts.incremental.as_ref()?;
21 let saved_file = if let Some(path) = path {
22 let file_name = format!("{}.o", cgu_name);
23 let path_in_incr_dir = in_incr_comp_dir_sess(sess, &file_name);
24 match link_or_copy(path, &path_in_incr_dir) {
25 Ok(_) => Some(file_name),
28 "error copying object file `{}` to incremental directory as `{}`: {}",
30 path_in_incr_dir.display(),
40 let work_product = WorkProduct { cgu_name: cgu_name.to_string(), saved_file };
42 let work_product_id = WorkProductId::from_cgu_name(cgu_name);
43 Some((work_product_id, work_product))
46 /// Removes files for a given work product.
47 pub fn delete_workproduct_files(sess: &Session, work_product: &WorkProduct) {
48 if let Some(ref file_name) = work_product.saved_file {
49 let path = in_incr_comp_dir_sess(sess, file_name);
50 match std_fs::remove_file(&path) {
54 "file-system error deleting outdated file `{}`: {}",