Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
140 lines (124 loc) · 5.68 KB
/
Copy pathbuild.rs
File metadata and controls
140 lines (124 loc) · 5.68 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! Build script for voxtype
//!
//! Generates man pages from CLI definitions using clap_mangen.
use clap::CommandFactory;
use std::env;
use std::io::Error;
use std::path::PathBuf;
// Include the CLI module tree. The CLI lives under src/cli/, with mod.rs as
// its entry; we attach it to the build-script's own crate via #[path] so
// `Cli::command()` below resolves through the normal module system.
//
// The build script only needs `Cli` for man-page generation, but the module
// re-exports several subcommand enums and impl methods that go with it. They
// are intentionally unused here; suppress dead-code warnings so the build
// script does not pollute `cargo clippy --all-targets -- -D warnings`.
#[path = "src/cli/mod.rs"]
#[allow(dead_code, unused_imports)]
mod cli;
use cli::Cli;
// Shared with `cargo xtask man`, which packaging uses to render the same
// pages without compiling voxtype.
#[path = "xtask/src/man.rs"]
mod man;
fn main() -> Result<(), Error> {
// Before the man-page early return: the version and the CUDA major must
// be stamped for every build, not just release builds. parakeet.rs reads
// VOXTYPE_BUILD_CUDA_MAJOR with env!, so a dev or clippy build with a
// *-cuda feature fails to compile without it.
expose_build_version();
expose_cuda_build_major();
// Only generate man pages for release builds or when explicitly requested
let profile = env::var("PROFILE").unwrap_or_default();
let generate = env::var("VOXTYPE_GEN_MANPAGES").is_ok() || profile == "release";
if !generate {
return Ok(());
}
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap_or_else(|_| "target".to_string()));
let man_dir = out_dir.join("man");
man::write_man_pages(&Cli::command(), &man_dir)?;
// Tell cargo to rerun if CLI definitions change
println!("cargo:rerun-if-changed=src/cli");
println!("cargo:rerun-if-changed=xtask/src/man.rs");
// Print location of generated man pages
println!(
"cargo:warning=Man pages generated in: {}",
man_dir.display()
);
Ok(())
}
/// Make untagged builds distinguishable in `--version` output.
///
/// Cargo.toml is bumped to the release version before the tag exists, so
/// every CI or local build between the bump and the tag self-reports as the
/// release. On 2026-08-31 a stale binary honestly claiming "1.0.1" was
/// missing a day of fixes, and nothing it printed could tell it apart from
/// the real release build. Stamp the short commit hash into the version
/// unless HEAD sits exactly on the matching release tag: `--version` prints
/// `1.1.0` only for a build of tag v1.1.0 and `1.1.0+g<sha>` for everything
/// else.
///
/// Builds without git (release tarballs — the AUR source package builds from
/// one, so there is no .git dir) get no env var; `cli::VERSION` then falls
/// back to the bare crate version. Git trouble must never fail the build and
/// never leak a placeholder like "+gunknown" into the version string.
fn expose_build_version() {
// Re-run when HEAD moves (commit, checkout). On a branch HEAD only holds
// `ref: refs/heads/<name>`, which a commit leaves unchanged, so also watch
// the branch ref and packed-refs (where `git gc` moves it). Creating a
// tag without a new commit does not retrigger by itself; release binaries
// are always clean `--no-cache` Docker builds, so that staleness is fine.
let mut watched = vec!["HEAD".to_string(), "packed-refs".to_string()];
if let Some(branch_ref) = git(&["symbolic-ref", "-q", "HEAD"]) {
watched.push(branch_ref);
}
for name in &watched {
if let Some(path) = git(&["rev-parse", "--git-path", name]) {
if std::path::Path::new(&path).exists() {
println!("cargo:rerun-if-changed={path}");
}
}
}
let Some(sha) = git(&["rev-parse", "--short", "HEAD"]) else {
return;
};
let pkg = env::var("CARGO_PKG_VERSION").unwrap_or_default();
let at_release_tag = git(&["tag", "--points-at", "HEAD"])
.is_some_and(|tags| tags.lines().any(|t| t == format!("v{pkg}")));
let version = if at_release_tag {
pkg
} else {
format!("{pkg}+g{sha}")
};
println!("cargo:rustc-env=VOXTYPE_BUILD_VERSION={version}");
}
/// Run git, returning trimmed stdout only on success with non-empty output.
fn git(args: &[&str]) -> Option<String> {
let out = std::process::Command::new("git").args(args).output().ok()?;
if !out.status.success() {
return None;
}
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
(!s.is_empty()).then_some(s)
}
/// Mirror ort-sys's build-time CUDA version selection so the binary's runtime
/// probe can reject mismatched hosts before ort attempts (and crashes on)
/// EP registration. ort 2.0.0-rc.12 picks cu12 vs cu13 prebuilt at compile time
/// based on the same env var; we read it here and emit a compile-time constant
/// the parakeet code path uses to short-circuit graceful fallback.
fn expose_cuda_build_major() {
println!("cargo:rerun-if-env-changed=ORT_CUDA_VERSION");
// src/cpu.rs reads this via option_env! to pick the baseline SIGILL
// message; without the rerun hint an incremental build keeps the stale
// value when the env changes.
println!("cargo:rerun-if-env-changed=VOXTYPE_VARIANT");
let major = match env::var("ORT_CUDA_VERSION").as_deref() {
Ok("12") => "12",
Ok("13") => "13",
// ort-sys defaults to cu12 when unset (see resolve.rs in ort-sys 2.0.0-rc.12).
// Match that default so a debug build without ORT_CUDA_VERSION set agrees
// with the bundled prebuilt.
_ => "12",
};
println!("cargo:rustc-env=VOXTYPE_BUILD_CUDA_MAJOR={major}");
}