Added tests for RUSTC_LINKER buils script env var
authorHenrik Laxhuber <henrik@laxhuber.com>
Mon, 23 Apr 2018 15:11:10 +0000 (17:11 +0200)
committerHenrik Laxhuber <henrik@laxhuber.com>
Mon, 23 Apr 2018 15:11:10 +0000 (17:11 +0200)
Added tests for the environment variable RUSTC_LINKER that is
passed to the build script.
Also slightly improved readability of the code responsible for
passing the env var.

src/cargo/core/compiler/custom_build.rs
tests/testsuite/build_script.rs

index cb08d4a705da48960a16481f89f68dc2f2fce3f5..320af6403141c714051f06cb6d3203d64852348e 100644 (file)
@@ -146,8 +146,8 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
         .env("RUSTDOC", &*cx.config.rustdoc()?)
         .inherit_jobserver(&cx.jobserver);
 
-    if cx.build_config.target.linker.is_some() {
-        cmd.env("RUSTC_LINKER", &cx.build_config.target.linker.as_ref().unwrap());
+    if let Some(ref linker) = cx.build_config.target.linker {
+        cmd.env("RUSTC_LINKER", linker);
     }
 
     if let Some(links) = unit.pkg.manifest().links() {
index af8f5cc7f4111a06029185e087647fd730fce2dd..0b87ebb81fd1e395b19b39c6eefff8db7de1f4b5 100644 (file)
@@ -4,7 +4,7 @@ use std::io::prelude::*;
 use std::path::PathBuf;
 
 use cargotest::{rustc_host, sleep_ms};
-use cargotest::support::{execs, project};
+use cargotest::support::{cross_compile, execs, project};
 use cargotest::support::paths::CargoPathExt;
 use cargotest::support::registry::Package;
 use hamcrest::{assert_that, existing_dir, existing_file};
@@ -134,6 +134,8 @@ fn custom_build_env_vars() {
 
                 let rustdoc = env::var("RUSTDOC").unwrap();
                 assert_eq!(rustdoc, "rustdoc");
+
+                assert!(env::var("RUSTC_LINKER").is_err());
             }}
         "#,
         p.root()
@@ -149,6 +151,46 @@ fn custom_build_env_vars() {
         p.cargo("build").arg("--features").arg("bar_feat"),
         execs().with_status(0),
     );
+
+    // Test passing linker from .cargo/config to env var
+    if cross_compile::disabled() { return; }
+    let target = cross_compile::alternate();
+    let p = project("foo")
+        .file("Cargo.toml",
+              r#"
+              [project]
+              name = "foo"
+              version = "0.0.1"
+              "#
+        )
+        .file(
+            ".cargo/config",
+            &format!(
+                r#"
+                [target.{}]
+                linker = "/path/to/linker"
+                "#,
+                target
+            )
+        )
+        .file(
+            "build.rs",
+            r#"
+            use std::env;
+
+            fn main() {
+                assert_eq!(env::var("RUSTC_LINKER"), "/path/to/linker");
+            }
+        "#)
+        .file("src/lib.rs", "")
+        .build();
+
+    // no crate type set => linker never called => build succeeds if and
+    // only if build.rs succeeds, despite linker binary not existing.
+    assert_that(
+        p.cargo("build").arg("--target").arg(&target).arg("-v"),
+        execs().with_status(0),
+    );
 }
 
 #[test]