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