Skip to content

Commit b556398

Browse files
committed
Auto merge of rust-lang#7646 - camsteffen:relative-target, r=flip1995
Target directory cleanup changelog: none * .cargo/config now has `target-dir` specified so that it is inherited by child projects. The target directory needs to be shared with clippy_dev, but not necessarily at the project root. (cc rust-lang#7625) * Uses `std::env::current_exe` (and its parent directories) whenever possible * `CLIPPY_DRIVER_PATH` and `TARGET_LIBS` are no longer required from rustc bootstrap (but `HOST_LIBS` still is). These can be removed from the rustc side after merging. * `CLIPPY_DOGFOOD` and the separate target directory are removed. This was originally added to mitigate rust-lang#7343. r? `@flip1995`
2 parents a64b769 + 9e08e7f commit b556398

File tree

6 files changed

+30
-80
lines changed

6 files changed

+30
-80
lines changed

.cargo/config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[alias]
22
uitest = "test --test compile-test"
3-
dev = "run --target-dir clippy_dev/target --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
4-
lintcheck = "run --target-dir lintcheck/target --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
3+
dev = "run --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
4+
lintcheck = "run --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
55
collect-metadata = "test --test dogfood --features metadata-collector-lint -- run_metadata_collection_lint --ignored"
66

77
[build]
88
# -Zbinary-dep-depinfo allows us to track which rlib files to use for compiling UI tests
99
rustflags = ["-Zunstable-options", "-Zbinary-dep-depinfo"]
10+
target-dir = "target"

clippy_dev/src/bless.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! `bless` updates the reference files in the repo with changed output files
22
//! from the last test run.
33
4-
use std::env;
54
use std::ffi::OsStr;
65
use std::fs;
76
use std::lazy::SyncLazy;
@@ -10,17 +9,9 @@ use walkdir::WalkDir;
109

1110
use crate::clippy_project_root;
1211

13-
// NOTE: this is duplicated with tests/cargo/mod.rs What to do?
14-
pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
15-
Some(v) => v.into(),
16-
None => env::current_dir().unwrap().join("target"),
17-
});
18-
1912
static CLIPPY_BUILD_TIME: SyncLazy<Option<std::time::SystemTime>> = SyncLazy::new(|| {
20-
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
21-
let mut path = PathBuf::from(&**CARGO_TARGET_DIR);
22-
path.push(profile);
23-
path.push("cargo-clippy");
13+
let mut path = std::env::current_exe().unwrap();
14+
path.set_file_name("cargo-clippy");
2415
fs::metadata(path).ok()?.modified().ok()
2516
});
2617

@@ -94,10 +85,7 @@ fn updated_since_clippy_build(path: &Path) -> Option<bool> {
9485
}
9586

9687
fn build_dir() -> PathBuf {
97-
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
98-
let mut path = PathBuf::new();
99-
path.push(CARGO_TARGET_DIR.clone());
100-
path.push(profile);
101-
path.push("test_build_base");
88+
let mut path = std::env::current_exe().unwrap();
89+
path.set_file_name("test");
10290
path
10391
}

src/main.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use rustc_tools_util::VersionInfo;
66
use std::env;
7-
use std::ffi::OsString;
87
use std::path::PathBuf;
98
use std::process::{self, Command};
109

@@ -14,7 +13,7 @@ Usage:
1413
cargo clippy [options] [--] [<opts>...]
1514
1615
Common options:
17-
--no-deps Run Clippy only on the given crate, without linting the dependencies
16+
--no-deps Run Clippy only on the given crate, without linting the dependencies
1817
--fix Automatically apply lint suggestions. This flag implies `--no-deps`
1918
-h, --help Print this message
2019
-V, --version Print version info and exit
@@ -116,22 +115,6 @@ impl ClippyCmd {
116115
path
117116
}
118117

119-
fn target_dir() -> Option<(&'static str, OsString)> {
120-
env::var_os("CLIPPY_DOGFOOD")
121-
.map(|_| {
122-
env::var_os("CARGO_MANIFEST_DIR").map_or_else(
123-
|| std::ffi::OsString::from("clippy_dogfood"),
124-
|d| {
125-
std::path::PathBuf::from(d)
126-
.join("target")
127-
.join("dogfood")
128-
.into_os_string()
129-
},
130-
)
131-
})
132-
.map(|p| ("CARGO_TARGET_DIR", p))
133-
}
134-
135118
fn into_std_cmd(self) -> Command {
136119
let mut cmd = Command::new("cargo");
137120
let clippy_args: String = self
@@ -141,7 +124,6 @@ impl ClippyCmd {
141124
.collect();
142125

143126
cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
144-
.envs(ClippyCmd::target_dir())
145127
.env("CLIPPY_ARGS", clippy_args)
146128
.arg(self.cargo_subcommand)
147129
.args(&self.args);

tests/cargo/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
use std::env;
2-
use std::lazy::SyncLazy;
3-
use std::path::PathBuf;
4-
5-
pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
6-
Some(v) => v.into(),
7-
None => env::current_dir().unwrap().join("target"),
8-
});
9-
10-
pub static TARGET_LIB: SyncLazy<PathBuf> = SyncLazy::new(|| {
11-
if let Some(path) = option_env!("TARGET_LIBS") {
12-
path.into()
13-
} else {
14-
let mut dir = CARGO_TARGET_DIR.clone();
15-
if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
16-
dir.push(target);
17-
}
18-
dir.push(env!("PROFILE"));
19-
dir
20-
}
21-
});
22-
231
#[must_use]
242
pub fn is_rustc_test_suite() -> bool {
253
option_env!("RUSTC_TEST_SUITE").is_some()

tests/compile-test.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(test)] // compiletest_rs requires this attribute
2-
#![feature(once_cell)]
32
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
43
#![warn(rust_2018_idioms, unused_lifetimes)]
54

