@pkmn/engine 0.1.0-dev.63cef311 → 0.1.0-dev.6cc9e067

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 (63) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +135 -73
  3. package/build/pkg/addon.d.ts +7 -0
  4. package/build/pkg/addon.js +111 -0
  5. package/build/pkg/addon.js.map +1 -0
  6. package/build/pkg/data/ids.json +12 -12
  7. package/build/pkg/data/layout.json +27 -5
  8. package/build/pkg/data/protocol.json +69 -12
  9. package/build/pkg/data.d.ts +5 -6
  10. package/build/pkg/data.js +3 -5
  11. package/build/pkg/data.js.map +1 -1
  12. package/build/pkg/gen1.d.ts +5 -4
  13. package/build/pkg/gen1.js +49 -33
  14. package/build/pkg/gen1.js.map +1 -1
  15. package/build/pkg/index.d.ts +17 -13
  16. package/build/pkg/index.js.map +1 -1
  17. package/build/pkg/protocol.d.ts +4 -2
  18. package/build/pkg/protocol.js +65 -44
  19. package/build/pkg/protocol.js.map +1 -1
  20. package/build/test/display.js +400 -0
  21. package/build/test/display.js.map +1 -0
  22. package/build/tools/debug.js +134 -0
  23. package/build/tools/debug.js.map +1 -0
  24. package/build.zig +168 -140
  25. package/package.json +86 -46
  26. package/src/bin/install-pkmn-engine +79 -39
  27. package/src/bin/pkmn-debug +62 -0
  28. package/src/lib/common/battle.zig +17 -0
  29. package/src/lib/common/data.zig +19 -31
  30. package/src/lib/common/debug.zig +21 -11
  31. package/src/lib/common/js.zig +118 -0
  32. package/src/lib/common/optional.zig +67 -0
  33. package/src/lib/common/options.zig +12 -3
  34. package/src/lib/common/protocol.zig +942 -403
  35. package/src/lib/common/rational.zig +357 -0
  36. package/src/lib/common/rng.zig +35 -26
  37. package/src/lib/common/util.zig +12 -0
  38. package/src/lib/gen1/calc.zig +426 -0
  39. package/src/lib/gen1/chance.zig +1061 -0
  40. package/src/lib/gen1/data/moves.zig +202 -190
  41. package/src/lib/gen1/data/species.zig +5 -5
  42. package/src/lib/gen1/data/types.zig +10 -10
  43. package/src/lib/gen1/data.zig +106 -92
  44. package/src/lib/gen1/helpers.zig +602 -41
  45. package/src/lib/gen1/mechanics.zig +1228 -821
  46. package/src/lib/gen2/calc.zig +143 -0
  47. package/src/lib/gen2/chance.zig +1315 -0
  48. package/src/lib/gen2/data/items.zig +14 -20
  49. package/src/lib/gen2/data/moves.zig +664 -570
  50. package/src/lib/gen2/data/species.zig +2 -2
  51. package/src/lib/gen2/data/types.zig +137 -34
  52. package/src/lib/gen2/data.zig +134 -96
  53. package/src/lib/gen2/generated.zig +806 -0
  54. package/src/lib/gen2/helpers.zig +50 -26
  55. package/src/lib/gen2/mechanics.zig +3591 -12
  56. package/src/lib/node.zig +131 -0
  57. package/src/lib/pkmn.zig +86 -12
  58. package/src/lib/wasm.zig +13 -0
  59. package/src/test/display/pkmn.html.tmpl +368 -0
  60. package/src/lib/binding/node.zig +0 -229
  61. package/src/lib/binding/wasm.zig +0 -10
  62. package/src/lib/gen1/README.md +0 -778
  63. package/src/lib/gen2/README.md +0 -309
package/build.zig CHANGED
@@ -1,18 +1,23 @@
1
1
  const std = @import("std");
2
2
  const builtin = @import("builtin");
3
3
 
4
- const NativeTargetInfo = std.zig.system.NativeTargetInfo;
5
-
6
- pub const Options = struct { showdown: ?bool = null, trace: ?bool = null };
4
+ pub const Options = struct {
5
+ showdown: ?bool = null,
6
+ log: ?bool = null,
7
+ chance: ?bool = null,
8
+ calc: ?bool = null,
9
+ };
7
10
 
