1 #![cfg_attr(test, allow(dead_code))]
2 
3 use std::env;
4 use std::fs::File;
5 use std::io::prelude::*;
6 use std::path::PathBuf;
7 
main()8 fn main() {
9     let mut args = env::args();
10     let program = args.next().expect("Unexpected empty args");
11 
12     let out_dir = PathBuf::from(
13         env::var_os("GCCTEST_OUT_DIR").expect(&format!("{}: GCCTEST_OUT_DIR not found", program)),
14     );
15 
16     // Find the first nonexistent candidate file to which the program's args can be written.
17     for i in 0.. {
18         let candidate = &out_dir.join(format!("out{}", i));
19 
20         // If the file exists, commands have already run. Try again.
21         if candidate.exists() {
22             continue;
23         }
24 
25         // Create a file and record the args passed to the command.
26         let mut f = File::create(candidate).expect(&format!(
27             "{}: can't create candidate: {}",
28             program,
29             candidate.to_string_lossy()
30         ));
31         for arg in args {
32             writeln!(f, "{}", arg).expect(&format!(
33                 "{}: can't write to candidate: {}",
34                 program,
35                 candidate.to_string_lossy()
36             ));
37         }
38         break;
39     }
40 
41     // Create a file used by some tests.
42     let path = &out_dir.join("libfoo.a");
43     File::create(path).expect(&format!(
44         "{}: can't create libfoo.a: {}",
45         program,
46         path.to_string_lossy()
47     ));
48 }
49