summaryrefslogtreecommitdiff
path: root/cc/tests
diff options
context:
space:
mode:
Diffstat (limited to 'cc/tests')
-rw-r--r--cc/tests/cflags.rs18
-rw-r--r--cc/tests/cxxflags.rs18
-rw-r--r--cc/tests/support/mod.rs48
-rw-r--r--cc/tests/test.rs41
4 files changed, 84 insertions, 41 deletions
diff --git a/cc/tests/cflags.rs b/cc/tests/cflags.rs
new file mode 100644
index 0000000..df6b0a7
--- /dev/null
+++ b/cc/tests/cflags.rs
@@ -0,0 +1,18 @@
+extern crate cc;
+extern crate tempdir;
+
+mod support;
+
+use std::env;
+use support::Test;
+
+/// This test is in its own module because it modifies the environment and would affect other tests
+/// when run in parallel with them.
+#[test]
+fn gnu_no_warnings_if_cflags() {
+ env::set_var("CFLAGS", "-arbitrary");
+ let test = Test::gnu();
+ test.gcc().file("foo.c").compile("foo");
+
+ test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
+}
diff --git a/cc/tests/cxxflags.rs b/cc/tests/cxxflags.rs
new file mode 100644
index 0000000..26426af
--- /dev/null
+++ b/cc/tests/cxxflags.rs
@@ -0,0 +1,18 @@
+extern crate cc;
+extern crate tempdir;
+
+mod support;
+
+use std::env;
+use support::Test;
+
+/// This test is in its own module because it modifies the environment and would affect other tests
+/// when run in parallel with them.
+#[test]
+fn gnu_no_warnings_if_cxxflags() {
+ env::set_var("CXXFLAGS", "-arbitrary");
+ let test = Test::gnu();
+ test.gcc().file("foo.cpp").cpp(true).compile("foo");
+
+ test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
+}
diff --git a/cc/tests/support/mod.rs b/cc/tests/support/mod.rs
index 72ca3fa..7d74719 100644
--- a/cc/tests/support/mod.rs
+++ b/cc/tests/support/mod.rs
@@ -1,10 +1,11 @@
#![allow(dead_code)]
use std::env;
-use std::ffi::OsStr;
+use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
+use std::io;
use std::io::prelude::*;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use cc;
use tempdir::TempDir;
@@ -26,9 +27,10 @@ impl Test {
if gcc.ends_with("deps") {
gcc.pop();
}
+ let td = TempDir::new_in(&gcc, "gcc-test").unwrap();
gcc.push(format!("gcc-shim{}", env::consts::EXE_SUFFIX));
Test {
- td: TempDir::new("gcc-test").unwrap(),
+ td: td,
gcc: gcc,
msvc: false,
}
@@ -48,17 +50,18 @@ impl Test {
}
pub fn shim(&self, name: &str) -> &Test {
- let fname = format!("{}{}", name, env::consts::EXE_SUFFIX);
- fs::hard_link(&self.gcc, self.td.path().join(&fname))
- .or_else(|_| fs::copy(&self.gcc, self.td.path().join(&fname)).map(|_| ()))
- .unwrap();
+ link_or_copy(
+ &self.gcc,
+ self.td
+ .path()
+ .join(&format!("{}{}", name, env::consts::EXE_SUFFIX)),
+ )
+ .unwrap();
self
}
pub fn gcc(&self) -> cc::Build {
let mut cfg = cc::Build::new();
- let mut path = env::split_paths(&env::var_os("PATH").unwrap()).collect::<Vec<_>>();
- path.insert(0, self.td.path().to_owned());
let target = if self.msvc {
"x86_64-pc-windows-msvc"
} else {
@@ -70,7 +73,7 @@ impl Test {
.opt_level(2)
.debug(false)
.out_dir(self.td.path())
- .__set_env("PATH", env::join_paths(path).unwrap())
+ .__set_env("PATH", self.path())
.__set_env("GCCTEST_OUT_DIR", self.td.path());
if self.msvc {
cfg.compiler(self.td.path().join("cl"));
@@ -79,6 +82,12 @@ impl Test {
cfg
}
+ fn path(&self) -> OsString {
+ let mut path = env::split_paths(&env::var_os("PATH").unwrap()).collect::<Vec<_>>();
+ path.insert(0, self.td.path().to_owned());
+ env::join_paths(path).unwrap()
+ }
+
pub fn cmd(&self, i: u32) -> Execution {
let mut s = String::new();
File::open(self.td.path().join(format!("out{}", i)))
@@ -131,3 +140,22 @@ impl Execution {
self
}
}
+
+/// Hard link an executable or copy it if that fails.
+///
+/// We first try to hard link an executable to save space. If that fails (as on Windows with
+/// different mount points, issue #60), we copy.
+#[cfg(not(target_os = "macos"))]
+fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
+ let from = from.as_ref();
+ let to = to.as_ref();
+ fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
+}
+
+/// Copy an executable.
+///
+/// On macOS, hard linking the executable leads to strange failures (issue #419), so we just copy.
+#[cfg(target_os = "macos")]
+fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
+ fs::copy(from, to).map(|_| ())
+}
diff --git a/cc/tests/test.rs b/cc/tests/test.rs
index 5147b77..74eca1e 100644
--- a/cc/tests/test.rs
+++ b/cc/tests/test.rs
@@ -1,7 +1,6 @@
extern crate cc;
extern crate tempdir;
-use std::env;
use support::Test;
mod support;
@@ -112,26 +111,6 @@ fn gnu_warnings_overridable() {
}
#[test]
-fn gnu_no_warnings_if_cflags() {
- env::set_var("CFLAGS", "-Wflag-does-not-exist");
- let test = Test::gnu();
- test.gcc().file("foo.c").compile("foo");
-
- test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
- env::set_var("CFLAGS", "");
-}
-
-#[test]
-fn gnu_no_warnings_if_cxxflags() {
- env::set_var("CXXFLAGS", "-Wflag-does-not-exist");
- let test = Test::gnu();
- test.gcc().file("foo.c").compile("foo");
-
- test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
- env::set_var("CXXFLAGS", "");
-}
-
-#[test]
fn gnu_x86_64() {
for vendor in &["unknown-linux-gnu", "apple-darwin"] {
let target = format!("x86_64-{}", vendor);
@@ -311,11 +290,11 @@ fn msvc_smoke() {
test.gcc().file("foo.c").compile("foo");
test.cmd(0)
- .must_have("/O2")
+ .must_have("-O2")
.must_have("foo.c")
- .must_not_have("/Z7")
- .must_have("/c")
- .must_have("/MD");
+ .must_not_have("-Z7")
+ .must_have("-c")
+ .must_have("-MD");
test.cmd(1).must_have(test.td.path().join("foo.o"));
}
@@ -324,14 +303,14 @@ fn msvc_opt_level_0() {
let test = Test::msvc();
test.gcc().opt_level(0).file("foo.c").compile("foo");
- test.cmd(0).must_not_have("/O2");
+ test.cmd(0).must_not_have("-O2");
}
#[test]
fn msvc_debug() {
let test = Test::msvc();
test.gcc().debug(true).file("foo.c").compile("foo");
- test.cmd(0).must_have("/Z7");
+ test.cmd(0).must_have("-Z7");
}
#[test]
@@ -339,7 +318,7 @@ fn msvc_include() {
let test = Test::msvc();
test.gcc().include("foo/bar").file("foo.c").compile("foo");
- test.cmd(0).must_have("/I").must_have("foo/bar");
+ test.cmd(0).must_have("-I").must_have("foo/bar");
}
#[test]
@@ -351,7 +330,7 @@ fn msvc_define() {
.file("foo.c")
.compile("foo");
- test.cmd(0).must_have("/DFOO=bar").must_have("/DBAR");
+ test.cmd(0).must_have("-DFOO=bar").must_have("-DBAR");
}
#[test]
@@ -359,7 +338,7 @@ fn msvc_static_crt() {
let test = Test::msvc();
test.gcc().static_crt(true).file("foo.c").compile("foo");
- test.cmd(0).must_have("/MT");
+ test.cmd(0).must_have("-MT");
}
#[test]
@@ -367,5 +346,5 @@ fn msvc_no_static_crt() {
let test = Test::msvc();
test.gcc().static_crt(false).file("foo.c").compile("foo");
- test.cmd(0).must_have("/MD");
+ test.cmd(0).must_have("-MD");
}