Skip to content

Commit e755955

Browse files
committed
Auto merge of #12211 - weihanglo:refactor-compiler-invocation, r=epage
refactor: compiler invocations
2 parents 58b22f0 + d4067e4 commit e755955

File tree

11 files changed

+190
-179
lines changed

11 files changed

+190
-179
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! [`CompileMode::RunCustomBuild`]: super::CompileMode
3232
//! [instructions]: https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script
3333
34-
use super::{fingerprint, Context, Job, LinkType, Unit, Work};
34+
use super::{fingerprint, Context, Job, Unit, Work};
3535
use crate::core::compiler::artifact;
3636
use crate::core::compiler::context::Metadata;
3737
use crate::core::compiler::job_queue::JobState;
@@ -62,7 +62,7 @@ pub struct BuildOutput {
6262
/// Names and link kinds of libraries, suitable for the `-l` flag.
6363
pub library_links: Vec<String>,
6464
/// Linker arguments suitable to be passed to `-C link-arg=<args>`
65-
pub linker_args: Vec<(LinkType, String)>,
65+
pub linker_args: Vec<(LinkArgTarget, String)>,
6666
/// Various `--cfg` flags to pass to the compiler.
6767
pub cfgs: Vec<String>,
6868
/// Various `--check-cfg` flags to pass to the compiler.
@@ -146,6 +146,47 @@ pub struct BuildDeps {
146146
pub rerun_if_env_changed: Vec<String>,
147147
}
148148

149+
/// Represents one of the instructions from `cargo:rustc-link-arg-*` build
150+
/// script instruction family.
151+
///
152+
/// In other words, indicates targets that custom linker arguments applies to.
153+
///
154+
/// See the [build script documentation][1] for more.
155+
///
156+
/// [1]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#cargorustc-link-argflag
157+
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
158+
pub enum LinkArgTarget {
159+
/// Represents `cargo:rustc-link-arg=FLAG`.
160+
All,
161+
/// Represents `cargo:rustc-cdylib-link-arg=FLAG`.
162+
Cdylib,
163+
/// Represents `cargo:rustc-link-arg-bins=FLAG`.
164+
Bin,
165+
/// Represents `cargo:rustc-link-arg-bin=BIN=FLAG`.
166+
SingleBin(String),
167+
/// Represents `cargo:rustc-link-arg-tests=FLAG`.
168+
Test,
169+
/// Represents `cargo:rustc-link-arg-benches=FLAG`.
170+
Bench,
171+
/// Represents `cargo:rustc-link-arg-examples=FLAG`.
172+
Example,
173+
}
174+
175+
impl LinkArgTarget {
176+
/// Checks if this link type applies to a given [`Target`].
177+
pub fn applies_to(&self, target: &Target) -> bool {
178+
match self {
179+
LinkArgTarget::All => true,
180+
LinkArgTarget::Cdylib => target.is_cdylib(),
181+
LinkArgTarget::Bin => target.is_bin(),
182+
LinkArgTarget::SingleBin(name) => target.is_bin() && target.name() == name,
183+
LinkArgTarget::Test => target.is_test(),
184+
LinkArgTarget::Bench => target.is_bench(),
185+
LinkArgTarget::Example => target.is_exe_example(),
186+
}
187+
}
188+
}
189+
149190
/// Prepares a `Work` that executes the target as a custom build script.
150191
pub fn prepare(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
151192
let _p = profile::start(format!(
@@ -711,10 +752,10 @@ impl BuildOutput {
711752
key, pkg_descr
712753
));
713754
}
714-
linker_args.push((LinkType::Cdylib, value))
755+
linker_args.push((LinkArgTarget::Cdylib, value))
715756
}
716757
"rustc-link-arg-bins" => {
717-
check_and_add_target!("bin", Target::is_bin, LinkType::Bin);
758+
check_and_add_target!("bin", Target::is_bin, LinkArgTarget::Bin);
718759
}
719760
"rustc-link-arg-bin" => {
720761
let mut parts = value.splitn(2, '=');
@@ -742,19 +783,19 @@ impl BuildOutput {
742783
bin_name
743784
);
744785
}
745-
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
786+
linker_args.push((LinkArgTarget::SingleBin(bin_name), arg.to_string()));
746787
}
747788
"rustc-link-arg-tests" => {
748-
check_and_add_target!("test", Target::is_test, LinkType::Test);
789+
check_and_add_target!("test", Target::is_test, LinkArgTarget::Test);
749790
}
750791
"rustc-link-arg-benches" => {
751-
check_and_add_target!("benchmark", Target::is_bench, LinkType::Bench);
792+
check_and_add_target!("benchmark", Target::is_bench, LinkArgTarget::Bench);
752793
}
753794
"rustc-link-arg-examples" => {
754-
check_and_add_target!("example", Target::is_example, LinkType::Example);
795+
check_and_add_target!("example", Target::is_example, LinkArgTarget::Example);
755796
}
756797
"rustc-link-arg" => {
757-
linker_args.push((LinkType::All, value));
798+
linker_args.push((LinkArgTarget::All, value));
758799
}
759800
"rustc-cfg" => cfgs.push(value.to_string()),
760801
"rustc-check-cfg" => {

0 commit comments

Comments
 (0)