const std = @import("std"); const builtin = @import("builtin"); // build.zig.zon's minimum_zig_version names the one Zig version this package builds with. CI's setup-zig reads it too. const ZIG_VERSION = std.SemanticVersion.parse(@import("build.zig.zon").minimum_zig_version) catch unreachable; const SupportedTarget = struct { zig_target: []const u8, output_name: []const u8, description: []const u8, }; // Unpinned linux-gnu targets can bind host libm symbols such as GLIBC_2.29. const SUPPORTED_TARGETS = [_]SupportedTarget{ .{ .zig_target = "x86_64-linux-gnu.2.17", .output_name = "x86_64-linux", .description = "Linux x86_64" }, .{ .zig_target = "aarch64-linux-gnu.2.17", .output_name = "aarch64-linux", .description = "Linux aarch64" }, .{ .zig_target = "x86_64-linux-musl", .output_name = "x86_64-linux-musl", .description = "Linux x86_64 (musl)" }, .{ .zig_target = "aarch64-linux-musl", .output_name = "aarch64-linux-musl", .description = "Linux aarch64 (musl)" }, .{ .zig_target = "x86_64-macos.13.0", .output_name = "x86_64-macos", .description = "macOS x86_64 (Intel)" }, .{ .zig_target = "aarch64-macos.13.0", .output_name = "aarch64-macos", .description = "macOS aarch64 (Apple Silicon)" }, .{ .zig_target = "x86_64-windows-gnu", .output_name = "x86_64-windows", .description = "Windows x86_64" }, .{ .zig_target = "aarch64-windows-gnu", .output_name = "aarch64-windows", .description = "Windows aarch64" }, }; const DEFAULT_MACOS_SDK_PATH = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"; const LIB_NAME = "opentui"; const ROOT_SOURCE_FILE = "src/lib.zig"; const GHOSTTY_VT_VERSION = "0.1.0-dev+b988efcf"; const YOGA_CXX_FLAGS = [_][]const u8{ "-std=c++20", "-fexceptions", "-frtti", // Bind Yoga's C++ calls inside the library instead of through the PLT. "-fvisibility-inlines-hidden", "-fno-semantic-interposition", }; const YOGA_CXX_SOURCES = [_][]const u8{ "yoga/YGConfig.cpp", "yoga/YGEnums.cpp", "yoga/YGNode.cpp", "yoga/YGNodeLayout.cpp", "yoga/YGNodeStyle.cpp", "yoga/YGPixelGrid.cpp", "yoga/YGValue.cpp", "yoga/algorithm/AbsoluteLayout.cpp", "yoga/algorithm/Baseline.cpp", "yoga/algorithm/Cache.cpp", "yoga/algorithm/CalculateLayout.cpp", "yoga/algorithm/FlexLine.cpp", "yoga/algorithm/PixelGrid.cpp", "yoga/config/Config.cpp", "yoga/debug/AssertFatal.cpp", "yoga/debug/Log.cpp", "yoga/event/event.cpp", "yoga/node/LayoutResults.cpp", "yoga/node/Node.cpp", }; const LCMS2_SOURCES = [_][]const u8{ "src/cmsalpha.c", "src/cmscam02.c", "src/cmscgats.c", "src/cmscnvrt.c", "src/cmserr.c", "src/cmsgamma.c", "src/cmsgmt.c", "src/cmshalf.c", "src/cmsintrp.c", "src/cmsio0.c", "src/cmsio1.c", "src/cmslut.c", "src/cmsmd5.c", "src/cmsmtrx.c", "src/cmsnamed.c", "src/cmsopt.c", "src/cmspack.c", "src/cmspcs.c", "src/cmsplugin.c", "src/cmsps2.c", "src/cmssamp.c", "src/cmssm.c", "src/cmstypes.c", "src/cmsvirt.c", "src/cmswtpnt.c", "src/cmsxform.c", }; // Zig 0.17 compiles a host query without a CPU model for the baseline CPU, // so native artifacts name the native CPU. fn nativeTarget(b: *std.Build) std.Build.ResolvedTarget { return b.resolveTargetQuery(.{ .cpu_model = .native }); } fn nativeExecutableTarget(b: *std.Build) std.Build.ResolvedTarget { if (builtin.target.os.tag != .linux) { return nativeTarget(b); } // Zig 0.16's ELF linker still fails on newer glibc startup objects that // ship .sframe relocations. Keep shipped libraries on linux-gnu, but use // musl for local native executables so test/debug/bench still work. var query = b.graph.host.query; query.cpu_model = .native; query.abi = .musl; query.glibc_version = null; return b.resolveTargetQuery(query); } fn pathExists(b: *std.Build, path: []const u8) bool { if (path.len == 0) return false; std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch return false; return true; } fn isMacOSSDKPath(path: []const u8) bool { const trimmed_path = std.mem.trimEnd(u8, path, "/"); if (trimmed_path.len == 0) return false; const base_name = std.fs.path.basename(trimmed_path); return std.mem.startsWith(u8, base_name, "MacOSX") and std.mem.endsWith(u8, base_name, ".sdk"); } fn macOSSDKHasFramework(b: *std.Build, sdk_path: []const u8, framework: []const u8) bool { return pathExists(b, b.pathJoin(&.{ sdk_path, "System", "Library", "Frameworks", b.fmt("{s}.framework", .{framework}) })); } fn isMacOSSDKAvailable(b: *std.Build, sdk_path: []const u8) bool { return isMacOSSDKPath(sdk_path) and pathExists(b, b.pathJoin(&.{ sdk_path, "usr", "lib" })) and macOSSDKHasFramework(b, sdk_path, "CoreFoundation") and macOSSDKHasFramework(b, sdk_path, "AppKit") and macOSSDKHasFramework(b, sdk_path, "Foundation") and macOSSDKHasFramework(b, sdk_path, "ImageIO") and macOSSDKHasFramework(b, sdk_path, "CoreAudio") and macOSSDKHasFramework(b, sdk_path, "AudioToolbox"); } fn resolveMacOSSDKPath(b: *std.Build) ?[]const u8 { // Zig 0.17 reuses a configuration unless build.zig says it can't. This search reads // the environment and the file system, so each build configures again. b.graph.poisonCache(); if (b.option([]const u8, "macos-sdk", "Path to a macOS SDK for CoreAudio headers and framework linking")) |sdk_path| { if (isMacOSSDKAvailable(b, sdk_path)) return sdk_path; std.debug.print("macOS SDK path '{s}' must be a MacOSX*.sdk with the required frameworks\n", .{sdk_path}); return null; } const env_vars = [_][]const u8{ "SDKROOT", "MACOS_SDK_PATH", "MACOSX_SDK_PATH" }; for (env_vars) |env_var| { if (b.graph.environ_map.get(env_var)) |sdk_path| { if (isMacOSSDKAvailable(b, sdk_path)) return sdk_path; } } if (builtin.target.os.tag == .macos and std.zig.system.darwin.isSdkInstalled(b.allocator, b.graph.io)) { const sdk_target = b.resolveTargetQuery(.{ .cpu_arch = .aarch64, .os_tag = .macos }); if (std.zig.system.darwin.getSdk(b.allocator, b.graph.io, &sdk_target.result)) |sdk_path| { if (isMacOSSDKAvailable(b, sdk_path)) return sdk_path; } } if (isMacOSSDKAvailable(b, DEFAULT_MACOS_SDK_PATH)) return DEFAULT_MACOS_SDK_PATH; return null; } fn printMissingMacOSSDK(target_description: []const u8) void { std.debug.print( "macOS SDK not found for {s}. Set SDKROOT, MACOS_SDK_PATH, or -Dmacos-sdk=/path/to/MacOSX.sdk.\n", .{target_description}, ); } fn addMiniaudioShim( b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget, macos_sdk_path: ?[]const u8, ) void { const c_flags: []const []const u8 = switch (target.result.os.tag) { .macos => blk: { const flags = b.allocator.alloc([]const u8, 4) catch @panic("OOM"); flags[0] = "-std=c99"; flags[1] = "-DMA_NO_RUNTIME_LINKING"; flags[2] = "-isysroot"; flags[3] = macos_sdk_path.?; break :blk flags; }, else => &.{"-std=c99"}, }; module.addIncludePath(b.path("src")); module.link_libc = true; module.addCSourceFile(.{ .file = b.path("src/miniaudio_shim.c"), .flags = c_flags, }); } fn appendCFlags(b: *std.Build, base: []const []const u8, extra: []const []const u8) []const []const u8 { const flags = b.allocator.alloc([]const u8, base.len + extra.len) catch @panic("OOM"); @memcpy(flags[0..base.len], base); @memcpy(flags[base.len..], extra); return flags; } fn addImageShim(b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget, macos_sdk_path: ?[]const u8) void { const flags: []const []const u8 = switch (target.result.os.tag) { .macos => &.{ "-std=c99", "-ffp-contract=off", "-fvisibility=hidden", "-isysroot", macos_sdk_path.? }, else => &.{ "-std=c99", "-ffp-contract=off", "-fvisibility=hidden" }, }; module.addCSourceFile(.{ .file = b.path("src/image-shim.c"), .flags = flags, }); module.addIncludePath(b.path("src/vendor/lcms2/include")); module.addIncludePath(b.path("src/vendor/lcms2/src")); module.addCSourceFiles(.{ .root = b.path("src/vendor/lcms2"), .files = &LCMS2_SOURCES, // image.zig serializes every LittleCMS operation with icc_cache_mutex. .flags = flags, }); // One upstream SIMD sRGB idiom forms `fp32_to_srgb8_tab4 - 912`; its // clamped indexes 912...1015 resolve to actual table elements 0...103. // The reads are in range, but the pre-array pointer trips C bounds // instrumentation. Disable only `bounds` for this translation unit; // pointer-overflow, alignment, and other sanitizers remain enabled. This // is separate from our coefficient-copy alignment patch. See vendor/stb/README.md. const resize_flags: []const []const u8 = switch (target.result.os.tag) { .macos => &.{ "-std=c99", "-ffp-contract=off", "-fvisibility=hidden", "-fno-sanitize=bounds", "-isysroot", macos_sdk_path.? }, else => &.{ "-std=c99", "-ffp-contract=off", "-fvisibility=hidden", "-fno-sanitize=bounds" }, }; module.addCSourceFile(.{ .file = b.path("src/image-resize-shim.c"), .flags = resize_flags, }); const webp_flags: []const []const u8 = switch (target.result.os.tag) { .macos => &.{ "-std=c99", "-fvisibility=hidden", "-DWEBP_EXTERN=extern", "-isysroot", macos_sdk_path.? }, else => &.{ "-std=c99", "-fvisibility=hidden", "-DWEBP_EXTERN=extern" }, }; const webp_dispatch_flags = if (target.result.cpu.arch == .x86_64) appendCFlags(b, webp_flags, &.{ "-DWEBP_HAVE_SSE2", "-DWEBP_HAVE_SSE41", "-DWEBP_HAVE_AVX2" }) else webp_flags; module.addIncludePath(b.path("src/vendor/libwebp")); module.addCSourceFile(.{ .file = b.path("src/image-webp-config.c"), .flags = webp_dispatch_flags, }); module.addCSourceFiles(.{ .root = b.path("src/vendor/libwebp"), .files = &.{ "src/dec/alpha_dec.c", "src/dec/buffer_dec.c", "src/dec/frame_dec.c", "src/dec/idec_dec.c", "src/dec/io_dec.c", "src/dec/quant_dec.c", "src/dec/tree_dec.c", "src/dec/vp8_dec.c", "src/dec/vp8l_dec.c", "src/dec/webp_dec.c", "src/dsp/alpha_processing.c", "src/dsp/cpu.c", "src/dsp/dec.c", "src/dsp/dec_clip_tables.c", "src/dsp/filters.c", "src/dsp/lossless.c", "src/dsp/rescaler.c", "src/dsp/upsampling.c", "src/dsp/yuv.c", "src/utils/bit_reader_utils.c", "src/utils/color_cache_utils.c", "src/utils/filters_utils.c", "src/utils/huffman_utils.c", "src/utils/palette.c", "src/utils/quant_levels_dec_utils.c", "src/utils/random_utils.c", "src/utils/rescaler_utils.c", "src/utils/thread_utils.c", "src/utils/utils.c", }, .flags = webp_dispatch_flags, }); switch (target.result.cpu.arch) { .x86_64 => { module.addCSourceFiles(.{ .root = b.path("src/vendor/libwebp"), .files = &.{ "src/dsp/alpha_processing_sse2.c", "src/dsp/dec_sse2.c", "src/dsp/filters_sse2.c", "src/dsp/lossless_sse2.c", "src/dsp/rescaler_sse2.c", "src/dsp/upsampling_sse2.c", "src/dsp/yuv_sse2.c", }, .flags = webp_flags, }); module.addCSourceFile(.{ .file = b.path("src/image-webp-sse41.c"), .flags = webp_flags, }); module.addCSourceFile(.{ .file = b.path("src/image-webp-avx2.c"), .flags = webp_flags, }); }, .aarch64 => module.addCSourceFiles(.{ .root = b.path("src/vendor/libwebp"), .files = &.{ "src/dsp/alpha_processing_neon.c", "src/dsp/dec_neon.c", "src/dsp/filters_neon.c", "src/dsp/lossless_neon.c", "src/dsp/rescaler_neon.c", "src/dsp/upsampling_neon.c", "src/dsp/yuv_neon.c", }, .flags = webp_flags, }), else => {}, } } fn addMacOSSDKSearchPaths(b: *std.Build, module: *std.Build.Module, sdk_path: []const u8) void { const include_path = b.pathJoin(&.{ sdk_path, "usr", "include" }); const framework_path = b.pathJoin(&.{ sdk_path, "System", "Library", "Frameworks" }); const lib_path = b.pathJoin(&.{ sdk_path, "usr", "lib" }); module.addSystemIncludePath(.{ .cwd_relative = include_path }); module.addSystemFrameworkPath(.{ .cwd_relative = framework_path }); module.addFrameworkPath(.{ .cwd_relative = framework_path }); module.addLibraryPath(.{ .cwd_relative = lib_path }); } fn addMacOSClipboardDependencies(b: *std.Build, module: *std.Build.Module, sdk_path: []const u8) void { module.addCSourceFile(.{ .file = b.path("src/clipboard/macos-shim.m"), .flags = &.{ "-fobjc-arc", "-fobjc-arc-exceptions", "-isysroot", sdk_path }, }); module.linkFramework("AppKit", .{}); module.linkFramework("Foundation", .{}); module.linkFramework("ImageIO", .{}); module.linkSystemLibrary("pthread", .{}); addMacOSSDKSearchPaths(b, module, sdk_path); } fn addMacOSSystemLibraries(b: *std.Build, module: *std.Build.Module, sdk_path: []const u8) void { addMacOSClipboardDependencies(b, module, sdk_path); module.linkFramework("CoreFoundation", .{}); module.linkFramework("CoreAudio", .{}); module.linkFramework("AudioToolbox", .{}); } // Set by -Drequire-video. Release builds pass it so a missing FFmpeg prefix fails instead of shipping without video. var require_video = false; var video_enabled = true; var video_unavailable_warned: std.StringHashMapUnmanaged(void) = .empty; // Names the FFmpeg prefix that scripts/build-ffmpeg.ts builds for this target, if any. fn ffmpegTargetName(b: *std.Build, target: std.Build.ResolvedTarget) ?[]const u8 { const arch = switch (target.result.cpu.arch) { .x86_64 => "x86_64", .aarch64 => "aarch64", else => return null, }; const platform = switch (target.result.os.tag) { .macos => "macos", .windows => if (target.result.abi == .gnu) "windows" else return null, .linux => if (target.result.abi.isMusl()) "linux-musl" else if (target.result.abi == .gnu) "linux" else return null, else => return null, }; return b.fmt("{s}-{s}", .{ arch, platform }); } fn addVideoDependencies(b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget, macos_sdk_path: ?[]const u8) void { const base_flags: []const []const u8 = switch (target.result.os.tag) { .macos => &.{ "-std=c11", "-fvisibility=hidden", "-isysroot", macos_sdk_path.? }, else => &.{ "-std=c11", "-fvisibility=hidden" }, }; const unavailable_flags = appendCFlags(b, base_flags, &.{"-DOTV_NO_FFMPEG"}); const target_name = (if (video_enabled) ffmpegTargetName(b, target) else null) orelse { module.addCSourceFile(.{ .file = b.path("src/video-shim.c"), .flags = unavailable_flags }); return; }; const prefix = b.root.joinString(b.allocator, b.fmt(".cache/ffmpeg/prefix/{s}", .{target_name})) catch @panic("OOM"); // scripts/build-ffmpeg.ts writes this marker after a complete install. Its // revision names the Zig version whose zig cc compiled the objects. const marker = std.Io.Dir.cwd().readFileAlloc(b.graph.io, b.pathJoin(&.{ prefix, "opentui-build" }), b.allocator, .limited(64)) catch ""; if (!std.mem.endsWith(u8, marker, "-zig-" ++ builtin.zig_version_string)) { if (require_video) { std.debug.print( "error: FFmpeg for {s} at {s} is missing or built by another Zig version.\nRun `bun scripts/build-ffmpeg.ts --target={s}` in packages/native (see src/vendor/ffmpeg/README.md).\n", .{ target_name, prefix, target_name }, ); std.process.exit(1); } if (!video_unavailable_warned.contains(target_name)) { video_unavailable_warned.put(b.allocator, target_name, {}) catch @panic("OOM"); std.debug.print( "warning: FFmpeg for {s} is missing or built by another Zig version; building without video. Run `bun scripts/build-ffmpeg.ts --target={s}` in packages/native.\n", .{ target_name, target_name }, ); } module.addCSourceFile(.{ .file = b.path("src/video-shim.c"), .flags = unavailable_flags }); return; } module.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ prefix, "include" }) }); module.addCSourceFile(.{ .file = b.path("src/video-shim.c"), .flags = base_flags }); // Objects, not archives: a static libopentui would store archives as nested members other linkers cannot read. const objects = std.Io.Dir.cwd().readFileAlloc(b.graph.io, b.pathJoin(&.{ prefix, "objects.txt" }), b.allocator, .limited(1 << 20)) catch |err| { std.debug.print("error: cannot read the FFmpeg object list in {s}: {s}\n", .{ prefix, @errorName(err) }); std.process.exit(1); }; var lines = std.mem.tokenizeAny(u8, objects, "\r\n"); while (lines.next()) |object| module.addObjectFile(.{ .cwd_relative = b.pathJoin(&.{ prefix, object }) }); switch (target.result.os.tag) { // A system import library: pkg-config has nothing for it, and on CI's Windows runners Strawberry Perl's // pkg-config.BAT fails and fails the compile step. .windows => module.linkSystemLibrary("bcrypt", .{ .use_pkg_config = .no }), .macos => { module.linkFramework("VideoToolbox", .{}); module.linkFramework("CoreMedia", .{}); module.linkFramework("CoreVideo", .{}); module.linkFramework("CoreServices", .{}); module.linkFramework("QuartzCore", .{}); }, else => {}, } } fn addNativeAudioDependencies( b: *std.Build, module: *std.Build.Module, target: std.Build.ResolvedTarget, macos_sdk_path: ?[]const u8, ) void { addMiniaudioShim(b, module, target, macos_sdk_path); addImageShim(b, module, target, macos_sdk_path); addVideoDependencies(b, module, target, macos_sdk_path); switch (target.result.os.tag) { .macos => addMacOSSystemLibraries(b, module, macos_sdk_path.?), .linux => { module.linkSystemLibrary("dl", .{}); module.linkSystemLibrary("pthread", .{}); module.linkSystemLibrary("m", .{}); }, else => {}, } } fn addYogaDependencies(b: *std.Build, module: *std.Build.Module, test_allocator: bool) void { const yoga_dep = b.dependency("yoga", .{}); var flags: []const []const u8 = &YOGA_CXX_FLAGS; if (test_allocator) flags = appendCFlags(b, flags, &.{"-DOT_YOGA_TEST_ALLOCATOR"}); // Zig 0.17's macOS math.h includes float.h for INFINITY and NAN alone, and // libc++'s float.h then skips the full include, so Yoga's FLT_MAX is missing. if (module.resolved_target.?.result.os.tag == .macos) flags = appendCFlags(b, flags, &.{"-DFLT_MAX=__FLT_MAX__"}); module.link_libcpp = true; module.addIncludePath(yoga_dep.path("")); module.addCSourceFiles(.{ .root = yoga_dep.path(""), .files = &YOGA_CXX_SOURCES, .flags = flags, }); module.addCSourceFile(.{ .file = b.path("src/yoga-bridge.cpp"), .flags = flags }); } fn ghosttyVtAvailable(target: std.Build.ResolvedTarget) bool { switch (target.result.cpu.arch) { .x86_64, .aarch64 => {}, else => return false, } return switch (target.result.os.tag) { .linux => target.result.abi.isMusl() or target.result.abi == .gnu, .macos => true, .windows => target.result.abi == .gnu, else => false, }; } fn addTranslatedCImports( b: *std.Build, module: *std.Build.Module, optimize: std.lang.Optimize, target: std.Build.ResolvedTarget, ) void { const miniaudio_translate = b.addTranslateC(.{ .root_source_file = b.path("src/vendor/miniaudio/miniaudio.h"), .target = target, .optimize = optimize, }); miniaudio_translate.addIncludePath(b.path("src")); module.addImport("miniaudio", miniaudio_translate.createModule()); const yoga_dep = b.dependency("yoga", .{}); const yoga_translate = b.addTranslateC(.{ .root_source_file = b.path("src/yoga-bridge.h"), .target = target, .optimize = optimize, }); yoga_translate.addIncludePath(yoga_dep.path("")); module.addImport("yoga", yoga_translate.createModule()); addContextABIHeader(b, module, optimize, target); } fn addContextABIHeader( b: *std.Build, module: *std.Build.Module, optimize: std.lang.Optimize, target: std.Build.ResolvedTarget, ) void { const header = b.addTranslateC(.{ .root_source_file = b.path("include/opentui.h"), .target = target, .optimize = optimize, }); module.addImport("context_abi_c", header.createModule()); } /// Files that tests embed by module name, such as Core's input parser conformance vectors. fn addTestFixtures(b: *std.Build, module: *std.Build.Module) void { module.addAnonymousImport("input_conformance", .{ .root_source_file = b.path("../core/src/lib/stdin-parser.conformance.json"), }); module.addAnonymousImport("markdown_conformance", .{ .root_source_file = b.path("../core/src/renderables/markdown.conformance.json"), }); module.addAnonymousImport("sibling_paint_order", .{ .root_source_file = b.path("../core/src/tests/fixtures/sibling-paint-order.json"), }); } /// Apply dependencies to a module fn applyDependencies( b: *std.Build, module: *std.Build.Module, optimize: std.lang.Optimize, target: std.Build.ResolvedTarget, build_options: *std.Build.Step.Options, ) void { module.addOptions("build_options", build_options); addTranslatedCImports(b, module, optimize, target); const ghostty_vt_available = ghosttyVtAvailable(target); const ghostty_vt_options = b.addOptions(); ghostty_vt_options.addOption(bool, "available", ghostty_vt_available); module.addOptions("ghostty_vt_options", ghostty_vt_options); if (ghostty_vt_available) { if (b.lazyDependency("ghostty", .{ .target = target, .optimize = .fast, // Enable once OpenTUI uses Ghostty VT at runtime. Until then, // Highway and simdutf only add binary size and exported symbols. .simd = false, .@"emit-lib-vt" = true, .@"emit-xcframework" = false, .@"emit-themes" = false, .i18n = false, .@"lib-version-string" = GHOSTTY_VT_VERSION, // The embedded terminal draws cells only, so it must not answer Kitty graphics or glyph // protocol commands. Kitty graphics also needs wuffs, which the vendored archive does not carry. .@"vt-features" = "-kitty-graphics,-glyph-protocol", })) |ghostty| { module.addImport("ghostty_vt", ghostty.module("ghostty-vt")); } } // Add uucode for grapheme break detection and width calculation if (b.lazyDependency("uucode", .{ .target = target, .optimize = optimize, .fields = @as([]const []const u8, &.{ "grapheme_break", "east_asian_width", "general_category", "is_default_ignorable", }), .fields_1 = @as([]const []const u8, &.{"wcwidth_zero_in_grapheme"}), })) |uucode_dep| { module.addImport("uucode", uucode_dep.module("uucode")); } } fn checkZigVersion() void { const current_version = builtin.zig_version; if (current_version.major == ZIG_VERSION.major and current_version.minor == ZIG_VERSION.minor and current_version.patch == ZIG_VERSION.patch) return; std.debug.print("\x1b[31mError: Unsupported Zig version {}.{}.{}\x1b[0m\n", .{ current_version.major, current_version.minor, current_version.patch, }); std.debug.print("Supported Zig versions:\n - {}.{}.{}\n", .{ ZIG_VERSION.major, ZIG_VERSION.minor, ZIG_VERSION.patch }); std.debug.print("\nPlease install a supported Zig version to continue.\n", .{}); std.process.exit(1); } pub fn build(b: *std.Build) void { checkZigVersion(); const optimize = b.standardOptimizeOption(.{}); const bench_optimize = b.option(std.lang.Optimize, "bench-optimize", "Optimize mode for benchmarks") orelse .fast; const test_optimize = b.option(std.lang.Optimize, "test-optimize", "Optimize mode for native tests") orelse .debug; const debug_use_llvm = b.option(bool, "debug-llvm", "Use LLVM backend for debug/test artifacts"); const target_option = b.option([]const u8, "library-target", "Build static/shared libraries for a specific target (e.g., 'x86_64-linux-gnu.2.17')."); const build_all = b.option(bool, "all", "Build for all supported targets") orelse false; const gpa_safe_stats = b.option(bool, "gpa-safe-stats", "Enable GPA safety checks for trustworthy allocator stats") orelse false; require_video = b.option(bool, "require-video", "Fail when a target's FFmpeg prefix is missing instead of building without video") orelse false; video_enabled = b.option(bool, "video", "Enable FFmpeg video decoding when available") orelse true; if (require_video and !video_enabled) { std.debug.print("error: -Drequire-video conflicts with -Dvideo=false\n", .{}); std.process.exit(1); } const macos_sdk_path = resolveMacOSSDKPath(b); const build_options = b.addOptions(); build_options.addOption(bool, "gpa_safe_stats", gpa_safe_stats); const module_target = b.standardTargetOptions(.{}); const opentui_module = b.addModule("opentui", .{ .root_source_file = b.path("src/opentui.zig"), .target = module_target, .optimize = optimize, }); applyDependencies(b, opentui_module, optimize, module_target, build_options); addNativeAudioDependencies(b, opentui_module, module_target, macos_sdk_path); addYogaDependencies(b, opentui_module, false); if (target_option) |target_str| { // Build single target buildSingleTarget(b, target_str, optimize, build_options, macos_sdk_path) catch |err| { std.debug.print("Error building target '{s}': {}\n", .{ target_str, err }); std.process.exit(1); }; } else if (build_all) { // Build all supported targets buildAllTargets(b, optimize, build_options, macos_sdk_path) catch |err| { std.debug.print("Error building all targets: {}\n", .{err}); std.process.exit(1); }; } else { // Build for native target only (default) buildNativeTarget(b, optimize, build_options, macos_sdk_path) catch |err| { std.debug.print("Error building native target: {}\n", .{err}); std.process.exit(1); }; } // Test step (native only) const test_step = b.step("test", "Run unit tests"); const native_target = nativeExecutableTarget(b); const test_mod = b.createModule(.{ .root_source_file = b.path("src/test.zig"), .target = native_target, .optimize = test_optimize, }); applyDependencies(b, test_mod, test_optimize, native_target, build_options); addTestFixtures(b, test_mod); const test_artifact = b.addTest(.{ .root_module = test_mod, .filters = if (b.option([]const u8, "test-filter", "Skip tests that do not match filter")) |f| &.{f} else &.{}, .use_llvm = debug_use_llvm, }); if (native_target.result.os.tag == .macos and macos_sdk_path == null) { printMissingMacOSSDK("native macOS tests"); std.process.exit(1); } addNativeAudioDependencies(b, test_mod, native_target, macos_sdk_path); addYogaDependencies(b, test_mod, true); const run_test = b.addRunArtifact(test_artifact); test_step.dependOn(&run_test.step); // utils.zig replaces std functions whose loads Zig 0.17.1's LLVM backend // splits into bytes (ziglang/zig#37120). Test them in optimized LLVM code. const utils_test = b.addTest(.{ .name = "utils-llvm", .root_module = b.createModule(.{ .root_source_file = b.path("src/utils.zig"), .target = native_target, .optimize = .safe, }), .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(utils_test).step); // Branch coverage: the same tests, compiled with LLVM SanitizerCoverage edge counters // (`fuzz = true`) and a runner that dumps every counter. `scripts/branch-cov.ts` // symbolizes the PCs and lists every branch arm no test reached. const cov_step = b.step("test-cov", "Run unit tests and dump branch-edge counters (see scripts/branch-cov.ts)"); const cov_mod = b.createModule(.{ .root_source_file = b.path("src/test.zig"), .target = native_target, .optimize = .debug, .fuzz = true, }); applyDependencies(b, cov_mod, .debug, native_target, build_options); addTestFixtures(b, cov_mod); const cov_artifact = b.addTest(.{ .name = "test-cov", .root_module = cov_mod, .filters = if (b.option([]const u8, "cov-filter", "Skip tests that do not match filter (test-cov)")) |f| &.{f} else &.{}, .test_runner = .{ .path = b.path("src/cov_runner.zig"), .mode = .simple }, .use_llvm = true, }); addNativeAudioDependencies(b, cov_mod, native_target, macos_sdk_path); addYogaDependencies(b, cov_mod, true); const install_cov = b.addInstallArtifact(cov_artifact, .{}); cov_step.dependOn(&install_cov.step); // `bun scripts/native-abi.ts --check --all-targets` checks C layouts on every supported target. const abi_step = b.step("test-abi", "Run C context acceptance against static/shared libraries"); // Shared-library acceptance needs the host's dynamic loader. Pin glibc to // avoid the startup-object issue described in nativeExecutableTarget. const abi_target = if (builtin.target.os.tag == .linux and !builtin.target.abi.isMusl()) b.resolveTargetQuery(.{ .cpu_arch = builtin.target.cpu.arch, .os_tag = .linux, .abi = .gnu, .glibc_version = .{ .major = 2, .minor = 17, .patch = 0 }, }) else native_target; for (createLibraries(b, abi_target, .debug, build_options, macos_sdk_path)) |lib| { const fixture_module = b.createModule(.{ .target = abi_target, .optimize = .debug, .link_libc = true, }); fixture_module.linkLibrary(lib); fixture_module.addCSourceFile(.{ .file = b.path("src/tests/context-abi.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" }, }); const fixture = b.addExecutable(.{ .name = b.fmt("context-abi-{s}", .{@tagName(lib.linkage.?)}), .root_module = fixture_module, }); const run_fixture = b.addRunArtifact(fixture); abi_step.dependOn(&run_fixture.step); } const lifetime_step = b.step("test-lifetime", "Run measurement lifetime tests with GPA leak accounting"); const lifetime_options = b.addOptions(); lifetime_options.addOption(bool, "gpa_safe_stats", true); const lifetime_mod = b.createModule(.{ .root_source_file = b.path("src/test.zig"), .target = native_target, .optimize = .debug, }); applyDependencies(b, lifetime_mod, .debug, native_target, lifetime_options); addTestFixtures(b, lifetime_mod); const lifetime_artifact = b.addTest(.{ .root_module = lifetime_mod, .filters = &.{ "measure", "NativeRenderable", "Context Yoga target" }, .use_llvm = debug_use_llvm, }); addNativeAudioDependencies(b, lifetime_mod, native_target, macos_sdk_path); addYogaDependencies(b, lifetime_mod, true); const run_lifetime = b.addRunArtifact(lifetime_artifact); lifetime_step.dependOn(&run_lifetime.step); // Bench step (native only) const bench_step = b.step("bench", "Run benchmarks"); const bench_mod = b.createModule(.{ .root_source_file = b.path("src/bench.zig"), .target = native_target, .optimize = bench_optimize, }); applyDependencies(b, bench_mod, bench_optimize, native_target, build_options); const bench_exe = b.addExecutable(.{ .name = "opentui-bench", .root_module = bench_mod, }); bench_mod.link_libc = true; // The scene bench drives a whole Context, which owns audio resources; this adds the image and video shims too. addNativeAudioDependencies(b, bench_mod, native_target, macos_sdk_path); addYogaDependencies(b, bench_mod, false); if (native_target.result.os.tag == .macos) addMacOSSDKSearchPaths(b, bench_mod, macos_sdk_path.?); // The benches in bench-isolated.zig compile as their own unit, so other benches cannot change their code. const bench_isolated_mod = b.createModule(.{ .root_source_file = b.path("src/bench-isolated.zig"), .target = native_target, .optimize = bench_optimize, .link_libc = true, }); applyDependencies(b, bench_isolated_mod, bench_optimize, native_target, build_options); bench_mod.addObject(b.addObject(.{ .name = "opentui-bench-isolated", .root_module = bench_isolated_mod })); const run_bench = b.addRunArtifact(bench_exe); run_bench.addPassthruArgs(); bench_step.dependOn(&run_bench.step); const bench_ffi_step = b.step("bench-ffi", "Build NativeSpanFeed benchmark library"); const bench_ffi_target = nativeTarget(b); const bench_ffi_mod = b.createModule(.{ .root_source_file = b.path("src/native-span-feed-bench-lib.zig"), .target = bench_ffi_target, .optimize = bench_optimize, }); applyDependencies(b, bench_ffi_mod, bench_optimize, bench_ffi_target, build_options); const bench_ffi_lib = b.addLibrary(.{ .name = "native_span_feed_bench", .root_module = bench_ffi_mod, .linkage = .dynamic, }); if (bench_ffi_target.result.os.tag == .macos and macos_sdk_path == null) { printMissingMacOSSDK("native macOS benchmark FFI library"); std.process.exit(1); } addNativeAudioDependencies(b, bench_ffi_mod, bench_ffi_target, macos_sdk_path); addYogaDependencies(b, bench_ffi_mod, false); const install_bench_ffi = b.addInstallArtifact(bench_ffi_lib, .{}); bench_ffi_step.dependOn(&install_bench_ffi.step); bench_step.dependOn(bench_ffi_step); // Debug step (native only) const debug_step = b.step("debug", "Run debug executable"); const debug_mod = b.createModule(.{ .root_source_file = b.path("src/debug-view.zig"), .target = native_target, .optimize = .debug, }); applyDependencies(b, debug_mod, .debug, native_target, build_options); const debug_exe = b.addExecutable(.{ .name = "opentui-debug", .root_module = debug_mod, .use_llvm = debug_use_llvm, }); const run_debug = b.addRunArtifact(debug_exe); debug_step.dependOn(&run_debug.step); } fn buildAllTargets( b: *std.Build, optimize: std.lang.Optimize, build_options: *std.Build.Step.Options, macos_sdk_path: ?[]const u8, ) !void { for (SUPPORTED_TARGETS) |supported_target| { try buildTarget( b, supported_target.zig_target, supported_target.output_name, supported_target.description, optimize, build_options, macos_sdk_path, ); } } fn buildNativeTarget( b: *std.Build, optimize: std.lang.Optimize, build_options: *std.Build.Step.Options, macos_sdk_path: ?[]const u8, ) !void { // Find the matching supported target for the native platform const native_arch = @tagName(builtin.target.cpu.arch); const native_os = @tagName(builtin.target.os.tag); for (SUPPORTED_TARGETS) |supported_target| { // Check if this target matches the native platform if (std.mem.find(u8, supported_target.zig_target, native_arch) != null and std.mem.find(u8, supported_target.zig_target, native_os) != null) { try buildTarget( b, supported_target.zig_target, supported_target.output_name, supported_target.description, optimize, build_options, macos_sdk_path, ); return; } } std.debug.print("No matching supported target for native platform ({s}-{s})\n", .{ native_arch, native_os }); return error.UnsupportedNativeTarget; } fn buildSingleTarget( b: *std.Build, target_str: []const u8, optimize: std.lang.Optimize, build_options: *std.Build.Step.Options, macos_sdk_path: ?[]const u8, ) !void { // Check if it matches a known target, use its output_name for (SUPPORTED_TARGETS) |supported_target| { if (std.mem.eql(u8, target_str, supported_target.zig_target)) { try buildTarget( b, supported_target.zig_target, supported_target.output_name, supported_target.description, optimize, build_options, macos_sdk_path, ); return; } } // Custom target - use target string as output name const description = try std.fmt.allocPrint(b.allocator, "Custom target: {s}", .{target_str}); try buildTarget(b, target_str, target_str, description, optimize, build_options, macos_sdk_path); } fn buildTarget( b: *std.Build, zig_target: []const u8, output_name: []const u8, description: []const u8, optimize: std.lang.Optimize, build_options: *std.Build.Step.Options, macos_sdk_path: ?[]const u8, ) !void { const target_query = try std.Target.Query.parse(.{ .arch_os_abi = zig_target }); const target = b.resolveTargetQuery(target_query); if (target.result.os.tag == .macos and macos_sdk_path == null) { printMissingMacOSSDK(description); return error.MissingMacOSSDK; } const install_path = try std.fmt.allocPrint(b.allocator, "../lib/{s}", .{output_name}); const build_step_name = try std.fmt.allocPrint(b.allocator, "build-{s}", .{output_name}); const build_step = b.step(build_step_name, try std.fmt.allocPrint(b.allocator, "Build for {s}", .{description})); for (createLibraries(b, target, optimize, build_options, macos_sdk_path)) |lib| { const windows = target.result.os.tag == .windows; if (lib.isStaticLibrary() and (windows or target.result.os.tag == .linux)) { // Linux has no system libc++ to count on, so the archive carries Zig's. MSVC has none of MinGW's // runtime, so the Windows archive carries what of it MSVC lacks. The shared library installs the header. const run = b.addRunArtifact(bundleTool(b, windows)); run.addArgs(&.{ b.graph.zig_exe, zig_target, @tagName(optimize) }); run.addFileArg(b.addWriteFiles().add("probe.cpp", "int opentui_probe;\n")); if (windows) run.addFileArg(b.path("src/windows-msvc-shim.c")); if (windows) run.addDirectoryArg(std.Build.LazyPath.zig_lib.path(b, "libc/mingw/lib-common")); run.addArtifactArg(lib); const name = if (windows) "opentui-static.lib" else "libopentui.a"; const archive = run.addOutputFileArg(name); const install_archive = b.addInstallFileWithDir(archive, .{ .custom = install_path }, name); build_step.dependOn(&install_archive.step); b.getInstallStep().dependOn(&install_archive.step); continue; } const install_dir = b.addInstallArtifact(lib, .{ .dest_dir = .{ .override = .{ .custom = install_path } }, .h_dir = .{ .override = .{ .custom = install_path } }, .implib_dir = if (lib.producesImplib()) .{ .override = .{ .custom = install_path } } else .disabled, .pdb_dir = if (optimize == .debug) .default else if (lib.producesPdbFile()) .{ .override = .{ .custom = install_path } } else .disabled, }); build_step.dependOn(&install_dir.step); b.getInstallStep().dependOn(&install_dir.step); } } var bundle_tools: [2]?*std.Build.Step.Compile = .{ null, null }; /// scripts/bundle-windows-runtime.zig, or scripts/bundle-libcxx.zig for Linux. fn bundleTool(b: *std.Build, windows: bool) *std.Build.Step.Compile { const tool = &bundle_tools[@intFromBool(windows)]; const name = if (windows) "bundle-windows-runtime" else "bundle-libcxx"; if (tool.* == null) tool.* = b.addExecutable(.{ .name = name, .root_module = b.createModule(.{ .root_source_file = b.path(b.fmt("scripts/{s}.zig", .{name})), .target = b.graph.host, }), }); return tool.*.?; } fn createLibraries( b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, build_options: *std.Build.Step.Options, macos_sdk_path: ?[]const u8, ) [2]*std.Build.Step.Compile { const module = b.createModule(.{ .root_source_file = b.path(ROOT_SOURCE_FILE), .target = target, .optimize = optimize, // Release symbols must describe this exact optimized code. The package // build detaches them before stripping only the distribution copy. .strip = false, .pic = true, }); applyDependencies(b, module, optimize, target, build_options); addNativeAudioDependencies(b, module, target, macos_sdk_path); addYogaDependencies(b, module, false); const shared = b.addLibrary(.{ .name = LIB_NAME, .root_module = module, .linkage = .dynamic, }); if (target.result.os.tag == .linux and optimize != .debug) shared.build_id = .sha1; // Zig's default DLL entry skips the MinGW CRT startup, so CRT state such as // the DLL atexit table would stay uninitialized. MinGW's strtod registers // there, and both the Zig code and FFmpeg call it, so this applies with or // without video. if (target.result.os.tag == .windows and target.result.abi == .gnu) { shared.entry = .{ .symbol_name = "DllMainCRTStartup" }; } const static = b.addLibrary(.{ // COFF uses .lib for both archives and DLL import libraries. .name = if (target.result.os.tag == .windows) "opentui-static" else LIB_NAME, .root_module = module, .linkage = .static, }); shared.installHeader(b.path("include/opentui.h"), "opentui.h"); static.installHeader(b.path("include/opentui.h"), "opentui.h"); return .{ shared, static }; }