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