@pkmn/engine 0.1.0-dev.565de616 → 0.1.0-dev.587596a6
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.
- package/LICENSE +1 -1
- package/README.md +21 -13
- package/build/pkg/addon/browser.js +4 -1
- package/build/pkg/addon/browser.js.map +1 -1
- package/build/pkg/addon/node.js +20 -7
- package/build/pkg/addon/node.js.map +1 -1
- package/build/pkg/addon/wasm.js +41 -9
- package/build/pkg/addon/wasm.js.map +1 -1
- package/build/pkg/addon.d.ts +7 -7
- package/build/pkg/addon.js +8 -17
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/gen1.d.ts +5 -5
- package/build/pkg/gen1.js +28 -19
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +6 -97
- package/build/pkg/index.js +23 -75
- package/build/pkg/index.js.map +1 -1
- package/build/tools/debug.js +36 -9
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +18 -7
- package/build/tools/display/debug.jsx.map +1 -1
- package/build/tools/display/index.js +18 -7
- package/build/tools/display/index.js.map +1 -1
- package/build/tools/display/ui.jsx +18 -8
- package/build/tools/display/ui.jsx.map +1 -1
- package/build/tools/display/util.js +16 -1
- package/build/tools/display/util.js.map +1 -1
- package/build.zig +181 -198
- package/package.json +31 -130
- package/src/bin/install-pkmn-engine +24 -8
- package/src/lib/bindings/c.zig +200 -0
- package/src/lib/bindings/node.zig +117 -0
- package/src/lib/bindings/wasm.zig +50 -0
- package/src/lib/common/array.zig +5 -12
- package/src/lib/common/debug.zig +11 -21
- package/src/lib/common/js.zig +3 -9
- package/src/lib/common/optional.zig +11 -21
- package/src/lib/common/protocol.zig +176 -163
- package/src/lib/common/rational.zig +275 -284
- package/src/lib/common/util.zig +14 -31
- package/src/lib/gen1/calc.zig +65 -62
- package/src/lib/gen1/chance.zig +17 -32
- package/src/lib/gen1/data/types.zig +2 -2
- package/src/lib/gen1/data.zig +5 -5
- package/src/lib/gen1/helpers.zig +8 -10
- package/src/lib/gen1/mechanics.zig +32 -25
- package/src/lib/gen2/calc.zig +3 -6
- package/src/lib/gen2/chance.zig +12 -18
- package/src/lib/gen2/data/types.zig +6 -3
- package/src/lib/gen2/data.zig +8 -7
- package/src/lib/gen2/helpers.zig +4 -6
- package/src/lib/gen2/mechanics.zig +28 -17
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +3 -118
- package/src/lib/pkmn.zig +94 -19
- package/src/lib/wasm.zig +10 -9
- package/src/tools/display/dom.ts +5 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/tools/display/util.ts"],"names":[],"mappings":";;;AA2BA,MAAM,MAAM,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAe,EAAE,CAAC;AACvF,MAAM,IAAI,GAAG,CAAC,IAAc,EAAE,EAAE;IAC9B,OAAO,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAAE,IAAI,CAAC,GAAG,EAAE,CAAC;IACzD,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,CAAC,IAAgB,EAAE,EAAE,CACnC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAa,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;SAClE,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,MAAM,CAAE,IAAI,CAAC,MAAc,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE9D,MAAM,MAAM,GAAG,CAAC,MAAoB,EAAE,EAAE,CAC7C,MAAM,CAA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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,17 +12,6 @@ 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
16
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
24
17
|
const build_options = b.addOptions();
|
|
@@ -27,28 +20,30 @@ pub fn module(b: *std.Build, options: Options) *std.Build.Module {
|
|
|
27
20
|
build_options.addOption(?bool, "chance", options.chance);
|
|
28
21
|
build_options.addOption(?bool, "calc", options.calc);
|
|
29
22
|
|
|
30
|
-
const root_source_file
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.{ .
|
|
34
|
-
|
|
35
|
-
.{ .root_source_file = root_source_file, .imports = imports });
|
|
23
|
+
const root_source_file: std.Build.LazyPath =
|
|
24
|
+
.{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
|
|
25
|
+
const imports: []const std.Build.Module.Import =
|
|
26
|
+
&.{.{ .name = "build_options", .module = build_options.createModule() }};
|
|
27
|
+
return b.createModule(.{ .root_source_file = root_source_file, .imports = imports });
|
|
36
28
|
}
|
|
37
29
|
|
|
38
30
|
pub fn build(b: *std.Build) !void {
|
|
39
31
|
const target = b.standardTargetOptions(.{});
|
|
40
32
|
const optimize = b.standardOptimizeOption(.{});
|
|
41
33
|
|
|
42
|
-
const default: ?bool = if (force_pic) false else null;
|
|
43
34
|
const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
|
|
44
35
|
const node_import_lib =
|
|
45
36
|
b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
|
|
37
|
+
const demo = if (try exists(b, "src/tools/demo.zig"))
|
|
38
|
+
b.option(bool, "demo", "Build the demo library") orelse false
|
|
39
|
+
else
|
|
40
|
+
false;
|
|
46
41
|
const wasm = b.option(bool, "wasm", "Build a WASM library") orelse false;
|
|
47
42
|
const wasm_stack_size =
|
|
48
43
|
b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
|
|
49
44
|
const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
|
|
50
|
-
const strip = b.option(bool, "strip", "Strip debugging symbols from binary")
|
|
51
|
-
const pic = b.option(bool, "pic", "Force position independent code")
|
|
45
|
+
const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
|
|
46
|
+
const pic = b.option(bool, "pic", "Force position independent code");
|
|
52
47
|
const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
|
|
53
48
|
const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
|
|
54
49
|
|
|
@@ -62,12 +57,6 @@ pub fn build(b: *std.Build) !void {
|
|
|
62
57
|
var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
|
|
63
58
|
std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
|
|
64
59
|
|
|
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
|
-
|
|
71
60
|
const showdown =
|
|
72
61
|
b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
|
|
73
62
|
const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
|
|
@@ -100,11 +89,8 @@ pub fn build(b: *std.Build) !void {
|
|
|
100
89
|
|
|
101
90
|
const name = if (showdown) "pkmn-showdown" else "pkmn";
|
|
102
91
|
|
|
103
|
-
const pkmn
|
|
104
|
-
.root_source_file =
|
|
105
|
-
.{ .path = "src/lib/pkmn.zig" }
|
|
106
|
-
else
|
|
107
|
-
b.path("src/lib/pkmn.zig"),
|
|
92
|
+
const pkmn: *std.Build.Module = b.addModule("pkmn", .{
|
|
93
|
+
.root_source_file = b.path("src/lib/pkmn.zig"),
|
|
108
94
|
.optimize = optimize,
|
|
109
95
|
.target = target,
|
|
110
96
|
.imports = &.{.{ .name = "build_options", .module = options.createModule() }},
|
|
@@ -112,33 +98,37 @@ pub fn build(b: *std.Build) !void {
|
|
|
112
98
|
|
|
113
99
|
var c = false;
|
|
114
100
|
if (node_headers) |headers| {
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
const lib = b.addSharedLibrary(if (force_pic) .{
|
|
118
|
-
.name = addon,
|
|
119
|
-
.root_source_file = path,
|
|
120
|
-
.optimize = optimize,
|
|
101
|
+
const translate_c = b.addTranslateC(.{
|
|
102
|
+
.root_source_file = b.path("src/lib/napi.h"),
|
|
121
103
|
.target = target,
|
|
122
|
-
} else .{
|
|
123
|
-
.name = addon,
|
|
124
|
-
.root_source_file = path,
|
|
125
104
|
.optimize = optimize,
|
|
126
|
-
.target = target,
|
|
127
|
-
.strip = strip,
|
|
128
|
-
.pic = pic,
|
|
129
105
|
});
|
|
130
|
-
(
|
|
131
|
-
|
|
132
|
-
lib.
|
|
106
|
+
translate_c.addSystemIncludePath(b.path(headers));
|
|
107
|
+
const addon = b.fmt("{s}.node", .{name});
|
|
108
|
+
const path = b.path("src/lib/node.zig");
|
|
109
|
+
const lib = b.addLibrary(.{
|
|
110
|
+
.linkage = .dynamic,
|
|
111
|
+
.name = addon,
|
|
112
|
+
.root_module = b.createModule(.{
|
|
113
|
+
.root_source_file = path,
|
|
114
|
+
.optimize = optimize,
|
|
115
|
+
.target = target,
|
|
116
|
+
.strip = strip,
|
|
117
|
+
.pic = pic,
|
|
118
|
+
}),
|
|
119
|
+
});
|
|
120
|
+
lib.root_module.addOptions("build_options", options);
|
|
121
|
+
lib.root_module.addImport("napi", translate_c.createModule());
|
|
122
|
+
lib.root_module.link_libc = true;
|
|
133
123
|
if (node_import_lib) |il| {
|
|
134
|
-
lib.addObjectFile(
|
|
135
|
-
} else if (
|
|
136
|
-
|
|
124
|
+
lib.root_module.addObjectFile(b.path(il));
|
|
125
|
+
} else if (target.result.os.tag == .windows) {
|
|
126
|
+
var err = std.Io.File.stderr().writer(b.graph.io, &.{});
|
|
127
|
+
try err.interface.writeAll("Must provide --node-import-library path on Windows\n");
|
|
137
128
|
std.process.exit(1);
|
|
138
129
|
}
|
|
139
130
|
lib.linker_allow_shlib_undefined = true;
|
|
140
131
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
141
|
-
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
142
132
|
// Always emit to build/lib because this is where the driver code expects to find it
|
|
143
133
|
// TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
|
|
144
134
|
//
|
|
@@ -149,50 +139,48 @@ pub fn build(b: *std.Build) !void {
|
|
|
149
139
|
// rename the file ourself in install-pkmn-engine
|
|
150
140
|
b.installArtifact(lib);
|
|
151
141
|
} else if (wasm) {
|
|
152
|
-
|
|
142
|
+
const path = "src/lib/wasm.zig";
|
|
143
|
+
try buildWasm(b, name, path, optimize, strip, pic, wasm_stack_size, null, options);
|
|
144
|
+
} else if (demo) {
|
|
145
|
+
const path = "src/tools/demo.zig";
|
|
146
|
+
const n = if (showdown) "demo-showdown" else "demo";
|
|
147
|
+
const mod = pkmn;
|
|
148
|
+
try buildWasm(b, n, path, optimize, strip, pic, wasm_stack_size, mod, options);
|
|
153
149
|
} else if (dynamic) {
|
|
154
|
-
const path =
|
|
155
|
-
const lib = b.
|
|
150
|
+
const path = b.path("src/lib/c.zig");
|
|
151
|
+
const lib = b.addLibrary(.{
|
|
152
|
+
.linkage = .dynamic,
|
|
156
153
|
.name = name,
|
|
157
|
-
.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.version = try std.SemanticVersion.parse(version),
|
|
165
|
-
.optimize = optimize,
|
|
166
|
-
.target = target,
|
|
167
|
-
.strip = strip,
|
|
168
|
-
.pic = pic,
|
|
154
|
+
.root_module = b.createModule(.{
|
|
155
|
+
.root_source_file = path,
|
|
156
|
+
.optimize = optimize,
|
|
157
|
+
.target = target,
|
|
158
|
+
.strip = strip,
|
|
159
|
+
.pic = pic,
|
|
160
|
+
}),
|
|
169
161
|
});
|
|
170
|
-
|
|
171
|
-
lib.addIncludePath(
|
|
162
|
+
lib.root_module.addOptions("build_options", options);
|
|
163
|
+
lib.root_module.addIncludePath(b.path("src/include"));
|
|
172
164
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
173
|
-
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
174
165
|
b.installArtifact(lib);
|
|
175
166
|
c = true;
|
|
176
167
|
} else {
|
|
177
|
-
const path =
|
|
178
|
-
const lib = b.
|
|
168
|
+
const path = b.path("src/lib/c.zig");
|
|
169
|
+
const lib = b.addLibrary(.{
|
|
170
|
+
.linkage = .static,
|
|
179
171
|
.name = name,
|
|
180
|
-
.
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
.target = target,
|
|
188
|
-
.strip = strip,
|
|
189
|
-
.pic = pic,
|
|
172
|
+
.root_module = b.createModule(.{
|
|
173
|
+
.root_source_file = path,
|
|
174
|
+
.optimize = optimize,
|
|
175
|
+
.target = target,
|
|
176
|
+
.strip = strip,
|
|
177
|
+
.pic = pic,
|
|
178
|
+
}),
|
|
190
179
|
});
|
|
191
|
-
|
|
192
|
-
lib.addIncludePath(
|
|
180
|
+
lib.root_module.addOptions("build_options", options);
|
|
181
|
+
lib.root_module.addIncludePath(b.path("src/include"));
|
|
193
182
|
lib.bundle_compiler_rt = true;
|
|
194
183
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
195
|
-
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
196
184
|
if (emit_asm) {
|
|
197
185
|
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
198
186
|
lib.getEmittedAsm(),
|
|
@@ -212,22 +200,25 @@ pub fn build(b: *std.Build) !void {
|
|
|
212
200
|
}
|
|
213
201
|
|
|
214
202
|
if (c) {
|
|
215
|
-
const path =
|
|
216
|
-
if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
|
|
203
|
+
const path = b.path("src/include/pkmn.h");
|
|
217
204
|
const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
|
|
218
205
|
b.getInstallStep().dependOn(&header.step);
|
|
219
206
|
|
|
220
207
|
const pc = b.fmt("lib{s}.pc", .{name});
|
|
221
|
-
const
|
|
208
|
+
const cwd = try std.process.getCwdAlloc(b.allocator);
|
|
209
|
+
const file = try std.Io.Dir.path.relative(
|
|
222
210
|
b.allocator,
|
|
223
211
|
try std.process.getCwdAlloc(b.allocator),
|
|
212
|
+
&b.graph.environ_map,
|
|
213
|
+
cwd,
|
|
224
214
|
try b.cache_root.join(b.allocator, &.{pc}),
|
|
225
215
|
);
|
|
226
|
-
const pkgconfig_file = try std.
|
|
216
|
+
const pkgconfig_file = try std.Io.Dir.cwd().createFile(b.graph.io, file, .{});
|
|
217
|
+
defer pkgconfig_file.close(b.graph.io);
|
|
227
218
|
|
|
228
219
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
229
|
-
|
|
230
|
-
try writer.print(
|
|
220
|
+
var writer = pkgconfig_file.writer(b.graph.io, &.{});
|
|
221
|
+
try writer.interface.print(
|
|
231
222
|
\\prefix={0s}/{1s}
|
|
232
223
|
\\includedir=${{prefix}}/include
|
|
233
224
|
\\libdir=${{prefix}}/lib
|
|
@@ -239,12 +230,11 @@ pub fn build(b: *std.Build) !void {
|
|
|
239
230
|
\\Cflags: -I${{includedir}}
|
|
240
231
|
\\Libs: -L${{libdir}} -l{2s}
|
|
241
232
|
, .{ dirname, b.install_path, name, repository.next().?, description, version });
|
|
242
|
-
defer pkgconfig_file.close();
|
|
243
233
|
|
|
244
234
|
b.installFile(file, b.fmt("share/pkgconfig/{s}", .{pc}));
|
|
245
235
|
}
|
|
246
236
|
|
|
247
|
-
const config = .{
|
|
237
|
+
const config: Config = .{
|
|
248
238
|
.target = target,
|
|
249
239
|
.optimize = optimize,
|
|
250
240
|
.pic = pic,
|
|
@@ -255,7 +245,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
255
245
|
// TODO: tests can be run multiple times due to @imports
|
|
256
246
|
const tests = TestStep.create(b, options, config);
|
|
257
247
|
|
|
258
|
-
var exes
|
|
248
|
+
var exes: ArrayList(*std.Build.Step.Compile) = .empty;
|
|
259
249
|
const tools: ToolConfig = .{
|
|
260
250
|
.options = .{
|
|
261
251
|
.showdown = showdown,
|
|
@@ -310,65 +300,72 @@ fn buildWasm(
|
|
|
310
300
|
strip: ?bool,
|
|
311
301
|
pic: ?bool,
|
|
312
302
|
wasm_stack_size: u64,
|
|
303
|
+
import: ?*std.Build.Module,
|
|
313
304
|
options: anytype,
|
|
314
305
|
) !void {
|
|
315
|
-
const entry = @hasDecl(Step.Compile, "Entry");
|
|
316
306
|
const mode = switch (optimize) {
|
|
317
307
|
.ReleaseFast, .ReleaseSafe => .ReleaseSmall,
|
|
318
308
|
else => optimize,
|
|
319
309
|
};
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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(.{
|
|
333
338
|
.root_source_file = path,
|
|
334
339
|
.optimize = mode,
|
|
335
340
|
.target = freestanding,
|
|
336
341
|
.strip = strip,
|
|
337
342
|
.pic = pic,
|
|
338
|
-
})
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
|
|
354
|
-
if (force_pic and pic orelse false) bin.force_pic = pic;
|
|
355
|
-
(if (root_module) bin.root_module else bin).addOptions("build_options", options);
|
|
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);
|
|
356
358
|
|
|
357
359
|
const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
|
|
358
360
|
if (optimize != .Debug and opt != null) {
|
|
359
361
|
const out = b.fmt("build/lib/{s}.wasm", .{name});
|
|
360
|
-
const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
|
|
361
|
-
sh.addArtifactArg(
|
|
362
|
+
const sh = b.addSystemCommand(&.{ opt.?, "--enable-bulk-memory", "--enable-simd", "-O4" });
|
|
363
|
+
sh.addArtifactArg(exe);
|
|
362
364
|
sh.addArg("-o");
|
|
363
|
-
|
|
364
|
-
sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
|
|
365
|
-
} else {
|
|
366
|
-
sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
|
|
367
|
-
}
|
|
365
|
+
sh.addFileArg(b.path(out));
|
|
368
366
|
b.getInstallStep().dependOn(&sh.step);
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
|
|
367
|
+
} else {
|
|
368
|
+
b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{
|
|
372
369
|
.dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
|
|
373
370
|
}).step);
|
|
374
371
|
}
|
|
@@ -376,12 +373,11 @@ fn buildWasm(
|
|
|
376
373
|
|
|
377
374
|
fn maybeStrip(
|
|
378
375
|
b: *std.Build,
|
|
379
|
-
artifact: *Step.Compile,
|
|
376
|
+
artifact: *std.Build.Step.Compile,
|
|
380
377
|
step: *std.Build.Step,
|
|
381
378
|
strip: ?bool,
|
|
382
379
|
cmd: ?[]const u8,
|
|
383
380
|
) void {
|
|
384
|
-
if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
|
|
385
381
|
if (!(strip orelse false) or cmd == null) return;
|
|
386
382
|
// Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
|
|
387
383
|
const mac = builtin.os.tag == .macos;
|
|
@@ -394,7 +390,7 @@ fn maybeStrip(
|
|
|
394
390
|
}
|
|
395
391
|
|
|
396
392
|
const Config = struct {
|
|
397
|
-
target: ResolvedTarget,
|
|
393
|
+
target: std.Build.ResolvedTarget,
|
|
398
394
|
optimize: std.builtin.OptimizeMode,
|
|
399
395
|
pic: ?bool,
|
|
400
396
|
strip: ?bool,
|
|
@@ -405,7 +401,7 @@ const TestStep = struct {
|
|
|
405
401
|
step: std.Build.Step,
|
|
406
402
|
build: bool,
|
|
407
403
|
|
|
408
|
-
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 {
|
|
409
405
|
const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
|
|
410
406
|
const test_filter =
|
|
411
407
|
b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
|
@@ -414,28 +410,21 @@ const TestStep = struct {
|
|
|
414
410
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
|
|
415
411
|
self.* = .{ .step = step, .build = test_filter == null };
|
|
416
412
|
|
|
417
|
-
const path =
|
|
418
|
-
const tests = b.addTest(
|
|
419
|
-
.
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
.
|
|
428
|
-
.single_threaded = true,
|
|
429
|
-
.strip = config.strip,
|
|
430
|
-
.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 &.{},
|
|
431
424
|
});
|
|
432
|
-
|
|
425
|
+
tests.root_module.addOptions("build_options", options);
|
|
433
426
|
|
|
434
427
|
maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
|
|
435
|
-
if (force_pic) {
|
|
436
|
-
tests.single_threaded = true;
|
|
437
|
-
if (config.pic orelse false) tests.force_pic = config.pic;
|
|
438
|
-
}
|
|
439
428
|
if (coverage) |c| {
|
|
440
429
|
tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
|
|
441
430
|
}
|
|
@@ -452,48 +441,36 @@ const ToolConfig = struct {
|
|
|
452
441
|
tool: struct {
|
|
453
442
|
tests: ?*TestStep,
|
|
454
443
|
name: ?[]const u8 = null,
|
|
455
|
-
exes: *
|
|
444
|
+
exes: *ArrayList(*std.Build.Step.Compile),
|
|
456
445
|
},
|
|
457
446
|
};
|
|
458
447
|
|
|
459
|
-
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
|
|
460
|
-
|
|
461
|
-
error.FileNotFound => return null,
|
|
462
|
-
else => |e| return e,
|
|
463
|
-
};
|
|
448
|
+
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.Run {
|
|
449
|
+
if (!try exists(b, path)) return null;
|
|
464
450
|
var name = config.tool.name orelse std.fs.path.basename(path);
|
|
465
451
|
const index = std.mem.lastIndexOfScalar(u8, name, '.');
|
|
466
452
|
if (index) |i| name = name[0..i];
|
|
467
453
|
if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
|
|
468
454
|
|
|
469
|
-
const exe = b.addExecutable(
|
|
455
|
+
const exe = b.addExecutable(.{
|
|
470
456
|
.name = name,
|
|
471
|
-
.
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
.optimize = config.general.optimize,
|
|
479
|
-
.single_threaded = true,
|
|
480
|
-
.strip = config.general.strip,
|
|
481
|
-
.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
|
+
}),
|
|
482
464
|
});
|
|
483
465
|
|
|
484
|
-
if (
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
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", .{});
|
|
489
470
|
}
|
|
490
471
|
|
|
491
|
-
if (force_pic) {
|
|
492
|
-
exe.single_threaded = true;
|
|
493
|
-
if (config.general.pic orelse false) exe.force_pic = config.general.pic;
|
|
494
|
-
}
|
|
495
472
|
if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
|
|
496
|
-
config.tool.exes.append(exe) catch @panic("OOM");
|
|
473
|
+
config.tool.exes.append(b.allocator, exe) catch @panic("OOM");
|
|
497
474
|
|
|
498
475
|
const run = b.addRunArtifact(exe);
|
|
499
476
|
maybeStrip(b, exe, &run.step, config.general.strip, config.general.cmd);
|
|
@@ -502,10 +479,18 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
|
|
|
502
479
|
return run;
|
|
503
480
|
}
|
|
504
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
|
+
|
|
505
490
|
const ToolsStep = struct {
|
|
506
491
|
step: std.Build.Step,
|
|
507
492
|
|
|
508
|
-
pub fn create(b: *std.Build, exes: *
|
|
493
|
+
pub fn create(b: *std.Build, exes: *ArrayList(*std.Build.Step.Compile)) *ToolsStep {
|
|
509
494
|
const self = b.allocator.create(ToolsStep) catch @panic("OOM");
|
|
510
495
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
|
|
511
496
|
self.* = .{ .step = step };
|
|
@@ -514,21 +499,19 @@ const ToolsStep = struct {
|
|
|
514
499
|
}
|
|
515
500
|
};
|
|
516
501
|
|
|
517
|
-
fn
|
|
518
|
-
|
|
519
|
-
return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
const WASM = @embedFile("src/lib/wasm.zig");
|
|
502
|
+
pub fn exports(b: *std.Build, bytes: []const u8) ![][]const u8 {
|
|
503
|
+
var symbols: ArrayList([]const u8) = .empty;
|
|
523
504
|
|
|
524
|
-
|
|
525
|
-
var symbols = std.ArrayList([]const u8).init(b.allocator);
|
|
526
|
-
|
|
527
|
-
var it = std.mem.splitSequence(u8, WASM, "export const ");
|
|
505
|
+
var it = std.mem.splitSequence(u8, bytes, "export ");
|
|
528
506
|
_ = it.next();
|
|
529
507
|
while (it.next()) |s| {
|
|
530
|
-
|
|
531
|
-
|
|
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
|
+
}
|
|
532
515
|
}
|
|
533
516
|
|
|
534
517
|
return symbols.items;
|