Skip to content

Commit 01d0251

Browse files
committed
update zig nightly api changes
1 parent 70def02 commit 01d0251

File tree

1 file changed

+184
-22
lines changed

1 file changed

+184
-22
lines changed

build.zig

Lines changed: 184 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,14 @@ comptime {
2424
}
2525

2626
pub fn build(b: *std.Build) void {
27-
const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic;
28-
const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS;
29-
const target = b.standardTargetOptions(.{});
30-
const optimize = b.standardOptimizeOption(.{});
31-
32-
if (enableTLS) {
33-
std.log.info("enable TLS support", .{});
34-
if (!target.isNative()) {
35-
std.log.info("when enable tls, not support cross compile", .{});
36-
std.os.exit(1);
37-
}
27+
if (current_zig.minor == 11) {
28+
build_11(b);
29+
} else if (current_zig.minor == 12) {
30+
build_12(b);
3831
}
39-
40-
const webui = build_webui(b, optimize, target, isStatic, enableTLS);
41-
// webui.linkLibrary(build_civetweb(b, optimize, target, isStatic, enableTLS));
42-
43-
webui.installHeader("include/webui.h", "webui.h");
44-
45-
build_examples(b, optimize, target, webui);
46-
47-
b.installArtifact(webui);
4832
}
4933

50-
fn build_examples(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_lib: *Compile) void {
34+
fn build_examples_12(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, webui_lib: *Compile) void {
5135
var lazy_path = Build.LazyPath{
5236
.path = "examples/C",
5337
};
@@ -139,7 +123,139 @@ fn build_examples(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_
139123
}
140124
}
141125

142-
fn build_webui(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static: bool, enable_tls: bool) *Compile {
126+
fn build_examples_11(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_lib: *Compile) void {
127+
var lazy_path = Build.LazyPath{
128+
.path = "examples/C",
129+
};
130+
131+
const build_all_step = b.step("build_all", "build all examples");
132+
133+
const examples_path = lazy_path.getPath(b);
134+
135+
var iter_dir =
136+
std.fs.openIterableDirAbsolute(examples_path, .{}) catch |err| {
137+
log.err("open examples_path failed, err is {}", .{err});
138+
std.os.exit(1);
139+
};
140+
defer iter_dir.close();
141+
142+
var itera = iter_dir.iterate();
143+
144+
while (itera.next()) |val| {
145+
if (val) |entry| {
146+
if (entry.kind == .directory) {
147+
const example_name = entry.name;
148+
const path = std.fmt.allocPrint(b.allocator, "examples/C/{s}/main.c", .{example_name}) catch |err| {
149+
log.err("fmt path for examples failed, err is {}", .{err});
150+
std.os.exit(1);
151+
};
152+
153+
const exe = b.addExecutable(.{
154+
.name = example_name,
155+
.target = target,
156+
.optimize = optimize,
157+
});
158+
159+
exe.addCSourceFile(.{
160+
.file = .{
161+
.path = path,
162+
},
163+
.flags = &.{},
164+
});
165+
166+
exe.linkLibrary(webui_lib);
167+
168+
const exe_install = b.addInstallArtifact(exe, .{});
169+
170+
build_all_step.dependOn(&exe_install.step);
171+
172+
const exe_run = b.addRunArtifact(exe);
173+
exe_run.step.dependOn(&exe_install.step);
174+
175+
if (comptime (current_zig.minor > 11)) {
176+
const cwd = std.fmt.allocPrint(b.allocator, "examples/C/{s}", .{example_name}) catch |err| {
177+
log.err("fmt path for examples failed, err is {}", .{err});
178+
std.os.exit(1);
179+
};
180+
exe_run.setCwd(.{
181+
.path = cwd,
182+
});
183+
} else {
184+
const cwd = std.fmt.allocPrint(b.allocator, "{s}/{s}", .{ examples_path, example_name }) catch |err| {
185+
log.err("fmt path for examples failed, err is {}", .{err});
186+
std.os.exit(1);
187+
};
188+
exe_run.cwd = cwd;
189+
}
190+
191+
const step_name = std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}) catch |err| {
192+
log.err("fmt step_name for examples failed, err is {}", .{err});
193+
std.os.exit(1);
194+
};
195+
196+
const step_desc = std.fmt.allocPrint(b.allocator, "run example {s}", .{example_name}) catch |err| {
197+
log.err("fmt step_desc for examples failed, err is {}", .{err});
198+
std.os.exit(1);
199+
};
200+
201+
const exe_run_step = b.step(step_name, step_desc);
202+
exe_run_step.dependOn(&exe_run.step);
203+
}
204+
} else {
205+
break;
206+
}
207+
} else |err| {
208+
log.err("iterate examples_path failed, err is {}", .{err});
209+
std.os.exit(1);
210+
}
211+
}
212+
213+
fn build_webui_12(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, is_static: bool, enable_tls: bool) *Compile {
214+
const name = "webui";
215+
const webui = if (is_static) b.addStaticLibrary(.{ .name = name, .target = target, .optimize = optimize }) else b.addSharedLibrary(.{ .name = name, .target = target, .optimize = optimize });
216+
217+
const extra_flags = if (target.query.os_tag == .windows or (target.query.os_tag == null and builtin.os.tag == .windows))
218+
"-DMUST_IMPLEMENT_CLOCK_GETTIME"
219+
else
220+
"";
221+
222+
const cflags = if (enable_tls)
223+
[_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1", extra_flags }
224+
else
225+
[_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-DNO_SSL", extra_flags, "", "" };
226+
227+
webui.addCSourceFile(.{
228+
.file = .{ .path = "src/webui.c" },
229+
.flags = if (enable_tls)
230+
&[_][]const u8{ "-DNO_SSL", "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1" }
231+
else
232+
&[_][]const u8{"-DNO_SSL"},
233+
});
234+
235+
webui.addCSourceFile(.{
236+
.file = .{ .path = "src/civetweb/civetweb.c" },
237+
.flags = &cflags,
238+
});
239+
240+
webui.linkLibC();
241+
242+
webui.addIncludePath(.{ .path = "include" });
243+
244+
if (target.query.os_tag == .windows or (target.query.os_tag == null and builtin.os.tag == .windows)) {
245+
webui.linkSystemLibrary("ws2_32");
246+
if (enable_tls) {
247+
webui.linkSystemLibrary("bcrypt");
248+
}
249+
}
250+
if (enable_tls) {
251+
webui.linkSystemLibrary("ssl");
252+
webui.linkSystemLibrary("crypto");
253+
}
254+
255+
return webui;
256+
}
257+
258+
fn build_webui_11(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static: bool, enable_tls: bool) *Compile {
143259
const name = "webui";
144260
const webui = if (is_static) b.addStaticLibrary(.{ .name = name, .target = target, .optimize = optimize }) else b.addSharedLibrary(.{ .name = name, .target = target, .optimize = optimize });
145261

@@ -183,3 +299,49 @@ fn build_webui(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static
183299

184300
return webui;
185301
}
302+
303+
fn build_11(b: *Build) void {
304+
const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic;
305+
const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS;
306+
const target = b.standardTargetOptions(.{});
307+
const optimize = b.standardOptimizeOption(.{});
308+
309+
if (enableTLS) {
310+
std.log.info("enable TLS support", .{});
311+
if (!target.isNative()) {
312+
std.log.info("when enable tls, not support cross compile", .{});
313+
std.os.exit(1);
314+
}
315+
}
316+
317+
const webui = build_webui_11(b, optimize, target, isStatic, enableTLS);
318+
319+
webui.installHeader("include/webui.h", "webui.h");
320+
321+
build_examples_11(b, optimize, target, webui);
322+
323+
b.installArtifact(webui);
324+
}
325+
326+
fn build_12(b: *Build) void {
327+
const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic;
328+
const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS;
329+
const target = b.standardTargetOptions(.{});
330+
const optimize = b.standardOptimizeOption(.{});
331+
332+
if (enableTLS) {
333+
std.log.info("enable TLS support", .{});
334+
if (!target.query.isNative()) {
335+
std.log.info("when enable tls, not support cross compile", .{});
336+
std.os.exit(1);
337+
}
338+
}
339+
340+
const webui = build_webui_12(b, optimize, target, isStatic, enableTLS);
341+
342+
webui.installHeader("include/webui.h", "webui.h");
343+
344+
build_examples_12(b, optimize, target, webui);
345+
346+
b.installArtifact(webui);
347+
}

0 commit comments

Comments
 (0)