Skip to content

Commit 97007c9

Browse files
committed
Auto merge of #9557 - danielframpton:extra-link-args, r=ehuss
Stabilize the rustc-link-arg option This change removes the unstable option (tracked by #9426) and unconditionally accepts additional linker arguments (as implemented in #7811 and #8441). Documentation is moved from unstable to what appeared to be the correct location. I am not aware of any significant concerns with the option and it appears consistent with some other existing stable linker options. Please let me know if this is not the appropriate process or if there is anything that I am missing from the PR.
2 parents 9535dc3 + e737039 commit 97007c9

File tree

6 files changed

+99
-157
lines changed

6 files changed

+99
-157
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
315315
paths::create_dir_all(&script_dir)?;
316316
paths::create_dir_all(&script_out_dir)?;
317317

318-
let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
319318
let nightly_features_allowed = cx.bcx.config.nightly_features_allowed;
320319
let targets: Vec<Target> = unit.pkg.targets().to_vec();
321320
// Need a separate copy for the fresh closure.
@@ -427,7 +426,6 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
427426
&pkg_descr,
428427
&script_out_dir,
429428
&script_out_dir,
430-
extra_link_arg,
431429
nightly_features_allowed,
432430
&targets,
433431
)?;
@@ -455,7 +453,6 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
455453
&pkg_descr,
456454
&prev_script_out_dir,
457455
&script_out_dir,
458-
extra_link_arg,
459456
nightly_features_allowed,
460457
&targets_fresh,
461458
)?,
@@ -508,7 +505,6 @@ impl BuildOutput {
508505
pkg_descr: &str,
509506
script_out_dir_when_generated: &Path,
510507
script_out_dir: &Path,
511-
extra_link_arg: bool,
512508
nightly_features_allowed: bool,
513509
targets: &[Target],
514510
) -> CargoResult<BuildOutput> {
@@ -519,7 +515,6 @@ impl BuildOutput {
519515
pkg_descr,
520516
script_out_dir_when_generated,
521517
script_out_dir,
522-
extra_link_arg,
523518
nightly_features_allowed,
524519
targets,
525520
)
@@ -535,7 +530,6 @@ impl BuildOutput {
535530
pkg_descr: &str,
536531
script_out_dir_when_generated: &Path,
537532
script_out_dir: &Path,
538-
extra_link_arg: bool,
539533
nightly_features_allowed: bool,
540534
targets: &[Target],
541535
) -> CargoResult<BuildOutput> {
@@ -606,59 +600,47 @@ impl BuildOutput {
606600
linker_args.push((LinkType::Cdylib, value))
607601
}
608602
"rustc-link-arg-bins" => {
609-
if extra_link_arg {
610-
if !targets.iter().any(|target| target.is_bin()) {
611-
bail!(
612-
"invalid instruction `cargo:{}` from {}\n\
613-
The package {} does not have a bin target.",
614-
key,
615-
whence,
616-
pkg_descr
617-
);
618-
}
619-
linker_args.push((LinkType::Bin, value));
620-
} else {
621-
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
603+
if !targets.iter().any(|target| target.is_bin()) {
604+
bail!(
605+
"invalid instruction `cargo:{}` from {}\n\
606+
The package {} does not have a bin target.",
607+
key,
608+
whence,
609+
pkg_descr
610+
);
622611
}
612+
linker_args.push((LinkType::Bin, value));
623613
}
624614
"rustc-link-arg-bin" => {
625-
if extra_link_arg {
626-
let mut parts = value.splitn(2, '=');
627-
let bin_name = parts.next().unwrap().to_string();
628-
let arg = parts.next().ok_or_else(|| {
629-
anyhow::format_err!(
630-
"invalid instruction `cargo:{}={}` from {}\n\
631-
The instruction should have the form cargo:{}=BIN=ARG",
632-
key,
633-
value,
634-
whence,
635-
key
636-
)
637-
})?;
638-
if !targets
639-
.iter()
640-
.any(|target| target.is_bin() && target.name() == bin_name)
641-
{
642-
bail!(
643-
"invalid instruction `cargo:{}` from {}\n\
644-
The package {} does not have a bin target with the name `{}`.",
645-
key,
646-
whence,
647-
pkg_descr,
648-
bin_name
649-
);
650-
}
651-
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
652-
} else {
653-
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
615+
let mut parts = value.splitn(2, '=');
616+
let bin_name = parts.next().unwrap().to_string();
617+
let arg = parts.next().ok_or_else(|| {
618+
anyhow::format_err!(
619+
"invalid instruction `cargo:{}={}` from {}\n\
620+
The instruction should have the form cargo:{}=BIN=ARG",
621+
key,
622+
value,
623+
whence,
624+
key
625+
)
626+
})?;
627+
if !targets
628+
.iter()
629+
.any(|target| target.is_bin() && target.name() == bin_name)
630+
{
631+
bail!(
632+
"invalid instruction `cargo:{}` from {}\n\
633+
The package {} does not have a bin target with the name `{}`.",
634+
key,
635+
whence,
636+
pkg_descr,
637+
bin_name
638+
);
654639
}
640+
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
655641
}
656642
"rustc-link-arg" => {
657-
if extra_link_arg {
658-
linker_args.push((LinkType::All, value));
659-
} else {
660-
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
661-
}
643+
linker_args.push((LinkType::All, value));
662644
}
663645
"rustc-cfg" => cfgs.push(value.to_string()),
664646
"rustc-env" => {
@@ -953,16 +935,13 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
953935
.and_then(|bytes| paths::bytes2path(&bytes))
954936
.unwrap_or_else(|_| script_out_dir.clone());
955937

956-
let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
957-
958938
(
959939
BuildOutput::parse_file(
960940
&output_file,
961941
unit.pkg.library().map(|t| t.crate_name()),
962942
&unit.pkg.to_string(),
963943
&prev_script_out_dir,
964944
&script_out_dir,
965-
extra_link_arg,
966945
cx.bcx.config.nightly_features_allowed,
967946
unit.pkg.targets(),
968947
)

src/cargo/core/features.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,6 @@ unstable_cli_options!(
633633
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
634634
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
635635
future_incompat_report: bool = ("Enable creation of a future-incompat report for all dependencies"),
636-
extra_link_arg: bool = ("Allow `cargo:rustc-link-arg` in build scripts"),
637636
features: Option<Vec<String>> = (HIDDEN),
638637
jobserver_per_rustc: bool = (HIDDEN),
639638
minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum"),
@@ -689,6 +688,9 @@ const STABILIZED_FEATURES: &str = "The new feature resolver is now available \
689688
See https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-resolver-version-2 \
690689
for more information.";
691690

691+
const STABILIZED_EXTRA_LINK_ARG: &str = "Additional linker arguments are now \
692+
supported without passing this flag.";
693+
692694
fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
693695
where
694696
D: serde::Deserializer<'de>,
@@ -859,7 +861,6 @@ impl CliUnstable {
859861
"terminal-width" => self.terminal_width = Some(parse_usize_opt(v)?),
860862
"namespaced-features" => self.namespaced_features = parse_empty(k, v)?,
861863
"weak-dep-features" => self.weak_dep_features = parse_empty(k, v)?,
862-
"extra-link-arg" => self.extra_link_arg = parse_empty(k, v)?,
863864
"credential-process" => self.credential_process = parse_empty(k, v)?,
864865
"skip-rustdoc-fingerprint" => self.skip_rustdoc_fingerprint = parse_empty(k, v)?,
865866
"compile-progress" => stabilized_warn(k, "1.30", STABILIZED_COMPILE_PROGRESS),
@@ -869,6 +870,7 @@ impl CliUnstable {
869870
"config-profile" => stabilized_warn(k, "1.43", STABILIZED_CONFIG_PROFILE),
870871
"crate-versions" => stabilized_warn(k, "1.47", STABILIZED_CRATE_VERSIONS),
871872
"package-features" => stabilized_warn(k, "1.51", STABILIZED_PACKAGE_FEATURES),
873+
"extra-link-arg" => stabilized_warn(k, "1.56", STABILIZED_EXTRA_LINK_ARG),
872874
"future-incompat-report" => self.future_incompat_report = parse_empty(k, v)?,
873875
_ => bail!("unknown `-Z` flag specified: {}", k),
874876
}

src/cargo/util/config/target.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn load_config_table(config: &Config, prefix: &str) -> CargoResult<TargetConfig>
121121
// Links do not support environment variables.
122122
let target_key = ConfigKey::from_str(prefix);
123123
let links_overrides = match config.get_table(&target_key)? {
124-
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
124+
Some(links) => parse_links_overrides(&target_key, links.val)?,
125125
None => BTreeMap::new(),
126126
};
127127
Ok(TargetConfig {
@@ -135,10 +135,7 @@ fn load_config_table(config: &Config, prefix: &str) -> CargoResult<TargetConfig>
135135
fn parse_links_overrides(
136136
target_key: &ConfigKey,
137137
links: HashMap<String, CV>,
138-
config: &Config,
139138
) -> CargoResult<BTreeMap<String, BuildOutput>> {
140-
let extra_link_arg = config.cli_unstable().extra_link_arg;
141-
142139
let mut links_overrides = BTreeMap::new();
143140
for (lib_name, value) in links {
144141
// Skip these keys, it shares the namespace with `TargetConfig`.
@@ -182,28 +179,14 @@ fn parse_links_overrides(
182179
output.linker_args.extend(args);
183180
}
184181
"rustc-link-arg-bins" => {
185-
if extra_link_arg {
186-
let args = value.list(key)?;
187-
let args = args.iter().map(|v| (LinkType::Bin, v.0.clone()));
188-
output.linker_args.extend(args);
189-
} else {
190-
config.shell().warn(format!(
191-
"target config `{}.{}` requires -Zextra-link-arg flag",
192-
target_key, key
193-
))?;
194-
}
182+
let args = value.list(key)?;
183+
let args = args.iter().map(|v| (LinkType::Bin, v.0.clone()));
184+
output.linker_args.extend(args);
195185
}
196186
"rustc-link-arg" => {
197-
if extra_link_arg {
198-
let args = value.list(key)?;
199-
let args = args.iter().map(|v| (LinkType::All, v.0.clone()));
200-
output.linker_args.extend(args);
201-
} else {
202-
config.shell().warn(format!(
203-
"target config `{}.{}` requires -Zextra-link-arg flag",
204-
target_key, key
205-
))?;
206-
}
187+
let args = value.list(key)?;
188+
let args = args.iter().map(|v| (LinkType::All, v.0.clone()));
189+
output.linker_args.extend(args);
207190
}
208191
"rustc-cfg" => {
209192
let list = value.list(key)?;

src/doc/src/reference/build-scripts.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ one detailed below.
9494
re-run the script.
9595
* [`cargo:rerun-if-env-changed=VAR`](#rerun-if-env-changed) — Tells Cargo when
9696
to re-run the script.
97+
* [`cargo:rustc-link-arg=FLAG`](#rustc-link-arg) – Passes custom flags to a
98+
linker for benchmarks, binaries, `cdylib` crates, examples, and tests.
99+
* [`cargo:rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) – Passes custom
100+
flags to a linker for the binary `BIN`.
101+
* [`cargo:rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) – Passes custom
102+
flags to a linker for binaries.
97103
* [`cargo:rustc-link-lib=[KIND=]NAME`](#rustc-link-lib) — Adds a library to
98104
link.
99105
* [`cargo:rustc-link-search=[KIND=]PATH`](#rustc-link-search) — Adds to the
@@ -110,6 +116,38 @@ one detailed below.
110116
* [`cargo:KEY=VALUE`](#the-links-manifest-key) — Metadata, used by `links`
111117
scripts.
112118

119+
120+
<a id="rustc-link-arg"></a>
121+
#### `cargo:rustc-link-arg=FLAG`
122+
123+
The `rustc-link-arg` instruction tells Cargo to pass the [`-C link-arg=FLAG`
124+
option][link-arg] to the compiler, but only when building supported targets
125+
(benchmarks, binaries, `cdylib` crates, examples, and tests). Its usage is
126+
highly platform specific. It is useful to set the shared library version or
127+
linker script.
128+
129+
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
130+
131+
<a id="rustc-link-arg-bin"></a>
132+
#### `cargo:rustc-link-arg-bin=BIN=FLAG`
133+
134+
The `rustc-link-arg-bin` instruction tells Cargo to pass the [`-C
135+
link-arg=FLAG` option][link-arg] to the compiler, but only when building
136+
the binary target with name `BIN`. Its usage is highly platform specific. It is useful
137+
to set a linker script or other linker options.
138+
139+
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
140+
141+
<a id="rustc-link-arg-bins"></a>
142+
#### `cargo:rustc-link-arg-bins=FLAG`
143+
144+
The `rustc-link-arg-bins` instruction tells Cargo to pass the [`-C
145+
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
146+
binary target. Its usage is highly platform specific. It is useful
147+
to set a linker script or other linker options.
148+
149+
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
150+
113151
<a id="rustc-link-lib"></a>
114152
#### `cargo:rustc-link-lib=[KIND=]NAME`
115153

src/doc/src/reference/unstable.md

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Each new feature described below should explain how to use it.
6363
* Unstable-specific features
6464
* [-Z allow-features](#allow-features) — Provides a way to restrict which unstable features are used.
6565
* Build scripts and linking
66-
* [extra-link-arg](#extra-link-arg) — Allows build scripts to pass extra link arguments in more cases.
6766
* [Metabuild](#metabuild) — Provides declarative build scripts.
6867
* Resolver and features
6968
* [no-index-update](#no-index-update) — Prevents cargo from updating the index cache.
@@ -133,51 +132,6 @@ to any Rust tools that cargo ends up calling (like `rustc` or
133132
`rustdoc`). Thus, if you run `cargo -Zallow-features=`, no unstable
134133
Cargo _or_ Rust features can be used.
135134

136-
### extra-link-arg
137-
* Tracking Issue: [#9426](https://github.com/rust-lang/cargo/issues/9426)
138-
* Original Pull Request: [#7811](https://github.com/rust-lang/cargo/pull/7811)
139-
140-
The `-Z extra-link-arg` flag makes the following instructions available
141-
in build scripts:
142-
143-
* [`cargo:rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) – Passes custom
144-
flags to a linker for binaries.
145-
* [`cargo:rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) – Passes custom
146-
flags to a linker for the binary `BIN`.
147-
* [`cargo:rustc-link-arg=FLAG`](#rustc-link-arg) – Passes custom flags to a
148-
linker for benchmarks, binaries, `cdylib` crates, examples, and tests.
149-
150-
<a id="rustc-link-arg-bins"></a>
151-
#### `cargo:rustc-link-arg-bins=FLAG`
152-
153-
The `rustc-link-arg-bins` instruction tells Cargo to pass the [`-C
154-
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
155-
binary target. Its usage is highly platform specific. It is useful
156-
to set a linker script or other linker options.
157-
158-
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
159-
160-
<a id="rustc-link-arg-bin"></a>
161-
#### `cargo:rustc-link-arg-bin=BIN=FLAG`
162-
163-
The `rustc-link-arg-bin` instruction tells Cargo to pass the [`-C
164-
link-arg=FLAG` option][link-arg] to the compiler, but only when building
165-
the binary target with name `BIN`. Its usage is highly platform specific. It is useful
166-
to set a linker script or other linker options.
167-
168-
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
169-
170-
<a id="rustc-link-arg"></a>
171-
#### `cargo:rustc-link-arg=FLAG`
172-
173-
The `rustc-link-arg` instruction tells Cargo to pass the [`-C link-arg=FLAG`
174-
option][link-arg] to the compiler, but only when building supported targets
175-
(benchmarks, binaries, `cdylib` crates, examples, and tests). Its usage is
176-
highly platform specific. It is useful to set the shared library version or
177-
linker script.
178-
179-
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
180-
181135
### no-index-update
182136
* Original Issue: [#3479](https://github.com/rust-lang/cargo/issues/3479)
183137
* Tracking Issue: [#7404](https://github.com/rust-lang/cargo/issues/7404)
@@ -1479,3 +1433,10 @@ for more information on using the features CLI options.
14791433
The `resolver` feature in `Cargo.toml` has been stabilized in the 1.51 release.
14801434
See the [resolver versions](resolver.md#resolver-versions) for more
14811435
information about specifying resolvers.
1436+
1437+
### extra-link-arg
1438+
1439+
The `extra-link-arg` feature to specify additional linker arguments in build
1440+
scripts has been stabilized in the 1.56 release. See the [build script
1441+
documentation](build-scripts.md#outputs-of-the-build-script) for more
1442+
information on specifying extra linker arguments.

0 commit comments

Comments
 (0)