-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
51 lines (45 loc) · 1.55 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
use std::{env, error::Error};
fn main() -> Result<(), Box<dyn Error>> {
let cargo = vergen_gix::CargoBuilder::all_cargo()?;
let rustc = vergen_gix::RustcBuilder::all_rustc()?;
let git = vergen_gix::GixBuilder::all_git()?;
vergen_gix::Emitter::default()
.add_instructions(&cargo)?
.add_instructions(&rustc)?
.add_instructions(&git)?
.emit()?;
println!(
"cargo:rustc-env=LONG_VERSION_PROFILE={}",
env::var("PROFILE").unwrap()
);
println!("cargo:rustc-env=LONG_VERSION_ENV={}", env());
println!(
"cargo:rustc-env=LONG_VERSION_FEATURES={}",
enabled_features()
);
Ok(())
}
/// Build environment information.
fn env() -> String {
if env::var("GITHUB_ACTION").is_ok() {
// The following variables are expected to exist when building within a GitHub workflow.
// https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
return format!("GitHub CI {}", env::var("GITHUB_WORKFLOW_REF").unwrap());
}
"unknown environment".to_string()
}
fn enabled_features() -> String {
let prefix = "CARGO_FEATURE_";
let features = env::vars()
.filter(|(key, _)| key.starts_with(prefix))
.map(|(key, _)| {
key.strip_prefix(prefix)
.unwrap()
.to_lowercase()
.replace('_', "-")
});
features
.map(|name| format!("+{name}"))
.collect::<Vec<_>>()
.join(" ")
}