From de0f71c6a100ab63aaf6b16b8fdb91f176b4a7df Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 Sep 2023 12:32:41 -0700 Subject: [PATCH] c api: add ghostty_info to get metadata about the build --- include/ghostty.h | 14 ++++++++++++++ src/main_c.zig | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/ghostty.h b/include/ghostty.h index 50ef2bcb0..7b4afa1d5 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -284,9 +284,22 @@ typedef struct { bool physical; } ghostty_input_trigger_s; +typedef enum { + GHOSTTY_BUILD_MODE_DEBUG, + GHOSTTY_BUILD_MODE_RELEASE_SAFE, + GHOSTTY_BUILD_MODE_RELEASE_FAST, + GHOSTTY_BUILD_MODE_RELEASE_SMALL, +} ghostty_build_mode_e; + // Fully defined types. This MUST be kept in sync with equivalent Zig // structs. To find the Zig struct, grep for this type name. The documentation // for all of these types is available in the Zig source. +typedef struct { + ghostty_build_mode_e build_mode; + const char *version; + uintptr_t version_len; +} ghostty_info_s; + typedef struct { const char *message; } ghostty_error_s; @@ -338,6 +351,7 @@ typedef struct { // Published API int ghostty_init(void); +ghostty_info_s ghostty_info(void); ghostty_config_t ghostty_config_new(); void ghostty_config_free(ghostty_config_t); diff --git a/src/main_c.zig b/src/main_c.zig index 05cf43897..08f328dab 100644 --- a/src/main_c.zig +++ b/src/main_c.zig @@ -9,6 +9,7 @@ const std = @import("std"); const assert = std.debug.assert; const builtin = @import("builtin"); +const build_config = @import("build_config.zig"); const main = @import("main.zig"); const apprt = @import("apprt.zig"); @@ -23,6 +24,20 @@ pub const std_options = main.std_options; pub usingnamespace @import("config.zig").CAPI; pub usingnamespace apprt.runtime.CAPI; +/// ghostty_info_s +const Info = extern struct { + mode: BuildMode, + version: [*]const u8, + version_len: usize, + + const BuildMode = enum(c_int) { + debug, + release_safe, + release_fast, + release_small, + }; +}; + /// Initialize ghostty global state. It is possible to have more than /// one global state but it has zero practical benefit. export fn ghostty_init() c_int { @@ -30,3 +45,16 @@ export fn ghostty_init() c_int { main.state.init(); return 0; } + +export fn ghostty_info() Info { + return .{ + .mode = switch (builtin.mode) { + .Debug => .debug, + .ReleaseSafe => .release_safe, + .ReleaseFast => .release_fast, + .ReleaseSmall => .release_small, + }, + .version = build_config.version_string.ptr, + .version_len = build_config.version_string.len, + }; +}