Skip to content

Adding zflecs to sokol-based build config #18

@Umenokin

Description

@Umenokin

** Duplicate of the main repository

Hi folks,

I am trying to confirm that I will be able to use zflecs and possibly other zig-gamedev libs in my project.
It seems I am able to compile it successfully for the native platforms, but getting an error building it for the web.
My steps:

  1. Add zflecs to zon file: zig fetch --save=zflecs git+https://github.com/zig-gamedev/zflecs.git

  2. Add to build config.
    2.1 Native

    const exe_mod = b.createModule(.{
         .root_source_file = b.path("src/main.zig"),
         .target = target,
         .optimize = options.optimize,
     });
    
     const exe = b.addExecutable(.{
         .name = "app",
         .root_module = exe_mod,
     });
    
     exe.root_module.addImport("sokol", dep_sokol.module("sokol"));
    
     const zflecs = b.dependency("zflecs", .{});
     exe.root_module.addImport("zflecs", zflecs.module("root"));
     exe.linkLibrary(zflecs.artifact("flecs"));
    

    2.2 Web

    const lib = b.addStaticLibrary(.{
        .name = "app",
        .target = target,
        .optimize = options.optimize,
        .root_source_file = b.path("src/main.zig"),
    });
    
    lib.root_module.addImport("sokol", dep_sokol.module("sokol"));
    
    const zflecs = b.dependency("zflecs", .{});
    lib.root_module.addImport("zflecs", zflecs.module("root"));
    lib.linkLibrary(zflecs.artifact("flecs"));
    
    // create a build step which invokes the Emscripten linker
    const emsdk = dep_sokol.builder.dependency("emsdk", .{});
    const link_step = try sokol.emLinkStep(b, .{
        .lib_main = lib,
        .target = target,
        .optimize = options.optimize,
        .emsdk = emsdk,
        .use_webgl2 = options.backend != .wgpu,
        .use_webgpu = options.backend == .wgpu,
        .use_emmalloc = true,
        .use_filesystem = false,
        .release_use_closure = options.backend != .wgpu,
        .shell_file_path = dep_sokol.path("src/sokol/web/shell.html"),
    });
    // attach Emscripten linker output to default install step
    b.getInstallStep().dependOn(&link_step.step);
    // ...and a special run step to start the web build output via 'emrun'
    const run = sokol.emRunStep(b, .{ .name = "app", .emsdk = emsdk });
    run.step.dependOn(&link_step.step);
    b.step("run", "Run app").dependOn(&run.step);
    
  3. Add basic initialization to make sure the linking is ok:

const sokol = @import("sokol");
const ecs = @import("zflecs");

...

pub fn main() void {
    const world = ecs.init();
    defer _ = ecs.fini(world);

    sapp.run(.{
        .init_cb = init,
        .frame_cb = frame,
        .cleanup_cb = cleanup,
        .width = 640,
        .height = 480,
        .icon = .{ .sokol_default = true },
        .window_title = "clear.zig",
        .logger = .{ .func = slog.func },
        .win32_console_attach = true,
    });
}

Native build works just fine, but getting an error when building for the web zig build -Dtarget=wasm32-emscripten

/Users/user/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/lib/std/debug.zig:870:24: error: no field named 'base_address' in struct 'debug.SelfInfo.Module__struct_7161'
                module.base_address,
                       ^~~~~~~~~~~~
/Users/user/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/lib/std/debug/SelfInfo.zig:798:27: note: struct declared here
    .wasi, .emscripten => struct {
                          ^~~~~~
error: the following command failed with 1 compilation errors:
/Users/user/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/zig build-lib -ODebug -target wasm32-emscripten -mcpu baseline -I /Users/user/dev/test-app/app-client/.zig-cache/o/ee37129807542079360a73b5258f6822 --dep sokol --dep zflecs -Mroot=/Users/user/dev/test-app/app-client/src/main.zig -I /Users/user/dev/test-app/app-client/.zig-cache/o/b29ba9067f6a406b2885eaf016943dfe -Msokol=/Users/user/.cache/zig/p/sokol-0.1.0-pb1HK1CHLACn6xGB1TLwO5bgMQNY735HMVDwXZ-UHj0C/src/sokol/sokol.zig -Mzflecs=/Users/user/.cache/zig/p/zflecs-0.2.0-dev-1PN3yuhMNQDj3J57z74E3AIGM8bmVg3FhAd6XZKb_A9W/src/zflecs.zig -lc --cache-dir /Users/user/dev/test-app/app-client/.zig-cache --global-cache-dir /Users/user/.cache/zig --name app -static --zig-lib-dir /Users/user/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/lib/ --listen=- 
Build Summary: 4/8 steps succeeded; 1 failed
install transitive failure
└─ install generated/ transitive failure
   ├─ emcc transitive failure
   │  ├─ zig build-lib app Debug wasm32-emscripten 1 errors
   │  └─ zig build-lib app Debug wasm32-emscripten (+6 more reused dependencies)
   └─ emcc (+4 more reused dependencies)
error: the following build command failed with exit code 1:
/Users/user/dev/test-app/app-client/.zig-cache/o/3b467ad4105444738519ed65b7850836/build /Users/user/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/zig /Users/user/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zig/macos-aarch64-0.14.0/lib /Users/user/dev/test-app/app-client /Users/user/dev/test-app/app-client/.zig-cache /Users/user/.cache/zig --app 0x4bec60fe -Z0cf29144e1057ec0 -Dtarget=wasm32-emscripten                          

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions