Skip to content

Commit 6c3217e

Browse files
committed
more fix & added C sample test
1 parent 74ecc63 commit 6c3217e

File tree

3 files changed

+102
-61
lines changed

3 files changed

+102
-61
lines changed

bindings/zig/build.zig

Lines changed: 93 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
const std = @import("std");
44

5+
const pkgBuilder = struct {
6+
mode: std.builtin.OptimizeMode,
7+
target: std.zig.CrossTarget,
8+
build: *std.Build.CompileStep,
9+
};
10+
511
// Although this function looks imperative, note that its job is to
612
// declaratively construct a build graph that will be executed by an external
713
// runner.
@@ -17,13 +23,9 @@ pub fn build(b: *std.Build) void {
1723
// set a preferred release mode, allowing the user to decide how to optimize.
1824
const optimize = b.standardOptimizeOption(.{});
1925

20-
const pkg_dep = b.option(bool, "fetch", "Download libzmq with zig-pkg [default: false]") orelse false;
21-
22-
const libzmq_dep = b.dependency("libzmq", .{
23-
.optimize = optimize,
24-
.target = target,
25-
});
26-
const libzmq = libzmq_dep.artifact("zmq");
26+
// Options
27+
const pkg_dep = b.option(bool, "fetch", "Download libzmq with zig-pkg [default: true]") orelse true;
28+
const shared = b.option(bool, "shared", "Build shared library [default: true]") orelse true;
2729

2830
const config_header = if (!target.isWindows()) b.addConfigHeader(.{
2931
.style = .blank,
@@ -41,12 +43,11 @@ pub fn build(b: *std.Build) void {
4143
.include_path = "platform.h",
4244
}, .{});
4345

44-
const shared = b.option(bool, "shared", "Build shared Library [default: false]") orelse false;
45-
const lib = if (shared) b.addSharedLibrary(.{
46+
const libczmq = if (shared) b.addSharedLibrary(.{
4647
.name = "czmq_zig",
4748
// In this case the main source file is merely a path, however, in more
4849
// complicated build scripts, this could be a generated file.
49-
//.root_source_file = .{ .path = "src/main.zig" },
50+
//.root_source_file = .{ .path = "src/czmq.zig" },
5051
.version = .{
5152
.major = 4,
5253
.minor = 2,
@@ -60,54 +61,89 @@ pub fn build(b: *std.Build) void {
6061
.target = target,
6162
.optimize = optimize,
6263
});
63-
lib.addConfigHeader(config_header);
64-
lib.addIncludePath("../../include");
65-
lib.addIncludePath(config_header.include_path);
66-
lib.addCSourceFiles(lib_src, lib_flags);
64+
libczmq.addConfigHeader(config_header);
65+
libczmq.addIncludePath("../../include");
66+
libczmq.addIncludePath(config_header.include_path);
67+
libczmq.addCSourceFiles(lib_src, lib_flags);
6768
if (target.isWindows() and shared) {
68-
lib.linkSystemLibraryName("ws2_32");
69-
lib.linkSystemLibraryName("rpcrt4");
70-
lib.linkSystemLibraryName("iphlpapi");
69+
libczmq.linkSystemLibraryName("ws2_32");
70+
libczmq.linkSystemLibraryName("rpcrt4");
71+
libczmq.linkSystemLibraryName("iphlpapi");
7172
}
72-
if (pkg_dep)
73-
lib.linkLibrary(libzmq)
74-
else
75-
lib.linkSystemLibrary("zmq");
73+
if (pkg_dep) {
74+
const libzmq_dep = b.dependency("libzmq", .{
75+
.optimize = optimize,
76+
.target = target,
77+
});
78+
const libzmq = libzmq_dep.artifact("zmq");
79+
libczmq.linkLibrary(libzmq);
80+
libczmq.installLibraryHeaders(libzmq);
81+
} else libczmq.linkSystemLibrary("zmq");
7682
if (target.isLinux() and shared) {
77-
lib.linkSystemLibrary("dl");
78-
lib.linkSystemLibrary("rt");
83+
libczmq.linkSystemLibrary("dl");
84+
libczmq.linkSystemLibrary("rt");
7985
}
86+
87+
// TODO: No support MSVC libC++ (ucrt/msvcrt/vcruntime)
88+
// https://github.com/ziglang/zig/issues/4785 - drop replacement for MSVC
8089
if (target.getAbi() != .msvc) {
81-
lib.linkLibCpp();
82-
lib.linkLibC();
90+
libczmq.linkLibCpp();
91+
libczmq.linkLibC();
8392
}
8493
// This declares intent for the library to be installed into the standard
8594
// location when the user invokes the "install" step (the default step when
8695
// running `zig build`).
87-
lib.install();
88-
lib.installHeadersDirectory("../../include", "");
89-
lib.installLibraryHeaders(libzmq);
96+
libczmq.install();
97+
b.installDirectory(.{
98+
.source_dir = "../../include",
99+
.install_dir = .header,
100+
.install_subdir = "",
101+
.exclude_extensions = &.{"am"},
102+
});
103+
104+
build_test(b, .{ .target = target, .mode = optimize, .build = libczmq });
105+
buildSample(b, .{ .target = target, .mode = optimize, .build = libczmq }, "hello_czmq", "../../examples/security/hello.c");
106+
}
107+
108+
fn buildSample(b: *std.Build.Builder, lib: pkgBuilder, name: []const u8, file: []const u8) void {
109+
const test_exe = b.addExecutable(.{
110+
.name = name,
111+
.optimize = lib.mode,
112+
.target = lib.target,
113+
});
114+
test_exe.linkLibrary(lib.build);
115+
test_exe.addSystemIncludePath("../../include");
116+
test_exe.addCSourceFile(file, lib_flags);
117+
if (lib.target.isWindows())
118+
test_exe.linkSystemLibraryName("ws2_32");
119+
test_exe.linkLibC();
120+
test_exe.install();
121+
122+
const run_cmd = test_exe.run();
123+
run_cmd.step.dependOn(b.getInstallStep());
124+
if (b.args) |args| {
125+
run_cmd.addArgs(args);
126+
}
90127

128+
const run_step = b.step("run", b.fmt("Run the {s}", .{name}));
129+
run_step.dependOn(&run_cmd.step);
130+
}
131+
132+
fn build_test(b: *std.Build.Builder, pkg: pkgBuilder) void {
91133
// Creates a step for unit testing.
92134
const libtest = b.addTest(.{
93135
.root_source_file = .{ .path = "src/czmq.zig" },
94-
.target = target,
95-
.optimize = optimize,
136+
.target = pkg.target,
137+
.optimize = pkg.mode,
96138
});
97-
libtest.addConfigHeader(config_header);
98-
libtest.addIncludePath(config_header.include_path);
99-
libtest.addIncludePath("../../include");
100-
libtest.addCSourceFiles(lib_src, lib_flags);
101-
if (target.isWindows() and shared) {
139+
// libtest.addIncludePath("../../include");
140+
if (pkg.target.isWindows()) {
102141
libtest.linkSystemLibraryName("ws2_32");
103142
libtest.linkSystemLibraryName("rpcrt4");
104143
libtest.linkSystemLibraryName("iphlpapi");
105144
}
106-
if (pkg_dep)
107-
libtest.linkLibrary(libzmq)
108-
else
109-
libtest.linkSystemLibrary("zmq");
110-
if (target.getAbi() != .msvc) {
145+
libtest.linkLibrary(pkg.build);
146+
if (pkg.target.getAbi() != .msvc) {
111147
libtest.linkLibCpp();
112148
libtest.linkLibC();
113149
}
@@ -126,7 +162,10 @@ const lib_flags: []const []const u8 = &.{
126162
};
127163
const lib_src: []const []const u8 = &.{
128164
"../../src/zactor.c",
165+
"../../src/zargs.c",
129166
"../../src/zarmour.c",
167+
"../../src/zauth.c",
168+
"../../src/zbeacon.c",
130169
"../../src/zcert.c",
131170
"../../src/zcertstore.c",
132171
"../../src/zchunk.c",
@@ -137,33 +176,30 @@ const lib_src: []const []const u8 = &.{
137176
"../../src/zdir_patch.c",
138177
"../../src/zfile.c",
139178
"../../src/zframe.c",
179+
"../../src/zgossip.c",
180+
"../../src/zgossip_msg.c",
140181
"../../src/zhash.c",
141182
"../../src/zhashx.c",
183+
"../../src/zhttp_client.c",
184+
"../../src/zhttp_request.c",
185+
"../../src/zhttp_response.c",
186+
"../../src/zhttp_server.c",
187+
"../../src/zhttp_server_options.c",
142188
"../../src/ziflist.c",
143189
"../../src/zlist.c",
144190
"../../src/zlistx.c",
145191
"../../src/zloop.c",
192+
"../../src/zmonitor.c",
146193
"../../src/zmsg.c",
194+
"../../src/zosc.c",
147195
"../../src/zpoller.c",
196+
"../../src/zproc.c",
197+
"../../src/zproxy.c",
198+
"../../src/zrex.c",
148199
"../../src/zsock.c",
149200
"../../src/zstr.c",
150201
"../../src/zsys.c",
151-
"../../src/zuuid.c",
152-
"../../src/zauth.c",
153-
"../../src/zbeacon.c",
154-
"../../src/zgossip.c",
155-
"../../src/zmonitor.c",
156-
"../../src/zproxy.c",
157-
"../../src/zrex.c",
158-
"../../src/zgossip_msg.c",
159-
"../../src/ztrie.c",
160-
"../../src/zargs.c",
161-
"../../src/zproc.c",
162202
"../../src/ztimerset.c",
163-
"../../src/zhttp_server.c",
164-
"../../src/zhttp_client.c",
165-
"../../src/zhttp_request.c",
166-
"../../src/zhttp_response.c",
167-
"../../src/zhttp_server_options.c",
168-
"../../src/zosc.c",
203+
"../../src/ztrie.c",
204+
"../../src/zuuid.c",
169205
};

bindings/zig/build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
.dependencies = .{
66
.libzmq = .{
7-
.url = "https://github.com/zeromq/libzmq/archive/2ba59dac6f115d30e1648be21a8349531df87241.tar.gz",
8-
.hash = "12202e8734a0a6bf884eeba59d493fbf907bdf173115495e56e97a45ff5424d89ab1",
7+
.url = "https://github.com/zeromq/libzmq/archive/88c7d08321890b63e2dc7605150d7f3834aa30e2.tar.gz",
8+
.hash = "12206e4de251cf62141dbb593a590391071917444dc915bc8f3604f94721d1f0031e",
99
},
1010
},
1111
}

bindings/zig/src/czmq.zig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
const std = @import("std");
2-
const czmq = @cImport(@cInclude("czmq.h"));
2+
pub const czmq = @cImport(@cInclude("czmq.h"));
33
const testing = std.testing;
44
const debug = std.debug;
5+
const tls = std.crypto.tls;
6+
const x25519 = std.crypto.dh.X25519;
7+
const cstr = [*:0]const u8;
8+
const str = []const u8;
59

610
test "Reference all decls" {
711
_ = czmq;
812
testing.refAllDecls(@This());
913
}
1014

1115
test "Hello 0MQ" {
16+
const msg: cstr = "Hello, Curves!\n";
1217
var publisher: ?*czmq.zsock_t = czmq.zsock_new(czmq.ZMQ_PUB);
1318
czmq.zsock_set_curve_server(publisher, @boolToInt(true));
14-
debug.print("\nHello, Curves!{s}", .{"\n"});
19+
debug.print("\n{s}", .{msg});
1520
czmq.zsock_destroy(&publisher);
1621
}

0 commit comments

Comments
 (0)