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_ProcRes("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_ProcRes("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_ProcRes("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_strbuf!("failure produced the wrong error: {}",
130 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
132 let mut proc_res = compile_test(config, props, testfile);
134 if !proc_res.status.success() {
135 fatal_ProcRes("compilation failed!".to_string(), &proc_res);
138 proc_res = exec_compiled_test(config, props, testfile);
140 if !proc_res.status.success() {
141 fatal_ProcRes("test run failed!".to_string(), &proc_res);
144 let proc_res = jit_test(config, props, testfile);
146 if !proc_res.status.success() {
147 fatal_ProcRes("jit failed!".to_string(), &proc_res);
152 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
153 if props.pp_exact.is_some() {
154 logv(config, "testing for exact pretty-printing".to_string());
156 logv(config, "testing for converging pretty-printing".to_string());
160 match props.pp_exact { Some(_) => 1, None => 2 };
162 let src = File::open(testfile).read_to_end().unwrap();
163 let src = str::from_utf8(src.as_slice()).unwrap().to_string();
164 let mut srcs = vec!(src);
167 while round < rounds {
168 logv(config, format_strbuf!("pretty-printing round {}", round));
169 let proc_res = print_source(config,
172 (*srcs.get(round)).to_string(),
175 if !proc_res.status.success() {
176 fatal_ProcRes(format_strbuf!("pretty-printing failed in round {}",
181 let ProcRes{ stdout, .. } = proc_res;
186 let mut expected = match props.pp_exact {
188 let filepath = testfile.dir_path().join(file);
189 let s = File::open(&filepath).read_to_end().unwrap();
190 str::from_utf8(s.as_slice()).unwrap().to_string()
192 None => { (*srcs.get(srcs.len() - 2u)).clone() }
194 let mut actual = (*srcs.get(srcs.len() - 1u)).clone();
196 if props.pp_exact.is_some() {
197 // Now we have to care about line endings
198 let cr = "\r".to_string();
199 actual = actual.replace(cr.as_slice(), "").to_string();
200 expected = expected.replace(cr.as_slice(), "").to_string();
203 compare_source(expected.as_slice(), actual.as_slice());
205 // Finally, let's make sure it actually appears to remain valid code
206 let proc_res = typecheck_source(config, props, testfile, actual);
208 if !proc_res.status.success() {
209 fatal_ProcRes("pretty-printed source does not typecheck".to_string(),
212 if props.no_pretty_expanded { return }
214 // additionally, run `--pretty expanded` and try to build it.
215 let proc_res = print_source(config, props, testfile, (*srcs.get(round)).clone(), "expanded");
216 if !proc_res.status.success() {
217 fatal_ProcRes(format_strbuf!("pretty-printing (expanded) failed"),
221 let ProcRes{ stdout: expanded_src, .. } = proc_res;
222 let proc_res = typecheck_source(config, props, testfile, expanded_src);
223 if !proc_res.status.success() {
224 fatal_ProcRes(format_strbuf!("pretty-printed source (expanded) does \
231 fn print_source(config: &Config,
235 pretty_type: &str) -> ProcRes {
236 compose_and_run(config,
241 pretty_type.to_string()),
242 props.exec_env.clone(),
243 config.compile_lib_path.as_slice(),
247 fn make_pp_args(config: &Config,
250 pretty_type: String) -> ProcArgs {
251 let aux_dir = aux_output_dir_name(config, testfile);
252 // FIXME (#9639): This needs to handle non-utf8 paths
253 let mut args = vec!("-".to_string(),
254 "--pretty".to_string(),
256 format_strbuf!("--target={}", config.target),
258 aux_dir.as_str().unwrap().to_string());
259 args.push_all_move(split_maybe_args(&config.target_rustcflags));
260 args.push_all_move(split_maybe_args(&props.compile_flags));
262 prog: config.rustc_path.as_str().unwrap().to_string(),
267 fn compare_source(expected: &str, actual: &str) {
268 if expected != actual {
269 error("pretty-printed source does not match expected \
270 source".to_string());
273 ------------------------------------------\n\
275 ------------------------------------------\n\
277 ------------------------------------------\n\
279 ------------------------------------------\n\
286 fn typecheck_source(config: &Config, props: &TestProps,
287 testfile: &Path, src: String) -> ProcRes {
288 let args = make_typecheck_args(config, props, testfile);
289 compose_and_run_compiler(config, props, testfile, args, Some(src))
292 fn make_typecheck_args(config: &Config, props: &TestProps, testfile: &Path) -> ProcArgs {
293 let aux_dir = aux_output_dir_name(config, testfile);
294 let target = if props.force_host {
295 config.host.as_slice()
297 config.target.as_slice()
299 // FIXME (#9639): This needs to handle non-utf8 paths
300 let mut args = vec!("-".to_string(),
301 "--no-trans".to_string(),
302 "--crate-type=lib".to_string(),
303 format_strbuf!("--target={}", target),
305 config.build_base.as_str().unwrap().to_string(),
307 aux_dir.as_str().unwrap().to_string());
308 args.push_all_move(split_maybe_args(&config.target_rustcflags));
309 args.push_all_move(split_maybe_args(&props.compile_flags));
310 // FIXME (#9639): This needs to handle non-utf8 paths
312 prog: config.rustc_path.as_str().unwrap().to_string(),
318 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
319 let mut config = Config {
320 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
321 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
325 let config = &mut config;
326 let DebuggerCommands { commands, check_lines, .. } = parse_debugger_commands(testfile, "gdb");
327 let mut cmds = commands.connect("\n").to_string();
329 // compile test file (it shoud have 'compile-flags:-g' in the header)
330 let compiler_run_result = compile_test(config, props, testfile);
331 if !compiler_run_result.status.success() {
332 fatal_ProcRes("compilation failed!".to_string(), &compiler_run_result);
335 let exe_file = make_exe_name(config, testfile);
338 let debugger_run_result;
339 match config.target.as_slice() {
340 "arm-linux-androideabi" => {
342 cmds = cmds.replace("run", "continue").to_string();
344 // write debugger script
345 let script_str = ["set charset UTF-8".to_string(),
346 format_strbuf!("file {}",
350 "target remote :5039".to_string(),
352 "quit".to_string()].connect("\n");
353 debug!("script_str = {}", script_str);
354 dump_output_file(config,
356 script_str.as_slice(),
361 config.adb_path.as_slice(),
364 exe_file.as_str().unwrap().to_string(),
365 config.adb_test_dir.clone()
367 vec!(("".to_string(), "".to_string())),
368 Some("".to_string()))
369 .expect(format_strbuf!("failed to exec `{}`",
373 config.adb_path.as_slice(),
375 "forward".to_string(),
376 "tcp:5039".to_string(),
377 "tcp:5039".to_string()
379 vec!(("".to_string(), "".to_string())),
380 Some("".to_string()))
381 .expect(format_strbuf!("failed to exec `{}`", config.adb_path));
383 let adb_arg = format_strbuf!("export LD_LIBRARY_PATH={}; \
384 gdbserver :5039 {}/{}",
385 config.adb_test_dir.clone(),
386 config.adb_test_dir.clone(),
389 .unwrap()).unwrap());
391 let mut process = procsrv::run_background("",
398 vec!(("".to_string(),
400 Some("".to_string()))
401 .expect(format_strbuf!("failed to exec `{}`",
404 //waiting 1 second for gdbserver start
406 let result = task::try(proc() {
407 tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
415 let tool_path = match config.android_cross_path.as_str() {
416 Some(x) => x.to_string(),
417 None => fatal("cannot find android cross path".to_string())
420 let debugger_script = make_out_name(config, testfile, "debugger.script");
421 // FIXME (#9639): This needs to handle non-utf8 paths
423 vec!("-quiet".to_string(),
424 "-batch".to_string(),
426 format_strbuf!("-command={}",
427 debugger_script.as_str().unwrap()));
429 let gdb_path = tool_path.append("/bin/arm-linux-androideabi-gdb");
430 let procsrv::Result {
436 debugger_opts.as_slice(),
437 vec!(("".to_string(), "".to_string())),
439 .expect(format_strbuf!("failed to exec `{}`", gdb_path));
441 let cmdline = make_cmdline("",
442 "arm-linux-androideabi-gdb",
443 debugger_opts.as_slice());
444 logv(config, format_strbuf!("executing {}", cmdline));
448 debugger_run_result = ProcRes {
454 process.signal_kill().unwrap();
458 // write debugger script
460 "set charset UTF-8".to_string(),
464 debug!("script_str = {}", script_str);
465 dump_output_file(config,
467 script_str.as_slice(),
470 // run debugger script with gdb
472 fn debugger() -> String {
473 "gdb.exe".to_string()
476 fn debugger() -> String {
480 let debugger_script = make_out_name(config, testfile, "debugger.script");
482 // FIXME (#9639): This needs to handle non-utf8 paths
484 vec!("-quiet".to_string(),
485 "-batch".to_string(),
487 format_strbuf!("-command={}",
488 debugger_script.as_str().unwrap()),
489 exe_file.as_str().unwrap().to_string());
490 proc_args = ProcArgs {
494 debugger_run_result = compose_and_run(config,
503 if !debugger_run_result.status.success() {
504 fatal("gdb failed to execute".to_string());
507 check_debugger_output(&debugger_run_result, check_lines.as_slice());
510 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
511 use std::io::process::{Command, ProcessOutput};
513 if config.lldb_python_dir.is_none() {
514 fatal("Can't run LLDB test because LLDB's python path is not \
518 let mut config = Config {
519 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
520 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
524 let config = &mut config;
526 // compile test file (it shoud have 'compile-flags:-g' in the header)
527 let compile_result = compile_test(config, props, testfile);
528 if !compile_result.status.success() {
529 fatal_ProcRes("compilation failed!".to_string(), &compile_result);
532 let exe_file = make_exe_name(config, testfile);
534 // Parse debugger commands etc from test files
535 let DebuggerCommands {
539 } = parse_debugger_commands(testfile, "lldb");
541 // Write debugger script:
542 // We don't want to hang when calling `quit` while the process is still running
543 let mut script_str = String::from_str("settings set auto-confirm true\n");
545 // Set breakpoints on every line that contains the string "#break"
546 for line in breakpoint_lines.iter() {
547 script_str.push_str(format!("breakpoint set --line {}\n",
551 // Append the other commands
552 for line in commands.iter() {
553 script_str.push_str(line.as_slice());
554 script_str.push_str("\n");
557 // Finally, quit the debugger
558 script_str.push_str("quit\n");
560 // Write the script into a file
561 debug!("script_str = {}", script_str);
562 dump_output_file(config,
564 script_str.as_slice(),
566 let debugger_script = make_out_name(config, testfile, "debugger.script");
568 // Let LLDB execute the script via lldb_batchmode.py
569 let debugger_run_result = run_lldb(config, &exe_file, &debugger_script);
571 if !debugger_run_result.status.success() {
572 fatal_ProcRes("Error while running LLDB".to_string(),
573 &debugger_run_result);
576 check_debugger_output(&debugger_run_result, check_lines.as_slice());
578 fn run_lldb(config: &Config, test_executable: &Path, debugger_script: &Path) -> ProcRes {
579 // Prepare the lldb_batchmode which executes the debugger script
580 let mut cmd = Command::new("python");
581 cmd.arg("./src/etc/lldb_batchmode.py")
582 .arg(test_executable)
583 .arg(debugger_script)
584 .env([("PYTHONPATH", config.lldb_python_dir.clone().unwrap().as_slice())]);
586 let (status, out, err) = match cmd.spawn() {
588 let ProcessOutput { status, output, error } =
589 process.wait_with_output().unwrap();
592 str::from_utf8(output.as_slice()).unwrap().to_string(),
593 str::from_utf8(error.as_slice()).unwrap().to_string())
596 fatal(format_strbuf!("Failed to setup Python process for \
602 dump_output(config, test_executable, out.as_slice(), err.as_slice());
607 cmdline: format_strbuf!("{}", cmd)
612 struct DebuggerCommands {
613 commands: Vec<String>,
614 check_lines: Vec<String>,
615 breakpoint_lines: Vec<uint>,
618 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
619 -> DebuggerCommands {
620 use std::io::{BufferedReader, File};
622 let command_directive = format!("{}-command", debugger_prefix);
623 let check_directive = format!("{}-check", debugger_prefix);
625 let mut breakpoint_lines = vec!();
626 let mut commands = vec!();
627 let mut check_lines = vec!();
629 let mut reader = BufferedReader::new(File::open(file_path).unwrap());
630 for line in reader.lines() {
633 if line.as_slice().contains("#break") {
634 breakpoint_lines.push(counter);
637 header::parse_name_value_directive(
639 command_directive.to_string()).map(|cmd| {
643 header::parse_name_value_directive(
645 check_directive.to_string()).map(|cmd| {
646 check_lines.push(cmd)
650 fatal(format_strbuf!("Error while parsing debugger commands: \
660 check_lines: check_lines,
661 breakpoint_lines: breakpoint_lines
665 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
666 if options.is_none() {
670 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
671 let options_to_remove = [
674 "--debuginfo".to_string()
677 split_maybe_args(options).move_iter()
678 .filter(|x| !options_to_remove.contains(x))
679 .collect::<Vec<String>>()
685 fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String]) {
686 let num_check_lines = check_lines.len();
687 if num_check_lines > 0 {
688 // Allow check lines to leave parts unspecified (e.g., uninitialized
689 // bits in the wrong case of an enum) with the notation "[...]".
690 let check_fragments: Vec<Vec<String>> =
691 check_lines.iter().map(|s| {
695 .map(|x| x.to_string())
698 // check if each line in props.check_lines appears in the
701 for line in debugger_run_result.stdout.as_slice().lines() {
702 let mut rest = line.trim();
703 let mut first = true;
704 let mut failed = false;
705 for frag in check_fragments.get(i).iter() {
706 let found = if first {
707 if rest.starts_with(frag.as_slice()) {
713 rest.find_str(frag.as_slice())
721 rest = rest.slice_from(i + frag.len());
726 if !failed && rest.len() == 0 {
729 if i == num_check_lines {
734 if i != num_check_lines {
735 fatal_ProcRes(format_strbuf!("line not found in debugger output: \
737 check_lines.get(i).unwrap()),
738 debugger_run_result);
743 fn check_error_patterns(props: &TestProps,
745 proc_res: &ProcRes) {
746 if props.error_patterns.is_empty() {
747 fatal(format_strbuf!("no error pattern specified in {}",
748 testfile.display().as_maybe_owned().as_slice()));
751 if proc_res.status.success() {
752 fatal("process did not return an error status".to_string());
755 let mut next_err_idx = 0u;
756 let mut next_err_pat = props.error_patterns.get(next_err_idx);
757 let mut done = false;
758 let output_to_check = if props.check_stdout {
759 format_strbuf!("{}{}", proc_res.stdout, proc_res.stderr)
761 proc_res.stderr.clone()
763 for line in output_to_check.as_slice().lines() {
764 if line.contains(next_err_pat.as_slice()) {
765 debug!("found error pattern {}", *next_err_pat);
767 if next_err_idx == props.error_patterns.len() {
768 debug!("found all error patterns");
772 next_err_pat = props.error_patterns.get(next_err_idx);
777 let missing_patterns =
778 props.error_patterns.slice(next_err_idx, props.error_patterns.len());
779 if missing_patterns.len() == 1u {
780 fatal_ProcRes(format_strbuf!("error pattern '{}' not found!",
781 missing_patterns[0]),
784 for pattern in missing_patterns.iter() {
785 error(format_strbuf!("error pattern '{}' not found!", *pattern));
787 fatal_ProcRes("multiple error patterns not found".to_string(),
792 fn check_no_compiler_crash(proc_res: &ProcRes) {
793 for line in proc_res.stderr.as_slice().lines() {
794 if line.starts_with("error: internal compiler error:") {
795 fatal_ProcRes("compiler encountered internal error".to_string(),
801 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
803 proc_res: &ProcRes) {
805 // true if we found the error in question
806 let mut found_flags = Vec::from_elem(
807 expected_errors.len(), false);
809 if proc_res.status.success() {
810 fatal("process did not return an error status".to_string());
813 let prefixes = expected_errors.iter().map(|ee| {
814 format_strbuf!("{}:{}:", testfile.display(), ee.line)
815 }).collect::<Vec<String> >();
817 #[cfg(target_os = "win32")]
818 fn to_lower( s : &str ) -> String {
820 let c : Vec<char> = i.map( |c| {
822 c.to_ascii().to_lowercase().to_char()
827 str::from_chars(c.as_slice()).to_string()
830 #[cfg(target_os = "win32")]
831 fn prefix_matches( line : &str, prefix : &str ) -> bool {
832 to_lower(line).as_slice().starts_with(to_lower(prefix).as_slice())
835 #[cfg(target_os = "linux")]
836 #[cfg(target_os = "macos")]
837 #[cfg(target_os = "freebsd")]
838 fn prefix_matches( line : &str, prefix : &str ) -> bool {
839 line.starts_with( prefix )
842 // Scan and extract our error/warning messages,
844 // filename:line1:col1: line2:col2: *error:* msg
845 // filename:line1:col1: line2:col2: *warning:* msg
846 // where line1:col1: is the starting point, line2:col2:
847 // is the ending point, and * represents ANSI color codes.
848 for line in proc_res.stderr.as_slice().lines() {
849 let mut was_expected = false;
850 for (i, ee) in expected_errors.iter().enumerate() {
851 if !*found_flags.get(i) {
852 debug!("prefix={} ee.kind={} ee.msg={} line={}",
853 prefixes.get(i).as_slice(),
857 if prefix_matches(line, prefixes.get(i).as_slice()) &&
858 line.contains(ee.kind.as_slice()) &&
859 line.contains(ee.msg.as_slice()) {
860 *found_flags.get_mut(i) = true;
867 // ignore this msg which gets printed at the end
868 if line.contains("aborting due to") {
872 if !was_expected && is_compiler_error_or_warning(line) {
873 fatal_ProcRes(format_strbuf!("unexpected compiler error or \
880 for (i, &flag) in found_flags.iter().enumerate() {
882 let ee = expected_errors.get(i);
883 fatal_ProcRes(format_strbuf!("expected {} on line {} not found: \
893 fn is_compiler_error_or_warning(line: &str) -> bool {
896 scan_until_char(line, ':', &mut i) &&
897 scan_char(line, ':', &mut i) &&
898 scan_integer(line, &mut i) &&
899 scan_char(line, ':', &mut i) &&
900 scan_integer(line, &mut i) &&
901 scan_char(line, ':', &mut i) &&
902 scan_char(line, ' ', &mut i) &&
903 scan_integer(line, &mut i) &&
904 scan_char(line, ':', &mut i) &&
905 scan_integer(line, &mut i) &&
906 scan_char(line, ' ', &mut i) &&
907 (scan_string(line, "error", &mut i) ||
908 scan_string(line, "warning", &mut i));
911 fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
912 if *idx >= haystack.len() {
915 let opt = haystack.slice_from(*idx).find(needle);
923 fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
924 if *idx >= haystack.len() {
927 let range = haystack.char_range_at(*idx);
928 if range.ch != needle {
935 fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
937 while i < haystack.len() {
938 let range = haystack.char_range_at(i);
939 if range.ch < '0' || '9' < range.ch {
951 fn scan_string(haystack: &str, needle: &str, idx: &mut uint) -> bool {
952 let mut haystack_i = *idx;
953 let mut needle_i = 0u;
954 while needle_i < needle.len() {
955 if haystack_i >= haystack.len() {
958 let range = haystack.char_range_at(haystack_i);
959 haystack_i = range.next;
960 if !scan_char(needle, range.ch, &mut needle_i) {
980 fn compile_test(config: &Config, props: &TestProps,
981 testfile: &Path) -> ProcRes {
982 compile_test_(config, props, testfile, [])
985 fn jit_test(config: &Config, props: &TestProps, testfile: &Path) -> ProcRes {
986 compile_test_(config, props, testfile, ["--jit".to_string()])
989 fn compile_test_(config: &Config, props: &TestProps,
990 testfile: &Path, extra_args: &[String]) -> ProcRes {
991 let aux_dir = aux_output_dir_name(config, testfile);
992 // FIXME (#9639): This needs to handle non-utf8 paths
993 let link_args = vec!("-L".to_string(),
994 aux_dir.as_str().unwrap().to_string());
995 let args = make_compile_args(config,
997 link_args.append(extra_args),
998 |a, b| ThisFile(make_exe_name(a, b)), testfile);
999 compose_and_run_compiler(config, props, testfile, args, None)
1002 fn exec_compiled_test(config: &Config, props: &TestProps,
1003 testfile: &Path) -> ProcRes {
1005 let env = props.exec_env.clone();
1007 match config.target.as_slice() {
1009 "arm-linux-androideabi" => {
1010 _arm_exec_compiled_test(config, props, testfile, env)
1014 compose_and_run(config,
1016 make_run_args(config, props, testfile),
1018 config.run_lib_path.as_slice(),
1024 fn compose_and_run_compiler(
1029 input: Option<String>) -> ProcRes {
1031 if !props.aux_builds.is_empty() {
1032 ensure_dir(&aux_output_dir_name(config, testfile));
1035 let aux_dir = aux_output_dir_name(config, testfile);
1036 // FIXME (#9639): This needs to handle non-utf8 paths
1037 let extra_link_args = vec!("-L".to_string(), aux_dir.as_str().unwrap().to_string());
1039 for rel_ab in props.aux_builds.iter() {
1040 let abs_ab = config.aux_base.join(rel_ab.as_slice());
1041 let aux_props = header::load_props(&abs_ab);
1042 let crate_type = if aux_props.no_prefer_dynamic {
1045 vec!("--crate-type=dylib".to_string())
1048 make_compile_args(config,
1051 extra_link_args.iter()
1052 .map(|x| x.to_string())
1053 .collect::<Vec<_>>()
1056 let f = make_lib_name(a, b, testfile);
1057 ThisDirectory(f.dir_path())
1060 let auxres = compose_and_run(config,
1064 config.compile_lib_path.as_slice(),
1066 if !auxres.status.success() {
1068 format_strbuf!("auxiliary build of {} failed to compile: ",
1073 match config.target.as_slice() {
1074 "arm-linux-androideabi" => {
1075 _arm_push_aux_shared_library(config, testfile);
1081 compose_and_run(config,
1085 config.compile_lib_path.as_slice(),
1089 fn ensure_dir(path: &Path) {
1090 if path.is_dir() { return; }
1091 fs::mkdir(path, io::UserRWX).unwrap();
1094 fn compose_and_run(config: &Config, testfile: &Path,
1095 ProcArgs{ args, prog }: ProcArgs,
1096 procenv: Vec<(String, String)> ,
1098 input: Option<String>) -> ProcRes {
1099 return program_output(config, testfile, lib_path,
1100 prog, args, procenv, input);
1103 enum TargetLocation {
1105 ThisDirectory(Path),
1108 fn make_compile_args(config: &Config,
1110 extras: Vec<String> ,
1111 xform: |&Config, &Path| -> TargetLocation,
1114 let xform_file = xform(config, testfile);
1115 let target = if props.force_host {
1116 config.host.as_slice()
1118 config.target.as_slice()
1120 // FIXME (#9639): This needs to handle non-utf8 paths
1121 let mut args = vec!(testfile.as_str().unwrap().to_string(),
1123 config.build_base.as_str().unwrap().to_string(),
1124 format_strbuf!("--target={}", target));
1125 args.push_all(extras.as_slice());
1126 if !props.no_prefer_dynamic {
1127 args.push("-C".to_string());
1128 args.push("prefer-dynamic".to_string());
1130 let path = match xform_file {
1132 args.push("-o".to_string());
1135 ThisDirectory(path) => {
1136 args.push("--out-dir".to_string());
1140 args.push(path.as_str().unwrap().to_string());
1141 if props.force_host {
1142 args.push_all_move(split_maybe_args(&config.host_rustcflags));
1144 args.push_all_move(split_maybe_args(&config.target_rustcflags));
1146 args.push_all_move(split_maybe_args(&props.compile_flags));
1148 prog: config.rustc_path.as_str().unwrap().to_string(),
1153 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> Path {
1154 // what we return here is not particularly important, as it
1155 // happens; rustc ignores everything except for the directory.
1156 let auxname = output_testname(auxfile);
1157 aux_output_dir_name(config, testfile).join(&auxname)
1160 fn make_exe_name(config: &Config, testfile: &Path) -> Path {
1161 let mut f = output_base_name(config, testfile);
1162 if !os::consts::EXE_SUFFIX.is_empty() {
1163 match f.filename().map(|s| Vec::from_slice(s).append(os::consts::EXE_SUFFIX.as_bytes())) {
1164 Some(v) => f.set_filename(v),
1171 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path) ->
1173 // If we've got another tool to run under (valgrind),
1174 // then split apart its command
1175 let mut args = split_maybe_args(&config.runtool);
1176 let exe_file = make_exe_name(config, testfile);
1178 // FIXME (#9639): This needs to handle non-utf8 paths
1179 args.push(exe_file.as_str().unwrap().to_string());
1181 // Add the arguments in the run_flags directive
1182 args.push_all_move(split_maybe_args(&props.run_flags));
1184 let prog = args.shift().unwrap();
1191 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1197 if s.is_whitespace() {
1208 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1209 args: Vec<String> , env: Vec<(String, String)> ,
1210 input: Option<String>) -> ProcRes {
1213 let cmdline = make_cmdline(lib_path,
1216 logv(config, format_strbuf!("executing {}", cmdline));
1219 let procsrv::Result {
1223 } = procsrv::run(lib_path,
1227 input).expect(format_strbuf!("failed to exec `{}`",
1229 dump_output(config, testfile, out.as_slice(), err.as_slice());
1238 // Linux and mac don't require adjusting the library search path
1239 #[cfg(target_os = "linux")]
1240 #[cfg(target_os = "macos")]
1241 #[cfg(target_os = "freebsd")]
1242 fn make_cmdline(_libpath: &str, prog: &str, args: &[String]) -> String {
1243 format_strbuf!("{} {}", prog, args.connect(" "))
1246 #[cfg(target_os = "win32")]
1247 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1248 format_strbuf!("{} {} {}",
1249 lib_path_cmd_prefix(libpath),
1254 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1255 // for diagnostic purposes
1256 #[cfg(target_os = "win32")]
1257 fn lib_path_cmd_prefix(path: &str) -> String {
1258 format_strbuf!("{}=\"{}\"",
1259 util::lib_path_env_var(),
1260 util::make_new_path(path))
1263 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1264 dump_output_file(config, testfile, out, "out");
1265 dump_output_file(config, testfile, err, "err");
1266 maybe_dump_to_stdout(config, out, err);
1269 fn dump_output_file(config: &Config, testfile: &Path,
1270 out: &str, extension: &str) {
1271 let outfile = make_out_name(config, testfile, extension);
1272 File::create(&outfile).write(out.as_bytes()).unwrap();
1275 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> Path {
1276 output_base_name(config, testfile).with_extension(extension)
1279 fn aux_output_dir_name(config: &Config, testfile: &Path) -> Path {
1280 let mut f = output_base_name(config, testfile);
1281 match f.filename().map(|s| Vec::from_slice(s).append(bytes!(".libaux"))) {
1282 Some(v) => f.set_filename(v),
1288 fn output_testname(testfile: &Path) -> Path {
1289 Path::new(testfile.filestem().unwrap())
1292 fn output_base_name(config: &Config, testfile: &Path) -> Path {
1294 .join(&output_testname(testfile))
1295 .with_extension(config.stage_id.as_slice())
1298 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1300 println!("------{}------------------------------", "stdout");
1301 println!("{}", out);
1302 println!("------{}------------------------------", "stderr");
1303 println!("{}", err);
1304 println!("------------------------------------------");
1308 fn error(err: String) { println!("\nerror: {}", err); }
1310 fn fatal(err: String) -> ! { error(err); fail!(); }
1312 fn fatal_ProcRes(err: String, proc_res: &ProcRes) -> ! {
1318 ------------------------------------------\n\
1320 ------------------------------------------\n\
1322 ------------------------------------------\n\
1324 ------------------------------------------\n\
1326 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1331 fn _arm_exec_compiled_test(config: &Config,
1334 env: Vec<(String, String)>)
1336 let args = make_run_args(config, props, testfile);
1337 let cmdline = make_cmdline("",
1338 args.prog.as_slice(),
1339 args.args.as_slice());
1341 // get bare program string
1342 let mut tvec: Vec<String> = args.prog
1345 .map(|ts| ts.to_string())
1347 let prog_short = tvec.pop().unwrap();
1350 let copy_result = procsrv::run("",
1351 config.adb_path.as_slice(),
1355 config.adb_test_dir.clone()
1357 vec!(("".to_string(), "".to_string())),
1358 Some("".to_string()))
1359 .expect(format_strbuf!("failed to exec `{}`", config.adb_path));
1362 println!("push ({}) {} {} {}",
1369 logv(config, format_strbuf!("executing ({}) {}", config.target, cmdline));
1371 let mut runargs = Vec::new();
1373 // run test via adb_run_wrapper
1374 runargs.push("shell".to_string());
1375 for (key, val) in env.move_iter() {
1376 runargs.push(format_strbuf!("{}={}", key, val));
1378 runargs.push(format_strbuf!("{}/adb_run_wrapper.sh",
1379 config.adb_test_dir));
1380 runargs.push(format_strbuf!("{}", config.adb_test_dir));
1381 runargs.push(format_strbuf!("{}", prog_short));
1383 for tv in args.args.iter() {
1384 runargs.push(tv.to_string());
1387 config.adb_path.as_slice(),
1389 vec!(("".to_string(), "".to_string())), Some("".to_string()))
1390 .expect(format_strbuf!("failed to exec `{}`", config.adb_path));
1392 // get exitcode of result
1393 runargs = Vec::new();
1394 runargs.push("shell".to_string());
1395 runargs.push("cat".to_string());
1396 runargs.push(format_strbuf!("{}/{}.exitcode",
1397 config.adb_test_dir,
1400 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1402 config.adb_path.as_slice(),
1404 vec!(("".to_string(), "".to_string())),
1405 Some("".to_string()))
1406 .expect(format_strbuf!("failed to exec `{}`", config.adb_path));
1408 let mut exitcode: int = 0;
1409 for c in exitcode_out.as_slice().chars() {
1410 if !c.is_digit() { break; }
1411 exitcode = exitcode * 10 + match c {
1412 '0' .. '9' => c as int - ('0' as int),
1417 // get stdout of result
1418 runargs = Vec::new();
1419 runargs.push("shell".to_string());
1420 runargs.push("cat".to_string());
1421 runargs.push(format_strbuf!("{}/{}.stdout",
1422 config.adb_test_dir,
1425 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1427 config.adb_path.as_slice(),
1429 vec!(("".to_string(), "".to_string())),
1430 Some("".to_string()))
1431 .expect(format_strbuf!("failed to exec `{}`", config.adb_path));
1433 // get stderr of result
1434 runargs = Vec::new();
1435 runargs.push("shell".to_string());
1436 runargs.push("cat".to_string());
1437 runargs.push(format_strbuf!("{}/{}.stderr",
1438 config.adb_test_dir,
1441 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1443 config.adb_path.as_slice(),
1445 vec!(("".to_string(), "".to_string())),
1446 Some("".to_string()))
1447 .expect(format_strbuf!("failed to exec `{}`", config.adb_path));
1451 stdout_out.as_slice(),
1452 stderr_out.as_slice());
1455 status: process::ExitStatus(exitcode),
1462 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1463 let tdir = aux_output_dir_name(config, testfile);
1465 let dirs = fs::readdir(&tdir).unwrap();
1466 for file in dirs.iter() {
1467 if file.extension_str() == Some("so") {
1468 // FIXME (#9639): This needs to handle non-utf8 paths
1469 let copy_result = procsrv::run("",
1470 config.adb_path.as_slice(),
1476 config.adb_test_dir.to_string()
1478 vec!(("".to_string(),
1480 Some("".to_string()))
1481 .expect(format_strbuf!("failed to exec `{}`",
1485 println!("push ({}) {} {} {}",
1486 config.target, file.display(),
1487 copy_result.out, copy_result.err);
1493 // codegen tests (vs. clang)
1495 fn make_o_name(config: &Config, testfile: &Path) -> Path {
1496 output_base_name(config, testfile).with_extension("o")
1499 fn append_suffix_to_stem(p: &Path, suffix: &str) -> Path {
1500 if suffix.len() == 0 {
1503 let stem = p.filestem().unwrap();
1504 p.with_filename(Vec::from_slice(stem).append(bytes!("-")).append(suffix.as_bytes()))
1508 fn compile_test_and_save_bitcode(config: &Config, props: &TestProps,
1509 testfile: &Path) -> ProcRes {
1510 let aux_dir = aux_output_dir_name(config, testfile);
1511 // FIXME (#9639): This needs to handle non-utf8 paths
1512 let link_args = vec!("-L".to_string(),
1513 aux_dir.as_str().unwrap().to_string());
1514 let llvm_args = vec!("--emit=obj".to_string(),
1515 "--crate-type=lib".to_string(),
1517 "save-temps".to_string());
1518 let args = make_compile_args(config,
1520 link_args.append(llvm_args.as_slice()),
1521 |a, b| ThisFile(make_o_name(a, b)), testfile);
1522 compose_and_run_compiler(config, props, testfile, args, None)
1525 fn compile_cc_with_clang_and_save_bitcode(config: &Config, _props: &TestProps,
1526 testfile: &Path) -> ProcRes {
1527 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1528 let bitcodefile = append_suffix_to_stem(&bitcodefile, "clang");
1529 let testcc = testfile.with_extension("cc");
1530 let proc_args = ProcArgs {
1531 // FIXME (#9639): This needs to handle non-utf8 paths
1532 prog: config.clang_path.get_ref().as_str().unwrap().to_string(),
1533 args: vec!("-c".to_string(),
1534 "-emit-llvm".to_string(),
1536 bitcodefile.as_str().unwrap().to_string(),
1537 testcc.as_str().unwrap().to_string())
1539 compose_and_run(config, testfile, proc_args, Vec::new(), "", None)
1542 fn extract_function_from_bitcode(config: &Config, _props: &TestProps,
1543 fname: &str, testfile: &Path,
1544 suffix: &str) -> ProcRes {
1545 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1546 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1547 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1548 let prog = config.llvm_bin_path.get_ref().join("llvm-extract");
1549 let proc_args = ProcArgs {
1550 // FIXME (#9639): This needs to handle non-utf8 paths
1551 prog: prog.as_str().unwrap().to_string(),
1552 args: vec!(format_strbuf!("-func={}", fname),
1553 format_strbuf!("-o={}", extracted_bc.as_str().unwrap()),
1554 bitcodefile.as_str().unwrap().to_string())
1556 compose_and_run(config, testfile, proc_args, Vec::new(), "", None)
1559 fn disassemble_extract(config: &Config, _props: &TestProps,
1560 testfile: &Path, suffix: &str) -> ProcRes {
1561 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1562 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1563 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1564 let extracted_ll = extracted_bc.with_extension("ll");
1565 let prog = config.llvm_bin_path.get_ref().join("llvm-dis");
1566 let proc_args = ProcArgs {
1567 // FIXME (#9639): This needs to handle non-utf8 paths
1568 prog: prog.as_str().unwrap().to_string(),
1569 args: vec!(format_strbuf!("-o={}", extracted_ll.as_str().unwrap()),
1570 extracted_bc.as_str().unwrap().to_string())
1572 compose_and_run(config, testfile, proc_args, Vec::new(), "", None)
1576 fn count_extracted_lines(p: &Path) -> uint {
1577 let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
1578 let x = str::from_utf8(x.as_slice()).unwrap();
1583 fn run_codegen_test(config: &Config, props: &TestProps,
1584 testfile: &Path, mm: &mut MetricMap) {
1586 if config.llvm_bin_path.is_none() {
1587 fatal("missing --llvm-bin-path".to_string());
1590 if config.clang_path.is_none() {
1591 fatal("missing --clang-path".to_string());
1594 let mut proc_res = compile_test_and_save_bitcode(config, props, testfile);
1595 if !proc_res.status.success() {
1596 fatal_ProcRes("compilation failed!".to_string(), &proc_res);
1599 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "");
1600 if !proc_res.status.success() {
1601 fatal_ProcRes("extracting 'test' function failed".to_string(),
1605 proc_res = disassemble_extract(config, props, testfile, "");
1606 if !proc_res.status.success() {
1607 fatal_ProcRes("disassembling extract failed".to_string(), &proc_res);
1611 let mut proc_res = compile_cc_with_clang_and_save_bitcode(config, props, testfile);
1612 if !proc_res.status.success() {
1613 fatal_ProcRes("compilation failed!".to_string(), &proc_res);
1616 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "clang");
1617 if !proc_res.status.success() {
1618 fatal_ProcRes("extracting 'test' function failed".to_string(),
1622 proc_res = disassemble_extract(config, props, testfile, "clang");
1623 if !proc_res.status.success() {
1624 fatal_ProcRes("disassembling extract failed".to_string(), &proc_res);
1627 let base = output_base_name(config, testfile);
1628 let base_extract = append_suffix_to_stem(&base, "extract");
1630 let base_clang = append_suffix_to_stem(&base, "clang");
1631 let base_clang_extract = append_suffix_to_stem(&base_clang, "extract");
1633 let base_lines = count_extracted_lines(&base_extract);
1634 let clang_lines = count_extracted_lines(&base_clang_extract);
1636 mm.insert_metric("clang-codegen-ratio",
1637 (base_lines as f64) / (clang_lines as f64),