Native PostgreSQL driver / client for Zig
1const std = @import("std");
2
3pub fn build(b: *std.Build) !void {
4 const target = b.standardTargetOptions(.{});
5 const optimize = b.standardOptimizeOption(.{});
6
7 // setup our dependencies
8 const dep_opts = .{ .target = target, .optimize = optimize };
9
10 const openssl_lib_name = b.option([]const u8, "openssl_lib_name", "");
11 const openssl_lib_path = b.option(std.Build.LazyPath, "openssl_lib_path", "");
12 const openssl_include_path = b.option(std.Build.LazyPath, "openssl_include_path", "");
13 const openssl = b.option(bool, "openssl", "Enable OpenSSL/TLS support") orelse
14 (openssl_lib_name != null or openssl_lib_path != null or openssl_include_path != null);
15
16 // When openssl is not enabled, skip translate-c entirely and substitute
17 // an empty stub module. Otherwise the build fails on hosts/targets where
18 // <openssl/ssl.h> isn't reachable — e.g. when -Dtarget switches Zig into
19 // cross-compile mode and stops searching the system include paths.
20 const openssl_module = if (openssl) blk: {
21 const Translator = @import("translate_c").Translator;
22 const translate_c = b.dependency("translate_c", .{});
23 const t: Translator = .init(translate_c, .{
24 .c_source_file = b.path("src/openssl.h"),
25 .target = target,
26 .optimize = optimize,
27 });
28 if (openssl_include_path) |p| t.addIncludePath(p);
29 break :blk t.mod;
30 } else b.createModule(.{ .root_source_file = b.path("src/openssl_stub.zig") });
31
32 // Expose this as a module that others can import
33 const pg_module = b.addModule("pg", .{
34 .target = target,
35 .optimize = optimize,
36 .root_source_file = b.path("src/pg.zig"),
37 .imports = &.{
38 .{ .name = "buffer", .module = b.dependency("buffer", dep_opts).module("buffer") },
39 .{ .name = "metrics", .module = b.dependency("metrics", dep_opts).module("metrics") },
40 .{ .name = "openssl", .module = openssl_module },
41 },
42 });
43
44 if (openssl) {
45 if (openssl_lib_path) |p| pg_module.addLibraryPath(p);
46 pg_module.linkSystemLibrary("crypto", .{});
47 pg_module.linkSystemLibrary(openssl_lib_name orelse "ssl", .{});
48 pg_module.link_libc = true;
49 }
50
51 var column_names = false;
52 const column_names_opt = b.option(bool, "column_names", "");
53
54 if (column_names_opt) |val| {
55 column_names = val;
56 }
57
58 {
59 const options = b.addOptions();
60 options.addOption(bool, "openssl", openssl);
61 options.addOption(bool, "column_names", column_names);
62 pg_module.addOptions("config", options);
63 }
64
65 {
66 // test step — always built with openssl enabled
67 const Translator = @import("translate_c").Translator;
68 const translate_c = b.dependency("translate_c", .{});
69 const t: Translator = .init(translate_c, .{
70 .c_source_file = b.path("src/openssl.h"),
71 .target = target,
72 .optimize = optimize,
73 });
74 if (openssl_include_path) |p| t.addIncludePath(p);
75
76 const lib_test = b.addTest(.{
77 .root_module = b.createModule(.{
78 .target = target,
79 .optimize = optimize,
80 .root_source_file = b.path("src/pg.zig"),
81 .imports = &.{
82 .{ .name = "buffer", .module = b.dependency("buffer", dep_opts).module("buffer") },
83 .{ .name = "metrics", .module = b.dependency("metrics", dep_opts).module("metrics") },
84 .{ .name = "openssl", .module = t.mod },
85 },
86 }),
87 .test_runner = .{ .path = b.path("test_runner.zig"), .mode = .simple },
88 });
89 if (openssl_lib_path) |p|
90 lib_test.root_module.addLibraryPath(p);
91 lib_test.root_module.linkSystemLibrary("crypto", .{});
92 lib_test.root_module.linkSystemLibrary("ssl", .{});
93
94 {
95 const options = b.addOptions();
96 options.addOption(bool, "openssl", true);
97 options.addOption(bool, "column_names", false);
98 lib_test.root_module.addOptions("config", options);
99 }
100
101 const run_test = b.addRunArtifact(lib_test);
102 run_test.has_side_effects = true;
103
104 const test_step = b.step("test", "Run unit tests");
105 test_step.dependOn(&run_test.step);
106 }
107}