Skip to content

Commit

Permalink
[wasi] Add build support for using wasm-opt (dotnet#95900)
Browse files Browse the repository at this point in the history
* [wasm/wasi] Extract target for running wasm-opt, and use for both browser, and wasi

* [wasm/wasi] Add new wasm-props.json to the runtime pack

For browser this is `emcc-props.json` renamed. And for wasi, it's a new
file.
  • Loading branch information
radical authored Dec 13, 2023
1 parent acc3f0c commit c5c54ef
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 88 deletions.
3 changes: 2 additions & 1 deletion eng/liveBuilds.targets
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
$(LibrariesNativeArtifactsPath)src\*.js;
$(LibrariesNativeArtifactsPath)src\emcc-default.rsp;
$(LibrariesNativeArtifactsPath)src\emcc-link.rsp;
$(LibrariesNativeArtifactsPath)src\emcc-props.json;"
$(LibrariesNativeArtifactsPath)src\wasm-props.json;"
NativeSubDirectory="src"
IsNative="true" />
<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'browser'"
Expand All @@ -246,6 +246,7 @@
<LibrariesRuntimeFiles Include="
$(LibrariesNativeArtifactsPath)src\wasi-default.rsp;
$(LibrariesNativeArtifactsPath)src\wasi-link.rsp;
$(LibrariesNativeArtifactsPath)src\wasm-props.json;
$(LibrariesNativeArtifactsPath)src\*.c"
NativeSubDirectory="src"
IsNative="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
<PlatformManifestFileEntry Include="wasm-config.h" IsNative="true" />
<PlatformManifestFileEntry Include="emcc-default.rsp" IsNative="true" />
<PlatformManifestFileEntry Include="emcc-link.rsp" IsNative="true" />
<PlatformManifestFileEntry Include="emcc-props.json" IsNative="true" />
<PlatformManifestFileEntry Include="wasm-props.json" IsNative="true" />
<PlatformManifestFileEntry Include="wasi-default.rsp" IsNative="true" />
<PlatformManifestFileEntry Include="wasi-link.rsp" IsNative="true" />
<PlatformManifestFileEntry Include="ILLink.Substitutions.WasmIntrinsics.xml" IsNative="true" />
Expand Down
34 changes: 16 additions & 18 deletions src/mono/wasi/build/WasiApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
_WasmGenerateNodeScripts;
</WasmGenerateAppBundleDependsOn>

<WasmEnableExceptionHandling Condition="'$(WasmEnableExceptionHandling)' == ''">false</WasmEnableExceptionHandling>
<WasmEnableSIMD Condition="'$(WasmEnableSIMD)' == ''">false</WasmEnableSIMD>

<SelfContained Condition="'$(IsWasiProject)' == 'true'">true</SelfContained>

<!-- Temporarily `false`, till sdk gets a fix for supporting the new file -->
<WasmEmitSymbolMap Condition="'$(WasmEmitSymbolMap)' == '' and '$(RunAOTCompilation)' != 'true'">false</WasmEmitSymbolMap>
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>

<WasmRunWasmOpt Condition="'$(WasmRunWasmOpt)' == ''">false</WasmRunWasmOpt>

<!--<WasiBundleAssemblies Condition="'$(WasiBundleAssemblies)' == ''">true</WasiBundleAssemblies>-->
<!-- FIXME: rename to WasmHost?? -->
<!--<WasiRunner Condition="'$(WasiRunner)' == ''">wasmtime</WasiRunner>-->
Expand Down Expand Up @@ -112,8 +111,6 @@
<_MonoAotCrossCompilerPath>@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','wasi-wasm'))</_MonoAotCrossCompilerPath>
<_WasmDefaultFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'wasi-default.rsp'))</_WasmDefaultFlagsRsp>
<_WasmDefaultLinkFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'wasi-link.rsp'))</_WasmDefaultLinkFlagsRsp>
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">false</WasmNativeStrip>
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == ''">true</WasmNativeStrip>
<WasmNativeDebugSymbols Condition="'$(WasmNativeDebugSymbols)' == ''">true</WasmNativeDebugSymbols>
<WasmLinkIcalls Condition="'$(WasmLinkIcalls)' == ''">$(WasmBuildNative)</WasmLinkIcalls>

