@pkmn/engine 0.1.0-dev.84cc1a02 → 0.1.0-dev.86ae1278

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