Skip to content

Commit 662213b

Browse files
authored
Cleanup for rustc-link-arg-cdylib (#15326)
This does a little bit of cleanup around rustc-link-arg-cdylib build script instruction: * Reorders the documentation so that it is consistent. * Updates the documentation to mention both the new (`rustc-link-arg-cdylib`) and old (`rustc-cdylib-link-arg`) forms are documented, with the new form as the primary form. * Adds a test for the old form, since we didn't have any. Somehow I missed all this in #8441 and #9557.
2 parents 01c5293 + 5516ffa commit 662213b

File tree

2 files changed

+70
-27
lines changed

2 files changed

+70
-27
lines changed

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

+30-27
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ one detailed below.
105105
to re-run the script.
106106
* [`cargo::rustc-link-arg=FLAG`](#rustc-link-arg) --- Passes custom flags to a
107107
linker for benchmarks, binaries, `cdylib` crates, examples, and tests.
108+
* [`cargo::rustc-link-arg-cdylib=FLAG`](#rustc-cdylib-link-arg) --- Passes custom
109+
flags to a linker for cdylib crates.
108110
* [`cargo::rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) --- Passes custom
109111
flags to a linker for the binary `BIN`.
110112
* [`cargo::rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) --- Passes custom
@@ -126,8 +128,6 @@ one detailed below.
126128
* [`cargo::rustc-check-cfg=CHECK_CFG`](#rustc-check-cfg) -- Register custom `cfg`s as
127129
expected for compile-time checking of configs.
128130
* [`cargo::rustc-env=VAR=VALUE`](#rustc-env) --- Sets an environment variable.
129-
* [`cargo::rustc-cdylib-link-arg=FLAG`](#rustc-cdylib-link-arg) --- Passes custom
130-
flags to a linker for cdylib crates.
131131
- [`cargo::error=MESSAGE`](#cargo-error) --- Displays an error on the terminal.
132132
* [`cargo::warning=MESSAGE`](#cargo-warning) --- Displays a warning on the
133133
terminal.
@@ -147,6 +147,16 @@ linker script.
147147

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

150+
### `cargo::rustc-link-arg-cdylib=FLAG` {#rustc-cdylib-link-arg}
151+
152+
The `rustc-link-arg-cdylib` instruction tells Cargo to pass the [`-C
153+
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
154+
`cdylib` library target. Its usage is highly platform specific. It is useful
155+
to set the shared library version or the runtime-path.
156+
157+
For historical reasons, the `cargo::rustc-cdylib-link-arg` form is an alias
158+
for `cargo::rustc-link-arg-cdylib`, and has the same meaning.
159+
150160
### `cargo::rustc-link-arg-bin=BIN=FLAG` {#rustc-link-arg-bin}
151161

152162
The `rustc-link-arg-bin` instruction tells Cargo to pass the [`-C
@@ -161,6 +171,24 @@ link-arg=FLAG` option][link-arg] to the compiler, but only when building a
161171
binary target. Its usage is highly platform specific. It is useful
162172
to set a linker script or other linker options.
163173

174+
### `cargo::rustc-link-arg-tests=FLAG` {#rustc-link-arg-tests}
175+
176+
The `rustc-link-arg-tests` instruction tells Cargo to pass the [`-C
177+
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
178+
tests target.
179+
180+
### `cargo::rustc-link-arg-examples=FLAG` {#rustc-link-arg-examples}
181+
182+
The `rustc-link-arg-examples` instruction tells Cargo to pass the [`-C
183+
link-arg=FLAG` option][link-arg] to the compiler, but only when building an examples
184+
target.
185+
186+
### `cargo::rustc-link-arg-benches=FLAG` {#rustc-link-arg-benches}
187+
188+
The `rustc-link-arg-benches` instruction tells Cargo to pass the [`-C
189+
link-arg=FLAG` option][link-arg] to the compiler, but only when building a benchmark
190+
target.
191+
164192
### `cargo::rustc-link-lib=LIB` {#rustc-link-lib}
165193

166194
The `rustc-link-lib` instruction tells Cargo to link the given library using
@@ -185,24 +213,6 @@ The optional `KIND` may be one of `dylib`, `static`, or `framework`. See the
185213
[option-link]: ../../rustc/command-line-arguments.md#option-l-link-lib
186214
[FFI]: ../../nomicon/ffi.md
187215

188-
### `cargo::rustc-link-arg-tests=FLAG` {#rustc-link-arg-tests}
189-
190-
The `rustc-link-arg-tests` instruction tells Cargo to pass the [`-C
191-
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
192-
tests target.
193-
194-
### `cargo::rustc-link-arg-examples=FLAG` {#rustc-link-arg-examples}
195-
196-
The `rustc-link-arg-examples` instruction tells Cargo to pass the [`-C
197-
link-arg=FLAG` option][link-arg] to the compiler, but only when building an examples
198-
target.
199-
200-
### `cargo::rustc-link-arg-benches=FLAG` {#rustc-link-arg-benches}
201-
202-
The `rustc-link-arg-benches` instruction tells Cargo to pass the [`-C
203-
link-arg=FLAG` option][link-arg] to the compiler, but only when building a benchmark
204-
target.
205-
206216
### `cargo::rustc-link-search=[KIND=]PATH` {#rustc-link-search}
207217

208218
The `rustc-link-search` instruction tells Cargo to pass the [`-L`
@@ -307,13 +317,6 @@ Cargo][env-cargo].
307317
[env-macro]: ../../std/macro.env.html
308318
[env-cargo]: environment-variables.md#environment-variables-cargo-sets-for-crates
309319

310-
### `cargo::rustc-cdylib-link-arg=FLAG` {#rustc-cdylib-link-arg}
311-
312-
The `rustc-cdylib-link-arg` instruction tells Cargo to pass the [`-C
313-
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
314-
`cdylib` library target. Its usage is highly platform specific. It is useful
315-
to set the shared library version or the runtime-path.
316-
317320
### `cargo::error=MESSAGE` {#cargo-error}
318321

319322
The `error` instruction tells Cargo to display an error after the build script

tests/testsuite/build_script_extra_link_arg.rs

+40
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,43 @@ fn build_script_extra_link_arg_examples() {
403403
)
404404
.run();
405405
}
406+
407+
#[cargo_test]
408+
fn cdylib_both_forms() {
409+
// Cargo accepts two different forms for the cdylib link instruction,
410+
// which have the same meaning.
411+
let p = project()
412+
.file(
413+
"Cargo.toml",
414+
r#"
415+
[package]
416+
name = "foo"
417+
version = "0.1.0"
418+
edition = "2015"
419+
420+
[lib]
421+
crate-type = ["cdylib"]
422+
"#,
423+
)
424+
.file("src/lib.rs", "")
425+
.file(
426+
"build.rs",
427+
r#"
428+
fn main() {
429+
println!("cargo::rustc-cdylib-link-arg=--bogus-flag-one");
430+
println!("cargo::rustc-link-arg-cdylib=--bogus-flag-two");
431+
}
432+
"#,
433+
)
434+
.build();
435+
p.cargo("build -v")
436+
.without_status()
437+
.with_stderr_data(str![[r#"
438+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
439+
[RUNNING] `rustc --crate-name build_script_build [..]
440+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
441+
[RUNNING] `rustc --crate-name foo [..]--crate-type cdylib [..]-C link-arg=--bogus-flag-one -C link-arg=--bogus-flag-two[..]
442+
...
443+
"#]])
444+
.run();
445+
}

0 commit comments

Comments
 (0)