From e9bccdb2ec7c7e630ac853dbeb501fff657dfe50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Fri, 17 Feb 2023 10:50:47 -0300 Subject: [PATCH 1/7] zig binding zig binding test (need zig version: 0.11) --- bindings/zig/build.zig | 55 +++++++++++++++++++++++++++++++++++++++ bindings/zig/src/main.zig | 15 +++++++++++ 2 files changed, 70 insertions(+) create mode 100644 bindings/zig/build.zig create mode 100644 bindings/zig/src/main.zig diff --git a/bindings/zig/build.zig b/bindings/zig/build.zig new file mode 100644 index 000000000..40687c66d --- /dev/null +++ b/bindings/zig/build.zig @@ -0,0 +1,55 @@ +//! Works only Zig version: 0.11.0 or higher + +const std = @import("std"); + +// 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(.{}); + + // 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 lib = b.addStaticLibrary(.{ + .name = "zig-czmq", + // 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/main.zig" }, + .target = target, + .optimize = optimize, + }); + lib.addIncludePath("../../include"); + lib.addLibraryPath("../../build"); + lib.linkSystemLibrary("czmq"); + lib.linkSystemLibrary("zmq"); + lib.linkLibC(); + // 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(); + + // Creates a step for unit testing. + const libtest = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + libtest.addIncludePath("../../include"); + libtest.addLibraryPath("../../build"); + libtest.linkSystemLibrary("czmq"); + libtest.linkSystemLibrary("zmq"); + libtest.linkLibC(); + + // 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(&libtest.step); +} diff --git a/bindings/zig/src/main.zig b/bindings/zig/src/main.zig new file mode 100644 index 000000000..da30bc759 --- /dev/null +++ b/bindings/zig/src/main.zig @@ -0,0 +1,15 @@ +const std = @import("std"); +const czmq = @cImport(@cInclude("czmq.h")); +const testing = std.testing; + +test "Reference all decls" { + _ = czmq; + testing.refAllDecls(@This()); +} + +test "Hello 0MQ" { + var publisher: ?*czmq.zsock_t = czmq.zsock_new(czmq.ZMQ_PUB); + czmq.zsock_set_curve_server(publisher, @boolToInt(true)); + std.debug.print("\nHello, Curves!{s}", .{"\n"}); + czmq.zsock_destroy(&publisher); +} From 503d42a874dd2eb2d7a31f00c89772470345b636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Fri, 17 Feb 2023 13:05:20 -0300 Subject: [PATCH 2/7] build czmq Replace cmake/autoconf to zig build. This build example need libzmq only --- bindings/zig/build.zig | 81 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/bindings/zig/build.zig b/bindings/zig/build.zig index 40687c66d..3f1f3ff4f 100644 --- a/bindings/zig/build.zig +++ b/bindings/zig/build.zig @@ -17,17 +17,40 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const lib = b.addStaticLibrary(.{ - .name = "zig-czmq", + const config_header = if (!target.isWindows()) b.addConfigHeader(.{ + .style = .blank, + .include_path = "platform.h", + }, .{ + .CZMQ_BUILD_DRAFT_API = {}, + .HAVE_LINUX_WIRELESS_H = {}, + .HAVE_NET_IF_H = {}, + .HAVE_NET_IF_MEDIA_H = null, + .HAVE_GETIFADDRS = {}, + .HAVE_FREEIFADDRS = {}, + .HAVE_DECL_AI_V4MAPPED = 0, + }) else b.addConfigHeader(.{ + .style = .blank, + .include_path = "platform.h", + }, .{}); + + const lib = b.addSharedLibrary(.{ + .name = "zig_czmq", // 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/main.zig" }, + .version = .{ .major = 4, .minor = 2, .patch = 2 }, .target = target, .optimize = optimize, }); + lib.addConfigHeader(config_header); lib.addIncludePath("../../include"); - lib.addLibraryPath("../../build"); - lib.linkSystemLibrary("czmq"); + lib.addIncludePath(config_header.include_path); + lib.addCSourceFiles(lib_src, lib_flags); + if (target.isWindows()) { + lib.linkSystemLibraryName("ws2_32"); + lib.linkSystemLibraryName("rpcrt4"); + lib.linkSystemLibraryName("iphlpapi"); + } lib.linkSystemLibrary("zmq"); lib.linkLibC(); // This declares intent for the library to be installed into the standard @@ -41,9 +64,15 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + libtest.addConfigHeader(config_header); + libtest.addIncludePath(config_header.include_path); libtest.addIncludePath("../../include"); - libtest.addLibraryPath("../../build"); - libtest.linkSystemLibrary("czmq"); + libtest.addCSourceFiles(lib_src, lib_flags); + if (target.isWindows()) { + libtest.linkSystemLibraryName("ws2_32"); + libtest.linkSystemLibraryName("rpcrt4"); + libtest.linkSystemLibraryName("iphlpapi"); + } libtest.linkSystemLibrary("zmq"); libtest.linkLibC(); @@ -53,3 +82,43 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run library tests"); test_step.dependOn(&libtest.step); } + +const lib_flags: []const []const u8 = &.{ + "-std=gnu99", + "-O3", + // "-Wall", + "-fno-sanitize=all", // disable undefined sanitizer (default on zig/clang wrapper) +}; +const lib_src: []const []const u8 = &.{ + "../../src/zactor.c", + "../../src/zarmour.c", + "../../src/zcert.c", + "../../src/zcertstore.c", + "../../src/zchunk.c", + "../../src/zclock.c", + "../../src/zconfig.c", + "../../src/zdigest.c", + "../../src/zdir.c", + "../../src/zdir_patch.c", + "../../src/zfile.c", + "../../src/zframe.c", + "../../src/zhash.c", + "../../src/zhashx.c", + "../../src/ziflist.c", + "../../src/zlist.c", + "../../src/zlistx.c", + "../../src/zloop.c", + "../../src/zmsg.c", + "../../src/zpoller.c", + "../../src/zsock.c", + "../../src/zstr.c", + "../../src/zsys.c", + "../../src/zuuid.c", + "../../src/zauth.c", + "../../src/zbeacon.c", + "../../src/zgossip.c", + "../../src/zmonitor.c", + "../../src/zproxy.c", + "../../src/zrex.c", + "../../src/zgossip_msg.c", +}; From d23621a55e13888786922f205f0de32d1aa1464d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Fri, 17 Feb 2023 13:27:23 -0300 Subject: [PATCH 3/7] make readme Currently the has only been tested on Linux (wsl2). The purpose to use version 0.11 because of initial support for zig pkg manager. TODO: add libzmq module to build all deps, on single build (ref.: https://github.com/ziglang/zig/issues/14307) --- bindings/zig/Readme.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 bindings/zig/Readme.md diff --git a/bindings/zig/Readme.md b/bindings/zig/Readme.md new file mode 100644 index 000000000..86a0cc84b --- /dev/null +++ b/bindings/zig/Readme.md @@ -0,0 +1,16 @@ +# CZMQ - Zig bindings (**Experimental**) + +## Require + +- Zig version: [0.11 or higher - download](https://ziglang.org/download) + +### How to build library + +```bash +$> zig build -Doptimize=ReleaseFast +# output: $PWD/zig-out/lib +``` +### Test +```bash +$> zig build test +``` From fec2f96dcf7cef8685f259d022e6f491130a0dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Mon, 20 Feb 2023 17:02:24 -0300 Subject: [PATCH 4/7] download & install libzmq_dep --- bindings/zig/.gitignore | 1 + bindings/zig/build.zig | 7 ++++++- bindings/zig/build.zig.zon | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 bindings/zig/.gitignore create mode 100644 bindings/zig/build.zig.zon diff --git a/bindings/zig/.gitignore b/bindings/zig/.gitignore new file mode 100644 index 000000000..4c80a226f --- /dev/null +++ b/bindings/zig/.gitignore @@ -0,0 +1 @@ +zig-* diff --git a/bindings/zig/build.zig b/bindings/zig/build.zig index 3f1f3ff4f..e859d7176 100644 --- a/bindings/zig/build.zig +++ b/bindings/zig/build.zig @@ -17,6 +17,10 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); + const libzmq = b.dependency("libzmq", .{ + .optimize = optimize, + .target = target, + }); const config_header = if (!target.isWindows()) b.addConfigHeader(.{ .style = .blank, .include_path = "platform.h", @@ -51,7 +55,8 @@ pub fn build(b: *std.Build) void { lib.linkSystemLibraryName("rpcrt4"); lib.linkSystemLibraryName("iphlpapi"); } - lib.linkSystemLibrary("zmq"); + lib.linkLibrary(libzmq.artifact("zmq")); + //lib.linkSystemLibrary("zmq"); lib.linkLibC(); // This declares intent for the library to be installed into the standard // location when the user invokes the "install" step (the default step when diff --git a/bindings/zig/build.zig.zon b/bindings/zig/build.zig.zon new file mode 100644 index 000000000..199ce77e0 --- /dev/null +++ b/bindings/zig/build.zig.zon @@ -0,0 +1,11 @@ +.{ + .name = "libzig_czmq", + .version = "4.2.2", + + .dependencies = .{ + .libzmq = .{ + .url = "https://github.com/kassane/libzmq/archive/89416003587ea21f36b8538791c4825fad012fba.tar.gz", + .hash = "122026d048d3a2925e86b3121a98aa8546a6109ff8ecffa75e7d9f2d1aea614f7250", + }, + }, +} From fe832627ef35aa2072b64adf5c9d3a9c3d7f64b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Tue, 21 Feb 2023 11:12:25 -0300 Subject: [PATCH 5/7] first successful library build --- bindings/zig/build.zig | 14 +++++++------- bindings/zig/build.zig.zon | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bindings/zig/build.zig b/bindings/zig/build.zig index e859d7176..332279bb9 100644 --- a/bindings/zig/build.zig +++ b/bindings/zig/build.zig @@ -17,10 +17,11 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const libzmq = b.dependency("libzmq", .{ + const libzmq_dep = b.dependency("libzmq", .{ .optimize = optimize, .target = target, }); + const libzmq = libzmq_dep.artifact("zmq"); const config_header = if (!target.isWindows()) b.addConfigHeader(.{ .style = .blank, .include_path = "platform.h", @@ -55,13 +56,14 @@ pub fn build(b: *std.Build) void { lib.linkSystemLibraryName("rpcrt4"); lib.linkSystemLibraryName("iphlpapi"); } - lib.linkLibrary(libzmq.artifact("zmq")); - //lib.linkSystemLibrary("zmq"); + lib.linkLibrary(libzmq); lib.linkLibC(); // 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(); + lib.installHeadersDirectory("../../include", ""); + lib.installLibraryHeaders(libzmq); // Creates a step for unit testing. const libtest = b.addTest(.{ @@ -78,9 +80,8 @@ pub fn build(b: *std.Build) void { libtest.linkSystemLibraryName("rpcrt4"); libtest.linkSystemLibraryName("iphlpapi"); } - libtest.linkSystemLibrary("zmq"); + libtest.linkLibrary(libzmq); libtest.linkLibC(); - // 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". @@ -91,8 +92,7 @@ pub fn build(b: *std.Build) void { const lib_flags: []const []const u8 = &.{ "-std=gnu99", "-O3", - // "-Wall", - "-fno-sanitize=all", // disable undefined sanitizer (default on zig/clang wrapper) + "-Wall", }; const lib_src: []const []const u8 = &.{ "../../src/zactor.c", diff --git a/bindings/zig/build.zig.zon b/bindings/zig/build.zig.zon index 199ce77e0..31ea71280 100644 --- a/bindings/zig/build.zig.zon +++ b/bindings/zig/build.zig.zon @@ -4,8 +4,8 @@ .dependencies = .{ .libzmq = .{ - .url = "https://github.com/kassane/libzmq/archive/89416003587ea21f36b8538791c4825fad012fba.tar.gz", - .hash = "122026d048d3a2925e86b3121a98aa8546a6109ff8ecffa75e7d9f2d1aea614f7250", + .url = "https://github.com/kassane/libzmq/archive/a6438dc78d00f3736ede988b0fdc2f6361c442b4.tar.gz", + .hash = "1220fc3abc4aa9fb75c61b00297a2ec1d4f70d7f65f4151f6f7a93f59a265a56bc9e", }, }, } From 74ecc636a26e927b6c3fdb260f69e2419140b872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Wed, 22 Feb 2023 17:54:46 -0300 Subject: [PATCH 6/7] Some changes... --- bindings/zig/build.zig | 62 ++++++++++++++++++++----- bindings/zig/build.zig.zon | 6 +-- bindings/zig/src/{main.zig => czmq.zig} | 3 +- 3 files changed, 56 insertions(+), 15 deletions(-) rename bindings/zig/src/{main.zig => czmq.zig} (83%) diff --git a/bindings/zig/build.zig b/bindings/zig/build.zig index 332279bb9..e7cce3336 100644 --- a/bindings/zig/build.zig +++ b/bindings/zig/build.zig @@ -17,11 +17,14 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); + const pkg_dep = b.option(bool, "fetch", "Download libzmq with zig-pkg [default: false]") orelse false; + const libzmq_dep = b.dependency("libzmq", .{ .optimize = optimize, .target = target, }); const libzmq = libzmq_dep.artifact("zmq"); + const config_header = if (!target.isWindows()) b.addConfigHeader(.{ .style = .blank, .include_path = "platform.h", @@ -38,12 +41,22 @@ pub fn build(b: *std.Build) void { .include_path = "platform.h", }, .{}); - const lib = b.addSharedLibrary(.{ - .name = "zig_czmq", + const shared = b.option(bool, "shared", "Build shared Library [default: false]") orelse false; + const lib = if (shared) b.addSharedLibrary(.{ + .name = "czmq_zig", // 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/main.zig" }, - .version = .{ .major = 4, .minor = 2, .patch = 2 }, + //.root_source_file = .{ .path = "src/main.zig" }, + .version = .{ + .major = 4, + .minor = 2, + .patch = 2, + }, + .target = target, + .optimize = optimize, + }) else b.addStaticLibrary(.{ + .name = "czmq_zig", + .root_source_file = .{ .path = "src/czmq.zig" }, .target = target, .optimize = optimize, }); @@ -51,13 +64,23 @@ pub fn build(b: *std.Build) void { lib.addIncludePath("../../include"); lib.addIncludePath(config_header.include_path); lib.addCSourceFiles(lib_src, lib_flags); - if (target.isWindows()) { + if (target.isWindows() and shared) { lib.linkSystemLibraryName("ws2_32"); lib.linkSystemLibraryName("rpcrt4"); lib.linkSystemLibraryName("iphlpapi"); } - lib.linkLibrary(libzmq); - lib.linkLibC(); + if (pkg_dep) + lib.linkLibrary(libzmq) + else + lib.linkSystemLibrary("zmq"); + if (target.isLinux() and shared) { + lib.linkSystemLibrary("dl"); + lib.linkSystemLibrary("rt"); + } + if (target.getAbi() != .msvc) { + lib.linkLibCpp(); + lib.linkLibC(); + } // 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`). @@ -67,7 +90,7 @@ pub fn build(b: *std.Build) void { // Creates a step for unit testing. const libtest = b.addTest(.{ - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = .{ .path = "src/czmq.zig" }, .target = target, .optimize = optimize, }); @@ -75,13 +98,19 @@ pub fn build(b: *std.Build) void { libtest.addIncludePath(config_header.include_path); libtest.addIncludePath("../../include"); libtest.addCSourceFiles(lib_src, lib_flags); - if (target.isWindows()) { + if (target.isWindows() and shared) { libtest.linkSystemLibraryName("ws2_32"); libtest.linkSystemLibraryName("rpcrt4"); libtest.linkSystemLibraryName("iphlpapi"); } - libtest.linkLibrary(libzmq); - libtest.linkLibC(); + if (pkg_dep) + libtest.linkLibrary(libzmq) + else + libtest.linkSystemLibrary("zmq"); + if (target.getAbi() != .msvc) { + libtest.linkLibCpp(); + libtest.linkLibC(); + } // 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". @@ -93,6 +122,7 @@ const lib_flags: []const []const u8 = &.{ "-std=gnu99", "-O3", "-Wall", + "-pedantic", }; const lib_src: []const []const u8 = &.{ "../../src/zactor.c", @@ -126,4 +156,14 @@ const lib_src: []const []const u8 = &.{ "../../src/zproxy.c", "../../src/zrex.c", "../../src/zgossip_msg.c", + "../../src/ztrie.c", + "../../src/zargs.c", + "../../src/zproc.c", + "../../src/ztimerset.c", + "../../src/zhttp_server.c", + "../../src/zhttp_client.c", + "../../src/zhttp_request.c", + "../../src/zhttp_response.c", + "../../src/zhttp_server_options.c", + "../../src/zosc.c", }; diff --git a/bindings/zig/build.zig.zon b/bindings/zig/build.zig.zon index 31ea71280..68aee6cfd 100644 --- a/bindings/zig/build.zig.zon +++ b/bindings/zig/build.zig.zon @@ -1,11 +1,11 @@ .{ - .name = "libzig_czmq", + .name = "libczmq_zig", .version = "4.2.2", .dependencies = .{ .libzmq = .{ - .url = "https://github.com/kassane/libzmq/archive/a6438dc78d00f3736ede988b0fdc2f6361c442b4.tar.gz", - .hash = "1220fc3abc4aa9fb75c61b00297a2ec1d4f70d7f65f4151f6f7a93f59a265a56bc9e", + .url = "https://github.com/zeromq/libzmq/archive/2ba59dac6f115d30e1648be21a8349531df87241.tar.gz", + .hash = "12202e8734a0a6bf884eeba59d493fbf907bdf173115495e56e97a45ff5424d89ab1", }, }, } diff --git a/bindings/zig/src/main.zig b/bindings/zig/src/czmq.zig similarity index 83% rename from bindings/zig/src/main.zig rename to bindings/zig/src/czmq.zig index da30bc759..89efa5ce7 100644 --- a/bindings/zig/src/main.zig +++ b/bindings/zig/src/czmq.zig @@ -1,6 +1,7 @@ const std = @import("std"); const czmq = @cImport(@cInclude("czmq.h")); const testing = std.testing; +const debug = std.debug; test "Reference all decls" { _ = czmq; @@ -10,6 +11,6 @@ test "Reference all decls" { test "Hello 0MQ" { var publisher: ?*czmq.zsock_t = czmq.zsock_new(czmq.ZMQ_PUB); czmq.zsock_set_curve_server(publisher, @boolToInt(true)); - std.debug.print("\nHello, Curves!{s}", .{"\n"}); + debug.print("\nHello, Curves!{s}", .{"\n"}); czmq.zsock_destroy(&publisher); } From 6c3217e2393fac6c8bbee8e15dc3438849dbf87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Thu, 23 Feb 2023 17:15:00 -0300 Subject: [PATCH 7/7] more fix & added C sample test --- bindings/zig/build.zig | 150 +++++++++++++++++++++++-------------- bindings/zig/build.zig.zon | 4 +- bindings/zig/src/czmq.zig | 9 ++- 3 files changed, 102 insertions(+), 61 deletions(-) diff --git a/bindings/zig/build.zig b/bindings/zig/build.zig index e7cce3336..833fd18e9 100644 --- a/bindings/zig/build.zig +++ b/bindings/zig/build.zig @@ -2,6 +2,12 @@ const std = @import("std"); +const pkgBuilder = struct { + mode: std.builtin.OptimizeMode, + target: std.zig.CrossTarget, + build: *std.Build.CompileStep, +}; + // Although this function looks imperative, note that its job is to // declaratively construct a build graph that will be executed by an external // runner. @@ -17,13 +23,9 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const pkg_dep = b.option(bool, "fetch", "Download libzmq with zig-pkg [default: false]") orelse false; - - const libzmq_dep = b.dependency("libzmq", .{ - .optimize = optimize, - .target = target, - }); - const libzmq = libzmq_dep.artifact("zmq"); + // Options + const pkg_dep = b.option(bool, "fetch", "Download libzmq with zig-pkg [default: true]") orelse true; + const shared = b.option(bool, "shared", "Build shared library [default: true]") orelse true; const config_header = if (!target.isWindows()) b.addConfigHeader(.{ .style = .blank, @@ -41,12 +43,11 @@ pub fn build(b: *std.Build) void { .include_path = "platform.h", }, .{}); - const shared = b.option(bool, "shared", "Build shared Library [default: false]") orelse false; - const lib = if (shared) b.addSharedLibrary(.{ + const libczmq = if (shared) b.addSharedLibrary(.{ .name = "czmq_zig", // 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/main.zig" }, + //.root_source_file = .{ .path = "src/czmq.zig" }, .version = .{ .major = 4, .minor = 2, @@ -60,54 +61,89 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); - lib.addConfigHeader(config_header); - lib.addIncludePath("../../include"); - lib.addIncludePath(config_header.include_path); - lib.addCSourceFiles(lib_src, lib_flags); + libczmq.addConfigHeader(config_header); + libczmq.addIncludePath("../../include"); + libczmq.addIncludePath(config_header.include_path); + libczmq.addCSourceFiles(lib_src, lib_flags); if (target.isWindows() and shared) { - lib.linkSystemLibraryName("ws2_32"); - lib.linkSystemLibraryName("rpcrt4"); - lib.linkSystemLibraryName("iphlpapi"); + libczmq.linkSystemLibraryName("ws2_32"); + libczmq.linkSystemLibraryName("rpcrt4"); + libczmq.linkSystemLibraryName("iphlpapi"); } - if (pkg_dep) - lib.linkLibrary(libzmq) - else - lib.linkSystemLibrary("zmq"); + if (pkg_dep) { + const libzmq_dep = b.dependency("libzmq", .{ + .optimize = optimize, + .target = target, + }); + const libzmq = libzmq_dep.artifact("zmq"); + libczmq.linkLibrary(libzmq); + libczmq.installLibraryHeaders(libzmq); + } else libczmq.linkSystemLibrary("zmq"); if (target.isLinux() and shared) { - lib.linkSystemLibrary("dl"); - lib.linkSystemLibrary("rt"); + libczmq.linkSystemLibrary("dl"); + libczmq.linkSystemLibrary("rt"); } + + // TODO: No support MSVC libC++ (ucrt/msvcrt/vcruntime) + // https://github.com/ziglang/zig/issues/4785 - drop replacement for MSVC if (target.getAbi() != .msvc) { - lib.linkLibCpp(); - lib.linkLibC(); + libczmq.linkLibCpp(); + libczmq.linkLibC(); } // 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(); - lib.installHeadersDirectory("../../include", ""); - lib.installLibraryHeaders(libzmq); + libczmq.install(); + b.installDirectory(.{ + .source_dir = "../../include", + .install_dir = .header, + .install_subdir = "", + .exclude_extensions = &.{"am"}, + }); + + build_test(b, .{ .target = target, .mode = optimize, .build = libczmq }); + buildSample(b, .{ .target = target, .mode = optimize, .build = libczmq }, "hello_czmq", "../../examples/security/hello.c"); +} + +fn buildSample(b: *std.Build.Builder, lib: pkgBuilder, name: []const u8, file: []const u8) void { + const test_exe = b.addExecutable(.{ + .name = name, + .optimize = lib.mode, + .target = lib.target, + }); + test_exe.linkLibrary(lib.build); + test_exe.addSystemIncludePath("../../include"); + test_exe.addCSourceFile(file, lib_flags); + if (lib.target.isWindows()) + test_exe.linkSystemLibraryName("ws2_32"); + test_exe.linkLibC(); + test_exe.install(); + + const run_cmd = test_exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + const run_step = b.step("run", b.fmt("Run the {s}", .{name})); + run_step.dependOn(&run_cmd.step); +} + +fn build_test(b: *std.Build.Builder, pkg: pkgBuilder) void { // Creates a step for unit testing. const libtest = b.addTest(.{ .root_source_file = .{ .path = "src/czmq.zig" }, - .target = target, - .optimize = optimize, + .target = pkg.target, + .optimize = pkg.mode, }); - libtest.addConfigHeader(config_header); - libtest.addIncludePath(config_header.include_path); - libtest.addIncludePath("../../include"); - libtest.addCSourceFiles(lib_src, lib_flags); - if (target.isWindows() and shared) { + // libtest.addIncludePath("../../include"); + if (pkg.target.isWindows()) { libtest.linkSystemLibraryName("ws2_32"); libtest.linkSystemLibraryName("rpcrt4"); libtest.linkSystemLibraryName("iphlpapi"); } - if (pkg_dep) - libtest.linkLibrary(libzmq) - else - libtest.linkSystemLibrary("zmq"); - if (target.getAbi() != .msvc) { + libtest.linkLibrary(pkg.build); + if (pkg.target.getAbi() != .msvc) { libtest.linkLibCpp(); libtest.linkLibC(); } @@ -126,7 +162,10 @@ const lib_flags: []const []const u8 = &.{ }; const lib_src: []const []const u8 = &.{ "../../src/zactor.c", + "../../src/zargs.c", "../../src/zarmour.c", + "../../src/zauth.c", + "../../src/zbeacon.c", "../../src/zcert.c", "../../src/zcertstore.c", "../../src/zchunk.c", @@ -137,33 +176,30 @@ const lib_src: []const []const u8 = &.{ "../../src/zdir_patch.c", "../../src/zfile.c", "../../src/zframe.c", + "../../src/zgossip.c", + "../../src/zgossip_msg.c", "../../src/zhash.c", "../../src/zhashx.c", + "../../src/zhttp_client.c", + "../../src/zhttp_request.c", + "../../src/zhttp_response.c", + "../../src/zhttp_server.c", + "../../src/zhttp_server_options.c", "../../src/ziflist.c", "../../src/zlist.c", "../../src/zlistx.c", "../../src/zloop.c", + "../../src/zmonitor.c", "../../src/zmsg.c", + "../../src/zosc.c", "../../src/zpoller.c", + "../../src/zproc.c", + "../../src/zproxy.c", + "../../src/zrex.c", "../../src/zsock.c", "../../src/zstr.c", "../../src/zsys.c", - "../../src/zuuid.c", - "../../src/zauth.c", - "../../src/zbeacon.c", - "../../src/zgossip.c", - "../../src/zmonitor.c", - "../../src/zproxy.c", - "../../src/zrex.c", - "../../src/zgossip_msg.c", - "../../src/ztrie.c", - "../../src/zargs.c", - "../../src/zproc.c", "../../src/ztimerset.c", - "../../src/zhttp_server.c", - "../../src/zhttp_client.c", - "../../src/zhttp_request.c", - "../../src/zhttp_response.c", - "../../src/zhttp_server_options.c", - "../../src/zosc.c", + "../../src/ztrie.c", + "../../src/zuuid.c", }; diff --git a/bindings/zig/build.zig.zon b/bindings/zig/build.zig.zon index 68aee6cfd..b64d133b1 100644 --- a/bindings/zig/build.zig.zon +++ b/bindings/zig/build.zig.zon @@ -4,8 +4,8 @@ .dependencies = .{ .libzmq = .{ - .url = "https://github.com/zeromq/libzmq/archive/2ba59dac6f115d30e1648be21a8349531df87241.tar.gz", - .hash = "12202e8734a0a6bf884eeba59d493fbf907bdf173115495e56e97a45ff5424d89ab1", + .url = "https://github.com/zeromq/libzmq/archive/88c7d08321890b63e2dc7605150d7f3834aa30e2.tar.gz", + .hash = "12206e4de251cf62141dbb593a590391071917444dc915bc8f3604f94721d1f0031e", }, }, } diff --git a/bindings/zig/src/czmq.zig b/bindings/zig/src/czmq.zig index 89efa5ce7..b7c5b6d14 100644 --- a/bindings/zig/src/czmq.zig +++ b/bindings/zig/src/czmq.zig @@ -1,7 +1,11 @@ const std = @import("std"); -const czmq = @cImport(@cInclude("czmq.h")); +pub const czmq = @cImport(@cInclude("czmq.h")); const testing = std.testing; const debug = std.debug; +const tls = std.crypto.tls; +const x25519 = std.crypto.dh.X25519; +const cstr = [*:0]const u8; +const str = []const u8; test "Reference all decls" { _ = czmq; @@ -9,8 +13,9 @@ test "Reference all decls" { } test "Hello 0MQ" { + const msg: cstr = "Hello, Curves!\n"; var publisher: ?*czmq.zsock_t = czmq.zsock_new(czmq.ZMQ_PUB); czmq.zsock_set_curve_server(publisher, @boolToInt(true)); - debug.print("\nHello, Curves!{s}", .{"\n"}); + debug.print("\n{s}", .{msg}); czmq.zsock_destroy(&publisher); }