sccache 0.1.0

Sccache is a ccache-like tool. It is used as a compiler wrapper and avoids compilation when possible, storing a cache in a remote storage using the S3 API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright 2016 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use cache::{
    Cache,
    Storage,
    hash_key,
};
use compiler::{
    clang,
    gcc,
    msvc,
};
use filetime::FileTime;
use futures::{self,Future};
use log::LogLevel::{Debug,Trace};
use mock_command::{
    CommandChild,
    CommandCreatorSync,
    RunCommand,
    exit_status,
};
use sha1;
use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fmt;
use std::fs::{self,File};
use std::io::prelude::*;
use std::io::{
    self,
    BufReader,
    Error,
    ErrorKind,
};
use std::path::Path;
use std::process::{self,Stdio};
use std::str;
use std::thread;
use std::time::Duration;
use tempdir::TempDir;

/// Supported compilers.
#[derive(Debug, PartialEq, Clone)]
pub enum CompilerKind {
    /// GCC
    Gcc,
    /// clang
    Clang,
    /// Microsoft Visual C++
    Msvc {
        /// The prefix used in the output of `-showIncludes`.
        includes_prefix: String,
    },
}

impl CompilerKind {
    pub fn parse_arguments(&self, arguments: &[String]) -> CompilerArguments {
        match *self {
            // GCC and clang share the same argument parsing logic, but
            // accept different sets of arguments.
            CompilerKind::Gcc => gcc::parse_arguments(arguments, gcc::argument_takes_value),
            CompilerKind::Clang => gcc::parse_arguments(arguments, clang::argument_takes_value),
            CompilerKind::Msvc { .. } => msvc::parse_arguments(arguments),
        }
    }

    pub fn preprocess<T : CommandCreatorSync>(&self, creator: T, compiler: &Compiler, parsed_args: &ParsedArguments, cwd: &str) -> io::Result<process::Output> {
        match *self {
            CompilerKind::Gcc | CompilerKind::Clang => {
                // GCC and clang use the same preprocessor invocation.
                gcc::preprocess(creator, compiler, parsed_args, cwd)
            },
            CompilerKind::Msvc { ref includes_prefix } => msvc::preprocess(creator, compiler, parsed_args, cwd, includes_prefix),
        }
    }

    pub fn compile<T : CommandCreatorSync>(&self, creator: T, compiler: &Compiler, preprocessor_output: Vec<u8>, parsed_args: &ParsedArguments, cwd: &str) -> io::Result<(Cacheable, process::Output)> {
        match *self {
            CompilerKind::Gcc => gcc::compile(creator, compiler, preprocessor_output, parsed_args, cwd),
            CompilerKind::Clang => clang::compile(creator, compiler, preprocessor_output, parsed_args, cwd),
            CompilerKind::Msvc { .. } => msvc::compile(creator, compiler, preprocessor_output, parsed_args, cwd),
        }
    }
}

/// The results of parsing a compiler commandline.
#[allow(dead_code)]
#[derive(Debug, PartialEq)]
pub struct ParsedArguments {
    /// The input source file.
    pub input: String,
    /// The file extension of the input source file.
    pub extension: String,
    /// The file in which to generate dependencies.
    pub depfile: Option<String>,
    /// Output files, keyed by a simple name, like "obj".
    pub outputs: HashMap<&'static str, String>,
    /// Commandline arguments for the preprocessor.
    pub preprocessor_args: Vec<String>,
    /// Commandline arguments for the preprocessor or the compiler.
    pub common_args: Vec<String>,
}

impl ParsedArguments {
    pub fn output_file(&self) -> Cow<str> {
        self.outputs.get("obj").and_then(|o| Path::new(o).file_name().map(|f| f.to_string_lossy())).unwrap_or(Cow::Borrowed("Unknown filename"))
    }
}

/// Possible results of parsing compiler arguments.
#[derive(Debug, PartialEq)]
pub enum CompilerArguments {
    /// Commandline can be handled.
    Ok(ParsedArguments),
    /// Cannot cache this compilation.
    CannotCache,
    /// This commandline is not a compile.
    NotCompilation,
}

/// Information about a compiler.
#[derive(Clone)]
pub struct Compiler {
    /// The path to the compiler binary.
    pub executable: String,
    /// The last modified time of `executable`.
    pub mtime: FileTime,
    /// The sha-1 digest of `executable`, as a hex string.
    pub digest: String,
    /// The kind of compiler, from the set of known compilers.
    pub kind: CompilerKind,
}

