]>
git.proxmox.com Git - rustc.git/blob - vendor/cc/tests/support/mod.rs
4 use std
::ffi
::{OsStr, OsString}
;
5 use std
::fs
::{self, File}
;
7 use std
::io
::prelude
::*;
8 use std
::path
::{Path, PathBuf}
;
19 pub struct Execution
{
24 pub fn new() -> Test
{
25 let mut gcc
= PathBuf
::from(env
::current_exe().unwrap());
27 if gcc
.ends_with("deps") {
30 let td
= TempDir
::new_in(&gcc
, "gcc-test").unwrap();
31 gcc
.push(format
!("gcc-shim{}", env
::consts
::EXE_SUFFIX
));
39 pub fn gnu() -> Test
{
41 t
.shim("cc").shim("c++").shim("ar");
45 pub fn msvc() -> Test
{
46 let mut t
= Test
::new();
47 t
.shim("cl").shim("lib.exe");
52 pub fn shim(&self, name
: &str) -> &Test
{
57 .join(&format
!("{}{}", name
, env
::consts
::EXE_SUFFIX
)),
63 pub fn gcc(&self) -> cc
::Build
{
64 let mut cfg
= cc
::Build
::new();
65 let target
= if self.msvc
{
66 "x86_64-pc-windows-msvc"
68 "x86_64-unknown-linux-gnu"
75 .out_dir(self.td
.path())
76 .__set_env("PATH", self.path())
77 .__set_env("GCCTEST_OUT_DIR", self.td
.path());
79 cfg
.compiler(self.td
.path().join("cl"));
80 cfg
.archiver(self.td
.path().join("lib.exe"));
85 fn path(&self) -> OsString
{
86 let mut path
= env
::split_paths(&env
::var_os("PATH").unwrap()).collect
::<Vec
<_
>>();
87 path
.insert(0, self.td
.path().to_owned());
88 env
::join_paths(path
).unwrap()
91 pub fn cmd(&self, i
: u32) -> Execution
{
92 let mut s
= String
::new();
93 File
::open(self.td
.path().join(format
!("out{}", i
)))
95 .read_to_string(&mut s
)
98 args
: s
.lines().map(|s
| s
.to_string()).collect(),
104 pub fn must_have
<P
: AsRef
<OsStr
>>(&self, p
: P
) -> &Execution
{
105 if !self.has(p
.as_ref()) {
106 panic
!("didn't find {:?} in {:?}", p
.as_ref(), self.args
);
112 pub fn must_not_have
<P
: AsRef
<OsStr
>>(&self, p
: P
) -> &Execution
{
113 if self.has(p
.as_ref()) {
114 panic
!("found {:?}", p
.as_ref());
120 pub fn has(&self, p
: &OsStr
) -> bool
{
121 self.args
.iter().any(|arg
| OsStr
::new(arg
) == p
)
124 pub fn must_have_in_order(&self, before
: &str, after
: &str) -> &Execution
{
125 let before_position
= self
128 .rposition(|x
| OsStr
::new(x
) == OsStr
::new(before
));
129 let after_position
= self
132 .rposition(|x
| OsStr
::new(x
) == OsStr
::new(after
));
133 match (before_position
, after_position
) {
134 (Some(b
), Some(a
)) if b
< a
=> {}
136 "{:?} (last position: {:?}) did not appear before {:?} (last position: {:?})",
144 /// Hard link an executable or copy it if that fails.
146 /// We first try to hard link an executable to save space. If that fails (as on Windows with
147 /// different mount points, issue #60), we copy.
148 #[cfg(not(target_os = "macos"))]
149 fn link_or_copy
<P
: AsRef
<Path
>, Q
: AsRef
<Path
>>(from
: P
, to
: Q
) -> io
::Result
<()> {
150 let from
= from
.as_ref();
151 let to
= to
.as_ref();
152 fs
::hard_link(from
, to
).or_else(|_
| fs
::copy(from
, to
).map(|_
| ()))
155 /// Copy an executable.
157 /// On macOS, hard linking the executable leads to strange failures (issue #419), so we just copy.
158 #[cfg(target_os = "macos")]
159 fn link_or_copy
<P
: AsRef
<Path
>, Q
: AsRef
<Path
>>(from
: P
, to
: Q
) -> io
::Result
<()> {
160 fs
::copy(from
, to
).map(|_
| ())