@pkmn/engine 0.1.0-dev.80019d10 → 0.1.0-dev.8058a1d0

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 (84) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +59 -24
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -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 +110 -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 +26 -4
  13. package/build/pkg/addon.js +39 -71
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +27 -5
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +42 -91
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +29 -1
  26. package/build/pkg/protocol.js +109 -107
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +84 -75
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +66 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +360 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +157 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +259 -112
  39. package/package.json +61 -132
  40. package/src/bin/install-pkmn-engine +47 -21
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +200 -0
  43. package/src/lib/{binding → bindings}/node.zig +15 -32
  44. package/src/lib/bindings/wasm.zig +50 -0
  45. package/src/lib/common/array.zig +70 -0
  46. package/src/lib/common/data.zig +4 -4
  47. package/src/lib/common/debug.zig +33 -28
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +12 -18
  50. package/src/lib/common/options.zig +6 -0
  51. package/src/lib/common/protocol.zig +256 -224
  52. package/src/lib/common/rational.zig +340 -253
  53. package/src/lib/common/rng.zig +7 -9
  54. package/src/lib/common/util.zig +33 -1
  55. package/src/lib/gen1/calc.zig +724 -175
  56. package/src/lib/gen1/chance.zig +453 -291
  57. package/src/lib/gen1/data/moves.zig +2 -4
  58. package/src/lib/gen1/data/species.zig +2 -4
  59. package/src/lib/gen1/data/types.zig +3 -4
  60. package/src/lib/gen1/data.zig +29 -32
  61. package/src/lib/gen1/helpers.zig +91 -549
  62. package/src/lib/gen1/mechanics.zig +417 -294
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +85 -503
  65. package/src/lib/gen2/data/items.zig +1 -3
  66. package/src/lib/gen2/data/moves.zig +2 -4
  67. package/src/lib/gen2/data/species.zig +1 -3
  68. package/src/lib/gen2/data/types.zig +7 -5
  69. package/src/lib/gen2/data.zig +27 -29
  70. package/src/lib/gen2/generated.zig +19 -26
  71. package/src/lib/gen2/helpers.zig +45 -26
  72. package/src/lib/gen2/mechanics.zig +234 -180
  73. package/src/lib/napi.h +2 -0
  74. package/src/lib/node.zig +14 -0
  75. package/src/lib/pkmn.zig +125 -29
  76. package/src/lib/wasm.zig +13 -0
  77. package/src/tools/display/dom.ts +108 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/binding/wasm.zig +0 -12
  82. package/src/lib/gen1/README.md +0 -894
  83. package/src/lib/gen2/README.md +0 -329
  84. package/src/test/display/pkmn.html.tmpl +0 -368
package/build.zig CHANGED
@@ -1,5 +1,9 @@
1
- const std = @import("std");
2
1
  const builtin = @import("builtin");
2
+ const std = @import("std");
3
+
4
+ fn ArrayList(comptime T: type) type {
5
+ return std.array_list.Aligned(T, null);
6
+ }
3
7
 
4
8
  pub const Options = struct {
5
9
  showdown: ?bool = null,
@@ -9,16 +13,16 @@ pub const Options = struct {
9
13
  };
10
14
 
11
15
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
12
- const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
13
16
  const build_options = b.addOptions();
14
17
  build_options.addOption(?bool, "showdown", options.showdown);
15
18
  build_options.addOption(?bool, "log", options.log);
16
19
  build_options.addOption(?bool, "chance", options.chance);
17
20
  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
- });
21
+
22
+ const root_source_file = b.path("src/lib/pkmn.zig");
23
+ const imports: []const std.Build.Module.Import =
24
+ &.{.{ .name = "build_options", .module = build_options.createModule() }};
25
+ return b.createModule(.{ .root_source_file = root_source_file, .imports = imports });
22
26
  }
23
27
 
