-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.zig
330 lines (296 loc) · 11.6 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const log_level = b.option(
std.log.Level,
"log-level",
"The log level for the application. default .err",
) orelse .err;
const build_options = b.addOptions();
build_options.addOption(std.log.Level, "log_level", log_level);
// expose module 'flatbufferz' to dependees
const lib_mod = b.addModule(
"flatbufferz",
.{ .root_source_file = b.path("src/lib.zig") },
);
try lib_mod.import_table.put(b.allocator, "flatbufferz", lib_mod);
const zig_clap_pkg = b.dependency("clap", .{
.target = target,
.optimize = optimize,
});
const zig_clap = zig_clap_pkg.module("clap");
const exe = b.addExecutable(.{
.name = "flatc-zig",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("flatbufferz", lib_mod);
exe.root_module.addImport("zig-clap", zig_clap);
exe.root_module.addOptions("build_options", build_options);
b.installArtifact(exe);
const exe_run = b.addRunArtifact(exe);
exe_run.has_side_effects = true;
exe_run.step.dependOn(b.getInstallStep());
if (b.args) |args| exe_run.addArgs(args);
const run_step = b.step("run", "Run the app");
run_step.dependOn(&exe_run.step);
// generate files that need to be avaliable in tests
const gen_step = try GenStep.create(b, exe, &.{
"examples/sample.fbs",
"examples/monster_test.fbs",
"examples/include_test/order.fbs",
"examples/include_test/sub/no_namespace.fbs",
"examples/optional_scalars.fbs",
}, &.{ "-I", "examples/include_test", "-I", "examples/include_test/sub" }, "flatc-zig");
const gen_mod = b.createModule(.{
.root_source_file = gen_step.module.root_source_file,
.imports = &.{.{ .name = "flatbufferz", .module = lib_mod }},
});
const examples_mod = b.createModule(
.{ .root_source_file = b.path("examples/lib.zig") },
);
const exe_tests = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
exe_tests.root_module.addOptions("build_options", build_options);
exe_tests.root_module.addImport("flatbufferz", lib_mod);
exe_tests.root_module.addImport("generated", gen_mod);
exe_tests.root_module.addImport("examples", examples_mod);
exe_tests.step.dependOn(&gen_step.step);
const test_step = b.step("test", "Run unit tests");
const tests_run = b.addRunArtifact(exe_tests);
test_step.dependOn(&tests_run.step);
tests_run.has_side_effects = true;
const sample_exe = b.addExecutable(.{
.name = "sample",
.root_source_file = b.path("examples/sample_binary.zig"),
.target = target,
.optimize = optimize,
});
sample_exe.root_module.addOptions("build_options", build_options);
sample_exe.root_module.addImport("flatbufferz", lib_mod);
sample_exe.root_module.addImport("generated", gen_mod);
sample_exe.step.dependOn(&gen_step.step);
b.installArtifact(sample_exe);
const sample_run = b.addRunArtifact(sample_exe);
sample_run.has_side_effects = true;
const sample_run_step = b.step("run-sample", "Run the sample app");
sample_run_step.dependOn(&sample_run.step);
const flatbuffers_dep = b.dependency("flatbuffers", .{
.target = target,
.optimize = optimize,
});
const flatc = b.addExecutable(.{
.name = "flatc",
.root_source_file = null,
.target = target,
.optimize = optimize,
});
//
// This flatc build was created by running the following commands. The resulting
// build/compile_commands.json flags, includes and cpp files were used to
// make this build file.
//
// $ mkdir build && cd build
// $ cmake .. -DFLATBUFFERS_BUILD_FLATC=on -DFLATBUFFERS_BUILD_FLATLIB=off -DFLATBUFFERS_BUILD_TESTS=off -DFLATBUFFERS_BUILD_FLATHASH=off -DFLATBUFFERS_SKIP_MONSTER_EXTRA=on -DFLATBUFFERS_STRICT_MODE=on
// $ cmake --build . -- -n > cmake_build_commands.txt
//
const cpp_flags = [_][]const u8{
"-Wall",
"-Werror",
"-fno-rtti",
// "-Wno-error=stringop-overflow",
"-pedantic",
"-Wextra",
"-Wno-unused-parameter",
"-Wold-style-cast",
"-fsigned-char",
"-Wnon-virtual-dtor",
"-Wunused-result",
"-Wunused-parameter",
"-Werror=unused-parameter",
"-Wmissing-declarations",
"-Wzero-as-null-pointer-constant",
"-faligned-new",
// "-Werror=implicit-fallthrough=2",
"-Wextra-semi",
};
const flatbuffers_files = [_][]const u8{
"src/idl_parser.cpp",
"src/idl_gen_text.cpp",
"src/reflection.cpp",
"src/util.cpp",
"src/idl_gen_binary.cpp",
"src/idl_gen_cpp.cpp",
"src/idl_gen_csharp.cpp",
"src/idl_gen_dart.cpp",
"src/idl_gen_kotlin.cpp",
"src/idl_gen_kotlin_kmp.cpp",
"src/idl_gen_go.cpp",
"src/idl_gen_java.cpp",
"src/idl_gen_ts.cpp",
"src/idl_gen_php.cpp",
"src/idl_gen_python.cpp",
"src/idl_gen_lobster.cpp",
"src/idl_gen_rust.cpp",
"src/idl_gen_fbs.cpp",
"src/idl_gen_grpc.cpp",
"src/idl_gen_json_schema.cpp",
"src/idl_gen_swift.cpp",
"src/file_name_saving_file_manager.cpp",
"src/file_binary_writer.cpp",
"src/file_writer.cpp",
"src/flatc.cpp",
"src/flatc_main.cpp",
"src/binary_annotator.cpp",
"src/annotated_binary_text_gen.cpp",
"src/bfbs_gen_lua.cpp",
"src/bfbs_gen_nim.cpp",
"src/code_generators.cpp",
"grpc/src/compiler/cpp_generator.cc",
"grpc/src/compiler/go_generator.cc",
"grpc/src/compiler/java_generator.cc",
"grpc/src/compiler/python_generator.cc",
"grpc/src/compiler/swift_generator.cc",
"grpc/src/compiler/ts_generator.cc",
};
for (&flatbuffers_files) |file| {
flatc.addCSourceFile(.{ .file = flatbuffers_dep.path(file), .flags = &cpp_flags });
}
for ([_][]const u8{ "include", "grpc" }) |include_path|
flatc.addIncludePath(flatbuffers_dep.path(include_path));
flatc.linkLibCpp();
b.installArtifact(flatc);
build_options.addOptionPath("flatc_exe_path", flatc.getEmittedBin());
exe.step.dependOn(&flatc.step);
const flatc_run = b.addRunArtifact(flatc);
flatc_run.has_side_effects = true;
flatc_run.cwd = b.path(".");
if (b.args) |args| flatc_run.addArgs(args);
const flatc_run_step = b.step("flatc", "Run packaged flatc compiler");
flatc_run_step.dependOn(&flatc_run.step);
}
pub const GenStep = struct {
step: std.Build.Step,
b: *std.Build,
sources: std.ArrayListUnmanaged(std.Build.GeneratedFile) = .{},
cache_path: []const u8,
lib_file: std.Build.GeneratedFile,
module: *std.Build.Module,
/// init a GenStep, create zig-cache/flatc-zig if not exists, setup
/// dependencies, and setup args to exe.run()
pub fn create(
b: *std.Build,
exe: *std.Build.Step.Compile,
files: []const []const u8,
args: []const []const u8,
cache_subdir: []const u8,
) !*GenStep {
const self = b.allocator.create(GenStep) catch unreachable;
const cache_root = std.fs.path.basename(b.cache_root.path orelse ".");
const cache_path = try std.fs.path.join(
b.allocator,
&.{ cache_root, cache_subdir },
);
const lib_path = try std.fs.path.join(
b.allocator,
&.{ cache_path, "lib.zig" },
);
self.* = GenStep{
.step = std.Build.Step.init(.{
.id = .custom,
.name = "build-template",
.owner = b,
.makeFn = make,
}),
.b = b,
.cache_path = cache_path,
.lib_file = .{
.step = &self.step,
.path = lib_path,
},
.module = b.createModule(.{ .root_source_file = b.path(lib_path) }),
};
for (files) |file| {
const source = try self.sources.addOne(b.allocator);
source.* = .{ .path = file, .step = &self.step };
// source.addStepDependencies(&self.step);
}
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(&exe.step);
try b.cache_root.handle.makePath(cache_subdir);
run_cmd.addArgs(&.{ "-o", cache_path });
run_cmd.addArgs(args);
run_cmd.addArgs(files);
self.step.dependOn(&run_cmd.step);
return self;
}
/// iterate over all files in self.cache_path
/// and create a 'lib.zig' file at self.lib_file.path which exports all
/// generated .fb.zig files
fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) !void {
const self: *GenStep = @fieldParentPtr("step", step);
var file = try std.fs.cwd().createFile(self.lib_file.path.?, .{});
defer file.close();
const writer = file.writer();
try self.visit(self.cache_path, writer);
}
// recursively visit path and child directories
fn visit(self: *const GenStep, path: []const u8, writer: anytype) !void {
var dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind == .directory) {
const sub_path = try std.fs.path.join(self.b.allocator, &.{ path, entry.name });
defer self.b.allocator.free(sub_path);
try self.visit(sub_path, writer);
continue;
}
if (entry.kind != .file) continue;
// extract file name identifier: a/b/foo.fb.zig => foo
const endidx = std.mem.lastIndexOf(u8, entry.name, ".fb.zig") orelse
continue;
const startidx = if (std.mem.lastIndexOfScalar(
u8,
entry.name[0..endidx],
'/',
)) |i| i + 1 else 0;
const name = entry.name[startidx..endidx];
// remove illegal characters to make a zig identifier
var buf: [256]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
const fbswriter = fbs.writer();
if (self.cache_path.len < path.len) {
_ = try fbswriter.write(path[self.cache_path.len + 1 ..]);
_ = try fbswriter.writeByte('_');
}
_ = try fbswriter.write(name);
const ident = fbs.getWritten();
if (!std.ascii.isAlphabetic(ident[0]) and ident[0] != '_') {
std.log.err(
"invalid identifier '{s}'. filename must start with alphabetic or underscore",
.{ident},
);
return error.InvalidIdentifier;
}
for (ident, 0..) |c, i| {
if (!(std.ascii.isAlphanumeric(c) or c == '-')) ident[i] = '_';
}
if (self.cache_path.len < path.len)
try writer.print(
\\pub const {s} = @import("{s}{c}{s}.fb.zig");
\\
, .{ ident, path[self.cache_path.len + 1 ..], std.fs.path.sep, entry.name[0..endidx] })
else
try writer.print(
\\pub const {s} = @import("{s}.fb.zig");
\\
, .{ ident, entry.name[0..endidx] });
}
}
};