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