Skip to content

Commit 784d315

Browse files
committed
set dylib path correctly for target when running cargo test etc
1 parent 20a085e commit 784d315

File tree

6 files changed

+122
-23
lines changed

6 files changed

+122
-23
lines changed

appveyor.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ install:
2828
- if defined MINGW_URL 7z x -y %MINGW_ARCHIVE% > nul
2929
- if defined MINGW_URL set PATH=%CD%\%MINGW_DIR%\bin;C:\msys64\usr\bin;%PATH%
3030

31-
- appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
31+
# FIXME(#3394) use master rustup
32+
# for the purposes of testing this PR, use rustup 1.2.0 because there is possibly a
33+
# change https://github.com/rust-lang-nursery/rustup.rs/pull/1093 about to land
34+
# nightly rustup that will also fix some of the failing windows tests.
35+
- curl -sSfO https://static.rust-lang.org/rustup/archive/1.2.0/x86_64-pc-windows-msvc/rustup-init.exe
3236
- rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly-2017-03-03
3337
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
3438
- if NOT "%TARGET%" == "x86_64-pc-windows-msvc" rustup target add %TARGET%

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pub struct Compilation<'cfg> {
3535
/// which have dynamic dependencies.
3636
pub plugins_dylib_path: PathBuf,
3737

