dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

[wasm][wasi] No main function should be required when building a library #96869

Closed silesmo closed 3 months ago

silesmo commented 8 months ago

Description

Currently mono requires a main function when building for wasi. This shouldn't be required.

Related: https://github.com/dotnet/runtime/issues/96419

@AaronRobinsonMSFT @lewing @yowl @jsturtevant

Reproduction Steps

Try to build example with main removed. Example: https://github.com/silesmo/wasm-unmanaged-callers-bug/blob/main/mono-example/MonoEntrypoint.cs

Expected behavior

No main function should be required for a library.

Actual behavior

Main function is required.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

ghost commented 8 months ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Currently mono requires a main function when building for wasi. This shouldn't be required. Related: https://github.com/dotnet/runtime/issues/96419 @AaronRobinsonMSFT @lewing @yowl @jsturtevant ### Reproduction Steps Try to build example with `main` removed. Example: https://github.com/silesmo/wasm-unmanaged-callers-bug/blob/main/mono-example/MonoEntrypoint.cs ### Expected behavior No main function should be required for a library. ### Actual behavior Main function is required. ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration _No response_ ### Other information _No response_
Author: silesmo
Assignees: -
Labels: `arch-wasm`, `untriaged`, `area-Build-mono`, `os-wasi`
Milestone: -
maraf commented 8 months ago

Similar requirement tracked for browser in https://github.com/dotnet/runtime/issues/77191

lewing commented 3 months ago

See the notes https://github.com/dotnet/runtimelab/blob/feature/NativeAOT-LLVM/src/coreclr/nativeaot/Bootstrap/main.cpp#L179-L229 and some background here https://dylibso.com/blog/wasi-command-reactor/ cc @pavelsavara

pavelsavara commented 3 months ago

This should be triggered by <OuputType>library</OutputType>

maraf commented 3 months ago

One problem I encountered last year was IL trimmer complaining about missing root

Implementation has two parts

maraf commented 3 months ago

We probably might need a different mechanism to compile library as wasm library. Currently we need RuntimeIdentifier=*-wasm + OutputType=exe.

In typicaly solution user would have several library projects and (one or some) exe projects that are applications. Those library projects might have RID set to *-wasm to enable wasm API. Currenly only the application projects would do the wasm build. Which is correct IMO. If we support wasm build on OutputType=library, it would a lot of additional steps in such solution.

Other parts of .NETE SDK include special steps in build by including a special SDKs in project definition (like Microsoft.NET.Sdk.Razor). We should do the same and do wasm library only when the project references Microsoft.NET.Sdk.WebAssembly.

The Microsoft.NET.Sdk.WebAssembly lives in SDK and wasi build lives in workload, but we can use UsingMicrosoftNETSdkWebAssembly to check it the wasm SDK being used

Currently Wasm SDK always imports web related SDKs https://github.com/dotnet/sdk/blob/main/src/WasmSdk/Sdk/Sdk.props#L32. We need to add a guard to do it only for browser-wasm. In the meantime we can workaround it by explicitly setting UsingMicrosoftNETSdkStaticWebAssets=true in the csproj.