Expand Down Expand Up @@ -239,6 +236,7 @@
</ItemGroup>

<Error Text="Could not find NativeFileReference %(NativeFileReference.Identity)" Condition="'%(NativeFileReference.Identity)' != '' and !Exists(%(NativeFileReference.Identity))" />
<Error Condition="'$(WasmSingleFileBundle)' == 'true' and '$(WasmMainAssemblyFileName)' == ''" Text="%24(WasmMainAssemblyFileName) is not set" />
</Target>

<Target Name="_WasiWriteCompileRsp" BeforeTargets="_WasmWriteRspForCompilingNativeSourceFiles">
Expand Down Expand Up @@ -322,21 +320,21 @@

<_WasmLinkDependencies Include="@(_WasiFilePathForFixup)" />

<_WasiSdkClangArgs Condition="'$(OS)' == 'Windows_NT'" Include="&quot;$([System.String]::new(%(_WasiFilePathForFixup.Identity)).Replace('\', '/'))&quot;" />
<_WasiSdkClangArgs Condition="'$(OS)' != 'Windows_NT'" Include="@(_WasiFilePathForFixup -> '&quot;%(Identity)&quot;')" />
<_WasiLinkStepArgs Condition="'$(OS)' == 'Windows_NT'" Include="&quot;$([System.String]::new(%(_WasiFilePathForFixup.Identity)).Replace('\', '/'))&quot;" />
<_WasiLinkStepArgs Condition="'$(OS)' != 'Windows_NT'" Include="@(_WasiFilePathForFixup -> '&quot;%(Identity)&quot;')" />

<_WasiSdkClangArgs Include="-Wl,--export=malloc,--export=free,--export=__heap_base,--export=__data_end" />
<_WasiLinkStepArgs Include="-Wl,--export=malloc,--export=free,--export=__heap_base,--export=__data_end" />
<!-- keep in sync with src\mono\wasi\wasi.proj -->
<_WasiSdkClangArgs Include="-Wl,-z,stack-size=8388608,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman"/>
<_WasiSdkClangArgs Include="-Wl,-s" Condition="'$(WasmNativeStrip)' == 'true'"/>
<_WasiLinkStepArgs Include="-Wl,-z,stack-size=8388608,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman"/>
<_WasiLinkStepArgs Include="-Wl,-s" Condition="'$(WasmNativeStrip)' == 'true'"/>

<_WasiSdkClangArgs Include="&quot;@$(_WasmDefaultLinkFlagsRsp.Replace('\', '/'))&quot;" />
<_WasiSdkClangArgs Include="@(_WasiClangXLinkerFlags -> '-Xlinker %(Identity)', ' ')" />
<_WasiSdkClangArgs Include="@(_WasiClangLDFlags)" />
<_WasiLinkStepArgs Include="&quot;@$(_WasmDefaultLinkFlagsRsp.Replace('\', '/'))&quot;" />
<_WasiLinkStepArgs Include="@(_WasiClangXLinkerFlags -> '-Xlinker %(Identity)', ' ')" />
<_WasiLinkStepArgs Include="@(_WasiClangLDFlags)" />

<_WasiSdkClangArgs Include="-o &quot;$(_WasiOutputFileName.Replace('\', '/'))&quot;" />
<_WasiLinkStepArgs Include="-o &quot;$(_WasiOutputFileName.Replace('\', '/'))&quot;" />

<_WasmLinkStepArgs Include="@(_WasiSdkClangArgs)" />
<_WasmLinkStepArgs Include="@(_WasiLinkStepArgs)" />
</ItemGroup>
</Target>

Expand All @@ -352,6 +350,8 @@
<Message Importance="High" Text="Performing WASI SDK build: &quot;$(WasiClang)&quot; &quot;$(_WasmLinkRsp)&quot;" />
<Exec Command="&quot;$(WasiClang)&quot; &quot;@$(_WasmLinkRsp)&quot;" />

