a modern tui library written in zig
at v0.3.0 131 lines 5.0 kB view raw
1const std = @import("std"); 2 3pub fn build(b: *std.Build) void { 4 const include_libxev = b.option(bool, "libxev", "Enable support for libxev library (default: true)") orelse true; 5 const include_images = b.option(bool, "images", "Enable support for images (default: true)") orelse true; 6 const include_nvim = b.option(bool, "nvim", "Enable support for the neovim widget (default: false)") orelse true; 7 const include_text_input = b.option(bool, "text_input", "Enable support for the TextInput widget (default: true)") orelse true; 8 9 const options = b.addOptions(); 10 options.addOption(bool, "libxev", include_libxev); 11 options.addOption(bool, "images", include_images); 12 options.addOption(bool, "nvim", include_nvim); 13 options.addOption(bool, "text_input", include_text_input); 14 15 const options_mod = options.createModule(); 16 17 const target = b.standardTargetOptions(.{}); 18 const optimize = b.standardOptimizeOption(.{}); 19 const root_source_file = b.path("src/main.zig"); 20 21 // Dependencies 22 const zg_dep = b.dependency("zg", .{ 23 .optimize = optimize, 24 .target = target, 25 }); 26 const zigimg_dep = if (include_images) b.lazyDependency("zigimg", .{ 27 .optimize = optimize, 28 .target = target, 29 }) else null; 30 const gap_buffer_dep = if (include_text_input) b.lazyDependency("gap_buffer", .{ 31 .optimize = optimize, 32 .target = target, 33 }) else null; 34 const znvim_dep = if (include_nvim) b.lazyDependency("znvim", .{ 35 .optimize = optimize, 36 .target = target, 37 }) else null; 38 const xev_dep = if (include_libxev) b.lazyDependency("libxev", .{ 39 .optimize = optimize, 40 .target = target, 41 }) else null; 42 43 // Module 44 const vaxis_mod = b.addModule("vaxis", .{ 45 .root_source_file = root_source_file, 46 .target = target, 47 .optimize = optimize, 48 }); 49 vaxis_mod.addImport("code_point", zg_dep.module("code_point")); 50 vaxis_mod.addImport("grapheme", zg_dep.module("grapheme")); 51 vaxis_mod.addImport("DisplayWidth", zg_dep.module("DisplayWidth")); 52 if (zigimg_dep) |dep| vaxis_mod.addImport("zigimg", dep.module("zigimg")); 53 if (gap_buffer_dep) |dep| vaxis_mod.addImport("gap_buffer", dep.module("gap_buffer")); 54 if (znvim_dep) |dep| vaxis_mod.addImport("znvim", dep.module("znvim")); 55 if (xev_dep) |dep| vaxis_mod.addImport("xev", dep.module("xev")); 56 vaxis_mod.addImport("build_options", options_mod); 57 58 // Examples 59 const Example = enum { 60 cli, 61 image, 62 main, 63 nvim, 64 table, 65 text_input, 66 vaxis, 67 xev, 68 }; 69 const example_option = b.option(Example, "example", "Example to run (default: text_input)") orelse .text_input; 70 const example_step = b.step("example", "Run example"); 71 const example = b.addExecutable(.{ 72 .name = "example", 73 // future versions should use b.path, see zig PR #19597 74 .root_source_file = b.path( 75 b.fmt("examples/{s}.zig", .{@tagName(example_option)}), 76 ), 77 .target = target, 78 .optimize = optimize, 79 }); 80 example.root_module.addImport("vaxis", vaxis_mod); 81 if (xev_dep) |dep| example.root_module.addImport("xev", dep.module("xev")); 82 83 const example_run = b.addRunArtifact(example); 84 example_step.dependOn(&example_run.step); 85 86 // Tests 87 const tests_step = b.step("test", "Run tests"); 88 89 const tests = b.addTest(.{ 90 .root_source_file = b.path("src/main.zig"), 91 .target = target, 92 .optimize = optimize, 93 }); 94 tests.root_module.addImport("code_point", zg_dep.module("code_point")); 95 tests.root_module.addImport("grapheme", zg_dep.module("grapheme")); 96 tests.root_module.addImport("DisplayWidth", zg_dep.module("DisplayWidth")); 97 if (zigimg_dep) |dep| tests.root_module.addImport("zigimg", dep.module("zigimg")); 98 if (gap_buffer_dep) |dep| tests.root_module.addImport("gap_buffer", dep.module("gap_buffer")); 99 if (znvim_dep) |dep| tests.root_module.addImport("znvim", dep.module("znvim")); 100 tests.root_module.addImport("build_options", options_mod); 101 102 const tests_run = b.addRunArtifact(tests); 103 b.installArtifact(tests); 104 tests_step.dependOn(&tests_run.step); 105 106 // Lints 107 const lints_step = b.step("lint", "Run lints"); 108 109 const lints = b.addFmt(.{ 110 .paths = &.{ "src", "build.zig" }, 111 .check = true, 112 }); 113 114 lints_step.dependOn(&lints.step); 115 b.default_step.dependOn(lints_step); 116 117 // Docs 118 const docs_step = b.step("docs", "Build the vaxis library docs"); 119 const docs_obj = b.addObject(.{ 120 .name = "vaxis", 121 .root_source_file = root_source_file, 122 .target = target, 123 .optimize = optimize, 124 }); 125 const docs = docs_obj.getEmittedDocs(); 126 docs_step.dependOn(&b.addInstallDirectory(.{ 127 .source_dir = docs, 128 .install_dir = .prefix, 129 .install_subdir = "docs", 130 }).step); 131}