]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_incremental/src/persist/work_product.rs
Rollup merge of #99772 - ehuss:reenable-submodule-archive, r=Mark-Simulacrum
[rust.git] / compiler / rustc_incremental / src / persist / work_product.rs
1 //! Functions for saving and removing intermediate [work products].
2 //!
3 //! [work products]: WorkProduct
4
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;
11 use std::path::Path;
12
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(
15     sess: &Session,
16     cgu_name: &str,
17     files: &[(&'static str, &Path)],
18 ) -> Option<(WorkProductId, WorkProduct)> {
19     debug!(?cgu_name, ?files);
20     sess.opts.incremental.as_ref()?;
21
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) {
27             Ok(_) => {
28                 let _ = saved_files.insert(ext.to_string(), file_name);
29             }
30             Err(err) => {
31                 sess.warn(&format!(
32                     "error copying object file `{}` to incremental directory as `{}`: {}",
33                     path.display(),
34                     path_in_incr_dir.display(),
35                     err
36                 ));
37             }
38         }
39     }
40
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))
45 }
46
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) {
52             sess.warn(&format!(
53                 "file-system error deleting outdated file `{}`: {}",
54                 path.display(),
55                 err
56             ));
57         }
58     }
59 }