Skip to content

Commit

Permalink
[wasm][wasi] Throw error when WasmBuildNative is explicitly set to fa…
Browse files Browse the repository at this point in the history
…lse during a single-file build (#98087)

Implement general rule when WasmBuildNative is explicitly false
  • Loading branch information
mkhamoyan authored Feb 21, 2024
1 parent 52e1858 commit 7af3a6f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
6 changes: 6 additions & 0 deletions eng/testing/linker/project.csproj.template
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
{AdditionalProjectReferences}
</ItemGroup>

<Target Name="RemoveInvariantGlobalization" BeforeTargets="_SetWasmBuildNativeDefaults" Condition="'$(TargetArchitecture)' == 'wasm'">
<ItemGroup>
<_BoolPropertiesThatTriggerRelinking Remove="InvariantGlobalization" />
</ItemGroup>
</Target>

<Target Name="LocateNativeCompiler"
Condition="'$(PublishAot)' == 'true' and '$(_hostOS)' != 'win'"
BeforeTargets="SetupOSSpecificProps">
Expand Down
1 change: 0 additions & 1 deletion src/mono/browser/build/BrowserWasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@

<_BoolPropertiesThatTriggerRelinking Include="WasmEnableSIMD" DefaultValueInRuntimePack="true" />
<_BoolPropertiesThatTriggerRelinking Include="WasmEnableExceptionHandling" DefaultValueInRuntimePack="true" />
<_BoolPropertiesThatTriggerRelinking Include="WasmNativeStrip" DefaultValueInRuntimePack="true" />
</ItemGroup>

<Target Name="_GetWasmGenerateAppBundleDependencies">
Expand Down
1 change: 0 additions & 1 deletion src/mono/wasi/build/WasiApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<ItemGroup>
<_BoolPropertiesThatTriggerRelinking Include="WasmEnableSIMD" DefaultValueInRuntimePack="false" />
<!--<_BoolPropertiesThatTriggerRelinking Include="WasmEnableExceptionHandling" DefaultValueInRuntimePack="true" />-->
<_BoolPropertiesThatTriggerRelinking Include="WasmNativeStrip" DefaultValueInRuntimePack="true" />
</ItemGroup>

<Target Name="_GetWasiGenerateAppBundleDependencies">
Expand Down
19 changes: 19 additions & 0 deletions src/mono/wasm/Wasm.Build.Tests/NativeBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,24 @@ public void IntermediateBitcodeToObjectFilesAreNotLLVMIR(BuildArgs buildArgs, st
+ " It might fail if it was incorrectly compiled to a bitcode file, instead of wasm.");
}

[Theory]
[BuildAndRun(host: RunHost.None, aot: true)]
public void NativeBuildIsRequired(BuildArgs buildArgs, string id)
{
string projectName = $"native_build_{buildArgs.Config}_{buildArgs.AOT}";

buildArgs = buildArgs with { ProjectName = projectName, ExtraBuildArgs = "-p:WasmBuildNative=false -p:WasmSingleFileBundle=true" };
buildArgs = ExpandBuildArgs(buildArgs);

(_, string output) = BuildProject(
buildArgs,
id: id,
new BuildProjectOptions(
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), s_mainReturns42),
DotnetWasmFromRuntimePack: false,
ExpectSuccess: false));

Assert.Contains("WasmBuildNative is required", output);
}
}
}
23 changes: 16 additions & 7 deletions src/mono/wasm/build/WasmApp.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
<UsingTask TaskName="MonoTargetsTasks.MarshalingPInvokeScanner" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />
<UsingTask TaskName="EmitBundleObjectFiles" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />

<ItemGroup>
<_BoolPropertiesThatTriggerRelinking Include="InvariantTimezone" DefaultValueInRuntimePack="false" />
<_BoolPropertiesThatTriggerRelinking Include="InvariantGlobalization" DefaultValueInRuntimePack="false" />
<_BoolPropertiesThatTriggerRelinking Include="WasmNativeStrip" DefaultValueInRuntimePack="true" />
<_BoolPropertiesThatTriggerRelinking Include="WasmSingleFileBundle" DefaultValueInRuntimePack="false" />
</ItemGroup>

<PropertyGroup>
<PrepareInputsForWasmBuildDependsOn>
$(PrepareInputsForWasmBuildDependsOn);
Expand Down Expand Up @@ -494,14 +501,18 @@
Text="$(_ToolchainMissingErrorMessage) SDK is required for AOT'ing assemblies." />

<ItemGroup>
<_BoolPropertiesThatTriggerRelinking Include="InvariantTimezone" DefaultValueInRuntimePack="false" />
<_BoolPropertiesThatTriggerRelinking Include="InvariantGlobalization" DefaultValueInRuntimePack="false" />
<_ChangedBoolPropertiesThatTriggerRelinking Include="%(_BoolPropertiesThatTriggerRelinking.Identity)" Condition="'$(%(_BoolPropertiesThatTriggerRelinking.Identity))' != '' and
'$(%(_BoolPropertiesThatTriggerRelinking.Identity))' != '%(_BoolPropertiesThatTriggerRelinking.DefaultValueInRuntimePack)'" />
</ItemGroup>
<PropertyGroup>
<_WasmBuildNativeRequired Condition="@(_ChangedBoolPropertiesThatTriggerRelinking->Count()) > 0">true</_WasmBuildNativeRequired>
</PropertyGroup>

<Error Condition="'$(WasmBuildNative)' == 'false' and '$(_WasmBuildNativeRequired)' == 'true'"
Text="WasmBuildNative is required because %(_ChangedBoolPropertiesThatTriggerRelinking.Identity)=$(%(_ChangedBoolPropertiesThatTriggerRelinking.Identity)), but WasmBuildNative is already set to 'false'." />

<PropertyGroup>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and
'$(%(_BoolPropertiesThatTriggerRelinking.Identity))' != '' and
'$(%(_BoolPropertiesThatTriggerRelinking.Identity))' != '%(_BoolPropertiesThatTriggerRelinking.DefaultValueInRuntimePack)'">true</WasmBuildNative>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(_WasmBuildNativeRequired)' == 'true'">true</WasmBuildNative>
</PropertyGroup>

<!-- When Building -->
Expand All @@ -510,15 +521,13 @@
<WasmBuildNative Condition="'$(RunAOTCompilation)' == 'true' and '$(RunAOTCompilationAfterBuild)' == 'true'">true</WasmBuildNative>

<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and @(NativeFileReference->Count()) > 0" >true</WasmBuildNative>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(WasmSingleFileBundle)' == 'true'">true</WasmBuildNative>
</PropertyGroup>

<!-- When Publishing -->
<PropertyGroup Condition="'$(WasmBuildingForNestedPublish)' == 'true'">
<!-- AOT==true overrides WasmBuildNative -->
<WasmBuildNative Condition="'$(RunAOTCompilation)' == 'true'">true</WasmBuildNative>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and @(NativeFileReference->Count()) > 0" >true</WasmBuildNative>
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(WasmSingleFileBundle)' == 'true'">true</WasmBuildNative>

<!-- not aot, not trimmed app, no reason to relink -->
<WasmBuildNative Condition="'$(WasmBuildNative)' == '' and '$(PublishTrimmed)' != 'true'">false</WasmBuildNative>
Expand Down

0 comments on commit 7af3a6f

Please sign in to comment.