forked from capy-ui/capy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
182 lines (155 loc) · 7.27 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
const std = @import("std");
const http = @import("deps.zig").imports.apple_pie;
const install = @import("build_capy.zig").install;
const installBuild = @import("build_capy.zig").installBuild;
const FileSource = std.build.FileSource;
/// Step used to run a web server
const WebServerStep = struct {
step: std.build.Step,
exe: *std.build.LibExeObjStep,
builder: *std.build.Builder,
pub fn create(builder: *std.build.Builder, exe: *std.build.LibExeObjStep) *WebServerStep {
const self = builder.allocator.create(WebServerStep) catch unreachable;
self.* = .{
.step = std.build.Step.init(.custom, "webserver", builder.allocator, WebServerStep.make),
.exe = exe,
.builder = builder,
};
return self;
}
const Context = struct {
exe: *std.build.LibExeObjStep,
builder: *std.build.Builder,
};
pub fn make(step: *std.build.Step) !void {
const self = @fieldParentPtr(WebServerStep, "step", step);
const allocator = self.builder.allocator;
var context = Context{ .builder = self.builder, .exe = self.exe };
const builder = http.router.Builder(*Context);
std.debug.print("Web server opened at http://localhost:8080/\n", .{});
try http.listenAndServe(
allocator,
try std.net.Address.parseIp("127.0.0.1", 8080),
&context,
comptime http.router.Router(*Context, &.{
builder.get("/", index),
builder.get("/capy.js", indexJs),
builder.get("/zig-app.wasm", wasmFile),
}),
);
}
fn index(context: *Context, response: *http.Response, request: http.Request) !void {
const allocator = request.arena;
const buildRoot = context.builder.build_root;
const file = try std.fs.cwd().openFile(try std.fs.path.join(allocator, &.{ buildRoot, "src/backends/wasm/index.html" }), .{});
defer file.close();
const text = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
try response.headers.put("Content-Type", "text/html");
try response.writer().writeAll(text);
}
fn indexJs(context: *Context, response: *http.Response, request: http.Request) !void {
const allocator = request.arena;
const buildRoot = context.builder.build_root;
const file = try std.fs.cwd().openFile(try std.fs.path.join(allocator, &.{ buildRoot, "src/backends/wasm/capy.js" }), .{});
defer file.close();
const text = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
try response.headers.put("Content-Type", "application/javascript");
try response.writer().writeAll(text);
}
fn wasmFile(context: *Context, response: *http.Response, request: http.Request) !void {
const allocator = request.arena;
const path = context.exe.getOutputSource().getPath(context.builder);
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
const text = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
try response.headers.put("Content-Type", "application/wasm");
try response.writer().writeAll(text);
}
};
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
var examplesDir = try std.fs.cwd().openIterableDir("examples", .{});
defer examplesDir.close();
const broken = switch (target.getOsTag()) {
.windows => &[_][]const u8{ "osm-viewer", "fade", "slide-viewer", "demo", "notepad", "dev-tools", "many-counters" },
else => &[_][]const u8{"many-counters"},
};
var walker = try examplesDir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (entry.kind == .File and std.mem.eql(u8, std.fs.path.extension(entry.path), ".zig")) {
const name = try std.mem.replaceOwned(u8, b.allocator, entry.path[0 .. entry.path.len - 4], std.fs.path.sep_str, "-");
defer b.allocator.free(name);
// it is not freed as the path is used later for building
const programPath = FileSource.relative(b.pathJoin(&.{ "examples", entry.path }));
const exe: *std.build.LibExeObjStep = if (target.toTarget().isWasm())
b.addSharedLibrary(.{ .name = name, .root_source_file = programPath, .target = target, .optimize = optimize })
else
b.addExecutable(.{ .name = name, .root_source_file = programPath, .target = target, .optimize = optimize });
try install(exe, .{});
const install_step = b.addInstallArtifact(exe);
const working = blk: {
for (broken) |broken_name| {
if (std.mem.eql(u8, name, broken_name))
break :blk false;
}
break :blk true;
};
if (working) {
b.getInstallStep().dependOn(&install_step.step);
} else {
std.log.warn("'{s}' is broken (disabled by default)", .{name});
}
if (target.toTarget().isWasm()) {
if (@import("builtin").zig_backend != .stage2_llvm) {
const serve = WebServerStep.create(b, exe);
serve.step.dependOn(&exe.install_step.?.step);
const serve_step = b.step(name, "Start a web server to run this example");
serve_step.dependOn(&serve.step);
}
} else {
const run_cmd = exe.run();
run_cmd.step.dependOn(&exe.install_step.?.step);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step(name, "Run this example");
run_step.dependOn(&run_cmd.step);
}
}
}
const lib = b.addSharedLibrary(.{
.name = "capy",
.root_source_file = FileSource.relative("src/c_api.zig"),
.version = std.builtin.Version{ .major = 0, .minor = 3, .patch = 0 },
.target = target,
.optimize = optimize,
});
lib.linkLibC();
try install(lib, .{});
// lib.emit_h = true;
lib.install();
const sharedlib_install_step = b.addInstallArtifact(lib);
b.getInstallStep().dependOn(&sharedlib_install_step.step);
const buildc_step = b.step("shared", "Build capy as a shared library (with C ABI)");
buildc_step.dependOn(&lib.install_step.?.step);
const tests = b.addTest(.{
.root_source_file = FileSource.relative("src/main.zig"),
.target = target,
.optimize = optimize,
});
// tests.emit_docs = .emit;
try install(tests, .{});
const test_step = b.step("test", "Run unit tests and also generate the documentation");
test_step.dependOn(&tests.step);
const coverage_tests = b.addTest(.{
.root_source_file = FileSource.relative("src/main.zig"),
.target = target,
.optimize = optimize,
});
coverage_tests.exec_cmd_args = &.{ "kcov", "--clean", "--include-pattern=src/", "kcov-output", null };
try install(coverage_tests, .{});
const cov_step = b.step("coverage", "Perform code coverage of unit tests. This requires 'kcov' to be installed.");
cov_step.dependOn(&coverage_tests.step);
}