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 rustc::middle::cstore::LOCAL_CRATE;
12 use rustc::session::Session;
13 use rustc::ty::TyCtxt;
17 use std::path::{Path, PathBuf};
20 pub fn dep_graph_path(tcx: TyCtxt) -> Option<PathBuf> {
21 tcx_path(tcx, LOCAL_CRATE, "local")
24 pub fn metadata_hash_path(tcx: TyCtxt, cnum: ast::CrateNum) -> Option<PathBuf> {
25 tcx_path(tcx, cnum, "metadata")
28 pub fn tcx_work_products_path(tcx: TyCtxt) -> Option<PathBuf> {
29 let crate_name = tcx.crate_name(LOCAL_CRATE);
30 sess_work_products_path(tcx.sess, &crate_name)
33 pub fn sess_work_products_path(sess: &Session,
34 local_crate_name: &str)
36 let crate_disambiguator = sess.local_crate_disambiguator();
37 path(sess, local_crate_name, &crate_disambiguator, "work-products")
40 pub fn in_incr_comp_dir(sess: &Session, file_name: &str) -> Option<PathBuf> {
41 sess.opts.incremental.as_ref().map(|incr_dir| incr_dir.join(file_name))
44 fn tcx_path(tcx: TyCtxt,
48 path(tcx.sess, &tcx.crate_name(cnum), &tcx.crate_disambiguator(cnum), middle)
51 fn path(sess: &Session,
53 crate_disambiguator: &str,
56 // For now, just save/load dep-graph from
57 // directory/dep_graph.rbml
58 sess.opts.incremental.as_ref().and_then(|incr_dir| {
59 match create_dir_racy(&incr_dir) {
63 &format!("could not create the directory `{}`: {}",
64 incr_dir.display(), err));
69 let file_name = format!("{}-{}.{}.bin", crate_name, crate_disambiguator, middle);
71 Some(incr_dir.join(file_name))
75 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
76 // threads or processes.
77 fn create_dir_racy(path: &Path) -> io::Result<()> {
78 match fs::create_dir(path) {
79 Ok(()) => return Ok(()),
80 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
81 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
82 Err(e) => return Err(e),
85 Some(p) => try!(create_dir_racy(p)),
86 None => return Err(io::Error::new(io::ErrorKind::Other,
87 "failed to create whole tree")),
89 match fs::create_dir(path) {
91 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),