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