Skip to content

Commit bd230e8

Browse files
committed
Update build script docs
Signed-off-by: hi-rustin <[email protected]>
1 parent dfb2795 commit bd230e8

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

src/doc/src/reference/build-script-examples.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn main() {
7171
}
7272
"
7373
).unwrap();
74-
println!("cargo:rerun-if-changed=build.rs");
74+
println!("cargo::rerun-if-changed=build.rs");
7575
}
7676
```
7777

@@ -173,9 +173,9 @@ fn main() {
173173
.current_dir(&Path::new(&out_dir))
174174
.status().unwrap();
175175
176-
println!("cargo:rustc-link-search=native={}", out_dir);
177-
println!("cargo:rustc-link-lib=static=hello");
178-
println!("cargo:rerun-if-changed=src/hello.c");
176+
println!("cargo::rustc-link-search=native={}", out_dir);
177+
println!("cargo::rustc-link-lib=static=hello");
178+
println!("cargo::rerun-if-changed=src/hello.c");
179179
}
180180
```
181181

@@ -214,7 +214,7 @@ fn main() {
214214
cc::Build::new()
215215
.file("src/hello.c")
216216
.compile("hello");
217-
println!("cargo:rerun-if-changed=src/hello.c");
217+
println!("cargo::rerun-if-changed=src/hello.c");
218218
}
219219
```
220220

@@ -316,7 +316,7 @@ The build script is fairly simple:
316316
317317
fn main() {
318318
pkg_config::Config::new().probe("zlib").unwrap();
319-
println!("cargo:rerun-if-changed=build.rs");
319+
println!("cargo::rerun-if-changed=build.rs");
320320
}
321321
```
322322

@@ -344,9 +344,9 @@ Run `cargo build -vv` to see the output from the build script. On a system
344344
with `libz` already installed, it may look something like this:
345345

346346
```text
347-
[libz-sys 0.1.0] cargo:rustc-link-search=native=/usr/lib
348-
[libz-sys 0.1.0] cargo:rustc-link-lib=z
349-
[libz-sys 0.1.0] cargo:rerun-if-changed=build.rs
347+
[libz-sys 0.1.0] cargo::rustc-link-search=native=/usr/lib
348+
[libz-sys 0.1.0] cargo::rustc-link-lib=z
349+
[libz-sys 0.1.0] cargo::rerun-if-changed=build.rs
350350
```
351351

352352
Nice! `pkg-config` did all the work of finding the library and telling Cargo
@@ -408,7 +408,7 @@ fn main() {
408408
cfg.include(include);
409409
}
410410
cfg.compile("zuser");
411-
println!("cargo:rerun-if-changed=src/zuser.c");
411+
println!("cargo::rerun-if-changed=src/zuser.c");
412412
}
413413
```
414414

@@ -440,7 +440,7 @@ script looks something [like
440440
this](https://github.com/sfackler/rust-openssl/blob/dc72a8e2c429e46c275e528b61a733a66e7877fc/openssl-sys/build/main.rs#L216):
441441

442442
```rust,ignore
443-
println!("cargo:version_number={:x}", openssl_version);
443+
println!("cargo::version_number={:x}", openssl_version);
444444
```
445445

446446
This instruction causes the `DEP_OPENSSL_VERSION_NUMBER` environment variable
@@ -460,19 +460,19 @@ if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
460460
let version = u64::from_str_radix(&version, 16).unwrap();
461461
462462
if version >= 0x1_00_01_00_0 {
463-
println!("cargo:rustc-cfg=ossl101");
463+
println!("cargo::rustc-cfg=ossl101");
464464
}
465465
if version >= 0x1_00_02_00_0 {
466-
println!("cargo:rustc-cfg=ossl102");
466+
println!("cargo::rustc-cfg=ossl102");
467467
}
468468
if version >= 0x1_01_00_00_0 {
469-
println!("cargo:rustc-cfg=ossl110");
469+
println!("cargo::rustc-cfg=ossl110");
470470
}
471471
if version >= 0x1_01_00_07_0 {
472-
println!("cargo:rustc-cfg=ossl110g");
472+
println!("cargo::rustc-cfg=ossl110g");
473473
}
474474
if version >= 0x1_01_01_00_0 {
475-
println!("cargo:rustc-cfg=ossl111");
475+
println!("cargo::rustc-cfg=ossl111");
476476
}
477477
}
478478
```

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

