@pkmn/engine 0.1.0-dev.29e0e9e1 → 0.1.0-dev.2b11d5c7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -3
- package/README.md +46 -18
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +44 -0
- package/build/pkg/addon/browser.js.map +1 -0
- package/build/pkg/addon/node.d.ts +3 -0
- package/build/pkg/addon/node.js +97 -0
- package/build/pkg/addon/node.js.map +1 -0
- package/build/pkg/addon/wasm.d.ts +2 -0
- package/build/pkg/addon/wasm.js +53 -0
- package/build/pkg/addon/wasm.js.map +1 -0
- package/build/pkg/addon.d.ts +25 -3
- package/build/pkg/addon.js +39 -71
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +15 -2
- package/build/pkg/data/protocol.json +23 -9
- package/build/pkg/data.d.ts +27 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +13 -17
- package/build/pkg/gen1.js +6 -10
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +40 -91
- package/build/pkg/index.js +6 -63
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +20 -1
- package/build/pkg/protocol.js +109 -107
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +36 -71
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +141 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +142 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +349 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +18 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +294 -90
- package/package.json +59 -132
- package/src/bin/install-pkmn-engine +30 -20
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +254 -0
- package/src/lib/{binding → bindings}/node.zig +8 -21
- package/src/lib/bindings/wasm.zig +59 -0
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/data.zig +4 -4
- package/src/lib/common/debug.zig +39 -8
- package/src/lib/common/js.zig +4 -2
- package/src/lib/common/optional.zig +14 -10
- package/src/lib/common/options.zig +6 -0
- package/src/lib/common/protocol.zig +77 -80
- package/src/lib/common/rational.zig +128 -32
- package/src/lib/common/rng.zig +7 -9
- package/src/lib/common/util.zig +50 -1
- package/src/lib/gen1/calc.zig +686 -159
- package/src/lib/gen1/chance.zig +405 -280
- package/src/lib/gen1/data/moves.zig +2 -4
- package/src/lib/gen1/data/species.zig +2 -4
- package/src/lib/gen1/data/types.zig +1 -2
- package/src/lib/gen1/data.zig +24 -27
- package/src/lib/gen1/helpers.zig +92 -548
- package/src/lib/gen1/mechanics.zig +379 -258
- package/src/lib/gen2/calc.zig +37 -27
- package/src/lib/gen2/chance.zig +84 -496
- package/src/lib/gen2/data/items.zig +1 -3
- package/src/lib/gen2/data/moves.zig +2 -4
- package/src/lib/gen2/data/species.zig +1 -3
- package/src/lib/gen2/data/types.zig +1 -2
- package/src/lib/gen2/data.zig +19 -22
- package/src/lib/gen2/generated.zig +19 -26
- package/src/lib/gen2/helpers.zig +46 -25
- package/src/lib/gen2/mechanics.zig +217 -163
- package/src/lib/node.zig +18 -0
- package/src/lib/pkmn.zig +37 -15
- package/src/lib/wasm.zig +17 -0
- package/src/tools/display/dom.ts +96 -0
- package/src/tools/display/ui.css +354 -0
- package/build/test/display.js +0 -400
- package/build/test/display.js.map +0 -1
- package/src/lib/binding/wasm.zig +0 -12
- package/src/lib/gen1/README.md +0 -894
- package/src/lib/gen2/README.md +0 -329
- package/src/test/display/pkmn.html.tmpl +0 -368
package/build.zig
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const std = @import("std");
|
|
2
1
|
const builtin = @import("builtin");
|
|
2
|
+
const std = @import("std");
|
|
3
3
|
|
|
4
4
|
pub const Options = struct {
|
|
5
5
|
showdown: ?bool = null,
|
|
@@ -8,6 +8,17 @@ pub const Options = struct {
|
|
|
8
8
|
calc: ?bool = null,
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
const Step = if (@hasDecl(std.Build, "Step")) std.Build.Step else .{
|
|
12
|
+
.Compile = std.Build.CompileStep,
|
|
13
|
+
.Options = std.Build.OptionsStep,
|
|
14
|
+
.Run = std.Build.RunStep,
|
|
15
|
+
};
|
|
16
|
+
const has_path = @hasField(std.Build.LazyPath, "path");
|
|
17
|
+
const root_module = @hasField(Step.Compile, "root_module");
|
|
18
|
+
const force_pic = !@hasField(std.Build.SharedLibraryOptions, "pic");
|
|
19
|
+
const ResolvedTarget =
|
|
20
|
+
if (@hasDecl(std.Build, "ResolvedTarget")) std.Build.ResolvedTarget else std.zig.CrossTarget;
|
|
21
|
+
|
|
11
22
|
pub fn module(b: *std.Build, options: Options) *std.Build.Module {
|
|
12
23
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
13
24
|
const build_options = b.addOptions();
|
|
@@ -15,16 +26,26 @@ pub fn module(b: *std.Build, options: Options) *std.Build.Module {
|
|
|
15
26
|
build_options.addOption(?bool, "log", options.log);
|
|
16
27
|
build_options.addOption(?bool, "chance", options.chance);
|
|
17
28
|
build_options.addOption(?bool, "calc", options.calc);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
.
|
|
21
|
-
|
|
29
|
+
|
|
30
|
+
const root_source_file: std.Build.LazyPath =
|
|
31
|
+
.{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
|
|
32
|
+
const Import = if (@hasDecl(std.Build.Module, "Import"))
|
|
33
|
+
std.Build.Module.Import
|
|
34
|
+
else
|
|
35
|
+
std.Build.ModuleDependency;
|
|
36
|
+
const imports: []const Import =
|
|
37
|
+
&.{.{ .name = "build_options", .module = build_options.createModule() }};
|
|
38
|
+
return b.createModule(if (@hasDecl(std.Build, "CreateModuleOptions"))
|
|
39
|
+
.{ .source_file = root_source_file, .dependencies = imports }
|
|
40
|
+
else
|
|
41
|
+
.{ .root_source_file = root_source_file, .imports = imports });
|
|
22
42
|
}
|
|
23
43
|
|
|
24
44
|
pub fn build(b: *std.Build) !void {
|
|
25
45
|
const target = b.standardTargetOptions(.{});
|
|
26
46
|
const optimize = b.standardOptimizeOption(.{});
|
|
27
47
|
|
|
48
|
+
const default: ?bool = if (force_pic) false else null;
|
|
28
49
|
const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
|
|
29
50
|
const node_import_lib =
|
|
30
51
|
b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
|
|
@@ -32,8 +53,8 @@ pub fn build(b: *std.Build) !void {
|
|
|
32
53
|
const wasm_stack_size =
|
|
33
54
|
b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
|
|
34
55
|
const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
|
|
35
|
-
const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse
|
|
36
|
-
const pic = b.option(bool, "pic", "Force position independent code") orelse
|
|
56
|
+
const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse default;
|
|
57
|
+
const pic = b.option(bool, "pic", "Force position independent code") orelse default;
|
|
37
58
|
const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
|
|
38
59
|
const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
|
|
39
60
|
|
|
@@ -44,14 +65,21 @@ pub fn build(b: *std.Build) !void {
|
|
|
44
65
|
defer parsed.deinit();
|
|
45
66
|
const version = parsed.value.object.get("version").?.string;
|
|
46
67
|
const description = parsed.value.object.get("description").?.string;
|
|
47
|
-
var repository = std.mem.
|
|
68
|
+
var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
|
|
48
69
|
std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
|
|
49
70
|
|
|
71
|
+
const fast = try std.SemanticVersion.parse("0.12.0-dev.866+3a47bc715");
|
|
72
|
+
if (optimize != .Debug and fast.order(builtin.zig_version) == .lt) {
|
|
73
|
+
std.log.warn("Release builds are ~10-20% slower than before Zig " ++
|
|
74
|
+
"v0.12.0-dev.866+3a47bc715 due to ziglang/zig#17768", .{});
|
|
75
|
+
}
|
|
76
|
+
|
|
50
77
|
const showdown =
|
|
51
78
|
b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
|
|
52
79
|
const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
|
|
53
80
|
const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
|
|
54
81
|
const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
|
|
82
|
+
const extra = b.option([]const []const u8, "option", "Enable extra option");
|
|
55
83
|
|
|
56
84
|
const options = b.addOptions();
|
|
57
85
|
options.addOption(?bool, "showdown", showdown);
|
|
@@ -59,32 +87,66 @@ pub fn build(b: *std.Build) !void {
|
|
|
59
87
|
options.addOption(?bool, "chance", chance);
|
|
60
88
|
options.addOption(?bool, "calc", calc);
|
|
61
89
|
|
|
90
|
+
if (extra) |opts| {
|
|
91
|
+
for (opts) |opt| {
|
|
92
|
+
if (std.mem.indexOfScalar(u8, opt, '=')) |i| {
|
|
93
|
+
options.addOption(?bool, opt[0..i], if (std.mem.eql(u8, opt[i + 1 ..], "true"))
|
|
94
|
+
true
|
|
95
|
+
else if (std.mem.eql(u8, opt[i + 1 ..], "false"))
|
|
96
|
+
false
|
|
97
|
+
else {
|
|
98
|
+
std.log.err("Invalid option: {s}\n", .{opt});
|
|
99
|
+
return error.InvalidOption;
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
options.addOption(?bool, opt, true);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
62
107
|
const name = if (showdown) "pkmn-showdown" else "pkmn";
|
|
63
108
|
|
|
109
|
+
const pkmn = if (@hasDecl(std.Build, "CreateModuleOptions")) null else b.addModule("pkmn", .{
|
|
110
|
+
.root_source_file = if (has_path)
|
|
111
|
+
.{ .path = "src/lib/pkmn.zig" }
|
|
112
|
+
else
|
|
113
|
+
b.path("src/lib/pkmn.zig"),
|
|
114
|
+
.optimize = optimize,
|
|
115
|
+
.target = target,
|
|
116
|
+
.imports = &.{.{ .name = "build_options", .module = options.createModule() }},
|
|
117
|
+
});
|
|
118
|
+
|
|
64
119
|
var c = false;
|
|
65
120
|
if (node_headers) |headers| {
|
|
66
121
|
const addon = b.fmt("{s}.node", .{name});
|
|
67
|
-
const
|
|
122
|
+
const path = if (has_path) .{ .path = "src/lib/node.zig" } else b.path("src/lib/node.zig");
|
|
123
|
+
const lib = b.addSharedLibrary(if (force_pic) .{
|
|
124
|
+
.name = addon,
|
|
125
|
+
.root_source_file = path,
|
|
126
|
+
.optimize = optimize,
|
|
127
|
+
.target = target,
|
|
128
|
+
} else .{
|
|
68
129
|
.name = addon,
|
|
69
|
-
.
|
|
70
|
-
.root_source_file = .{ .path = "src/lib/binding/node.zig" },
|
|
130
|
+
.root_source_file = path,
|
|
71
131
|
.optimize = optimize,
|
|
72
132
|
.target = target,
|
|
133
|
+
.strip = strip,
|
|
134
|
+
.pic = pic,
|
|
73
135
|
});
|
|
74
|
-
lib.addOptions("build_options", options);
|
|
75
|
-
lib.addSystemIncludePath(.{ .path = headers });
|
|
136
|
+
(if (root_module) lib.root_module else lib).addOptions("build_options", options);
|
|
137
|
+
lib.addSystemIncludePath(if (has_path) .{ .path = headers } else b.path(headers));
|
|
76
138
|
lib.linkLibC();
|
|
77
139
|
if (node_import_lib) |il| {
|
|
78
|
-
lib.addObjectFile(.{ .path = il });
|
|
79
|
-
} else if (
|
|
140
|
+
lib.addObjectFile(if (has_path) .{ .path = il } else b.path(il));
|
|
141
|
+
} else if (detect(target).os.tag == .windows) {
|
|
80
142
|
try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
|
|
81
143
|
std.process.exit(1);
|
|
82
144
|
}
|
|
83
145
|
lib.linker_allow_shlib_undefined = true;
|
|
84
146
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
85
|
-
if (pic) lib.force_pic = pic;
|
|
147
|
+
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
86
148
|
// Always emit to build/lib because this is where the driver code expects to find it
|
|
87
|
-
// TODO
|
|
149
|
+
// TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
|
|
88
150
|
//
|
|
89
151
|
// lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
|
|
90
152
|
// b.getInstallStep().dependOn(&lib.step);
|
|
@@ -93,59 +155,50 @@ pub fn build(b: *std.Build) !void {
|
|
|
93
155
|
// rename the file ourself in install-pkmn-engine
|
|
94
156
|
b.installArtifact(lib);
|
|
95
157
|
} else if (wasm) {
|
|
96
|
-
|
|
97
|
-
.name = name,
|
|
98
|
-
.main_pkg_path = .{ .path = "." },
|
|
99
|
-
.root_source_file = .{ .path = "src/lib/binding/wasm.zig" },
|
|
100
|
-
.optimize = switch (optimize) {
|
|
101
|
-
.ReleaseFast, .ReleaseSafe => .ReleaseSmall,
|
|
102
|
-
else => optimize,
|
|
103
|
-
},
|
|
104
|
-
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
|
|
105
|
-
});
|
|
106
|
-
lib.addOptions("build_options", options);
|
|
107
|
-
lib.stack_size = wasm_stack_size;
|
|
108
|
-
lib.rdynamic = true;
|
|
109
|
-
lib.strip = strip;
|
|
110
|
-
if (pic) lib.force_pic = pic;
|
|
111
|
-
const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
|
|
112
|
-
if (optimize != .Debug and opt != null) {
|
|
113
|
-
const out = b.fmt("build/lib/{s}.wasm", .{name});
|
|
114
|
-
const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
|
|
115
|
-
sh.addArtifactArg(lib);
|
|
116
|
-
sh.addArg("-o");
|
|
117
|
-
sh.addFileSourceArg(.{ .path = out });
|
|
118
|
-
b.getInstallStep().dependOn(&sh.step);
|
|
119
|
-
}
|
|
120
|
-
b.installArtifact(lib);
|
|
158
|
+
try buildWasm(b, name, "src/lib/wasm.zig", optimize, strip, pic, wasm_stack_size, options);
|
|
121
159
|
} else if (dynamic) {
|
|
122
|
-
const
|
|
160
|
+
const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
|
|
161
|
+
const lib = b.addSharedLibrary(if (force_pic) .{
|
|
162
|
+
.name = name,
|
|
163
|
+
.root_source_file = path,
|
|
164
|
+
.version = try std.SemanticVersion.parse(version),
|
|
165
|
+
.optimize = optimize,
|
|
166
|
+
.target = target,
|
|
167
|
+
} else .{
|
|
123
168
|
.name = name,
|
|
124
|
-
.
|
|
125
|
-
.root_source_file = .{ .path = "src/lib/binding/c.zig" },
|
|
169
|
+
.root_source_file = path,
|
|
126
170
|
.version = try std.SemanticVersion.parse(version),
|
|
127
171
|
.optimize = optimize,
|
|
128
172
|
.target = target,
|
|
173
|
+
.strip = strip,
|
|
174
|
+
.pic = pic,
|
|
129
175
|
});
|
|
130
|
-
lib.addOptions("build_options", options);
|
|
131
|
-
lib.addIncludePath(.{ .path = "src/include" });
|
|
176
|
+
(if (root_module) lib.root_module else lib).addOptions("build_options", options);
|
|
177
|
+
lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
|
|
132
178
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
133
|
-
if (pic) lib.force_pic = pic;
|
|
179
|
+
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
134
180
|
b.installArtifact(lib);
|
|
135
181
|
c = true;
|
|
136
182
|
} else {
|
|
137
|
-
const
|
|
183
|
+
const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
|
|
184
|
+
const lib = b.addStaticLibrary(if (force_pic) .{
|
|
185
|
+
.name = name,
|
|
186
|
+
.root_source_file = path,
|
|
187
|
+
.optimize = optimize,
|
|
188
|
+
.target = target,
|
|
189
|
+
} else .{
|
|
138
190
|
.name = name,
|
|
139
|
-
.
|
|
140
|
-
.root_source_file = .{ .path = "src/lib/binding/c.zig" },
|
|
191
|
+
.root_source_file = path,
|
|
141
192
|
.optimize = optimize,
|
|
142
193
|
.target = target,
|
|
194
|
+
.strip = strip,
|
|
195
|
+
.pic = pic,
|
|
143
196
|
});
|
|
144
|
-
lib.addOptions("build_options", options);
|
|
145
|
-
lib.addIncludePath(.{ .path = "src/include" });
|
|
197
|
+
(if (root_module) lib.root_module else lib).addOptions("build_options", options);
|
|
198
|
+
lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
|
|
146
199
|
lib.bundle_compiler_rt = true;
|
|
147
200
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
148
|
-
if (pic) lib.force_pic = pic;
|
|
201
|
+
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
149
202
|
if (emit_asm) {
|
|
150
203
|
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
151
204
|
lib.getEmittedAsm(),
|
|
@@ -165,15 +218,17 @@ pub fn build(b: *std.Build) !void {
|
|
|
165
218
|
}
|
|
166
219
|
|
|
167
220
|
if (c) {
|
|
168
|
-
const
|
|
169
|
-
.{ .path = "src/include/pkmn.h" }
|
|
170
|
-
|
|
171
|
-
"pkmn.h",
|
|
172
|
-
);
|
|
221
|
+
const path =
|
|
222
|
+
if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
|
|
223
|
+
const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
|
|
173
224
|
b.getInstallStep().dependOn(&header.step);
|
|
174
225
|
|
|
175
226
|
const pc = b.fmt("lib{s}.pc", .{name});
|
|
176
|
-
const file = try
|
|
227
|
+
const file = try std.fs.path.relative(
|
|
228
|
+
b.allocator,
|
|
229
|
+
try std.process.getCwdAlloc(b.allocator),
|
|
230
|
+
try b.cache_root.join(b.allocator, &.{pc}),
|
|
231
|
+
);
|
|
177
232
|
const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
|
|
178
233
|
|
|
179
234
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
@@ -195,7 +250,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
195
250
|
b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
|
|
196
251
|
}
|
|
197
252
|
|
|
198
|
-
const config = .{
|
|
253
|
+
const config: Config = .{
|
|
199
254
|
.target = target,
|
|
200
255
|
.optimize = optimize,
|
|
201
256
|
.pic = pic,
|
|
@@ -206,7 +261,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
206
261
|
// TODO: tests can be run multiple times due to @imports
|
|
207
262
|
const tests = TestStep.create(b, options, config);
|
|
208
263
|
|
|
209
|
-
var exes = std.ArrayList(*
|
|
264
|
+
var exes = std.ArrayList(*Step.Compile).init(b.allocator);
|
|
210
265
|
const tools: ToolConfig = .{
|
|
211
266
|
.options = .{
|
|
212
267
|
.showdown = showdown,
|
|
@@ -214,6 +269,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
214
269
|
.chance = chance,
|
|
215
270
|
.calc = calc,
|
|
216
271
|
},
|
|
272
|
+
.module = pkmn,
|
|
217
273
|
.general = config,
|
|
218
274
|
.tool = .{
|
|
219
275
|
.tests = if (tests.build) tests else null,
|
|
@@ -252,15 +308,116 @@ pub fn build(b: *std.Build) !void {
|
|
|
252
308
|
}
|
|
253
309
|
}
|
|
254
310
|
|
|
311
|
+
fn buildWasm(
|
|
312
|
+
b: *std.Build,
|
|
313
|
+
name: []const u8,
|
|
314
|
+
root_src_file: []const u8,
|
|
315
|
+
optimize: std.builtin.OptimizeMode,
|
|
316
|
+
strip: ?bool,
|
|
317
|
+
pic: ?bool,
|
|
318
|
+
wasm_stack_size: u64,
|
|
319
|
+
options: anytype,
|
|
320
|
+
) !void {
|
|
321
|
+
const entry = @hasDecl(Step.Compile, "Entry");
|
|
322
|
+
const mode = switch (optimize) {
|
|
323
|
+
.ReleaseFast, .ReleaseSafe => .ReleaseSmall,
|
|
324
|
+
else => optimize,
|
|
325
|
+
};
|
|
326
|
+
// https://webassembly.org/features/
|
|
327
|
+
var features = std.Target.wasm.featureSet(&.{
|
|
328
|
+
.atomics,
|
|
329
|
+
.bulk_memory,
|
|
330
|
+
// .exception_handling,
|
|
331
|
+
.extended_const,
|
|
332
|
+
// .half_precision,
|
|
333
|
+
// .multimemory,
|
|
334
|
+
.mutable_globals,
|
|
335
|
+
.nontrapping_fptoint,
|
|
336
|
+
.reference_types,
|
|
337
|
+
// .relaxed_simd,
|
|
338
|
+
.sign_ext,
|
|
339
|
+
.simd128,
|
|
340
|
+
// .tail_call,
|
|
341
|
+
});
|
|
342
|
+
if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
|
|
343
|
+
features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
|
|
344
|
+
}
|
|
345
|
+
const freestanding = if (@hasDecl(std.Build, "resolveTargetQuery"))
|
|
346
|
+
b.resolveTargetQuery(.{
|
|
347
|
+
.cpu_arch = .wasm32,
|
|
348
|
+
.os_tag = .freestanding,
|
|
349
|
+
.cpu_features_add = features,
|
|
350
|
+
})
|
|
351
|
+
else
|
|
352
|
+
ResolvedTarget{
|
|
353
|
+
.cpu_arch = .wasm32,
|
|
354
|
+
.os_tag = .freestanding,
|
|
355
|
+
.cpu_features_add = features,
|
|
356
|
+
};
|
|
357
|
+
const path = if (has_path) .{ .path = root_src_file } else b.path(root_src_file);
|
|
358
|
+
const bin = if (entry) bin: {
|
|
359
|
+
const exe = b.addExecutable(if (force_pic) .{
|
|
360
|
+
.name = name,
|
|
361
|
+
.root_source_file = path,
|
|
362
|
+
.optimize = mode,
|
|
363
|
+
.target = freestanding,
|
|
364
|
+
} else .{
|
|
365
|
+
.name = name,
|
|
366
|
+
.root_source_file = path,
|
|
367
|
+
.optimize = mode,
|
|
368
|
+
.target = freestanding,
|
|
369
|
+
.strip = strip,
|
|
370
|
+
.pic = pic,
|
|
371
|
+
});
|
|
372
|
+
(if (root_module) exe.root_module else exe).export_symbol_names = try exports(b);
|
|
373
|
+
exe.entry = .disabled;
|
|
374
|
+
break :bin exe;
|
|
375
|
+
} else bin: {
|
|
376
|
+
const lib = b.addSharedLibrary(.{
|
|
377
|
+
.name = name,
|
|
378
|
+
.root_source_file = path,
|
|
379
|
+
.optimize = mode,
|
|
380
|
+
.target = freestanding,
|
|
381
|
+
});
|
|
382
|
+
lib.rdynamic = true;
|
|
383
|
+
break :bin lib;
|
|
384
|
+
};
|
|
385
|
+
bin.stack_size = wasm_stack_size;
|
|
386
|
+
if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
|
|
387
|
+
if (force_pic and pic orelse false) bin.force_pic = pic;
|
|
388
|
+
(if (root_module) bin.root_module else bin).addOptions("build_options", options);
|
|
389
|
+
|
|
390
|
+
const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
|
|
391
|
+
if (optimize != .Debug and opt != null) {
|
|
392
|
+
const out = b.fmt("build/lib/{s}.wasm", .{name});
|
|
393
|
+
const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
|
|
394
|
+
sh.addArtifactArg(bin);
|
|
395
|
+
sh.addArg("-o");
|
|
396
|
+
if (@hasDecl(@TypeOf(sh.*), "addFileSourceArg")) {
|
|
397
|
+
sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
|
|
398
|
+
} else {
|
|
399
|
+
sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
|
|
400
|
+
}
|
|
401
|
+
b.getInstallStep().dependOn(&sh.step);
|
|
402
|
+
if (!entry) b.installArtifact(bin);
|
|
403
|
+
} else if (entry) {
|
|
404
|
+
b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
|
|
405
|
+
.dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
|
|
406
|
+
}).step);
|
|
407
|
+
} else {
|
|
408
|
+
b.installArtifact(bin);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
255
412
|
fn maybeStrip(
|
|
256
413
|
b: *std.Build,
|
|
257
|
-
artifact: *
|
|
414
|
+
artifact: *Step.Compile,
|
|
258
415
|
step: *std.Build.Step,
|
|
259
|
-
strip: bool,
|
|
416
|
+
strip: ?bool,
|
|
260
417
|
cmd: ?[]const u8,
|
|
261
418
|
) void {
|
|
262
|
-
artifact.strip = strip;
|
|
263
|
-
if (!strip or cmd == null) return;
|
|
419
|
+
if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
|
|
420
|
+
if (!(strip orelse false) or cmd == null) return;
|
|
264
421
|
// Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
|
|
265
422
|
const mac = builtin.os.tag == .macos;
|
|
266
423
|
if (mac and artifact.isDynamicLibrary()) return;
|
|
@@ -272,10 +429,10 @@ fn maybeStrip(
|
|
|
272
429
|
}
|
|
273
430
|
|
|
274
431
|
const Config = struct {
|
|
275
|
-
target:
|
|
432
|
+
target: ResolvedTarget,
|
|
276
433
|
optimize: std.builtin.OptimizeMode,
|
|
277
|
-
pic: bool,
|
|
278
|
-
strip: bool,
|
|
434
|
+
pic: ?bool,
|
|
435
|
+
strip: ?bool,
|
|
279
436
|
cmd: ?[]const u8,
|
|
280
437
|
};
|
|
281
438
|
|
|
@@ -283,9 +440,8 @@ const TestStep = struct {
|
|
|
283
440
|
step: std.Build.Step,
|
|
284
441
|
build: bool,
|
|
285
442
|
|
|
286
|
-
pub fn create(b: *std.Build, options: *
|
|
443
|
+
pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
|
|
287
444
|
const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
|
|
288
|
-
const test_file = b.option([]const u8, "test-file", "Input file for test");
|
|
289
445
|
const test_filter =
|
|
290
446
|
b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
|
291
447
|
|
|
@@ -293,22 +449,28 @@ const TestStep = struct {
|
|
|
293
449
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
|
|
294
450
|
self.* = .{ .step = step, .build = test_filter == null };
|
|
295
451
|
|
|
296
|
-
const path =
|
|
297
|
-
const tests = b.addTest(.{
|
|
298
|
-
.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
.root_source_file =
|
|
452
|
+
const path = if (has_path) .{ .path = "src/lib/test.zig" } else b.path("src/lib/test.zig");
|
|
453
|
+
const tests = b.addTest(if (force_pic) .{
|
|
454
|
+
.root_source_file = path,
|
|
455
|
+
.optimize = config.optimize,
|
|
456
|
+
.target = config.target,
|
|
457
|
+
.filter = test_filter,
|
|
458
|
+
} else .{
|
|
459
|
+
.root_source_file = path,
|
|
304
460
|
.optimize = config.optimize,
|
|
305
461
|
.target = config.target,
|
|
306
462
|
.filter = test_filter,
|
|
463
|
+
.single_threaded = true,
|
|
464
|
+
.strip = config.strip,
|
|
465
|
+
.pic = config.pic,
|
|
307
466
|
});
|
|
308
|
-
tests.addOptions("build_options", options);
|
|
309
|
-
|
|
467
|
+
(if (root_module) tests.root_module else tests).addOptions("build_options", options);
|
|
468
|
+
|
|
310
469
|
maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
|
|
311
|
-
if (
|
|
470
|
+
if (force_pic) {
|
|
471
|
+
tests.single_threaded = true;
|
|
472
|
+
if (config.pic orelse false) tests.force_pic = config.pic;
|
|
473
|
+
}
|
|
312
474
|
if (coverage) |c| {
|
|
313
475
|
tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
|
|
314
476
|
}
|
|
@@ -320,15 +482,16 @@ const TestStep = struct {
|
|
|
320
482
|
|
|
321
483
|
const ToolConfig = struct {
|
|
322
484
|
options: Options,
|
|
485
|
+
module: ?*std.Build.Module,
|
|
323
486
|
general: Config,
|
|
324
487
|
tool: struct {
|
|
325
488
|
tests: ?*TestStep,
|
|
326
489
|
name: ?[]const u8 = null,
|
|
327
|
-
exes: *std.ArrayList(*
|
|
490
|
+
exes: *std.ArrayList(*Step.Compile),
|
|
328
491
|
},
|
|
329
492
|
};
|
|
330
493
|
|
|
331
|
-
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*
|
|
494
|
+
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
|
|
332
495
|
std.fs.cwd().access(path, .{}) catch |err| switch (err) {
|
|
333
496
|
error.FileNotFound => return null,
|
|
334
497
|
else => |e| return e,
|
|
@@ -338,16 +501,32 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.RunSte
|
|
|
338
501
|
if (index) |i| name = name[0..i];
|
|
339
502
|
if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
|
|
340
503
|
|
|
341
|
-
const exe = b.addExecutable(.{
|
|
504
|
+
const exe = b.addExecutable(if (force_pic) .{
|
|
505
|
+
.name = name,
|
|
506
|
+
.root_source_file = if (has_path) .{ .path = path } else b.path(path),
|
|
507
|
+
.target = config.general.target,
|
|
508
|
+
.optimize = config.general.optimize,
|
|
509
|
+
} else .{
|
|
342
510
|
.name = name,
|
|
343
|
-
.root_source_file = .{ .path = path },
|
|
511
|
+
.root_source_file = if (has_path) .{ .path = path } else b.path(path),
|
|
344
512
|
.target = config.general.target,
|
|
345
513
|
.optimize = config.general.optimize,
|
|
514
|
+
.single_threaded = true,
|
|
515
|
+
.strip = config.general.strip,
|
|
516
|
+
.pic = config.general.pic,
|
|
346
517
|
});
|
|
347
|
-
exe.addModule("pkmn", module(b, config.options));
|
|
348
518
|
|
|
349
|
-
|
|
350
|
-
|
|
519
|
+
if (root_module) {
|
|
520
|
+
const import = if (config.module) |m| m else module(b, config.options);
|
|
521
|
+
exe.root_module.addImport("pkmn", import);
|
|
522
|
+
} else {
|
|
523
|
+
exe.addModule("pkmn", module(b, config.options));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
if (force_pic) {
|
|
527
|
+
exe.single_threaded = true;
|
|
528
|
+
if (config.general.pic orelse false) exe.force_pic = config.general.pic;
|
|
529
|
+
}
|
|
351
530
|
if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
|
|
352
531
|
config.tool.exes.append(exe) catch @panic("OOM");
|
|
353
532
|
|
|
@@ -361,7 +540,7 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.RunSte
|
|
|
361
540
|
const ToolsStep = struct {
|
|
362
541
|
step: std.Build.Step,
|
|
363
542
|
|
|
364
|
-
pub fn create(b: *std.Build, exes: *std.ArrayList(*
|
|
543
|
+
pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
|
|
365
544
|
const self = b.allocator.create(ToolsStep) catch @panic("OOM");
|
|
366
545
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
|
|
367
546
|
self.* = .{ .step = step };
|
|
@@ -369,3 +548,28 @@ const ToolsStep = struct {
|
|
|
369
548
|
return self;
|
|
370
549
|
}
|
|
371
550
|
};
|
|
551
|
+
|
|
552
|
+
fn detect(target: anytype) std.Target {
|
|
553
|
+
if (@hasField(@TypeOf(target), "result")) return target.result;
|
|
554
|
+
return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const WASM = @embedFile("src/lib/wasm.zig");
|
|
558
|
+
|
|
559
|
+
fn exports(b: *std.Build) ![][]const u8 {
|
|
560
|
+
var symbols = std.ArrayList([]const u8).init(b.allocator);
|
|
561
|
+
|
|
562
|
+
var it = std.mem.splitSequence(u8, WASM, "export ");
|
|
563
|
+
_ = it.next();
|
|
564
|
+
while (it.next()) |s| {
|
|
565
|
+
if (std.mem.startsWith(u8, s, "const ")) {
|
|
566
|
+
const i = std.mem.indexOf(u8, s[6..], " ").?;
|
|
567
|
+
try symbols.append(s[6 .. 6 + i]);
|
|
568
|
+
} else { // "fn "
|
|
569
|
+
const i = std.mem.indexOf(u8, s[3..], "(").?;
|
|
570
|
+
try symbols.append(s[3 .. 3 + i]);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return symbols.items;
|
|
575
|
+
}
|