@pkmn/engine 0.1.0-dev.3a62fd35 → 0.1.0-dev.3ed876db

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 (76) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +15 -11
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +44 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +97 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +21 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +22 -0
  13. package/build/pkg/addon.js +35 -58
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data.d.ts +25 -4
  16. package/build/pkg/data.js.map +1 -1
  17. package/build/pkg/gen1.js +1 -1
  18. package/build/pkg/gen1.js.map +1 -1
  19. package/build/pkg/index.d.ts +23 -9
  20. package/build/pkg/index.js +17 -11
  21. package/build/pkg/index.js.map +1 -1
  22. package/build/pkg/protocol.d.ts +20 -1
  23. package/build/pkg/protocol.js +109 -107
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +36 -70
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +141 -0
  28. package/build/tools/display/debug.jsx.map +1 -0
  29. package/build/tools/display/index.js +141 -0
  30. package/build/tools/display/index.js.map +1 -0
  31. package/build/tools/display/ui.jsx +349 -0
  32. package/build/tools/display/ui.jsx.map +1 -0
  33. package/build/tools/display/util.js +18 -0
  34. package/build/tools/display/util.js.map +1 -0
  35. package/build.zig +237 -83
  36. package/package.json +53 -26
  37. package/src/bin/install-pkmn-engine +17 -7
  38. package/src/bin/pkmn-debug +1 -1
  39. package/src/lib/common/array.zig +77 -0
  40. package/src/lib/common/data.zig +8 -8
  41. package/src/lib/common/debug.zig +39 -8
  42. package/src/lib/common/js.zig +4 -2
  43. package/src/lib/common/optional.zig +13 -9
  44. package/src/lib/common/options.zig +6 -0
  45. package/src/lib/common/protocol.zig +55 -52
  46. package/src/lib/common/rational.zig +121 -29
  47. package/src/lib/common/rng.zig +11 -13
  48. package/src/lib/common/util.zig +50 -1
  49. package/src/lib/gen1/calc.zig +683 -156
  50. package/src/lib/gen1/chance.zig +415 -290
  51. package/src/lib/gen1/data/moves.zig +2 -4
  52. package/src/lib/gen1/data/species.zig +2 -4
  53. package/src/lib/gen1/data/types.zig +1 -2
  54. package/src/lib/gen1/data.zig +38 -41
  55. package/src/lib/gen1/helpers.zig +92 -548
  56. package/src/lib/gen1/mechanics.zig +354 -228
  57. package/src/lib/gen2/calc.zig +37 -27
  58. package/src/lib/gen2/chance.zig +90 -502
  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 +1 -2
  63. package/src/lib/gen2/data.zig +26 -29
  64. package/src/lib/gen2/generated.zig +18 -23
  65. package/src/lib/gen2/helpers.zig +46 -25
  66. package/src/lib/gen2/mechanics.zig +180 -133
  67. package/src/lib/node.zig +2 -4
  68. package/src/lib/pkmn.zig +14 -16
  69. package/src/lib/wasm.zig +1 -2
  70. package/src/tools/display/dom.ts +96 -0
  71. package/src/tools/display/ui.css +354 -0
  72. package/build/test/display.js +0 -400
  73. package/build/test/display.js.map +0 -1
  74. package/src/lib/gen1/README.md +0 -894
  75. package/src/lib/gen2/README.md +0 -329
  76. package/src/test/display/pkmn.html.tmpl +0 -368
package/build.zig CHANGED
@@ -1,5 +1,5 @@
1
- const std = @import("std");
2
1
  const builtin = @import("builtin");
2
+ const std = @import("std");
3
3
 
4
4
  pub const Options = struct {
5
5
  showdown: ?bool = null,
@@ -8,6 +8,17 @@ pub const Options = struct {
8
8
  calc: ?bool = null,
9
9
  };
10
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
+
11
22
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
12
23
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
13
24
  const build_options = b.addOptions();
@@ -15,16 +26,20 @@ pub fn module(b: *std.Build, options: Options) *std.Build.Module {
15
26
  build_options.addOption(?bool, "log", options.log);
16
27
  build_options.addOption(?bool, "chance", options.chance);
17
28
  build_options.addOption(?bool, "calc", options.calc);
18
- return b.createModule(.{
19
- .root_source_file = .{ .path = dirname ++ "/src/lib/pkmn.zig" },
20
- .imports = &.{.{ .name = "build_options", .module = build_options.createModule() }},
21
- });
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 });
22
36
  }
