]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_incremental/src/persist/work_product.rs
Merge commit '8d14c94b5c0a66241b4244f1c60ac5859cec1d97' into clippyup
[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_fs_util::link_or_copy;
7 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
8 use rustc_session::Session;
9 use std::fs as std_fs;
10 use std::path::PathBuf;
11
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(
14     sess: &Session,
15     cgu_name: &str,
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()?;
20
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),
26             Err(err) => {
27                 sess.warn(&format!(
28                     "error copying object file `{}` to incremental directory as `{}`: {}",
29                     path.display(),
30                     path_in_incr_dir.display(),
31                     err
32                 ));
33                 return None;
34             }
35         }
36     } else {
37         None
38     };
39
40     let work_product = WorkProduct { cgu_name: cgu_name.to_string(), saved_file };
41
42     let work_product_id = WorkProductId::from_cgu_name(cgu_name);
43     Some((work_product_id, work_product))
44 }
45
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) {
51             Ok(()) => {}
52             Err(err) => {
53                 sess.warn(&format!(
54                     "file-system error deleting outdated file `{}`: {}",
55                     path.display(),
56                     err
57                 ));
58             }
59         }
60     }
61 }