@pkmn/engine 0.1.0-dev.36b5d56b → 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 (61) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +28 -13
  3. package/build/pkg/addon/browser.js +4 -1
  4. package/build/pkg/addon/browser.js.map +1 -1
  5. package/build/pkg/addon/node.js +20 -7
  6. package/build/pkg/addon/node.js.map +1 -1
  7. package/build/pkg/addon/wasm.js +41 -9
  8. package/build/pkg/addon/wasm.js.map +1 -1
  9. package/build/pkg/addon.d.ts +7 -7
  10. package/build/pkg/addon.js +8 -17
  11. package/build/pkg/addon.js.map +1 -1
  12. package/build/pkg/data/layout.json +15 -2
  13. package/build/pkg/data/protocol.json +23 -9
  14. package/build/pkg/gen1.d.ts +15 -18
  15. package/build/pkg/gen1.js +28 -19
  16. package/build/pkg/gen1.js.map +1 -1
  17. package/build/pkg/index.d.ts +37 -100
  18. package/build/pkg/index.js +23 -75
  19. package/build/pkg/index.js.map +1 -1
  20. package/build/pkg/protocol.d.ts +11 -2
  21. package/build/pkg/protocol.js.map +1 -1
  22. package/build/tools/debug.js +54 -11
  23. package/build/tools/debug.js.map +1 -1
  24. package/build/tools/display/debug.jsx +19 -94
  25. package/build/tools/display/debug.jsx.map +1 -1
  26. package/build/tools/display/index.js +18 -7
  27. package/build/tools/display/index.js.map +1 -1
  28. package/build/tools/display/ui.jsx +20 -9
  29. package/build/tools/display/ui.jsx.map +1 -1
  30. package/build/tools/display/util.js +140 -1
  31. package/build/tools/display/util.js.map +1 -1
  32. package/build.zig +200 -201
  33. package/package.json +31 -130
  34. package/src/bin/install-pkmn-engine +25 -9
  35. package/src/lib/bindings/c.zig +200 -0
  36. package/src/lib/bindings/node.zig +117 -0
  37. package/src/lib/bindings/wasm.zig +50 -0
  38. package/src/lib/common/array.zig +5 -12
  39. package/src/lib/common/debug.zig +33 -59
  40. package/src/lib/common/js.zig +3 -9
  41. package/src/lib/common/optional.zig +11 -21
  42. package/src/lib/common/protocol.zig +224 -189
  43. package/src/lib/common/rational.zig +275 -284
  44. package/src/lib/common/util.zig +14 -31
  45. package/src/lib/gen1/calc.zig +139 -117
  46. package/src/lib/gen1/chance.zig +112 -75
  47. package/src/lib/gen1/data/types.zig +2 -2
  48. package/src/lib/gen1/data.zig +5 -5
  49. package/src/lib/gen1/helpers.zig +8 -10
  50. package/src/lib/gen1/mechanics.zig +50 -43
  51. package/src/lib/gen2/calc.zig +3 -6
  52. package/src/lib/gen2/chance.zig +12 -18
  53. package/src/lib/gen2/data/types.zig +6 -3
  54. package/src/lib/gen2/data.zig +8 -7
  55. package/src/lib/gen2/helpers.zig +4 -6
  56. package/src/lib/gen2/mechanics.zig +36 -25
  57. package/src/lib/napi.h +2 -0
  58. package/src/lib/node.zig +3 -118
  59. package/src/lib/pkmn.zig +94 -19
  60. package/src/lib/wasm.zig +10 -9
  61. package/src/tools/display/dom.ts +46 -34
package/build.zig CHANGED
@@ -1,6 +1,10 @@
1
1
  const builtin = @import("builtin");
2
2
  const std = @import("std");
3
3
 