Lines changed: 46 additions & 46 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+
> **Note:** The old invocation prefix `cargo:` (one colon only) is deprecated
81+
> and won't get any new features. To migrate, use two-colons prefix `cargo::`,
82+
> which was added in Rust 1.77. If you were using `cargo:KEY=VALUE` for
83+
> arbitrary links manifest key-value pairs, it is encouraged to switch to
84+
> `cargo::metadata=KEY=VALUE`. Stick to `cargo:` only if the support of Rust
85+
> version older than 1.77 is required.
86+
87+
> 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,40 +106,39 @@ 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

134-
135-
### `cargo:rustc-link-arg=FLAG` {#rustc-link-arg}
141+
### `cargo::rustc-link-arg=FLAG` {#rustc-link-arg}
136142

137143
The `rustc-link-arg` instruction tells Cargo to pass the [`-C link-arg=FLAG`
138144
option][link-arg] to the compiler, but only when building supported targets
@@ -142,23 +148,21 @@ linker script.
142148

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

145-
### `cargo:rustc-link-arg-bin=BIN=FLAG` {#rustc-link-arg-bin}
151+
### `cargo::rustc-link-arg-bin=BIN=FLAG` {#rustc-link-arg-bin}
146152

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

152-
153-
### `cargo:rustc-link-arg-bins=FLAG` {#rustc-link-arg-bins}
158+
### `cargo::rustc-link-arg-bins=FLAG` {#rustc-link-arg-bins}
154159

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

160-
161-
### `cargo:rustc-link-lib=LIB` {#rustc-link-lib}
165+
### `cargo::rustc-link-lib=LIB` {#rustc-link-lib}
162166

163167
The `rustc-link-lib` instruction tells Cargo to link the given library using
164168
the compiler's [`-l` flag][option-link]. This is typically used to link a
@@ -182,27 +186,25 @@ The optional `KIND` may be one of `dylib`, `static`, or `framework`. See the
182186
[option-link]: ../../rustc/command-line-arguments.md#option-l-link-lib
183187
[FFI]: ../../nomicon/ffi.md
184188

185-
186-
### `cargo:rustc-link-arg-tests=FLAG` {#rustc-link-arg-tests}
189+
### `cargo::rustc-link-arg-tests=FLAG` {#rustc-link-arg-tests}
187190

188191
The `rustc-link-arg-tests` instruction tells Cargo to pass the [`-C
189192
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
190193
tests target.
191194

192-
193-
### `cargo:rustc-link-arg-examples=FLAG` {#rustc-link-arg-examples}
195+
### `cargo::rustc-link-arg-examples=FLAG` {#rustc-link-arg-examples}
194196

195197
The `rustc-link-arg-examples` instruction tells Cargo to pass the [`-C
196198
link-arg=FLAG` option][link-arg] to the compiler, but only when building an examples
197199
target.
198200

199-
### `cargo:rustc-link-arg-benches=FLAG` {#rustc-link-arg-benches}
201+
### `cargo::rustc-link-arg-benches=FLAG` {#rustc-link-arg-benches}
200202

201203
The `rustc-link-arg-benches` instruction tells Cargo to pass the [`-C
202204
link-arg=FLAG` option][link-arg] to the compiler, but only when building a benchmark
203205
target.
204206

205-
### `cargo:rustc-link-search=[KIND=]PATH` {#rustc-link-search}
207+
### `cargo::rustc-link-search=[KIND=]PATH` {#rustc-link-search}
206208

207209
The `rustc-link-search` instruction tells Cargo to pass the [`-L`
208210
flag][option-search] to the compiler to add a directory to the library search
@@ -220,14 +222,14 @@ is fine).
220222

221223
[option-search]: ../../rustc/command-line-arguments.md#option-l-search-path
222224

223-
### `cargo:rustc-flags=FLAGS` {#rustc-flags}
225+
### `cargo::rustc-flags=FLAGS` {#rustc-flags}
224226

225227
The `rustc-flags` instruction tells Cargo to pass the given space-separated
226228
flags to the compiler. This only allows the `-l` and `-L` flags, and is
227229
equivalent to using [`rustc-link-lib`](#rustc-link-lib) and
228230
[`rustc-link-search`](#rustc-link-search).
229231

230-
### `cargo:rustc-cfg=KEY[="VALUE"]` {#rustc-cfg}
232+
### `cargo::rustc-cfg=KEY[="VALUE"]` {#rustc-cfg}
231233

232234
The `rustc-cfg` instruction tells Cargo to pass the given value to the
233235
[`--cfg` flag][option-cfg] to the compiler. This may be used for compile-time
@@ -239,16 +241,16 @@ used to enable an optional dependency, or enable other Cargo features.
239241
Be aware that [Cargo features] use the form `feature="foo"`. `cfg` values
240242
passed with this flag are not restricted to that form, and may provide just a
241243
single identifier, or any arbitrary key/value pair. For example, emitting
242-
`cargo:rustc-cfg=abc` will then allow code to use `#[cfg(abc)]` (note the lack
244+
`cargo::rustc-cfg=abc` will then allow code to use `#[cfg(abc)]` (note the lack
243245
of `feature=`). Or an arbitrary key/value pair may be used with an `=` symbol
244-
like `cargo:rustc-cfg=my_component="foo"`. The key should be a Rust
246+
like `cargo::rustc-cfg=my_component="foo"`. The key should be a Rust
245247
identifier, the value should be a string.
246248

247249
[cargo features]: features.md
248250
[conditional compilation]: ../../reference/conditional-compilation.md
249251
[option-cfg]: ../../rustc/command-line-arguments.md#option-cfg
250252

251-
### `cargo:rustc-env=VAR=VALUE` {#rustc-env}
253+
### `cargo::rustc-env=VAR=VALUE` {#rustc-env}
252254

253255
The `rustc-env` instruction tells Cargo to set the given environment variable
254256
when compiling the package. The value can be then retrieved by the [`env!`
@@ -268,15 +270,14 @@ Cargo][env-cargo].
268270
[env-macro]: ../../std/macro.env.html
269271
[env-cargo]: environment-variables.md#environment-variables-cargo-sets-for-crates
270272

271-
### `cargo:rustc-cdylib-link-arg=FLAG` {#rustc-cdylib-link-arg}
273+
### `cargo::rustc-cdylib-link-arg=FLAG` {#rustc-cdylib-link-arg}
272274

273275
The `rustc-cdylib-link-arg` instruction tells Cargo to pass the [`-C
274276
link-arg=FLAG` option][link-arg] to the compiler, but only when building a
275277
`cdylib` library target. Its usage is highly platform specific. It is useful
276278
to set the shared library version or the runtime-path.
277279

278-
279-
### `cargo:warning=MESSAGE` {#cargo-warning}
280+
### `cargo::warning=MESSAGE` {#cargo-warning}
280281

281282
The `warning` instruction tells Cargo to display a warning after the build
282283
script has finished running. Warnings are only shown for `path` dependencies
@@ -321,7 +322,7 @@ FAQ](../faq.md#why-is-cargo-rebuilding-my-code).
321322

322323
[`exclude` and `include` fields]: manifest.md#the-exclude-and-include-fields
323324

324-
### `cargo:rerun-if-changed=PATH` {#rerun-if-changed}
325+
### `cargo::rerun-if-changed=PATH` {#rerun-if-changed}
325326

326327
The `rerun-if-changed` instruction tells Cargo to re-run the build script if
327328
the file at the given path has changed. Currently, Cargo only uses the
@@ -333,14 +334,14 @@ If the path points to a directory, it will scan the entire directory for
333334
any modifications.
334335

335336
If the build script inherently does not need to re-run under any circumstance,
336-
then emitting `cargo:rerun-if-changed=build.rs` is a simple way to prevent it
337+
then emitting `cargo::rerun-if-changed=build.rs` is a simple way to prevent it
337338
from being re-run (otherwise, the default if no `rerun-if` instructions are
338339
emitted is to scan the entire package directory for changes). Cargo
339340
automatically handles whether or not the script itself needs to be recompiled,
340341
and of course the script will be re-run after it has been recompiled.
341342
Otherwise, specifying `build.rs` is redundant and unnecessary.
342343

343-
### `cargo:rerun-if-env-changed=NAME` {#rerun-if-env-changed}
344+
### `cargo::rerun-if-env-changed=NAME` {#rerun-if-env-changed}
344345

345346
The `rerun-if-env-changed` instruction tells Cargo to re-run the build script
346347
if the value of an environment variable of the given name has changed.
@@ -351,7 +352,6 @@ variables like `TARGET` that [Cargo sets for build scripts][build-env]. The
351352
environment variables in use are those received by `cargo` invocations, not
352353
those received by the executable of the build script.
353354

354-
355355
## The `links` Manifest Key
356356

357357
The `package.links` key may be set in the `Cargo.toml` manifest to declare

0 commit comments

Comments
 (0)