Skip to content

Commit ab0cb30

Browse files
committed
Auto merge of rust-lang#5145 - lzutao:rmeta, r=flip1995
Fix error E0460 when compiled on Rustc repo Sadly, this mostly reverts rust-lang#5121. Now I use HashMap to only store one rlib per crate. But that would not work when non-compatible version of the same crate show up. changelog: none
2 parents 2d9ef4e + 9eb913a commit ab0cb30

File tree

3 files changed

+78
-89
lines changed

3 files changed

+78
-89
lines changed

tests/cargo/mod.rs

+14-60
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,29 @@
1-
use cargo_metadata::{Message::CompilerArtifact, MetadataCommand};
1+
use lazy_static::lazy_static;
22
use std::env;
3-
use std::ffi::OsStr;
4-
use std::mem;
53
use std::path::PathBuf;
6-
use std::process::Command;
74

8-
pub struct BuildInfo {
9-
cargo_target_dir: PathBuf,
10-
}
11-
12-
impl BuildInfo {
13-
pub fn new() -> Self {
14-
let data = MetadataCommand::new().exec().unwrap();
15-
Self {
16-
cargo_target_dir: data.target_directory,
5+
lazy_static! {
6+
pub static ref CARGO_TARGET_DIR: PathBuf = {
7+
match env::var_os("CARGO_TARGET_DIR") {
8+
Some(v) => v.into(),
9+
None => "target".into(),
1710
}
18-
}
19-
20-
pub fn host_lib(&self) -> PathBuf {
21-
if let Some(path) = option_env!("HOST_LIBS") {
22-
PathBuf::from(path)
23-
} else {
24-
self.cargo_target_dir.join(env!("PROFILE"))
25-
}
26-
}
27-
28-
pub fn target_lib(&self) -> PathBuf {
11+
};
12+
pub static ref TARGET_LIB: PathBuf = {
2913
if let Some(path) = option_env!("TARGET_LIBS") {
3014
path.into()
3115
} else {
32-
let mut dir = self.cargo_target_dir.clone();
16+
let mut dir = CARGO_TARGET_DIR.clone();
3317
if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
3418
dir.push(target);
3519
}
3620
dir.push(env!("PROFILE"));
3721
dir
3822
}
39-
}
40-
41-
pub fn clippy_driver_path(&self) -> PathBuf {
42-
if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
43-
PathBuf::from(path)
44-
} else {
45-
self.target_lib().join("clippy-driver")
46-
}
47-
}
48-
49-
// When we'll want to use `extern crate ..` for a dependency that is used
50-
// both by the crate and the compiler itself, we can't simply pass -L flags
51-
// as we'll get a duplicate matching versions. Instead, disambiguate with
52-
// `--extern dep=path`.
53-
// See https://github.com/rust-lang/rust-clippy/issues/4015.
54-
pub fn third_party_crates() -> Vec<(&'static str, PathBuf)> {
55-
const THIRD_PARTY_CRATES: [&str; 4] = ["serde", "serde_derive", "regex", "clippy_lints"];
56-
let cargo = env::var_os("CARGO");
57-
let cargo = cargo.as_deref().unwrap_or_else(|| OsStr::new("cargo"));
58-
let output = Command::new(cargo)
59-
.arg("build")
60-
.arg("--test=compile-test")
61-
.arg("--message-format=json")
62-
.output()
63-
.unwrap();
23+
};
24+
}
6425

65-
let mut crates = Vec::with_capacity(THIRD_PARTY_CRATES.len());
66-
for message in cargo_metadata::parse_messages(output.stdout.as_slice()) {
67-
if let CompilerArtifact(mut artifact) = message.unwrap() {
68-
if let Some(&krate) = THIRD_PARTY_CRATES.iter().find(|&&krate| krate == artifact.target.name) {
69-
crates.push((krate, mem::take(&mut artifact.filenames[0])));
70-
}
71-
}
72-
}
73-
crates
74-
}
26+
#[must_use]
27+
pub fn is_rustc_test_suite() -> bool {
28+
option_env!("RUSTC_TEST_SUITE").is_some()
7529
}

tests/compile-test.rs

+58-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(test)]
1+
#![feature(test)] // compiletest_rs requires this attribute
22

33
use compiletest_rs as compiletest;
44
use compiletest_rs::common::Mode as TestMode;
@@ -11,51 +11,87 @@ use std::path::{Path, PathBuf};
1111

1212
mod cargo;
1313

