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, Rustdoc};
17 use header::TestProps;
24 use std::fs::{self, File};
25 use std::io::BufReader;
26 use std::io::prelude::*;
27 use std::net::TcpStream;
28 use std::path::{Path, PathBuf, Component};
29 use std::process::{Command, Output, ExitStatus};
31 pub fn run(config: Config, testfile: &Path) {
32 match &*config.target {
34 "arm-linux-androideabi" | "aarch64-linux-android" => {
35 if !config.adb_device_status {
36 panic!("android device not available");
44 // We're going to be dumping a lot of info. Start on a new line.
47 debug!("running {:?}", testfile.display());
48 let props = header::load_props(&testfile);
49 debug!("loaded props");
51 CompileFail => run_cfail_test(&config, &props, &testfile),
52 ParseFail => run_cfail_test(&config, &props, &testfile),
53 RunFail => run_rfail_test(&config, &props, &testfile),
54 RunPass => run_rpass_test(&config, &props, &testfile),
55 RunPassValgrind => run_valgrind_test(&config, &props, &testfile),
56 Pretty => run_pretty_test(&config, &props, &testfile),
57 DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
58 DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
59 Codegen => run_codegen_test(&config, &props, &testfile),
60 Rustdoc => run_rustdoc_test(&config, &props, &testfile),
64 fn get_output(props: &TestProps, proc_res: &ProcRes) -> String {
65 if props.check_stdout {
66 format!("{}{}", proc_res.stdout, proc_res.stderr)
68 proc_res.stderr.clone()
72 fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
73 let proc_res = compile_test(config, props, testfile);
75 if proc_res.status.success() {
76 fatal_proc_rec(&format!("{} test compiled successfully!", config.mode)[..],
80 check_correct_failure_status(&proc_res);
82 if proc_res.status.success() {
83 fatal("process did not return an error status");
86 let output_to_check = get_output(props, &proc_res);
87 let expected_errors = errors::load_errors(testfile);
88 if !expected_errors.is_empty() {
89 if !props.error_patterns.is_empty() {
90 fatal("both error pattern and expected errors specified");
92 check_expected_errors(expected_errors, testfile, &proc_res);
94 check_error_patterns(props, testfile, &output_to_check, &proc_res);
96 check_no_compiler_crash(&proc_res);
97 check_forbid_output(props, &output_to_check, &proc_res);
100 fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
101 let proc_res = compile_test(config, props, testfile);
103 if !proc_res.status.success() {
104 fatal_proc_rec("compilation failed!", &proc_res);
107 let proc_res = exec_compiled_test(config, props, testfile);
109 // The value our Makefile configures valgrind to return on failure
110 const VALGRIND_ERR: i32 = 100;
111 if proc_res.status.code() == Some(VALGRIND_ERR) {
112 fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
115 let output_to_check = get_output(props, &proc_res);
116 check_correct_failure_status(&proc_res);
117 check_error_patterns(props, testfile, &output_to_check, &proc_res);
120 fn check_correct_failure_status(proc_res: &ProcRes) {
121 // The value the rust runtime returns on failure
122 const RUST_ERR: i32 = 101;
123 if proc_res.status.code() != Some(RUST_ERR) {
125 &format!("failure produced the wrong error: {}",
131 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
132 let proc_res = compile_test(config, props, testfile);
134 if !proc_res.status.success() {
135 fatal_proc_rec("compilation failed!", &proc_res);
138 let proc_res = exec_compiled_test(config, props, testfile);
140 if !proc_res.status.success() {
141 fatal_proc_rec("test run failed!", &proc_res);
145 fn run_valgrind_test(config: &Config, props: &TestProps, testfile: &Path) {
146 if config.valgrind_path.is_none() {
147 assert!(!config.force_valgrind);
148 return run_rpass_test(config, props, testfile);
151 let mut proc_res = compile_test(config, props, testfile);
153 if !proc_res.status.success() {
154 fatal_proc_rec("compilation failed!", &proc_res);
157 let mut new_config = config.clone();
158 new_config.runtool = new_config.valgrind_path.clone();
159 proc_res = exec_compiled_test(&new_config, props, testfile);
161 if !proc_res.status.success() {
162 fatal_proc_rec("test run failed!", &proc_res);
166 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
167 if props.pp_exact.is_some() {
168 logv(config, "testing for exact pretty-printing".to_owned());
170 logv(config, "testing for converging pretty-printing".to_owned());
174 match props.pp_exact { Some(_) => 1, None => 2 };
176 let mut src = String::new();
177 File::open(testfile).unwrap().read_to_string(&mut src).unwrap();
178 let mut srcs = vec!(src);
181 while round < rounds {
182 logv(config, format!("pretty-printing round {}", round));
183 let proc_res = print_source(config,
186 srcs[round].to_owned(),
189 if !proc_res.status.success() {
190 fatal_proc_rec(&format!("pretty-printing failed in round {}", round),
194 let ProcRes{ stdout, .. } = proc_res;
199 let mut expected = match props.pp_exact {
201 let filepath = testfile.parent().unwrap().join(file);
202 let mut s = String::new();
203 File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
206 None => { srcs[srcs.len() - 2].clone() }
208 let mut actual = srcs[srcs.len() - 1].clone();
210 if props.pp_exact.is_some() {
211 // Now we have to care about line endings
212 let cr = "\r".to_owned();
213 actual = actual.replace(&cr, "").to_owned();
214 expected = expected.replace(&cr, "").to_owned();
217 compare_source(&expected, &actual);
219 // If we're only making sure that the output matches then just stop here
220 if props.pretty_compare_only { return; }
222 // Finally, let's make sure it actually appears to remain valid code
223 let proc_res = typecheck_source(config, props, testfile, actual);
225 if !proc_res.status.success() {
226 fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
228 if !props.pretty_expanded { return }
230 // additionally, run `--pretty expanded` and try to build it.
231 let proc_res = print_source(config, props, testfile, srcs[round].clone(), "expanded");
232 if !proc_res.status.success() {
233 fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
236 let ProcRes{ stdout: expanded_src, .. } = proc_res;
237 let proc_res = typecheck_source(config, props, testfile, expanded_src);
238 if !proc_res.status.success() {
239 fatal_proc_rec("pretty-printed source (expanded) does not typecheck",
245 fn print_source(config: &Config,
249 pretty_type: &str) -> ProcRes {
250 let aux_dir = aux_output_dir_name(config, testfile);
251 compose_and_run(config,
256 pretty_type.to_owned()),
257 props.exec_env.clone(),
258 &config.compile_lib_path,
259 Some(aux_dir.to_str().unwrap()),
263 fn make_pp_args(config: &Config,
266 pretty_type: String) -> ProcArgs {
267 let aux_dir = aux_output_dir_name(config, testfile);
268 // FIXME (#9639): This needs to handle non-utf8 paths
269 let mut args = vec!("-".to_owned(),
270 "-Zunstable-options".to_owned(),
271 "--unpretty".to_owned(),
273 format!("--target={}", config.target),
275 aux_dir.to_str().unwrap().to_owned());
276 args.extend(split_maybe_args(&config.target_rustcflags));
277 args.extend(split_maybe_args(&props.compile_flags));
279 prog: config.rustc_path.to_str().unwrap().to_owned(),
284 fn compare_source(expected: &str, actual: &str) {
285 if expected != actual {
286 error("pretty-printed source does not match expected source");
289 ------------------------------------------\n\
291 ------------------------------------------\n\
293 ------------------------------------------\n\
295 ------------------------------------------\n\
302 fn typecheck_source(config: &Config, props: &TestProps,
303 testfile: &Path, src: String) -> ProcRes {
304 let args = make_typecheck_args(config, props, testfile);
305 compose_and_run_compiler(config, props, testfile, args, Some(src))
308 fn make_typecheck_args(config: &Config, props: &TestProps, testfile: &Path) -> ProcArgs {
309 let aux_dir = aux_output_dir_name(config, testfile);
310 let target = if props.force_host {
315 // FIXME (#9639): This needs to handle non-utf8 paths
316 let mut args = vec!("-".to_owned(),
317 "-Zno-trans".to_owned(),
318 format!("--target={}", target),
320 config.build_base.to_str().unwrap().to_owned(),
322 aux_dir.to_str().unwrap().to_owned());
323 args.extend(split_maybe_args(&config.target_rustcflags));
324 args.extend(split_maybe_args(&props.compile_flags));
325 // FIXME (#9639): This needs to handle non-utf8 paths
327 prog: config.rustc_path.to_str().unwrap().to_owned(),
333 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
334 let mut config = Config {
335 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
336 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
340 let config = &mut config;
341 let DebuggerCommands {
345 } = parse_debugger_commands(testfile, "gdb");
346 let mut cmds = commands.join("\n");
348 // compile test file (it should have 'compile-flags:-g' in the header)
349 let compiler_run_result = compile_test(config, props, testfile);
350 if !compiler_run_result.status.success() {
351 fatal_proc_rec("compilation failed!", &compiler_run_result);
354 let exe_file = make_exe_name(config, testfile);
356 let debugger_run_result;
357 match &*config.target {
358 "arm-linux-androideabi" | "aarch64-linux-android" => {
360 cmds = cmds.replace("run", "continue");
362 // write debugger script
363 let mut script_str = String::with_capacity(2048);
364 script_str.push_str(&format!("set charset {}\n", charset()));
365 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
366 script_str.push_str("target remote :5039\n");
367 script_str.push_str(&format!("set solib-search-path \
368 ./{}/stage2/lib/rustlib/{}/lib/\n",
369 config.host, config.target));
370 for line in &breakpoint_lines {
371 script_str.push_str(&format!("break {:?}:{}\n",
372 testfile.file_name().unwrap()
376 script_str.push_str(&cmds);
377 script_str.push_str("\nquit\n");
379 debug!("script_str = {}", script_str);
380 dump_output_file(config,
391 exe_file.to_str().unwrap().to_owned(),
392 config.adb_test_dir.clone()
394 vec!(("".to_owned(), "".to_owned())),
396 .expect(&format!("failed to exec `{:?}`", config.adb_path));
402 "forward".to_owned(),
403 "tcp:5039".to_owned(),
404 "tcp:5039".to_owned()
406 vec!(("".to_owned(), "".to_owned())),
408 .expect(&format!("failed to exec `{:?}`", config.adb_path));
410 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
411 gdbserver{} :5039 {}/{}",
412 config.adb_test_dir.clone(),
413 if config.target.contains("aarch64")
415 config.adb_test_dir.clone(),
416 exe_file.file_name().unwrap().to_str()
419 let mut process = procsrv::run_background("",
430 .expect(&format!("failed to exec `{:?}`", config.adb_path));
432 //waiting 1 second for gdbserver start
433 ::std::thread::sleep_ms(1000);
434 if TcpStream::connect("127.0.0.1:5039").is_ok() {
439 let tool_path = match config.android_cross_path.to_str() {
440 Some(x) => x.to_owned(),
441 None => fatal("cannot find android cross path")
444 let debugger_script = make_out_name(config, testfile, "debugger.script");
445 // FIXME (#9639): This needs to handle non-utf8 paths
447 vec!("-quiet".to_owned(),
450 format!("-command={}", debugger_script.to_str().unwrap()));
452 let mut gdb_path = tool_path;
453 gdb_path.push_str(&format!("/bin/{}-gdb", config.target));
454 let procsrv::Result {
462 vec!(("".to_owned(), "".to_owned())),
464 .expect(&format!("failed to exec `{:?}`", gdb_path));
466 let cmdline = make_cmdline("",
467 &format!("{}-gdb", config.target),
469 logv(config, format!("executing {}", cmdline));
473 debugger_run_result = ProcRes {
474 status: Status::Normal(status),
479 if process.kill().is_err() {
480 println!("Adb process is already finished.");
485 let rust_src_root = find_rust_src_root(config)
486 .expect("Could not find Rust source root");
487 let rust_pp_module_rel_path = Path::new("./src/etc");
488 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
492 // write debugger script
493 let mut script_str = String::with_capacity(2048);
494 script_str.push_str(&format!("set charset {}\n", charset()));
495 script_str.push_str("show version\n");
497 match config.gdb_version {
498 Some(ref version) => {
499 println!("NOTE: compiletest thinks it is using GDB version {}",
502 if header::gdb_version_to_int(version) >
503 header::gdb_version_to_int("7.4") {
504 // Add the directory containing the pretty printers to
505 // GDB's script auto loading safe path
507 &format!("add-auto-load-safe-path {}\n",
508 rust_pp_module_abs_path.replace(r"\", r"\\"))
513 println!("NOTE: compiletest does not know which version of \
518 // The following line actually doesn't have to do anything with
519 // pretty printing, it just tells GDB to print values on one line:
520 script_str.push_str("set print pretty off\n");
522 // Add the pretty printer directory to GDB's source-file search path
523 script_str.push_str(&format!("directory {}\n",
524 rust_pp_module_abs_path));
526 // Load the target executable
527 script_str.push_str(&format!("file {}\n",
528 exe_file.to_str().unwrap()
529 .replace(r"\", r"\\")));
531 // Add line breakpoints
532 for line in &breakpoint_lines {
533 script_str.push_str(&format!("break '{}':{}\n",
534 testfile.file_name().unwrap()
539 script_str.push_str(&cmds);
540 script_str.push_str("\nquit\n");
542 debug!("script_str = {}", script_str);
543 dump_output_file(config,
548 // run debugger script with gdb
549 fn debugger() -> &'static str {
550 if cfg!(windows) {"gdb.exe"} else {"gdb"}
553 let debugger_script = make_out_name(config, testfile, "debugger.script");
555 // FIXME (#9639): This needs to handle non-utf8 paths
557 vec!("-quiet".to_owned(),
560 format!("-command={}", debugger_script.to_str().unwrap()));
562 let proc_args = ProcArgs {
563 prog: debugger().to_owned(),
567 let environment = vec![("PYTHONPATH".to_owned(), rust_pp_module_abs_path)];
569 debugger_run_result = compose_and_run(config,
573 &config.run_lib_path,
579 if !debugger_run_result.status.success() {
580 fatal("gdb failed to execute");
583 check_debugger_output(&debugger_run_result, &check_lines);
586 fn find_rust_src_root(config: &Config) -> Option<PathBuf> {
587 let mut path = config.src_base.clone();
588 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
591 if path.join(&path_postfix).is_file() {
599 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
600 if config.lldb_python_dir.is_none() {
601 fatal("Can't run LLDB test because LLDB's python path is not set.");
604 let mut config = Config {
605 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
606 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
610 let config = &mut config;
612 // compile test file (it should have 'compile-flags:-g' in the header)
613 let compile_result = compile_test(config, props, testfile);
614 if !compile_result.status.success() {
615 fatal_proc_rec("compilation failed!", &compile_result);
618 let exe_file = make_exe_name(config, testfile);
620 match config.lldb_version {
621 Some(ref version) => {
622 println!("NOTE: compiletest thinks it is using LLDB version {}",
626 println!("NOTE: compiletest does not know which version of \
631 // Parse debugger commands etc from test files
632 let DebuggerCommands {
637 } = parse_debugger_commands(testfile, "lldb");
639 // Write debugger script:
640 // We don't want to hang when calling `quit` while the process is still running
641 let mut script_str = String::from("settings set auto-confirm true\n");
643 // Make LLDB emit its version, so we have it documented in the test output
644 script_str.push_str("version\n");
646 // Switch LLDB into "Rust mode"
647 let rust_src_root = find_rust_src_root(config)
648 .expect("Could not find Rust source root");
649 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
650 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
655 script_str.push_str(&format!("command script import {}\n",
656 &rust_pp_module_abs_path[..])[..]);
657 script_str.push_str("type summary add --no-value ");
658 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
659 script_str.push_str("-x \".*\" --category Rust\n");
660 script_str.push_str("type category enable Rust\n");
662 // Set breakpoints on every line that contains the string "#break"
663 for line in &breakpoint_lines {
664 script_str.push_str(&format!("breakpoint set --line {}\n", line));
667 // Append the other commands
668 for line in &commands {
669 script_str.push_str(line);
670 script_str.push_str("\n");
673 // Finally, quit the debugger
674 script_str.push_str("\nquit\n");
676 // Write the script into a file
677 debug!("script_str = {}", script_str);
678 dump_output_file(config,
682 let debugger_script = make_out_name(config, testfile, "debugger.script");
684 // Let LLDB execute the script via lldb_batchmode.py
685 let debugger_run_result = run_lldb(config,
690 if !debugger_run_result.status.success() {
691 fatal_proc_rec("Error while running LLDB", &debugger_run_result);
694 check_debugger_output(&debugger_run_result, &check_lines);
696 fn run_lldb(config: &Config,
697 test_executable: &Path,
698 debugger_script: &Path,
699 rust_src_root: &Path)
701 // Prepare the lldb_batchmode which executes the debugger script
702 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
705 Command::new(&config.python)
706 .arg(&lldb_script_path)
707 .arg(test_executable)
708 .arg(debugger_script)
710 config.lldb_python_dir.as_ref().unwrap()))
714 fn cmd2procres(config: &Config, test_executable: &Path, cmd: &mut Command)
716 let (status, out, err) = match cmd.output() {
717 Ok(Output { status, stdout, stderr }) => {
719 String::from_utf8(stdout).unwrap(),
720 String::from_utf8(stderr).unwrap())
723 fatal(&format!("Failed to setup Python process for \
724 LLDB script: {}", e))
728 dump_output(config, test_executable, &out, &err);
730 status: Status::Normal(status),
733 cmdline: format!("{:?}", cmd)
737 struct DebuggerCommands {
738 commands: Vec<String>,
739 check_lines: Vec<String>,
740 breakpoint_lines: Vec<usize>,
743 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
744 -> DebuggerCommands {
745 let command_directive = format!("{}-command", debugger_prefix);
746 let check_directive = format!("{}-check", debugger_prefix);
748 let mut breakpoint_lines = vec!();
749 let mut commands = vec!();
750 let mut check_lines = vec!();
752 let reader = BufReader::new(File::open(file_path).unwrap());
753 for line in reader.lines() {
756 if line.contains("#break") {
757 breakpoint_lines.push(counter);
760 header::parse_name_value_directive(
762 &command_directive).map(|cmd| {
766 header::parse_name_value_directive(
768 &check_directive).map(|cmd| {
769 check_lines.push(cmd)
773 fatal(&format!("Error while parsing debugger commands: {}", e))
781 check_lines: check_lines,
782 breakpoint_lines: breakpoint_lines,
786 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
787 if options.is_none() {
791 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
792 let options_to_remove = [
795 "--debuginfo".to_owned()
798 split_maybe_args(options).into_iter()
799 .filter(|x| !options_to_remove.contains(x))
800 .collect::<Vec<String>>()
805 fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String]) {
806 let num_check_lines = check_lines.len();
807 if num_check_lines > 0 {
808 // Allow check lines to leave parts unspecified (e.g., uninitialized
809 // bits in the wrong case of an enum) with the notation "[...]".
810 let check_fragments: Vec<Vec<String>> =
811 check_lines.iter().map(|s| {
818 // check if each line in props.check_lines appears in the
821 for line in debugger_run_result.stdout.lines() {
822 let mut rest = line.trim();
823 let mut first = true;
824 let mut failed = false;
825 for frag in &check_fragments[i] {
826 let found = if first {
827 if rest.starts_with(frag) {
841 rest = &rest[(i + frag.len())..];
846 if !failed && rest.is_empty() {
849 if i == num_check_lines {
854 if i != num_check_lines {
855 fatal_proc_rec(&format!("line not found in debugger output: {}",
856 check_lines.get(i).unwrap()),
857 debugger_run_result);
862 fn check_error_patterns(props: &TestProps,
864 output_to_check: &str,
865 proc_res: &ProcRes) {
866 if props.error_patterns.is_empty() {
867 fatal(&format!("no error pattern specified in {:?}", testfile.display()));
869 let mut next_err_idx = 0;
870 let mut next_err_pat = &props.error_patterns[next_err_idx];
871 let mut done = false;
872 for line in output_to_check.lines() {
873 if line.contains(next_err_pat) {
874 debug!("found error pattern {}", next_err_pat);
876 if next_err_idx == props.error_patterns.len() {
877 debug!("found all error patterns");
881 next_err_pat = &props.error_patterns[next_err_idx];
886 let missing_patterns = &props.error_patterns[next_err_idx..];
887 if missing_patterns.len() == 1 {
888 fatal_proc_rec(&format!("error pattern '{}' not found!", missing_patterns[0]),
891 for pattern in missing_patterns {
892 error(&format!("error pattern '{}' not found!", *pattern));
894 fatal_proc_rec("multiple error patterns not found", proc_res);
898 fn check_no_compiler_crash(proc_res: &ProcRes) {
899 for line in proc_res.stderr.lines() {
900 if line.starts_with("error: internal compiler error:") {
901 fatal_proc_rec("compiler encountered internal error",
907 fn check_forbid_output(props: &TestProps,
908 output_to_check: &str,
909 proc_res: &ProcRes) {
910 for pat in &props.forbid_output {
911 if output_to_check.contains(pat) {
912 fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
917 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError>,
919 proc_res: &ProcRes) {
921 // true if we found the error in question
922 let mut found_flags = vec![false; expected_errors.len()];
924 if proc_res.status.success() {
925 fatal("process did not return an error status");
928 let prefixes = expected_errors.iter().map(|ee| {
929 format!("{}:{}:", testfile.display(), ee.line)
930 }).collect::<Vec<String>>();
932 fn prefix_matches(line: &str, prefix: &str) -> bool {
933 use std::ascii::AsciiExt;
934 // On windows just translate all '\' path separators to '/'
935 let line = line.replace(r"\", "/");
937 line.to_ascii_lowercase().starts_with(&prefix.to_ascii_lowercase())
939 line.starts_with(prefix)
943 // A multi-line error will have followup lines which start with a space
945 fn continuation( line: &str) -> bool {
946 line.starts_with(" ") || line.starts_with("(")
949 // Scan and extract our error/warning messages,
951 // filename:line1:col1: line2:col2: *error:* msg
952 // filename:line1:col1: line2:col2: *warning:* msg
953 // where line1:col1: is the starting point, line2:col2:
954 // is the ending point, and * represents ANSI color codes.
956 // This pattern is ambiguous on windows, because filename may contain
957 // a colon, so any path prefix must be detected and removed first.
958 for line in proc_res.stderr.lines() {
959 let mut was_expected = false;
961 for (i, ee) in expected_errors.iter().enumerate() {
963 debug!("prefix={} ee.kind={} ee.msg={} line={}",
968 // Suggestions have no line number in their output, so take on the line number of
969 // the previous expected error
970 if ee.kind == "suggestion" {
971 assert!(expected_errors[prev].kind == "help",
972 "SUGGESTIONs must be preceded by a HELP");
973 if line.contains(&ee.msg) {
974 found_flags[i] = true;
979 if (prefix_matches(line, &prefixes[i]) || continuation(line)) &&
980 line.contains(&ee.kind) &&
981 line.contains(&ee.msg) {
982 found_flags[i] = true;
990 // ignore this msg which gets printed at the end
991 if line.contains("aborting due to") {
995 if !was_expected && is_compiler_error_or_warning(line) {
996 fatal_proc_rec(&format!("unexpected compiler error or warning: '{}'",
1002 for (i, &flag) in found_flags.iter().enumerate() {
1004 let ee = &expected_errors[i];
1005 fatal_proc_rec(&format!("expected {} on line {} not found: {}",
1006 ee.kind, ee.line, ee.msg),
1012 fn is_compiler_error_or_warning(mut line: &str) -> bool {
1013 // Remove initial prefix which may contain a colon
1014 let mut components = Path::new(line).components();
1015 if let Some(Component::Prefix(_)) = components.peek() {
1019 // Safe as path was originally constructed from a &str ^
1020 line = components.as_path().to_str().unwrap();
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 usize) -> 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 usize) -> bool {
1052 if *idx >= haystack.len() {
1055 let ch = haystack.char_at(*idx);
1059 *idx += ch.len_utf8();
1063 fn scan_integer(haystack: &str, idx: &mut usize) -> bool {
1065 while i < haystack.len() {
1066 let ch = haystack.char_at(i);
1067 if ch < '0' || '9' < ch {
1079 fn scan_string(haystack: &str, needle: &str, idx: &mut usize) -> 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 ch = haystack.char_at(haystack_i);
1087 haystack_i += ch.len_utf8();
1088 if !scan_char(needle, 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 let aux_dir = aux_output_dir_name(config, testfile);
1141 // FIXME (#9639): This needs to handle non-utf8 paths
1142 let link_args = vec!("-L".to_owned(),
1143 aux_dir.to_str().unwrap().to_owned());
1144 let args = make_compile_args(config,
1147 |a, b| TargetLocation::ThisFile(make_exe_name(a, b)), testfile);
1148 compose_and_run_compiler(config, props, testfile, args, None)
1151 fn document(config: &Config, props: &TestProps,
1152 testfile: &Path) -> (ProcRes, PathBuf) {
1153 let aux_dir = aux_output_dir_name(config, testfile);
1154 let out_dir = output_base_name(config, testfile);
1155 let _ = fs::remove_dir_all(&out_dir);
1156 ensure_dir(&out_dir);
1157 let mut args = vec!["-L".to_owned(),
1158 aux_dir.to_str().unwrap().to_owned(),
1160 out_dir.to_str().unwrap().to_owned(),
1161 testfile.to_str().unwrap().to_owned()];
1162 args.extend(split_maybe_args(&props.compile_flags));
1163 let args = ProcArgs {
1164 prog: config.rustdoc_path.to_str().unwrap().to_owned(),
1167 (compose_and_run_compiler(config, props, testfile, args, None), out_dir)
1170 fn exec_compiled_test(config: &Config, props: &TestProps,
1171 testfile: &Path) -> ProcRes {
1173 let env = props.exec_env.clone();
1175 match &*config.target {
1177 "arm-linux-androideabi" | "aarch64-linux-android" => {
1178 _arm_exec_compiled_test(config, props, testfile, env)
1182 let aux_dir = aux_output_dir_name(config, testfile);
1183 compose_and_run(config,
1185 make_run_args(config, props, testfile),
1187 &config.run_lib_path,
1188 Some(aux_dir.to_str().unwrap()),
1194 fn compose_and_run_compiler(config: &Config, props: &TestProps,
1195 testfile: &Path, args: ProcArgs,
1196 input: Option<String>) -> ProcRes {
1197 if !props.aux_builds.is_empty() {
1198 ensure_dir(&aux_output_dir_name(config, testfile));
1201 let aux_dir = aux_output_dir_name(config, testfile);
1202 // FIXME (#9639): This needs to handle non-utf8 paths
1203 let extra_link_args = vec!["-L".to_owned(),
1204 aux_dir.to_str().unwrap().to_owned()];
1206 for rel_ab in &props.aux_builds {
1207 let abs_ab = config.aux_base.join(rel_ab);
1208 let aux_props = header::load_props(&abs_ab);
1209 let mut crate_type = if aux_props.no_prefer_dynamic {
1212 // We primarily compile all auxiliary libraries as dynamic libraries
1213 // to avoid code size bloat and large binaries as much as possible
1214 // for the test suite (otherwise including libstd statically in all
1215 // executables takes up quite a bit of space).
1217 // For targets like MUSL, however, there is no support for dynamic
1218 // libraries so we just go back to building a normal library. Note,
1219 // however, that if the library is built with `force_host` then it's
1220 // ok to be a dylib as the host should always support dylibs.
1221 if config.target.contains("musl") && !aux_props.force_host {
1222 vec!("--crate-type=lib".to_owned())
1224 vec!("--crate-type=dylib".to_owned())
1227 crate_type.extend(extra_link_args.clone());
1229 make_compile_args(config,
1233 let f = make_lib_name(a, b, testfile);
1234 let parent = f.parent().unwrap();
1235 TargetLocation::ThisDirectory(parent.to_path_buf())
1238 let auxres = compose_and_run(config,
1242 &config.compile_lib_path,
1243 Some(aux_dir.to_str().unwrap()),
1245 if !auxres.status.success() {
1247 &format!("auxiliary build of {:?} failed to compile: ",
1252 match &*config.target {
1253 "arm-linux-androideabi" | "aarch64-linux-android" => {
1254 _arm_push_aux_shared_library(config, testfile);
1260 compose_and_run(config,
1264 &config.compile_lib_path,
1265 Some(aux_dir.to_str().unwrap()),
1269 fn ensure_dir(path: &Path) {
1270 if path.is_dir() { return; }
1271 fs::create_dir(path).unwrap();
1274 fn compose_and_run(config: &Config, testfile: &Path,
1275 ProcArgs{ args, prog }: ProcArgs,
1276 procenv: Vec<(String, String)> ,
1278 aux_path: Option<&str>,
1279 input: Option<String>) -> ProcRes {
1280 return program_output(config, testfile, lib_path,
1281 prog, aux_path, args, procenv, input);
1284 enum TargetLocation {
1286 ThisDirectory(PathBuf),
1289 fn make_compile_args<F>(config: &Config,
1291 extras: Vec<String> ,
1295 F: FnOnce(&Config, &Path) -> TargetLocation,
1297 let xform_file = xform(config, testfile);
1298 let target = if props.force_host {
1303 // FIXME (#9639): This needs to handle non-utf8 paths
1304 let mut args = vec!(testfile.to_str().unwrap().to_owned(),
1306 config.build_base.to_str().unwrap().to_owned(),
1307 format!("--target={}", target));
1308 args.push_all(&extras);
1309 if !props.no_prefer_dynamic {
1310 args.push("-C".to_owned());
1311 args.push("prefer-dynamic".to_owned());
1313 let path = match xform_file {
1314 TargetLocation::ThisFile(path) => {
1315 args.push("-o".to_owned());
1318 TargetLocation::ThisDirectory(path) => {
1319 args.push("--out-dir".to_owned());
1323 args.push(path.to_str().unwrap().to_owned());
1324 if props.force_host {
1325 args.extend(split_maybe_args(&config.host_rustcflags));
1327 args.extend(split_maybe_args(&config.target_rustcflags));
1329 args.extend(split_maybe_args(&props.compile_flags));
1331 prog: config.rustc_path.to_str().unwrap().to_owned(),
1336 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> PathBuf {
1337 // what we return here is not particularly important, as it
1338 // happens; rustc ignores everything except for the directory.
1339 let auxname = output_testname(auxfile);
1340 aux_output_dir_name(config, testfile).join(&auxname)
1343 fn make_exe_name(config: &Config, testfile: &Path) -> PathBuf {
1344 let mut f = output_base_name(config, testfile);
1345 if !env::consts::EXE_SUFFIX.is_empty() {
1346 let mut fname = f.file_name().unwrap().to_os_string();
1347 fname.push(env::consts::EXE_SUFFIX);
1348 f.set_file_name(&fname);
1353 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path)
1355 // If we've got another tool to run under (valgrind),
1356 // then split apart its command
1357 let mut args = split_maybe_args(&config.runtool);
1358 let exe_file = make_exe_name(config, testfile);
1360 // FIXME (#9639): This needs to handle non-utf8 paths
1361 args.push(exe_file.to_str().unwrap().to_owned());
1363 // Add the arguments in the run_flags directive
1364 args.extend(split_maybe_args(&props.run_flags));
1366 let prog = args.remove(0);
1373 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1379 if s.chars().all(|c| c.is_whitespace()) {
1390 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1391 aux_path: Option<&str>, args: Vec<String>,
1392 env: Vec<(String, String)>,
1393 input: Option<String>) -> ProcRes {
1396 let cmdline = make_cmdline(lib_path,
1399 logv(config, format!("executing {}", cmdline));
1402 let procsrv::Result {
1406 } = procsrv::run(lib_path,
1411 input).expect(&format!("failed to exec `{}`", prog));
1412 dump_output(config, testfile, &out, &err);
1414 status: Status::Normal(status),
1421 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1424 // Linux and mac don't require adjusting the library search path
1426 format!("{} {}", prog, args.join(" "))
1428 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1429 // for diagnostic purposes
1430 fn lib_path_cmd_prefix(path: &str) -> String {
1431 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1434 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1438 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1439 dump_output_file(config, testfile, out, "out");
1440 dump_output_file(config, testfile, err, "err");
1441 maybe_dump_to_stdout(config, out, err);
1444 fn dump_output_file(config: &Config, testfile: &Path,
1445 out: &str, extension: &str) {
1446 let outfile = make_out_name(config, testfile, extension);
1447 File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1450 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> PathBuf {
1451 output_base_name(config, testfile).with_extension(extension)
1454 fn aux_output_dir_name(config: &Config, testfile: &Path) -> PathBuf {
1455 let f = output_base_name(config, testfile);
1456 let mut fname = f.file_name().unwrap().to_os_string();
1457 fname.push(&format!(".{}.libaux", config.mode));
1458 f.with_file_name(&fname)
1461 fn output_testname(testfile: &Path) -> PathBuf {
1462 PathBuf::from(testfile.file_stem().unwrap())
1465 fn output_base_name(config: &Config, testfile: &Path) -> PathBuf {
1467 .join(&output_testname(testfile))
1468 .with_extension(&config.stage_id)
1471 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1473 println!("------{}------------------------------", "stdout");
1474 println!("{}", out);
1475 println!("------{}------------------------------", "stderr");
1476 println!("{}", err);
1477 println!("------------------------------------------");
1481 fn error(err: &str) { println!("\nerror: {}", err); }
1483 fn fatal(err: &str) -> ! { error(err); panic!(); }
1485 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1491 ------------------------------------------\n\
1493 ------------------------------------------\n\
1495 ------------------------------------------\n\
1497 ------------------------------------------\n\
1499 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1504 fn _arm_exec_compiled_test(config: &Config,
1507 env: Vec<(String, String)>)
1509 let args = make_run_args(config, props, testfile);
1510 let cmdline = make_cmdline("",
1514 // get bare program string
1515 let mut tvec: Vec<String> = args.prog
1519 let prog_short = tvec.pop().unwrap();
1522 let copy_result = procsrv::run("",
1528 config.adb_test_dir.clone()
1530 vec!(("".to_owned(), "".to_owned())),
1531 Some("".to_owned()))
1532 .expect(&format!("failed to exec `{}`", config.adb_path));
1535 println!("push ({}) {} {} {}",
1542 logv(config, format!("executing ({}) {}", config.target, cmdline));
1544 let mut runargs = Vec::new();
1546 // run test via adb_run_wrapper
1547 runargs.push("shell".to_owned());
1548 for (key, val) in env {
1549 runargs.push(format!("{}={}", key, val));
1551 runargs.push(format!("{}/../adb_run_wrapper.sh", config.adb_test_dir));
1552 runargs.push(format!("{}", config.adb_test_dir));
1553 runargs.push(format!("{}", prog_short));
1555 for tv in &args.args {
1556 runargs.push(tv.to_owned());
1562 vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1563 .expect(&format!("failed to exec `{}`", config.adb_path));
1565 // get exitcode of result
1566 runargs = Vec::new();
1567 runargs.push("shell".to_owned());
1568 runargs.push("cat".to_owned());
1569 runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1571 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1576 vec!(("".to_owned(), "".to_owned())),
1577 Some("".to_owned()))
1578 .expect(&format!("failed to exec `{}`", config.adb_path));
1580 let mut exitcode: i32 = 0;
1581 for c in exitcode_out.chars() {
1582 if !c.is_numeric() { break; }
1583 exitcode = exitcode * 10 + match c {
1584 '0' ... '9' => c as i32 - ('0' as i32),
1589 // get stdout of result
1590 runargs = Vec::new();
1591 runargs.push("shell".to_owned());
1592 runargs.push("cat".to_owned());
1593 runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1595 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1600 vec!(("".to_owned(), "".to_owned())),
1601 Some("".to_owned()))
1602 .expect(&format!("failed to exec `{}`", config.adb_path));
1604 // get stderr of result
1605 runargs = Vec::new();
1606 runargs.push("shell".to_owned());
1607 runargs.push("cat".to_owned());
1608 runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1610 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1615 vec!(("".to_owned(), "".to_owned())),
1616 Some("".to_owned()))
1617 .expect(&format!("failed to exec `{}`", config.adb_path));
1625 status: Status::Parsed(exitcode),
1632 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1633 let tdir = aux_output_dir_name(config, testfile);
1635 let dirs = fs::read_dir(&tdir).unwrap();
1637 let file = file.unwrap().path();
1638 if file.extension().and_then(|s| s.to_str()) == Some("so") {
1639 // FIXME (#9639): This needs to handle non-utf8 paths
1640 let copy_result = procsrv::run("",
1648 config.adb_test_dir.to_owned(),
1650 vec!(("".to_owned(),
1652 Some("".to_owned()))
1653 .expect(&format!("failed to exec `{}`", config.adb_path));
1656 println!("push ({}) {:?} {} {}",
1657 config.target, file.display(),
1658 copy_result.out, copy_result.err);
1664 // codegen tests (using FileCheck)
1666 fn compile_test_and_save_ir(config: &Config, props: &TestProps,
1667 testfile: &Path) -> ProcRes {
1668 let aux_dir = aux_output_dir_name(config, testfile);
1669 // FIXME (#9639): This needs to handle non-utf8 paths
1670 let mut link_args = vec!("-L".to_owned(),
1671 aux_dir.to_str().unwrap().to_owned());
1672 let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1673 link_args.extend(llvm_args);
1674 let args = make_compile_args(config,
1677 |a, b| TargetLocation::ThisDirectory(
1678 output_base_name(a, b).parent()
1679 .unwrap().to_path_buf()),
1681 compose_and_run_compiler(config, props, testfile, args, None)
1684 fn check_ir_with_filecheck(config: &Config, testfile: &Path) -> ProcRes {
1685 let irfile = output_base_name(config, testfile).with_extension("ll");
1686 let prog = config.llvm_bin_path.as_ref().unwrap().join("FileCheck");
1687 let proc_args = ProcArgs {
1688 // FIXME (#9639): This needs to handle non-utf8 paths
1689 prog: prog.to_str().unwrap().to_owned(),
1690 args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1691 testfile.to_str().unwrap().to_owned())
1693 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1696 fn run_codegen_test(config: &Config, props: &TestProps, testfile: &Path) {
1698 if config.llvm_bin_path.is_none() {
1699 fatal("missing --llvm-bin-path");
1702 let mut proc_res = compile_test_and_save_ir(config, props, testfile);
1703 if !proc_res.status.success() {
1704 fatal_proc_rec("compilation failed!", &proc_res);
1707 proc_res = check_ir_with_filecheck(config, testfile);
1708 if !proc_res.status.success() {
1709 fatal_proc_rec("verification with 'FileCheck' failed",
1714 fn charset() -> &'static str {
1715 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1716 if cfg!(target_os = "bitrig") {
1718 } else if cfg!(target_os = "freebsd") {
1725 fn run_rustdoc_test(config: &Config, props: &TestProps, testfile: &Path) {
1726 let (proc_res, out_dir) = document(config, props, testfile);
1727 if !proc_res.status.success() {
1728 fatal_proc_rec("rustdoc failed!", &proc_res);
1730 let root = find_rust_src_root(config).unwrap();
1732 let res = cmd2procres(config,
1734 Command::new(&config.python)
1735 .arg(root.join("src/etc/htmldocck.py"))
1738 if !res.status.success() {
1739 fatal_proc_rec("htmldocck failed!", &res);