Skip to content

Commit 918a415

Browse files
committed
Auto merge of #1314 - AnthIste:build-script-updates, r=alexcrichton
The examples given for `build.rs` do not compile against the latest rust. Instead of adapting to use modules such as `std::old_io`, I attempted to port the examples to use the new `io` and `path` modules.
2 parents e8f68cc + 484dd6f commit 918a415

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/doc/build-script.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,17 @@ some code. Let's see what's inside the build script:
209209
```rust,no_run
210210
// build.rs
211211
212-
use std::os;
213-
use std::io::File;
212+
use std::env;
213+
use std::fs::File;
214+
use std::io::Write;
215+
use std::path::Path;
214216
215217
fn main() {
216-
let dst = Path::new(os::getenv("OUT_DIR").unwrap());
217-
let mut f = File::create(&dst.join("hello.rs")).unwrap();
218-
f.write_str("
218+
let out_dir = env::var("OUT_DIR").unwrap();
219+
let dest_path = Path::new(&out_dir).join("hello.rs");
220+
let mut f = File::create(&dest_path).unwrap();
221+
222+
f.write_all(b"
219223
pub fn message() -> &'static str {
220224
\"Hello, World!\"
221225
}
@@ -294,19 +298,19 @@ the build script now:
294298
```rust,no_run
295299
// build.rs
296300
297-
use std::io::Command;
298-
use std::os;
301+
use std::process::Command;
302+
use std::env;
299303
300304
fn main() {
301-
let out_dir = os::getenv("OUT_DIR").unwrap();
305+
let out_dir = env::var("OUT_DIR").unwrap();
302306
303307
// note that there are a number of downsides to this approach, the comments
304308
// below detail how to improve the portability of these commands.
305309
Command::new("gcc").args(&["src/hello.c", "-c", "-o"])
306-
.arg(format!("{}/hello.o", out_dir))
310+
.arg(&format!("{}/hello.o", out_dir))
307311
.status().unwrap();
308312
Command::new("ar").args(&["crus", "libhello.a", "hello.o"])
309-
.cwd(&Path::new(&out_dir))
313+
.current_dir(&Path::new(&out_dir))
310314
.status().unwrap();
311315
312316
println!("cargo:rustc-flags=-L native={} -l static=hello", out_dir);

0 commit comments

Comments
 (0)