-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.zig
118 lines (99 loc) · 3.73 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
const std = @import("std");
const sysjs = @import("src/main.zig");
pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
_ = b.addModule("mach-sysjs", .{
.root_source_file = .{ .path = "src/main.zig" },
});
// Use sysjs to generate bindings
try sysjs.generateFiles(
b.allocator,
"example/sysjs.zig",
"example/sysjs_generated.zig",
"example/sysjs_generated.js",
);
const app = try App.init(b, .{
.name = "example",
.src = "example/main.zig",
.optimize = optimize,
.deps = &.{
.{
.name = "sysjs",
.generated_js_file = "example/sysjs_generated.js",
.generated_zig_file = "example/sysjs_generated.zig",
},
},
});
var example_compile_step = b.step("example", "Compile example");
example_compile_step.dependOn(&app.install.step);
}
pub const index_html = @embedFile("www/index.html");
pub const Dependency = struct {
name: []const u8,
generated_js_file: []const u8,
generated_zig_file: []const u8,
};
pub const App = struct {
b: *std.Build,
name: []const u8,
compile: *std.Build.Step.Compile,
install: *std.Build.Step.InstallArtifact,
pub fn init(b: *std.Build, options: struct {
name: []const u8,
src: []const u8,
optimize: std.builtin.OptimizeMode,
deps: []const Dependency = &.{},
}) !App {
b.lib_dir = b.fmt("{s}/www", .{b.install_path});
const wasm32_freestanding = std.zig.CrossTarget{ .cpu_arch = .wasm32, .os_tag = .freestanding };
const lib = b.addExecutable(.{
.name = options.name,
.root_source_file = .{ .path = options.src },
.optimize = options.optimize,
.target = b.resolveTargetQuery(wasm32_freestanding),
});
lib.entry = .disabled;
lib.rdynamic = true;
b.installArtifact(lib);
const install = b.addInstallArtifact(lib, .{});
var inits = std.ArrayList(u8).init(b.allocator);
var imports = std.ArrayList(u8).init(b.allocator);
const import_writer = imports.writer();
for (options.deps) |dep| {
try import_writer.print(
"import * as {s} from './{s}';\n",
.{ dep.name, std.fs.path.basename(dep.generated_js_file) },
);
try inits.writer().print("{s}.init(wasm);\n", .{dep.name});
const module = b.createModule(.{
.root_source_file = .{ .path = b.pathFromRoot(dep.generated_zig_file) },
});
lib.root_module.addImport(dep.name, module);
const install_js = b.addInstallFile(
.{ .path = dep.generated_js_file },
try std.fs.path.join(b.allocator, &.{ "www/", std.fs.path.basename(dep.generated_js_file) }),
);
install.step.dependOn(&install_js.step);
}
try import_writer.writeAll("let imports = {\nenv: {\n");
for (options.deps) |dep| {
try import_writer.print("...{s},\n", .{dep.name});
}
try import_writer.writeAll("}\n};\n");
// TODO: do the format at runtime
const index_formatted = try std.fmt.allocPrint(b.allocator, index_html, .{
.app_name = options.name,
.wasm_path = options.name,
.imports = imports.items,
.inits = inits.items,
});
const write_index_html = b.addWriteFile(b.getInstallPath(.lib, "index.html"), index_formatted);
install.step.dependOn(&write_index_html.step);
return .{
.b = b,
.name = options.name,
.compile = lib,
.install = install,
};
}
};