/// Specifics about cache misses.
#[derive(Debug, PartialEq)]
pub enum MissType {
    /// The compilation was not found in the cache, nothing more.
    Normal,
    /// There was a cache entry, but an error occurred trying to read it.
    CacheReadError,
    /// Cache lookup was overridden, recompilation was forced.
    ForcedRecache,
}

/// Information about a successful cache write.
pub struct CacheWriteInfo {
    pub object_file: String,
    pub duration: Duration,
}

/// Result of writing to cache storage.
pub type CacheWriteResult = Result<CacheWriteInfo, String>;

/// A `Future` that may provide a `CacheWriteResult`.
pub type CacheWriteFuture = futures::BoxFuture<CacheWriteResult, futures::Canceled>;

/// The result of a compilation or cache retrieval.
pub enum CompileResult {
    /// An error made the compilation not possible.
    Error,
    /// Result was found in cache.
    CacheHit,
    /// Result was not found in cache.
    ///
    /// The `CacheWriteFuture` will resolve when the result is finished
    /// being stored in the cache.
    CacheMiss(MissType, CacheWriteFuture),
    /// Not in cache, but the compilation result was determined to be not cacheable.
    NotCacheable,
    /// Not in cache, but compilation failed.
    CompileFailed,
}


/// Can't derive(Debug) because of `CacheWriteFuture`.
impl fmt::Debug for CompileResult {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &CompileResult::Error => write!(f, "CompileResult::Error"),
            &CompileResult::CacheHit => write!(f, "CompileResult::CacheHit"),
            &CompileResult::CacheMiss(ref m, _) => write!(f, "CompileResult::CacheMiss({:?}, _)", m),
            &CompileResult::NotCacheable => write!(f, "CompileResult::NotCacheable"),
            &CompileResult::CompileFailed => write!(f, "CompileResult::CompileFailed"),
        }
    }
}

/// Can't use derive(PartialEq) because of the `CacheWriteFuture`.
impl PartialEq<CompileResult> for CompileResult {
    fn eq(&self, other: &CompileResult) -> bool {
        match (self, other) {
            (&CompileResult::Error, &CompileResult::Error) => true,
            (&CompileResult::CacheHit, &CompileResult::CacheHit) => true,
            (&CompileResult::CacheMiss(ref m, _), &CompileResult::CacheMiss(ref n, _)) => m == n,
            (&CompileResult::NotCacheable, &CompileResult::NotCacheable) => true,
            (&CompileResult::CompileFailed, &CompileResult::CompileFailed) => true,
            _ => false,
        }
    }
}

/// Can this result be stored in cache?
#[derive(Debug, PartialEq)]
pub enum Cacheable {
    Yes,
    No,
}

/// Control of caching behavior.
#[derive(Debug, PartialEq)]
pub enum CacheControl {
    /// Default caching behavior.
    Default,
    /// Ignore existing cache entries, force recompilation.
    ForceRecache,
}

impl Compiler {
    /// Create a new `Compiler` of `kind`, with `executable` as the binary.
    ///
    /// This will generate a hash of the contents of `executable`, so
    /// don't call it where it shouldn't block on I/O.
    pub fn new(executable: &str, kind: CompilerKind) -> io::Result<Compiler> {
        let attr = try!(fs::metadata(executable));
        let f = try!(File::open(executable));
        let mut m = sha1::Sha1::new();
        let mut reader = BufReader::new(f);
        loop {
            let mut buffer = [0; 1024];
            let count = try!(reader.read(&mut buffer[..]));
            if count == 0 {
                break;
            }
            m.update(&buffer[..count]);
        }
        Ok(Compiler {
            executable: executable.to_owned(),
            mtime: FileTime::from_last_modification_time(&attr),
            digest: m.digest().to_string(),
            kind: kind,
        })
    }

    /// Check that this compiler can handle and cache when run with `arguments`, and parse out the relevant bits.
    ///
    /// Not all compiler options can be cached, so this tests the set of
    /// options for each compiler.
    pub fn parse_arguments(&self, arguments: &[String]) -> CompilerArguments {
        if log_enabled!(Debug) {
            let cmd_str = arguments.join(" ");
            debug!("parse_arguments: `{}`", cmd_str);
        }
        let parsed_args = self.kind.parse_arguments(arguments);
        match parsed_args {
            CompilerArguments::Ok(_) => debug!("parse_arguments: Ok"),
            CompilerArguments::CannotCache => debug!("parse_arguments: CannotCache"),
            CompilerArguments::NotCompilation => debug!("parse_arguments: NotCompilation"),
        };
        parsed_args
    }

