@pkmn/engine 0.1.0-dev.deda8593 → 0.1.0-dev.e0a786f8

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