From 5e268ec8f19f640a064542429270737d2f19ad31 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sat, 14 Dec 2024 16:36:29 -0800 Subject: [PATCH] Support emscripten builds (#7) * Options default false * Initial emscripten commit --- build.zig | 40 +++++++++++++++++++++++++++++----------- src/gui.zig | 4 ++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/build.zig b/build.zig index bab6226..5d67983 100644 --- a/build.zig +++ b/build.zig @@ -24,17 +24,17 @@ pub fn build(b: *std.Build) void { bool, "with_implot", "Build with bundled implot source", - ) orelse true, + ) orelse false, .with_gizmo = b.option( bool, "with_gizmo", "Build with bundled ImGuizmo tool", - ) orelse true, + ) orelse false, .with_node_editor = b.option( bool, "with_node_editor", "Build with bundled ImGui node editor", - ) orelse true, + ) orelse false, .with_te = b.option( bool, "with_te", @@ -104,12 +104,24 @@ pub fn build(b: *std.Build) void { b.installArtifact(imgui); + const emscripten = target.result.os.tag == .emscripten; + if (emscripten) { + imgui.defineCMacro("__EMSCRIPTEN__", null); + // TODO: read from enviroment or `emcc --version` + imgui.defineCMacro("__EMSCRIPTEN_major__", "3"); + imgui.defineCMacro("__EMSCRIPTEN_minor__", "1"); + imgui.root_module.stack_protector = false; + //imgui.root_module.disable_stack_probing = true; + } + imgui.addIncludePath(b.path("libs")); imgui.addIncludePath(b.path("libs/imgui")); - imgui.linkLibC(); - if (target.result.abi != .msvc) - imgui.linkLibCpp(); + if (!emscripten) { + imgui.linkLibC(); + if (target.result.abi != .msvc) + imgui.linkLibCpp(); + } imgui.addCSourceFile(.{ .file = b.path("src/zgui.cpp"), @@ -254,11 +266,17 @@ pub fn build(b: *std.Build) void { switch (options.backend) { .glfw_wgpu => { - if (b.lazyDependency("zglfw", .{})) |zglfw| { - imgui.addIncludePath(zglfw.path("libs/glfw/include")); - } - if (b.lazyDependency("zgpu", .{})) |zgpu| { - imgui.addIncludePath(zgpu.path("libs/dawn/include")); + if (emscripten) { + imgui.addSystemIncludePath(.{ + .cwd_relative = b.pathJoin(&.{ b.sysroot.?, "include" }), + }); + } else { + if (b.lazyDependency("zglfw", .{})) |zglfw| { + imgui.addIncludePath(zglfw.path("libs/glfw/include")); + } + if (b.lazyDependency("zgpu", .{})) |zgpu| { + imgui.addIncludePath(zgpu.path("libs/dawn/include")); + } } imgui.addCSourceFiles(.{ .files = &.{ diff --git a/src/gui.zig b/src/gui.zig index b5b6ad4..d0ea5c0 100644 --- a/src/gui.zig +++ b/src/gui.zig @@ -3442,12 +3442,12 @@ var temp_buffer: ?std.ArrayList(u8) = null; pub fn format(comptime fmt: []const u8, args: anytype) []const u8 { const len = std.fmt.count(fmt, args); - if (len > temp_buffer.?.items.len) temp_buffer.?.resize(len + 64) catch unreachable; + if (len > temp_buffer.?.items.len) temp_buffer.?.resize(@intCast(len + 64)) catch unreachable; return std.fmt.bufPrint(temp_buffer.?.items, fmt, args) catch unreachable; } pub fn formatZ(comptime fmt: []const u8, args: anytype) [:0]const u8 { const len = std.fmt.count(fmt ++ "\x00", args); - if (len > temp_buffer.?.items.len) temp_buffer.?.resize(len + 64) catch unreachable; + if (len > temp_buffer.?.items.len) temp_buffer.?.resize(@intCast(len + 64)) catch unreachable; return std.fmt.bufPrintZ(temp_buffer.?.items, fmt, args) catch unreachable; } //--------------------------------------------------------------------------------------------------