sokol-zig icon indicating copy to clipboard operation
sokol-zig copied to clipboard

Emscripten LTO: Linking two modules of different target triples

Open kassane opened this issue 1 year ago • 2 comments

Actually Zig isn't enabling LTO on release targets ( wasm32 in particular). Enabling it manually gives this warning during linking.

$ zig build run-clear -Dtarget=wasm32-emscripten -Doptimize=ReleaseSmall
run-clear
└─ run /home/kassane/.cache/zig/p/1220146d92ca77f3cabb151fd035f6fa2ea03481ea07f3358ec912f17acdba791a8a/upstream/emscripten/emrun
   └─ install generated/
      └─ emcc stderr
wasm-ld: warning: Linking two modules of different target triples: '/home/kassane/sokol-zig/.zig-cache/o/b17f1f6a7714e069fb53b8a52b736ac9/libsokol_clib.a(sokol_app.o at 20300)' is 'wasm32-unknown-emscripten-musl' whereas 'ld-temp.o' is 'wasm32-unknown-emscripten'
wasm-ld: warning: Linking two modules of different target triples: '/home/kassane/sokol-zig/.zig-cache/o/b17f1f6a7714e069fb53b8a52b736ac9/libsokol_clib.a(sokol_glue.o at 379232)' is 'wasm32-unknown-emscripten-musl' whereas 'ld-temp.o' is 'wasm32-unknown-emscripten'
wasm-ld: warning: Linking two modules of different target triples: '/home/kassane/sokol-zig/.zig-cache/o/b17f1f6a7714e069fb53b8a52b736ac9/libsokol_clib.a(sokol_gfx.o at 95292)' is 'wasm32-unknown-emscripten-musl' whereas 'ld-temp.o' is 'wasm32-unknown-emscripten'
wasm-ld: warning: Linking two modules of different target triples: '/home/kassane/sokol-zig/.zig-cache/o/b17f1f6a7714e069fb53b8a52b736ac9/libsokol_clib.a(sokol_log.o at 11768)' is 'wasm32-unknown-emscripten-musl' whereas 'ld-temp.o' is 'wasm32-unknown-emscripten'

Similar issue in sokol-d: https://github.com/kassane/sokol-d/issues/3#issuecomment-2351602235

How to fix?

Replace wasm32-emscripten (default is musl) to wasm32-emscripten-none!

# first clean .zig-cache and zig-out
$ zig build run-clear -Dtarget=wasm32-emscripten-none -Doptimize=ReleaseSmall
Now listening at http://0.0.0.0:6931/

kassane avatar Sep 15 '24 14:09 kassane

Hmm good to know but strange, I would actually have expected that MUSL is the correct C library to use (since Emscripten is also using MUSL).

floooh avatar Sep 16 '24 08:09 floooh

This is probably because zig ensures its self-dependencies by default. Unlike other toolchains (ldc2, rustc, ...) that depend on emscripten libc.

During the tests I had no complications in running the applications, only the warnings issued. (same target data-layout)

kassane avatar Sep 16 '24 11:09 kassane

patch change
diff --git a/build.zig b/build.zig
index dd3aa67..b7a7db5 100644
--- a/build.zig
+++ b/build.zig
@@ -107,6 +107,7 @@ fn buildExample(b: *Build, comptime name: []const u8, options: ExampleOptions) !
             .target = options.target,
             .optimize = options.optimize,
         });
+        example.want_lto = true;
         example.root_module.addImport("sokol", options.mod_sokol);
 
         // create a special emcc linker run step
@@ -120,6 +121,7 @@ fn buildExample(b: *Build, comptime name: []const u8, options: ExampleOptions) !
             .use_webgl2 = backend != .wgpu,
             .use_emmalloc = true,
             .use_filesystem = false,
+            .release_use_lto = example.want_lto orelse false,
             .shell_file_path = b.path("src/sokol/web/shell.html"),
             .extra_args = &.{"-sSTACK_SIZE=512KB"},
         });
@@ -171,6 +173,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*Build.Step.Compile {
     b.installArtifact(lib);
 
     if (options.target.result.isWasm()) {
+        lib.want_lto = true;
         // make sure we're building for the wasm32-emscripten target, not wasm32-freestanding
         if (lib.rootModuleTarget().os.tag != .emscripten) {
             std.log.err("Please build with 'zig build -Dtarget=wasm32-emscripten", .{});

emcc 3.1.61

sokol-zig git:(master) ✗ ~/zig/master/files/zig build run-clear --summary all -Dtarget=wasm32-emscripten-none -Doptimize=ReleaseSafe
run-clear
└─ run /home/kassane/.cache/zig/p/12200ba39d83227f5de08287b043b011a2eb855cdb077f4b165edce30564ba73400e/upstream/emscripten/emrun
   └─ install generated/
      └─ emcc failure
wasm-ld: error: Invalid summary version 10. Version should be in the range [1-9].
wasm-ld: error: Invalid summary version 10. Version should be in the range [1-9].
wasm-ld: error: Invalid summary version 10. Version should be in the range [1-9].
wasm-ld: error: Invalid summary version 10. Version should be in the range [1-9].

emcc 3.1.71

sokol-zig git:(master) ✗ ~/zig/master/files/zig build run-clear --summary all -Dtarget=wasm32-emscripten-none -Doptimize=ReleaseSafe
run-clear
└─ run /home/kassane/.cache/zig/p/12201d024e47d97dea4d1d6024a9cf1b35ca8be8e4fa1a0ecf6ee9080e479467183a/upstream/emscripten/emrun
   └─ install generated/
      └─ emcc stderr
'-half-precision' is not a recognized feature for this target (ignoring feature)
'-half-precision' is not a recognized feature for this target (ignoring feature)
Now listening at http://0.0.0.0:6931/

kassane avatar Dec 05 '24 20:12 kassane