Skip to content

Commit 8e990d8

Browse files
committed
Optimizing the build process
Optimizing the build process, we can now use zig to build all C examples
1 parent be5e294 commit 8e990d8

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

build.zig

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const CrossTarget = std.zig.CrossTarget;
77
const Compile = Build.Step.Compile;
88
const Module = Build.Module;
99

10+
const log = std.log.scoped(.WebUI);
11+
1012
const min_zig_string = "0.11.0";
1113

1214
const default_isStatic = true;
@@ -40,9 +42,104 @@ pub fn build(b: *std.Build) void {
4042

4143
webui.installHeader("include/webui.h", "webui.h");
4244

45+
build_examples(b, optimize, target, webui);
46+
4347
b.installArtifact(webui);
4448
}
4549

50+
fn build_examples(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_lib: *Compile) void {
51+
var lazy_path = Build.LazyPath{
52+
.path = "examples/C",
53+
};
54+
55+
const build_all_step = b.step("build_all", "build all examples");
56+
57+
const examples_path = lazy_path.getPath(b);
58+
59+
var iter_dir = if (comptime current_zig.minor == 11)
60+
std.fs.openIterableDirAbsolute(examples_path, .{}) catch |err| {
61+
log.err("open examples_path failed, err is {}", .{err});
62+
std.os.exit(1);
63+
}
64+
else
65+
std.fs.openDirAbsolute(examples_path, .{ .iterate = true }) catch |err| {
66+
log.err("open examples_path failed, err is {}", .{err});
67+
std.os.exit(1);
68+
};
69+
defer iter_dir.close();
70+
71+
var itera = iter_dir.iterate();
72+
73+
while (itera.next()) |val| {
74+
if (val) |entry| {
75+
if (entry.kind == .directory) {
76+
const example_name = entry.name;
77+
const path = std.fmt.allocPrint(b.allocator, "examples/C/{s}/main.c", .{example_name}) catch |err| {
78+
log.err("fmt path for examples failed, err is {}", .{err});
79+
std.os.exit(1);
80+
};
81+
82+
const exe = b.addExecutable(.{
83+
.name = example_name,
84+
.target = target,
85+
.optimize = optimize,
86+
});
87+
88+
exe.addCSourceFile(.{
89+
.file = .{
90+
.path = path,
91+
},
92+
.flags = &.{},
93+
});
94+
95+
exe.linkLibrary(webui_lib);
96+
97+
const exe_install = b.addInstallArtifact(exe, .{});
98+
99+
build_all_step.dependOn(&exe_install.step);
100+
101+
const exe_run = b.addRunArtifact(exe);
102+
exe_run.step.dependOn(&exe_install.step);
103+
104+
if (comptime (current_zig.minor > 11)) {
105+
const cwd = std.fmt.allocPrint(b.allocator, "examples/C/{s}", .{example_name}) catch |err| {
106+
log.err("fmt path for examples failed, err is {}", .{err});
107+
std.os.exit(1);
108+
};
109+
exe_run.setCwd(.{
110+
.path = cwd,
111+
});
112+
} else {
113+
const cwd = std.fmt.allocPrint(b.allocator, "{s}/{s}", .{ examples_path, example_name }) catch |err| {
114+
log.err("fmt path for examples failed, err is {}", .{err});
115+
std.os.exit(1);
116+
};
117+
// TODO: fix this
118+
exe_run.cwd = cwd;
119+
}
120+
121+
const step_name = std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}) catch |err| {
122+
log.err("fmt step_name for examples failed, err is {}", .{err});
123+
std.os.exit(1);
124+
};
125+
126+
const step_desc = std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}) catch |err| {
127+
log.err("fmt step_desc for examples failed, err is {}", .{err});
128+
std.os.exit(1);
129+
};
130+
131+
const exe_run_step = b.step(step_name, step_desc);
132+
exe_run_step.dependOn(&exe_run.step);
133+
}
134+
} else {
135+
break;
136+
}
137+
} else |err| {
138+
log.err("iterate examples_path failed, err is {}", .{err});
139+
std.os.exit(1);
140+
}
141+
}
142+
46143
fn build_webui(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static: bool, enable_tls: bool) *Compile {
47144
const name = "webui";
48145
const webui = if (is_static) b.addStaticLibrary(.{ .name = name, .target = target, .optimize = optimize }) else b.addSharedLibrary(.{ .name = name, .target = target, .optimize = optimize });

0 commit comments

Comments
 (0)