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!",
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 specified");
87 check_expected_errors(expected_errors, testfile, &proc_res);
89 check_error_patterns(props, testfile, &proc_res);
91 check_no_compiler_crash(&proc_res);
94 fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
95 let proc_res = if !config.jit {
96 let proc_res = compile_test(config, props, testfile);
98 if !proc_res.status.success() {
99 fatal_proc_rec("compilation failed!", &proc_res);
102 exec_compiled_test(config, props, testfile)
104 jit_test(config, props, testfile)
107 // The value our Makefile configures valgrind to return on failure
108 static VALGRIND_ERR: int = 100;
109 if proc_res.status.matches_exit_status(VALGRIND_ERR) {
110 fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
113 check_correct_failure_status(&proc_res);
114 check_error_patterns(props, testfile, &proc_res);
117 fn check_correct_failure_status(proc_res: &ProcRes) {
118 // The value the rust runtime returns on failure
119 static RUST_ERR: int = 101;
120 if !proc_res.status.matches_exit_status(RUST_ERR) {
122 format!("failure produced the wrong error: {}",
123 proc_res.status).as_slice(),
128 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
130 let mut proc_res = compile_test(config, props, testfile);
132 if !proc_res.status.success() {
133 fatal_proc_rec("compilation failed!", &proc_res);
136 proc_res = exec_compiled_test(config, props, testfile);
138 if !proc_res.status.success() {
139 fatal_proc_rec("test run failed!", &proc_res);
142 let proc_res = jit_test(config, props, testfile);
144 if !proc_res.status.success() {
145 fatal_proc_rec("jit failed!", &proc_res);
150 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
151 if props.pp_exact.is_some() {
152 logv(config, "testing for exact pretty-printing".to_string());
154 logv(config, "testing for converging pretty-printing".to_string());
158 match props.pp_exact { Some(_) => 1, None => 2 };
160 let src = File::open(testfile).read_to_end().unwrap();
161 let src = String::from_utf8(src.clone()).unwrap();
162 let mut srcs = vec!(src);
165 while round < rounds {
166 logv(config, format!("pretty-printing round {}", round));
167 let proc_res = print_source(config,
170 srcs[round].to_string(),
173 if !proc_res.status.success() {
174 fatal_proc_rec(format!("pretty-printing failed in 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 String::from_utf8(s).unwrap()
190 None => { srcs[srcs.len() - 2u].clone() }
192 let mut actual = srcs[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", &proc_res);
209 if props.no_pretty_expanded { return }
211 // additionally, run `--pretty expanded` and try to build it.
212 let proc_res = print_source(config, props, testfile, srcs[round].clone(), "expanded");
213 if !proc_res.status.success() {
214 fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
217 let ProcRes{ stdout: expanded_src, .. } = proc_res;
218 let proc_res = typecheck_source(config, props, testfile, expanded_src);
219 if !proc_res.status.success() {
220 fatal_proc_rec("pretty-printed source (expanded) does not typecheck",
226 fn print_source(config: &Config,
230 pretty_type: &str) -> ProcRes {
231 let aux_dir = aux_output_dir_name(config, testfile);
232 compose_and_run(config,
237 pretty_type.to_string()),
238 props.exec_env.clone(),
239 config.compile_lib_path.as_slice(),
240 Some(aux_dir.as_str().unwrap()),
244 fn make_pp_args(config: &Config,
247 pretty_type: String) -> ProcArgs {
248 let aux_dir = aux_output_dir_name(config, testfile);
249 // FIXME (#9639): This needs to handle non-utf8 paths
250 let mut args = vec!("-".to_string(),
251 "--pretty".to_string(),
253 format!("--target={}", config.target),
255 aux_dir.as_str().unwrap().to_string());
256 args.push_all_move(split_maybe_args(&config.target_rustcflags));
257 args.push_all_move(split_maybe_args(&props.compile_flags));
259 prog: config.rustc_path.as_str().unwrap().to_string(),
264 fn compare_source(expected: &str, actual: &str) {
265 if expected != actual {
266 error("pretty-printed source does not match expected source");
269 ------------------------------------------\n\
271 ------------------------------------------\n\
273 ------------------------------------------\n\
275 ------------------------------------------\n\
282 fn typecheck_source(config: &Config, props: &TestProps,
283 testfile: &Path, src: String) -> ProcRes {
284 let args = make_typecheck_args(config, props, testfile);
285 compose_and_run_compiler(config, props, testfile, args, Some(src))
288 fn make_typecheck_args(config: &Config, props: &TestProps, testfile: &Path) -> ProcArgs {
289 let aux_dir = aux_output_dir_name(config, testfile);
290 let target = if props.force_host {
291 config.host.as_slice()
293 config.target.as_slice()
295 // FIXME (#9639): This needs to handle non-utf8 paths
296 let mut args = vec!("-".to_string(),
297 "--no-trans".to_string(),
298 "--crate-type=lib".to_string(),
299 format!("--target={}", target),
301 config.build_base.as_str().unwrap().to_string(),
303 aux_dir.as_str().unwrap().to_string());
304 args.push_all_move(split_maybe_args(&config.target_rustcflags));
305 args.push_all_move(split_maybe_args(&props.compile_flags));
306 // FIXME (#9639): This needs to handle non-utf8 paths
308 prog: config.rustc_path.as_str().unwrap().to_string(),
314 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
315 let mut config = Config {
316 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
317 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
321 let config = &mut config;
322 let DebuggerCommands { commands, check_lines, .. } = parse_debugger_commands(testfile, "gdb");
323 let mut cmds = commands.connect("\n");
325 // compile test file (it shoud have 'compile-flags:-g' in the header)
326 let compiler_run_result = compile_test(config, props, testfile);
327 if !compiler_run_result.status.success() {
328 fatal_proc_rec("compilation failed!", &compiler_run_result);
331 let exe_file = make_exe_name(config, testfile);
334 let debugger_run_result;
335 match config.target.as_slice() {
336 "arm-linux-androideabi" => {
338 cmds = cmds.replace("run", "continue").to_string();
340 // write debugger script
341 let script_str = ["set charset UTF-8".to_string(),
342 format!("file {}", exe_file.as_str().unwrap()
344 "target remote :5039".to_string(),
346 "quit".to_string()].connect("\n");
347 debug!("script_str = {}", script_str);
348 dump_output_file(config,
350 script_str.as_slice(),
355 config.adb_path.as_slice(),
359 exe_file.as_str().unwrap().to_string(),
360 config.adb_test_dir.clone()
362 vec!(("".to_string(), "".to_string())),
363 Some("".to_string()))
364 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
367 config.adb_path.as_slice(),
370 "forward".to_string(),
371 "tcp:5039".to_string(),
372 "tcp:5039".to_string()
374 vec!(("".to_string(), "".to_string())),
375 Some("".to_string()))
376 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
378 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
379 gdbserver :5039 {}/{}",
380 config.adb_test_dir.clone(),
381 config.adb_test_dir.clone(),
384 .unwrap()).unwrap());
386 let mut process = procsrv::run_background("",
394 vec!(("".to_string(),
396 Some("".to_string()))
397 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
399 //waiting 1 second for gdbserver start
401 let result = task::try(proc() {
402 tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
410 let tool_path = match config.android_cross_path.as_str() {
411 Some(x) => x.to_string(),
412 None => fatal("cannot find android cross path")
415 let debugger_script = make_out_name(config, testfile, "debugger.script");
416 // FIXME (#9639): This needs to handle non-utf8 paths
418 vec!("-quiet".to_string(),
419 "-batch".to_string(),
421 format!("-command={}", debugger_script.as_str().unwrap()));
423 let gdb_path = tool_path.append("/bin/arm-linux-androideabi-gdb");
424 let procsrv::Result {
431 debugger_opts.as_slice(),
432 vec!(("".to_string(), "".to_string())),
434 .expect(format!("failed to exec `{}`", gdb_path).as_slice());
436 let cmdline = make_cmdline("",
437 "arm-linux-androideabi-gdb",
438 debugger_opts.as_slice());
439 logv(config, format!("executing {}", cmdline));
443 debugger_run_result = ProcRes {
449 process.signal_kill().unwrap();
453 // write debugger script
455 "set charset UTF-8".to_string(),
459 debug!("script_str = {}", script_str);
460 dump_output_file(config,
462 script_str.as_slice(),
465 // run debugger script with gdb
467 fn debugger() -> String {
468 "gdb.exe".to_string()
471 fn debugger() -> String {
475 let debugger_script = make_out_name(config, testfile, "debugger.script");
477 // FIXME (#9639): This needs to handle non-utf8 paths
479 vec!("-quiet".to_string(),
480 "-batch".to_string(),
482 format!("-command={}", debugger_script.as_str().unwrap()),
483 exe_file.as_str().unwrap().to_string());
484 proc_args = ProcArgs {
488 debugger_run_result = compose_and_run(config,
492 config.run_lib_path.as_slice(),
498 if !debugger_run_result.status.success() {
499 fatal("gdb failed to execute");
502 check_debugger_output(&debugger_run_result, check_lines.as_slice());
505 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
506 use std::io::process::{Command, ProcessOutput};
508 if config.lldb_python_dir.is_none() {
509 fatal("Can't run LLDB test because LLDB's python path is not set.");
512 let mut config = Config {
513 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
514 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
518 let config = &mut config;
520 // compile test file (it shoud have 'compile-flags:-g' in the header)
521 let compile_result = compile_test(config, props, testfile);
522 if !compile_result.status.success() {
523 fatal_proc_rec("compilation failed!", &compile_result);
526 let exe_file = make_exe_name(config, testfile);
528 // Parse debugger commands etc from test files
529 let DebuggerCommands {
533 } = parse_debugger_commands(testfile, "lldb");
535 // Write debugger script:
536 // We don't want to hang when calling `quit` while the process is still running
537 let mut script_str = String::from_str("settings set auto-confirm true\n");
539 // Make LLDB emit its version, so we have it documented in the test output
540 script_str.push_str("version\n");
542 // Switch LLDB into "Rust mode"
543 script_str.push_str("command script import ./src/etc/lldb_rust_formatters.py\n");
544 script_str.push_str("type summary add --no-value ");
545 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
546 script_str.push_str("-x \".*\" --category Rust\n");
547 script_str.push_str("type category enable Rust\n");
549 // Set breakpoints on every line that contains the string "#break"
550 for line in breakpoint_lines.iter() {
551 script_str.push_str(format!("breakpoint set --line {}\n",
555 // Append the other commands
556 for line in commands.iter() {
557 script_str.push_str(line.as_slice());
558 script_str.push_str("\n");
561 // Finally, quit the debugger
562 script_str.push_str("quit\n");
564 // Write the script into a file
565 debug!("script_str = {}", script_str);
566 dump_output_file(config,
568 script_str.as_slice(),
570 let debugger_script = make_out_name(config, testfile, "debugger.script");
572 // Let LLDB execute the script via lldb_batchmode.py
573 let debugger_run_result = run_lldb(config, &exe_file, &debugger_script);
575 if !debugger_run_result.status.success() {
576 fatal_proc_rec("Error while running LLDB", &debugger_run_result);
579 check_debugger_output(&debugger_run_result, check_lines.as_slice());
581 fn run_lldb(config: &Config, test_executable: &Path, debugger_script: &Path) -> ProcRes {
582 // Prepare the lldb_batchmode which executes the debugger script
583 let mut cmd = Command::new("python");
584 cmd.arg("./src/etc/lldb_batchmode.py")
585 .arg(test_executable)
586 .arg(debugger_script)
587 .env_set_all([("PYTHONPATH", config.lldb_python_dir.clone().unwrap().as_slice())]);
589 let (status, out, err) = match cmd.spawn() {
591 let ProcessOutput { status, output, error } =
592 process.wait_with_output().unwrap();
595 String::from_utf8(output).unwrap(),
596 String::from_utf8(error).unwrap())
599 fatal(format!("Failed to setup Python process for \
600 LLDB script: {}", e).as_slice())
604 dump_output(config, test_executable, out.as_slice(), err.as_slice());
609 cmdline: format!("{}", cmd)
614 struct DebuggerCommands {
615 commands: Vec<String>,
616 check_lines: Vec<String>,
617 breakpoint_lines: Vec<uint>,
620 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
621 -> DebuggerCommands {
622 use std::io::{BufferedReader, File};
624 let command_directive = format!("{}-command", debugger_prefix);
625 let check_directive = format!("{}-check", debugger_prefix);
627 let mut breakpoint_lines = vec!();
628 let mut commands = vec!();
629 let mut check_lines = vec!();
631 let mut reader = BufferedReader::new(File::open(file_path).unwrap());
632 for line in reader.lines() {
635 if line.as_slice().contains("#break") {
636 breakpoint_lines.push(counter);
639 header::parse_name_value_directive(
641 command_directive.as_slice()).map(|cmd| {
645 header::parse_name_value_directive(
647 check_directive.as_slice()).map(|cmd| {
648 check_lines.push(cmd)
652 fatal(format!("Error while parsing debugger commands: {}",
661 check_lines: check_lines,
662 breakpoint_lines: breakpoint_lines
666 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
667 if options.is_none() {
671 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
672 let options_to_remove = [
675 "--debuginfo".to_string()
678 split_maybe_args(options).move_iter()
679 .filter(|x| !options_to_remove.contains(x))
680 .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[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_proc_rec(format!("line not found in debugger output: {}",
736 check_lines.get(i).unwrap()).as_slice(),
737 debugger_run_result);
742 fn check_error_patterns(props: &TestProps,
744 proc_res: &ProcRes) {
745 if props.error_patterns.is_empty() {
746 fatal(format!("no error pattern specified in {}",
747 testfile.display()).as_slice());
750 if proc_res.status.success() {
751 fatal("process did not return an error status");
754 let mut next_err_idx = 0u;
755 let mut next_err_pat = &props.error_patterns[next_err_idx];
756 let mut done = false;
757 let output_to_check = if props.check_stdout {
758 format!("{}{}", proc_res.stdout, proc_res.stderr)
760 proc_res.stderr.clone()
762 for line in output_to_check.as_slice().lines() {
763 if line.contains(next_err_pat.as_slice()) {
764 debug!("found error pattern {}", next_err_pat);
766 if next_err_idx == props.error_patterns.len() {
767 debug!("found all error patterns");
771 next_err_pat = &props.error_patterns[next_err_idx];
776 let missing_patterns =
777 props.error_patterns.slice(next_err_idx, props.error_patterns.len());
778 if missing_patterns.len() == 1u {
779 fatal_proc_rec(format!("error pattern '{}' not found!",
780 missing_patterns[0]).as_slice(),
783 for pattern in missing_patterns.iter() {
784 error(format!("error pattern '{}' not found!",
785 *pattern).as_slice());
787 fatal_proc_rec("multiple error patterns not found", proc_res);
791 fn check_no_compiler_crash(proc_res: &ProcRes) {
792 for line in proc_res.stderr.as_slice().lines() {
793 if line.starts_with("error: internal compiler error:") {
794 fatal_proc_rec("compiler encountered internal error",
800 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
802 proc_res: &ProcRes) {
804 // true if we found the error in question
805 let mut found_flags = Vec::from_elem(
806 expected_errors.len(), false);
808 if proc_res.status.success() {
809 fatal("process did not return an error status");
812 let prefixes = expected_errors.iter().map(|ee| {
813 format!("{}:{}:", testfile.display(), ee.line)
814 }).collect::<Vec<String> >();
816 #[cfg(target_os = "win32")]
817 fn to_lower( s : &str ) -> String {
819 let c : Vec<char> = i.map( |c| {
821 c.to_ascii().to_lowercase().to_char()
826 String::from_chars(c.as_slice())
829 #[cfg(target_os = "win32")]
830 fn prefix_matches( line : &str, prefix : &str ) -> bool {
831 to_lower(line).as_slice().starts_with(to_lower(prefix).as_slice())
834 #[cfg(target_os = "linux")]
835 #[cfg(target_os = "macos")]
836 #[cfg(target_os = "freebsd")]
837 fn prefix_matches( line : &str, prefix : &str ) -> bool {
838 line.starts_with( prefix )
841 // Scan and extract our error/warning messages,
843 // filename:line1:col1: line2:col2: *error:* msg
844 // filename:line1:col1: line2:col2: *warning:* msg
845 // where line1:col1: is the starting point, line2:col2:
846 // is the ending point, and * represents ANSI color codes.
847 for line in proc_res.stderr.as_slice().lines() {
848 let mut was_expected = false;
849 for (i, ee) in expected_errors.iter().enumerate() {
851 debug!("prefix={} ee.kind={} ee.msg={} line={}",
852 prefixes[i].as_slice(),
856 if prefix_matches(line, prefixes[i].as_slice()) &&
857 line.contains(ee.kind.as_slice()) &&
858 line.contains(ee.msg.as_slice()) {
859 *found_flags.get_mut(i) = true;
866 // ignore this msg which gets printed at the end
867 if line.contains("aborting due to") {
871 if !was_expected && is_compiler_error_or_warning(line) {
872 fatal_proc_rec(format!("unexpected compiler error or warning: '{}'",
878 for (i, &flag) in found_flags.iter().enumerate() {
880 let ee = &expected_errors[i];
881 fatal_proc_rec(format!("expected {} on line {} not found: {}",
882 ee.kind, ee.line, ee.msg).as_slice(),
888 fn is_compiler_error_or_warning(line: &str) -> bool {
891 scan_until_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_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_string(line, "error", &mut i) ||
903 scan_string(line, "warning", &mut i));
906 fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
907 if *idx >= haystack.len() {
910 let opt = haystack.slice_from(*idx).find(needle);
918 fn scan_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
919 if *idx >= haystack.len() {
922 let range = haystack.char_range_at(*idx);
923 if range.ch != needle {
930 fn scan_integer(haystack: &str, idx: &mut uint) -> bool {
932 while i < haystack.len() {
933 let range = haystack.char_range_at(i);
934 if range.ch < '0' || '9' < range.ch {
946 fn scan_string(haystack: &str, needle: &str, idx: &mut uint) -> bool {
947 let mut haystack_i = *idx;
948 let mut needle_i = 0u;
949 while needle_i < needle.len() {
950 if haystack_i >= haystack.len() {
953 let range = haystack.char_range_at(haystack_i);
954 haystack_i = range.next;
955 if !scan_char(needle, range.ch, &mut needle_i) {
975 fn compile_test(config: &Config, props: &TestProps,
976 testfile: &Path) -> ProcRes {
977 compile_test_(config, props, testfile, [])
980 fn jit_test(config: &Config, props: &TestProps, testfile: &Path) -> ProcRes {
981 compile_test_(config, props, testfile, ["--jit".to_string()])
984 fn compile_test_(config: &Config, props: &TestProps,
985 testfile: &Path, extra_args: &[String]) -> ProcRes {
986 let aux_dir = aux_output_dir_name(config, testfile);
987 // FIXME (#9639): This needs to handle non-utf8 paths
988 let link_args = vec!("-L".to_string(),
989 aux_dir.as_str().unwrap().to_string());
990 let args = make_compile_args(config,
992 link_args.append(extra_args),
993 |a, b| ThisFile(make_exe_name(a, b)), testfile);
994 compose_and_run_compiler(config, props, testfile, args, None)
997 fn exec_compiled_test(config: &Config, props: &TestProps,
998 testfile: &Path) -> ProcRes {
1000 let env = props.exec_env.clone();
1002 match config.target.as_slice() {
1004 "arm-linux-androideabi" => {
1005 _arm_exec_compiled_test(config, props, testfile, env)
1009 let aux_dir = aux_output_dir_name(config, testfile);
1010 compose_and_run(config,
1012 make_run_args(config, props, testfile),
1014 config.run_lib_path.as_slice(),
1015 Some(aux_dir.as_str().unwrap()),
1021 fn compose_and_run_compiler(
1026 input: Option<String>) -> ProcRes {
1028 if !props.aux_builds.is_empty() {
1029 ensure_dir(&aux_output_dir_name(config, testfile));
1032 let aux_dir = aux_output_dir_name(config, testfile);
1033 // FIXME (#9639): This needs to handle non-utf8 paths
1034 let extra_link_args = vec!("-L".to_string(), aux_dir.as_str().unwrap().to_string());
1036 for rel_ab in props.aux_builds.iter() {
1037 let abs_ab = config.aux_base.join(rel_ab.as_slice());
1038 let aux_props = header::load_props(&abs_ab);
1039 let crate_type = if aux_props.no_prefer_dynamic {
1042 vec!("--crate-type=dylib".to_string())
1045 make_compile_args(config,
1048 extra_link_args.as_slice()),
1050 let f = make_lib_name(a, b, testfile);
1051 ThisDirectory(f.dir_path())
1054 let auxres = compose_and_run(config,
1058 config.compile_lib_path.as_slice(),
1059 Some(aux_dir.as_str().unwrap()),
1061 if !auxres.status.success() {
1063 format!("auxiliary build of {} failed to compile: ",
1064 abs_ab.display()).as_slice(),
1068 match config.target.as_slice() {
1069 "arm-linux-androideabi" => {
1070 _arm_push_aux_shared_library(config, testfile);
1076 compose_and_run(config,
1080 config.compile_lib_path.as_slice(),
1081 Some(aux_dir.as_str().unwrap()),
1085 fn ensure_dir(path: &Path) {
1086 if path.is_dir() { return; }
1087 fs::mkdir(path, io::UserRWX).unwrap();
1090 fn compose_and_run(config: &Config, testfile: &Path,
1091 ProcArgs{ args, prog }: ProcArgs,
1092 procenv: Vec<(String, String)> ,
1094 aux_path: Option<&str>,
1095 input: Option<String>) -> ProcRes {
1096 return program_output(config, testfile, lib_path,
1097 prog, aux_path, args, procenv, input);
1100 enum TargetLocation {
1102 ThisDirectory(Path),
1105 fn make_compile_args(config: &Config,
1107 extras: Vec<String> ,
1108 xform: |&Config, &Path| -> TargetLocation,
1111 let xform_file = xform(config, testfile);
1112 let target = if props.force_host {
1113 config.host.as_slice()
1115 config.target.as_slice()
1117 // FIXME (#9639): This needs to handle non-utf8 paths
1118 let mut args = vec!(testfile.as_str().unwrap().to_string(),
1120 config.build_base.as_str().unwrap().to_string(),
1121 format!("--target={}", target));
1122 args.push_all(extras.as_slice());
1123 if !props.no_prefer_dynamic {
1124 args.push("-C".to_string());
1125 args.push("prefer-dynamic".to_string());
1127 let path = match xform_file {
1129 args.push("-o".to_string());
1132 ThisDirectory(path) => {
1133 args.push("--out-dir".to_string());
1137 args.push(path.as_str().unwrap().to_string());
1138 if props.force_host {
1139 args.push_all_move(split_maybe_args(&config.host_rustcflags));
1141 args.push_all_move(split_maybe_args(&config.target_rustcflags));
1143 args.push_all_move(split_maybe_args(&props.compile_flags));
1145 prog: config.rustc_path.as_str().unwrap().to_string(),
1150 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> Path {
1151 // what we return here is not particularly important, as it
1152 // happens; rustc ignores everything except for the directory.
1153 let auxname = output_testname(auxfile);
1154 aux_output_dir_name(config, testfile).join(&auxname)
1157 fn make_exe_name(config: &Config, testfile: &Path) -> Path {
1158 let mut f = output_base_name(config, testfile);
1159 if !os::consts::EXE_SUFFIX.is_empty() {
1160 match f.filename().map(|s| Vec::from_slice(s).append(os::consts::EXE_SUFFIX.as_bytes())) {
1161 Some(v) => f.set_filename(v),
1168 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path) ->
1170 // If we've got another tool to run under (valgrind),
1171 // then split apart its command
1172 let mut args = split_maybe_args(&config.runtool);
1173 let exe_file = make_exe_name(config, testfile);
1175 // FIXME (#9639): This needs to handle non-utf8 paths
1176 args.push(exe_file.as_str().unwrap().to_string());
1178 // Add the arguments in the run_flags directive
1179 args.push_all_move(split_maybe_args(&props.run_flags));
1181 let prog = args.shift().unwrap();
1188 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1194 if s.is_whitespace() {
1205 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1206 aux_path: Option<&str>, args: Vec<String>,
1207 env: Vec<(String, String)>,
1208 input: Option<String>) -> ProcRes {
1211 let cmdline = make_cmdline(lib_path,
1214 logv(config, format!("executing {}", cmdline));
1217 let procsrv::Result {
1221 } = procsrv::run(lib_path,
1226 input).expect(format!("failed to exec `{}`", prog).as_slice());
1227 dump_output(config, testfile, out.as_slice(), err.as_slice());
1236 // Linux and mac don't require adjusting the library search path
1237 #[cfg(target_os = "linux")]
1238 #[cfg(target_os = "macos")]
1239 #[cfg(target_os = "freebsd")]
1240 fn make_cmdline(_libpath: &str, prog: &str, args: &[String]) -> String {
1241 format!("{} {}", prog, args.connect(" "))
1244 #[cfg(target_os = "win32")]
1245 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1246 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.connect(" "))
1249 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1250 // for diagnostic purposes
1251 #[cfg(target_os = "win32")]
1252 fn lib_path_cmd_prefix(path: &str) -> String {
1253 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1256 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1257 dump_output_file(config, testfile, out, "out");
1258 dump_output_file(config, testfile, err, "err");
1259 maybe_dump_to_stdout(config, out, err);
1262 fn dump_output_file(config: &Config, testfile: &Path,
1263 out: &str, extension: &str) {
1264 let outfile = make_out_name(config, testfile, extension);
1265 File::create(&outfile).write(out.as_bytes()).unwrap();
1268 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> Path {
1269 output_base_name(config, testfile).with_extension(extension)
1272 fn aux_output_dir_name(config: &Config, testfile: &Path) -> Path {
1273 let mut f = output_base_name(config, testfile);
1274 match f.filename().map(|s| Vec::from_slice(s).append(b".libaux")) {
1275 Some(v) => f.set_filename(v),
1281 fn output_testname(testfile: &Path) -> Path {
1282 Path::new(testfile.filestem().unwrap())
1285 fn output_base_name(config: &Config, testfile: &Path) -> Path {
1287 .join(&output_testname(testfile))
1288 .with_extension(config.stage_id.as_slice())
1291 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1293 println!("------{}------------------------------", "stdout");
1294 println!("{}", out);
1295 println!("------{}------------------------------", "stderr");
1296 println!("{}", err);
1297 println!("------------------------------------------");
1301 fn error(err: &str) { println!("\nerror: {}", err); }
1303 fn fatal(err: &str) -> ! { error(err); fail!(); }
1305 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1311 ------------------------------------------\n\
1313 ------------------------------------------\n\
1315 ------------------------------------------\n\
1317 ------------------------------------------\n\
1319 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1324 fn _arm_exec_compiled_test(config: &Config,
1327 env: Vec<(String, String)>)
1329 let args = make_run_args(config, props, testfile);
1330 let cmdline = make_cmdline("",
1331 args.prog.as_slice(),
1332 args.args.as_slice());
1334 // get bare program string
1335 let mut tvec: Vec<String> = args.prog
1338 .map(|ts| ts.to_string())
1340 let prog_short = tvec.pop().unwrap();
1343 let copy_result = procsrv::run("",
1344 config.adb_path.as_slice(),
1349 config.adb_test_dir.clone()
1351 vec!(("".to_string(), "".to_string())),
1352 Some("".to_string()))
1353 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1356 println!("push ({}) {} {} {}",
1363 logv(config, format!("executing ({}) {}", config.target, cmdline));
1365 let mut runargs = Vec::new();
1367 // run test via adb_run_wrapper
1368 runargs.push("shell".to_string());
1369 for (key, val) in env.move_iter() {
1370 runargs.push(format!("{}={}", key, val));
1372 runargs.push(format!("{}/adb_run_wrapper.sh", config.adb_test_dir));
1373 runargs.push(format!("{}", config.adb_test_dir));
1374 runargs.push(format!("{}", prog_short));
1376 for tv in args.args.iter() {
1377 runargs.push(tv.to_string());
1380 config.adb_path.as_slice(),
1383 vec!(("".to_string(), "".to_string())), Some("".to_string()))
1384 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1386 // get exitcode of result
1387 runargs = Vec::new();
1388 runargs.push("shell".to_string());
1389 runargs.push("cat".to_string());
1390 runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1392 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1394 config.adb_path.as_slice(),
1397 vec!(("".to_string(), "".to_string())),
1398 Some("".to_string()))
1399 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1401 let mut exitcode: int = 0;
1402 for c in exitcode_out.as_slice().chars() {
1403 if !c.is_digit() { break; }
1404 exitcode = exitcode * 10 + match c {
1405 '0' .. '9' => c as int - ('0' as int),
1410 // get stdout of result
1411 runargs = Vec::new();
1412 runargs.push("shell".to_string());
1413 runargs.push("cat".to_string());
1414 runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1416 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1418 config.adb_path.as_slice(),
1421 vec!(("".to_string(), "".to_string())),
1422 Some("".to_string()))
1423 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1425 // get stderr of result
1426 runargs = Vec::new();
1427 runargs.push("shell".to_string());
1428 runargs.push("cat".to_string());
1429 runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1431 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1433 config.adb_path.as_slice(),
1436 vec!(("".to_string(), "".to_string())),
1437 Some("".to_string()))
1438 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1442 stdout_out.as_slice(),
1443 stderr_out.as_slice());
1446 status: process::ExitStatus(exitcode),
1453 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1454 let tdir = aux_output_dir_name(config, testfile);
1456 let dirs = fs::readdir(&tdir).unwrap();
1457 for file in dirs.iter() {
1458 if file.extension_str() == Some("so") {
1459 // FIXME (#9639): This needs to handle non-utf8 paths
1460 let copy_result = procsrv::run("",
1461 config.adb_path.as_slice(),
1468 config.adb_test_dir.to_string()
1470 vec!(("".to_string(),
1472 Some("".to_string()))
1473 .expect(format!("failed to exec `{}`", config.adb_path).as_slice());
1476 println!("push ({}) {} {} {}",
1477 config.target, file.display(),
1478 copy_result.out, copy_result.err);
1484 // codegen tests (vs. clang)
1486 fn make_o_name(config: &Config, testfile: &Path) -> Path {
1487 output_base_name(config, testfile).with_extension("o")
1490 fn append_suffix_to_stem(p: &Path, suffix: &str) -> Path {
1491 if suffix.len() == 0 {
1494 let stem = p.filestem().unwrap();
1495 p.with_filename(Vec::from_slice(stem).append(b"-").append(suffix.as_bytes()))
1499 fn compile_test_and_save_bitcode(config: &Config, props: &TestProps,
1500 testfile: &Path) -> ProcRes {
1501 let aux_dir = aux_output_dir_name(config, testfile);
1502 // FIXME (#9639): This needs to handle non-utf8 paths
1503 let link_args = vec!("-L".to_string(),
1504 aux_dir.as_str().unwrap().to_string());
1505 let llvm_args = vec!("--emit=obj".to_string(),
1506 "--crate-type=lib".to_string(),
1508 "save-temps".to_string());
1509 let args = make_compile_args(config,
1511 link_args.append(llvm_args.as_slice()),
1512 |a, b| ThisFile(make_o_name(a, b)), testfile);
1513 compose_and_run_compiler(config, props, testfile, args, None)
1516 fn compile_cc_with_clang_and_save_bitcode(config: &Config, _props: &TestProps,
1517 testfile: &Path) -> ProcRes {
1518 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1519 let bitcodefile = append_suffix_to_stem(&bitcodefile, "clang");
1520 let testcc = testfile.with_extension("cc");
1521 let proc_args = ProcArgs {
1522 // FIXME (#9639): This needs to handle non-utf8 paths
1523 prog: config.clang_path.get_ref().as_str().unwrap().to_string(),
1524 args: vec!("-c".to_string(),
1525 "-emit-llvm".to_string(),
1527 bitcodefile.as_str().unwrap().to_string(),
1528 testcc.as_str().unwrap().to_string())
1530 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1533 fn extract_function_from_bitcode(config: &Config, _props: &TestProps,
1534 fname: &str, testfile: &Path,
1535 suffix: &str) -> ProcRes {
1536 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1537 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1538 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1539 let prog = config.llvm_bin_path.get_ref().join("llvm-extract");
1540 let proc_args = ProcArgs {
1541 // FIXME (#9639): This needs to handle non-utf8 paths
1542 prog: prog.as_str().unwrap().to_string(),
1543 args: vec!(format!("-func={}", fname),
1544 format!("-o={}", extracted_bc.as_str().unwrap()),
1545 bitcodefile.as_str().unwrap().to_string())
1547 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1550 fn disassemble_extract(config: &Config, _props: &TestProps,
1551 testfile: &Path, suffix: &str) -> ProcRes {
1552 let bitcodefile = output_base_name(config, testfile).with_extension("bc");
1553 let bitcodefile = append_suffix_to_stem(&bitcodefile, suffix);
1554 let extracted_bc = append_suffix_to_stem(&bitcodefile, "extract");
1555 let extracted_ll = extracted_bc.with_extension("ll");
1556 let prog = config.llvm_bin_path.get_ref().join("llvm-dis");
1557 let proc_args = ProcArgs {
1558 // FIXME (#9639): This needs to handle non-utf8 paths
1559 prog: prog.as_str().unwrap().to_string(),
1560 args: vec!(format!("-o={}", extracted_ll.as_str().unwrap()),
1561 extracted_bc.as_str().unwrap().to_string())
1563 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1567 fn count_extracted_lines(p: &Path) -> uint {
1568 let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
1569 let x = str::from_utf8(x.as_slice()).unwrap();
1574 fn run_codegen_test(config: &Config, props: &TestProps,
1575 testfile: &Path, mm: &mut MetricMap) {
1577 if config.llvm_bin_path.is_none() {
1578 fatal("missing --llvm-bin-path");
1581 if config.clang_path.is_none() {
1582 fatal("missing --clang-path");
1585 let mut proc_res = compile_test_and_save_bitcode(config, props, testfile);
1586 if !proc_res.status.success() {
1587 fatal_proc_rec("compilation failed!", &proc_res);
1590 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "");
1591 if !proc_res.status.success() {
1592 fatal_proc_rec("extracting 'test' function failed",
1596 proc_res = disassemble_extract(config, props, testfile, "");
1597 if !proc_res.status.success() {
1598 fatal_proc_rec("disassembling extract failed", &proc_res);
1602 let mut proc_res = compile_cc_with_clang_and_save_bitcode(config, props, testfile);
1603 if !proc_res.status.success() {
1604 fatal_proc_rec("compilation failed!", &proc_res);
1607 proc_res = extract_function_from_bitcode(config, props, "test", testfile, "clang");
1608 if !proc_res.status.success() {
1609 fatal_proc_rec("extracting 'test' function failed",
1613 proc_res = disassemble_extract(config, props, testfile, "clang");
1614 if !proc_res.status.success() {
1615 fatal_proc_rec("disassembling extract failed", &proc_res);
1618 let base = output_base_name(config, testfile);
1619 let base_extract = append_suffix_to_stem(&base, "extract");
1621 let base_clang = append_suffix_to_stem(&base, "clang");
1622 let base_clang_extract = append_suffix_to_stem(&base_clang, "extract");
1624 let base_lines = count_extracted_lines(&base_extract);
1625 let clang_lines = count_extracted_lines(&base_clang_extract);
1627 mm.insert_metric("clang-codegen-ratio",
1628 (base_lines as f64) / (clang_lines as f64),