From 70b6e0b716b658b9ae6fe1a86ae5dd5d8c9a0f25 Mon Sep 17 00:00:00 2001 From: ppatt Date: Mon, 1 Dec 2025 12:52:22 +0200 Subject: [PATCH] Add Emscripten sysroot include paths to enable C library compilation for .emscripten build target --- README.md | 33 +++++++++++++++++++++++++++++++++ build.zig | 14 ++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/README.md b/README.md index 2d6feeb..1121831 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,39 @@ pub fn build(b: *std.Build) void { } ``` +Zemscripten `build.zig` example: +```zig +pub fn build(b: *std.Build) void { + const target = b.resolveTargetQuery(.{ + .cpu_arch = .wasm32, + .os_tag = .emscripten, + }); + + const wasm = b.addLibrary(.{ .name = "zemscripten-build", .linkage = .static, .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + }) }); + + // Add Emscripten include paths for @cImport + const emsdk_path = b.dependency("emsdk", .{}).path("").getPath(b); + const emscripten_include_path = b.pathJoin(&.{ emsdk_path, "upstream", "emscripten", "cache", "sysroot", "include" }); + wasm.root_module.addSystemIncludePath(.{ .cwd_relative = emscripten_include_path }); + + const zemscripten = b.dependency("zemscripten", .{}); + const zmesh = b.dependency("zmesh", .{ + .target = target, + .optimize = optimize, + .emscripten_include_path = emscripten_include_path, + }); + + wasm.root_module.addImport("zmesh", zmesh.module("root")); + wasm.linkLibrary(zmesh.artifact("zmesh")); + + ... +} +``` + Now in your code you may import and use `zmesh`: ```zig diff --git a/build.zig b/build.zig index 5768607..3fc731e 100644 --- a/build.zig +++ b/build.zig @@ -15,6 +15,11 @@ pub fn build(b: *std.Build) void { "shared", "Build as shared library", ) orelse false, + .emscripten_include_path = b.option( + []const u8, + "emscripten_include_path", + "Path to emscripten include directory for Emscripten builds", + ), }; const options_step = b.addOptions(); @@ -54,6 +59,15 @@ pub fn build(b: *std.Build) void { if (target.result.abi != .msvc) zmesh_lib.linkLibCpp(); + // Add Emscripten sysroot include paths when building for emscripten target + if (target.result.os.tag == .emscripten) { + if (options.emscripten_include_path) |emscripten_include_path| { + zmesh_lib.addSystemIncludePath(.{ .cwd_relative = emscripten_include_path }); + } else { + std.debug.panic("emscripten_include_path option must be provided when building for emscripten target\n", .{}); + } + } + const par_shapes_t = if (options.shape_use_32bit_indices) "-DPAR_SHAPES_T=uint32_t" else