-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.zig
More file actions
440 lines (387 loc) · 16.2 KB
/
build.zig
File metadata and controls
440 lines (387 loc) · 16.2 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const test_verbose = b.option(bool, "test-verbose", "Show verbose test output") orelse false;
const test_fail_first = b.option(bool, "test-fail-first", "Stop on first test failure") orelse false;
const test_show_logs = b.option(bool, "test-show-logs", "Show captured log output for tests") orelse false;
const benchmark_output = b.option([]const u8, "benchmark-output", "Path to write benchmark results to a file");
const benchmark_debug = b.option(bool, "benchmark-debug", "Enable debug build mode for benchmarks") orelse false;
// Dependencies section
// Benchmarks lib
const benchmarks_dep = b.dependency("zbench", .{});
// OpenTelemetry proto package ships protobuf as a dependency so we'll use it.
const otel_pb_dep = b.dependency("opentelemetry_proto", .{
.optimize = optimize,
.target = target,
});
const otel_proto_mod = otel_pb_dep.module("opentelemetry-proto");
const protobuf_mod = otel_pb_dep.builder.dependency("protobuf", .{
.optimize = optimize,
.target = target,
}).module("protobuf");
// TODO: remove when 0.16.0 is released
// zlib for gzip compression
// Build our own zlib library using the upstream source from madler/zlib
// Use lazyDependency to avoid executing the incompatible build.zig from the dependency
const zlib_upstream = b.lazyDependency("zlib", .{}) orelse return error.MissingZlibDependency;
const zlib_lib = b.addLibrary(.{
.name = "z",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
.linkage = .static,
});
zlib_lib.linkLibC();
zlib_lib.addIncludePath(zlib_upstream.path(""));
zlib_lib.addCSourceFiles(.{
.root = zlib_upstream.path(""),
.files = &.{
"adler32.c",
"compress.c",
"crc32.c",
"deflate.c",
"gzclose.c",
"gzlib.c",
"gzread.c",
"gzwrite.c",
"inflate.c",
"infback.c",
"inftrees.c",
"inffast.c",
"trees.c",
"uncompr.c",
"zutil.c",
},
.flags = &.{
"-DHAVE_SYS_TYPES_H",
"-DHAVE_STDINT_H",
"-DHAVE_STDDEF_H",
"-DZ_HAVE_UNISTD_H",
"-fno-sanitize=undefined", // Disable UBSan for C code to avoid linking issues
},
});
// Modules section
const sdk_mod = b.addModule("sdk", .{
.root_source_file = b.path("src/sdk.zig"),
.target = target,
.optimize = optimize,
.strip = false,
.unwind_tables = .sync,
.imports = &.{
.{ .name = "protobuf", .module = protobuf_mod },
.{ .name = "opentelemetry-proto", .module = otel_proto_mod },
},
});
// Static library for the OpenTelemetry SDK C users
const sdk_lib = b.addLibrary(.{
.name = "opentelemetry-sdk",
.linkage = .static,
.root_module = b.createModule(.{
.root_source_file = b.path("src/c.zig"),
.target = target,
.optimize = optimize,
}),
});
sdk_lib.linkLibrary(zlib_lib); // TODO: remove when 0.16.0 is released
b.installArtifact(sdk_lib);
// Install include headers for C users
b.installDirectory(.{
.source_dir = b.path("include"),
.install_dir = .header,
.install_subdir = "",
});
// Providing a way for the user to request running the unit tests.
const test_step = b.step("test", "Run unit tests");
// Creates a step for unit testing the SDK.
// This only builds the test executable but does not run it.
const sdk_unit_tests = b.addTest(.{
.root_module = sdk_mod,
// Use custom test runner to capture log output
.test_runner = .{ .path = b.path("src/test_runner.zig"), .mode = .simple },
// Allow passing test filter using the build args.
.filters = b.args orelse &[0][]const u8{},
});
sdk_unit_tests.linkLibrary(zlib_lib); // TODO: remove when 0.16.0 is released
// Pass test options as build options
const test_options = b.addOptions();
test_options.addOption(bool, "verbose", test_verbose);
test_options.addOption(bool, "fail_first", test_fail_first);
test_options.addOption(bool, "show_logs", test_show_logs);
sdk_unit_tests.root_module.addOptions("test_options", test_options);
const run_sdk_unit_tests = b.addRunArtifact(sdk_unit_tests);
test_step.dependOn(&run_sdk_unit_tests.step);
// Examples
const examples_step = b.step("examples", "Build and run all examples");
const examples_filter = b.option([]const u8, "examples-filter", "Filter examples to build");
// Attach an OTLP stub module to allow examples to use it.
const otel_stub_mod = b.createModule(.{
.root_source_file = b.path("examples/otlp_stub/server.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "opentelemetry-sdk", .module = sdk_mod },
.{ .name = "protobuf", .module = protobuf_mod },
.{ .name = "opentelemetry-proto", .module = otel_proto_mod },
},
});
// TODO add examples for other signals
const examples_dirs: []const []const u8 = &.{ "metrics", "trace", "logs", "baggage", "propagation" };
for (examples_dirs) |example_dir| {
const example = buildExamples(
b,
b.path(b.pathJoin(&.{ "examples", example_dir })),
sdk_mod,
otel_stub_mod,
otel_proto_mod,
examples_filter,
) catch |err| {
std.debug.print("Error building metrics examples: {}\n", .{err});
return err;
};
defer b.allocator.free(example);
for (example) |step| {
const run_example = b.addRunArtifact(step);
examples_step.dependOn(&run_example.step);
}
}
// C examples
const c_example_step = b.step("examples-c", "Build and run C examples");
const c_examples = [_][]const u8{
"logs",
"metrics",
"trace",
};
for (c_examples) |example_name| {
const c_example_exe = b.addExecutable(.{
.name = example_name,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
c_example_exe.addCSourceFile(.{
.file = b.path(b.pathJoin(&.{
"examples",
"c",
b.fmt("{s}.c", .{example_name}),
})),
.flags = &.{
"-std=c11",
"-Wall",
"-Wextra",
},
});
c_example_exe.addIncludePath(b.path("include"));
c_example_exe.linkLibrary(sdk_lib);
const run_c_example = b.addRunArtifact(c_example_exe);
c_example_step.dependOn(&run_c_example.step);
// Also install each C example executable
b.installArtifact(c_example_exe);
}
// Benchmarks
const benchmarks_step = b.step("benchmarks", "Build and run all benchmarks");
const benchmark_mod = benchmarks_dep.module("zbench");
const benchmarked_signals: []const []const u8 = &.{ "logs", "metrics", "trace" };
for (benchmarked_signals) |signal| {
const signal_benchmarks = buildBenchmarks(b, b.path(b.pathJoin(&.{ "benchmarks", signal })), sdk_mod, benchmark_mod, benchmark_debug) catch |err| {
std.debug.print("Error building {s} benchmarks: {}\n", .{ signal, err });
return err;
};
defer b.allocator.free(signal_benchmarks);
for (signal_benchmarks) |step| {
const run_signal_benchmark = b.addRunArtifact(step);
// If output file is specified, redirect stderr to file
if (benchmark_output) |output_path| {
// Set stderr to write to file
run_signal_benchmark.setEnvironmentVariable("BENCHMARK_OUTPUT_FILE", output_path);
}
benchmarks_step.dependOn(&run_signal_benchmark.step);
}
}
// Integration tests step
const integration_step = b.step("integration", "Run integration tests (requires Docker)");
const integration_tests = buildIntegrationTests(b, b.path("integration_tests"), sdk_mod) catch |build_err| {
std.debug.print("Error building integration tests: {}\n", .{build_err});
return build_err;
};
defer b.allocator.free(integration_tests);
for (integration_tests) |step| {
const run_integration_test = b.addRunArtifact(step);
integration_step.dependOn(&run_integration_test.step);
}
// Documentation webiste with autodoc
const sdk_docs = b.addObject(.{
.name = "sdk",
.root_module = b.createModule(.{
.root_source_file = b.path("src/sdk.zig"),
.target = target,
.optimize = .Debug,
}),
});
const install_docs = b.addInstallDirectory(.{
.source_dir = sdk_docs.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const docs_step = b.step("docs", "Copy documentation artifacts to prefix path");
docs_step.dependOn(&sdk_lib.step);
docs_step.dependOn(&install_docs.step);
}
fn buildExamples(
b: *std.Build,
examples_dir: std.Build.LazyPath,
otel_sdk_mod: *std.Build.Module,
otlp_stub_mod: *std.Build.Module,
proto_mod: *std.Build.Module,
name_filter: ?[]const u8,
) ![]*std.Build.Step.Compile {
var exes: std.ArrayList(*std.Build.Step.Compile) = .{};
errdefer exes.deinit(b.allocator);
var ex_dir = try examples_dir.getPath3(b, null).openDir("", .{ .iterate = true });
defer ex_dir.close();
var ex_dir_iter = ex_dir.iterate();
while (try ex_dir_iter.next()) |file| {
if (getZigFileName(file.name)) |name| {
// Discard the modules that do not match the filter
if (name_filter) |filter| {
if (std.mem.indexOf(u8, name, filter) == null) continue;
}
const file_name = try examples_dir.join(b.allocator, file.name);
const b_mod = b.createModule(.{
.root_source_file = file_name,
.target = otel_sdk_mod.resolved_target.?,
// We set the optimization level to ReleaseSafe for examples
// because we want to have safety checks, and execute assertions.
.optimize = .ReleaseSafe,
.imports = &.{
.{ .name = "opentelemetry-sdk", .module = otel_sdk_mod },
.{ .name = "otlp-stub", .module = otlp_stub_mod },
.{ .name = "opentelemetry-proto", .module = proto_mod },
},
});
try exes.append(b.allocator, b.addExecutable(.{
.name = name,
.root_module = b_mod,
}));
}
}
return exes.toOwnedSlice(b.allocator);
}
fn buildBenchmarks(
b: *std.Build,
bench_dir: std.Build.LazyPath,
otel_mod: *std.Build.Module,
benchmark_mod: *std.Build.Module,
debug_mode: bool,
) ![]*std.Build.Step.Compile {
var bench_tests: std.ArrayList(*std.Build.Step.Compile) = .{};
errdefer bench_tests.deinit(b.allocator);
var test_dir = try bench_dir.getPath3(b, null).openDir("", .{ .iterate = true });
defer test_dir.close();
var iter = test_dir.iterate();
while (try iter.next()) |file| {
if (getZigFileName(file.name)) |name| {
const file_name = try bench_dir.join(b.allocator, file.name);
const b_mod = b.createModule(.{
.root_source_file = file_name,
.target = otel_mod.resolved_target.?,
// We set the optimization level to ReleaseFast for benchmarks
// because we want to have the best performance.
.optimize = if (debug_mode) .Debug else .ReleaseFast,
.strip = !debug_mode,
.imports = &.{
.{ .name = "opentelemetry-sdk", .module = otel_mod },
.{ .name = "benchmark", .module = benchmark_mod },
},
});
const test_step = b.addTest(.{
.name = name,
.root_module = b_mod,
// Allow passing benchmark filter using the build args.
.filters = b.args orelse &[0][]const u8{},
});
try bench_tests.append(b.allocator, test_step);
}
}
return bench_tests.toOwnedSlice(b.allocator);
}
fn buildIntegrationTests(
b: *std.Build,
integration_dir: std.Build.LazyPath,
otel_mod: *std.Build.Module,
) ![]*std.Build.Step.Compile {
var integration_tests = std.ArrayList(*std.Build.Step.Compile){};
errdefer integration_tests.deinit(b.allocator);
var test_dir = integration_dir.getPath3(b, null).openDir("", .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => return integration_tests.toOwnedSlice(b.allocator),
else => return err,
};
defer test_dir.close();
// Create common module for shared integration test utilities
const common_path = try integration_dir.join(b.allocator, "common.zig");
const common_mod = b.createModule(.{
.root_source_file = common_path,
.target = otel_mod.resolved_target.?,
.optimize = .ReleaseSafe,
.imports = &.{
.{ .name = "opentelemetry-sdk", .module = otel_mod },
},
});
var iter = test_dir.iterate();
while (try iter.next()) |file| {
if (getZigFileName(file.name)) |name| {
// Skip common.zig as it's not a test executable
if (std.mem.eql(u8, name, "common")) continue;
// If integration filter is specified, skip non-matching tests
const integration_filter = b.args;
const file_name = try integration_dir.join(b.allocator, file.name);
if (integration_filter) |filter| {
for (filter) |filter_entry| {
if (std.mem.eql(u8, name, filter_entry)) {
const b_mod = b.createModule(.{
.root_source_file = file_name,
.target = otel_mod.resolved_target.?,
// Use ReleaseSafe for integration tests to have safety checks
.optimize = .ReleaseSafe,
.imports = &.{
.{ .name = "opentelemetry-sdk", .module = otel_mod },
.{ .name = "common", .module = common_mod },
},
});
try integration_tests.append(b.allocator, b.addExecutable(.{
.name = name,
.root_module = b_mod,
}));
}
}
} else {
const b_mod = b.createModule(.{
.root_source_file = file_name,
.target = otel_mod.resolved_target.?,
// Use ReleaseSafe for integration tests to have safety checks
.optimize = .ReleaseSafe,
.imports = &.{
.{ .name = "opentelemetry-sdk", .module = otel_mod },
.{ .name = "common", .module = common_mod },
},
});
try integration_tests.append(b.allocator, b.addExecutable(.{
.name = name,
.root_module = b_mod,
}));
}
}
}
return integration_tests.toOwnedSlice(b.allocator);
}
fn getZigFileName(file_name: []const u8) ?[]const u8 {
// Get the file name without extension, checking if it ends with '.zig'.
// If it doesn't end in 'zig' then ignore.
const index = std.mem.lastIndexOfScalar(u8, file_name, '.') orelse return null;
if (index == 0) return null; // discard dotfiles
if (!std.mem.eql(u8, file_name[index + 1 ..], "zig")) return null;
return file_name[0..index];
}