2013-04-18 04:51:12 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2012-11-23 01:24:30 -06:00
|
|
|
use json;
|
2013-09-06 20:59:29 -05:00
|
|
|
use json::ToJson;
|
2012-12-18 20:05:16 -06:00
|
|
|
use serialize::{Encoder, Encodable, Decoder, Decodable};
|
2013-07-22 15:57:40 -05:00
|
|
|
use arc::{Arc,RWArc};
|
2013-07-17 18:10:11 -05:00
|
|
|
use treemap::TreeMap;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::cell::Cell;
|
2013-08-01 01:12:20 -05:00
|
|
|
use std::comm::{PortOne, oneshot};
|
2013-10-25 19:04:37 -05:00
|
|
|
use std::{str, task};
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
|
|
|
use std::io::{File, Decorator};
|
|
|
|
use std::io::mem::MemWriter;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2012-11-22 20:20:28 -06:00
|
|
|
/**
|
|
|
|
*
|
2013-04-18 04:51:12 -05:00
|
|
|
* This is a loose clone of the [fbuild build system](https://github.com/felix-lang/fbuild),
|
|
|
|
* made a touch more generic (not wired to special cases on files) and much
|
|
|
|
* less metaprogram-y due to rust's comparative weakness there, relative to
|
|
|
|
* python.
|
2012-11-22 20:20:28 -06:00
|
|
|
*
|
2013-04-18 04:51:12 -05:00
|
|
|
* It's based around _imperative builds_ that happen to have some function
|
2012-11-22 20:20:28 -06:00
|
|
|
* calls cached. That is, it's _just_ a mechanism for describing cached
|
|
|
|
* functions. This makes it much simpler and smaller than a "build system"
|
|
|
|
* that produces an IR and evaluates it. The evaluation order is normal
|
|
|
|
* function calls. Some of them just return really quickly.
|
|
|
|
*
|
|
|
|
* A cached function consumes and produces a set of _works_. A work has a
|
|
|
|
* name, a kind (that determines how the value is to be checked for
|
|
|
|
* freshness) and a value. Works must also be (de)serializable. Some
|
|
|
|
* examples of works:
|
|
|
|
*
|
|
|
|
* kind name value
|
|
|
|
* ------------------------
|
|
|
|
* cfg os linux
|
|
|
|
* file foo.c <sha1>
|
|
|
|
* url foo.com <etag>
|
|
|
|
*
|
|
|
|
* Works are conceptually single units, but we store them most of the time
|
|
|
|
* in maps of the form (type,name) => value. These are WorkMaps.
|
|
|
|
*
|
2013-04-18 04:51:12 -05:00
|
|
|
* A cached function divides the works it's interested in into inputs and
|
2013-01-07 15:08:43 -06:00
|
|
|
* outputs, and subdivides those into declared (input) works and
|
2012-11-22 20:20:28 -06:00
|
|
|
* discovered (input and output) works.
|
|
|
|
*
|
2013-01-07 15:08:43 -06:00
|
|
|
* A _declared_ input or is one that is given to the workcache before
|
2012-11-22 20:20:28 -06:00
|
|
|
* any work actually happens, in the "prep" phase. Even when a function's
|
|
|
|
* work-doing part (the "exec" phase) never gets called, it has declared
|
2013-01-07 15:08:43 -06:00
|
|
|
* inputs, which can be checked for freshness (and potentially
|
2012-11-22 20:20:28 -06:00
|
|
|
* used to determine that the function can be skipped).
|
|
|
|
*
|
|
|
|
* The workcache checks _all_ works for freshness, but uses the set of
|
|
|
|
* discovered outputs from the _previous_ exec (which it will re-discover
|
|
|
|
* and re-record each time the exec phase runs).
|
|
|
|
*
|
|
|
|
* Therefore the discovered works cached in the db might be a
|
|
|
|
* mis-approximation of the current discoverable works, but this is ok for
|
|
|
|
* the following reason: we assume that if an artifact A changed from
|
|
|
|
* depending on B,C,D to depending on B,C,D,E, then A itself changed (as
|
|
|
|
* part of the change-in-dependencies), so we will be ok.
|
|
|
|
*
|
|
|
|
* Each function has a single discriminated output work called its _result_.
|
|
|
|
* This is only different from other works in that it is returned, by value,
|
|
|
|
* from a call to the cacheable function; the other output works are used in
|
|
|
|
* passing to invalidate dependencies elsewhere in the cache, but do not
|
|
|
|
* otherwise escape from a function invocation. Most functions only have one
|
|
|
|
* output work anyways.
|
|
|
|
*
|
|
|
|
* A database (the central store of a workcache) stores a mappings:
|
|
|
|
*
|
2013-01-07 15:08:43 -06:00
|
|
|
* (fn_name,{declared_input}) => ({discovered_input},
|
2012-11-22 20:20:28 -06:00
|
|
|
* {discovered_output},result)
|
|
|
|
*
|
2013-01-07 15:08:43 -06:00
|
|
|
* (Note: fbuild, which workcache is based on, has the concept of a declared
|
|
|
|
* output as separate from a discovered output. This distinction exists only
|
|
|
|
* as an artifact of how fbuild works: via annotations on function types
|
|
|
|
* and metaprogramming, with explicit dependency declaration as a fallback.
|
|
|
|
* Workcache is more explicit about dependencies, and as such treats all
|
|
|
|
* outputs the same, as discovered-during-the-last-run.)
|
|
|
|
*
|
2012-11-22 20:20:28 -06:00
|
|
|
*/
|
|
|
|
|
2013-07-17 18:10:11 -05:00
|
|
|
#[deriving(Clone, Eq, Encodable, Decodable, TotalOrd, TotalEq)]
|
2012-11-22 20:20:28 -06:00
|
|
|
struct WorkKey {
|
|
|
|
kind: ~str,
|
|
|
|
name: ~str
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl WorkKey {
|
|
|
|
pub fn new(kind: &str, name: &str) -> WorkKey {
|
|
|
|
WorkKey {
|
|
|
|
kind: kind.to_owned(),
|
|
|
|
name: name.to_owned(),
|
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 20:59:29 -05:00
|
|
|
// FIXME #8883: The key should be a WorkKey and not a ~str.
|
|
|
|
// This is working around some JSON weirdness.
|
|
|
|
#[deriving(Clone, Eq, Encodable, Decodable)]
|
|
|
|
struct WorkMap(TreeMap<~str, KindMap>);
|
|
|
|
|
2013-07-17 18:10:11 -05:00
|
|
|
#[deriving(Clone, Eq, Encodable, Decodable)]
|
2013-09-06 20:59:29 -05:00
|
|
|
struct KindMap(TreeMap<~str, ~str>);
|
2013-07-02 14:47:32 -05:00
|
|
|
|
2013-03-29 11:04:35 -05:00
|
|
|
impl WorkMap {
|
2013-07-17 18:10:11 -05:00
|
|
|
fn new() -> WorkMap { WorkMap(TreeMap::new()) }
|
2013-09-06 20:59:29 -05:00
|
|
|
|
|
|
|
fn insert_work_key(&mut self, k: WorkKey, val: ~str) {
|
|
|
|
let WorkKey { kind, name } = k;
|
|
|
|
match self.find_mut(&name) {
|
|
|
|
Some(&KindMap(ref mut m)) => { m.insert(kind, val); return; }
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
let mut new_map = TreeMap::new();
|
|
|
|
new_map.insert(kind, val);
|
|
|
|
self.insert(name, KindMap(new_map));
|
|
|
|
}
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub struct Database {
|
2013-10-19 19:33:09 -05:00
|
|
|
priv db_filename: Path,
|
|
|
|
priv db_cache: TreeMap<~str, ~str>,
|
2013-03-28 14:13:38 -05:00
|
|
|
db_dirty: bool
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Database {
|
2013-07-17 23:12:36 -05:00
|
|
|
|
|
|
|
pub fn new(p: Path) -> Database {
|
2013-09-06 20:59:29 -05:00
|
|
|
let mut rslt = Database {
|
2013-07-17 23:12:36 -05:00
|
|
|
db_filename: p,
|
|
|
|
db_cache: TreeMap::new(),
|
|
|
|
db_dirty: false
|
2013-09-06 20:59:29 -05:00
|
|
|
};
|
2013-10-25 19:04:37 -05:00
|
|
|
if rslt.db_filename.exists() {
|
2013-09-06 20:59:29 -05:00
|
|
|
rslt.load();
|
2013-07-17 23:12:36 -05:00
|
|
|
}
|
2013-09-06 20:59:29 -05:00
|
|
|
rslt
|
2013-07-17 23:12:36 -05:00
|
|
|
}
|
|
|
|
|
2013-07-17 18:18:43 -05:00
|
|
|
pub fn prepare(&self,
|
2013-05-31 17:17:22 -05:00
|
|
|
fn_name: &str,
|
|
|
|
declared_inputs: &WorkMap)
|
|
|
|
-> Option<(WorkMap, WorkMap, ~str)> {
|
2013-01-07 15:08:43 -06:00
|
|
|
let k = json_encode(&(fn_name, declared_inputs));
|
2013-01-28 15:06:09 -06:00
|
|
|
match self.db_cache.find(&k) {
|
2013-01-07 15:08:43 -06:00
|
|
|
None => None,
|
2013-03-03 16:49:44 -06:00
|
|
|
Some(v) => Some(json_decode(*v))
|
2013-01-07 15:08:43 -06:00
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
2013-01-07 15:08:43 -06:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cache(&mut self,
|
|
|
|
fn_name: &str,
|
|
|
|
declared_inputs: &WorkMap,
|
|
|
|
discovered_inputs: &WorkMap,
|
|
|
|
discovered_outputs: &WorkMap,
|
|
|
|
result: &str) {
|
2013-01-07 15:08:43 -06:00
|
|
|
let k = json_encode(&(fn_name, declared_inputs));
|
|
|
|
let v = json_encode(&(discovered_inputs,
|
|
|
|
discovered_outputs,
|
|
|
|
result));
|
|
|
|
self.db_cache.insert(k,v);
|
|
|
|
self.db_dirty = true
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
2013-09-06 20:59:29 -05:00
|
|
|
|
|
|
|
// FIXME #4330: This should have &mut self and should set self.db_dirty to false.
|
|
|
|
fn save(&self) {
|
2013-10-30 01:31:07 -05:00
|
|
|
let f = @mut File::create(&self.db_filename);
|
2013-10-13 20:48:47 -05:00
|
|
|
self.db_cache.to_json().to_pretty_writer(f as @mut io::Writer);
|
2013-09-06 20:59:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load(&mut self) {
|
|
|
|
assert!(!self.db_dirty);
|
2013-10-25 19:04:37 -05:00
|
|
|
assert!(self.db_filename.exists());
|
2013-10-30 01:31:07 -05:00
|
|
|
match io::result(|| File::open(&self.db_filename)) {
|
2013-10-25 19:04:37 -05:00
|
|
|
Err(e) => fail!("Couldn't load workcache database {}: {}",
|
|
|
|
self.db_filename.display(),
|
|
|
|
e.desc),
|
|
|
|
Ok(r) =>
|
|
|
|
match json::from_reader(@mut r.unwrap() as @mut io::Reader) {
|
2013-10-21 15:08:31 -05:00
|
|
|
Err(e) => fail!("Couldn't parse workcache database (from file {}): {}",
|
2013-09-26 19:21:59 -05:00
|
|
|
self.db_filename.display(), e.to_str()),
|
2013-09-06 20:59:29 -05:00
|
|
|
Ok(r) => {
|
2013-11-29 13:11:52 -06:00
|
|
|
let mut decoder = json::Decoder::init(r);
|
2013-09-06 20:59:29 -05:00
|
|
|
self.db_cache = Decodable::decode(&mut decoder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl Drop for Database {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-09-06 20:59:29 -05:00
|
|
|
if self.db_dirty {
|
|
|
|
self.save();
|
|
|
|
}
|
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub struct Logger {
|
2013-01-10 17:16:15 -06:00
|
|
|
// FIXME #4432: Fill in
|
2013-10-19 19:33:09 -05:00
|
|
|
priv a: ()
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Logger {
|
2013-07-17 23:12:36 -05:00
|
|
|
|
|
|
|
pub fn new() -> Logger {
|
|
|
|
Logger { a: () }
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn info(&self, i: &str) {
|
2013-10-21 15:08:31 -05:00
|
|
|
info!("workcache: {}", i);
|
2012-12-11 15:47:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub type FreshnessMap = TreeMap<~str,extern fn(&str,&str)->bool>;
|
2013-09-06 20:59:29 -05:00
|
|
|
|
2013-07-18 15:05:18 -05:00
|
|
|
#[deriving(Clone)]
|
2013-08-07 02:11:34 -05:00
|
|
|
pub struct Context {
|
2013-07-22 15:57:40 -05:00
|
|
|
db: RWArc<Database>,
|
2013-10-19 19:33:09 -05:00
|
|
|
priv logger: RWArc<Logger>,
|
|
|
|
priv cfg: Arc<json::Object>,
|
2013-09-06 20:59:29 -05:00
|
|
|
/// Map from kinds (source, exe, url, etc.) to a freshness function.
|
|
|
|
/// The freshness function takes a name (e.g. file path) and value
|
|
|
|
/// (e.g. hash of file contents) and determines whether it's up-to-date.
|
|
|
|
/// For example, in the file case, this would read the file off disk,
|
|
|
|
/// hash it, and return the result of comparing the given hash and the
|
|
|
|
/// read hash for equality.
|
2013-10-19 19:33:09 -05:00
|
|
|
priv freshness: Arc<FreshnessMap>
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub struct Prep<'self> {
|
2013-10-19 19:33:09 -05:00
|
|
|
priv ctxt: &'self Context,
|
|
|
|
priv fn_name: &'self str,
|
|
|
|
priv declared_inputs: WorkMap,
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:11:34 -05:00
|
|
|
pub struct Exec {
|
2013-10-19 19:33:09 -05:00
|
|
|
priv discovered_inputs: WorkMap,
|
|
|
|
priv discovered_outputs: WorkMap
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-09-13 21:07:51 -05:00
|
|
|
enum Work<'self, T> {
|
|
|
|
WorkValue(T),
|
|
|
|
WorkFromTask(&'self Prep<'self>, PortOne<(Exec, T)>),
|
2013-01-28 12:46:43 -06:00
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn json_encode<'self, T:Encodable<json::Encoder<'self>>>(t: &T) -> ~str {
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
let mut encoder = json::Encoder::init(&mut writer as &mut io::Writer);
|
2013-10-13 20:48:47 -05:00
|
|
|
t.encode(&mut encoder);
|
2013-11-28 06:52:11 -06:00
|
|
|
str::from_utf8_owned(writer.inner())
|
2013-05-01 19:54:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(#5121)
|
2013-03-29 11:04:35 -05:00
|
|
|
fn json_decode<T:Decodable<json::Decoder>>(s: &str) -> T {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("json decoding: {}", s);
|
2013-10-13 20:48:47 -05:00
|
|
|
let j = json::from_str(s).unwrap();
|
2013-11-29 13:11:52 -06:00
|
|
|
let mut decoder = json::Decoder::init(j);
|
2013-10-13 20:48:47 -05:00
|
|
|
Decodable::decode(&mut decoder)
|
2013-01-07 15:08:43 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Context {
|
2013-07-17 23:12:36 -05:00
|
|
|
|
2013-07-22 15:57:40 -05:00
|
|
|
pub fn new(db: RWArc<Database>,
|
|
|
|
lg: RWArc<Logger>,
|
|
|
|
cfg: Arc<json::Object>) -> Context {
|
2013-09-06 20:59:29 -05:00
|
|
|
Context::new_with_freshness(db, lg, cfg, Arc::new(TreeMap::new()))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_freshness(db: RWArc<Database>,
|
2013-09-09 00:00:13 -05:00
|
|
|
lg: RWArc<Logger>,
|
|
|
|
cfg: Arc<json::Object>,
|
|
|
|
freshness: Arc<FreshnessMap>) -> Context {
|
2013-03-29 11:04:35 -05:00
|
|
|
Context {
|
|
|
|
db: db,
|
|
|
|
logger: lg,
|
|
|
|
cfg: cfg,
|
2013-09-06 20:59:29 -05:00
|
|
|
freshness: freshness
|
2013-03-29 11:04:35 -05:00
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-07-17 23:12:36 -05:00
|
|
|
pub fn prep<'a>(&'a self, fn_name: &'a str) -> Prep<'a> {
|
|
|
|
Prep::new(self, fn_name)
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn with_prep<'a,
|
|
|
|
T>(
|
|
|
|
&'a self,
|
|
|
|
fn_name: &'a str,
|
|
|
|
blk: |p: &mut Prep| -> T)
|
|
|
|
-> T {
|
2013-07-17 23:12:36 -05:00
|
|
|
let mut p = self.prep(fn_name);
|
|
|
|
blk(&mut p)
|
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
|
2013-01-07 15:08:43 -06:00
|
|
|
}
|
|
|
|
|
2013-09-06 20:59:29 -05:00
|
|
|
impl Exec {
|
2013-09-09 00:00:13 -05:00
|
|
|
pub fn discover_input(&mut self,
|
|
|
|
dependency_kind: &str,
|
|
|
|
dependency_name: &str,
|
|
|
|
dependency_val: &str) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Discovering input {} {} {}", dependency_kind, dependency_name, dependency_val);
|
2013-09-06 20:59:29 -05:00
|
|
|
self.discovered_inputs.insert_work_key(WorkKey::new(dependency_kind, dependency_name),
|
|
|
|
dependency_val.to_owned());
|
|
|
|
}
|
2013-09-09 00:00:13 -05:00
|
|
|
pub fn discover_output(&mut self,
|
|
|
|
dependency_kind: &str,
|
|
|
|
dependency_name: &str,
|
|
|
|
dependency_val: &str) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Discovering output {} {} {}", dependency_kind, dependency_name, dependency_val);
|
2013-09-06 20:59:29 -05:00
|
|
|
self.discovered_outputs.insert_work_key(WorkKey::new(dependency_kind, dependency_name),
|
|
|
|
dependency_val.to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns pairs of (kind, name)
|
|
|
|
pub fn lookup_discovered_inputs(&self) -> ~[(~str, ~str)] {
|
|
|
|
let mut rs = ~[];
|
|
|
|
for (k, v) in self.discovered_inputs.iter() {
|
|
|
|
for (k1, _) in v.iter() {
|
|
|
|
rs.push((k1.clone(), k.clone()));
|
2013-09-09 00:00:13 -05:00
|
|
|
}
|
2013-09-06 20:59:29 -05:00
|
|
|
}
|
|
|
|
rs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 23:12:36 -05:00
|
|
|
impl<'self> Prep<'self> {
|
|
|
|
fn new(ctxt: &'self Context, fn_name: &'self str) -> Prep<'self> {
|
|
|
|
Prep {
|
|
|
|
ctxt: ctxt,
|
|
|
|
fn_name: fn_name,
|
|
|
|
declared_inputs: WorkMap::new()
|
|
|
|
}
|
|
|
|
}
|
2013-09-06 20:59:29 -05:00
|
|
|
|
|
|
|
pub fn lookup_declared_inputs(&self) -> ~[~str] {
|
|
|
|
let mut rs = ~[];
|
|
|
|
for (_, v) in self.declared_inputs.iter() {
|
|
|
|
for (inp, _) in v.iter() {
|
|
|
|
rs.push(inp.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rs
|
|
|
|
}
|
2013-07-17 23:12:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Prep<'self> {
|
2013-09-09 00:00:13 -05:00
|
|
|
pub fn declare_input(&mut self, kind: &str, name: &str, val: &str) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Declaring input {} {} {}", kind, name, val);
|
2013-09-06 20:59:29 -05:00
|
|
|
self.declared_inputs.insert_work_key(WorkKey::new(kind, name),
|
2013-03-28 14:13:38 -05:00
|
|
|
val.to_owned());
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:08:43 -06:00
|
|
|
fn is_fresh(&self, cat: &str, kind: &str,
|
|
|
|
name: &str, val: &str) -> bool {
|
2013-03-28 14:13:38 -05:00
|
|
|
let k = kind.to_owned();
|
2013-07-18 15:05:18 -05:00
|
|
|
let f = self.ctxt.freshness.get().find(&k);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("freshness for: {}/{}/{}/{}", cat, kind, name, val)
|
2013-07-17 18:10:11 -05:00
|
|
|
let fresh = match f {
|
2013-10-21 15:08:31 -05:00
|
|
|
None => fail!("missing freshness-function for '{}'", kind),
|
2013-07-17 18:10:11 -05:00
|
|
|
Some(f) => (*f)(name, val)
|
|
|
|
};
|
2013-11-20 17:46:49 -06:00
|
|
|
self.ctxt.logger.write(|lg| {
|
2013-07-18 15:05:18 -05:00
|
|
|
if fresh {
|
2013-09-27 22:18:50 -05:00
|
|
|
lg.info(format!("{} {}:{} is fresh",
|
2013-07-18 15:05:18 -05:00
|
|
|
cat, kind, name));
|
|
|
|
} else {
|
2013-09-27 22:18:50 -05:00
|
|
|
lg.info(format!("{} {}:{} is not fresh",
|
2013-07-18 15:05:18 -05:00
|
|
|
cat, kind, name))
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-07-17 18:10:11 -05:00
|
|
|
fresh
|
2012-12-11 15:47:53 -06:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:08:43 -06:00
|
|
|
fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool {
|
2013-09-06 20:59:29 -05:00
|
|
|
for (k_name, kindmap) in map.iter() {
|
|
|
|
for (k_kind, v) in kindmap.iter() {
|
|
|
|
if ! self.is_fresh(cat, *k_kind, *k_name, *v) {
|
|
|
|
return false;
|
2012-12-11 15:47:53 -06:00
|
|
|
}
|
2013-09-06 20:59:29 -05:00
|
|
|
}
|
2012-12-11 15:47:53 -06:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
pub fn exec<'self, T:Send +
|
|
|
|
Encodable<json::Encoder<'self>> +
|
2013-07-17 23:12:36 -05:00
|
|
|
Decodable<json::Decoder>>(
|
2013-11-18 17:39:02 -06:00
|
|
|
&'self self, blk: proc(&mut Exec) -> T) -> T {
|
2013-07-17 23:12:36 -05:00
|
|
|
self.exec_work(blk).unwrap()
|
|
|
|
}
|
|
|
|
|
2013-11-29 12:12:08 -06:00
|
|
|
fn exec_work<'self, T:Send +
|
|
|
|
Encodable<json::Encoder<'self>> +
|
2013-07-17 23:12:36 -05:00
|
|
|
Decodable<json::Decoder>>( // FIXME(#5121)
|
2013-11-18 17:39:02 -06:00
|
|
|
&'self self, blk: proc(&mut Exec) -> T) -> Work<'self, T> {
|
2013-02-15 01:30:30 -06:00
|
|
|
let mut bo = Some(blk);
|
2012-11-22 20:20:28 -06:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("exec_work: looking up {} and {:?}", self.fn_name,
|
2013-09-06 20:59:29 -05:00
|
|
|
self.declared_inputs);
|
2013-11-20 17:46:49 -06:00
|
|
|
let cached = self.ctxt.db.read(|db| {
|
2013-07-17 18:18:43 -05:00
|
|
|
db.prepare(self.fn_name, &self.declared_inputs)
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2012-12-11 15:47:53 -06:00
|
|
|
|
2013-09-13 21:07:51 -05:00
|
|
|
match cached {
|
2013-03-28 14:13:38 -05:00
|
|
|
Some((ref disc_in, ref disc_out, ref res))
|
2013-07-17 23:12:36 -05:00
|
|
|
if self.all_fresh("declared input",&self.declared_inputs) &&
|
|
|
|
self.all_fresh("discovered input", disc_in) &&
|
|
|
|
self.all_fresh("discovered output", disc_out) => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Cache hit!");
|
|
|
|
debug!("Trying to decode: {:?} / {:?} / {}",
|
2013-09-06 20:59:29 -05:00
|
|
|
disc_in, disc_out, *res);
|
2013-09-13 21:07:51 -05:00
|
|
|
Work::from_value(json_decode(*res))
|
2013-03-28 14:13:38 -05:00
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
|
2013-03-28 14:13:38 -05:00
|
|
|
_ => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Cache miss!");
|
2013-04-26 20:52:15 -05:00
|
|
|
let (port, chan) = oneshot();
|
2013-07-17 16:41:50 -05:00
|
|
|
let blk = bo.take_unwrap();
|
2013-06-04 05:03:58 -05:00
|
|
|
let chan = Cell::new(chan);
|
2013-03-28 14:13:38 -05:00
|
|
|
|
2013-11-20 17:46:49 -06:00
|
|
|
// XXX: What happens if the task fails?
|
2013-04-26 20:52:15 -05:00
|
|
|
do task::spawn {
|
2013-09-06 20:59:29 -05:00
|
|
|
let mut exe = Exec {
|
2013-03-28 14:13:38 -05:00
|
|
|
discovered_inputs: WorkMap::new(),
|
|
|
|
discovered_outputs: WorkMap::new(),
|
|
|
|
};
|
|
|
|
let chan = chan.take();
|
2013-09-06 20:59:29 -05:00
|
|
|
let v = blk(&mut exe);
|
2013-08-01 01:12:20 -05:00
|
|
|
chan.send((exe, v));
|
2013-01-07 15:08:43 -06:00
|
|
|
}
|
2013-09-13 21:07:51 -05:00
|
|
|
Work::from_task(self, port)
|
2013-01-07 15:08:43 -06:00
|
|
|
}
|
2013-09-13 21:07:51 -05:00
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 23:12:36 -05:00
|
|
|
impl<'self, T:Send +
|
2013-11-29 12:12:08 -06:00
|
|
|
Encodable<json::Encoder<'self>> +
|
2013-07-17 23:12:36 -05:00
|
|
|
Decodable<json::Decoder>>
|
|
|
|
Work<'self, T> { // FIXME(#5121)
|
|
|
|
|
2013-09-13 21:07:51 -05:00
|
|
|
pub fn from_value(elt: T) -> Work<'self, T> {
|
|
|
|
WorkValue(elt)
|
|
|
|
}
|
|
|
|
pub fn from_task(prep: &'self Prep<'self>, port: PortOne<(Exec, T)>)
|
|
|
|
-> Work<'self, T> {
|
|
|
|
WorkFromTask(prep, port)
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
|
2013-07-17 18:33:38 -05:00
|
|
|
pub fn unwrap(self) -> T {
|
2013-09-13 21:07:51 -05:00
|
|
|
match self {
|
|
|
|
WorkValue(v) => v,
|
|
|
|
WorkFromTask(prep, port) => {
|
2013-08-01 01:12:20 -05:00
|
|
|
let (exe, v) = port.recv();
|
2013-07-17 18:33:38 -05:00
|
|
|
let s = json_encode(&v);
|
2013-11-20 17:46:49 -06:00
|
|
|
prep.ctxt.db.write(|db| {
|
2013-07-17 18:33:38 -05:00
|
|
|
db.cache(prep.fn_name,
|
|
|
|
&prep.declared_inputs,
|
|
|
|
&exe.discovered_inputs,
|
|
|
|
&exe.discovered_outputs,
|
2013-11-20 17:46:49 -06:00
|
|
|
s)
|
|
|
|
});
|
2013-07-17 18:33:38 -05:00
|
|
|
v
|
2013-07-17 18:18:43 -05:00
|
|
|
}
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 18:33:38 -05:00
|
|
|
|
2013-09-06 20:59:29 -05:00
|
|
|
#[test]
|
2013-11-14 02:11:11 -06:00
|
|
|
#[cfg(not(target_os="android"))] // FIXME(#10455)
|
2012-11-22 20:20:28 -06:00
|
|
|
fn test() {
|
2013-09-06 22:29:16 -05:00
|
|
|
use std::{os, run};
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io::fs;
|
2013-10-22 22:16:12 -05:00
|
|
|
use std::str::from_utf8_owned;
|
2012-12-27 20:24:18 -06:00
|
|
|
|
2013-09-15 23:52:36 -05:00
|
|
|
// Create a path to a new file 'filename' in the directory in which
|
|
|
|
// this test is running.
|
|
|
|
fn make_path(filename: ~str) -> Path {
|
2013-10-05 21:49:32 -05:00
|
|
|
let pth = os::self_exe_path().expect("workcache::test failed").with_filename(filename);
|
2013-10-25 19:04:37 -05:00
|
|
|
if pth.exists() {
|
2013-10-31 17:15:30 -05:00
|
|
|
fs::unlink(&pth);
|
2013-09-15 23:52:36 -05:00
|
|
|
}
|
|
|
|
return pth;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pth = make_path(~"foo.c");
|
2013-10-30 01:31:07 -05:00
|
|
|
File::create(&pth).write(bytes!("int main() { return 0; }"));
|
2013-07-17 23:12:36 -05:00
|
|
|
|
2013-09-15 23:52:36 -05:00
|
|
|
let db_path = make_path(~"db.json");
|
2013-09-06 22:29:16 -05:00
|
|
|
|
|
|
|
let cx = Context::new(RWArc::new(Database::new(db_path)),
|
2013-07-22 15:57:40 -05:00
|
|
|
RWArc::new(Logger::new()),
|
|
|
|
Arc::new(TreeMap::new()));
|
2012-11-22 20:20:28 -06:00
|
|
|
|
2013-11-20 17:46:49 -06:00
|
|
|
let s = cx.with_prep("test1", |prep| {
|
2013-07-18 15:05:18 -05:00
|
|
|
|
|
|
|
let subcx = cx.clone();
|
2013-09-15 23:52:36 -05:00
|
|
|
let pth = pth.clone();
|
2013-07-18 15:05:18 -05:00
|
|
|
|
2013-10-30 01:31:07 -05:00
|
|
|
let file_content = from_utf8_owned(File::open(&pth).read_to_end());
|
2013-10-22 22:16:12 -05:00
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
2013-10-22 22:16:12 -05:00
|
|
|
prep.declare_input("file", pth.as_str().unwrap(), file_content);
|
2012-11-22 20:20:28 -06:00
|
|
|
do prep.exec |_exe| {
|
2013-09-15 23:52:36 -05:00
|
|
|
let out = make_path(~"foo.o");
|
2013-09-26 19:21:59 -05:00
|
|
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
|
|
|
run::process_status("gcc", [pth.as_str().unwrap().to_owned(),
|
|
|
|
~"-o",
|
|
|
|
out.as_str().unwrap().to_owned()]);
|
2013-07-18 15:05:18 -05:00
|
|
|
|
|
|
|
let _proof_of_concept = subcx.prep("subfn");
|
|
|
|
// Could run sub-rules inside here.
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
|
|
|
out.as_str().unwrap().to_owned()
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-09-15 23:52:36 -05:00
|
|
|
|
2013-10-13 20:48:47 -05:00
|
|
|
println(s);
|
2012-11-22 20:20:28 -06:00
|
|
|
}
|