<CallTarget Targets="_RunWasmOptPostLink" Condition="'$(WasmRunWasmOpt)' == 'true'" />

<!-- FIXME: this will be done by the bundler -->
<Copy SourceFiles="$(_WasiOutputFileName)" DestinationFolder="$(WasmAppDir)" />
<ItemGroup>
Expand All @@ -370,9 +370,7 @@
<Error Condition="'$(_WasiOutputFileName)' == ''" Text="Could not determine $(_WasiOutputFileName)" />
</Target>

<Target Name="_CheckToolchainIsExpectedVersion">
<Message Text="** TODO: Implement wasiclang version check target" Importance="High" />
</Target>
<Target Name="_CheckToolchainIsExpectedVersion" />

<Target Name="_GenerateRunWasmtimeScript">
<PropertyGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasi/build/WasiSdk.Defaults.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

<WasiSdkBinPath>$([MSBuild]::NormalizeDirectory($(WasiSdkRoot), 'bin'))</WasiSdkBinPath>
</PropertyGroup>
<ItemGroup>
<WasmToolchainEnvVars Include="PATH=$(WasiSdkBinPath)$(_PathSeparator)$([MSBuild]::Escape($(PATH)))" />
</ItemGroup>
</Project>
11 changes: 4 additions & 7 deletions src/mono/wasi/wasi.proj
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

<Target Name="GenerateWasiPropsAndRspFiles">

<!-- Generate wasi-props.json -->
<!-- Generate wasm-props.json -->
<ItemGroup>
<!-- TODOWASI
<_WasiLinkFlags Include="-Wl, - - allow-undefined"/>
Expand Down Expand Up @@ -146,21 +146,18 @@
</ItemGroup>

<PropertyGroup>
<_WasmOptConfigurationFlags>@(WasmOptConfigurationFlags, '%3B ') </_WasmOptConfigurationFlags>
<_WasiPropsJson>
<![CDATA[
{
"items": {
"WasmOptConfigurationFlags": [
{ "identity": "WasmOptConfigurationFlags", "value": "$(_WasmOptConfigurationFlags)" }
]
"WasmOptConfigurationFlags": [@(WasmOptConfigurationFlags -> '%22%(Identity)%22', ',')],
}
}
]]>
</_WasiPropsJson>
</PropertyGroup>

<WriteLinesToFile File="$(NativeBinDir)src\emcc-props.json"
<WriteLinesToFile File="$(NativeBinDir)src\wasm-props.json"
Lines="$(_WasiPropsJson)"
Overwrite="true"
WriteOnlyWhenDifferent="true" />
Expand Down Expand Up @@ -266,7 +263,7 @@
<ItemGroup>
<IcuDataFiles Include="$(NativeBinDir)*.dat" />
<WasmSrcFiles Include="$(NativeBinDir)src\*.c;" />
<WasmSrcFiles Include="$(_WasiDefaultsRspPath);$(_WasiCompileRspPath);$(_WasiLinkRspPath)" />
<WasmSrcFiles Include="$(_WasiDefaultsRspPath);$(_WasiCompileRspPath);$(_WasiLinkRspPath);$(NativeBinDir)src\wasm-props.json" />
<WasmHeaderFiles Include="$(NativeBinDir)include\wasm\*.h" />
</ItemGroup>

Expand Down
63 changes: 11 additions & 52 deletions src/mono/wasm/build/BrowserWasmApp.targets
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project>
<PropertyGroup>
<!-- Post Wasm MVP features -->
<WasmEnableExceptionHandling Condition="'$(WasmEnableExceptionHandling)' == ''">true</WasmEnableExceptionHandling>
<WasmEnableSIMD Condition="'$(WasmEnableSIMD)' == ''">$(WasmEnableExceptionHandling)</WasmEnableSIMD>
</PropertyGroup>

<Import Project="$(WasmCommonTargetsPath)WasmApp.Common.targets" />

