forked from microsoft/onefuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
72 lines (60 loc) · 2.09 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::process::Command;
use std::{env, process::Stdio};
fn run_cmd(args: &[&str]) -> Result<String, Box<dyn Error>> {
let cmd = Command::new(args[0])
.args(&args[1..])
.stdin(Stdio::null())
.output()?;
if cmd.status.success() {
Ok(String::from_utf8_lossy(&cmd.stdout).trim().to_string())
} else {
Err(From::from("failed"))
}
}
fn read_file(filename: &str) -> Result<String, Box<dyn Error>> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
contents = contents.trim().to_string();
Ok(contents)
}
fn print_values(version: &str, sha: &str) {
println!("cargo:rustc-env=ONEFUZZ_VERSION={version}");
println!("cargo:rustc-env=GIT_VERSION={sha}");
}
fn print_version(include_sha: bool, include_local: bool, sha: &str) -> Result<(), Box<dyn Error>> {
let mut version = read_file("../../../CURRENT_VERSION")?;
if include_sha {
version.push('+');
version.push_str(sha);
// if we're a non-release build, check to see if git has
// unstaged changes
if include_local && run_cmd(&["git", "diff", "--quiet"]).is_err() {
version.push('.');
version.push_str("localchanges");
}
}
print_values(&version, sha);
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let sha = run_cmd(&["git", "rev-parse", "HEAD"])?;
let hardcode_version = env::var("ONEFUZZ_SET_VERSION");
if let Ok(hardcode_version) = &hardcode_version {
print_values(hardcode_version, &sha);
return Ok(());
}
// If we're built off of a tag, we accept CURRENT_VERSION as is. Otherwise
// modify it to indicate local build
let (include_sha, include_local_changes) = if let Ok(val) = env::var("GITHUB_REF") {
(!val.starts_with("refs/tags/"), false)
} else {
(true, true)
};
print_version(include_sha, include_local_changes, &sha)
}