build.zig (6047B)
1 const std = @import("std"); 2 3 var alloc = std.heap.DebugAllocator(.{}){}; 4 5 fn cFiles(name: []const u8) ![][]const u8 { 6 var dir = try std.fs.cwd().openDir(name, .{.iterate = true}); 7 defer dir.close(); 8 const dba = alloc.allocator(); 9 var files = std.ArrayList([]const u8).empty; 10 var iter = dir.iterate(); 11 while (try iter.next()) |dent| { 12 if (std.mem.endsWith(u8, dent.name, ".c")) { 13 try files.append(dba, try std.fs.path.join(dba, &.{name, dent.name})); 14 } 15 } 16 return files.toOwnedSlice(dba); 17 } 18 19 pub fn build(b: *std.Build) void { 20 // Standard target options allows the person running `zig build` to choose 21 // what target to build for. Here we do not override the defaults, which 22 // means any target is allowed, and the default is native. Other options 23 // for restricting supported target set are available. 24 const target = b.standardTargetOptions(.{}); 25 26 const fence_mod = b.createModule(.{ 27 .root_source_file = b.path("fence/root.zig"), 28 .target = target, 29 }); 30 const fence = b.addLibrary(.{ 31 .name = "fence", 32 .root_module = fence_mod, 33 }); 34 // https://github.com/ziglang/zig/issues/6817 35 fence.bundle_compiler_rt = true; 36 const header_file = b.addInstallHeaderFile(b.path("fence/fence.h"), "fence.h"); 37 b.getInstallStep().dependOn(&header_file.step); 38 39 const extlibs = [_][]const u8{ 40 "cairo", "glib-2.0", "gtk-3", "gdk-3", "gdk_pixbuf-2.0", "pango-1.0", "gio-2.0", 41 "iconv", 42 "gnutls", "gmp", "unistring", "nettle", "tasn1", "p11-kit", "hogweed", "libidn2", 43 }; 44 45 const common_mod = b.createModule(.{ 46 .target = target, 47 .link_libc = true, 48 }); 49 common_mod.linkLibrary(fence); 50 for (extlibs) |l| { 51 common_mod.linkSystemLibrary(l, .{}); 52 } 53 const common = b.addLibrary(.{ 54 .name = "clawscommon", 55 .root_module = common_mod, 56 }); 57 common.addCSourceFiles(.{ 58 .files = cFiles("common") catch unreachable, 59 .flags = &.{"-g"}, 60 }); 61 common.addIncludePath(b.path("zig-out/include")); 62 63 const gtk_mod = b.createModule(.{ 64 .target = target, 65 .link_libc = true, 66 }); 67 for (extlibs) |l| { 68 gtk_mod.linkSystemLibrary(l, .{}); 69 } 70 gtk_mod.linkSystemLibrary("atk-1.0", .{}); 71 const gtk = b.addLibrary(.{ 72 .name = "clawsgtk", 73 .root_module = gtk_mod, 74 }); 75 gtk.addCSourceFiles(.{ 76 .files = cFiles("gtk") catch unreachable, 77 .flags = &.{"-g"}, 78 }); 79 gtk.addIncludePath(b.path(".")); 80 gtk.addIncludePath(b.path("common")); 81 82 const etpan_mod = b.createModule(.{ 83 .target = target, 84 .link_libc = true, 85 }); 86 for (extlibs) |l| { 87 etpan_mod.linkSystemLibrary(l, .{}); 88 } 89 etpan_mod.linkLibrary(common); 90 etpan_mod.linkSystemLibrary("etpan", .{}); 91 etpan_mod.linkSystemLibrary("atk-1.0", .{}); 92 const etpan = b.addLibrary(.{ 93 .name = "clawsetpan", 94 .root_module = etpan_mod, 95 }); 96 etpan.addCSourceFiles(.{ 97 .files = cFiles("etpan") catch unreachable, 98 .flags = &.{"-g"}, 99 }); 100 etpan.addIncludePath(b.path(".")); 101 etpan.addIncludePath(b.path("common")); 102 103 const exe_mod = b.createModule(.{ 104 .target = target, 105 .optimize = b.standardOptimizeOption(.{}), 106 .link_libc = true, 107 }); 108 exe_mod.linkLibrary(fence); 109 exe_mod.linkLibrary(common); 110 exe_mod.linkLibrary(gtk); 111 exe_mod.linkLibrary(etpan); 112 for (extlibs) |l| { 113 exe_mod.linkSystemLibrary(l, .{}); 114 } 115 116 // This creates another `std.Build.Step.Compile`, but this one builds an executable 117 // rather than a static library. 118 const exe = b.addExecutable(.{ 119 .name = "talons", 120 .root_module = exe_mod, 121 }); 122 exe.addCSourceFiles(.{ 123 .files = cFiles(".") catch unreachable, 124 .flags = &.{ 125 "-Wall", 126 "-DGTK_DISABLE_DEPRECATION_WARNINGS", 127 "-DGDK_DISABLE_DEPRECATION_WARNINGS", 128 "-g", 129 "-std=c17", 130 }, 131 }); 132 exe.addIncludePath(b.path("..")); 133 exe.addIncludePath(b.path(".")); 134 exe.addIncludePath(b.path("etpan")); 135 exe.addIncludePath(b.path("gtk")); 136 exe.addIncludePath(b.path("common")); 137 exe.addIncludePath(b.path("zig-out/include")); 138 exe.addSystemIncludePath(b.path("../../../../../usr/local/include/atk-1.0")); 139 140 // exe.step.dependOn(&fence.step); 141 142 // This declares intent for the executable to be installed into the 143 // standard location when the user invokes the "install" step (the default 144 // step when running `zig build`). 145 b.installArtifact(exe); 146 147 // This *creates* a Run step in the build graph, to be executed when another 148 // step is evaluated that depends on it. The next line below will establish 149 // such a dependency. 150 const run_cmd = b.addRunArtifact(exe); 151 152 // By making the run step depend on the install step, it will be run from the 153 // installation directory rather than directly from within the cache directory. 154 // This is not necessary, however, if the application depends on other installed 155 // files, this ensures they will be present and in the expected location. 156 run_cmd.step.dependOn(b.getInstallStep()); 157 158 // This creates a build step. It will be visible in the `zig build --help` menu, 159 // and can be selected like this: `zig build run` 160 // This will evaluate the `run` step rather than the default, which is "install". 161 const run_step = b.step("run", "Run the app"); 162 run_step.dependOn(&run_cmd.step); 163 164 const exe_unit_tests = b.addTest(.{ 165 .root_module = exe_mod, 166 }); 167 168 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); 169 170 // Similar to creating the run step earlier, this exposes a `test` step to 171 // the `zig build --help` menu, providing a way for the user to request 172 // running the unit tests. 173 const test_step = b.step("test", "Run unit tests"); 174 test_step.dependOn(&run_exe_unit_tests.step); 175 }