    /// Look up a cached compile result in `storage`. If not found, run the compile and store the result.
    pub fn get_cached_or_compile<T: CommandCreatorSync>(&self, creator: T, storage: &Storage, arguments: &[String], parsed_args: &ParsedArguments, cwd: &str, cache_control: CacheControl) -> io::Result<(CompileResult, process::Output)> {
        let out_file = parsed_args.output_file();
        if log_enabled!(Debug) {
            let cmd_str = arguments.join(" ");
            debug!("[{}]: get_cached_or_compile: {}", out_file, cmd_str);
        }
        let preprocessor_result = try!(self.kind.preprocess(creator.clone(), self, parsed_args, cwd).map_err(|e| { debug!("[{}]: preprocessor failed: {:?}", out_file, e); e }));
        // If the preprocessor failed, just return that result.
        if !preprocessor_result.status.success() {
            return Ok((CompileResult::Error, preprocessor_result));
        }
        trace!("[{}]: Preprocessor output is {} bytes", out_file, preprocessor_result.stdout.len());

        let key = hash_key(self, arguments, &preprocessor_result.stdout);
        trace!("[{}]: Hash key: {}", out_file, key);
        let pwd = Path::new(cwd);
        let outputs = parsed_args.outputs.iter()
            .map(|(key, path)| (key, pwd.join(path)))
            .collect::<HashMap<_, _>>();
        // If `ForceRecache` is enabled, we won't check the cache.
        let cache_status = if cache_control == CacheControl::ForceRecache {
            Cache::Recache
        } else {
            storage.get(&key)
        };
        match cache_status {
            Cache::Hit(mut entry) => {
                debug!("[{}]: Cache hit!", out_file);
                for (key, path) in &outputs {
                    let mut f = try!(File::create(path));
                    try!(entry.get_object(key, &mut f));
                }
                let mut stdout = io::Cursor::new(vec!());
                let mut stderr = io::Cursor::new(vec!());
                entry.get_object("stdout", &mut stdout).unwrap_or(());
                entry.get_object("stderr", &mut stderr).unwrap_or(());
                Ok((CompileResult::CacheHit,
                    process::Output {
                        status: exit_status(0),
                        stdout: stdout.into_inner(),
                        stderr: stderr.into_inner(),
                    }))
            },

            res @ Cache::Miss | res @ Cache::Recache | res @ Cache::Error(_) => {
                let miss_type = match res {
                    Cache::Miss => { debug!("[{}]: Cache miss!", out_file); MissType::Normal }
                    Cache::Recache => { debug!("[{}]: Cache recache!", out_file); MissType::ForcedRecache }
                    Cache::Error(e) => {
                        debug!("[{}]: Cache read error: {:?}", out_file, e);
                        //TODO: store the error in CacheReadError in some way
                        MissType::CacheReadError
                    }
                    Cache::Hit(_) => MissType::Normal,
                };
                let process::Output { stdout, .. } = preprocessor_result;
                let (cacheable, compiler_result) = try!(self.kind.compile(creator, self, stdout, parsed_args, cwd));
                if compiler_result.status.success() {
                    if cacheable == Cacheable::Yes {
                        debug!("[{}]: Compiled, storing in cache", out_file);
                        let mut entry = try!(storage.start_put(&key));
                        for (key, path) in &outputs {
                            let mut f = try!(File::open(&path));
                            try!(entry.put_object(key, &mut f)
                                 .or_else(|e| {
                                     error!("[{}]: Failed to put object `{:?}` in storage: {:?}", out_file, path, e);
                                     Err(e)
                                 }));
                        }
                        if !compiler_result.stdout.is_empty() {
                            try!(entry.put_object("stdout", &mut io::Cursor::new(&compiler_result.stdout)));
                        }
                        if !compiler_result.stderr.is_empty() {
                            try!(entry.put_object("stderr", &mut io::Cursor::new(&compiler_result.stderr)));
                        }
                        // Try to finish storing the newly-written cache
                        // entry. We'll get the result back elsewhere.
                        let out_file = out_file.into_owned();
                        let future = storage.finish_put(&key, entry)
                            .map(move |res| {
                                match res {
                                    Ok(_) => debug!("[{}]: Stored in cache successfully!", out_file),
                                    Err(ref e) => debug!("[{}]: Cache write error: {:?}", out_file, e),
                                }
                                res.map(|duration| CacheWriteInfo {
                                    object_file: out_file,
                                    duration: duration,
                                })
                            }).boxed();
                        Ok((CompileResult::CacheMiss(miss_type, future), compiler_result))
                    } else {
                        // Not cacheable
                        debug!("[{}]: Compiled but not cacheable", out_file);
                        Ok((CompileResult::NotCacheable, compiler_result))
                    }
                } else {
                    debug!("[{}]: Compiled but failed, not storing in cache", out_file);
                    Ok((CompileResult::CompileFailed, compiler_result))
                }
            }
        }
    }
}

