forked from xataio/pgzx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
203 lines (172 loc) · 6.75 KB
/
build.zig
File metadata and controls
203 lines (172 loc) · 6.75 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const std = @import("std");
pub const Build = @import("src/pgzx/build.zig");
fn createPgsysModule(
b: *std.Build,
pgbuild: *Build,
name: []const u8,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Module {
const module = b.addModule(name, .{
.root_source_file = b.path("./src/pgzx/c.zig"),
.target = target,
.optimize = optimize,
});
// Internal C headers
module.addIncludePath(b.path("./src/pgzx/c/include/"));
// Postgres Headers
module.addIncludePath(.{
.cwd_relative = pgbuild.getIncludeServerDir(),
});
module.addIncludePath(.{
.cwd_relative = pgbuild.getIncludeDir(),
});
return module;
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const test_filter = b.option([]const u8, "test_filter", "Run a single test suite by name");
var pgbuild = Build.create(b, .{
.target = target,
.optimize = optimize,
});
const steps = .{
.docs = b.step("docs", "Generate documentation"),
.serve_docs = b.step("serve_docs", "Docs HTTP server http://localhost:8080/#docs.pgzx"),
// Unit tests installer target
// Optionally build and install the extension, so we can hook up with t a debugger and run tests manually.
.install_unit = b.step("install-unit", "Install unit tests extension (for manual testing)"),
.unit = b.step("unit", "Run pgzx unit tests"),
};
// pgzx_pgsys module: C bindings to Postgres
const pgzx_pgsys = createPgsysModule(b, pgbuild, "pgzx_pgsys", target, optimize);
{
pgzx_pgsys.addLibraryPath(.{
.cwd_relative = pgbuild.getLibDir(),
});
// libpq support
pgzx_pgsys.addCSourceFiles(.{
.files = &[_][]const u8{
"./src/pgzx/c/libpqsrv.c",
},
.flags = &[_][]const u8{
"-I", pgbuild.getIncludeDir(),
"-I", pgbuild.getIncludeServerDir(),
},
});
pgzx_pgsys.linkSystemLibrary("pq", .{});
}
const pgzx_pgsys_host = createPgsysModule(b, pgbuild, "pgzx_pgsys_host", b.graph.host, optimize);
// codegen
// The codegen produces Zig files that are imported as modules by pgzx.
const node_tags_src = blk: {
const gennodetags_module = b.addModule("gennodetags", .{
.root_source_file = b.path("./tools/gennodetags/main.zig"),
.target = b.graph.host,
.link_libc = true,
});
gennodetags_module.addIncludePath(.{ .cwd_relative = pgbuild.getIncludeServerDir() });
gennodetags_module.addIncludePath(.{ .cwd_relative = pgbuild.getIncludeDir() });
gennodetags_module.addImport("pgzx_pgsys", pgzx_pgsys_host);
const tool = b.addExecutable(.{
.name = "gennodetags",
.root_module = gennodetags_module,
});
const tool_step = b.addRunArtifact(tool);
break :blk tool_step.addOutputFileArg("nodetags.zig");
};
// pgzx: main project module.
// This module re-exports pgzx_pgsys, other generated modules, and utility functions.
const pgzx = blk: {
const module = b.addModule("pgzx", .{
.root_source_file = b.path("./src/pgzx.zig"),
.target = target,
.optimize = optimize,
});
module.addImport("pgzx_pgsys", pgzx_pgsys);
module.addAnonymousImport("gen_node_tags", .{
.root_source_file = node_tags_src,
.imports = &.{
.{ .name = "pgzx_pgsys", .module = pgzx_pgsys },
},
});
break :blk module;
};
// docs step
{
// Internal target to build the docs from the document. The generated
// documentation is installed in the <prefix>/share/pgzx/doc folder.
const obj = b.addObject(.{
.name = "docs",
.root_module = pgzx,
});
const install_docs = b.addInstallDirectory(.{
.source_dir = obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "share/pgzx/docs",
});
steps.docs.dependOn(&install_docs.step);
const del_docs = b.addSystemCommand(&[_][]const u8{
"rm", "-fr", "./docs",
});
const copy_docs = b.addSystemCommand(&[_][]const u8{
"cp", "-fr", "./zig-out/share/pgzx/docs", ".",
});
copy_docs.step.dependOn(&del_docs.step);
copy_docs.step.dependOn(&install_docs.step);
steps.docs.dependOn(©_docs.step);
// Use python to serve the docs via http.
const serve_docs = b.addSystemCommand(&[_][]const u8{
"python3", "-m", "http.server", "8080", "--directory", "./docs",
});
serve_docs.step.dependOn(©_docs.step);
steps.serve_docs.dependOn(&serve_docs.step);
}
// Unit test extension
const test_ext = blk: {
const test_options = b.addOptions();
test_options.addOption(bool, "testfn", true);
test_options.addOption([]const u8, "test_filter", test_filter orelse "");
const tests = pgbuild.addInstallExtension(.{
.name = "pgzx_unit",
.version = .{ .major = 0, .minor = 1 },
.source_file = b.path("src/testing.zig"),
.root_dir = "src/testing",
.link_libc = true,
.link_allow_shlib_undefined = true,
});
tests.lib.root_module.addOptions("build_options", test_options);
tests.lib.root_module.addIncludePath(b.path("./src/pgzx/c/include/"));
tests.lib.root_module.addImport("pgzx_pgsys", pgzx_pgsys);
tests.lib.root_module.addImport("pgzx", pgzx);
tests.lib.root_module.addAnonymousImport("gen_node_tags", .{
.root_source_file = node_tags_src,
.imports = &.{
.{ .name = "pgzx_pgsys", .module = pgzx_pgsys },
},
});
steps.install_unit.dependOn(&tests.step);
break :blk tests;
};
// Unit test runner
{
const psql_run_tests = pgbuild.addRunTests(.{
.name = "pgzx_unit",
.db_user = "postgres",
.db_port = 5432,
});
psql_run_tests.step.dependOn(&test_ext.step);
steps.unit.dependOn(&psql_run_tests.step);
}
// Examples
const examples_step = b.step("examples", "Build all examples");
inline for ([_][]const u8{ "char_count_zig", "pgaudit_zig", "pghostname_zig", "spi_sql", "sqlfns" }) |example| {
const build_cmd = b.addSystemCommand(&[_][]const u8{
"zig",
"build",
});
build_cmd.cwd = b.path(b.pathJoin(&.{"examples", example}));
examples_step.dependOn(&build_cmd.step);
}
}