]> git.proxmox.com Git - rustc.git/blob - src/compiletest/runtest.rs
5a076b8437e2694695d9f2528a4c4c716deb1f33
[rustc.git] / src / compiletest / runtest.rs
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.
4 //
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.
10
11 use self::TargetLocation::*;
12
13 use common::Config;
14 use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
15 use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc};
16 use errors;
17 use header::TestProps;
18 use header;
19 use procsrv;
20 use util::logv;
21
22 use std::env;
23 use std::fmt;
24 use std::fs::{self, File};
25 use std::io::BufReader;
26 use std::io::prelude::*;
27 use std::net::TcpStream;
28 use std::path::{Path, PathBuf};
29 use std::process::{Command, Output, ExitStatus};
30
31 pub fn run(config: Config, testfile: &Path) {
32 match &*config.target {
33
34 "arm-linux-androideabi" | "aarch64-linux-android" => {
35 if !config.adb_device_status {
36 panic!("android device not available");
37 }
38 }
39
40 _=> { }
41 }
42
43 if config.verbose {
44 // We're going to be dumping a lot of info. Start on a new line.
45 print!("\n\n");
46 }
47 debug!("running {:?}", testfile.display());
48 let props = header::load_props(&testfile);
49 debug!("loaded props");
50 match config.mode {
51 CompileFail => run_cfail_test(&config, &props, &testfile),
52 ParseFail => run_cfail_test(&config, &props, &testfile),
53 RunFail => run_rfail_test(&config, &props, &testfile),
54 RunPass => run_rpass_test(&config, &props, &testfile),
55 RunPassValgrind => run_valgrind_test(&config, &props, &testfile),
56 Pretty => run_pretty_test(&config, &props, &testfile),
57 DebugInfoGdb => run_debuginfo_gdb_test(&config, &props, &testfile),
58 DebugInfoLldb => run_debuginfo_lldb_test(&config, &props, &testfile),
59 Codegen => run_codegen_test(&config, &props, &testfile),
60 Rustdoc => run_rustdoc_test(&config, &props, &testfile),
61 }
62 }
63
64 fn get_output(props: &TestProps, proc_res: &ProcRes) -> String {
65 if props.check_stdout {
66 format!("{}{}", proc_res.stdout, proc_res.stderr)
67 } else {
68 proc_res.stderr.clone()
69 }
70 }
71
72 fn run_cfail_test(config: &Config, props: &TestProps, testfile: &Path) {
73 let proc_res = compile_test(config, props, testfile);
74
75 if proc_res.status.success() {
76 fatal_proc_rec(&format!("{} test compiled successfully!", config.mode)[..],
77 &proc_res);
78 }
79
80 check_correct_failure_status(&proc_res);
81
82 if proc_res.status.success() {
83 fatal("process did not return an error status");
84 }
85
86 let output_to_check = get_output(props, &proc_res);
87 let expected_errors = errors::load_errors(testfile);
88 if !expected_errors.is_empty() {
89 if !props.error_patterns.is_empty() {
90 fatal("both error pattern and expected errors specified");
91 }
92 check_expected_errors(expected_errors, testfile, &proc_res);
93 } else {
94 check_error_patterns(props, testfile, &output_to_check, &proc_res);
95 }
96 check_no_compiler_crash(&proc_res);
97 check_forbid_output(props, &output_to_check, &proc_res);
98 }
99
100 fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
101 let proc_res = compile_test(config, props, testfile);
102
103 if !proc_res.status.success() {
104 fatal_proc_rec("compilation failed!", &proc_res);
105 }
106
107 let proc_res = exec_compiled_test(config, props, testfile);
108
109 // The value our Makefile configures valgrind to return on failure
110 const VALGRIND_ERR: i32 = 100;
111 if proc_res.status.code() == Some(VALGRIND_ERR) {
112 fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
113 }
114
115 let output_to_check = get_output(props, &proc_res);
116 check_correct_failure_status(&proc_res);
117 check_error_patterns(props, testfile, &output_to_check, &proc_res);
118 }
119
120 fn check_correct_failure_status(proc_res: &ProcRes) {
121 // The value the rust runtime returns on failure
122 const RUST_ERR: i32 = 101;
123 if proc_res.status.code() != Some(RUST_ERR) {
124 fatal_proc_rec(
125 &format!("failure produced the wrong error: {}",
126 proc_res.status),
127 proc_res);
128 }
129 }
130
131 fn run_rpass_test(config: &Config, props: &TestProps, testfile: &Path) {
132 let proc_res = compile_test(config, props, testfile);
133
134 if !proc_res.status.success() {
135 fatal_proc_rec("compilation failed!", &proc_res);
136 }
137
138 let proc_res = exec_compiled_test(config, props, testfile);
139
140 if !proc_res.status.success() {
141 fatal_proc_rec("test run failed!", &proc_res);
142 }
143 }
144
145 fn run_valgrind_test(config: &Config, props: &TestProps, testfile: &Path) {
146 if config.valgrind_path.is_none() {
147 assert!(!config.force_valgrind);
148 return run_rpass_test(config, props, testfile);
149 }
150
151 let mut proc_res = compile_test(config, props, testfile);
152
153 if !proc_res.status.success() {
154 fatal_proc_rec("compilation failed!", &proc_res);
155 }
156
157 let mut new_config = config.clone();
158 new_config.runtool = new_config.valgrind_path.clone();
159 proc_res = exec_compiled_test(&new_config, props, testfile);
160
161 if !proc_res.status.success() {
162 fatal_proc_rec("test run failed!", &proc_res);
163 }
164 }
165
166 fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) {
167 if props.pp_exact.is_some() {
168 logv(config, "testing for exact pretty-printing".to_string());
169 } else {
170 logv(config, "testing for converging pretty-printing".to_string());
171 }
172
173 let rounds =
174 match props.pp_exact { Some(_) => 1, None => 2 };
175
176 let mut src = String::new();
177 File::open(testfile).unwrap().read_to_string(&mut src).unwrap();
178 let mut srcs = vec!(src);
179
180 let mut round = 0;
181 while round < rounds {
182 logv(config, format!("pretty-printing round {}", round));
183 let proc_res = print_source(config,
184 props,
185 testfile,
186 srcs[round].to_string(),
187 &props.pretty_mode);
188
189 if !proc_res.status.success() {
190 fatal_proc_rec(&format!("pretty-printing failed in round {}", round),
191 &proc_res);
192 }
193
194 let ProcRes{ stdout, .. } = proc_res;
195 srcs.push(stdout);
196 round += 1;
197 }
198
199 let mut expected = match props.pp_exact {
200 Some(ref file) => {
201 let filepath = testfile.parent().unwrap().join(file);
202 let mut s = String::new();
203 File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
204 s
205 }
206 None => { srcs[srcs.len() - 2].clone() }
207 };
208 let mut actual = srcs[srcs.len() - 1].clone();
209
210 if props.pp_exact.is_some() {
211 // Now we have to care about line endings
212 let cr = "\r".to_string();
213 actual = actual.replace(&cr, "").to_string();
214 expected = expected.replace(&cr, "").to_string();
215 }
216
217 compare_source(&expected, &actual);
218
219 // If we're only making sure that the output matches then just stop here
220 if props.pretty_compare_only { return; }
221
222 // Finally, let's make sure it actually appears to remain valid code
223 let proc_res = typecheck_source(config, props, testfile, actual);
224
225 if !proc_res.status.success() {
226 fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
227 }
228 if !props.pretty_expanded { return }
229
230 // additionally, run `--pretty expanded` and try to build it.
231 let proc_res = print_source(config, props, testfile, srcs[round].clone(), "expanded");
232 if !proc_res.status.success() {
233 fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
234 }
235
236 let ProcRes{ stdout: expanded_src, .. } = proc_res;
237 let proc_res = typecheck_source(config, props, testfile, expanded_src);
238 if !proc_res.status.success() {
239 fatal_proc_rec("pretty-printed source (expanded) does not typecheck",
240 &proc_res);
241 }
242
243 return;
244
245 fn print_source(config: &Config,
246 props: &TestProps,
247 testfile: &Path,
248 src: String,
249 pretty_type: &str) -> ProcRes {
250 let aux_dir = aux_output_dir_name(config, testfile);
251 compose_and_run(config,
252 testfile,
253 make_pp_args(config,
254 props,
255 testfile,
256 pretty_type.to_string()),
257 props.exec_env.clone(),
258 &config.compile_lib_path,
259 Some(aux_dir.to_str().unwrap()),
260 Some(src))
261 }
262
263 fn make_pp_args(config: &Config,
264 props: &TestProps,
265 testfile: &Path,
266 pretty_type: String) -> ProcArgs {
267 let aux_dir = aux_output_dir_name(config, testfile);
268 // FIXME (#9639): This needs to handle non-utf8 paths
269 let mut args = vec!("-".to_string(),
270 "-Zunstable-options".to_string(),
271 "--pretty".to_string(),
272 pretty_type,
273 format!("--target={}", config.target),
274 "-L".to_string(),
275 aux_dir.to_str().unwrap().to_string());
276 args.extend(split_maybe_args(&config.target_rustcflags));
277 args.extend(split_maybe_args(&props.compile_flags));
278 return ProcArgs {
279 prog: config.rustc_path.to_str().unwrap().to_string(),
280 args: args,
281 };
282 }
283
284 fn compare_source(expected: &str, actual: &str) {
285 if expected != actual {
286 error("pretty-printed source does not match expected source");
287 println!("\n\
288 expected:\n\
289 ------------------------------------------\n\
290 {}\n\
291 ------------------------------------------\n\
292 actual:\n\
293 ------------------------------------------\n\
294 {}\n\
295 ------------------------------------------\n\
296 \n",
297 expected, actual);
298 panic!();
299 }
300 }
301
302 fn typecheck_source(config: &Config, props: &TestProps,
303 testfile: &Path, src: String) -> ProcRes {
304 let args = make_typecheck_args(config, props, testfile);
305 compose_and_run_compiler(config, props, testfile, args, Some(src))
306 }
307
308 fn make_typecheck_args(config: &Config, props: &TestProps, testfile: &Path) -> ProcArgs {
309 let aux_dir = aux_output_dir_name(config, testfile);
310 let target = if props.force_host {
311 &*config.host
312 } else {
313 &*config.target
314 };
315 // FIXME (#9639): This needs to handle non-utf8 paths
316 let mut args = vec!("-".to_string(),
317 "-Zno-trans".to_string(),
318 "--crate-type=lib".to_string(),
319 format!("--target={}", target),
320 "-L".to_string(),
321 config.build_base.to_str().unwrap().to_string(),
322 "-L".to_string(),
323 aux_dir.to_str().unwrap().to_string());
324 args.extend(split_maybe_args(&config.target_rustcflags));
325 args.extend(split_maybe_args(&props.compile_flags));
326 // FIXME (#9639): This needs to handle non-utf8 paths
327 return ProcArgs {
328 prog: config.rustc_path.to_str().unwrap().to_string(),
329 args: args,
330 };
331 }
332 }
333
334 fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
335 let mut config = Config {
336 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
337 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
338 .. config.clone()
339 };
340
341 let config = &mut config;
342 let DebuggerCommands {
343 commands,
344 check_lines,
345 breakpoint_lines
346 } = parse_debugger_commands(testfile, "gdb");
347 let mut cmds = commands.connect("\n");
348
349 // compile test file (it should have 'compile-flags:-g' in the header)
350 let compiler_run_result = compile_test(config, props, testfile);
351 if !compiler_run_result.status.success() {
352 fatal_proc_rec("compilation failed!", &compiler_run_result);
353 }
354
355 let exe_file = make_exe_name(config, testfile);
356
357 let debugger_run_result;
358 match &*config.target {
359 "arm-linux-androideabi" | "aarch64-linux-android" => {
360
361 cmds = cmds.replace("run", "continue");
362
363 // write debugger script
364 let mut script_str = String::with_capacity(2048);
365 script_str.push_str(&format!("set charset {}\n", charset()));
366 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
367 script_str.push_str("target remote :5039\n");
368 script_str.push_str(&format!("set solib-search-path \
369 ./{}/stage2/lib/rustlib/{}/lib/\n",
370 config.host, config.target));
371 for line in &breakpoint_lines {
372 script_str.push_str(&format!("break {:?}:{}\n",
373 testfile.file_name().unwrap()
374 .to_string_lossy(),
375 *line)[..]);
376 }
377 script_str.push_str(&cmds);
378 script_str.push_str("\nquit\n");
379
380 debug!("script_str = {}", script_str);
381 dump_output_file(config,
382 testfile,
383 &script_str,
384 "debugger.script");
385
386
387 procsrv::run("",
388 &config.adb_path,
389 None,
390 &[
391 "push".to_string(),
392 exe_file.to_str().unwrap().to_string(),
393 config.adb_test_dir.clone()
394 ],
395 vec!(("".to_string(), "".to_string())),
396 Some("".to_string()))
397 .expect(&format!("failed to exec `{:?}`", config.adb_path));
398
399 procsrv::run("",
400 &config.adb_path,
401 None,
402 &[
403 "forward".to_string(),
404 "tcp:5039".to_string(),
405 "tcp:5039".to_string()
406 ],
407 vec!(("".to_string(), "".to_string())),
408 Some("".to_string()))
409 .expect(&format!("failed to exec `{:?}`", config.adb_path));
410
411 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
412 gdbserver{} :5039 {}/{}",
413 config.adb_test_dir.clone(),
414 if config.target.contains("aarch64")
415 {"64"} else {""},
416 config.adb_test_dir.clone(),
417 exe_file.file_name().unwrap().to_str()
418 .unwrap());
419
420 let mut process = procsrv::run_background("",
421 &config.adb_path
422 ,
423 None,
424 &[
425 "shell".to_string(),
426 adb_arg.clone()
427 ],
428 vec!(("".to_string(),
429 "".to_string())),
430 Some("".to_string()))
431 .expect(&format!("failed to exec `{:?}`", config.adb_path));
432 loop {
433 //waiting 1 second for gdbserver start
434 ::std::thread::sleep_ms(1000);
435 if TcpStream::connect("127.0.0.1:5039").is_ok() {
436 break
437 }
438 }
439
440 let tool_path = match config.android_cross_path.to_str() {
441 Some(x) => x.to_string(),
442 None => fatal("cannot find android cross path")
443 };
444
445 let debugger_script = make_out_name(config, testfile, "debugger.script");
446 // FIXME (#9639): This needs to handle non-utf8 paths
447 let debugger_opts =
448 vec!("-quiet".to_string(),
449 "-batch".to_string(),
450 "-nx".to_string(),
451 format!("-command={}", debugger_script.to_str().unwrap()));
452
453 let mut gdb_path = tool_path;
454 gdb_path.push_str(&format!("/bin/{}-gdb", config.target));
455 let procsrv::Result {
456 out,
457 err,
458 status
459 } = procsrv::run("",
460 &gdb_path,
461 None,
462 &debugger_opts,
463 vec!(("".to_string(), "".to_string())),
464 None)
465 .expect(&format!("failed to exec `{:?}`", gdb_path));
466 let cmdline = {
467 let cmdline = make_cmdline("",
468 &format!("{}-gdb", config.target),
469 &debugger_opts);
470 logv(config, format!("executing {}", cmdline));
471 cmdline
472 };
473
474 debugger_run_result = ProcRes {
475 status: Status::Normal(status),
476 stdout: out,
477 stderr: err,
478 cmdline: cmdline
479 };
480 if process.kill().is_err() {
481 println!("Adb process is already finished.");
482 }
483 }
484
485 _=> {
486 let rust_src_root = find_rust_src_root(config)
487 .expect("Could not find Rust source root");
488 let rust_pp_module_rel_path = Path::new("./src/etc");
489 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
490 .to_str()
491 .unwrap()
492 .to_string();
493 // write debugger script
494 let mut script_str = String::with_capacity(2048);
495 script_str.push_str(&format!("set charset {}\n", charset()));
496 script_str.push_str("show version\n");
497
498 match config.gdb_version {
499 Some(ref version) => {
500 println!("NOTE: compiletest thinks it is using GDB version {}",
501 version);
502
503 if header::gdb_version_to_int(version) >
504 header::gdb_version_to_int("7.4") {
505 // Add the directory containing the pretty printers to
506 // GDB's script auto loading safe path
507 script_str.push_str(
508 &format!("add-auto-load-safe-path {}\n",
509 rust_pp_module_abs_path.replace(r"\", r"\\"))
510 );
511 }
512 }
513 _ => {
514 println!("NOTE: compiletest does not know which version of \
515 GDB it is using");
516 }
517 }
518
519 // The following line actually doesn't have to do anything with
520 // pretty printing, it just tells GDB to print values on one line:
521 script_str.push_str("set print pretty off\n");
522
523 // Add the pretty printer directory to GDB's source-file search path
524 script_str.push_str(&format!("directory {}\n",
525 rust_pp_module_abs_path));
526
527 // Load the target executable
528 script_str.push_str(&format!("file {}\n",
529 exe_file.to_str().unwrap()
530 .replace(r"\", r"\\")));
531
532 // Add line breakpoints
533 for line in &breakpoint_lines {
534 script_str.push_str(&format!("break '{}':{}\n",
535 testfile.file_name().unwrap()
536 .to_string_lossy(),
537 *line));
538 }
539
540 script_str.push_str(&cmds);
541 script_str.push_str("\nquit\n");
542
543 debug!("script_str = {}", script_str);
544 dump_output_file(config,
545 testfile,
546 &script_str,
547 "debugger.script");
548
549 // run debugger script with gdb
550 fn debugger() -> &'static str {
551 if cfg!(windows) {"gdb.exe"} else {"gdb"}
552 }
553
554 let debugger_script = make_out_name(config, testfile, "debugger.script");
555
556 // FIXME (#9639): This needs to handle non-utf8 paths
557 let debugger_opts =
558 vec!("-quiet".to_string(),
559 "-batch".to_string(),
560 "-nx".to_string(),
561 format!("-command={}", debugger_script.to_str().unwrap()));
562
563 let proc_args = ProcArgs {
564 prog: debugger().to_string(),
565 args: debugger_opts,
566 };
567
568 let environment = vec![("PYTHONPATH".to_string(), rust_pp_module_abs_path)];
569
570 debugger_run_result = compose_and_run(config,
571 testfile,
572 proc_args,
573 environment,
574 &config.run_lib_path,
575 None,
576 None);
577 }
578 }
579
580 if !debugger_run_result.status.success() {
581 fatal("gdb failed to execute");
582 }
583
584 check_debugger_output(&debugger_run_result, &check_lines);
585 }
586
587 fn find_rust_src_root(config: &Config) -> Option<PathBuf> {
588 let mut path = config.src_base.clone();
589 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
590
591 while path.pop() {
592 if path.join(&path_postfix).is_file() {
593 return Some(path);
594 }
595 }
596
597 return None;
598 }
599
600 fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) {
601 if config.lldb_python_dir.is_none() {
602 fatal("Can't run LLDB test because LLDB's python path is not set.");
603 }
604
605 let mut config = Config {
606 target_rustcflags: cleanup_debug_info_options(&config.target_rustcflags),
607 host_rustcflags: cleanup_debug_info_options(&config.host_rustcflags),
608 .. config.clone()
609 };
610
611 let config = &mut config;
612
613 // compile test file (it should have 'compile-flags:-g' in the header)
614 let compile_result = compile_test(config, props, testfile);
615 if !compile_result.status.success() {
616 fatal_proc_rec("compilation failed!", &compile_result);
617 }
618
619 let exe_file = make_exe_name(config, testfile);
620
621 match config.lldb_version {
622 Some(ref version) => {
623 println!("NOTE: compiletest thinks it is using LLDB version {}",
624 version);
625 }
626 _ => {
627 println!("NOTE: compiletest does not know which version of \
628 LLDB it is using");
629 }
630 }
631
632 // Parse debugger commands etc from test files
633 let DebuggerCommands {
634 commands,
635 check_lines,
636 breakpoint_lines,
637 ..
638 } = parse_debugger_commands(testfile, "lldb");
639
640 // Write debugger script:
641 // We don't want to hang when calling `quit` while the process is still running
642 let mut script_str = String::from("settings set auto-confirm true\n");
643
644 // Make LLDB emit its version, so we have it documented in the test output
645 script_str.push_str("version\n");
646
647 // Switch LLDB into "Rust mode"
648 let rust_src_root = find_rust_src_root(config)
649 .expect("Could not find Rust source root");
650 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
651 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
652 .to_str()
653 .unwrap()
654 .to_string();
655
656 script_str.push_str(&format!("command script import {}\n",
657 &rust_pp_module_abs_path[..])[..]);
658 script_str.push_str("type summary add --no-value ");
659 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
660 script_str.push_str("-x \".*\" --category Rust\n");
661 script_str.push_str("type category enable Rust\n");
662
663 // Set breakpoints on every line that contains the string "#break"
664 for line in &breakpoint_lines {
665 script_str.push_str(&format!("breakpoint set --line {}\n", line));
666 }
667
668 // Append the other commands
669 for line in &commands {
670 script_str.push_str(line);
671 script_str.push_str("\n");
672 }
673
674 // Finally, quit the debugger
675 script_str.push_str("\nquit\n");
676
677 // Write the script into a file
678 debug!("script_str = {}", script_str);
679 dump_output_file(config,
680 testfile,
681 &script_str,
682 "debugger.script");
683 let debugger_script = make_out_name(config, testfile, "debugger.script");
684
685 // Let LLDB execute the script via lldb_batchmode.py
686 let debugger_run_result = run_lldb(config,
687 &exe_file,
688 &debugger_script,
689 &rust_src_root);
690
691 if !debugger_run_result.status.success() {
692 fatal_proc_rec("Error while running LLDB", &debugger_run_result);
693 }
694
695 check_debugger_output(&debugger_run_result, &check_lines);
696
697 fn run_lldb(config: &Config,
698 test_executable: &Path,
699 debugger_script: &Path,
700 rust_src_root: &Path)
701 -> ProcRes {
702 // Prepare the lldb_batchmode which executes the debugger script
703 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
704 cmd2procres(config,
705 test_executable,
706 Command::new(&config.python)
707 .arg(&lldb_script_path)
708 .arg(test_executable)
709 .arg(debugger_script)
710 .env("PYTHONPATH",
711 config.lldb_python_dir.as_ref().unwrap()))
712 }
713 }
714
715 fn cmd2procres(config: &Config, test_executable: &Path, cmd: &mut Command)
716 -> ProcRes {
717 let (status, out, err) = match cmd.output() {
718 Ok(Output { status, stdout, stderr }) => {
719 (status,
720 String::from_utf8(stdout).unwrap(),
721 String::from_utf8(stderr).unwrap())
722 },
723 Err(e) => {
724 fatal(&format!("Failed to setup Python process for \
725 LLDB script: {}", e))
726 }
727 };
728
729 dump_output(config, test_executable, &out, &err);
730 ProcRes {
731 status: Status::Normal(status),
732 stdout: out,
733 stderr: err,
734 cmdline: format!("{:?}", cmd)
735 }
736 }
737
738 struct DebuggerCommands {
739 commands: Vec<String>,
740 check_lines: Vec<String>,
741 breakpoint_lines: Vec<usize>,
742 }
743
744 fn parse_debugger_commands(file_path: &Path, debugger_prefix: &str)
745 -> DebuggerCommands {
746 let command_directive = format!("{}-command", debugger_prefix);
747 let check_directive = format!("{}-check", debugger_prefix);
748
749 let mut breakpoint_lines = vec!();
750 let mut commands = vec!();
751 let mut check_lines = vec!();
752 let mut counter = 1;
753 let reader = BufReader::new(File::open(file_path).unwrap());
754 for line in reader.lines() {
755 match line {
756 Ok(line) => {
757 if line.contains("#break") {
758 breakpoint_lines.push(counter);
759 }
760
761 header::parse_name_value_directive(
762 &line,
763 &command_directive).map(|cmd| {
764 commands.push(cmd)
765 });
766
767 header::parse_name_value_directive(
768 &line,
769 &check_directive).map(|cmd| {
770 check_lines.push(cmd)
771 });
772 }
773 Err(e) => {
774 fatal(&format!("Error while parsing debugger commands: {}", e))
775 }
776 }
777 counter += 1;
778 }
779
780 DebuggerCommands {
781 commands: commands,
782 check_lines: check_lines,
783 breakpoint_lines: breakpoint_lines,
784 }
785 }
786
787 fn cleanup_debug_info_options(options: &Option<String>) -> Option<String> {
788 if options.is_none() {
789 return None;
790 }
791
792 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
793 let options_to_remove = [
794 "-O".to_string(),
795 "-g".to_string(),
796 "--debuginfo".to_string()
797 ];
798 let new_options =
799 split_maybe_args(options).into_iter()
800 .filter(|x| !options_to_remove.contains(x))
801 .collect::<Vec<String>>()
802 .connect(" ");
803 Some(new_options)
804 }
805
806 fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String]) {
807 let num_check_lines = check_lines.len();
808 if num_check_lines > 0 {
809 // Allow check lines to leave parts unspecified (e.g., uninitialized
810 // bits in the wrong case of an enum) with the notation "[...]".
811 let check_fragments: Vec<Vec<String>> =
812 check_lines.iter().map(|s| {
813 s
814 .trim()
815 .split("[...]")
816 .map(|x| x.to_string())
817 .collect()
818 }).collect();
819 // check if each line in props.check_lines appears in the
820 // output (in order)
821 let mut i = 0;
822 for line in debugger_run_result.stdout.lines() {
823 let mut rest = line.trim();
824 let mut first = true;
825 let mut failed = false;
826 for frag in &check_fragments[i] {
827 let found = if first {
828 if rest.starts_with(frag) {
829 Some(0)
830 } else {
831 None
832 }
833 } else {
834 rest.find(frag)
835 };
836 match found {
837 None => {
838 failed = true;
839 break;
840 }
841 Some(i) => {
842 rest = &rest[(i + frag.len())..];
843 }
844 }
845 first = false;
846 }
847 if !failed && rest.is_empty() {
848 i += 1;
849 }
850 if i == num_check_lines {
851 // all lines checked
852 break;
853 }
854 }
855 if i != num_check_lines {
856 fatal_proc_rec(&format!("line not found in debugger output: {}",
857 check_lines.get(i).unwrap()),
858 debugger_run_result);
859 }
860 }
861 }
862
863 fn check_error_patterns(props: &TestProps,
864 testfile: &Path,
865 output_to_check: &str,
866 proc_res: &ProcRes) {
867 if props.error_patterns.is_empty() {
868 fatal(&format!("no error pattern specified in {:?}", testfile.display()));
869 }
870 let mut next_err_idx = 0;
871 let mut next_err_pat = &props.error_patterns[next_err_idx];
872 let mut done = false;
873 for line in output_to_check.lines() {
874 if line.contains(next_err_pat) {
875 debug!("found error pattern {}", next_err_pat);
876 next_err_idx += 1;
877 if next_err_idx == props.error_patterns.len() {
878 debug!("found all error patterns");
879 done = true;
880 break;
881 }
882 next_err_pat = &props.error_patterns[next_err_idx];
883 }
884 }
885 if done { return; }
886
887 let missing_patterns = &props.error_patterns[next_err_idx..];
888 if missing_patterns.len() == 1 {
889 fatal_proc_rec(&format!("error pattern '{}' not found!", missing_patterns[0]),
890 proc_res);
891 } else {
892 for pattern in missing_patterns {
893 error(&format!("error pattern '{}' not found!", *pattern));
894 }
895 fatal_proc_rec("multiple error patterns not found", proc_res);
896 }
897 }
898
899 fn check_no_compiler_crash(proc_res: &ProcRes) {
900 for line in proc_res.stderr.lines() {
901 if line.starts_with("error: internal compiler error:") {
902 fatal_proc_rec("compiler encountered internal error",
903 proc_res);
904 }
905 }
906 }
907
908 fn check_forbid_output(props: &TestProps,
909 output_to_check: &str,
910 proc_res: &ProcRes) {
911 for pat in &props.forbid_output {
912 if output_to_check.contains(pat) {
913 fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
914 }
915 }
916 }
917
918 fn check_expected_errors(expected_errors: Vec<errors::ExpectedError>,
919 testfile: &Path,
920 proc_res: &ProcRes) {
921
922 // true if we found the error in question
923 let mut found_flags = vec![false; expected_errors.len()];
924
925 if proc_res.status.success() {
926 fatal("process did not return an error status");
927 }
928
929 let prefixes = expected_errors.iter().map(|ee| {
930 format!("{}:{}:", testfile.display(), ee.line)
931 }).collect::<Vec<String>>();
932
933 fn prefix_matches(line: &str, prefix: &str) -> bool {
934 use std::ascii::AsciiExt;
935 // On windows just translate all '\' path separators to '/'
936 let line = line.replace(r"\", "/");
937 if cfg!(windows) {
938 line.to_ascii_lowercase().starts_with(&prefix.to_ascii_lowercase())
939 } else {
940 line.starts_with(prefix)
941 }
942 }
943
944 // A multi-line error will have followup lines which start with a space
945 // or open paren.
946 fn continuation( line: &str) -> bool {
947 line.starts_with(" ") || line.starts_with("(")
948 }
949
950 // Scan and extract our error/warning messages,
951 // which look like:
952 // filename:line1:col1: line2:col2: *error:* msg
953 // filename:line1:col1: line2:col2: *warning:* msg
954 // where line1:col1: is the starting point, line2:col2:
955 // is the ending point, and * represents ANSI color codes.
956 for line in proc_res.stderr.lines() {
957 let mut was_expected = false;
958 let mut prev = 0;
959 for (i, ee) in expected_errors.iter().enumerate() {
960 if !found_flags[i] {
961 debug!("prefix={} ee.kind={} ee.msg={} line={}",
962 prefixes[i],
963 ee.kind,
964 ee.msg,
965 line);
966 // Suggestions have no line number in their output, so take on the line number of
967 // the previous expected error
968 if ee.kind == "suggestion" {
969 assert!(expected_errors[prev].kind == "help",
970 "SUGGESTIONs must be preceded by a HELP");
971 if line.contains(&ee.msg) {
972 found_flags[i] = true;
973 was_expected = true;
974 break;
975 }
976 }
977 if (prefix_matches(line, &prefixes[i]) || continuation(line)) &&
978 line.contains(&ee.kind) &&
979 line.contains(&ee.msg) {
980 found_flags[i] = true;
981 was_expected = true;
982 break;
983 }
984 }
985 prev = i;
986 }
987
988 // ignore this msg which gets printed at the end
989 if line.contains("aborting due to") {
990 was_expected = true;
991 }
992
993 if !was_expected && is_compiler_error_or_warning(line) {
994 fatal_proc_rec(&format!("unexpected compiler error or warning: '{}'",
995 line),
996 proc_res);
997 }
998 }
999
1000 for (i, &flag) in found_flags.iter().enumerate() {
1001 if !flag {
1002 let ee = &expected_errors[i];
1003 fatal_proc_rec(&format!("expected {} on line {} not found: {}",
1004 ee.kind, ee.line, ee.msg),
1005 proc_res);
1006 }
1007 }
1008 }
1009
1010 fn is_compiler_error_or_warning(line: &str) -> bool {
1011 let mut i = 0;
1012 return
1013 scan_until_char(line, ':', &mut i) &&
1014 scan_char(line, ':', &mut i) &&
1015 scan_integer(line, &mut i) &&
1016 scan_char(line, ':', &mut i) &&
1017 scan_integer(line, &mut i) &&
1018 scan_char(line, ':', &mut i) &&
1019 scan_char(line, ' ', &mut i) &&
1020 scan_integer(line, &mut i) &&
1021 scan_char(line, ':', &mut i) &&
1022 scan_integer(line, &mut i) &&
1023 scan_char(line, ' ', &mut i) &&
1024 (scan_string(line, "error", &mut i) ||
1025 scan_string(line, "warning", &mut i));
1026 }
1027
1028 fn scan_until_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1029 if *idx >= haystack.len() {
1030 return false;
1031 }
1032 let opt = haystack[(*idx)..].find(needle);
1033 if opt.is_none() {
1034 return false;
1035 }
1036 *idx = opt.unwrap();
1037 return true;
1038 }
1039
1040 fn scan_char(haystack: &str, needle: char, idx: &mut usize) -> bool {
1041 if *idx >= haystack.len() {
1042 return false;
1043 }
1044 let ch = haystack.char_at(*idx);
1045 if ch != needle {
1046 return false;
1047 }
1048 *idx += ch.len_utf8();
1049 return true;
1050 }
1051
1052 fn scan_integer(haystack: &str, idx: &mut usize) -> bool {
1053 let mut i = *idx;
1054 while i < haystack.len() {
1055 let ch = haystack.char_at(i);
1056 if ch < '0' || '9' < ch {
1057 break;
1058 }
1059 i += ch.len_utf8();
1060 }
1061 if i == *idx {
1062 return false;
1063 }
1064 *idx = i;
1065 return true;
1066 }
1067
1068 fn scan_string(haystack: &str, needle: &str, idx: &mut usize) -> bool {
1069 let mut haystack_i = *idx;
1070 let mut needle_i = 0;
1071 while needle_i < needle.len() {
1072 if haystack_i >= haystack.len() {
1073 return false;
1074 }
1075 let ch = haystack.char_at(haystack_i);
1076 haystack_i += ch.len_utf8();
1077 if !scan_char(needle, ch, &mut needle_i) {
1078 return false;
1079 }
1080 }
1081 *idx = haystack_i;
1082 return true;
1083 }
1084
1085 struct ProcArgs {
1086 prog: String,
1087 args: Vec<String>,
1088 }
1089
1090 struct ProcRes {
1091 status: Status,
1092 stdout: String,
1093 stderr: String,
1094 cmdline: String,
1095 }
1096
1097 enum Status {
1098 Parsed(i32),
1099 Normal(ExitStatus),
1100 }
1101
1102 impl Status {
1103 fn code(&self) -> Option<i32> {
1104 match *self {
1105 Status::Parsed(i) => Some(i),
1106 Status::Normal(ref e) => e.code(),
1107 }
1108 }
1109
1110 fn success(&self) -> bool {
1111 match *self {
1112 Status::Parsed(i) => i == 0,
1113 Status::Normal(ref e) => e.success(),
1114 }
1115 }
1116 }
1117
1118 impl fmt::Display for Status {
1119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1120 match *self {
1121 Status::Parsed(i) => write!(f, "exit code: {}", i),
1122 Status::Normal(ref e) => e.fmt(f),
1123 }
1124 }
1125 }
1126
1127 fn compile_test(config: &Config, props: &TestProps,
1128 testfile: &Path) -> ProcRes {
1129 compile_test_(config, props, testfile, &[])
1130 }
1131
1132 fn compile_test_(config: &Config, props: &TestProps,
1133 testfile: &Path, extra_args: &[String]) -> ProcRes {
1134 let aux_dir = aux_output_dir_name(config, testfile);
1135 // FIXME (#9639): This needs to handle non-utf8 paths
1136 let mut link_args = vec!("-L".to_string(),
1137 aux_dir.to_str().unwrap().to_string());
1138 link_args.extend(extra_args.iter().cloned());
1139 let args = make_compile_args(config,
1140 props,
1141 link_args,
1142 |a, b| TargetLocation::ThisFile(make_exe_name(a, b)), testfile);
1143 compose_and_run_compiler(config, props, testfile, args, None)
1144 }
1145
1146 fn document(config: &Config, props: &TestProps,
1147 testfile: &Path, extra_args: &[String]) -> (ProcRes, PathBuf) {
1148 let aux_dir = aux_output_dir_name(config, testfile);
1149 let out_dir = output_base_name(config, testfile);
1150 let _ = fs::remove_dir_all(&out_dir);
1151 ensure_dir(&out_dir);
1152 let mut args = vec!["-L".to_string(),
1153 aux_dir.to_str().unwrap().to_string(),
1154 "-o".to_string(),
1155 out_dir.to_str().unwrap().to_string(),
1156 testfile.to_str().unwrap().to_string()];
1157 args.extend(extra_args.iter().cloned());
1158 args.extend(split_maybe_args(&props.compile_flags));
1159 let args = ProcArgs {
1160 prog: config.rustdoc_path.to_str().unwrap().to_string(),
1161 args: args,
1162 };
1163 (compose_and_run_compiler(config, props, testfile, args, None), out_dir)
1164 }
1165
1166 fn exec_compiled_test(config: &Config, props: &TestProps,
1167 testfile: &Path) -> ProcRes {
1168
1169 let env = props.exec_env.clone();
1170
1171 match &*config.target {
1172
1173 "arm-linux-androideabi" | "aarch64-linux-android" => {
1174 _arm_exec_compiled_test(config, props, testfile, env)
1175 }
1176
1177 _=> {
1178 let aux_dir = aux_output_dir_name(config, testfile);
1179 compose_and_run(config,
1180 testfile,
1181 make_run_args(config, props, testfile),
1182 env,
1183 &config.run_lib_path,
1184 Some(aux_dir.to_str().unwrap()),
1185 None)
1186 }
1187 }
1188 }
1189
1190 fn compose_and_run_compiler(config: &Config, props: &TestProps,
1191 testfile: &Path, args: ProcArgs,
1192 input: Option<String>) -> ProcRes {
1193 if !props.aux_builds.is_empty() {
1194 ensure_dir(&aux_output_dir_name(config, testfile));
1195 }
1196
1197 let aux_dir = aux_output_dir_name(config, testfile);
1198 // FIXME (#9639): This needs to handle non-utf8 paths
1199 let extra_link_args = vec!["-L".to_string(),
1200 aux_dir.to_str().unwrap().to_string()];
1201
1202 for rel_ab in &props.aux_builds {
1203 let abs_ab = config.aux_base.join(rel_ab);
1204 let aux_props = header::load_props(&abs_ab);
1205 let mut crate_type = if aux_props.no_prefer_dynamic {
1206 Vec::new()
1207 } else {
1208 // We primarily compile all auxiliary libraries as dynamic libraries
1209 // to avoid code size bloat and large binaries as much as possible
1210 // for the test suite (otherwise including libstd statically in all
1211 // executables takes up quite a bit of space).
1212 //
1213 // For targets like MUSL, however, there is no support for dynamic
1214 // libraries so we just go back to building a normal library. Note,
1215 // however, that if the library is built with `force_host` then it's
1216 // ok to be a dylib as the host should always support dylibs.
1217 if config.target.contains("musl") && !aux_props.force_host {
1218 vec!("--crate-type=lib".to_string())
1219 } else {
1220 vec!("--crate-type=dylib".to_string())
1221 }
1222 };
1223 crate_type.extend(extra_link_args.clone());
1224 let aux_args =
1225 make_compile_args(config,
1226 &aux_props,
1227 crate_type,
1228 |a,b| {
1229 let f = make_lib_name(a, b, testfile);
1230 let parent = f.parent().unwrap();
1231 TargetLocation::ThisDirectory(parent.to_path_buf())
1232 },
1233 &abs_ab);
1234 let auxres = compose_and_run(config,
1235 &abs_ab,
1236 aux_args,
1237 Vec::new(),
1238 &config.compile_lib_path,
1239 Some(aux_dir.to_str().unwrap()),
1240 None);
1241 if !auxres.status.success() {
1242 fatal_proc_rec(
1243 &format!("auxiliary build of {:?} failed to compile: ",
1244 abs_ab.display()),
1245 &auxres);
1246 }
1247
1248 match &*config.target {
1249 "arm-linux-androideabi" | "aarch64-linux-android" => {
1250 _arm_push_aux_shared_library(config, testfile);
1251 }
1252 _ => {}
1253 }
1254 }
1255
1256 compose_and_run(config,
1257 testfile,
1258 args,
1259 Vec::new(),
1260 &config.compile_lib_path,
1261 Some(aux_dir.to_str().unwrap()),
1262 input)
1263 }
1264
1265 fn ensure_dir(path: &Path) {
1266 if path.is_dir() { return; }
1267 fs::create_dir(path).unwrap();
1268 }
1269
1270 fn compose_and_run(config: &Config, testfile: &Path,
1271 ProcArgs{ args, prog }: ProcArgs,
1272 procenv: Vec<(String, String)> ,
1273 lib_path: &str,
1274 aux_path: Option<&str>,
1275 input: Option<String>) -> ProcRes {
1276 return program_output(config, testfile, lib_path,
1277 prog, aux_path, args, procenv, input);
1278 }
1279
1280 enum TargetLocation {
1281 ThisFile(PathBuf),
1282 ThisDirectory(PathBuf),
1283 }
1284
1285 fn make_compile_args<F>(config: &Config,
1286 props: &TestProps,
1287 extras: Vec<String> ,
1288 xform: F,
1289 testfile: &Path)
1290 -> ProcArgs where
1291 F: FnOnce(&Config, &Path) -> TargetLocation,
1292 {
1293 let xform_file = xform(config, testfile);
1294 let target = if props.force_host {
1295 &*config.host
1296 } else {
1297 &*config.target
1298 };
1299 // FIXME (#9639): This needs to handle non-utf8 paths
1300 let mut args = vec!(testfile.to_str().unwrap().to_string(),
1301 "-L".to_string(),
1302 config.build_base.to_str().unwrap().to_string(),
1303 format!("--target={}", target));
1304 args.push_all(&extras);
1305 if !props.no_prefer_dynamic {
1306 args.push("-C".to_string());
1307 args.push("prefer-dynamic".to_string());
1308 }
1309 let path = match xform_file {
1310 TargetLocation::ThisFile(path) => {
1311 args.push("-o".to_string());
1312 path
1313 }
1314 TargetLocation::ThisDirectory(path) => {
1315 args.push("--out-dir".to_string());
1316 path
1317 }
1318 };
1319 args.push(path.to_str().unwrap().to_string());
1320 if props.force_host {
1321 args.extend(split_maybe_args(&config.host_rustcflags));
1322 } else {
1323 args.extend(split_maybe_args(&config.target_rustcflags));
1324 }
1325 args.extend(split_maybe_args(&props.compile_flags));
1326 return ProcArgs {
1327 prog: config.rustc_path.to_str().unwrap().to_string(),
1328 args: args,
1329 };
1330 }
1331
1332 fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> PathBuf {
1333 // what we return here is not particularly important, as it
1334 // happens; rustc ignores everything except for the directory.
1335 let auxname = output_testname(auxfile);
1336 aux_output_dir_name(config, testfile).join(&auxname)
1337 }
1338
1339 fn make_exe_name(config: &Config, testfile: &Path) -> PathBuf {
1340 let mut f = output_base_name(config, testfile);
1341 if !env::consts::EXE_SUFFIX.is_empty() {
1342 let mut fname = f.file_name().unwrap().to_os_string();
1343 fname.push(env::consts::EXE_SUFFIX);
1344 f.set_file_name(&fname);
1345 }
1346 f
1347 }
1348
1349 fn make_run_args(config: &Config, props: &TestProps, testfile: &Path)
1350 -> ProcArgs {
1351 // If we've got another tool to run under (valgrind),
1352 // then split apart its command
1353 let mut args = split_maybe_args(&config.runtool);
1354 let exe_file = make_exe_name(config, testfile);
1355
1356 // FIXME (#9639): This needs to handle non-utf8 paths
1357 args.push(exe_file.to_str().unwrap().to_string());
1358
1359 // Add the arguments in the run_flags directive
1360 args.extend(split_maybe_args(&props.run_flags));
1361
1362 let prog = args.remove(0);
1363 return ProcArgs {
1364 prog: prog,
1365 args: args,
1366 };
1367 }
1368
1369 fn split_maybe_args(argstr: &Option<String>) -> Vec<String> {
1370 match *argstr {
1371 Some(ref s) => {
1372 s
1373 .split(' ')
1374 .filter_map(|s| {
1375 if s.chars().all(|c| c.is_whitespace()) {
1376 None
1377 } else {
1378 Some(s.to_string())
1379 }
1380 }).collect()
1381 }
1382 None => Vec::new()
1383 }
1384 }
1385
1386 fn program_output(config: &Config, testfile: &Path, lib_path: &str, prog: String,
1387 aux_path: Option<&str>, args: Vec<String>,
1388 env: Vec<(String, String)>,
1389 input: Option<String>) -> ProcRes {
1390 let cmdline =
1391 {
1392 let cmdline = make_cmdline(lib_path,
1393 &prog,
1394 &args);
1395 logv(config, format!("executing {}", cmdline));
1396 cmdline
1397 };
1398 let procsrv::Result {
1399 out,
1400 err,
1401 status
1402 } = procsrv::run(lib_path,
1403 &prog,
1404 aux_path,
1405 &args,
1406 env,
1407 input).expect(&format!("failed to exec `{}`", prog));
1408 dump_output(config, testfile, &out, &err);
1409 return ProcRes {
1410 status: Status::Normal(status),
1411 stdout: out,
1412 stderr: err,
1413 cmdline: cmdline,
1414 };
1415 }
1416
1417 fn make_cmdline(libpath: &str, prog: &str, args: &[String]) -> String {
1418 use util;
1419
1420 // Linux and mac don't require adjusting the library search path
1421 if cfg!(unix) {
1422 format!("{} {}", prog, args.connect(" "))
1423 } else {
1424 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1425 // for diagnostic purposes
1426 fn lib_path_cmd_prefix(path: &str) -> String {
1427 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1428 }
1429
1430 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.connect(" "))
1431 }
1432 }
1433
1434 fn dump_output(config: &Config, testfile: &Path, out: &str, err: &str) {
1435 dump_output_file(config, testfile, out, "out");
1436 dump_output_file(config, testfile, err, "err");
1437 maybe_dump_to_stdout(config, out, err);
1438 }
1439
1440 fn dump_output_file(config: &Config, testfile: &Path,
1441 out: &str, extension: &str) {
1442 let outfile = make_out_name(config, testfile, extension);
1443 File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1444 }
1445
1446 fn make_out_name(config: &Config, testfile: &Path, extension: &str) -> PathBuf {
1447 output_base_name(config, testfile).with_extension(extension)
1448 }
1449
1450 fn aux_output_dir_name(config: &Config, testfile: &Path) -> PathBuf {
1451 let f = output_base_name(config, testfile);
1452 let mut fname = f.file_name().unwrap().to_os_string();
1453 fname.push(&format!(".{}.libaux", config.mode));
1454 f.with_file_name(&fname)
1455 }
1456
1457 fn output_testname(testfile: &Path) -> PathBuf {
1458 PathBuf::from(testfile.file_stem().unwrap())
1459 }
1460
1461 fn output_base_name(config: &Config, testfile: &Path) -> PathBuf {
1462 config.build_base
1463 .join(&output_testname(testfile))
1464 .with_extension(&config.stage_id)
1465 }
1466
1467 fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
1468 if config.verbose {
1469 println!("------{}------------------------------", "stdout");
1470 println!("{}", out);
1471 println!("------{}------------------------------", "stderr");
1472 println!("{}", err);
1473 println!("------------------------------------------");
1474 }
1475 }
1476
1477 fn error(err: &str) { println!("\nerror: {}", err); }
1478
1479 fn fatal(err: &str) -> ! { error(err); panic!(); }
1480
1481 fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
1482 print!("\n\
1483 error: {}\n\
1484 status: {}\n\
1485 command: {}\n\
1486 stdout:\n\
1487 ------------------------------------------\n\
1488 {}\n\
1489 ------------------------------------------\n\
1490 stderr:\n\
1491 ------------------------------------------\n\
1492 {}\n\
1493 ------------------------------------------\n\
1494 \n",
1495 err, proc_res.status, proc_res.cmdline, proc_res.stdout,
1496 proc_res.stderr);
1497 panic!();
1498 }
1499
1500 fn _arm_exec_compiled_test(config: &Config,
1501 props: &TestProps,
1502 testfile: &Path,
1503 env: Vec<(String, String)>)
1504 -> ProcRes {
1505 let args = make_run_args(config, props, testfile);
1506 let cmdline = make_cmdline("",
1507 &args.prog,
1508 &args.args);
1509
1510 // get bare program string
1511 let mut tvec: Vec<String> = args.prog
1512 .split('/')
1513 .map(|ts| ts.to_string())
1514 .collect();
1515 let prog_short = tvec.pop().unwrap();
1516
1517 // copy to target
1518 let copy_result = procsrv::run("",
1519 &config.adb_path,
1520 None,
1521 &[
1522 "push".to_string(),
1523 args.prog.clone(),
1524 config.adb_test_dir.clone()
1525 ],
1526 vec!(("".to_string(), "".to_string())),
1527 Some("".to_string()))
1528 .expect(&format!("failed to exec `{}`", config.adb_path));
1529
1530 if config.verbose {
1531 println!("push ({}) {} {} {}",
1532 config.target,
1533 args.prog,
1534 copy_result.out,
1535 copy_result.err);
1536 }
1537
1538 logv(config, format!("executing ({}) {}", config.target, cmdline));
1539
1540 let mut runargs = Vec::new();
1541
1542 // run test via adb_run_wrapper
1543 runargs.push("shell".to_string());
1544 for (key, val) in env {
1545 runargs.push(format!("{}={}", key, val));
1546 }
1547 runargs.push(format!("{}/../adb_run_wrapper.sh", config.adb_test_dir));
1548 runargs.push(format!("{}", config.adb_test_dir));
1549 runargs.push(format!("{}", prog_short));
1550
1551 for tv in &args.args {
1552 runargs.push(tv.to_string());
1553 }
1554 procsrv::run("",
1555 &config.adb_path,
1556 None,
1557 &runargs,
1558 vec!(("".to_string(), "".to_string())), Some("".to_string()))
1559 .expect(&format!("failed to exec `{}`", config.adb_path));
1560
1561 // get exitcode of result
1562 runargs = Vec::new();
1563 runargs.push("shell".to_string());
1564 runargs.push("cat".to_string());
1565 runargs.push(format!("{}/{}.exitcode", config.adb_test_dir, prog_short));
1566
1567 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1568 procsrv::run("",
1569 &config.adb_path,
1570 None,
1571 &runargs,
1572 vec!(("".to_string(), "".to_string())),
1573 Some("".to_string()))
1574 .expect(&format!("failed to exec `{}`", config.adb_path));
1575
1576 let mut exitcode: i32 = 0;
1577 for c in exitcode_out.chars() {
1578 if !c.is_numeric() { break; }
1579 exitcode = exitcode * 10 + match c {
1580 '0' ... '9' => c as i32 - ('0' as i32),
1581 _ => 101,
1582 }
1583 }
1584
1585 // get stdout of result
1586 runargs = Vec::new();
1587 runargs.push("shell".to_string());
1588 runargs.push("cat".to_string());
1589 runargs.push(format!("{}/{}.stdout", config.adb_test_dir, prog_short));
1590
1591 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1592 procsrv::run("",
1593 &config.adb_path,
1594 None,
1595 &runargs,
1596 vec!(("".to_string(), "".to_string())),
1597 Some("".to_string()))
1598 .expect(&format!("failed to exec `{}`", config.adb_path));
1599
1600 // get stderr of result
1601 runargs = Vec::new();
1602 runargs.push("shell".to_string());
1603 runargs.push("cat".to_string());
1604 runargs.push(format!("{}/{}.stderr", config.adb_test_dir, prog_short));
1605
1606 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1607 procsrv::run("",
1608 &config.adb_path,
1609 None,
1610 &runargs,
1611 vec!(("".to_string(), "".to_string())),
1612 Some("".to_string()))
1613 .expect(&format!("failed to exec `{}`", config.adb_path));
1614
1615 dump_output(config,
1616 testfile,
1617 &stdout_out,
1618 &stderr_out);
1619
1620 ProcRes {
1621 status: Status::Parsed(exitcode),
1622 stdout: stdout_out,
1623 stderr: stderr_out,
1624 cmdline: cmdline
1625 }
1626 }
1627
1628 fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
1629 let tdir = aux_output_dir_name(config, testfile);
1630
1631 let dirs = fs::read_dir(&tdir).unwrap();
1632 for file in dirs {
1633 let file = file.unwrap().path();
1634 if file.extension().and_then(|s| s.to_str()) == Some("so") {
1635 // FIXME (#9639): This needs to handle non-utf8 paths
1636 let copy_result = procsrv::run("",
1637 &config.adb_path,
1638 None,
1639 &[
1640 "push".to_string(),
1641 file.to_str()
1642 .unwrap()
1643 .to_string(),
1644 config.adb_test_dir.to_string(),
1645 ],
1646 vec!(("".to_string(),
1647 "".to_string())),
1648 Some("".to_string()))
1649 .expect(&format!("failed to exec `{}`", config.adb_path));
1650
1651 if config.verbose {
1652 println!("push ({}) {:?} {} {}",
1653 config.target, file.display(),
1654 copy_result.out, copy_result.err);
1655 }
1656 }
1657 }
1658 }
1659
1660 // codegen tests (using FileCheck)
1661
1662 fn compile_test_and_save_ir(config: &Config, props: &TestProps,
1663 testfile: &Path) -> ProcRes {
1664 let aux_dir = aux_output_dir_name(config, testfile);
1665 // FIXME (#9639): This needs to handle non-utf8 paths
1666 let mut link_args = vec!("-L".to_string(),
1667 aux_dir.to_str().unwrap().to_string());
1668 let llvm_args = vec!("--emit=llvm-ir".to_string(),
1669 "--crate-type=lib".to_string());
1670 link_args.extend(llvm_args);
1671 let args = make_compile_args(config,
1672 props,
1673 link_args,
1674 |a, b| TargetLocation::ThisDirectory(
1675 output_base_name(a, b).parent()
1676 .unwrap().to_path_buf()),
1677 testfile);
1678 compose_and_run_compiler(config, props, testfile, args, None)
1679 }
1680
1681 fn check_ir_with_filecheck(config: &Config, testfile: &Path) -> ProcRes {
1682 let irfile = output_base_name(config, testfile).with_extension("ll");
1683 let prog = config.llvm_bin_path.as_ref().unwrap().join("FileCheck");
1684 let proc_args = ProcArgs {
1685 // FIXME (#9639): This needs to handle non-utf8 paths
1686 prog: prog.to_str().unwrap().to_string(),
1687 args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1688 testfile.to_str().unwrap().to_string())
1689 };
1690 compose_and_run(config, testfile, proc_args, Vec::new(), "", None, None)
1691 }
1692
1693 fn run_codegen_test(config: &Config, props: &TestProps, testfile: &Path) {
1694
1695 if config.llvm_bin_path.is_none() {
1696 fatal("missing --llvm-bin-path");
1697 }
1698
1699 let mut proc_res = compile_test_and_save_ir(config, props, testfile);
1700 if !proc_res.status.success() {
1701 fatal_proc_rec("compilation failed!", &proc_res);
1702 }
1703
1704 proc_res = check_ir_with_filecheck(config, testfile);
1705 if !proc_res.status.success() {
1706 fatal_proc_rec("verification with 'FileCheck' failed",
1707 &proc_res);
1708 }
1709 }
1710
1711 fn charset() -> &'static str {
1712 if cfg!(any(target_os = "bitrig", target_os = "freebsd")) {
1713 "auto"
1714 } else {
1715 "UTF-8"
1716 }
1717 }
1718
1719 fn run_rustdoc_test(config: &Config, props: &TestProps, testfile: &Path) {
1720 let (proc_res, out_dir) = document(config, props, testfile, &[]);
1721 if !proc_res.status.success() {
1722 fatal_proc_rec("rustdoc failed!", &proc_res);
1723 }
1724 let root = find_rust_src_root(config).unwrap();
1725
1726 let res = cmd2procres(config,
1727 testfile,
1728 Command::new(&config.python)
1729 .arg(root.join("src/etc/htmldocck.py"))
1730 .arg(out_dir)
1731 .arg(testfile));
1732 if !res.status.success() {
1733 fatal_proc_rec("htmldocck failed!", &res);
1734 }
1735 }