/// Write `contents` to `path`.
fn write_file(path : &Path, contents: &[u8]) -> io::Result<()> {
    let mut f = try!(File::create(path));
    f.write_all(contents)
}

/// If `executable` is a known compiler, return `Some(CompilerKind)`.
pub fn detect_compiler_kind<T : CommandCreatorSync>(mut creator : T,  executable : &str) -> Option<CompilerKind> {
    trace!("detect_compiler");
    TempDir::new("sccache")
        .and_then(|dir| {
            let src = dir.path().join("testfile.c");
            try!(write_file(&src, b"#if defined(_MSC_VER)
msvc
#elif defined(__clang__)
clang
#elif defined(__GNUC__)
gcc
#endif
"));

            let args = vec!(OsString::from("-E"), OsString::from(&src));
            if log_enabled!(Trace) {
                let va = args.iter().map(|a| a.to_str().unwrap()).collect::<Vec<&str>>();
                trace!("compiler: {}, args: '{}'", executable, va.join(" "));
            }
            creator.new_command_sync(&executable)
                .args(&args)
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .spawn()
                .and_then(|child| child.wait_with_output())
                .and_then(|output| {
                    str::from_utf8(&output.stdout)
                        .or_else(|_| Err(Error::new(ErrorKind::Other, "Failed to parse output")))
                        .and_then(|stdout| {
                            for line in stdout.lines() {
                                //TODO: do something smarter here.
                                if line == "gcc" {
                                    debug!("Found GCC");
                                    return Ok(Some(CompilerKind::Gcc));
                                } else if line == "clang" {
                                    debug!("Found clang");
                                    return Ok(Some(CompilerKind::Clang));
                                } else if line == "msvc" {
                                    debug!("Found MSVC");
                                    let prefix = try!(msvc::detect_showincludes_prefix(&mut creator, &executable));
                                    trace!("showIncludes prefix: '{}'", prefix);
                                    return Ok(Some(CompilerKind::Msvc {
                                        includes_prefix: prefix,
                                    }));
                                }
                            }
                            Ok(None)
                        })
                })
        }).unwrap_or_else(|e| {
            warn!("Failed to run compiler: {}", e);
            None
        })
}

/// If `executable` is a known compiler, return `Some(Compiler)` containing information about it.
pub fn get_compiler_info<T : CommandCreatorSync>(creator : T,  executable : &str) -> Option<Compiler> {
    detect_compiler_kind(creator, executable)
        .and_then(|kind| {
            Compiler::new(executable, kind)
                .or_else(|e| {
                    error!("Failed to create Compiler: {}", e);
                    Err(())
                })
                .ok()
        })
}

/// If `input`, write it to `child`'s stdin while also reading `child`'s stdout and stderr, then wait on `child` and return its status and output.
///
/// This was lifted from `std::process::Child::wait_with_output` and modified
/// to also write to stdin.
pub fn wait_with_input_output<T: CommandChild + 'static>(mut child: T, input: Option<Vec<u8>>) -> io::Result<process::Output> {
    let stdin = input.and_then(|i| {
        child.take_stdin().map(|mut stdin| {
            thread::spawn(move || {
                stdin.write_all(&i)
            })
        })
    });
    fn read<R>(mut input: R) -> thread::JoinHandle<io::Result<Vec<u8>>>
        where R: Read + Send + 'static
    {
        thread::spawn(move || {
            let mut ret = Vec::new();
            input.read_to_end(&mut ret).map(|_| ret)
        })
    }
    // Finish writing stdin before waiting, because waiting drops stdin.
    stdin.and_then(|t| t.join().unwrap().ok());
    let stdout = child.take_stdout().map(read);
    let stderr = child.take_stderr().map(read);
    let status = try!(child.wait());
    let stdout = stdout.and_then(|t| t.join().unwrap().ok());
    let stderr = stderr.and_then(|t| t.join().unwrap().ok());

    Ok(process::Output {
        status: status,
        stdout: stdout.unwrap_or_default(),
        stderr: stderr.unwrap_or_default(),
    })
}

/// Run `command`, writing `input` to its stdin if it is `Some` and return the exit status and output.
pub fn run_input_output<C: RunCommand>(mut command: C, input: Option<Vec<u8>>) -> io::Result<process::Output> {
    command.stdin(if input.is_some() { Stdio::piped() } else { Stdio::inherit() })
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .and_then(|child| wait_with_input_output(child, input))
}

#[cfg(test)]
mod test {
    use super::*;
    use cache::disk::DiskCache;
    use mock_command::*;
    use std::fs::{self,File};
    use std::io::Write;
    use test::utils::*;

    #[test]
    fn test_detect_compiler_kind_gcc() {
        let creator = new_creator();
        next_command(&creator, Ok(MockChild::new(exit_status(0), "foo\nbar\ngcc", "")));
        assert_eq!(Some(CompilerKind::Gcc), detect_compiler_kind(creator.clone(), "/foo/bar"));
    }

    #[test]
    fn test_detect_compiler_kind_clang() {
        let creator = new_creator();
        next_command(&creator, Ok(MockChild::new(exit_status(0), "clang\nfoo", "")));
        assert_eq!(Some(CompilerKind::Clang), detect_compiler_kind(creator.clone(), "/foo/bar"));
    }

    #[test]
    fn test_detect_compiler_kind_msvc() {
        use env_logger;
        match env_logger::init() {
            Ok(_) => {},
            Err(_) => {},
        }
        let creator = new_creator();
        let f = TestFixture::new();
        let srcfile = f.touch("stdio.h").unwrap();
        let mut s = srcfile.to_str().unwrap();
        if s.starts_with("\\\\?\\") {
            s = &s[4..];
        }
        let prefix = String::from("blah: ");
        let stdout = format!("{}{}\r\n", prefix, s);
        // Compiler detection output
        next_command(&creator, Ok(MockChild::new(exit_status(0), "foo\nmsvc\nbar", "")));
        // showincludes prefix detection output
        next_command(&creator, Ok(MockChild::new(exit_status(0), &stdout, &String::new())));
        assert_eq!(Some(CompilerKind::Msvc { includes_prefix: prefix }), detect_compiler_kind(creator.clone(), "/foo/bar"));
    }

    #[test]
    fn test_detect_compiler_kind_unknown() {
        let creator = new_creator();
        next_command(&creator, Ok(MockChild::new(exit_status(0), "something", "")));
        assert_eq!(None, detect_compiler_kind(creator.clone(), "/foo/bar"));
    }

    #[test]
    fn test_detect_compiler_kind_process_fail() {
        let creator = new_creator();
        next_command(&creator, Ok(MockChild::new(exit_status(1), "", "")));
        assert_eq!(None, detect_compiler_kind(creator.clone(), "/foo/bar"));
    }

    #[test]
    fn test_get_compiler_info() {
        let creator = new_creator();
        let f = TestFixture::new();
        // Pretend to be GCC.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
        let c = get_compiler_info(creator.clone(),
                                  f.bins[0].to_str().unwrap()).unwrap();
        assert_eq!(f.bins[0].to_str().unwrap(), c.executable);
        // sha-1 digest of an empty file.
        assert_eq!("da39a3ee5e6b4b0d3255bfef95601890afd80709", c.digest);
        assert_eq!(CompilerKind::Gcc, c.kind);
    }

    #[test]
    fn test_compiler_get_cached_or_compile_uncached() {
        use env_logger;
        match env_logger::init() {
            Ok(_) => {},
            Err(_) => {},
        }
        let creator = new_creator();
        let f = TestFixture::new();
        let storage = DiskCache::new(&f.tempdir.path());
        // Pretend to be GCC.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
        let c = get_compiler_info(creator.clone(),
                                  f.bins[0].to_str().unwrap()).unwrap();
        // The preprocessor invocation.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "preprocessor output", "")));
        // The compiler invocation.
        const COMPILER_STDOUT : &'static [u8] = b"compiler stdout";
        const COMPILER_STDERR : &'static [u8] = b"compiler stderr";
        let obj = f.tempdir.path().join("foo.o");
        let o = obj.clone();
        next_command_calls(&creator, move || {
            // Pretend to compile something.
            match File::create(&o)
                .and_then(|mut f| f.write_all(b"file contents")) {
                    Ok(_) => Ok(MockChild::new(exit_status(0), COMPILER_STDOUT, COMPILER_STDERR)),
                    Err(e) => Err(e),
                }
        });
        let cwd = f.tempdir.path().to_str().unwrap();
        let arguments = stringvec!["-c", "foo.c", "-o", "foo.o"];
        let parsed_args = match c.parse_arguments(&arguments) {
            CompilerArguments::Ok(parsed) => parsed,
            o @ _ => panic!("Bad result from parse_arguments: {:?}", o),
        };
        let (cached, res) = c.get_cached_or_compile(creator.clone(), &storage, &arguments, &parsed_args, cwd, CacheControl::Default).unwrap();
        // Ensure that the object file was created.
        assert_eq!(true, fs::metadata(&obj).and_then(|m| Ok(m.len() > 0)).unwrap());
        assert!(match cached {
            CompileResult::CacheMiss(MissType::Normal, _) => true,
            _ => false,
        });
        assert_eq!(exit_status(0), res.status);
        assert_eq!(COMPILER_STDOUT, res.stdout.as_slice());
        assert_eq!(COMPILER_STDERR, res.stderr.as_slice());
        // Now compile again, which should be a cache hit.
        fs::remove_file(&obj).unwrap();
        // The preprocessor invocation.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "preprocessor output", "")));
        // There should be no actual compiler invocation.
        let (cached, res) = c.get_cached_or_compile(creator.clone(), &storage, &arguments, &parsed_args, cwd, CacheControl::Default).unwrap();
        // Ensure that the object file was created.
        assert_eq!(true, fs::metadata(&obj).and_then(|m| Ok(m.len() > 0)).unwrap());
        assert_eq!(CompileResult::CacheHit, cached);
        assert_eq!(exit_status(0), res.status);
        //FIXME: this is broken!
        assert_eq!(COMPILER_STDOUT, res.stdout.as_slice());
        assert_eq!(COMPILER_STDERR, res.stderr.as_slice());
    }

    #[test]
    fn test_compiler_get_cached_or_compile_cached() {
        use env_logger;
        match env_logger::init() {
            Ok(_) => {},
            Err(_) => {},
        }
        let creator = new_creator();
        let f = TestFixture::new();
        let storage = DiskCache::new(&f.tempdir.path());
        // Pretend to be GCC.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
        let c = get_compiler_info(creator.clone(),
                                  f.bins[0].to_str().unwrap()).unwrap();
        // The preprocessor invocation.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "preprocessor output", "")));
        // The compiler invocation.
        const COMPILER_STDOUT : &'static [u8] = b"compiler stdout";
        const COMPILER_STDERR : &'static [u8] = b"compiler stderr";
        let obj = f.tempdir.path().join("foo.o");
        let o = obj.clone();
        next_command_calls(&creator, move || {
            // Pretend to compile something.
            match File::create(&o)
                .and_then(|mut f| f.write_all(b"file contents")) {
                    Ok(_) => Ok(MockChild::new(exit_status(0), COMPILER_STDOUT, COMPILER_STDERR)),
                    Err(e) => Err(e),
                }
        });
        let cwd = f.tempdir.path().to_str().unwrap();
        let arguments = stringvec!["-c", "foo.c", "-o", "foo.o"];
        let parsed_args = match c.parse_arguments(&arguments) {
            CompilerArguments::Ok(parsed) => parsed,
            o @ _ => panic!("Bad result from parse_arguments: {:?}", o),
        };
        let (cached, res) = c.get_cached_or_compile(creator.clone(), &storage, &arguments, &parsed_args, cwd, CacheControl::Default).unwrap();
        // Ensure that the object file was created.
        assert_eq!(true, fs::metadata(&obj).and_then(|m| Ok(m.len() > 0)).unwrap());
        assert!(match cached {
            CompileResult::CacheMiss(MissType::Normal, _) => true,
            _ => false,
        });
        assert_eq!(exit_status(0), res.status);
        assert_eq!(COMPILER_STDOUT, res.stdout.as_slice());
        assert_eq!(COMPILER_STDERR, res.stderr.as_slice());
        // Now compile again, which should be a cache hit.
        fs::remove_file(&obj).unwrap();
        // The preprocessor invocation.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "preprocessor output", "")));
        // There should be no actual compiler invocation.
        let (cached, res) = c.get_cached_or_compile(creator.clone(), &storage, &arguments, &parsed_args, cwd, CacheControl::Default).unwrap();
        // Ensure that the object file was created.
        assert_eq!(true, fs::metadata(&obj).and_then(|m| Ok(m.len() > 0)).unwrap());
        assert_eq!(CompileResult::CacheHit, cached);
        assert_eq!(exit_status(0), res.status);
        assert_eq!(COMPILER_STDOUT, res.stdout.as_slice());
        assert_eq!(COMPILER_STDERR, res.stderr.as_slice());
    }

    #[test]
    fn test_compiler_get_cached_or_compile_force_recache() {
        use env_logger;
        match env_logger::init() {
            Ok(_) => {},
            Err(_) => {},
        }
        let creator = new_creator();
        let f = TestFixture::new();
        let storage = DiskCache::new(&f.tempdir.path());
        // Pretend to be GCC.
        next_command(&creator, Ok(MockChild::new(exit_status(0), "gcc", "")));
        let c = get_compiler_info(creator.clone(),
                                  f.bins[0].to_str().unwrap()).unwrap();
        const COMPILER_STDOUT: &'static [u8] = b"compiler stdout";
        const COMPILER_STDERR: &'static [u8] = b"compiler stderr";
        // The compiler should be invoked twice, since we're forcing
        // recaching.
        let obj = f.tempdir.path().join("foo.o");
        for _ in 0..2 {
            // The preprocessor invocation.
            next_command(&creator, Ok(MockChild::new(exit_status(0), "preprocessor output", "")));
            // The compiler invocation.
            let o = obj.clone();
            next_command_calls(&creator, move || {
                // Pretend to compile something.
                match File::create(&o)
                    .and_then(|mut f| f.write_all(b"file contents")) {
                        Ok(_) => Ok(MockChild::new(exit_status(0), COMPILER_STDOUT, COMPILER_STDERR)),
                        Err(e) => Err(e),
                    }
            });
        }
        let cwd = f.tempdir.path().to_str().unwrap();
        let arguments = stringvec!["-c", "foo.c", "-o", "foo.o"];
        let parsed_args = match c.parse_arguments(&arguments) {
            CompilerArguments::Ok(parsed) => parsed,
            o @ _ => panic!("Bad result from parse_arguments: {:?}", o),
        };
        let (cached, res) = c.get_cached_or_compile(creator.clone(), &storage, &arguments, &parsed_args, cwd, CacheControl::Default).unwrap();
        // Ensure that the object file was created.
        assert_eq!(true, fs::metadata(&obj).and_then(|m| Ok(m.len() > 0)).unwrap());
        assert!(match cached {
            CompileResult::CacheMiss(MissType::Normal, _) => true,
            _ => false,
        });
        assert_eq!(exit_status(0), res.status);
        assert_eq!(COMPILER_STDOUT, res.stdout.as_slice());
        assert_eq!(COMPILER_STDERR, res.stderr.as_slice());
        // Now compile again, but force recaching.
        fs::remove_file(&obj).unwrap();
        let (cached, res) = c.get_cached_or_compile(creator.clone(), &storage, &arguments, &parsed_args, cwd, CacheControl::ForceRecache).unwrap();
        // Ensure that the object file was created.
        assert_eq!(true, fs::metadata(&obj).and_then(|m| Ok(m.len() > 0)).unwrap());
        assert!(match cached {
            CompileResult::CacheMiss(MissType::ForcedRecache, _) => true,
            _ => false,
        });
        assert_eq!(exit_status(0), res.status);
        assert_eq!(COMPILER_STDOUT, res.stdout.as_slice());
        assert_eq!(COMPILER_STDERR, res.stderr.as_slice());
    }
}