38+
/// The path to rustc's own libstd
39+
pub host_dylib_path: Option<PathBuf>,
40+
41+
/// The path to libstd for the target
42+
pub target_dylib_path: Option<PathBuf>,
43+
3844
/// Extra environment variables that were passed to compilations and should
3945
/// be passed to future invocations of programs.
4046
pub extra_env: HashMap<PackageId, Vec<(String, String)>>,
@@ -57,6 +63,8 @@ impl<'cfg> Compilation<'cfg> {
5763
root_output: PathBuf::from("/"),
5864
deps_output: PathBuf::from("/"),
5965
plugins_dylib_path: PathBuf::from("/"),
66+
host_dylib_path: None,
67+
target_dylib_path: None,
6068
tests: Vec::new(),
6169
binaries: Vec::new(),
6270
extra_env: HashMap::new(),
@@ -98,7 +106,9 @@ impl<'cfg> Compilation<'cfg> {
98106
-> CargoResult<ProcessBuilder> {
99107

100108
let mut search_path = if is_host {
101-
vec![self.plugins_dylib_path.clone()]
109+
let mut search_path = vec![self.plugins_dylib_path.clone()];
110+
search_path.push(self.host_dylib_path.iter().collect());
111+
search_path
102112
} else {
103113
let mut search_path = vec![];
104114

@@ -128,6 +138,7 @@ impl<'cfg> Compilation<'cfg> {
128138
}
129139
search_path.push(self.root_output.clone());
130140
search_path.push(self.deps_output.clone());
141+
search_path.push(self.target_dylib_path.iter().collect());
131142
search_path
132143
};
133144

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
243243
None
244244
};
245245

246+
if let Some(ref sysroot) = self.config.rustc()?.sysroot {
247+
let mut rustlib = sysroot.clone();
248+
if kind == Kind::Host {
249+
if cfg!(windows) {
250+
rustlib.push("bin");
251+
} else {
252+
rustlib.push("lib");
253+
}
254+
self.compilation.host_dylib_path = Some(rustlib);
255+
} else {
256+
rustlib.push("lib");
257+
rustlib.push("rustlib");
258+
rustlib.push(self.target_triple());
259+
rustlib.push("lib");
260+
self.compilation.target_dylib_path = Some(rustlib);
261+
}
262+
};
263+
246264
let info = match kind {
247265
Kind::Target => &mut self.target_info,
248266
Kind::Host => &mut self.host_info,

src/cargo/util/rustc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct Rustc {
77
pub wrapper: Option<PathBuf>,
88
pub verbose_version: String,
99
pub host: String,
10+
pub sysroot: Option<PathBuf>,
1011
}
1112

1213
impl Rustc {
@@ -35,11 +36,20 @@ impl Rustc {
3536
triple.to_string()
3637
};
3738

39+
let sysroot = {
40+
let mut cmd = util::process(&path);
41+
cmd.arg("--print=sysroot");
42+
cmd.exec_with_output().ok()
43+
.and_then(|output| String::from_utf8(output.stdout).ok() )
44+
.map(|s| PathBuf::from(s.trim()))
45+
};
46+
3847
Ok(Rustc {
3948
path: path,
4049
wrapper: wrapper,
4150
verbose_version: verbose_version,
4251
host: host,
52+
sysroot: sysroot
4353
})
4454
}
4555

tests/cargotest/lib.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ extern crate url;
2020

2121
use std::ffi::OsStr;
2222
use std::time::Duration;
23-
use std::path::{Path, PathBuf};
2423

2524
use cargo::util::Rustc;
26-
use cargo::util::paths;
25+
use std::path::PathBuf;
2726

2827
pub mod support;
2928
pub mod install;
@@ -67,25 +66,6 @@ fn _process(t: &OsStr) -> cargo::util::ProcessBuilder {
6766
.env_remove("GIT_COMMITTER_EMAIL")
6867
.env_remove("CARGO_TARGET_DIR") // we assume 'target'
6968
.env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows
70-
71-
// We'll need dynamic libraries at some point in this test suite, so ensure
72-
// that the rustc libdir is somewhere in LD_LIBRARY_PATH as appropriate.
73-
let mut rustc = RUSTC.with(|r| r.process());
74-
let output = rustc.arg("--print").arg("sysroot").exec_with_output().unwrap();
75-
let libdir = String::from_utf8(output.stdout).unwrap();
76-
let libdir = Path::new(libdir.trim());
77-
let libdir = if cfg!(windows) {
78-
libdir.join("bin")
79-
} else {
80-
libdir.join("lib")
81-
};
82-
let mut paths = paths::dylib_path();
83-
println!("libdir: {:?}", libdir);
84-
if !paths.contains(&libdir) {
85-
paths.push(libdir);
86-
p.env(paths::dylib_path_envvar(),
87-
paths::join_paths(&paths, paths::dylib_path_envvar()).unwrap());
88-
}
8969
return p
9070
}
9171

tests/cross-compile.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,3 +1053,79 @@ fn platform_specific_variables_reflected_in_build_scripts() {
10531053
assert_that(p.cargo("build").arg("-v").arg("--target").arg(&target),
10541054
execs().with_status(0));
10551055
}
1056+
1057+
#[test]
1058+
fn cross_test_dylib() {
1059+
if disabled() { return }
1060+
1061+
let target = alternate();
1062+
1063+
let p = project("foo")
1064+
.file("Cargo.toml", r#"
1065+
[package]
1066+
name = "foo"
1067+
version = "0.0.1"
1068+
authors = []
1069+
1070+
[lib]
1071+
name = "foo"
1072+
crate_type = ["dylib"]
1073+
1074+
[dependencies.bar]
1075+
path = "bar"
1076+
"#)
1077+
.file("src/lib.rs", r#"
1078+
extern crate bar as the_bar;
1079+
1080+
pub fn bar() { the_bar::baz(); }
1081+
1082+
#[test]
1083+
fn foo() { bar(); }
1084+
"#)
1085+
.file("tests/test.rs", r#"
1086+
extern crate foo as the_foo;
1087+
1088+
#[test]
1089+
fn foo() { the_foo::bar(); }
1090+
"#)
1091+
.file("bar/Cargo.toml", r#"
1092+
[package]
1093+
name = "bar"
1094+
version = "0.0.1"
1095+
authors = []
1096+
1097+
[lib]
1098+
name = "bar"
1099+
crate_type = ["dylib"]
1100+
"#)
1101+
.file("bar/src/lib.rs", &format!(r#"
1102+
use std::env;
1103+
pub fn baz() {{
1104+
assert_eq!(env::consts::ARCH, "{}");
1105+
}}
1106+
"#, alternate_arch()));
1107+
1108+
assert_that(p.cargo_process("test").arg("--target").arg(&target),
1109+
execs().with_status(0)
1110+
.with_stderr(&format!("\
1111+
[COMPILING] bar v0.0.1 ({dir}/bar)
1112+
[COMPILING] foo v0.0.1 ({dir})
1113+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1114+
[RUNNING] target[/]{arch}[/]debug[/]deps[/]foo-[..][EXE]
1115+
[RUNNING] target[/]{arch}[/]debug[/]deps[/]test-[..][EXE]",
1116+
dir = p.url(), arch = alternate()))
1117+
.with_stdout("
1118+
running 1 test
1119+
test foo ... ok
1120+
1121+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1122+
1123+
1124+
running 1 test
1125+
test foo ... ok
1126+
1127+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
1128+
1129+
"));
1130+
1131+
}

0 commit comments

Comments
 (0)