-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.zig
126 lines (106 loc) · 4.76 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
const std = @import("std");
const protobuf = @import("protobuf");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Protobuf code generation from the OpenTelemetry proto files.
const protobuf_dep = b.dependency("protobuf", .{
.target = target,
.optimize = optimize,
});
const protoc_step = protobuf.RunProtocStep.create(b, protobuf_dep.builder, target, .{
// Output directory for the generated zig files
.destination_directory = b.path("src"),
.source_files = &.{
// Add more protobuf definitions as the API grows
"proto-src/opentelemetry/proto/common/v1/common.proto",
"proto-src/opentelemetry/proto/resource/v1/resource.proto",
"proto-src/opentelemetry/proto/metrics/v1/metrics.proto",
"proto-src/opentelemetry/proto/trace/v1/trace.proto",
},
.include_directories = &.{
// Importsin proto files requires that the top-level directory
// containing te proto files is included
"proto-src/",
},
});
// Debug protoc generation in all builds
protoc_step.verbose = true;
const gen_proto = b.step("gen-proto", "Generates Zig files from protobuf definitions");
gen_proto.dependOn(&protoc_step.step);
const sdk_lib = b.addStaticLibrary(.{
.name = "opentelemetry-sdk",
.root_module = b.createModule(.{
.root_source_file = b.path("src/sdk.zig"),
.target = target,
.optimize = optimize,
.strip = false,
.unwind_tables = .sync,
}),
});
sdk_lib.root_module.addImport("protobuf", protobuf_dep.module("protobuf"));
b.installArtifact(sdk_lib);
// 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_source_file = b.path("src/sdk.zig"),
.target = target,
.optimize = optimize,
// Allow passing test filter using the build args.
.filters = b.args orelse &[0][]const u8{},
});
sdk_unit_tests.root_module.addImport("protobuf", protobuf_dep.module("protobuf"));
const run_sdk_unit_tests = b.addRunArtifact(sdk_unit_tests);
// Creates a step for unit testing the SDK.
// This only builds the test executable but does not run it.
const api_unit_tests = b.addTest(.{
.root_source_file = b.path("src/api.zig"),
.target = target,
.optimize = optimize,
.filters = b.args orelse &[0][]const u8{},
});
api_unit_tests.root_module.addImport("protobuf", protobuf_dep.module("protobuf"));
const api_sdk_unit_tests = b.addRunArtifact(api_unit_tests);
test_step.dependOn(&run_sdk_unit_tests.step);
test_step.dependOn(&api_sdk_unit_tests.step);
// Examples
const examples_step = b.step("examples", "Build and run all examples");
examples_step.dependOn(&sdk_lib.step);
const metrics_examples = buildExamples(b, "examples/metrics", sdk_lib.root_module) catch |err| {
std.debug.print("Error building metrics examples: {}\n", .{err});
return;
};
defer b.allocator.free(metrics_examples);
for (metrics_examples) |step| {
const run_metrics_example = b.addRunArtifact(step);
examples_step.dependOn(&run_metrics_example.step);
}
}
fn buildExamples(b: *std.Build, base_dir: []const u8, otel_mod: *std.Build.Module) ![]*std.Build.Step.Compile {
var exes = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
errdefer exes.deinit();
var ex_dir = try std.fs.cwd().openDir(base_dir, .{ .iterate = true });
defer ex_dir.close();
var ex_dir_iter = ex_dir.iterate();
while (try ex_dir_iter.next()) |file| {
// Get the file name.
// If it doesn't end in 'zig' then ignore.
const index = std.mem.lastIndexOfScalar(u8, file.name, '.') orelse continue;
if (index == 0) continue; // discard dotfiles
if (!std.mem.eql(u8, file.name[index + 1 ..], "zig")) continue;
const name = file.name[0..index];
const example = b.addExecutable(.{
.name = name,
.root_source_file = b.path(try std.fs.path.join(b.allocator, &.{ base_dir, file.name })),
.target = otel_mod.resolved_target.?,
// We set the optimization level to ReleaseSafe for examples
// because we want to have safety checks.
.optimize = .ReleaseSafe,
});
example.root_module.addImport("opentelemetry-sdk", otel_mod);
try exes.append(example);
}
return exes.toOwnedSlice();
}