init
This builds a very basic version of the nats.c client (no TLS, no streaming/jetstream/whatever, since those bring in complex dependencies and I do not need them at the moment). Right now it contains a simple test program that demonstrates the functionality (cool!), but the plan is for the nats.zig to bind the API into a nicer, zig-like shape and re-export it. Then this becomes a package. The current function could become a test, though it's a bit complex for a unit test (and requires connecting to an externally-running NATS server in order to work).
This commit is contained in:
124
nats-c.build.zig
Normal file
124
nats-c.build.zig
Normal file
@@ -0,0 +1,124 @@
|
||||
const std = @import("std");
|
||||
|
||||
const NatsCOptions = struct {
|
||||
name: []const u8,
|
||||
target: std.zig.CrossTarget,
|
||||
optimize: std.builtin.OptimizeMode,
|
||||
};
|
||||
|
||||
pub fn nats_c_lib(
|
||||
b: *std.Build,
|
||||
options: NatsCOptions,
|
||||
) *std.Build.Step.Compile {
|
||||
const lib = b.addStaticLibrary(.{
|
||||
.name = options.name,
|
||||
.target = options.target,
|
||||
.optimize = options.optimize,
|
||||
});
|
||||
|
||||
lib.disable_sanitize_c = true;
|
||||
|
||||
lib.linkLibC();
|
||||
lib.addCSourceFiles(&common_sources, &.{"-fno-sanitize=undefined"});
|
||||
lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" });
|
||||
// if building with streaming support
|
||||
// lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" });
|
||||
// lib.addCSourceFiles(&streaming_sources, &.{"-fno-sanitize=undefined"});
|
||||
|
||||
switch (lib.target_info.target.os.tag) {
|
||||
.windows => {
|
||||
lib.addCSourceFiles(&win_sources, &.{"-fno-sanitize=undefined"});
|
||||
lib.defineCMacro("_WIN32", null);
|
||||
lib.linkSystemLibrary("Ws2_32");
|
||||
},
|
||||
.macos => {
|
||||
lib.addCSourceFiles(&unix_sources, &.{"-fno-sanitize=undefined"});
|
||||
lib.defineCMacro("DARWIN", null);
|
||||
},
|
||||
else => {
|
||||
lib.addCSourceFiles(&unix_sources, &.{"-fno-sanitize=undefined"});
|
||||
lib.defineCMacro("_GNU_SOURCE", null);
|
||||
lib.defineCMacro("LINUX", null);
|
||||
// may need to link pthread and rt. Not sure if those are inluded with linkLibC
|
||||
lib.linkSystemLibrary("pthread");
|
||||
lib.linkSystemLibrary("rt");
|
||||
},
|
||||
}
|
||||
|
||||
lib.defineCMacro("_REENTRANT", null);
|
||||
|
||||
inline for (install_headers) |header| {
|
||||
lib.installHeader(nats_src_prefix ++ header, "nats/" ++ header);
|
||||
}
|
||||
|
||||
b.installArtifact(lib);
|
||||
|
||||
return lib;
|
||||
}
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
_ = nats_c_lib(b, .{ .name = "nats-c", .target = target, .optimize = optimize });
|
||||
}
|
||||
|
||||
const nats_src_prefix = "deps/nats.c/src/";
|
||||
|
||||
const install_headers = [_][]const u8{
|
||||
"nats.h",
|
||||
"status.h",
|
||||
"version.h",
|
||||
};
|
||||
|
||||
const common_sources = [_][]const u8{
|
||||
nats_src_prefix ++ "asynccb.c",
|
||||
nats_src_prefix ++ "comsock.c",
|
||||
nats_src_prefix ++ "crypto.c",
|
||||
nats_src_prefix ++ "js.c",
|
||||
nats_src_prefix ++ "kv.c",
|
||||
nats_src_prefix ++ "nats.c",
|
||||
nats_src_prefix ++ "nkeys.c",
|
||||
nats_src_prefix ++ "opts.c",
|
||||
nats_src_prefix ++ "pub.c",
|
||||
nats_src_prefix ++ "stats.c",
|
||||
nats_src_prefix ++ "sub.c",
|
||||
nats_src_prefix ++ "url.c",
|
||||
nats_src_prefix ++ "buf.c",
|
||||
nats_src_prefix ++ "conn.c",
|
||||
nats_src_prefix ++ "hash.c",
|
||||
nats_src_prefix ++ "jsm.c",
|
||||
nats_src_prefix ++ "msg.c",
|
||||
nats_src_prefix ++ "natstime.c",
|
||||
nats_src_prefix ++ "nuid.c",
|
||||
nats_src_prefix ++ "parser.c",
|
||||
nats_src_prefix ++ "srvpool.c",
|
||||
nats_src_prefix ++ "status.c",
|
||||
nats_src_prefix ++ "timer.c",
|
||||
nats_src_prefix ++ "util.c",
|
||||
};
|
||||
|
||||
const unix_sources = [_][]const u8{
|
||||
nats_src_prefix ++ "unix/cond.c",
|
||||
nats_src_prefix ++ "unix/mutex.c",
|
||||
nats_src_prefix ++ "unix/sock.c",
|
||||
nats_src_prefix ++ "unix/thread.c",
|
||||
};
|
||||
|
||||
const win_sources = [_][]const u8{
|
||||
nats_src_prefix ++ "win/cond.c",
|
||||
nats_src_prefix ++ "win/mutex.c",
|
||||
nats_src_prefix ++ "win/sock.c",
|
||||
nats_src_prefix ++ "win/strings.c",
|
||||
nats_src_prefix ++ "win/thread.c",
|
||||
};
|
||||
|
||||
const streaming_sources = [_][]const u8{
|
||||
nats_src_prefix ++ "stan/conn.c",
|
||||
nats_src_prefix ++ "stan/copts.c",
|
||||
nats_src_prefix ++ "stan/msg.c",
|
||||
nats_src_prefix ++ "stan/protocol.pb-c.c",
|
||||
nats_src_prefix ++ "stan/pub.c",
|
||||
nats_src_prefix ++ "stan/sopts.c",
|
||||
nats_src_prefix ++ "stan/sub.c",
|
||||
};
|
Reference in New Issue
Block a user