From e8a1fe4d87e26de369bc46f3be5c60109460164c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 31 Dec 2022 09:04:11 -0800 Subject: [PATCH] move wasm target struct around to avoid analyzing wasm file on non-wasm --- src/font/main.zig | 4 ++-- src/os/wasm.zig | 10 ---------- src/os/wasm/log.zig | 3 ++- src/os/wasm/target.zig | 14 ++++++++++++++ src/renderer.zig | 4 ++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/font/main.zig b/src/font/main.zig index 9eed0c403..163333a15 100644 --- a/src/font/main.zig +++ b/src/font/main.zig @@ -56,8 +56,8 @@ pub const Backend = enum { /// Returns the default backend for a build environment. This is /// meant to be called at comptime. pub fn default() Backend { - const wasm = @import("../os/wasm.zig"); - if (wasm.target) |target| return switch (target) { + const wasm_target = @import("../os/wasm/target.zig"); + if (wasm_target.target) |target| return switch (target) { .browser => .web_canvas, }; diff --git a/src/os/wasm.zig b/src/os/wasm.zig index 0c933471d..bd2054b00 100644 --- a/src/os/wasm.zig +++ b/src/os/wasm.zig @@ -14,16 +14,6 @@ comptime { /// in JS will be backed by a SharedArrayBuffer and some behaviors change. pub const shared_mem = options.wasm_shared; -/// Our specific target platform. -pub const target: ?Target = if (!builtin.target.isWasm()) null else target: { - const result = @intToEnum(Target, @enumToInt(options.wasm_target)); - // This maybe isn't necessary but I don't know if enums without a specific - // tag type and value are guaranteed to be the same between build.zig - // compilation and our own source compilation so I have this just in case. - std.debug.assert(std.mem.eql(u8, @tagName(result), @tagName(options.wasm_target))); - break :target result; -}; - /// The allocator to use in wasm environments. /// /// The return values of this should NOT be sent to the host environment diff --git a/src/os/wasm/log.zig b/src/os/wasm/log.zig index eda7cc2ed..d1163e6e3 100644 --- a/src/os/wasm/log.zig +++ b/src/os/wasm/log.zig @@ -1,9 +1,10 @@ const std = @import("std"); const builtin = @import("builtin"); const wasm = @import("../wasm.zig"); +const wasm_target = @import("target.zig"); // Use the correct implementation -pub usingnamespace if (wasm.target) |target| switch (target) { +pub usingnamespace if (wasm_target.target) |target| switch (target) { .browser => Browser, } else struct {}; diff --git a/src/os/wasm/target.zig b/src/os/wasm/target.zig index bd897cc21..c8bf51778 100644 --- a/src/os/wasm/target.zig +++ b/src/os/wasm/target.zig @@ -1,6 +1,20 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const options = @import("build_options"); + /// The wasm target platform. This is used to toggle certain features /// on and off since the standard triple target is often not specific /// enough (i.e. we can't tell wasm32-freestanding is for browser or not). pub const Target = enum { browser, }; + +/// Our specific target platform. +pub const target: ?Target = if (!builtin.target.isWasm()) null else target: { + const result = @intToEnum(Target, @enumToInt(options.wasm_target)); + // This maybe isn't necessary but I don't know if enums without a specific + // tag type and value are guaranteed to be the same between build.zig + // compilation and our own source compilation so I have this just in case. + std.debug.assert(std.mem.eql(u8, @tagName(result), @tagName(options.wasm_target))); + break :target result; +}; diff --git a/src/renderer.zig b/src/renderer.zig index 721cdc224..e28f024aa 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -21,8 +21,8 @@ pub const State = @import("renderer/State.zig"); /// The implementation to use for the renderer. This is comptime chosen /// so that every build has exactly one renderer implementation. -const wasm = @import("os/wasm.zig"); -pub const Renderer = if (wasm.target) |target| switch (target) { +const wasm_target = @import("os/wasm/target.zig"); +pub const Renderer = if (wasm_target.target) |target| switch (target) { .browser => WebGL, } else switch (builtin.os.tag) { .macos => Metal,