@pkmn/engine 0.1.0-dev.6195e056 → 0.1.0-dev.62eefbc0
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 +155 -81
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +47 -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 +110 -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 +29 -0
- package/build/pkg/addon.js +80 -0
- package/build/pkg/addon.js.map +1 -0
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +40 -5
- package/build/pkg/data/protocol.json +84 -13
- package/build/pkg/data.d.ts +28 -7
- package/build/pkg/data.js +3 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +18 -20
- package/build/pkg/gen1.js +76 -51
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +57 -103
- package/build/pkg/index.js +23 -70
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +33 -3
- package/build/pkg/protocol.js +127 -104
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +144 -0
- package/build/tools/debug.js.map +1 -0
- package/build/tools/display/debug.jsx +66 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +152 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +360 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +157 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +349 -174
- package/package.json +95 -133
- package/src/bin/install-pkmn-engine +100 -42
- package/src/bin/pkmn-debug +62 -0
- package/src/lib/bindings/c.zig +200 -0
- package/src/lib/bindings/node.zig +117 -0
- package/src/lib/bindings/wasm.zig +50 -0
- package/src/lib/common/array.zig +70 -0
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +79 -0
- package/src/lib/common/js.zig +114 -0
- package/src/lib/common/optional.zig +61 -0
- package/src/lib/common/options.zig +18 -3
- package/src/lib/common/protocol.zig +1072 -497
- package/src/lib/common/rational.zig +440 -0
- package/src/lib/common/rng.zig +31 -24
- package/src/lib/common/util.zig +44 -0
- package/src/lib/gen1/calc.zig +975 -0
- package/src/lib/gen1/chance.zig +1222 -0
- package/src/lib/gen1/data/moves.zig +203 -193
- package/src/lib/gen1/data/species.zig +6 -8
- package/src/lib/gen1/data/types.zig +12 -13
- package/src/lib/gen1/data.zig +105 -94
- package/src/lib/gen1/helpers.zig +138 -55
- package/src/lib/gen1/mechanics.zig +1469 -948
- package/src/lib/gen2/calc.zig +150 -0
- package/src/lib/gen2/chance.zig +896 -0
- package/src/lib/gen2/data/items.zig +15 -23
- package/src/lib/gen2/data/moves.zig +665 -573
- package/src/lib/gen2/data/species.zig +3 -5
- package/src/lib/gen2/data/types.zig +141 -36
- package/src/lib/gen2/data.zig +140 -104
- package/src/lib/gen2/generated.zig +801 -0
- package/src/lib/gen2/helpers.zig +58 -45
- package/src/lib/gen2/mechanics.zig +3582 -30
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +14 -0
- package/src/lib/pkmn.zig +177 -22
- package/src/lib/wasm.zig +13 -0
- package/src/tools/display/dom.ts +108 -0
- package/src/tools/display/ui.css +354 -0
- package/src/lib/binding/node.zig +0 -229
- package/src/lib/binding/wasm.zig +0 -10
- package/src/lib/gen1/README.md +0 -777
- package/src/lib/gen2/README.md +0 -309
package/build.zig
CHANGED
|
@@ -1,18 +1,37 @@
|
|
|
1
|
-
const std = @import("std");
|
|
2
1
|
const builtin = @import("builtin");
|
|
2
|
+
const std = @import("std");
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
fn ArrayList(comptime T: type) type {
|
|
5
|
+
return std.array_list.Aligned(T, null);
|
|
6
|
+
}
|
|
5
7
|
|
|
6
|
-
pub const Options = struct {
|
|
8
|
+
pub const Options = struct {
|
|
9
|
+
showdown: ?bool = null,
|
|
10
|
+
log: ?bool = null,
|
|
11
|
+
chance: ?bool = null,
|
|
12
|
+
calc: ?bool = null,
|
|
13
|
+
};
|
|
7
14
|
|
|
8
|
-
pub fn module(
|
|
9
|
-
|
|
15
|
+
pub fn module(
|
|
16
|
+
b: *std.Build,
|
|
17
|
+
options: Options,
|
|
18
|
+
target: std.Build.ResolvedTarget,
|
|
19
|
+
optimize: std.builtin.OptimizeMode,
|
|
20
|
+
) *std.Build.Module {
|
|
10
21
|
const build_options = b.addOptions();
|
|
11
22
|
build_options.addOption(?bool, "showdown", options.showdown);
|
|
12
|
-
build_options.addOption(?bool, "
|
|
23
|
+
build_options.addOption(?bool, "log", options.log);
|
|
24
|
+
build_options.addOption(?bool, "chance", options.chance);
|
|
25
|
+
build_options.addOption(?bool, "calc", options.calc);
|
|
26
|
+
|
|
27
|
+
const root_source_file = b.path("src/lib/pkmn.zig");
|
|
28
|
+
const imports: []const std.Build.Module.Import =
|
|
29
|
+
&.{.{ .name = "build_options", .module = build_options.createModule() }};
|
|
13
30
|
return b.createModule(.{
|
|
14
|
-
.
|
|
15
|
-
.
|
|
31
|
+
.root_source_file = root_source_file,
|
|
32
|
+
.target = target,
|
|
33
|
+
.optimize = optimize,
|
|
34
|
+
.imports = imports,
|
|
16
35
|
});
|
|
17
36
|
}
|
|
18
37
|
|
|
@@ -23,141 +42,193 @@ pub fn build(b: *std.Build) !void {
|
|
|
23
42
|
const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
|
|
24
43
|
const node_import_lib =
|
|
25
44
|
b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
|
|
45
|
+
const demo = if (try exists(b, "src/tools/demo.zig"))
|
|
46
|
+
b.option(bool, "demo", "Build the demo library") orelse false
|
|
47
|
+
else
|
|
48
|
+
false;
|
|
26
49
|
const wasm = b.option(bool, "wasm", "Build a WASM library") orelse false;
|
|
27
50
|
const wasm_stack_size =
|
|
28
51
|
b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
|
|
29
52
|
const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
|
|
30
|
-
const strip = b.option(bool, "strip", "Strip debugging symbols from binary")
|
|
31
|
-
const pic = b.option(bool, "pic", "Force position independent code")
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
53
|
+
const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
|
|
54
|
+
const pic = b.option(bool, "pic", "Force position independent code");
|
|
55
|
+
const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
|
|
56
|
+
const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
|
|
57
|
+
|
|
58
|
+
const cmd = b.findProgram(&.{"strip"}, &.{}) catch null;
|
|
59
|
+
|
|
60
|
+
const json = @embedFile("package.json");
|
|
61
|
+
var parsed = try std.json.parseFromSlice(std.json.Value, b.allocator, json, .{});
|
|
62
|
+
defer parsed.deinit();
|
|
63
|
+
const version = parsed.value.object.get("version").?.string;
|
|
64
|
+
const description = parsed.value.object.get("description").?.string;
|
|
65
|
+
var repository = std.mem.splitScalar(u8, parsed.value.object.get("repository").?.string, ':');
|
|
42
66
|
std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
|
|
43
67
|
|
|
44
68
|
const showdown =
|
|
45
69
|
b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
|
|
46
|
-
const
|
|
70
|
+
const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
|
|
71
|
+
const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
|
|
72
|
+
const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
|
|
73
|
+
const extra = b.option([]const []const u8, "option", "Enable extra option");
|
|
47
74
|
|
|
48
75
|
const options = b.addOptions();
|
|
49
76
|
options.addOption(?bool, "showdown", showdown);
|
|
50
|
-
options.addOption(?bool, "
|
|
77
|
+
options.addOption(?bool, "log", log);
|
|
78
|
+
options.addOption(?bool, "chance", chance);
|
|
79
|
+
options.addOption(?bool, "calc", calc);
|
|
80
|
+
|
|
81
|
+
if (extra) |opts| {
|
|
82
|
+
for (opts) |opt| {
|
|
83
|
+
if (std.mem.indexOfScalar(u8, opt, '=')) |i| {
|
|
84
|
+
options.addOption(?bool, opt[0..i], if (std.mem.eql(u8, opt[i + 1 ..], "true"))
|
|
85
|
+
true
|
|
86
|
+
else if (std.mem.eql(u8, opt[i + 1 ..], "false"))
|
|
87
|
+
false
|
|
88
|
+
else {
|
|
89
|
+
std.log.err("Invalid option: {s}\n", .{opt});
|
|
90
|
+
return error.InvalidOption;
|
|
91
|
+
});
|
|
92
|
+
} else {
|
|
93
|
+
options.addOption(?bool, opt, true);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
51
97
|
|
|
52
98
|
const name = if (showdown) "pkmn-showdown" else "pkmn";
|
|
53
99
|
|
|
100
|
+
const pkmn: *std.Build.Module = b.addModule("pkmn", .{
|
|
101
|
+
.root_source_file = b.path("src/lib/pkmn.zig"),
|
|
102
|
+
.optimize = optimize,
|
|
103
|
+
.target = target,
|
|
104
|
+
.imports = &.{.{ .name = "build_options", .module = options.createModule() }},
|
|
105
|
+
});
|
|
106
|
+
|
|
54
107
|
var c = false;
|
|
55
108
|
if (node_headers) |headers| {
|
|
109
|
+
const translate_c = b.addTranslateC(.{
|
|
110
|
+
.root_source_file = b.path("src/lib/napi.h"),
|
|
111
|
+
.target = target,
|
|
112
|
+
.optimize = optimize,
|
|
113
|
+
});
|
|
114
|
+
translate_c.addSystemIncludePath(b.path(headers));
|
|
56
115
|
const addon = b.fmt("{s}.node", .{name});
|
|
57
|
-
const
|
|
116
|
+
const path = b.path("src/lib/node.zig");
|
|
117
|
+
const lib = b.addLibrary(.{
|
|
118
|
+
.linkage = .dynamic,
|
|
58
119
|
.name = addon,
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
120
|
+
.root_module = b.createModule(.{
|
|
121
|
+
.root_source_file = path,
|
|
122
|
+
.optimize = optimize,
|
|
123
|
+
.target = target,
|
|
124
|
+
.strip = strip,
|
|
125
|
+
.pic = pic,
|
|
126
|
+
}),
|
|
62
127
|
});
|
|
63
|
-
lib.addOptions("build_options", options);
|
|
64
|
-
lib.
|
|
65
|
-
lib.
|
|
66
|
-
lib.linkLibC();
|
|
128
|
+
lib.root_module.addOptions("build_options", options);
|
|
129
|
+
lib.root_module.addImport("napi", translate_c.createModule());
|
|
130
|
+
lib.root_module.link_libc = true;
|
|
67
131
|
if (node_import_lib) |il| {
|
|
68
|
-
lib.addObjectFile(il);
|
|
69
|
-
} else if (
|
|
70
|
-
|
|
132
|
+
lib.root_module.addObjectFile(b.path(il));
|
|
133
|
+
} else if (target.result.os.tag == .windows) {
|
|
134
|
+
var err = std.Io.File.stderr().writer(b.graph.io, &.{});
|
|
135
|
+
try err.interface.writeAll("Must provide --node-import-library path on Windows\n");
|
|
71
136
|
std.process.exit(1);
|
|
72
137
|
}
|
|
73
138
|
lib.linker_allow_shlib_undefined = true;
|
|
74
139
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
75
|
-
if (pic) lib.force_pic = pic;
|
|
76
140
|
// Always emit to build/lib because this is where the driver code expects to find it
|
|
77
|
-
// TODO
|
|
141
|
+
// TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
|
|
78
142
|
//
|
|
79
143
|
// lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
|
|
80
144
|
// b.getInstallStep().dependOn(&lib.step);
|
|
81
145
|
//
|
|
82
146
|
// But ziglang/zig#14647 broke this so we now need to do an install() and then manually
|
|
83
147
|
// rename the file ourself in install-pkmn-engine
|
|
84
|
-
|
|
148
|
+
b.installArtifact(lib);
|
|
85
149
|
} else if (wasm) {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
|
|
94
|
-
});
|
|
95
|
-
lib.addOptions("build_options", options);
|
|
96
|
-
lib.setMainPkgPath("./");
|
|
97
|
-
lib.stack_size = wasm_stack_size;
|
|
98
|
-
lib.rdynamic = true;
|
|
99
|
-
lib.strip = strip;
|
|
100
|
-
if (pic) lib.force_pic = pic;
|
|
101
|
-
const opt = b.findProgram(
|
|
102
|
-
&[_][]const u8{"wasm-opt"},
|
|
103
|
-
&[_][]const u8{"./node_modules/.bin"},
|
|
104
|
-
) catch null;
|
|
105
|
-
if (optimize != .Debug and opt != null) {
|
|
106
|
-
const out = b.fmt("build/lib/{s}.wasm", .{name});
|
|
107
|
-
const sh = b.addSystemCommand(&[_][]const u8{ opt.?, "-O4" });
|
|
108
|
-
sh.addArtifactArg(lib);
|
|
109
|
-
sh.addArg("-o");
|
|
110
|
-
sh.addFileSourceArg(.{ .path = out });
|
|
111
|
-
b.getInstallStep().dependOn(&sh.step);
|
|
112
|
-
}
|
|
113
|
-
lib.install();
|
|
150
|
+
const path = "src/lib/wasm.zig";
|
|
151
|
+
try buildWasm(b, name, path, optimize, strip, pic, wasm_stack_size, null, options);
|
|
152
|
+
} else if (demo) {
|
|
153
|
+
const path = "src/tools/demo.zig";
|
|
154
|
+
const n = if (showdown) "demo-showdown" else "demo";
|
|
155
|
+
const mod = pkmn;
|
|
156
|
+
try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
|
|
114
157
|
} else if (dynamic) {
|
|
115
|
-
const
|
|
158
|
+
const path = b.path("src/lib/c.zig");
|
|
159
|
+
const lib = b.addLibrary(.{
|
|
160
|
+
.linkage = .dynamic,
|
|
116
161
|
.name = name,
|
|
117
|
-
.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
162
|
+
.root_module = b.createModule(.{
|
|
163
|
+
.root_source_file = path,
|
|
164
|
+
.optimize = optimize,
|
|
165
|
+
.target = target,
|
|
166
|
+
.strip = strip,
|
|
167
|
+
.pic = pic,
|
|
168
|
+
}),
|
|
121
169
|
});
|
|
122
|
-
lib.addOptions("build_options", options);
|
|
123
|
-
lib.
|
|
124
|
-
lib.addIncludePath("src/include");
|
|
170
|
+
lib.root_module.addOptions("build_options", options);
|
|
171
|
+
lib.root_module.addIncludePath(b.path("src/include"));
|
|
125
172
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
126
|
-
|
|
127
|
-
lib.install();
|
|
173
|
+
b.installArtifact(lib);
|
|
128
174
|
c = true;
|
|
129
175
|
} else {
|
|
130
|
-
const
|
|
176
|
+
const path = b.path("src/lib/c.zig");
|
|
177
|
+
const lib = b.addLibrary(.{
|
|
178
|
+
.linkage = .static,
|
|
131
179
|
.name = name,
|
|
132
|
-
.
|
|
133
|
-
|
|
134
|
-
|
|
180
|
+
.root_module = b.createModule(.{
|
|
181
|
+
.root_source_file = path,
|
|
182
|
+
.optimize = optimize,
|
|
183
|
+
.target = target,
|
|
184
|
+
.strip = strip,
|
|
185
|
+
.pic = pic,
|
|
186
|
+
}),
|
|
135
187
|
});
|
|
136
|
-
lib.addOptions("build_options", options);
|
|
137
|
-
lib.
|
|
138
|
-
|
|
139
|
-
|
|
188
|
+
lib.root_module.addOptions("build_options", options);
|
|
189
|
+
lib.root_module.addIncludePath(b.path("src/include"));
|
|
190
|
+
if (target.result.os.tag != .macos) {
|
|
191
|
+
lib.bundle_compiler_rt = true;
|
|
192
|
+
}
|
|
140
193
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
141
|
-
if (
|
|
142
|
-
|
|
194
|
+
if (emit_asm) {
|
|
195
|
+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
196
|
+
lib.getEmittedAsm(),
|
|
197
|
+
.prefix,
|
|
198
|
+
b.fmt("{s}.s", .{name}),
|
|
199
|
+
).step);
|
|
200
|
+
}
|
|
201
|
+
if (emit_ll) {
|
|
202
|
+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
203
|
+
lib.getEmittedLlvmIr(),
|
|
204
|
+
.prefix,
|
|
205
|
+
b.fmt("{s}.ll", .{name}),
|
|
206
|
+
).step);
|
|
207
|
+
}
|
|
208
|
+
b.installArtifact(lib);
|
|
143
209
|
c = true;
|
|
144
210
|
}
|
|
145
211
|
|
|
146
212
|
if (c) {
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
.header,
|
|
150
|
-
"pkmn.h",
|
|
151
|
-
);
|
|
213
|
+
const path = b.path("src/include/pkmn.h");
|
|
214
|
+
const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
|
|
152
215
|
b.getInstallStep().dependOn(&header.step);
|
|
153
216
|
|
|
154
217
|
const pc = b.fmt("lib{s}.pc", .{name});
|
|
155
|
-
const
|
|
156
|
-
const
|
|
218
|
+
const cwd = try std.process.currentPathAlloc(b.graph.io, b.allocator);
|
|
219
|
+
const file = try std.Io.Dir.path.relative(
|
|
220
|
+
b.allocator,
|
|
221
|
+
cwd,
|
|
222
|
+
&b.graph.environ_map,
|
|
223
|
+
cwd,
|
|
224
|
+
try b.cache_root.join(b.allocator, &.{pc}),
|
|
225
|
+
);
|
|
226
|
+
const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
|
|
227
|
+
defer pkgconfig_file.close(b.graph.io);
|
|
157
228
|
|
|
158
229
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
159
|
-
|
|
160
|
-
try writer.print(
|
|
230
|
+
var writer = pkgconfig_file.writer(b.graph.io, &.{});
|
|
231
|
+
try writer.interface.print(
|
|
161
232
|
\\prefix={0s}/{1s}
|
|
162
233
|
\\includedir=${{prefix}}/include
|
|
163
234
|
\\libdir=${{prefix}}/lib
|
|
@@ -169,26 +240,31 @@ pub fn build(b: *std.Build) !void {
|
|
|
169
240
|
\\Cflags: -I${{includedir}}
|
|
170
241
|
\\Libs: -L${{libdir}} -l{2s}
|
|
171
242
|
, .{ dirname, b.install_path, name, repository.next().?, description, version });
|
|
172
|
-
defer pkgconfig_file.close();
|
|
173
243
|
|
|
174
244
|
b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
|
|
175
245
|
}
|
|
176
246
|
|
|
177
|
-
const config = .{
|
|
247
|
+
const config: Config = .{
|
|
178
248
|
.target = target,
|
|
179
249
|
.optimize = optimize,
|
|
180
250
|
.pic = pic,
|
|
181
251
|
.strip = strip,
|
|
182
252
|
.cmd = cmd,
|
|
183
253
|
};
|
|
254
|
+
|
|
255
|
+
// TODO: tests can be run multiple times due to @imports
|
|
184
256
|
const tests = TestStep.create(b, options, config);
|
|
185
257
|
|
|
186
|
-
var exes
|
|
258
|
+
var exes: ArrayList(*std.Build.Step.Compile) = .empty;
|
|
187
259
|
const tools: ToolConfig = .{
|
|
260
|
+
.options = .{
|
|
261
|
+
.showdown = showdown,
|
|
262
|
+
.log = log,
|
|
263
|
+
.chance = chance,
|
|
264
|
+
.calc = calc,
|
|
265
|
+
},
|
|
188
266
|
.general = config,
|
|
189
267
|
.tool = .{
|
|
190
|
-
.showdown = showdown,
|
|
191
|
-
.trace = trace,
|
|
192
268
|
.tests = if (tests.build) tests else null,
|
|
193
269
|
.exes = &exes,
|
|
194
270
|
},
|
|
@@ -197,155 +273,254 @@ pub fn build(b: *std.Build) !void {
|
|
|
197
273
|
var benchmark_config = tools;
|
|
198
274
|
benchmark_config.general.optimize = .ReleaseFast;
|
|
199
275
|
benchmark_config.general.strip = true;
|
|
200
|
-
const benchmark = tool(b, "src/test/benchmark.zig", benchmark_config);
|
|
276
|
+
const benchmark = try tool(b, "src/test/benchmark.zig", benchmark_config);
|
|
201
277
|
|
|
202
278
|
var fuzz_config = tools;
|
|
203
279
|
fuzz_config.general.strip = false;
|
|
204
280
|
fuzz_config.tool.name = "fuzz";
|
|
205
|
-
const fuzz = tool(b, "src/test/
|
|
281
|
+
const fuzz = try tool(b, "src/test/fuzz.zig", fuzz_config);
|
|
282
|
+
|
|
283
|
+
const analyze = try tool(b, "src/tools/analyze.zig", tools);
|
|
284
|
+
const dump = try tool(b, "src/tools/dump.zig", tools);
|
|
285
|
+
const transitions = try tool(b, "src/tools/transitions.zig", tools);
|
|
286
|
+
|
|
287
|
+
// FIXME: serde randomly fails to build in some release configurations
|
|
288
|
+
var hack = tools;
|
|
289
|
+
if (optimize != .Debug) hack.tool.tests = null;
|
|
290
|
+
const serde = try tool(b, "src/tools/serde.zig", hack);
|
|
291
|
+
|
|
292
|
+
if (analyze) |t| b.step("analyze", "Run LLVM analysis tool").dependOn(&t.step);
|
|
293
|
+
if (benchmark) |t| b.step("benchmark", "Run benchmark code").dependOn(&t.step);
|
|
294
|
+
if (dump) |t| b.step("dump", "Run protocol dump tool").dependOn(&t.step);
|
|
295
|
+
if (fuzz) |t| b.step("fuzz", "Run fuzz tester").dependOn(&t.step);
|
|
296
|
+
if (serde) |t| b.step("serde", "Run serialization/deserialization tool").dependOn(&t.step);
|
|
297
|
+
b.step("test", "Run all tests").dependOn(&tests.step);
|
|
298
|
+
b.step("tools", "Install tools").dependOn(&ToolsStep.create(b, &exes).step);
|
|
299
|
+
if (transitions) |t| {
|
|
300
|
+
b.step("transitions", "Visualize transitions algorithm search").dependOn(&t.step);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
206
303
|
|
|
207
|
-
|
|
208
|
-
|
|
304
|
+
fn buildWasm(
|
|
305
|
+
b: *std.Build,
|
|
306
|
+
name: []const u8,
|
|
307
|
+
root_src_file: []const u8,
|
|
308
|
+
optimize: std.builtin.OptimizeMode,
|
|
309
|
+
strip: ?bool,
|
|
310
|
+
pic: ?bool,
|
|
311
|
+
wasm_stack_size: u64,
|
|
312
|
+
import: ?*std.Build.Module,
|
|
313
|
+
options: anytype,
|
|
314
|
+
) !void {
|
|
315
|
+
const mode = switch (optimize) {
|
|
316
|
+
.ReleaseFast, .ReleaseSafe => .ReleaseSmall,
|
|
317
|
+
else => optimize,
|
|
318
|
+
};
|
|
319
|
+
// https://webassembly.org/features/
|
|
320
|
+
var features = std.Target.wasm.featureSet(&.{
|
|
321
|
+
.atomics,
|
|
322
|
+
.bulk_memory,
|
|
323
|
+
// .exception_handling,
|
|
324
|
+
.extended_const,
|
|
325
|
+
// .half_precision,
|
|
326
|
+
// .multimemory,
|
|
327
|
+
.mutable_globals,
|
|
328
|
+
.nontrapping_fptoint,
|
|
329
|
+
.reference_types,
|
|
330
|
+
// .relaxed_simd,
|
|
331
|
+
.sign_ext,
|
|
332
|
+
.simd128,
|
|
333
|
+
// .tail_call,
|
|
334
|
+
});
|
|
335
|
+
if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
|
|
336
|
+
features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
|
|
337
|
+
}
|
|
338
|
+
const freestanding = b.resolveTargetQuery(.{
|
|
339
|
+
.cpu_arch = .wasm32,
|
|
340
|
+
.os_tag = .freestanding,
|
|
341
|
+
.cpu_features_add = features,
|
|
342
|
+
});
|
|
343
|
+
const path = b.path(root_src_file);
|
|
344
|
+
const exe = b.addExecutable(.{
|
|
345
|
+
.name = name,
|
|
346
|
+
.root_module = b.createModule(.{
|
|
347
|
+
.root_source_file = path,
|
|
348
|
+
.optimize = mode,
|
|
349
|
+
.target = freestanding,
|
|
350
|
+
.strip = strip,
|
|
351
|
+
.pic = pic,
|
|
352
|
+
}),
|
|
353
|
+
});
|
|
209
354
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const
|
|
355
|
+
var file = try std.Io.Dir.cwd().openFile(b.graph.io, root_src_file, .{});
|
|
356
|
+
defer file.close(b.graph.io);
|
|
357
|
+
var reader = file.reader(b.graph.io, &.{});
|
|
358
|
+
const bytes = try reader.interface.allocRemaining(b.allocator, .unlimited);
|
|
359
|
+
exe.root_module.export_symbol_names = try exports(b, bytes);
|
|
360
|
+
exe.entry = .disabled;
|
|
214
361
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
b.
|
|
362
|
+
if (import) |i| {
|
|
363
|
+
exe.root_module.addImport("pkmn", i);
|
|
364
|
+
}
|
|
365
|
+
exe.stack_size = wasm_stack_size;
|
|
366
|
+
exe.root_module.addOptions("build_options", options);
|
|
367
|
+
|
|
368
|
+
const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
|
|
369
|
+
if (optimize != .Debug and opt != null) {
|
|
370
|
+
const out = b.fmt("build/lib/{s}.wasm", .{name});
|
|
371
|
+
const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
|
|
372
|
+
sh.addArtifactArg(exe);
|
|
373
|
+
sh.addArg("-o");
|
|
374
|
+
sh.addFileArg(b.path(out));
|
|
375
|
+
b.getInstallStep().dependOn(&sh.step);
|
|
376
|
+
} else {
|
|
377
|
+
b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
|
|
378
|
+
.dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
|
|
379
|
+
}).step);
|
|
380
|
+
}
|
|
222
381
|
}
|
|
223
382
|
|
|
224
383
|
fn maybeStrip(
|
|
225
384
|
b: *std.Build,
|
|
226
|
-
artifact: *std.Build.
|
|
385
|
+
artifact: *std.Build.Step.Compile,
|
|
227
386
|
step: *std.Build.Step,
|
|
228
|
-
strip: bool,
|
|
387
|
+
strip: ?bool,
|
|
229
388
|
cmd: ?[]const u8,
|
|
230
389
|
) void {
|
|
231
|
-
|
|
232
|
-
if (!strip or cmd == null) return;
|
|
390
|
+
if (!(strip orelse false) or cmd == null) return;
|
|
233
391
|
// Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
|
|
234
392
|
const mac = builtin.os.tag == .macos;
|
|
235
393
|
if (mac and artifact.isDynamicLibrary()) return;
|
|
236
394
|
// Assuming GNU strip, which complains "illegal pathname found in archive member"...
|
|
237
395
|
if (!mac and artifact.isStaticLibrary()) return;
|
|
238
|
-
const sh = b.addSystemCommand(
|
|
396
|
+
const sh = b.addSystemCommand(&.{ cmd.?, if (mac) "-x" else "-s" });
|
|
239
397
|
sh.addArtifactArg(artifact);
|
|
240
398
|
step.dependOn(&sh.step);
|
|
241
399
|
}
|
|
242
400
|
|
|
243
401
|
const Config = struct {
|
|
244
|
-
target: std.
|
|
402
|
+
target: std.Build.ResolvedTarget,
|
|
245
403
|
optimize: std.builtin.OptimizeMode,
|
|
246
|
-
pic: bool,
|
|
247
|
-
strip: bool,
|
|
404
|
+
pic: ?bool,
|
|
405
|
+
strip: ?bool,
|
|
248
406
|
cmd: ?[]const u8,
|
|
249
407
|
};
|
|
250
408
|
|
|
251
|
-
const TESTS = [_][]const u8{
|
|
252
|
-
"src/lib/common/test.zig",
|
|
253
|
-
"src/lib/gen1/test.zig",
|
|
254
|
-
"src/lib/gen2/test.zig",
|
|
255
|
-
};
|
|
256
|
-
|
|
257
409
|
const TestStep = struct {
|
|
258
410
|
step: std.Build.Step,
|
|
259
411
|
build: bool,
|
|
260
412
|
|
|
261
|
-
pub fn create(b: *std.Build, options: *std.Build.
|
|
413
|
+
pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
|
|
262
414
|
const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
|
|
263
|
-
const test_file = b.option([]const u8, "test-file", "Input file for test");
|
|
264
|
-
const test_bin = b.option([]const u8, "test-bin", "Emit test binary to");
|
|
265
415
|
const test_filter =
|
|
266
416
|
b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
|
267
|
-
const test_no_exec =
|
|
268
|
-
b.option(bool, "test-no-exec", "Compiles test binary without running it") orelse false;
|
|
269
417
|
|
|
270
418
|
const self = b.allocator.create(TestStep) catch @panic("OOM");
|
|
271
419
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
|
|
272
|
-
self.* =
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
.name = std.fs.path.basename(std.fs.path.dirname(path).?),
|
|
279
|
-
.root_source_file = .{ .path = path },
|
|
420
|
+
self.* = .{ .step = step, .build = test_filter == null };
|
|
421
|
+
|
|
422
|
+
const path = b.path("src/lib/test.zig");
|
|
423
|
+
const tests = b.addTest(.{
|
|
424
|
+
.root_module = b.createModule(.{
|
|
425
|
+
.root_source_file = path,
|
|
280
426
|
.optimize = config.optimize,
|
|
281
427
|
.target = config.target,
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
if (coverage) |c| {
|
|
294
|
-
tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
|
|
295
|
-
}
|
|
296
|
-
self.step.dependOn(if (test_no_exec) &tests.step else &tests.run().step);
|
|
428
|
+
.single_threaded = true,
|
|
429
|
+
.strip = config.strip,
|
|
430
|
+
.pic = config.pic,
|
|
431
|
+
}),
|
|
432
|
+
.filters = if (test_filter) |filter| &.{filter} else &.{},
|
|
433
|
+
});
|
|
434
|
+
tests.root_module.addOptions("build_options", options);
|
|
435
|
+
|
|
436
|
+
maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
|
|
437
|
+
if (coverage) |c| {
|
|
438
|
+
tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
|
|
297
439
|
}
|
|
440
|
+
self.step.dependOn(&b.addRunArtifact(tests).step);
|
|
441
|
+
|
|
298
442
|
return self;
|
|
299
443
|
}
|
|
300
444
|
};
|
|
301
445
|
|
|
302
446
|
const ToolConfig = struct {
|
|
447
|
+
options: Options,
|
|
303
448
|
general: Config,
|
|
304
449
|
tool: struct {
|
|
305
|
-
showdown: bool,
|
|
306
|
-
trace: bool,
|
|
307
450
|
tests: ?*TestStep,
|
|
308
451
|
name: ?[]const u8 = null,
|
|
309
|
-
exes: *
|
|
452
|
+
exes: *ArrayList(*std.Build.Step.Compile),
|
|
310
453
|
},
|
|
311
454
|
};
|
|
312
455
|
|
|
313
|
-
fn tool(b: *std.Build, path: []const u8, config: ToolConfig)
|
|
456
|
+
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
|
|
457
|
+
if (!try exists(b, path)) return null;
|
|
314
458
|
var name = config.tool.name orelse std.fs.path.basename(path);
|
|
315
459
|
const index = std.mem.lastIndexOfScalar(u8, name, '.');
|
|
316
460
|
if (index) |i| name = name[0..i];
|
|
317
|
-
if (config.
|
|
461
|
+
if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
|
|
318
462
|
|
|
319
463
|
const exe = b.addExecutable(.{
|
|
320
464
|
.name = name,
|
|
321
|
-
.
|
|
322
|
-
|
|
323
|
-
|
|
465
|
+
.root_module = b.createModule(.{
|
|
466
|
+
.root_source_file = b.path(path),
|
|
467
|
+
.target = config.general.target,
|
|
468
|
+
.optimize = config.general.optimize,
|
|
469
|
+
.strip = config.general.strip,
|
|
470
|
+
.pic = config.general.pic,
|
|
471
|
+
}),
|
|
324
472
|
});
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
473
|
+
|
|
474
|
+
const import = module(b, config.options, config.general.target, config.general.optimize);
|
|
475
|
+
exe.root_module.addImport("pkmn", import);
|
|
476
|
+
if (config.general.target.result.os.tag == .windows) {
|
|
477
|
+
exe.root_module.linkSystemLibrary("advapi32", .{});
|
|
478
|
+
}
|
|
479
|
+
|
|
331
480
|
if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
|
|
332
|
-
config.tool.exes.append(exe) catch @panic("OOM");
|
|
481
|
+
config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
|
|
333
482
|
|
|
334
|
-
const run =
|
|
483
|
+
const run = b.addRunArtifact(exe);
|
|
335
484
|
maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
|
|
336
485
|
if (b.args) |args| run.addArgs(args);
|
|
337
486
|
|
|
338
487
|
return run;
|
|
339
488
|
}
|
|
340
489
|
|
|
490
|
+
fn exists(b: *std.Build, path: []const u8) !bool {
|
|
491
|
+
std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch |err| switch (err) {
|
|
492
|
+
error.FileNotFound => return false,
|
|
493
|
+
else => |e| return e,
|
|
494
|
+
};
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
|
|
341
498
|
const ToolsStep = struct {
|
|
342
499
|
step: std.Build.Step,
|
|
343
500
|
|
|
344
|
-
pub fn create(b: *std.Build, exes: *
|
|
501
|
+
pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
|
|
345
502
|
const self = b.allocator.create(ToolsStep) catch @panic("OOM");
|
|
346
503
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
|
|
347
|
-
self.* =
|
|
348
|
-
for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t).step);
|
|
504
|
+
self.* = .{ .step = step };
|
|
505
|
+
for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t, .{}).step);
|
|
349
506
|
return self;
|
|
350
507
|
}
|
|
351
508
|
};
|
|
509
|
+
|
|
510
|
+
pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
|
|
511
|
+
var symbols: ArrayList([]const u8) = .empty;
|
|
512
|
+
|
|
513
|
+
var it = std.mem.splitSequence(u8, bytes, "export ");
|
|
514
|
+
_ = it.next();
|
|
515
|
+
while (it.next()) |s| {
|
|
516
|
+
if (std.mem.startsWith(u8, s, "const ")) {
|
|
517
|
+
const i = std.mem.indexOf(u8, s[6..], " ").?;
|
|
518
|
+
try symbols.append(b.allocator, s[6 .. 6 + i]);
|
|
519
|
+
} else { // "fn "
|
|
520
|
+
const i = std.mem.indexOf(u8, s[3..], "(").?;
|
|
521
|
+
try symbols.append(b.allocator, s[3 .. 3 + i]);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return symbols.items;
|
|
526
|
+
}
|