Skip to content

Commit bc342a4

Browse files
authored
Merge pull request #1107 from maccoda/master
Adding build scripts page.
2 parents 92f47db + 890b04d commit bc342a4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
- [Dependencies](cargo/deps.md)
8888
- [Conventions](cargo/conventions.md)
8989
- [Tests](cargo/test.md)
90+
- [Build Scripts](cargo/build_scripts.md)
9091

9192
- [Attributes](attribute.md)
9293
- [`dead_code`](attribute/unused.md)

src/cargo/build_scripts.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Build Scripts
2+
3+
Sometimes a normal build from cargo is not enough. Perhaps your crate needs some
4+
pre-requisites before cargo will successfully compile, things like code
5+
generation, or some native code that needs to be compiled. To solve this problem
6+
we have build scripts that Cargo can run.
7+
8+
To add a build script to your package it can either be specified in the
9+
`Cargo.toml` as follows:
10+
11+
```toml
12+
[package]
13+
...
14+
build = "build.rs"
15+
```
16+
17+
Otherwise Cargo will look for a `build.rs` file in the project directory by
18+
default.
19+
20+
## How to use a build script
21+
22+
The build script is simply another Rust file that will be compiled and invoked
23+
prior to compiling anything else in the package. Hence it can be used to fulfil
24+
pre-requisites of your crate.
25+
26+
Cargo provides the script with inputs via environment variables [specified
27+
here] that can be used.
28+
29+
The script provides output via stdout. All lines printed are written to
30+
`target/debug/build/<pkg>/output`. Further, lines prefixed with `cargo:` will be
31+
interpreted by Cargo directly and hence can be used to define parameters for the
32+
packages compilation.
33+
34+
For further specification and examples have a read of the [cargo specification].
35+
36+
[specified here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
37+
38+
[cargo specification]: https://doc.rust-lang.org/cargo/reference/build-scripts.html

0 commit comments

Comments
 (0)