14-
#[must_use]
15-
fn rustc_test_suite() -> Option<PathBuf> {
16-
option_env!("RUSTC_TEST_SUITE").map(PathBuf::from)
14+
fn host_lib() -> PathBuf {
15+
if let Some(path) = option_env!("HOST_LIBS") {
16+
PathBuf::from(path)
17+
} else {
18+
cargo::CARGO_TARGET_DIR.join(env!("PROFILE"))
19+
}
1720
}
1821

19-
#[must_use]
20-
fn rustc_lib_path() -> PathBuf {
21-
option_env!("RUSTC_LIB_PATH").unwrap().into()
22+
fn clippy_driver_path() -> PathBuf {
23+
if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
24+
PathBuf::from(path)
25+
} else {
26+
cargo::TARGET_LIB.join("clippy-driver")
27+
}
28+
}
29+
30+
// When we'll want to use `extern crate ..` for a dependency that is used
31+
// both by the crate and the compiler itself, we can't simply pass -L flags
32+
// as we'll get a duplicate matching versions. Instead, disambiguate with
33+
// `--extern dep=path`.
34+
// See https://github.com/rust-lang/rust-clippy/issues/4015.
35+
//
36+
// FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files.
37+
// Because it would force-rebuild if the options passed to `build` command is not the same
38+
// as what we manually pass to `cargo` invocation
39+
fn third_party_crates() -> String {
40+
use std::collections::HashMap;
41+
static CRATES: &[&str] = &["serde", "serde_derive", "regex", "clippy_lints"];
42+
let dep_dir = cargo::TARGET_LIB.join("deps");
43+
let mut crates: HashMap<&str, PathBuf> = HashMap::with_capacity(CRATES.len());
44+
for entry in fs::read_dir(dep_dir).unwrap() {
45+
let path = match entry {
46+
Ok(entry) => entry.path(),
47+
_ => continue,
48+
};
49+
if let Some(name) = path.file_name().and_then(OsStr::to_str) {
50+
for dep in CRATES {
51+
if name.starts_with(&format!("lib{}-", dep)) && name.ends_with(".rlib") {
52+
crates.entry(dep).or_insert(path);
53+
break;
54+
}
55+
}
56+
}
57+
}
58+
59+
let v: Vec<_> = crates
60+
.into_iter()
61+
.map(|(dep, path)| format!("--extern {}={}", dep, path.display()))
62+
.collect();
63+
v.join(" ")
2264
}
2365

2466
fn default_config() -> compiletest::Config {
25-
let build_info = cargo::BuildInfo::new();
2667
let mut config = compiletest::Config::default();
2768

2869
if let Ok(name) = env::var("TESTNAME") {
2970
config.filter = Some(name);
3071
}
3172

32-
if rustc_test_suite().is_some() {
33-
let path = rustc_lib_path();
73+
if let Some(path) = option_env!("RUSTC_LIB_PATH") {
74+
let path = PathBuf::from(path);
3475
config.run_lib_path = path.clone();
3576
config.compile_lib_path = path;
3677
}
3778

38-
let disambiguated: Vec<_> = cargo::BuildInfo::third_party_crates()
39-
.iter()
40-
.map(|(krate, path)| format!("--extern {}={}", krate, path.display()))
41-
.collect();
42-
4379
config.target_rustcflags = Some(format!(
4480
"-L {0} -L {1} -Dwarnings -Zui-testing {2}",
45-
build_info.host_lib().join("deps").display(),
46-
build_info.target_lib().join("deps").display(),
47-
disambiguated.join(" ")
81+
host_lib().join("deps").display(),
82+
cargo::TARGET_LIB.join("deps").display(),
83+
third_party_crates(),
4884
));
4985

50-
config.build_base = if rustc_test_suite().is_some() {
51-
// we don't need access to the stderr files on travis
86+
config.build_base = if cargo::is_rustc_test_suite() {
87+
// This make the stderr files go to clippy OUT_DIR on rustc repo build dir
5288
let mut path = PathBuf::from(env!("OUT_DIR"));
5389
path.push("test_build_base");
5490
path
5591
} else {
56-
build_info.host_lib().join("test_build_base")
92+
host_lib().join("test_build_base")
5793
};
58-
config.rustc_path = build_info.clippy_driver_path();
94+
config.rustc_path = clippy_driver_path();
5995
config
6096
}
6197

tests/dogfood.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1+
// Dogfood cannot run on Windows
2+
#![cfg(not(windows))]
3+
14
use lazy_static::lazy_static;
25
use std::path::PathBuf;
36
use std::process::Command;
47

5-
#[allow(dead_code)]
68
mod cargo;
79

810
lazy_static! {
9-
static ref CLIPPY_PATH: PathBuf = {
10-
let build_info = cargo::BuildInfo::new();
11-
build_info.target_lib().join("cargo-clippy")
12-
};
11+
static ref CLIPPY_PATH: PathBuf = cargo::TARGET_LIB.join("cargo-clippy");
1312
}
1413

1514
#[test]
1615
fn dogfood_clippy() {
1716
// run clippy on itself and fail the test if lint warnings are reported
18-
if option_env!("RUSTC_TEST_SUITE").is_some() || cfg!(windows) {
17+
if cargo::is_rustc_test_suite() {
1918
return;
2019
}
2120
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
@@ -44,7 +43,7 @@ fn dogfood_clippy() {
4443
#[test]
4544
fn dogfood_subprojects() {
4645
// run clippy on remaining subprojects and fail the test if lint warnings are reported
47-
if option_env!("RUSTC_TEST_SUITE").is_some() || cfg!(windows) {
46+
if cargo::is_rustc_test_suite() {
4847
return;
4948
}
5049
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

0 commit comments

Comments
 (0)