Skip to content

Commit

Permalink
more fixes - assert header from emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jan 22, 2024
1 parent 901a153 commit 329f31b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
49 changes: 41 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,18 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
csrc_root ++ "sokol_glue.c",
csrc_root ++ "sokol_debugtext.c",
csrc_root ++ "sokol_shape.c",
csrc_root ++ "sokol_imgui.c",
};
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = csrc },
.flags = cflags,
});
}
const cimgui = buildImgui(b, .{ .target = options.target, .optimize = options.optimize });
lib.addCSourceFile(.{
.file = .{ .path = csrc_root ++ "sokol_imgui.c" },
.flags = cflags,
});
const cimgui = try buildImgui(b, .{ .target = options.target, .optimize = options.optimize, .emsdk = options.emsdk });
for (cimgui.root_module.include_dirs.items) |dir| {
try lib.root_module.include_dirs.append(b.allocator, dir);
}
Expand Down Expand Up @@ -227,6 +230,7 @@ pub fn build(b: *Build) !void {
"cube",
"blend",
"mrt",
"imgui",
"saudio",
"sgl_context",
"debugtext_print",
Expand Down Expand Up @@ -415,6 +419,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
try cmds.append("-L-w"); // hide linker warnings
}
if (options.target.result.isWasm()) {
try cmds.append("--d-version=CarelessAlocation");
try cmds.append("-L-allow-undefined");
}

Expand Down Expand Up @@ -523,6 +528,22 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {

if (options.artifact) |lib_sokol| {
ldc_exec.addArtifactArg(lib_sokol);
var it = lib_sokol.root_module.iterateDependencies(lib_sokol, false);
while (it.next()) |item| {
for (item.module.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |compile_step| {
switch (compile_step.kind) {
.lib => {
ldc_exec.addArtifactArg(compile_step);
},
else => {},
}
},
else => {},
}
}
}
}

const example_run = b.addSystemCommand(&.{b.pathJoin(&.{ b.install_path, outputDir, options.name })});
Expand Down Expand Up @@ -808,7 +829,7 @@ fn emSdkSetupStep(b: *Build, emsdk: *Build.Dependency) !?*Build.Step.Run {
}
}

fn buildImgui(b: *Build, options: struct { target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode }) *CompileStep {
fn buildImgui(b: *Build, options: LibSokolOptions) !*CompileStep {
const imgui_cpp = b.dependency("imgui", .{});
const imgui_cpp_dir = imgui_cpp.path("").getPath(b);
const cimgui = b.dependency("cimgui", .{});
Expand All @@ -821,10 +842,23 @@ fn buildImgui(b: *Build, options: struct { target: Build.ResolvedTarget, optimiz
});
if (libimgui.linkage == .static)
libimgui.pie = true
else
else if (libimgui.linkage == .static)
libimgui.root_module.pic = true;
libimgui.addIncludePath(.{ .path = cimgui_dir });
libimgui.addIncludePath(.{ .path = imgui_cpp_dir });
libimgui.defineCMacro("IMGUI_DISABLE_OBSOLETE_FUNCTIONS", "1");
if (libimgui.rootModuleTarget().isWasm()) {
// make sure we're building for the wasm32-emscripten target, not wasm32-freestanding
if (libimgui.rootModuleTarget().os.tag != .emscripten) {
std.log.err("Please build with 'zig build -Dtarget=wasm32-emscripten", .{});
return error.Wasm32EmscriptenExpected;
}
// add the Emscripten system include seach path
const emsdk_sysroot = b.pathJoin(&.{ emSdkPath(b, options.emsdk.?), "upstream", "emscripten", "cache", "sysroot" });
const include_path = b.pathJoin(&.{ emsdk_sysroot, "include" });
libimgui.addSystemIncludePath(.{ .path = include_path });
libimgui.defineCMacro("IMGUI_DISABLE_FILE_FUNCTIONS", null);
}
libimgui.addCSourceFiles(.{
.files = &.{
b.pathJoin(&.{ cimgui_dir, "cimgui.cpp" }),
Expand All @@ -837,17 +871,16 @@ fn buildImgui(b: *Build, options: struct { target: Build.ResolvedTarget, optimiz
.flags = &.{
"-Wall",
"-Wextra",
"-Wformat",
"-fno-rtti",
"-fno-exceptions",
"-Werror",
"-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1",
"-Wno-unused-parameter",
"-Wno-missing-field-initializers",
"-fno-threadsafe-statics",
},
});
libimgui.root_module.sanitize_c = false;
if (libimgui.rootModuleTarget().os.tag == .windows)
libimgui.linkSystemLibrary2("imm32", .{ .use_pkg_config = .no });
libimgui.linkSystemLibrary("imm32");

// https://github.com/ziglang/zig/issues/5312
if (libimgui.rootModuleTarget().abi != .msvc) {
Expand Down
20 changes: 8 additions & 12 deletions src/sokol/imgui.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

module sokol.imgui;

import core.stdc.stdint;
import core.stdc.stdarg : va_list;
import sapp = sokol.app;
import sg = sokol.gfx;


version(WebAssembly){
alias ImFileHandle = void*;
}else{
import core.stdc.stdio: FILE;
alias ImFileHandle = FILE*;
}

extern (C):
nothrow:
@nogc:
Expand Down Expand Up @@ -1492,17 +1499,6 @@ struct StbTexteditRow
int num_chars;
}

static if({
version(WebAssembly) return false;
else version(WASI) return false;
else return true;
}){
import core.stdc.stdio: FILE;
alias ImFileHandle = FILE*;
}else{
alias ImFileHandle = void*;
}

struct ImVec1
{
float x;
Expand Down

0 comments on commit 329f31b

Please sign in to comment.