@@ -46,14 +45,6 @@ extern crate quote;
4645
#[allow(unused_extern_crates)]
4746
extern crate syn;
4847

49-
fn host_lib() -> PathBuf {
50-
option_env!("HOST_LIBS").map_or(cargo::CARGO_TARGET_DIR.join(env!("PROFILE")), PathBuf::from)
51-
}
52-
53-
fn clippy_driver_path() -> PathBuf {
54-
option_env!("CLIPPY_DRIVER_PATH").map_or(cargo::TARGET_LIB.join("clippy-driver"), PathBuf::from)
55-
}
56-
5748
/// Produces a string with an `--extern` flag for all UI test crate
5849
/// dependencies.
5950
///
@@ -104,7 +95,7 @@ fn extern_flags() -> String {
10495
}
10596
crates
10697
.into_iter()
107-
.map(|(name, path)| format!("--extern {}={} ", name, path))
98+
.map(|(name, path)| format!(" --extern {}={}", name, path))
10899
.collect()
109100
}
110101

@@ -120,19 +111,29 @@ fn default_config() -> compiletest::Config {
120111
config.run_lib_path = path.clone();
121112
config.compile_lib_path = path;
122113
}
114+
let current_exe_path = std::env::current_exe().unwrap();
115+
let deps_path = current_exe_path.parent().unwrap();
116+
let profile_path = deps_path.parent().unwrap();
123117

124118
// Using `-L dependency={}` enforces that external dependencies are added with `--extern`.
125119
// This is valuable because a) it allows us to monitor what external dependencies are used
126120
// and b) it ensures that conflicting rlibs are resolved properly.
121+
let host_libs = option_env!("HOST_LIBS")
122+
.map(|p| format!(" -L dependency={}", Path::new(p).join("deps").display()))
123+
.unwrap_or_default();
127124
config.target_rustcflags = Some(format!(
128-
"--emit=metadata -L dependency={} -L dependency={} -Dwarnings -Zui-testing {}",
129-
host_lib().join("deps").display(),
130-
cargo::TARGET_LIB.join("deps").display(),
125+
"--emit=metadata -Dwarnings -Zui-testing -L dependency={}{}{}",
126+
deps_path.display(),
127+
host_libs,
131128
extern_flags(),
132129
));
133130

134-
config.build_base = host_lib().join("test_build_base");
135-
config.rustc_path = clippy_driver_path();
131+
config.build_base = profile_path.join("test");
132+
config.rustc_path = profile_path.join(if cfg!(windows) {
133+
"clippy-driver.exe"
134+
} else {
135+
"clippy-driver"
136+
});
136137
config
137138
}
138139

tests/dogfood.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ use std::process::Command;
1515

1616
mod cargo;
1717

18-
static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| cargo::TARGET_LIB.join("cargo-clippy"));
18+
static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| {
19+
let mut path = std::env::current_exe().unwrap();
20+
assert!(path.pop()); // deps
21+
path.set_file_name("cargo-clippy");
22+
path
23+
});
1924

2025
#[test]
2126
fn dogfood_clippy() {
@@ -28,7 +33,6 @@ fn dogfood_clippy() {
2833
let mut command = Command::new(&*CLIPPY_PATH);
2934
command
3035
.current_dir(root_dir)
31-
.env("CLIPPY_DOGFOOD", "1")
3236
.env("CARGO_INCREMENTAL", "0")
3337
.arg("clippy")
3438
.arg("--all-targets")
@@ -74,7 +78,6 @@ fn test_no_deps_ignores_path_deps_in_workspaces() {
7478
// Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
7579
let output = Command::new(&*CLIPPY_PATH)
7680
.current_dir(&cwd)
77-
.env("CLIPPY_DOGFOOD", "1")
7881
.env("CARGO_INCREMENTAL", "0")
7982
.arg("clippy")
8083
.args(&["-p", "subcrate"])
@@ -94,7 +97,6 @@ fn test_no_deps_ignores_path_deps_in_workspaces() {
9497
// Test that without the `--no-deps` argument, `path_dep` is linted.
9598
let output = Command::new(&*CLIPPY_PATH)
9699
.current_dir(&cwd)
97-
.env("CLIPPY_DOGFOOD", "1")
98100
.env("CARGO_INCREMENTAL", "0")
99101
.arg("clippy")
100102
.args(&["-p", "subcrate"])
@@ -121,7 +123,6 @@ fn test_no_deps_ignores_path_deps_in_workspaces() {
121123
let successful_build = || {
122124
let output = Command::new(&*CLIPPY_PATH)
123125
.current_dir(&cwd)
124-
.env("CLIPPY_DOGFOOD", "1")
125126
.env("CARGO_INCREMENTAL", "0")
126127
.arg("clippy")
127128
.args(&["-p", "subcrate"])
@@ -223,7 +224,6 @@ fn run_clippy_for_project(project: &str) {
223224

224225
command
225226
.current_dir(root_dir.join(project))
226-
.env("CLIPPY_DOGFOOD", "1")
227227
.env("CARGO_INCREMENTAL", "0")
228228
.arg("clippy")
229229
.arg("--all-targets")

0 commit comments

Comments
 (0)