-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
126 lines (103 loc) · 4.43 KB
/
Copy pathbuild.zig
File metadata and controls
126 lines (103 loc) · 4.43 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
const std = @import("std");
const protobuf = @import("protobuf");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("src/c.h"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
translate_c.addIncludePath(b.path("libs"));
const c_module = translate_c.createModule();
const protobuf_dep = b.dependency("protobuf", .{
.target = target,
.optimize = optimize,
});
const mod = b.addModule("pg_query", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.imports = &.{
.{ .name = "c", .module = c_module },
.{ .name = "protobuf", .module = protobuf_dep.module("protobuf") },
},
});
const proto_gen_mod = b.createModule(.{
.root_source_file = b.path("src/proto_gen/pg_query.pb.zig"),
.target = target,
.imports = &.{
.{ .name = "protobuf", .module = protobuf_dep.module("protobuf") },
},
});
mod.addImport("pg_query_proto", proto_gen_mod);
const wf = b.addNamedWriteFiles("libs");
_ = wf.addCopyFile(b.path("libs/libpg_query.a"), "libpg_query.a");
_ = wf.addCopyFile(b.path("libs/pg_query.h"), "pg_query.h");
const exe = b.addExecutable(.{
.name = "pg_query",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "pg_query", .module = mod },
.{ .name = "c", .module = c_module },
.{ .name = "protobuf", .module = protobuf_dep.module("protobuf") },
},
}),
});
exe.root_module.addObjectFile(b.path("libs/libpg_query.a"));
exe.root_module.addIncludePath(b.path("libs"));
exe.use_llvm = true;
b.installArtifact(exe);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
const gen_proto = b.step("gen-proto", "generates zig files from protocol buffer definitions");
const protoc_step = protobuf.RunProtocStep.create(protobuf_dep.builder, target, .{
// out directory for the generated zig files
.destination_directory = b.path("src/proto_gen"),
// Optional LazyPath to `protoc`. If null, zig-protobuf will download Google's release of
// the compiler.
// .protoc = b.path("protoc"),
.source_files = &.{
b.path("proto/pg_query.proto"),
},
.include_directories = &.{b.path("proto")},
// Preserve unknown fields during binary decode/encode round trips.
// Defaults to false.
.preserve_unknown_fields = false,
});
gen_proto.dependOn(&protoc_step.step);
// The generated protobuf code contains recursive types (Node → node_union → * → Node)
// where ~190 fields use `?Node` (value type), causing infinite size recursion.
// This patch rewrites them to `?*Node` (pointer type) which breaks the cycle.
// The zig-protobuf library already supports ?*MessageType at decode/encode/deinit.
const patch_step = b.addSystemCommand(&.{ "sed", "-i", "s/: ?Node = null,/: ?*Node = null,/g", "src/proto_gen/pg_query.pb.zig" });
patch_step.step.dependOn(&protoc_step.step);
gen_proto.dependOn(&patch_step.step);
const mod_tests = b.addTest(.{
.root_module = mod,
});
mod_tests.root_module.addObjectFile(b.path("libs/libpg_query.a"));
mod_tests.root_module.addIncludePath(b.path("libs"));
mod_tests.use_llvm = true;
const run_mod_tests = b.addRunArtifact(mod_tests);
const exe_tests = b.addTest(.{
.root_module = exe.root_module,
});
exe_tests.root_module.addObjectFile(b.path("libs/libpg_query.a"));
exe_tests.root_module.addIncludePath(b.path("libs"));
exe_tests.use_llvm = true;
const run_exe_tests = b.addRunArtifact(exe_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
}