8
11
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
9
12
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
10
13
  const build_options = b.addOptions();
11
14
  build_options.addOption(?bool, "showdown", options.showdown);
12
- build_options.addOption(?bool, "trace", options.trace);
15
+ build_options.addOption(?bool, "log", options.log);
16
+ build_options.addOption(?bool, "chance", options.chance);
17
+ build_options.addOption(?bool, "calc", options.calc);
13
18
  return b.createModule(.{
14
- .source_file = .{ .path = dirname ++ "/src/lib/pkmn.zig" },
15
- .dependencies = &.{.{ .name = "build_options", .module = build_options.createModule() }},
19
+ .root_source_file = .{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" },
20
+ .imports = &.{.{ .name = "build_options", .module = build_options.createModule() }},
16
21
  });
17
22
  }
18
23
 
@@ -27,27 +32,32 @@ pub fn build(b: *std.Build) !void {
27
32
  const wasm_stack_size =
28
33
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
29
34
  const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
30
- const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse false;
31
- const pic = b.option(bool, "pic", "Force position independent code") orelse false;
32
-
33
- const cmd = b.findProgram(&[_][]const u8{"strip"}, &[_][]const u8{}) catch null;
34
-
35
- var parser = std.json.Parser.init(b.allocator, false);
36
- defer parser.deinit();
37
- var tree = try parser.parse(@embedFile("package.json"));
38
- defer tree.deinit();
39
- const version = tree.root.Object.get("version").?.String;
40
- const description = tree.root.Object.get("description").?.String;
41
- var repository = std.mem.split(u8, tree.root.Object.get("repository").?.String, ":");
35
+ const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
36
+ const pic = b.option(bool, "pic", "Force position independent code");
37
+ const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
38
+ const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
39
+
40
+ const cmd = b.findProgram(&.{"strip"}, &.{}) catch null;
41
+
42
+ const json = @embedFile("package.json");
43
+ var parsed = try std.json.parseFromSlice(std.json.Value, b.allocator, json, .{});
44
+ defer parsed.deinit();
45
+ const version = parsed.value.object.get("version").?.string;
46
+ const description = parsed.value.object.get("description").?.string;
47
+ var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
42
48
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
43
49
 
44
50
  const showdown =
45
51
  b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
46
- const trace = b.option(bool, "trace", "Enable trace logs") orelse false;
52
+ const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
53
+ const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
54
+ const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
47
55
 
48
56
  const options = b.addOptions();
49
57
  options.addOption(?bool, "showdown", showdown);
50
- options.addOption(?bool, "trace", trace);
58
+ options.addOption(?bool, "log", log);
59
+ options.addOption(?bool, "chance", chance);
60
+ options.addOption(?bool, "calc", calc);
51
61
 
52
62
  const name = if (showdown) "pkmn-showdown" else "pkmn";
53
63
 
@@ -56,103 +66,128 @@ pub fn build(b: *std.Build) !void {
56
66
  const addon = b.fmt("{s}.node", .{name});
57
67
  const lib = b.addSharedLibrary(.{
58
68
  .name = addon,
59
- .root_source_file = .{ .path = "src/lib/binding/node.zig" },
69
+ .root_source_file = b.path("src/lib/node.zig"),
60
70
  .optimize = optimize,
61
71
  .target = target,
72
+ .strip = strip,
73
+ .pic = pic,
62
74
  });
63
- lib.addOptions("build_options", options);
64
- lib.setMainPkgPath("./");
65
- lib.addSystemIncludePath(headers);
75
+ lib.root_module.addOptions("build_options", options);
76
+ lib.addSystemIncludePath(b.path(headers));
66
77
  lib.linkLibC();
67
78
  if (node_import_lib) |il| {
68
- lib.addObjectFile(il);
69
- } else if ((try NativeTargetInfo.detect(target)).target.os.tag == .windows) {
79
+ lib.addObjectFile(b.path(il));
80
+ } else if (target.result.os.tag == .windows) {
70
81
  try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
71
82
  std.process.exit(1);
72
83
  }
73
84
  lib.linker_allow_shlib_undefined = true;
74
85
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
75
- if (pic) lib.force_pic = pic;
76
86
  // Always emit to build/lib because this is where the driver code expects to find it
77
- // TODO(ziglang/zig#2231): using the following used to work (though be hacky):
87
+ // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
78
88
  //
79
89
  // lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
80
90
  // b.getInstallStep().dependOn(&lib.step);
81
91
  //
82
92
  // But ziglang/zig#14647 broke this so we now need to do an install() and then manually
83
93
  // rename the file ourself in install-pkmn-engine
84
- lib.install();
94
+ b.installArtifact(lib);
85
95
  } else if (wasm) {
86
- const lib = b.addSharedLibrary(.{
96
+ const exe = b.addExecutable(.{
87
97
  .name = name,
88
- .root_source_file = .{ .path = "src/lib/binding/wasm.zig" },
98
+ .root_source_file = b.path("src/lib/wasm.zig"),
89
99
  .optimize = switch (optimize) {
90
100
  .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
91
101
  else => optimize,
92
102
  },
93
- .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
103
+ .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
104
+ .strip = strip,
105
+ .pic = pic,
94
106
  });
95
- lib.addOptions("build_options", options);
96
- lib.setMainPkgPath("./");
97
- lib.stack_size = wasm_stack_size;
98
- lib.rdynamic = true;
99
- lib.strip = strip;
100
- if (pic) lib.force_pic = pic;
101
- const opt = b.findProgram(
102
- &[_][]const u8{"wasm-opt"},
103
- &[_][]const u8{"./node_modules/.bin"},
104
- ) catch null;
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;
105
119
  if (optimize != .Debug and opt != null) {
106
120
  const out = b.fmt("build/lib/{s}.wasm", .{name});
107
- const sh = b.addSystemCommand(&[_][]const u8{ opt.?, "-O4" });
108
- sh.addArtifactArg(lib);
121
+ const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
122
+ sh.addArtifactArg(exe);
109
123
  sh.addArg("-o");
110
- sh.addFileSourceArg(.{ .path = out });
124
+ sh.addFileArg(b.path(out));
111
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);
112
130
  }
113
- lib.install();
114
131
  } else if (dynamic) {
115
132
  const lib = b.addSharedLibrary(.{
116
133
  .name = name,
117
- .root_source_file = .{ .path = "src/lib/binding/c.zig" },
118
- .version = try std.builtin.Version.parse(version),
134
+ .root_source_file = b.path("src/lib/c.zig"),
135
+ .version = try std.SemanticVersion.parse(version),
119
136
  .optimize = optimize,
120
137
  .target = target,
138
+ .strip = strip,
139
+ .pic = pic,
121
140
  });
122
- lib.addOptions("build_options", options);
123
- lib.setMainPkgPath("./");
124
- lib.addIncludePath("src/include");
141
+ lib.root_module.addOptions("build_options", options);
142
+ lib.addIncludePath(b.path("src/include"));
125
143
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
126
- if (pic) lib.force_pic = pic;
127
- lib.install();
144
+ b.installArtifact(lib);
128
145
  c = true;
129
146
  } else {
130
147
  const lib = b.addStaticLibrary(.{
131
148
  .name = name,
132
- .root_source_file = .{ .path = "src/lib/binding/c.zig" },
149
+ .root_source_file = b.path("src/lib/c.zig"),
133
150
  .optimize = optimize,
134
151
  .target = target,
152
+ .strip = strip,
153
+ .pic = pic,
135
154
  });
136
- lib.addOptions("build_options", options);
137
- lib.setMainPkgPath("./");
138
- lib.addIncludePath("src/include");
155
+ lib.root_module.addOptions("build_options", options);
156
+ lib.addIncludePath(b.path("src/include"));
139
157
  lib.bundle_compiler_rt = true;
140
158
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
141
- if (pic) lib.force_pic = pic;
142
- lib.install();
159
+ if (emit_asm) {
160
+ b.getInstallStep().dependOn(&b.addInstallFileWithDir(
161
+ lib.getEmittedAsm(),
162
+ .prefix,
163
+ b.fmt("{s}.s", .{name}),
164
+ ).step);
165
+ }
166
+ if (emit_ll) {
167
+ b.getInstallStep().dependOn(&b.addInstallFileWithDir(
168
+ lib.getEmittedLlvmIr(),
169
+ .prefix,
170
+ b.fmt("{s}.ll", .{name}),
171
+ ).step);
172
+ }
173
+ b.installArtifact(lib);
143
174
  c = true;
144
175
  }
145
176
 
146
177
  if (c) {
147
178
  const header = b.addInstallFileWithDir(
148
- .{ .path = "src/include/pkmn.h" },
179
+ b.path("src/include/pkmn.h"),
149
180
  .header,
150
181
  "pkmn.h",
151
182
  );
152
183
  b.getInstallStep().dependOn(&header.step);
153
184
 
154
185
  const pc = b.fmt("lib{s}.pc", .{name});
155
- const file = try b.cache_root.join(b.allocator, &.{pc});
186
+ const file = try std.fs.path.relative(
187
+ b.allocator,
188
+ try std.process.getCwdAlloc(b.allocator),
189
+ try b.cache_root.join(b.allocator, &.{pc}),
190
+ );
156
191
  const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
157
192
 
158
193
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
@@ -181,14 +216,20 @@ pub fn build(b: *std.Build) !void {
181
216
  .strip = strip,
182
217
  .cmd = cmd,
183
218
  };
219
+
220
+ // TODO: tests can be run multiple times due to @imports
184
221
  const tests = TestStep.create(b, options, config);
185
222
 
186
- var exes = std.ArrayList(*std.Build.CompileStep).init(b.allocator);
223
+ var exes = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
187
224
  const tools: ToolConfig = .{
225
+ .options = .{
226
+ .showdown = showdown,
227
+ .log = log,
228
+ .chance = chance,
229
+ .calc = calc,
230
+ },
188
231
  .general = config,
189
232
  .tool = .{
190
- .showdown = showdown,
191
- .trace = trace,
192
233
  .tests = if (tests.build) tests else null,
193
234
  .exes = &exes,
194
235
  },
@@ -197,141 +238,128 @@ pub fn build(b: *std.Build) !void {
197
238
  var benchmark_config = tools;
198
239
  benchmark_config.general.optimize = .ReleaseFast;
199
240
  benchmark_config.general.strip = true;
200
- const benchmark = tool(b, "src/test/benchmark.zig", benchmark_config);
241
+ const benchmark = try tool(b, "src/test/benchmark.zig", benchmark_config);
201
242
 
202
243
  var fuzz_config = tools;
203
244
  fuzz_config.general.strip = false;
204
245
  fuzz_config.tool.name = "fuzz";
205
- const fuzz = tool(b, "src/test/benchmark.zig", fuzz_config);
206
-
207
- const serde = tool(b, "src/tools/serde.zig", tools);
208
- const protocol = tool(b, "src/tools/protocol.zig", tools);
209
-
210
- const lint_exe =
211
- b.addExecutable(.{ .name = "lint", .root_source_file = .{ .path = "src/tools/lint.zig" } });
212
- if (tests.build) tests.step.dependOn(&lint_exe.step);
213
- const lint = lint_exe.run();
214
-
215
- b.step("benchmark", "Run benchmark code").dependOn(&benchmark.step);
216
- b.step("fuzz", "Run fuzz tester").dependOn(&fuzz.step);
217
- b.step("lint", "Lint source files").dependOn(&lint.step);
218
- b.step("protocol", "Run protocol dump tool").dependOn(&protocol.step);
219
- b.step("serde", "Run serialization/deserialization tool").dependOn(&serde.step);
246
+ const fuzz = try tool(b, "src/test/fuzz.zig", fuzz_config);
247
+
248
+ const analyze = try tool(b, "src/tools/analyze.zig", tools);
249
+ const dump = try tool(b, "src/tools/dump.zig", tools);
250
+ const transitions = try tool(b, "src/tools/transitions.zig", tools);
251
+
252
+ // FIXME: serde randomly fails to build in some release configurations
253
+ var hack = tools;
254
+ if (optimize != .Debug) hack.tool.tests = null;
255
+ const serde = try tool(b, "src/tools/serde.zig", hack);
256
+
257
+ if (analyze) |t| b.step("analyze", "Run LLVM analysis tool").dependOn(&t.step);
258
+ if (benchmark) |t| b.step("benchmark", "Run benchmark code").dependOn(&t.step);
259
+ if (dump) |t| b.step("dump", "Run protocol dump tool").dependOn(&t.step);
260
+ if (fuzz) |t| b.step("fuzz", "Run fuzz tester").dependOn(&t.step);
261
+ if (serde) |t| b.step("serde", "Run serialization/deserialization tool").dependOn(&t.step);
220
262
  b.step("test", "Run all tests").dependOn(&tests.step);
221
263
  b.step("tools", "Install tools").dependOn(&ToolsStep.create(b, &exes).step);
264
+ if (transitions) |t| {
265
+ b.step("transitions", "Visualize transitions algorithm search").dependOn(&t.step);
266
+ }
222
267
  }
223
268
 
224
269
  fn maybeStrip(
225
270
  b: *std.Build,
226
- artifact: *std.Build.CompileStep,
271
+ artifact: *std.Build.Step.Compile,
227
272
  step: *std.Build.Step,
228
- strip: bool,
273
+ strip: ?bool,
229
274
  cmd: ?[]const u8,
230
275
  ) void {
231
- artifact.strip = strip;
232
- if (!strip or cmd == null) return;
276
+ if (!(strip orelse false) or cmd == null) return;
233
277
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
234
278
  const mac = builtin.os.tag == .macos;
235
279
  if (mac and artifact.isDynamicLibrary()) return;
236
280
  // Assuming GNU strip, which complains "illegal pathname found in archive member"...
237
281
  if (!mac and artifact.isStaticLibrary()) return;
238
- const sh = b.addSystemCommand(&[_][]const u8{ cmd.?, if (mac) "-x" else "-s" });
282
+ const sh = b.addSystemCommand(&.{ cmd.?, if (mac) "-x" else "-s" });
239
283
  sh.addArtifactArg(artifact);
240
284
  step.dependOn(&sh.step);
241
285
  }
242
286
 
243
287
  const Config = struct {
244
- target: std.zig.CrossTarget,
288
+ target: std.Build.ResolvedTarget,
245
289
  optimize: std.builtin.OptimizeMode,
246
- pic: bool,
247
- strip: bool,
290
+ pic: ?bool,
291
+ strip: ?bool,
248
292
  cmd: ?[]const u8,
249
293
  };
250
294
 
251
- const TESTS = [_][]const u8{
252
- "src/lib/common/test.zig",
253
- "src/lib/gen1/test.zig",
254
- "src/lib/gen2/test.zig",
255
- };
256
-
257
295
  const TestStep = struct {
258
296
  step: std.Build.Step,
259
297
  build: bool,
260
298
 
261
- pub fn create(b: *std.Build, options: *std.Build.OptionsStep, config: Config) *TestStep {
299
+ pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
262
300
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
263
- const test_file = b.option([]const u8, "test-file", "Input file for test");
264
- const test_bin = b.option([]const u8, "test-bin", "Emit test binary to");
265
301
  const test_filter =
266
302
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
267
- const test_no_exec =
268
- b.option(bool, "test-no-exec", "Compiles test binary without running it") orelse false;
269
303
 
270
304
  const self = b.allocator.create(TestStep) catch @panic("OOM");
271
305
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
272
- self.* = TestStep{ .step = step, .build = test_filter == null };
273
-
274
- const paths: []const []const u8 =
275
- if (test_file) |t| &.{t} else if (coverage != null) &.{"src/lib/test.zig"} else &TESTS;
276
- for (paths) |path| {
277
- const tests = b.addTest(.{
278
- .name = std.fs.path.basename(std.fs.path.dirname(path).?),
279
- .root_source_file = .{ .path = path },
280
- .optimize = config.optimize,
281
- .target = config.target,
282
- });
283
- tests.setMainPkgPath("./");
284
- tests.setFilter(test_filter);
285
- tests.addOptions("build_options", options);
286
- tests.single_threaded = true;
287
- maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
288
- if (config.pic) tests.force_pic = config.pic;
289
- if (test_bin) |bin| {
290
- tests.name = std.fs.path.basename(bin);
291
- if (std.fs.path.dirname(bin)) |dir| tests.setOutputDir(dir);
292
- }
293
- if (coverage) |c| {
294
- tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
295
- }
296
- self.step.dependOn(if (test_no_exec) &tests.step else &tests.run().step);
306
+ self.* = .{ .step = step, .build = test_filter == null };
307
+
308
+ const tests = b.addTest(.{
309
+ .root_source_file = b.path("src/lib/test.zig"),
310
+ .optimize = config.optimize,
311
+ .target = config.target,
312
+ .filter = test_filter,
313
+ .single_threaded = true,
314
+ .strip = config.strip,
315
+ .pic = config.pic,
316
+ });
317
+ tests.root_module.addOptions("build_options", options);
318
+ maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
319
+ if (coverage) |c| {
320
+ tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
297
321
  }
322
+ self.step.dependOn(&b.addRunArtifact(tests).step);
323
+
298
324
  return self;
299
325
  }
300
326
  };
301
327
 
302
328
  const ToolConfig = struct {
329
+ options: Options,
303
330
  general: Config,
304
331
  tool: struct {
305
- showdown: bool,
306
- trace: bool,
307
332
  tests: ?*TestStep,
308
333
  name: ?[]const u8 = null,
309
- exes: *std.ArrayList(*std.Build.CompileStep),
334
+ exes: *std.ArrayList(*std.Build.Step.Compile),
310
335
  },
311
336
  };
312
337
 
313
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) *std.Build.RunStep {
338
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
339
+ std.fs.cwd().access(path, .{}) catch |err| switch (err) {
340
+ error.FileNotFound => return null,
341
+ else => |e| return e,
342
+ };
314
343
  var name = config.tool.name orelse std.fs.path.basename(path);
315
344
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
316
345
  if (index) |i| name = name[0..i];
317
- if (config.tool.showdown) name = b.fmt("{s}-showdown", .{name});
346
+ if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
318
347
 
319
348
  const exe = b.addExecutable(.{
320
349
  .name = name,
321
- .root_source_file = .{ .path = path },
350
+ .root_source_file = b.path(path),
322
351
  .target = config.general.target,
323
352
  .optimize = config.general.optimize,
353
+ .single_threaded = true,
354
+ .strip = config.general.strip,
355
+ .pic = config.general.pic,
324
356
  });
325
- exe.addModule("pkmn", module(b, .{
326
- .showdown = config.tool.showdown,
327
- .trace = config.tool.trace,
328
- }));
329
- exe.single_threaded = true;
330
- if (config.general.pic) exe.force_pic = config.general.pic;
357
+ exe.root_module.addImport("pkmn", module(b, config.options));
358
+
331
359
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
332
360
  config.tool.exes.append(exe) catch @panic("OOM");
333
361
 
334
- const run = exe.run();
362
+ const run = b.addRunArtifact(exe);
335
363
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
336
364
  if (b.args) |args| run.addArgs(args);
337
365
 
@@ -341,11 +369,11 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) *std.Build.RunStep
341
369
  const ToolsStep = struct {
342
370
  step: std.Build.Step,
343
371
 
344
- pub fn create(b: *std.Build, exes: *std.ArrayList(*std.Build.CompileStep)) *ToolsStep {
372
+ pub fn create(b: *std.Build, exes: *std.ArrayList(*std.Build.Step.Compile)) *ToolsStep {
345
373
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
346
374
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
347
- self.* = ToolsStep{ .step = step };
348
- for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t).step);
375
+ self.* = .{ .step = step };
376
+ for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t, .{}).step);
349
377
  return self;
350
378
  }
351
379
  };