xref: /cloud-hypervisor/build.rs (revision 10ee003d66f7c6018964c335eef2648b26d104d9)
1ebd83699SBo Chen // Copyright © 2020 Intel Corporation
2ebd83699SBo Chen //
3ebd83699SBo Chen // SPDX-License-Identifier: Apache-2.0
4ebd83699SBo Chen //
5ebd83699SBo Chen 
69f9cfeb5SWei Liu use std::env;
7ebd83699SBo Chen use std::process::Command;
8ebd83699SBo Chen 
main()9ebd83699SBo Chen fn main() {
109e6301beSWei Liu     let mut version = "v".to_owned() + env!("CARGO_PKG_VERSION");
11559d1840SBo Chen 
12f32487f8SRob Bradford     if let Ok(git_out) = Command::new("git").args(["describe", "--dirty"]).output() {
13559d1840SBo Chen         if git_out.status.success() {
14559d1840SBo Chen             if let Ok(git_out_str) = String::from_utf8(git_out.stdout) {
15559d1840SBo Chen                 version = git_out_str;
169f9cfeb5SWei Liu                 // Pop the trailing newline.
179f9cfeb5SWei Liu                 version.pop();
18559d1840SBo Chen             }
19559d1840SBo Chen         }
20559d1840SBo Chen     }
21ebd83699SBo Chen 
229f9cfeb5SWei Liu     // Append CH_EXTRA_VERSION to version if it is set.
239f9cfeb5SWei Liu     if let Ok(extra_version) = env::var("CH_EXTRA_VERSION") {
249f9cfeb5SWei Liu         println!("cargo:rerun-if-env-changed=CH_EXTRA_VERSION");
25*10ee003dSBo Chen         version.push_str(&format!("-{extra_version}"));
269f9cfeb5SWei Liu     }
279f9cfeb5SWei Liu 
28ebd83699SBo Chen     // This println!() has a special behavior, as it will set the environment
2959012cccSOmer Faruk Bayram     // variable BUILD_VERSION, so that it can be reused from the binary.
30ebd83699SBo Chen     // Particularly, this is used from src/main.rs to display the exact
31ebd83699SBo Chen     // version.
3259012cccSOmer Faruk Bayram     println!("cargo:rustc-env=BUILD_VERSION={version}");
33ebd83699SBo Chen }
34