]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/persist/util.rs
make the filename computation take a cratenum
[rust.git] / src / librustc_incremental / persist / util.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use calculate_svh::SvhCalculate;
12 use rustc::dep_graph::DepNode;
13 use rustc::hir::def_id::DefId;
14 use rustc::middle::cstore::LOCAL_CRATE;
15 use rustc::ty::TyCtxt;
16
17 use std::fs;
18 use std::io;
19 use std::path::{Path, PathBuf};
20 use syntax::ast;
21
22 pub fn dep_graph_path(tcx: TyCtxt) -> Option<PathBuf> {
23     path(tcx, LOCAL_CRATE, "local")
24 }
25
26 pub fn metadata_hash_path(tcx: TyCtxt, cnum: ast::CrateNum) -> Option<PathBuf> {
27     path(tcx, cnum, "metadata")
28 }
29
30 fn path(tcx: TyCtxt, cnum: ast::CrateNum, suffix: &str) -> Option<PathBuf> {
31     // For now, just save/load dep-graph from
32     // directory/dep_graph.rbml
33     tcx.sess.opts.incremental.as_ref().and_then(|incr_dir| {
34         match create_dir_racy(&incr_dir) {
35             Ok(()) => {}
36             Err(err) => {
37                 tcx.sess.err(
38                     &format!("could not create the directory `{}`: {}",
39                              incr_dir.display(), err));
40                 return None;
41             }
42         }
43
44         let crate_name = tcx.crate_name(cnum);
45         let crate_disambiguator = tcx.crate_disambiguator(cnum);
46         let file_name = format!("{}-{}.{}.bin",
47                                 crate_name,
48                                 crate_disambiguator,
49                                 suffix);
50         Some(incr_dir.join(file_name))
51     })
52 }
53
54 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
55 // threads or processes.
56 fn create_dir_racy(path: &Path) -> io::Result<()> {
57     match fs::create_dir(path) {
58         Ok(()) => return Ok(()),
59         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
60         Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
61         Err(e) => return Err(e),
62     }
63     match path.parent() {
64         Some(p) => try!(create_dir_racy(p)),
65         None => return Err(io::Error::new(io::ErrorKind::Other,
66                                           "failed to create whole tree")),
67     }
68     match fs::create_dir(path) {
69         Ok(()) => Ok(()),
70         Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
71         Err(e) => Err(e),
72     }
73 }
74
75 pub trait DepNodeHash {
76     /// Hash this dep-node, if it is of the kind that we know how to
77     /// hash.
78     fn hash<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<u64>;
79 }
80
81 impl DepNodeHash for DepNode<DefId> {
82     fn hash<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<u64> {
83         match *self {
84             DepNode::Hir(def_id) => {
85                 // FIXME(#32753) -- should we use a distinct hash here
86                 assert!(def_id.is_local());
87                 Some(tcx.calculate_item_hash(def_id))
88             }
89             _ => None
90         }
91     }
92 }