24
28
  pub fn build(b: *std.Build) !void {
@@ -28,12 +32,16 @@ pub fn build(b: *std.Build) !void {
28
32
  const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
29
33
  const node_import_lib =
30
34
  b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
35
+ const demo = if (try exists(b, "src/tools/demo.zig"))
36
+ b.option(bool, "demo", "Build the demo library") orelse false
37
+ else
38
+ false;
31
39
  const wasm = b.option(bool, "wasm", "Build a WASM library") orelse false;
32
40
  const wasm_stack_size =
33
41
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
34
42
  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;
43
+ const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
44
+ const pic = b.option(bool, "pic", "Force position independent code");
37
45
  const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
38
46
  const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
39
47
 
@@ -44,7 +52,7 @@ pub fn build(b: *std.Build) !void {
44
52
  defer parsed.deinit();
45
53
  const version = parsed.value.object.get("version").?.string;
46
54
  const description = parsed.value.object.get("description").?.string;
47
- var repository = std.mem.split(u8, parsed.value.object.get("repository").?.string, ":");
55
+ var repository = std.mem.splitScalar(u8, parsed.value.object.get("repository").?.string, ':');
48
56
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
49
57
 
50
58
  const showdown =
@@ -52,6 +60,7 @@ pub fn build(b: *std.Build) !void {
52
60
  const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
53
61
  const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
54
62
  const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
63
+ const extra = b.option([]const []const u8, "option", "Enable extra option");
55
64
 
56
65
  const options = b.addOptions();
57
66
  options.addOption(?bool, "showdown", showdown);
@@ -59,32 +68,67 @@ pub fn build(b: *std.Build) !void {
59
68
  options.addOption(?bool, "chance", chance);
60
69
  options.addOption(?bool, "calc", calc);
61
70
 
71
+ if (extra) |opts| {
72
+ for (opts) |opt| {
73
+ if (std.mem.indexOfScalar(u8, opt, '=')) |i| {
74
+ options.addOption(?bool, opt[0..i], if (std.mem.eql(u8, opt[i + 1 ..], "true"))
75
+ true
76
+ else if (std.mem.eql(u8, opt[i + 1 ..], "false"))
77
+ false
78
+ else {
79
+ std.log.err("Invalid option: {s}\n", .{opt});
80
+ return error.InvalidOption;
81
+ });
82
+ } else {
83
+ options.addOption(?bool, opt, true);
84
+ }
85
+ }
86
+ }
87
+
62
88
  const name = if (showdown) "pkmn-showdown" else "pkmn";
63
89
 
90
+ const pkmn: *std.Build.Module = b.addModule("pkmn", .{
91
+ .root_source_file = b.path("src/lib/pkmn.zig"),
92
+ .optimize = optimize,
93
+ .target = target,
94
+ .imports = &.{.{ .name = "build_options", .module = options.createModule() }},
95
+ });
96
+
64
97
  var c = false;
65
98
  if (node_headers) |headers| {
99
+ const translate_c = b.addTranslateC(.{
100
+ .root_source_file = b.path("src/lib/napi.h"),
101
+ .target = target,
102
+ .optimize = optimize,
103
+ });
104
+ translate_c.addSystemIncludePath(b.path(headers));
66
105
  const addon = b.fmt("{s}.node", .{name});
67
- const lib = b.addSharedLibrary(.{
106
+ const path = b.path("src/lib/node.zig");
107
+ const lib = b.addLibrary(.{
108
+ .linkage = .dynamic,
68
109
  .name = addon,
69
- .main_pkg_path = .{ .path = "." },
70
- .root_source_file = .{ .path = "src/lib/binding/node.zig" },
71
- .optimize = optimize,
72
- .target = target,
110
+ .root_module = b.createModule(.{
111
+ .root_source_file = path,
112
+ .optimize = optimize,
113
+ .target = target,
114
+ .strip = strip,
115
+ .pic = pic,
116
+ }),
73
117
  });
74
- lib.addOptions("build_options", options);
75
- lib.addSystemIncludePath(.{ .path = headers });
76
- lib.linkLibC();
118
+ lib.root_module.addOptions("build_options", options);
119
+ lib.root_module.addImport("napi", translate_c.createModule());
120
+ lib.root_module.link_libc = true;
77
121
  if (node_import_lib) |il| {
78
- lib.addObjectFile(.{ .path = il });
79
- } else if ((try std.zig.system.NativeTargetInfo.detect(target)).target.os.tag == .windows) {
80
- try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
122
+ lib.root_module.addObjectFile(b.path(il));
123
+ } else if (target.result.os.tag == .windows) {
124
+ var err = std.Io.File.stderr().writer(b.graph.io, &.{});
125
+ try err.interface.writeAll("Must provide --node-import-library path on Windows\n");
81
126
  std.process.exit(1);
82
127
  }
83
128
  lib.linker_allow_shlib_undefined = true;
84
129
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
85
- if (pic) lib.force_pic = pic;
86
130
  // 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):
131
+ // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
88
132
  //
89
133
  // lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
90
134
  // b.getInstallStep().dependOn(&lib.step);
@@ -93,59 +137,50 @@ pub fn build(b: *std.Build) !void {
93
137
  // rename the file ourself in install-pkmn-engine
94
138
  b.installArtifact(lib);
95
139
  } 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);
140
+ const path = "src/lib/wasm.zig";
141
+ try buildWasm(b, name, path, optimize, strip, pic, wasm_stack_size, null, options);
142
+ } else if (demo) {
143
+ const path = "src/tools/demo.zig";
144
+ const n = if (showdown) "demo-showdown" else "demo";
145
+ const mod = pkmn;
146
+ try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
121
147
  } else if (dynamic) {
122
- const lib = b.addSharedLibrary(.{
148
+ const path = b.path("src/lib/c.zig");
149
+ const lib = b.addLibrary(.{
150
+ .linkage = .dynamic,
123
151
  .name = name,
124
- .main_pkg_path = .{ .path = "." },
125
- .root_source_file = .{ .path = "src/lib/binding/c.zig" },
126
- .version = try std.SemanticVersion.parse(version),
127
- .optimize = optimize,
128
- .target = target,
152
+ .root_module = b.createModule(.{
153
+ .root_source_file = path,
154
+ .optimize = optimize,
155
+ .target = target,
156
+ .strip = strip,
157
+ .pic = pic,
158
+ }),
129
159
  });
130
- lib.addOptions("build_options", options);
131
- lib.addIncludePath(.{ .path = "src/include" });
160
+ lib.root_module.addOptions("build_options", options);
161
+ lib.root_module.addIncludePath(b.path("src/include"));
132
162
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
133
- if (pic) lib.force_pic = pic;
134
163
  b.installArtifact(lib);
135
164
  c = true;
136
165
  } else {
137
- const lib = b.addStaticLibrary(.{
166
+ const path = b.path("src/lib/c.zig");
167
+ const lib = b.addLibrary(.{
168
+ .linkage = .static,
138
169
  .name = name,
139
- .main_pkg_path = .{ .path = "." },
140
- .root_source_file = .{ .path = "src/lib/binding/c.zig" },
141
- .optimize = optimize,
142
- .target = target,
170
+ .root_module = b.createModule(.{
171
+ .root_source_file = path,
172
+ .optimize = optimize,
173
+ .target = target,
174
+ .strip = strip,
175
+ .pic = pic,
176
+ }),
143
177
  });
144
- lib.addOptions("build_options", options);
145
- lib.addIncludePath(.{ .path = "src/include" });
146
- lib.bundle_compiler_rt = true;
178
+ lib.root_module.addOptions("build_options", options);
179
+ lib.root_module.addIncludePath(b.path("src/include"));
180
+ if (target.result.os.tag != .macos) {
181
+ lib.bundle_compiler_rt = true;
182
+ }
147
183
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
148
- if (pic) lib.force_pic = pic;
149
184
  if (emit_asm) {
150
185
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
151
186
  lib.getEmittedAsm(),
@@ -165,20 +200,25 @@ pub fn build(b: *std.Build) !void {
165
200
  }
166
201
 
167
202
  if (c) {
168
- const header = b.addInstallFileWithDir(
169
- .{ .path = "src/include/pkmn.h" },
170
- .header,
171
- "pkmn.h",
172
- );
203
+ const path = b.path("src/include/pkmn.h");
204
+ const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
173
205
  b.getInstallStep().dependOn(&header.step);
174
206
 
175
207
  const pc = b.fmt("lib{s}.pc", .{name});
176
- const file = try b.cache_root.join(b.allocator, &.{pc});
177
- const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
208
+ const cwd = try std.process.currentPathAlloc(b.graph.io, b.allocator);
209
+ const file = try std.Io.Dir.path.relative(
210
+ b.allocator,
211
+ cwd,
212
+ &b.graph.environ_map,
213
+ cwd,
214
+ try b.cache_root.join(b.allocator, &.{pc}),
215
+ );
216
+ const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
217
+ defer pkgconfig_file.close(b.graph.io);
178
218
 
179
219
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
180
- const writer = pkgconfig_file.writer();
181
- try writer.print(
220
+ var writer = pkgconfig_file.writer(b.graph.io, &.{});
221
+ try writer.interface.print(
182
222
  \\prefix={0s}/{1s}
183
223
  \\includedir=${{prefix}}/include
184
224
  \\libdir=${{prefix}}/lib
@@ -190,12 +230,11 @@ pub fn build(b: *std.Build) !void {
190
230
  \\Cflags: -I${{includedir}}
191
231
  \\Libs: -L${{libdir}} -l{2s}
192
232
  , .{ dirname, b.install_path, name, repository.next().?, description, version });
193
- defer pkgconfig_file.close();
194
233
 
195
234
  b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
196
235
  }
197
236
 
198
- const config = .{
237
+ const config: Config = .{
199
238
  .target = target,
200
239
  .optimize = optimize,
201
240
  .pic = pic,
@@ -206,7 +245,7 @@ pub fn build(b: *std.Build) !void {
206
245
  // TODO: tests can be run multiple times due to @imports
207
246
  const tests = TestStep.create(b, options, config);
208
247
 
209
- var exes = std.ArrayList(*std.Build.CompileStep).init(b.allocator);
248
+ var exes: ArrayList(*std.Build.Step.Compile) = .empty;
210
249
  const tools: ToolConfig = .{
211
250
  .options = .{
212
251
  .showdown = showdown,
@@ -214,6 +253,7 @@ pub fn build(b: *std.Build) !void {
214
253
  .chance = chance,
215
254
  .calc = calc,
216
255
  },
256
+ .module = pkmn,
217
257
  .general = config,
218
258
  .tool = .{
219
259
  .tests = if (tests.build) tests else null,
@@ -252,15 +292,93 @@ pub fn build(b: *std.Build) !void {
252
292
  }
253
293
  }
254
294
 
295
+ fn buildWasm(
296
+ b: *std.Build,
297
+ name: []const u8,
298
+ root_src_file: []const u8,
299
+ optimize: std.builtin.OptimizeMode,
300
+ strip: ?bool,
301
+ pic: ?bool,
302
+ wasm_stack_size: u64,
303
+ import: ?*std.Build.Module,
304
+ options: anytype,
305
+ ) !void {
306
+ const mode = switch (optimize) {
307
+ .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
308
+ else => optimize,
309
+ };
310
+ // https://webassembly.org/features/
311
+ var features = std.Target.wasm.featureSet(&.{
312
+ .atomics,
313
+ .bulk_memory,
314
+ // .exception_handling,
315
+ .extended_const,
316
+ // .half_precision,
317
+ // .multimemory,
318
+ .mutable_globals,
319
+ .nontrapping_fptoint,
320
+ .reference_types,
321
+ // .relaxed_simd,
322
+ .sign_ext,
323
+ .simd128,
324
+ // .tail_call,
325
+ });
326
+ if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
327
+ features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
328
+ }
329
+ const freestanding = b.resolveTargetQuery(.{
330
+ .cpu_arch = .wasm32,
331
+ .os_tag = .freestanding,
332
+ .cpu_features_add = features,
333
+ });
334
+ const path = b.path(root_src_file);
335
+ const exe = b.addExecutable(.{
336
+ .name = name,
337
+ .root_module = b.createModule(.{
338
+ .root_source_file = path,
339
+ .optimize = mode,
340
+ .target = freestanding,
341
+ .strip = strip,
342
+ .pic = pic,
343
+ }),
344
+ });
345
+
346
+ var file = try std.Io.Dir.cwd().openFile(b.graph.io, root_src_file, .{});
347
+ defer file.close(b.graph.io);
348
+ var reader = file.reader(b.graph.io, &.{});
349
+ const bytes = try reader.interface.allocRemaining(b.allocator, .unlimited);
350
+ exe.root_module.export_symbol_names = try exports(b, bytes);
351
+ exe.entry = .disabled;
352
+
353
+ if (import) |i| {
354
+ exe.root_module.addImport("pkmn", i);
355
+ }
356
+ exe.stack_size = wasm_stack_size;
357
+ exe.root_module.addOptions("build_options", options);
358
+
359
+ const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
360
+ if (optimize != .Debug and opt != null) {
361
+ const out = b.fmt("build/lib/{s}.wasm", .{name});
362
+ const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
363
+ sh.addArtifactArg(exe);
364
+ sh.addArg("-o");
365
+ sh.addFileArg(b.path(out));
366
+ b.getInstallStep().dependOn(&sh.step);
367
+ } else {
368
+ b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
369
+ .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
370
+ }).step);
371
+ }
372
+ }
373
+
255
374
  fn maybeStrip(
256
375
  b: *std.Build,
257
- artifact: *std.Build.CompileStep,
376
+ artifact: *std.Build.Step.Compile,
258
377
  step: *std.Build.Step,
259
- strip: bool,
378
+ strip: ?bool,
260
379
  cmd: ?[]const u8,
261
380
  ) void {
262
- artifact.strip = strip;
263
- if (!strip or cmd == null) return;
381
+ if (!(strip orelse false) or cmd == null) return;
264
382
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
265
383
  const mac = builtin.os.tag == .macos;
266
384
  if (mac and artifact.isDynamicLibrary()) return;
@@ -272,10 +390,10 @@ fn maybeStrip(
272
390
  }
273
391
 
274
392
  const Config = struct {
275
- target: std.zig.CrossTarget,
393
+ target: std.Build.ResolvedTarget,
276
394
  optimize: std.builtin.OptimizeMode,
277
- pic: bool,
278
- strip: bool,
395
+ pic: ?bool,
396
+ strip: ?bool,
279
397
  cmd: ?[]const u8,
280
398
  };
281
399
 
@@ -283,9 +401,8 @@ const TestStep = struct {
283
401
  step: std.Build.Step,
284
402
  build: bool,
285
403
 
286
- pub fn create(b: *std.Build, options: *std.Build.OptionsStep, config: Config) *TestStep {
404
+ pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
287
405
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
288
- const test_file = b.option([]const u8, "test-file", "Input file for test");
289
406
  const test_filter =
290
407
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
291
408
 
@@ -293,22 +410,21 @@ const TestStep = struct {
293
410
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
294
411
  self.* = .{ .step = step, .build = test_filter == null };
295
412
 
296
- const path = test_file orelse "src/lib/test.zig";
413
+ const path = b.path("src/lib/test.zig");
297
414
  const tests = b.addTest(.{
298
- .name = b.fmt("{s}-{s}", .{
299
- std.fs.path.basename(std.fs.path.dirname(path).?),
300
- std.fs.path.stem(std.fs.path.basename(path)),
415
+ .root_module = b.createModule(.{
416
+ .root_source_file = path,
417
+ .optimize = config.optimize,
418
+ .target = config.target,
419
+ .single_threaded = true,
420
+ .strip = config.strip,
421
+ .pic = config.pic,
301
422
  }),
302
- .main_pkg_path = .{ .path = "." },
303
- .root_source_file = .{ .path = path },
304
- .optimize = config.optimize,
305
- .target = config.target,
306
- .filter = test_filter,
423
+ .filters = if (test_filter) |filter| &.{filter} else &.{},
307
424
  });
308
- tests.addOptions("build_options", options);
309
- tests.single_threaded = true;
425
+ tests.root_module.addOptions("build_options", options);
426
+
310
427
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
311
- if (config.pic) tests.force_pic = config.pic;
312
428
  if (coverage) |c| {
313
429
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
314
430
  }
@@ -320,19 +436,17 @@ const TestStep = struct {
320
436
 
321
437
  const ToolConfig = struct {
322
438
  options: Options,
439
+ module: ?*std.Build.Module,
323
440
  general: Config,
324
441
  tool: struct {
325
442
  tests: ?*TestStep,
326
443
  name: ?[]const u8 = null,
327
- exes: *std.ArrayList(*std.Build.CompileStep),
444
+ exes: *ArrayList(*std.Build.Step.Compile),
328
445
  },
329
446
  };
330
447
 
331
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.RunStep {
332
- std.fs.cwd().access(path, .{}) catch |err| switch (err) {
333
- error.FileNotFound => return null,
334
- else => |e| return e,
335
- };
448
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
449
+ if (!try exists(b, path)) return null;
336
450
  var name = config.tool.name orelse std.fs.path.basename(path);
337
451
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
338
452
  if (index) |i| name = name[0..i];
@@ -340,16 +454,23 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.RunSte
340
454
 
341
455
  const exe = b.addExecutable(.{
342
456
  .name = name,
343
- .root_source_file = .{ .path = path },
344
- .target = config.general.target,
345
- .optimize = config.general.optimize,
457
+ .root_module = b.createModule(.{
458
+ .root_source_file = b.path(path),
459
+ .target = config.general.target,
460
+ .optimize = config.general.optimize,
461
+ .strip = config.general.strip,
462
+ .pic = config.general.pic,
463
+ }),
346
464
  });
347
- exe.addModule("pkmn", module(b, config.options));
348
465
 
349
- exe.single_threaded = true;
350
- if (config.general.pic) exe.force_pic = config.general.pic;
466
+ const import = if (config.module) |m| m else module(b, config.options);
467
+ exe.root_module.addImport("pkmn", import);
468
+ if (config.general.target.result.os.tag == .windows) {
469
+ exe.root_module.linkSystemLibrary("advapi32", .{});
470
+ }
471
+
351
472
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
352
- config.tool.exes.append(exe) catch @panic("OOM");
473
+ config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
353
474
 
354
475
  const run = b.addRunArtifact(exe);
355
476
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
@@ -358,10 +479,18 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.RunSte
358
479
  return run;
359
480
  }
360
481
 
482
+ fn exists(b: *std.Build, path: []const u8) !bool {
483
+ std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch |err| switch (err) {
484
+ error.FileNotFound => return false,
485
+ else => |e| return e,
486
+ };
487
+ return true;
488
+ }
489
+
361
490
  const ToolsStep = struct {
362
491
  step: std.Build.Step,
363
492
 
364
- pub fn create(b: *std.Build, exes: *std.ArrayList(*std.Build.CompileStep)) *ToolsStep {
493
+ pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
365
494
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
366
495
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
367
496
  self.* = .{ .step = step };
@@ -369,3 +498,21 @@ const ToolsStep = struct {
369
498
  return self;
370
499
  }
371
500
  };
501
+
502
+ pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
503
+ var symbols: ArrayList([]const u8) = .empty;
504
+
505
+ var it = std.mem.splitSequence(u8, bytes, "export ");
506
+ _ = it.next();
507
+ while (it.next()) |s| {
508
+ if (std.mem.startsWith(u8, s, "const ")) {
509
+ const i = std.mem.indexOf(u8, s[6..], " ").?;
510
+ try symbols.append(b.allocator, s[6 .. 6 + i]);
511
+ } else { // "fn "
512
+ const i = std.mem.indexOf(u8, s[3..], "(").?;
513
+ try symbols.append(b.allocator, s[3 .. 3 + i]);
514
+ }
515
+ }
516
+
517
+ return symbols.items;
518
+ }