Skip to main content

libghostty_vt/
build_info.rs

1//! Query compile-time build configuration of libghostty-vt.
2//!
3//! These values reflect the options the library was built with and are constant for the lifetime of the process.
4//!
5//! # Example
6//! ```rust
7//! use libghostty_vt::{Error, build_info::*};
8//!
9//! fn print_build_info() -> Result<(), Error> {
10//!     println!(
11//!         "SIMD: {}",
12//!         if supports_simd().unwrap_or(false) { "enabled" } else { "disabled" }
13//!     );
14//!     println!(
15//!         "Kitty graphics: {}",
16//!         if supports_kitty_graphics().unwrap_or(false) { "enabled" } else { "disabled" }
17//!     );
18//!     println!(
19//!         "Tmux control mode: {}",
20//!         if supports_tmux_control_mode().unwrap_or(false) { "enabled" } else { "disabled" }
21//!     );
22//!     Ok(())
23//! }
24//! ```
25
26use std::mem::MaybeUninit;
27
28use crate::{
29    error::{Error, Result, from_result},
30    ffi::{self, BuildInfo as Info},
31};
32
33/// Whether SIMD-accelerated code paths are enabled.
34pub fn supports_simd() -> Result<bool> {
35    build_info(Info::SIMD)
36}
37
38/// Whether Kitty graphics protocol support is available.
39pub fn supports_kitty_graphics() -> Result<bool> {
40    build_info(Info::KITTY_GRAPHICS)
41}
42
43/// Whether tmux control mode support is available.
44pub fn supports_tmux_control_mode() -> Result<bool> {
45    build_info(Info::TMUX_CONTROL_MODE)
46}
47
48/// The optimization mode the library was built with.
49pub fn optimize_mode() -> Result<OptimizeMode> {
50    build_info::<ffi::OptimizeMode::Type>(Info::OPTIMIZE)
51        .and_then(|v| v.try_into().map_err(|_| Error::InvalidValue))
52}
53
54/// The full version string (e.g. "1.2.3" or "1.2.3-dev+abcdef").
55pub fn version_string() -> Result<&'static str> {
56    build_info::<ffi::String>(Info::VERSION_STRING)
57        // SAFETY: API guarantees
58        .map(|s| unsafe { s.to_str() })
59}
60/// The major version number.
61pub fn major_version() -> Result<usize> {
62    build_info(Info::VERSION_MAJOR)
63}
64/// The minor version number.
65pub fn minor_version() -> Result<usize> {
66    build_info(Info::VERSION_MINOR)
67}
68/// The patch version number.
69pub fn patch_version() -> Result<usize> {
70    build_info(Info::VERSION_PATCH)
71}
72/// The pre metadata string (e.g. "alpha", "beta", "dev").
73///
74/// Has zero length if no pre metadata is present.
75pub fn pre_version() -> Result<&'static str> {
76    build_info::<ffi::String>(Info::VERSION_PRE)
77        // SAFETY: API guarantees
78        .map(|s| unsafe { s.to_str() })
79}
80/// The build metadata string (e.g. commit hash).
81///
82/// Has zero length if no build metadata is present.
83pub fn build_version() -> Result<&'static str> {
84    build_info::<ffi::String>(Info::VERSION_BUILD)
85        // SAFETY: API guarantees
86        .map(|s| unsafe { s.to_str() })
87}
88
89fn build_info<T>(tag: ffi::BuildInfo::Type) -> Result<T> {
90    let mut value = MaybeUninit::zeroed();
91    let result = unsafe { ffi::ghostty_build_info(tag, std::ptr::from_mut(&mut value).cast()) };
92    from_result(result)?;
93    // SAFETY: Value should be initialized after successful call.
94    Ok(unsafe { value.assume_init() })
95}
96
97/// The optimization mode libghostty is compiled with.
98#[repr(u32)]
99#[derive(Copy, Clone, Debug, PartialEq, Eq, int_enum::IntEnum)]
100pub enum OptimizeMode {
101    /// Debug mode.
102    ///
103    /// Very slow with all safety checks enabled.
104    Debug = ffi::OptimizeMode::DEBUG,
105    /// Release mode optimized for safety.
106    ///
107    /// Faster than debug due to better code generation,
108    /// but still very slow due to active safety checks.
109    ReleaseSafe = ffi::OptimizeMode::RELEASE_SAFE,
110    /// Release mode optimized for size.
111    ReleaseSmall = ffi::OptimizeMode::RELEASE_SMALL,
112    /// Release mode optimized for speed.
113    ReleaseFast = ffi::OptimizeMode::RELEASE_FAST,
114}