@pkmn/engine 0.1.0-dev.30895c5a → 0.1.0-dev.3158fa12

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