build.zig (3311B)
1 const std = @import("std"); 2 3 /// ## Table of contents 4 /// 5 /// - Opts (`zig build -D<opt>` setup) 6 /// - Deps (external `build.zig` Builds) 7 /// - Mods (Code + semantically relevant config, e.g. target and optimization levels) 8 /// - Compiles (Module + "implementation details" of how to do the compile, e.g. use_llvm) 9 /// - Bins (Compile + Run, a hack to make the build system work better[^fno-emit-bin]) 10 /// - Steps (`zig build <command>` setup) 11 /// 12 /// [^fno-emit-bin]: See issue [#18877](https://github.com/ziglang/zig/issues/18877) 13 pub fn build(b: *std.Build) !void { 14 // ======== 15 // Opts 16 const target_opt = b.standardTargetOptions(.{}); 17 const optimize_opt = b.standardOptimizeOption(.{}); 18 const llvm_opt = b.option( 19 bool, 20 "llvm", 21 "Force llvm to be used or not (default: whatever the compiler default is)", 22 ) orelse null; 23 24 const tracy_opt = b.option( 25 bool, 26 "tracy", 27 "Enable Tracy profiling (default: false)", 28 ) orelse false; 29 30 // ======== 31 // Deps 32 const tracy_dep = b.dependency("tracy", .{ 33 .enable = tracy_opt, 34 }); 35 36 // ======== 37 // Mods 38 const mymarkdown_mod = b.addModule("mymarkdown", .{ 39 .root_source_file = b.path("src/root.zig"), 40 .target = target_opt, 41 .optimize = optimize_opt, 42 .imports = &.{ 43 .{ .name = "tracy", .module = tracy_dep.module("tracy") }, 44 }, 45 }); 46 47 const mymarkdown_cli_mod = b.addModule("mymarkdown_cli", .{ 48 .root_source_file = b.path("src/main.zig"), 49 .target = target_opt, 50 .optimize = optimize_opt, 51 .imports = &.{ 52 .{ .name = "mymarkdown", .module = mymarkdown_mod }, 53 .{ .name = "tracy", .module = tracy_dep.module("tracy") }, 54 }, 55 }); 56 57 // ======== 58 // Compiles 59 const mymarkdown_cli_compile = b.addExecutable(.{ 60 .name = "mymarkdown", 61 .root_module = mymarkdown_cli_mod, 62 .use_llvm = llvm_opt, 63 }); 64 65 const mymarkdown_test_compile = b.addTest(.{ 66 .name = "mymarkdown_test", 67 .root_module = mymarkdown_mod, 68 .use_llvm = llvm_opt, 69 }); 70 71 // ======== 72 // Bins 73 const mymarkdown_cli_bin: Binary = try .make(b, mymarkdown_cli_compile); 74 const mymarkdown_test_bin: Binary = try .make(b, mymarkdown_test_compile); 75 76 // ======== 77 // Steps 78 const check_step = b.step("check", "Check if the mymarkdown CLI compiles"); 79 check_step.dependOn(&mymarkdown_cli_compile.step); 80 81 const test_step = b.step("test", "Run tests"); 82 test_step.dependOn(check_step); 83 mymarkdown_test_bin.runInStep(test_step); 84 85 const run_step = b.step("run", "Run the mymarkdown CLI"); 86 mymarkdown_cli_bin.runInStep(run_step); 87 mymarkdown_cli_bin.install(b); 88 } 89 90 const Binary = struct { 91 run: *std.Build.Step.Run, 92 93 fn make(b: *std.Build, compile: *std.Build.Step.Compile) !Binary { 94 const copy = try b.allocator.create(std.Build.Step.Compile); 95 copy.* = compile.*; 96 return .{ .run = b.addRunArtifact(copy) }; 97 } 98 99 fn runInStep(runnable: Binary, step: *std.Build.Step) void { 100 step.dependOn(&runnable.run.step); 101 } 102 103 fn install(runnable: Binary, b: *std.Build) void { 104 b.installArtifact(runnable.run.producer.?); 105 } 106 };