Skip to content

Commit b9ef026

Browse files
committed
Add build time tooling section
issue rust-lang-nursery#292 - Add new `build_tools.md` file/section - Add a simple `cc` crate example
1 parent 04e18c0 commit b9ef026

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
- [Networking](net.md)
1010
- [Application development](app.md)
1111
- [Logging](logging.md)
12+
- [Build Time Tooling](build_tools.md)

src/build_tools.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Build Time Tooling
2+
3+
This section covers "build-time" tooling, or code that is run prior to compiling a crate's source code.
4+
Conventionally, build-time code lives in a **build.rs** file and is commonly referred to as a "build script".
5+
Common use cases include rust code generation and compilation of bundled C/C++/asm code.
6+
See crates.io's [documentation on the matter][build-script-docs] for more information.
7+
8+
9+
| Recipe | Crates | Categories |
10+
|--------|--------|------------|
11+
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
12+
13+
14+
[ex-cc-static-bundled]: #ex-cc-static-bundled
15+
<a name="ex-cc-static-bundled"></a>
16+
## Compile and link statically to a bundled C library
17+
18+
[![cc-badge]][cc] [![cat-development-tools-badge]][cat-development-tools]
19+
20+
To accommodate scenarios where additional C, C++, or assembly is required in a project, the [**cc**][cc] crate
21+
offers a simple api for compiling bundled C/C++/asm code into static libraries (**.a**) that can be statically linked to by **rustc**.
22+
23+
The following example has some bundled C code (**src/hello.c**) that will be used from rust.
24+
Before compiling our rust source code, the "build" file (**build.rs**) specified in **Cargo.toml** will run.
25+
Using the [**cc**][cc] crate, a static library file will be produced (in this case, **libhello.a**, see
26+
[`compile` docs][cc-build-compile]) which can then be used from rust by declaring the external function signatures in an `extern` block.
27+
28+
Since the bundled C is very simple, only a single source file needs to be passed to [`cc::Build`][cc-build].
29+
For more complex build requirements, [`cc::Build`][cc-build] offers a full suite of builder methods for specifying
30+
[`include`][cc-build-include] paths and extra compiler [`flag`][cc-build-flag]s.
31+
32+
### `Cargo.toml`
33+
34+
```toml
35+
[package]
36+
...
37+
build = "build.rs"
38+
39+
[build-dependencies]
40+
cc = "1"
41+
42+
[dependencies]
43+
error-chain = "0.11"
44+
```
45+
46+
### `build.rs`
47+
48+
```rust,no_run
49+
extern crate cc;
50+
51+
fn main() {
52+
cc::Build::new()
53+
.file("src/hello.c")
54+
.compile("hello"); // outputs `libhello.a`
55+
}
56+
```
57+
58+
### `src/hello.c`
59+
60+
```c
61+
#include <stdio.h>
62+
63+
64+
void hello() {
65+
printf("Hello from C!\n");
66+
}
67+
68+
void greet(const char* name) {
69+
printf("Hello, %s!\n", name);
70+
}
71+
```
72+
73+
### `src/main.rs`
74+
75+
```rust,no_run
76+
# #[macro_use] extern crate error_chain;
77+
use std::ffi::CString;
78+
use std::os::raw::c_char;
79+
80+
#
81+
# error_chain! {
82+
# foreign_links {
83+
# NulError(::std::ffi::NulError);
84+
# Io(::std::io::Error);
85+
# }
86+
# }
87+
#
88+
#
89+
# fn prompt(s: &str) -> Result<String> {
90+
# use std::io::Write;
91+
# print!("{}", s);
92+
# std::io::stdout().flush()?;
93+
# let mut input = String::new();
94+
# std::io::stdin().read_line(&mut input)?;
95+
# Ok(input.trim().to_string())
96+
# }
97+
#
98+
99+
extern {
100+
fn hello();
101+
fn greet(name: *const c_char);
102+
}
103+
104+
105+
fn run() -> Result<()> {
106+
unsafe { hello() }
107+
let name = prompt("What's your name? ")?;
108+
let c_name = CString::new(name)?;
109+
unsafe { greet(c_name.as_ptr()) }
110+
Ok(())
111+
}
112+
113+
#
114+
# quick_main!(run);
115+
```
116+
117+
118+
{{#include links.md}}
119+
120+
<!-- Other Reference -->
121+
122+
[build-script-docs]: http://doc.crates.io/build-script.html
123+
[playground]: https://play.rust-lang.org
124+
[cc-build]: https://docs.rs/cc/*/cc/struct.Build.html
125+
[cc-build-include]: https://docs.rs/cc/*/cc/struct.Build.html#method.include
126+
[cc-build-flag]: https://docs.rs/cc/*/cc/struct.Build.html#method.flag
127+
[cc-build-compile]: https://docs.rs/cc/*/cc/struct.Build.html#method.compile
128+

src/intro.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ community. It needs and welcomes help. For details see
117117
| [Log to the Unix syslog][ex-log-syslog] | [![log-badge]][log] [![syslog-badge]][syslog] | [![cat-debugging-badge]][cat-debugging] |
118118
| [Log messages to a custom location][ex-log-custom] | [![log-badge]][log] | [![cat-debugging-badge]][cat-debugging] |
119119

120+
## [Build Time Tooling](build_tools.html)
121+
122+
| Recipe | Crates | Categories |
123+
|--------|--------|------------|
124+
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] |
125+
126+
120127
{{#include links.md}}
121128

122129
<!-- Examples -->
@@ -125,6 +132,7 @@ community. It needs and welcomes help. For details see
125132
[ex-base64]: encoding.html#ex-base64
126133
[ex-bitflags]: basics.html#ex-bitflags
127134
[ex-byteorder-le]: basics.html#ex-byteorder-le
135+
[ex-cc-static-bundled]: build_tools.html#ex-cc-static-bundled
128136
[ex-clap-basic]: app.html#ex-clap-basic
129137
[ex-crossbeam-spawn]: concurrency.html#ex-crossbeam-spawn
130138
[ex-csv-serde]: encoding.html#ex-csv-serde

src/links.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Keep lines sorted.
1717
[cat-cryptography]: https://crates.io/categories/cryptography
1818
[cat-debugging-badge]: https://badge-cache.kominick.com/badge/debugging--x.svg?style=social
1919
[cat-debugging]: https://crates.io/categories/debugging
20+
[cat-development-tools-badge]: https://badge-cache.kominick.com/badge/development_tools--x.svg?style=social
21+
[cat-development-tools]: https://crates.io/categories/development-tools
2022
[cat-encoding-badge]: https://badge-cache.kominick.com/badge/encoding--x.svg?style=social
2123
[cat-encoding]: https://crates.io/categories/encoding
2224
[cat-filesystem-badge]: https://badge-cache.kominick.com/badge/filesystem--x.svg?style=social
@@ -44,6 +46,8 @@ Keep lines sorted.
4446
[bitflags]: https://docs.rs/bitflags/
4547
[byteorder-badge]: https://badge-cache.kominick.com/crates/v/byteorder.svg?label=byteorder
4648
[byteorder]: https://docs.rs/byteorder/
49+
[cc-badge]: https://badge-cache.kominick.com/crates/v/cc.svg?label=cc
50+
[cc]: https://docs.rs/cc
4751
[chrono-badge]: https://badge-cache.kominick.com/crates/v/chrono.svg?label=chrono
4852
[chrono]: https://docs.rs/chrono/
4953
[clap-badge]: https://badge-cache.kominick.com/crates/v/clap.svg?label=clap

0 commit comments

Comments
 (0)