Skip to content

Commit 04ac517

Browse files
committed
Update build script docs
Signed-off-by: hi-rustin <[email protected]>
1 parent 57a56e9 commit 04ac517

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

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

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ that script and execute it just before building the package.
1515
// Example custom build script.
1616
fn main() {
1717
// Tell Cargo that if the given file changes, to rerun this build script.
18-
println!("cargo:rerun-if-changed=src/hello.c");
18+
println!("cargo::rerun-if-changed=src/hello.c");
1919
// Use the `cc` crate to build a C file and statically link it.
2020
cc::Build::new()
2121
.file("src/hello.c")
@@ -42,7 +42,7 @@ scripts.
4242
Just before a package is built, Cargo will compile a build script into an
4343
executable (if it has not already been built). It will then run the script,
4444
which may perform any number of tasks. The script may communicate with Cargo
45-
by printing specially formatted commands prefixed with `cargo:` to stdout.
45+
by printing specially formatted commands prefixed with `cargo::` to stdout.
4646

4747
The build script will be rebuilt if any of its source files or dependencies
4848
change.
@@ -74,16 +74,23 @@ directory specified in the [`OUT_DIR` environment variable][build-env]. Scripts
7474
should not modify any files outside of that directory.
7575

7676
Build scripts communicate with Cargo by printing to stdout. Cargo will
77-
interpret each line that starts with `cargo:` as an instruction that will
77+
interpret each line that starts with `cargo::` as an instruction that will
7878
influence compilation of the package. All other lines are ignored.
7979

80-
> Note: The order of `cargo:` instructions printed by the build script *may*
80+
After the merge of [#12201], various instruction formats are now available, including:
81+
82+
* `cargo::KEY=VALUE` or `cargo:KEY=VALUE` for setting Cargo recognized keys.
83+
* `cargo::metadata=KEY=VALUE` or `cargo:KEY=VALUE` for setting unrecognized metadata keys.
84+
85+
[#12201]: https://github.com/rust-lang/cargo/pull/12201
86+
87+
> Note: The order of `cargo::` instructions printed by the build script *may*
8188
> affect the order of arguments that `cargo` passes to `rustc`. In turn, the
8289
> order of arguments passed to `rustc` may affect the order of arguments passed
8390
> to the linker. Therefore, you will want to pay attention to the order of the
8491
> build script's instructions. For example, if object `foo` needs to link against
8592
> library `bar`, you may want to make sure that library `bar`'s
86-
> [`cargo:rustc-link-lib`](#rustc-link-lib) instruction appears *after*
93+
> [`cargo::rustc-link-lib`](#rustc-link-lib) instruction appears *after*
8794
> instructions to link object `foo`.
8895
8996
The output of the script is hidden from the terminal during normal
@@ -99,41 +106,41 @@ configuration). The stderr output is also saved in that same directory.
99106
The following is a summary of the instructions that Cargo recognizes, with each
100107
one detailed below.
101108

102-
* [`cargo:rerun-if-changed=PATH`](#rerun-if-changed) --- Tells Cargo when to
109+
* [`cargo::rerun-if-changed=PATH`](#rerun-if-changed) --- Tells Cargo when to
103110
re-run the script.
104-
* [`cargo:rerun-if-env-changed=VAR`](#rerun-if-env-changed) --- Tells Cargo when
111+
* [`cargo::rerun-if-env-changed=VAR`](#rerun-if-env-changed) --- Tells Cargo when
105112
to re-run the script.
106-
* [`cargo:rustc-link-arg=FLAG`](#rustc-link-arg) --- Passes custom flags to a
113+
* [`cargo::rustc-link-arg=FLAG`](#rustc-link-arg) --- Passes custom flags to a
107114
linker for benchmarks, binaries, `cdylib` crates, examples, and tests.
108-
* [`cargo:rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) --- Passes custom
115+
* [`cargo::rustc-link-arg-bin=BIN=FLAG`](#rustc-link-arg-bin) --- Passes custom
109116
flags to a linker for the binary `BIN`.
110-
* [`cargo:rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) --- Passes custom
117+
* [`cargo::rustc-link-arg-bins=FLAG`](#rustc-link-arg-bins) --- Passes custom
111118
flags to a linker for binaries.
112-
* [`cargo:rustc-link-arg-tests=FLAG`](#rustc-link-arg-tests) --- Passes custom
119+
* [`cargo::rustc-link-arg-tests=FLAG`](#rustc-link-arg-tests) --- Passes custom
113120
flags to a linker for tests.
114-
* [`cargo:rustc-link-arg-examples=FLAG`](#rustc-link-arg-examples) --- Passes custom
121+
* [`cargo::rustc-link-arg-examples=FLAG`](#rustc-link-arg-examples) --- Passes custom
115122
flags to a linker for examples.
116-
* [`cargo:rustc-link-arg-benches=FLAG`](#rustc-link-arg-benches) --- Passes custom
123+
* [`cargo::rustc-link-arg-benches=FLAG`](#rustc-link-arg-benches) --- Passes custom
117124
flags to a linker for benchmarks.
118-
* [`cargo:rustc-link-lib=LIB`](#rustc-link-lib) --- Adds a library to
125+
* [`cargo::rustc-link-lib=LIB`](#rustc-link-lib) --- Adds a library to
119126
link.
120-
* [`cargo:rustc-link-search=[KIND=]PATH`](#rustc-link-search) --- Adds to the
127+
* [`cargo::rustc-link-search=[KIND=]PATH`](#rustc-link-search) --- Adds to the
121128
library search path.
122-
* [`cargo:rustc-flags=FLAGS`](#rustc-flags) --- Passes certain flags to the
129+
* [`cargo::rustc-flags=FLAGS`](#rustc-flags) --- Passes certain flags to the
123130
compiler.
124-
* [`cargo:rustc-cfg=KEY[="VALUE"]`](#rustc-cfg) --- Enables compile-time `cfg`
131+
* [`cargo::rustc-cfg=KEY[="VALUE"]`](#rustc-cfg) --- Enables compile-time `cfg`
125132
settings.
126-
* [`cargo:rustc-env=VAR=VALUE`](#rustc-env) --- Sets an environment variable.
127-
* [`cargo:rustc-cdylib-link-arg=FLAG`](#rustc-cdylib-link-arg) --- Passes custom
133+
* [`cargo::rustc-env=VAR=VALUE`](#rustc-env) --- Sets an environment variable.
134+
* [`cargo::rustc-cdylib-link-arg=FLAG`](#rustc-cdylib-link-arg) --- Passes custom
128135
flags to a linker for cdylib crates.
129-
* [`cargo:warning=MESSAGE`](#cargo-warning) --- Displays a warning on the
136+
* [`cargo::warning=MESSAGE`](#cargo-warning) --- Displays a warning on the
130137
terminal.
131-
* [`cargo:KEY=VALUE`](#the-links-manifest-key) --- Metadata, used by `links`
138+
* [`cargo::metadata=KEY=VALUE`](#the-links-manifest-key) --- Metadata, used by `links`
132139
scripts.
133140

134141

135142
<a id="rustc-link-arg"></a>
136-
#### `cargo:rustc-link-arg=FLAG`
143+
#### `cargo::rustc-link-arg=FLAG`
137144

138145
The `rustc-link-arg` instruction tells Cargo to pass the [`-C link-arg=FLAG`
139146
option][link-arg] to the compiler, but only when building supported targets
@@ -144,7 +151,7 @@ linker script.
144151
[link-arg]: ../../rustc/codegen-options/index.md#link-arg
145152

146153
<a id="rustc-link-arg-bin"></a>
147-
#### `cargo:rustc-link-arg-bin=BIN=FLAG`
154+
#### `cargo::rustc-link-arg-bin=BIN=FLAG`
148155

149156
The `rustc-link-arg-bin` instruction tells Cargo to pass the [`-C
150157
link-arg=FLAG` option][link-arg] to the compiler, but only when building
@@ -153,7 +160,7 @@ to set a linker script or other linker options.
153160

154161

155162
<a id="rustc-link-arg-bins"></a>
156-
#### `cargo:rustc-link-arg-bins=FLAG`
163+
#### `cargo::rustc-link-arg-bins=FLAG`
157164

158165
The `rustc-link-arg-bins` instruction tells Cargo to pass the [`-C
159166
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
@@ -162,7 +169,7 @@ to set a linker script or other linker options.
162169

163170

164171
<a id="rustc-link-lib"></a>
165-
#### `cargo:rustc-link-lib=LIB`
172+
#### `cargo::rustc-link-lib=LIB`
166173

167174
The `rustc-link-lib` instruction tells Cargo to link the given library using
168175
the compiler's [`-l` flag][option-link]. This is typically used to link a
@@ -188,29 +195,29 @@ The optional `KIND` may be one of `dylib`, `static`, or `framework`. See the
188195

189196

190197
<a id="rustc-link-arg-tests"></a>
191-
#### `cargo:rustc-link-arg-tests=FLAG`
198+
#### `cargo::rustc-link-arg-tests=FLAG`
192199

193200
The `rustc-link-arg-tests` instruction tells Cargo to pass the [`-C
194201
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
195202
tests target.
196203

197204

198205
<a id="rustc-link-arg-examples"></a>
199-
#### `cargo:rustc-link-arg-examples=FLAG`
206+
#### `cargo::rustc-link-arg-examples=FLAG`
200207

201208
The `rustc-link-arg-examples` instruction tells Cargo to pass the [`-C
202209
link-arg=FLAG` option][link-arg] to the compiler, but only when building an examples
203210
target.
204211

205212
<a id="rustc-link-arg-benches"></a>
206-
#### `cargo:rustc-link-arg-benches=FLAG`
213+
#### `cargo::rustc-link-arg-benches=FLAG`
207214

208215
The `rustc-link-arg-benches` instruction tells Cargo to pass the [`-C
209216
link-arg=FLAG` option][link-arg] to the compiler, but only when building a benchmark
210217
target.
211218

212219
<a id="rustc-link-search"></a>
213-
#### `cargo:rustc-link-search=[KIND=]PATH`
220+
#### `cargo::rustc-link-search=[KIND=]PATH`
214221

215222
The `rustc-link-search` instruction tells Cargo to pass the [`-L`
216223
flag][option-search] to the compiler to add a directory to the library search
@@ -229,15 +236,15 @@ is fine).
229236
[option-search]: ../../rustc/command-line-arguments.md#option-l-search-path
230237

231238
<a id="rustc-flags"></a>
232-
#### `cargo:rustc-flags=FLAGS`
239+
#### `cargo::rustc-flags=FLAGS`
233240

234241
The `rustc-flags` instruction tells Cargo to pass the given space-separated
235242
flags to the compiler. This only allows the `-l` and `-L` flags, and is
236243
equivalent to using [`rustc-link-lib`](#rustc-link-lib) and
237244
[`rustc-link-search`](#rustc-link-search).
238245

239246
<a id="rustc-cfg"></a>
240-
#### `cargo:rustc-cfg=KEY[="VALUE"]`
247+
#### `cargo::rustc-cfg=KEY[="VALUE"]`
241248

242249
The `rustc-cfg` instruction tells Cargo to pass the given value to the
243250
[`--cfg` flag][option-cfg] to the compiler. This may be used for compile-time
@@ -249,17 +256,17 @@ used to enable an optional dependency, or enable other Cargo features.
249256
Be aware that [Cargo features] use the form `feature="foo"`. `cfg` values
250257
passed with this flag are not restricted to that form, and may provide just a
251258
single identifier, or any arbitrary key/value pair. For example, emitting
252-
`cargo:rustc-cfg=abc` will then allow code to use `#[cfg(abc)]` (note the lack
259+
`cargo::rustc-cfg=abc` will then allow code to use `#[cfg(abc)]` (note the lack
253260
of `feature=`). Or an arbitrary key/value pair may be used with an `=` symbol
254-
like `cargo:rustc-cfg=my_component="foo"`. The key should be a Rust
261+
like `cargo::rustc-cfg=my_component="foo"`. The key should be a Rust
255262
identifier, the value should be a string.
256263

257264
[cargo features]: features.md
258265
[conditional compilation]: ../../reference/conditional-compilation.md
259266
[option-cfg]: ../../rustc/command-line-arguments.md#option-cfg
260267

261268
<a id="rustc-env"></a>
262-
#### `cargo:rustc-env=VAR=VALUE`
269+
#### `cargo::rustc-env=VAR=VALUE`
263270

264271
The `rustc-env` instruction tells Cargo to set the given environment variable
265272
when compiling the package. The value can be then retrieved by the [`env!`
@@ -280,7 +287,7 @@ Cargo][env-cargo].
280287
[env-cargo]: environment-variables.md#environment-variables-cargo-sets-for-crates
281288

282289
<a id="rustc-cdylib-link-arg"></a>
283-
#### `cargo:rustc-cdylib-link-arg=FLAG`
290+
#### `cargo::rustc-cdylib-link-arg=FLAG`
284291

285292
The `rustc-cdylib-link-arg` instruction tells Cargo to pass the [`-C
286293
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
@@ -289,7 +296,7 @@ to set the shared library version or the runtime-path.
289296

290297

291298
<a id="cargo-warning"></a>
292-
#### `cargo:warning=MESSAGE`
299+
#### `cargo::warning=MESSAGE`
293300

294301
The `warning` instruction tells Cargo to display a warning after the build
295302
script has finished running. Warnings are only shown for `path` dependencies
@@ -335,7 +342,7 @@ FAQ](../faq.md#why-is-cargo-rebuilding-my-code).
335342
[`exclude` and `include` fields]: manifest.md#the-exclude-and-include-fields
336343

337344
<a id="rerun-if-changed"></a>
338-
#### `cargo:rerun-if-changed=PATH`
345+
#### `cargo::rerun-if-changed=PATH`
339346

340347
The `rerun-if-changed` instruction tells Cargo to re-run the build script if
341348
the file at the given path has changed. Currently, Cargo only uses the
@@ -347,15 +354,15 @@ If the path points to a directory, it will scan the entire directory for
347354
any modifications.
348355

349356
If the build script inherently does not need to re-run under any circumstance,
350-
then emitting `cargo:rerun-if-changed=build.rs` is a simple way to prevent it
357+
then emitting `cargo::rerun-if-changed=build.rs` is a simple way to prevent it
351358
from being re-run (otherwise, the default if no `rerun-if` instructions are
352359
emitted is to scan the entire package directory for changes). Cargo
353360
automatically handles whether or not the script itself needs to be recompiled,
354361
and of course the script will be re-run after it has been recompiled.
355362
Otherwise, specifying `build.rs` is redundant and unnecessary.
356363

357364
<a id="rerun-if-env-changed"></a>
358-
#### `cargo:rerun-if-env-changed=NAME`
365+
#### `cargo::rerun-if-env-changed=NAME`
359366

360367
The `rerun-if-env-changed` instruction tells Cargo to re-run the build script
361368
if the value of an environment variable of the given name has changed.

0 commit comments

Comments
 (0)