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