rustc_incremental/persist/
work_product.rs

1//! Functions for saving and removing intermediate [work products].
2//!
3//! [work products]: WorkProduct
4
5use std::fs as std_fs;
6use std::path::Path;
7
8use rustc_data_structures::unord::UnordMap;
9use rustc_fs_util::link_or_copy;
10use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
11use rustc_session::Session;
12use tracing::debug;
13
14use crate::errors;
15use crate::persist::fs::*;
16
17/// Copies a CGU work product to the incremental compilation directory, so next compilation can
18/// find and reuse it.
19pub fn copy_cgu_workproduct_to_incr_comp_cache_dir(
20    sess: &Session,
21    cgu_name: &str,
22    files: &[(&'static str, &Path)],
23) -> Option<(WorkProductId, WorkProduct)> {
24    debug!(?cgu_name, ?files);
25    sess.opts.incremental.as_ref()?;
26
27    let mut saved_files = UnordMap::default();
28    for (ext, path) in files {
29        let file_name = format!("{cgu_name}.{ext}");
30        let path_in_incr_dir = in_incr_comp_dir_sess(sess, &file_name);
31        match link_or_copy(path, &path_in_incr_dir) {
32            Ok(_) => {
33                let _ = saved_files.insert(ext.to_string(), file_name);
34            }
35            Err(err) => {
36                sess.dcx().emit_warn(errors::CopyWorkProductToCache {
37                    from: path,
38                    to: &path_in_incr_dir,
39                    err,
40                });
41            }
42        }
43    }
44
45    let work_product = WorkProduct { cgu_name: cgu_name.to_string(), saved_files };
46    debug!(?work_product);
47    let work_product_id = WorkProductId::from_cgu_name(cgu_name);
48    Some((work_product_id, work_product))
49}
50
51/// Removes files for a given work product.
52pub(crate) fn delete_workproduct_files(sess: &Session, work_product: &WorkProduct) {
53    for (_, path) in work_product.saved_files.items().into_sorted_stable_ord() {
54        let path = in_incr_comp_dir_sess(sess, path);
55        if let Err(err) = std_fs::remove_file(&path) {
56            sess.dcx().emit_warn(errors::DeleteWorkProduct { path: &path, err });
57        }
58    }
59}