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.
12 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
13 use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc, CodegenUnits};
15 use header::TestProps;
21 use std::collections::HashSet;
23 use std::fs::{self, File};
24 use std::io::BufReader;
25 use std::io::prelude::*;
26 use std::net::TcpStream;
27 use std::path::{Path, PathBuf, Component};
28 use std::process::{Command, Output, ExitStatus};
30 pub fn run(config: Config, testfile: &Path) {
31 match &*config.target {
33 "arm-linux-androideabi" | "aarch64-linux-android" => {
34 if !config.adb_device_status {
35 panic!("android device not available");
43 // We're going to be dumping a lot of info. Start on a new line.
46 debug!("running {:?}", testfile.display());
47 let props = header::load_props(&testfile);
48 debug!("loaded props");
50 CompileFail => run_cfail_test(&config, &props, &testfile),
51 ParseFail => run_cfail_test(&config, &props, &testfile),
52 RunFail => run_rfail_test(&config, &props, &testfile),
53 RunPass => run_rpass_test(&config, &props, &testfile),
54 RunPassValgrind => run_valgrind_test(&config, &props, &testfile),
55 Pretty => run_pretty_test(&config, &props, &testfile),
56 DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
57 DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
58 Codegen => run_codegen_test(&config, &props, &testfile),
59 Rustdoc => run_rustdoc_test(&config, &props, &testfile),
60 CodegenUnits => run_codegen_units_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(::std::time::Duration::new(1,0));
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(line: &str) -> bool {
1013 let mut c = Path::new(line).components();
1014 let line = match c.next() {
1015 Some(Component::Prefix(_)) => c.as_path().to_str().unwrap(),
1021 scan_until_char(line, ':', &mut i) &&
1022 scan_char(line, ':', &mut i) &&
1023 scan_integer(line, &mut i) &&
1024 scan_char(line, ':', &mut i) &&
1025 scan_integer(line, &mut i) &&
1026 scan_char(line, ':', &mut i) &&
1027 scan_char(line, ' ', &mut i) &&
1028 scan_integer(line, &mut i) &&
1029 scan_char(line, ':', &mut i) &&
1030 scan_integer(line, &mut i) &&
1031 scan_char(line, ' ', &mut i) &&
1032 (scan_string(line, "error", &mut i) ||
1033 scan_string(line, "warning", &mut i));
1036 fn scan_until_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1037 if *idx >= haystack.len() {
1040 let opt = haystack[(*idx)..].find(needle);
1044 *idx = opt.unwrap();
1048 fn scan_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1049 if *idx >= haystack.len() {
1052 let ch = haystack.char_at(*idx);
1056 *idx += ch.len_utf8();
1060 fn scan_integer(haystack: &str, idx: &mut usize) -> bool {
1062 while i < haystack.len() {
1063 let ch = haystack.char_at(i);
1064 if ch < '0' || '9' < ch {
1076 fn scan_string(haystack: &str, needle: &str, idx: &mut usize) -> bool {
1077 let mut haystack_i = *idx;
1078 let mut needle_i = 0;
1079 while needle_i < needle.len() {
1080 if haystack_i >= haystack.len() {
1083 let ch = haystack.char_at(haystack_i);
1084 haystack_i += ch.len_utf8();
1085 if !scan_char(needle, ch, &mut needle_i) {
1111 fn code(&self) -> Option<i32> {
1113 Status::Parsed(i) => Some(i),
1114 Status::Normal(ref e) => e.code(),
1118 fn success(&self) -> bool {
1120 Status::Parsed(i) => i == 0,
1121 Status::Normal(ref e) => e.success(),
1126 impl fmt::Display for Status {
1127 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1129 Status::Parsed(i) => write!(f, "exit code: {}", i),
1130 Status::Normal(ref e) => e.fmt(f),
1135 fn compile_test(config: &Config, props: &TestProps,
1136 testfile: &Path) -> ProcRes {
1137 let aux_dir = aux_output_dir_name(config, testfile);
1138 // FIXME (#9639): This needs to handle non-utf8 paths
1139 let link_args = vec!("-L".to_owned(),
1140 aux_dir.to_str().unwrap().to_owned());
1141 let args = make_compile_args(config,
1144 |a, b| TargetLocation::ThisFile(make_exe_name(a, b)), testfile);
1145 compose_and_run_compiler(config, props, testfile, args, None)
1148 fn document(config: &Config, props: &TestProps,
1149 testfile: &Path, out_dir: &Path) -> ProcRes {
1150 if props.build_aux_docs {
1151 for rel_ab in &props.aux_builds {
1152 let abs_ab = config.aux_base.join(rel_ab);
1153 let aux_props = header::load_props(&abs_ab);
1155 let auxres = document(config, &aux_props, &abs_ab, out_dir);
1156 if !auxres.status.success() {
1162 let aux_dir = aux_output_dir_name(config, testfile);
1163 let mut args = vec!["-L".to_owned(),
1164 aux_dir.to_str().unwrap().to_owned(),
1166 out_dir.to_str().unwrap().to_owned(),
1167 testfile.to_str().unwrap().to_owned()];
1168 args.extend(split_maybe_args(&props.compile_flags));
1169 let args = ProcArgs {
1170 prog: config.rustdoc_path.to_str().unwrap().to_owned(),
1173 compose_and_run_compiler(config, props, testfile, args, None)
1176 fn exec_compiled_test(config: &Config, props: &TestProps,
1177 testfile: &Path) -> ProcRes {
1179 let env = props.exec_env.clone();
1181 match &*config.target {
1183 "arm-linux-androideabi" | "aarch64-linux-android" => {
1184 _arm_exec_compiled_test(config, props, testfile, env)
1188 let aux_dir = aux_output_dir_name(config, testfile);
1189 compose_and_run(config,
1191 make_run_args(config, props, testfile),
1193 &config.run_lib_path,
1194 Some(aux_dir.to_str().unwrap()),
1200 fn compose_and_run_compiler(config: &Config, props: &TestProps,
1201 testfile: &Path, args: ProcArgs,
1202 input: Option<String>) -> ProcRes {
1203 if !props.aux_builds.is_empty() {
1204 ensure_dir(&aux_output_dir_name(config, testfile));
1207 let aux_dir = aux_output_dir_name(config, testfile);
1208 // FIXME (#9639): This needs to handle non-utf8 paths
1209 let extra_link_args = vec!["-L".to_owned(),
1210 aux_dir.to_str().unwrap().to_owned()];
1212 for rel_ab in &props.aux_builds {
1213 let abs_ab = config.aux_base.join(rel_ab);
1214 let aux_props = header::load_props(&abs_ab);
1215 let mut crate_type = if aux_props.no_prefer_dynamic {
1218 // We primarily compile all auxiliary libraries as dynamic libraries
1219 // to avoid code size bloat and large binaries as much as possible
1220 // for the test suite (otherwise including libstd statically in all
1221 // executables takes up quite a bit of space).
1223 // For targets like MUSL, however, there is no support for dynamic
1224 // libraries so we just go back to building a normal library. Note,
1225 // however, that if the library is built with `force_host` then it's
1226 // ok to be a dylib as the host should always support dylibs.
1227 if config.target.contains("musl") && !aux_props.force_host {
1228 vec!("--crate-type=lib".to_owned())
1230 vec!("--crate-type=dylib".to_owned())
1233 crate_type.extend(extra_link_args.clone());
1235 make_compile_args(config,
1239 let f = make_lib_name(a, b, testfile);
1240 let parent = f.parent().unwrap();
1241 TargetLocation::ThisDirectory(parent.to_path_buf())
1244 let auxres = compose_and_run(config,
1248 &config.compile_lib_path,
1249 Some(aux_dir.to_str().unwrap()),
1251 if !auxres.status.success() {
1253 &format!("auxiliary build of {:?} failed to compile: ",
1258 match &*config.target {
1259 "arm-linux-androideabi" | "aarch64-linux-android" => {
1260 _arm_push_aux_shared_library(config, testfile);
1266 compose_and_run(config,
1270 &config.compile_lib_path,
1271 Some(aux_dir.to_str().unwrap()),
1275 fn ensure_dir(path: &Path) {
1276 if path.is_dir() { return; }
1277 fs::create_dir(path).unwrap();
1280 fn compose_and_run(config: &Config, testfile: &Path,
1281 ProcArgs{ args, prog }: ProcArgs,
1282 procenv: Vec<(String, String)> ,
1284 aux_path: Option<&str>,
1285 input: Option<String>) -> ProcRes {
1286 return program_output(config, testfile, lib_path,
1287 prog, aux_path, args, procenv, input);
1290 enum TargetLocation {
1292 ThisDirectory(PathBuf),
1295 fn make_compile_args<F>(config: &Config,
1297 extras: Vec<String> ,
1301 F: FnOnce(&Config, &Path) -> TargetLocation,
1303 let xform_file = xform(config, testfile);
1304 let target = if props.force_host {
1309 // FIXME (#9639): This needs to handle non-utf8 paths
1310 let mut args = vec!(testfile.to_str().unwrap().to_owned(),
1312 config.build_base.to_str().unwrap().to_owned(),
1313 format!("--target={}", target));
1314 args.extend_from_slice(&extras);
1315 if !props.no_prefer_dynamic {
1316 args.push("-C".to_owned());
1317 args.push("prefer-dynamic".to_owned());
1319 let path = match xform_file {
1320 TargetLocation::ThisFile(path) => {
1321 args.push("-o".to_owned());
1324 TargetLocation::ThisDirectory(path) => {
1325 args.push("--out-dir".to_owned());
1329 args.push(path.to_str().unwrap().to_owned());
1330 if props.force_host {
1331 args.extend(split_maybe_args(&config.host_rustcflags));
1333 args.extend(split_maybe_args(&config.target_rustcflags));
1335 args.extend(split_maybe_args(&props.compile_flags));
1337 prog: config.rustc_path.to_str().unwrap().to_owned(),
1342 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> PathBuf {
1343 // what we return here is not particularly important, as it
1344 // happens; rustc ignores everything except for the directory.
1345 let auxname = output_testname(auxfile);
1346 aux_output_dir_name(config, testfile).join(&auxname)
1349 fn make_exe_name(config: &Config, testfile: &Path) -> PathBuf {
1350 let mut f = output_base_name(config, testfile);
1351 if !env::consts::EXE_SUFFIX.is_empty() {
1352 let mut fname = f.file_name().unwrap().to_os_string();
1353 fname.push(env::consts::EXE_SUFFIX);
1354 f.set_file_name(&fname);
1359 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path)
1361 // If we've got another tool to run under (valgrind),
1362 // then split apart its command
1363 let mut args = split_maybe_args(&config.runtool);
1364 let exe_file = make_exe_name(config, testfile);
1366 // FIXME (#9639): This needs to handle non-utf8 paths
1367 args.push(exe_file.to_str().unwrap().to_owned());
1369 // Add the arguments in the run_flags directive
1370 args.extend(split_maybe_args(&props.run_flags));
1372 let prog = args.remove(0);
1379 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1385 if s.chars().all(|c| c.is_whitespace()) {
1396 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1397 aux_path: Option<&str>, args: Vec<String>,
1398 env: Vec<(String, String)>,
1399 input: Option<String>) -> ProcRes {
1402 let cmdline = make_cmdline(lib_path,
1405 logv(config, format!("executing {}", cmdline));
1408 let procsrv::Result {
1412 } = procsrv::run(lib_path,
1417 input).expect(&format!("failed to exec `{}`", prog));
1418 dump_output(config, testfile, &out, &err);
1420 status: Status::Normal(status),
1427 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1430 // Linux and mac don't require adjusting the library search path
1432 format!("{} {}", prog, args.join(" "))
1434 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1435 // for diagnostic purposes
1436 fn lib_path_cmd_prefix(path: &str) -> String {
1437 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1440 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1444 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1445 dump_output_file(config, testfile, out, "out");
1446 dump_output_file(config, testfile, err, "err");
1447 maybe_dump_to_stdout(config, out, err);
1450 fn dump_output_file(config: &Config, testfile: &Path,
1451 out: &str, extension: &str) {
1452 let outfile = make_out_name(config, testfile, extension);
1453 File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1456 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> PathBuf {
1457 output_base_name(config, testfile).with_extension(extension)
1460 fn aux_output_dir_name(config: &Config, testfile: &Path) -> PathBuf {
1461 let f = output_base_name(config, testfile);
1462 let mut fname = f.file_name().unwrap().to_os_string();
1463 fname.push(&format!(".{}.libaux", config.mode));
1464 f.with_file_name(&fname)
1467 fn output_testname(testfile: &Path) -> PathBuf {
1468 PathBuf::from(testfile.file_stem().unwrap())
1471 fn output_base_name(config: &Config, testfile: &Path) -> PathBuf {
1473 .join(&output_testname(testfile))
1474 .with_extension(&config.stage_id)
1477 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1479 println!("------{}------------------------------", "stdout");
1480 println!("{}", out);
1481 println!("------{}------------------------------", "stderr");
1482 println!("{}", err);
1483 println!("------------------------------------------");
1487 fn error(err: &str) { println!("\nerror: {}", err); }
1489 fn fatal(err: &str) -> ! { error(err); panic!(); }
1491 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1497 ------------------------------------------\n\
1499 ------------------------------------------\n\
1501 ------------------------------------------\n\
1503 ------------------------------------------\n\
1505 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1510 fn _arm_exec_compiled_test(config: &Config,
1513 env: Vec<(String, String)>)
1515 let args = make_run_args(config, props, testfile);
1516 let cmdline = make_cmdline("",
1520 // get bare program string
1521 let mut tvec: Vec<String> = args.prog
1525 let prog_short = tvec.pop().unwrap();
1528 let copy_result = procsrv::run("",
1534 config.adb_test_dir.clone()
1536 vec!(("".to_owned(), "".to_owned())),
1537 Some("".to_owned()))
1538 .expect(&format!("failed to exec `{}`", config.adb_path));
1541 println!("push ({}) {} {} {}",
1548 logv(config, format!("executing ({}) {}", config.target, cmdline));
1550 let mut runargs = Vec::new();
1552 // run test via adb_run_wrapper
1553 runargs.push("shell".to_owned());
1554 for (key, val) in env {
1555 runargs.push(format!("{}={}", key, val));
1557 runargs.push(format!("{}/../adb_run_wrapper.sh", config.adb_test_dir));
1558 runargs.push(format!("{}", config.adb_test_dir));
1559 runargs.push(format!("{}", prog_short));
1561 for tv in &args.args {
1562 runargs.push(tv.to_owned());
1568 vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1569 .expect(&format!("failed to exec `{}`", config.adb_path));
1571 // get exitcode of result
1572 runargs = Vec::new();
1573 runargs.push("shell".to_owned());
1574 runargs.push("cat".to_owned());
1575 runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1577 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1582 vec!(("".to_owned(), "".to_owned())),
1583 Some("".to_owned()))
1584 .expect(&format!("failed to exec `{}`", config.adb_path));
1586 let mut exitcode: i32 = 0;
1587 for c in exitcode_out.chars() {
1588 if !c.is_numeric() { break; }
1589 exitcode = exitcode * 10 + match c {
1590 '0' ... '9' => c as i32 - ('0' as i32),
1595 // get stdout of result
1596 runargs = Vec::new();
1597 runargs.push("shell".to_owned());
1598 runargs.push("cat".to_owned());
1599 runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1601 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1606 vec!(("".to_owned(), "".to_owned())),
1607 Some("".to_owned()))
1608 .expect(&format!("failed to exec `{}`", config.adb_path));
1610 // get stderr of result
1611 runargs = Vec::new();
1612 runargs.push("shell".to_owned());
1613 runargs.push("cat".to_owned());
1614 runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1616 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1621 vec!(("".to_owned(), "".to_owned())),
1622 Some("".to_owned()))
1623 .expect(&format!("failed to exec `{}`", config.adb_path));
1631 status: Status::Parsed(exitcode),
1638 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1639 let tdir = aux_output_dir_name(config, testfile);
1641 let dirs = fs::read_dir(&tdir).unwrap();
1643 let file = file.unwrap().path();
1644 if file.extension().and_then(|s| s.to_str()) == Some("so") {
1645 // FIXME (#9639): This needs to handle non-utf8 paths
1646 let copy_result = procsrv::run("",
1654 config.adb_test_dir.to_owned(),
1656 vec!(("".to_owned(),
1658 Some("".to_owned()))
1659 .expect(&format!("failed to exec `{}`", config.adb_path));
1662 println!("push ({}) {:?} {} {}",
1663 config.target, file.display(),
1664 copy_result.out, copy_result.err);
1670 // codegen tests (using FileCheck)
1672 fn compile_test_and_save_ir(config: &Config, props: &TestProps,
1673 testfile: &Path) -> ProcRes {
1674 let aux_dir = aux_output_dir_name(config, testfile);
1675 // FIXME (#9639): This needs to handle non-utf8 paths
1676 let mut link_args = vec!("-L".to_owned(),
1677 aux_dir.to_str().unwrap().to_owned());
1678 let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1679 link_args.extend(llvm_args);
1680 let args = make_compile_args(config,
1683 |a, b| TargetLocation::ThisDirectory(
1684 output_base_name(a, b).parent()
1685 .unwrap().to_path_buf()),
1687 compose_and_run_compiler(config, props, testfile, args, None)
1690 fn check_ir_with_filecheck(config: &Config, testfile: &Path) -> ProcRes {
1691 let irfile = output_base_name(config, testfile).with_extension("ll");
1692 let prog = config.llvm_bin_path.as_ref().unwrap().join("FileCheck");
1693 let proc_args = ProcArgs {
1694 // FIXME (#9639): This needs to handle non-utf8 paths
1695 prog: prog.to_str().unwrap().to_owned(),
1696 args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1697 testfile.to_str().unwrap().to_owned())
1699 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1702 fn run_codegen_test(config: &Config, props: &TestProps, testfile: &Path) {
1704 if config.llvm_bin_path.is_none() {
1705 fatal("missing --llvm-bin-path");
1708 let mut proc_res = compile_test_and_save_ir(config, props, testfile);
1709 if !proc_res.status.success() {
1710 fatal_proc_rec("compilation failed!", &proc_res);
1713 proc_res = check_ir_with_filecheck(config, testfile);
1714 if !proc_res.status.success() {
1715 fatal_proc_rec("verification with 'FileCheck' failed",
1720 fn charset() -> &'static str {
1721 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1722 if cfg!(target_os = "bitrig") {
1724 } else if cfg!(target_os = "freebsd") {
1731 fn run_rustdoc_test(config: &Config, props: &TestProps, testfile: &Path) {
1732 let out_dir = output_base_name(config, testfile);
1733 let _ = fs::remove_dir_all(&out_dir);
1734 ensure_dir(&out_dir);
1736 let proc_res = document(config, props, testfile, &out_dir);
1737 if !proc_res.status.success() {
1738 fatal_proc_rec("rustdoc failed!", &proc_res);
1740 let root = find_rust_src_root(config).unwrap();
1742 let res = cmd2procres(config,
1744 Command::new(&config.python)
1745 .arg(root.join("src/etc/htmldocck.py"))
1748 if !res.status.success() {
1749 fatal_proc_rec("htmldocck failed!", &res);
1753 fn run_codegen_units_test(config: &Config, props: &TestProps, testfile: &Path) {
1754 let proc_res = compile_test(config, props, testfile);
1756 if !proc_res.status.success() {
1757 fatal_proc_rec("compilation failed!", &proc_res);
1760 check_no_compiler_crash(&proc_res);
1762 let prefix = "TRANS_ITEM ";
1764 let actual: HashSet<String> = proc_res
1767 .filter(|line| line.starts_with(prefix))
1768 .map(|s| (&s[prefix.len()..]).to_string())
1771 let expected: HashSet<String> = errors::load_errors(testfile)
1773 .map(|e| e.msg.trim().to_string())
1776 if actual != expected {
1777 let mut missing: Vec<_> = expected.difference(&actual).collect();
1780 let mut too_much: Vec<_> = actual.difference(&expected).collect();
1783 println!("Expected and actual sets of codegen-items differ.\n\
1784 These items should have been contained but were not:\n\n\
1786 These items were contained but should not have been:\n\n\
1788 missing.iter().fold("".to_string(), |s1, s2| s1 + "\n" + s2),
1789 too_much.iter().fold("".to_string(), |s1, s2| s1 + "\n" + s2));