Skip to content

Commit

Permalink
Support emscripten builds (#7)
Browse files Browse the repository at this point in the history
* Options default false

* Initial emscripten commit
  • Loading branch information
ckrowland authored Dec 15, 2024
1 parent 4f8ee94 commit 5e268ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
40 changes: 29 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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 = &.{
Expand Down
4 changes: 2 additions & 2 deletions src/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
//--------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 5e268ec

Please sign in to comment.