<UsingTask TaskName="Microsoft.WebAssembly.Build.Tasks.WasmAppBuilder" AssemblyFile="$(WasmAppBuilderTasksAssemblyPath)" />

<PropertyGroup>
<PrepareInputsForWasmBuildDependsOn>
$(PrepareInputsForWasmBuildDependsOn);
_ReadEmccProps
</PrepareInputsForWasmBuildDependsOn>

<PrepareForWasmBuildNativeDependsOn>
_CheckToolchainIsExpectedVersion;
_PrepareForBrowserWasmBuildNative;
Expand All @@ -27,9 +28,6 @@
_WasmGenerateRunV8Script;
</WasmGenerateAppBundleDependsOn>

<!-- Post Wasm MVP features -->
<WasmEnableExceptionHandling Condition="'$(WasmEnableExceptionHandling)' == ''">true</WasmEnableExceptionHandling>
<WasmEnableSIMD Condition="'$(WasmEnableSIMD)' == ''">$(WasmEnableExceptionHandling)</WasmEnableSIMD>
<WasmEnableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
<WasmEnableJsInteropByValue Condition="'$(WasmEnableJsInteropByValue)' == '' and ( '$(WasmEnableThreads)' == 'true' or '$(MonoWasmBuildVariant)' == 'multithread' )">true</WasmEnableJsInteropByValue>
<WasmEnableJsInteropByValue Condition="'$(WasmEnableJsInteropByValue)' == ''">false</WasmEnableJsInteropByValue>
Expand Down Expand Up @@ -59,6 +57,8 @@
<_WasmDefaultFlags Condition="'$(WasmEnableExceptionHandling)' == 'false'">-fexceptions</_WasmDefaultFlags>
<_WasmDefaultFlags Condition="'$(WasmEnableExceptionHandling)' != 'false'">-fwasm-exceptions</_WasmDefaultFlags>
<_WasmDefaultFlags Condition="'$(WasmEnableSIMD)' == 'true'">-msimd128</_WasmDefaultFlags>

<_WasmOutputFileName Condition="'$(WasmSingleFileBundle)' != 'true'">dotnet.native.wasm</_WasmOutputFileName>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -208,15 +208,8 @@
</ItemGroup>

<PropertyGroup>
<!-- semicolon is a msbuild property separator. It is also the path separator on windows.
So, we need to escape it here, so the paths don't get split up when converting
to string[] for passing to Exec task -->
<_PathSeparator Condition="'$(OS)' == 'Windows_NT'">%3B</_PathSeparator>
<_PathSeparator Condition="'$(OS)' != 'Windows_NT'">:</_PathSeparator>

<_EmscriptenPrependPATHProperty>@(EmscriptenPrependPATH -> '%(Identity)', '$(_PathSeparator)')</_EmscriptenPrependPATHProperty>
</PropertyGroup>

<ItemGroup>
<EmscriptenEnvVars Include="PATH=$(_EmscriptenPrependPATHProperty)$(_PathSeparator)$([MSBuild]::Escape($(PATH)))" />

Expand Down Expand Up @@ -246,6 +239,8 @@
<_WasmDefaultLinkFlagsRsp>$([MSBuild]::NormalizePath($(_WasmRuntimePackSrcDir), 'emcc-link.rsp'))</_WasmDefaultLinkFlagsRsp>
<!--<_WasmLLVMPathForAOT>$(EmscriptenUpstreamBinPath)</_WasmLLVMPathForAOT>-->
<WasmLinkIcalls Condition="'$(WasmLinkIcalls)' == ''">$(WasmBuildNative)</WasmLinkIcalls>
<WasmRunWasmOpt Condition="'$(WasmRunWasmOpt)' == '' and '$(WasmNativeStrip)' == 'true'">true</WasmRunWasmOpt>
<WasmRunWasmOpt Condition="'$(WasmRunWasmOpt)' == ''">false</WasmRunWasmOpt>

