]> git.lizzy.rs Git - rust.git/blob - src/compiletest/runtest.rs
Implement the translation item collector.
[rust.git] / src / compiletest / 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 errors;
15 use header::TestProps;
16 use header;
17 use procsrv;
18 use util::logv;
19
20 use std::env;
21 use std::collections::HashSet;
22 use std::fmt;
23 use std::fs::{self, File};
24 use std::io::BufReader;
25 use std::io::prelude::*;
26 use std::net::TcpStream;
27 use std::path::{Path, PathBuf, Component};
28 use std::process::{Command, Output, ExitStatus};
29
30 pub fn run(config: Config, testfile: &Path) {
31     match &*config.target {
32
33         "arm-linux-androideabi" | "aarch64-linux-android" => {
34             if !config.adb_device_status {
35                 panic!("android device not available");
36             }
37         }
38
39         _=> { }
40     }
41
42     if config.verbose {
43         // We're going to be dumping a lot of info. Start on a new line.
44         print!("\n\n");
45     }
46     debug!("running {:?}", testfile.display());
47     let props = header::load_props(&testfile);
48     debug!("loaded props");
49     match config.mode {
50         CompileFail => run_cfail_test(&config, &props, &testfile),
51         ParseFail => run_cfail_test(&config, &props, &testfile),
52         RunFail => run_rfail_test(&config, &props, &testfile),
53         RunPass => run_rpass_test(&config, &props, &testfile),
54         RunPassValgrind => run_valgrind_test(&config, &props, &testfile),
55         Pretty => run_pretty_test(&config, &props, &testfile),
56         DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
57         DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
58         Codegen => run_codegen_test(&config, &props, &testfile),
59         Rustdoc => run_rustdoc_test(&config, &props, &testfile),
60         CodegenUnits => run_codegen_units_test(&config, &props, &testfile),
61     }
62 }
63
64 fn get_output(props: &TestProps, proc_res: &ProcRes) -> String {
65     if props.check_stdout {
66         format!("{}{}", proc_res.stdout, proc_res.stderr)
67     } else {
68         proc_res.stderr.clone()
69     }
70 }
71
72 fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
73     let proc_res = compile_test(config, props, testfile);
74
75     if proc_res.status.success() {
76         fatal_proc_rec(&format!("{} test compiled successfully!", config.mode)[..],
77                       &proc_res);
78     }
79
80     check_correct_failure_status(&proc_res);
81
82     if proc_res.status.success() {
83         fatal("process did not return an error status");
84     }
85
86     let output_to_check = get_output(props, &proc_res);
87     let expected_errors = errors::load_errors(testfile);
88     if !expected_errors.is_empty() {
89         if !props.error_patterns.is_empty() {
90             fatal("both error pattern and expected errors specified");
91         }
92         check_expected_errors(expected_errors, testfile, &proc_res);
93     } else {
94         check_error_patterns(props, testfile, &output_to_check, &proc_res);
95     }
96     check_no_compiler_crash(&proc_res);
97     check_forbid_output(props, &output_to_check, &proc_res);
98 }
99
100 fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
101     let proc_res = compile_test(config, props, testfile);
102
103     if !proc_res.status.success() {
104         fatal_proc_rec("compilation failed!", &proc_res);
105     }
106
107     let proc_res = exec_compiled_test(config, props, testfile);
108
109     // The value our Makefile configures valgrind to return on failure
110     const VALGRIND_ERR: i32 = 100;
111     if proc_res.status.code() == Some(VALGRIND_ERR) {
112         fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
113     }
114
115     let output_to_check = get_output(props, &proc_res);
116     check_correct_failure_status(&proc_res);
117     check_error_patterns(props, testfile, &output_to_check, &proc_res);
118 }
119
120 fn check_correct_failure_status(proc_res: &ProcRes) {
121     // The value the rust runtime returns on failure
122     const RUST_ERR: i32 = 101;
123     if proc_res.status.code() != Some(RUST_ERR) {
124         fatal_proc_rec(
125             &format!("failure produced the wrong error: {}",
126                      proc_res.status),
127             proc_res);
128     }
129 }
130
131 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
132     let proc_res = compile_test(config, props, testfile);
133
134     if !proc_res.status.success() {
135         fatal_proc_rec("compilation failed!", &proc_res);
136     }
137
138     let proc_res = exec_compiled_test(config, props, testfile);
139
140     if !proc_res.status.success() {
141         fatal_proc_rec("test run failed!", &proc_res);
142     }
143 }
144
145 fn run_valgrind_test(config: &Config, props: &TestProps, testfile: &Path) {
146     if config.valgrind_path.is_none() {
147         assert!(!config.force_valgrind);
148         return run_rpass_test(config, props, testfile);
149     }
150
151     let mut proc_res = compile_test(config, props, testfile);
152
153     if !proc_res.status.success() {
154         fatal_proc_rec("compilation failed!", &proc_res);
155     }
156
157     let mut new_config = config.clone();
158     new_config.runtool = new_config.valgrind_path.clone();
159     proc_res = exec_compiled_test(&new_config, props, testfile);
160
161     if !proc_res.status.success() {
162         fatal_proc_rec("test run failed!", &proc_res);
163     }
164 }
165
166 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
167     if props.pp_exact.is_some() {
168         logv(config, "testing for exact pretty-printing".to_owned());
169     } else {
170         logv(config, "testing for converging pretty-printing".to_owned());
171     }
172
173     let rounds =
174         match props.pp_exact { Some(_) => 1, None => 2 };
175
176     let mut src = String::new();
177     File::open(testfile).unwrap().read_to_string(&mut src).unwrap();
178     let mut srcs = vec!(src);
179
180     let mut round = 0;
181     while round < rounds {
182         logv(config, format!("pretty-printing round {}", round));
183         let proc_res = print_source(config,
184                                     props,
185                                     testfile,
186                                     srcs[round].to_owned(),
187                                     &props.pretty_mode);
188
189         if !proc_res.status.success() {
190             fatal_proc_rec(&format!("pretty-printing failed in round {}", round),
191                           &proc_res);
192         }
193
194         let ProcRes{ stdout, .. } = proc_res;
195         srcs.push(stdout);
196         round += 1;
197     }
198
199     let mut expected = match props.pp_exact {
200         Some(ref file) => {
201             let filepath = testfile.parent().unwrap().join(file);
202             let mut s = String::new();
203             File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
204             s
205         }
206         None => { srcs[srcs.len() - 2].clone() }
207     };
208     let mut actual = srcs[srcs.len() - 1].clone();
209
210     if props.pp_exact.is_some() {
211         // Now we have to care about line endings
212         let cr = "\r".to_owned();
213         actual = actual.replace(&cr, "").to_owned();
214         expected = expected.replace(&cr, "").to_owned();
215     }
216
217     compare_source(&expected, &actual);
218
219     // If we're only making sure that the output matches then just stop here
220     if props.pretty_compare_only { return; }
221
222     // Finally, let's make sure it actually appears to remain valid code
223     let proc_res = typecheck_source(config, props, testfile, actual);
224
225     if !proc_res.status.success() {
226         fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
227     }
228     if !props.pretty_expanded { return }
229
230     // additionally, run `--pretty expanded` and try to build it.
231     let proc_res = print_source(config, props, testfile, srcs[round].clone(), "expanded");
232     if !proc_res.status.success() {
233         fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
234     }
235
236     let ProcRes{ stdout: expanded_src, .. } = proc_res;
237     let proc_res = typecheck_source(config, props, testfile, expanded_src);
238     if !proc_res.status.success() {
239         fatal_proc_rec("pretty-printed source (expanded) does not typecheck",
240                       &proc_res);
241     }
242
243     return;
244
245     fn print_source(config: &Config,
246                     props: &TestProps,
247                     testfile: &Path,
248                     src: String,
249                     pretty_type: &str) -> ProcRes {
250         let aux_dir = aux_output_dir_name(config, testfile);
251         compose_and_run(config,
252                         testfile,
253                         make_pp_args(config,
254                                      props,
255                                      testfile,
256                                      pretty_type.to_owned()),
257                         props.exec_env.clone(),
258                         &config.compile_lib_path,
259                         Some(aux_dir.to_str().unwrap()),
260                         Some(src))
261     }
262
263     fn make_pp_args(config: &Config,
264                     props: &TestProps,
265                     testfile: &Path,
266                     pretty_type: String) -> ProcArgs {
267         let aux_dir = aux_output_dir_name(config, testfile);
268         // FIXME (#9639): This needs to handle non-utf8 paths
269         let mut args = vec!("-".to_owned(),
270                             "-Zunstable-options".to_owned(),
271                             "--unpretty".to_owned(),
272                             pretty_type,
273                             format!("--target={}", config.target),
274                             "-L".to_owned(),
275                             aux_dir.to_str().unwrap().to_owned());
276         args.extend(split_maybe_args(&config.target_rustcflags));
277         args.extend(split_maybe_args(&props.compile_flags));
278         return ProcArgs {
279             prog: config.rustc_path.to_str().unwrap().to_owned(),
280             args: args,
281         };
282     }
283
284     fn compare_source(expected: &str, actual: &str) {
285         if expected != actual {
286             error("pretty-printed source does not match expected source");
287             println!("\n\
288 expected:\n\
289 ------------------------------------------\n\
290 {}\n\
291 ------------------------------------------\n\
292 actual:\n\
293 ------------------------------------------\n\
294 {}\n\
295 ------------------------------------------\n\
296 \n",
297                      expected, actual);
298             panic!();
299         }
300     }
301
302     fn typecheck_source(config: &Config, props: &TestProps,
303                         testfile: &Path, src: String) -> ProcRes {
304         let args = make_typecheck_args(config, props, testfile);
305         compose_and_run_compiler(config, props, testfile, args, Some(src))
306     }
307
308     fn make_typecheck_args(config: &Config, props: &TestProps, testfile: &Path) -> ProcArgs {
309         let aux_dir = aux_output_dir_name(config, testfile);
310         let target = if props.force_host {
311             &*config.host
312         } else {
313             &*config.target
314         };
315         // FIXME (#9639): This needs to handle non-utf8 paths
316         let mut args = vec!("-".to_owned(),
317                             "-Zno-trans".to_owned(),
318                             format!("--target={}", target),
319                             "-L".to_owned(),
320                             config.build_base.to_str().unwrap().to_owned(),
321                             "-L".to_owned(),
322                             aux_dir.to_str().unwrap().to_owned());
323         args.extend(split_maybe_args(&config.target_rustcflags));
324         args.extend(split_maybe_args(&props.compile_flags));
325         // FIXME (#9639): This needs to handle non-utf8 paths
326         return ProcArgs {
327             prog: config.rustc_path.to_str().unwrap().to_owned(),
328             args: args,
329         };
330     }
331 }
332
333 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
334     let mut config = Config {
335         target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
336         host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
337         .. config.clone()
338     };
339
340     let config = &mut config;
341     let DebuggerCommands {
342         commands,
343         check_lines,
344         breakpoint_lines
345     } = parse_debugger_commands(testfile, "gdb");
346     let mut cmds = commands.join("\n");
347
348     // compile test file (it should have 'compile-flags:-g' in the header)
349     let compiler_run_result = compile_test(config, props, testfile);
350     if !compiler_run_result.status.success() {
351         fatal_proc_rec("compilation failed!", &compiler_run_result);
352     }
353
354     let exe_file = make_exe_name(config, testfile);
355
356     let debugger_run_result;
357     match &*config.target {
358         "arm-linux-androideabi" | "aarch64-linux-android" => {
359
360             cmds = cmds.replace("run", "continue");
361
362             // write debugger script
363             let mut script_str = String::with_capacity(2048);
364             script_str.push_str(&format!("set charset {}\n", charset()));
365             script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
366             script_str.push_str("target remote :5039\n");
367             script_str.push_str(&format!("set solib-search-path \
368                                          ./{}/stage2/lib/rustlib/{}/lib/\n",
369                                          config.host, config.target));
370             for line in &breakpoint_lines {
371                 script_str.push_str(&format!("break {:?}:{}\n",
372                                              testfile.file_name().unwrap()
373                                                      .to_string_lossy(),
374                                              *line)[..]);
375             }
376             script_str.push_str(&cmds);
377             script_str.push_str("\nquit\n");
378
379             debug!("script_str = {}", script_str);
380             dump_output_file(config,
381                              testfile,
382                              &script_str,
383                              "debugger.script");
384
385
386             procsrv::run("",
387                          &config.adb_path,
388                          None,
389                          &[
390                             "push".to_owned(),
391                             exe_file.to_str().unwrap().to_owned(),
392                             config.adb_test_dir.clone()
393                          ],
394                          vec!(("".to_owned(), "".to_owned())),
395                          Some("".to_owned()))
396                 .expect(&format!("failed to exec `{:?}`", config.adb_path));
397
398             procsrv::run("",
399                          &config.adb_path,
400                          None,
401                          &[
402                             "forward".to_owned(),
403                             "tcp:5039".to_owned(),
404                             "tcp:5039".to_owned()
405                          ],
406                          vec!(("".to_owned(), "".to_owned())),
407                          Some("".to_owned()))
408                 .expect(&format!("failed to exec `{:?}`", config.adb_path));
409
410             let adb_arg = format!("export LD_LIBRARY_PATH={}; \
411                                    gdbserver{} :5039 {}/{}",
412                                   config.adb_test_dir.clone(),
413                                   if config.target.contains("aarch64")
414                                   {"64"} else {""},
415                                   config.adb_test_dir.clone(),
416                                   exe_file.file_name().unwrap().to_str()
417                                           .unwrap());
418
419             let mut process = procsrv::run_background("",
420                                                       &config.adb_path
421                                                             ,
422                                                       None,
423                                                       &[
424                                                         "shell".to_owned(),
425                                                         adb_arg.clone()
426                                                       ],
427                                                       vec!(("".to_owned(),
428                                                             "".to_owned())),
429                                                       Some("".to_owned()))
430                 .expect(&format!("failed to exec `{:?}`", config.adb_path));
431             loop {
432                 //waiting 1 second for gdbserver start
433                 ::std::thread::sleep(::std::time::Duration::new(1,0));
434                 if TcpStream::connect("127.0.0.1:5039").is_ok() {
435                     break
436                 }
437             }
438
439             let tool_path = match config.android_cross_path.to_str() {
440                 Some(x) => x.to_owned(),
441                 None => fatal("cannot find android cross path")
442             };
443
444             let debugger_script = make_out_name(config, testfile, "debugger.script");
445             // FIXME (#9639): This needs to handle non-utf8 paths
446             let debugger_opts =
447                 vec!("-quiet".to_owned(),
448                      "-batch".to_owned(),
449                      "-nx".to_owned(),
450                      format!("-command={}", debugger_script.to_str().unwrap()));
451
452             let mut gdb_path = tool_path;
453             gdb_path.push_str(&format!("/bin/{}-gdb", config.target));
454             let procsrv::Result {
455                 out,
456                 err,
457                 status
458             } = procsrv::run("",
459                              &gdb_path,
460                              None,
461                              &debugger_opts,
462                              vec!(("".to_owned(), "".to_owned())),
463                              None)
464                 .expect(&format!("failed to exec `{:?}`", gdb_path));
465             let cmdline = {
466                 let cmdline = make_cmdline("",
467                                            &format!("{}-gdb", config.target),
468                                            &debugger_opts);
469                 logv(config, format!("executing {}", cmdline));
470                 cmdline
471             };
472
473             debugger_run_result = ProcRes {
474                 status: Status::Normal(status),
475                 stdout: out,
476                 stderr: err,
477                 cmdline: cmdline
478             };
479             if process.kill().is_err() {
480                 println!("Adb process is already finished.");
481             }
482         }
483
484         _=> {
485             let rust_src_root = find_rust_src_root(config)
486                 .expect("Could not find Rust source root");
487             let rust_pp_module_rel_path = Path::new("./src/etc");
488             let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
489                                                        .to_str()
490                                                        .unwrap()
491                                                        .to_owned();
492             // write debugger script
493             let mut script_str = String::with_capacity(2048);
494             script_str.push_str(&format!("set charset {}\n", charset()));
495             script_str.push_str("show version\n");
496
497             match config.gdb_version {
498                 Some(ref version) => {
499                     println!("NOTE: compiletest thinks it is using GDB version {}",
500                              version);
501
502                     if header::gdb_version_to_int(version) >
503                         header::gdb_version_to_int("7.4") {
504                         // Add the directory containing the pretty printers to
505                         // GDB's script auto loading safe path
506                         script_str.push_str(
507                             &format!("add-auto-load-safe-path {}\n",
508                                      rust_pp_module_abs_path.replace(r"\", r"\\"))
509                                 );
510                     }
511                 }
512                 _ => {
513                     println!("NOTE: compiletest does not know which version of \
514                               GDB it is using");
515                 }
516             }
517
518             // The following line actually doesn't have to do anything with
519             // pretty printing, it just tells GDB to print values on one line:
520             script_str.push_str("set print pretty off\n");
521
522             // Add the pretty printer directory to GDB's source-file search path
523             script_str.push_str(&format!("directory {}\n",
524                                          rust_pp_module_abs_path));
525
526             // Load the target executable
527             script_str.push_str(&format!("file {}\n",
528                                          exe_file.to_str().unwrap()
529                                                  .replace(r"\", r"\\")));
530
531             // Add line breakpoints
532             for line in &breakpoint_lines {
533                 script_str.push_str(&format!("break '{}':{}\n",
534                                              testfile.file_name().unwrap()
535                                                      .to_string_lossy(),
536                                              *line));
537             }
538
539             script_str.push_str(&cmds);
540             script_str.push_str("\nquit\n");
541
542             debug!("script_str = {}", script_str);
543             dump_output_file(config,
544                              testfile,
545                              &script_str,
546                              "debugger.script");
547
548             // run debugger script with gdb
549             fn debugger() -> &'static str {
550                 if cfg!(windows) {"gdb.exe"} else {"gdb"}
551             }
552
553             let debugger_script = make_out_name(config, testfile, "debugger.script");
554
555             // FIXME (#9639): This needs to handle non-utf8 paths
556             let debugger_opts =
557                 vec!("-quiet".to_owned(),
558                      "-batch".to_owned(),
559                      "-nx".to_owned(),
560                      format!("-command={}", debugger_script.to_str().unwrap()));
561
562             let proc_args = ProcArgs {
563                 prog: debugger().to_owned(),
564                 args: debugger_opts,
565             };
566
567             let environment = vec![("PYTHONPATH".to_owned(), rust_pp_module_abs_path)];
568
569             debugger_run_result = compose_and_run(config,
570                                                   testfile,
571                                                   proc_args,
572                                                   environment,
573                                                   &config.run_lib_path,
574                                                   None,
575                                                   None);
576         }
577     }
578
579     if !debugger_run_result.status.success() {
580         fatal("gdb failed to execute");
581     }
582
583     check_debugger_output(&debugger_run_result, &check_lines);
584 }
585
586 fn find_rust_src_root(config: &Config) -> Option<PathBuf> {
587     let mut path = config.src_base.clone();
588     let path_postfix = Path::new("src/etc/lldb_batchmode.py");
589
590     while path.pop() {
591         if path.join(&path_postfix).is_file() {
592             return Some(path);
593         }
594     }
595
596     return None;
597 }
598
599 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
600     if config.lldb_python_dir.is_none() {
601         fatal("Can't run LLDB test because LLDB's python path is not set.");
602     }
603
604     let mut config = Config {
605         target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
606         host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
607         .. config.clone()
608     };
609
610     let config = &mut config;
611
612     // compile test file (it should have 'compile-flags:-g' in the header)
613     let compile_result = compile_test(config, props, testfile);
614     if !compile_result.status.success() {
615         fatal_proc_rec("compilation failed!", &compile_result);
616     }
617
618     let exe_file = make_exe_name(config, testfile);
619
620     match config.lldb_version {
621         Some(ref version) => {
622             println!("NOTE: compiletest thinks it is using LLDB version {}",
623                      version);
624         }
625         _ => {
626             println!("NOTE: compiletest does not know which version of \
627                       LLDB it is using");
628         }
629     }
630
631     // Parse debugger commands etc from test files
632     let DebuggerCommands {
633         commands,
634         check_lines,
635         breakpoint_lines,
636         ..
637     } = parse_debugger_commands(testfile, "lldb");
638
639     // Write debugger script:
640     // We don't want to hang when calling `quit` while the process is still running
641     let mut script_str = String::from("settings set auto-confirm true\n");
642
643     // Make LLDB emit its version, so we have it documented in the test output
644     script_str.push_str("version\n");
645
646     // Switch LLDB into "Rust mode"
647     let rust_src_root = find_rust_src_root(config)
648         .expect("Could not find Rust source root");
649     let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
650     let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
651                                                .to_str()
652                                                .unwrap()
653                                                .to_owned();
654
655     script_str.push_str(&format!("command script import {}\n",
656                                  &rust_pp_module_abs_path[..])[..]);
657     script_str.push_str("type summary add --no-value ");
658     script_str.push_str("--python-function lldb_rust_formatters.print_val ");
659     script_str.push_str("-x \".*\" --category Rust\n");
660     script_str.push_str("type category enable Rust\n");
661
662     // Set breakpoints on every line that contains the string "#break"
663     for line in &breakpoint_lines {
664         script_str.push_str(&format!("breakpoint set --line {}\n", line));
665     }
666
667     // Append the other commands
668     for line in &commands {
669         script_str.push_str(line);
670         script_str.push_str("\n");
671     }
672
673     // Finally, quit the debugger
674     script_str.push_str("\nquit\n");
675
676     // Write the script into a file
677     debug!("script_str = {}", script_str);
678     dump_output_file(config,
679                      testfile,
680                      &script_str,
681                      "debugger.script");
682     let debugger_script = make_out_name(config, testfile, "debugger.script");
683
684     // Let LLDB execute the script via lldb_batchmode.py
685     let debugger_run_result = run_lldb(config,
686                                        &exe_file,
687                                        &debugger_script,
688                                        &rust_src_root);
689
690     if !debugger_run_result.status.success() {
691         fatal_proc_rec("Error while running LLDB", &debugger_run_result);
692     }
693
694     check_debugger_output(&debugger_run_result, &check_lines);
695
696     fn run_lldb(config: &Config,
697                 test_executable: &Path,
698                 debugger_script: &Path,
699                 rust_src_root: &Path)
700                 -> ProcRes {
701         // Prepare the lldb_batchmode which executes the debugger script
702         let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
703         cmd2procres(config,
704                     test_executable,
705                     Command::new(&config.python)
706                             .arg(&lldb_script_path)
707                             .arg(test_executable)
708                             .arg(debugger_script)
709                             .env("PYTHONPATH",
710                                  config.lldb_python_dir.as_ref().unwrap()))
711     }
712 }
713
714 fn cmd2procres(config: &Config, test_executable: &Path, cmd: &mut Command)
715               -> ProcRes {
716     let (status, out, err) = match cmd.output() {
717         Ok(Output { status, stdout, stderr }) => {
718             (status,
719              String::from_utf8(stdout).unwrap(),
720              String::from_utf8(stderr).unwrap())
721         },
722         Err(e) => {
723             fatal(&format!("Failed to setup Python process for \
724                             LLDB script: {}", e))
725         }
726     };
727
728     dump_output(config, test_executable, &out, &err);
729     ProcRes {
730         status: Status::Normal(status),
731         stdout: out,
732         stderr: err,
733         cmdline: format!("{:?}", cmd)
734     }
735 }
736
737 struct DebuggerCommands {
738     commands: Vec<String>,
739     check_lines: Vec<String>,
740     breakpoint_lines: Vec<usize>,
741 }
742
743 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
744                            -> DebuggerCommands {
745     let command_directive = format!("{}-command", debugger_prefix);
746     let check_directive = format!("{}-check", debugger_prefix);
747
748     let mut breakpoint_lines = vec!();
749     let mut commands = vec!();
750     let mut check_lines = vec!();
751     let mut counter = 1;
752     let reader = BufReader::new(File::open(file_path).unwrap());
753     for line in reader.lines() {
754         match line {
755             Ok(line) => {
756                 if line.contains("#break") {
757                     breakpoint_lines.push(counter);
758                 }
759
760                 header::parse_name_value_directive(
761                         &line,
762                         &command_directive).map(|cmd| {
763                     commands.push(cmd)
764                 });
765
766                 header::parse_name_value_directive(
767                         &line,
768                         &check_directive).map(|cmd| {
769                     check_lines.push(cmd)
770                 });
771             }
772             Err(e) => {
773                 fatal(&format!("Error while parsing debugger commands: {}", e))
774             }
775         }
776         counter += 1;
777     }
778
779     DebuggerCommands {
780         commands: commands,
781         check_lines: check_lines,
782         breakpoint_lines: breakpoint_lines,
783     }
784 }
785
786 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
787     if options.is_none() {
788         return None;
789     }
790
791     // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
792     let options_to_remove = [
793         "-O".to_owned(),
794         "-g".to_owned(),
795         "--debuginfo".to_owned()
796     ];
797     let new_options =
798         split_maybe_args(options).into_iter()
799                                  .filter(|x| !options_to_remove.contains(x))
800                                  .collect::<Vec<String>>()
801                                  .join(" ");
802     Some(new_options)
803 }
804
805 fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String]) {
806     let num_check_lines = check_lines.len();
807     if num_check_lines > 0 {
808         // Allow check lines to leave parts unspecified (e.g., uninitialized
809         // bits in the wrong case of an enum) with the notation "[...]".
810         let check_fragments: Vec<Vec<String>> =
811             check_lines.iter().map(|s| {
812                 s
813                  .trim()
814                  .split("[...]")
815                  .map(str::to_owned)
816                  .collect()
817             }).collect();
818         // check if each line in props.check_lines appears in the
819         // output (in order)
820         let mut i = 0;
821         for line in debugger_run_result.stdout.lines() {
822             let mut rest = line.trim();
823             let mut first = true;
824             let mut failed = false;
825             for frag in &check_fragments[i] {
826                 let found = if first {
827                     if rest.starts_with(frag) {
828                         Some(0)
829                     } else {
830                         None
831                     }
832                 } else {
833                     rest.find(frag)
834                 };
835                 match found {
836                     None => {
837                         failed = true;
838                         break;
839                     }
840                     Some(i) => {
841                         rest = &rest[(i + frag.len())..];
842                     }
843                 }
844                 first = false;
845             }
846             if !failed && rest.is_empty() {
847                 i += 1;
848             }
849             if i == num_check_lines {
850                 // all lines checked
851                 break;
852             }
853         }
854         if i != num_check_lines {
855             fatal_proc_rec(&format!("line not found in debugger output: {}",
856                                     check_lines.get(i).unwrap()),
857                           debugger_run_result);
858         }
859     }
860 }
861
862 fn check_error_patterns(props: &TestProps,
863                         testfile: &Path,
864                         output_to_check: &str,
865                         proc_res: &ProcRes) {
866     if props.error_patterns.is_empty() {
867         fatal(&format!("no error pattern specified in {:?}", testfile.display()));
868     }
869     let mut next_err_idx = 0;
870     let mut next_err_pat = &props.error_patterns[next_err_idx];
871     let mut done = false;
872     for line in output_to_check.lines() {
873         if line.contains(next_err_pat) {
874             debug!("found error pattern {}", next_err_pat);
875             next_err_idx += 1;
876             if next_err_idx == props.error_patterns.len() {
877                 debug!("found all error patterns");
878                 done = true;
879                 break;
880             }
881             next_err_pat = &props.error_patterns[next_err_idx];
882         }
883     }
884     if done { return; }
885
886     let missing_patterns = &props.error_patterns[next_err_idx..];
887     if missing_patterns.len() == 1 {
888         fatal_proc_rec(&format!("error pattern '{}' not found!", missing_patterns[0]),
889                       proc_res);
890     } else {
891         for pattern in missing_patterns {
892             error(&format!("error pattern '{}' not found!", *pattern));
893         }
894         fatal_proc_rec("multiple error patterns not found", proc_res);
895     }
896 }
897
898 fn check_no_compiler_crash(proc_res: &ProcRes) {
899     for line in proc_res.stderr.lines() {
900         if line.starts_with("error: internal compiler error:") {
901             fatal_proc_rec("compiler encountered internal error",
902                           proc_res);
903         }
904     }
905 }
906
907 fn check_forbid_output(props: &TestProps,
908                        output_to_check: &str,
909                        proc_res: &ProcRes) {
910     for pat in &props.forbid_output {
911         if output_to_check.contains(pat) {
912             fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
913         }
914     }
915 }
916
917 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError>,
918                          testfile: &Path,
919                          proc_res: &ProcRes) {
920
921     // true if we found the error in question
922     let mut found_flags = vec![false; expected_errors.len()];
923
924     if proc_res.status.success() {
925         fatal("process did not return an error status");
926     }
927
928     let prefixes = expected_errors.iter().map(|ee| {
929         format!("{}:{}:", testfile.display(), ee.line)
930     }).collect::<Vec<String>>();
931
932     fn prefix_matches(line: &str, prefix: &str) -> bool {
933         use std::ascii::AsciiExt;
934         // On windows just translate all '\' path separators to '/'
935         let line = line.replace(r"\", "/");
936         if cfg!(windows) {
937             line.to_ascii_lowercase().starts_with(&prefix.to_ascii_lowercase())
938         } else {
939             line.starts_with(prefix)
940         }
941     }
942
943     // A multi-line error will have followup lines which start with a space
944     // or open paren.
945     fn continuation( line: &str) -> bool {
946         line.starts_with(" ") || line.starts_with("(")
947     }
948
949     // Scan and extract our error/warning messages,
950     // which look like:
951     //    filename:line1:col1: line2:col2: *error:* msg
952     //    filename:line1:col1: line2:col2: *warning:* msg
953     // where line1:col1: is the starting point, line2:col2:
954     // is the ending point, and * represents ANSI color codes.
955     //
956     // This pattern is ambiguous on windows, because filename may contain
957     // a colon, so any path prefix must be detected and removed first.
958     for line in proc_res.stderr.lines() {
959         let mut was_expected = false;
960         let mut prev = 0;
961         for (i, ee) in expected_errors.iter().enumerate() {
962             if !found_flags[i] {
963                 debug!("prefix={} ee.kind={} ee.msg={} line={}",
964                        prefixes[i],
965                        ee.kind,
966                        ee.msg,
967                        line);
968                 // Suggestions have no line number in their output, so take on the line number of
969                 // the previous expected error
970                 if ee.kind == "suggestion" {
971                     assert!(expected_errors[prev].kind == "help",
972                             "SUGGESTIONs must be preceded by a HELP");
973                     if line.contains(&ee.msg) {
974                         found_flags[i] = true;
975                         was_expected = true;
976                         break;
977                     }
978                 }
979                 if (prefix_matches(line, &prefixes[i]) || continuation(line)) &&
980                     line.contains(&ee.kind) &&
981                     line.contains(&ee.msg) {
982                     found_flags[i] = true;
983                     was_expected = true;
984                     break;
985                 }
986             }
987             prev = i;
988         }
989
990         // ignore this msg which gets printed at the end
991         if line.contains("aborting due to") {
992             was_expected = true;
993         }
994
995         if !was_expected && is_compiler_error_or_warning(line) {
996             fatal_proc_rec(&format!("unexpected compiler error or warning: '{}'",
997                                     line),
998                           proc_res);
999         }
1000     }
1001
1002     for (i, &flag) in found_flags.iter().enumerate() {
1003         if !flag {
1004             let ee = &expected_errors[i];
1005             fatal_proc_rec(&format!("expected {} on line {} not found: {}",
1006                                     ee.kind, ee.line, ee.msg),
1007                           proc_res);
1008         }
1009     }
1010 }
1011
1012 fn is_compiler_error_or_warning(line: &str) -> bool {
1013     let mut c = Path::new(line).components();
1014     let line = match c.next() {
1015         Some(Component::Prefix(_)) => c.as_path().to_str().unwrap(),
1016         _ => line,
1017     };
1018
1019     let mut i = 0;
1020     return
1021         scan_until_char(line, ':', &mut i) &&
1022         scan_char(line, ':', &mut i) &&
1023         scan_integer(line, &mut i) &&
1024         scan_char(line, ':', &mut i) &&
1025         scan_integer(line, &mut i) &&
1026         scan_char(line, ':', &mut i) &&
1027         scan_char(line, ' ', &mut i) &&
1028         scan_integer(line, &mut i) &&
1029         scan_char(line, ':', &mut i) &&
1030         scan_integer(line, &mut i) &&
1031         scan_char(line, ' ', &mut i) &&
1032         (scan_string(line, "error", &mut i) ||
1033          scan_string(line, "warning", &mut i));
1034 }
1035
1036 fn scan_until_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1037     if *idx >= haystack.len() {
1038         return false;
1039     }
1040     let opt = haystack[(*idx)..].find(needle);
1041     if opt.is_none() {
1042         return false;
1043     }
1044     *idx = opt.unwrap();
1045     return true;
1046 }
1047
1048 fn scan_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1049     if *idx >= haystack.len() {
1050         return false;
1051     }
1052     let ch = haystack.char_at(*idx);
1053     if ch != needle {
1054         return false;
1055     }
1056     *idx += ch.len_utf8();
1057     return true;
1058 }
1059
1060 fn scan_integer(haystack: &str, idx: &mut usize) -> bool {
1061     let mut i = *idx;
1062     while i < haystack.len() {
1063         let ch = haystack.char_at(i);
1064         if ch < '0' || '9' < ch {
1065             break;
1066         }
1067         i += ch.len_utf8();
1068     }
1069     if i == *idx {
1070         return false;
1071     }
1072     *idx = i;
1073     return true;
1074 }
1075
1076 fn scan_string(haystack: &str, needle: &str, idx: &mut usize) -> bool {
1077     let mut haystack_i = *idx;
1078     let mut needle_i = 0;
1079     while needle_i < needle.len() {
1080         if haystack_i >= haystack.len() {
1081             return false;
1082         }
1083         let ch = haystack.char_at(haystack_i);
1084         haystack_i += ch.len_utf8();
1085         if !scan_char(needle, ch, &mut needle_i) {
1086             return false;
1087         }
1088     }
1089     *idx = haystack_i;
1090     return true;
1091 }
1092
1093 struct ProcArgs {
1094     prog: String,
1095     args: Vec<String>,
1096 }
1097
1098 struct ProcRes {
1099     status: Status,
1100     stdout: String,
1101     stderr: String,
1102     cmdline: String,
1103 }
1104
1105 enum Status {
1106     Parsed(i32),
1107     Normal(ExitStatus),
1108 }
1109
1110 impl Status {
1111     fn code(&self) -> Option<i32> {
1112         match *self {
1113             Status::Parsed(i) => Some(i),
1114             Status::Normal(ref e) => e.code(),
1115         }
1116     }
1117
1118     fn success(&self) -> bool {
1119         match *self {
1120             Status::Parsed(i) => i == 0,
1121             Status::Normal(ref e) => e.success(),
1122         }
1123     }
1124 }
1125
1126 impl fmt::Display for Status {
1127     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1128         match *self {
1129             Status::Parsed(i) => write!(f, "exit code: {}", i),
1130             Status::Normal(ref e) => e.fmt(f),
1131         }
1132     }
1133 }
1134
1135 fn compile_test(config: &Config, props: &TestProps,
1136                 testfile: &Path) -> ProcRes {
1137     let aux_dir = aux_output_dir_name(config, testfile);
1138     // FIXME (#9639): This needs to handle non-utf8 paths
1139     let link_args = vec!("-L".to_owned(),
1140                          aux_dir.to_str().unwrap().to_owned());
1141     let args = make_compile_args(config,
1142                                  props,
1143                                  link_args,
1144                                  |a, b| TargetLocation::ThisFile(make_exe_name(a, b)), testfile);
1145     compose_and_run_compiler(config, props, testfile, args, None)
1146 }
1147
1148 fn document(config: &Config, props: &TestProps,
1149             testfile: &Path, out_dir: &Path) -> ProcRes {
1150     if props.build_aux_docs {
1151         for rel_ab in &props.aux_builds {
1152             let abs_ab = config.aux_base.join(rel_ab);
1153             let aux_props = header::load_props(&abs_ab);
1154
1155             let auxres = document(config, &aux_props, &abs_ab, out_dir);
1156             if !auxres.status.success() {
1157                 return auxres;
1158             }
1159         }
1160     }
1161
1162     let aux_dir = aux_output_dir_name(config, testfile);
1163     let mut args = vec!["-L".to_owned(),
1164                         aux_dir.to_str().unwrap().to_owned(),
1165                         "-o".to_owned(),
1166                         out_dir.to_str().unwrap().to_owned(),
1167                         testfile.to_str().unwrap().to_owned()];
1168     args.extend(split_maybe_args(&props.compile_flags));
1169     let args = ProcArgs {
1170         prog: config.rustdoc_path.to_str().unwrap().to_owned(),
1171         args: args,
1172     };
1173     compose_and_run_compiler(config, props, testfile, args, None)
1174 }
1175
1176 fn exec_compiled_test(config: &Config, props: &TestProps,
1177                       testfile: &Path) -> ProcRes {
1178
1179     let env = props.exec_env.clone();
1180
1181     match &*config.target {
1182
1183         "arm-linux-androideabi" | "aarch64-linux-android" => {
1184             _arm_exec_compiled_test(config, props, testfile, env)
1185         }
1186
1187         _=> {
1188             let aux_dir = aux_output_dir_name(config, testfile);
1189             compose_and_run(config,
1190                             testfile,
1191                             make_run_args(config, props, testfile),
1192                             env,
1193                             &config.run_lib_path,
1194                             Some(aux_dir.to_str().unwrap()),
1195                             None)
1196         }
1197     }
1198 }
1199
1200 fn compose_and_run_compiler(config: &Config, props: &TestProps,
1201                             testfile: &Path, args: ProcArgs,
1202                             input: Option<String>) -> ProcRes {
1203     if !props.aux_builds.is_empty() {
1204         ensure_dir(&aux_output_dir_name(config, testfile));
1205     }
1206
1207     let aux_dir = aux_output_dir_name(config, testfile);
1208     // FIXME (#9639): This needs to handle non-utf8 paths
1209     let extra_link_args = vec!["-L".to_owned(),
1210                                aux_dir.to_str().unwrap().to_owned()];
1211
1212     for rel_ab in &props.aux_builds {
1213         let abs_ab = config.aux_base.join(rel_ab);
1214         let aux_props = header::load_props(&abs_ab);
1215         let mut crate_type = if aux_props.no_prefer_dynamic {
1216             Vec::new()
1217         } else {
1218             // We primarily compile all auxiliary libraries as dynamic libraries
1219             // to avoid code size bloat and large binaries as much as possible
1220             // for the test suite (otherwise including libstd statically in all
1221             // executables takes up quite a bit of space).
1222             //
1223             // For targets like MUSL, however, there is no support for dynamic
1224             // libraries so we just go back to building a normal library. Note,
1225             // however, that if the library is built with `force_host` then it's
1226             // ok to be a dylib as the host should always support dylibs.
1227             if config.target.contains("musl") && !aux_props.force_host {
1228                 vec!("--crate-type=lib".to_owned())
1229             } else {
1230                 vec!("--crate-type=dylib".to_owned())
1231             }
1232         };
1233         crate_type.extend(extra_link_args.clone());
1234         let aux_args =
1235             make_compile_args(config,
1236                               &aux_props,
1237                               crate_type,
1238                               |a,b| {
1239                                   let f = make_lib_name(a, b, testfile);
1240                                   let parent = f.parent().unwrap();
1241                                   TargetLocation::ThisDirectory(parent.to_path_buf())
1242                               },
1243                               &abs_ab);
1244         let auxres = compose_and_run(config,
1245                                      &abs_ab,
1246                                      aux_args,
1247                                      Vec::new(),
1248                                      &config.compile_lib_path,
1249                                      Some(aux_dir.to_str().unwrap()),
1250                                      None);
1251         if !auxres.status.success() {
1252             fatal_proc_rec(
1253                 &format!("auxiliary build of {:?} failed to compile: ",
1254                         abs_ab.display()),
1255                 &auxres);
1256         }
1257
1258         match &*config.target {
1259             "arm-linux-androideabi"  | "aarch64-linux-android" => {
1260                 _arm_push_aux_shared_library(config, testfile);
1261             }
1262             _ => {}
1263         }
1264     }
1265
1266     compose_and_run(config,
1267                     testfile,
1268                     args,
1269                     Vec::new(),
1270                     &config.compile_lib_path,
1271                     Some(aux_dir.to_str().unwrap()),
1272                     input)
1273 }
1274
1275 fn ensure_dir(path: &Path) {
1276     if path.is_dir() { return; }
1277     fs::create_dir(path).unwrap();
1278 }
1279
1280 fn compose_and_run(config: &Config, testfile: &Path,
1281                    ProcArgs{ args, prog }: ProcArgs,
1282                    procenv: Vec<(String, String)> ,
1283                    lib_path: &str,
1284                    aux_path: Option<&str>,
1285                    input: Option<String>) -> ProcRes {
1286     return program_output(config, testfile, lib_path,
1287                           prog, aux_path, args, procenv, input);
1288 }
1289
1290 enum TargetLocation {
1291     ThisFile(PathBuf),
1292     ThisDirectory(PathBuf),
1293 }
1294
1295 fn make_compile_args<F>(config: &Config,
1296                         props: &TestProps,
1297                         extras: Vec<String> ,
1298                         xform: F,
1299                         testfile: &Path)
1300                         -> ProcArgs where
1301     F: FnOnce(&Config, &Path) -> TargetLocation,
1302 {
1303     let xform_file = xform(config, testfile);
1304     let target = if props.force_host {
1305         &*config.host
1306     } else {
1307         &*config.target
1308     };
1309     // FIXME (#9639): This needs to handle non-utf8 paths
1310     let mut args = vec!(testfile.to_str().unwrap().to_owned(),
1311                         "-L".to_owned(),
1312                         config.build_base.to_str().unwrap().to_owned(),
1313                         format!("--target={}", target));
1314     args.extend_from_slice(&extras);
1315     if !props.no_prefer_dynamic {
1316         args.push("-C".to_owned());
1317         args.push("prefer-dynamic".to_owned());
1318     }
1319     let path = match xform_file {
1320         TargetLocation::ThisFile(path) => {
1321             args.push("-o".to_owned());
1322             path
1323         }
1324         TargetLocation::ThisDirectory(path) => {
1325             args.push("--out-dir".to_owned());
1326             path
1327         }
1328     };
1329     args.push(path.to_str().unwrap().to_owned());
1330     if props.force_host {
1331         args.extend(split_maybe_args(&config.host_rustcflags));
1332     } else {
1333         args.extend(split_maybe_args(&config.target_rustcflags));
1334     }
1335     args.extend(split_maybe_args(&props.compile_flags));
1336     return ProcArgs {
1337         prog: config.rustc_path.to_str().unwrap().to_owned(),
1338         args: args,
1339     };
1340 }
1341
1342 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> PathBuf {
1343     // what we return here is not particularly important, as it
1344     // happens; rustc ignores everything except for the directory.
1345     let auxname = output_testname(auxfile);
1346     aux_output_dir_name(config, testfile).join(&auxname)
1347 }
1348
1349 fn make_exe_name(config: &Config, testfile: &Path) -> PathBuf {
1350     let mut f = output_base_name(config, testfile);
1351     if !env::consts::EXE_SUFFIX.is_empty() {
1352         let mut fname = f.file_name().unwrap().to_os_string();
1353         fname.push(env::consts::EXE_SUFFIX);
1354         f.set_file_name(&fname);
1355     }
1356     f
1357 }
1358
1359 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path)
1360                  -> ProcArgs {
1361     // If we've got another tool to run under (valgrind),
1362     // then split apart its command
1363     let mut args = split_maybe_args(&config.runtool);
1364     let exe_file = make_exe_name(config, testfile);
1365
1366     // FIXME (#9639): This needs to handle non-utf8 paths
1367     args.push(exe_file.to_str().unwrap().to_owned());
1368
1369     // Add the arguments in the run_flags directive
1370     args.extend(split_maybe_args(&props.run_flags));
1371
1372     let prog = args.remove(0);
1373     return ProcArgs {
1374         prog: prog,
1375         args: args,
1376     };
1377 }
1378
1379 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1380     match *argstr {
1381         Some(ref s) => {
1382             s
1383              .split(' ')
1384              .filter_map(|s| {
1385                  if s.chars().all(|c| c.is_whitespace()) {
1386                      None
1387                  } else {
1388                      Some(s.to_owned())
1389                  }
1390              }).collect()
1391         }
1392         None => Vec::new()
1393     }
1394 }
1395
1396 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1397                   aux_path: Option<&str>, args: Vec<String>,
1398                   env: Vec<(String, String)>,
1399                   input: Option<String>) -> ProcRes {
1400     let cmdline =
1401         {
1402             let cmdline = make_cmdline(lib_path,
1403                                        &prog,
1404                                        &args);
1405             logv(config, format!("executing {}", cmdline));
1406             cmdline
1407         };
1408     let procsrv::Result {
1409         out,
1410         err,
1411         status
1412     } = procsrv::run(lib_path,
1413                      &prog,
1414                      aux_path,
1415                      &args,
1416                      env,
1417                      input).expect(&format!("failed to exec `{}`", prog));
1418     dump_output(config, testfile, &out, &err);
1419     return ProcRes {
1420         status: Status::Normal(status),
1421         stdout: out,
1422         stderr: err,
1423         cmdline: cmdline,
1424     };
1425 }
1426
1427 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1428     use util;
1429
1430     // Linux and mac don't require adjusting the library search path
1431     if cfg!(unix) {
1432         format!("{} {}", prog, args.join(" "))
1433     } else {
1434         // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1435         // for diagnostic purposes
1436         fn lib_path_cmd_prefix(path: &str) -> String {
1437             format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1438         }
1439
1440         format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1441     }
1442 }
1443
1444 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1445     dump_output_file(config, testfile, out, "out");
1446     dump_output_file(config, testfile, err, "err");
1447     maybe_dump_to_stdout(config, out, err);
1448 }
1449
1450 fn dump_output_file(config: &Config, testfile: &Path,
1451                     out: &str, extension: &str) {
1452     let outfile = make_out_name(config, testfile, extension);
1453     File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1454 }
1455
1456 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> PathBuf {
1457     output_base_name(config, testfile).with_extension(extension)
1458 }
1459
1460 fn aux_output_dir_name(config: &Config, testfile: &Path) -> PathBuf {
1461     let f = output_base_name(config, testfile);
1462     let mut fname = f.file_name().unwrap().to_os_string();
1463     fname.push(&format!(".{}.libaux", config.mode));
1464     f.with_file_name(&fname)
1465 }
1466
1467 fn output_testname(testfile: &Path) -> PathBuf {
1468     PathBuf::from(testfile.file_stem().unwrap())
1469 }
1470
1471 fn output_base_name(config: &Config, testfile: &Path) -> PathBuf {
1472     config.build_base
1473         .join(&output_testname(testfile))
1474         .with_extension(&config.stage_id)
1475 }
1476
1477 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1478     if config.verbose {
1479         println!("------{}------------------------------", "stdout");
1480         println!("{}", out);
1481         println!("------{}------------------------------", "stderr");
1482         println!("{}", err);
1483         println!("------------------------------------------");
1484     }
1485 }
1486
1487 fn error(err: &str) { println!("\nerror: {}", err); }
1488
1489 fn fatal(err: &str) -> ! { error(err); panic!(); }
1490
1491 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1492     print!("\n\
1493 error: {}\n\
1494 status: {}\n\
1495 command: {}\n\
1496 stdout:\n\
1497 ------------------------------------------\n\
1498 {}\n\
1499 ------------------------------------------\n\
1500 stderr:\n\
1501 ------------------------------------------\n\
1502 {}\n\
1503 ------------------------------------------\n\
1504 \n",
1505              err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1506              proc_res.stderr);
1507     panic!();
1508 }
1509
1510 fn _arm_exec_compiled_test(config: &Config,
1511                            props: &TestProps,
1512                            testfile: &Path,
1513                            env: Vec<(String, String)>)
1514                            -> ProcRes {
1515     let args = make_run_args(config, props, testfile);
1516     let cmdline = make_cmdline("",
1517                                &args.prog,
1518                                &args.args);
1519
1520     // get bare program string
1521     let mut tvec: Vec<String> = args.prog
1522                                     .split('/')
1523                                     .map(str::to_owned)
1524                                     .collect();
1525     let prog_short = tvec.pop().unwrap();
1526
1527     // copy to target
1528     let copy_result = procsrv::run("",
1529                                    &config.adb_path,
1530                                    None,
1531                                    &[
1532                                     "push".to_owned(),
1533                                     args.prog.clone(),
1534                                     config.adb_test_dir.clone()
1535                                    ],
1536                                    vec!(("".to_owned(), "".to_owned())),
1537                                    Some("".to_owned()))
1538         .expect(&format!("failed to exec `{}`", config.adb_path));
1539
1540     if config.verbose {
1541         println!("push ({}) {} {} {}",
1542                  config.target,
1543                  args.prog,
1544                  copy_result.out,
1545                  copy_result.err);
1546     }
1547
1548     logv(config, format!("executing ({}) {}", config.target, cmdline));
1549
1550     let mut runargs = Vec::new();
1551
1552     // run test via adb_run_wrapper
1553     runargs.push("shell".to_owned());
1554     for (key, val) in env {
1555         runargs.push(format!("{}={}", key, val));
1556     }
1557     runargs.push(format!("{}/../adb_run_wrapper.sh", config.adb_test_dir));
1558     runargs.push(format!("{}", config.adb_test_dir));
1559     runargs.push(format!("{}", prog_short));
1560
1561     for tv in &args.args {
1562         runargs.push(tv.to_owned());
1563     }
1564     procsrv::run("",
1565                  &config.adb_path,
1566                  None,
1567                  &runargs,
1568                  vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1569         .expect(&format!("failed to exec `{}`", config.adb_path));
1570
1571     // get exitcode of result
1572     runargs = Vec::new();
1573     runargs.push("shell".to_owned());
1574     runargs.push("cat".to_owned());
1575     runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1576
1577     let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1578         procsrv::run("",
1579                      &config.adb_path,
1580                      None,
1581                      &runargs,
1582                      vec!(("".to_owned(), "".to_owned())),
1583                      Some("".to_owned()))
1584         .expect(&format!("failed to exec `{}`", config.adb_path));
1585
1586     let mut exitcode: i32 = 0;
1587     for c in exitcode_out.chars() {
1588         if !c.is_numeric() { break; }
1589         exitcode = exitcode * 10 + match c {
1590             '0' ... '9' => c as i32 - ('0' as i32),
1591             _ => 101,
1592         }
1593     }
1594
1595     // get stdout of result
1596     runargs = Vec::new();
1597     runargs.push("shell".to_owned());
1598     runargs.push("cat".to_owned());
1599     runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1600
1601     let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1602         procsrv::run("",
1603                      &config.adb_path,
1604                      None,
1605                      &runargs,
1606                      vec!(("".to_owned(), "".to_owned())),
1607                      Some("".to_owned()))
1608         .expect(&format!("failed to exec `{}`", config.adb_path));
1609
1610     // get stderr of result
1611     runargs = Vec::new();
1612     runargs.push("shell".to_owned());
1613     runargs.push("cat".to_owned());
1614     runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1615
1616     let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1617         procsrv::run("",
1618                      &config.adb_path,
1619                      None,
1620                      &runargs,
1621                      vec!(("".to_owned(), "".to_owned())),
1622                      Some("".to_owned()))
1623         .expect(&format!("failed to exec `{}`", config.adb_path));
1624
1625     dump_output(config,
1626                 testfile,
1627                 &stdout_out,
1628                 &stderr_out);
1629
1630     ProcRes {
1631         status: Status::Parsed(exitcode),
1632         stdout: stdout_out,
1633         stderr: stderr_out,
1634         cmdline: cmdline
1635     }
1636 }
1637
1638 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1639     let tdir = aux_output_dir_name(config, testfile);
1640
1641     let dirs = fs::read_dir(&tdir).unwrap();
1642     for file in dirs {
1643         let file = file.unwrap().path();
1644         if file.extension().and_then(|s| s.to_str()) == Some("so") {
1645             // FIXME (#9639): This needs to handle non-utf8 paths
1646             let copy_result = procsrv::run("",
1647                                            &config.adb_path,
1648                                            None,
1649                                            &[
1650                                             "push".to_owned(),
1651                                             file.to_str()
1652                                                 .unwrap()
1653                                                 .to_owned(),
1654                                             config.adb_test_dir.to_owned(),
1655                                            ],
1656                                            vec!(("".to_owned(),
1657                                                  "".to_owned())),
1658                                            Some("".to_owned()))
1659                 .expect(&format!("failed to exec `{}`", config.adb_path));
1660
1661             if config.verbose {
1662                 println!("push ({}) {:?} {} {}",
1663                     config.target, file.display(),
1664                     copy_result.out, copy_result.err);
1665             }
1666         }
1667     }
1668 }
1669
1670 // codegen tests (using FileCheck)
1671
1672 fn compile_test_and_save_ir(config: &Config, props: &TestProps,
1673                                  testfile: &Path) -> ProcRes {
1674     let aux_dir = aux_output_dir_name(config, testfile);
1675     // FIXME (#9639): This needs to handle non-utf8 paths
1676     let mut link_args = vec!("-L".to_owned(),
1677                              aux_dir.to_str().unwrap().to_owned());
1678     let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1679     link_args.extend(llvm_args);
1680     let args = make_compile_args(config,
1681                                  props,
1682                                  link_args,
1683                                  |a, b| TargetLocation::ThisDirectory(
1684                                      output_base_name(a, b).parent()
1685                                         .unwrap().to_path_buf()),
1686                                  testfile);
1687     compose_and_run_compiler(config, props, testfile, args, None)
1688 }
1689
1690 fn check_ir_with_filecheck(config: &Config, testfile: &Path) -> ProcRes {
1691     let irfile = output_base_name(config, testfile).with_extension("ll");
1692     let prog = config.llvm_bin_path.as_ref().unwrap().join("FileCheck");
1693     let proc_args = ProcArgs {
1694         // FIXME (#9639): This needs to handle non-utf8 paths
1695         prog: prog.to_str().unwrap().to_owned(),
1696         args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1697                    testfile.to_str().unwrap().to_owned())
1698     };
1699     compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1700 }
1701
1702 fn run_codegen_test(config: &Config, props: &TestProps, testfile: &Path) {
1703
1704     if config.llvm_bin_path.is_none() {
1705         fatal("missing --llvm-bin-path");
1706     }
1707
1708     let mut proc_res = compile_test_and_save_ir(config, props, testfile);
1709     if !proc_res.status.success() {
1710         fatal_proc_rec("compilation failed!", &proc_res);
1711     }
1712
1713     proc_res = check_ir_with_filecheck(config, testfile);
1714     if !proc_res.status.success() {
1715         fatal_proc_rec("verification with 'FileCheck' failed",
1716                       &proc_res);
1717     }
1718 }
1719
1720 fn charset() -> &'static str {
1721     // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1722     if cfg!(target_os = "bitrig") {
1723         "auto"
1724     } else if cfg!(target_os = "freebsd") {
1725         "ISO-8859-1"
1726     } else {
1727         "UTF-8"
1728     }
1729 }
1730
1731 fn run_rustdoc_test(config: &Config, props: &TestProps, testfile: &Path) {
1732     let out_dir = output_base_name(config, testfile);
1733     let _ = fs::remove_dir_all(&out_dir);
1734     ensure_dir(&out_dir);
1735
1736     let proc_res = document(config, props, testfile, &out_dir);
1737     if !proc_res.status.success() {
1738         fatal_proc_rec("rustdoc failed!", &proc_res);
1739     }
1740     let root = find_rust_src_root(config).unwrap();
1741
1742     let res = cmd2procres(config,
1743                           testfile,
1744                           Command::new(&config.python)
1745                                   .arg(root.join("src/etc/htmldocck.py"))
1746                                   .arg(out_dir)
1747                                   .arg(testfile));
1748     if !res.status.success() {
1749         fatal_proc_rec("htmldocck failed!", &res);
1750     }
1751 }
1752
1753 fn run_codegen_units_test(config: &Config, props: &TestProps, testfile: &Path) {
1754     let proc_res = compile_test(config, props, testfile);
1755
1756     if !proc_res.status.success() {
1757         fatal_proc_rec("compilation failed!", &proc_res);
1758     }
1759
1760     check_no_compiler_crash(&proc_res);
1761
1762     let prefix = "TRANS_ITEM ";
1763
1764     let actual: HashSet<String> = proc_res
1765         .stdout
1766         .lines()
1767         .filter(|line| line.starts_with(prefix))
1768         .map(|s| (&s[prefix.len()..]).to_string())
1769         .collect();
1770
1771     let expected: HashSet<String> = errors::load_errors(testfile)
1772         .iter()
1773         .map(|e| e.msg.trim().to_string())
1774         .collect();
1775
1776     if actual != expected {
1777         let mut missing: Vec<_> = expected.difference(&actual).collect();
1778         missing.sort();
1779
1780         let mut too_much: Vec<_> = actual.difference(&expected).collect();
1781         too_much.sort();
1782
1783         println!("Expected and actual sets of codegen-items differ.\n\
1784                   These items should have been contained but were not:\n\n\
1785                   {}\n\n\
1786                   These items were contained but should not have been:\n\n\
1787                   {}\n\n",
1788             missing.iter().fold("".to_string(), |s1, s2| s1 + "\n" + s2),
1789             too_much.iter().fold("".to_string(), |s1, s2| s1 + "\n" + s2));
1790         panic!();
1791     }
1792 }