@pkmn/engine 0.1.0-dev.b3197aa3 → 0.1.0-dev.b396399c

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 (47) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +17 -53
  3. package/build/pkg/addon/wasm.js.map +1 -1
  4. package/build/pkg/addon.d.ts +4 -4
  5. package/build/pkg/addon.js.map +1 -1
  6. package/build/pkg/data.js.map +1 -1
  7. package/build/pkg/gen1.js.map +1 -1
  8. package/build/pkg/protocol.d.ts +11 -2
  9. package/build/pkg/protocol.js.map +1 -1
  10. package/build/tools/debug.js +37 -3
  11. package/build/tools/debug.js.map +1 -1
  12. package/build/tools/display/debug.jsx +1 -87
  13. package/build/tools/display/debug.jsx.map +1 -1
  14. package/build/tools/display/ui.jsx +3 -2
  15. package/build/tools/display/ui.jsx.map +1 -1
  16. package/build/tools/display/util.js +125 -1
  17. package/build/tools/display/util.js.map +1 -1
  18. package/build.zig +141 -232
  19. package/package.json +22 -21
  20. package/src/bin/install-pkmn-engine +22 -7
  21. package/src/lib/bindings/c.zig +27 -82
  22. package/src/lib/bindings/node.zig +7 -7
  23. package/src/lib/bindings/wasm.zig +6 -15
  24. package/src/lib/common/array.zig +5 -12
  25. package/src/lib/common/debug.zig +33 -59
  26. package/src/lib/common/js.zig +3 -5
  27. package/src/lib/common/optional.zig +11 -21
  28. package/src/lib/common/protocol.zig +224 -189
  29. package/src/lib/common/rational.zig +18 -38
  30. package/src/lib/common/util.zig +14 -46
  31. package/src/lib/gen1/calc.zig +121 -108
  32. package/src/lib/gen1/chance.zig +112 -75
  33. package/src/lib/gen1/data/types.zig +1 -1
  34. package/src/lib/gen1/data.zig +5 -5
  35. package/src/lib/gen1/helpers.zig +8 -10
  36. package/src/lib/gen1/mechanics.zig +31 -31
  37. package/src/lib/gen2/calc.zig +3 -6
  38. package/src/lib/gen2/chance.zig +12 -18
  39. package/src/lib/gen2/data.zig +5 -5
  40. package/src/lib/gen2/helpers.zig +4 -6
  41. package/src/lib/gen2/mechanics.zig +16 -16
  42. package/src/lib/napi.h +2 -0
  43. package/src/lib/node.zig +1 -1
  44. package/src/lib/pkmn.zig +91 -17
  45. package/src/lib/wasm.zig +2 -6
  46. package/src/tools/display/dom.ts +40 -25
  47. package/src/lib/common/napi.zig +0 -4
package/build.zig CHANGED
@@ -1,6 +1,10 @@
1
1
  const builtin = @import("builtin");
2
2
  const std = @import("std");
3
3
 
