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