]> git.lizzy.rs Git - rust.git/blob - src/compiletest/runtest.rs
Auto merge of #30189 - crumblingstatue:patch-1, r=alexcrichton
[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 self::TargetLocation::*;
12
13 use common::Config;
14 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
15 use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc};
16 use errors;
17 use header::TestProps;
18 use header;
19 use procsrv;
20 use util::logv;
21
22 use std::env;
23 use std::fmt;
24 use std::fs::{self, File};
25 use std::io::BufReader;
26 use std::io::prelude::*;
27 use std::net::TcpStream;
28 use std::path::{Path, PathBuf, Component};
29 use std::process::{Command, Output, ExitStatus};
30
31 pub fn run(config: Config, testfile: &Path) {
32     match &*config.target {
33
34         "arm-linux-androideabi" | "aarch64-linux-android" => {
35             if !config.adb_device_status {
36                 panic!("android device not available");
37             }
38         }
39
40         _=> { }
41     }
42
43     if config.verbose {
44         // We're going to be dumping a lot of info. Start on a new line.
45         print!("\n\n");
46     }
47     debug!("running {:?}", testfile.display());
48     let props = header::load_props(&testfile);
49     debug!("loaded props");
50     match config.mode {
51         CompileFail => run_cfail_test(&config, &props, &testfile),
52         ParseFail => run_cfail_test(&config, &props, &testfile),
53         RunFail => run_rfail_test(&config, &props, &testfile),
54         RunPass => run_rpass_test(&config, &props, &testfile),
55         RunPassValgrind => run_valgrind_test(&config, &props, &testfile),
56         Pretty => run_pretty_test(&config, &props, &testfile),
57         DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
58         DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
59         Codegen => run_codegen_test(&config, &props, &testfile),
60         Rustdoc => run_rustdoc_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(mut line: &str) -> bool {
1013     // Remove initial prefix which may contain a colon
1014     let mut components = Path::new(line).components();
1015     if let Some(Component::Prefix(_)) = components.peek() {
1016         components.next();
1017     }
1018
1019     // Safe as path was originally constructed from a &str ^
1020     line = components.as_path().to_str().unwrap();
1021
1022     let mut i = 0;
1023     return
1024         scan_until_char(line, ':', &mut i) &&
1025         scan_char(line, ':', &mut i) &&
1026         scan_integer(line, &mut i) &&
1027         scan_char(line, ':', &mut i) &&
1028         scan_integer(line, &mut i) &&
1029         scan_char(line, ':', &mut i) &&
1030         scan_char(line, ' ', &mut i) &&
1031         scan_integer(line, &mut i) &&
1032         scan_char(line, ':', &mut i) &&
1033         scan_integer(line, &mut i) &&
1034         scan_char(line, ' ', &mut i) &&
1035         (scan_string(line, "error", &mut i) ||
1036          scan_string(line, "warning", &mut i));
1037 }
1038
1039 fn scan_until_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1040     if *idx >= haystack.len() {
1041         return false;
1042     }
1043     let opt = haystack[(*idx)..].find(needle);
1044     if opt.is_none() {
1045         return false;
1046     }
1047     *idx = opt.unwrap();
1048     return true;
1049 }
1050
1051 fn scan_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1052     if *idx >= haystack.len() {
1053         return false;
1054     }
1055     let ch = haystack.char_at(*idx);
1056     if ch != needle {
1057         return false;
1058     }
1059     *idx += ch.len_utf8();
1060     return true;
1061 }
1062
1063 fn scan_integer(haystack: &str, idx: &mut usize) -> bool {
1064     let mut i = *idx;
1065     while i < haystack.len() {
1066         let ch = haystack.char_at(i);
1067         if ch < '0' || '9' < ch {
1068             break;
1069         }
1070         i += ch.len_utf8();
1071     }
1072     if i == *idx {
1073         return false;
1074     }
1075     *idx = i;
1076     return true;
1077 }
1078
1079 fn scan_string(haystack: &str, needle: &str, idx: &mut usize) -> bool {
1080     let mut haystack_i = *idx;
1081     let mut needle_i = 0;
1082     while needle_i < needle.len() {
1083         if haystack_i >= haystack.len() {
1084             return false;
1085         }
1086         let ch = haystack.char_at(haystack_i);
1087         haystack_i += ch.len_utf8();
1088         if !scan_char(needle, ch, &mut needle_i) {
1089             return false;
1090         }
1091     }
1092     *idx = haystack_i;
1093     return true;
1094 }
1095
1096 struct ProcArgs {
1097     prog: String,
1098     args: Vec<String>,
1099 }
1100
1101 struct ProcRes {
1102     status: Status,
1103     stdout: String,
1104     stderr: String,
1105     cmdline: String,
1106 }
1107
1108 enum Status {
1109     Parsed(i32),
1110     Normal(ExitStatus),
1111 }
1112
1113 impl Status {
1114     fn code(&self) -> Option<i32> {
1115         match *self {
1116             Status::Parsed(i) => Some(i),
1117             Status::Normal(ref e) => e.code(),
1118         }
1119     }
1120
1121     fn success(&self) -> bool {
1122         match *self {
1123             Status::Parsed(i) => i == 0,
1124             Status::Normal(ref e) => e.success(),
1125         }
1126     }
1127 }
1128
1129 impl fmt::Display for Status {
1130     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1131         match *self {
1132             Status::Parsed(i) => write!(f, "exit code: {}", i),
1133             Status::Normal(ref e) => e.fmt(f),
1134         }
1135     }
1136 }
1137
1138 fn compile_test(config: &Config, props: &TestProps,
1139                 testfile: &Path) -> ProcRes {
1140     let aux_dir = aux_output_dir_name(config, testfile);
1141     // FIXME (#9639): This needs to handle non-utf8 paths
1142     let link_args = vec!("-L".to_owned(),
1143                          aux_dir.to_str().unwrap().to_owned());
1144     let args = make_compile_args(config,
1145                                  props,
1146                                  link_args,
1147                                  |a, b| TargetLocation::ThisFile(make_exe_name(a, b)), testfile);
1148     compose_and_run_compiler(config, props, testfile, args, None)
1149 }
1150
1151 fn document(config: &Config, props: &TestProps,
1152             testfile: &Path, out_dir: &Path) -> ProcRes {
1153     if props.build_aux_docs {
1154         for rel_ab in &props.aux_builds {
1155             let abs_ab = config.aux_base.join(rel_ab);
1156             let aux_props = header::load_props(&abs_ab);
1157
1158             let auxres = document(config, &aux_props, &abs_ab, out_dir);
1159             if !auxres.status.success() {
1160                 return auxres;
1161             }
1162         }
1163     }
1164
1165     let aux_dir = aux_output_dir_name(config, testfile);
1166     let mut args = vec!["-L".to_owned(),
1167                         aux_dir.to_str().unwrap().to_owned(),
1168                         "-o".to_owned(),
1169                         out_dir.to_str().unwrap().to_owned(),
1170                         testfile.to_str().unwrap().to_owned()];
1171     args.extend(split_maybe_args(&props.compile_flags));
1172     let args = ProcArgs {
1173         prog: config.rustdoc_path.to_str().unwrap().to_owned(),
1174         args: args,
1175     };
1176     compose_and_run_compiler(config, props, testfile, args, None)
1177 }
1178
1179 fn exec_compiled_test(config: &Config, props: &TestProps,
1180                       testfile: &Path) -> ProcRes {
1181
1182     let env = props.exec_env.clone();
1183
1184     match &*config.target {
1185
1186         "arm-linux-androideabi" | "aarch64-linux-android" => {
1187             _arm_exec_compiled_test(config, props, testfile, env)
1188         }
1189
1190         _=> {
1191             let aux_dir = aux_output_dir_name(config, testfile);
1192             compose_and_run(config,
1193                             testfile,
1194                             make_run_args(config, props, testfile),
1195                             env,
1196                             &config.run_lib_path,
1197                             Some(aux_dir.to_str().unwrap()),
1198                             None)
1199         }
1200     }
1201 }
1202
1203 fn compose_and_run_compiler(config: &Config, props: &TestProps,
1204                             testfile: &Path, args: ProcArgs,
1205                             input: Option<String>) -> ProcRes {
1206     if !props.aux_builds.is_empty() {
1207         ensure_dir(&aux_output_dir_name(config, testfile));
1208     }
1209
1210     let aux_dir = aux_output_dir_name(config, testfile);
1211     // FIXME (#9639): This needs to handle non-utf8 paths
1212     let extra_link_args = vec!["-L".to_owned(),
1213                                aux_dir.to_str().unwrap().to_owned()];
1214
1215     for rel_ab in &props.aux_builds {
1216         let abs_ab = config.aux_base.join(rel_ab);
1217         let aux_props = header::load_props(&abs_ab);
1218         let mut crate_type = if aux_props.no_prefer_dynamic {
1219             Vec::new()
1220         } else {
1221             // We primarily compile all auxiliary libraries as dynamic libraries
1222             // to avoid code size bloat and large binaries as much as possible
1223             // for the test suite (otherwise including libstd statically in all
1224             // executables takes up quite a bit of space).
1225             //
1226             // For targets like MUSL, however, there is no support for dynamic
1227             // libraries so we just go back to building a normal library. Note,
1228             // however, that if the library is built with `force_host` then it's
1229             // ok to be a dylib as the host should always support dylibs.
1230             if config.target.contains("musl") && !aux_props.force_host {
1231                 vec!("--crate-type=lib".to_owned())
1232             } else {
1233                 vec!("--crate-type=dylib".to_owned())
1234             }
1235         };
1236         crate_type.extend(extra_link_args.clone());
1237         let aux_args =
1238             make_compile_args(config,
1239                               &aux_props,
1240                               crate_type,
1241                               |a,b| {
1242                                   let f = make_lib_name(a, b, testfile);
1243                                   let parent = f.parent().unwrap();
1244                                   TargetLocation::ThisDirectory(parent.to_path_buf())
1245                               },
1246                               &abs_ab);
1247         let auxres = compose_and_run(config,
1248                                      &abs_ab,
1249                                      aux_args,
1250                                      Vec::new(),
1251                                      &config.compile_lib_path,
1252                                      Some(aux_dir.to_str().unwrap()),
1253                                      None);
1254         if !auxres.status.success() {
1255             fatal_proc_rec(
1256                 &format!("auxiliary build of {:?} failed to compile: ",
1257                         abs_ab.display()),
1258                 &auxres);
1259         }
1260
1261         match &*config.target {
1262             "arm-linux-androideabi"  | "aarch64-linux-android" => {
1263                 _arm_push_aux_shared_library(config, testfile);
1264             }
1265             _ => {}
1266         }
1267     }
1268
1269     compose_and_run(config,
1270                     testfile,
1271                     args,
1272                     Vec::new(),
1273                     &config.compile_lib_path,
1274                     Some(aux_dir.to_str().unwrap()),
1275                     input)
1276 }
1277
1278 fn ensure_dir(path: &Path) {
1279     if path.is_dir() { return; }
1280     fs::create_dir(path).unwrap();
1281 }
1282
1283 fn compose_and_run(config: &Config, testfile: &Path,
1284                    ProcArgs{ args, prog }: ProcArgs,
1285                    procenv: Vec<(String, String)> ,
1286                    lib_path: &str,
1287                    aux_path: Option<&str>,
1288                    input: Option<String>) -> ProcRes {
1289     return program_output(config, testfile, lib_path,
1290                           prog, aux_path, args, procenv, input);
1291 }
1292
1293 enum TargetLocation {
1294     ThisFile(PathBuf),
1295     ThisDirectory(PathBuf),
1296 }
1297
1298 fn make_compile_args<F>(config: &Config,
1299                         props: &TestProps,
1300                         extras: Vec<String> ,
1301                         xform: F,
1302                         testfile: &Path)
1303                         -> ProcArgs where
1304     F: FnOnce(&Config, &Path) -> TargetLocation,
1305 {
1306     let xform_file = xform(config, testfile);
1307     let target = if props.force_host {
1308         &*config.host
1309     } else {
1310         &*config.target
1311     };
1312     // FIXME (#9639): This needs to handle non-utf8 paths
1313     let mut args = vec!(testfile.to_str().unwrap().to_owned(),
1314                         "-L".to_owned(),
1315                         config.build_base.to_str().unwrap().to_owned(),
1316                         format!("--target={}", target));
1317     args.push_all(&extras);
1318     if !props.no_prefer_dynamic {
1319         args.push("-C".to_owned());
1320         args.push("prefer-dynamic".to_owned());
1321     }
1322     let path = match xform_file {
1323         TargetLocation::ThisFile(path) => {
1324             args.push("-o".to_owned());
1325             path
1326         }
1327         TargetLocation::ThisDirectory(path) => {
1328             args.push("--out-dir".to_owned());
1329             path
1330         }
1331     };
1332     args.push(path.to_str().unwrap().to_owned());
1333     if props.force_host {
1334         args.extend(split_maybe_args(&config.host_rustcflags));
1335     } else {
1336         args.extend(split_maybe_args(&config.target_rustcflags));
1337     }
1338     args.extend(split_maybe_args(&props.compile_flags));
1339     return ProcArgs {
1340         prog: config.rustc_path.to_str().unwrap().to_owned(),
1341         args: args,
1342     };
1343 }
1344
1345 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> PathBuf {
1346     // what we return here is not particularly important, as it
1347     // happens; rustc ignores everything except for the directory.
1348     let auxname = output_testname(auxfile);
1349     aux_output_dir_name(config, testfile).join(&auxname)
1350 }
1351
1352 fn make_exe_name(config: &Config, testfile: &Path) -> PathBuf {
1353     let mut f = output_base_name(config, testfile);
1354     if !env::consts::EXE_SUFFIX.is_empty() {
1355         let mut fname = f.file_name().unwrap().to_os_string();
1356         fname.push(env::consts::EXE_SUFFIX);
1357         f.set_file_name(&fname);
1358     }
1359     f
1360 }
1361
1362 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path)
1363                  -> ProcArgs {
1364     // If we've got another tool to run under (valgrind),
1365     // then split apart its command
1366     let mut args = split_maybe_args(&config.runtool);
1367     let exe_file = make_exe_name(config, testfile);
1368
1369     // FIXME (#9639): This needs to handle non-utf8 paths
1370     args.push(exe_file.to_str().unwrap().to_owned());
1371
1372     // Add the arguments in the run_flags directive
1373     args.extend(split_maybe_args(&props.run_flags));
1374
1375     let prog = args.remove(0);
1376     return ProcArgs {
1377         prog: prog,
1378         args: args,
1379     };
1380 }
1381
1382 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1383     match *argstr {
1384         Some(ref s) => {
1385             s
1386              .split(' ')
1387              .filter_map(|s| {
1388                  if s.chars().all(|c| c.is_whitespace()) {
1389                      None
1390                  } else {
1391                      Some(s.to_owned())
1392                  }
1393              }).collect()
1394         }
1395         None => Vec::new()
1396     }
1397 }
1398
1399 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1400                   aux_path: Option<&str>, args: Vec<String>,
1401                   env: Vec<(String, String)>,
1402                   input: Option<String>) -> ProcRes {
1403     let cmdline =
1404         {
1405             let cmdline = make_cmdline(lib_path,
1406                                        &prog,
1407                                        &args);
1408             logv(config, format!("executing {}", cmdline));
1409             cmdline
1410         };
1411     let procsrv::Result {
1412         out,
1413         err,
1414         status
1415     } = procsrv::run(lib_path,
1416                      &prog,
1417                      aux_path,
1418                      &args,
1419                      env,
1420                      input).expect(&format!("failed to exec `{}`", prog));
1421     dump_output(config, testfile, &out, &err);
1422     return ProcRes {
1423         status: Status::Normal(status),
1424         stdout: out,
1425         stderr: err,
1426         cmdline: cmdline,
1427     };
1428 }
1429
1430 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1431     use util;
1432
1433     // Linux and mac don't require adjusting the library search path
1434     if cfg!(unix) {
1435         format!("{} {}", prog, args.join(" "))
1436     } else {
1437         // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1438         // for diagnostic purposes
1439         fn lib_path_cmd_prefix(path: &str) -> String {
1440             format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1441         }
1442
1443         format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1444     }
1445 }
1446
1447 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1448     dump_output_file(config, testfile, out, "out");
1449     dump_output_file(config, testfile, err, "err");
1450     maybe_dump_to_stdout(config, out, err);
1451 }
1452
1453 fn dump_output_file(config: &Config, testfile: &Path,
1454                     out: &str, extension: &str) {
1455     let outfile = make_out_name(config, testfile, extension);
1456     File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1457 }
1458
1459 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> PathBuf {
1460     output_base_name(config, testfile).with_extension(extension)
1461 }
1462
1463 fn aux_output_dir_name(config: &Config, testfile: &Path) -> PathBuf {
1464     let f = output_base_name(config, testfile);
1465     let mut fname = f.file_name().unwrap().to_os_string();
1466     fname.push(&format!(".{}.libaux", config.mode));
1467     f.with_file_name(&fname)
1468 }
1469
1470 fn output_testname(testfile: &Path) -> PathBuf {
1471     PathBuf::from(testfile.file_stem().unwrap())
1472 }
1473
1474 fn output_base_name(config: &Config, testfile: &Path) -> PathBuf {
1475     config.build_base
1476         .join(&output_testname(testfile))
1477         .with_extension(&config.stage_id)
1478 }
1479
1480 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1481     if config.verbose {
1482         println!("------{}------------------------------", "stdout");
1483         println!("{}", out);
1484         println!("------{}------------------------------", "stderr");
1485         println!("{}", err);
1486         println!("------------------------------------------");
1487     }
1488 }
1489
1490 fn error(err: &str) { println!("\nerror: {}", err); }
1491
1492 fn fatal(err: &str) -> ! { error(err); panic!(); }
1493
1494 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1495     print!("\n\
1496 error: {}\n\
1497 status: {}\n\
1498 command: {}\n\
1499 stdout:\n\
1500 ------------------------------------------\n\
1501 {}\n\
1502 ------------------------------------------\n\
1503 stderr:\n\
1504 ------------------------------------------\n\
1505 {}\n\
1506 ------------------------------------------\n\
1507 \n",
1508              err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1509              proc_res.stderr);
1510     panic!();
1511 }
1512
1513 fn _arm_exec_compiled_test(config: &Config,
1514                            props: &TestProps,
1515                            testfile: &Path,
1516                            env: Vec<(String, String)>)
1517                            -> ProcRes {
1518     let args = make_run_args(config, props, testfile);
1519     let cmdline = make_cmdline("",
1520                                &args.prog,
1521                                &args.args);
1522
1523     // get bare program string
1524     let mut tvec: Vec<String> = args.prog
1525                                     .split('/')
1526                                     .map(str::to_owned)
1527                                     .collect();
1528     let prog_short = tvec.pop().unwrap();
1529
1530     // copy to target
1531     let copy_result = procsrv::run("",
1532                                    &config.adb_path,
1533                                    None,
1534                                    &[
1535                                     "push".to_owned(),
1536                                     args.prog.clone(),
1537                                     config.adb_test_dir.clone()
1538                                    ],
1539                                    vec!(("".to_owned(), "".to_owned())),
1540                                    Some("".to_owned()))
1541         .expect(&format!("failed to exec `{}`", config.adb_path));
1542
1543     if config.verbose {
1544         println!("push ({}) {} {} {}",
1545                  config.target,
1546                  args.prog,
1547                  copy_result.out,
1548                  copy_result.err);
1549     }
1550
1551     logv(config, format!("executing ({}) {}", config.target, cmdline));
1552
1553     let mut runargs = Vec::new();
1554
1555     // run test via adb_run_wrapper
1556     runargs.push("shell".to_owned());
1557     for (key, val) in env {
1558         runargs.push(format!("{}={}", key, val));
1559     }
1560     runargs.push(format!("{}/../adb_run_wrapper.sh", config.adb_test_dir));
1561     runargs.push(format!("{}", config.adb_test_dir));
1562     runargs.push(format!("{}", prog_short));
1563
1564     for tv in &args.args {
1565         runargs.push(tv.to_owned());
1566     }
1567     procsrv::run("",
1568                  &config.adb_path,
1569                  None,
1570                  &runargs,
1571                  vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1572         .expect(&format!("failed to exec `{}`", config.adb_path));
1573
1574     // get exitcode of result
1575     runargs = Vec::new();
1576     runargs.push("shell".to_owned());
1577     runargs.push("cat".to_owned());
1578     runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1579
1580     let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1581         procsrv::run("",
1582                      &config.adb_path,
1583                      None,
1584                      &runargs,
1585                      vec!(("".to_owned(), "".to_owned())),
1586                      Some("".to_owned()))
1587         .expect(&format!("failed to exec `{}`", config.adb_path));
1588
1589     let mut exitcode: i32 = 0;
1590     for c in exitcode_out.chars() {
1591         if !c.is_numeric() { break; }
1592         exitcode = exitcode * 10 + match c {
1593             '0' ... '9' => c as i32 - ('0' as i32),
1594             _ => 101,
1595         }
1596     }
1597
1598     // get stdout of result
1599     runargs = Vec::new();
1600     runargs.push("shell".to_owned());
1601     runargs.push("cat".to_owned());
1602     runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1603
1604     let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1605         procsrv::run("",
1606                      &config.adb_path,
1607                      None,
1608                      &runargs,
1609                      vec!(("".to_owned(), "".to_owned())),
1610                      Some("".to_owned()))
1611         .expect(&format!("failed to exec `{}`", config.adb_path));
1612
1613     // get stderr of result
1614     runargs = Vec::new();
1615     runargs.push("shell".to_owned());
1616     runargs.push("cat".to_owned());
1617     runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1618
1619     let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1620         procsrv::run("",
1621                      &config.adb_path,
1622                      None,
1623                      &runargs,
1624                      vec!(("".to_owned(), "".to_owned())),
1625                      Some("".to_owned()))
1626         .expect(&format!("failed to exec `{}`", config.adb_path));
1627
1628     dump_output(config,
1629                 testfile,
1630                 &stdout_out,
1631                 &stderr_out);
1632
1633     ProcRes {
1634         status: Status::Parsed(exitcode),
1635         stdout: stdout_out,
1636         stderr: stderr_out,
1637         cmdline: cmdline
1638     }
1639 }
1640
1641 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1642     let tdir = aux_output_dir_name(config, testfile);
1643
1644     let dirs = fs::read_dir(&tdir).unwrap();
1645     for file in dirs {
1646         let file = file.unwrap().path();
1647         if file.extension().and_then(|s| s.to_str()) == Some("so") {
1648             // FIXME (#9639): This needs to handle non-utf8 paths
1649             let copy_result = procsrv::run("",
1650                                            &config.adb_path,
1651                                            None,
1652                                            &[
1653                                             "push".to_owned(),
1654                                             file.to_str()
1655                                                 .unwrap()
1656                                                 .to_owned(),
1657                                             config.adb_test_dir.to_owned(),
1658                                            ],
1659                                            vec!(("".to_owned(),
1660                                                  "".to_owned())),
1661                                            Some("".to_owned()))
1662                 .expect(&format!("failed to exec `{}`", config.adb_path));
1663
1664             if config.verbose {
1665                 println!("push ({}) {:?} {} {}",
1666                     config.target, file.display(),
1667                     copy_result.out, copy_result.err);
1668             }
1669         }
1670     }
1671 }
1672
1673 // codegen tests (using FileCheck)
1674
1675 fn compile_test_and_save_ir(config: &Config, props: &TestProps,
1676                                  testfile: &Path) -> ProcRes {
1677     let aux_dir = aux_output_dir_name(config, testfile);
1678     // FIXME (#9639): This needs to handle non-utf8 paths
1679     let mut link_args = vec!("-L".to_owned(),
1680                              aux_dir.to_str().unwrap().to_owned());
1681     let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1682     link_args.extend(llvm_args);
1683     let args = make_compile_args(config,
1684                                  props,
1685                                  link_args,
1686                                  |a, b| TargetLocation::ThisDirectory(
1687                                      output_base_name(a, b).parent()
1688                                         .unwrap().to_path_buf()),
1689                                  testfile);
1690     compose_and_run_compiler(config, props, testfile, args, None)
1691 }
1692
1693 fn check_ir_with_filecheck(config: &Config, testfile: &Path) -> ProcRes {
1694     let irfile = output_base_name(config, testfile).with_extension("ll");
1695     let prog = config.llvm_bin_path.as_ref().unwrap().join("FileCheck");
1696     let proc_args = ProcArgs {
1697         // FIXME (#9639): This needs to handle non-utf8 paths
1698         prog: prog.to_str().unwrap().to_owned(),
1699         args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1700                    testfile.to_str().unwrap().to_owned())
1701     };
1702     compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1703 }
1704
1705 fn run_codegen_test(config: &Config, props: &TestProps, testfile: &Path) {
1706
1707     if config.llvm_bin_path.is_none() {
1708         fatal("missing --llvm-bin-path");
1709     }
1710
1711     let mut proc_res = compile_test_and_save_ir(config, props, testfile);
1712     if !proc_res.status.success() {
1713         fatal_proc_rec("compilation failed!", &proc_res);
1714     }
1715
1716     proc_res = check_ir_with_filecheck(config, testfile);
1717     if !proc_res.status.success() {
1718         fatal_proc_rec("verification with 'FileCheck' failed",
1719                       &proc_res);
1720     }
1721 }
1722
1723 fn charset() -> &'static str {
1724     // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1725     if cfg!(target_os = "bitrig") {
1726         "auto"
1727     } else if cfg!(target_os = "freebsd") {
1728         "ISO-8859-1"
1729     } else {
1730         "UTF-8"
1731     }
1732 }
1733
1734 fn run_rustdoc_test(config: &Config, props: &TestProps, testfile: &Path) {
1735     let out_dir = output_base_name(config, testfile);
1736     let _ = fs::remove_dir_all(&out_dir);
1737     ensure_dir(&out_dir);
1738
1739     let proc_res = document(config, props, testfile, &out_dir);
1740     if !proc_res.status.success() {
1741         fatal_proc_rec("rustdoc failed!", &proc_res);
1742     }
1743     let root = find_rust_src_root(config).unwrap();
1744
1745     let res = cmd2procres(config,
1746                           testfile,
1747                           Command::new(&config.python)
1748                                   .arg(root.join("src/etc/htmldocck.py"))
1749                                   .arg(out_dir)
1750                                   .arg(testfile));
1751     if !res.status.success() {
1752         fatal_proc_rec("htmldocck failed!", &res);
1753     }
1754 }