-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
76 lines (63 loc) · 2.16 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::fs::File;
use std::io::Write;
use std::process::Command;
use shadow_rs::{Format, SdResult};
fn main() -> SdResult<()> {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
let output = Command::new("git")
.args(["describe", "--tags", &git_hash])
.output()
.unwrap();
let build_version = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rustc-env=BUILD_VERSION={}", build_version);
#[cfg(target_os = "linux")]
{
// TODO: How can we pull in the library statically?
// println!(r"cargo:rustc-link-arg-bins=-static");
println!(r"cargo:rustc-link-lib=ch347");
println!(r"cargo:rustc-link-search=lib/x86_64");
}
#[cfg(target_os = "windows")]
{
println!(r"cargo:rustc-link-lib=static=CH347DLLA64");
println!(r"cargo:rustc-link-search=static_lib");
}
shadow_rs::new_hook(hook)
}
fn hook(file: &File) -> SdResult<()> {
append_write_const(file)?;
Ok(())
}
fn append_write_const(mut file: &File) -> SdResult<()> {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
let output = Command::new("git")
.args(["describe", "--tags", &git_hash])
.output()
.unwrap();
let build_version = String::from_utf8(output.stdout).unwrap();
// let hook_const: &str = &format!(r#"pub const GIT_HASH: &str = "{}";"#, git_hash);
// writeln!(file, "{}", hook_const)?;
// let hook_const: &str = &format!(r#"pub const BUILD_VERSION: &str = "{}";"#, build_version);
// writeln!(file, "{}", hook_const)?;
let about_message: &str = &format!(
"build_date: {}\r\ngit_hash: {}\r\nbuild_version: {}",
shadow_rs::DateTime::now().human_format(),
git_hash.trim(),
build_version.trim()
);
writeln!(
file,
r#"pub const {}: &str = "{}";"#,
"ABOUT_MESSABE", about_message
)?;
Ok(())
}