dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.42k stars 199 forks source link

[NativeAOT LLVM] Cannot compile to WebAssembly while following the doc #2375

Closed ArnaudValensi closed 1 year ago

ArnaudValensi commented 1 year ago

Hi,

I’m following the documentation to compile a WebAssembly native library:

https://github.com/dotnet/runtimelab/blob/feature/NativeAOT-LLVM/docs/using-nativeaot/compiling.md#webassembly-native-libraries

I get the following error:

wasm-ld : error : C:\Users\avalensi\dev\imgui\repos\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto\libc-debug.a(htons.o): attempt to add bitcode file after LTO. [C:\Users\avalens
i\dev\imgui\repos\NativeLibraryWasm\NativeLibraryWasm.csproj]
wasm-ld : error : C:\Users\avalensi\dev\imgui\repos\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto\libc-debug.a(ntohs.o): attempt to add bitcode file after LTO. [C:\Users\avalens
i\dev\imgui\repos\NativeLibraryWasm\NativeLibraryWasm.csproj]
emcc : error : 'C:/Users/avalensi/dev/imgui/repos/emsdk/upstream/bin\wasm-ld.exe -o bin/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.wasm C:/Users/avalensi/.nuget/packages/runtime.browser-was
m.microsoft.dotnet.ilcompiler.llvm/8.0.0-preview.7.23414.1/framework/libSystem.Native.a C:/Users/avalensi/.nuget/packages/runtime.browser-wasm.microsoft.dotnet.ilcompiler.llvm/8.0.0-preview.7.23414
.1/framework/libSystem.Globalization.Native.a C:/Users/avalensi/.nuget/packages/runtime.browser-wasm.microsoft.dotnet.ilcompiler.llvm/8.0.0-preview.7.23414.1/sdk/libbootstrapperdll.a C:/Users/avale
nsi/.nuget/packages/runtime.browser-wasm.microsoft.dotnet.ilcompiler.llvm/8.0.0-preview.7.23414.1/sdk/libPortableRuntime.a C:/Users/avalensi/.nuget/packages/runtime.browser-wasm.microsoft.dotnet.il
compiler.llvm/8.0.0-preview.7.23414.1/sdk/libWasmExceptionHandling.a obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.0.o obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.1.o obj/Debug/
net8.0/browser-wasm/native/NativeLibraryWasm.2.o obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.3.o obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.4.o obj/Debug/net8.0/browser-wasm/
native/NativeLibraryWasm.5.o obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.6.o obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.7.o obj/Debug/net8.0/browser-wasm/native/NativeLibrary
Wasm.o obj/Debug/net8.0/browser-wasm/native/NativeLibraryWasm.external.o -LC:\Users\avalensi\dev\imgui\repos\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten\lto -lGL -lal -lhtml5 -lst
ubs-debug -lnoexit -lc-debug -ldlmalloc -lcompiler_rt-wasm-sjlj -lc++-except -lc++abi-debug-except -lunwind-except -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -wasm-enable-sjlj -m
llvm -disable-lsr -mllvm -wasm-enable-eh -mllvm -exception-model=wasm C:\Users\avalensi\AppData\Local\Temp\tmp0uom0w3alibemscripten_js_symbols.so --import-undefined --export-if-defined=Answer --exp
ort-if-defined=__get_exception_message --export-if-defined=free --export-if-defined=__cpp_exception --export-if-defined=__cxa_increment_exception_refcount --export-if-defined=__cxa_decrement_except
ion_refcount --export-if-defined=__thrown_object_from_unwind_exception --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_lib_deps --export-if-defin
ed=__stop_em_lib_deps --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=main --export-if-defined=fflush --export-if-defined=emscripten_stack_get_end --export-if
-defined=emscripten_stack_get_free --export-if-defined=emscripten_stack_get_base --export-if-defined=emscripten_stack_get_current --export-if-defined=emscripten_stack_init --export-if-defined=stack
Save --export-if-defined=stackRestore --export-if-defined=stackAlloc --export-if-defined=__errno_location --export-if-defined=__trap --export-if-defined=__wasm_call_ctors --export-table -z stack-si
ze=65536 --initial-memory=16777216 --no-entry --max-memory=2147483648 --stack-first' failed (returned 1) [C:\Users\avalensi\dev\imgui\repos\NativeLibraryWasm\NativeLibraryWasm.csproj]
C:\Users\avalensi\.nuget\packages\microsoft.dotnet.ilcompiler.llvm\8.0.0-preview.7.23414.1\build\Microsoft.NETCore.Native.targets(539,5): error MSB3073: The command ""C:/Users/avalensi/dev/imgui/re
pos/emsdk/upstream/emscripten/emcc.bat" @obj\Debug\net8.0\browser-wasm\native\link.rsp -s EXPORTED_FUNCTIONS=_Answer -s EXPORTED_RUNTIME_METHODS=cwrap --post-js=invokeLibraryFunction.js" exited wit
h code 1. [C:\Users\avalensi\dev\imgui\repos\NativeLibraryWasm\NativeLibraryWasm.csproj]

I get the same error when trying to compile a regular executable (with a main function).

I’m using:

Here is the code of my project (which is the result of following exactly the guide):

https://github.com/ArnaudValensi/net8-wasm-native-library

yowl commented 1 year ago

Can you try emscripten 3.1.23 ? I've not tried anything later to be honest.

ArnaudValensi commented 1 year ago

Yes, this is working. Thanks. Maybe we should add that to the doc?

yowl commented 1 year ago

Would be nice to update emscripten, but you are right. I'll update the docs

jkotas commented 1 year ago

Fixed by https://github.com/dotnet/runtimelab/pull/2376