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