Skip to content

Commit fea4ae1

Browse files
committed
fix(build): add Zig 0.14 compatibility for library and examples
- update build.zig to support Zig 0.14 changes in addLibrary/addExecutable APIs - conditionally handle library and executable creation based on Zig version - improve linker flag debug logging for better clarity
1 parent 0636124 commit fea4ae1

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

build.zig

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn build(b: *Build) !void {
4343
log(.info, .WebUI, "Done.", .{});
4444
}
4545

46-
const webui = if (is_dynamic) b.addSharedLibrary(.{
46+
const webui = if (builtin.zig_version.minor == 14) (if (is_dynamic) b.addSharedLibrary(.{
4747
.name = lib_name,
4848
.target = target,
4949
.optimize = optimize,
@@ -52,7 +52,18 @@ pub fn build(b: *Build) !void {
5252
.name = lib_name,
5353
.target = target,
5454
.optimize = optimize,
55+
})) else b.addLibrary(.{
56+
.name = lib_name,
57+
.linkage = if (is_dynamic) .dynamic else .static,
58+
.root_module = b.createModule(.{
59+
// not, pic enabled, zig will not allow to build a static library
60+
// .pic = is_dynamic,
61+
.pic = if (is_dynamic) is_dynamic else true,
62+
.target = target,
63+
.optimize = optimize,
64+
}),
5565
});
66+
5667
try addLinkerFlags(b, webui, enable_tls, debug_dependencies, enable_webui_log);
5768

5869
b.installArtifact(webui);
@@ -132,10 +143,12 @@ fn addLinkerFlags(
132143
for (webui.root_module.link_objects.items) |lo| {
133144
switch (lo) {
134145
.c_source_file => |csf| {
135-
log(.debug, .WebUI, "{s} linker flags: {s}", .{
146+
log(.debug, .WebUI, "{s} linker flags:\n", .{
136147
csf.file.src_path.sub_path,
137-
csf.flags,
138148
});
149+
for (csf.flags) |flag| {
150+
log(.debug, .WebUI, " {s}", .{flag});
151+
}
139152
},
140153
else => {},
141154
}
@@ -165,10 +178,17 @@ fn build_examples(b: *Build, webui: *Compile) !void {
165178
}
166179
const example_name = val.name;
167180

168-
const exe = b.addExecutable(.{
181+
const exe = b.addExecutable(if (builtin.zig_version.minor == 14) .{
169182
.name = example_name,
170183
.target = target,
171184
.optimize = optimize,
185+
} else .{
186+
.name = example_name,
187+
.root_module = b.createModule(.{
188+
.target = target,
189+
.optimize = optimize,
190+
.pic = true,
191+
}),
172192
});
173193
const path = try std.fmt.allocPrint(b.allocator, "examples/C/{s}/main.c", .{example_name});
174194
defer b.allocator.free(path);

0 commit comments

Comments
 (0)