-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
45 lines (39 loc) · 1.65 KB
/
build.rs
File metadata and controls
45 lines (39 loc) · 1.65 KB
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
// Copyright (C) 2025 SUSE LLC
// SPDX-License-Identifier: GPL-2.0-or-later
use std::env;
use std::fs;
use std::process::Command;
fn run(program: &str, args: &[&str]) -> String {
let output = Command::new(program)
.args(args)
.output()
.unwrap_or_else(|err| panic!("failed to execute {}: {}", program, err));
if !output.status.success() {
panic!("{} exited with error: {}", program, output.status);
}
String::from_utf8(output.stdout)
.unwrap_or_else(|err| panic!("output from {} is not valid UTF-8: {}", program, err))
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-env-changed=SUSE_KABI_TOOLS_VERSION");
// Check if the version is explicitly set, for instance, by a distribution package recipe.
if env::var("SUSE_KABI_TOOLS_VERSION").is_ok() {
return;
}
// Execute git-describe to retrieve version information.
let raw_version = run("git", &["describe", "--dirty"]);
let version = raw_version.trim().strip_prefix('v').unwrap_or(&raw_version);
println!("cargo:rustc-env=SUSE_KABI_TOOLS_VERSION={}", version);
// List items that the `git describe --dirty` command depends on.
let ls_files = run("git", &["ls-files"]);
for file in ls_files.lines() {
println!("cargo:rerun-if-changed={}", file);
}
println!("cargo:rerun-if-changed=.git/HEAD");
let raw_head = fs::read_to_string(".git/HEAD").expect("file .git/HEAD should be readable");
if let Some(head) = raw_head.trim().strip_prefix("ref: ") {
println!("cargo:rerun-if-changed=.git/{}", head);
}
println!("cargo:rerun-if-changed=.git/index");
}