23
37
 
24
38
  pub fn build(b: *std.Build) !void {
25
39
  const target = b.standardTargetOptions(.{});
26
40
  const optimize = b.standardOptimizeOption(.{});
27
41
 
42
+ const default: ?bool = if (force_pic) false else null;
28
43
  const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
29
44
  const node_import_lib =
30
45
  b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
@@ -32,8 +47,8 @@ pub fn build(b: *std.Build) !void {
32
47
  const wasm_stack_size =
33
48
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
34
49
  const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
35
- const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
36
- 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;
37
52
  const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
38
53
  const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
39
54
 
@@ -44,14 +59,21 @@ pub fn build(b: *std.Build) !void {
44
59
  defer parsed.deinit();
45
60
  const version = parsed.value.object.get("version").?.string;
46
61
  const description = parsed.value.object.get("description").?.string;
47
- var repository = std.mem.split(u8, parsed.value.object.get("repository").?.string, ":");
62
+ var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
48
63
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
49
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
+
50
71
  const showdown =
51
72
  b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
52
73
  const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
53
74
  const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
54
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");
55
77
 
56
78
  const options = b.addOptions();
57
79
  options.addOption(?bool, "showdown", showdown);
@@ -59,32 +81,66 @@ pub fn build(b: *std.Build) !void {
59
81
  options.addOption(?bool, "chance", chance);
60
82
  options.addOption(?bool, "calc", calc);
61
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
+
62
101
  const name = if (showdown) "pkmn-showdown" else "pkmn";
63
102
 
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"),
108
+ .optimize = optimize,
109
+ .target = target,
110
+ .imports = &.{.{ .name = "build_options", .module = options.createModule() }},
111
+ });
112
+
64
113
  var c = false;
65
114
  if (node_headers) |headers| {
66
115
  const addon = b.fmt("{s}.node", .{name});
67
- 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 .{
68
123
  .name = addon,
69
- .root_source_file = .{ .path = "src/lib/node.zig" },
124
+ .root_source_file = path,
70
125
  .optimize = optimize,
71
126
  .target = target,
72
127
  .strip = strip,
73
128
  .pic = pic,
74
129
  });
75
- lib.root_module.addOptions("build_options", options);
76
- lib.addSystemIncludePath(.{ .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));
77
132
  lib.linkLibC();
78
133
  if (node_import_lib) |il| {
79
- lib.addObjectFile(.{ .path = il });
80
- } 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) {
81
136
  try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
82
137
  std.process.exit(1);
83
138
  }
84
139
  lib.linker_allow_shlib_undefined = true;
85
140
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
141
+ if (force_pic and pic orelse false) lib.force_pic = pic;
86
142
  // Always emit to build/lib because this is where the driver code expects to find it
87
- // TODO(ziglang/zig#2231): using the following used to work (though was hacky):
143
+ // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
88
144
  //
89
145
  // lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
90
146
  // b.getInstallStep().dependOn(&lib.step);
@@ -93,69 +149,50 @@ pub fn build(b: *std.Build) !void {
93
149
  // rename the file ourself in install-pkmn-engine
94
150
  b.installArtifact(lib);
95
151
  } else if (wasm) {
96
- const exe = b.addExecutable(.{
97
- .name = name,
98
- .root_source_file = .{ .path = "src/lib/wasm.zig" },
99
- .optimize = switch (optimize) {
100
- .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
101
- else => optimize,
102
- },
103
- .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
104
- .strip = strip,
105
- .pic = pic,
106
- });
107
- exe.root_module.addOptions("build_options", options);
108
- exe.root_module.export_symbol_names = &[_][]const u8{
109
- "SHOWDOWN",
110
- "LOG",
111
- "CHANCE",
112
- "CALC",
113
- "GEN1_CHOICES_SIZE",
114
- "GEN1_LOGS_SIZE",
115
- };
116
- exe.entry = .disabled;
117
- exe.stack_size = wasm_stack_size;
118
- const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
119
- if (optimize != .Debug and opt != null) {
120
- const out = b.fmt("build/lib/{s}.wasm", .{name});
121
- const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
122
- sh.addArtifactArg(exe);
123
- sh.addArg("-o");
124
- sh.addFileArg(.{ .path = out });
125
- b.getInstallStep().dependOn(&sh.step);
126
- } else {
127
- b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
128
- .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
129
- }).step);
130
- }
152
+ try buildWasm(b, name, "src/lib/wasm.zig", optimize, strip, pic, wasm_stack_size, options);
131
153
  } else if (dynamic) {
132
- 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) .{
133
156
  .name = name,
134
- .root_source_file = .{ .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,
135
164
  .version = try std.SemanticVersion.parse(version),
136
165
  .optimize = optimize,
137
166
  .target = target,
138
167
  .strip = strip,
139
168
  .pic = pic,
140
169
  });
