]> git.proxmox.com Git - rustc.git/blame - src/tools/compiletest/src/runtest.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / tools / compiletest / src / runtest.rs
CommitLineData
a7813a04
XL
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
11use common::Config;
12use common::{CompileFail, ParseFail, Pretty, RunFail, RunPass, RunPassValgrind};
13use common::{Codegen, DebugInfoLldb, DebugInfoGdb, Rustdoc, CodegenUnits};
5bcae85e 14use common::{Incremental, RunMake, Ui, MirOpt};
a7813a04
XL
15use errors::{self, ErrorKind, Error};
16use json;
17use header::TestProps;
18use header;
19use procsrv;
20use test::TestPaths;
21use uidiff;
22use util::logv;
23
24use std::env;
25use std::collections::HashSet;
26use std::fmt;
27use std::fs::{self, File};
28use std::io::{self, BufReader};
29use std::io::prelude::*;
30use std::net::TcpStream;
31use std::path::{Path, PathBuf};
32use std::process::{Command, Output, ExitStatus};
33use std::str;
34
35pub fn run(config: Config, testpaths: &TestPaths) {
36 match &*config.target {
37
38 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
39 if !config.adb_device_status {
40 panic!("android device not available");
41 }
42 }
43
44 _=> { }
45 }
46
47 if config.verbose {
48 // We're going to be dumping a lot of info. Start on a new line.
49 print!("\n\n");
50 }
51 debug!("running {:?}", testpaths.file.display());
52 let base_props = TestProps::from_file(&testpaths.file);
53
54 let base_cx = TestCx { config: &config,
55 props: &base_props,
56 testpaths: testpaths,
57 revision: None };
58 base_cx.init_all();
59
60 if base_props.revisions.is_empty() {
61 base_cx.run_revision()
62 } else {
63 for revision in &base_props.revisions {
64 let mut revision_props = base_props.clone();
65 revision_props.load_from(&testpaths.file, Some(&revision));
66 let rev_cx = TestCx {
67 config: &config,
68 props: &revision_props,
69 testpaths: testpaths,
70 revision: Some(revision)
71 };
72 rev_cx.run_revision();
73 }
74 }
75
76 base_cx.complete_all();
77}
78
79struct TestCx<'test> {
80 config: &'test Config,
81 props: &'test TestProps,
82 testpaths: &'test TestPaths,
83 revision: Option<&'test str>
84}
85
86struct DebuggerCommands {
87 commands: Vec<String>,
88 check_lines: Vec<String>,
89 breakpoint_lines: Vec<usize>,
90}
91
92impl<'test> TestCx<'test> {
93 /// invoked once before any revisions have been processed
94 fn init_all(&self) {
95 assert!(self.revision.is_none(), "init_all invoked for a revision");
96 match self.config.mode {
97 Incremental => self.init_incremental_test(),
98 _ => { }
99 }
100 }
101
102 /// Code executed for each revision in turn (or, if there are no
103 /// revisions, exactly once, with revision == None).
104 fn run_revision(&self) {
105 match self.config.mode {
106 CompileFail => self.run_cfail_test(),
107 ParseFail => self.run_cfail_test(),
108 RunFail => self.run_rfail_test(),
109 RunPass => self.run_rpass_test(),
110 RunPassValgrind => self.run_valgrind_test(),
111 Pretty => self.run_pretty_test(),
112 DebugInfoGdb => self.run_debuginfo_gdb_test(),
113 DebugInfoLldb => self.run_debuginfo_lldb_test(),
114 Codegen => self.run_codegen_test(),
115 Rustdoc => self.run_rustdoc_test(),
116 CodegenUnits => self.run_codegen_units_test(),
117 Incremental => self.run_incremental_test(),
118 RunMake => self.run_rmake_test(),
119 Ui => self.run_ui_test(),
5bcae85e 120 MirOpt => self.run_mir_opt_test(),
a7813a04
XL
121 }
122 }
123
124 /// Invoked after all revisions have executed.
125 fn complete_all(&self) {
126 assert!(self.revision.is_none(), "init_all invoked for a revision");
127 }
128
129 fn run_cfail_test(&self) {
130 let proc_res = self.compile_test();
131
9e0c209e
SL
132 if self.props.must_compile_successfully {
133 if !proc_res.status.success() {
134 self.fatal_proc_rec(
135 "test compilation failed although it shouldn't!",
136 &proc_res);
137 }
138 } else {
139 if proc_res.status.success() {
140 self.fatal_proc_rec(
141 &format!("{} test compiled successfully!", self.config.mode)[..],
142 &proc_res);
143 }
a7813a04 144
9e0c209e
SL
145 self.check_correct_failure_status(&proc_res);
146 }
a7813a04 147
a7813a04
XL
148 let output_to_check = self.get_output(&proc_res);
149 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
150 if !expected_errors.is_empty() {
151 if !self.props.error_patterns.is_empty() {
152 self.fatal("both error pattern and expected errors specified");
153 }
154 self.check_expected_errors(expected_errors, &proc_res);
155 } else {
156 self.check_error_patterns(&output_to_check, &proc_res);
157 }
9e0c209e 158
a7813a04
XL
159 self.check_no_compiler_crash(&proc_res);
160 self.check_forbid_output(&output_to_check, &proc_res);
161 }
162
163 fn run_rfail_test(&self) {
164 let proc_res = self.compile_test();
165
166 if !proc_res.status.success() {
167 self.fatal_proc_rec("compilation failed!", &proc_res);
168 }
169
170 let proc_res = self.exec_compiled_test();
171
172 // The value our Makefile configures valgrind to return on failure
173 const VALGRIND_ERR: i32 = 100;
174 if proc_res.status.code() == Some(VALGRIND_ERR) {
175 self.fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
176 }
177
178 let output_to_check = self.get_output(&proc_res);
179 self.check_correct_failure_status(&proc_res);
180 self.check_error_patterns(&output_to_check, &proc_res);
181 }
182
183 fn get_output(&self, proc_res: &ProcRes) -> String {
184 if self.props.check_stdout {
185 format!("{}{}", proc_res.stdout, proc_res.stderr)
186 } else {
187 proc_res.stderr.clone()
188 }
189 }
190
191 fn check_correct_failure_status(&self, proc_res: &ProcRes) {
192 // The value the rust runtime returns on failure
193 const RUST_ERR: i32 = 101;
194 if proc_res.status.code() != Some(RUST_ERR) {
195 self.fatal_proc_rec(
196 &format!("failure produced the wrong error: {}",
197 proc_res.status),
198 proc_res);
199 }
200 }
201
202 fn run_rpass_test(&self) {
203 let proc_res = self.compile_test();
204
205 if !proc_res.status.success() {
206 self.fatal_proc_rec("compilation failed!", &proc_res);
207 }
208
9e0c209e
SL
209 let expected_errors = errors::load_errors(&self.testpaths.file, self.revision);
210 if !expected_errors.is_empty() {
211 self.check_expected_errors(expected_errors, &proc_res);
212 }
213
a7813a04
XL
214 let proc_res = self.exec_compiled_test();
215
216 if !proc_res.status.success() {
217 self.fatal_proc_rec("test run failed!", &proc_res);
218 }
219 }
220
221 fn run_valgrind_test(&self) {
222 assert!(self.revision.is_none(), "revisions not relevant here");
223
224 if self.config.valgrind_path.is_none() {
225 assert!(!self.config.force_valgrind);
226 return self.run_rpass_test();
227 }
228
229 let mut proc_res = self.compile_test();
230
231 if !proc_res.status.success() {
232 self.fatal_proc_rec("compilation failed!", &proc_res);
233 }
234
235 let mut new_config = self.config.clone();
236 new_config.runtool = new_config.valgrind_path.clone();
237 let new_cx = TestCx { config: &new_config, ..*self };
238 proc_res = new_cx.exec_compiled_test();
239
240 if !proc_res.status.success() {
241 self.fatal_proc_rec("test run failed!", &proc_res);
242 }
243 }
244
245 fn run_pretty_test(&self) {
246 if self.props.pp_exact.is_some() {
247 logv(self.config, "testing for exact pretty-printing".to_owned());
248 } else {
249 logv(self.config, "testing for converging pretty-printing".to_owned());
250 }
251
252 let rounds = match self.props.pp_exact { Some(_) => 1, None => 2 };
253
254 let mut src = String::new();
255 File::open(&self.testpaths.file).unwrap().read_to_string(&mut src).unwrap();
256 let mut srcs = vec!(src);
257
258 let mut round = 0;
259 while round < rounds {
260 logv(self.config, format!("pretty-printing round {} revision {:?}",
261 round, self.revision));
262 let proc_res = self.print_source(srcs[round].to_owned(), &self.props.pretty_mode);
263
264 if !proc_res.status.success() {
265 self.fatal_proc_rec(&format!("pretty-printing failed in round {} revision {:?}",
266 round, self.revision),
267 &proc_res);
268 }
269
270 let ProcRes{ stdout, .. } = proc_res;
271 srcs.push(stdout);
272 round += 1;
273 }
274
275 let mut expected = match self.props.pp_exact {
276 Some(ref file) => {
277 let filepath = self.testpaths.file.parent().unwrap().join(file);
278 let mut s = String::new();
279 File::open(&filepath).unwrap().read_to_string(&mut s).unwrap();
280 s
281 }
282 None => { srcs[srcs.len() - 2].clone() }
283 };
284 let mut actual = srcs[srcs.len() - 1].clone();
285
286 if self.props.pp_exact.is_some() {
287 // Now we have to care about line endings
288 let cr = "\r".to_owned();
289 actual = actual.replace(&cr, "").to_owned();
290 expected = expected.replace(&cr, "").to_owned();
291 }
292
293 self.compare_source(&expected, &actual);
294
295 // If we're only making sure that the output matches then just stop here
296 if self.props.pretty_compare_only { return; }
297
298 // Finally, let's make sure it actually appears to remain valid code
299 let proc_res = self.typecheck_source(actual);
300 if !proc_res.status.success() {
301 self.fatal_proc_rec("pretty-printed source does not typecheck", &proc_res);
302 }
303
304 if !self.props.pretty_expanded { return }
305
306 // additionally, run `--pretty expanded` and try to build it.
307 let proc_res = self.print_source(srcs[round].clone(), "expanded");
308 if !proc_res.status.success() {
309 self.fatal_proc_rec("pretty-printing (expanded) failed", &proc_res);
310 }
311
312 let ProcRes{ stdout: expanded_src, .. } = proc_res;
313 let proc_res = self.typecheck_source(expanded_src);
314 if !proc_res.status.success() {
315 self.fatal_proc_rec(
316 "pretty-printed source (expanded) does not typecheck",
317 &proc_res);
318 }
319 }
320
321 fn print_source(&self,
322 src: String,
323 pretty_type: &str)
324 -> ProcRes {
325 let aux_dir = self.aux_output_dir_name();
326 self.compose_and_run(self.make_pp_args(pretty_type.to_owned()),
327 self.props.exec_env.clone(),
328 self.config.compile_lib_path.to_str().unwrap(),
329 Some(aux_dir.to_str().unwrap()),
330 Some(src))
331 }
332
333 fn make_pp_args(&self,
334 pretty_type: String)
335 -> ProcArgs {
336 let aux_dir = self.aux_output_dir_name();
337 // FIXME (#9639): This needs to handle non-utf8 paths
338 let mut args = vec!("-".to_owned(),
339 "-Zunstable-options".to_owned(),
340 "--unpretty".to_owned(),
341 pretty_type,
342 format!("--target={}", self.config.target),
343 "-L".to_owned(),
344 aux_dir.to_str().unwrap().to_owned());
345 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
346 args.extend(self.props.compile_flags.iter().cloned());
347 return ProcArgs {
348 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
349 args: args,
350 };
351 }
352
353 fn compare_source(&self,
354 expected: &str,
355 actual: &str) {
356 if expected != actual {
357 self.error("pretty-printed source does not match expected source");
358 println!("\n\
359expected:\n\
360------------------------------------------\n\
361{}\n\
362------------------------------------------\n\
363actual:\n\
364------------------------------------------\n\
365{}\n\
366------------------------------------------\n\
367\n",
368 expected, actual);
369 panic!();
370 }
371 }
372
373 fn typecheck_source(&self, src: String) -> ProcRes {
374 let args = self.make_typecheck_args();
375 self.compose_and_run_compiler(args, Some(src))
376 }
377
378 fn make_typecheck_args(&self) -> ProcArgs {
379 let aux_dir = self.aux_output_dir_name();
380 let target = if self.props.force_host {
381 &*self.config.host
382 } else {
383 &*self.config.target
384 };
3157f602
XL
385
386 let out_dir = self.output_base_name().with_extension("pretty-out");
387 let _ = fs::remove_dir_all(&out_dir);
388 self.create_dir_racy(&out_dir);
389
a7813a04
XL
390 // FIXME (#9639): This needs to handle non-utf8 paths
391 let mut args = vec!("-".to_owned(),
392 "-Zno-trans".to_owned(),
3157f602
XL
393 "--out-dir".to_owned(),
394 out_dir.to_str().unwrap().to_owned(),
a7813a04
XL
395 format!("--target={}", target),
396 "-L".to_owned(),
397 self.config.build_base.to_str().unwrap().to_owned(),
398 "-L".to_owned(),
399 aux_dir.to_str().unwrap().to_owned());
400 if let Some(revision) = self.revision {
401 args.extend(vec![
402 format!("--cfg"),
403 format!("{}", revision),
404 ]);
405 }
406 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
407 args.extend(self.props.compile_flags.iter().cloned());
408 // FIXME (#9639): This needs to handle non-utf8 paths
409 return ProcArgs {
410 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
411 args: args,
412 };
413 }
414
415 fn run_debuginfo_gdb_test(&self) {
416 assert!(self.revision.is_none(), "revisions not relevant here");
417
418 let config = Config {
419 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
420 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
421 .. self.config.clone()
422 };
423
424 let test_cx = TestCx {
425 config: &config,
426 ..*self
427 };
428
429 test_cx.run_debuginfo_gdb_test_no_opt();
430 }
431
432 fn run_debuginfo_gdb_test_no_opt(&self) {
433 let DebuggerCommands {
434 commands,
435 check_lines,
436 breakpoint_lines
437 } = self.parse_debugger_commands("gdb");
438 let mut cmds = commands.join("\n");
439
440 // compile test file (it should have 'compile-flags:-g' in the header)
441 let compiler_run_result = self.compile_test();
442 if !compiler_run_result.status.success() {
443 self.fatal_proc_rec("compilation failed!", &compiler_run_result);
444 }
445
446 let exe_file = self.make_exe_name();
447
448 let debugger_run_result;
449 match &*self.config.target {
450 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
451
452 cmds = cmds.replace("run", "continue");
453
454 let tool_path = match self.config.android_cross_path.to_str() {
455 Some(x) => x.to_owned(),
456 None => self.fatal("cannot find android cross path")
457 };
458
459 // write debugger script
460 let mut script_str = String::with_capacity(2048);
461 script_str.push_str(&format!("set charset {}\n", Self::charset()));
462 script_str.push_str(&format!("set sysroot {}\n", tool_path));
463 script_str.push_str(&format!("file {}\n", exe_file.to_str().unwrap()));
464 script_str.push_str("target remote :5039\n");
465 script_str.push_str(&format!("set solib-search-path \
466 ./{}/stage2/lib/rustlib/{}/lib/\n",
467 self.config.host, self.config.target));
468 for line in &breakpoint_lines {
469 script_str.push_str(&format!("break {:?}:{}\n",
470 self.testpaths.file.file_name()
471 .unwrap()
472 .to_string_lossy(),
473 *line)[..]);
474 }
475 script_str.push_str(&cmds);
476 script_str.push_str("\nquit\n");
477
478 debug!("script_str = {}", script_str);
479 self.dump_output_file(&script_str, "debugger.script");
480
481
482 procsrv::run("",
483 &self.config.adb_path,
484 None,
485 &[
486 "push".to_owned(),
487 exe_file.to_str().unwrap().to_owned(),
488 self.config.adb_test_dir.clone()
489 ],
490 vec!(("".to_owned(), "".to_owned())),
491 Some("".to_owned()))
492 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
493
494 procsrv::run("",
495 &self.config.adb_path,
496 None,
497 &[
498 "forward".to_owned(),
499 "tcp:5039".to_owned(),
500 "tcp:5039".to_owned()
501 ],
502 vec!(("".to_owned(), "".to_owned())),
503 Some("".to_owned()))
504 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
505
506 let adb_arg = format!("export LD_LIBRARY_PATH={}; \
507 gdbserver{} :5039 {}/{}",
508 self.config.adb_test_dir.clone(),
509 if self.config.target.contains("aarch64")
510 {"64"} else {""},
511 self.config.adb_test_dir.clone(),
512 exe_file.file_name().unwrap().to_str()
513 .unwrap());
514
515 let mut process = procsrv::run_background("",
516 &self.config.adb_path
517 ,
518 None,
519 &[
520 "shell".to_owned(),
521 adb_arg.clone()
522 ],
523 vec!(("".to_owned(),
524 "".to_owned())),
525 Some("".to_owned()))
526 .expect(&format!("failed to exec `{:?}`", self.config.adb_path));
527 loop {
528 //waiting 1 second for gdbserver start
529 ::std::thread::sleep(::std::time::Duration::new(1,0));
530 if TcpStream::connect("127.0.0.1:5039").is_ok() {
531 break
532 }
533 }
534
535 let debugger_script = self.make_out_name("debugger.script");
536 // FIXME (#9639): This needs to handle non-utf8 paths
537 let debugger_opts =
538 vec!("-quiet".to_owned(),
539 "-batch".to_owned(),
540 "-nx".to_owned(),
541 format!("-command={}", debugger_script.to_str().unwrap()));
542
543 let mut gdb_path = tool_path;
544 gdb_path.push_str(&format!("/bin/{}-gdb", self.config.target));
545 let procsrv::Result {
546 out,
547 err,
548 status
549 } = procsrv::run("",
550 &gdb_path,
551 None,
552 &debugger_opts,
553 vec!(("".to_owned(), "".to_owned())),
554 None)
555 .expect(&format!("failed to exec `{:?}`", gdb_path));
556 let cmdline = {
557 let cmdline = self.make_cmdline("",
558 &format!("{}-gdb", self.config.target),
559 &debugger_opts);
560 logv(self.config, format!("executing {}", cmdline));
561 cmdline
562 };
563
564 debugger_run_result = ProcRes {
565 status: Status::Normal(status),
566 stdout: out,
567 stderr: err,
568 cmdline: cmdline
569 };
570 if process.kill().is_err() {
571 println!("Adb process is already finished.");
572 }
573 }
574
575 _=> {
576 let rust_src_root = self.find_rust_src_root()
577 .expect("Could not find Rust source root");
578 let rust_pp_module_rel_path = Path::new("./src/etc");
579 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
580 .to_str()
581 .unwrap()
582 .to_owned();
583 // write debugger script
584 let mut script_str = String::with_capacity(2048);
585 script_str.push_str(&format!("set charset {}\n", Self::charset()));
586 script_str.push_str("show version\n");
587
588 match self.config.gdb_version {
589 Some(ref version) => {
590 println!("NOTE: compiletest thinks it is using GDB version {}",
591 version);
592
593 if header::gdb_version_to_int(version) >
594 header::gdb_version_to_int("7.4") {
595 // Add the directory containing the pretty printers to
596 // GDB's script auto loading safe path
597 script_str.push_str(
598 &format!("add-auto-load-safe-path {}\n",
599 rust_pp_module_abs_path.replace(r"\", r"\\"))
600 );
601 }
602 }
603 _ => {
604 println!("NOTE: compiletest does not know which version of \
605 GDB it is using");
606 }
607 }
608
609 // The following line actually doesn't have to do anything with
610 // pretty printing, it just tells GDB to print values on one line:
611 script_str.push_str("set print pretty off\n");
612
613 // Add the pretty printer directory to GDB's source-file search path
614 script_str.push_str(&format!("directory {}\n",
615 rust_pp_module_abs_path));
616
617 // Load the target executable
618 script_str.push_str(&format!("file {}\n",
619 exe_file.to_str().unwrap()
620 .replace(r"\", r"\\")));
621
622 // Add line breakpoints
623 for line in &breakpoint_lines {
624 script_str.push_str(&format!("break '{}':{}\n",
625 self.testpaths.file.file_name().unwrap()
626 .to_string_lossy(),
627 *line));
628 }
629
630 script_str.push_str(&cmds);
631 script_str.push_str("\nquit\n");
632
633 debug!("script_str = {}", script_str);
634 self.dump_output_file(&script_str, "debugger.script");
635
636 // run debugger script with gdb
637 fn debugger() -> &'static str {
638 if cfg!(windows) {"gdb.exe"} else {"gdb"}
639 }
640
641 let debugger_script = self.make_out_name("debugger.script");
642
643 // FIXME (#9639): This needs to handle non-utf8 paths
644 let debugger_opts =
645 vec!("-quiet".to_owned(),
646 "-batch".to_owned(),
647 "-nx".to_owned(),
648 format!("-command={}", debugger_script.to_str().unwrap()));
649
650 let proc_args = ProcArgs {
651 prog: debugger().to_owned(),
652 args: debugger_opts,
653 };
654
655 let environment = vec![("PYTHONPATH".to_owned(), rust_pp_module_abs_path)];
656
657 debugger_run_result =
658 self.compose_and_run(proc_args,
659 environment,
660 self.config.run_lib_path.to_str().unwrap(),
661 None,
662 None);
663 }
664 }
665
666 if !debugger_run_result.status.success() {
667 self.fatal("gdb failed to execute");
668 }
669
670 self.check_debugger_output(&debugger_run_result, &check_lines);
671 }
672
673 fn find_rust_src_root(&self) -> Option<PathBuf> {
674 let mut path = self.config.src_base.clone();
675 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
676
677 while path.pop() {
678 if path.join(&path_postfix).is_file() {
679 return Some(path);
680 }
681 }
682
683 return None;
684 }
685
686 fn run_debuginfo_lldb_test(&self) {
687 assert!(self.revision.is_none(), "revisions not relevant here");
688
689 if self.config.lldb_python_dir.is_none() {
690 self.fatal("Can't run LLDB test because LLDB's python path is not set.");
691 }
692
693 let config = Config {
694 target_rustcflags: self.cleanup_debug_info_options(&self.config.target_rustcflags),
695 host_rustcflags: self.cleanup_debug_info_options(&self.config.host_rustcflags),
696 .. self.config.clone()
697 };
698
699
700 let test_cx = TestCx {
701 config: &config,
702 ..*self
703 };
704
705 test_cx.run_debuginfo_lldb_test_no_opt();
706 }
707
708 fn run_debuginfo_lldb_test_no_opt(&self) {
709 // compile test file (it should have 'compile-flags:-g' in the header)
710 let compile_result = self.compile_test();
711 if !compile_result.status.success() {
712 self.fatal_proc_rec("compilation failed!", &compile_result);
713 }
714
715 let exe_file = self.make_exe_name();
716
717 match self.config.lldb_version {
718 Some(ref version) => {
719 println!("NOTE: compiletest thinks it is using LLDB version {}",
720 version);
721 }
722 _ => {
723 println!("NOTE: compiletest does not know which version of \
724 LLDB it is using");
725 }
726 }
727
728 // Parse debugger commands etc from test files
729 let DebuggerCommands {
730 commands,
731 check_lines,
732 breakpoint_lines,
733 ..
734 } = self.parse_debugger_commands("lldb");
735
736 // Write debugger script:
737 // We don't want to hang when calling `quit` while the process is still running
738 let mut script_str = String::from("settings set auto-confirm true\n");
739
740 // Make LLDB emit its version, so we have it documented in the test output
741 script_str.push_str("version\n");
742
743 // Switch LLDB into "Rust mode"
744 let rust_src_root = self.find_rust_src_root().expect("Could not find Rust source root");
745 let rust_pp_module_rel_path = Path::new("./src/etc/lldb_rust_formatters.py");
746 let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path)
747 .to_str()
748 .unwrap()
749 .to_owned();
750
751 script_str.push_str(&format!("command script import {}\n",
752 &rust_pp_module_abs_path[..])[..]);
753 script_str.push_str("type summary add --no-value ");
754 script_str.push_str("--python-function lldb_rust_formatters.print_val ");
755 script_str.push_str("-x \".*\" --category Rust\n");
756 script_str.push_str("type category enable Rust\n");
757
758 // Set breakpoints on every line that contains the string "#break"
759 let source_file_name = self.testpaths.file.file_name().unwrap().to_string_lossy();
760 for line in &breakpoint_lines {
761 script_str.push_str(&format!("breakpoint set --file '{}' --line {}\n",
762 source_file_name,
763 line));
764 }
765
766 // Append the other commands
767 for line in &commands {
768 script_str.push_str(line);
769 script_str.push_str("\n");
770 }
771
772 // Finally, quit the debugger
773 script_str.push_str("\nquit\n");
774
775 // Write the script into a file
776 debug!("script_str = {}", script_str);
777 self.dump_output_file(&script_str, "debugger.script");
778 let debugger_script = self.make_out_name("debugger.script");
779
780 // Let LLDB execute the script via lldb_batchmode.py
781 let debugger_run_result = self.run_lldb(&exe_file,
782 &debugger_script,
783 &rust_src_root);
784
785 if !debugger_run_result.status.success() {
786 self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
787 }
788
789 self.check_debugger_output(&debugger_run_result, &check_lines);
790 }
791
792 fn run_lldb(&self,
793 test_executable: &Path,
794 debugger_script: &Path,
795 rust_src_root: &Path)
796 -> ProcRes {
797 // Prepare the lldb_batchmode which executes the debugger script
798 let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
799 self.cmd2procres(Command::new(&self.config.lldb_python)
800 .arg(&lldb_script_path)
801 .arg(test_executable)
802 .arg(debugger_script)
803 .env("PYTHONPATH",
804 self.config.lldb_python_dir.as_ref().unwrap()))
805 }
806
807 fn cmd2procres(&self, cmd: &mut Command) -> ProcRes {
808 let (status, out, err) = match cmd.output() {
809 Ok(Output { status, stdout, stderr }) => {
810 (status,
811 String::from_utf8(stdout).unwrap(),
812 String::from_utf8(stderr).unwrap())
813 },
814 Err(e) => {
815 self.fatal(&format!("Failed to setup Python process for \
816 LLDB script: {}", e))
817 }
818 };
819
820 self.dump_output(&out, &err);
821 ProcRes {
822 status: Status::Normal(status),
823 stdout: out,
824 stderr: err,
825 cmdline: format!("{:?}", cmd)
826 }
827 }
828
829 fn parse_debugger_commands(&self, debugger_prefix: &str) -> DebuggerCommands {
830 let command_directive = format!("{}-command", debugger_prefix);
831 let check_directive = format!("{}-check", debugger_prefix);
832
833 let mut breakpoint_lines = vec!();
834 let mut commands = vec!();
835 let mut check_lines = vec!();
836 let mut counter = 1;
837 let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
838 for line in reader.lines() {
839 match line {
840 Ok(line) => {
841 if line.contains("#break") {
842 breakpoint_lines.push(counter);
843 }
844
845 header::parse_name_value_directive(
846 &line,
847 &command_directive).map(|cmd| {
848 commands.push(cmd)
849 });
850
851 header::parse_name_value_directive(
852 &line,
853 &check_directive).map(|cmd| {
854 check_lines.push(cmd)
855 });
856 }
857 Err(e) => {
858 self.fatal(&format!("Error while parsing debugger commands: {}", e))
859 }
860 }
861 counter += 1;
862 }
863
864 DebuggerCommands {
865 commands: commands,
866 check_lines: check_lines,
867 breakpoint_lines: breakpoint_lines,
868 }
869 }
870
871 fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
872 if options.is_none() {
873 return None;
874 }
875
876 // Remove options that are either unwanted (-O) or may lead to duplicates due to RUSTFLAGS.
877 let options_to_remove = [
878 "-O".to_owned(),
879 "-g".to_owned(),
880 "--debuginfo".to_owned()
881 ];
882 let new_options =
883 self.split_maybe_args(options).into_iter()
884 .filter(|x| !options_to_remove.contains(x))
885 .collect::<Vec<String>>();
886
887 Some(new_options.join(" "))
888 }
889
890 fn check_debugger_output(&self, debugger_run_result: &ProcRes, check_lines: &[String]) {
891 let num_check_lines = check_lines.len();
892
893 let mut check_line_index = 0;
894 for line in debugger_run_result.stdout.lines() {
895 if check_line_index >= num_check_lines {
896 break;
897 }
898
899 if check_single_line(line, &(check_lines[check_line_index])[..]) {
900 check_line_index += 1;
901 }
902 }
903 if check_line_index != num_check_lines && num_check_lines > 0 {
904 self.fatal_proc_rec(&format!("line not found in debugger output: {}",
905 check_lines[check_line_index]),
906 debugger_run_result);
907 }
908
909 fn check_single_line(line: &str, check_line: &str) -> bool {
910 // Allow check lines to leave parts unspecified (e.g., uninitialized
911 // bits in the wrong case of an enum) with the notation "[...]".
912 let line = line.trim();
913 let check_line = check_line.trim();
914 let can_start_anywhere = check_line.starts_with("[...]");
915 let can_end_anywhere = check_line.ends_with("[...]");
916
917 let check_fragments: Vec<&str> = check_line.split("[...]")
918 .filter(|frag| !frag.is_empty())
919 .collect();
920 if check_fragments.is_empty() {
921 return true;
922 }
923
924 let (mut rest, first_fragment) = if can_start_anywhere {
925 match line.find(check_fragments[0]) {
926 Some(pos) => (&line[pos + check_fragments[0].len() ..], 1),
927 None => return false
928 }
929 } else {
930 (line, 0)
931 };
932
933 for fragment_index in first_fragment .. check_fragments.len() {
934 let current_fragment = check_fragments[fragment_index];
935 match rest.find(current_fragment) {
936 Some(pos) => {
937 rest = &rest[pos + current_fragment.len() .. ];
938 }
939 None => return false
940 }
941 }
942
943 if !can_end_anywhere && !rest.is_empty() {
944 return false;
945 }
946
947 return true;
948 }
949 }
950
951 fn check_error_patterns(&self,
952 output_to_check: &str,
953 proc_res: &ProcRes) {
954 if self.props.error_patterns.is_empty() {
9e0c209e
SL
955 if self.props.must_compile_successfully {
956 return
957 } else {
958 self.fatal(&format!("no error pattern specified in {:?}",
959 self.testpaths.file.display()));
960 }
a7813a04
XL
961 }
962 let mut next_err_idx = 0;
963 let mut next_err_pat = self.props.error_patterns[next_err_idx].trim();
964 let mut done = false;
965 for line in output_to_check.lines() {
966 if line.contains(next_err_pat) {
967 debug!("found error pattern {}", next_err_pat);
968 next_err_idx += 1;
969 if next_err_idx == self.props.error_patterns.len() {
970 debug!("found all error patterns");
971 done = true;
972 break;
973 }
974 next_err_pat = self.props.error_patterns[next_err_idx].trim();
975 }
976 }
977 if done { return; }
978
979 let missing_patterns = &self.props.error_patterns[next_err_idx..];
980 if missing_patterns.len() == 1 {
981 self.fatal_proc_rec(
982 &format!("error pattern '{}' not found!", missing_patterns[0]),
983 proc_res);
984 } else {
985 for pattern in missing_patterns {
986 self.error(&format!("error pattern '{}' not found!", *pattern));
987 }
988 self.fatal_proc_rec("multiple error patterns not found", proc_res);
989 }
990 }
991
992 fn check_no_compiler_crash(&self, proc_res: &ProcRes) {
993 for line in proc_res.stderr.lines() {
9e0c209e 994 if line.contains("error: internal compiler error") {
a7813a04
XL
995 self.fatal_proc_rec("compiler encountered internal error", proc_res);
996 }
997 }
998 }
999
1000 fn check_forbid_output(&self,
1001 output_to_check: &str,
1002 proc_res: &ProcRes) {
1003 for pat in &self.props.forbid_output {
1004 if output_to_check.contains(pat) {
1005 self.fatal_proc_rec("forbidden pattern found in compiler output", proc_res);
1006 }
1007 }
1008 }
1009
1010 fn check_expected_errors(&self,
1011 expected_errors: Vec<errors::Error>,
1012 proc_res: &ProcRes) {
9e0c209e
SL
1013 if proc_res.status.success() &&
1014 expected_errors.iter().any(|x| x.kind == Some(ErrorKind::Error)) {
a7813a04
XL
1015 self.fatal_proc_rec("process did not return an error status", proc_res);
1016 }
1017
1018 let file_name =
1019 format!("{}", self.testpaths.file.display())
1020 .replace(r"\", "/"); // on windows, translate all '\' path separators to '/'
1021
1022 // If the testcase being checked contains at least one expected "help"
1023 // message, then we'll ensure that all "help" messages are expected.
1024 // Otherwise, all "help" messages reported by the compiler will be ignored.
1025 // This logic also applies to "note" messages.
1026 let expect_help = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Help));
1027 let expect_note = expected_errors.iter().any(|ee| ee.kind == Some(ErrorKind::Note));
1028
1029 // Parse the JSON output from the compiler and extract out the messages.
1030 let actual_errors = json::parse_output(&file_name, &proc_res.stderr, &proc_res);
3157f602 1031 let mut unexpected = Vec::new();
a7813a04
XL
1032 let mut found = vec![false; expected_errors.len()];
1033 for actual_error in &actual_errors {
1034 let opt_index =
1035 expected_errors
1036 .iter()
1037 .enumerate()
1038 .position(|(index, expected_error)| {
1039 !found[index] &&
1040 actual_error.line_num == expected_error.line_num &&
1041 (expected_error.kind.is_none() ||
1042 actual_error.kind == expected_error.kind) &&
1043 actual_error.msg.contains(&expected_error.msg)
1044 });
1045
1046 match opt_index {
1047 Some(index) => {
1048 // found a match, everybody is happy
1049 assert!(!found[index]);
1050 found[index] = true;
1051 }
1052
1053 None => {
1054 if self.is_unexpected_compiler_message(actual_error, expect_help, expect_note) {
1055 self.error(
1056 &format!("{}:{}: unexpected {:?}: '{}'",
1057 file_name,
1058 actual_error.line_num,
1059 actual_error.kind.as_ref()
1060 .map_or(String::from("message"),
1061 |k| k.to_string()),
1062 actual_error.msg));
3157f602 1063 unexpected.push(actual_error.clone());
a7813a04
XL
1064 }
1065 }
1066 }
1067 }
1068
3157f602 1069 let mut not_found = Vec::new();
a7813a04
XL
1070 // anything not yet found is a problem
1071 for (index, expected_error) in expected_errors.iter().enumerate() {
1072 if !found[index] {
1073 self.error(
1074 &format!("{}:{}: expected {} not found: {}",
1075 file_name,
1076 expected_error.line_num,
1077 expected_error.kind.as_ref()
1078 .map_or("message".into(),
1079 |k| k.to_string()),
1080 expected_error.msg));
3157f602 1081 not_found.push(expected_error.clone());
a7813a04
XL
1082 }
1083 }
1084
3157f602 1085 if unexpected.len() > 0 || not_found.len() > 0 {
a7813a04
XL
1086 self.error(
1087 &format!("{} unexpected errors found, {} expected errors not found",
3157f602 1088 unexpected.len(), not_found.len()));
a7813a04
XL
1089 print!("status: {}\ncommand: {}\n",
1090 proc_res.status, proc_res.cmdline);
3157f602
XL
1091 if unexpected.len() > 0 {
1092 println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1093 }
1094 if not_found.len() > 0 {
1095 println!("not found errors (from test file): {:#?}\n", not_found);
1096 }
a7813a04
XL
1097 panic!();
1098 }
1099 }
1100
1101 /// Returns true if we should report an error about `actual_error`,
1102 /// which did not match any of the expected error. We always require
1103 /// errors/warnings to be explicitly listed, but only require
1104 /// helps/notes if there are explicit helps/notes given.
1105 fn is_unexpected_compiler_message(&self,
1106 actual_error: &Error,
1107 expect_help: bool,
1108 expect_note: bool)
1109 -> bool {
1110 match actual_error.kind {
1111 Some(ErrorKind::Help) => expect_help,
1112 Some(ErrorKind::Note) => expect_note,
1113 Some(ErrorKind::Error) => true,
1114 Some(ErrorKind::Warning) => true,
1115 Some(ErrorKind::Suggestion) => false,
1116 None => false
1117 }
1118 }
1119
1120 fn compile_test(&self) -> ProcRes {
1121 let aux_dir = self.aux_output_dir_name();
1122 // FIXME (#9639): This needs to handle non-utf8 paths
1123 let link_args = vec!("-L".to_owned(),
1124 aux_dir.to_str().unwrap().to_owned());
1125 let args = self.make_compile_args(link_args,
1126 &self.testpaths.file,
1127 TargetLocation::ThisFile(self.make_exe_name()));
1128 self.compose_and_run_compiler(args, None)
1129 }
1130
1131 fn document(&self, out_dir: &Path) -> ProcRes {
1132 if self.props.build_aux_docs {
1133 for rel_ab in &self.props.aux_builds {
1134 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1135 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1136 let aux_cx = TestCx {
1137 config: self.config,
1138 props: &aux_props,
1139 testpaths: &aux_testpaths,
1140 revision: self.revision
1141 };
1142 let auxres = aux_cx.document(out_dir);
1143 if !auxres.status.success() {
1144 return auxres;
1145 }
1146 }
1147 }
1148
1149 let aux_dir = self.aux_output_dir_name();
1150 let mut args = vec!["-L".to_owned(),
1151 aux_dir.to_str().unwrap().to_owned(),
1152 "-o".to_owned(),
1153 out_dir.to_str().unwrap().to_owned(),
1154 self.testpaths.file.to_str().unwrap().to_owned()];
1155 args.extend(self.props.compile_flags.iter().cloned());
1156 let args = ProcArgs {
1157 prog: self.config.rustdoc_path.to_str().unwrap().to_owned(),
1158 args: args,
1159 };
1160 self.compose_and_run_compiler(args, None)
1161 }
1162
1163 fn exec_compiled_test(&self) -> ProcRes {
1164 let env = self.props.exec_env.clone();
1165
1166 match &*self.config.target {
1167
1168 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1169 self._arm_exec_compiled_test(env)
1170 }
1171
1172 _=> {
1173 let aux_dir = self.aux_output_dir_name();
1174 self.compose_and_run(self.make_run_args(),
1175 env,
1176 self.config.run_lib_path.to_str().unwrap(),
1177 Some(aux_dir.to_str().unwrap()),
1178 None)
1179 }
1180 }
1181 }
1182
1183 /// For each `aux-build: foo/bar` annotation, we check to find the
1184 /// file in a `aux` directory relative to the test itself.
1185 fn compute_aux_test_paths(&self, rel_ab: &str) -> TestPaths {
1186 let test_ab = self.testpaths.file
1187 .parent()
1188 .expect("test file path has no parent")
1189 .join("auxiliary")
1190 .join(rel_ab);
1191 if !test_ab.exists() {
1192 self.fatal(&format!("aux-build `{}` source not found", test_ab.display()))
1193 }
1194
1195 TestPaths {
1196 file: test_ab,
1197 base: self.testpaths.base.clone(),
1198 relative_dir: self.testpaths.relative_dir
1199 .join("auxiliary")
1200 .join(rel_ab)
1201 .parent()
1202 .expect("aux-build path has no parent")
1203 .to_path_buf()
1204 }
1205 }
1206
1207 fn compose_and_run_compiler(&self, args: ProcArgs, input: Option<String>) -> ProcRes {
1208 if !self.props.aux_builds.is_empty() {
1209 self.create_dir_racy(&self.aux_output_dir_name());
1210 }
1211
1212 let aux_dir = self.aux_output_dir_name();
1213 // FIXME (#9639): This needs to handle non-utf8 paths
1214 let extra_link_args = vec!["-L".to_owned(),
1215 aux_dir.to_str().unwrap().to_owned()];
1216
1217 for rel_ab in &self.props.aux_builds {
1218 let aux_testpaths = self.compute_aux_test_paths(rel_ab);
1219 let aux_props = self.props.from_aux_file(&aux_testpaths.file, self.revision);
1220 let mut crate_type = if aux_props.no_prefer_dynamic {
1221 Vec::new()
1222 } else {
1223 // We primarily compile all auxiliary libraries as dynamic libraries
1224 // to avoid code size bloat and large binaries as much as possible
1225 // for the test suite (otherwise including libstd statically in all
1226 // executables takes up quite a bit of space).
1227 //
1228 // For targets like MUSL or Emscripten, however, there is no support for
1229 // dynamic libraries so we just go back to building a normal library. Note,
1230 // however, that for MUSL if the library is built with `force_host` then
1231 // it's ok to be a dylib as the host should always support dylibs.
1232 if (self.config.target.contains("musl") && !aux_props.force_host) ||
1233 self.config.target.contains("emscripten")
1234 {
1235 vec!("--crate-type=lib".to_owned())
1236 } else {
1237 vec!("--crate-type=dylib".to_owned())
1238 }
1239 };
1240 crate_type.extend(extra_link_args.clone());
1241 let aux_output = {
1242 let f = self.make_lib_name(&self.testpaths.file);
1243 let parent = f.parent().unwrap();
1244 TargetLocation::ThisDirectory(parent.to_path_buf())
1245 };
1246 let aux_cx = TestCx {
1247 config: self.config,
1248 props: &aux_props,
1249 testpaths: &aux_testpaths,
1250 revision: self.revision
1251 };
1252 let aux_args = aux_cx.make_compile_args(crate_type, &aux_testpaths.file, aux_output);
1253 let auxres = aux_cx.compose_and_run(aux_args,
1254 Vec::new(),
1255 aux_cx.config.compile_lib_path.to_str().unwrap(),
1256 Some(aux_dir.to_str().unwrap()),
1257 None);
1258 if !auxres.status.success() {
1259 self.fatal_proc_rec(
1260 &format!("auxiliary build of {:?} failed to compile: ",
1261 aux_testpaths.file.display()),
1262 &auxres);
1263 }
1264
1265 match &*self.config.target {
1266 "arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
1267 self._arm_push_aux_shared_library();
1268 }
1269 _ => {}
1270 }
1271 }
1272
1273 self.compose_and_run(args,
1274 self.props.rustc_env.clone(),
1275 self.config.compile_lib_path.to_str().unwrap(),
1276 Some(aux_dir.to_str().unwrap()),
1277 input)
1278 }
1279
1280 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
1281 // threads or processes.
1282 fn create_dir_racy(&self, path: &Path) {
1283 match fs::create_dir(path) {
1284 Ok(()) => return,
1285 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return,
1286 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
1287 Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1288 }
1289 self.create_dir_racy(path.parent().unwrap());
1290 match fs::create_dir(path) {
1291 Ok(()) => {}
1292 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => {}
1293 Err(e) => panic!("failed to create dir {:?}: {}", path, e),
1294 }
1295 }
1296
1297 fn compose_and_run(&self,
1298 ProcArgs{ args, prog }: ProcArgs,
1299 procenv: Vec<(String, String)> ,
1300 lib_path: &str,
1301 aux_path: Option<&str>,
1302 input: Option<String>) -> ProcRes {
1303 return self.program_output(lib_path, prog, aux_path, args, procenv, input);
1304 }
1305
1306 fn make_compile_args(&self,
1307 extras: Vec<String> ,
1308 input_file: &Path,
1309 output_file: TargetLocation)
1310 -> ProcArgs
1311 {
1312 let target = if self.props.force_host {
1313 &*self.config.host
1314 } else {
1315 &*self.config.target
1316 };
1317
1318 // FIXME (#9639): This needs to handle non-utf8 paths
1319 let mut args = vec!(input_file.to_str().unwrap().to_owned(),
1320 "-L".to_owned(),
1321 self.config.build_base.to_str().unwrap().to_owned(),
1322 format!("--target={}", target));
1323
1324 if let Some(revision) = self.revision {
1325 args.extend(vec![
1326 format!("--cfg"),
1327 format!("{}", revision),
1328 ]);
1329 }
1330
1331 if let Some(ref incremental_dir) = self.props.incremental_dir {
1332 args.extend(vec![
1333 format!("-Z"),
1334 format!("incremental={}", incremental_dir.display()),
1335 ]);
1336 }
1337
1338
1339 match self.config.mode {
1340 CompileFail |
1341 ParseFail |
9e0c209e 1342 RunPass |
a7813a04
XL
1343 Incremental => {
1344 // If we are extracting and matching errors in the new
1345 // fashion, then you want JSON mode. Old-skool error
1346 // patterns still match the raw compiler output.
1347 if self.props.error_patterns.is_empty() {
1348 args.extend(["--error-format",
5bcae85e 1349 "json"]
a7813a04
XL
1350 .iter()
1351 .map(|s| s.to_string()));
1352 }
1353 }
5bcae85e
SL
1354 MirOpt => {
1355 args.extend(["-Z",
1356 "dump-mir=all",
1357 "-Z",
1358 "mir-opt-level=3",
1359 "-Z"]
1360 .iter()
1361 .map(|s| s.to_string()));
1362
1363
1364 let mir_dump_dir = self.get_mir_dump_dir();
1365 self.create_dir_racy(mir_dump_dir.as_path());
1366 let mut dir_opt = "dump-mir-dir=".to_string();
1367 dir_opt.push_str(mir_dump_dir.to_str().unwrap());
1368 debug!("dir_opt: {:?}", dir_opt);
1369
1370 args.push(dir_opt);
1371 }
a7813a04 1372 RunFail |
a7813a04
XL
1373 RunPassValgrind |
1374 Pretty |
1375 DebugInfoGdb |
1376 DebugInfoLldb |
1377 Codegen |
1378 Rustdoc |
1379 RunMake |
1380 Ui |
1381 CodegenUnits => {
1382 // do not use JSON output
1383 }
1384 }
1385
1386 args.extend_from_slice(&extras);
1387 if !self.props.no_prefer_dynamic {
1388 args.push("-C".to_owned());
1389 args.push("prefer-dynamic".to_owned());
1390 }
1391 let path = match output_file {
1392 TargetLocation::ThisFile(path) => {
1393 args.push("-o".to_owned());
1394 path
1395 }
1396 TargetLocation::ThisDirectory(path) => {
1397 args.push("--out-dir".to_owned());
1398 path
1399 }
1400 };
1401 args.push(path.to_str().unwrap().to_owned());
1402 if self.props.force_host {
1403 args.extend(self.split_maybe_args(&self.config.host_rustcflags));
1404 } else {
1405 args.extend(self.split_maybe_args(&self.config.target_rustcflags));
1406 }
1407 args.extend(self.props.compile_flags.iter().cloned());
1408 return ProcArgs {
1409 prog: self.config.rustc_path.to_str().unwrap().to_owned(),
1410 args: args,
1411 };
1412 }
1413
1414 fn make_lib_name(&self, auxfile: &Path) -> PathBuf {
1415 // what we return here is not particularly important, as it
1416 // happens; rustc ignores everything except for the directory.
1417 let auxname = self.output_testname(auxfile);
1418 self.aux_output_dir_name().join(&auxname)
1419 }
1420
1421 fn make_exe_name(&self) -> PathBuf {
1422 let mut f = self.output_base_name();
1423 // FIXME: This is using the host architecture exe suffix, not target!
1424 if self.config.target == "asmjs-unknown-emscripten" {
1425 let mut fname = f.file_name().unwrap().to_os_string();
1426 fname.push(".js");
1427 f.set_file_name(&fname);
1428 } else if !env::consts::EXE_SUFFIX.is_empty() {
1429 let mut fname = f.file_name().unwrap().to_os_string();
1430 fname.push(env::consts::EXE_SUFFIX);
1431 f.set_file_name(&fname);
1432 }
1433 f
1434 }
1435
1436 fn make_run_args(&self) -> ProcArgs {
1437 // If we've got another tool to run under (valgrind),
1438 // then split apart its command
1439 let mut args = self.split_maybe_args(&self.config.runtool);
1440
1441 // If this is emscripten, then run tests under nodejs
1442 if self.config.target == "asmjs-unknown-emscripten" {
1443 args.push("nodejs".to_owned());
1444 }
1445
1446 let exe_file = self.make_exe_name();
1447
1448 // FIXME (#9639): This needs to handle non-utf8 paths
1449 args.push(exe_file.to_str().unwrap().to_owned());
1450
1451 // Add the arguments in the run_flags directive
1452 args.extend(self.split_maybe_args(&self.props.run_flags));
1453
1454 let prog = args.remove(0);
1455 return ProcArgs {
1456 prog: prog,
1457 args: args,
1458 };
1459 }
1460
1461 fn split_maybe_args(&self, argstr: &Option<String>) -> Vec<String> {
1462 match *argstr {
1463 Some(ref s) => {
1464 s
1465 .split(' ')
1466 .filter_map(|s| {
1467 if s.chars().all(|c| c.is_whitespace()) {
1468 None
1469 } else {
1470 Some(s.to_owned())
1471 }
1472 }).collect()
1473 }
1474 None => Vec::new()
1475 }
1476 }
1477
1478 fn program_output(&self,
1479 lib_path: &str,
1480 prog: String,
1481 aux_path: Option<&str>,
1482 args: Vec<String>,
1483 env: Vec<(String, String)>,
1484 input: Option<String>)
1485 -> ProcRes {
1486 let cmdline =
1487 {
1488 let cmdline = self.make_cmdline(lib_path,
1489 &prog,
1490 &args);
1491 logv(self.config, format!("executing {}", cmdline));
1492 cmdline
1493 };
1494 let procsrv::Result {
1495 out,
1496 err,
1497 status
1498 } = procsrv::run(lib_path,
1499 &prog,
1500 aux_path,
1501 &args,
1502 env,
1503 input).expect(&format!("failed to exec `{}`", prog));
1504 self.dump_output(&out, &err);
1505 return ProcRes {
1506 status: Status::Normal(status),
1507 stdout: out,
1508 stderr: err,
1509 cmdline: cmdline,
1510 };
1511 }
1512
1513 fn make_cmdline(&self, libpath: &str, prog: &str, args: &[String]) -> String {
1514 use util;
1515
1516 // Linux and mac don't require adjusting the library search path
1517 if cfg!(unix) {
1518 format!("{} {}", prog, args.join(" "))
1519 } else {
1520 // Build the LD_LIBRARY_PATH variable as it would be seen on the command line
1521 // for diagnostic purposes
1522 fn lib_path_cmd_prefix(path: &str) -> String {
1523 format!("{}=\"{}\"", util::lib_path_env_var(), util::make_new_path(path))
1524 }
1525
1526 format!("{} {} {}", lib_path_cmd_prefix(libpath), prog, args.join(" "))
1527 }
1528 }
1529
1530 fn dump_output(&self, out: &str, err: &str) {
1531 self.dump_output_file(out, "out");
1532 self.dump_output_file(err, "err");
1533 self.maybe_dump_to_stdout(out, err);
1534 }
1535
1536 fn dump_output_file(&self,
1537 out: &str,
1538 extension: &str) {
1539 let outfile = self.make_out_name(extension);
1540 File::create(&outfile).unwrap().write_all(out.as_bytes()).unwrap();
1541 }
1542
1543 fn make_out_name(&self, extension: &str) -> PathBuf {
1544 self.output_base_name().with_extension(extension)
1545 }
1546
1547 fn aux_output_dir_name(&self) -> PathBuf {
1548 let f = self.output_base_name();
1549 let mut fname = f.file_name().unwrap().to_os_string();
1550 fname.push(&format!(".{}.libaux", self.config.mode));
1551 f.with_file_name(&fname)
1552 }
1553
1554 fn output_testname(&self, filepath: &Path) -> PathBuf {
1555 PathBuf::from(filepath.file_stem().unwrap())
1556 }
1557
1558 /// Given a test path like `compile-fail/foo/bar.rs` Returns a name like
1559 ///
1560 /// <output>/foo/bar-stage1
1561 fn output_base_name(&self) -> PathBuf {
1562 let dir = self.config.build_base.join(&self.testpaths.relative_dir);
1563
1564 // Note: The directory `dir` is created during `collect_tests_from_dir`
1565 dir
1566 .join(&self.output_testname(&self.testpaths.file))
1567 .with_extension(&self.config.stage_id)
1568 }
1569
1570 fn maybe_dump_to_stdout(&self, out: &str, err: &str) {
1571 if self.config.verbose {
1572 println!("------{}------------------------------", "stdout");
1573 println!("{}", out);
1574 println!("------{}------------------------------", "stderr");
1575 println!("{}", err);
1576 println!("------------------------------------------");
1577 }
1578 }
1579
1580 fn error(&self, err: &str) {
1581 match self.revision {
1582 Some(rev) => println!("\nerror in revision `{}`: {}", rev, err),
1583 None => println!("\nerror: {}", err)
1584 }
1585 }
1586
1587 fn fatal(&self, err: &str) -> ! {
1588 self.error(err); panic!();
1589 }
1590
1591 fn fatal_proc_rec(&self, err: &str, proc_res: &ProcRes) -> ! {
1592 self.error(err);
1593 proc_res.fatal(None);
1594 }
1595
1596 fn _arm_exec_compiled_test(&self, env: Vec<(String, String)>) -> ProcRes {
1597 let args = self.make_run_args();
1598 let cmdline = self.make_cmdline("", &args.prog, &args.args);
1599
1600 // get bare program string
1601 let mut tvec: Vec<String> = args.prog
1602 .split('/')
1603 .map(str::to_owned)
1604 .collect();
1605 let prog_short = tvec.pop().unwrap();
1606
1607 // copy to target
1608 let copy_result = procsrv::run("",
1609 &self.config.adb_path,
1610 None,
1611 &[
1612 "push".to_owned(),
1613 args.prog.clone(),
1614 self.config.adb_test_dir.clone()
1615 ],
1616 vec!(("".to_owned(), "".to_owned())),
1617 Some("".to_owned()))
1618 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1619
1620 if self.config.verbose {
1621 println!("push ({}) {} {} {}",
1622 self.config.target,
1623 args.prog,
1624 copy_result.out,
1625 copy_result.err);
1626 }
1627
1628 logv(self.config, format!("executing ({}) {}", self.config.target, cmdline));
1629
1630 let mut runargs = Vec::new();
1631
1632 // run test via adb_run_wrapper
1633 runargs.push("shell".to_owned());
1634 for (key, val) in env {
1635 runargs.push(format!("{}={}", key, val));
1636 }
1637 runargs.push(format!("{}/../adb_run_wrapper.sh", self.config.adb_test_dir));
1638 runargs.push(format!("{}", self.config.adb_test_dir));
1639 runargs.push(format!("{}", prog_short));
1640
1641 for tv in &args.args {
1642 runargs.push(tv.to_owned());
1643 }
1644 procsrv::run("",
1645 &self.config.adb_path,
1646 None,
1647 &runargs,
1648 vec!(("".to_owned(), "".to_owned())), Some("".to_owned()))
1649 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1650
1651 // get exitcode of result
1652 runargs = Vec::new();
1653 runargs.push("shell".to_owned());
1654 runargs.push("cat".to_owned());
1655 runargs.push(format!("{}/{}.exitcode", self.config.adb_test_dir, prog_short));
1656
1657 let procsrv::Result{ out: exitcode_out, err: _, status: _ } =
1658 procsrv::run("",
1659 &self.config.adb_path,
1660 None,
1661 &runargs,
1662 vec!(("".to_owned(), "".to_owned())),
1663 Some("".to_owned()))
1664 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1665
1666 let mut exitcode: i32 = 0;
1667 for c in exitcode_out.chars() {
1668 if !c.is_numeric() { break; }
1669 exitcode = exitcode * 10 + match c {
1670 '0' ... '9' => c as i32 - ('0' as i32),
1671 _ => 101,
1672 }
1673 }
1674
1675 // get stdout of result
1676 runargs = Vec::new();
1677 runargs.push("shell".to_owned());
1678 runargs.push("cat".to_owned());
1679 runargs.push(format!("{}/{}.stdout", self.config.adb_test_dir, prog_short));
1680
1681 let procsrv::Result{ out: stdout_out, err: _, status: _ } =
1682 procsrv::run("",
1683 &self.config.adb_path,
1684 None,
1685 &runargs,
1686 vec!(("".to_owned(), "".to_owned())),
1687 Some("".to_owned()))
1688 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1689
1690 // get stderr of result
1691 runargs = Vec::new();
1692 runargs.push("shell".to_owned());
1693 runargs.push("cat".to_owned());
1694 runargs.push(format!("{}/{}.stderr", self.config.adb_test_dir, prog_short));
1695
1696 let procsrv::Result{ out: stderr_out, err: _, status: _ } =
1697 procsrv::run("",
1698 &self.config.adb_path,
1699 None,
1700 &runargs,
1701 vec!(("".to_owned(), "".to_owned())),
1702 Some("".to_owned()))
1703 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1704
1705 self.dump_output(&stdout_out, &stderr_out);
1706
1707 ProcRes {
1708 status: Status::Parsed(exitcode),
1709 stdout: stdout_out,
1710 stderr: stderr_out,
1711 cmdline: cmdline
1712 }
1713 }
1714
1715 fn _arm_push_aux_shared_library(&self) {
1716 let tdir = self.aux_output_dir_name();
1717
1718 let dirs = fs::read_dir(&tdir).unwrap();
1719 for file in dirs {
1720 let file = file.unwrap().path();
1721 if file.extension().and_then(|s| s.to_str()) == Some("so") {
1722 // FIXME (#9639): This needs to handle non-utf8 paths
1723 let copy_result = procsrv::run("",
1724 &self.config.adb_path,
1725 None,
1726 &[
1727 "push".to_owned(),
1728 file.to_str()
1729 .unwrap()
1730 .to_owned(),
1731 self.config.adb_test_dir.to_owned(),
1732 ],
1733 vec!(("".to_owned(),
1734 "".to_owned())),
1735 Some("".to_owned()))
1736 .expect(&format!("failed to exec `{}`", self.config.adb_path));
1737
1738 if self.config.verbose {
1739 println!("push ({}) {:?} {} {}",
1740 self.config.target, file.display(),
1741 copy_result.out, copy_result.err);
1742 }
1743 }
1744 }
1745 }
1746
1747 // codegen tests (using FileCheck)
1748
1749 fn compile_test_and_save_ir(&self) -> ProcRes {
1750 let aux_dir = self.aux_output_dir_name();
1751 // FIXME (#9639): This needs to handle non-utf8 paths
1752 let mut link_args = vec!("-L".to_owned(),
1753 aux_dir.to_str().unwrap().to_owned());
1754 let llvm_args = vec!("--emit=llvm-ir".to_owned(),);
1755 link_args.extend(llvm_args);
1756 let args = self.make_compile_args(link_args,
1757 &self.testpaths.file,
1758 TargetLocation::ThisDirectory(
1759 self.output_base_name().parent()
1760 .unwrap()
1761 .to_path_buf()));
1762 self.compose_and_run_compiler(args, None)
1763 }
1764
1765 fn check_ir_with_filecheck(&self) -> ProcRes {
1766 let irfile = self.output_base_name().with_extension("ll");
1767 let prog = self.config.llvm_filecheck.as_ref().unwrap();
1768 let proc_args = ProcArgs {
1769 // FIXME (#9639): This needs to handle non-utf8 paths
1770 prog: prog.to_str().unwrap().to_owned(),
1771 args: vec!(format!("-input-file={}", irfile.to_str().unwrap()),
1772 self.testpaths.file.to_str().unwrap().to_owned())
1773 };
1774 self.compose_and_run(proc_args, Vec::new(), "", None, None)
1775 }
1776
1777 fn run_codegen_test(&self) {
1778 assert!(self.revision.is_none(), "revisions not relevant here");
1779
1780 if self.config.llvm_filecheck.is_none() {
1781 self.fatal("missing --llvm-filecheck");
1782 }
1783
1784 let mut proc_res = self.compile_test_and_save_ir();
1785 if !proc_res.status.success() {
1786 self.fatal_proc_rec("compilation failed!", &proc_res);
1787 }
1788
1789 proc_res = self.check_ir_with_filecheck();
1790 if !proc_res.status.success() {
1791 self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
1792 }
1793 }
1794
1795 fn charset() -> &'static str {
1796 // FreeBSD 10.1 defaults to GDB 6.1.1 which doesn't support "auto" charset
1797 if cfg!(target_os = "bitrig") {
1798 "auto"
1799 } else if cfg!(target_os = "freebsd") {
1800 "ISO-8859-1"
1801 } else {
1802 "UTF-8"
1803 }
1804 }
1805
1806 fn run_rustdoc_test(&self) {
1807 assert!(self.revision.is_none(), "revisions not relevant here");
1808
1809 let out_dir = self.output_base_name();
1810 let _ = fs::remove_dir_all(&out_dir);
1811 self.create_dir_racy(&out_dir);
1812
1813 let proc_res = self.document(&out_dir);
1814 if !proc_res.status.success() {
1815 self.fatal_proc_rec("rustdoc failed!", &proc_res);
1816 }
1817 let root = self.find_rust_src_root().unwrap();
1818
1819 let res = self.cmd2procres(Command::new(&self.config.docck_python)
1820 .arg(root.join("src/etc/htmldocck.py"))
1821 .arg(out_dir)
1822 .arg(&self.testpaths.file));
1823 if !res.status.success() {
1824 self.fatal_proc_rec("htmldocck failed!", &res);
1825 }
1826 }
1827
1828 fn run_codegen_units_test(&self) {
1829 assert!(self.revision.is_none(), "revisions not relevant here");
1830
1831 let proc_res = self.compile_test();
1832
1833 if !proc_res.status.success() {
1834 self.fatal_proc_rec("compilation failed!", &proc_res);
1835 }
1836
1837 self.check_no_compiler_crash(&proc_res);
1838
1839 const PREFIX: &'static str = "TRANS_ITEM ";
1840 const CGU_MARKER: &'static str = "@@";
1841
1842 let actual: Vec<TransItem> = proc_res
1843 .stdout
1844 .lines()
1845 .filter(|line| line.starts_with(PREFIX))
1846 .map(str_to_trans_item)
1847 .collect();
1848
1849 let expected: Vec<TransItem> = errors::load_errors(&self.testpaths.file, None)
1850 .iter()
1851 .map(|e| str_to_trans_item(&e.msg[..]))
1852 .collect();
1853
1854 let mut missing = Vec::new();
1855 let mut wrong_cgus = Vec::new();
1856
1857 for expected_item in &expected {
1858 let actual_item_with_same_name = actual.iter()
1859 .find(|ti| ti.name == expected_item.name);
1860
1861 if let Some(actual_item) = actual_item_with_same_name {
1862 if !expected_item.codegen_units.is_empty() {
1863 // Also check for codegen units
1864 if expected_item.codegen_units != actual_item.codegen_units {
1865 wrong_cgus.push((expected_item.clone(), actual_item.clone()));
1866 }
1867 }
1868 } else {
1869 missing.push(expected_item.string.clone());
1870 }
1871 }
1872
1873 let unexpected: Vec<_> =
1874 actual.iter()
1875 .filter(|acgu| !expected.iter().any(|ecgu| acgu.name == ecgu.name))
1876 .map(|acgu| acgu.string.clone())
1877 .collect();
1878
1879 if !missing.is_empty() {
1880 missing.sort();
1881
1882 println!("\nThese items should have been contained but were not:\n");
1883
1884 for item in &missing {
1885 println!("{}", item);
1886 }
1887
1888 println!("\n");
1889 }
1890
1891 if !unexpected.is_empty() {
1892 let sorted = {
1893 let mut sorted = unexpected.clone();
1894 sorted.sort();
1895 sorted
1896 };
1897
1898 println!("\nThese items were contained but should not have been:\n");
1899
1900 for item in sorted {
1901 println!("{}", item);
1902 }
1903
1904 println!("\n");
1905 }
1906
1907 if !wrong_cgus.is_empty() {
1908 wrong_cgus.sort_by_key(|pair| pair.0.name.clone());
1909 println!("\nThe following items were assigned to wrong codegen units:\n");
1910
1911 for &(ref expected_item, ref actual_item) in &wrong_cgus {
1912 println!("{}", expected_item.name);
1913 println!(" expected: {}", codegen_units_to_str(&expected_item.codegen_units));
1914 println!(" actual: {}", codegen_units_to_str(&actual_item.codegen_units));
1915 println!("");
1916 }
1917 }
1918
1919 if !(missing.is_empty() && unexpected.is_empty() && wrong_cgus.is_empty())
1920 {
1921 panic!();
1922 }
1923
1924 #[derive(Clone, Eq, PartialEq)]
1925 struct TransItem {
1926 name: String,
1927 codegen_units: HashSet<String>,
1928 string: String,
1929 }
1930
1931 // [TRANS_ITEM] name [@@ (cgu)+]
1932 fn str_to_trans_item(s: &str) -> TransItem {
1933 let s = if s.starts_with(PREFIX) {
1934 (&s[PREFIX.len()..]).trim()
1935 } else {
1936 s.trim()
1937 };
1938
1939 let full_string = format!("{}{}", PREFIX, s.trim().to_owned());
1940
1941 let parts: Vec<&str> = s.split(CGU_MARKER)
1942 .map(str::trim)
1943 .filter(|s| !s.is_empty())
1944 .collect();
1945
1946 let name = parts[0].trim();
1947
1948 let cgus = if parts.len() > 1 {
1949 let cgus_str = parts[1];
1950
1951 cgus_str.split(" ")
1952 .map(str::trim)
1953 .filter(|s| !s.is_empty())
1954 .map(str::to_owned)
1955 .collect()
1956 }
1957 else {
1958 HashSet::new()
1959 };
1960
1961 TransItem {
1962 name: name.to_owned(),
1963 codegen_units: cgus,
1964 string: full_string,
1965 }
1966 }
1967
1968 fn codegen_units_to_str(cgus: &HashSet<String>) -> String
1969 {
1970 let mut cgus: Vec<_> = cgus.iter().collect();
1971 cgus.sort();
1972
1973 let mut string = String::new();
1974 for cgu in cgus {
1975 string.push_str(&cgu[..]);
1976 string.push_str(" ");
1977 }
1978
1979 string
1980 }
1981 }
1982
1983 fn init_incremental_test(&self) {
1984 // (See `run_incremental_test` for an overview of how incremental tests work.)
1985
1986 // Before any of the revisions have executed, create the
1987 // incremental workproduct directory. Delete any old
1988 // incremental work products that may be there from prior
1989 // runs.
1990 let incremental_dir = self.incremental_dir();
1991 if incremental_dir.exists() {
9e0c209e
SL
1992 // Canonicalizing the path will convert it to the //?/ format
1993 // on Windows, which enables paths longer than 260 character
1994 let canonicalized = incremental_dir.canonicalize().unwrap();
1995 fs::remove_dir_all(canonicalized).unwrap();
a7813a04
XL
1996 }
1997 fs::create_dir_all(&incremental_dir).unwrap();
1998
1999 if self.config.verbose {
2000 print!("init_incremental_test: incremental_dir={}", incremental_dir.display());
2001 }
2002 }
2003
2004 fn run_incremental_test(&self) {
2005 // Basic plan for a test incremental/foo/bar.rs:
2006 // - load list of revisions rpass1, cfail2, rpass3
2007 // - each should begin with `rpass`, `cfail`, or `cfail`
2008 // - if `rpass`, expect compile and execution to succeed
2009 // - if `cfail`, expect compilation to fail
2010 // - if `rfail`, expect execution to fail
2011 // - create a directory build/foo/bar.incremental
2012 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass1
2013 // - because name of revision starts with "rpass", expect success
2014 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C cfail2
2015 // - because name of revision starts with "cfail", expect an error
2016 // - load expected errors as usual, but filter for those that end in `[rfail2]`
2017 // - compile foo/bar.rs with -Z incremental=.../foo/bar.incremental and -C rpass3
2018 // - because name of revision starts with "rpass", expect success
2019 // - execute build/foo/bar.exe and save output
2020 //
2021 // FIXME -- use non-incremental mode as an oracle? That doesn't apply
2022 // to #[rustc_dirty] and clean tests I guess
2023
2024 let revision = self.revision.expect("incremental tests require a list of revisions");
2025
2026 // Incremental workproduct directory should have already been created.
2027 let incremental_dir = self.incremental_dir();
2028 assert!(incremental_dir.exists(), "init_incremental_test failed to create incremental dir");
2029
2030 // Add an extra flag pointing at the incremental directory.
2031 let mut revision_props = self.props.clone();
2032 revision_props.incremental_dir = Some(incremental_dir);
5bcae85e 2033 revision_props.compile_flags.push(String::from("-Zincremental-info"));
a7813a04
XL
2034
2035 let revision_cx = TestCx {
2036 config: self.config,
2037 props: &revision_props,
2038 testpaths: self.testpaths,
2039 revision: self.revision,
2040 };
2041
2042 if self.config.verbose {
2043 print!("revision={:?} revision_props={:#?}", revision, revision_props);
2044 }
2045
2046 if revision.starts_with("rpass") {
2047 revision_cx.run_rpass_test();
2048 } else if revision.starts_with("rfail") {
2049 revision_cx.run_rfail_test();
2050 } else if revision.starts_with("cfail") {
2051 revision_cx.run_cfail_test();
2052 } else {
2053 revision_cx.fatal(
2054 "revision name must begin with rpass, rfail, or cfail");
2055 }
2056 }
2057
2058 /// Directory where incremental work products are stored.
2059 fn incremental_dir(&self) -> PathBuf {
9e0c209e 2060 self.output_base_name().with_extension("inc")
a7813a04
XL
2061 }
2062
2063 fn run_rmake_test(&self) {
2064 // FIXME(#11094): we should fix these tests
2065 if self.config.host != self.config.target {
2066 return
2067 }
2068
2069 let cwd = env::current_dir().unwrap();
2070 let src_root = self.config.src_base.parent().unwrap()
2071 .parent().unwrap()
2072 .parent().unwrap();
2073 let src_root = cwd.join(&src_root);
2074
2075 let tmpdir = cwd.join(self.output_base_name());
2076 if tmpdir.exists() {
2077 self.aggressive_rm_rf(&tmpdir).unwrap();
2078 }
2079 self.create_dir_racy(&tmpdir);
2080
2081 let mut cmd = Command::new("make");
2082 cmd.current_dir(&self.testpaths.file)
2083 .env("TARGET", &self.config.target)
2084 .env("PYTHON", &self.config.docck_python)
2085 .env("S", src_root)
2086 .env("RUST_BUILD_STAGE", &self.config.stage_id)
2087 .env("RUSTC", cwd.join(&self.config.rustc_path))
2088 .env("RUSTDOC", cwd.join(&self.config.rustdoc_path))
2089 .env("TMPDIR", &tmpdir)
2090 .env("LD_LIB_PATH_ENVVAR", procsrv::dylib_env_var())
2091 .env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
2092 .env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
2093 .env("LLVM_COMPONENTS", &self.config.llvm_components)
2094 .env("LLVM_CXXFLAGS", &self.config.llvm_cxxflags);
2095
2096 if self.config.target.contains("msvc") {
2097 // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
2098 // and that `lib.exe` lives next to it.
2099 let lib = Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
2100
2101 // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
2102 // a path and instead passes `C:\msys64\foo`, so convert all
2103 // `/`-arguments to MSVC here to `-` arguments.
2104 let cflags = self.config.cflags.split(' ').map(|s| s.replace("/", "-"))
2105 .collect::<Vec<_>>().join(" ");
2106
2107 cmd.env("IS_MSVC", "1")
2108 .env("MSVC_LIB", format!("'{}' -nologo", lib.display()))
2109 .env("CC", format!("'{}' {}", self.config.cc, cflags))
2110 .env("CXX", &self.config.cxx);
2111 } else {
2112 cmd.env("CC", format!("{} {}", self.config.cc, self.config.cflags))
2113 .env("CXX", format!("{} {}", self.config.cxx, self.config.cflags));
2114 }
2115
2116 let output = cmd.output().expect("failed to spawn `make`");
2117 if !output.status.success() {
2118 let res = ProcRes {
2119 status: Status::Normal(output.status),
2120 stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
2121 stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
2122 cmdline: format!("{:?}", cmd),
2123 };
2124 self.fatal_proc_rec("make failed", &res);
2125 }
2126 }
2127
2128 fn aggressive_rm_rf(&self, path: &Path) -> io::Result<()> {
9e0c209e
SL
2129 for e in path.read_dir()? {
2130 let entry = e?;
a7813a04 2131 let path = entry.path();
9e0c209e
SL
2132 if entry.file_type()?.is_dir() {
2133 self.aggressive_rm_rf(&path)?;
a7813a04
XL
2134 } else {
2135 // Remove readonly files as well on windows (by default we can't)
9e0c209e 2136 fs::remove_file(&path).or_else(|e| {
a7813a04 2137 if cfg!(windows) && e.kind() == io::ErrorKind::PermissionDenied {
9e0c209e 2138 let mut meta = entry.metadata()?.permissions();
a7813a04 2139 meta.set_readonly(false);
9e0c209e 2140 fs::set_permissions(&path, meta)?;
a7813a04
XL
2141 fs::remove_file(&path)
2142 } else {
2143 Err(e)
2144 }
9e0c209e 2145 })?;
a7813a04
XL
2146 }
2147 }
2148 fs::remove_dir(path)
2149 }
2150
2151 fn run_ui_test(&self) {
2152 println!("ui: {}", self.testpaths.file.display());
2153
2154 let proc_res = self.compile_test();
2155
2156 let expected_stderr_path = self.expected_output_path("stderr");
2157 let expected_stderr = self.load_expected_output(&expected_stderr_path);
2158
2159 let expected_stdout_path = self.expected_output_path("stdout");
2160 let expected_stdout = self.load_expected_output(&expected_stdout_path);
2161
2162 let normalized_stdout = self.normalize_output(&proc_res.stdout);
2163 let normalized_stderr = self.normalize_output(&proc_res.stderr);
2164
2165 let mut errors = 0;
2166 errors += self.compare_output("stdout", &normalized_stdout, &expected_stdout);
2167 errors += self.compare_output("stderr", &normalized_stderr, &expected_stderr);
2168
2169 if errors > 0 {
2170 println!("To update references, run this command from build directory:");
2171 let relative_path_to_file =
2172 self.testpaths.relative_dir
2173 .join(self.testpaths.file.file_name().unwrap());
2174 println!("{}/update-references.sh '{}' '{}'",
2175 self.config.src_base.display(),
2176 self.config.build_base.display(),
2177 relative_path_to_file.display());
2178 self.fatal_proc_rec(&format!("{} errors occurred comparing output.", errors),
2179 &proc_res);
2180 }
2181 }
2182
5bcae85e
SL
2183 fn run_mir_opt_test(&self) {
2184 let proc_res = self.compile_test();
2185
2186 if !proc_res.status.success() {
2187 self.fatal_proc_rec("compilation failed!", &proc_res);
2188 }
2189
2190 let proc_res = self.exec_compiled_test();
2191
2192 if !proc_res.status.success() {
2193 self.fatal_proc_rec("test run failed!", &proc_res);
2194 }
2195 self.check_mir_dump();
2196 }
2197
2198 fn check_mir_dump(&self) {
2199 let mut test_file_contents = String::new();
2200 fs::File::open(self.testpaths.file.clone()).unwrap()
2201 .read_to_string(&mut test_file_contents)
2202 .unwrap();
2203 if let Some(idx) = test_file_contents.find("// END RUST SOURCE") {
2204 let (_, tests_text) = test_file_contents.split_at(idx + "// END_RUST SOURCE".len());
2205 let tests_text_str = String::from(tests_text);
2206 let mut curr_test : Option<&str> = None;
2207 let mut curr_test_contents = Vec::new();
2208 for l in tests_text_str.lines() {
2209 debug!("line: {:?}", l);
2210 if l.starts_with("// START ") {
2211 let (_, t) = l.split_at("// START ".len());
2212 curr_test = Some(t);
2213 } else if l.starts_with("// END") {
2214 let (_, t) = l.split_at("// END ".len());
2215 if Some(t) != curr_test {
2216 panic!("mismatched START END test name");
2217 }
2218 self.compare_mir_test_output(curr_test.unwrap(), &curr_test_contents);
2219 curr_test = None;
2220 curr_test_contents.clear();
2221 } else if l.is_empty() {
2222 // ignore
2223 } else if l.starts_with("// ") {
2224 let (_, test_content) = l.split_at("// ".len());
2225 curr_test_contents.push(test_content);
2226 }
2227 }
2228 }
2229 }
2230
2231 fn compare_mir_test_output(&self, test_name: &str, expected_content: &Vec<&str>) {
2232 let mut output_file = PathBuf::new();
2233 output_file.push(self.get_mir_dump_dir());
2234 output_file.push(test_name);
2235 debug!("comparing the contests of: {:?}", output_file);
2236 debug!("with: {:?}", expected_content);
2237
2238 let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
2239 let mut dumped_string = String::new();
2240 dumped_file.read_to_string(&mut dumped_string).unwrap();
2241 let mut dumped_lines = dumped_string.lines().filter(|l| !l.is_empty());
2242 let mut expected_lines = expected_content.iter().filter(|l| !l.is_empty());
2243
2244 // We expect each non-empty line from expected_content to appear
2245 // in the dump in order, but there may be extra lines interleaved
2246 while let Some(expected_line) = expected_lines.next() {
2247 let e_norm = normalize_mir_line(expected_line);
2248 if e_norm.is_empty() {
2249 continue;
2250 };
2251 let mut found = false;
2252 while let Some(dumped_line) = dumped_lines.next() {
2253 let d_norm = normalize_mir_line(dumped_line);
2254 debug!("found: {:?}", d_norm);
2255 debug!("expected: {:?}", e_norm);
2256 if e_norm == d_norm {
2257 found = true;
2258 break;
2259 };
2260 }
2261 if !found {
2262 panic!("ran out of mir dump output to match against");
2263 }
2264 }
2265 }
2266
2267 fn get_mir_dump_dir(&self) -> PathBuf {
2268 let mut mir_dump_dir = PathBuf::from(self.config.build_base
2269 .as_path()
2270 .to_str()
2271 .unwrap());
2272 debug!("input_file: {:?}", self.testpaths.file);
2273 mir_dump_dir.push(self.testpaths.file.file_stem().unwrap().to_str().unwrap());
2274 mir_dump_dir
2275 }
2276
a7813a04
XL
2277 fn normalize_output(&self, output: &str) -> String {
2278 let parent_dir = self.testpaths.file.parent().unwrap();
2279 let parent_dir_str = parent_dir.display().to_string();
2280 output.replace(&parent_dir_str, "$DIR")
2281 .replace("\\", "/") // normalize for paths on windows
2282 .replace("\r\n", "\n") // normalize for linebreaks on windows
2283 .replace("\t", "\\t") // makes tabs visible
2284 }
2285
2286 fn expected_output_path(&self, kind: &str) -> PathBuf {
2287 let extension = match self.revision {
2288 Some(r) => format!("{}.{}", r, kind),
2289 None => kind.to_string(),
2290 };
2291 self.testpaths.file.with_extension(extension)
2292 }
2293
2294 fn load_expected_output(&self, path: &Path) -> String {
2295 if !path.exists() {
2296 return String::new();
2297 }
2298
2299 let mut result = String::new();
2300 match File::open(path).and_then(|mut f| f.read_to_string(&mut result)) {
2301 Ok(_) => result,
2302 Err(e) => {
2303 self.fatal(&format!("failed to load expected output from `{}`: {}",
2304 path.display(), e))
2305 }
2306 }
2307 }
2308
2309 fn compare_output(&self, kind: &str, actual: &str, expected: &str) -> usize {
2310 if actual == expected {
2311 return 0;
2312 }
2313
2314 println!("normalized {}:\n{}\n", kind, actual);
2315 println!("expected {}:\n{}\n", kind, expected);
2316 println!("diff of {}:\n", kind);
2317 for line in uidiff::diff_lines(actual, expected) {
2318 println!("{}", line);
2319 }
2320
2321 let output_file = self.output_base_name().with_extension(kind);
2322 match File::create(&output_file).and_then(|mut f| f.write_all(actual.as_bytes())) {
2323 Ok(()) => { }
2324 Err(e) => {
2325 self.fatal(&format!("failed to write {} to `{}`: {}",
2326 kind, output_file.display(), e))
2327 }
2328 }
2329
2330 println!("\nThe actual {0} differed from the expected {0}.", kind);
2331 println!("Actual {} saved to {}", kind, output_file.display());
2332 1
2333 }
2334}
2335
2336struct ProcArgs {
2337 prog: String,
2338 args: Vec<String>,
2339}
2340
2341pub struct ProcRes {
2342 status: Status,
2343 stdout: String,
2344 stderr: String,
2345 cmdline: String,
2346}
2347
2348enum Status {
2349 Parsed(i32),
2350 Normal(ExitStatus),
2351}
2352
2353impl ProcRes {
2354 pub fn fatal(&self, err: Option<&str>) -> ! {
2355 if let Some(e) = err {
2356 println!("\nerror: {}", e);
2357 }
2358 print!("\
2359 status: {}\n\
2360 command: {}\n\
2361 stdout:\n\
2362 ------------------------------------------\n\
2363 {}\n\
2364 ------------------------------------------\n\
2365 stderr:\n\
2366 ------------------------------------------\n\
2367 {}\n\
2368 ------------------------------------------\n\
2369 \n",
2370 self.status, self.cmdline, self.stdout,
2371 self.stderr);
2372 panic!();
2373 }
2374}
2375
2376impl Status {
2377 fn code(&self) -> Option<i32> {
2378 match *self {
2379 Status::Parsed(i) => Some(i),
2380 Status::Normal(ref e) => e.code(),
2381 }
2382 }
2383
2384 fn success(&self) -> bool {
2385 match *self {
2386 Status::Parsed(i) => i == 0,
2387 Status::Normal(ref e) => e.success(),
2388 }
2389 }
2390}
2391
2392impl fmt::Display for Status {
2393 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2394 match *self {
2395 Status::Parsed(i) => write!(f, "exit code: {}", i),
2396 Status::Normal(ref e) => e.fmt(f),
2397 }
2398 }
2399}
2400
2401enum TargetLocation {
2402 ThisFile(PathBuf),
2403 ThisDirectory(PathBuf),
2404}
2405
5bcae85e
SL
2406fn normalize_mir_line(line: &str) -> String {
2407 let no_comments = if let Some(idx) = line.find("//") {
2408 let (l, _) = line.split_at(idx);
2409 l
2410 } else {
2411 line
2412 };
2413 no_comments.replace(char::is_whitespace, "")
2414}