<_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h</_WasmICallTablePath>
<_WasmRuntimeICallTablePath>$(_WasmIntermediateOutputPath)runtime-icall-table.h</_WasmRuntimeICallTablePath>
Expand Down Expand Up @@ -500,16 +495,7 @@
<FileWrites Include="$(_WasmIntermediateOutputPath)dotnet.native.js.symbols" Condition="'$(WasmEmitSymbolMap)' == 'true'" />
</ItemGroup>

<ItemGroup>
<!-- WasmOptConfigurationFlags property is set by reading from emcc-props.json -->
<WasmOptConfigurationFlags Condition="'$(WasmOptConfigurationFlags)' != ''" Include="$(WasmOptConfigurationFlags)" />
</ItemGroup>

<Message Text="Stripping symbols from dotnet.native.wasm ..." Importance="High" Condition="'$(WasmNativeStrip)' == 'true'" />
<Exec Command="wasm-opt$(_ExeExt) --enable-simd --enable-exception-handling @(WasmOptConfigurationFlags, ' ') --strip-dwarf &quot;$(_WasmIntermediateOutputPath)dotnet.native.wasm&quot; -o &quot;$(_WasmIntermediateOutputPath)dotnet.native.wasm&quot;"
Condition="'$(WasmNativeStrip)' == 'true'"
IgnoreStandardErrorWarningFormat="true"
EnvironmentVariables="@(EmscriptenEnvVars)" />
<CallTarget Targets="_RunWasmOptPostLink" Condition="'$(WasmRunWasmOpt)' == 'true'" />

<OnError ExecuteTargets="_OnError_WasmLinkDotNet_UndefinedSymbols"
Condition="'$(WasmAllowUndefinedSymbols)' != 'true' and '$(_EmccLinkStepExitCode)' != '0' and $([System.Text.RegularExpressions.Regex]::IsMatch($(_EmccLinkStepConsoleOutput), 'wasm-ld *: *error *:.*undefined symbol:'))" />
Expand Down Expand Up @@ -556,33 +542,6 @@
<Error Condition="'$(RuntimeEmccVersionRaw)' != '$(ActualEmccVersionRaw)'" Text="$(_VersionMismatchMessage)" />
</Target>

<UsingTask TaskName="ReadEmccProps" AssemblyFile="$(MonoTargetsTasksAssemblyPath)"
TaskFactory="JsonToItemsTaskFactory.JsonToItemsTaskFactory">
<ParameterGroup>
<EmccProperties ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
<WasmOptConfigurationFlags ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
<EmccDefaultExportedFunctions ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
<EmccDefaultExportedRuntimeMethods ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="false" Output="true" />
</ParameterGroup>
</UsingTask>

<Target Name="_ReadEmccProps" Condition="'$(_IsToolchainMissing)' != 'true'">
<ReadEmccProps JsonFilePath="$(_WasmRuntimePackSrcDir)emcc-props.json">
<Output TaskParameter="EmccProperties" ItemName="_EmccPropItems" />
<Output TaskParameter="WasmOptConfigurationFlags" ItemName="_WasmOptConfigurationFlagsItems" />
<Output TaskParameter="EmccDefaultExportedFunctions" ItemName="EmccExportedFunction" />
<Output TaskParameter="EmccDefaultExportedRuntimeMethods" ItemName="EmccExportedRuntimeMethod" />
</ReadEmccProps>

<CreateProperty Value="%(_EmccPropItems.Value)">
<Output TaskParameter="Value" PropertyName="%(_EmccPropItems.Identity)" />
</CreateProperty>

<CreateProperty Value="%(_WasmOptConfigurationFlagsItems.Value)">
<Output TaskParameter="Value" PropertyName="%(_WasmOptConfigurationFlagsItems.Identity)" />
</CreateProperty>
</Target>

<Target Name="_CompleteWasmBuildNative" AfterTargets="WasmLinkDotNet">
<ItemGroup>
<WasmNativeAsset Include="$(_WasmIntermediateOutputPath)dotnet.native.wasm" />
Expand Down
Loading

0 comments on commit c5c54ef

Please sign in to comment.