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