xref: /cloud-hypervisor/build.rs (revision b686a5bb24f949e3b201308d69b01e85c14f1ad6)
1 // Copyright © 2020 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 
6 use std::env;
7 use std::process::Command;
8 
9 fn main() {
10     let mut version = "v".to_owned() + env!("CARGO_PKG_VERSION");
11 
12     if let Ok(git_out) = Command::new("git").args(["describe", "--dirty"]).output() {
13         if git_out.status.success() {
14             if let Ok(git_out_str) = String::from_utf8(git_out.stdout) {
15                 version = git_out_str;
16                 // Pop the trailing newline.
17                 version.pop();
18             }
19         }
20     }
21 
22     // Append CH_EXTRA_VERSION to version if it is set.
23     if let Ok(extra_version) = env::var("CH_EXTRA_VERSION") {
24         println!("cargo:rerun-if-env-changed=CH_EXTRA_VERSION");
25         version.push_str(&format!("-{}", extra_version));
26     }
27 
28     // This println!() has a special behavior, as it will set the environment
29     // variable BUILD_VERSION, so that it can be reused from the binary.
30     // Particularly, this is used from src/main.rs to display the exact
31     // version.
32     println!("cargo:rustc-env=BUILD_VERSION={version}");
33 }
34