@pkmn/engine 0.1.0-dev.3393edb3 → 0.1.0-dev.3846c8e6

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