From 1d36411cc421d5737f0fd8c123ff2fcc74780d09 Mon Sep 17 00:00:00 2001 From: Henrik Laxhuber Date: Mon, 23 Apr 2018 17:11:10 +0200 Subject: [PATCH] Added tests for RUSTC_LINKER buils script env var 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 | 4 +-- tests/testsuite/build_script.rs | 44 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index cb08d4a70..320af6403 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -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() { diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index af8f5cc7f..0b87ebb81 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -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] -- 2.30.2