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

[wasm] Allow to build browser in library mode #106388

Merged
merged 13 commits into from
Oct 3, 2024
9 changes: 9 additions & 0 deletions src/mono/browser/build/BrowserWasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
<ProjectCapability Include="DotNetCoreWeb"/>
</ItemGroup>

<!-- When trimming non-exe projects, root the whole intermediate assembly. -->
<Target Name="_RootEntireIntermediateAssembly" AfterTargets="PrepareForILLink" BeforeTargets="_RunILLink" Condition="'$(RuntimeIdentifier)' == 'browser-wasm' And '$(OutputType)' == 'Library'">
<ItemGroup>
<TrimmerRootAssembly Condition="'%(Identity)' == '@(IntermediateAssembly)'">
<RootMode>all</RootMode>
</TrimmerRootAssembly>
</ItemGroup>
</Target>

<Target Name="_GetWasmGenerateAppBundleDependencies">
<Warning Condition="'$(InvariantGlobalization)' == 'true' and '$(HybridGlobalization)' == 'true'" Text="%24(HybridGlobalization) has no effect when %24(InvariantGlobalization) is set to true." />
<Warning Condition="'$(WasmIcuDataFileName)' != '' and '$(HybridGlobalization)' == 'true'" Text="%24(WasmIcuDataFileName) has no effect when %24(HybridGlobalization) is set to true." />
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasi/wasi.proj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
PInvokeModules="@(WasmPInvokeModule)"
PInvokeOutputPath="$(WasmPInvokeTablePath)"
InterpToNativeOutputPath="$(WasmInterpToNativeTablePath)"
IsLibraryMode="$(_IsLibraryMode)">
IsLibraryMode="$(_IsLibraryMode)"
RuntimeIdentifier="$(RuntimeIdentifier)">
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
</ManagedToNativeGenerator>
</Target>
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/build/WasmApp.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,8 @@
PInvokeOutputPath="$(_WasmPInvokeTablePath)"
InterpToNativeOutputPath="$(_WasmInterpToNativeTablePath)"
CacheFilePath="$(_WasmM2NCachePath)"
IsLibraryMode="$(_IsLibraryMode)">
IsLibraryMode="$(_IsLibraryMode)"
RuntimeIdentifier="$(RuntimeIdentifier)">
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
</ManagedToNativeGenerator>
</Target>
Expand Down
4 changes: 3 additions & 1 deletion src/tasks/WasmAppBuilder/ManagedToNativeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ManagedToNativeGenerator : Task

public bool IsLibraryMode { get; set; }

public string RuntimeIdentifier { get; set; } = string.Empty;
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved

[Output]
public string[]? FileWrites { get; private set; }

Expand Down Expand Up @@ -71,7 +73,7 @@ private void ExecuteInternal(LogAdapter log)
List<string> managedAssemblies = FilterOutUnmanagedBinaries(Assemblies);
if (ShouldRun(managedAssemblies))
{
var pinvoke = new PInvokeTableGenerator(FixupSymbolName, log, IsLibraryMode);
var pinvoke = new PInvokeTableGenerator(FixupSymbolName, log, IsLibraryMode, RuntimeIdentifier);
var icall = new IcallTableGenerator(RuntimeIcallTableFile, FixupSymbolName, log);

var resolver = new PathAssemblyResolver(managedAssemblies);
Expand Down
6 changes: 4 additions & 2 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ internal sealed class PInvokeTableGenerator
private readonly List<PInvokeCallback> callbacks = new();
private readonly PInvokeCollector _pinvokeCollector;
private readonly bool _isLibraryMode;
private readonly string _runtimeIdentifier;

public PInvokeTableGenerator(Func<string, string> fixupSymbolName, LogAdapter log, bool isLibraryMode = false)
public PInvokeTableGenerator(Func<string, string> fixupSymbolName, LogAdapter log, bool isLibraryMode = false, string runtimeIdentifier = "browser-wasm")
mkhamoyan marked this conversation as resolved.
Show resolved Hide resolved
{
Log = log;
_fixupSymbolName = fixupSymbolName;
_pinvokeCollector = new(log);
_isLibraryMode = isLibraryMode;
_runtimeIdentifier = runtimeIdentifier;
}

public void ScanAssembly(Assembly asm)
Expand Down Expand Up @@ -376,7 +378,7 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> callbacks)
(!cb.IsVoid ? $"{w.NewLine} {MapType(cb.ReturnType)} result;" : "")}}

if (!(InterpEntry_T{{cb_index}})wasm_native_to_interp_ftndescs [{{cb_index}}].func) {{{
(cb.IsExport && _isLibraryMode ? $"initialize_runtime();{w.NewLine}" : "")}}
(cb.IsExport && _isLibraryMode && _runtimeIdentifier == "wasi-wasm" ? $"initialize_runtime();{w.NewLine}" : "")}}
mono_wasm_marshal_get_managed_wrapper ("{{EscapeLiteral(cb.AssemblyName)}}", "{{EscapeLiteral(cb.Namespace)}}", "{{EscapeLiteral(cb.TypeName)}}", "{{EscapeLiteral(cb.MethodName)}}", {{cb.Token}}, {{cb.Parameters.Length}});
}

Expand Down
Loading