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, Pretty, RunFail, RunPass, DebugInfoGdb};
13 use common::{Codegen, DebugInfoLldb};
15 use header::TestProps;
19 #[cfg(target_os = "windows")]
23 use std::io::fs::PathExtensions;
25 use std::io::net::tcp;
26 use std::io::process::ProcessExit;
32 use std::string::String;
34 use std::time::Duration;
37 pub fn run(config: Config, testfile: String) {
39 match config.target.as_slice() {
41 "arm-linux-androideabi" => {
42 if !config.adb_device_status {
43 fail!("android device not available");
50 let mut _mm = MetricMap::new();
51 run_metrics(config, testfile, &mut _mm);
54 pub fn run_metrics(config: Config, testfile: String, mm: &mut MetricMap) {
56 // We're going to be dumping a lot of info. Start on a new line.
59 let testfile = Path::new(testfile);
60 debug!("running {}", testfile.display());
61 let props = header::load_props(&testfile);
62 debug!("loaded props");
64 CompileFail => run_cfail_test(&config, &props, &testfile),
65 RunFail => run_rfail_test(&config, &props, &testfile),
66 RunPass => run_rpass_test(&config, &props, &testfile),
67 Pretty => run_pretty_test(&config, &props, &testfile),
68 DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
69 DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
70 Codegen => run_codegen_test(&config, &props, &testfile, mm),
74 fn get_output(props: &TestProps, proc_res: &ProcRes) -> String {
75 if props.check_stdout {
76 format!("{}{}", proc_res.stdout, proc_res.stderr)
78 proc_res.stderr.clone()
82 fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
83 let proc_res = compile_test(config, props, testfile);
85 if proc_res.status.success() {
86 fatal_proc_rec("compile-fail test compiled successfully!",
90 check_correct_failure_status(&proc_res);
92 if proc_res.status.success() {
93 fatal("process did not return an error status");
96 let output_to_check = get_output(props, &proc_res);
97 let expected_errors = errors::load_errors(&config.cfail_regex, testfile);
98 if !expected_errors.is_empty() {
99 if !props.error_patterns.is_empty() {
100 fatal("both error pattern and expected errors specified");
102 check_expected_errors(expected_errors, testfile, &proc_res);
104 check_error_patterns(props, testfile, output_to_check.as_slice(), &proc_res);
106 check_no_compiler_crash(&proc_res);
107 check_forbid_output(props, output_to_check.as_slice(), &proc_res);
110 fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
111 let proc_res = if !config.jit {
112 let proc_res = compile_test(config, props, testfile);
114 if !proc_res.status.success() {
115 fatal_proc_rec("compilation failed!", &proc_res);
118 exec_compiled_test(config, props, testfile)
120 jit_test(config, props, testfile)
123 // The value our Makefile configures valgrind to return on failure
124 static VALGRIND_ERR: int = 100;
125 if proc_res.status.matches_exit_status(VALGRIND_ERR) {
126 fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
129 let output_to_check = get_output(props, &proc_res);
130 check_correct_failure_status(&proc_res);
131 check_error_patterns(props, testfile, output_to_check.as_slice(), &proc_res);
134 fn check_correct_failure_status(proc_res: &ProcRes) {
135 // The value the rust runtime returns on failure
136 static RUST_ERR: int = 101;
137 if !proc_res.status.matches_exit_status(RUST_ERR) {
139 format!("failure produced the wrong error: {}",
140 proc_res.status).as_slice(),
145 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
147 let mut proc_res = compile_test(config, props, testfile);
149 if !proc_res.status.success() {
150 fatal_proc_rec("compilation failed!", &proc_res);
153 proc_res = exec_compiled_test(config, props, testfile);
155 if !proc_res.status.success() {
156 fatal_proc_rec("test run failed!", &proc_res);
159 let proc_res = jit_test(config, props, testfile);
161 if !proc_res.status.success() {
162 fatal_proc_rec("jit failed!", &proc_res);
167 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
168 if props.pp_exact.is_some() {
169 logv(config, "testing for exact pretty-printing".to_string());
171 logv(config, "testing for converging pretty-printing".to_string());
175 match props.pp_exact { Some(_) => 1, None => 2 };
177 let src = File::open(testfile).read_to_end().unwrap();
178 let src = String::from_utf8(src.clone()).unwrap();
179 let mut srcs = vec!(src);
182 while round < rounds {
183 logv(config, format!("pretty-printing round {}", round));
184 let proc_res = print_source(config,
187 srcs[round].to_string(),
188 props.pretty_mode.as_slice());
190 if !proc_res.status.success() {
191 fatal_proc_rec(format!("pretty-printing failed in round {}",
196 let ProcRes{ stdout, .. } = proc_res;
201 let mut expected = match props.pp_exact {
203 let filepath = testfile.dir_path().join(file);
204 let s = File::open(&filepath).read_to_end().unwrap();
205 String::from_utf8(s).unwrap()
207 None => { srcs[srcs.len() - 2u].clone() }
209 let mut actual = srcs[srcs.len() - 1u].clone();
211 if props.pp_exact.is_some() {
212 // Now we have to care about line endings
213 let cr = "\r".to_string();
214 actual = actual.replace(cr.as_slice(), "").to_string();
215 expected = expected.replace(cr.as_slice(), "").to_string();
218 compare_source(expected.as_slice(), actual.as_slice());
220 // If we're only making sure that the output matches then just stop here
221 if props.pretty_compare_only { return; }
223 // Finally, let's make sure it actually appears to remain valid code
224 let proc_res = typecheck_source(config, props, testfile, actual);
226 if !proc_res.status.success() {
227 fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
229 if props.no_pretty_expanded { return }
231 // additionally, run `--pretty expanded` and try to build it.
232 let proc_res = print_source(config, props, testfile, srcs[round].clone(), "expanded");
233 if !proc_res.status.success() {
234 fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
237 let ProcRes{ stdout: expanded_src, .. } = proc_res;
238 let proc_res = typecheck_source(config, props, testfile, expanded_src);
239 if !proc_res.status.success() {
240 fatal_proc_rec("pretty-printed source (expanded) does not typecheck",
246 fn print_source(config: &Config,
250 pretty_type: &str) -> ProcRes {
251 let aux_dir = aux_output_dir_name(config, testfile);
252 compose_and_run(config,
257 pretty_type.to_string()),
258 props.exec_env.clone(),
259 config.compile_lib_path.as_slice(),
260 Some(aux_dir.as_str().unwrap()),
264 fn make_pp_args(config: &Config,
267 pretty_type: String) -> ProcArgs {
268 let aux_dir = aux_output_dir_name(config, testfile);
269 // FIXME (#9639): This needs to handle non-utf8 paths
270 let mut args = vec!("-".to_string(),
271 "--pretty".to_string(),
273 format!("--target={}", config.target),
275 aux_dir.as_str().unwrap().to_string());
276 args.extend(split_maybe_args(&config.target_rustcflags).into_iter());
277 args.extend(split_maybe_args(&props.compile_flags).into_iter());
279 prog: config.rustc_path.as_str().unwrap().to_string(),
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 {
311 config.host.as_slice()
313 config.target.as_slice()
315 // FIXME (#9639): This needs to handle non-utf8 paths
316 let mut args = vec!("-".to_string(),
317 "--no-trans".to_string(),
318 "--crate-type=lib".to_string(),
319 format!("--target={}", target),
321 config.build_base.as_str().unwrap().to_string(),
323 aux_dir.as_str().unwrap().to_string());
324 args.extend(split_maybe_args(&config.target_rustcflags).into_iter());
325 args.extend(split_maybe_args(&props.compile_flags).into_iter());
326 // FIXME (#9639): This needs to handle non-utf8 paths
328 prog: config.rustc_path.as_str().unwrap().to_string(),
334 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
335 let mut config = Config {
336 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
337 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
341 let config = &mut config;
342 let DebuggerCommands {
345 use_gdb_pretty_printer,
347 } = parse_debugger_commands(testfile, "gdb");
348 let mut cmds = commands.connect("\n");
350 // compile test file (it should have 'compile-flags:-g' in the header)
351 let compiler_run_result = compile_test(config, props, testfile);
352 if !compiler_run_result.status.success() {
353 fatal_proc_rec("compilation failed!", &compiler_run_result);
356 let exe_file = make_exe_name(config, testfile);
358 let debugger_run_result;
359 match config.target.as_slice() {
360 "arm-linux-androideabi" => {
362 cmds = cmds.replace("run", "continue").to_string();
364 // write debugger script
365 let script_str = ["set charset UTF-8".to_string(),
366 format!("file {}", exe_file.as_str().unwrap()
368 "target remote :5039".to_string(),
370 "quit".to_string()].connect("\n");
371 debug!("script_str = {}", script_str);
372 dump_output_file(config,
374 script_str.as_slice(),
379 config.adb_path.as_slice(),
383 exe_file.as_str().unwrap().to_string(),
384 config.adb_test_dir.clone()
386 vec!(("".to_string(), "".to_string())),
387 Some("".to_string()))
388 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
391 config.adb_path.as_slice(),
394 "forward".to_string(),
395 "tcp:5039".to_string(),
396 "tcp:5039".to_string()
398 vec!(("".to_string(), "".to_string())),
399 Some("".to_string()))
400 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
402 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
403 gdbserver :5039 {}/{}",
404 config.adb_test_dir.clone(),
405 config.adb_test_dir.clone(),
408 .unwrap()).unwrap());
410 let mut process = procsrv::run_background("",
418 vec!(("".to_string(),
420 Some("".to_string()))
421 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
423 //waiting 1 second for gdbserver start
424 timer::sleep(Duration::milliseconds(1000));
425 let result = task::try(proc() {
426 tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
434 let tool_path = match config.android_cross_path.as_str() {
435 Some(x) => x.to_string(),
436 None => fatal("cannot find android cross path")
439 let debugger_script = make_out_name(config, testfile, "debugger.script");
440 // FIXME (#9639): This needs to handle non-utf8 paths
442 vec!("-quiet".to_string(),
443 "-batch".to_string(),
445 format!("-command={}", debugger_script.as_str().unwrap()));
447 let mut gdb_path = tool_path;
448 gdb_path.push_str("/bin/arm-linux-androideabi-gdb");
449 let procsrv::Result {
456 debugger_opts.as_slice(),
457 vec!(("".to_string(), "".to_string())),
459 .expect(format!("failed to exec `{}`", gdb_path).as_slice());
461 let cmdline = make_cmdline("",
462 "arm-linux-androideabi-gdb",
463 debugger_opts.as_slice());
464 logv(config, format!("executing {}", cmdline));
468 debugger_run_result = ProcRes {
474 process.signal_kill().unwrap();
478 let rust_src_root = find_rust_src_root(config)
479 .expect("Could not find Rust source root");
480 let rust_pp_module_rel_path = Path::new("./src/etc");
481 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
485 // write debugger script
486 let mut script_str = String::with_capacity(2048);
488 script_str.push_str("set charset UTF-8\n");
489 script_str.push_str("show version\n");
491 match config.gdb_version {
492 Some(ref version) => {
493 println!("NOTE: compiletest thinks it is using GDB version {}",
496 if header::gdb_version_to_int(version.as_slice()) >
497 header::gdb_version_to_int("7.4") {
498 // Add the directory containing the pretty printers to
499 // GDB's script auto loading safe path ...
501 format!("add-auto-load-safe-path {}\n",
502 rust_pp_module_abs_path.replace("\\", "\\\\").as_slice())
504 // ... and also the test directory
506 format!("add-auto-load-safe-path {}\n",
507 config.build_base.as_str().unwrap().replace("\\", "\\\\"))
512 println!("NOTE: compiletest does not know which version of \
517 // Load the target executable
518 script_str.push_str(format!("file {}\n",
519 exe_file.as_str().unwrap().replace("\\", "\\\\"))
522 script_str.push_str(cmds.as_slice());
523 script_str.push_str("quit\n");
525 debug!("script_str = {}", script_str);
526 dump_output_file(config,
528 script_str.as_slice(),
531 if use_gdb_pretty_printer {
532 // Only emit the gdb auto-loading script if pretty printers
533 // should actually be loaded
534 dump_gdb_autoload_script(config, testfile);
537 // run debugger script with gdb
539 fn debugger() -> String {
540 "gdb.exe".to_string()
543 fn debugger() -> String {
547 let debugger_script = make_out_name(config, testfile, "debugger.script");
549 // FIXME (#9639): This needs to handle non-utf8 paths
551 vec!("-quiet".to_string(),
552 "-batch".to_string(),
554 format!("-command={}", debugger_script.as_str().unwrap()));
556 let proc_args = ProcArgs {
561 let environment = vec![("PYTHONPATH".to_string(), rust_pp_module_abs_path)];
563 debugger_run_result = compose_and_run(config,
567 config.run_lib_path.as_slice(),
573 if !debugger_run_result.status.success() {
574 fatal("gdb failed to execute");
577 check_debugger_output(&debugger_run_result, check_lines.as_slice());
579 fn dump_gdb_autoload_script(config: &Config, testfile: &Path) {
580 let mut script_path = output_base_name(config, testfile);
581 let mut script_file_name = script_path.filename().unwrap().to_vec();
582 script_file_name.push_all("-gdb.py".as_bytes());
583 script_path.set_filename(script_file_name.as_slice());
585 let script_content = "import gdb_rust_pretty_printing\n\
586 gdb_rust_pretty_printing.register_printers(gdb.current_objfile())\n"
589 File::create(&script_path).write(script_content).unwrap();
593 fn find_rust_src_root(config: &Config) -> Option<Path> {
594 let mut path = config.src_base.clone();
595 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
598 if path.join(path_postfix.clone()).is_file() {
606 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
607 use std::io::process::{Command, ProcessOutput};
609 if config.lldb_python_dir.is_none() {
610 fatal("Can't run LLDB test because LLDB's python path is not set.");
613 let mut config = Config {
614 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
615 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
619 let config = &mut config;
621 // compile test file (it should have 'compile-flags:-g' in the header)
622 let compile_result = compile_test(config, props, testfile);
623 if !compile_result.status.success() {
624 fatal_proc_rec("compilation failed!", &compile_result);
627 let exe_file = make_exe_name(config, testfile);
629 // Parse debugger commands etc from test files
630 let DebuggerCommands {
635 } = parse_debugger_commands(testfile, "lldb");
637 // Write debugger script:
638 // We don't want to hang when calling `quit` while the process is still running
639 let mut script_str = String::from_str("settings set auto-confirm true\n");
641 // Make LLDB emit its version, so we have it documented in the test output
642 script_str.push_str("version\n");
644 // Switch LLDB into "Rust mode"
645 script_str.push_str("command script import ./src/etc/lldb_rust_formatters.py\n");
646 script_str.push_str("type summary add --no-value ");
647 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
648 script_str.push_str("-x \".*\" --category Rust\n");
649 script_str.push_str("type category enable Rust\n");
651 // Set breakpoints on every line that contains the string "#break"
652 for line in breakpoint_lines.iter() {
653 script_str.push_str(format!("breakpoint set --line {}\n",
657 // Append the other commands
658 for line in commands.iter() {
659 script_str.push_str(line.as_slice());
660 script_str.push_str("\n");
663 // Finally, quit the debugger
664 script_str.push_str("quit\n");
666 // Write the script into a file
667 debug!("script_str = {}", script_str);
668 dump_output_file(config,
670 script_str.as_slice(),
672 let debugger_script = make_out_name(config, testfile, "debugger.script");
674 // Let LLDB execute the script via lldb_batchmode.py
675 let debugger_run_result = run_lldb(config, &exe_file, &debugger_script);
677 if !debugger_run_result.status.success() {
678 fatal_proc_rec("Error while running LLDB", &debugger_run_result);
681 check_debugger_output(&debugger_run_result, check_lines.as_slice());
683 fn run_lldb(config: &Config, test_executable: &Path, debugger_script: &Path) -> ProcRes {
684 // Prepare the lldb_batchmode which executes the debugger script
685 let mut cmd = Command::new("python");
686 cmd.arg("./src/etc/lldb_batchmode.py")
687 .arg(test_executable)
688 .arg(debugger_script)
689 .env_set_all([("PYTHONPATH", config.lldb_python_dir.clone().unwrap().as_slice())]);
691 let (status, out, err) = match cmd.spawn() {
693 let ProcessOutput { status, output, error } =
694 process.wait_with_output().unwrap();
697 String::from_utf8(output).unwrap(),
698 String::from_utf8(error).unwrap())
701 fatal(format!("Failed to setup Python process for \
702 LLDB script: {}", e).as_slice())
706 dump_output(config, test_executable, out.as_slice(), err.as_slice());
711 cmdline: format!("{}", cmd)
716 struct DebuggerCommands {
717 commands: Vec<String>,
718 check_lines: Vec<String>,
719 breakpoint_lines: Vec<uint>,
720 use_gdb_pretty_printer: bool
723 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
724 -> DebuggerCommands {
725 use std::io::{BufferedReader, File};
727 let command_directive = format!("{}-command", debugger_prefix);
728 let check_directive = format!("{}-check", debugger_prefix);
730 let mut breakpoint_lines = vec!();
731 let mut commands = vec!();
732 let mut check_lines = vec!();
733 let mut use_gdb_pretty_printer = false;
735 let mut reader = BufferedReader::new(File::open(file_path).unwrap());
736 for line in reader.lines() {
739 if line.as_slice().contains("#break") {
740 breakpoint_lines.push(counter);
743 if line.as_slice().contains("gdb-use-pretty-printer") {
744 use_gdb_pretty_printer = true;
747 header::parse_name_value_directive(
749 command_directive.as_slice()).map(|cmd| {
753 header::parse_name_value_directive(
755 check_directive.as_slice()).map(|cmd| {
756 check_lines.push(cmd)
760 fatal(format!("Error while parsing debugger commands: {}",
769 check_lines: check_lines,
770 breakpoint_lines: breakpoint_lines,
771 use_gdb_pretty_printer: use_gdb_pretty_printer,
775 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
776 if options.is_none() {
780 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
781 let options_to_remove = [
784 "--debuginfo".to_string()
787 split_maybe_args(options).into_iter()
788 .filter(|x| !options_to_remove.contains(x))
789 .collect::<Vec<String>>()
794 fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String]) {
795 let num_check_lines = check_lines.len();
796 if num_check_lines > 0 {
797 // Allow check lines to leave parts unspecified (e.g., uninitialized
798 // bits in the wrong case of an enum) with the notation "[...]".
799 let check_fragments: Vec<Vec<String>> =
800 check_lines.iter().map(|s| {
804 .map(|x| x.to_string())
807 // check if each line in props.check_lines appears in the
810 for line in debugger_run_result.stdout.as_slice().lines() {
811 let mut rest = line.trim();
812 let mut first = true;
813 let mut failed = false;
814 for frag in check_fragments[i].iter() {
815 let found = if first {
816 if rest.starts_with(frag.as_slice()) {
822 rest.find_str(frag.as_slice())
830 rest = rest.slice_from(i + frag.len());
835 if !failed && rest.len() == 0 {
838 if i == num_check_lines {
843 if i != num_check_lines {
844 fatal_proc_rec(format!("line not found in debugger output: {}",
845 check_lines.get(i).unwrap()).as_slice(),
846 debugger_run_result);
851 fn check_error_patterns(props: &TestProps,
853 output_to_check: &str,
854 proc_res: &ProcRes) {
855 if props.error_patterns.is_empty() {
856 fatal(format!("no error pattern specified in {}",
857 testfile.display()).as_slice());
859 let mut next_err_idx = 0u;
860 let mut next_err_pat = &props.error_patterns[next_err_idx];
861 let mut done = false;
862 for line in output_to_check.as_slice().lines() {
863 if line.contains(next_err_pat.as_slice()) {
864 debug!("found error pattern {}", next_err_pat);
866 if next_err_idx == props.error_patterns.len() {
867 debug!("found all error patterns");
871 next_err_pat = &props.error_patterns[next_err_idx];
876 let missing_patterns =
877 props.error_patterns.slice(next_err_idx, props.error_patterns.len());
878 if missing_patterns.len() == 1u {
879 fatal_proc_rec(format!("error pattern '{}' not found!",
880 missing_patterns[0]).as_slice(),
883 for pattern in missing_patterns.iter() {
884 error(format!("error pattern '{}' not found!",
885 *pattern).as_slice());
887 fatal_proc_rec("multiple error patterns not found", proc_res);
891 fn check_no_compiler_crash(proc_res: &ProcRes) {
892 for line in proc_res.stderr.as_slice().lines() {
893 if line.starts_with("error: internal compiler error:") {
894 fatal_proc_rec("compiler encountered internal error",
900 fn check_forbid_output(props: &TestProps,
901 output_to_check: &str,
902 proc_res: &ProcRes) {
903 for pat in props.forbid_output.iter() {
904 if output_to_check.contains(pat.as_slice()) {
905 fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
910 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
912 proc_res: &ProcRes) {
914 // true if we found the error in question
915 let mut found_flags = Vec::from_elem(
916 expected_errors.len(), false);
918 if proc_res.status.success() {
919 fatal("process did not return an error status");
922 let prefixes = expected_errors.iter().map(|ee| {
923 format!("{}:{}:", testfile.display(), ee.line)
924 }).collect::<Vec<String> >();
926 #[cfg(target_os = "windows")]
927 fn to_lower( s : &str ) -> String {
929 let c : Vec<char> = i.map( |c| {
931 c.to_ascii().to_lowercase().to_char()
936 String::from_chars(c.as_slice())
939 #[cfg(target_os = "windows")]
940 fn prefix_matches( line : &str, prefix : &str ) -> bool {
941 to_lower(line).as_slice().starts_with(to_lower(prefix).as_slice())
944 #[cfg(target_os = "linux")]
945 #[cfg(target_os = "macos")]
946 #[cfg(target_os = "freebsd")]
947 #[cfg(target_os = "dragonfly")]
948 fn prefix_matches( line : &str, prefix : &str ) -> bool {
949 line.starts_with( prefix )
952 // Scan and extract our error/warning messages,
954 // filename:line1:col1: line2:col2: *error:* msg
955 // filename:line1:col1: line2:col2: *warning:* msg
956 // where line1:col1: is the starting point, line2:col2:
957 // is the ending point, and * represents ANSI color codes.
958 for line in proc_res.stderr.as_slice().lines() {
959 let mut was_expected = false;
960 for (i, ee) in expected_errors.iter().enumerate() {
962 debug!("prefix={} ee.kind={} ee.msg={} line={}",
963 prefixes[i].as_slice(),
967 if prefix_matches(line, prefixes[i].as_slice()) &&
968 line.contains(ee.kind.as_slice()) &&
969 line.contains(ee.msg.as_slice()) {
970 *found_flags.get_mut(i) = true;
977 // ignore this msg which gets printed at the end
978 if line.contains("aborting due to") {
982 if !was_expected && is_compiler_error_or_warning(line) {
983 fatal_proc_rec(format!("unexpected compiler error or warning: '{}'",
989 for (i, &flag) in found_flags.iter().enumerate() {
991 let ee = &expected_errors[i];
992 fatal_proc_rec(format!("expected {} on line {} not found: {}",
993 ee.kind, ee.line, ee.msg).as_slice(),
999 fn is_compiler_error_or_warning(line: &str) -> bool {
1002 scan_until_char(line, ':', &mut i) &&
1003 scan_char(line, ':', &mut i) &&
1004 scan_integer(line, &mut i) &&
1005 scan_char(line, ':', &mut i) &&
1006 scan_integer(line, &mut i) &&
1007 scan_char(line, ':', &mut i) &&
1008 scan_char(line, ' ', &mut i) &&
1009 scan_integer(line, &mut i) &&
1010 scan_char(line, ':', &mut i) &&
1011 scan_integer(line, &mut i) &&
1012 scan_char(line, ' ', &mut i) &&
1013 (scan_string(line, "error", &mut i) ||
1014 scan_string(line, "warning", &mut i));
1017 fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
1018 if *idx >= haystack.len() {
1021 let opt = haystack.slice_from(*idx).find(needle);
1025 *idx = opt.unwrap();
1029 fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
1030 if *idx >= haystack.len() {
1033 let range = haystack.char_range_at(*idx);
1034 if range.ch != needle {
1041 fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
1043 while i < haystack.len() {
1044 let range = haystack.char_range_at(i);
1045 if range.ch < '0' || '9' < range.ch {
1057 fn scan_string(haystack: &str, needle: &str, idx: &mut uint) -> bool {
1058 let mut haystack_i = *idx;
1059 let mut needle_i = 0u;
1060 while needle_i < needle.len() {
1061 if haystack_i >= haystack.len() {
1064 let range = haystack.char_range_at(haystack_i);
1065 haystack_i = range.next;
1066 if !scan_char(needle, range.ch, &mut needle_i) {
1080 status: ProcessExit,
1086 fn compile_test(config: &Config, props: &TestProps,
1087 testfile: &Path) -> ProcRes {
1088 compile_test_(config, props, testfile, [])
1091 fn jit_test(config: &Config, props: &TestProps, testfile: &Path) -> ProcRes {
1092 compile_test_(config, props, testfile, ["--jit".to_string()])
1095 fn compile_test_(config: &Config, props: &TestProps,
1096 testfile: &Path, extra_args: &[String]) -> ProcRes {
1097 let aux_dir = aux_output_dir_name(config, testfile);
1098 // FIXME (#9639): This needs to handle non-utf8 paths
1099 let mut link_args = vec!("-L".to_string(),
1100 aux_dir.as_str().unwrap().to_string());
1101 link_args.extend(extra_args.iter().map(|s| s.clone()));
1102 let args = make_compile_args(config,
1105 |a, b| ThisFile(make_exe_name(a, b)), testfile);
1106 compose_and_run_compiler(config, props, testfile, args, None)
1109 fn exec_compiled_test(config: &Config, props: &TestProps,
1110 testfile: &Path) -> ProcRes {
1112 let env = props.exec_env.clone();
1114 match config.target.as_slice() {
1116 "arm-linux-androideabi" => {
1117 _arm_exec_compiled_test(config, props, testfile, env)
1121 let aux_dir = aux_output_dir_name(config, testfile);
1122 compose_and_run(config,
1124 make_run_args(config, props, testfile),
1126 config.run_lib_path.as_slice(),
1127 Some(aux_dir.as_str().unwrap()),
1133 fn compose_and_run_compiler(
1138 input: Option<String>) -> ProcRes {
1140 if !props.aux_builds.is_empty() {
1141 ensure_dir(&aux_output_dir_name(config, testfile));
1144 let aux_dir = aux_output_dir_name(config, testfile);
1145 // FIXME (#9639): This needs to handle non-utf8 paths
1146 let extra_link_args = vec!("-L".to_string(), aux_dir.as_str().unwrap().to_string());
1148 for rel_ab in props.aux_builds.iter() {
1149 let abs_ab = config.aux_base.join(rel_ab.as_slice());
1150 let aux_props = header::load_props(&abs_ab);
1151 let mut crate_type = if aux_props.no_prefer_dynamic {
1154 vec!("--crate-type=dylib".to_string())
1156 crate_type.extend(extra_link_args.clone().into_iter());
1158 make_compile_args(config,
1162 let f = make_lib_name(a, b, testfile);
1163 ThisDirectory(f.dir_path())
1166 let auxres = compose_and_run(config,
1170 config.compile_lib_path.as_slice(),
1171 Some(aux_dir.as_str().unwrap()),
1173 if !auxres.status.success() {
1175 format!("auxiliary build of {} failed to compile: ",
1176 abs_ab.display()).as_slice(),
1180 match config.target.as_slice() {
1181 "arm-linux-androideabi" => {
1182 _arm_push_aux_shared_library(config, testfile);
1188 compose_and_run(config,
1192 config.compile_lib_path.as_slice(),
1193 Some(aux_dir.as_str().unwrap()),
1197 fn ensure_dir(path: &Path) {
1198 if path.is_dir() { return; }
1199 fs::mkdir(path, io::UserRWX).unwrap();
1202 fn compose_and_run(config: &Config, testfile: &Path,
1203 ProcArgs{ args, prog }: ProcArgs,
1204 procenv: Vec<(String, String)> ,
1206 aux_path: Option<&str>,
1207 input: Option<String>) -> ProcRes {
1208 return program_output(config, testfile, lib_path,
1209 prog, aux_path, args, procenv, input);
1212 enum TargetLocation {
1214 ThisDirectory(Path),
1217 fn make_compile_args(config: &Config,
1219 extras: Vec<String> ,
1220 xform: |&Config, &Path| -> TargetLocation,
1223 let xform_file = xform(config, testfile);
1224 let target = if props.force_host {
1225 config.host.as_slice()
1227 config.target.as_slice()
1229 // FIXME (#9639): This needs to handle non-utf8 paths
1230 let mut args = vec!(testfile.as_str().unwrap().to_string(),
1232 config.build_base.as_str().unwrap().to_string(),
1233 format!("--target={}", target));
1234 args.push_all(extras.as_slice());
1235 if !props.no_prefer_dynamic {
1236 args.push("-C".to_string());
1237 args.push("prefer-dynamic".to_string());
1239 let path = match xform_file {
1241 args.push("-o".to_string());
1244 ThisDirectory(path) => {
1245 args.push("--out-dir".to_string());
1249 args.push(path.as_str().unwrap().to_string());
1250 if props.force_host {
1251 args.extend(split_maybe_args(&config.host_rustcflags).into_iter());
1253 args.extend(split_maybe_args(&config.target_rustcflags).into_iter());
1255 args.extend(split_maybe_args(&props.compile_flags).into_iter());
1257 prog: config.rustc_path.as_str().unwrap().to_string(),
1262 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> Path {
1263 // what we return here is not particularly important, as it
1264 // happens; rustc ignores everything except for the directory.
1265 let auxname = output_testname(auxfile);
1266 aux_output_dir_name(config, testfile).join(&auxname)
1269 fn make_exe_name(config: &Config, testfile: &Path) -> Path {
1270 let mut f = output_base_name(config, testfile);
1271 if !os::consts::EXE_SUFFIX.is_empty() {
1272 let mut fname = f.filename().unwrap().to_vec();
1273 fname.extend(os::consts::EXE_SUFFIX.bytes());
1274 f.set_filename(fname);
1279 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path) ->
1281 // If we've got another tool to run under (valgrind),
1282 // then split apart its command
1283 let mut args = split_maybe_args(&config.runtool);
1284 let exe_file = make_exe_name(config, testfile);
1286 // FIXME (#9639): This needs to handle non-utf8 paths
1287 args.push(exe_file.as_str().unwrap().to_string());
1289 // Add the arguments in the run_flags directive
1290 args.extend(split_maybe_args(&props.run_flags).into_iter());
1292 let prog = args.remove(0).unwrap();
1299 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1305 if s.is_whitespace() {
1316 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1317 aux_path: Option<&str>, args: Vec<String>,
1318 env: Vec<(String, String)>,
1319 input: Option<String>) -> ProcRes {
1322 let cmdline = make_cmdline(lib_path,
1325 logv(config, format!("executing {}", cmdline));
1328 let procsrv::Result {
1332 } = procsrv::run(lib_path,
1337 input).expect(format!("failed to exec `{}`", prog).as_slice());
1338 dump_output(config, testfile, out.as_slice(), err.as_slice());
1347 // Linux and mac don't require adjusting the library search path
1348 #[cfg(target_os = "linux")]
1349 #[cfg(target_os = "macos")]
1350 #[cfg(target_os = "freebsd")]
1351 #[cfg(target_os = "dragonfly")]
1352 fn make_cmdline(_libpath: &str, prog: &str, args: &[String]) -> String {
1353 format!("{} {}", prog, args.connect(" "))
1356 #[cfg(target_os = "windows")]
1357 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1358 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.connect(" "))
1361 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1362 // for diagnostic purposes
1363 #[cfg(target_os = "windows")]
1364 fn lib_path_cmd_prefix(path: &str) -> String {
1365 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1368 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1369 dump_output_file(config, testfile, out, "out");
1370 dump_output_file(config, testfile, err, "err");
1371 maybe_dump_to_stdout(config, out, err);
1374 fn dump_output_file(config: &Config, testfile: &Path,
1375 out: &str, extension: &str) {
1376 let outfile = make_out_name(config, testfile, extension);
1377 File::create(&outfile).write(out.as_bytes()).unwrap();
1380 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> Path {
1381 output_base_name(config, testfile).with_extension(extension)
1384 fn aux_output_dir_name(config: &Config, testfile: &Path) -> Path {
1385 let f = output_base_name(config, testfile);
1386 let mut fname = f.filename().unwrap().to_vec();
1387 fname.extend("libaux".bytes());
1388 f.with_filename(fname)
1391 fn output_testname(testfile: &Path) -> Path {
1392 Path::new(testfile.filestem().unwrap())
1395 fn output_base_name(config: &Config, testfile: &Path) -> Path {
1397 .join(&output_testname(testfile))
1398 .with_extension(config.stage_id.as_slice())
1401 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1403 println!("------{}------------------------------", "stdout");
1404 println!("{}", out);
1405 println!("------{}------------------------------", "stderr");
1406 println!("{}", err);
1407 println!("------------------------------------------");
1411 fn error(err: &str) { println!("\nerror: {}", err); }
1413 fn fatal(err: &str) -> ! { error(err); fail!(); }
1415 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1421 ------------------------------------------\n\
1423 ------------------------------------------\n\
1425 ------------------------------------------\n\
1427 ------------------------------------------\n\
1429 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1434 fn _arm_exec_compiled_test(config: &Config,
1437 env: Vec<(String, String)>)
1439 let args = make_run_args(config, props, testfile);
1440 let cmdline = make_cmdline("",
1441 args.prog.as_slice(),
1442 args.args.as_slice());
1444 // get bare program string
1445 let mut tvec: Vec<String> = args.prog
1448 .map(|ts| ts.to_string())
1450 let prog_short = tvec.pop().unwrap();
1453 let copy_result = procsrv::run("",
1454 config.adb_path.as_slice(),
1459 config.adb_test_dir.clone()
1461 vec!(("".to_string(), "".to_string())),
1462 Some("".to_string()))
1463 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1466 println!("push ({}) {} {} {}",
1473 logv(config, format!("executing ({}) {}", config.target, cmdline));
1475 let mut runargs = Vec::new();
1477 // run test via adb_run_wrapper
1478 runargs.push("shell".to_string());
1479 for (key, val) in env.into_iter() {
1480 runargs.push(format!("{}={}", key, val));
1482 runargs.push(format!("{}/adb_run_wrapper.sh", config.adb_test_dir));
1483 runargs.push(format!("{}", config.adb_test_dir));
1484 runargs.push(format!("{}", prog_short));
1486 for tv in args.args.iter() {
1487 runargs.push(tv.to_string());
1490 config.adb_path.as_slice(),
1493 vec!(("".to_string(), "".to_string())), Some("".to_string()))
1494 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1496 // get exitcode of result
1497 runargs = Vec::new();
1498 runargs.push("shell".to_string());
1499 runargs.push("cat".to_string());
1500 runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1502 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1504 config.adb_path.as_slice(),
1507 vec!(("".to_string(), "".to_string())),
1508 Some("".to_string()))
1509 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1511 let mut exitcode: int = 0;
1512 for c in exitcode_out.as_slice().chars() {
1513 if !c.is_digit() { break; }
1514 exitcode = exitcode * 10 + match c {
1515 '0' .. '9' => c as int - ('0' as int),
1520 // get stdout of result
1521 runargs = Vec::new();
1522 runargs.push("shell".to_string());
1523 runargs.push("cat".to_string());
1524 runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1526 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1528 config.adb_path.as_slice(),
1531 vec!(("".to_string(), "".to_string())),
1532 Some("".to_string()))
1533 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1535 // get stderr of result
1536 runargs = Vec::new();
1537 runargs.push("shell".to_string());
1538 runargs.push("cat".to_string());
1539 runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1541 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1543 config.adb_path.as_slice(),
1546 vec!(("".to_string(), "".to_string())),
1547 Some("".to_string()))
1548 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1552 stdout_out.as_slice(),
1553 stderr_out.as_slice());
1556 status: process::ExitStatus(exitcode),
1563 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1564 let tdir = aux_output_dir_name(config, testfile);
1566 let dirs = fs::readdir(&tdir).unwrap();
1567 for file in dirs.iter() {
1568 if file.extension_str() == Some("so") {
1569 // FIXME (#9639): This needs to handle non-utf8 paths
1570 let copy_result = procsrv::run("",
1571 config.adb_path.as_slice(),
1578 config.adb_test_dir.to_string()
1580 vec!(("".to_string(),
1582 Some("".to_string()))
1583 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1586 println!("push ({}) {} {} {}",
1587 config.target, file.display(),
1588 copy_result.out, copy_result.err);
1594 // codegen tests (vs. clang)
1596 fn append_suffix_to_stem(p: &Path, suffix: &str) -> Path {
1597 if suffix.len() == 0 {
1600 let mut stem = p.filestem().unwrap().to_vec();
1601 stem.extend("-".bytes());
1602 stem.extend(suffix.bytes());
1603 p.with_filename(stem)
1607 fn compile_test_and_save_bitcode(config: &Config, props: &TestProps,
1608 testfile: &Path) -> ProcRes {
1609 let aux_dir = aux_output_dir_name(config, testfile);
1610 // FIXME (#9639): This needs to handle non-utf8 paths
1611 let mut link_args = vec!("-L".to_string(),
1612 aux_dir.as_str().unwrap().to_string());
1613 let llvm_args = vec!("--emit=bc,obj".to_string(),
1614 "--crate-type=lib".to_string());
1615 link_args.extend(llvm_args.into_iter());
1616 let args = make_compile_args(config,
1619 |a, b| ThisDirectory(output_base_name(a, b).dir_path()),
1621 compose_and_run_compiler(config, props, testfile, args, None)
1624 fn compile_cc_with_clang_and_save_bitcode(config: &Config, _props: &TestProps,
1625 testfile: &Path) -> ProcRes {
1626 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1627 let bitcodefile = append_suffix_to_stem(&bitcodefile, "clang");
1628 let testcc = testfile.with_extension("cc");
1629 let proc_args = ProcArgs {
1630 // FIXME (#9639): This needs to handle non-utf8 paths
1631 prog: config.clang_path.as_ref().unwrap().as_str().unwrap().to_string(),
1632 args: vec!("-c".to_string(),
1633 "-emit-llvm".to_string(),
1635 bitcodefile.as_str().unwrap().to_string(),
1636 testcc.as_str().unwrap().to_string())
1638 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1641 fn extract_function_from_bitcode(config: &Config, _props: &TestProps,
1642 fname: &str, testfile: &Path,
1643 suffix: &str) -> ProcRes {
1644 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1645 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1646 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1647 let prog = config.llvm_bin_path.as_ref().unwrap().join("llvm-extract");
1648 let proc_args = ProcArgs {
1649 // FIXME (#9639): This needs to handle non-utf8 paths
1650 prog: prog.as_str().unwrap().to_string(),
1651 args: vec!(format!("-func={}", fname),
1652 format!("-o={}", extracted_bc.as_str().unwrap()),
1653 bitcodefile.as_str().unwrap().to_string())
1655 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1658 fn disassemble_extract(config: &Config, _props: &TestProps,
1659 testfile: &Path, suffix: &str) -> ProcRes {
1660 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1661 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1662 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1663 let extracted_ll = extracted_bc.with_extension("ll");
1664 let prog = config.llvm_bin_path.as_ref().unwrap().join("llvm-dis");
1665 let proc_args = ProcArgs {
1666 // FIXME (#9639): This needs to handle non-utf8 paths
1667 prog: prog.as_str().unwrap().to_string(),
1668 args: vec!(format!("-o={}", extracted_ll.as_str().unwrap()),
1669 extracted_bc.as_str().unwrap().to_string())
1671 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1675 fn count_extracted_lines(p: &Path) -> uint {
1676 let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
1677 let x = str::from_utf8(x.as_slice()).unwrap();
1682 fn run_codegen_test(config: &Config, props: &TestProps,
1683 testfile: &Path, mm: &mut MetricMap) {
1685 if config.llvm_bin_path.is_none() {
1686 fatal("missing --llvm-bin-path");
1689 if config.clang_path.is_none() {
1690 fatal("missing --clang-path");
1693 let mut proc_res = compile_test_and_save_bitcode(config, props, testfile);
1694 if !proc_res.status.success() {
1695 fatal_proc_rec("compilation failed!", &proc_res);
1698 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "");
1699 if !proc_res.status.success() {
1700 fatal_proc_rec("extracting 'test' function failed",
1704 proc_res = disassemble_extract(config, props, testfile, "");
1705 if !proc_res.status.success() {
1706 fatal_proc_rec("disassembling extract failed", &proc_res);
1710 let mut proc_res = compile_cc_with_clang_and_save_bitcode(config, props, testfile);
1711 if !proc_res.status.success() {
1712 fatal_proc_rec("compilation failed!", &proc_res);
1715 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "clang");
1716 if !proc_res.status.success() {
1717 fatal_proc_rec("extracting 'test' function failed",
1721 proc_res = disassemble_extract(config, props, testfile, "clang");
1722 if !proc_res.status.success() {
1723 fatal_proc_rec("disassembling extract failed", &proc_res);
1726 let base = output_base_name(config, testfile);
1727 let base_extract = append_suffix_to_stem(&base, "extract");
1729 let base_clang = append_suffix_to_stem(&base, "clang");
1730 let base_clang_extract = append_suffix_to_stem(&base_clang, "extract");
1732 let base_lines = count_extracted_lines(&base_extract);
1733 let clang_lines = count_extracted_lines(&base_clang_extract);
1735 mm.insert_metric("clang-codegen-ratio",
1736 (base_lines as f64) / (clang_lines as f64),