From 5cea7e2934d38ac206bd1ec91b2c70f8d46b85ce Mon Sep 17 00:00:00 2001 From: mochalins <117967760+mochalins@users.noreply.github.com> Date: Sat, 12 Oct 2024 10:47:04 +0900 Subject: [PATCH 1/3] scm: Limit paths in `build.zig.zon` `build.zig.zon`'s paths are meant to specify what is needed when `webui` is fetched as a dependency from another Zig build system. When being used as a dependency, files such as tests/examples need not be included. --- build.zig.zon | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/build.zig.zon b/build.zig.zon index 0c2690bc1..170b03402 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,5 +1,13 @@ .{ .name = "webui", .version = "2.5.0-beta.2", - .paths = .{""}, + .paths = .{ + "src", + "include", + "bridge", + "build.zig", + "build.zig.zon", + "LICENSE", + "README.md", + }, } From 3973962a8a3fe1c1d2b6d746e6e3457968348281 Mon Sep 17 00:00:00 2001 From: mochalins <117967760+mochalins@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:27:50 +0900 Subject: [PATCH 2/3] scm: Clean up `build.zig`, verbose logging levels --- build.zig | 92 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 27 deletions(-) diff --git a/build.zig b/build.zig index db43ffe19..290bdfea4 100644 --- a/build.zig +++ b/build.zig @@ -6,9 +6,9 @@ const OptimizeMode = std.builtin.OptimizeMode; const Compile = Build.Step.Compile; const Module = Build.Module; -const log = std.log.scoped(.WebUI); const lib_name = "webui"; const zig_ver = builtin.zig_version.minor; +var global_log_level: std.log.Level = .warn; pub fn build(b: *Build) !void { const target = b.standardTargetOptions(.{}); @@ -16,30 +16,32 @@ pub fn build(b: *Build) !void { const is_dynamic = b.option(bool, "dynamic", "build the dynamic library") orelse false; const enable_tls = b.option(bool, "enable-tls", "enable TLS support") orelse false; - const verbose = b.option(bool, "verbose", "enable verbose output") orelse false; + const verbose = b.option(std.log.Level, "verbose", "set verbose output") orelse .warn; - switch (zig_ver) { + global_log_level = verbose; + + switch (comptime zig_ver) { 11 => { if (enable_tls and !target.isNative()) { - log.err("cross compilation is not supported with TLS enabled", .{}); - std.os.exit(1); + log(.err, .WebUI, "cross compilation is not supported with TLS enabled", .{}); + return error.InvalidBuildConfiguration; } }, 12, 13, 14 => { if (enable_tls and !target.query.isNative()) { - log.err("cross compilation is not supported with TLS enabled", .{}); - std.process.exit(1); + log(.err, .WebUI, "cross compilation is not supported with TLS enabled", .{}); + return error.InvalidBuildConfiguration; } }, - else => @compileError("unsupported Zig version!"), + else => return error.UnsupportedZigVersion, } - if (verbose) { - std.debug.print("Building {s} WebUI library{s}...\n", .{ - if (is_dynamic) "dynamic" else "static", - if (enable_tls) " with TLS support" else "", - }); - defer std.debug.print("Done.\n", .{}); + log(.info, .WebUI, "Building {s} WebUI library{s}...", .{ + if (is_dynamic) "dynamic" else "static", + if (enable_tls) " with TLS support" else "", + }); + defer { + log(.info, .WebUI, "Done.", .{}); } const webui = if (is_dynamic) b.addSharedLibrary(.{ @@ -55,10 +57,7 @@ pub fn build(b: *Build) !void { b.installArtifact(webui); - build_examples(b, webui) catch |err| { - log.err("failed to build examples: {}", .{err}); - if (zig_ver < 12) std.os.exit(1) else std.process.exit(1); - }; + try build_examples(b, webui); } fn addLinkerFlags(b: *Build, webui: *Compile, enable_tls: bool) !void { @@ -66,18 +65,23 @@ fn addLinkerFlags(b: *Build, webui: *Compile, enable_tls: bool) !void { const is_windows = if (zig_ver < 12) webui_target.isWindows() else webui_target.os.tag == .windows; // Prepare compiler flags. - const tls_flags = &[_][]const u8{ "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1" }; - var civetweb_flags = std.ArrayList([]const u8).init(b.allocator); - try civetweb_flags.appendSlice(&[_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-Wno-error=date-time" }); - try civetweb_flags.appendSlice(if (enable_tls) tls_flags else &[_][]const u8{ "-DUSE_WEBSOCKET", "-DNO_SSL" }); + const no_tls_flags: []const []const u8 = &.{"-DNO_SSL"}; + const tls_flags: []const []const u8 = &.{ "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1" }; + const civetweb_flags: []const []const u8 = &.{ + "-DNDEBUG", + "-DNO_CACHING", + "-DNO_CGI", + "-DUSE_WEBSOCKET", + "-Wno-error=date-time", + }; webui.addCSourceFile(.{ .file = if (zig_ver < 12) .{ .path = "src/webui.c" } else b.path("src/webui.c"), - .flags = if (enable_tls) tls_flags else &[_][]const u8{"-DNO_SSL"}, + .flags = if (enable_tls) tls_flags else no_tls_flags, }); webui.addCSourceFile(.{ .file = if (zig_ver < 12) .{ .path = "src/civetweb/civetweb.c" } else b.path("src/civetweb/civetweb.c"), - .flags = civetweb_flags.items, + .flags = if (enable_tls) civetweb_flags ++ tls_flags else civetweb_flags ++ .{"-DUSE_WEBSOCKET"} ++ no_tls_flags, }); webui.linkLibC(); webui.addIncludePath(if (zig_ver < 12) .{ .path = "include" } else b.path("include")); @@ -109,6 +113,18 @@ fn addLinkerFlags(b: *Build, webui: *Compile, enable_tls: bool) !void { webui.linkSystemLibrary("ssl"); webui.linkSystemLibrary("crypto"); } + + for (if (zig_ver < 12) webui.link_objects.items else webui.root_module.link_objects.items) |lo| { + switch (lo) { + .c_source_file => |csf| { + log(.debug, .WebUI, "{s} linker flags: {s}", .{ + if (zig_ver < 12) csf.file.path else csf.file.src_path.sub_path, + csf.flags, + }); + }, + else => {}, + } + } } fn build_examples(b: *Build, webui: *Compile) !void { @@ -118,9 +134,9 @@ fn build_examples(b: *Build, webui: *Compile) !void { const examples_path = (if (zig_ver < 12) (Build.LazyPath{ .path = "examples/C" }) else b.path("examples/C")).getPath(b); var examples_dir = if (zig_ver < 12) - try std.fs.openIterableDirAbsolute(examples_path, .{}) + try std.fs.cwd().openIterableDir(examples_path, .{}) else - try std.fs.openDirAbsolute(examples_path, .{ .iterate = true }); + try std.fs.cwd().openDir(examples_path, .{ .iterate = true }); defer examples_dir.close(); var paths = examples_dir.iterate(); @@ -128,10 +144,11 @@ fn build_examples(b: *Build, webui: *Compile) !void { if (val.kind != .directory) { continue; } - const example_name = val.name; + const exe = b.addExecutable(.{ .name = example_name, .target = target, .optimize = optimize }); const path = try std.fmt.allocPrint(b.allocator, "examples/C/{s}/main.c", .{example_name}); + defer b.allocator.free(path); exe.addCSourceFile(.{ .file = if (zig_ver < 12) .{ .path = path } else b.path(path), .flags = &.{} }); exe.linkLibrary(webui); @@ -139,9 +156,12 @@ fn build_examples(b: *Build, webui: *Compile) !void { const exe_install = b.addInstallArtifact(exe, .{}); const exe_run = b.addRunArtifact(exe); const step_name = try std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}); + defer b.allocator.free(step_name); const step_desc = try std.fmt.allocPrint(b.allocator, "run example {s}", .{example_name}); + defer b.allocator.free(step_desc); const cwd = try std.fmt.allocPrint(b.allocator, "src/examples/{s}", .{example_name}); + defer b.allocator.free(cwd); if (zig_ver < 12) exe_run.cwd = cwd else exe_run.setCwd(b.path(cwd)); exe_run.step.dependOn(&exe_install.step); @@ -149,3 +169,21 @@ fn build_examples(b: *Build, webui: *Compile) !void { b.step(step_name, step_desc).dependOn(&exe_run.step); } } + +/// Function to runtime-scope log levels based on build flag, for all scopes. +fn log( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + const should_print: bool = @intFromEnum(global_log_level) >= @intFromEnum(level); + if (should_print) { + switch (comptime level) { + .err => std.log.scoped(scope).err(format, args), + .warn => std.log.scoped(scope).warn(format, args), + .info => std.log.scoped(scope).info(format, args), + .debug => std.log.scoped(scope).debug(format, args), + } + } +} From 692599ed830843f0ff869d51742431afe293db92 Mon Sep 17 00:00:00 2001 From: mochalins <117967760+mochalins@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:34:37 +0900 Subject: [PATCH 3/3] scm: Fix Zig CI to use `verbose` enum --- .github/workflows/zig.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/zig.yml b/.github/workflows/zig.yml index 368c704f0..ff850bc8b 100644 --- a/.github/workflows/zig.yml +++ b/.github/workflows/zig.yml @@ -22,15 +22,15 @@ jobs: with: version: ${{ matrix.version }} - name: Build static library - run: zig build -Dverbose + run: zig build -Dverbose=debug - name: Build dynamic library if: ${{ !cancelled() }} - run: zig build -Ddynamic -Dverbose + run: zig build -Ddynamic -Dverbose=debug - name: Build static library with TLS support if: runner.os == 'Linux' - run: zig build -Denable-tls -Dverbose + run: zig build -Denable-tls -Dverbose=debug - name: Build dynamic library with TLS support if: runner.os == 'Linux' - run: zig build -Ddynamic -Denable-tls -Dverbose + run: zig build -Ddynamic -Denable-tls -Dverbose=debug - name: Build examples run: zig build examples