Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Build package and shims for Emscripten emsdk

License

NotificationsYou must be signed in to change notification settings

zig-gamedev/zemscripten

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zig build package and shims forEmscripten emsdk

How to use it

Addzemscripten and (optionally)emsdk to your build.zig.zon dependencies

    zig fetch --save https://github.com/emscripten-core/emsdk/archive/refs/tags/4.0.3.tar.gz

Emsdk must be activated before it can be used. You can useactivateEmsdkStep to create a build step for that:

constactivate_emsdk_step=@import("zemscripten").activateEmsdkStep(b);

Add zemscripten's "root" module to your wasm compile target., then create anemcc build step. We use zemscripten's default flags and settings which can be overridden for your project specific requirements. Refer to theemcc documentation. Example build.zig code:

constwasm=b.addStaticLibrary(.{        .name="MyGame",        .root_source_file=b.path("src/main.zig"),        .target=target,        .optimize=optimize,    });constzemscripten=b.dependency("zemscripten", .{});wasm.root_module.addImport("zemscripten",zemscripten.module("root"));constemcc_flags=@import("zemscripten").emccDefaultFlags(b.allocator,optimize);varemcc_settings=@import("zemscripten").emccDefaultSettings(b.allocator, .{        .optimize=optimize,    });tryemcc_settings.put("ALLOW_MEMORY_GROWTH","1");constemcc_step=@import("zemscripten").emccStep(b,wasm,        .{            .optimize=optimize,            .flags=emcc_flags,            .settings=emcc_settings,            .use_preload_plugins=true,            .embed_paths= &.{},            .preload_paths= &.{},            .out_file_name=null,// emcc output arg will default to {wasm.name}.html if unset            .install_dir= .{ .custom="web" },        },    );emcc_step.dependOn(activate_emsdk_step);b.getInstallStep().dependOn(emcc_step);

To use a custom html file emccStep() accepts a shell_file_path option:

constemcc_step=@import("zemscripten").emccStep(b,wasm,        .{            .optimize=optimize,            .flags=emcc_flags,            .settings=emcc_settings,            .use_preload_plugins=true,            .embed_paths= &.{},            .preload_paths= &.{},            .install_dir= .{ .custom="web" },            .shell_file_path=b.path("path/to/file"),        },    );

Now you can use the provided Zig panic and log overrides in your wasm's root module and define the entry point that invoked by the js output ofemcc (by default it looks for a symbol namedmain). For example:

conststd=@import("std");constzemscripten=@import("zemscripten");pubconstpanic=zemscripten.panic;pubconststd_options=std.Options{    .logFn=zemscripten.log,};exportfnmain()c_int {std.log.info("hello, world.", .{});return0;}

You can also define a run step that invokesemrun. This will serve the html locally over HTTP and try to open it using your default browser. Example build.zig code:

consthtml_filename=trystd.fmt.allocPrint(b.allocator,"{s}.html", .{wasm.name});constemrun_args= .{};constemrun_step=@import("zemscripten").emrunStep(b,b.getInstallPath(.{ .custom="web" },html_filename),&emrun_args,    );emrun_step.dependOn(emcc_step);b.step("emrun","Build and open the web app locally using emrun").dependOn(emrun_step);

See theemrun documentation for the difference args that can be used.

About

Build package and shims for Emscripten emsdk

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp