Skip to content

Commit c8ebd3d

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

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

src/SUMMARY.md

+1
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 Tools](build_tools.md)

src/build_tools.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Build Tools
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` code.
6+
See `crates.io`'s [documentation on the matter][build-script-docs] for more information.
7+
8+
Notes:
9+
- Examples in this section will likely show content for several files.
10+
- Since [`play.rust-lang.org`][playground] doesn't support specifying/running build scripts, most of
11+
the following examples will fail when trying to run them directly in the browser with the
12+
"play" (<i class="fa fa-play"></i>) button. Running will require setting up a local project
13+
and making the modifications shown.
14+
15+
| Recipe | Crates | Categories |
16+
|--------|--------|------------|
17+
| [Compile and link statically to a bundled C library][ex-cc-static-bundled] | [![cc-badge]][cc] | [![cat-development-tools-badge]][cat-development-tools] [![cat-rust-patterns-badge]][cat-rust-patterns] [![cat-external-ffi-bindings-badge]][cat-external-ffi-bindings] |
18+
19+
20+
[ex-cc-static-bundled]: #ex-cc-static-bundled
21+
<a name="ex-cc-static-bundled"></a>
22+
## Compile and link statically to a bundled C library
23+
24+
[![cat-development-tools-badge]][cat-development-tools] [![cc-badge]][cc] [![cat-rust-patterns-badge]][cat-rust-patterns] [![cat-external-ffi-bindings-badge]][cat-external-ffi-bindings]
25+
26+
To accommodate scenarios where additional `C`, `C++`, or `assembly` is required in a project, the [`cc`][cc] crate
27+
offers a simple api for compiling bundled `c/c++/asm` code into static libraries (`.a`) that can be statically linked to by `rustc`.
28+
29+
### `Cargo.toml`
30+
31+
```toml
32+
[package]
33+
...
34+
build = "build.rs"
35+
36+
[build-dependencies]
37+
cc = "1"
38+
39+
[dependencies]
40+
error-chain = "0.11"
41+
```
42+
43+
### `build.rs`
44+
45+
```rust,no_run
46+
extern crate cc;
47+
48+
fn main() {
49+
// builds a static lib: `libhello.a`
50+
cc::Build::new()
51+
.file("src/hello.c")
52+
.compile("hello");
53+
}
54+
```
55+
56+
### `src/hello.c`
57+
58+
```c
59+
#include <stdio.h>
60+
61+
62+
void hello() {
63+
printf("Hello from C!\n");
64+
}
65+
66+
void greet(const char* name) {
67+
printf("Hello, %s!\n", name);
68+
}
69+
```
70+
71+
### `src/main.rs`
72+
73+
```rust,no_run
74+
# #[macro_use] extern crate error_chain;
75+
use std::ffi::CString;
76+
use std::os::raw::c_char;
77+
78+
#
79+
# error_chain! {
80+
# foreign_links {
81+
# NulError(::std::ffi::NulError);
82+
# Io(::std::io::Error);
83+
# }
84+
# }
85+
#
86+
#
87+
# fn prompt(s: &str) -> Result<String> {
88+
# use std::io::Write;
89+
# print!("{}", s);
90+
# std::io::stdout().flush()?;
91+
# let mut input = String::new();
92+
# std::io::stdin().read_line(&mut input)?;
93+
# Ok(input.trim().to_string())
94+
# }
95+
#
96+
97+
extern {
98+
fn hello();
99+
fn greet(name: *const c_char);
100+
}
101+
102+
103+
fn run() -> Result<()> {
104+
unsafe { hello() }
105+
let name = prompt("What's your name? ")?;
106+
let c_name = CString::new(name)?;
107+
unsafe { greet(c_name.as_ptr()) }
108+
Ok(())
109+
}
110+
111+
#
112+
# quick_main!(run);
113+
```
114+
115+
116+
{{#include links.md}}
117+
118+
<!-- Other Reference -->
119+
120+
[build-script-docs]: http://doc.crates.io/build-script.html
121+
[playground]: https://play.rust-lang.org
122+

src/intro.md

+8
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 Tools](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] [![cat-rust-patterns-badge]][cat-rust-patterns] [![cat-external-ffi-bindings-badge]][cat-external-ffi-bindings] |
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

+6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ 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
24+
[cat-external-ffi-bindings-badge]: https://badge-cache.kominick.com/badge/external_ffi_bindings--x.svg?style=social
25+
[cat-external-ffi-bindings]: https://crate.io/categories/external-ffi-bindings
2226
[cat-filesystem-badge]: https://badge-cache.kominick.com/badge/filesystem--x.svg?style=social
2327
[cat-filesystem]: https://crates.io/categories/filesystem
2428
[cat-net-badge]: https://badge-cache.kominick.com/badge/net--x.svg?style=social
@@ -44,6 +48,8 @@ Keep lines sorted.
4448
[bitflags]: https://docs.rs/bitflags/
4549
[byteorder-badge]: https://badge-cache.kominick.com/crates/v/byteorder.svg?label=byteorder
4650
[byteorder]: https://docs.rs/byteorder/
51+
[cc-badge]: https://badge-cache.kominick.com/crates/v/cc.svg?label=cc
52+
[cc]: https://docs.rs/cc
4753
[chrono-badge]: https://badge-cache.kominick.com/crates/v/chrono.svg?label=chrono
4854
[chrono]: https://docs.rs/chrono/
4955
[clap-badge]: https://badge-cache.kominick.com/crates/v/clap.svg?label=clap

0 commit comments

Comments
 (0)