]> git.lizzy.rs Git - rust.git/blob - src/tools/compiletest/src/runtest.rs
Rollup merge of #35591 - GuillaumeGomez:generics_span, r=jntrmr
[rust.git] / src / tools / compiletest / src / runtest.rs
1 // Copyright 2012-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 common::Config;
12 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
13 use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc, CodegenUnits};
14 use common::{Incremental, RunMake, Ui, MirOpt};
15 use errors::{self, ErrorKind, Error};
16 use json;
17 use header::TestProps;
18 use header;
19 use procsrv;
20 use test::TestPaths;
21 use uidiff;
22 use util::logv;
23
24 use std::env;
25 use std::collections::HashSet;
26 use std::fmt;
27 use std::fs::{self, File};
28 use std::io::{self, BufReader};
29 use std::io::prelude::*;
30 use std::net::TcpStream;
31 use std::path::{Path, PathBuf};
32 use std::process::{Command, Output, ExitStatus};
33 use std::str;
34
35 pub fn run(config: Config, testpaths: &TestPaths) {
36     match &*config.target {
37
38         "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
39             if !config.adb_device_status {
40                 panic!("android device not available");
41             }
42         }
43
44         _=> { }
45     }
46
47     if config.verbose {
48         // We're going to be dumping a lot of info. Start on a new line.
49         print!("\n\n");
50     }
51     debug!("running {:?}", testpaths.file.display());
52     let base_props = TestProps::from_file(&testpaths.file);
53
54     let base_cx = TestCx { config: &config,
55                            props: &base_props,
56                            testpaths: testpaths,
57                            revision: None };
58     base_cx.init_all();
59
60     if base_props.revisions.is_empty() {
61         base_cx.run_revision()
62     } else {
63         for revision in &base_props.revisions {
64             let mut revision_props = base_props.clone();
65             revision_props.load_from(&testpaths.file, Some(&revision));
66             let rev_cx = TestCx {
67                 config: &config,
68                 props: &revision_props,
69                 testpaths: testpaths,
70                 revision: Some(revision)
71             };
72             rev_cx.run_revision();
73         }
74     }
75
76     base_cx.complete_all();
77 }
78
79 struct TestCx<'test> {
80     config: &'test Config,
81     props: &'test TestProps,
82     testpaths: &'test TestPaths,
83     revision: Option<&'test str>
84 }
85
86 struct DebuggerCommands {
87     commands: Vec<String>,
88     check_lines: Vec<String>,
89     breakpoint_lines: Vec<usize>,
90 }
91
92 impl<'test> TestCx<'test> {
93     /// invoked once before any revisions have been processed
94     fn init_all(&self) {
95         assert!(self.revision.is_none(), "init_all invoked for a revision");
96         match self.config.mode {
97             Incremental => self.init_incremental_test(),
98             _ => { }
99         }
100     }
101
102     /// Code executed for each revision in turn (or, if there are no
103     /// revisions, exactly once, with revision == None).
104     fn run_revision(&self) {
105         match self.config.mode {
106             CompileFail => self.run_cfail_test(),
107             ParseFail => self.run_cfail_test(),
108             RunFail => self.run_rfail_test(),
109             RunPass => self.run_rpass_test(),
110             RunPassValgrind => self.run_valgrind_test(),
111             Pretty => self.run_pretty_test(),
112             DebugInfoGdb => self.run_debuginfo_gdb_test(),
113             DebugInfoLldb => self.run_debuginfo_lldb_test(),
114             Codegen => self.run_codegen_test(),
115             Rustdoc => self.run_rustdoc_test(),
116             CodegenUnits => self.run_codegen_units_test(),
117             Incremental => self.run_incremental_test(),
118             RunMake => self.run_rmake_test(),
119             Ui => self.run_ui_test(),
120             MirOpt => self.run_mir_opt_test(),
121         }
122     }
123
124     /// Invoked after all revisions have executed.
125     fn complete_all(&self) {
126         assert!(self.revision.is_none(), "init_all invoked for a revision");
127     }
128
129     fn run_cfail_test(&self) {
130         let proc_res = self.compile_test();
131
132         if proc_res.status.success() {
133             self.fatal_proc_rec(
134                 &format!("{} test compiled successfully!", self.config.mode)[..],
135                 &proc_res);
136         }
137
138         self.check_correct_failure_status(&proc_res);
139
140         let output_to_check = self.get_output(&proc_res);
141         let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
142         if !expected_errors.is_empty() {
143             if !self.props.error_patterns.is_empty() {
144                 self.fatal("both error pattern and expected errors specified");
145             }
146             self.check_expected_errors(expected_errors, &proc_res);
147         } else {
148             self.check_error_patterns(&output_to_check, &proc_res);
149         }
150         self.check_no_compiler_crash(&proc_res);
151         self.check_forbid_output(&output_to_check, &proc_res);
152     }
153
154     fn run_rfail_test(&self) {
155         let proc_res = self.compile_test();
156
157         if !proc_res.status.success() {
158             self.fatal_proc_rec("compilation failed!", &proc_res);
159         }
160
161         let proc_res = self.exec_compiled_test();
162
163         // The value our Makefile configures valgrind to return on failure
164         const VALGRIND_ERR: i32 = 100;
165         if proc_res.status.code() == Some(VALGRIND_ERR) {
166             self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
167         }
168
169         let output_to_check = self.get_output(&proc_res);
170         self.check_correct_failure_status(&proc_res);
171         self.check_error_patterns(&output_to_check, &proc_res);
172     }
173
174     fn get_output(&self, proc_res: &ProcRes) -> String {
175         if self.props.check_stdout {
176             format!("{}{}", proc_res.stdout, proc_res.stderr)
177         } else {
178             proc_res.stderr.clone()
179         }
180     }
181
182     fn check_correct_failure_status(&self, proc_res: &ProcRes) {
183         // The value the rust runtime returns on failure
184         const RUST_ERR: i32 = 101;
185         if proc_res.status.code() != Some(RUST_ERR) {
186             self.fatal_proc_rec(
187                 &format!("failure produced the wrong error: {}",
188                          proc_res.status),
189                 proc_res);
190         }
191     }
192
193     fn run_rpass_test(&self) {
194         let proc_res = self.compile_test();
195
196         if !proc_res.status.success() {
197             self.fatal_proc_rec("compilation failed!", &proc_res);
198         }
199
200         let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
201         if !expected_errors.is_empty() {
202             self.check_expected_errors(expected_errors, &proc_res);
203         }
204
205         let proc_res = self.exec_compiled_test();
206
207         if !proc_res.status.success() {
208             self.fatal_proc_rec("test run failed!", &proc_res);
209         }
210     }
211
212     fn run_valgrind_test(&self) {
213         assert!(self.revision.is_none(), "revisions not relevant here");
214
215         if self.config.valgrind_path.is_none() {
216             assert!(!self.config.force_valgrind);
217             return self.run_rpass_test();
218         }
219
220         let mut proc_res = self.compile_test();
221
222         if !proc_res.status.success() {
223             self.fatal_proc_rec("compilation failed!", &proc_res);
224         }
225
226         let mut new_config = self.config.clone();
227         new_config.runtool = new_config.valgrind_path.clone();
228         let new_cx = TestCx { config: &new_config, ..*self };
229         proc_res = new_cx.exec_compiled_test();
230
231         if !proc_res.status.success() {
232             self.fatal_proc_rec("test run failed!", &proc_res);
233         }
234     }
235
236     fn run_pretty_test(&self) {
237         if self.props.pp_exact.is_some() {
238             logv(self.config, "testing for exact pretty-printing".to_owned());
239         } else {
240             logv(self.config, "testing for converging pretty-printing".to_owned());
241         }
242
243         let rounds = match self.props.pp_exact { Some(_) => 1, None => 2 };
244
245         let mut src = String::new();
246         File::open(&self.testpaths.file).unwrap().read_to_string(&mut src).unwrap();
247         let mut srcs = vec!(src);
248
249         let mut round = 0;
250         while round < rounds {
251             logv(self.config, format!("pretty-printing round {} revision {:?}",
252                                       round, self.revision));
253             let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
254
255             if !proc_res.status.success() {
256                 self.fatal_proc_rec(&format!("pretty-printing failed in round {} revision {:?}",
257                                              round, self.revision),
258                                     &proc_res);
259             }
260
261             let ProcRes{ stdout, .. } = proc_res;
262             srcs.push(stdout);
263             round += 1;
264         }
265
266         let mut expected = match self.props.pp_exact {
267             Some(ref file) => {
268                 let filepath = self.testpaths.file.parent().unwrap().join(file);
269                 let mut s = String::new();
270                 File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
271                 s
272             }
273             None => { srcs[srcs.len() - 2].clone() }
274         };
275         let mut actual = srcs[srcs.len() - 1].clone();
276
277         if self.props.pp_exact.is_some() {
278             // Now we have to care about line endings
279             let cr = "\r".to_owned();
280             actual = actual.replace(&cr, "").to_owned();
281             expected = expected.replace(&cr, "").to_owned();
282         }
283
284         self.compare_source(&expected, &actual);
285
286         // If we're only making sure that the output matches then just stop here
287         if self.props.pretty_compare_only { return; }
288
289         // Finally, let's make sure it actually appears to remain valid code
290         let proc_res = self.typecheck_source(actual);
291         if !proc_res.status.success() {
292             self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
293         }
294
295         if !self.props.pretty_expanded { return }
296
297         // additionally, run `--pretty expanded` and try to build it.
298         let proc_res = self.print_source(srcs[round].clone(), "expanded");
299         if !proc_res.status.success() {
300             self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
301         }
302
303         let ProcRes{ stdout: expanded_src, .. } = proc_res;
304         let proc_res = self.typecheck_source(expanded_src);
305         if !proc_res.status.success() {
306             self.fatal_proc_rec(
307                 "pretty-printed source (expanded) does not typecheck",
308                 &proc_res);
309         }
310     }
311
312     fn print_source(&self,
313                     src: String,
314                     pretty_type: &str)
315                     -> ProcRes {
316         let aux_dir = self.aux_output_dir_name();
317         self.compose_and_run(self.make_pp_args(pretty_type.to_owned()),
318                              self.props.exec_env.clone(),
319                              self.config.compile_lib_path.to_str().unwrap(),
320                              Some(aux_dir.to_str().unwrap()),
321                              Some(src))
322     }
323
324     fn make_pp_args(&self,
325                     pretty_type: String)
326                     -> ProcArgs {
327         let aux_dir = self.aux_output_dir_name();
328         // FIXME (#9639): This needs to handle non-utf8 paths
329         let mut args = vec!("-".to_owned(),
330                             "-Zunstable-options".to_owned(),
331                             "--unpretty".to_owned(),
332                             pretty_type,
333                             format!("--target={}", self.config.target),
334                             "-L".to_owned(),
335                             aux_dir.to_str().unwrap().to_owned());
336         args.extend(self.split_maybe_args(&self.config.target_rustcflags));
337         args.extend(self.props.compile_flags.iter().cloned());
338         return ProcArgs {
339             prog: self.config.rustc_path.to_str().unwrap().to_owned(),
340             args: args,
341         };
342     }
343
344     fn compare_source(&self,
345                       expected: &str,
346                       actual: &str) {
347         if expected != actual {
348             self.error("pretty-printed source does not match expected source");
349             println!("\n\
350 expected:\n\
351 ------------------------------------------\n\
352 {}\n\
353 ------------------------------------------\n\
354 actual:\n\
355 ------------------------------------------\n\
356 {}\n\
357 ------------------------------------------\n\
358 \n",
359                      expected, actual);
360             panic!();
361         }
362     }
363
364     fn typecheck_source(&self, src: String) -> ProcRes {
365         let args = self.make_typecheck_args();
366         self.compose_and_run_compiler(args, Some(src))
367     }
368
369     fn make_typecheck_args(&self) -> ProcArgs {
370         let aux_dir = self.aux_output_dir_name();
371         let target = if self.props.force_host {
372             &*self.config.host
373         } else {
374             &*self.config.target
375         };
376
377         let out_dir = self.output_base_name().with_extension("pretty-out");
378         let _ = fs::remove_dir_all(&out_dir);
379         self.create_dir_racy(&out_dir);
380
381         // FIXME (#9639): This needs to handle non-utf8 paths
382         let mut args = vec!("-".to_owned(),
383                             "-Zno-trans".to_owned(),
384                             "--out-dir".to_owned(),
385                             out_dir.to_str().unwrap().to_owned(),
386                             format!("--target={}", target),
387                             "-L".to_owned(),
388                             self.config.build_base.to_str().unwrap().to_owned(),
389                             "-L".to_owned(),
390                             aux_dir.to_str().unwrap().to_owned());
391         if let Some(revision) = self.revision {
392             args.extend(vec![
393                 format!("--cfg"),
394                 format!("{}", revision),
395             ]);
396         }
397         args.extend(self.split_maybe_args(&self.config.target_rustcflags));
398         args.extend(self.props.compile_flags.iter().cloned());
399         // FIXME (#9639): This needs to handle non-utf8 paths
400         return ProcArgs {
401             prog: self.config.rustc_path.to_str().unwrap().to_owned(),
402             args: args,
403         };
404     }
405
406     fn run_debuginfo_gdb_test(&self) {
407         assert!(self.revision.is_none(), "revisions not relevant here");
408
409         let config = Config {
410             target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
411             host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
412             .. self.config.clone()
413         };
414
415         let test_cx = TestCx {
416             config: &config,
417             ..*self
418         };
419
420         test_cx.run_debuginfo_gdb_test_no_opt();
421     }
422
423     fn run_debuginfo_gdb_test_no_opt(&self) {
424         let DebuggerCommands {
425             commands,
426             check_lines,
427             breakpoint_lines
428         } = self.parse_debugger_commands("gdb");
429         let mut cmds = commands.join("\n");
430
431         // compile test file (it should have 'compile-flags:-g' in the header)
432         let compiler_run_result = self.compile_test();
433         if !compiler_run_result.status.success() {
434             self.fatal_proc_rec("compilation failed!", &compiler_run_result);
435         }
436
437         let exe_file = self.make_exe_name();
438
439         let debugger_run_result;
440         match &*self.config.target {
441             "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
442
443                 cmds = cmds.replace("run", "continue");
444
445                 let tool_path = match self.config.android_cross_path.to_str() {
446                     Some(x) => x.to_owned(),
447                     None => self.fatal("cannot find android cross path")
448                 };
449
450                 // write debugger script
451                 let mut script_str = String::with_capacity(2048);
452                 script_str.push_str(&format!("set charset {}\n", Self::charset()));
453                 script_str.push_str(&format!("set sysroot {}\n", tool_path));
454                 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
455                 script_str.push_str("target remote :5039\n");
456                 script_str.push_str(&format!("set solib-search-path \
457                                               ./{}/stage2/lib/rustlib/{}/lib/\n",
458                                              self.config.host, self.config.target));
459                 for line in &breakpoint_lines {
460                     script_str.push_str(&format!("break {:?}:{}\n",
461                                                  self.testpaths.file.file_name()
462                                                  .unwrap()
463                                                  .to_string_lossy(),
464                                                  *line)[..]);
465                 }
466                 script_str.push_str(&cmds);
467                 script_str.push_str("\nquit\n");
468
469                 debug!("script_str = {}", script_str);
470                 self.dump_output_file(&script_str, "debugger.script");
471
472
473                 procsrv::run("",
474                              &self.config.adb_path,
475                              None,
476                              &[
477                                  "push".to_owned(),
478                                  exe_file.to_str().unwrap().to_owned(),
479                                  self.config.adb_test_dir.clone()
480                              ],
481                              vec!(("".to_owned(), "".to_owned())),
482                              Some("".to_owned()))
483                     .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
484
485                 procsrv::run("",
486                              &self.config.adb_path,
487                              None,
488                              &[
489                                  "forward".to_owned(),
490                                  "tcp:5039".to_owned(),
491                                  "tcp:5039".to_owned()
492                              ],
493                              vec!(("".to_owned(), "".to_owned())),
494                              Some("".to_owned()))
495                     .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
496
497                 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
498                                        gdbserver{} :5039 {}/{}",
499                                       self.config.adb_test_dir.clone(),
500                                       if self.config.target.contains("aarch64")
501                                       {"64"} else {""},
502                                       self.config.adb_test_dir.clone(),
503                                       exe_file.file_name().unwrap().to_str()
504                                       .unwrap());
505
506                 let mut process = procsrv::run_background("",
507                                                           &self.config.adb_path
508                                                           ,
509                                                           None,
510                                                           &[
511                                                               "shell".to_owned(),
512                                                               adb_arg.clone()
513                                                           ],
514                                                           vec!(("".to_owned(),
515                                                                 "".to_owned())),
516                                                           Some("".to_owned()))
517                     .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
518                 loop {
519                     //waiting 1 second for gdbserver start
520                     ::std::thread::sleep(::std::time::Duration::new(1,0));
521                     if TcpStream::connect("127.0.0.1:5039").is_ok() {
522                         break
523                     }
524                 }
525
526                 let debugger_script = self.make_out_name("debugger.script");
527                 // FIXME (#9639): This needs to handle non-utf8 paths
528                 let debugger_opts =
529                     vec!("-quiet".to_owned(),
530                          "-batch".to_owned(),
531                          "-nx".to_owned(),
532                          format!("-command={}", debugger_script.to_str().unwrap()));
533
534                 let mut gdb_path = tool_path;
535                 gdb_path.push_str(&format!("/bin/{}-gdb", self.config.target));
536                 let procsrv::Result {
537                     out,
538                     err,
539                     status
540                 } = procsrv::run("",
541                                  &gdb_path,
542                                  None,
543                                  &debugger_opts,
544                                  vec!(("".to_owned(), "".to_owned())),
545                                  None)
546                     .expect(&format!("failed to exec `{:?}`", gdb_path));
547                 let cmdline = {
548                     let cmdline = self.make_cmdline("",
549                                                     &format!("{}-gdb", self.config.target),
550                                                     &debugger_opts);
551                     logv(self.config, format!("executing {}", cmdline));
552                     cmdline
553                 };
554
555                 debugger_run_result = ProcRes {
556                     status: Status::Normal(status),
557                     stdout: out,
558                     stderr: err,
559                     cmdline: cmdline
560                 };
561                 if process.kill().is_err() {
562                     println!("Adb process is already finished.");
563                 }
564             }
565
566             _=> {
567                 let rust_src_root = self.find_rust_src_root()
568                                         .expect("Could not find Rust source root");
569                 let rust_pp_module_rel_path = Path::new("./src/etc");
570                 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
571                                                            .to_str()
572                                                            .unwrap()
573                                                            .to_owned();
574                 // write debugger script
575                 let mut script_str = String::with_capacity(2048);
576                 script_str.push_str(&format!("set charset {}\n", Self::charset()));
577                 script_str.push_str("show version\n");
578
579                 match self.config.gdb_version {
580                     Some(ref version) => {
581                         println!("NOTE: compiletest thinks it is using GDB version {}",
582                                  version);
583
584                         if header::gdb_version_to_int(version) >
585                             header::gdb_version_to_int("7.4") {
586                                 // Add the directory containing the pretty printers to
587                                 // GDB's script auto loading safe path
588                                 script_str.push_str(
589                                     &format!("add-auto-load-safe-path {}\n",
590                                              rust_pp_module_abs_path.replace(r"\", r"\\"))
591                                 );
592                             }
593                     }
594                     _ => {
595                         println!("NOTE: compiletest does not know which version of \
596                                   GDB it is using");
597                     }
598                 }
599
600                 // The following line actually doesn't have to do anything with
601                 // pretty printing, it just tells GDB to print values on one line:
602                 script_str.push_str("set print pretty off\n");
603
604                 // Add the pretty printer directory to GDB's source-file search path
605                 script_str.push_str(&format!("directory {}\n",
606                                              rust_pp_module_abs_path));
607
608                 // Load the target executable
609                 script_str.push_str(&format!("file {}\n",
610                                              exe_file.to_str().unwrap()
611                                              .replace(r"\", r"\\")));
612
613                 // Add line breakpoints
614                 for line in &breakpoint_lines {
615                     script_str.push_str(&format!("break '{}':{}\n",
616                                                  self.testpaths.file.file_name().unwrap()
617                                                  .to_string_lossy(),
618                                                  *line));
619                 }
620
621                 script_str.push_str(&cmds);
622                 script_str.push_str("\nquit\n");
623
624                 debug!("script_str = {}", script_str);
625                 self.dump_output_file(&script_str, "debugger.script");
626
627                 // run debugger script with gdb
628                 fn debugger() -> &'static str {
629                     if cfg!(windows) {"gdb.exe"} else {"gdb"}
630                 }
631
632                 let debugger_script = self.make_out_name("debugger.script");
633
634                 // FIXME (#9639): This needs to handle non-utf8 paths
635                 let debugger_opts =
636                     vec!("-quiet".to_owned(),
637                          "-batch".to_owned(),
638                          "-nx".to_owned(),
639                          format!("-command={}", debugger_script.to_str().unwrap()));
640
641                 let proc_args = ProcArgs {
642                     prog: debugger().to_owned(),
643                     args: debugger_opts,
644                 };
645
646                 let environment = vec![("PYTHONPATH".to_owned(), rust_pp_module_abs_path)];
647
648                 debugger_run_result =
649                     self.compose_and_run(proc_args,
650                                          environment,
651                                          self.config.run_lib_path.to_str().unwrap(),
652                                          None,
653                                          None);
654             }
655         }
656
657         if !debugger_run_result.status.success() {
658             self.fatal("gdb failed to execute");
659         }
660
661         self.check_debugger_output(&debugger_run_result, &check_lines);
662     }
663
664     fn find_rust_src_root(&self) -> Option<PathBuf> {
665         let mut path = self.config.src_base.clone();
666         let path_postfix = Path::new("src/etc/lldb_batchmode.py");
667
668         while path.pop() {
669             if path.join(&path_postfix).is_file() {
670                 return Some(path);
671             }
672         }
673
674         return None;
675     }
676
677     fn run_debuginfo_lldb_test(&self) {
678         assert!(self.revision.is_none(), "revisions not relevant here");
679
680         if self.config.lldb_python_dir.is_none() {
681             self.fatal("Can't run LLDB test because LLDB's python path is not set.");
682         }
683
684         let config = Config {
685             target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
686             host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
687             .. self.config.clone()
688         };
689
690
691         let test_cx = TestCx {
692             config: &config,
693             ..*self
694         };
695
696         test_cx.run_debuginfo_lldb_test_no_opt();
697     }
698
699     fn run_debuginfo_lldb_test_no_opt(&self) {
700         // compile test file (it should have 'compile-flags:-g' in the header)
701         let compile_result = self.compile_test();
702         if !compile_result.status.success() {
703             self.fatal_proc_rec("compilation failed!", &compile_result);
704         }
705
706         let exe_file = self.make_exe_name();
707
708         match self.config.lldb_version {
709             Some(ref version) => {
710                 println!("NOTE: compiletest thinks it is using LLDB version {}",
711                          version);
712             }
713             _ => {
714                 println!("NOTE: compiletest does not know which version of \
715                           LLDB it is using");
716             }
717         }
718
719         // Parse debugger commands etc from test files
720         let DebuggerCommands {
721             commands,
722             check_lines,
723             breakpoint_lines,
724             ..
725         } = self.parse_debugger_commands("lldb");
726
727         // Write debugger script:
728         // We don't want to hang when calling `quit` while the process is still running
729         let mut script_str = String::from("settings set auto-confirm true\n");
730
731         // Make LLDB emit its version, so we have it documented in the test output
732         script_str.push_str("version\n");
733
734         // Switch LLDB into "Rust mode"
735         let rust_src_root = self.find_rust_src_root().expect("Could not find Rust source root");
736         let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
737         let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
738                                                    .to_str()
739                                                    .unwrap()
740                                                    .to_owned();
741
742         script_str.push_str(&format!("command script import {}\n",
743                                      &rust_pp_module_abs_path[..])[..]);
744         script_str.push_str("type summary add --no-value ");
745         script_str.push_str("--python-function lldb_rust_formatters.print_val ");
746         script_str.push_str("-x \".*\" --category Rust\n");
747         script_str.push_str("type category enable Rust\n");
748
749         // Set breakpoints on every line that contains the string "#break"
750         let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
751         for line in &breakpoint_lines {
752             script_str.push_str(&format!("breakpoint set --file '{}' --line {}\n",
753                                          source_file_name,
754                                          line));
755         }
756
757         // Append the other commands
758         for line in &commands {
759             script_str.push_str(line);
760             script_str.push_str("\n");
761         }
762
763         // Finally, quit the debugger
764         script_str.push_str("\nquit\n");
765
766         // Write the script into a file
767         debug!("script_str = {}", script_str);
768         self.dump_output_file(&script_str, "debugger.script");
769         let debugger_script = self.make_out_name("debugger.script");
770
771         // Let LLDB execute the script via lldb_batchmode.py
772         let debugger_run_result = self.run_lldb(&exe_file,
773                                                 &debugger_script,
774                                                 &rust_src_root);
775
776         if !debugger_run_result.status.success() {
777             self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
778         }
779
780         self.check_debugger_output(&debugger_run_result, &check_lines);
781     }
782
783     fn run_lldb(&self,
784                 test_executable: &Path,
785                 debugger_script: &Path,
786                 rust_src_root: &Path)
787                 -> ProcRes {
788         // Prepare the lldb_batchmode which executes the debugger script
789         let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
790         self.cmd2procres(Command::new(&self.config.lldb_python)
791                          .arg(&lldb_script_path)
792                          .arg(test_executable)
793                          .arg(debugger_script)
794                          .env("PYTHONPATH",
795                               self.config.lldb_python_dir.as_ref().unwrap()))
796     }
797
798     fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
799         let (status, out, err) = match cmd.output() {
800             Ok(Output { status, stdout, stderr }) => {
801                 (status,
802                  String::from_utf8(stdout).unwrap(),
803                  String::from_utf8(stderr).unwrap())
804             },
805             Err(e) => {
806                 self.fatal(&format!("Failed to setup Python process for \
807                                       LLDB script: {}", e))
808             }
809         };
810
811         self.dump_output(&out, &err);
812         ProcRes {
813             status: Status::Normal(status),
814             stdout: out,
815             stderr: err,
816             cmdline: format!("{:?}", cmd)
817         }
818     }
819
820     fn parse_debugger_commands(&self, debugger_prefix: &str) -> DebuggerCommands {
821         let command_directive = format!("{}-command", debugger_prefix);
822         let check_directive = format!("{}-check", debugger_prefix);
823
824         let mut breakpoint_lines = vec!();
825         let mut commands = vec!();
826         let mut check_lines = vec!();
827         let mut counter = 1;
828         let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
829         for line in reader.lines() {
830             match line {
831                 Ok(line) => {
832                     if line.contains("#break") {
833                         breakpoint_lines.push(counter);
834                     }
835
836                     header::parse_name_value_directive(
837                         &line,
838                         &command_directive).map(|cmd| {
839                             commands.push(cmd)
840                         });
841
842                     header::parse_name_value_directive(
843                         &line,
844                         &check_directive).map(|cmd| {
845                             check_lines.push(cmd)
846                         });
847                 }
848                 Err(e) => {
849                     self.fatal(&format!("Error while parsing debugger commands: {}", e))
850                 }
851             }
852             counter += 1;
853         }
854
855         DebuggerCommands {
856             commands: commands,
857             check_lines: check_lines,
858             breakpoint_lines: breakpoint_lines,
859         }
860     }
861
862     fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
863         if options.is_none() {
864             return None;
865         }
866
867         // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
868         let options_to_remove = [
869             "-O".to_owned(),
870             "-g".to_owned(),
871             "--debuginfo".to_owned()
872         ];
873         let new_options =
874             self.split_maybe_args(options).into_iter()
875                                           .filter(|x| !options_to_remove.contains(x))
876                                           .collect::<Vec<String>>();
877
878         Some(new_options.join(" "))
879     }
880
881     fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
882         let num_check_lines = check_lines.len();
883
884         let mut check_line_index = 0;
885         for line in debugger_run_result.stdout.lines() {
886             if check_line_index >= num_check_lines {
887                 break;
888             }
889
890             if check_single_line(line, &(check_lines[check_line_index])[..]) {
891                 check_line_index += 1;
892             }
893         }
894         if check_line_index != num_check_lines && num_check_lines > 0 {
895             self.fatal_proc_rec(&format!("line not found in debugger output: {}",
896                                          check_lines[check_line_index]),
897                                 debugger_run_result);
898         }
899
900         fn check_single_line(line: &str, check_line: &str) -> bool {
901             // Allow check lines to leave parts unspecified (e.g., uninitialized
902             // bits in the  wrong case of an enum) with the notation "[...]".
903             let line = line.trim();
904             let check_line = check_line.trim();
905             let can_start_anywhere = check_line.starts_with("[...]");
906             let can_end_anywhere = check_line.ends_with("[...]");
907
908             let check_fragments: Vec<&str> = check_line.split("[...]")
909                                                        .filter(|frag| !frag.is_empty())
910                                                        .collect();
911             if check_fragments.is_empty() {
912                 return true;
913             }
914
915             let (mut rest, first_fragment) = if can_start_anywhere {
916                 match line.find(check_fragments[0]) {
917                     Some(pos) => (&line[pos + check_fragments[0].len() ..], 1),
918                     None => return false
919                 }
920             } else {
921                 (line, 0)
922             };
923
924             for fragment_index in first_fragment .. check_fragments.len() {
925                 let current_fragment = check_fragments[fragment_index];
926                 match rest.find(current_fragment) {
927                     Some(pos) => {
928                         rest = &rest[pos + current_fragment.len() .. ];
929                     }
930                     None => return false
931                 }
932             }
933
934             if !can_end_anywhere && !rest.is_empty() {
935                 return false;
936             }
937
938             return true;
939         }
940     }
941
942     fn check_error_patterns(&self,
943                             output_to_check: &str,
944                             proc_res: &ProcRes) {
945         if self.props.error_patterns.is_empty() {
946             self.fatal(&format!("no error pattern specified in {:?}",
947                                 self.testpaths.file.display()));
948         }
949         let mut next_err_idx = 0;
950         let mut next_err_pat = self.props.error_patterns[next_err_idx].trim();
951         let mut done = false;
952         for line in output_to_check.lines() {
953             if line.contains(next_err_pat) {
954                 debug!("found error pattern {}", next_err_pat);
955                 next_err_idx += 1;
956                 if next_err_idx == self.props.error_patterns.len() {
957                     debug!("found all error patterns");
958                     done = true;
959                     break;
960                 }
961                 next_err_pat = self.props.error_patterns[next_err_idx].trim();
962             }
963         }
964         if done { return; }
965
966         let missing_patterns = &self.props.error_patterns[next_err_idx..];
967         if missing_patterns.len() == 1 {
968             self.fatal_proc_rec(
969                 &format!("error pattern '{}' not found!", missing_patterns[0]),
970                 proc_res);
971         } else {
972             for pattern in missing_patterns {
973                 self.error(&format!("error pattern '{}' not found!", *pattern));
974             }
975             self.fatal_proc_rec("multiple error patterns not found", proc_res);
976         }
977     }
978
979     fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
980         for line in proc_res.stderr.lines() {
981             if line.starts_with("error: internal compiler error:") {
982                 self.fatal_proc_rec("compiler encountered internal error", proc_res);
983             }
984         }
985     }
986
987     fn check_forbid_output(&self,
988                            output_to_check: &str,
989                            proc_res: &ProcRes) {
990         for pat in &self.props.forbid_output {
991             if output_to_check.contains(pat) {
992                 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
993             }
994         }
995     }
996
997     fn check_expected_errors(&self,
998                              expected_errors: Vec<errors::Error>,
999                              proc_res: &ProcRes) {
1000         if proc_res.status.success() &&
1001             expected_errors.iter().any(|x| x.kind == Some(ErrorKind::Error)) {
1002             self.fatal_proc_rec("process did not return an error status", proc_res);
1003         }
1004
1005         let file_name =
1006             format!("{}", self.testpaths.file.display())
1007             .replace(r"\", "/"); // on windows, translate all '\' path separators to '/'
1008
1009         // If the testcase being checked contains at least one expected "help"
1010         // message, then we'll ensure that all "help" messages are expected.
1011         // Otherwise, all "help" messages reported by the compiler will be ignored.
1012         // This logic also applies to "note" messages.
1013         let expect_help = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Help));
1014         let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
1015
1016         // Parse the JSON output from the compiler and extract out the messages.
1017         let actual_errors = json::parse_output(&file_name, &proc_res.stderr, &proc_res);
1018         let mut unexpected = Vec::new();
1019         let mut found = vec![false; expected_errors.len()];
1020         for actual_error in &actual_errors {
1021             let opt_index =
1022                 expected_errors
1023                 .iter()
1024                 .enumerate()
1025                 .position(|(index, expected_error)| {
1026                     !found[index] &&
1027                         actual_error.line_num == expected_error.line_num &&
1028                         (expected_error.kind.is_none() ||
1029                          actual_error.kind == expected_error.kind) &&
1030                         actual_error.msg.contains(&expected_error.msg)
1031                 });
1032
1033             match opt_index {
1034                 Some(index) => {
1035                     // found a match, everybody is happy
1036                     assert!(!found[index]);
1037                     found[index] = true;
1038                 }
1039
1040                 None => {
1041                     if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1042                         self.error(
1043                             &format!("{}:{}: unexpected {:?}: '{}'",
1044                                      file_name,
1045                                      actual_error.line_num,
1046                                      actual_error.kind.as_ref()
1047                                      .map_or(String::from("message"),
1048                                              |k| k.to_string()),
1049                                      actual_error.msg));
1050                         unexpected.push(actual_error.clone());
1051                     }
1052                 }
1053             }
1054         }
1055
1056         let mut not_found = Vec::new();
1057         // anything not yet found is a problem
1058         for (index, expected_error) in expected_errors.iter().enumerate() {
1059             if !found[index] {
1060                 self.error(
1061                     &format!("{}:{}: expected {} not found: {}",
1062                              file_name,
1063                              expected_error.line_num,
1064                              expected_error.kind.as_ref()
1065                              .map_or("message".into(),
1066                                      |k| k.to_string()),
1067                              expected_error.msg));
1068                 not_found.push(expected_error.clone());
1069             }
1070         }
1071
1072         if unexpected.len() > 0 || not_found.len() > 0 {
1073             self.error(
1074                 &format!("{} unexpected errors found, {} expected errors not found",
1075                          unexpected.len(), not_found.len()));
1076             print!("status: {}\ncommand: {}\n",
1077                    proc_res.status, proc_res.cmdline);
1078             if unexpected.len() > 0 {
1079                 println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1080             }
1081             if not_found.len() > 0 {
1082                 println!("not found errors (from test file): {:#?}\n", not_found);
1083             }
1084             panic!();
1085         }
1086     }
1087
1088     /// Returns true if we should report an error about `actual_error`,
1089     /// which did not match any of the expected error. We always require
1090     /// errors/warnings to be explicitly listed, but only require
1091     /// helps/notes if there are explicit helps/notes given.
1092     fn is_unexpected_compiler_message(&self,
1093                                       actual_error: &Error,
1094                                       expect_help: bool,
1095                                       expect_note: bool)
1096                                       -> bool {
1097         match actual_error.kind {
1098             Some(ErrorKind::Help) => expect_help,
1099             Some(ErrorKind::Note) => expect_note,
1100             Some(ErrorKind::Error) => true,
1101             Some(ErrorKind::Warning) => true,
1102             Some(ErrorKind::Suggestion) => false,
1103             None => false
1104         }
1105     }
1106
1107     fn compile_test(&self) -> ProcRes {
1108         let aux_dir = self.aux_output_dir_name();
1109         // FIXME (#9639): This needs to handle non-utf8 paths
1110         let link_args = vec!("-L".to_owned(),
1111                              aux_dir.to_str().unwrap().to_owned());
1112         let args = self.make_compile_args(link_args,
1113                                           &self.testpaths.file,
1114                                           TargetLocation::ThisFile(self.make_exe_name()));
1115         self.compose_and_run_compiler(args, None)
1116     }
1117
1118     fn document(&self, out_dir: &Path) -> ProcRes {
1119         if self.props.build_aux_docs {
1120             for rel_ab in &self.props.aux_builds {
1121                 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1122                 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1123                 let aux_cx = TestCx {
1124                     config: self.config,
1125                     props: &aux_props,
1126                     testpaths: &aux_testpaths,
1127                     revision: self.revision
1128                 };
1129                 let auxres = aux_cx.document(out_dir);
1130                 if !auxres.status.success() {
1131                     return auxres;
1132                 }
1133             }
1134         }
1135
1136         let aux_dir = self.aux_output_dir_name();
1137         let mut args = vec!["-L".to_owned(),
1138                             aux_dir.to_str().unwrap().to_owned(),
1139                             "-o".to_owned(),
1140                             out_dir.to_str().unwrap().to_owned(),
1141                             self.testpaths.file.to_str().unwrap().to_owned()];
1142         args.extend(self.props.compile_flags.iter().cloned());
1143         let args = ProcArgs {
1144             prog: self.config.rustdoc_path.to_str().unwrap().to_owned(),
1145             args: args,
1146         };
1147         self.compose_and_run_compiler(args, None)
1148     }
1149
1150     fn exec_compiled_test(&self) -> ProcRes {
1151         let env = self.props.exec_env.clone();
1152
1153         match &*self.config.target {
1154
1155             "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1156                 self._arm_exec_compiled_test(env)
1157             }
1158
1159             _=> {
1160                 let aux_dir = self.aux_output_dir_name();
1161                 self.compose_and_run(self.make_run_args(),
1162                                      env,
1163                                      self.config.run_lib_path.to_str().unwrap(),
1164                                      Some(aux_dir.to_str().unwrap()),
1165                                      None)
1166             }
1167         }
1168     }
1169
1170     /// For each `aux-build: foo/bar` annotation, we check to find the
1171     /// file in a `aux` directory relative to the test itself.
1172     fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1173         let test_ab = self.testpaths.file
1174                                     .parent()
1175                                     .expect("test file path has no parent")
1176                                     .join("auxiliary")
1177                                     .join(rel_ab);
1178         if !test_ab.exists() {
1179             self.fatal(&format!("aux-build `{}` source not found", test_ab.display()))
1180         }
1181
1182         TestPaths {
1183             file: test_ab,
1184             base: self.testpaths.base.clone(),
1185             relative_dir: self.testpaths.relative_dir
1186                                         .join("auxiliary")
1187                                         .join(rel_ab)
1188                                         .parent()
1189                                         .expect("aux-build path has no parent")
1190                                         .to_path_buf()
1191         }
1192     }
1193
1194     fn compose_and_run_compiler(&self, args: ProcArgs, input: Option<String>) -> ProcRes {
1195         if !self.props.aux_builds.is_empty() {
1196             self.create_dir_racy(&self.aux_output_dir_name());
1197         }
1198
1199         let aux_dir = self.aux_output_dir_name();
1200         // FIXME (#9639): This needs to handle non-utf8 paths
1201         let extra_link_args = vec!["-L".to_owned(),
1202                                    aux_dir.to_str().unwrap().to_owned()];
1203
1204         for rel_ab in &self.props.aux_builds {
1205             let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1206             let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1207             let mut crate_type = if aux_props.no_prefer_dynamic {
1208                 Vec::new()
1209             } else {
1210                 // We primarily compile all auxiliary libraries as dynamic libraries
1211                 // to avoid code size bloat and large binaries as much as possible
1212                 // for the test suite (otherwise including libstd statically in all
1213                 // executables takes up quite a bit of space).
1214                 //
1215                 // For targets like MUSL or Emscripten, however, there is no support for
1216                 // dynamic libraries so we just go back to building a normal library. Note,
1217                 // however, that for MUSL if the library is built with `force_host` then
1218                 // it's ok to be a dylib as the host should always support dylibs.
1219                 if (self.config.target.contains("musl") && !aux_props.force_host) ||
1220                     self.config.target.contains("emscripten")
1221                 {
1222                     vec!("--crate-type=lib".to_owned())
1223                 } else {
1224                     vec!("--crate-type=dylib".to_owned())
1225                 }
1226             };
1227             crate_type.extend(extra_link_args.clone());
1228             let aux_output = {
1229                 let f = self.make_lib_name(&self.testpaths.file);
1230                 let parent = f.parent().unwrap();
1231                 TargetLocation::ThisDirectory(parent.to_path_buf())
1232             };
1233             let aux_cx = TestCx {
1234                 config: self.config,
1235                 props: &aux_props,
1236                 testpaths: &aux_testpaths,
1237                 revision: self.revision
1238             };
1239             let aux_args = aux_cx.make_compile_args(crate_type, &aux_testpaths.file, aux_output);
1240             let auxres = aux_cx.compose_and_run(aux_args,
1241                                                 Vec::new(),
1242                                                 aux_cx.config.compile_lib_path.to_str().unwrap(),
1243                                                 Some(aux_dir.to_str().unwrap()),
1244                                                 None);
1245             if !auxres.status.success() {
1246                 self.fatal_proc_rec(
1247                     &format!("auxiliary build of {:?} failed to compile: ",
1248                              aux_testpaths.file.display()),
1249                     &auxres);
1250             }
1251
1252             match &*self.config.target {
1253                 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1254                     self._arm_push_aux_shared_library();
1255                 }
1256                 _ => {}
1257             }
1258         }
1259
1260         self.compose_and_run(args,
1261                              self.props.rustc_env.clone(),
1262                              self.config.compile_lib_path.to_str().unwrap(),
1263                              Some(aux_dir.to_str().unwrap()),
1264                              input)
1265     }
1266
1267     // Like std::fs::create_dir_all, except handles concurrent calls among multiple
1268     // threads or processes.
1269     fn create_dir_racy(&self, path: &Path) {
1270         match fs::create_dir(path) {
1271             Ok(()) => return,
1272             Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return,
1273             Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
1274             Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1275         }
1276         self.create_dir_racy(path.parent().unwrap());
1277         match fs::create_dir(path) {
1278             Ok(()) => {}
1279             Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => {}
1280             Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1281         }
1282     }
1283
1284     fn compose_and_run(&self,
1285                        ProcArgs{ args, prog }: ProcArgs,
1286                        procenv: Vec<(String, String)> ,
1287                        lib_path: &str,
1288                        aux_path: Option<&str>,
1289                        input: Option<String>) -> ProcRes {
1290         return self.program_output(lib_path, prog, aux_path, args, procenv, input);
1291     }
1292
1293     fn make_compile_args(&self,
1294                          extras: Vec<String> ,
1295                          input_file: &Path,
1296                          output_file: TargetLocation)
1297                          -> ProcArgs
1298     {
1299         let target = if self.props.force_host {
1300             &*self.config.host
1301         } else {
1302             &*self.config.target
1303         };
1304
1305         // FIXME (#9639): This needs to handle non-utf8 paths
1306         let mut args = vec!(input_file.to_str().unwrap().to_owned(),
1307                             "-L".to_owned(),
1308                             self.config.build_base.to_str().unwrap().to_owned(),
1309                             format!("--target={}", target));
1310
1311         if let Some(revision) = self.revision {
1312             args.extend(vec![
1313                 format!("--cfg"),
1314                 format!("{}", revision),
1315             ]);
1316         }
1317
1318         if let Some(ref incremental_dir) = self.props.incremental_dir {
1319             args.extend(vec![
1320                 format!("-Z"),
1321                 format!("incremental={}", incremental_dir.display()),
1322             ]);
1323         }
1324
1325
1326         match self.config.mode {
1327             CompileFail |
1328             ParseFail |
1329             RunPass |
1330             Incremental => {
1331                 // If we are extracting and matching errors in the new
1332                 // fashion, then you want JSON mode. Old-skool error
1333                 // patterns still match the raw compiler output.
1334                 if self.props.error_patterns.is_empty() {
1335                     args.extend(["--error-format",
1336                                  "json"]
1337                                 .iter()
1338                                 .map(|s| s.to_string()));
1339                 }
1340             }
1341             MirOpt => {
1342                 args.extend(["-Z",
1343                              "dump-mir=all",
1344                              "-Z",
1345                              "mir-opt-level=3",
1346                              "-Z"]
1347                             .iter()
1348                             .map(|s| s.to_string()));
1349
1350
1351                 let mir_dump_dir = self.get_mir_dump_dir();
1352                 self.create_dir_racy(mir_dump_dir.as_path());
1353                 let mut dir_opt = "dump-mir-dir=".to_string();
1354                 dir_opt.push_str(mir_dump_dir.to_str().unwrap());
1355                 debug!("dir_opt: {:?}", dir_opt);
1356
1357                 args.push(dir_opt);
1358             }
1359             RunFail |
1360             RunPassValgrind |
1361             Pretty |
1362             DebugInfoGdb |
1363             DebugInfoLldb |
1364             Codegen |
1365             Rustdoc |
1366             RunMake |
1367             Ui |
1368             CodegenUnits => {
1369                 // do not use JSON output
1370             }
1371         }
1372
1373         args.extend_from_slice(&extras);
1374         if !self.props.no_prefer_dynamic {
1375             args.push("-C".to_owned());
1376             args.push("prefer-dynamic".to_owned());
1377         }
1378         let path = match output_file {
1379             TargetLocation::ThisFile(path) => {
1380                 args.push("-o".to_owned());
1381                 path
1382             }
1383             TargetLocation::ThisDirectory(path) => {
1384                 args.push("--out-dir".to_owned());
1385                 path
1386             }
1387         };
1388         args.push(path.to_str().unwrap().to_owned());
1389         if self.props.force_host {
1390             args.extend(self.split_maybe_args(&self.config.host_rustcflags));
1391         } else {
1392             args.extend(self.split_maybe_args(&self.config.target_rustcflags));
1393         }
1394         args.extend(self.props.compile_flags.iter().cloned());
1395         return ProcArgs {
1396             prog: self.config.rustc_path.to_str().unwrap().to_owned(),
1397             args: args,
1398         };
1399     }
1400
1401     fn make_lib_name(&self, auxfile: &Path) -> PathBuf {
1402         // what we return here is not particularly important, as it
1403         // happens; rustc ignores everything except for the directory.
1404         let auxname = self.output_testname(auxfile);
1405         self.aux_output_dir_name().join(&auxname)
1406     }
1407
1408     fn make_exe_name(&self) -> PathBuf {
1409         let mut f = self.output_base_name();
1410         // FIXME: This is using the host architecture exe suffix, not target!
1411         if self.config.target == "asmjs-unknown-emscripten" {
1412             let mut fname = f.file_name().unwrap().to_os_string();
1413             fname.push(".js");
1414             f.set_file_name(&fname);
1415         } else if !env::consts::EXE_SUFFIX.is_empty() {
1416             let mut fname = f.file_name().unwrap().to_os_string();
1417             fname.push(env::consts::EXE_SUFFIX);
1418             f.set_file_name(&fname);
1419         }
1420         f
1421     }
1422
1423     fn make_run_args(&self) -> ProcArgs {
1424         // If we've got another tool to run under (valgrind),
1425         // then split apart its command
1426         let mut args = self.split_maybe_args(&self.config.runtool);
1427
1428         // If this is emscripten, then run tests under nodejs
1429         if self.config.target == "asmjs-unknown-emscripten" {
1430             args.push("nodejs".to_owned());
1431         }
1432
1433         let exe_file = self.make_exe_name();
1434
1435         // FIXME (#9639): This needs to handle non-utf8 paths
1436         args.push(exe_file.to_str().unwrap().to_owned());
1437
1438         // Add the arguments in the run_flags directive
1439         args.extend(self.split_maybe_args(&self.props.run_flags));
1440
1441         let prog = args.remove(0);
1442         return ProcArgs {
1443             prog: prog,
1444             args: args,
1445         };
1446     }
1447
1448     fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1449         match *argstr {
1450             Some(ref s) => {
1451                 s
1452                     .split(' ')
1453                     .filter_map(|s| {
1454                         if s.chars().all(|c| c.is_whitespace()) {
1455                             None
1456                         } else {
1457                             Some(s.to_owned())
1458                         }
1459                     }).collect()
1460             }
1461             None => Vec::new()
1462         }
1463     }
1464
1465     fn program_output(&self,
1466                       lib_path: &str,
1467                       prog: String,
1468                       aux_path: Option<&str>,
1469                       args: Vec<String>,
1470                       env: Vec<(String, String)>,
1471                       input: Option<String>)
1472                       -> ProcRes {
1473         let cmdline =
1474         {
1475             let cmdline = self.make_cmdline(lib_path,
1476                                             &prog,
1477                                             &args);
1478             logv(self.config, format!("executing {}", cmdline));
1479             cmdline
1480         };
1481         let procsrv::Result {
1482             out,
1483             err,
1484             status
1485         } = procsrv::run(lib_path,
1486                          &prog,
1487                          aux_path,
1488                          &args,
1489                          env,
1490                          input).expect(&format!("failed to exec `{}`", prog));
1491         self.dump_output(&out, &err);
1492         return ProcRes {
1493             status: Status::Normal(status),
1494             stdout: out,
1495             stderr: err,
1496             cmdline: cmdline,
1497         };
1498     }
1499
1500     fn make_cmdline(&self, libpath: &str, prog: &str, args: &[String]) -> String {
1501         use util;
1502
1503         // Linux and mac don't require adjusting the library search path
1504         if cfg!(unix) {
1505             format!("{} {}", prog, args.join(" "))
1506         } else {
1507             // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1508             // for diagnostic purposes
1509             fn lib_path_cmd_prefix(path: &str) -> String {
1510                 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1511             }
1512
1513             format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1514         }
1515     }
1516
1517     fn dump_output(&self, out: &str, err: &str) {
1518         self.dump_output_file(out, "out");
1519         self.dump_output_file(err, "err");
1520         self.maybe_dump_to_stdout(out, err);
1521     }
1522
1523     fn dump_output_file(&self,
1524                         out: &str,
1525                         extension: &str) {
1526         let outfile = self.make_out_name(extension);
1527         File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1528     }
1529
1530     fn make_out_name(&self, extension: &str) -> PathBuf {
1531         self.output_base_name().with_extension(extension)
1532     }
1533
1534     fn aux_output_dir_name(&self) -> PathBuf {
1535         let f = self.output_base_name();
1536         let mut fname = f.file_name().unwrap().to_os_string();
1537         fname.push(&format!(".{}.libaux", self.config.mode));
1538         f.with_file_name(&fname)
1539     }
1540
1541     fn output_testname(&self, filepath: &Path) -> PathBuf {
1542         PathBuf::from(filepath.file_stem().unwrap())
1543     }
1544
1545     /// Given a test path like `compile-fail/foo/bar.rs` Returns a name like
1546     ///
1547     ///     <output>/foo/bar-stage1
1548     fn output_base_name(&self) -> PathBuf {
1549         let dir = self.config.build_base.join(&self.testpaths.relative_dir);
1550
1551         // Note: The directory `dir` is created during `collect_tests_from_dir`
1552         dir
1553             .join(&self.output_testname(&self.testpaths.file))
1554             .with_extension(&self.config.stage_id)
1555     }
1556
1557     fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1558         if self.config.verbose {
1559             println!("------{}------------------------------", "stdout");
1560             println!("{}", out);
1561             println!("------{}------------------------------", "stderr");
1562             println!("{}", err);
1563             println!("------------------------------------------");
1564         }
1565     }
1566
1567     fn error(&self, err: &str) {
1568         match self.revision {
1569             Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1570             None => println!("\nerror: {}", err)
1571         }
1572     }
1573
1574     fn fatal(&self, err: &str) -> ! {
1575         self.error(err); panic!();
1576     }
1577
1578     fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1579         self.error(err);
1580         proc_res.fatal(None);
1581     }
1582
1583     fn _arm_exec_compiled_test(&self, env: Vec<(String, String)>) -> ProcRes {
1584         let args = self.make_run_args();
1585         let cmdline = self.make_cmdline("", &args.prog, &args.args);
1586
1587         // get bare program string
1588         let mut tvec: Vec<String> = args.prog
1589                                         .split('/')
1590                                         .map(str::to_owned)
1591                                         .collect();
1592         let prog_short = tvec.pop().unwrap();
1593
1594         // copy to target
1595         let copy_result = procsrv::run("",
1596                                        &self.config.adb_path,
1597                                        None,
1598                                        &[
1599                                            "push".to_owned(),
1600                                            args.prog.clone(),
1601                                            self.config.adb_test_dir.clone()
1602                                        ],
1603                                        vec!(("".to_owned(), "".to_owned())),
1604                                        Some("".to_owned()))
1605             .expect(&format!("failed to exec `{}`", self.config.adb_path));
1606
1607         if self.config.verbose {
1608             println!("push ({}) {} {} {}",
1609                      self.config.target,
1610                      args.prog,
1611                      copy_result.out,
1612                      copy_result.err);
1613         }
1614
1615         logv(self.config, format!("executing ({}) {}", self.config.target, cmdline));
1616
1617         let mut runargs = Vec::new();
1618
1619         // run test via adb_run_wrapper
1620         runargs.push("shell".to_owned());
1621         for (key, val) in env {
1622             runargs.push(format!("{}={}", key, val));
1623         }
1624         runargs.push(format!("{}/../adb_run_wrapper.sh", self.config.adb_test_dir));
1625         runargs.push(format!("{}", self.config.adb_test_dir));
1626         runargs.push(format!("{}", prog_short));
1627
1628         for tv in &args.args {
1629             runargs.push(tv.to_owned());
1630         }
1631         procsrv::run("",
1632                      &self.config.adb_path,
1633                      None,
1634                      &runargs,
1635                      vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1636             .expect(&format!("failed to exec `{}`", self.config.adb_path));
1637
1638         // get exitcode of result
1639         runargs = Vec::new();
1640         runargs.push("shell".to_owned());
1641         runargs.push("cat".to_owned());
1642         runargs.push(format!("{}/{}.exitcode", self.config.adb_test_dir, prog_short));
1643
1644         let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1645             procsrv::run("",
1646                          &self.config.adb_path,
1647                          None,
1648                          &runargs,
1649                          vec!(("".to_owned(), "".to_owned())),
1650                          Some("".to_owned()))
1651             .expect(&format!("failed to exec `{}`", self.config.adb_path));
1652
1653         let mut exitcode: i32 = 0;
1654         for c in exitcode_out.chars() {
1655             if !c.is_numeric() { break; }
1656             exitcode = exitcode * 10 + match c {
1657                 '0' ... '9' => c as i32 - ('0' as i32),
1658                 _ => 101,
1659             }
1660         }
1661
1662         // get stdout of result
1663         runargs = Vec::new();
1664         runargs.push("shell".to_owned());
1665         runargs.push("cat".to_owned());
1666         runargs.push(format!("{}/{}.stdout", self.config.adb_test_dir, prog_short));
1667
1668         let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1669             procsrv::run("",
1670                          &self.config.adb_path,
1671                          None,
1672                          &runargs,
1673                          vec!(("".to_owned(), "".to_owned())),
1674                          Some("".to_owned()))
1675             .expect(&format!("failed to exec `{}`", self.config.adb_path));
1676
1677         // get stderr of result
1678         runargs = Vec::new();
1679         runargs.push("shell".to_owned());
1680         runargs.push("cat".to_owned());
1681         runargs.push(format!("{}/{}.stderr", self.config.adb_test_dir, prog_short));
1682
1683         let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1684             procsrv::run("",
1685                          &self.config.adb_path,
1686                          None,
1687                          &runargs,
1688                          vec!(("".to_owned(), "".to_owned())),
1689                          Some("".to_owned()))
1690             .expect(&format!("failed to exec `{}`", self.config.adb_path));
1691
1692         self.dump_output(&stdout_out, &stderr_out);
1693
1694         ProcRes {
1695             status: Status::Parsed(exitcode),
1696             stdout: stdout_out,
1697             stderr: stderr_out,
1698             cmdline: cmdline
1699         }
1700     }
1701
1702     fn _arm_push_aux_shared_library(&self) {
1703         let tdir = self.aux_output_dir_name();
1704
1705         let dirs = fs::read_dir(&tdir).unwrap();
1706         for file in dirs {
1707             let file = file.unwrap().path();
1708             if file.extension().and_then(|s| s.to_str()) == Some("so") {
1709                 // FIXME (#9639): This needs to handle non-utf8 paths
1710                 let copy_result = procsrv::run("",
1711                                                &self.config.adb_path,
1712                                                None,
1713                                                &[
1714                                                    "push".to_owned(),
1715                                                    file.to_str()
1716                                                        .unwrap()
1717                                                        .to_owned(),
1718                                                    self.config.adb_test_dir.to_owned(),
1719                                                ],
1720                                                vec!(("".to_owned(),
1721                                                      "".to_owned())),
1722                                                Some("".to_owned()))
1723                     .expect(&format!("failed to exec `{}`", self.config.adb_path));
1724
1725                 if self.config.verbose {
1726                     println!("push ({}) {:?} {} {}",
1727                              self.config.target, file.display(),
1728                              copy_result.out, copy_result.err);
1729                 }
1730             }
1731         }
1732     }
1733
1734     // codegen tests (using FileCheck)
1735
1736     fn compile_test_and_save_ir(&self) -> ProcRes {
1737         let aux_dir = self.aux_output_dir_name();
1738         // FIXME (#9639): This needs to handle non-utf8 paths
1739         let mut link_args = vec!("-L".to_owned(),
1740                                  aux_dir.to_str().unwrap().to_owned());
1741         let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1742         link_args.extend(llvm_args);
1743         let args = self.make_compile_args(link_args,
1744                                           &self.testpaths.file,
1745                                           TargetLocation::ThisDirectory(
1746                                               self.output_base_name().parent()
1747                                                                      .unwrap()
1748                                                                      .to_path_buf()));
1749         self.compose_and_run_compiler(args, None)
1750     }
1751
1752     fn check_ir_with_filecheck(&self) -> ProcRes {
1753         let irfile = self.output_base_name().with_extension("ll");
1754         let prog = self.config.llvm_filecheck.as_ref().unwrap();
1755         let proc_args = ProcArgs {
1756             // FIXME (#9639): This needs to handle non-utf8 paths
1757             prog: prog.to_str().unwrap().to_owned(),
1758             args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1759                        self.testpaths.file.to_str().unwrap().to_owned())
1760         };
1761         self.compose_and_run(proc_args, Vec::new(), "", None, None)
1762     }
1763
1764     fn run_codegen_test(&self) {
1765         assert!(self.revision.is_none(), "revisions not relevant here");
1766
1767         if self.config.llvm_filecheck.is_none() {
1768             self.fatal("missing --llvm-filecheck");
1769         }
1770
1771         let mut proc_res = self.compile_test_and_save_ir();
1772         if !proc_res.status.success() {
1773             self.fatal_proc_rec("compilation failed!", &proc_res);
1774         }
1775
1776         proc_res = self.check_ir_with_filecheck();
1777         if !proc_res.status.success() {
1778             self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
1779         }
1780     }
1781
1782     fn charset() -> &'static str {
1783         // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1784         if cfg!(target_os = "bitrig") {
1785             "auto"
1786         } else if cfg!(target_os = "freebsd") {
1787             "ISO-8859-1"
1788         } else {
1789             "UTF-8"
1790         }
1791     }
1792
1793     fn run_rustdoc_test(&self) {
1794         assert!(self.revision.is_none(), "revisions not relevant here");
1795
1796         let out_dir = self.output_base_name();
1797         let _ = fs::remove_dir_all(&out_dir);
1798         self.create_dir_racy(&out_dir);
1799
1800         let proc_res = self.document(&out_dir);
1801         if !proc_res.status.success() {
1802             self.fatal_proc_rec("rustdoc failed!", &proc_res);
1803         }
1804         let root = self.find_rust_src_root().unwrap();
1805
1806         let res = self.cmd2procres(Command::new(&self.config.docck_python)
1807                                    .arg(root.join("src/etc/htmldocck.py"))
1808                                    .arg(out_dir)
1809                                    .arg(&self.testpaths.file));
1810         if !res.status.success() {
1811             self.fatal_proc_rec("htmldocck failed!", &res);
1812         }
1813     }
1814
1815     fn run_codegen_units_test(&self) {
1816         assert!(self.revision.is_none(), "revisions not relevant here");
1817
1818         let proc_res = self.compile_test();
1819
1820         if !proc_res.status.success() {
1821             self.fatal_proc_rec("compilation failed!", &proc_res);
1822         }
1823
1824         self.check_no_compiler_crash(&proc_res);
1825
1826         const PREFIX: &'static str = "TRANS_ITEM ";
1827         const CGU_MARKER: &'static str = "@@";
1828
1829         let actual: Vec<TransItem> = proc_res
1830             .stdout
1831             .lines()
1832             .filter(|line| line.starts_with(PREFIX))
1833             .map(str_to_trans_item)
1834             .collect();
1835
1836         let expected: Vec<TransItem> = errors::load_errors(&self.testpaths.file, None)
1837             .iter()
1838             .map(|e| str_to_trans_item(&e.msg[..]))
1839             .collect();
1840
1841         let mut missing = Vec::new();
1842         let mut wrong_cgus = Vec::new();
1843
1844         for expected_item in &expected {
1845             let actual_item_with_same_name = actual.iter()
1846                                                    .find(|ti| ti.name == expected_item.name);
1847
1848             if let Some(actual_item) = actual_item_with_same_name {
1849                 if !expected_item.codegen_units.is_empty() {
1850                     // Also check for codegen units
1851                     if expected_item.codegen_units != actual_item.codegen_units {
1852                         wrong_cgus.push((expected_item.clone(), actual_item.clone()));
1853                     }
1854                 }
1855             } else {
1856                 missing.push(expected_item.string.clone());
1857             }
1858         }
1859
1860         let unexpected: Vec<_> =
1861             actual.iter()
1862                   .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
1863                   .map(|acgu| acgu.string.clone())
1864                   .collect();
1865
1866         if !missing.is_empty() {
1867             missing.sort();
1868
1869             println!("\nThese items should have been contained but were not:\n");
1870
1871             for item in &missing {
1872                 println!("{}", item);
1873             }
1874
1875             println!("\n");
1876         }
1877
1878         if !unexpected.is_empty() {
1879             let sorted = {
1880                 let mut sorted = unexpected.clone();
1881                 sorted.sort();
1882                 sorted
1883             };
1884
1885             println!("\nThese items were contained but should not have been:\n");
1886
1887             for item in sorted {
1888                 println!("{}", item);
1889             }
1890
1891             println!("\n");
1892         }
1893
1894         if !wrong_cgus.is_empty() {
1895             wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
1896             println!("\nThe following items were assigned to wrong codegen units:\n");
1897
1898             for &(ref expected_item, ref actual_item) in &wrong_cgus {
1899                 println!("{}", expected_item.name);
1900                 println!("  expected: {}", codegen_units_to_str(&expected_item.codegen_units));
1901                 println!("  actual:   {}", codegen_units_to_str(&actual_item.codegen_units));
1902                 println!("");
1903             }
1904         }
1905
1906         if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty())
1907         {
1908             panic!();
1909         }
1910
1911         #[derive(Clone, Eq, PartialEq)]
1912         struct TransItem {
1913             name: String,
1914             codegen_units: HashSet<String>,
1915             string: String,
1916         }
1917
1918         // [TRANS_ITEM] name [@@ (cgu)+]
1919         fn str_to_trans_item(s: &str) -> TransItem {
1920             let s = if s.starts_with(PREFIX) {
1921                 (&s[PREFIX.len()..]).trim()
1922             } else {
1923                 s.trim()
1924             };
1925
1926             let full_string = format!("{}{}", PREFIX, s.trim().to_owned());
1927
1928             let parts: Vec<&str> = s.split(CGU_MARKER)
1929                                     .map(str::trim)
1930                                     .filter(|s| !s.is_empty())
1931                                     .collect();
1932
1933             let name = parts[0].trim();
1934
1935             let cgus = if parts.len() > 1 {
1936                 let cgus_str = parts[1];
1937
1938                 cgus_str.split(" ")
1939                         .map(str::trim)
1940                         .filter(|s| !s.is_empty())
1941                         .map(str::to_owned)
1942                         .collect()
1943             }
1944             else {
1945                 HashSet::new()
1946             };
1947
1948             TransItem {
1949                 name: name.to_owned(),
1950                 codegen_units: cgus,
1951                 string: full_string,
1952             }
1953         }
1954
1955         fn codegen_units_to_str(cgus: &HashSet<String>) -> String
1956         {
1957             let mut cgus: Vec<_> = cgus.iter().collect();
1958             cgus.sort();
1959
1960             let mut string = String::new();
1961             for cgu in cgus {
1962                 string.push_str(&cgu[..]);
1963                 string.push_str(" ");
1964             }
1965
1966             string
1967         }
1968     }
1969
1970     fn init_incremental_test(&self) {
1971         // (See `run_incremental_test` for an overview of how incremental tests work.)
1972
1973         // Before any of the revisions have executed, create the
1974         // incremental workproduct directory.  Delete any old
1975         // incremental work products that may be there from prior
1976         // runs.
1977         let incremental_dir = self.incremental_dir();
1978         if incremental_dir.exists() {
1979             fs::remove_dir_all(&incremental_dir).unwrap();
1980         }
1981         fs::create_dir_all(&incremental_dir).unwrap();
1982
1983         if self.config.verbose {
1984             print!("init_incremental_test: incremental_dir={}", incremental_dir.display());
1985         }
1986     }
1987
1988     fn run_incremental_test(&self) {
1989         // Basic plan for a test incremental/foo/bar.rs:
1990         // - load list of revisions rpass1, cfail2, rpass3
1991         //   - each should begin with `rpass`, `cfail`, or `cfail`
1992         //   - if `rpass`, expect compile and execution to succeed
1993         //   - if `cfail`, expect compilation to fail
1994         //   - if `rfail`, expect execution to fail
1995         // - create a directory build/foo/bar.incremental
1996         // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
1997         //   - because name of revision starts with "rpass", expect success
1998         // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
1999         //   - because name of revision starts with "cfail", expect an error
2000         //   - load expected errors as usual, but filter for those that end in `[rfail2]`
2001         // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
2002         //   - because name of revision starts with "rpass", expect success
2003         // - execute build/foo/bar.exe and save output
2004         //
2005         // FIXME -- use non-incremental mode as an oracle? That doesn't apply
2006         // to #[rustc_dirty] and clean tests I guess
2007
2008         let revision = self.revision.expect("incremental tests require a list of revisions");
2009
2010         // Incremental workproduct directory should have already been created.
2011         let incremental_dir = self.incremental_dir();
2012         assert!(incremental_dir.exists(), "init_incremental_test failed to create incremental dir");
2013
2014         // Add an extra flag pointing at the incremental directory.
2015         let mut revision_props = self.props.clone();
2016         revision_props.incremental_dir = Some(incremental_dir);
2017         revision_props.compile_flags.push(String::from("-Zincremental-info"));
2018
2019         let revision_cx = TestCx {
2020             config: self.config,
2021             props: &revision_props,
2022             testpaths: self.testpaths,
2023             revision: self.revision,
2024         };
2025
2026         if self.config.verbose {
2027             print!("revision={:?} revision_props={:#?}", revision, revision_props);
2028         }
2029
2030         if revision.starts_with("rpass") {
2031             revision_cx.run_rpass_test();
2032         } else if revision.starts_with("rfail") {
2033             revision_cx.run_rfail_test();
2034         } else if revision.starts_with("cfail") {
2035             revision_cx.run_cfail_test();
2036         } else {
2037             revision_cx.fatal(
2038                 "revision name must begin with rpass, rfail, or cfail");
2039         }
2040     }
2041
2042     /// Directory where incremental work products are stored.
2043     fn incremental_dir(&self) -> PathBuf {
2044         self.output_base_name().with_extension("incremental")
2045     }
2046
2047     fn run_rmake_test(&self) {
2048         // FIXME(#11094): we should fix these tests
2049         if self.config.host != self.config.target {
2050             return
2051         }
2052
2053         let cwd = env::current_dir().unwrap();
2054         let src_root = self.config.src_base.parent().unwrap()
2055                                            .parent().unwrap()
2056                                            .parent().unwrap();
2057         let src_root = cwd.join(&src_root);
2058
2059         let tmpdir = cwd.join(self.output_base_name());
2060         if tmpdir.exists() {
2061             self.aggressive_rm_rf(&tmpdir).unwrap();
2062         }
2063         self.create_dir_racy(&tmpdir);
2064
2065         let mut cmd = Command::new("make");
2066         cmd.current_dir(&self.testpaths.file)
2067            .env("TARGET", &self.config.target)
2068            .env("PYTHON", &self.config.docck_python)
2069            .env("S", src_root)
2070            .env("RUST_BUILD_STAGE", &self.config.stage_id)
2071            .env("RUSTC", cwd.join(&self.config.rustc_path))
2072            .env("RUSTDOC", cwd.join(&self.config.rustdoc_path))
2073            .env("TMPDIR", &tmpdir)
2074            .env("LD_LIB_PATH_ENVVAR", procsrv::dylib_env_var())
2075            .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2076            .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2077            .env("LLVM_COMPONENTS", &self.config.llvm_components)
2078            .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags);
2079
2080         if self.config.target.contains("msvc") {
2081             // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2082             // and that `lib.exe` lives next to it.
2083             let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2084
2085             // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2086             // a path and instead passes `C:\msys64\foo`, so convert all
2087             // `/`-arguments to MSVC here to `-` arguments.
2088             let cflags = self.config.cflags.split(' ').map(|s| s.replace("/", "-"))
2089                                                  .collect::<Vec<_>>().join(" ");
2090
2091             cmd.env("IS_MSVC", "1")
2092                .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2093                .env("CC", format!("'{}' {}", self.config.cc, cflags))
2094                .env("CXX", &self.config.cxx);
2095         } else {
2096             cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2097                .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags));
2098         }
2099
2100         let output = cmd.output().expect("failed to spawn `make`");
2101         if !output.status.success() {
2102             let res = ProcRes {
2103                 status: Status::Normal(output.status),
2104                 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2105                 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2106                 cmdline: format!("{:?}", cmd),
2107             };
2108             self.fatal_proc_rec("make failed", &res);
2109         }
2110     }
2111
2112     fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
2113         for e in try!(path.read_dir()) {
2114             let entry = try!(e);
2115             let path = entry.path();
2116             if try!(entry.file_type()).is_dir() {
2117                 try!(self.aggressive_rm_rf(&path));
2118             } else {
2119                 // Remove readonly files as well on windows (by default we can't)
2120                 try!(fs::remove_file(&path).or_else(|e| {
2121                     if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
2122                         let mut meta = try!(entry.metadata()).permissions();
2123                         meta.set_readonly(false);
2124                         try!(fs::set_permissions(&path, meta));
2125                         fs::remove_file(&path)
2126                     } else {
2127                         Err(e)
2128                     }
2129                 }))
2130             }
2131         }
2132         fs::remove_dir(path)
2133     }
2134
2135     fn run_ui_test(&self) {
2136         println!("ui: {}", self.testpaths.file.display());
2137
2138         let proc_res = self.compile_test();
2139
2140         let expected_stderr_path = self.expected_output_path("stderr");
2141         let expected_stderr = self.load_expected_output(&expected_stderr_path);
2142
2143         let expected_stdout_path = self.expected_output_path("stdout");
2144         let expected_stdout = self.load_expected_output(&expected_stdout_path);
2145
2146         let normalized_stdout = self.normalize_output(&proc_res.stdout);
2147         let normalized_stderr = self.normalize_output(&proc_res.stderr);
2148
2149         let mut errors = 0;
2150         errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2151         errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2152
2153         if errors > 0 {
2154             println!("To update references, run this command from build directory:");
2155             let relative_path_to_file =
2156                 self.testpaths.relative_dir
2157                               .join(self.testpaths.file.file_name().unwrap());
2158             println!("{}/update-references.sh '{}' '{}'",
2159                      self.config.src_base.display(),
2160                      self.config.build_base.display(),
2161                      relative_path_to_file.display());
2162             self.fatal_proc_rec(&format!("{} errors occurred comparing output.", errors),
2163                                 &proc_res);
2164         }
2165     }
2166
2167     fn run_mir_opt_test(&self) {
2168         let proc_res = self.compile_test();
2169
2170         if !proc_res.status.success() {
2171             self.fatal_proc_rec("compilation failed!", &proc_res);
2172         }
2173
2174         let proc_res = self.exec_compiled_test();
2175
2176         if !proc_res.status.success() {
2177             self.fatal_proc_rec("test run failed!", &proc_res);
2178         }
2179         self.check_mir_dump();
2180     }
2181
2182     fn check_mir_dump(&self) {
2183         let mut test_file_contents = String::new();
2184         fs::File::open(self.testpaths.file.clone()).unwrap()
2185                                                    .read_to_string(&mut test_file_contents)
2186                                                    .unwrap();
2187         if let Some(idx) =  test_file_contents.find("// END RUST SOURCE") {
2188             let (_, tests_text) = test_file_contents.split_at(idx + "// END_RUST SOURCE".len());
2189             let tests_text_str = String::from(tests_text);
2190             let mut curr_test : Option<&str> = None;
2191             let mut curr_test_contents = Vec::new();
2192             for l in tests_text_str.lines() {
2193                 debug!("line: {:?}", l);
2194                 if l.starts_with("// START ") {
2195                     let (_, t) = l.split_at("// START ".len());
2196                     curr_test = Some(t);
2197                 } else if l.starts_with("// END") {
2198                     let (_, t) = l.split_at("// END ".len());
2199                     if Some(t) != curr_test {
2200                         panic!("mismatched START END test name");
2201                     }
2202                     self.compare_mir_test_output(curr_test.unwrap(), &curr_test_contents);
2203                     curr_test = None;
2204                     curr_test_contents.clear();
2205                 } else if l.is_empty() {
2206                     // ignore
2207                 } else if l.starts_with("// ") {
2208                     let (_, test_content) = l.split_at("// ".len());
2209                     curr_test_contents.push(test_content);
2210                 }
2211             }
2212         }
2213     }
2214
2215     fn compare_mir_test_output(&self, test_name: &str, expected_content: &Vec<&str>) {
2216         let mut output_file = PathBuf::new();
2217         output_file.push(self.get_mir_dump_dir());
2218         output_file.push(test_name);
2219         debug!("comparing the contests of: {:?}", output_file);
2220         debug!("with: {:?}", expected_content);
2221
2222         let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
2223         let mut dumped_string = String::new();
2224         dumped_file.read_to_string(&mut dumped_string).unwrap();
2225         let mut dumped_lines = dumped_string.lines().filter(|l| !l.is_empty());
2226         let mut expected_lines = expected_content.iter().filter(|l| !l.is_empty());
2227
2228         // We expect each non-empty line from expected_content to appear
2229         // in the dump in order, but there may be extra lines interleaved
2230         while let Some(expected_line) = expected_lines.next() {
2231             let e_norm = normalize_mir_line(expected_line);
2232             if e_norm.is_empty() {
2233                 continue;
2234             };
2235             let mut found = false;
2236             while let Some(dumped_line) = dumped_lines.next() {
2237                 let d_norm = normalize_mir_line(dumped_line);
2238                 debug!("found: {:?}", d_norm);
2239                 debug!("expected: {:?}", e_norm);
2240                 if e_norm == d_norm {
2241                     found = true;
2242                     break;
2243                 };
2244             }
2245             if !found {
2246                 panic!("ran out of mir dump output to match against");
2247             }
2248         }
2249     }
2250
2251     fn get_mir_dump_dir(&self) -> PathBuf {
2252         let mut mir_dump_dir = PathBuf::from(self.config.build_base
2253                                                     .as_path()
2254                                                     .to_str()
2255                                                     .unwrap());
2256         debug!("input_file: {:?}", self.testpaths.file);
2257         mir_dump_dir.push(self.testpaths.file.file_stem().unwrap().to_str().unwrap());
2258         mir_dump_dir
2259     }
2260
2261     fn normalize_output(&self, output: &str) -> String {
2262         let parent_dir = self.testpaths.file.parent().unwrap();
2263         let parent_dir_str = parent_dir.display().to_string();
2264         output.replace(&parent_dir_str, "$DIR")
2265               .replace("\\", "/") // normalize for paths on windows
2266               .replace("\r\n", "\n") // normalize for linebreaks on windows
2267               .replace("\t", "\\t") // makes tabs visible
2268     }
2269
2270     fn expected_output_path(&self, kind: &str) -> PathBuf {
2271         let extension = match self.revision {
2272             Some(r) => format!("{}.{}", r, kind),
2273             None => kind.to_string(),
2274         };
2275         self.testpaths.file.with_extension(extension)
2276     }
2277
2278     fn load_expected_output(&self, path: &Path) -> String {
2279         if !path.exists() {
2280             return String::new();
2281         }
2282
2283         let mut result = String::new();
2284         match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
2285             Ok(_) => result,
2286             Err(e) => {
2287                 self.fatal(&format!("failed to load expected output from `{}`: {}",
2288                                     path.display(), e))
2289             }
2290         }
2291     }
2292
2293     fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2294         if actual == expected {
2295             return 0;
2296         }
2297
2298         println!("normalized {}:\n{}\n", kind, actual);
2299         println!("expected {}:\n{}\n", kind, expected);
2300         println!("diff of {}:\n", kind);
2301         for line in uidiff::diff_lines(actual, expected) {
2302             println!("{}", line);
2303         }
2304
2305         let output_file = self.output_base_name().with_extension(kind);
2306         match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
2307             Ok(()) => { }
2308             Err(e) => {
2309                 self.fatal(&format!("failed to write {} to `{}`: {}",
2310                                     kind, output_file.display(), e))
2311             }
2312         }
2313
2314         println!("\nThe actual {0} differed from the expected {0}.", kind);
2315         println!("Actual {} saved to {}", kind, output_file.display());
2316         1
2317     }
2318 }
2319
2320 struct ProcArgs {
2321     prog: String,
2322     args: Vec<String>,
2323 }
2324
2325 pub struct ProcRes {
2326     status: Status,
2327     stdout: String,
2328     stderr: String,
2329     cmdline: String,
2330 }
2331
2332 enum Status {
2333     Parsed(i32),
2334     Normal(ExitStatus),
2335 }
2336
2337 impl ProcRes {
2338     pub fn fatal(&self, err: Option<&str>) -> ! {
2339         if let Some(e) = err {
2340             println!("\nerror: {}", e);
2341         }
2342         print!("\
2343             status: {}\n\
2344             command: {}\n\
2345             stdout:\n\
2346             ------------------------------------------\n\
2347             {}\n\
2348             ------------------------------------------\n\
2349             stderr:\n\
2350             ------------------------------------------\n\
2351             {}\n\
2352             ------------------------------------------\n\
2353             \n",
2354                self.status, self.cmdline, self.stdout,
2355                self.stderr);
2356         panic!();
2357     }
2358 }
2359
2360 impl Status {
2361     fn code(&self) -> Option<i32> {
2362         match *self {
2363             Status::Parsed(i) => Some(i),
2364             Status::Normal(ref e) => e.code(),
2365         }
2366     }
2367
2368     fn success(&self) -> bool {
2369         match *self {
2370             Status::Parsed(i) => i == 0,
2371             Status::Normal(ref e) => e.success(),
2372         }
2373     }
2374 }
2375
2376 impl fmt::Display for Status {
2377     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2378         match *self {
2379             Status::Parsed(i) => write!(f, "exit code: {}", i),
2380             Status::Normal(ref e) => e.fmt(f),
2381         }
2382     }
2383 }
2384
2385 enum TargetLocation {
2386     ThisFile(PathBuf),
2387     ThisDirectory(PathBuf),
2388 }
2389
2390 fn normalize_mir_line(line: &str) -> String {
2391     let no_comments = if let Some(idx) = line.find("//") {
2392         let (l, _) = line.split_at(idx);
2393         l
2394     } else {
2395         line
2396     };
2397     no_comments.replace(char::is_whitespace, "")
2398 }