Skip to content

Commit 5a710c2

Browse files
authored
scm: Support dependency debug building (#526)
1 parent 387bebe commit 5a710c2

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

build.zig

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ const lib_name = "webui";
1010
const zig_ver = builtin.zig_version.minor;
1111
var global_log_level: std.log.Level = .warn;
1212

13+
/// Vendored dependencies of webui.
14+
pub const Dependency = enum {
15+
civetweb,
16+
// TODO: Check and add all vendored dependencies, e.g. "webview"
17+
};
18+
19+
const DebugDependencies = std.EnumSet(Dependency);
20+
1321
pub fn build(b: *Build) !void {
1422
const target = b.standardTargetOptions(.{});
1523
const optimize = b.standardOptimizeOption(.{});
@@ -23,6 +31,9 @@ pub fn build(b: *Build) !void {
2331
const enable_tls = b.option(bool, "enable-tls", "enable TLS support") orelse false;
2432
const verbose = b.option(std.log.Level, "verbose", "set verbose output") orelse .warn;
2533
global_log_level = verbose;
34+
// TODO: Support list of dependencies once support is limited to >0.13.0
35+
const debug = b.option(Dependency, "debug", "enable dependency debug output");
36+
const debug_dependencies = DebugDependencies.initMany(if (debug) |d| &.{d} else &.{});
2637

2738
if (enable_tls and !target.query.isNative()) {
2839
log(.err, .WebUI, "cross compilation is not supported with TLS enabled", .{});
@@ -47,22 +58,28 @@ pub fn build(b: *Build) !void {
4758
.target = target,
4859
.optimize = optimize,
4960
});
50-
try addLinkerFlags(b, webui, enable_tls);
61+
try addLinkerFlags(b, webui, enable_tls, debug_dependencies);
5162

5263
b.installArtifact(webui);
5364

5465
try build_examples(b, webui);
5566
}
5667

57-
fn addLinkerFlags(b: *Build, webui: *Compile, enable_tls: bool) !void {
68+
fn addLinkerFlags(
69+
b: *Build,
70+
webui: *Compile,
71+
enable_tls: bool,
72+
debug_dependencies: DebugDependencies,
73+
) !void {
5874
const webui_target = webui.rootModuleTarget();
5975
const is_windows = webui_target.os.tag == .windows;
6076

77+
const debug = webui.root_module.optimize.? == .Debug;
78+
6179
// Prepare compiler flags.
6280
const no_tls_flags: []const []const u8 = &.{"-DNO_SSL"};
6381
const tls_flags: []const []const u8 = &.{ "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1" };
6482
const civetweb_flags: []const []const u8 = &.{
65-
"-DNDEBUG",
6683
"-DNO_CACHING",
6784
"-DNO_CGI",
6885
"-DUSE_WEBSOCKET",
@@ -73,9 +90,18 @@ fn addLinkerFlags(b: *Build, webui: *Compile, enable_tls: bool) !void {
7390
.file = b.path("src/webui.c"),
7491
.flags = if (enable_tls) tls_flags else no_tls_flags,
7592
});
93+
94+
const civetweb_debug = debug and debug_dependencies.contains(.civetweb);
7695
webui.addCSourceFile(.{
7796
.file = b.path("src/civetweb/civetweb.c"),
78-
.flags = if (enable_tls) civetweb_flags ++ tls_flags else civetweb_flags ++ .{"-DUSE_WEBSOCKET"} ++ no_tls_flags,
97+
.flags = if (enable_tls and !civetweb_debug)
98+
civetweb_flags ++ tls_flags ++ .{"-DNDEBUG"}
99+
else if (enable_tls and civetweb_debug)
100+
civetweb_flags ++ tls_flags
101+
else if (!enable_tls and !civetweb_debug)
102+
civetweb_flags ++ .{"-DUSE_WEBSOCKET"} ++ no_tls_flags ++ .{"-DNDEBUG"}
103+
else
104+
civetweb_flags ++ .{"-DUSE_WEBSOCKET"} ++ no_tls_flags,
79105
});
80106
webui.linkLibC();
81107
webui.addIncludePath(b.path("include"));
@@ -140,7 +166,11 @@ fn build_examples(b: *Build, webui: *Compile) !void {
140166
}
141167
const example_name = val.name;
142168

143-
const exe = b.addExecutable(.{ .name = example_name, .target = target, .optimize = optimize });
169+
const exe = b.addExecutable(.{
170+
.name = example_name,
171+
.target = target,
172+
.optimize = optimize,
173+
});
144174
const path = try std.fmt.allocPrint(b.allocator, "examples/C/{s}/main.c", .{example_name});
145175
defer b.allocator.free(path);
146176

0 commit comments

Comments
 (0)