Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions crates/rustc_plugin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,13 @@ fn only_run_on_file(
// Add compile filter to specify the target corresponding to the given file
cmd.arg("-p").arg(format!("{}:{}", pkg.name, pkg.version));

// See https://doc.rust-lang.org/cargo/commands/cargo-check.html#target-selection for possible compile kinds
enum CompileKind {
Lib,
Bin,
Example,
Test,
Bench,
ProcMacro,
}

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

Expand All @@ -207,10 +214,37 @@ fn only_run_on_file(
cmd.args(["--bin", &target.name]);
}
CompileKind::ProcMacro => {}
CompileKind::Example => {
cmd.args(["--example", &target.name]);
}
CompileKind::Test => {
cmd.args(["--test", &target.name]);
}
CompileKind::Bench => {
cmd.args(["--bench", &target.name]);
}
}

cmd.env(SPECIFIC_CRATE, pkg.name.replace('-', "_"));
cmd.env(SPECIFIC_TARGET, kind_str);
cmd.env(
SPECIFIC_CRATE,
match kind {
CompileKind::Lib => &pkg.name,
CompileKind::Bin => &pkg.name,
CompileKind::Example => &target.name,
CompileKind::Test => &target.name,
CompileKind::Bench => &target.name,
CompileKind::ProcMacro => &pkg.name,
}
.replace('-', "_"),
);
cmd.env(
SPECIFIC_TARGET,
if matches!(kind, CompileKind::Bench | CompileKind::Example) {
"bin"
} else {
kind_str
},
);

log::debug!(
"Package: {}, target kind {}, target name {}",
Expand Down
19 changes: 12 additions & 7 deletions crates/rustc_plugin/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,7 @@ pub fn driver_main<T: RustcPlugin>(plugin: T) {
let primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
let run_on_all_crates = env::var(RUN_ON_ALL_CRATES).is_ok();
let normal_rustc = arg_value(&args, "--print", |_| true).is_some();
let is_target_crate = match (env::var(SPECIFIC_CRATE), env::var(SPECIFIC_TARGET)) {
(Ok(krate), Ok(target)) => {
arg_value(&args, "--crate-name", |name| name == krate).is_some()
&& arg_value(&args, "--crate-type", |name| name == target).is_some()
}
_ => true,
};
let is_target_crate = is_target_crate(&args);
let run_plugin =
!normal_rustc && (run_on_all_crates || primary_package) && is_target_crate;

Expand All @@ -165,3 +159,14 @@ is_target_crate={is_target_crate}"
}
}))
}

fn is_target_crate(args: &[String]) -> bool {
match (env::var(SPECIFIC_CRATE), env::var(SPECIFIC_TARGET)) {
(Ok(krate), Ok(target)) => {
arg_value(args, "--crate-name", |name| name == krate).is_some()
&& (arg_value(args, "--crate-type", |_| true).is_none() // integration test crate
|| arg_value(args, "--crate-type", |name| name == target).is_some())
}
_ => true,
}
}