@pkmn/engine 0.1.0-dev.683fba11 → 0.1.0-dev.6ba42cd5

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