Skip to content

Commit 5e268ec

Browse files
authored
Support emscripten builds (#7)
* Options default false * Initial emscripten commit
1 parent 4f8ee94 commit 5e268ec

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

build.zig

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ pub fn build(b: *std.Build) void {
2424
bool,
2525
"with_implot",
2626
"Build with bundled implot source",
27-
) orelse true,
27+
) orelse false,
2828
.with_gizmo = b.option(
2929
bool,
3030
"with_gizmo",
3131
"Build with bundled ImGuizmo tool",
32-
) orelse true,
32+
) orelse false,
3333
.with_node_editor = b.option(
3434
bool,
3535
"with_node_editor",
3636
"Build with bundled ImGui node editor",
37-
) orelse true,
37+
) orelse false,
3838
.with_te = b.option(
3939
bool,
4040
"with_te",
@@ -104,12 +104,24 @@ pub fn build(b: *std.Build) void {
104104

105105
b.installArtifact(imgui);
106106

107+
const emscripten = target.result.os.tag == .emscripten;
108+
if (emscripten) {
109+
imgui.defineCMacro("__EMSCRIPTEN__", null);
110+
// TODO: read from enviroment or `emcc --version`
111+
imgui.defineCMacro("__EMSCRIPTEN_major__", "3");
112+
imgui.defineCMacro("__EMSCRIPTEN_minor__", "1");
113+
imgui.root_module.stack_protector = false;
114+
//imgui.root_module.disable_stack_probing = true;
115+
}
116+
107117
imgui.addIncludePath(b.path("libs"));
108118
imgui.addIncludePath(b.path("libs/imgui"));
109119

110-
imgui.linkLibC();
111-
if (target.result.abi != .msvc)
112-
imgui.linkLibCpp();
120+
if (!emscripten) {
121+
imgui.linkLibC();
122+
if (target.result.abi != .msvc)
123+
imgui.linkLibCpp();
124+
}
113125

114126
imgui.addCSourceFile(.{
115127
.file = b.path("src/zgui.cpp"),
@@ -254,11 +266,17 @@ pub fn build(b: *std.Build) void {
254266

255267
switch (options.backend) {
256268
.glfw_wgpu => {
257-
if (b.lazyDependency("zglfw", .{})) |zglfw| {
258-
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
259-
}
260-
if (b.lazyDependency("zgpu", .{})) |zgpu| {
261-
imgui.addIncludePath(zgpu.path("libs/dawn/include"));
269+
if (emscripten) {
270+
imgui.addSystemIncludePath(.{
271+
.cwd_relative = b.pathJoin(&.{ b.sysroot.?, "include" }),
272+
});
273+
} else {
274+
if (b.lazyDependency("zglfw", .{})) |zglfw| {
275+
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
276+
}
277+
if (b.lazyDependency("zgpu", .{})) |zgpu| {
278+
imgui.addIncludePath(zgpu.path("libs/dawn/include"));
279+
}
262280
}
263281
imgui.addCSourceFiles(.{
264282
.files = &.{

src/gui.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,12 +3442,12 @@ var temp_buffer: ?std.ArrayList(u8) = null;
34423442

34433443
pub fn format(comptime fmt: []const u8, args: anytype) []const u8 {
34443444
const len = std.fmt.count(fmt, args);
3445-
if (len > temp_buffer.?.items.len) temp_buffer.?.resize(len + 64) catch unreachable;
3445+
if (len > temp_buffer.?.items.len) temp_buffer.?.resize(@intCast(len + 64)) catch unreachable;
34463446
return std.fmt.bufPrint(temp_buffer.?.items, fmt, args) catch unreachable;
34473447
}
34483448
pub fn formatZ(comptime fmt: []const u8, args: anytype) [:0]const u8 {
34493449
const len = std.fmt.count(fmt ++ "\x00", args);
3450-
if (len > temp_buffer.?.items.len) temp_buffer.?.resize(len + 64) catch unreachable;
3450+
if (len > temp_buffer.?.items.len) temp_buffer.?.resize(@intCast(len + 64)) catch unreachable;
34513451
return std.fmt.bufPrintZ(temp_buffer.?.items, fmt, args) catch unreachable;
34523452
}
34533453
//--------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)