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};
14 use common::{Incremental, RunMake, Ui, MirOpt};
15 use errors::{self, ErrorKind, Error};
17 use header::TestProps;
24 use std::collections::HashSet;
27 use std::fs::{self, File};
28 use std::io::prelude::*;
29 use std::io::{self, BufReader};
30 use std::path::{Path, PathBuf};
31 use std::process::{Command, Output, ExitStatus};
34 use extract_gdb_version;
36 pub fn run(config: Config, testpaths: &TestPaths) {
37 match &*config.target {
39 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
40 if !config.adb_device_status {
41 panic!("android device not available");
46 // android has it's own gdb handling
47 if config.mode == DebugInfoGdb && config.gdb.is_none() {
48 panic!("gdb not available but debuginfo gdb debuginfo test requested");
54 // We're going to be dumping a lot of info. Start on a new line.
57 debug!("running {:?}", testpaths.file.display());
58 let base_props = TestProps::from_file(&testpaths.file);
60 let base_cx = TestCx { config: &config,
66 if base_props.revisions.is_empty() {
67 base_cx.run_revision()
69 for revision in &base_props.revisions {
70 let mut revision_props = base_props.clone();
71 revision_props.load_from(&testpaths.file, Some(&revision));
74 props: &revision_props,
76 revision: Some(revision)
78 rev_cx.run_revision();
82 base_cx.complete_all();
85 struct TestCx<'test> {
86 config: &'test Config,
87 props: &'test TestProps,
88 testpaths: &'test TestPaths,
89 revision: Option<&'test str>
92 struct DebuggerCommands {
93 commands: Vec<String>,
94 check_lines: Vec<String>,
95 breakpoint_lines: Vec<usize>,
98 impl<'test> TestCx<'test> {
99 /// invoked once before any revisions have been processed
101 assert!(self.revision.is_none(), "init_all invoked for a revision");
102 match self.config.mode {
103 Incremental => self.init_incremental_test(),
108 /// Code executed for each revision in turn (or, if there are no
109 /// revisions, exactly once, with revision == None).
110 fn run_revision(&self) {
111 match self.config.mode {
112 CompileFail => self.run_cfail_test(),
113 ParseFail => self.run_cfail_test(),
114 RunFail => self.run_rfail_test(),
115 RunPass => self.run_rpass_test(),
116 RunPassValgrind => self.run_valgrind_test(),
117 Pretty => self.run_pretty_test(),
118 DebugInfoGdb => self.run_debuginfo_gdb_test(),
119 DebugInfoLldb => self.run_debuginfo_lldb_test(),
120 Codegen => self.run_codegen_test(),
121 Rustdoc => self.run_rustdoc_test(),
122 CodegenUnits => self.run_codegen_units_test(),
123 Incremental => self.run_incremental_test(),
124 RunMake => self.run_rmake_test(),
125 Ui => self.run_ui_test(),
126 MirOpt => self.run_mir_opt_test(),
130 /// Invoked after all revisions have executed.
131 fn complete_all(&self) {
132 assert!(self.revision.is_none(), "init_all invoked for a revision");
135 fn run_cfail_test(&self) {
136 let proc_res = self.compile_test();
138 if self.props.must_compile_successfully {
139 if !proc_res.status.success() {
141 "test compilation failed although it shouldn't!",
145 if proc_res.status.success() {
147 &format!("{} test compiled successfully!", self.config.mode)[..],
151 self.check_correct_failure_status(&proc_res);
154 let output_to_check = self.get_output(&proc_res);
155 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
156 if !expected_errors.is_empty() {
157 if !self.props.error_patterns.is_empty() {
158 self.fatal("both error pattern and expected errors specified");
160 self.check_expected_errors(expected_errors, &proc_res);
162 self.check_error_patterns(&output_to_check, &proc_res);
165 self.check_no_compiler_crash(&proc_res);
166 self.check_forbid_output(&output_to_check, &proc_res);
169 fn run_rfail_test(&self) {
170 let proc_res = self.compile_test();
172 if !proc_res.status.success() {
173 self.fatal_proc_rec("compilation failed!", &proc_res);
176 let proc_res = self.exec_compiled_test();
178 // The value our Makefile configures valgrind to return on failure
179 const VALGRIND_ERR: i32 = 100;
180 if proc_res.status.code() == Some(VALGRIND_ERR) {
181 self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
184 let output_to_check = self.get_output(&proc_res);
185 self.check_correct_failure_status(&proc_res);
186 self.check_error_patterns(&output_to_check, &proc_res);
189 fn get_output(&self, proc_res: &ProcRes) -> String {
190 if self.props.check_stdout {
191 format!("{}{}", proc_res.stdout, proc_res.stderr)
193 proc_res.stderr.clone()
197 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
198 // The value the rust runtime returns on failure
199 const RUST_ERR: i32 = 101;
200 if proc_res.status.code() != Some(RUST_ERR) {
202 &format!("failure produced the wrong error: {}",
208 fn run_rpass_test(&self) {
209 let proc_res = self.compile_test();
211 if !proc_res.status.success() {
212 self.fatal_proc_rec("compilation failed!", &proc_res);
215 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
216 if !expected_errors.is_empty() {
217 self.check_expected_errors(expected_errors, &proc_res);
220 let proc_res = self.exec_compiled_test();
222 if !proc_res.status.success() {
223 self.fatal_proc_rec("test run failed!", &proc_res);
227 fn run_valgrind_test(&self) {
228 assert!(self.revision.is_none(), "revisions not relevant here");
230 if self.config.valgrind_path.is_none() {
231 assert!(!self.config.force_valgrind);
232 return self.run_rpass_test();
235 let mut proc_res = self.compile_test();
237 if !proc_res.status.success() {
238 self.fatal_proc_rec("compilation failed!", &proc_res);
241 let mut new_config = self.config.clone();
242 new_config.runtool = new_config.valgrind_path.clone();
243 let new_cx = TestCx { config: &new_config, ..*self };
244 proc_res = new_cx.exec_compiled_test();
246 if !proc_res.status.success() {
247 self.fatal_proc_rec("test run failed!", &proc_res);
251 fn run_pretty_test(&self) {
252 if self.props.pp_exact.is_some() {
253 logv(self.config, "testing for exact pretty-printing".to_owned());
255 logv(self.config, "testing for converging pretty-printing".to_owned());
258 let rounds = match self.props.pp_exact { Some(_) => 1, None => 2 };
260 let mut src = String::new();
261 File::open(&self.testpaths.file).unwrap().read_to_string(&mut src).unwrap();
262 let mut srcs = vec![src];
265 while round < rounds {
266 logv(self.config, format!("pretty-printing round {} revision {:?}",
267 round, self.revision));
268 let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
270 if !proc_res.status.success() {
271 self.fatal_proc_rec(&format!("pretty-printing failed in round {} revision {:?}",
272 round, self.revision),
276 let ProcRes{ stdout, .. } = proc_res;
281 let mut expected = match self.props.pp_exact {
283 let filepath = self.testpaths.file.parent().unwrap().join(file);
284 let mut s = String::new();
285 File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
288 None => { srcs[srcs.len() - 2].clone() }
290 let mut actual = srcs[srcs.len() - 1].clone();
292 if self.props.pp_exact.is_some() {
293 // Now we have to care about line endings
294 let cr = "\r".to_owned();
295 actual = actual.replace(&cr, "").to_owned();
296 expected = expected.replace(&cr, "").to_owned();
299 self.compare_source(&expected, &actual);
301 // If we're only making sure that the output matches then just stop here
302 if self.props.pretty_compare_only { return; }
304 // Finally, let's make sure it actually appears to remain valid code
305 let proc_res = self.typecheck_source(actual);
306 if !proc_res.status.success() {
307 self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
310 if !self.props.pretty_expanded { return }
312 // additionally, run `--pretty expanded` and try to build it.
313 let proc_res = self.print_source(srcs[round].clone(), "expanded");
314 if !proc_res.status.success() {
315 self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
318 let ProcRes{ stdout: expanded_src, .. } = proc_res;
319 let proc_res = self.typecheck_source(expanded_src);
320 if !proc_res.status.success() {
322 "pretty-printed source (expanded) does not typecheck",
327 fn print_source(&self,
331 let aux_dir = self.aux_output_dir_name();
332 self.compose_and_run(self.make_pp_args(pretty_type.to_owned()),
333 self.props.exec_env.clone(),
334 self.config.compile_lib_path.to_str().unwrap(),
335 Some(aux_dir.to_str().unwrap()),
339 fn make_pp_args(&self,
342 let aux_dir = self.aux_output_dir_name();
343 // FIXME (#9639): This needs to handle non-utf8 paths
344 let mut args = vec!["-".to_owned(),
345 "-Zunstable-options".to_owned(),
346 "--unpretty".to_owned(),
348 format!("--target={}", self.config.target),
350 aux_dir.to_str().unwrap().to_owned()];
351 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
352 args.extend(self.props.compile_flags.iter().cloned());
354 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
359 fn compare_source(&self,
362 if expected != actual {
363 self.error("pretty-printed source does not match expected source");
366 ------------------------------------------\n\
368 ------------------------------------------\n\
370 ------------------------------------------\n\
372 ------------------------------------------\n\
379 fn typecheck_source(&self, src: String) -> ProcRes {
380 let args = self.make_typecheck_args();
381 self.compose_and_run_compiler(args, Some(src))
384 fn make_typecheck_args(&self) -> ProcArgs {
385 let aux_dir = self.aux_output_dir_name();
386 let target = if self.props.force_host {
392 let out_dir = self.output_base_name().with_extension("pretty-out");
393 let _ = fs::remove_dir_all(&out_dir);
394 self.create_dir_racy(&out_dir);
396 // FIXME (#9639): This needs to handle non-utf8 paths
397 let mut args = vec!["-".to_owned(),
398 "-Zno-trans".to_owned(),
399 "--out-dir".to_owned(),
400 out_dir.to_str().unwrap().to_owned(),
401 format!("--target={}", target),
403 self.config.build_base.to_str().unwrap().to_owned(),
405 aux_dir.to_str().unwrap().to_owned()];
406 if let Some(revision) = self.revision {
409 format!("{}", revision),
412 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
413 args.extend(self.props.compile_flags.iter().cloned());
414 // FIXME (#9639): This needs to handle non-utf8 paths
416 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
421 fn run_debuginfo_gdb_test(&self) {
422 assert!(self.revision.is_none(), "revisions not relevant here");
424 let config = Config {
425 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
426 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
427 .. self.config.clone()
430 let test_cx = TestCx {
435 test_cx.run_debuginfo_gdb_test_no_opt();
438 fn run_debuginfo_gdb_test_no_opt(&self) {
439 let prefixes = if self.config.gdb_native_rust {
441 static PREFIXES: &'static [&'static str] = &["gdb", "gdbr"];
442 println!("NOTE: compiletest thinks it is using GDB with native rust support");
446 static PREFIXES: &'static [&'static str] = &["gdb", "gdbg"];
447 println!("NOTE: compiletest thinks it is using GDB without native rust support");
451 let DebuggerCommands {
455 } = self.parse_debugger_commands(prefixes);
456 let mut cmds = commands.join("\n");
458 // compile test file (it should have 'compile-flags:-g' in the header)
459 let compiler_run_result = self.compile_test();
460 if !compiler_run_result.status.success() {
461 self.fatal_proc_rec("compilation failed!", &compiler_run_result);
464 let exe_file = self.make_exe_name();
466 let debugger_run_result;
467 match &*self.config.target {
468 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
470 cmds = cmds.replace("run", "continue");
472 let tool_path = match self.config.android_cross_path.to_str() {
473 Some(x) => x.to_owned(),
474 None => self.fatal("cannot find android cross path")
477 // write debugger script
478 let mut script_str = String::with_capacity(2048);
479 script_str.push_str(&format!("set charset {}\n", Self::charset()));
480 script_str.push_str(&format!("set sysroot {}\n", tool_path));
481 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
482 script_str.push_str("target remote :5039\n");
483 script_str.push_str(&format!("set solib-search-path \
484 ./{}/stage2/lib/rustlib/{}/lib/\n",
485 self.config.host, self.config.target));
486 for line in &breakpoint_lines {
487 script_str.push_str(&format!("break {:?}:{}\n",
488 self.testpaths.file.file_name()
493 script_str.push_str(&cmds);
494 script_str.push_str("\nquit\n");
496 debug!("script_str = {}", script_str);
497 self.dump_output_file(&script_str, "debugger.script");
501 &self.config.adb_path,
505 exe_file.to_str().unwrap().to_owned(),
506 self.config.adb_test_dir.clone()
510 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
513 &self.config.adb_path,
516 "forward".to_owned(),
517 "tcp:5039".to_owned(),
518 "tcp:5039".to_owned()
522 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
524 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
525 gdbserver{} :5039 {}/{}",
526 self.config.adb_test_dir.clone(),
527 if self.config.target.contains("aarch64")
529 self.config.adb_test_dir.clone(),
530 exe_file.file_name().unwrap().to_str()
533 let mut process = procsrv::run_background("",
534 &self.config.adb_path
543 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
545 // Wait for the gdbserver to print out "Listening on port ..."
546 // at which point we know that it's started and then we can
547 // execute the debugger below.
548 let mut stdout = BufReader::new(process.stdout.take().unwrap());
549 let mut line = String::new();
552 stdout.read_line(&mut line).unwrap();
553 if line.starts_with("Listening on port 5039") {
559 let debugger_script = self.make_out_name("debugger.script");
560 // FIXME (#9639): This needs to handle non-utf8 paths
562 vec!["-quiet".to_owned(),
565 format!("-command={}", debugger_script.to_str().unwrap())];
567 let mut gdb_path = tool_path;
568 gdb_path.push_str(&format!("/bin/{}-gdb", self.config.target));
569 let procsrv::Result {
579 .expect(&format!("failed to exec `{:?}`", gdb_path));
581 let cmdline = self.make_cmdline("",
582 &format!("{}-gdb", self.config.target),
584 logv(self.config, format!("executing {}", cmdline));
588 debugger_run_result = ProcRes {
589 status: Status::Normal(status),
594 if process.kill().is_err() {
595 println!("Adb process is already finished.");
600 let rust_src_root = self.find_rust_src_root()
601 .expect("Could not find Rust source root");
602 let rust_pp_module_rel_path = Path::new("./src/etc");
603 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
607 // write debugger script
608 let mut script_str = String::with_capacity(2048);
609 script_str.push_str(&format!("set charset {}\n", Self::charset()));
610 script_str.push_str("show version\n");
612 match self.config.gdb_version {
614 println!("NOTE: compiletest thinks it is using GDB version {}",
617 if version > extract_gdb_version("7.4").unwrap() {
618 // Add the directory containing the pretty printers to
619 // GDB's script auto loading safe path
621 &format!("add-auto-load-safe-path {}\n",
622 rust_pp_module_abs_path.replace(r"\", r"\\"))
627 println!("NOTE: compiletest does not know which version of \
632 // The following line actually doesn't have to do anything with
633 // pretty printing, it just tells GDB to print values on one line:
634 script_str.push_str("set print pretty off\n");
636 // Add the pretty printer directory to GDB's source-file search path
637 script_str.push_str(&format!("directory {}\n",
638 rust_pp_module_abs_path));
640 // Load the target executable
641 script_str.push_str(&format!("file {}\n",
642 exe_file.to_str().unwrap()
643 .replace(r"\", r"\\")));
645 // Add line breakpoints
646 for line in &breakpoint_lines {
647 script_str.push_str(&format!("break '{}':{}\n",
648 self.testpaths.file.file_name().unwrap()
653 script_str.push_str(&cmds);
654 script_str.push_str("\nquit\n");
656 debug!("script_str = {}", script_str);
657 self.dump_output_file(&script_str, "debugger.script");
659 let debugger_script = self.make_out_name("debugger.script");
661 // FIXME (#9639): This needs to handle non-utf8 paths
663 vec!["-quiet".to_owned(),
666 format!("-command={}", debugger_script.to_str().unwrap())];
668 let proc_args = ProcArgs {
669 prog: self.config.gdb.as_ref().unwrap().to_owned(),
673 let environment = vec![("PYTHONPATH".to_owned(), rust_pp_module_abs_path)];
675 debugger_run_result =
676 self.compose_and_run(proc_args,
678 self.config.run_lib_path.to_str().unwrap(),
684 if !debugger_run_result.status.success() {
685 self.fatal("gdb failed to execute");
688 self.check_debugger_output(&debugger_run_result, &check_lines);
691 fn find_rust_src_root(&self) -> Option<PathBuf> {
692 let mut path = self.config.src_base.clone();
693 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
696 if path.join(&path_postfix).is_file() {
704 fn run_debuginfo_lldb_test(&self) {
705 assert!(self.revision.is_none(), "revisions not relevant here");
707 if self.config.lldb_python_dir.is_none() {
708 self.fatal("Can't run LLDB test because LLDB's python path is not set.");
711 let config = Config {
712 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
713 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
714 .. self.config.clone()
718 let test_cx = TestCx {
723 test_cx.run_debuginfo_lldb_test_no_opt();
726 fn run_debuginfo_lldb_test_no_opt(&self) {
727 // compile test file (it should have 'compile-flags:-g' in the header)
728 let compile_result = self.compile_test();
729 if !compile_result.status.success() {
730 self.fatal_proc_rec("compilation failed!", &compile_result);
733 let exe_file = self.make_exe_name();
735 match self.config.lldb_version {
736 Some(ref version) => {
737 println!("NOTE: compiletest thinks it is using LLDB version {}",
741 println!("NOTE: compiletest does not know which version of \
746 // Parse debugger commands etc from test files
747 let DebuggerCommands {
752 } = self.parse_debugger_commands(&["lldb"]);
754 // Write debugger script:
755 // We don't want to hang when calling `quit` while the process is still running
756 let mut script_str = String::from("settings set auto-confirm true\n");
758 // Make LLDB emit its version, so we have it documented in the test output
759 script_str.push_str("version\n");
761 // Switch LLDB into "Rust mode"
762 let rust_src_root = self.find_rust_src_root().expect("Could not find Rust source root");
763 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
764 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
769 script_str.push_str(&format!("command script import {}\n",
770 &rust_pp_module_abs_path[..])[..]);
771 script_str.push_str("type summary add --no-value ");
772 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
773 script_str.push_str("-x \".*\" --category Rust\n");
774 script_str.push_str("type category enable Rust\n");
776 // Set breakpoints on every line that contains the string "#break"
777 let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
778 for line in &breakpoint_lines {
779 script_str.push_str(&format!("breakpoint set --file '{}' --line {}\n",
784 // Append the other commands
785 for line in &commands {
786 script_str.push_str(line);
787 script_str.push_str("\n");
790 // Finally, quit the debugger
791 script_str.push_str("\nquit\n");
793 // Write the script into a file
794 debug!("script_str = {}", script_str);
795 self.dump_output_file(&script_str, "debugger.script");
796 let debugger_script = self.make_out_name("debugger.script");
798 // Let LLDB execute the script via lldb_batchmode.py
799 let debugger_run_result = self.run_lldb(&exe_file,
803 if !debugger_run_result.status.success() {
804 self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
807 self.check_debugger_output(&debugger_run_result, &check_lines);
811 test_executable: &Path,
812 debugger_script: &Path,
813 rust_src_root: &Path)
815 // Prepare the lldb_batchmode which executes the debugger script
816 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
817 self.cmd2procres(Command::new(&self.config.lldb_python)
818 .arg(&lldb_script_path)
819 .arg(test_executable)
820 .arg(debugger_script)
822 self.config.lldb_python_dir.as_ref().unwrap()))
825 fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
826 let (status, out, err) = match cmd.output() {
827 Ok(Output { status, stdout, stderr }) => {
829 String::from_utf8(stdout).unwrap(),
830 String::from_utf8(stderr).unwrap())
833 self.fatal(&format!("Failed to setup Python process for \
834 LLDB script: {}", e))
838 self.dump_output(&out, &err);
840 status: Status::Normal(status),
843 cmdline: format!("{:?}", cmd)
847 fn parse_debugger_commands(&self, debugger_prefixes: &[&str]) -> DebuggerCommands {
848 let directives = debugger_prefixes.iter().map(|prefix| (
849 format!("{}-command", prefix),
850 format!("{}-check", prefix),
851 )).collect::<Vec<_>>();
853 let mut breakpoint_lines = vec![];
854 let mut commands = vec![];
855 let mut check_lines = vec![];
857 let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
858 for line in reader.lines() {
861 if line.contains("#break") {
862 breakpoint_lines.push(counter);
865 for &(ref command_directive, ref check_directive) in &directives {
866 header::parse_name_value_directive(
868 &command_directive).map(|cmd| {
872 header::parse_name_value_directive(
874 &check_directive).map(|cmd| {
875 check_lines.push(cmd)
880 self.fatal(&format!("Error while parsing debugger commands: {}", e))
888 check_lines: check_lines,
889 breakpoint_lines: breakpoint_lines,
893 fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
894 if options.is_none() {
898 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
899 let options_to_remove = [
902 "--debuginfo".to_owned()
905 self.split_maybe_args(options).into_iter()
906 .filter(|x| !options_to_remove.contains(x))
907 .collect::<Vec<String>>();
909 Some(new_options.join(" "))
912 fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
913 let num_check_lines = check_lines.len();
915 let mut check_line_index = 0;
916 for line in debugger_run_result.stdout.lines() {
917 if check_line_index >= num_check_lines {
921 if check_single_line(line, &(check_lines[check_line_index])[..]) {
922 check_line_index += 1;
925 if check_line_index != num_check_lines && num_check_lines > 0 {
926 self.fatal_proc_rec(&format!("line not found in debugger output: {}",
927 check_lines[check_line_index]),
928 debugger_run_result);
931 fn check_single_line(line: &str, check_line: &str) -> bool {
932 // Allow check lines to leave parts unspecified (e.g., uninitialized
933 // bits in the wrong case of an enum) with the notation "[...]".
934 let line = line.trim();
935 let check_line = check_line.trim();
936 let can_start_anywhere = check_line.starts_with("[...]");
937 let can_end_anywhere = check_line.ends_with("[...]");
939 let check_fragments: Vec<&str> = check_line.split("[...]")
940 .filter(|frag| !frag.is_empty())
942 if check_fragments.is_empty() {
946 let (mut rest, first_fragment) = if can_start_anywhere {
947 match line.find(check_fragments[0]) {
948 Some(pos) => (&line[pos + check_fragments[0].len() ..], 1),
955 for fragment_index in first_fragment .. check_fragments.len() {
956 let current_fragment = check_fragments[fragment_index];
957 match rest.find(current_fragment) {
959 rest = &rest[pos + current_fragment.len() .. ];
965 if !can_end_anywhere && !rest.is_empty() {
973 fn check_error_patterns(&self,
974 output_to_check: &str,
975 proc_res: &ProcRes) {
976 if self.props.error_patterns.is_empty() {
977 if self.props.must_compile_successfully {
980 self.fatal(&format!("no error pattern specified in {:?}",
981 self.testpaths.file.display()));
984 let mut next_err_idx = 0;
985 let mut next_err_pat = self.props.error_patterns[next_err_idx].trim();
986 let mut done = false;
987 for line in output_to_check.lines() {
988 if line.contains(next_err_pat) {
989 debug!("found error pattern {}", next_err_pat);
991 if next_err_idx == self.props.error_patterns.len() {
992 debug!("found all error patterns");
996 next_err_pat = self.props.error_patterns[next_err_idx].trim();
1001 let missing_patterns = &self.props.error_patterns[next_err_idx..];
1002 if missing_patterns.len() == 1 {
1003 self.fatal_proc_rec(
1004 &format!("error pattern '{}' not found!", missing_patterns[0]),
1007 for pattern in missing_patterns {
1008 self.error(&format!("error pattern '{}' not found!", *pattern));
1010 self.fatal_proc_rec("multiple error patterns not found", proc_res);
1014 fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
1015 for line in proc_res.stderr.lines() {
1016 if line.contains("error: internal compiler error") {
1017 self.fatal_proc_rec("compiler encountered internal error", proc_res);
1022 fn check_forbid_output(&self,
1023 output_to_check: &str,
1024 proc_res: &ProcRes) {
1025 for pat in &self.props.forbid_output {
1026 if output_to_check.contains(pat) {
1027 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
1032 fn check_expected_errors(&self,
1033 expected_errors: Vec<errors::Error>,
1034 proc_res: &ProcRes) {
1035 if proc_res.status.success() &&
1036 expected_errors.iter().any(|x| x.kind == Some(ErrorKind::Error)) {
1037 self.fatal_proc_rec("process did not return an error status", proc_res);
1041 format!("{}", self.testpaths.file.display())
1042 .replace(r"\", "/"); // on windows, translate all '\' path separators to '/'
1044 // If the testcase being checked contains at least one expected "help"
1045 // message, then we'll ensure that all "help" messages are expected.
1046 // Otherwise, all "help" messages reported by the compiler will be ignored.
1047 // This logic also applies to "note" messages.
1048 let expect_help = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Help));
1049 let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
1051 // Parse the JSON output from the compiler and extract out the messages.
1052 let actual_errors = json::parse_output(&file_name, &proc_res.stderr, &proc_res);
1053 let mut unexpected = Vec::new();
1054 let mut found = vec![false; expected_errors.len()];
1055 for actual_error in &actual_errors {
1060 .position(|(index, expected_error)| {
1062 actual_error.line_num == expected_error.line_num &&
1063 (expected_error.kind.is_none() ||
1064 actual_error.kind == expected_error.kind) &&
1065 actual_error.msg.contains(&expected_error.msg)
1070 // found a match, everybody is happy
1071 assert!(!found[index]);
1072 found[index] = true;
1076 if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1078 &format!("{}:{}: unexpected {:?}: '{}'",
1080 actual_error.line_num,
1081 actual_error.kind.as_ref()
1082 .map_or(String::from("message"),
1085 unexpected.push(actual_error.clone());
1091 let mut not_found = Vec::new();
1092 // anything not yet found is a problem
1093 for (index, expected_error) in expected_errors.iter().enumerate() {
1096 &format!("{}:{}: expected {} not found: {}",
1098 expected_error.line_num,
1099 expected_error.kind.as_ref()
1100 .map_or("message".into(),
1102 expected_error.msg));
1103 not_found.push(expected_error.clone());
1107 if unexpected.len() > 0 || not_found.len() > 0 {
1109 &format!("{} unexpected errors found, {} expected errors not found",
1110 unexpected.len(), not_found.len()));
1111 print!("status: {}\ncommand: {}\n",
1112 proc_res.status, proc_res.cmdline);
1113 if unexpected.len() > 0 {
1114 println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1116 if not_found.len() > 0 {
1117 println!("not found errors (from test file): {:#?}\n", not_found);
1123 /// Returns true if we should report an error about `actual_error`,
1124 /// which did not match any of the expected error. We always require
1125 /// errors/warnings to be explicitly listed, but only require
1126 /// helps/notes if there are explicit helps/notes given.
1127 fn is_unexpected_compiler_message(&self,
1128 actual_error: &Error,
1132 match actual_error.kind {
1133 Some(ErrorKind::Help) => expect_help,
1134 Some(ErrorKind::Note) => expect_note,
1135 Some(ErrorKind::Error) => true,
1136 Some(ErrorKind::Warning) => true,
1137 Some(ErrorKind::Suggestion) => false,
1142 fn compile_test(&self) -> ProcRes {
1143 let aux_dir = self.aux_output_dir_name();
1144 // FIXME (#9639): This needs to handle non-utf8 paths
1145 let link_args = vec!["-L".to_owned(),
1146 aux_dir.to_str().unwrap().to_owned()];
1147 let args = self.make_compile_args(link_args,
1148 &self.testpaths.file,
1149 TargetLocation::ThisFile(self.make_exe_name()));
1150 self.compose_and_run_compiler(args, None)
1153 fn document(&self, out_dir: &Path) -> ProcRes {
1154 if self.props.build_aux_docs {
1155 for rel_ab in &self.props.aux_builds {
1156 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1157 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1158 let aux_cx = TestCx {
1159 config: self.config,
1161 testpaths: &aux_testpaths,
1162 revision: self.revision
1164 let auxres = aux_cx.document(out_dir);
1165 if !auxres.status.success() {
1171 let aux_dir = self.aux_output_dir_name();
1172 let mut args = vec!["-L".to_owned(),
1173 aux_dir.to_str().unwrap().to_owned(),
1175 out_dir.to_str().unwrap().to_owned(),
1176 self.testpaths.file.to_str().unwrap().to_owned()];
1177 args.extend(self.props.compile_flags.iter().cloned());
1178 let args = ProcArgs {
1179 prog: self.config.rustdoc_path.to_str().unwrap().to_owned(),
1182 self.compose_and_run_compiler(args, None)
1185 fn exec_compiled_test(&self) -> ProcRes {
1186 let env = self.props.exec_env.clone();
1188 match &*self.config.target {
1190 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1191 self._arm_exec_compiled_test(env)
1194 let aux_dir = self.aux_output_dir_name();
1195 self.compose_and_run(self.make_run_args(),
1197 self.config.run_lib_path.to_str().unwrap(),
1198 Some(aux_dir.to_str().unwrap()),
1204 /// For each `aux-build: foo/bar` annotation, we check to find the
1205 /// file in a `aux` directory relative to the test itself.
1206 fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1207 let test_ab = self.testpaths.file
1209 .expect("test file path has no parent")
1212 if !test_ab.exists() {
1213 self.fatal(&format!("aux-build `{}` source not found", test_ab.display()))
1218 base: self.testpaths.base.clone(),
1219 relative_dir: self.testpaths.relative_dir
1223 .expect("aux-build path has no parent")
1228 fn compose_and_run_compiler(&self, args: ProcArgs, input: Option<String>) -> ProcRes {
1229 if !self.props.aux_builds.is_empty() {
1230 self.create_dir_racy(&self.aux_output_dir_name());
1233 let aux_dir = self.aux_output_dir_name();
1234 // FIXME (#9639): This needs to handle non-utf8 paths
1235 let extra_link_args = vec!["-L".to_owned(),
1236 aux_dir.to_str().unwrap().to_owned()];
1238 for rel_ab in &self.props.aux_builds {
1239 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1240 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1241 let mut crate_type = if aux_props.no_prefer_dynamic {
1244 // We primarily compile all auxiliary libraries as dynamic libraries
1245 // to avoid code size bloat and large binaries as much as possible
1246 // for the test suite (otherwise including libstd statically in all
1247 // executables takes up quite a bit of space).
1249 // For targets like MUSL or Emscripten, however, there is no support for
1250 // dynamic libraries so we just go back to building a normal library. Note,
1251 // however, that for MUSL if the library is built with `force_host` then
1252 // it's ok to be a dylib as the host should always support dylibs.
1253 if (self.config.target.contains("musl") && !aux_props.force_host) ||
1254 self.config.target.contains("emscripten")
1256 vec!["--crate-type=lib".to_owned()]
1258 vec!["--crate-type=dylib".to_owned()]
1261 crate_type.extend(extra_link_args.clone());
1263 let f = self.make_lib_name(&self.testpaths.file);
1264 let parent = f.parent().unwrap();
1265 TargetLocation::ThisDirectory(parent.to_path_buf())
1267 let aux_cx = TestCx {
1268 config: self.config,
1270 testpaths: &aux_testpaths,
1271 revision: self.revision
1273 let aux_args = aux_cx.make_compile_args(crate_type, &aux_testpaths.file, aux_output);
1274 let auxres = aux_cx.compose_and_run(aux_args,
1276 aux_cx.config.compile_lib_path.to_str().unwrap(),
1277 Some(aux_dir.to_str().unwrap()),
1279 if !auxres.status.success() {
1280 self.fatal_proc_rec(
1281 &format!("auxiliary build of {:?} failed to compile: ",
1282 aux_testpaths.file.display()),
1286 match &*self.config.target {
1287 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1288 self._arm_push_aux_shared_library();
1294 self.compose_and_run(args,
1295 self.props.rustc_env.clone(),
1296 self.config.compile_lib_path.to_str().unwrap(),
1297 Some(aux_dir.to_str().unwrap()),
1301 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
1302 // threads or processes.
1303 fn create_dir_racy(&self, path: &Path) {
1304 match fs::create_dir(path) {
1306 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return,
1307 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
1308 Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1310 self.create_dir_racy(path.parent().unwrap());
1311 match fs::create_dir(path) {
1313 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => {}
1314 Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1318 fn compose_and_run(&self,
1319 ProcArgs{ args, prog }: ProcArgs,
1320 procenv: Vec<(String, String)> ,
1322 aux_path: Option<&str>,
1323 input: Option<String>) -> ProcRes {
1324 return self.program_output(lib_path, prog, aux_path, args, procenv, input);
1327 fn make_compile_args(&self,
1328 extras: Vec<String> ,
1330 output_file: TargetLocation)
1333 let target = if self.props.force_host {
1336 &*self.config.target
1339 // FIXME (#9639): This needs to handle non-utf8 paths
1340 let mut args = vec![input_file.to_str().unwrap().to_owned(),
1342 self.config.build_base.to_str().unwrap().to_owned()];
1344 // Optionally prevent default --target if specified in test compile-flags.
1345 let custom_target = self.props.compile_flags
1347 .fold(false, |acc, ref x| acc || x.starts_with("--target"));
1351 format!("--target={}", target),
1355 if let Some(revision) = self.revision {
1358 format!("{}", revision),
1362 if let Some(ref incremental_dir) = self.props.incremental_dir {
1365 format!("incremental={}", incremental_dir.display()),
1370 match self.config.mode {
1375 // If we are extracting and matching errors in the new
1376 // fashion, then you want JSON mode. Old-skool error
1377 // patterns still match the raw compiler output.
1378 if self.props.error_patterns.is_empty() {
1379 args.extend(["--error-format",
1382 .map(|s| s.to_string()));
1392 .map(|s| s.to_string()));
1395 let mir_dump_dir = self.get_mir_dump_dir();
1396 self.create_dir_racy(mir_dump_dir.as_path());
1397 let mut dir_opt = "dump-mir-dir=".to_string();
1398 dir_opt.push_str(mir_dump_dir.to_str().unwrap());
1399 debug!("dir_opt: {:?}", dir_opt);
1413 // do not use JSON output
1417 args.extend_from_slice(&extras);
1418 if !self.props.no_prefer_dynamic {
1419 args.push("-C".to_owned());
1420 args.push("prefer-dynamic".to_owned());
1422 let path = match output_file {
1423 TargetLocation::ThisFile(path) => {
1424 args.push("-o".to_owned());
1427 TargetLocation::ThisDirectory(path) => {
1428 args.push("--out-dir".to_owned());
1432 args.push(path.to_str().unwrap().to_owned());
1433 if self.props.force_host {
1434 args.extend(self.split_maybe_args(&self.config.host_rustcflags));
1436 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
1438 args.extend(self.props.compile_flags.iter().cloned());
1440 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
1445 fn make_lib_name(&self, auxfile: &Path) -> PathBuf {
1446 // what we return here is not particularly important, as it
1447 // happens; rustc ignores everything except for the directory.
1448 let auxname = self.output_testname(auxfile);
1449 self.aux_output_dir_name().join(&auxname)
1452 fn make_exe_name(&self) -> PathBuf {
1453 let mut f = self.output_base_name();
1454 // FIXME: This is using the host architecture exe suffix, not target!
1455 if self.config.target.contains("emscripten") {
1456 let mut fname = f.file_name().unwrap().to_os_string();
1458 f.set_file_name(&fname);
1459 } else if !env::consts::EXE_SUFFIX.is_empty() {
1460 let mut fname = f.file_name().unwrap().to_os_string();
1461 fname.push(env::consts::EXE_SUFFIX);
1462 f.set_file_name(&fname);
1467 fn make_run_args(&self) -> ProcArgs {
1468 // If we've got another tool to run under (valgrind),
1469 // then split apart its command
1470 let mut args = self.split_maybe_args(&self.config.runtool);
1472 // If this is emscripten, then run tests under nodejs
1473 if self.config.target.contains("emscripten") {
1474 if let Some(ref p) = self.config.nodejs {
1475 args.push(p.clone());
1477 self.fatal("no NodeJS binary found (--nodejs)");
1481 let exe_file = self.make_exe_name();
1483 // FIXME (#9639): This needs to handle non-utf8 paths
1484 args.push(exe_file.to_str().unwrap().to_owned());
1486 // Add the arguments in the run_flags directive
1487 args.extend(self.split_maybe_args(&self.props.run_flags));
1489 let prog = args.remove(0);
1496 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1502 if s.chars().all(|c| c.is_whitespace()) {
1513 fn program_output(&self,
1516 aux_path: Option<&str>,
1518 env: Vec<(String, String)>,
1519 input: Option<String>)
1523 let cmdline = self.make_cmdline(lib_path,
1526 logv(self.config, format!("executing {}", cmdline));
1529 let procsrv::Result {
1533 } = procsrv::run(lib_path,
1538 input).expect(&format!("failed to exec `{}`", prog));
1539 self.dump_output(&out, &err);
1541 status: Status::Normal(status),
1548 fn make_cmdline(&self, libpath: &str, prog: &str, args: &[String]) -> String {
1551 // Linux and mac don't require adjusting the library search path
1553 format!("{} {}", prog, args.join(" "))
1555 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1556 // for diagnostic purposes
1557 fn lib_path_cmd_prefix(path: &str) -> String {
1558 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1561 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1565 fn dump_output(&self, out: &str, err: &str) {
1566 self.dump_output_file(out, "out");
1567 self.dump_output_file(err, "err");
1568 self.maybe_dump_to_stdout(out, err);
1571 fn dump_output_file(&self,
1574 let outfile = self.make_out_name(extension);
1575 File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1578 fn make_out_name(&self, extension: &str) -> PathBuf {
1579 self.output_base_name().with_extension(extension)
1582 fn aux_output_dir_name(&self) -> PathBuf {
1583 let f = self.output_base_name();
1584 let mut fname = f.file_name().unwrap().to_os_string();
1585 fname.push(&format!(".{}.libaux", self.config.mode));
1586 f.with_file_name(&fname)
1589 fn output_testname(&self, filepath: &Path) -> PathBuf {
1590 PathBuf::from(filepath.file_stem().unwrap())
1593 /// Given a test path like `compile-fail/foo/bar.rs` Returns a name like
1595 /// <output>/foo/bar-stage1
1596 fn output_base_name(&self) -> PathBuf {
1597 let dir = self.config.build_base.join(&self.testpaths.relative_dir);
1599 // Note: The directory `dir` is created during `collect_tests_from_dir`
1601 .join(&self.output_testname(&self.testpaths.file))
1602 .with_extension(&self.config.stage_id)
1605 fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1606 if self.config.verbose {
1607 println!("------{}------------------------------", "stdout");
1608 println!("{}", out);
1609 println!("------{}------------------------------", "stderr");
1610 println!("{}", err);
1611 println!("------------------------------------------");
1615 fn error(&self, err: &str) {
1616 match self.revision {
1617 Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1618 None => println!("\nerror: {}", err)
1622 fn fatal(&self, err: &str) -> ! {
1623 self.error(err); panic!();
1626 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1627 self.try_print_open_handles();
1629 proc_res.fatal(None);
1632 // This function is a poor man's attempt to debug rust-lang/rust#38620, if
1633 // that's closed then this should be deleted
1635 // This is a very "opportunistic" debugging attempt, so we ignore all
1637 fn try_print_open_handles(&self) {
1641 if self.config.mode != Incremental {
1645 let filename = match self.testpaths.file.file_stem() {
1650 let mut cmd = Command::new("handle.exe");
1651 cmd.arg("-a").arg("-u");
1653 cmd.arg("-nobanner");
1654 let output = match cmd.output() {
1655 Ok(output) => output,
1658 println!("---------------------------------------------------");
1659 println!("ran extra command to debug rust-lang/rust#38620: ");
1660 println!("{:?}", cmd);
1661 println!("result: {}", output.status);
1662 println!("--- stdout ----------------------------------------");
1663 println!("{}", String::from_utf8_lossy(&output.stdout));
1664 println!("--- stderr ----------------------------------------");
1665 println!("{}", String::from_utf8_lossy(&output.stderr));
1666 println!("---------------------------------------------------");
1669 fn _arm_exec_compiled_test(&self, env: Vec<(String, String)>) -> ProcRes {
1670 let args = self.make_run_args();
1671 let cmdline = self.make_cmdline("", &args.prog, &args.args);
1673 // get bare program string
1674 let mut tvec: Vec<String> = args.prog
1678 let prog_short = tvec.pop().unwrap();
1681 let copy_result = procsrv::run("",
1682 &self.config.adb_path,
1687 self.config.adb_test_dir.clone()
1689 vec![("".to_owned(), "".to_owned())],
1690 Some("".to_owned()))
1691 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1693 if self.config.verbose {
1694 println!("push ({}) {} {} {}",
1701 logv(self.config, format!("executing ({}) {}", self.config.target, cmdline));
1703 let mut runargs = Vec::new();
1705 // run test via adb_run_wrapper
1706 runargs.push("shell".to_owned());
1707 for (key, val) in env {
1708 runargs.push(format!("{}={}", key, val));
1710 runargs.push(format!("{}/../adb_run_wrapper.sh", self.config.adb_test_dir));
1711 runargs.push(format!("{}", self.config.adb_test_dir));
1712 runargs.push(format!("{}", prog_short));
1714 for tv in &args.args {
1715 runargs.push(tv.to_owned());
1718 &self.config.adb_path,
1721 vec![("".to_owned(), "".to_owned())], Some("".to_owned()))
1722 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1724 // get exitcode of result
1725 runargs = Vec::new();
1726 runargs.push("shell".to_owned());
1727 runargs.push("cat".to_owned());
1728 runargs.push(format!("{}/{}.exitcode", self.config.adb_test_dir, prog_short));
1730 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1732 &self.config.adb_path,
1735 vec![("".to_owned(), "".to_owned())],
1736 Some("".to_owned()))
1737 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1739 let mut exitcode: i32 = 0;
1740 for c in exitcode_out.chars() {
1741 if !c.is_numeric() { break; }
1742 exitcode = exitcode * 10 + match c {
1743 '0' ... '9' => c as i32 - ('0' as i32),
1748 // get stdout of result
1749 runargs = Vec::new();
1750 runargs.push("shell".to_owned());
1751 runargs.push("cat".to_owned());
1752 runargs.push(format!("{}/{}.stdout", self.config.adb_test_dir, prog_short));
1754 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1756 &self.config.adb_path,
1759 vec![("".to_owned(), "".to_owned())],
1760 Some("".to_owned()))
1761 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1763 // get stderr of result
1764 runargs = Vec::new();
1765 runargs.push("shell".to_owned());
1766 runargs.push("cat".to_owned());
1767 runargs.push(format!("{}/{}.stderr", self.config.adb_test_dir, prog_short));
1769 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1771 &self.config.adb_path,
1774 vec![("".to_owned(), "".to_owned())],
1775 Some("".to_owned()))
1776 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1778 self.dump_output(&stdout_out, &stderr_out);
1781 status: Status::Parsed(exitcode),
1788 fn _arm_push_aux_shared_library(&self) {
1789 let tdir = self.aux_output_dir_name();
1791 let dirs = fs::read_dir(&tdir).unwrap();
1793 let file = file.unwrap().path();
1794 if file.extension().and_then(|s| s.to_str()) == Some("so") {
1795 // FIXME (#9639): This needs to handle non-utf8 paths
1796 let copy_result = procsrv::run("",
1797 &self.config.adb_path,
1804 self.config.adb_test_dir.to_owned(),
1806 vec![("".to_owned(),
1808 Some("".to_owned()))
1809 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1811 if self.config.verbose {
1812 println!("push ({}) {:?} {} {}",
1813 self.config.target, file.display(),
1814 copy_result.out, copy_result.err);
1820 // codegen tests (using FileCheck)
1822 fn compile_test_and_save_ir(&self) -> ProcRes {
1823 let aux_dir = self.aux_output_dir_name();
1824 // FIXME (#9639): This needs to handle non-utf8 paths
1825 let mut link_args = vec!["-L".to_owned(),
1826 aux_dir.to_str().unwrap().to_owned()];
1827 let llvm_args = vec!["--emit=llvm-ir".to_owned(),];
1828 link_args.extend(llvm_args);
1829 let args = self.make_compile_args(link_args,
1830 &self.testpaths.file,
1831 TargetLocation::ThisDirectory(
1832 self.output_base_name().parent()
1835 self.compose_and_run_compiler(args, None)
1838 fn check_ir_with_filecheck(&self) -> ProcRes {
1839 let irfile = self.output_base_name().with_extension("ll");
1840 let prog = self.config.llvm_filecheck.as_ref().unwrap();
1841 let proc_args = ProcArgs {
1842 // FIXME (#9639): This needs to handle non-utf8 paths
1843 prog: prog.to_str().unwrap().to_owned(),
1844 args: vec![format!("-input-file={}", irfile.to_str().unwrap()),
1845 self.testpaths.file.to_str().unwrap().to_owned()]
1847 self.compose_and_run(proc_args, Vec::new(), "", None, None)
1850 fn run_codegen_test(&self) {
1851 assert!(self.revision.is_none(), "revisions not relevant here");
1853 if self.config.llvm_filecheck.is_none() {
1854 self.fatal("missing --llvm-filecheck");
1857 let mut proc_res = self.compile_test_and_save_ir();
1858 if !proc_res.status.success() {
1859 self.fatal_proc_rec("compilation failed!", &proc_res);
1862 proc_res = self.check_ir_with_filecheck();
1863 if !proc_res.status.success() {
1864 self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
1868 fn charset() -> &'static str {
1869 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1870 if cfg!(target_os = "bitrig") {
1872 } else if cfg!(target_os = "freebsd") {
1879 fn run_rustdoc_test(&self) {
1880 assert!(self.revision.is_none(), "revisions not relevant here");
1882 let out_dir = self.output_base_name();
1883 let _ = fs::remove_dir_all(&out_dir);
1884 self.create_dir_racy(&out_dir);
1886 let proc_res = self.document(&out_dir);
1887 if !proc_res.status.success() {
1888 self.fatal_proc_rec("rustdoc failed!", &proc_res);
1890 let root = self.find_rust_src_root().unwrap();
1892 let res = self.cmd2procres(Command::new(&self.config.docck_python)
1893 .arg(root.join("src/etc/htmldocck.py"))
1895 .arg(&self.testpaths.file));
1896 if !res.status.success() {
1897 self.fatal_proc_rec("htmldocck failed!", &res);
1901 fn run_codegen_units_test(&self) {
1902 assert!(self.revision.is_none(), "revisions not relevant here");
1904 let proc_res = self.compile_test();
1906 if !proc_res.status.success() {
1907 self.fatal_proc_rec("compilation failed!", &proc_res);
1910 self.check_no_compiler_crash(&proc_res);
1912 const PREFIX: &'static str = "TRANS_ITEM ";
1913 const CGU_MARKER: &'static str = "@@";
1915 let actual: Vec<TransItem> = proc_res
1918 .filter(|line| line.starts_with(PREFIX))
1919 .map(str_to_trans_item)
1922 let expected: Vec<TransItem> = errors::load_errors(&self.testpaths.file, None)
1924 .map(|e| str_to_trans_item(&e.msg[..]))
1927 let mut missing = Vec::new();
1928 let mut wrong_cgus = Vec::new();
1930 for expected_item in &expected {
1931 let actual_item_with_same_name = actual.iter()
1932 .find(|ti| ti.name == expected_item.name);
1934 if let Some(actual_item) = actual_item_with_same_name {
1935 if !expected_item.codegen_units.is_empty() {
1936 // Also check for codegen units
1937 if expected_item.codegen_units != actual_item.codegen_units {
1938 wrong_cgus.push((expected_item.clone(), actual_item.clone()));
1942 missing.push(expected_item.string.clone());
1946 let unexpected: Vec<_> =
1948 .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
1949 .map(|acgu| acgu.string.clone())
1952 if !missing.is_empty() {
1955 println!("\nThese items should have been contained but were not:\n");
1957 for item in &missing {
1958 println!("{}", item);
1964 if !unexpected.is_empty() {
1966 let mut sorted = unexpected.clone();
1971 println!("\nThese items were contained but should not have been:\n");
1973 for item in sorted {
1974 println!("{}", item);
1980 if !wrong_cgus.is_empty() {
1981 wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
1982 println!("\nThe following items were assigned to wrong codegen units:\n");
1984 for &(ref expected_item, ref actual_item) in &wrong_cgus {
1985 println!("{}", expected_item.name);
1986 println!(" expected: {}", codegen_units_to_str(&expected_item.codegen_units));
1987 println!(" actual: {}", codegen_units_to_str(&actual_item.codegen_units));
1992 if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty())
1997 #[derive(Clone, Eq, PartialEq)]
2000 codegen_units: HashSet<String>,
2004 // [TRANS_ITEM] name [@@ (cgu)+]
2005 fn str_to_trans_item(s: &str) -> TransItem {
2006 let s = if s.starts_with(PREFIX) {
2007 (&s[PREFIX.len()..]).trim()
2012 let full_string = format!("{}{}", PREFIX, s.trim().to_owned());
2014 let parts: Vec<&str> = s.split(CGU_MARKER)
2016 .filter(|s| !s.is_empty())
2019 let name = parts[0].trim();
2021 let cgus = if parts.len() > 1 {
2022 let cgus_str = parts[1];
2026 .filter(|s| !s.is_empty())
2035 name: name.to_owned(),
2036 codegen_units: cgus,
2037 string: full_string,
2041 fn codegen_units_to_str(cgus: &HashSet<String>) -> String
2043 let mut cgus: Vec<_> = cgus.iter().collect();
2046 let mut string = String::new();
2048 string.push_str(&cgu[..]);
2049 string.push_str(" ");
2056 fn init_incremental_test(&self) {
2057 // (See `run_incremental_test` for an overview of how incremental tests work.)
2059 // Before any of the revisions have executed, create the
2060 // incremental workproduct directory. Delete any old
2061 // incremental work products that may be there from prior
2063 let incremental_dir = self.incremental_dir();
2064 if incremental_dir.exists() {
2065 // Canonicalizing the path will convert it to the //?/ format
2066 // on Windows, which enables paths longer than 260 character
2067 let canonicalized = incremental_dir.canonicalize().unwrap();
2068 fs::remove_dir_all(canonicalized).unwrap();
2070 fs::create_dir_all(&incremental_dir).unwrap();
2072 if self.config.verbose {
2073 print!("init_incremental_test: incremental_dir={}", incremental_dir.display());
2077 fn run_incremental_test(&self) {
2078 // Basic plan for a test incremental/foo/bar.rs:
2079 // - load list of revisions rpass1, cfail2, rpass3
2080 // - each should begin with `rpass`, `cfail`, or `cfail`
2081 // - if `rpass`, expect compile and execution to succeed
2082 // - if `cfail`, expect compilation to fail
2083 // - if `rfail`, expect execution to fail
2084 // - create a directory build/foo/bar.incremental
2085 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
2086 // - because name of revision starts with "rpass", expect success
2087 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
2088 // - because name of revision starts with "cfail", expect an error
2089 // - load expected errors as usual, but filter for those that end in `[rfail2]`
2090 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
2091 // - because name of revision starts with "rpass", expect success
2092 // - execute build/foo/bar.exe and save output
2094 // FIXME -- use non-incremental mode as an oracle? That doesn't apply
2095 // to #[rustc_dirty] and clean tests I guess
2097 let revision = self.revision.expect("incremental tests require a list of revisions");
2099 // Incremental workproduct directory should have already been created.
2100 let incremental_dir = self.incremental_dir();
2101 assert!(incremental_dir.exists(), "init_incremental_test failed to create incremental dir");
2103 // Add an extra flag pointing at the incremental directory.
2104 let mut revision_props = self.props.clone();
2105 revision_props.incremental_dir = Some(incremental_dir);
2106 revision_props.compile_flags.push(String::from("-Zincremental-info"));
2108 let revision_cx = TestCx {
2109 config: self.config,
2110 props: &revision_props,
2111 testpaths: self.testpaths,
2112 revision: self.revision,
2115 if self.config.verbose {
2116 print!("revision={:?} revision_props={:#?}", revision, revision_props);
2119 if revision.starts_with("rpass") {
2120 revision_cx.run_rpass_test();
2121 } else if revision.starts_with("rfail") {
2122 revision_cx.run_rfail_test();
2123 } else if revision.starts_with("cfail") {
2124 revision_cx.run_cfail_test();
2127 "revision name must begin with rpass, rfail, or cfail");
2131 /// Directory where incremental work products are stored.
2132 fn incremental_dir(&self) -> PathBuf {
2133 self.output_base_name().with_extension("inc")
2136 fn run_rmake_test(&self) {
2137 // FIXME(#11094): we should fix these tests
2138 if self.config.host != self.config.target {
2142 let cwd = env::current_dir().unwrap();
2143 let src_root = self.config.src_base.parent().unwrap()
2146 let src_root = cwd.join(&src_root);
2148 let tmpdir = cwd.join(self.output_base_name());
2149 if tmpdir.exists() {
2150 self.aggressive_rm_rf(&tmpdir).unwrap();
2152 self.create_dir_racy(&tmpdir);
2154 let host = &self.config.host;
2155 let make = if host.contains("bitrig") || host.contains("dragonfly") ||
2156 host.contains("freebsd") || host.contains("netbsd") ||
2157 host.contains("openbsd") {
2163 let mut cmd = Command::new(make);
2164 cmd.current_dir(&self.testpaths.file)
2165 .env("TARGET", &self.config.target)
2166 .env("PYTHON", &self.config.docck_python)
2168 .env("RUST_BUILD_STAGE", &self.config.stage_id)
2169 .env("RUSTC", cwd.join(&self.config.rustc_path))
2170 .env("RUSTDOC", cwd.join(&self.config.rustdoc_path))
2171 .env("TMPDIR", &tmpdir)
2172 .env("LD_LIB_PATH_ENVVAR", procsrv::dylib_env_var())
2173 .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2174 .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2175 .env("LLVM_COMPONENTS", &self.config.llvm_components)
2176 .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags);
2178 // We don't want RUSTFLAGS set from the outside to interfere with
2179 // compiler flags set in the test cases:
2180 cmd.env_remove("RUSTFLAGS");
2182 if self.config.target.contains("msvc") {
2183 // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2184 // and that `lib.exe` lives next to it.
2185 let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2187 // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2188 // a path and instead passes `C:\msys64\foo`, so convert all
2189 // `/`-arguments to MSVC here to `-` arguments.
2190 let cflags = self.config.cflags.split(' ').map(|s| s.replace("/", "-"))
2191 .collect::<Vec<_>>().join(" ");
2193 cmd.env("IS_MSVC", "1")
2194 .env("IS_WINDOWS", "1")
2195 .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2196 .env("CC", format!("'{}' {}", self.config.cc, cflags))
2197 .env("CXX", &self.config.cxx);
2199 cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2200 .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags));
2202 if self.config.target.contains("windows") {
2203 cmd.env("IS_WINDOWS", "1");
2207 let output = cmd.output().expect("failed to spawn `make`");
2208 if !output.status.success() {
2210 status: Status::Normal(output.status),
2211 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2212 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2213 cmdline: format!("{:?}", cmd),
2215 self.fatal_proc_rec("make failed", &res);
2219 fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
2220 for e in path.read_dir()? {
2222 let path = entry.path();
2223 if entry.file_type()?.is_dir() {
2224 self.aggressive_rm_rf(&path)?;
2226 // Remove readonly files as well on windows (by default we can't)
2227 fs::remove_file(&path).or_else(|e| {
2228 if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
2229 let mut meta = entry.metadata()?.permissions();
2230 meta.set_readonly(false);
2231 fs::set_permissions(&path, meta)?;
2232 fs::remove_file(&path)
2239 fs::remove_dir(path)
2242 fn run_ui_test(&self) {
2243 println!("ui: {}", self.testpaths.file.display());
2245 let proc_res = self.compile_test();
2247 let expected_stderr_path = self.expected_output_path("stderr");
2248 let expected_stderr = self.load_expected_output(&expected_stderr_path);
2250 let expected_stdout_path = self.expected_output_path("stdout");
2251 let expected_stdout = self.load_expected_output(&expected_stdout_path);
2253 let normalized_stdout = self.normalize_output(&proc_res.stdout);
2254 let normalized_stderr = self.normalize_output(&proc_res.stderr);
2257 errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2258 errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2261 println!("To update references, run this command from build directory:");
2262 let relative_path_to_file =
2263 self.testpaths.relative_dir
2264 .join(self.testpaths.file.file_name().unwrap());
2265 println!("{}/update-references.sh '{}' '{}'",
2266 self.config.src_base.display(),
2267 self.config.build_base.display(),
2268 relative_path_to_file.display());
2269 self.fatal_proc_rec(&format!("{} errors occurred comparing output.", errors),
2274 fn run_mir_opt_test(&self) {
2275 let proc_res = self.compile_test();
2277 if !proc_res.status.success() {
2278 self.fatal_proc_rec("compilation failed!", &proc_res);
2281 let proc_res = self.exec_compiled_test();
2283 if !proc_res.status.success() {
2284 self.fatal_proc_rec("test run failed!", &proc_res);
2286 self.check_mir_dump();
2289 fn check_mir_dump(&self) {
2290 let mut test_file_contents = String::new();
2291 fs::File::open(self.testpaths.file.clone()).unwrap()
2292 .read_to_string(&mut test_file_contents)
2294 if let Some(idx) = test_file_contents.find("// END RUST SOURCE") {
2295 let (_, tests_text) = test_file_contents.split_at(idx + "// END_RUST SOURCE".len());
2296 let tests_text_str = String::from(tests_text);
2297 let mut curr_test : Option<&str> = None;
2298 let mut curr_test_contents = Vec::new();
2299 for l in tests_text_str.lines() {
2300 debug!("line: {:?}", l);
2301 if l.starts_with("// START ") {
2302 let (_, t) = l.split_at("// START ".len());
2303 curr_test = Some(t);
2304 } else if l.starts_with("// END") {
2305 let (_, t) = l.split_at("// END ".len());
2306 if Some(t) != curr_test {
2307 panic!("mismatched START END test name");
2309 self.compare_mir_test_output(curr_test.unwrap(), &curr_test_contents);
2311 curr_test_contents.clear();
2312 } else if l.is_empty() {
2314 } else if l.starts_with("// ") {
2315 let (_, test_content) = l.split_at("// ".len());
2316 curr_test_contents.push(test_content);
2322 fn compare_mir_test_output(&self, test_name: &str, expected_content: &Vec<&str>) {
2323 let mut output_file = PathBuf::new();
2324 output_file.push(self.get_mir_dump_dir());
2325 output_file.push(test_name);
2326 debug!("comparing the contests of: {:?}", output_file);
2327 debug!("with: {:?}", expected_content);
2329 let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
2330 let mut dumped_string = String::new();
2331 dumped_file.read_to_string(&mut dumped_string).unwrap();
2332 let mut dumped_lines = dumped_string.lines().filter(|l| !l.is_empty());
2333 let mut expected_lines = expected_content.iter().filter(|l| !l.is_empty());
2335 // We expect each non-empty line from expected_content to appear
2336 // in the dump in order, but there may be extra lines interleaved
2337 while let Some(expected_line) = expected_lines.next() {
2338 let e_norm = normalize_mir_line(expected_line);
2339 if e_norm.is_empty() {
2342 let mut found = false;
2343 while let Some(dumped_line) = dumped_lines.next() {
2344 let d_norm = normalize_mir_line(dumped_line);
2345 debug!("found: {:?}", d_norm);
2346 debug!("expected: {:?}", e_norm);
2347 if e_norm == d_norm {
2353 let normalize_all = dumped_string.lines()
2354 .map(nocomment_mir_line)
2355 .filter(|l| !l.is_empty())
2356 .collect::<Vec<_>>()
2358 panic!("ran out of mir dump output to match against.\n\
2359 Did not find expected line: {:?}\n\
2363 expected_content.join("\n"),
2369 fn get_mir_dump_dir(&self) -> PathBuf {
2370 let mut mir_dump_dir = PathBuf::from(self.config.build_base
2374 debug!("input_file: {:?}", self.testpaths.file);
2375 mir_dump_dir.push(self.testpaths.file.file_stem().unwrap().to_str().unwrap());
2379 fn normalize_output(&self, output: &str) -> String {
2380 let parent_dir = self.testpaths.file.parent().unwrap();
2381 let parent_dir_str = parent_dir.display().to_string();
2382 output.replace(&parent_dir_str, "$DIR")
2383 .replace("\\", "/") // normalize for paths on windows
2384 .replace("\r\n", "\n") // normalize for linebreaks on windows
2385 .replace("\t", "\\t") // makes tabs visible
2388 fn expected_output_path(&self, kind: &str) -> PathBuf {
2389 let extension = match self.revision {
2390 Some(r) => format!("{}.{}", r, kind),
2391 None => kind.to_string(),
2393 self.testpaths.file.with_extension(extension)
2396 fn load_expected_output(&self, path: &Path) -> String {
2398 return String::new();
2401 let mut result = String::new();
2402 match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
2405 self.fatal(&format!("failed to load expected output from `{}`: {}",
2411 fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2412 if actual == expected {
2416 println!("normalized {}:\n{}\n", kind, actual);
2417 println!("expected {}:\n{}\n", kind, expected);
2418 println!("diff of {}:\n", kind);
2419 for line in uidiff::diff_lines(actual, expected) {
2420 println!("{}", line);
2423 let output_file = self.output_base_name().with_extension(kind);
2424 match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
2427 self.fatal(&format!("failed to write {} to `{}`: {}",
2428 kind, output_file.display(), e))
2432 println!("\nThe actual {0} differed from the expected {0}.", kind);
2433 println!("Actual {} saved to {}", kind, output_file.display());
2443 pub struct ProcRes {
2456 pub fn fatal(&self, err: Option<&str>) -> ! {
2457 if let Some(e) = err {
2458 println!("\nerror: {}", e);
2464 ------------------------------------------\n\
2466 ------------------------------------------\n\
2468 ------------------------------------------\n\
2470 ------------------------------------------\n\
2472 self.status, self.cmdline, self.stdout,
2479 fn code(&self) -> Option<i32> {
2481 Status::Parsed(i) => Some(i),
2482 Status::Normal(ref e) => e.code(),
2486 fn success(&self) -> bool {
2488 Status::Parsed(i) => i == 0,
2489 Status::Normal(ref e) => e.success(),
2494 impl fmt::Display for Status {
2495 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2497 Status::Parsed(i) => write!(f, "exit code: {}", i),
2498 Status::Normal(ref e) => e.fmt(f),
2503 enum TargetLocation {
2505 ThisDirectory(PathBuf),
2508 fn normalize_mir_line(line: &str) -> String {
2509 nocomment_mir_line(line).replace(char::is_whitespace, "")
2512 fn nocomment_mir_line(line: &str) -> &str {
2513 if let Some(idx) = line.find("//") {
2514 let (l, _) = line.split_at(idx);