floooh / sokol-zig

Zig bindings for the sokol headers (https://github.com/floooh/sokol)
zlib License
375 stars 48 forks source link

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

Open kassane opened 2 months ago

kassane commented 2 months ago

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/
floooh commented 2 months ago

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 commented 2 months ago

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)