@pkmn/engine 0.1.0-dev.f918c3bd → 0.1.0-dev.fb6653d6

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