forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
67 lines (57 loc) · 1.83 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
use itertools::Itertools;
use std::{env, io::prelude::*, path::PathBuf, process::Command};
fn main() {
#[cfg(feature = "freeze-stdlib")]
for entry in glob::glob("Lib/*/*.py").expect("Lib/ exists?").flatten() {
let display = entry.display();
println!("cargo:rerun-if-changed={}", display);
}
println!("cargo:rustc-env=RUSTPYTHON_GIT_HASH={}", git_hash());
println!(
"cargo:rustc-env=RUSTPYTHON_GIT_TIMESTAMP={}",
git_timestamp()
);
println!("cargo:rustc-env=RUSTPYTHON_GIT_TAG={}", git_tag());
println!("cargo:rustc-env=RUSTPYTHON_GIT_BRANCH={}", git_branch());
println!(
"cargo:rustc-env=RUSTC_VERSION={}",
rustc_version::version().unwrap()
);
println!(
"cargo:rustc-env=RUSTPYTHON_TARGET_TRIPLE={}",
env::var("TARGET").unwrap()
);
let mut env_path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
env_path.push("env_vars.rs");
let mut f = std::fs::File::create(env_path).unwrap();
write!(
f,
"sysvars! {{ {} }}",
std::env::vars_os().format_with(", ", |(k, v), f| f(&format_args!("{:?} => {:?}", k, v)))
)
.unwrap();
}
fn git_hash() -> String {
git(&["rev-parse", "--short", "HEAD"])
}
fn git_timestamp() -> String {
git(&["log", "-1", "--format=%ct"])
}
fn git_tag() -> String {
git(&["describe", "--all", "--always", "--dirty"])
}
fn git_branch() -> String {
git(&["name-rev", "--name-only", "HEAD"])
}
fn git(args: &[&str]) -> String {
command("git", args)
}
fn command(cmd: &str, args: &[&str]) -> String {
match Command::new(cmd).args(args).output() {
Ok(output) => match String::from_utf8(output.stdout) {
Ok(s) => s,
Err(err) => format!("(output error: {})", err),
},
Err(err) => format!("(command error: {})", err),
}
}