-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
59 lines (54 loc) · 1.64 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
// Copied from https://blog.biofan.org/2019/08/cargo-build-script/
fn get_git_version() -> String {
let version = env::var("CARGO_PKG_VERSION").unwrap();
let child = Command::new("git").args(["describe", "--always"]).output();
match child {
Ok(child) => {
let buf = String::from_utf8(child.stdout).expect("failed to read stdout");
version + "-" + &buf
}
Err(err) => {
eprintln!("`git describe` err: {}", err);
version
}
}
}
fn build_yak() {
let c_dir = PathBuf::from("./yak");
let mut has_yak = false;
if let Ok(iter_files) = c_dir.read_dir(){
for file in iter_files.flatten(){
let fpath = file.path();
if fpath
.extension()
.map_or_else(|| true, |x| x != "o" && x != "a")
{
println!("cargo:rerun-if-changed={:?}", fpath);
}
if !has_yak{
has_yak = true;
}
}
}
if has_yak {
let output = Command::new("make")
.arg("all")
.current_dir(&c_dir)
.output()
.expect("failed to execute process");
if !output.status.success() {
panic!("make error: {}", String::from_utf8_lossy(&output.stderr));
}
}
}
fn main() {
build_yak();
let version = get_git_version();
let mut f = File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("VERSION")).unwrap();
f.write_all(version.trim().as_bytes()).unwrap();
}