Skip to content

Commit 3ab3a64

Browse files
committed
Support test, example, and bench crates
1 parent da8896e commit 3ab3a64

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

crates/rustc_plugin/src/cli.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,13 @@ fn only_run_on_file(
168168
// Add compile filter to specify the target corresponding to the given file
169169
cmd.arg("-p").arg(format!("{}:{}", pkg.name, pkg.version));
170170

171+
// See https://doc.rust-lang.org/cargo/commands/cargo-check.html#target-selection for possible compile kinds
171172
enum CompileKind {
172173
Lib,
173174
Bin,
175+
Example,
176+
Test,
177+
Bench,
174178
ProcMacro,
175179
}
176180

@@ -181,6 +185,9 @@ fn only_run_on_file(
181185
"lib" | "rlib" | "dylib" | "staticlib" | "cdylib" => CompileKind::Lib,
182186
"bin" => CompileKind::Bin,
183187
"proc-macro" => CompileKind::ProcMacro,
188+
"example" => CompileKind::Example,
189+
"test" => CompileKind::Test,
190+
"bench" => CompileKind::Bench,
184191
_ => unreachable!("unexpected cargo crate type: {kind_str}"),
185192
};
186193

@@ -207,10 +214,37 @@ fn only_run_on_file(
207214
cmd.args(["--bin", &target.name]);
208215
}
209216
CompileKind::ProcMacro => {}
217+
CompileKind::Example => {
218+
cmd.args(["--example", &target.name]);
219+
}
220+
CompileKind::Test => {
221+
cmd.args(["--test", &target.name]);
222+
}
223+
CompileKind::Bench => {
224+
cmd.args(["--bench", &target.name]);
225+
}
210226
}
211227

212-
cmd.env(SPECIFIC_CRATE, pkg.name.replace('-', "_"));
213-
cmd.env(SPECIFIC_TARGET, kind_str);
228+
cmd.env(
229+
SPECIFIC_CRATE,
230+
match kind {
231+
CompileKind::Lib => &pkg.name,
232+
CompileKind::Bin => &pkg.name,
233+
CompileKind::Example => &target.name,
234+
CompileKind::Test => &target.name,
235+
CompileKind::Bench => &target.name,
236+
CompileKind::ProcMacro => &pkg.name,
237+
}
238+
.replace('-', "_"),
239+
);
240+
cmd.env(
241+
SPECIFIC_TARGET,
242+
if matches!(kind, CompileKind::Bench | CompileKind::Example) {
243+
"bin"
244+
} else {
245+
kind_str
246+
},
247+
);
214248

215249
log::debug!(
216250
"Package: {}, target kind {}, target name {}",

crates/rustc_plugin/src/driver.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,7 @@ pub fn driver_main<T: RustcPlugin>(plugin: T) {
138138
let primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
139139
let run_on_all_crates = env::var(RUN_ON_ALL_CRATES).is_ok();
140140
let normal_rustc = arg_value(&args, "--print", |_| true).is_some();
141-
let is_target_crate = match (env::var(SPECIFIC_CRATE), env::var(SPECIFIC_TARGET)) {
142-
(Ok(krate), Ok(target)) => {
143-
arg_value(&args, "--crate-name", |name| name == krate).is_some()
144-
&& arg_value(&args, "--crate-type", |name| name == target).is_some()
145-
}
146-
_ => true,
147-
};
141+
let is_target_crate = is_target_crate(&args);
148142
let run_plugin =
149143
!normal_rustc && (run_on_all_crates || primary_package) && is_target_crate;
150144

@@ -165,3 +159,17 @@ is_target_crate={is_target_crate}"
165159
}
166160
}))
167161
}
162+
163+
fn is_target_crate(args: &[String]) -> bool {
164+
log::debug!("SPECIFIC_CRATE {:?}", env::var(SPECIFIC_CRATE));
165+
log::debug!("SPECIFIC_TARGET {:?}", env::var(SPECIFIC_TARGET));
166+
log::debug!("all args: {:?}", env::args());
167+
match (env::var(SPECIFIC_CRATE), env::var(SPECIFIC_TARGET)) {
168+
(Ok(krate), Ok(target)) => {
169+
arg_value(args, "--crate-name", |name| name == krate).is_some()
170+
&& (arg_value(args, "--crate-type", |_| true).is_none() // integration test crate
171+
|| arg_value(args, "--crate-type", |name| name == target).is_some())
172+
}
173+
_ => true,
174+
}
175+
}

0 commit comments

Comments
 (0)