Skip to content

Commit 0895469

Browse files
committed
Allow LinkType to access the Target for checking if it applies.
1 parent 6ca6dd0 commit 0895469

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct BuildOutput {
2525
/// Names and link kinds of libraries, suitable for the `-l` flag.
2626
pub library_links: Vec<String>,
2727
/// Linker arguments suitable to be passed to `-C link-arg=<args>`
28-
pub linker_args: Vec<(Option<LinkType>, String)>,
28+
pub linker_args: Vec<(LinkType, String)>,
2929
/// Various `--cfg` flags to pass to the compiler.
3030
pub cfgs: Vec<String>,
3131
/// Additional environment variables to run the compiler with.
@@ -562,18 +562,18 @@ impl BuildOutput {
562562
"rustc-link-lib" => library_links.push(value.to_string()),
563563
"rustc-link-search" => library_paths.push(PathBuf::from(value)),
564564
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
565-
linker_args.push((Some(LinkType::Cdylib), value))
565+
linker_args.push((LinkType::Cdylib, value))
566566
}
567567
"rustc-link-arg-bins" => {
568568
if extra_link_arg {
569-
linker_args.push((Some(LinkType::Bin), value));
569+
linker_args.push((LinkType::Bin, value));
570570
} else {
571571
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
572572
}
573573
}
574574
"rustc-link-arg" => {
575575
if extra_link_arg {
576-
linker_args.push((None, value));
576+
linker_args.push((LinkType::All, value));
577577
} else {
578578
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
579579
}

src/cargo/core/compiler/mod.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,23 @@ const RUSTDOC_CRATE_VERSION_FLAG: &str = "--crate-version";
6464

6565
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
6666
pub enum LinkType {
67+
All,
6768
Cdylib,
6869
Bin,
6970
Test,
7071
Bench,
7172
Example,
7273
}
7374

74-
impl From<&super::Target> for Option<LinkType> {
75-
fn from(value: &super::Target) -> Self {
76-
if value.is_cdylib() {
77-
Some(LinkType::Cdylib)
78-
} else if value.is_bin() {
79-
Some(LinkType::Bin)
80-
} else if value.is_test() {
81-
Some(LinkType::Test)
82-
} else if value.is_bench() {
83-
Some(LinkType::Bench)
84-
} else if value.is_exe_example() {
85-
Some(LinkType::Example)
86-
} else {
87-
None
75+
impl LinkType {
76+
pub fn applies_to(&self, target: &Target) -> bool {
77+
match self {
78+
LinkType::All => true,
79+
LinkType::Cdylib => target.is_cdylib(),
80+
LinkType::Bin => target.is_bin(),
81+
LinkType::Test => target.is_test(),
82+
LinkType::Bench => target.is_bench(),
83+
LinkType::Example => target.is_exe_example(),
8884
}
8985
}
9086
}
@@ -227,7 +223,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
227223
// If we are a binary and the package also contains a library, then we
228224
// don't pass the `-l` flags.
229225
let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
230-
let link_type = (&unit.target).into();
231226

232227
let dep_info_name = if cx.files().use_extra_filename(unit) {
233228
format!(
@@ -280,7 +275,7 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
280275
&script_outputs,
281276
&build_scripts,
282277
pass_l_flag,
283-
link_type,
278+
&target,
284279
current_id,
285280
)?;
286281
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
@@ -371,7 +366,7 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
371366
build_script_outputs: &BuildScriptOutputs,
372367
build_scripts: &BuildScripts,
373368
pass_l_flag: bool,
374-
link_type: Option<LinkType>,
369+
target: &Target,
375370
current_id: PackageId,
376371
) -> CargoResult<()> {
377372
for key in build_scripts.to_link.iter() {
@@ -396,11 +391,9 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
396391
}
397392
}
398393

399-
if link_type.is_some() {
400-
for (lt, arg) in &output.linker_args {
401-
if lt.is_none() || *lt == link_type {
402-
rustc.arg("-C").arg(format!("link-arg={}", arg));
403-
}
394+
for (lt, arg) in &output.linker_args {
395+
if lt.applies_to(&target) {
396+
rustc.arg("-C").arg(format!("link-arg={}", arg));
404397
}
405398
}
406399
}

src/cargo/util/config/target.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ fn parse_links_overrides(
134134
}
135135
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
136136
let args = value.list(key)?;
137-
let args = args.iter().map(|v| (Some(LinkType::Cdylib), v.0.clone()));
137+
let args = args.iter().map(|v| (LinkType::Cdylib, v.0.clone()));
138138
output.linker_args.extend(args);
139139
}
140140
"rustc-link-arg-bins" => {
141141
if extra_link_arg {
142142
let args = value.list(key)?;
143-
let args = args.iter().map(|v| (Some(LinkType::Bin), v.0.clone()));
143+
let args = args.iter().map(|v| (LinkType::Bin, v.0.clone()));
144144
output.linker_args.extend(args);
145145
} else {
146146
config.shell().warn(format!(
@@ -152,7 +152,7 @@ fn parse_links_overrides(
152152
"rustc-link-arg" => {
153153
if extra_link_arg {
154154
let args = value.list(key)?;
155-
let args = args.iter().map(|v| (None, v.0.clone()));
155+
let args = args.iter().map(|v| (LinkType::All, v.0.clone()));
156156
output.linker_args.extend(args);
157157
} else {
158158
config.shell().warn(format!(

0 commit comments

Comments
 (0)