4
+ fn ArrayList(comptime T: type) type {
5
+ return std.array_list.Aligned(T, null);
6
+ }
7
+
4
8
  pub const Options = struct {
5
9
  showdown: ?bool = null,
6
10
  log: ?bool = null,
@@ -8,48 +12,27 @@ pub const Options = struct {
8
12
  calc: ?bool = null,
9
13
  };
10
14
 
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
-
22
15
  pub fn module(b: *std.Build, options: Options) *std.Build.Module {
23
- const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
24
16
  const build_options = b.addOptions();
25
17
  build_options.addOption(?bool, "showdown", options.showdown);
26
18
  build_options.addOption(?bool, "log", options.log);
27
19
  build_options.addOption(?bool, "chance", options.chance);
28
20
  build_options.addOption(?bool, "calc", options.calc);
29
21
 
30
- const root_source_file: std.Build.LazyPath =
31
- .{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
32
- const Import = if (@hasDecl(std.Build.Module, "Import"))
33
- std.Build.Module.Import
34
- else
35
- std.Build.ModuleDependency;
36
- const imports: []const Import =
22
+ const root_source_file = b.path("src/lib/pkmn.zig");
23
+ const imports: []const std.Build.Module.Import =
37
24
  &.{.{ .name = "build_options", .module = build_options.createModule() }};
38
- return b.createModule(if (@hasDecl(std.Build, "CreateModuleOptions"))
39
- .{ .source_file = root_source_file, .dependencies = imports }
40
- else
41
- .{ .root_source_file = root_source_file, .imports = imports });
25
+ return b.createModule(.{ .root_source_file = root_source_file, .imports = imports });
42
26
  }
43
27
 
44
28
  pub fn build(b: *std.Build) !void {
45
29
  const target = b.standardTargetOptions(.{});
46
30
  const optimize = b.standardOptimizeOption(.{});
47
31
 
48
- const default: ?bool = if (force_pic) false else null;
49
32
  const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
50
33
  const node_import_lib =
51
34
  b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
52
- const demo = if (try exists("src/tools/demo.zig"))
35
+ const demo = if (try exists(b, "src/tools/demo.zig"))
53
36
  b.option(bool, "demo", "Build the demo library") orelse false
54
37
  else
55
38
  false;
@@ -57,8 +40,8 @@ pub fn build(b: *std.Build) !void {
57
40
  const wasm_stack_size =
58
41
  b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
59
42
  const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
60
- const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse default;
61
- const pic = b.option(bool, "pic", "Force position independent code") orelse default;
43
+ const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
44
+ const pic = b.option(bool, "pic", "Force position independent code");
62
45
  const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
63
46
  const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
64
47
 
@@ -69,16 +52,9 @@ pub fn build(b: *std.Build) !void {
69
52
  defer parsed.deinit();
70
53
  const version = parsed.value.object.get("version").?.string;
71
54
  const description = parsed.value.object.get("description").?.string;
72
- var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
55
+ var repository = std.mem.splitScalar(u8, parsed.value.object.get("repository").?.string, ':');
73
56
  std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
74
57
 
75
- const fast = try std.SemanticVersion.parse("0.12.0-dev.866+3a47bc715");
76
- const patched = std.mem.endsWith(u8, builtin.zig_version_string, "patched");
77
- if (optimize != .Debug and fast.order(builtin.zig_version) == .lt and !patched) {
78
- std.log.warn("Release builds are ~10-20% slower than before Zig " ++
79
- "v0.12.0-dev.866+3a47bc715 due to ziglang/zig#17768", .{});
80
- }
81
-
82
58
  const showdown =
83
59
  b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
84
60
  const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
@@ -111,48 +87,46 @@ pub fn build(b: *std.Build) !void {
111
87
 
112
88
  const name = if (showdown) "pkmn-showdown" else "pkmn";
113
89
 
114
- const pkmn: ?*std.Build.Module = if (@hasDecl(std.Build, "CreateModuleOptions"))
115
- null
116
- else
117
- b.addModule("pkmn", .{
118
- .root_source_file = if (has_path)
119
- .{ .path = "src/lib/pkmn.zig" }
120
- else
121
- b.path("src/lib/pkmn.zig"),
122
- .optimize = optimize,
123
- .target = target,
124
- .imports = &.{.{ .name = "build_options", .module = options.createModule() }},
125
- });
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
+ });
126
96
 
127
97
  var c = false;
128
98
  if (node_headers) |headers| {
129
- const addon = b.fmt("{s}.node", .{name});
130
- const path = if (has_path) .{ .path = "src/lib/node.zig" } else b.path("src/lib/node.zig");
131
- const lib = b.addSharedLibrary(if (force_pic) .{
132
- .name = addon,
133
- .root_source_file = path,
134
- .optimize = optimize,
99
+ const translate_c = b.addTranslateC(.{
100
+ .root_source_file = b.path("src/lib/napi.h"),
135
101
  .target = target,
136
- } else .{
137
- .name = addon,
138
- .root_source_file = path,
139
102
  .optimize = optimize,
140
- .target = target,
141
- .strip = strip,
142
- .pic = pic,
143
103
  });
144
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
145
- lib.addSystemIncludePath(if (has_path) .{ .path = headers } else b.path(headers));
146
- lib.linkLibC();
104
+ translate_c.addSystemIncludePath(b.path(headers));
105
+ const addon = b.fmt("{s}.node", .{name});
106
+ const path = b.path("src/lib/node.zig");
107
+ const lib = b.addLibrary(.{
108
+ .linkage = .dynamic,
109
+ .name = addon,
110
+ .root_module = b.createModule(.{
111
+ .root_source_file = path,
112
+ .optimize = optimize,
113
+ .target = target,
114
+ .strip = strip,
115
+ .pic = pic,
116
+ }),
117
+ });
118
+ lib.root_module.addOptions("build_options", options);
119
+ lib.root_module.addImport("napi", translate_c.createModule());
120
+ lib.root_module.link_libc = true;
147
121
  if (node_import_lib) |il| {
148
- lib.addObjectFile(if (has_path) .{ .path = il } else b.path(il));
149
- } else if (detect(target).os.tag == .windows) {
150
- 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");
151
126
  std.process.exit(1);
152
127
  }
153
128
  lib.linker_allow_shlib_undefined = true;
154
129
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
155
- if (force_pic and pic orelse false) lib.force_pic = pic;
156
130
  // Always emit to build/lib because this is where the driver code expects to find it
157
131
  // TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
158
132
  //
@@ -168,56 +142,45 @@ pub fn build(b: *std.Build) !void {
168
142
  } else if (demo) {
169
143
  const path = "src/tools/demo.zig";
170
144
  const n = if (showdown) "demo-showdown" else "demo";
171
- const mod = pkmn orelse module(b, .{
172
- .showdown = showdown,
173
- .log = log,
174
- .chance = chance,
175
- .calc = calc,
176
- });
145
+ const mod = pkmn;
177
146
  try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
178
147
  } else if (dynamic) {
179
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
180
- const lib = b.addSharedLibrary(if (force_pic) .{
181
- .name = name,
182
- .root_source_file = path,
183
- .version = try std.SemanticVersion.parse(version),
184
- .optimize = optimize,
185
- .target = target,
186
- } else .{
148
+ const path = b.path("src/lib/c.zig");
149
+ const lib = b.addLibrary(.{
150
+ .linkage = .dynamic,
187
151
  .name = name,
188
- .root_source_file = path,
189
- .version = try std.SemanticVersion.parse(version),
190
- .optimize = optimize,
191
- .target = target,
192
- .strip = strip,
193
- .pic = pic,
152
+ .root_module = b.createModule(.{
153
+ .root_source_file = path,
154
+ .optimize = optimize,
155
+ .target = target,
156
+ .strip = strip,
157
+ .pic = pic,
158
+ }),
194
159
  });
195
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
196
- lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
160
+ lib.root_module.addOptions("build_options", options);
161
+ lib.root_module.addIncludePath(b.path("src/include"));
197
162
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
198
- if (force_pic and pic orelse false) lib.force_pic = pic;
199
163
  b.installArtifact(lib);
200
164
  c = true;
201
165
  } else {
202
- const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
203
- const lib = b.addStaticLibrary(if (force_pic) .{
204
- .name = name,
205
- .root_source_file = path,
206
- .optimize = optimize,
207
- .target = target,
208
- } else .{
166
+ const path = b.path("src/lib/c.zig");
167
+ const lib = b.addLibrary(.{
168
+ .linkage = .static,
209
169
  .name = name,
210
- .root_source_file = path,
211
- .optimize = optimize,
212
- .target = target,
213
- .strip = strip,
214
- .pic = pic,
170
+ .root_module = b.createModule(.{
171
+ .root_source_file = path,
172
+ .optimize = optimize,
173
+ .target = target,
174
+ .strip = strip,
175
+ .pic = pic,
176
+ }),
215
177
  });
216
- (if (root_module) lib.root_module else lib).addOptions("build_options", options);
217
- lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
218
- 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
+ }
219
183
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
220
- if (force_pic and pic orelse false) lib.force_pic = pic;
221
184
  if (emit_asm) {
222
185
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
223
186
  lib.getEmittedAsm(),
@@ -237,22 +200,25 @@ pub fn build(b: *std.Build) !void {
237
200
  }
238
201
 
239
202
  if (c) {
240
- const path =
241
- if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
203
+ const path = b.path("src/include/pkmn.h");
242
204
  const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
243
205
  b.getInstallStep().dependOn(&header.step);
244
206
 
245
207
  const pc = b.fmt("lib{s}.pc", .{name});
246
- const file = try std.fs.path.relative(
208
+ const cwd = try std.process.currentPathAlloc(b.graph.io, b.allocator);
209
+ const file = try std.Io.Dir.path.relative(
247
210
  b.allocator,
248
- try std.process.getCwdAlloc(b.allocator),
211
+ cwd,
212
+ &b.graph.environ_map,
213
+ cwd,
249
214
  try b.cache_root.join(b.allocator, &.{pc}),
250
215
  );
251
- const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
216
+ const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
217
+ defer pkgconfig_file.close(b.graph.io);
252
218
 
253
219
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
254
- const writer = pkgconfig_file.writer();
255
- try writer.print(
220
+ var writer = pkgconfig_file.writer(b.graph.io, &.{});
221
+ try writer.interface.print(
256
222
  \\prefix={0s}/{1s}
257
223
  \\includedir=${{prefix}}/include
258
224
  \\libdir=${{prefix}}/lib
@@ -264,7 +230,6 @@ pub fn build(b: *std.Build) !void {
264
230
  \\Cflags: -I${{includedir}}
265
231
  \\Libs: -L${{libdir}} -l{2s}
266
232
  , .{ dirname, b.install_path, name, repository.next().?, description, version });
267
- defer pkgconfig_file.close();
268
233
 
269
234
  b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
270
235
  }
@@ -280,7 +245,7 @@ pub fn build(b: *std.Build) !void {
280
245
  // TODO: tests can be run multiple times due to @imports
281
246
  const tests = TestStep.create(b, options, config);
282
247
 
283
- var exes = std.ArrayList(*Step.Compile).init(b.allocator);
248
+ var exes: ArrayList(*std.Build.Step.Compile) = .empty;
284
249
  const tools: ToolConfig = .{
285
250
  .options = .{
286
251
  .showdown = showdown,
@@ -338,7 +303,6 @@ fn buildWasm(
338
303
  import: ?*std.Build.Module,
339
304
  options: anytype,
340
305
  ) !void {
341
- const entry = @hasDecl(Step.Compile, "Entry");
342
306
  const mode = switch (optimize) {
343
307
  .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
344
308
  else => optimize,
@@ -362,92 +326,58 @@ fn buildWasm(
362
326
  if (builtin.zig_version.order(try std.SemanticVersion.parse("0.12.0")) != .lt) {
363
327
  features.addFeature(@intFromEnum(std.Target.wasm.Feature.multivalue));
364
328
  }
365
- const freestanding = if (@hasDecl(std.Build, "resolveTargetQuery"))
366
- b.resolveTargetQuery(.{
367
- .cpu_arch = .wasm32,
368
- .os_tag = .freestanding,
369
- .cpu_features_add = features,
370
- })
371
- else
372
- ResolvedTarget{
373
- .cpu_arch = .wasm32,
374
- .os_tag = .freestanding,
375
- .cpu_features_add = features,
376
- };
377
- const path = if (has_path) .{ .path = root_src_file } else b.path(root_src_file);
378
- const bin = if (entry) bin: {
379
- const exe = b.addExecutable(if (force_pic) .{
380
- .name = name,
381
- .root_source_file = path,
382
- .optimize = mode,
383
- .target = freestanding,
384
- } else .{
385
- .name = name,
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(.{
386
338
  .root_source_file = path,
387
339
  .optimize = mode,
388
340
  .target = freestanding,
389
341
  .strip = strip,
390
342
  .pic = pic,
391
- });
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;
392
352
 
393
- var file = try std.fs.cwd().openFile(root_src_file, .{});
394
- defer file.close();
395
- const bytes = try file.readToEndAlloc(b.allocator, std.math.maxInt(usize));
396
- (if (root_module) exe.root_module else exe).export_symbol_names = try exports(b, bytes);
397
- exe.entry = .disabled;
398
- break :bin exe;
399
- } else bin: {
400
- const lib = b.addSharedLibrary(.{
401
- .name = name,
402
- .root_source_file = path,
403
- .optimize = mode,
404
- .target = freestanding,
405
- });
406
- lib.rdynamic = true;
407
- break :bin lib;
408
- };
409
353
  if (import) |i| {
410
- if (root_module) {
411
- bin.root_module.addImport("pkmn", i);
412
- } else {
413
- bin.addModule("pkmn", i);
414
- }
354
+ exe.root_module.addImport("pkmn", i);
415
355
  }
416
- bin.stack_size = wasm_stack_size;
417
- if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
418
- if (force_pic and pic orelse false) bin.force_pic = pic;
419
- (if (root_module) bin.root_module else bin).addOptions("build_options", options);
356
+ exe.stack_size = wasm_stack_size;
357
+ exe.root_module.addOptions("build_options", options);
420
358
 
421
359
  const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
422
360
  if (optimize != .Debug and opt != null) {
423
361
  const out = b.fmt("build/lib/{s}.wasm", .{name});
424
362
  const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
425
- sh.addArtifactArg(bin);
363
+ sh.addArtifactArg(exe);
426
364
  sh.addArg("-o");
427
- if (@hasDecl(@TypeOf(sh.*), "addFileSourceArg")) {
428
- sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
429
- } else {
430
- sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
431
- }
365
+ sh.addFileArg(b.path(out));
432
366
  b.getInstallStep().dependOn(&sh.step);
433
- if (!entry) b.installArtifact(bin);
434
- } else if (entry) {
435
- b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
367
+ } else {
368
+ b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
436
369
  .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
437
370
  }).step);
438
- } else {
439
- b.installArtifact(bin);
440
371
  }
441
372
  }
442
373
 
443
374
  fn maybeStrip(
444
375
  b: *std.Build,
445
- artifact: *Step.Compile,
376
+ artifact: *std.Build.Step.Compile,
446
377
  step: *std.Build.Step,
447
378
  strip: ?bool,
448
379
  cmd: ?[]const u8,
449
380
  ) void {
450
- if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
451
381
  if (!(strip orelse false) or cmd == null) return;
452
382
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
453
383
  const mac = builtin.os.tag == .macos;
@@ -460,7 +390,7 @@ fn maybeStrip(
460
390
  }
461
391
 
462
392
  const Config = struct {
463
- target: ResolvedTarget,
393
+ target: std.Build.ResolvedTarget,
464
394
  optimize: std.builtin.OptimizeMode,
465
395
  pic: ?bool,
466
396
  strip: ?bool,
@@ -471,7 +401,7 @@ const TestStep = struct {
471
401
  step: std.Build.Step,
472
402
  build: bool,
473
403
 
474
- pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
404
+ pub fn create(b: *std.Build, options: *std.Build.Step.Options, config: Config) *TestStep {
475
405
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
476
406
  const test_filter =
477
407
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
@@ -480,28 +410,21 @@ const TestStep = struct {
480
410
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
481
411
  self.* = .{ .step = step, .build = test_filter == null };
482
412
 
483
- const path = if (has_path) .{ .path = "src/lib/test.zig" } else b.path("src/lib/test.zig");
484
- const tests = b.addTest(if (force_pic) .{
485
- .root_source_file = path,
486
- .optimize = config.optimize,
487
- .target = config.target,
488
- .filter = test_filter,
489
- } else .{
490
- .root_source_file = path,
491
- .optimize = config.optimize,
492
- .target = config.target,
493
- .filter = test_filter,
494
- .single_threaded = true,
495
- .strip = config.strip,
496
- .pic = config.pic,
413
+ const path = b.path("src/lib/test.zig");
414
+ const tests = b.addTest(.{
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,
422
+ }),
423
+ .filters = if (test_filter) |filter| &.{filter} else &.{},
497
424
  });
498
- (if (root_module) tests.root_module else tests).addOptions("build_options", options);
425
+ tests.root_module.addOptions("build_options", options);
499
426
 
500
427
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
501
- if (force_pic) {
502
- tests.single_threaded = true;
503
- if (config.pic orelse false) tests.force_pic = config.pic;
504
- }
505
428
  if (coverage) |c| {
506
429
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
507
430
  }
@@ -518,45 +441,36 @@ const ToolConfig = struct {
518
441
  tool: struct {
519
442
  tests: ?*TestStep,
520
443
  name: ?[]const u8 = null,
521
- exes: *std.ArrayList(*Step.Compile),
444
+ exes: *ArrayList(*std.Build.Step.Compile),
522
445
  },
523
446
  };
524
447
 
525
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
526
- if (!try exists(path)) return null;
448
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
449
+ if (!try exists(b, path)) return null;
527
450
  var name = config.tool.name orelse std.fs.path.basename(path);
528
451
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
529
452
  if (index) |i| name = name[0..i];
530
453
  if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
531
454
 
532
- const exe = b.addExecutable(if (force_pic) .{
455
+ const exe = b.addExecutable(.{
533
456
  .name = name,
534
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
535
- .target = config.general.target,
536
- .optimize = config.general.optimize,
537
- } else .{
538
- .name = name,
539
- .root_source_file = if (has_path) .{ .path = path } else b.path(path),
540
- .target = config.general.target,
541
- .optimize = config.general.optimize,
542
- .single_threaded = true,
543
- .strip = config.general.strip,
544
- .pic = config.general.pic,
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
+ }),
545
464
  });
546
465
 
547
- if (root_module) {
548
- const import = if (config.module) |m| m else module(b, config.options);
549
- exe.root_module.addImport("pkmn", import);
550
- } else {
551
- exe.addModule("pkmn", module(b, config.options));
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", .{});
552
470
  }
553
471
 
554
- if (force_pic) {
555
- exe.single_threaded = true;
556
- if (config.general.pic orelse false) exe.force_pic = config.general.pic;
557
- }
558
472
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
559
- config.tool.exes.append(exe) catch @panic("OOM");
473
+ config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
560
474
 
561
475
  const run = b.addRunArtifact(exe);
562
476
  maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
@@ -565,8 +479,8 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
565
479
  return run;
566
480
  }
567
481
 
568
- fn exists(path: []const u8) !bool {
569
- std.fs.cwd().access(path, .{}) catch |err| switch (err) {
482
+ fn exists(b: *std.Build, path: []const u8) !bool {
483
+ std.Io.Dir.cwd().access(b.graph.io, path, .{}) catch |err| switch (err) {
570
484
  error.FileNotFound => return false,
571
485
  else => |e| return e,
572
486
  };
@@ -576,7 +490,7 @@ fn exists(path: []const u8) !bool {
576
490
  const ToolsStep = struct {
577
491
  step: std.Build.Step,
578
492
 
579
- pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
493
+ pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
580
494
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
581
495
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
582
496
  self.* = .{ .step = step };
@@ -585,23 +499,18 @@ const ToolsStep = struct {
585
499
  }
586
500
  };
587
501
 
588
- fn detect(target: anytype) std.Target {
589
- if (@hasField(@TypeOf(target), "result")) return target.result;
590
- return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
591
- }
592
-
593
502
  pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
594
- var symbols = std.ArrayList([]const u8).init(b.allocator);
503
+ var symbols: ArrayList([]const u8) = .empty;
595
504
 
596
505
  var it = std.mem.splitSequence(u8, bytes, "export ");
597
506
  _ = it.next();
598
507
  while (it.next()) |s| {
599
508
  if (std.mem.startsWith(u8, s, "const ")) {
600
509
  const i = std.mem.indexOf(u8, s[6..], " ").?;
601
- try symbols.append(s[6 .. 6 + i]);
510
+ try symbols.append(b.allocator, s[6 .. 6 + i]);
602
511
  } else { // "fn "
603
512
  const i = std.mem.indexOf(u8, s[3..], "(").?;
604
- try symbols.append(s[3 .. 3 + i]);
513
+ try symbols.append(b.allocator, s[3 .. 3 + i]);
605
514
  }
606
515
  }
607
516