Skip to content

Commit 8eb4138

Browse files
pavelsavararadical
andauthored
[wasm] EmccExportedRuntimeMethod MSBuild item (#76455)
Co-authored-by: Ankit Jain <radical@gmail.com>
1 parent 01cc573 commit 8eb4138

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
<_SampleProject>Wasm.Advanced.Sample.csproj</_SampleProject>
55
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
66
<PublishTrimmed>true</PublishTrimmed>
7+
<!-- add OpenGL emulation -->
8+
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL</EmccExtraLDFlags>
79
</PropertyGroup>
810
<ItemGroup>
11+
<!-- add export GL object from Module -->
12+
<EmccExportedRuntimeMethod Include="GL" />
913
<NativeFileReference Include="fibonacci.c" />
1014
<TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptors.xml" />
1115
</ItemGroup>

src/mono/sample/wasm/browser-advanced/main.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function add(a, b) {
88
}
99

1010
try {
11-
const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig } = await dotnet
11+
const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig, Module } = await dotnet
1212
.withElementOnExit()
1313
// 'withModuleConfig' is internal lower level API
1414
// here we show how emscripten could be further configured
@@ -49,6 +49,9 @@ try {
4949
const config = getConfig();
5050
const exports = await getAssemblyExports(config.mainAssemblyName);
5151
const meaning = exports.Sample.Test.TestMeaning();
52+
if (typeof Module.GL !== "object") {
53+
exit(-10, "Can't find GL");
54+
}
5255
console.debug(`meaning: ${meaning}`);
5356
if (!exports.Sample.Test.IsPrime(meaning)) {
5457
document.getElementById("out").innerHTML = `${meaning} as computed on dotnet ver ${runtimeBuildInfo.productVersion}`;

src/mono/wasm/build/WasmApp.Native.targets

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,15 @@
368368
</ItemGroup>
369369
</Target>
370370

371-
<Target Name="_WasmWriteRspFilesForLinking">
372-
<PropertyGroup>
371+
<Target Name="_WasmWriteRspFilesForLinking" DependsOnTargets="_CheckEmccIsExpectedVersion">
372+
<PropertyGroup>
373373
<_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-wasm.a</_WasmEHLib>
374374
<_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>
375375
<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>
376376
<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>
377-
</PropertyGroup>
377+
<_EmccExportedRuntimeMethods>&quot;[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedRuntimeMethods>
378+
<_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>
379+
</PropertyGroup>
378380
<ItemGroup>
379381
<!-- order matters -->
380382
<_WasmNativeFileForLinking Include="%(_BitcodeFile.ObjectFile)" />
@@ -401,6 +403,9 @@
401403
<_EmccLinkStepArgs Include="-o &quot;$(_WasmIntermediateOutputPath)dotnet.js&quot;" />
402404
<_WasmLinkDependencies Include="$(_EmccLinkRsp)" />
403405

406+
<_EmccLinkStepArgs Include="-s EXPORTED_RUNTIME_METHODS=$(_EmccExportedRuntimeMethods)" />
407+
<_EmccLinkStepArgs Include="-s EXPORTED_FUNCTIONS=$(_EmccExportedFunctions)" />
408+
404409
<_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" />
405410
</ItemGroup>
406411

@@ -457,6 +462,8 @@
457462
<ReadEmccProps JsonFilePath="$(_WasmRuntimePackSrcDir)emcc-props.json">
458463
<Output TaskParameter="EmccProperties" ItemName="_EmccPropItems" />
459464
<Output TaskParameter="WasmOptConfigurationFlags" ItemName= "_WasmOptConfigurationFlagsItems" />
465+
<Output TaskParameter="EmccDefaultExportedFunctions" ItemName= "EmccExportedFunction" />
466+
<Output TaskParameter="EmccDefaultExportedRuntimeMethods" ItemName= "EmccExportedRuntimeMethod" />
460467
</ReadEmccProps>
461468

462469
<CreateProperty Value="%(_EmccPropItems.Value)">
@@ -641,6 +648,8 @@
641648
<ParameterGroup>
642649
<EmccProperties ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
643650
<WasmOptConfigurationFlags ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
651+
<EmccDefaultExportedFunctions ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
652+
<EmccDefaultExportedRuntimeMethods ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
644653
</ParameterGroup>
645654
</UsingTask>
646655
</Project>

src/mono/wasm/build/WasmApp.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
<WasmExtraConfig Include="json" Value="{ &quot;abc&quot;: 4 }" />
8383
<WasmExtraConfig Include="string_val" Value="&quot;abc&quot;" />
8484
<WasmExtraConfig Include="string_with_json" Value="&quot;{ &quot;abc&quot;: 4 }&quot;" />
85+
- @(EmccExportedRuntimeMethod) - Extra method for emcc flag EXPORTED_RUNTIME_METHODS
86+
- @(EmccExportedFunction) - Extra function for emcc flag EXPORTED_FUNCTIONS
8587
-->
8688

8789
<PropertyGroup>

src/mono/wasm/wasm.proj

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,48 @@
108108
<Error Text="Failed to parse emcc version, and hash from the full version string: '$(_EmccVersionRaw)'"
109109
Condition="'$(_EmccVersion)' == '' or '$(_EmccVersionHash)' == ''" />
110110

111+
<ItemGroup>
112+
<EmccExportedRuntimeMethod Include="FS" />
113+
<EmccExportedRuntimeMethod Include="print" />
114+
<EmccExportedRuntimeMethod Include="ccall" />
115+
<EmccExportedRuntimeMethod Include="cwrap" />
116+
<EmccExportedRuntimeMethod Include="setValue" />
117+
<EmccExportedRuntimeMethod Include="getValue" />
118+
<EmccExportedRuntimeMethod Include="UTF8ToString" />
119+
<EmccExportedRuntimeMethod Include="UTF8ArrayToString" />
120+
<EmccExportedRuntimeMethod Include="FS_createPath" />
121+
<EmccExportedRuntimeMethod Include="FS_createDataFile" />
122+
<EmccExportedRuntimeMethod Include="removeRunDependency" />
123+
<EmccExportedRuntimeMethod Include="addRunDependency" />
124+
<EmccExportedRuntimeMethod Include="FS_readFile" />
125+
126+
<EmccExportedFunction Include="_malloc" />
127+
<EmccExportedFunction Include="_memalign" />
128+
<EmccExportedFunction Include="_htons" />
129+
<EmccExportedFunction Include="_ntohs" />
130+
</ItemGroup>
131+
<!-- after 3.0 -->
132+
<ItemGroup Condition="$([MSBuild]::VersionGreaterThan('$(_EmccVersion)', '3.0'))">
133+
<EmccExportedFunction Include="_memset" />
134+
</ItemGroup>
135+
<!-- before 3.1.7 see https://github.com/dotnet/runtime/issues/64724 -->
136+
<ItemGroup Condition="$([MSBuild]::VersionLessThan('$(_EmccVersion)', '3.1.7'))">
137+
<EmccExportedFunction Include="__get_daylight" />
138+
<EmccExportedFunction Include="__get_timezone" />
139+
<EmccExportedFunction Include="__get_tzname" />
140+
<EmccExportedFunction Include="g_free" />
141+
</ItemGroup>
142+
<!-- after 3.1.7 -->
143+
<ItemGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(_EmccVersion)', '3.1.7'))">
144+
<EmccExportedFunction Include="stackSave" />
145+
<EmccExportedFunction Include="stackRestore" />
146+
<EmccExportedFunction Include="stackAlloc" />
147+
<EmccExportedFunction Include="_free" />
148+
</ItemGroup>
111149
<PropertyGroup>
112-
<_DefaultExportedFunctions Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(_EmccVersion)', '3.1.7'))"
113-
>_malloc,stackSave,stackRestore,stackAlloc,_memalign,_memset,_htons,_ntohs,_free</_DefaultExportedFunctions>
114-
115-
<!-- _htons,_ntohs,__get_daylight,__get_timezone,__get_tzname are exported temporarily, until the issue is fixed in emscripten, https://github.com/dotnet/runtime/issues/64724 -->
116-
<_DefaultExportedFunctions Condition="'$(_DefaultExportedFunctions)' == '' and $([MSBuild]::VersionGreaterThan('$(_EmccVersion)', '3.0'))"
117-
>g_free,_malloc,_memalign,_memset,_htons,_ntohs,__get_daylight,__get_timezone,__get_tzname</_DefaultExportedFunctions>
118-
<_DefaultExportedFunctions Condition="'$(_DefaultExportedFunctions)' == '' and $([MSBuild]::VersionLessThan('$(_EmccVersion)', '3.0'))"
119-
>g_free,_malloc,_htons,_ntohs,__get_daylight,__get_timezone,__get_tzname,_memalign</_DefaultExportedFunctions>
150+
<_EmccExportedRuntimeMethods>&quot;[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedRuntimeMethods>
151+
<_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>
120152
</PropertyGroup>
121-
122153
<ItemGroup>
123154
<_EmccCommonFlags Condition="'$(WasmEnableSIMD)' == 'true'" Include="-msimd128" />
124155
<_EmccCommonFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-s USE_PTHREADS=1" />
@@ -128,8 +159,8 @@
128159
<_EmccLinkFlags Include="-s ALLOW_MEMORY_GROWTH=1" />
129160
<_EmccLinkFlags Include="-s NO_EXIT_RUNTIME=1" />
130161
<_EmccLinkFlags Include="-s FORCE_FILESYSTEM=1" />
131-
<_EmccLinkFlags Include="-s EXPORTED_RUNTIME_METHODS=&quot;['FS','print','ccall','cwrap','setValue','getValue','UTF8ToString','UTF8ArrayToString','FS_createPath','FS_createDataFile','removeRunDependency','addRunDependency', 'FS_readFile']&quot;" />
132-
<_EmccLinkFlags Include="-s EXPORTED_FUNCTIONS=$(_DefaultExportedFunctions)" Condition="'$(_DefaultExportedFunctions)' != ''" />
162+
<_EmccLinkFlags Include="-s EXPORTED_RUNTIME_METHODS=$(_EmccExportedRuntimeMethods)" />
163+
<_EmccLinkFlags Include="-s EXPORTED_FUNCTIONS=$(_EmccExportedFunctions)" />
133164
<_EmccLinkFlags Include="--source-map-base http://example.com" />
134165
<_EmccLinkFlags Include="-s STRICT_JS=1" />
135166
<_EmccLinkFlags Include="-s EXPORT_NAME=&quot;'createDotnetRuntime'&quot;" />
@@ -167,7 +198,9 @@
167198
],
168199
"WasmOptConfigurationFlags": [
169200
{ "identity": "WasmOptConfigurationFlags", "value": "$(_WasmOptConfigurationFlags)" }
170-
]
201+
],
202+
"EmccDefaultExportedFunctions": [@(EmccExportedFunction -> '%22%(Identity)%22', ',')],
203+
"EmccDefaultExportedRuntimeMethods": [@(EmccExportedRuntimeMethod -> '%22%(Identity)%22', ',')]
171204
}
172205
}
173206
]]>

0 commit comments

Comments
 (0)