forked from
karlseguin.tngl.sh/http.zig
An HTTP/1.1 server for zig
3.9 kB
102 lines
1const std = @import("std");
2
3pub fn build(b: *std.Build) !void {
4 const target = b.standardTargetOptions(.{});
5 const optimize = b.standardOptimizeOption(.{});
6
7 const dep_opts = .{ .target = target, .optimize = optimize };
8 const metrics_module = b.dependency("metrics", dep_opts).module("metrics");
9 const websocket_module = b.dependency("websocket", dep_opts).module("websocket");
10
11 const enable_tsan = b.option(bool, "tsan", "Enable ThreadSanitizer");
12
13 const httpz_module = b.addModule("httpz", .{
14 .root_source_file = b.path("src/httpz.zig"),
15 .target = target,
16 .optimize = optimize,
17 .sanitize_thread = enable_tsan,
18 .imports = &.{
19 .{ .name = "metrics", .module = metrics_module },
20 .{ .name = "websocket", .module = websocket_module },
21 },
22 });
23 {
24 const options = b.addOptions();
25 options.addOption(bool, "httpz_blocking", false);
26 httpz_module.addOptions("build", options);
27 }
28
29 {
30 const test_filter = b.option([]const []const u8, "test-filter", "Filters for test: specify multiple times for multiple filters");
31 const tests = b.addTest(.{
32 .root_module = httpz_module,
33 .filters = test_filter orelse &.{},
34 .test_runner = .{ .path = b.path("test_runner.zig"), .mode = .simple },
35 });
36 tests.linkLibC();
37 const force_blocking = b.option(bool, "force_blocking", "Force blocking mode") orelse false;
38 {
39 const options = b.addOptions();
40 options.addOption(bool, "httpz_blocking", force_blocking);
41 tests.root_module.addOptions("build", options);
42 }
43 {
44 const options = b.addOptions();
45 options.addOption(bool, "websocket_blocking", force_blocking);
46 websocket_module.addOptions("build", options);
47 }
48
49 tests.root_module.addImport("metrics", metrics_module);
50 tests.root_module.addImport("websocket", websocket_module);
51 const run_test = b.addRunArtifact(tests);
52 run_test.has_side_effects = true;
53
54 const test_step = b.step("test", "Run tests");
55 test_step.dependOn(&run_test.step);
56 }
57
58 const examples = [_]struct {
59 file: []const u8,
60 name: []const u8,
61 libc: bool = false,
62 }{
63 .{ .file = "examples/01_basic.zig", .name = "example_1" },
64 .{ .file = "examples/02_handler.zig", .name = "example_2" },
65 .{ .file = "examples/03_dispatch.zig", .name = "example_3" },
66 .{ .file = "examples/04_action_context.zig", .name = "example_4" },
67 .{ .file = "examples/05_request_takeover.zig", .name = "example_5" },
68 .{ .file = "examples/06_middleware.zig", .name = "example_6" },
69 .{ .file = "examples/07_advanced_routing.zig", .name = "example_7" },
70 .{ .file = "examples/08_websocket.zig", .name = "example_8" },
71 .{ .file = "examples/09_shutdown.zig", .name = "example_9", .libc = true },
72 };
73
74 {
75 for (examples) |ex| {
76 const exe = b.addExecutable(.{
77 .name = ex.name,
78 .root_module = b.createModule(.{
79 .root_source_file = b.path(ex.file),
80 .target = target,
81 .optimize = optimize,
82 .imports = &.{
83 .{.name = "httpz", .module = httpz_module},
84 }
85 }),
86 });
87 if (ex.libc) {
88 exe.linkLibC();
89 }
90 b.installArtifact(exe);
91
92 const run_cmd = b.addRunArtifact(exe);
93 run_cmd.step.dependOn(b.getInstallStep());
94 if (b.args) |args| {
95 run_cmd.addArgs(args);
96 }
97
98 const run_step = b.step(ex.name, ex.file);
99 run_step.dependOn(&run_cmd.step);
100 }
101 }
102}