4
+ fn ArrayList(comptime T: type) type {
5
+ return std.array_list.Aligned(T, null);
6
+ }
7
+
4
8
  pub const Options = struct {
5
9
  showdown: ?bool = null,
6
10
  log: ?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,50 +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
- try buildWasm(b, name, "src/lib/wasm.zig", optimize, strip, pic, wasm_stack_size, options);
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);
135
147
  } else if (dynamic) {
136
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
137
- const lib = b.addSharedLibrary(if (force_pic) .{
138
- .name = name,
139
- .root_source_file = path,
140
- .version = try std.SemanticVersion.parse(version),
141
- .optimize = optimize,
142
- .target = target,
143
- } else .{
148
+ const path = b.path("src/lib/c.zig");
149
+ const lib = b.addLibrary(.{
150
+ .linkage = .dynamic,
144
151
  .name = name,
145
- .root_source_file = path,
146
- .version = try std.SemanticVersion.parse(version),
147
- .optimize = optimize,
148
- .target = target,
149
- .strip = strip,
150
- .pic = pic,
152
+ .root_module = b.createModule(.{
153
+ .root_source_file = path,
154
+ .optimize = optimize,
155
+ .target = target,
156
+ .strip = strip,
157
+ .pic = pic,
158
+ }),
151
159
  });
152
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
153
- 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"));
154
162
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
155
- if (force_pic and pic orelse false) lib.force_pic = pic;
156
163
  b.installArtifact(lib);
157
164
  c = true;
158
165
  } else {
159
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
160
- const lib = b.addStaticLibrary(if (force_pic) .{
161
- .name = name,
162
- .root_source_file = path,
163
- .optimize = optimize,
164
- .target = target,
165
- } else .{
166
+ const path = b.path("src/lib/c.zig");
167
+ const lib = b.addLibrary(.{
168
+ .linkage = .static,
166
169
  .name = name,
167
- .root_source_file = path,
168
- .optimize = optimize,
169
- .target = target,
170
- .strip = strip,
171
- .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
+ }),
172
177
  });
173
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
174
- 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"));
175
180
  lib.bundle_compiler_rt = true;
176
181
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
177
- if (force_pic and pic orelse false) lib.force_pic = pic;
178
182
  if (emit_asm) {
179
183
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
180
184
  lib.getEmittedAsm(),
@@ -194,22 +198,25 @@ pub fn build(b: *std.Build) !void {
194
198
  }
195
199
 
196
200
  if (c) {
197
- const path =
198
- if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
201
+ const path = b.path("src/include/pkmn.h");
199
202
  const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
200
203
  b.getInstallStep().dependOn(&header.step);
201
204
 
202
205
  const pc = b.fmt("lib{s}.pc", .{name});
203
- 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(
204
208
  b.allocator,
205
- try std.process.getCwdAlloc(b.allocator),
209
+ cwd,
210
+ &b.graph.environ_map,
211
+ cwd,
206
212
  try b.cache_root.join(b.allocator, &.{pc}),
207
213
  );
208
- 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);
209
216
 
210
217
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
211
- const writer = pkgconfig_file.writer();
212
- try writer.print(
218
+ var writer = pkgconfig_file.writer(b.graph.io, &.{});
219
+ try writer.interface.print(
213
220
  \\prefix={0s}/{1s}
214
221
  \\includedir=${{prefix}}/include
215
222
  \\libdir=${{prefix}}/lib
@@ -221,12 +228,11 @@ pub fn build(b: *std.Build) !void {
221
228
  \\Cflags: -I${{includedir}}
222
229
  \\Libs: -L${{libdir}} -l{2s}
223
230
  , .{ dirname, b.install_path, name, repository.next().?, description, version });
224
- defer pkgconfig_file.close();
225
231
 
226
232
  b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
227
233
  }
228
234
 
229
- const config = .{
235
+ const config: Config = .{
230
236
  .target = target,
231
237
  .optimize = optimize,
232
238
  .pic = pic,
@@ -237,7 +243,7 @@ pub fn build(b: *std.Build) !void {
237
243
  // TODO: tests can be run multiple times due to @imports
238
244
  const tests = TestStep.create(b, options, config);
239
245
 
240
- var exes = std.ArrayList(*Step.Compile).init(b.allocator);
246
+ var exes: ArrayList(*std.Build.Step.Compile) = .empty;
241
247
  const tools: ToolConfig = .{
242
248
  .options = .{
243
249
  .showdown = showdown,
@@ -292,65 +298,72 @@ fn buildWasm(
292
298
  strip: ?bool,
293
299
  pic: ?bool,
294
300
  wasm_stack_size: u64,
301
+ import: ?*std.Build.Module,
295
302
  options: anytype,
296
303
  ) !void {
297
- const entry = @hasDecl(Step.Compile, "Entry");
298
304
  const mode = switch (optimize) {
299
305
  .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
300
306
  else => optimize,
301
307
  };
302
- const freestanding = if (@hasDecl(std.Build, "resolveTargetQuery"))
303
- b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding })
304
- else
305
- ResolvedTarget{ .cpu_arch = .wasm32, .os_tag = .freestanding };
306
- const path = if (has_path) .{ .path = root_src_file } else b.path(root_src_file);
307
- const bin = if (entry) bin: {
308
- const exe = b.addExecutable(if (force_pic) .{
309
- .name = name,
310
- .root_source_file = path,
311
- .optimize = mode,
312
- .target = freestanding,
313
- } else .{
314
- .name = name,
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(.{
315
336
  .root_source_file = path,
316
337
  .optimize = mode,
317
338
  .target = freestanding,
318
339
  .strip = strip,
319
340
  .pic = pic,
320
- });
321
- (if (root_module) exe.root_module else exe).export_symbol_names = try exports(b);
322
- exe.entry = .disabled;
323
- break :bin exe;
324
- } else bin: {
325
- const lib = b.addSharedLibrary(.{
326
- .name = name,
327
- .root_source_file = path,
328
- .optimize = mode,
329
- .target = freestanding,
330
- });
331
- lib.rdynamic = true;
332
- break :bin lib;
333
- };
334
- bin.stack_size = wasm_stack_size;
335
- if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
336
- if (force_pic and pic orelse false) bin.force_pic = pic;
337
- (if (root_module) bin.root_module else bin).addOptions("build_options", options);
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);
338
356
 
339
357
  const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
340
358
  if (optimize != .Debug and opt != null) {
341
359
  const out = b.fmt("build/lib/{s}.wasm", .{name});
342
- const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
343
- sh.addArtifactArg(bin);
360
+ const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
361
+ sh.addArtifactArg(exe);
344
362
  sh.addArg("-o");
345
- if (@hasDecl(@TypeOf(sh.*), "addFileSourceArg")) {
346
- sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
347
- } else {
348
- sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
349
- }
363
+ sh.addFileArg(b.path(out));
350
364
  b.getInstallStep().dependOn(&sh.step);
351
- if (!entry) b.installArtifact(bin);
352
- } else if (entry) {
353
- b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
365
+ } else {
366
+ b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
354
367
  .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
355
368
  }).step);
356
369
  }
@@ -358,12 +371,11 @@ fn buildWasm(
358
371
 
359
372
  fn maybeStrip(
360
373
  b: *std.Build,
361
- artifact: *Step.Compile,
374
+ artifact: *std.Build.Step.Compile,
362
375
  step: *std.Build.Step,
363
376
  strip: ?bool,
364
377
  cmd: ?[]const u8,
365
378
  ) void {
366
- if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
367
379
  if (!(strip orelse false) or cmd == null) return;
368
380
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
369
381
  const mac = builtin.os.tag == .macos;
@@ -376,7 +388,7 @@ fn maybeStrip(
376
388
  }
377
389
 
378
390
  const Config = struct {
379
- target: ResolvedTarget,
391
+ target: std.Build.ResolvedTarget,
380
392
  optimize: std.builtin.OptimizeMode,
381
393
  pic: ?bool,
382
394
  strip: ?bool,
@@ -387,7 +399,7 @@ const TestStep = struct {
387
399
  step: std.Build.Step,
388
400
  build: bool,
389
401
 
390
- 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 {
391
403
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
392
404
  const test_filter =
393
405
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@@ -396,28 +408,21 @@ const TestStep = struct {
396
408
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
397
409
  self.* = .{ .step = step, .build = test_filter == null };
398
410
 
399
- const path = if (has_path) .{ .path = "src/lib/test.zig" } else b.path("src/lib/test.zig");
400
- const tests = b.addTest(if (force_pic) .{
401
- .root_source_file = path,
402
- .optimize = config.optimize,
403
- .target = config.target,
404
- .filter = test_filter,
405
- } else .{
406
- .root_source_file = path,
407
- .optimize = config.optimize,
408
- .target = config.target,
409
- .filter = test_filter,
410
- .single_threaded = true,
411
- .strip = config.strip,
412
- .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 &.{},
413
422
  });
414
- (if (root_module) tests.root_module else tests).addOptions("build_options", options);
423
+ tests.root_module.addOptions("build_options", options);
415
424
 
416
425
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
417
- if (force_pic) {
418
- tests.single_threaded = true;
419
- if (config.pic orelse false) tests.force_pic = config.pic;
420
- }
421
426
  if (coverage) |c| {
422
427
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
423
428
  }
@@ -434,48 +439,36 @@ const ToolConfig = struct {
434
439
  tool: struct {
435
440
  tests: ?*TestStep,
436
441
  name: ?[]const u8 = null,
437
- exes: *std.ArrayList(*Step.Compile),
442
+ exes: *ArrayList(*std.Build.Step.Compile),
438
443
  },
439
444
  };
440
445
 
441
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
442
- std.fs.cwd().access(path, .{}) catch |err| switch (err) {
443
- error.FileNotFound => return null,
444
- else => |e| return e,
445
- };
446
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
447
+ if (!try exists(b, path)) return null;
446
448
  var name = config.tool.name orelse std.fs.path.basename(path);
447
449
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
448
450
  if (index) |i| name = name[0..i];
449
451
  if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
450
452
 
451
- const exe = b.addExecutable(if (force_pic) .{
452
- .name = name,
453
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
454
- .target = config.general.target,
455
- .optimize = config.general.optimize,
456
- } else .{
453
+ const exe = b.addExecutable(.{
457
454
  .name = name,
458
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
459
- .target = config.general.target,
460
- .optimize = config.general.optimize,
461
- .single_threaded = true,
462
- .strip = config.general.strip,
463
- .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
+ }),
464
462
  });
465
463
 
466
- if (root_module) {
467
- const import = if (config.module) |m| m else module(b, config.options);
468
- exe.root_module.addImport("pkmn", import);
469
- } else {
470
- 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", .{});
471
468
  }
472
469
 
473
- if (force_pic) {
474
- exe.single_threaded = true;
475
- if (config.general.pic orelse false) exe.force_pic = config.general.pic;
476
- }
477
470
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
478
- config.tool.exes.append(exe) catch @panic("OOM");
471
+ config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
479
472
 
480
473
  const run = b.addRunArtifact(exe);
481
474
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
@@ -484,10 +477,18 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
484
477
  return run;
485
478
  }
486
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
+
487
488
  const ToolsStep = struct {
488
489
  step: std.Build.Step,
489
490
 
490
- 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 {
491
492
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
492
493
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
493
494
  self.* = .{ .step = step };
@@ -496,21 +497,19 @@ const ToolsStep = struct {
496
497
  }
497
498
  };
498
499
 
499
- fn detect(target: anytype) std.Target {
500
- if (@hasField(@TypeOf(target), "result")) return target.result;
501
- return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
502
- }
503
-
504
- const WASM = @embedFile("src/lib/wasm.zig");
500
+ pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
501
+ var symbols: ArrayList([]const u8) = .empty;
505
502
 
506
- fn exports(b: *std.Build) ![][]const u8 {
507
- var symbols = std.ArrayList([]const u8).init(b.allocator);
508
-
509
- var it = std.mem.splitSequence(u8, WASM, "export const ");
503
+ var it = std.mem.splitSequence(u8, bytes, "export ");
510
504
  _ = it.next();
511
505
  while (it.next()) |s| {
512
- const i = std.mem.indexOf(u8, s, " ").?;
513
- try symbols.append(s[0..i]);
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
+ }
514
513
  }
515
514
 
516
515
  return symbols.items;