build: update for zig-0.12.0-dev.2208+4debd4338

Incorporate various build API changes. Hopefully there won't be any
other major API changes before the 0.12.0 release.
This commit is contained in:
torque 2024-01-15 16:23:45 -08:00
parent 3462b3cdb6
commit 4124b912eb
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
3 changed files with 27 additions and 16 deletions

View File

@ -9,8 +9,9 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const nats = b.addModule("nats", .{ const nats = b.addModule("nats", .{
.source_file = .{ .path = "src/nats.zig" }, .root_source_file = .{ .path = "src/nats.zig" },
}); });
nats.addIncludePath(.{ .path = b.getInstallPath(.header, "") });
const nats_c = nats_build.nats_c_lib(b, .{ const nats_c = nats_build.nats_c_lib(b, .{
.name = "nats-c", .name = "nats-c",
@ -25,7 +26,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
tests.addModule("nats", nats); tests.root_module.addImport("nats", nats);
tests.linkLibrary(nats_c); tests.linkLibrary(nats_c);
const run_main_tests = b.addRunArtifact(tests); const run_main_tests = b.addRunArtifact(tests);
@ -42,7 +43,7 @@ pub fn build(b: *std.Build) void {
} }
const ExampleOptions = struct { const ExampleOptions = struct {
target: std.zig.CrossTarget, target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode, optimize: std.builtin.OptimizeMode,
nats_module: *std.Build.Module, nats_module: *std.Build.Module,
nats_c: *std.Build.Step.Compile, nats_c: *std.Build.Step.Compile,
@ -59,7 +60,7 @@ const examples = [_]Example{
.{ .name = "pub_bytes", .file = "examples/pub_bytes.zig" }, .{ .name = "pub_bytes", .file = "examples/pub_bytes.zig" },
}; };
pub fn add_examples(b: *std.build, options: ExampleOptions) void { pub fn add_examples(b: *std.Build, options: ExampleOptions) void {
const example_step = b.step("examples", "build examples"); const example_step = b.step("examples", "build examples");
inline for (examples) |example| { inline for (examples) |example| {
@ -70,7 +71,7 @@ pub fn add_examples(b: *std.build, options: ExampleOptions) void {
.optimize = .Debug, .optimize = .Debug,
}); });
ex_exe.addModule("nats", options.nats_module); ex_exe.root_module.addImport("nats", options.nats_module);
ex_exe.linkLibrary(options.nats_c); ex_exe.linkLibrary(options.nats_c);
const install = b.addInstallArtifact(ex_exe, .{}); const install = b.addInstallArtifact(ex_exe, .{});

View File

@ -1,10 +1,20 @@
.{ .{
.name = "nats-client", .name = "nats-client",
.version = "0.0.1", .version = "0.0.1",
.paths = .{
"src",
"deps/nats.c/src",
"deps/nats.c/LICENSE",
"deps/protobuf-c",
"build.zig",
"nats-c.build.zig",
"build.zig.zon",
"LICENSE",
},
.dependencies = .{ .dependencies = .{
.libressl = .{ .libressl = .{
.url = "https://github.com/epicyclic-dev/LibreSSL-portable/archive/2b68369a2b883714cea05357aa378b3a3e8ef2f6.tar.gz", .url = "https://github.com/epicyclic-dev/LibreSSL-portable/archive/9f74aeb1d2f5db5c375a1040cbd2b9abfa2749d1.tar.gz",
.hash = "12206b907fcb1dea424d122d29a0549bdc6c83648e0433973388b2efb6813b36a8fa", .hash = "122092a200f7e27e90974013d7e5cd5ef27438f67016852b5244ea287018263e78dc",
}, },
}, },
} }

View File

@ -5,7 +5,7 @@ const std = @import("std");
const NatsCOptions = struct { const NatsCOptions = struct {
name: []const u8, name: []const u8,
target: std.zig.CrossTarget, target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode, optimize: std.builtin.OptimizeMode,
}; };
@ -24,36 +24,36 @@ pub fn nats_c_lib(
}; };
lib.linkLibC(); lib.linkLibC();
lib.addCSourceFiles(&common_sources, &cflags); lib.addCSourceFiles(.{ .files = &common_sources, .flags = &cflags });
lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" }); lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" });
// if building with streaming support (protocol.pb-c.c includes // if building with streaming support (protocol.pb-c.c includes
// <protobuf-c/protobuf-c.h>, unfortunately) // <protobuf-c/protobuf-c.h>, unfortunately)
lib.addIncludePath(.{ .path = "deps" }); lib.addIncludePath(.{ .path = "deps" });
lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" }); lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" });
lib.addCSourceFiles(&streaming_sources, &cflags); lib.addCSourceFiles(.{ .files = &streaming_sources, .flags = &cflags });
lib.addCSourceFiles(&protobuf_c_sources, &cflags); lib.addCSourceFiles(.{ .files = &protobuf_c_sources, .flags = &cflags });
const ssl_dep = b.dependency("libressl", .{ const ssl_dep = b.dependency("libressl", .{
.target = options.target, .target = options.target,
.optimize = options.optimize, .optimize = options.optimize,
}); });
const tinfo = lib.target_info.target; const tinfo = options.target.result;
switch (tinfo.os.tag) { switch (tinfo.os.tag) {
.windows => { .windows => {
lib.addCSourceFiles(&win_sources, &cflags); lib.addCSourceFiles(.{ .files = &win_sources, .flags = &cflags });
if (tinfo.abi != .msvc) { if (tinfo.abi != .msvc) {
lib.addCSourceFiles(&.{"src/win-crosshack.c"}, &cflags); lib.addCSourceFiles(.{ .files = &.{"src/win-crosshack.c"}, .flags = &cflags });
} }
lib.defineCMacro("_WIN32", null); lib.defineCMacro("_WIN32", null);
lib.linkSystemLibrary("ws2_32"); lib.linkSystemLibrary("ws2_32");
}, },
.macos => { .macos => {
lib.addCSourceFiles(&unix_sources, &cflags); lib.addCSourceFiles(.{ .files = &unix_sources, .flags = &cflags });
lib.defineCMacro("DARWIN", null); lib.defineCMacro("DARWIN", null);
}, },
else => { else => {
lib.addCSourceFiles(&unix_sources, &cflags); lib.addCSourceFiles(.{ .files = &unix_sources, .flags = &cflags });
lib.defineCMacro("_GNU_SOURCE", null); lib.defineCMacro("_GNU_SOURCE", null);
lib.defineCMacro("LINUX", null); lib.defineCMacro("LINUX", null);
// may need to link pthread and rt. Not sure if those are included with linkLibC // may need to link pthread and rt. Not sure if those are included with linkLibC