Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emscripten LTO: Linking two modules of different target triples #81

Open
kassane opened this issue Sep 15, 2024 · 3 comments
Open

Emscripten LTO: Linking two modules of different target triples #81

kassane opened this issue Sep 15, 2024 · 3 comments

Comments

@kassane
Copy link
Contributor

kassane commented Sep 15, 2024

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: kassane/sokol-d#3 (comment)

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/
@floooh
Copy link
Owner

floooh commented Sep 16, 2024

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).

@kassane
Copy link
Contributor Author

kassane commented Sep 16, 2024

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
Copy link
Contributor Author

kassane commented Dec 5, 2024

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants