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 = "win32")]
24 use std::io::net::tcp;
25 use std::io::process::ProcessExit;
31 use std::string::String;
35 pub fn run(config: Config, testfile: String) {
37 match config.target.as_slice() {
39 "arm-linux-androideabi" => {
40 if !config.adb_device_status {
41 fail!("android device not available");
48 let mut _mm = MetricMap::new();
49 run_metrics(config, testfile, &mut _mm);
52 pub fn run_metrics(config: Config, testfile: String, mm: &mut MetricMap) {
54 // We're going to be dumping a lot of info. Start on a new line.
57 let testfile = Path::new(testfile);
58 debug!("running {}", testfile.display());
59 let props = header::load_props(&testfile);
60 debug!("loaded props");
62 CompileFail => run_cfail_test(&config, &props, &testfile),
63 RunFail => run_rfail_test(&config, &props, &testfile),
64 RunPass => run_rpass_test(&config, &props, &testfile),
65 Pretty => run_pretty_test(&config, &props, &testfile),
66 DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
67 DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
68 Codegen => run_codegen_test(&config, &props, &testfile, mm),
72 fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
73 let proc_res = compile_test(config, props, testfile);
75 if proc_res.status.success() {
76 fatal_proc_rec("compile-fail test compiled successfully!".to_string(),
80 check_correct_failure_status(&proc_res);
82 let expected_errors = errors::load_errors(&config.cfail_regex, testfile);
83 if !expected_errors.is_empty() {
84 if !props.error_patterns.is_empty() {
85 fatal("both error pattern and expected errors \
86 specified".to_string());
88 check_expected_errors(expected_errors, testfile, &proc_res);
90 check_error_patterns(props, testfile, &proc_res);
92 check_no_compiler_crash(&proc_res);
95 fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
96 let proc_res = if !config.jit {
97 let proc_res = compile_test(config, props, testfile);
99 if !proc_res.status.success() {
100 fatal_proc_rec("compilation failed!".to_string(), &proc_res);
103 exec_compiled_test(config, props, testfile)
105 jit_test(config, props, testfile)
108 // The value our Makefile configures valgrind to return on failure
109 static VALGRIND_ERR: int = 100;
110 if proc_res.status.matches_exit_status(VALGRIND_ERR) {
111 fatal_proc_rec("run-fail test isn't valgrind-clean!".to_string(),
115 check_correct_failure_status(&proc_res);
116 check_error_patterns(props, testfile, &proc_res);
119 fn check_correct_failure_status(proc_res: &ProcRes) {
120 // The value the rust runtime returns on failure
121 static RUST_ERR: int = 101;
122 if !proc_res.status.matches_exit_status(RUST_ERR) {
124 format!("failure produced the wrong error: {}", proc_res.status),
129 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
131 let mut proc_res = compile_test(config, props, testfile);
133 if !proc_res.status.success() {
134 fatal_proc_rec("compilation failed!".to_string(), &proc_res);
137 proc_res = exec_compiled_test(config, props, testfile);
139 if !proc_res.status.success() {
140 fatal_proc_rec("test run failed!".to_string(), &proc_res);
143 let proc_res = jit_test(config, props, testfile);
145 if !proc_res.status.success() {
146 fatal_proc_rec("jit failed!".to_string(), &proc_res);
151 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
152 if props.pp_exact.is_some() {
153 logv(config, "testing for exact pretty-printing".to_string());
155 logv(config, "testing for converging pretty-printing".to_string());
159 match props.pp_exact { Some(_) => 1, None => 2 };
161 let src = File::open(testfile).read_to_end().unwrap();
162 let src = str::from_utf8(src.as_slice()).unwrap().to_string();
163 let mut srcs = vec!(src);
166 while round < rounds {
167 logv(config, format!("pretty-printing round {}", round));
168 let proc_res = print_source(config,
171 (*srcs.get(round)).to_string(),
174 if !proc_res.status.success() {
175 fatal_proc_rec(format!("pretty-printing failed in round {}", round),
179 let ProcRes{ stdout, .. } = proc_res;
184 let mut expected = match props.pp_exact {
186 let filepath = testfile.dir_path().join(file);
187 let s = File::open(&filepath).read_to_end().unwrap();
188 str::from_utf8(s.as_slice()).unwrap().to_string()
190 None => { (*srcs.get(srcs.len() - 2u)).clone() }
192 let mut actual = (*srcs.get(srcs.len() - 1u)).clone();
194 if props.pp_exact.is_some() {
195 // Now we have to care about line endings
196 let cr = "\r".to_string();
197 actual = actual.replace(cr.as_slice(), "").to_string();
198 expected = expected.replace(cr.as_slice(), "").to_string();
201 compare_source(expected.as_slice(), actual.as_slice());
203 // Finally, let's make sure it actually appears to remain valid code
204 let proc_res = typecheck_source(config, props, testfile, actual);
206 if !proc_res.status.success() {
207 fatal_proc_rec("pretty-printed source does not typecheck".to_string(),
210 if props.no_pretty_expanded { return }
212 // additionally, run `--pretty expanded` and try to build it.
213 let proc_res = print_source(config, props, testfile, (*srcs.get(round)).clone(), "expanded");
214 if !proc_res.status.success() {
215 fatal_proc_rec(format!("pretty-printing (expanded) failed"), &proc_res);
218 let ProcRes{ stdout: expanded_src, .. } = proc_res;
219 let proc_res = typecheck_source(config, props, testfile, expanded_src);
220 if !proc_res.status.success() {
221 fatal_proc_rec(format!("pretty-printed source (expanded) does \
228 fn print_source(config: &Config,
232 pretty_type: &str) -> ProcRes {
233 let aux_dir = aux_output_dir_name(config, testfile);
234 compose_and_run(config,
239 pretty_type.to_string()),
240 props.exec_env.clone(),
241 config.compile_lib_path.as_slice(),
242 Some(aux_dir.as_str().unwrap()),
246 fn make_pp_args(config: &Config,
249 pretty_type: String) -> ProcArgs {
250 let aux_dir = aux_output_dir_name(config, testfile);
251 // FIXME (#9639): This needs to handle non-utf8 paths
252 let mut args = vec!("-".to_string(),
253 "--pretty".to_string(),
255 format!("--target={}", config.target),
257 aux_dir.as_str().unwrap().to_string());
258 args.push_all_move(split_maybe_args(&config.target_rustcflags));
259 args.push_all_move(split_maybe_args(&props.compile_flags));
261 prog: config.rustc_path.as_str().unwrap().to_string(),
266 fn compare_source(expected: &str, actual: &str) {
267 if expected != actual {
268 error("pretty-printed source does not match expected \
269 source".to_string());
272 ------------------------------------------\n\
274 ------------------------------------------\n\
276 ------------------------------------------\n\
278 ------------------------------------------\n\
285 fn typecheck_source(config: &Config, props: &TestProps,
286 testfile: &Path, src: String) -> ProcRes {
287 let args = make_typecheck_args(config, props, testfile);
288 compose_and_run_compiler(config, props, testfile, args, Some(src))
291 fn make_typecheck_args(config: &Config, props: &TestProps, testfile: &Path) -> ProcArgs {
292 let aux_dir = aux_output_dir_name(config, testfile);
293 let target = if props.force_host {
294 config.host.as_slice()
296 config.target.as_slice()
298 // FIXME (#9639): This needs to handle non-utf8 paths
299 let mut args = vec!("-".to_string(),
300 "--no-trans".to_string(),
301 "--crate-type=lib".to_string(),
302 format!("--target={}", target),
304 config.build_base.as_str().unwrap().to_string(),
306 aux_dir.as_str().unwrap().to_string());
307 args.push_all_move(split_maybe_args(&config.target_rustcflags));
308 args.push_all_move(split_maybe_args(&props.compile_flags));
309 // FIXME (#9639): This needs to handle non-utf8 paths
311 prog: config.rustc_path.as_str().unwrap().to_string(),
317 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
318 let mut config = Config {
319 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
320 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
324 let config = &mut config;
325 let DebuggerCommands { commands, check_lines, .. } = parse_debugger_commands(testfile, "gdb");
326 let mut cmds = commands.connect("\n").to_string();
328 // compile test file (it shoud have 'compile-flags:-g' in the header)
329 let compiler_run_result = compile_test(config, props, testfile);
330 if !compiler_run_result.status.success() {
331 fatal_proc_rec("compilation failed!".to_string(), &compiler_run_result);
334 let exe_file = make_exe_name(config, testfile);
337 let debugger_run_result;
338 match config.target.as_slice() {
339 "arm-linux-androideabi" => {
341 cmds = cmds.replace("run", "continue").to_string();
343 // write debugger script
344 let script_str = ["set charset UTF-8".to_string(),
345 format!("file {}", exe_file.as_str().unwrap()
347 "target remote :5039".to_string(),
349 "quit".to_string()].connect("\n");
350 debug!("script_str = {}", script_str);
351 dump_output_file(config,
353 script_str.as_slice(),
358 config.adb_path.as_slice(),
362 exe_file.as_str().unwrap().to_string(),
363 config.adb_test_dir.clone()
365 vec!(("".to_string(), "".to_string())),
366 Some("".to_string()))
367 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
370 config.adb_path.as_slice(),
373 "forward".to_string(),
374 "tcp:5039".to_string(),
375 "tcp:5039".to_string()
377 vec!(("".to_string(), "".to_string())),
378 Some("".to_string()))
379 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
381 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
382 gdbserver :5039 {}/{}",
383 config.adb_test_dir.clone(),
384 config.adb_test_dir.clone(),
387 .unwrap()).unwrap());
389 let mut process = procsrv::run_background("",
397 vec!(("".to_string(),
399 Some("".to_string()))
400 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
402 //waiting 1 second for gdbserver start
404 let result = task::try(proc() {
405 tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
413 let tool_path = match config.android_cross_path.as_str() {
414 Some(x) => x.to_string(),
415 None => fatal("cannot find android cross path".to_string())
418 let debugger_script = make_out_name(config, testfile, "debugger.script");
419 // FIXME (#9639): This needs to handle non-utf8 paths
421 vec!("-quiet".to_string(),
422 "-batch".to_string(),
424 format!("-command={}", debugger_script.as_str().unwrap()));
426 let gdb_path = tool_path.append("/bin/arm-linux-androideabi-gdb");
427 let procsrv::Result {
434 debugger_opts.as_slice(),
435 vec!(("".to_string(), "".to_string())),
437 .expect(format!("failed to exec `{}`", gdb_path).as_slice());
439 let cmdline = make_cmdline("",
440 "arm-linux-androideabi-gdb",
441 debugger_opts.as_slice());
442 logv(config, format!("executing {}", cmdline));
446 debugger_run_result = ProcRes {
452 process.signal_kill().unwrap();
456 // write debugger script
458 "set charset UTF-8".to_string(),
462 debug!("script_str = {}", script_str);
463 dump_output_file(config,
465 script_str.as_slice(),
468 // run debugger script with gdb
470 fn debugger() -> String {
471 "gdb.exe".to_string()
474 fn debugger() -> String {
478 let debugger_script = make_out_name(config, testfile, "debugger.script");
480 // FIXME (#9639): This needs to handle non-utf8 paths
482 vec!("-quiet".to_string(),
483 "-batch".to_string(),
485 format!("-command={}", debugger_script.as_str().unwrap()),
486 exe_file.as_str().unwrap().to_string());
487 proc_args = ProcArgs {
491 debugger_run_result = compose_and_run(config,
495 config.run_lib_path.as_slice(),
501 if !debugger_run_result.status.success() {
502 fatal("gdb failed to execute".to_string());
505 check_debugger_output(&debugger_run_result, check_lines.as_slice());
508 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
509 use std::io::process::{Command, ProcessOutput};
511 if config.lldb_python_dir.is_none() {
512 fatal("Can't run LLDB test because LLDB's python path is not \
516 let mut config = Config {
517 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
518 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
522 let config = &mut config;
524 // compile test file (it shoud have 'compile-flags:-g' in the header)
525 let compile_result = compile_test(config, props, testfile);
526 if !compile_result.status.success() {
527 fatal_proc_rec("compilation failed!".to_string(), &compile_result);
530 let exe_file = make_exe_name(config, testfile);
532 // Parse debugger commands etc from test files
533 let DebuggerCommands {
537 } = parse_debugger_commands(testfile, "lldb");
539 // Write debugger script:
540 // We don't want to hang when calling `quit` while the process is still running
541 let mut script_str = String::from_str("settings set auto-confirm true\n");
543 // Set breakpoints on every line that contains the string "#break"
544 for line in breakpoint_lines.iter() {
545 script_str.push_str(format!("breakpoint set --line {}\n",
549 // Append the other commands
550 for line in commands.iter() {
551 script_str.push_str(line.as_slice());
552 script_str.push_str("\n");
555 // Finally, quit the debugger
556 script_str.push_str("quit\n");
558 // Write the script into a file
559 debug!("script_str = {}", script_str);
560 dump_output_file(config,
562 script_str.as_slice(),
564 let debugger_script = make_out_name(config, testfile, "debugger.script");
566 // Let LLDB execute the script via lldb_batchmode.py
567 let debugger_run_result = run_lldb(config, &exe_file, &debugger_script);
569 if !debugger_run_result.status.success() {
570 fatal_proc_rec("Error while running LLDB".to_string(),
571 &debugger_run_result);
574 check_debugger_output(&debugger_run_result, check_lines.as_slice());
576 fn run_lldb(config: &Config, test_executable: &Path, debugger_script: &Path) -> ProcRes {
577 // Prepare the lldb_batchmode which executes the debugger script
578 let mut cmd = Command::new("python");
579 cmd.arg("./src/etc/lldb_batchmode.py")
580 .arg(test_executable)
581 .arg(debugger_script)
582 .env([("PYTHONPATH", config.lldb_python_dir.clone().unwrap().as_slice())]);
584 let (status, out, err) = match cmd.spawn() {
586 let ProcessOutput { status, output, error } =
587 process.wait_with_output().unwrap();
590 str::from_utf8(output.as_slice()).unwrap().to_string(),
591 str::from_utf8(error.as_slice()).unwrap().to_string())
594 fatal(format!("Failed to setup Python process for \
595 LLDB script: {}", e))
599 dump_output(config, test_executable, out.as_slice(), err.as_slice());
604 cmdline: format!("{}", cmd)
609 struct DebuggerCommands {
610 commands: Vec<String>,
611 check_lines: Vec<String>,
612 breakpoint_lines: Vec<uint>,
615 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
616 -> DebuggerCommands {
617 use std::io::{BufferedReader, File};
619 let command_directive = format!("{}-command", debugger_prefix);
620 let check_directive = format!("{}-check", debugger_prefix);
622 let mut breakpoint_lines = vec!();
623 let mut commands = vec!();
624 let mut check_lines = vec!();
626 let mut reader = BufferedReader::new(File::open(file_path).unwrap());
627 for line in reader.lines() {
630 if line.as_slice().contains("#break") {
631 breakpoint_lines.push(counter);
634 header::parse_name_value_directive(
636 command_directive.to_string()).map(|cmd| {
640 header::parse_name_value_directive(
642 check_directive.to_string()).map(|cmd| {
643 check_lines.push(cmd)
647 fatal(format!("Error while parsing debugger commands: {}", e))
655 check_lines: check_lines,
656 breakpoint_lines: breakpoint_lines
660 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
661 if options.is_none() {
665 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
666 let options_to_remove = [
669 "--debuginfo".to_string()
672 split_maybe_args(options).move_iter()
673 .filter(|x| !options_to_remove.contains(x))
674 .collect::<Vec<String>>()
680 fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String]) {
681 let num_check_lines = check_lines.len();
682 if num_check_lines > 0 {
683 // Allow check lines to leave parts unspecified (e.g., uninitialized
684 // bits in the wrong case of an enum) with the notation "[...]".
685 let check_fragments: Vec<Vec<String>> =
686 check_lines.iter().map(|s| {
690 .map(|x| x.to_string())
693 // check if each line in props.check_lines appears in the
696 for line in debugger_run_result.stdout.as_slice().lines() {
697 let mut rest = line.trim();
698 let mut first = true;
699 let mut failed = false;
700 for frag in check_fragments.get(i).iter() {
701 let found = if first {
702 if rest.starts_with(frag.as_slice()) {
708 rest.find_str(frag.as_slice())
716 rest = rest.slice_from(i + frag.len());
721 if !failed && rest.len() == 0 {
724 if i == num_check_lines {
729 if i != num_check_lines {
730 fatal_proc_rec(format!("line not found in debugger output: {}",
731 check_lines.get(i).unwrap()),
732 debugger_run_result);
737 fn check_error_patterns(props: &TestProps,
739 proc_res: &ProcRes) {
740 if props.error_patterns.is_empty() {
741 fatal(format!("no error pattern specified in {}",
742 testfile.display().as_maybe_owned().as_slice()));
745 if proc_res.status.success() {
746 fatal("process did not return an error status".to_string());
749 let mut next_err_idx = 0u;
750 let mut next_err_pat = props.error_patterns.get(next_err_idx);
751 let mut done = false;
752 let output_to_check = if props.check_stdout {
753 format!("{}{}", proc_res.stdout, proc_res.stderr)
755 proc_res.stderr.clone()
757 for line in output_to_check.as_slice().lines() {
758 if line.contains(next_err_pat.as_slice()) {
759 debug!("found error pattern {}", *next_err_pat);
761 if next_err_idx == props.error_patterns.len() {
762 debug!("found all error patterns");
766 next_err_pat = props.error_patterns.get(next_err_idx);
771 let missing_patterns =
772 props.error_patterns.slice(next_err_idx, props.error_patterns.len());
773 if missing_patterns.len() == 1u {
774 fatal_proc_rec(format!("error pattern '{}' not found!",
775 missing_patterns[0]),
778 for pattern in missing_patterns.iter() {
779 error(format!("error pattern '{}' not found!", *pattern));
781 fatal_proc_rec("multiple error patterns not found".to_string(),
786 fn check_no_compiler_crash(proc_res: &ProcRes) {
787 for line in proc_res.stderr.as_slice().lines() {
788 if line.starts_with("error: internal compiler error:") {
789 fatal_proc_rec("compiler encountered internal error".to_string(),
795 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
797 proc_res: &ProcRes) {
799 // true if we found the error in question
800 let mut found_flags = Vec::from_elem(
801 expected_errors.len(), false);
803 if proc_res.status.success() {
804 fatal("process did not return an error status".to_string());
807 let prefixes = expected_errors.iter().map(|ee| {
808 format!("{}:{}:", testfile.display(), ee.line)
809 }).collect::<Vec<String> >();
811 #[cfg(target_os = "win32")]
812 fn to_lower( s : &str ) -> String {
814 let c : Vec<char> = i.map( |c| {
816 c.to_ascii().to_lowercase().to_char()
821 str::from_chars(c.as_slice()).to_string()
824 #[cfg(target_os = "win32")]
825 fn prefix_matches( line : &str, prefix : &str ) -> bool {
826 to_lower(line).as_slice().starts_with(to_lower(prefix).as_slice())
829 #[cfg(target_os = "linux")]
830 #[cfg(target_os = "macos")]
831 #[cfg(target_os = "freebsd")]
832 fn prefix_matches( line : &str, prefix : &str ) -> bool {
833 line.starts_with( prefix )
836 // Scan and extract our error/warning messages,
838 // filename:line1:col1: line2:col2: *error:* msg
839 // filename:line1:col1: line2:col2: *warning:* msg
840 // where line1:col1: is the starting point, line2:col2:
841 // is the ending point, and * represents ANSI color codes.
842 for line in proc_res.stderr.as_slice().lines() {
843 let mut was_expected = false;
844 for (i, ee) in expected_errors.iter().enumerate() {
845 if !*found_flags.get(i) {
846 debug!("prefix={} ee.kind={} ee.msg={} line={}",
847 prefixes.get(i).as_slice(),
851 if prefix_matches(line, prefixes.get(i).as_slice()) &&
852 line.contains(ee.kind.as_slice()) &&
853 line.contains(ee.msg.as_slice()) {
854 *found_flags.get_mut(i) = true;
861 // ignore this msg which gets printed at the end
862 if line.contains("aborting due to") {
866 if !was_expected && is_compiler_error_or_warning(line) {
867 fatal_proc_rec(format!("unexpected compiler error or warning: '{}'",
873 for (i, &flag) in found_flags.iter().enumerate() {
875 let ee = expected_errors.get(i);
876 fatal_proc_rec(format!("expected {} on line {} not found: {}",
877 ee.kind, ee.line, ee.msg),
883 fn is_compiler_error_or_warning(line: &str) -> bool {
886 scan_until_char(line, ':', &mut i) &&
887 scan_char(line, ':', &mut i) &&
888 scan_integer(line, &mut i) &&
889 scan_char(line, ':', &mut i) &&
890 scan_integer(line, &mut i) &&
891 scan_char(line, ':', &mut i) &&
892 scan_char(line, ' ', &mut i) &&
893 scan_integer(line, &mut i) &&
894 scan_char(line, ':', &mut i) &&
895 scan_integer(line, &mut i) &&
896 scan_char(line, ' ', &mut i) &&
897 (scan_string(line, "error", &mut i) ||
898 scan_string(line, "warning", &mut i));
901 fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
902 if *idx >= haystack.len() {
905 let opt = haystack.slice_from(*idx).find(needle);
913 fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
914 if *idx >= haystack.len() {
917 let range = haystack.char_range_at(*idx);
918 if range.ch != needle {
925 fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
927 while i < haystack.len() {
928 let range = haystack.char_range_at(i);
929 if range.ch < '0' || '9' < range.ch {
941 fn scan_string(haystack: &str, needle: &str, idx: &mut uint) -> bool {
942 let mut haystack_i = *idx;
943 let mut needle_i = 0u;
944 while needle_i < needle.len() {
945 if haystack_i >= haystack.len() {
948 let range = haystack.char_range_at(haystack_i);
949 haystack_i = range.next;
950 if !scan_char(needle, range.ch, &mut needle_i) {
970 fn compile_test(config: &Config, props: &TestProps,
971 testfile: &Path) -> ProcRes {
972 compile_test_(config, props, testfile, [])
975 fn jit_test(config: &Config, props: &TestProps, testfile: &Path) -> ProcRes {
976 compile_test_(config, props, testfile, ["--jit".to_string()])
979 fn compile_test_(config: &Config, props: &TestProps,
980 testfile: &Path, extra_args: &[String]) -> ProcRes {
981 let aux_dir = aux_output_dir_name(config, testfile);
982 // FIXME (#9639): This needs to handle non-utf8 paths
983 let link_args = vec!("-L".to_string(),
984 aux_dir.as_str().unwrap().to_string());
985 let args = make_compile_args(config,
987 link_args.append(extra_args),
988 |a, b| ThisFile(make_exe_name(a, b)), testfile);
989 compose_and_run_compiler(config, props, testfile, args, None)
992 fn exec_compiled_test(config: &Config, props: &TestProps,
993 testfile: &Path) -> ProcRes {
995 let env = props.exec_env.clone();
997 match config.target.as_slice() {
999 "arm-linux-androideabi" => {
1000 _arm_exec_compiled_test(config, props, testfile, env)
1004 let aux_dir = aux_output_dir_name(config, testfile);
1005 compose_and_run(config,
1007 make_run_args(config, props, testfile),
1009 config.run_lib_path.as_slice(),
1010 Some(aux_dir.as_str().unwrap()),
1016 fn compose_and_run_compiler(
1021 input: Option<String>) -> ProcRes {
1023 if !props.aux_builds.is_empty() {
1024 ensure_dir(&aux_output_dir_name(config, testfile));
1027 let aux_dir = aux_output_dir_name(config, testfile);
1028 // FIXME (#9639): This needs to handle non-utf8 paths
1029 let extra_link_args = vec!("-L".to_string(), aux_dir.as_str().unwrap().to_string());
1031 for rel_ab in props.aux_builds.iter() {
1032 let abs_ab = config.aux_base.join(rel_ab.as_slice());
1033 let aux_props = header::load_props(&abs_ab);
1034 let crate_type = if aux_props.no_prefer_dynamic {
1037 vec!("--crate-type=dylib".to_string())
1040 make_compile_args(config,
1043 extra_link_args.iter()
1044 .map(|x| x.to_string())
1045 .collect::<Vec<_>>()
1048 let f = make_lib_name(a, b, testfile);
1049 ThisDirectory(f.dir_path())
1052 let auxres = compose_and_run(config,
1056 config.compile_lib_path.as_slice(),
1057 Some(aux_dir.as_str().unwrap()),
1059 if !auxres.status.success() {
1061 format!("auxiliary build of {} failed to compile: ",
1066 match config.target.as_slice() {
1067 "arm-linux-androideabi" => {
1068 _arm_push_aux_shared_library(config, testfile);
1074 compose_and_run(config,
1078 config.compile_lib_path.as_slice(),
1079 Some(aux_dir.as_str().unwrap()),
1083 fn ensure_dir(path: &Path) {
1084 if path.is_dir() { return; }
1085 fs::mkdir(path, io::UserRWX).unwrap();
1088 fn compose_and_run(config: &Config, testfile: &Path,
1089 ProcArgs{ args, prog }: ProcArgs,
1090 procenv: Vec<(String, String)> ,
1092 aux_path: Option<&str>,
1093 input: Option<String>) -> ProcRes {
1094 return program_output(config, testfile, lib_path,
1095 prog, aux_path, args, procenv, input);
1098 enum TargetLocation {
1100 ThisDirectory(Path),
1103 fn make_compile_args(config: &Config,
1105 extras: Vec<String> ,
1106 xform: |&Config, &Path| -> TargetLocation,
1109 let xform_file = xform(config, testfile);
1110 let target = if props.force_host {
1111 config.host.as_slice()
1113 config.target.as_slice()
1115 // FIXME (#9639): This needs to handle non-utf8 paths
1116 let mut args = vec!(testfile.as_str().unwrap().to_string(),
1118 config.build_base.as_str().unwrap().to_string(),
1119 format!("--target={}", target));
1120 args.push_all(extras.as_slice());
1121 if !props.no_prefer_dynamic {
1122 args.push("-C".to_string());
1123 args.push("prefer-dynamic".to_string());
1125 let path = match xform_file {
1127 args.push("-o".to_string());
1130 ThisDirectory(path) => {
1131 args.push("--out-dir".to_string());
1135 args.push(path.as_str().unwrap().to_string());
1136 if props.force_host {
1137 args.push_all_move(split_maybe_args(&config.host_rustcflags));
1139 args.push_all_move(split_maybe_args(&config.target_rustcflags));
1141 args.push_all_move(split_maybe_args(&props.compile_flags));
1143 prog: config.rustc_path.as_str().unwrap().to_string(),
1148 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> Path {
1149 // what we return here is not particularly important, as it
1150 // happens; rustc ignores everything except for the directory.
1151 let auxname = output_testname(auxfile);
1152 aux_output_dir_name(config, testfile).join(&auxname)
1155 fn make_exe_name(config: &Config, testfile: &Path) -> Path {
1156 let mut f = output_base_name(config, testfile);
1157 if !os::consts::EXE_SUFFIX.is_empty() {
1158 match f.filename().map(|s| Vec::from_slice(s).append(os::consts::EXE_SUFFIX.as_bytes())) {
1159 Some(v) => f.set_filename(v),
1166 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path) ->
1168 // If we've got another tool to run under (valgrind),
1169 // then split apart its command
1170 let mut args = split_maybe_args(&config.runtool);
1171 let exe_file = make_exe_name(config, testfile);
1173 // FIXME (#9639): This needs to handle non-utf8 paths
1174 args.push(exe_file.as_str().unwrap().to_string());
1176 // Add the arguments in the run_flags directive
1177 args.push_all_move(split_maybe_args(&props.run_flags));
1179 let prog = args.shift().unwrap();
1186 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1192 if s.is_whitespace() {
1203 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1204 aux_path: Option<&str>, args: Vec<String>,
1205 env: Vec<(String, String)>,
1206 input: Option<String>) -> ProcRes {
1209 let cmdline = make_cmdline(lib_path,
1212 logv(config, format!("executing {}", cmdline));
1215 let procsrv::Result {
1219 } = procsrv::run(lib_path,
1224 input).expect(format!("failed to exec `{}`", prog).as_slice());
1225 dump_output(config, testfile, out.as_slice(), err.as_slice());
1234 // Linux and mac don't require adjusting the library search path
1235 #[cfg(target_os = "linux")]
1236 #[cfg(target_os = "macos")]
1237 #[cfg(target_os = "freebsd")]
1238 fn make_cmdline(_libpath: &str, prog: &str, args: &[String]) -> String {
1239 format!("{} {}", prog, args.connect(" "))
1242 #[cfg(target_os = "win32")]
1243 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1244 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.connect(" "))
1247 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1248 // for diagnostic purposes
1249 #[cfg(target_os = "win32")]
1250 fn lib_path_cmd_prefix(path: &str) -> String {
1251 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1254 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1255 dump_output_file(config, testfile, out, "out");
1256 dump_output_file(config, testfile, err, "err");
1257 maybe_dump_to_stdout(config, out, err);
1260 fn dump_output_file(config: &Config, testfile: &Path,
1261 out: &str, extension: &str) {
1262 let outfile = make_out_name(config, testfile, extension);
1263 File::create(&outfile).write(out.as_bytes()).unwrap();
1266 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> Path {
1267 output_base_name(config, testfile).with_extension(extension)
1270 fn aux_output_dir_name(config: &Config, testfile: &Path) -> Path {
1271 let mut f = output_base_name(config, testfile);
1272 match f.filename().map(|s| Vec::from_slice(s).append(bytes!(".libaux"))) {
1273 Some(v) => f.set_filename(v),
1279 fn output_testname(testfile: &Path) -> Path {
1280 Path::new(testfile.filestem().unwrap())
1283 fn output_base_name(config: &Config, testfile: &Path) -> Path {
1285 .join(&output_testname(testfile))
1286 .with_extension(config.stage_id.as_slice())
1289 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1291 println!("------{}------------------------------", "stdout");
1292 println!("{}", out);
1293 println!("------{}------------------------------", "stderr");
1294 println!("{}", err);
1295 println!("------------------------------------------");
1299 fn error(err: String) { println!("\nerror: {}", err); }
1301 fn fatal(err: String) -> ! { error(err); fail!(); }
1303 fn fatal_proc_rec(err: String, proc_res: &ProcRes) -> ! {
1309 ------------------------------------------\n\
1311 ------------------------------------------\n\
1313 ------------------------------------------\n\
1315 ------------------------------------------\n\
1317 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1322 fn _arm_exec_compiled_test(config: &Config,
1325 env: Vec<(String, String)>)
1327 let args = make_run_args(config, props, testfile);
1328 let cmdline = make_cmdline("",
1329 args.prog.as_slice(),
1330 args.args.as_slice());
1332 // get bare program string
1333 let mut tvec: Vec<String> = args.prog
1336 .map(|ts| ts.to_string())
1338 let prog_short = tvec.pop().unwrap();
1341 let copy_result = procsrv::run("",
1342 config.adb_path.as_slice(),
1347 config.adb_test_dir.clone()
1349 vec!(("".to_string(), "".to_string())),
1350 Some("".to_string()))
1351 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1354 println!("push ({}) {} {} {}",
1361 logv(config, format!("executing ({}) {}", config.target, cmdline));
1363 let mut runargs = Vec::new();
1365 // run test via adb_run_wrapper
1366 runargs.push("shell".to_string());
1367 for (key, val) in env.move_iter() {
1368 runargs.push(format!("{}={}", key, val));
1370 runargs.push(format!("{}/adb_run_wrapper.sh", config.adb_test_dir));
1371 runargs.push(format!("{}", config.adb_test_dir));
1372 runargs.push(format!("{}", prog_short));
1374 for tv in args.args.iter() {
1375 runargs.push(tv.to_string());
1378 config.adb_path.as_slice(),
1381 vec!(("".to_string(), "".to_string())), Some("".to_string()))
1382 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1384 // get exitcode of result
1385 runargs = Vec::new();
1386 runargs.push("shell".to_string());
1387 runargs.push("cat".to_string());
1388 runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1390 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1392 config.adb_path.as_slice(),
1395 vec!(("".to_string(), "".to_string())),
1396 Some("".to_string()))
1397 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1399 let mut exitcode: int = 0;
1400 for c in exitcode_out.as_slice().chars() {
1401 if !c.is_digit() { break; }
1402 exitcode = exitcode * 10 + match c {
1403 '0' .. '9' => c as int - ('0' as int),
1408 // get stdout of result
1409 runargs = Vec::new();
1410 runargs.push("shell".to_string());
1411 runargs.push("cat".to_string());
1412 runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1414 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1416 config.adb_path.as_slice(),
1419 vec!(("".to_string(), "".to_string())),
1420 Some("".to_string()))
1421 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1423 // get stderr of result
1424 runargs = Vec::new();
1425 runargs.push("shell".to_string());
1426 runargs.push("cat".to_string());
1427 runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1429 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1431 config.adb_path.as_slice(),
1434 vec!(("".to_string(), "".to_string())),
1435 Some("".to_string()))
1436 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1440 stdout_out.as_slice(),
1441 stderr_out.as_slice());
1444 status: process::ExitStatus(exitcode),
1451 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1452 let tdir = aux_output_dir_name(config, testfile);
1454 let dirs = fs::readdir(&tdir).unwrap();
1455 for file in dirs.iter() {
1456 if file.extension_str() == Some("so") {
1457 // FIXME (#9639): This needs to handle non-utf8 paths
1458 let copy_result = procsrv::run("",
1459 config.adb_path.as_slice(),
1466 config.adb_test_dir.to_string()
1468 vec!(("".to_string(),
1470 Some("".to_string()))
1471 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1474 println!("push ({}) {} {} {}",
1475 config.target, file.display(),
1476 copy_result.out, copy_result.err);
1482 // codegen tests (vs. clang)
1484 fn make_o_name(config: &Config, testfile: &Path) -> Path {
1485 output_base_name(config, testfile).with_extension("o")
1488 fn append_suffix_to_stem(p: &Path, suffix: &str) -> Path {
1489 if suffix.len() == 0 {
1492 let stem = p.filestem().unwrap();
1493 p.with_filename(Vec::from_slice(stem).append(bytes!("-")).append(suffix.as_bytes()))
1497 fn compile_test_and_save_bitcode(config: &Config, props: &TestProps,
1498 testfile: &Path) -> ProcRes {
1499 let aux_dir = aux_output_dir_name(config, testfile);
1500 // FIXME (#9639): This needs to handle non-utf8 paths
1501 let link_args = vec!("-L".to_string(),
1502 aux_dir.as_str().unwrap().to_string());
1503 let llvm_args = vec!("--emit=obj".to_string(),
1504 "--crate-type=lib".to_string(),
1506 "save-temps".to_string());
1507 let args = make_compile_args(config,
1509 link_args.append(llvm_args.as_slice()),
1510 |a, b| ThisFile(make_o_name(a, b)), testfile);
1511 compose_and_run_compiler(config, props, testfile, args, None)
1514 fn compile_cc_with_clang_and_save_bitcode(config: &Config, _props: &TestProps,
1515 testfile: &Path) -> ProcRes {
1516 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1517 let bitcodefile = append_suffix_to_stem(&bitcodefile, "clang");
1518 let testcc = testfile.with_extension("cc");
1519 let proc_args = ProcArgs {
1520 // FIXME (#9639): This needs to handle non-utf8 paths
1521 prog: config.clang_path.get_ref().as_str().unwrap().to_string(),
1522 args: vec!("-c".to_string(),
1523 "-emit-llvm".to_string(),
1525 bitcodefile.as_str().unwrap().to_string(),
1526 testcc.as_str().unwrap().to_string())
1528 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1531 fn extract_function_from_bitcode(config: &Config, _props: &TestProps,
1532 fname: &str, testfile: &Path,
1533 suffix: &str) -> ProcRes {
1534 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1535 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1536 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1537 let prog = config.llvm_bin_path.get_ref().join("llvm-extract");
1538 let proc_args = ProcArgs {
1539 // FIXME (#9639): This needs to handle non-utf8 paths
1540 prog: prog.as_str().unwrap().to_string(),
1541 args: vec!(format!("-func={}", fname),
1542 format!("-o={}", extracted_bc.as_str().unwrap()),
1543 bitcodefile.as_str().unwrap().to_string())
1545 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1548 fn disassemble_extract(config: &Config, _props: &TestProps,
1549 testfile: &Path, suffix: &str) -> ProcRes {
1550 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1551 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1552 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1553 let extracted_ll = extracted_bc.with_extension("ll");
1554 let prog = config.llvm_bin_path.get_ref().join("llvm-dis");
1555 let proc_args = ProcArgs {
1556 // FIXME (#9639): This needs to handle non-utf8 paths
1557 prog: prog.as_str().unwrap().to_string(),
1558 args: vec!(format!("-o={}", extracted_ll.as_str().unwrap()),
1559 extracted_bc.as_str().unwrap().to_string())
1561 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1565 fn count_extracted_lines(p: &Path) -> uint {
1566 let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
1567 let x = str::from_utf8(x.as_slice()).unwrap();
1572 fn run_codegen_test(config: &Config, props: &TestProps,
1573 testfile: &Path, mm: &mut MetricMap) {
1575 if config.llvm_bin_path.is_none() {
1576 fatal("missing --llvm-bin-path".to_string());
1579 if config.clang_path.is_none() {
1580 fatal("missing --clang-path".to_string());
1583 let mut proc_res = compile_test_and_save_bitcode(config, props, testfile);
1584 if !proc_res.status.success() {
1585 fatal_proc_rec("compilation failed!".to_string(), &proc_res);
1588 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "");
1589 if !proc_res.status.success() {
1590 fatal_proc_rec("extracting 'test' function failed".to_string(),
1594 proc_res = disassemble_extract(config, props, testfile, "");
1595 if !proc_res.status.success() {
1596 fatal_proc_rec("disassembling extract failed".to_string(), &proc_res);
1600 let mut proc_res = compile_cc_with_clang_and_save_bitcode(config, props, testfile);
1601 if !proc_res.status.success() {
1602 fatal_proc_rec("compilation failed!".to_string(), &proc_res);
1605 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "clang");
1606 if !proc_res.status.success() {
1607 fatal_proc_rec("extracting 'test' function failed".to_string(),
1611 proc_res = disassemble_extract(config, props, testfile, "clang");
1612 if !proc_res.status.success() {
1613 fatal_proc_rec("disassembling extract failed".to_string(), &proc_res);
1616 let base = output_base_name(config, testfile);
1617 let base_extract = append_suffix_to_stem(&base, "extract");
1619 let base_clang = append_suffix_to_stem(&base, "clang");
1620 let base_clang_extract = append_suffix_to_stem(&base_clang, "extract");
1622 let base_lines = count_extracted_lines(&base_extract);
1623 let clang_lines = count_extracted_lines(&base_clang_extract);
1625 mm.insert_metric("clang-codegen-ratio",
1626 (base_lines as f64) / (clang_lines as f64),