141
- lib.root_module.addOptions("build_options", options);
142
- lib.addIncludePath(.{ .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"));
143
172
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
173
+ if (force_pic and pic orelse false) lib.force_pic = pic;
144
174
  b.installArtifact(lib);
145
175
  c = true;
146
176
  } else {
147
- 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 .{
148
184
  .name = name,
149
- .root_source_file = .{ .path = "src/lib/c.zig" },
185
+ .root_source_file = path,
150
186
  .optimize = optimize,
151
187
  .target = target,
152
188
  .strip = strip,
153
189
  .pic = pic,
154
190
  });
155
- lib.root_module.addOptions("build_options", options);
156
- lib.addIncludePath(.{ .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"));
157
193
  lib.bundle_compiler_rt = true;
158
194
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
195
+ if (force_pic and pic orelse false) lib.force_pic = pic;
159
196
  if (emit_asm) {
160
197
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
161
198
  lib.getEmittedAsm(),
@@ -175,15 +212,17 @@ pub fn build(b: *std.Build) !void {
175
212
  }
176
213
 
177
214
  if (c) {
178
- const header = b.addInstallFileWithDir(
179
- .{ .path = "src/include/pkmn.h" },
180
- .header,
181
- "pkmn.h",
182
- );
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");
183
218
  b.getInstallStep().dependOn(&header.step);
184
219
 
185
220
  const pc = b.fmt("lib{s}.pc", .{name});
186
- const file = try b.cache_root.join(b.allocator, &.{pc});
221
+ const file = try std.fs.path.relative(
222
+ b.allocator,
223
+ try std.process.getCwdAlloc(b.allocator),
224
+ try b.cache_root.join(b.allocator, &.{pc}),
225
+ );
187
226
  const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
188
227
 
189
228
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
@@ -216,7 +255,7 @@ pub fn build(b: *std.Build) !void {
216
255
  // TODO: tests can be run multiple times due to @imports
217
256
  const tests = TestStep.create(b, options, config);
218
257
 
219
- var exes = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
258
+ var exes = std.ArrayList(*Step.Compile).init(b.allocator);
220
259
  const tools: ToolConfig = .{
221
260
  .options = .{
222
261
  .showdown = showdown,
@@ -224,6 +263,7 @@ pub fn build(b: *std.Build) !void {
224
263
  .chance = chance,
225
264
  .calc = calc,
226
265
  },
266
+ .module = pkmn,
227
267
  .general = config,
228
268
  .tool = .{
229
269
  .tests = if (tests.build) tests else null,
@@ -262,13 +302,86 @@ pub fn build(b: *std.Build) !void {
262
302
  }
263
303
  }
264
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
+ 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,
333
+ .root_source_file = path,
334
+ .optimize = mode,
335
+ .target = freestanding,
336
+ .strip = strip,
337
+ .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);
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.?, "-O4" });
361
+ sh.addArtifactArg(bin);
362
+ 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
+ }
368
+ b.getInstallStep().dependOn(&sh.step);
369
+ if (!entry) b.installArtifact(bin);
370
+ } else if (entry) {
371
+ b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
372
+ .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
373
+ }).step);
374
+ }
375
+ }
376
+
265
377
  fn maybeStrip(
266
378
  b: *std.Build,
267
- artifact: *std.Build.Step.Compile,
379
+ artifact: *Step.Compile,
268
380
  step: *std.Build.Step,
269
381
  strip: ?bool,
270
382
  cmd: ?[]const u8,
271
383
  ) void {
384
+ if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
272
385
  if (!(strip orelse false) or cmd == null) return;
273
386
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
274
387
  const mac = builtin.os.tag == .macos;
@@ -281,7 +394,7 @@ fn maybeStrip(
281
394
  }
282
395
 
283
396
  const Config = struct {
284
- target: std.Build.ResolvedTarget,
397
+ target: ResolvedTarget,
285
398
  optimize: std.builtin.OptimizeMode,
286
399
  pic: ?bool,
287
400
  strip: ?bool,
@@ -292,9 +405,8 @@ const TestStep = struct {
292
405
  step: std.Build.Step,
293
406
  build: bool,
294
407
 
295
- pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
408
+ pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
296
409
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
297
- const test_file = b.option([]const u8, "test-file", "Input file for test");
298
410
  const test_filter =
299
411
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
300
412
 
@@ -302,13 +414,14 @@ const TestStep = struct {
302
414
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
303
415
  self.* = .{ .step = step, .build = test_filter == null };
304
416
 
305
- const path = test_file orelse "src/lib/test.zig";
306
- const tests = b.addTest(.{
307
- .name = b.fmt("{s}-{s}", .{
308
- std.fs.path.basename(std.fs.path.dirname(path).?),
309
- std.fs.path.stem(std.fs.path.basename(path)),
310
- }),
311
- .root_source_file = .{ .path = path },
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,
312
425
  .optimize = config.optimize,
313
426
  .target = config.target,
314
427
  .filter = test_filter,
@@ -316,8 +429,13 @@ const TestStep = struct {
316
429
  .strip = config.strip,
317
430
  .pic = config.pic,
318
431
  });
319
- tests.root_module.addOptions("build_options", options);
432
+ (if (root_module) tests.root_module else tests).addOptions("build_options", options);
433
+
320
434
  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
+ }
321
439
  if (coverage) |c| {
322
440
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
323
441
  }
@@ -329,15 +447,16 @@ const TestStep = struct {
329
447
 
330
448
  const ToolConfig = struct {
331
449
  options: Options,
450
+ module: ?*std.Build.Module,
332
451
  general: Config,
333
452
  tool: struct {
334
453
  tests: ?*TestStep,
335
454
  name: ?[]const u8 = null,
336
- exes: *std.ArrayList(*std.Build.Step.Compile),
455
+ exes: *std.ArrayList(*Step.Compile),
337
456
  },
338
457
  };
339
458
 
340
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
459
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
341
460
  std.fs.cwd().access(path, .{}) catch |err| switch (err) {
342
461
  error.FileNotFound => return null,
343
462
  else => |e| return e,
@@ -347,17 +466,32 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.R
347
466
  if (index) |i| name = name[0..i];
348
467
  if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
349
468
 
350
- const exe = b.addExecutable(.{
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 .{
351
475
  .name = name,
352
- .root_source_file = .{ .path = path },
476
+ .root_source_file = if (has_path) .{ .path = path } else b.path(path),
353
477
  .target = config.general.target,
354
478
  .optimize = config.general.optimize,
355
479
  .single_threaded = true,
356
480
  .strip = config.general.strip,
357
481
  .pic = config.general.pic,
358
482
  });
359
- exe.root_module.addImport("pkmn", module(b, config.options));
360
483
 
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));
489
+ }
490
+
491
+ if (force_pic) {
492
+ exe.single_threaded = true;
493
+ if (config.general.pic orelse false) exe.force_pic = config.general.pic;
494
+ }
361
495
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
362
496
  config.tool.exes.append(exe) catch @panic("OOM");
363
497
 
@@ -371,7 +505,7 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.R
371
505
  const ToolsStep = struct {
372
506
  step: std.Build.Step,
373
507
 
374
- pub fn create(b: *std.Build, exes: *std.ArrayList(*std.Build.Step.Compile)) *ToolsStep {
508
+ pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
375
509
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
376
510
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
377
511
  self.* = .{ .step = step };
@@ -379,3 +513,23 @@ const ToolsStep = struct {
379
513
  return self;
380
514
  }
381
515
  };
516
+
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);
526
+
527
+ var it = std.mem.splitSequence(u8, WASM, "export const ");
528
+ _ = it.next();
529
+ while (it.next()) |s| {
530
+ const i = std.mem.indexOf(u8, s, " ").?;
531
+ try symbols.append(s[0..i]);
532
+ }
533
+
534
+ return symbols.items;
535
+ }