Skip to content

Commit 9a7c861

Browse files
committed
refactor: rename and move LinkType to ExtraLinkArgFor under custom_build.rs
This is for linker arguments from build scripts, so should live there.
1 parent 52b0fe5 commit 9a7c861

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
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<(ExtraLinkArgFor, 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 ExtraLinkArgFor {
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 ExtraLinkArgFor {
176+
/// Checks if this link type applies to a given [`Target`].
177+
pub fn applies_to(&self, target: &Target) -> bool {
178+
match self {
179+
ExtraLinkArgFor::All => true,
180+
ExtraLinkArgFor::Cdylib => target.is_cdylib(),
181+
ExtraLinkArgFor::Bin => target.is_bin(),
182+
ExtraLinkArgFor::SingleBin(name) => target.is_bin() && target.name() == name,
183+
ExtraLinkArgFor::Test => target.is_test(),
184+
ExtraLinkArgFor::Bench => target.is_bench(),
185+
ExtraLinkArgFor::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((ExtraLinkArgFor::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, ExtraLinkArgFor::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((ExtraLinkArgFor::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, ExtraLinkArgFor::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, ExtraLinkArgFor::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, ExtraLinkArgFor::Example);
755796
}
756797
"rustc-link-arg" => {
757-
linker_args.push((LinkType::All, value));
798+
linker_args.push((ExtraLinkArgFor::All, value));
758799
}
759800
"rustc-cfg" => cfgs.push(value.to_string()),
760801
"rustc-check-cfg" => {

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub use self::compilation::{Compilation, Doctest, UnitOutput};
7676
pub use self::compile_kind::{CompileKind, CompileTarget};
7777
pub use self::context::{Context, Metadata};
7878
pub use self::crate_type::CrateType;
79+
pub use self::custom_build::ExtraLinkArgFor;
7980
pub use self::custom_build::{BuildOutput, BuildScriptOutputs, BuildScripts};
8081
pub(crate) use self::fingerprint::DirtyReason;
8182
pub use self::job_queue::Freshness;
@@ -99,44 +100,6 @@ use rustfix::diagnostics::Applicability;
99100

100101
const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
101102

102-
// TODO: Rename this to `ExtraLinkArgFor` or else, and move to compiler/custom_build.rs?
103-
/// Represents one of the instruction from `cargo:rustc-link-arg-*` build script
104-
/// instruction family.
105-
///
106-
/// In other words, indicates targets that custom linker arguments applies to.
107-
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
108-
pub enum LinkType {
109-
/// Represents `cargo:rustc-link-arg=FLAG`.
110-
All,
111-
/// Represents `cargo:rustc-cdylib-link-arg=FLAG`.
112-
Cdylib,
113-
/// Represents `cargo:rustc-link-arg-bins=FLAG`.
114-
Bin,
115-
/// Represents `cargo:rustc-link-arg-bin=BIN=FLAG`.
116-
SingleBin(String),
117-
/// Represents `cargo:rustc-link-arg-tests=FLAG`.
118-
Test,
119-
/// Represents `cargo:rustc-link-arg-benches=FLAG`.
120-
Bench,
121-
/// Represents `cargo:rustc-link-arg-examples=FLAG`.
122-
Example,
123-
}
124-
125-
impl LinkType {
126-
/// Checks if this link type applies to a given [`Target`].
127-
pub fn applies_to(&self, target: &Target) -> bool {
128-
match self {
129-
LinkType::All => true,
130-
LinkType::Cdylib => target.is_cdylib(),
131-
LinkType::Bin => target.is_bin(),
132-
LinkType::SingleBin(name) => target.is_bin() && target.name() == name,
133-
LinkType::Test => target.is_test(),
134-
LinkType::Bench => target.is_bench(),
135-
LinkType::Example => target.is_exe_example(),
136-
}
137-
}
138-
}
139-
140103
/// A glorified callback for executing calls to rustc. Rather than calling rustc
141104
/// directly, we'll use an `Executor`, giving clients an opportunity to intercept
142105
/// the build calls.
@@ -544,7 +507,8 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
544507
// clause should have been kept in the `if` block above. For
545508
// now, continue allowing it for cdylib only.
546509
// See https://github.com/rust-lang/cargo/issues/9562
547-
if lt.applies_to(target) && (key.0 == current_id || *lt == LinkType::Cdylib) {
510+
if lt.applies_to(target) && (key.0 == current_id || *lt == ExtraLinkArgFor::Cdylib)
511+
{
548512
rustc.arg("-C").arg(format!("link-arg={}", arg));
549513
}
550514
}

src/cargo/util/config/target.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{Config, ConfigKey, ConfigRelativePath, OptValue, PathAndArgs, StringList, CV};
2-
use crate::core::compiler::{BuildOutput, LinkType};
2+
use crate::core::compiler::{BuildOutput, ExtraLinkArgFor};
33
use crate::util::CargoResult;
44
use serde::Deserialize;
55
use std::collections::{BTreeMap, HashMap};
@@ -178,27 +178,27 @@ fn parse_links_overrides(
178178
.extend(list.iter().map(|v| PathBuf::from(&v.0)));
179179
}
180180
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
181-
let args = extra_link_args(LinkType::Cdylib, key, value)?;
181+
let args = extra_link_args(ExtraLinkArgFor::Cdylib, key, value)?;
182182
output.linker_args.extend(args);
183183
}
184184
"rustc-link-arg-bins" => {
185-
let args = extra_link_args(LinkType::Bin, key, value)?;
185+
let args = extra_link_args(ExtraLinkArgFor::Bin, key, value)?;
186186
output.linker_args.extend(args);
187187
}
188188
"rustc-link-arg" => {
189-
let args = extra_link_args(LinkType::All, key, value)?;
189+
let args = extra_link_args(ExtraLinkArgFor::All, key, value)?;
190190
output.linker_args.extend(args);
191191
}
192192
"rustc-link-arg-tests" => {
193-
let args = extra_link_args(LinkType::Test, key, value)?;
193+
let args = extra_link_args(ExtraLinkArgFor::Test, key, value)?;
194194
output.linker_args.extend(args);
195195
}
196196
"rustc-link-arg-benches" => {
197-
let args = extra_link_args(LinkType::Bench, key, value)?;
197+
let args = extra_link_args(ExtraLinkArgFor::Bench, key, value)?;
198198
output.linker_args.extend(args);
199199
}
200200
"rustc-link-arg-examples" => {
201-
let args = extra_link_args(LinkType::Example, key, value)?;
201+
let args = extra_link_args(ExtraLinkArgFor::Example, key, value)?;
202202
output.linker_args.extend(args);
203203
}
204204
"rustc-cfg" => {
@@ -237,10 +237,10 @@ fn parse_links_overrides(
237237
}
238238

239239
fn extra_link_args<'a>(
240-
link_type: LinkType,
240+
link_type: ExtraLinkArgFor,
241241
key: &str,
242242
value: &'a CV,
243-
) -> CargoResult<impl Iterator<Item = (LinkType, String)> + 'a> {
243+
) -> CargoResult<impl Iterator<Item = (ExtraLinkArgFor, String)> + 'a> {
244244
let args = value.list(key)?;
245245
Ok(args.iter().map(move |v| (link_type.clone(), v.0.clone())))
246246
}

0 commit comments

Comments
 (0)