Skip to content

Commit a5c63bc

Browse files
committed
chore: add comment to build.zig
1 parent 2e1a449 commit a5c63bc

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

build.zig

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,41 @@ const builtin = @import("builtin");
33

44
const Build = std.Build;
55

6+
// Minimum required Zig version for this project
67
const min_zig_string = "0.12.0";
78
const current_zig = builtin.zig_version;
89

910
// NOTE: we should note that when enable tls support we cannot compile with musl
1011

12+
// Compile-time check to ensure the Zig version meets the minimum requirement
1113
comptime {
1214
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
1315
if (current_zig.order(min_zig) == .lt) {
1416
@compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
1517
}
1618
}
1719

20+
// Define logger and useful type aliases
1821
const log = std.log.scoped(.WebUI);
1922
const OptimizeMode = std.builtin.OptimizeMode;
2023
const CrossTarget = std.Target.Query;
2124
const Compile = Build.Step.Compile;
2225
const Module = Build.Module;
2326

27+
// Default build configuration options
2428
const default_isStatic = true;
2529
const default_enableTLS = false;
2630

2731
pub fn build(b: *Build) !void {
32+
// Parse command-line options or use defaults
2833
const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic;
2934
const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS;
3035

36+
// Standard build options for target and optimization
3137
const target = b.standardTargetOptions(.{});
3238
const optimize = b.standardOptimizeOption(.{});
3339

40+
// TLS support has some limitations
3441
if (enableTLS) {
3542
log.info("enable TLS support", .{});
3643
if (!target.query.isNative()) {
@@ -39,15 +46,16 @@ pub fn build(b: *Build) !void {
3946
}
4047
}
4148

42-
// create a options for command parameter
49+
// Create build options that will be used as a module
4350
const flags_options = b.addOptions();
4451

45-
// add option
52+
// Configure compile-time options
4653
flags_options.addOption(bool, "enableTLS", enableTLS);
4754

48-
// create a new module for flags options
55+
// Create a module that exposes the options
4956
const flags_module = flags_options.createModule();
5057

58+
// Get the webui dependency with appropriate options
5159
const webui = b.dependency("webui", .{
5260
.target = target,
5361
.optimize = optimize,
@@ -56,6 +64,7 @@ pub fn build(b: *Build) !void {
5664
.verbose = .err,
5765
});
5866

67+
// Create the webui module that applications can import
5968
const webui_module = b.addModule("webui", .{
6069
.root_source_file = b.path(b.pathJoin(&.{ "src", "webui.zig" })),
6170
.imports = &.{
@@ -65,22 +74,26 @@ pub fn build(b: *Build) !void {
6574
},
6675
},
6776
});
77+
// Link against the webui library
6878
webui_module.linkLibrary(webui.artifact("webui"));
6979
if (!isStatic) {
80+
// For dynamic libraries, install the shared library
7081
b.installArtifact(webui.artifact("webui"));
7182
}
7283

73-
// generate docs
84+
// Setup documentation generation
7485
generate_docs(b, optimize, target, flags_module);
7586

76-
// build examples
87+
// Build example applications
7788
build_examples(b, optimize, target, webui_module, webui.artifact("webui")) catch |err| {
7889
log.err("failed to build examples: {}", .{err});
7990
std.process.exit(1);
8091
};
8192
}
8293

94+
// Function to generate API documentation
8395
fn generate_docs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, flags_module: *Module) void {
96+
// Create a temporary object for documentation generation
8497
const webui_lib = b.addObject(.{
8598
.name = "webui_lib",
8699
.root_source_file = b.path(b.pathJoin(&.{ "src", "webui.zig" })),
@@ -90,8 +103,10 @@ fn generate_docs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget
90103

91104
webui_lib.root_module.addImport("flags", flags_module);
92105

106+
// Create a build step for documentation
93107
const docs_step = b.step("docs", "Generate docs");
94108

109+
// Setup documentation installation
95110
const docs_install = b.addInstallDirectory(.{
96111
.source_dir = webui_lib.getEmittedDocs(),
97112
.install_dir = .prefix,
@@ -101,15 +116,18 @@ fn generate_docs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget
101116
docs_step.dependOn(&docs_install.step);
102117
}
103118

119+
// Function to build all example applications
104120
fn build_examples(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, webui_module: *Module, webui_lib: *Compile) !void {
105121

106-
// we use lazyPath to get absolute path of package
122+
// Get the absolute path to the examples directory
107123
var lazy_path = b.path("examples");
108124

125+
// Create a step to build all examples
109126
const build_all_step = b.step("examples", "build all examples");
110127

111128
const examples_path = lazy_path.getPath(b);
112129

130+
// Open the examples directory for iteration
113131
var iter_dir = std.fs.openDirAbsolute(
114132
examples_path,
115133
.{ .iterate = true },
@@ -123,6 +141,7 @@ fn build_examples(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarge
123141

124142
var itera = iter_dir.iterate();
125143

144+
// Iterate through all subdirectories in the examples directory
126145
while (try itera.next()) |val| {
127146
if (val.kind != .directory) {
128147
continue;
@@ -131,32 +150,37 @@ fn build_examples(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarge
131150
const example_name = val.name;
132151
const path = b.pathJoin(&.{ "examples", example_name, "main.zig" });
133152

153+
// Create an executable for each example
134154
const exe = b.addExecutable(.{
135155
.name = example_name,
136156
.root_source_file = b.path(path),
137157
.target = target,
138158
.optimize = optimize,
139159
});
140160

161+
// Add the webui module and link against the library
141162
exe.root_module.addImport("webui", webui_module);
142163
exe.linkLibrary(webui_lib);
143164

165+
// Setup installation
144166
const exe_install = b.addInstallArtifact(exe, .{});
145167

146168
build_all_step.dependOn(&exe_install.step);
147169

170+
// Create a run step for the example
148171
const exe_run = b.addRunArtifact(exe);
149172
exe_run.step.dependOn(&exe_install.step);
150173

174+
// Set the working directory for the run
151175
const cwd = b.path(b.pathJoin(&.{ "examples", example_name }));
152-
153176
exe_run.setCwd(cwd);
154177

178+
// Create a named step to run this specific example
155179
const step_name = try std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name});
156-
157180
const step_desc = try std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name});
158181

159182
const exe_run_step = b.step(step_name, step_desc);
160183
exe_run_step.dependOn(&exe_run.step);
161184
}
162185
}
186+

0 commit comments

Comments
 (0)