From 1479d4e24307ae2f0f01cc1d1e7f2f5c59b1c6d6 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 5 Feb 2023 09:34:36 +0100 Subject: [PATCH] Zig implementation: update to current zig API. --- zig-impl/build.zig | 87 +++++++++++++++--------------- zig-impl/src/bindings.zig | 2 +- zig-impl/src/context.zig | 2 +- zig-impl/src/exchange-fd.zig | 2 +- zig-impl/src/{ipc.zig => main.zig} | 0 zig-impl/src/switch.zig | 2 +- 6 files changed, 48 insertions(+), 47 deletions(-) rename zig-impl/src/{ipc.zig => main.zig} (100%) diff --git a/zig-impl/build.zig b/zig-impl/build.zig index 96b5b7d..6ea2f40 100644 --- a/zig-impl/build.zig +++ b/zig-impl/build.zig @@ -1,59 +1,60 @@ const std = @import("std"); -const bin_dir = "bin"; -const build_dir = "build"; +const VERSION = "0.1.5"; -pub fn build(b: *std.build.Builder) void { - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - // const mode = b.standardReleaseOptions(); - // const mode = std.builtin.Mode.Debug; - const mode = std.builtin.Mode.ReleaseSmall; +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); - const ipcd_exe = b.addExecutable("ipcd", "apps/ipcd.zig"); - ipcd_exe.addPackagePath("ipc", "./src/ipc.zig"); - ipcd_exe.setOutputDir(bin_dir); - ipcd_exe.linkLibC(); - ipcd_exe.setBuildMode(mode); - ipcd_exe.install(); + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); - const tcpd_exe = b.addExecutable("tcpd", "apps/tcpd.zig"); - tcpd_exe.addPackagePath("ipc", "./src/ipc.zig"); - tcpd_exe.setOutputDir(bin_dir); - tcpd_exe.linkLibC(); - tcpd_exe.setBuildMode(mode); - tcpd_exe.install(); + const lib = b.addStaticLibrary(.{ + .name = "ipc", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = .{ .path = "src/bindings.zig" }, + .target = target, + .optimize = optimize, + }); - const pong_exe = b.addExecutable("pong", "apps/pong.zig"); - pong_exe.addPackagePath("ipc", "./src/ipc.zig"); - pong_exe.setOutputDir(bin_dir); - pong_exe.linkLibC(); - pong_exe.setBuildMode(mode); - pong_exe.install(); - - - const pongd_exe = b.addExecutable("pongd", "apps/pongd.zig"); - pongd_exe.addPackagePath("ipc", "./src/ipc.zig"); - pongd_exe.setOutputDir(bin_dir); - pongd_exe.linkLibC(); - pongd_exe.setBuildMode(mode); - pongd_exe.install(); - - const lib = b.addStaticLibrary("ipc", "src/bindings.zig"); - lib.setOutputDir(build_dir); + // Link with the libc of the target system since the C allocator + // is required in the bindings. lib.linkLibC(); - lib.setBuildMode(mode); + + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). lib.install(); - const solib = b.addSharedLibrary("ipc", "src/bindings.zig", b.version(0, 1, 0)); - solib.setOutputDir(build_dir); + const solib = b.addSharedLibrary(.{ + .name = "ipc", + .root_source_file = .{ .path = "src/bindings.zig" }, + .version = comptime (try std.builtin.Version.parse(VERSION)), + .target = target, + .optimize = optimize, + }); solib.linkLibC(); - solib.setBuildMode(mode); solib.install(); - const main_tests = b.addTest("src/main.zig"); - main_tests.setBuildMode(mode); + // Creates a step for unit testing. + const main_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build test` + // This will evaluate the `test` step rather than the default, which is "install". const test_step = b.step("test", "Run library tests"); test_step.dependOn(&main_tests.step); } diff --git a/zig-impl/src/bindings.zig b/zig-impl/src/bindings.zig index a1105e4..75610dd 100644 --- a/zig-impl/src/bindings.zig +++ b/zig-impl/src/bindings.zig @@ -1,6 +1,6 @@ const std = @import("std"); const log = std.log.scoped(.libipc_bindings); -const ipc = @import("./ipc.zig"); +const ipc = @import("./main.zig"); const Context = ipc.Context; const Message = ipc.Message; const CBEventType = ipc.CBEvent.Type; diff --git a/zig-impl/src/context.zig b/zig-impl/src/context.zig index 28d9325..fe4afba 100644 --- a/zig-impl/src/context.zig +++ b/zig-impl/src/context.zig @@ -20,7 +20,7 @@ const print_eq = @import("./util.zig").print_eq; const Messages = @import("./message.zig").Messages; const SwitchDB = @import("./switch.zig").SwitchDB; const Connections = @import("./connection.zig").Connections; -const CBEventType = @import("./ipc.zig").CBEvent.Type; +const CBEventType = @import("./main.zig").CBEvent.Type; pub const PollFD = std.ArrayList(std.os.pollfd); diff --git a/zig-impl/src/exchange-fd.zig b/zig-impl/src/exchange-fd.zig index f8db48e..df7a8a6 100644 --- a/zig-impl/src/exchange-fd.zig +++ b/zig-impl/src/exchange-fd.zig @@ -120,7 +120,7 @@ pub fn recvmsg( ) SendMsgError!usize { while (true) { var m = msg; - const rc = system.recvmsg(sockfd, @ptrCast(*std.x.os.Socket.Message, &m), @intCast(c_int, flags)); + const rc = system.recvmsg(sockfd, @ptrCast(*std.os.msghdr, &m), @intCast(c_uint, flags)); if (builtin.os.tag == .windows) { if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { diff --git a/zig-impl/src/ipc.zig b/zig-impl/src/main.zig similarity index 100% rename from zig-impl/src/ipc.zig rename to zig-impl/src/main.zig diff --git a/zig-impl/src/switch.zig b/zig-impl/src/switch.zig index 0e391d5..9f41dbd 100644 --- a/zig-impl/src/switch.zig +++ b/zig-impl/src/switch.zig @@ -4,7 +4,7 @@ const fmt = std.fmt; const net = std.net; -const ipc = @import("./ipc.zig"); +const ipc = @import("./main.zig"); const Message = ipc.Message; const CBEventType = ipc.CBEvent.Type;