1 //! Functions for saving and removing intermediate [work products].
3 //! [work products]: WorkProduct
5 use crate::persist::fs::*;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_fs_util::link_or_copy;
8 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
9 use rustc_session::Session;
10 use std::fs as std_fs;
13 /// Copies a CGU work product to the incremental compilation directory, so next compilation can find and reuse it.
14 pub fn copy_cgu_workproduct_to_incr_comp_cache_dir(
17 files: &[(&'static str, &Path)],
18 ) -> Option<(WorkProductId, WorkProduct)> {
19 debug!(?cgu_name, ?files);
20 sess.opts.incremental.as_ref()?;
22 let mut saved_files = FxHashMap::default();
23 for (ext, path) in files {
24 let file_name = format!("{cgu_name}.{ext}");
25 let path_in_incr_dir = in_incr_comp_dir_sess(sess, &file_name);
26 match link_or_copy(path, &path_in_incr_dir) {
28 let _ = saved_files.insert(ext.to_string(), file_name);
32 "error copying object file `{}` to incremental directory as `{}`: {}",
34 path_in_incr_dir.display(),
41 let work_product = WorkProduct { cgu_name: cgu_name.to_string(), saved_files };
42 debug!(?work_product);
43 let work_product_id = WorkProductId::from_cgu_name(cgu_name);
44 Some((work_product_id, work_product))
47 /// Removes files for a given work product.
48 pub fn delete_workproduct_files(sess: &Session, work_product: &WorkProduct) {
49 for (_, path) in &work_product.saved_files {
50 let path = in_incr_comp_dir_sess(sess, path);
51 if let Err(err) = std_fs::remove_file(&path) {
53 "file-system error deleting outdated file `{}`: {}",