Skip to content

Commit 2a9fade

Browse files
authored
Merge pull request #1724 from blyxyas/clippy-timer
Integrate Clippy into the project
2 parents 1d1400b + 2f6ae0f commit 2a9fade

File tree

9 files changed

+137
-30
lines changed

9 files changed

+137
-30
lines changed

collector/benchlib/src/benchmark.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,15 @@ impl<'a> BenchmarkGroup<'a> {
119119

120120
fn profile_benchmark(self, args: ProfileArgs) -> anyhow::Result<()> {
121121
let Some(benchmark) = self.benchmarks.get(args.benchmark.as_str()) else {
122-
return Err(anyhow::anyhow!("Benchmark `{}` not found. Available benchmarks: {}", args.benchmark,
123-
self.benchmarks.keys().map(|s| s.to_string()).collect::<Vec<_>>().join(", ")));
122+
return Err(anyhow::anyhow!(
123+
"Benchmark `{}` not found. Available benchmarks: {}",
124+
args.benchmark,
125+
self.benchmarks
126+
.keys()
127+
.map(|s| s.to_string())
128+
.collect::<Vec<_>>()
129+
.join(", ")
130+
));
124131
};
125132
(benchmark.profile_fn)();
126133

collector/src/bin/collector.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use collector::runtime::{
3939
use collector::runtime::{profile_runtime, RuntimeCompilationOpts};
4040
use collector::toolchain::{
4141
create_toolchain_from_published_version, get_local_toolchain, Sysroot, Toolchain,
42+
ToolchainConfig,
4243
};
4344
use collector::utils::cachegrind::cachegrind_diff;
4445
use collector::utils::{is_installed, wait_for_future};
@@ -375,6 +376,10 @@ struct CompileTimeOptions {
375376
/// The path to the local rustdoc to measure
376377
#[arg(long)]
377378
rustdoc: Option<PathBuf>,
379+
380+
/// The path to the local clippy to measure
381+
#[arg(long)]
382+
clippy: Option<PathBuf>,
378383
}
379384

380385
#[derive(Debug, clap::Args)]
@@ -660,9 +665,7 @@ fn main_result() -> anyhow::Result<i32> {
660665
let toolchain = get_local_toolchain(
661666
&[Profile::Opt],
662667
rustc,
663-
None,
664-
None,
665-
None,
668+
ToolchainConfig::default(),
666669
id,
667670
target_triple.clone(),
668671
)?;
@@ -718,9 +721,7 @@ fn main_result() -> anyhow::Result<i32> {
718721
let toolchain = get_local_toolchain(
719722
&[Profile::Opt],
720723
rustc,
721-
None,
722-
None,
723-
None,
724+
ToolchainConfig::default(),
724725
id,
725726
target_triple.clone(),
726727
)?;
@@ -756,9 +757,11 @@ fn main_result() -> anyhow::Result<i32> {
756757
let toolchain = get_local_toolchain(
757758
&profiles,
758759
&local.rustc,
759-
opts.rustdoc.as_deref(),
760-
local.cargo.as_deref(),
761-
local.id.as_deref(),
760+
*ToolchainConfig::default()
761+
.rustdoc(opts.rustdoc.as_deref())
762+
.clippy(opts.clippy.as_deref())
763+
.cargo(local.cargo.as_deref())
764+
.id(local.id.as_deref()),
762765
"",
763766
target_triple,
764767
)?;
@@ -948,9 +951,11 @@ fn main_result() -> anyhow::Result<i32> {
948951
let toolchain = get_local_toolchain(
949952
profiles,
950953
rustc,
951-
opts.rustdoc.as_deref(),
952-
local.cargo.as_deref(),
953-
local.id.as_deref(),
954+
*ToolchainConfig::default()
955+
.rustdoc(opts.rustdoc.as_deref())
956+
.clippy(opts.clippy.as_deref())
957+
.cargo(local.cargo.as_deref())
958+
.id(local.id.as_deref()),
954959
suffix,
955960
target_triple.clone(),
956961
)?;
@@ -1062,9 +1067,9 @@ fn get_local_toolchain_for_runtime_benchmarks(
10621067
get_local_toolchain(
10631068
&[Profile::Opt],
10641069
&local.rustc,
1065-
None,
1066-
local.cargo.as_deref(),
1067-
local.id.as_deref(),
1070+
*ToolchainConfig::default()
1071+
.cargo(local.cargo.as_deref())
1072+
.id(local.id.as_deref()),
10681073
"",
10691074
target_triple.to_string(),
10701075
)
@@ -1193,9 +1198,9 @@ fn bench_published_artifact(
11931198
let artifact_id = ArtifactId::Tag(toolchain.id.clone());
11941199

11951200
let profiles = if collector::version_supports_doc(&toolchain.id) {
1196-
Profile::all()
1201+
vec![Profile::Check, Profile::Debug, Profile::Doc, Profile::Opt]
11971202
} else {
1198-
Profile::all_non_doc()
1203+
vec![Profile::Check, Profile::Debug, Profile::Opt]
11991204
};
12001205
let scenarios = if collector::version_supports_incremental(&toolchain.id) {
12011206
Scenario::all()

collector/src/bin/rustc-fake.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ fn main() {
3535
let mut args = args_os.collect::<Vec<_>>();
3636
let rustc = env::var_os("RUSTC_REAL").unwrap();
3737
let actually_rustdoc = name.ends_with("rustdoc-fake");
38+
let actually_clippy = name.ends_with("clippy-fake");
3839
let tool = if actually_rustdoc {
3940
env::var_os("RUSTDOC_REAL").unwrap()
41+
} else if actually_clippy {
42+
env::var_os("CLIPPY_REAL").unwrap()
4043
} else {
4144
rustc
4245
};

collector/src/compile/benchmark/profile.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ pub enum Profile {
1212
Debug,
1313
Doc,
1414
Opt,
15+
Clippy,
1516
}
1617

1718
impl Profile {
1819
pub fn all() -> Vec<Self> {
1920
vec![Profile::Check, Profile::Debug, Profile::Doc, Profile::Opt]
2021
}
2122

23+
// This also leaves Clippy out
2224
pub fn all_non_doc() -> Vec<Self> {
2325
vec![Profile::Check, Profile::Debug, Profile::Opt]
2426
}

collector/src/compile/execute/bencher.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl<'a> BenchProcessor<'a> {
8787
Profile::Debug => database::Profile::Debug,
8888
Profile::Doc => database::Profile::Doc,
8989
Profile::Opt => database::Profile::Opt,
90+
Profile::Clippy => database::Profile::Clippy,
9091
};
9192

9293
if let Some(files) = stats.2 {

collector/src/compile/execute/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl PerfTool {
7272
}
7373
ProfileTool(LlvmLines) => match profile {
7474
Profile::Debug | Profile::Opt => Some("llvm-lines"),
75-
Profile::Check | Profile::Doc => None,
75+
Profile::Check | Profile::Doc | Profile::Clippy => None,
7676
},
7777
}
7878
}
@@ -168,6 +168,10 @@ impl<'a> CargoProcess<'a> {
168168

169169
if let Some(r) = &self.toolchain.components.rustdoc {
170170
cmd.env("RUSTDOC", &*FAKE_RUSTDOC).env("RUSTDOC_REAL", r);
171+
};
172+
173+
if let Some(c) = &self.toolchain.components.clippy {
174+
cmd.env("CLIPPY", &*FAKE_CLIPPY).env("CLIPPY_REAL", c);
171175
}
172176
cmd
173177
}
@@ -236,6 +240,7 @@ impl<'a> CargoProcess<'a> {
236240
}
237241
Profile::Debug => {}
238242
Profile::Doc => {}
243+
Profile::Clippy => {}
239244
Profile::Opt => {
240245
cmd.arg("--release");
241246
}
@@ -354,6 +359,21 @@ lazy_static::lazy_static! {
354359
}
355360
fake_rustdoc
356361
};
362+
static ref FAKE_CLIPPY: PathBuf = {
363+
let mut fake_clippy = env::current_exe().unwrap();
364+
fake_clippy.pop();
365+
fake_clippy.push("clippy-fake");
366+
// link from rustc-fake to rustdoc-fake
367+
if !fake_clippy.exists() {
368+
#[cfg(unix)]
369+
use std::os::unix::fs::symlink;
370+
#[cfg(windows)]
371+
use std::os::windows::fs::symlink_file as symlink;
372+
373+
symlink(&*FAKE_RUSTC, &fake_clippy).expect("failed to make symbolic link");
374+
}
375+
fake_clippy
376+
};
357377
}
358378

359379
/// Used to indicate if we need to retry a run.

collector/src/toolchain.rs

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ impl SysrootDownload {
123123
let components = ToolchainComponents::from_binaries_and_libdir(
124124
sysroot_bin("rustc")?,
125125
Some(sysroot_bin("rustdoc")?),
126+
sysroot_bin("cargo-clippy").ok(),
126127
sysroot_bin("cargo")?,
127128
&self.directory.join(&self.rust_sha).join("lib"),
128129
)?;
@@ -241,6 +242,7 @@ impl Toolchain {
241242
pub struct ToolchainComponents {
242243
pub rustc: PathBuf,
243244
pub rustdoc: Option<PathBuf>,
245+
pub clippy: Option<PathBuf>,
244246
pub cargo: PathBuf,
245247
pub lib_rustc: Option<PathBuf>,
246248
pub lib_std: Option<PathBuf>,
@@ -252,12 +254,14 @@ impl ToolchainComponents {
252254
fn from_binaries_and_libdir(
253255
rustc: PathBuf,
254256
rustdoc: Option<PathBuf>,
257+
clippy: Option<PathBuf>,
255258
cargo: PathBuf,
256259
libdir: &Path,
257260
) -> anyhow::Result<Self> {
258261
let mut component = ToolchainComponents {
259262
rustc,
260263
rustdoc,
264+
clippy,
261265
cargo,
262266
..Default::default()
263267
};
@@ -288,6 +292,36 @@ impl ToolchainComponents {
288292
}
289293
}
290294

295+
#[derive(Clone, Copy, Default)]
296+
pub struct ToolchainConfig<'a> {
297+
rustdoc: Option<&'a Path>,
298+
clippy: Option<&'a Path>,
299+
cargo: Option<&'a Path>,
300+
id: Option<&'a str>,
301+
}
302+
303+
impl<'a> ToolchainConfig<'a> {
304+
pub fn rustdoc(&mut self, rustdoc: Option<&'a Path>) -> &mut Self {
305+
self.rustdoc = rustdoc;
306+
self
307+
}
308+
309+
pub fn clippy(&mut self, clippy: Option<&'a Path>) -> &mut Self {
310+
self.clippy = clippy;
311+
self
312+
}
313+
314+
pub fn cargo(&mut self, cargo: Option<&'a Path>) -> &mut Self {
315+
self.cargo = cargo;
316+
self
317+
}
318+
319+
pub fn id(&mut self, id: Option<&'a str>) -> &mut Self {
320+
self.id = id;
321+
self
322+
}
323+
}
324+
291325
/// Get a toolchain from the input.
292326
/// - `rustc`: check if the given one is acceptable.
293327
/// - `rustdoc`: if one is given, check if it is acceptable. Otherwise, if
@@ -297,9 +331,7 @@ impl ToolchainComponents {
297331
pub fn get_local_toolchain(
298332
profiles: &[Profile],
299333
rustc: &str,
300-
rustdoc: Option<&Path>,
301-
cargo: Option<&Path>,
302-
id: Option<&str>,
334+
toolchain_config: ToolchainConfig<'_>,
303335
id_suffix: &str,
304336
target_triple: String,
305337
) -> anyhow::Result<Toolchain> {
@@ -354,7 +386,7 @@ pub fn get_local_toolchain(
354386
debug!("found rustc: {:?}", &rustc);
355387

356388
// When the id comes from a +toolchain, the suffix is *not* added.
357-
let id = if let Some(id) = id {
389+
let id = if let Some(id) = toolchain_config.id {
358390
let mut id = id.to_owned();
359391
id.push_str(id_suffix);
360392
id
@@ -369,7 +401,7 @@ pub fn get_local_toolchain(
369401

370402
// When specifying rustc via a path, the suffix is always added to the
371403
// id.
372-
let mut id = if let Some(id) = id {
404+
let mut id = if let Some(id) = toolchain_config.id {
373405
id.to_owned()
374406
} else {
375407
"Id".to_string()
@@ -380,7 +412,7 @@ pub fn get_local_toolchain(
380412
};
381413

382414
let rustdoc =
383-
if let Some(rustdoc) = &rustdoc {
415+
if let Some(rustdoc) = &toolchain_config.rustdoc {
384416
Some(rustdoc.canonicalize().with_context(|| {
385417
format!("failed to canonicalize rustdoc executable {:?}", rustdoc)
386418
})?)
@@ -400,7 +432,28 @@ pub fn get_local_toolchain(
400432
None
401433
};
402434

403-
let cargo = if let Some(cargo) = &cargo {
435+
let clippy = if let Some(clippy) = &toolchain_config.clippy {
436+
Some(
437+
clippy.canonicalize().with_context(|| {
438+
format!("failed to canonicalize clippy executable {:?}", clippy)
439+
})?,
440+
)
441+
} else if profiles.contains(&Profile::Clippy) {
442+
// We need a `clippy`. Look for one next to `rustc`.
443+
if let Ok(clippy) = rustc.with_file_name("cargo-clippy").canonicalize() {
444+
debug!("found clippy: {:?}", &clippy);
445+
Some(clippy)
446+
} else {
447+
anyhow::bail!(
448+
"'Clippy' build specified but '--cargo-clippy' not specified and no 'cargo-clippy' found \
449+
next to 'rustc'"
450+
);
451+
}
452+
} else {
453+
// No `clippy` provided, but none needed.
454+
None
455+
};
456+
let cargo = if let Some(cargo) = &toolchain_config.cargo {
404457
cargo
405458
.canonicalize()
406459
.with_context(|| format!("failed to canonicalize cargo executable {:?}", cargo))?
@@ -428,7 +481,9 @@ pub fn get_local_toolchain(
428481
let lib_dir = get_lib_dir_from_rustc(&rustc).context("Cannot find libdir for rustc")?;
429482

430483
Ok(Toolchain {
431-
components: ToolchainComponents::from_binaries_and_libdir(rustc, rustdoc, cargo, &lib_dir)?,
484+
components: ToolchainComponents::from_binaries_and_libdir(
485+
rustc, rustdoc, clippy, cargo, &lib_dir,
486+
)?,
432487
id,
433488
triple: target_triple,
434489
})
@@ -465,16 +520,23 @@ pub fn create_toolchain_from_published_version(
465520
};
466521
let rustc = which("rustc")?;
467522
let rustdoc = which("rustdoc")?;
523+
let clippy = which("clippy")?;
468524
let cargo = which("cargo")?;
469525

470526
debug!("Found rustc: {}", rustc.display());
471527
debug!("Found rustdoc: {}", rustdoc.display());
528+
debug!("Found clippy: {}", clippy.display());
472529
debug!("Found cargo: {}", cargo.display());
473530

474531
let lib_dir = get_lib_dir_from_rustc(&rustc)?;
475532

476-
let components =
477-
ToolchainComponents::from_binaries_and_libdir(rustc, Some(rustdoc), cargo, &lib_dir)?;
533+
let components = ToolchainComponents::from_binaries_and_libdir(
534+
rustc,
535+
Some(rustdoc),
536+
Some(clippy),
537+
cargo,
538+
&lib_dir,
539+
)?;
478540

479541
Ok(Toolchain {
480542
components,

database/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ pub enum Profile {
224224
Doc,
225225
/// An optimized "release" build
226226
Opt,
227+
/// A Clippy run
228+
Clippy,
227229
}
228230

229231
impl Profile {
@@ -233,6 +235,7 @@ impl Profile {
233235
Profile::Opt => "opt",
234236
Profile::Debug => "debug",
235237
Profile::Doc => "doc",
238+
Profile::Clippy => "clippy",
236239
}
237240
}
238241
}
@@ -245,6 +248,7 @@ impl std::str::FromStr for Profile {
245248
"debug" => Profile::Debug,
246249
"doc" => Profile::Doc,
247250
"opt" => Profile::Opt,
251+
"clippy" => Profile::Clippy,
248252
_ => return Err(format!("{} is not a profile", s)),
249253
})
250254
}

0 commit comments

Comments
 (0)