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.
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.
11 use self::TargetLocation::*;
14 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
15 use common::{Codegen, DebugInfoLldb, DebugInfoGdb};
17 use header::TestProps;
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};
32 use std::time::Duration;
35 pub fn run(config: Config, testfile: &Path) {
36 match &*config.target {
38 "arm-linux-androideabi" | "aarch64-linux-android" => {
39 if !config.adb_device_status {
40 panic!("android device not available");
47 let mut _mm = MetricMap::new();
48 run_metrics(config, testfile, &mut _mm);
51 pub fn run_metrics(config: Config, testfile: &Path, mm: &mut MetricMap) {
53 // We're going to be dumping a lot of info. Start on a new line.
56 debug!("running {:?}", testfile.display());
57 let props = header::load_props(&testfile);
58 debug!("loaded props");
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),
72 fn get_output(props: &TestProps, proc_res: &ProcRes) -> String {
73 if props.check_stdout {
74 format!("{}{}", proc_res.stdout, proc_res.stderr)
76 proc_res.stderr.clone()
80 fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
81 let proc_res = compile_test(config, props, testfile);
83 if proc_res.status.success() {
84 fatal_proc_rec(&format!("{} test compiled successfully!", config.mode)[..],
88 check_correct_failure_status(&proc_res);
90 if proc_res.status.success() {
91 fatal("process did not return an error status");
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");
100 check_expected_errors(expected_errors, testfile, &proc_res);
102 check_error_patterns(props, testfile, &output_to_check, &proc_res);
104 check_no_compiler_crash(&proc_res);
105 check_forbid_output(props, &output_to_check, &proc_res);
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);
112 if !proc_res.status.success() {
113 fatal_proc_rec("compilation failed!", &proc_res);
116 exec_compiled_test(config, props, testfile)
118 jit_test(config, props, testfile)
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);
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);
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) {
137 &format!("failure produced the wrong error: {}",
143 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
145 let mut proc_res = compile_test(config, props, testfile);
147 if !proc_res.status.success() {
148 fatal_proc_rec("compilation failed!", &proc_res);
151 proc_res = exec_compiled_test(config, props, testfile);
153 if !proc_res.status.success() {
154 fatal_proc_rec("test run failed!", &proc_res);
157 let proc_res = jit_test(config, props, testfile);
159 if !proc_res.status.success() {
160 fatal_proc_rec("jit failed!", &proc_res);
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);
171 let mut proc_res = compile_test(config, props, testfile);
173 if !proc_res.status.success() {
174 fatal_proc_rec("compilation failed!", &proc_res);
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);
181 if !proc_res.status.success() {
182 fatal_proc_rec("test run failed!", &proc_res);
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());
190 logv(config, "testing for converging pretty-printing".to_string());
194 match props.pp_exact { Some(_) => 1, None => 2 };
196 let mut src = String::new();
197 File::open(testfile).unwrap().read_to_string(&mut src).unwrap();
198 let mut srcs = vec!(src);
201 while round < rounds {
202 logv(config, format!("pretty-printing round {}", round));
203 let proc_res = print_source(config,
206 srcs[round].to_string(),
209 if !proc_res.status.success() {
210 fatal_proc_rec(&format!("pretty-printing failed in round {}", round),
214 let ProcRes{ stdout, .. } = proc_res;
219 let mut expected = match props.pp_exact {
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();
226 None => { srcs[srcs.len() - 2].clone() }
228 let mut actual = srcs[srcs.len() - 1].clone();
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();
237 compare_source(&expected, &actual);
239 // If we're only making sure that the output matches then just stop here
240 if props.pretty_compare_only { return; }
242 // Finally, let's make sure it actually appears to remain valid code
243 let proc_res = typecheck_source(config, props, testfile, actual);
245 if !proc_res.status.success() {
246 fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
248 if props.no_pretty_expanded { return }
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);
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",
265 fn print_source(config: &Config,
269 pretty_type: &str) -> ProcRes {
270 let aux_dir = aux_output_dir_name(config, testfile);
271 compose_and_run(config,
276 pretty_type.to_string()),
277 props.exec_env.clone(),
278 &config.compile_lib_path,
279 Some(aux_dir.to_str().unwrap()),
283 fn make_pp_args(config: &Config,
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(),
293 format!("--target={}", config.target),
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());
299 prog: config.rustc_path.to_str().unwrap().to_string(),
304 fn compare_source(expected: &str, actual: &str) {
305 if expected != actual {
306 error("pretty-printed source does not match expected source");
309 ------------------------------------------\n\
311 ------------------------------------------\n\
313 ------------------------------------------\n\
315 ------------------------------------------\n\
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))
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 {
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),
341 config.build_base.to_str().unwrap().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
348 prog: config.rustc_path.to_str().unwrap().to_string(),
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),
361 let config = &mut config;
362 let DebuggerCommands {
366 } = parse_debugger_commands(testfile, "gdb");
367 let mut cmds = commands.connect("\n");
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);
375 let exe_file = make_exe_name(config, testfile);
377 let debugger_run_result;
378 match &*config.target {
379 "arm-linux-androideabi" | "aarch64-linux-android" => {
381 cmds = cmds.replace("run", "continue");
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()
397 script_str.push_str(&cmds);
398 script_str.push_str("\nquit\n");
400 debug!("script_str = {}", script_str);
401 dump_output_file(config,
412 exe_file.to_str().unwrap().to_string(),
413 config.adb_test_dir.clone()
415 vec!(("".to_string(), "".to_string())),
416 Some("".to_string()))
417 .expect(&format!("failed to exec `{:?}`", config.adb_path));
423 "forward".to_string(),
424 "tcp:5039".to_string(),
425 "tcp:5039".to_string()
427 vec!(("".to_string(), "".to_string())),
428 Some("".to_string()))
429 .expect(&format!("failed to exec `{:?}`", config.adb_path));
431 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
432 gdbserver{} :5039 {}/{}",
433 config.adb_test_dir.clone(),
434 if config.target.contains("aarch64")
436 config.adb_test_dir.clone(),
437 exe_file.file_name().unwrap().to_str()
440 let mut process = procsrv::run_background("",
448 vec!(("".to_string(),
450 Some("".to_string()))
451 .expect(&format!("failed to exec `{:?}`", config.adb_path));
453 //waiting 1 second for gdbserver start
456 ::std::old_io::timer::sleep(Duration::milliseconds(1000));
459 if TcpStream::connect("127.0.0.1:5039").is_ok() {
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")
469 let debugger_script = make_out_name(config, testfile, "debugger.script");
470 // FIXME (#9639): This needs to handle non-utf8 paths
472 vec!("-quiet".to_string(),
473 "-batch".to_string(),
475 format!("-command={}", debugger_script.to_str().unwrap()));
477 let mut gdb_path = tool_path;
478 gdb_path.push_str(&format!("/bin/{}-gdb", config.target));
479 let procsrv::Result {
487 vec!(("".to_string(), "".to_string())),
489 .expect(&format!("failed to exec `{:?}`", gdb_path));
491 let cmdline = make_cmdline("",
492 &format!("{}-gdb", config.target),
494 logv(config, format!("executing {}", cmdline));
498 debugger_run_result = ProcRes {
499 status: Status::Normal(status),
504 if process.kill().is_err() {
505 println!("Adb process is already finished.");
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)
517 // write debugger script
518 let mut script_str = String::with_capacity(2048);
520 script_str.push_str("set charset UTF-8\n");
521 script_str.push_str("show version\n");
523 match config.gdb_version {
524 Some(ref version) => {
525 println!("NOTE: compiletest thinks it is using GDB version {}",
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
533 &format!("add-auto-load-safe-path {}\n",
534 rust_pp_module_abs_path.replace(r"\", r"\\"))
539 println!("NOTE: compiletest does not know which version of \
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");
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));
552 // Load the target executable
553 script_str.push_str(&format!("file {}\n",
554 exe_file.to_str().unwrap()
555 .replace(r"\", r"\\")));
557 // Add line breakpoints
558 for line in &breakpoint_lines {
559 script_str.push_str(&format!("break '{}':{}\n",
560 testfile.file_name().unwrap()
565 script_str.push_str(&cmds);
566 script_str.push_str("\nquit\n");
568 debug!("script_str = {}", script_str);
569 dump_output_file(config,
574 // run debugger script with gdb
575 fn debugger() -> &'static str {
576 if cfg!(windows) {"gdb.exe"} else {"gdb"}
579 let debugger_script = make_out_name(config, testfile, "debugger.script");
581 // FIXME (#9639): This needs to handle non-utf8 paths
583 vec!("-quiet".to_string(),
584 "-batch".to_string(),
586 format!("-command={}", debugger_script.to_str().unwrap()));
588 let proc_args = ProcArgs {
589 prog: debugger().to_string(),
593 let environment = vec![("PYTHONPATH".to_string(), rust_pp_module_abs_path)];
595 debugger_run_result = compose_and_run(config,
599 &config.run_lib_path,
605 if !debugger_run_result.status.success() {
606 fatal("gdb failed to execute");
609 check_debugger_output(&debugger_run_result, &check_lines);
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");
617 if path.join(&path_postfix).is_file() {
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.");
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),
636 let config = &mut config;
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);
644 let exe_file = make_exe_name(config, testfile);
646 match config.lldb_version {
647 Some(ref version) => {
648 println!("NOTE: compiletest thinks it is using LLDB version {}",
652 println!("NOTE: compiletest does not know which version of \
657 // Parse debugger commands etc from test files
658 let DebuggerCommands {
663 } = parse_debugger_commands(testfile, "lldb");
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");
669 // Make LLDB emit its version, so we have it documented in the test output
670 script_str.push_str("version\n");
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)
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");
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));
693 // Append the other commands
694 for line in &commands {
695 script_str.push_str(line);
696 script_str.push_str("\n");
699 // Finally, quit the debugger
700 script_str.push_str("\nquit\n");
702 // Write the script into a file
703 debug!("script_str = {}", script_str);
704 dump_output_file(config,
708 let debugger_script = make_out_name(config, testfile, "debugger.script");
710 // Let LLDB execute the script via lldb_batchmode.py
711 let debugger_run_result = run_lldb(config,
716 if !debugger_run_result.status.success() {
717 fatal_proc_rec("Error while running LLDB", &debugger_run_result);
720 check_debugger_output(&debugger_run_result, &check_lines);
722 fn run_lldb(config: &Config,
723 test_executable: &Path,
724 debugger_script: &Path,
725 rust_src_root: &Path)
727 // Prepare the lldb_batchmode which executes the debugger script
728 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
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());
736 let (status, out, err) = match cmd.output() {
737 Ok(Output { status, stdout, stderr }) => {
739 String::from_utf8(stdout).unwrap(),
740 String::from_utf8(stderr).unwrap())
743 fatal(&format!("Failed to setup Python process for \
744 LLDB script: {}", e))
748 dump_output(config, test_executable, &out, &err);
750 status: Status::Normal(status),
753 cmdline: format!("{:?}", cmd)
758 struct DebuggerCommands {
759 commands: Vec<String>,
760 check_lines: Vec<String>,
761 breakpoint_lines: Vec<uint>,
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);
769 let mut breakpoint_lines = vec!();
770 let mut commands = vec!();
771 let mut check_lines = vec!();
773 let reader = BufReader::new(File::open(file_path).unwrap());
774 for line in reader.lines() {
777 if line.contains("#break") {
778 breakpoint_lines.push(counter);
781 header::parse_name_value_directive(
783 &command_directive).map(|cmd| {
787 header::parse_name_value_directive(
789 &check_directive).map(|cmd| {
790 check_lines.push(cmd)
794 fatal(&format!("Error while parsing debugger commands: {}", e))
802 check_lines: check_lines,
803 breakpoint_lines: breakpoint_lines,
807 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
808 if options.is_none() {
812 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
813 let options_to_remove = [
816 "--debuginfo".to_string()
819 split_maybe_args(options).into_iter()
820 .filter(|x| !options_to_remove.contains(x))
821 .collect::<Vec<String>>()
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| {
836 .map(|x| x.to_string())
839 // check if each line in props.check_lines appears in the
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) {
862 rest = &rest[(i + frag.len())..];
867 if !failed && rest.len() == 0 {
870 if i == num_check_lines {
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);
883 fn check_error_patterns(props: &TestProps,
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()));
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);
897 if next_err_idx == props.error_patterns.len() {
898 debug!("found all error patterns");
902 next_err_pat = &props.error_patterns[next_err_idx];
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]),
912 for pattern in missing_patterns {
913 error(&format!("error pattern '{}' not found!", *pattern));
915 fatal_proc_rec("multiple error patterns not found", proc_res);
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",
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);
938 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
940 proc_res: &ProcRes) {
942 // true if we found the error in question
943 let mut found_flags: Vec<_> = repeat(false).take(expected_errors.len()).collect();
945 if proc_res.status.success() {
946 fatal("process did not return an error status");
949 let prefixes = expected_errors.iter().map(|ee| {
950 format!("{}:{}:", testfile.display(), ee.line)
951 }).collect::<Vec<String>>();
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"\", "/");
958 line.to_ascii_lowercase().starts_with(&prefix.to_ascii_lowercase())
960 line.starts_with(prefix)
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") ||
970 // Should have 4 spaces: see issue 18946
971 line.starts_with("(")
974 // Scan and extract our error/warning messages,
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() {
984 debug!("prefix={} ee.kind={} ee.msg={} 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;
999 // ignore this msg which gets printed at the end
1000 if line.contains("aborting due to") {
1001 was_expected = true;
1004 if !was_expected && is_compiler_error_or_warning(line) {
1005 fatal_proc_rec(&format!("unexpected compiler error or warning: '{}'",
1011 for (i, &flag) in found_flags.iter().enumerate() {
1013 let ee = &expected_errors[i];
1014 fatal_proc_rec(&format!("expected {} on line {} not found: {}",
1015 ee.kind, ee.line, ee.msg),
1021 fn is_compiler_error_or_warning(line: &str) -> bool {
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));
1039 fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
1040 if *idx >= haystack.len() {
1043 let opt = haystack[(*idx)..].find(needle);
1047 *idx = opt.unwrap();
1051 fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
1052 if *idx >= haystack.len() {
1055 let range = haystack.char_range_at(*idx);
1056 if range.ch != needle {
1063 fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
1065 while i < haystack.len() {
1066 let range = haystack.char_range_at(i);
1067 if range.ch < '0' || '9' < range.ch {
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() {
1086 let range = haystack.char_range_at(haystack_i);
1087 haystack_i = range.next;
1088 if !scan_char(needle, range.ch, &mut needle_i) {
1114 fn code(&self) -> Option<i32> {
1116 Status::Parsed(i) => Some(i),
1117 Status::Normal(ref e) => e.code(),
1121 fn success(&self) -> bool {
1123 Status::Parsed(i) => i == 0,
1124 Status::Normal(ref e) => e.success(),
1129 impl fmt::Display for Status {
1130 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1132 Status::Parsed(i) => write!(f, "exit code: {}", i),
1133 Status::Normal(ref e) => e.fmt(f),
1138 fn compile_test(config: &Config, props: &TestProps,
1139 testfile: &Path) -> ProcRes {
1140 compile_test_(config, props, testfile, &[])
1143 fn jit_test(config: &Config, props: &TestProps, testfile: &Path) -> ProcRes {
1144 compile_test_(config, props, testfile, &["--jit".to_string()])
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,
1157 |a, b| TargetLocation::ThisFile(make_exe_name(a, b)), testfile);
1158 compose_and_run_compiler(config, props, testfile, args, None)
1161 fn exec_compiled_test(config: &Config, props: &TestProps,
1162 testfile: &Path) -> ProcRes {
1164 let env = props.exec_env.clone();
1166 match &*config.target {
1168 "arm-linux-androideabi" | "aarch64-linux-android" => {
1169 _arm_exec_compiled_test(config, props, testfile, env)
1173 let aux_dir = aux_output_dir_name(config, testfile);
1174 compose_and_run(config,
1176 make_run_args(config, props, testfile),
1178 &config.run_lib_path,
1179 Some(aux_dir.to_str().unwrap()),
1185 fn compose_and_run_compiler(
1190 input: Option<String>) -> ProcRes {
1192 if !props.aux_builds.is_empty() {
1193 ensure_dir(&aux_output_dir_name(config, testfile));
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());
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 {
1206 vec!("--crate-type=dylib".to_string())
1208 crate_type.extend(extra_link_args.clone().into_iter());
1210 make_compile_args(config,
1214 let f = make_lib_name(a, b, testfile);
1215 let parent = f.parent().unwrap();
1216 TargetLocation::ThisDirectory(parent.to_path_buf())
1219 let auxres = compose_and_run(config,
1223 &config.compile_lib_path,
1224 Some(aux_dir.to_str().unwrap()),
1226 if !auxres.status.success() {
1228 &format!("auxiliary build of {:?} failed to compile: ",
1233 match &*config.target {
1234 "arm-linux-androideabi" | "aarch64-linux-android" => {
1235 _arm_push_aux_shared_library(config, testfile);
1241 compose_and_run(config,
1245 &config.compile_lib_path,
1246 Some(aux_dir.to_str().unwrap()),
1250 fn ensure_dir(path: &Path) {
1251 if path.is_dir() { return; }
1252 fs::create_dir(path).unwrap();
1255 fn compose_and_run(config: &Config, testfile: &Path,
1256 ProcArgs{ args, prog }: ProcArgs,
1257 procenv: Vec<(String, String)> ,
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);
1265 enum TargetLocation {
1267 ThisDirectory(PathBuf),
1270 fn make_compile_args<F>(config: &Config,
1272 extras: Vec<String> ,
1276 F: FnOnce(&Config, &Path) -> TargetLocation,
1278 let xform_file = xform(config, testfile);
1279 let target = if props.force_host {
1284 // FIXME (#9639): This needs to handle non-utf8 paths
1285 let mut args = vec!(testfile.to_str().unwrap().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());
1294 let path = match xform_file {
1295 TargetLocation::ThisFile(path) => {
1296 args.push("-o".to_string());
1299 TargetLocation::ThisDirectory(path) => {
1300 args.push("--out-dir".to_string());
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());
1308 args.extend(split_maybe_args(&config.target_rustcflags).into_iter());
1310 args.extend(split_maybe_args(&props.compile_flags).into_iter());
1312 prog: config.rustc_path.to_str().unwrap().to_string(),
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)
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);
1334 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path) ->
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);
1341 // FIXME (#9639): This needs to handle non-utf8 paths
1342 args.push(exe_file.to_str().unwrap().to_string());
1344 // Add the arguments in the run_flags directive
1345 args.extend(split_maybe_args(&props.run_flags).into_iter());
1347 let prog = args.remove(0);
1354 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1360 if s.chars().all(|c| c.is_whitespace()) {
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 {
1377 let cmdline = make_cmdline(lib_path,
1380 logv(config, format!("executing {}", cmdline));
1383 let procsrv::Result {
1387 } = procsrv::run(lib_path,
1392 input).expect(&format!("failed to exec `{}`", prog));
1393 dump_output(config, testfile, &out, &err);
1395 status: Status::Normal(status),
1402 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1405 // Linux and mac don't require adjusting the library search path
1407 format!("{} {}", prog, args.connect(" "))
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))
1415 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.connect(" "))
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);
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();
1431 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> PathBuf {
1432 output_base_name(config, testfile).with_extension(extension)
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)
1442 fn output_testname(testfile: &Path) -> PathBuf {
1443 PathBuf::new(testfile.file_stem().unwrap())
1446 fn output_base_name(config: &Config, testfile: &Path) -> PathBuf {
1448 .join(&output_testname(testfile))
1449 .with_extension(&config.stage_id)
1452 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1454 println!("------{}------------------------------", "stdout");
1455 println!("{}", out);
1456 println!("------{}------------------------------", "stderr");
1457 println!("{}", err);
1458 println!("------------------------------------------");
1462 fn error(err: &str) { println!("\nerror: {}", err); }
1464 fn fatal(err: &str) -> ! { error(err); panic!(); }
1466 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1472 ------------------------------------------\n\
1474 ------------------------------------------\n\
1476 ------------------------------------------\n\
1478 ------------------------------------------\n\
1480 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1485 fn _arm_exec_compiled_test(config: &Config,
1488 env: Vec<(String, String)>)
1490 let args = make_run_args(config, props, testfile);
1491 let cmdline = make_cmdline("",
1495 // get bare program string
1496 let mut tvec: Vec<String> = args.prog
1498 .map(|ts| ts.to_string())
1500 let prog_short = tvec.pop().unwrap();
1503 let copy_result = procsrv::run("",
1509 config.adb_test_dir.clone()
1511 vec!(("".to_string(), "".to_string())),
1512 Some("".to_string()))
1513 .expect(&format!("failed to exec `{}`", config.adb_path));
1516 println!("push ({}) {} {} {}",
1523 logv(config, format!("executing ({}) {}", config.target, cmdline));
1525 let mut runargs = Vec::new();
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));
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));
1536 for tv in &args.args {
1537 runargs.push(tv.to_string());
1543 vec!(("".to_string(), "".to_string())), Some("".to_string()))
1544 .expect(&format!("failed to exec `{}`", config.adb_path));
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));
1552 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1557 vec!(("".to_string(), "".to_string())),
1558 Some("".to_string()))
1559 .expect(&format!("failed to exec `{}`", config.adb_path));
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),
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));
1576 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1581 vec!(("".to_string(), "".to_string())),
1582 Some("".to_string()))
1583 .expect(&format!("failed to exec `{}`", config.adb_path));
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));
1591 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1596 vec!(("".to_string(), "".to_string())),
1597 Some("".to_string()))
1598 .expect(&format!("failed to exec `{}`", config.adb_path));
1606 status: Status::Parsed(exitcode),
1613 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1614 let tdir = aux_output_dir_name(config, testfile);
1616 let dirs = fs::read_dir(&tdir).unwrap();
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("",
1629 config.adb_test_dir.to_string(),
1631 vec!(("".to_string(),
1633 Some("".to_string()))
1634 .expect(&format!("failed to exec `{}`", config.adb_path));
1637 println!("push ({}) {:?} {} {}",
1638 config.target, file.display(),
1639 copy_result.out, copy_result.err);
1645 // codegen tests (vs. clang)
1647 fn append_suffix_to_stem(p: &Path, suffix: &str) -> PathBuf {
1648 if suffix.len() == 0 {
1651 let mut stem = p.file_stem().unwrap().to_os_string();
1654 p.with_file_name(&stem)
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,
1670 |a, b| TargetLocation::ThisDirectory(
1671 output_base_name(a, b).parent()
1672 .unwrap().to_path_buf()),
1674 compose_and_run_compiler(config, props, testfile, args, None)
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(),
1688 bitcodefile.to_str().unwrap().to_string(),
1689 testcc.to_str().unwrap().to_string())
1691 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
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())
1708 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
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())
1724 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
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();
1736 fn run_codegen_test(config: &Config, props: &TestProps,
1737 testfile: &Path, mm: &mut MetricMap) {
1739 if config.llvm_bin_path.is_none() {
1740 fatal("missing --llvm-bin-path");
1743 if config.clang_path.is_none() {
1744 fatal("missing --clang-path");
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);
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",
1758 proc_res = disassemble_extract(config, props, testfile, "");
1759 if !proc_res.status.success() {
1760 fatal_proc_rec("disassembling extract failed", &proc_res);
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);
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",
1775 proc_res = disassemble_extract(config, props, testfile, "clang");
1776 if !proc_res.status.success() {
1777 fatal_proc_rec("disassembling extract failed", &proc_res);
1780 let base = output_base_name(config, testfile);
1781 let base_extract = append_suffix_to_stem(&base, "extract");
1783 let base_clang = append_suffix_to_stem(&base, "clang");
1784 let base_clang_extract = append_suffix_to_stem(&base_clang, "extract");
1786 let base_lines = count_extracted_lines(&base_extract);
1787 let clang_lines = count_extracted_lines(&base_clang_extract);
1789 mm.insert_metric("clang-codegen-ratio",
1790 (base_lines as f64) / (clang_lines as f64),