forked from jane0009/zig-postgres
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.zig
80 lines (64 loc) · 2.33 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const std = @import("std");
const builtin = @import("builtin");
const Builder = std.build.Builder;
const package_name = "postgres";
const package_path = "src/postgres.zig";
const examples = [2][]const u8{ "main", "custom_types" };
const include_dir = switch (builtin.target.os.tag) {
.linux => "/usr/include",
.windows => "C:\\Program Files\\PostgreSQL\\14\\include",
.macos => "/opt/homebrew/opt/libpq",
else => "/usr/include",
};
pub fn build(b: *Builder) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
b.addSearchPrefix(include_dir);
// Export zig-postgres as a module
b.addModule(.{
.name = package_name,
.source_file = .{ .path = package_path },
});
const postgres_module = b.modules.get(package_name) orelse unreachable;
const db_uri = b.option(
[]const u8,
"db",
"Specify the database url",
) orelse "postgresql://postgresql:postgresql@localhost:5432/mydb";
const db_options = b.addOptions();
db_options.addOption([]const u8, "db_uri", db_uri);
inline for (examples) |example| {
const exe = b.addExecutable(.{
.name = example,
.root_source_file = .{ .path = "examples/" ++ example ++ ".zig" },
.target = target,
.optimize = optimize,
});
exe.addOptions("build_options", db_options);
exe.addModule("postgres", postgres_module);
exe.linkSystemLibrary("pq");
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step(example, "Run the app");
run_step.dependOn(&run_cmd.step);
}
const lib = b.addStaticLibrary(.{
.name = package_name,
.root_source_file = .{ .path = "src/postgres.zig" },
.target = target,
.optimize = optimize,
});
lib.addOptions("build_options", db_options);
lib.linkSystemLibrary("pq");
const tests = b.addTest(.{
.root_source_file = .{ .path = "tests.zig" },
.target = target,
.optimize = optimize,
});
tests.linkSystemLibrary("pq");
tests.addModule("postgres", postgres_module);
tests.addOptions("build_options", db_options);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&tests.step);
}