From 89416003587ea21f36b8538791c4825fad012fba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Mon, 20 Feb 2023 12:20:11 -0300 Subject: [PATCH 01/10] Zig build --- .gitignore | 3 + build.zig | 306 ++++++++++++++++++++++++++++++++++++++++++++++++++ build.zig.zon | 6 + 3 files changed, 315 insertions(+) create mode 100644 build.zig create mode 100644 build.zig.zon diff --git a/.gitignore b/.gitignore index 22223e5745..b5184560f3 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,9 @@ stamp-h1 ## CMake cmake-build-debug/ build/ +## Zig Build +zig-out/ +zig-cache/ ## Android builds/android/prefix ## IntelliJ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000000..d5e284094e --- /dev/null +++ b/build.zig @@ -0,0 +1,306 @@ +const std = @import("std"); +const Builder = std.Build.Builder; + +pub fn build(b: *Builder) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + // Generating "platform.hpp" + const config_header = if (!target.isWindows()) b.addConfigHeader(.{ + .style = .blank, + .include_path = "platform.hpp", + }, .{ + .ZMQ_USE_EPOLL = 1, + .ZMQ_HAVE_CURVE = 1, + .ZMQ_USE_TWEETNACL = 1, + .ZMQ_HAVE_EVENTFD = 1, + .ZMQ_HAVE_IFADDRS = 1, + .ZMQ_HAVE_SOCK_CLOEXEC = 1, + .ZMQ_HAVE_SO_KEEPALIVED = 1, + .ZMQ_HAVE_SO_PEERCRED = 1, + .ZMQ_HAVE_TCP_KEEPCNT = 1, + .ZMQ_HAVE_TCP_KEEPIDLE = 1, + .ZMQ_HAVE_TCP_KEEPINTVL = 1, + .ZMQ_HAVE_UIO = 1, + .ZMQ_USE_BUILTIN_SHA1 = 1, + .HAVE_FORK = 1, + .HAVE_POSIX_MEMALIGN = 1, + .HAVE_ACCEPT4 = 1, + .HAVE_STRNLEN = 1, + .ZMQ_IOTHREAD_POLLER_USE_EPOLL = 1, + .ZMQ_USE_CV_IMPL_STL11 = 1, + .ZMQ_POLL_BASED_ON_POLL = 1, + .ZMQ_CACHELINE_SIZE = 64, + }) else b.addConfigHeader(.{ + .style = .blank, + .include_path = "platform.hpp", + }, .{ + .ZMQ_HAVE_CURVE = 1, + .ZMQ_USE_TWEETNACL = 1, + .ZMQ_USE_SELECT = 1, + }); + + var sources_cpp = std.ArrayList([]const u8).init(b.allocator); + sources_cpp.appendSlice(sources) catch @panic("slice error"); + if (target.getOsTag() == .windows) sources_cpp.appendSlice(msvc_sources) catch @panic("slice error"); + + const libzmq = b.addSharedLibrary(.{ + .name = "zmq", + .target = target, + .version = .{ + .major = 4, + .minor = 3, + .patch = 5, + }, + .optimize = optimize, + }); + if (optimize == .Debug or optimize == .ReleaseSafe) + libzmq.bundle_compiler_rt = true + else + libzmq.want_lto = true; + libzmq.addConfigHeader(config_header); + libzmq.addIncludePath("include"); + libzmq.addIncludePath("src"); + libzmq.addIncludePath(config_header.include_path); + for (sources_cpp.items) |src| { + libzmq.addCSourceFile(src, cxxflags); + } + libzmq.linkLibCpp(); // LLVM libc++ (builtin) + libzmq.linkLibC(); // OS libc + libzmq.install(); +} + +const cxxflags: []const []const u8 = &.{ + "-Oz", + "-Wall", + "-pedantic", +}; +const sources: []const []const u8 = &.{ + "src/kqueue.cpp", + "src/lb.cpp", + "src/mailbox.cpp", + "src/mailbox_safe.cpp", + "src/mechanism.cpp", + "src/mechanism_base.cpp", + "src/metadata.cpp", + "src/msg.cpp", + "src/mtrie.cpp", + "src/norm_engine.cpp", + "src/object.cpp", + "src/options.cpp", + "src/own.cpp", + "src/null_mechanism.cpp", + "src/pair.cpp", + "src/peer.cpp", + "src/pgm_receiver.cpp", + "src/pgm_sender.cpp", + "src/pgm_socket.cpp", + "src/pipe.cpp", + "src/plain_client.cpp", + "src/plain_server.cpp", + "src/poll.cpp", + "src/poller_base.cpp", + "src/polling_util.cpp", + "src/pollset.cpp", + "src/proxy.cpp", + "src/pub.cpp", + "src/pull.cpp", + "src/push.cpp", + "src/random.cpp", + "src/raw_encoder.cpp", + "src/raw_decoder.cpp", + "src/raw_engine.cpp", + "src/reaper.cpp", + "src/rep.cpp", + "src/req.cpp", + "src/router.cpp", + "src/select.cpp", + "src/server.cpp", + "src/session_base.cpp", + "src/signaler.cpp", + "src/socket_base.cpp", + "src/socks.cpp", + "src/socks_connecter.cpp", + "src/stream.cpp", + "src/stream_engine_base.cpp", + "src/sub.cpp", + "src/tcp.cpp", + "src/tcp_address.cpp", + "src/tcp_connecter.cpp", + "src/tcp_listener.cpp", + "src/thread.cpp", + "src/trie.cpp", + "src/radix_tree.cpp", + "src/v1_decoder.cpp", + "src/v1_encoder.cpp", + "src/v2_decoder.cpp", + "src/v2_encoder.cpp", + "src/v3_1_encoder.cpp", + "src/xpub.cpp", + "src/xsub.cpp", + "src/zmq.cpp", + "src/zmq_utils.cpp", + "src/decoder_allocators.cpp", + "src/socket_poller.cpp", + "src/timers.cpp", + // "src/config.hpp", + "src/radio.cpp", + "src/dish.cpp", + "src/udp_engine.cpp", + "src/udp_address.cpp", + "src/scatter.cpp", + "src/gather.cpp", + "src/ip_resolver.cpp", + "src/zap_client.cpp", + "src/zmtp_engine.cpp", + "src/tweetnacl.c", + + //"external/wepoll/wepoll.c", + "external/sha1/sha1.c", +}; +const msvc_sources: []const []const u8 = &.{ + // at least for VS, the header files must also be listed + "src/address.hpp", + "src/array.hpp", + "src/atomic_counter.hpp", + "src/atomic_ptr.hpp", + "src/blob.hpp", + "src/channel.hpp", + "src/client.hpp", + "src/clock.hpp", + "src/command.hpp", + "src/compat.hpp", + "src/condition_variable.hpp", + "src/config.hpp", + "src/ctx.hpp", + "src/curve_client.hpp", + "src/curve_client_tools.hpp", + "src/curve_mechanism_base.hpp", + "src/curve_server.hpp", + "src/dbuffer.hpp", + "src/dealer.hpp", + "src/decoder.hpp", + "src/decoder_allocators.hpp", + "src/devpoll.hpp", + "src/dgram.hpp", + "src/dish.hpp", + "src/dist.hpp", + "src/encoder.hpp", + "src/endpoint.hpp", + "src/epoll.hpp", + "src/err.hpp", + "src/fd.hpp", + "src/fq.hpp", + "src/gather.hpp", + "src/generic_mtrie.hpp", + "src/generic_mtrie_impl.hpp", + "src/gssapi_client.hpp", + "src/gssapi_mechanism_base.hpp", + "src/gssapi_server.hpp", + "src/i_decoder.hpp", + "src/i_encoder.hpp", + "src/i_engine.hpp", + "src/i_mailbox.hpp", + "src/i_poll_events.hpp", + "src/io_object.hpp", + "src/io_thread.hpp", + "src/ip.hpp", + "src/ipc_address.hpp", + "src/ipc_connecter.hpp", + "src/ipc_listener.hpp", + "src/kqueue.hpp", + "src/lb.hpp", + "src/likely.hpp", + "src/macros.hpp", + "src/mailbox.hpp", + "src/mailbox_safe.hpp", + "src/mechanism.hpp", + "src/mechanism_base.hpp", + "src/metadata.hpp", + "src/msg.hpp", + "src/mtrie.hpp", + "src/mutex.hpp", + "src/norm_engine.hpp", + "src/null_mechanism.hpp", + "src/object.hpp", + "src/options.hpp", + "src/own.hpp", + "src/pair.hpp", + "src/peer.hpp", + "src/pgm_receiver.hpp", + "src/pgm_sender.hpp", + "src/pgm_socket.hpp", + "src/pipe.hpp", + "src/plain_client.hpp", + "src/plain_common.hpp", + "src/plain_server.hpp", + "src/poll.hpp", + "src/poller.hpp", + "src/poller_base.hpp", + "src/polling_util.hpp", + "src/pollset.hpp", + "src/precompiled.hpp", + "src/proxy.hpp", + "src/pub.hpp", + "src/pull.hpp", + "src/push.hpp", + "src/radio.hpp", + "src/random.hpp", + "src/raw_decoder.hpp", + "src/raw_encoder.hpp", + "src/raw_engine.hpp", + "src/reaper.hpp", + "src/rep.hpp", + "src/req.hpp", + "src/router.hpp", + "src/scatter.hpp", + "src/secure_allocator.hpp", + "src/select.hpp", + "src/server.hpp", + "src/session_base.hpp", + "src/signaler.hpp", + "src/socket_base.hpp", + "src/socket_poller.hpp", + "src/socks.hpp", + "src/socks_connecter.hpp", + "src/stdint.hpp", + "src/stream.hpp", + "src/stream_engine_base.hpp", + "src/stream_connecter_base.hpp", + "src/stream_connecter_base.cpp", + "src/stream_listener_base.hpp", + "src/stream_listener_base.cpp", + "src/sub.hpp", + "src/tcp.hpp", + "src/tcp_address.hpp", + "src/tcp_connecter.hpp", + "src/tcp_listener.hpp", + "src/thread.hpp", + "src/timers.hpp", + "src/tipc_address.hpp", + "src/tipc_connecter.hpp", + "src/tipc_listener.hpp", + "src/trie.hpp", + "src/udp_address.hpp", + "src/udp_engine.hpp", + "src/v1_decoder.hpp", + "src/v1_encoder.hpp", + "src/v2_decoder.hpp", + "src/v2_encoder.hpp", + "src/v3_1_encoder.hpp", + "src/v2_protocol.hpp", + "src/vmci.hpp", + "src/vmci_address.hpp", + "src/vmci_connecter.hpp", + "src/vmci_listener.hpp", + "src/windows.hpp", + "src/wire.hpp", + "src/xpub.hpp", + "src/xsub.hpp", + "src/ypipe.hpp", + "src/ypipe_base.hpp", + "src/ypipe_conflate.hpp", + "src/yqueue.hpp", + "src/zap_client.hpp", + "src/zmtp_engine.hpp", +}; diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000000..170467cd1c --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,6 @@ +.{ + .name = "libzmq", + .version = "4.3.5", + + .dependencies = .{}, +} From 818c6a29d6f5a3c0bd517d62b53f2c6019cb3953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Tue, 21 Feb 2023 10:23:03 -0300 Subject: [PATCH 02/10] fix install include_dir and build static or shared library --- build.zig | 170 ++++++------------------------------------------------ 1 file changed, 19 insertions(+), 151 deletions(-) diff --git a/build.zig b/build.zig index d5e284094e..aabe72294a 100644 --- a/build.zig +++ b/build.zig @@ -1,3 +1,5 @@ +//! Requires zig version: 0.11 or higher +/// build: zig build -Doptimize=ReleaseFast -Dshared (or -Dshared=true/false) const std = @import("std"); const Builder = std.Build.Builder; @@ -5,6 +7,9 @@ pub fn build(b: *Builder) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + // Options - static library [default] + const shared = b.option(bool, "shared", "Build the Shared Library [default: false]") orelse false; + // Generating "platform.hpp" const config_header = if (!target.isWindows()) b.addConfigHeader(.{ .style = .blank, @@ -38,13 +43,23 @@ pub fn build(b: *Builder) void { .ZMQ_HAVE_CURVE = 1, .ZMQ_USE_TWEETNACL = 1, .ZMQ_USE_SELECT = 1, + .ZMQ_USE_CV_IMPL_STL11 = 1, + .ZMQ_CACHELINE_SIZE = 1, + .ZMQ_IOTHREAD_POLLER_USE_SELECT = 1, + .ZMQ_POLL_BASED_ON_SELECT = 1, + .ZMQ_USE_BUILTIN_SHA1 = 1, + .HAVE_STRNLEN = 1, }); var sources_cpp = std.ArrayList([]const u8).init(b.allocator); sources_cpp.appendSlice(sources) catch @panic("slice error"); - if (target.getOsTag() == .windows) sources_cpp.appendSlice(msvc_sources) catch @panic("slice error"); + //if (target.getOsTag() == .windows) sources_cpp.appendSlice(msvc_sources) catch @panic("slice error"); - const libzmq = b.addSharedLibrary(.{ + const libzmq = if (!shared) b.addStaticLibrary(.{ + .name = "zmq", + .target = target, + .optimize = optimize, + }) else b.addSharedLibrary(.{ .name = "zmq", .target = target, .version = .{ @@ -56,8 +71,7 @@ pub fn build(b: *Builder) void { }); if (optimize == .Debug or optimize == .ReleaseSafe) libzmq.bundle_compiler_rt = true - else - libzmq.want_lto = true; + else if (shared) libzmq.want_lto = true; libzmq.addConfigHeader(config_header); libzmq.addIncludePath("include"); libzmq.addIncludePath("src"); @@ -68,6 +82,7 @@ pub fn build(b: *Builder) void { libzmq.linkLibCpp(); // LLVM libc++ (builtin) libzmq.linkLibC(); // OS libc libzmq.install(); + libzmq.installHeadersDirectory("include", "zmq"); } const cxxflags: []const []const u8 = &.{ @@ -143,7 +158,6 @@ const sources: []const []const u8 = &.{ "src/decoder_allocators.cpp", "src/socket_poller.cpp", "src/timers.cpp", - // "src/config.hpp", "src/radio.cpp", "src/dish.cpp", "src/udp_engine.cpp", @@ -158,149 +172,3 @@ const sources: []const []const u8 = &.{ //"external/wepoll/wepoll.c", "external/sha1/sha1.c", }; -const msvc_sources: []const []const u8 = &.{ - // at least for VS, the header files must also be listed - "src/address.hpp", - "src/array.hpp", - "src/atomic_counter.hpp", - "src/atomic_ptr.hpp", - "src/blob.hpp", - "src/channel.hpp", - "src/client.hpp", - "src/clock.hpp", - "src/command.hpp", - "src/compat.hpp", - "src/condition_variable.hpp", - "src/config.hpp", - "src/ctx.hpp", - "src/curve_client.hpp", - "src/curve_client_tools.hpp", - "src/curve_mechanism_base.hpp", - "src/curve_server.hpp", - "src/dbuffer.hpp", - "src/dealer.hpp", - "src/decoder.hpp", - "src/decoder_allocators.hpp", - "src/devpoll.hpp", - "src/dgram.hpp", - "src/dish.hpp", - "src/dist.hpp", - "src/encoder.hpp", - "src/endpoint.hpp", - "src/epoll.hpp", - "src/err.hpp", - "src/fd.hpp", - "src/fq.hpp", - "src/gather.hpp", - "src/generic_mtrie.hpp", - "src/generic_mtrie_impl.hpp", - "src/gssapi_client.hpp", - "src/gssapi_mechanism_base.hpp", - "src/gssapi_server.hpp", - "src/i_decoder.hpp", - "src/i_encoder.hpp", - "src/i_engine.hpp", - "src/i_mailbox.hpp", - "src/i_poll_events.hpp", - "src/io_object.hpp", - "src/io_thread.hpp", - "src/ip.hpp", - "src/ipc_address.hpp", - "src/ipc_connecter.hpp", - "src/ipc_listener.hpp", - "src/kqueue.hpp", - "src/lb.hpp", - "src/likely.hpp", - "src/macros.hpp", - "src/mailbox.hpp", - "src/mailbox_safe.hpp", - "src/mechanism.hpp", - "src/mechanism_base.hpp", - "src/metadata.hpp", - "src/msg.hpp", - "src/mtrie.hpp", - "src/mutex.hpp", - "src/norm_engine.hpp", - "src/null_mechanism.hpp", - "src/object.hpp", - "src/options.hpp", - "src/own.hpp", - "src/pair.hpp", - "src/peer.hpp", - "src/pgm_receiver.hpp", - "src/pgm_sender.hpp", - "src/pgm_socket.hpp", - "src/pipe.hpp", - "src/plain_client.hpp", - "src/plain_common.hpp", - "src/plain_server.hpp", - "src/poll.hpp", - "src/poller.hpp", - "src/poller_base.hpp", - "src/polling_util.hpp", - "src/pollset.hpp", - "src/precompiled.hpp", - "src/proxy.hpp", - "src/pub.hpp", - "src/pull.hpp", - "src/push.hpp", - "src/radio.hpp", - "src/random.hpp", - "src/raw_decoder.hpp", - "src/raw_encoder.hpp", - "src/raw_engine.hpp", - "src/reaper.hpp", - "src/rep.hpp", - "src/req.hpp", - "src/router.hpp", - "src/scatter.hpp", - "src/secure_allocator.hpp", - "src/select.hpp", - "src/server.hpp", - "src/session_base.hpp", - "src/signaler.hpp", - "src/socket_base.hpp", - "src/socket_poller.hpp", - "src/socks.hpp", - "src/socks_connecter.hpp", - "src/stdint.hpp", - "src/stream.hpp", - "src/stream_engine_base.hpp", - "src/stream_connecter_base.hpp", - "src/stream_connecter_base.cpp", - "src/stream_listener_base.hpp", - "src/stream_listener_base.cpp", - "src/sub.hpp", - "src/tcp.hpp", - "src/tcp_address.hpp", - "src/tcp_connecter.hpp", - "src/tcp_listener.hpp", - "src/thread.hpp", - "src/timers.hpp", - "src/tipc_address.hpp", - "src/tipc_connecter.hpp", - "src/tipc_listener.hpp", - "src/trie.hpp", - "src/udp_address.hpp", - "src/udp_engine.hpp", - "src/v1_decoder.hpp", - "src/v1_encoder.hpp", - "src/v2_decoder.hpp", - "src/v2_encoder.hpp", - "src/v3_1_encoder.hpp", - "src/v2_protocol.hpp", - "src/vmci.hpp", - "src/vmci_address.hpp", - "src/vmci_connecter.hpp", - "src/vmci_listener.hpp", - "src/windows.hpp", - "src/wire.hpp", - "src/xpub.hpp", - "src/xsub.hpp", - "src/ypipe.hpp", - "src/ypipe_base.hpp", - "src/ypipe_conflate.hpp", - "src/yqueue.hpp", - "src/zap_client.hpp", - "src/zmtp_engine.hpp", -}; From a6438dc78d00f3736ede988b0fdc2f6361c442b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Tue, 21 Feb 2023 10:49:19 -0300 Subject: [PATCH 03/10] fix install_includeHeader - no make include/zmq folder --- build.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig b/build.zig index aabe72294a..1dbb0aa5ea 100644 --- a/build.zig +++ b/build.zig @@ -82,7 +82,7 @@ pub fn build(b: *Builder) void { libzmq.linkLibCpp(); // LLVM libc++ (builtin) libzmq.linkLibC(); // OS libc libzmq.install(); - libzmq.installHeadersDirectory("include", "zmq"); + libzmq.installHeadersDirectory("include", ""); } const cxxflags: []const []const u8 = &.{ From 41ee05379c1b3538e41e595370d71ff6c7defc3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Tue, 21 Feb 2023 15:24:24 -0300 Subject: [PATCH 04/10] more platform.hpp config - enabling windows & macos build --- build.zig | 133 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 84 insertions(+), 49 deletions(-) diff --git a/build.zig b/build.zig index 1dbb0aa5ea..724fede215 100644 --- a/build.zig +++ b/build.zig @@ -11,49 +11,76 @@ pub fn build(b: *Builder) void { const shared = b.option(bool, "shared", "Build the Shared Library [default: false]") orelse false; // Generating "platform.hpp" - const config_header = if (!target.isWindows()) b.addConfigHeader(.{ - .style = .blank, - .include_path = "platform.hpp", - }, .{ - .ZMQ_USE_EPOLL = 1, - .ZMQ_HAVE_CURVE = 1, - .ZMQ_USE_TWEETNACL = 1, - .ZMQ_HAVE_EVENTFD = 1, - .ZMQ_HAVE_IFADDRS = 1, - .ZMQ_HAVE_SOCK_CLOEXEC = 1, - .ZMQ_HAVE_SO_KEEPALIVED = 1, - .ZMQ_HAVE_SO_PEERCRED = 1, - .ZMQ_HAVE_TCP_KEEPCNT = 1, - .ZMQ_HAVE_TCP_KEEPIDLE = 1, - .ZMQ_HAVE_TCP_KEEPINTVL = 1, - .ZMQ_HAVE_UIO = 1, - .ZMQ_USE_BUILTIN_SHA1 = 1, - .HAVE_FORK = 1, - .HAVE_POSIX_MEMALIGN = 1, - .HAVE_ACCEPT4 = 1, - .HAVE_STRNLEN = 1, - .ZMQ_IOTHREAD_POLLER_USE_EPOLL = 1, - .ZMQ_USE_CV_IMPL_STL11 = 1, - .ZMQ_POLL_BASED_ON_POLL = 1, - .ZMQ_CACHELINE_SIZE = 64, - }) else b.addConfigHeader(.{ - .style = .blank, - .include_path = "platform.hpp", - }, .{ - .ZMQ_HAVE_CURVE = 1, - .ZMQ_USE_TWEETNACL = 1, - .ZMQ_USE_SELECT = 1, - .ZMQ_USE_CV_IMPL_STL11 = 1, - .ZMQ_CACHELINE_SIZE = 1, - .ZMQ_IOTHREAD_POLLER_USE_SELECT = 1, - .ZMQ_POLL_BASED_ON_SELECT = 1, - .ZMQ_USE_BUILTIN_SHA1 = 1, - .HAVE_STRNLEN = 1, - }); - - var sources_cpp = std.ArrayList([]const u8).init(b.allocator); - sources_cpp.appendSlice(sources) catch @panic("slice error"); - //if (target.getOsTag() == .windows) sources_cpp.appendSlice(msvc_sources) catch @panic("slice error"); + const config_header = switch (target.getOsTag()) { + .linux => b.addConfigHeader(.{ + .style = .blank, //.{ .cmake = .{ .path = "builds/cmake/platform.hpp.in" } }, + .include_path = "platform.hpp", + }, .{ + .ZMQ_HAVE_LINUX = {}, + .ZMQ_USE_EPOLL = 1, + .ZMQ_HAVE_CURVE = 1, + .ZMQ_USE_TWEETNACL = 1, + .ZMQ_HAVE_EVENTFD = 1, + .ZMQ_HAVE_IFADDRS = 1, + .ZMQ_HAVE_SOCK_CLOEXEC = 1, + .ZMQ_HAVE_SO_BINDTODEVICE = 1, + .ZMQ_HAVE_SO_KEEPALIVED = 1, + .ZMQ_HAVE_SO_PEERCRED = 1, + .ZMQ_HAVE_TCP_KEEPCNT = 1, + .ZMQ_HAVE_TCP_KEEPIDLE = 1, + .ZMQ_HAVE_TCP_KEEPINTVL = 1, + .ZMQ_HAVE_UIO = 1, + .ZMQ_HAVE_STRLCPY = 1, + .ZMQ_USE_BUILTIN_SHA1 = 1, + .HAVE_FORK = 1, + .HAVE_POSIX_MEMALIGN = 1, + .HAVE_ACCEPT4 = 1, + .HAVE_STRNLEN = 1, + .ZMQ_IOTHREAD_POLLER_USE_EPOLL = 1, + .ZMQ_USE_CV_IMPL_STL11 = 1, + .ZMQ_POLL_BASED_ON_POLL = 1, + .ZMQ_CACHELINE_SIZE = 64, + }), + .windows => b.addConfigHeader(.{ + .style = .blank, + .include_path = "platform.hpp", + }, .{ + .ZMQ_HAVE_WINDOWS = {}, + .ZMQ_HAVE_MINGW32 = {}, + .ZMQ_HAVE_CURVE = 1, + .ZMQ_USE_TWEETNACL = 1, + .ZMQ_USE_SELECT = 1, + .ZMQ_USE_CV_IMPL_STL11 = 1, + .ZMQ_CACHELINE_SIZE = 64, + .ZMQ_IOTHREAD_POLLER_USE_SELECT = 1, + .ZMQ_POLL_BASED_ON_SELECT = 1, + .ZMQ_USE_BUILTIN_SHA1 = 1, + .HAVE_STRNLEN = 1, + }), + .macos => b.addConfigHeader(.{ + .style = .blank, + .include_path = "platform.hpp", + }, .{ + .ZMQ_HAVE_OSX = {}, + .ZMQ_USE_KQUEUE = 1, + .ZMQ_POSIX_MEMALIGN = 1, + .ZMQ_CACHELINE_SIZE = 64, + .ZMQ_HAVE_CURVE = 1, + .ZMQ_USE_TWEETNACL = 1, + .ZMQ_HAVE_UIO = 1, + .ZMQ_HAVE_IFADDRS = 1, + .ZMQ_HAVE_OS_KEEPALIVE = 1, + .ZMQ_HAVE_TCP_KEEPALIVE = 1, + .ZMQ_HAVE_TCP_KEEPCNT = 1, + .ZMQ_HAVE_TCP_KEEPINTVL = 1, + .ZMQ_USE_BUILTIN_SHA1 = 1, + .ZMQ_IOTHREAD_POLLER_USE_KQEUE = 1, + .ZMQ_USE_CV_IMPL_STL11 = 1, + .HAVE_STRNLEN = 1, + .HAVE_FORK = 1, + }), + else => b.addConfigHeader(.{}, .{}), + }; const libzmq = if (!shared) b.addStaticLibrary(.{ .name = "zmq", @@ -75,9 +102,16 @@ pub fn build(b: *Builder) void { libzmq.addConfigHeader(config_header); libzmq.addIncludePath("include"); libzmq.addIncludePath("src"); + libzmq.addIncludePath("src/external"); libzmq.addIncludePath(config_header.include_path); - for (sources_cpp.items) |src| { - libzmq.addCSourceFile(src, cxxflags); + libzmq.addCSourceFiles(cxxSources, cxxFlags); + libzmq.addCSourceFiles(extraCsources, cFlags); + if (target.isWindows()) { + libzmq.addCSourceFile("external/wepoll/wepoll.c", cFlags); + libzmq.linkSystemLibraryName("ws2_32"); + libzmq.linkSystemLibraryName("rpcrt4"); + libzmq.linkSystemLibraryName("iphlpapi"); + //libzmq.linkSystemLibraryName("rt"); } libzmq.linkLibCpp(); // LLVM libc++ (builtin) libzmq.linkLibC(); // OS libc @@ -85,12 +119,13 @@ pub fn build(b: *Builder) void { libzmq.installHeadersDirectory("include", ""); } -const cxxflags: []const []const u8 = &.{ +const cFlags: []const []const u8 = &.{ "-Oz", "-Wall", "-pedantic", }; -const sources: []const []const u8 = &.{ +const cxxFlags = cFlags ++ [_][]const u8{"-std=c++14"}; +const cxxSources: []const []const u8 = &.{ "src/kqueue.cpp", "src/lb.cpp", "src/mailbox.cpp", @@ -167,8 +202,8 @@ const sources: []const []const u8 = &.{ "src/ip_resolver.cpp", "src/zap_client.cpp", "src/zmtp_engine.cpp", +}; +const extraCsources: []const []const u8 = &.{ "src/tweetnacl.c", - - //"external/wepoll/wepoll.c", "external/sha1/sha1.c", }; From f40ccb139a4fd46b84a8dcff1562319b97994d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Tue, 21 Feb 2023 15:26:11 -0300 Subject: [PATCH 05/10] fix external include_path --- build.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 724fede215..11cf52c05a 100644 --- a/build.zig +++ b/build.zig @@ -102,7 +102,7 @@ pub fn build(b: *Builder) void { libzmq.addConfigHeader(config_header); libzmq.addIncludePath("include"); libzmq.addIncludePath("src"); - libzmq.addIncludePath("src/external"); + libzmq.addIncludePath("external"); libzmq.addIncludePath(config_header.include_path); libzmq.addCSourceFiles(cxxSources, cxxFlags); libzmq.addCSourceFiles(extraCsources, cFlags); From 2ba59dac6f115d30e1648be21a8349531df87241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Wed, 22 Feb 2023 15:24:00 -0300 Subject: [PATCH 06/10] fix static build - missing files --- build.zig | 145 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 48 deletions(-) diff --git a/build.zig b/build.zig index 11cf52c05a..717aa60056 100644 --- a/build.zig +++ b/build.zig @@ -17,29 +17,37 @@ pub fn build(b: *Builder) void { .include_path = "platform.hpp", }, .{ .ZMQ_HAVE_LINUX = {}, - .ZMQ_USE_EPOLL = 1, - .ZMQ_HAVE_CURVE = 1, - .ZMQ_USE_TWEETNACL = 1, - .ZMQ_HAVE_EVENTFD = 1, - .ZMQ_HAVE_IFADDRS = 1, - .ZMQ_HAVE_SOCK_CLOEXEC = 1, - .ZMQ_HAVE_SO_BINDTODEVICE = 1, - .ZMQ_HAVE_SO_KEEPALIVED = 1, - .ZMQ_HAVE_SO_PEERCRED = 1, - .ZMQ_HAVE_TCP_KEEPCNT = 1, - .ZMQ_HAVE_TCP_KEEPIDLE = 1, - .ZMQ_HAVE_TCP_KEEPINTVL = 1, - .ZMQ_HAVE_UIO = 1, - .ZMQ_HAVE_STRLCPY = 1, - .ZMQ_USE_BUILTIN_SHA1 = 1, - .HAVE_FORK = 1, + .ZMQ_HAVE_PPOLL = {}, + .ZMQ_USE_EPOLL = {}, + .ZMQ_HAVE_CURVE = {}, + .ZMQ_USE_TWEETNACL = {}, + .ZMQ_HAVE_EVENTFD = {}, + .ZMQ_HAVE_IFADDRS = {}, + .ZMQ_HAVE_NOEXCEPT = {}, + .ZMQ_HAVE_SOCK_CLOEXEC = {}, + .ZMQ_HAVE_SO_BINDTODEVICE = {}, + .ZMQ_HAVE_SO_KEEPALIVED = {}, + .ZMQ_HAVE_SO_PEERCRED = {}, + .ZMQ_HAVE_TCP_KEEPCNT = {}, + .ZMQ_HAVE_TCP_KEEPIDLE = {}, + .ZMQ_HAVE_TCP_KEEPINTVL = {}, + .ZMQ_HAVE_UIO = {}, + .ZMQ_CLOCK_GETTIME = {}, + .ZMQ_HAVE_STRLCPY = {}, + .ZMQ_USE_BUILTIN_SHA1 = {}, + .HAVE_FORK = {}, + .ZMQ_HAVE_MKDTEMP = {}, .HAVE_POSIX_MEMALIGN = 1, - .HAVE_ACCEPT4 = 1, - .HAVE_STRNLEN = 1, - .ZMQ_IOTHREAD_POLLER_USE_EPOLL = 1, - .ZMQ_USE_CV_IMPL_STL11 = 1, - .ZMQ_POLL_BASED_ON_POLL = 1, + .HAVE_ACCEPT4 = {}, + .HAVE_IF_NAMETOINDEX = {}, + .HAVE_STRNLEN = {}, + .ZMQ_IOTHREAD_POLLER_USE_EPOLL = {}, + .ZMQ_IOTHREAD_POLLER_USE_EPOLL_CLOEXEC = {}, + .ZMQ_USE_CV_IMPL_STL11 = {}, + .ZMQ_HAVE_IPC = {}, + .ZMQ_POLL_BASED_ON_POLL = {}, .ZMQ_CACHELINE_SIZE = 64, + .ZMQ_USE_RADIX_TREE = {}, }), .windows => b.addConfigHeader(.{ .style = .blank, @@ -98,35 +106,83 @@ pub fn build(b: *Builder) void { }); if (optimize == .Debug or optimize == .ReleaseSafe) libzmq.bundle_compiler_rt = true - else if (shared) libzmq.want_lto = true; + else if (shared) { + //libzmq.want_lto = true; + libzmq.force_pic = true; + } + libzmq.strip = true; libzmq.addConfigHeader(config_header); libzmq.addIncludePath("include"); libzmq.addIncludePath("src"); libzmq.addIncludePath("external"); libzmq.addIncludePath(config_header.include_path); - libzmq.addCSourceFiles(cxxSources, cxxFlags); + libzmq.addCSourceFiles(switch (target.getOsTag()) { + .windows => cxxSources ++ [_][]const u8{"src/select.cpp"}, + .macos => cxxSources ++ [_][]const u8{"src/kqeue.cpp"}, + .linux => cxxSources ++ [_][]const u8{"src/epoll.cpp"}, + else => cxxSources, + }, cxxFlags); libzmq.addCSourceFiles(extraCsources, cFlags); if (target.isWindows()) { libzmq.addCSourceFile("external/wepoll/wepoll.c", cFlags); + // no pkg-config + libzmq.linkSystemLibraryName("kernel32"); + libzmq.linkSystemLibraryName("ntdll"); libzmq.linkSystemLibraryName("ws2_32"); libzmq.linkSystemLibraryName("rpcrt4"); libzmq.linkSystemLibraryName("iphlpapi"); - //libzmq.linkSystemLibraryName("rt"); + } else if (target.isDarwin()) { + // TODO + //libzmq.linkFramework(""); + } else { + libzmq.linkSystemLibrary("rt"); + } + // TODO: No support MSVC libc + if (target.getAbi() != .msvc) { + libzmq.linkLibCpp(); // LLVM libc++ (builtin) + libzmq.linkLibC(); // OS libc } - libzmq.linkLibCpp(); // LLVM libc++ (builtin) - libzmq.linkLibC(); // OS libc libzmq.install(); libzmq.installHeadersDirectory("include", ""); } const cFlags: []const []const u8 = &.{ - "-Oz", + "-O3", "-Wall", "-pedantic", + "-Wno-long-long", + "-Wno-uninitialized", + "-fno-sanitize=all", }; -const cxxFlags = cFlags ++ [_][]const u8{"-std=c++14"}; +const cxxFlags = cFlags; const cxxSources: []const []const u8 = &.{ - "src/kqueue.cpp", + "src/address.cpp", + "src/channel.cpp", + "src/client.cpp", + "src/clock.cpp", + "src/ctx.cpp", + "src/curve_client.cpp", + "src/curve_mechanism_base.cpp", + "src/curve_server.cpp", + "src/dealer.cpp", + "src/devpoll.cpp", + "src/dgram.cpp", + "src/dish.cpp", + "src/dist.cpp", + "src/endpoint.cpp", + "src/err.cpp", + "src/fq.cpp", + "src/gather.cpp", + "src/gssapi_mechanism_base.cpp", + "src/gssapi_client.cpp", + "src/gssapi_server.cpp", + "src/io_object.cpp", + "src/io_thread.cpp", + "src/ip.cpp", + "src/ip_resolver.cpp", + "src/ipc_address.cpp", + "src/ipc_connecter.cpp", + "src/ipc_listener.cpp", "src/lb.cpp", "src/mailbox.cpp", "src/mailbox_safe.cpp", @@ -136,10 +192,10 @@ const cxxSources: []const []const u8 = &.{ "src/msg.cpp", "src/mtrie.cpp", "src/norm_engine.cpp", + "src/null_mechanism.cpp", "src/object.cpp", "src/options.cpp", "src/own.cpp", - "src/null_mechanism.cpp", "src/pair.cpp", "src/peer.cpp", "src/pgm_receiver.cpp", @@ -152,19 +208,22 @@ const cxxSources: []const []const u8 = &.{ "src/poller_base.cpp", "src/polling_util.cpp", "src/pollset.cpp", + "src/precompiled.cpp", "src/proxy.cpp", "src/pub.cpp", "src/pull.cpp", "src/push.cpp", + "src/radio.cpp", + "src/radix_tree.cpp", "src/random.cpp", - "src/raw_encoder.cpp", "src/raw_decoder.cpp", + "src/raw_encoder.cpp", "src/raw_engine.cpp", "src/reaper.cpp", "src/rep.cpp", "src/req.cpp", "src/router.cpp", - "src/select.cpp", + "src/scatter.cpp", "src/server.cpp", "src/session_base.cpp", "src/signaler.cpp", @@ -172,6 +231,8 @@ const cxxSources: []const []const u8 = &.{ "src/socks.cpp", "src/socks_connecter.cpp", "src/stream.cpp", + "src/stream_connecter_base.cpp", + "src/stream_listener_base.cpp", "src/stream_engine_base.cpp", "src/sub.cpp", "src/tcp.cpp", @@ -179,29 +240,17 @@ const cxxSources: []const []const u8 = &.{ "src/tcp_connecter.cpp", "src/tcp_listener.cpp", "src/thread.cpp", + "src/timers.cpp", "src/trie.cpp", - "src/radix_tree.cpp", + "src/udp_address.cpp", + "src/udp_engine.cpp", "src/v1_decoder.cpp", - "src/v1_encoder.cpp", "src/v2_decoder.cpp", + "src/v1_encoder.cpp", "src/v2_encoder.cpp", - "src/v3_1_encoder.cpp", "src/xpub.cpp", "src/xsub.cpp", "src/zmq.cpp", - "src/zmq_utils.cpp", - "src/decoder_allocators.cpp", - "src/socket_poller.cpp", - "src/timers.cpp", - "src/radio.cpp", - "src/dish.cpp", - "src/udp_engine.cpp", - "src/udp_address.cpp", - "src/scatter.cpp", - "src/gather.cpp", - "src/ip_resolver.cpp", - "src/zap_client.cpp", - "src/zmtp_engine.cpp", }; const extraCsources: []const []const u8 = &.{ "src/tweetnacl.c", From a65b6b47fbe4abc64af0dad52c0d95992f6bc0db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Thu, 23 Feb 2023 09:13:58 -0300 Subject: [PATCH 07/10] More src files --- build.zig | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/build.zig b/build.zig index 717aa60056..c5710c97e4 100644 --- a/build.zig +++ b/build.zig @@ -165,6 +165,7 @@ const cxxSources: []const []const u8 = &.{ "src/curve_mechanism_base.cpp", "src/curve_server.cpp", "src/dealer.cpp", + "src/decoder_allocators.cpp", "src/devpoll.cpp", "src/dgram.cpp", "src/dish.cpp", @@ -173,8 +174,8 @@ const cxxSources: []const []const u8 = &.{ "src/err.cpp", "src/fq.cpp", "src/gather.cpp", - "src/gssapi_mechanism_base.cpp", "src/gssapi_client.cpp", + "src/gssapi_mechanism_base.cpp", "src/gssapi_server.cpp", "src/io_object.cpp", "src/io_thread.cpp", @@ -228,12 +229,13 @@ const cxxSources: []const []const u8 = &.{ "src/session_base.cpp", "src/signaler.cpp", "src/socket_base.cpp", + "src/socket_poller.cpp", "src/socks.cpp", "src/socks_connecter.cpp", "src/stream.cpp", "src/stream_connecter_base.cpp", - "src/stream_listener_base.cpp", "src/stream_engine_base.cpp", + "src/stream_listener_base.cpp", "src/sub.cpp", "src/tcp.cpp", "src/tcp_address.cpp", @@ -241,16 +243,23 @@ const cxxSources: []const []const u8 = &.{ "src/tcp_listener.cpp", "src/thread.cpp", "src/timers.cpp", + "src/tipc_address.cpp", + "src/tipc_connecter.cpp", + "src/tipc_listener.cpp", "src/trie.cpp", "src/udp_address.cpp", "src/udp_engine.cpp", "src/v1_decoder.cpp", - "src/v2_decoder.cpp", "src/v1_encoder.cpp", + "src/v2_decoder.cpp", "src/v2_encoder.cpp", + "src/v3_1_encoder.cpp", "src/xpub.cpp", "src/xsub.cpp", + "src/zap_client.cpp", "src/zmq.cpp", + "src/zmq_utils.cpp", + "src/zmtp_engine.cpp", }; const extraCsources: []const []const u8 = &.{ "src/tweetnacl.c", From 2bdfad7fe57e2c4027cc0fc5cc7a3a953df80ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Thu, 23 Feb 2023 16:12:15 -0300 Subject: [PATCH 08/10] improvement Windows/GNU static-build support --- build.zig | 64 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/build.zig b/build.zig index c5710c97e4..ab6bb2d547 100644 --- a/build.zig +++ b/build.zig @@ -55,37 +55,38 @@ pub fn build(b: *Builder) void { }, .{ .ZMQ_HAVE_WINDOWS = {}, .ZMQ_HAVE_MINGW32 = {}, - .ZMQ_HAVE_CURVE = 1, - .ZMQ_USE_TWEETNACL = 1, - .ZMQ_USE_SELECT = 1, - .ZMQ_USE_CV_IMPL_STL11 = 1, + .ZMQ_HAVE_CURVE = {}, + .ZMQ_USE_TWEETNACL = {}, + .ZMQ_USE_SELECT = {}, + .ZMQ_USE_CV_IMPL_STL11 = {}, .ZMQ_CACHELINE_SIZE = 64, - .ZMQ_IOTHREAD_POLLER_USE_SELECT = 1, - .ZMQ_POLL_BASED_ON_SELECT = 1, - .ZMQ_USE_BUILTIN_SHA1 = 1, + .ZMQ_IOTHREAD_POLLER_USE_SELECT = {}, + .ZMQ_POLL_BASED_ON_SELECT = {}, + .ZMQ_USE_BUILTIN_SHA1 = {}, .HAVE_STRNLEN = 1, + .ZMQ_USE_RADIX_TREE = {}, }), .macos => b.addConfigHeader(.{ .style = .blank, .include_path = "platform.hpp", }, .{ .ZMQ_HAVE_OSX = {}, - .ZMQ_USE_KQUEUE = 1, + .ZMQ_USE_KQUEUE = {}, .ZMQ_POSIX_MEMALIGN = 1, .ZMQ_CACHELINE_SIZE = 64, - .ZMQ_HAVE_CURVE = 1, - .ZMQ_USE_TWEETNACL = 1, - .ZMQ_HAVE_UIO = 1, - .ZMQ_HAVE_IFADDRS = 1, - .ZMQ_HAVE_OS_KEEPALIVE = 1, - .ZMQ_HAVE_TCP_KEEPALIVE = 1, - .ZMQ_HAVE_TCP_KEEPCNT = 1, - .ZMQ_HAVE_TCP_KEEPINTVL = 1, - .ZMQ_USE_BUILTIN_SHA1 = 1, - .ZMQ_IOTHREAD_POLLER_USE_KQEUE = 1, - .ZMQ_USE_CV_IMPL_STL11 = 1, - .HAVE_STRNLEN = 1, - .HAVE_FORK = 1, + .ZMQ_HAVE_CURVE = {}, + .ZMQ_USE_TWEETNACL = {}, + .ZMQ_HAVE_UIO = {}, + .ZMQ_HAVE_IFADDRS = {}, + .ZMQ_HAVE_OS_KEEPALIVE = {}, + .ZMQ_HAVE_TCP_KEEPALIVE = {}, + .ZMQ_HAVE_TCP_KEEPCNT = {}, + .ZMQ_HAVE_TCP_KEEPINTVL = {}, + .ZMQ_USE_BUILTIN_SHA1 = {}, + .ZMQ_IOTHREAD_POLLER_USE_KQEUE = {}, + .ZMQ_USE_CV_IMPL_STL11 = {}, + .HAVE_STRNLEN = {}, + .HAVE_FORK = {}, }), else => b.addConfigHeader(.{}, .{}), }; @@ -105,11 +106,8 @@ pub fn build(b: *Builder) void { .optimize = optimize, }); if (optimize == .Debug or optimize == .ReleaseSafe) - libzmq.bundle_compiler_rt = true - else if (shared) { - //libzmq.want_lto = true; - libzmq.force_pic = true; - } + libzmq.bundle_compiler_rt = true; + libzmq.strip = true; libzmq.addConfigHeader(config_header); libzmq.addIncludePath("include"); @@ -123,21 +121,28 @@ pub fn build(b: *Builder) void { else => cxxSources, }, cxxFlags); libzmq.addCSourceFiles(extraCsources, cFlags); + if (target.isWindows()) { libzmq.addCSourceFile("external/wepoll/wepoll.c", cFlags); // no pkg-config - libzmq.linkSystemLibraryName("kernel32"); - libzmq.linkSystemLibraryName("ntdll"); libzmq.linkSystemLibraryName("ws2_32"); libzmq.linkSystemLibraryName("rpcrt4"); libzmq.linkSystemLibraryName("iphlpapi"); + // MinGW + if (target.getAbi() == .gnu) { + // Need winpthread header & lib (zig has not included) + libzmq.linkSystemLibraryName("pthread.dll"); // pthread.dll.a (pthread.a get link error) + } } else if (target.isDarwin()) { // TODO //libzmq.linkFramework(""); } else { + // Linux libzmq.linkSystemLibrary("rt"); + libzmq.linkSystemLibrary("dl"); } - // TODO: No support MSVC libc + // TODO: No support MSVC libC++ (ucrt/msvcrt/vcruntime) + // https://github.com/ziglang/zig/issues/4785 - drop replacement for MSVC if (target.getAbi() != .msvc) { libzmq.linkLibCpp(); // LLVM libc++ (builtin) libzmq.linkLibC(); // OS libc @@ -152,7 +157,6 @@ const cFlags: []const []const u8 = &.{ "-pedantic", "-Wno-long-long", "-Wno-uninitialized", - "-fno-sanitize=all", }; const cxxFlags = cFlags; const cxxSources: []const []const u8 = &.{ From 88c7d08321890b63e2dc7605150d7f3834aa30e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Thu, 23 Feb 2023 16:43:34 -0300 Subject: [PATCH 09/10] Radix Benchmark - build run --- build.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/build.zig b/build.zig index ab6bb2d547..3946c3c463 100644 --- a/build.zig +++ b/build.zig @@ -3,6 +3,12 @@ const std = @import("std"); const Builder = std.Build.Builder; +const pkgBuilder = struct { + mode: std.builtin.OptimizeMode, + target: std.zig.CrossTarget, + build: *std.Build.CompileStep, +}; + pub fn build(b: *Builder) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -149,6 +155,36 @@ pub fn build(b: *Builder) void { } libzmq.install(); libzmq.installHeadersDirectory("include", ""); + + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + }, "radix_benchtest", "perf/benchmark_radix_tree.cpp"); +} + +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("src"); + test_exe.addCSourceFile(file, cFlags); + if (lib.target.isWindows()) + test_exe.linkSystemLibraryName("ws2_32"); + test_exe.linkLibCpp(); + 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); } const cFlags: []const []const u8 = &.{ From 3fc0cf5ea9f885052750ddc9ce0a9aeb3d695e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Tue, 28 Feb 2023 17:44:26 -0300 Subject: [PATCH 10/10] some fixes and build all perf-tools --- build.zig | 77 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/build.zig b/build.zig index 3946c3c463..ec12d19890 100644 --- a/build.zig +++ b/build.zig @@ -7,19 +7,21 @@ const pkgBuilder = struct { mode: std.builtin.OptimizeMode, target: std.zig.CrossTarget, build: *std.Build.CompileStep, + configH: *std.Build.ConfigHeaderStep, }; pub fn build(b: *Builder) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - // Options - static library [default] + // Option - static library [default] const shared = b.option(bool, "shared", "Build the Shared Library [default: false]") orelse false; + const perf = b.option(bool, "perf", "Build perf-tools [default: false]") orelse false; // Generating "platform.hpp" const config_header = switch (target.getOsTag()) { .linux => b.addConfigHeader(.{ - .style = .blank, //.{ .cmake = .{ .path = "builds/cmake/platform.hpp.in" } }, + .style = .blank, .include_path = "platform.hpp", }, .{ .ZMQ_HAVE_LINUX = {}, @@ -65,7 +67,7 @@ pub fn build(b: *Builder) void { .ZMQ_USE_TWEETNACL = {}, .ZMQ_USE_SELECT = {}, .ZMQ_USE_CV_IMPL_STL11 = {}, - .ZMQ_CACHELINE_SIZE = 64, + .ZMQ_CACHELINE_SIZE = std.atomic.cache_line, .ZMQ_IOTHREAD_POLLER_USE_SELECT = {}, .ZMQ_POLL_BASED_ON_SELECT = {}, .ZMQ_USE_BUILTIN_SHA1 = {}, @@ -79,7 +81,7 @@ pub fn build(b: *Builder) void { .ZMQ_HAVE_OSX = {}, .ZMQ_USE_KQUEUE = {}, .ZMQ_POSIX_MEMALIGN = 1, - .ZMQ_CACHELINE_SIZE = 64, + .ZMQ_CACHELINE_SIZE = std.atomic.cache_line, .ZMQ_HAVE_CURVE = {}, .ZMQ_USE_TWEETNACL = {}, .ZMQ_HAVE_UIO = {}, @@ -147,20 +149,63 @@ pub fn build(b: *Builder) void { libzmq.linkSystemLibrary("rt"); libzmq.linkSystemLibrary("dl"); } - // TODO: No support MSVC libC++ (ucrt/msvcrt/vcruntime) + // TODO: MSVC support libC++ (need: ucrt/msvcrt/vcruntime) // https://github.com/ziglang/zig/issues/4785 - drop replacement for MSVC - if (target.getAbi() != .msvc) { - libzmq.linkLibCpp(); // LLVM libc++ (builtin) - libzmq.linkLibC(); // OS libc - } + libzmq.linkLibCpp(); // LLVM libc++ (builtin) + libzmq.linkLibC(); // OS libc libzmq.install(); libzmq.installHeadersDirectory("include", ""); - buildSample(b, .{ - .target = target, - .mode = optimize, - .build = libzmq, - }, "radix_benchtest", "perf/benchmark_radix_tree.cpp"); + if (perf) { + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "bench_rt", "perf/benchmark_radix_tree.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "inproc_lat", "perf/inproc_lat.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "inproc_thr", "perf/inproc_thr.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "local_lat", "perf/local_lat.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "local_thr", "perf/local_thr.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "proxy_thr", "perf/proxy_thr.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "remote_thr", "perf/remote_thr.cpp"); + buildSample(b, .{ + .target = target, + .mode = optimize, + .build = libzmq, + .configH = config_header, + }, "remote_lat", "perf/remote_lat.cpp"); + } } fn buildSample(b: *std.Build.Builder, lib: pkgBuilder, name: []const u8, file: []const u8) void { @@ -170,6 +215,8 @@ fn buildSample(b: *std.Build.Builder, lib: pkgBuilder, name: []const u8, file: [ .target = lib.target, }); test_exe.linkLibrary(lib.build); + test_exe.addConfigHeader(lib.configH); + test_exe.addIncludePath(lib.configH.include_path); test_exe.addSystemIncludePath("src"); test_exe.addCSourceFile(file, cFlags); if (lib.target.isWindows()) @@ -183,7 +230,7 @@ fn buildSample(b: *std.Build.Builder, lib: pkgBuilder, name: []const u8, file: [ run_cmd.addArgs(args); } - const run_step = b.step("run", b.fmt("Run the {s}", .{name})); + const run_step = b.step(b.fmt("{s}", .{name}), b.fmt("Run the {s}", .{name})); run_step.dependOn(&run_cmd.step); }