@pkmn/engine 0.1.0-dev.c4dec0f7 → 0.1.0-dev.c4ffaf39

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