Skip to content

Commit

Permalink
Stabilize the extra link arg option
Browse files Browse the repository at this point in the history
  • Loading branch information
danielframpton committed Jul 22, 2021
1 parent 121f4b3 commit e737039
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 157 deletions.
91 changes: 35 additions & 56 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
paths::create_dir_all(&script_dir)?;
paths::create_dir_all(&script_out_dir)?;

let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;
let nightly_features_allowed = cx.bcx.config.nightly_features_allowed;
let targets: Vec<Target> = unit.pkg.targets().to_vec();
// Need a separate copy for the fresh closure.
Expand Down Expand Up @@ -427,7 +426,6 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
&pkg_descr,
&script_out_dir,
&script_out_dir,
extra_link_arg,
nightly_features_allowed,
&targets,
)?;
Expand Down Expand Up @@ -455,7 +453,6 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
&pkg_descr,
&prev_script_out_dir,
&script_out_dir,
extra_link_arg,
nightly_features_allowed,
&targets_fresh,
)?,
Expand Down Expand Up @@ -508,7 +505,6 @@ impl BuildOutput {
pkg_descr: &str,
script_out_dir_when_generated: &Path,
script_out_dir: &Path,
extra_link_arg: bool,
nightly_features_allowed: bool,
targets: &[Target],
) -> CargoResult<BuildOutput> {
Expand All @@ -519,7 +515,6 @@ impl BuildOutput {
pkg_descr,
script_out_dir_when_generated,
script_out_dir,
extra_link_arg,
nightly_features_allowed,
targets,
)
Expand All @@ -535,7 +530,6 @@ impl BuildOutput {
pkg_descr: &str,
script_out_dir_when_generated: &Path,
script_out_dir: &Path,
extra_link_arg: bool,
nightly_features_allowed: bool,
targets: &[Target],
) -> CargoResult<BuildOutput> {
Expand Down Expand Up @@ -606,59 +600,47 @@ impl BuildOutput {
linker_args.push((LinkType::Cdylib, value))
}
"rustc-link-arg-bins" => {
if extra_link_arg {
if !targets.iter().any(|target| target.is_bin()) {
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target.",
key,
whence,
pkg_descr
);
}
linker_args.push((LinkType::Bin, value));
} else {
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
if !targets.iter().any(|target| target.is_bin()) {
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target.",
key,
whence,
pkg_descr
);
}
linker_args.push((LinkType::Bin, value));
}
"rustc-link-arg-bin" => {
if extra_link_arg {
let mut parts = value.splitn(2, '=');
let bin_name = parts.next().unwrap().to_string();
let arg = parts.next().ok_or_else(|| {
anyhow::format_err!(
"invalid instruction `cargo:{}={}` from {}\n\
The instruction should have the form cargo:{}=BIN=ARG",
key,
value,
whence,
key
)
})?;
if !targets
.iter()
.any(|target| target.is_bin() && target.name() == bin_name)
{
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target with the name `{}`.",
key,
whence,
pkg_descr,
bin_name
);
}
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
} else {
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
let mut parts = value.splitn(2, '=');
let bin_name = parts.next().unwrap().to_string();
let arg = parts.next().ok_or_else(|| {
anyhow::format_err!(
"invalid instruction `cargo:{}={}` from {}\n\
The instruction should have the form cargo:{}=BIN=ARG",
key,
value,
whence,
key
)
})?;
if !targets
.iter()
.any(|target| target.is_bin() && target.name() == bin_name)
{
bail!(
"invalid instruction `cargo:{}` from {}\n\
The package {} does not have a bin target with the name `{}`.",
key,
whence,
pkg_descr,
bin_name
);
}
linker_args.push((LinkType::SingleBin(bin_name), arg.to_string()));
}
"rustc-link-arg" => {
if extra_link_arg {
linker_args.push((LinkType::All, value));
} else {
warnings.push(format!("cargo:{} requires -Zextra-link-arg flag", key));
}
linker_args.push((LinkType::All, value));
}
"rustc-cfg" => cfgs.push(value.to_string()),
"rustc-env" => {
Expand Down Expand Up @@ -953,16 +935,13 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
.and_then(|bytes| paths::bytes2path(&bytes))
.unwrap_or_else(|_| script_out_dir.clone());

let extra_link_arg = cx.bcx.config.cli_unstable().extra_link_arg;

(
BuildOutput::parse_file(
&output_file,
unit.pkg.library().map(|t| t.crate_name()),
&unit.pkg.to_string(),
&prev_script_out_dir,
&script_out_dir,
extra_link_arg,
cx.bcx.config.nightly_features_allowed,
unit.pkg.targets(),
)
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,6 @@ unstable_cli_options!(
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
future_incompat_report: bool = ("Enable creation of a future-incompat report for all dependencies"),
extra_link_arg: bool = ("Allow `cargo:rustc-link-arg` in build scripts"),
features: Option<Vec<String>> = (HIDDEN),
jobserver_per_rustc: bool = (HIDDEN),
minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum"),
Expand Down Expand Up @@ -689,6 +688,9 @@ const STABILIZED_FEATURES: &str = "The new feature resolver is now available \
See https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-resolver-version-2 \
for more information.";

const STABILIZED_EXTRA_LINK_ARG: &str = "Additional linker arguments are now \
supported without passing this flag.";

fn deserialize_build_std<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down Expand Up @@ -859,7 +861,6 @@ impl CliUnstable {
"terminal-width" => self.terminal_width = Some(parse_usize_opt(v)?),
"namespaced-features" => self.namespaced_features = parse_empty(k, v)?,
"weak-dep-features" => self.weak_dep_features = parse_empty(k, v)?,
"extra-link-arg" => self.extra_link_arg = parse_empty(k, v)?,
"credential-process" => self.credential_process = parse_empty(k, v)?,
"skip-rustdoc-fingerprint" => self.skip_rustdoc_fingerprint = parse_empty(k, v)?,
"compile-progress" => stabilized_warn(k, "1.30", STABILIZED_COMPILE_PROGRESS),
Expand All @@ -869,6 +870,7 @@ impl CliUnstable {
"config-profile" => stabilized_warn(k, "1.43", STABILIZED_CONFIG_PROFILE),
"crate-versions" => stabilized_warn(k, "1.47", STABILIZED_CRATE_VERSIONS),
"package-features" => stabilized_warn(k, "1.51", STABILIZED_PACKAGE_FEATURES),
"extra-link-arg" => stabilized_warn(k, "1.56", STABILIZED_EXTRA_LINK_ARG),
"future-incompat-report" => self.future_incompat_report = parse_empty(k, v)?,
_ => bail!("unknown `-Z` flag specified: {}", k),
}
Expand Down
31 changes: 7 additions & 24 deletions src/cargo/util/config/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn load_config_table(config: &Config, prefix: &str) -> CargoResult<TargetConfig>
// Links do not support environment variables.
let target_key = ConfigKey::from_str(prefix);
let links_overrides = match config.get_table(&target_key)? {
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
Some(links) => parse_links_overrides(&target_key, links.val)?,
None => BTreeMap::new(),
};
Ok(TargetConfig {
Expand All @@ -135,10 +135,7 @@ fn load_config_table(config: &Config, prefix: &str) -> CargoResult<TargetConfig>
fn parse_links_overrides(
target_key: &ConfigKey,
links: HashMap<String, CV>,
config: &Config,
) -> CargoResult<BTreeMap<String, BuildOutput>> {
let extra_link_arg = config.cli_unstable().extra_link_arg;

let mut links_overrides = BTreeMap::new();
for (lib_name, value) in links {
// Skip these keys, it shares the namespace with `TargetConfig`.
Expand Down Expand Up @@ -182,28 +179,14 @@ fn parse_links_overrides(
output.linker_args.extend(args);
}
"rustc-link-arg-bins" => {
if extra_link_arg {
let args = value.list(key)?;
let args = args.iter().map(|v| (LinkType::Bin, v.0.clone()));
output.linker_args.extend(args);
} else {
config.shell().warn(format!(
"target config `{}.{}` requires -Zextra-link-arg flag",
target_key, key
))?;
}
let args = value.list(key)?;
let args = args.iter().map(|v| (LinkType::Bin, v.0.clone()));
output.linker_args.extend(args);
}
"rustc-link-arg" => {
if extra_link_arg {
let args = value.list(key)?;
let args = args.iter().map(|v| (LinkType::All, v.0.clone()));
output.linker_args.extend(args);
} else {
config.shell().warn(format!(
"target config `{}.{}` requires -Zextra-link-arg flag",
target_key, key
))?;
}
let args = value.list(key)?;
let args = args.iter().map(|v| (LinkType::All, v.0.clone()));
output.linker_args.extend(args);
}
"rustc-cfg" => {
let list = value.list(key)?;
Expand Down
38 changes: 38 additions & 0 deletions src/doc/src/reference/build-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ one detailed below.
re-run the script.
* [`cargo:rerun-if-env-changed=VAR`](#rerun-if-env-changed) — Tells Cargo when
to re-run the script.
* [`cargo:rustc-link-arg=FLAG`](#rustc-link-arg) – Passes custom flags to a
linker for benchmarks, binaries, `cdylib` crates, examples, and tests.
* [`cargo:rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) – Passes custom
flags to a linker for the binary `BIN`.
* [`cargo:rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) – Passes custom
flags to a linker for binaries.
* [`cargo:rustc-link-lib=[KIND=]NAME`](#rustc-link-lib) — Adds a library to
link.
* [`cargo:rustc-link-search=[KIND=]PATH`](#rustc-link-search) — Adds to the
Expand All @@ -110,6 +116,38 @@ one detailed below.
* [`cargo:KEY=VALUE`](#the-links-manifest-key) — Metadata, used by `links`
scripts.


<a id="rustc-link-arg"></a>
#### `cargo:rustc-link-arg=FLAG`

The `rustc-link-arg` instruction tells Cargo to pass the [`-C link-arg=FLAG`
option][link-arg] to the compiler, but only when building supported targets
(benchmarks, binaries, `cdylib` crates, examples, and tests). Its usage is
highly platform specific. It is useful to set the shared library version or
linker script.

[link-arg]: ../../rustc/codegen-options/index.md#link-arg

<a id="rustc-link-arg-bin"></a>
#### `cargo:rustc-link-arg-bin=BIN=FLAG`

The `rustc-link-arg-bin` instruction tells Cargo to pass the [`-C
link-arg=FLAG` option][link-arg] to the compiler, but only when building
the binary target with name `BIN`. Its usage is highly platform specific. It is useful
to set a linker script or other linker options.

[link-arg]: ../../rustc/codegen-options/index.md#link-arg

<a id="rustc-link-arg-bins"></a>
#### `cargo:rustc-link-arg-bins=FLAG`

The `rustc-link-arg-bins` instruction tells Cargo to pass the [`-C
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
binary target. Its usage is highly platform specific. It is useful
to set a linker script or other linker options.

[link-arg]: ../../rustc/codegen-options/index.md#link-arg

<a id="rustc-link-lib"></a>
#### `cargo:rustc-link-lib=[KIND=]NAME`

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

### extra-link-arg
* Tracking Issue: [#9426](https://github.com/rust-lang/cargo/issues/9426)
* Original Pull Request: [#7811](https://github.com/rust-lang/cargo/pull/7811)

The `-Z extra-link-arg` flag makes the following instructions available
in build scripts:

* [`cargo:rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) – Passes custom
flags to a linker for binaries.
* [`cargo:rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) – Passes custom
flags to a linker for the binary `BIN`.
* [`cargo:rustc-link-arg=FLAG`](#rustc-link-arg) – Passes custom flags to a
linker for benchmarks, binaries, `cdylib` crates, examples, and tests.

<a id="rustc-link-arg-bins"></a>
#### `cargo:rustc-link-arg-bins=FLAG`

The `rustc-link-arg-bins` instruction tells Cargo to pass the [`-C
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
binary target. Its usage is highly platform specific. It is useful
to set a linker script or other linker options.

[link-arg]: ../../rustc/codegen-options/index.md#link-arg

<a id="rustc-link-arg-bin"></a>
#### `cargo:rustc-link-arg-bin=BIN=FLAG`

The `rustc-link-arg-bin` instruction tells Cargo to pass the [`-C
link-arg=FLAG` option][link-arg] to the compiler, but only when building
the binary target with name `BIN`. Its usage is highly platform specific. It is useful
to set a linker script or other linker options.

[link-arg]: ../../rustc/codegen-options/index.md#link-arg

<a id="rustc-link-arg"></a>
#### `cargo:rustc-link-arg=FLAG`

The `rustc-link-arg` instruction tells Cargo to pass the [`-C link-arg=FLAG`
option][link-arg] to the compiler, but only when building supported targets
(benchmarks, binaries, `cdylib` crates, examples, and tests). Its usage is
highly platform specific. It is useful to set the shared library version or
linker script.

[link-arg]: ../../rustc/codegen-options/index.md#link-arg

### no-index-update
* Original Issue: [#3479](https://github.com/rust-lang/cargo/issues/3479)
* Tracking Issue: [#7404](https://github.com/rust-lang/cargo/issues/7404)
Expand Down Expand Up @@ -1479,3 +1433,10 @@ for more information on using the features CLI options.
The `resolver` feature in `Cargo.toml` has been stabilized in the 1.51 release.
See the [resolver versions](resolver.md#resolver-versions) for more
information about specifying resolvers.

### extra-link-arg

The `extra-link-arg` feature to specify additional linker arguments in build
scripts has been stabilized in the 1.56 release. See the [build script
documentation](build-scripts.md#outputs-of-the-build-script) for more
information on specifying extra linker arguments.
Loading

0 comments on commit e737039

Please sign in to comment.