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.
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.
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;
19 use std::path::{Path, PathBuf};
22 pub fn dep_graph_path(tcx: TyCtxt) -> Option<PathBuf> {
23 path(tcx, LOCAL_CRATE, "local")
26 pub fn metadata_hash_path(tcx: TyCtxt, cnum: ast::CrateNum) -> Option<PathBuf> {
27 path(tcx, cnum, "metadata")
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) {
38 &format!("could not create the directory `{}`: {}",
39 incr_dir.display(), err));
44 let crate_name = tcx.crate_name(cnum);
45 let crate_disambiguator = tcx.crate_disambiguator(cnum);
46 let file_name = format!("{}-{}.{}.bin",
50 Some(incr_dir.join(file_name))
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),
64 Some(p) => try!(create_dir_racy(p)),
65 None => return Err(io::Error::new(io::ErrorKind::Other,
66 "failed to create whole tree")),
68 match fs::create_dir(path) {
70 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),
75 pub trait DepNodeHash {
76 /// Hash this dep-node, if it is of the kind that we know how to
78 fn hash<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<u64>;
81 impl DepNodeHash for DepNode<DefId> {
82 fn hash<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<u64> {
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))