Skip to content

Commit

Permalink
[One .NET] fix AOT builds with different settings
Browse files Browse the repository at this point in the history
Building a .NET 6 `Release` app with `UseInterpreter=true` and
`RunAOTCompilation=true` would fail with:

    Microsoft.Android.Sdk.Aot.targets(71,5): Unknown Mode value: Interpreter. 'Mode' must be one of: Normal,JustInterp,Full,FullInterp,Hybrid,LLVMOnly,LLVMOnlyInterp

Reviewing the code:

https://github.com/dotnet/runtime/blob/60edc0bdafce1849fec19e3ec4f766074de50fc3/src/tasks/AotCompilerTask/MonoAOTCompiler.cs#L1064-L1073

There does not appear to be a mode of `Normal` + `Interp`. So let's
just make AOT take precendence over `$(UseInterpreter)`. On Android
`$(UseInterpreter)` is a Debug-mode setting for Hot Reload.

Building a .NET 6 `Debug` app `RunAOTCompilation=true` would fail with:

    Microsoft.Android.Sdk.Aot.targets(49,5): error MSB4044: The "GetAotAssemblies" task was not given a value for the required parameter "AndroidApiLevel".

This is because the linker was skipped, particularly this target:

    <Target Name="_PrepareLinking"
        Condition=" '$(PublishTrimmed)' == 'true' "
        AfterTargets="ComputeResolvedFilesToPublishList"
        DependsOnTargets="GetReferenceAssemblyPaths;_CreatePropertiesCache">

`_CreatePropertiesCache` doesn't run, and so we have some empty
properties in this case. We can simply add `_CreatePropertiesCache` to
the `_AndroidAot` MSBuild target's `DependsOnTargets` to solve this
issue.
  • Loading branch information
jonathanpeppers committed Dec 8, 2021
1 parent f98c4a8 commit 8fb948f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).

<Target Name="_AndroidAot"
Condition=" '$(AotAssemblies)' == 'true' and '$(RuntimeIdentifier)' != '' "
DependsOnTargets="_AndroidAotInputs"
DependsOnTargets="_CreatePropertiesCache;_AndroidAotInputs"
Inputs="@(_AndroidAotInputs)"
Outputs="$(_AndroidStampDirectory)_AndroidAot.stamp">
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,37 @@ public void BenchmarkDotNet ()
builder.AssertHasNoWarnings ();
}

static readonly object [] SettingCombinationsSource = new object [] {
// Interpreter + AOT
new object [] {
/* isRelease */ true,
/* useInterpreter */ true,
/* publishTrimmed */ true,
/* aot */ true,
},
// Debug + AOT
new object [] {
/* isRelease */ false,
/* useInterpreter */ false,
/* publishTrimmed */ false,
/* aot */ true,
},
};

[Test]
[TestCaseSource (nameof (SettingCombinationsSource))]
public void SettingCombinations (bool isRelease, bool useInterpreter, bool publishTrimmed, bool aot)
{
var proj = new XASdkProject {
IsRelease = isRelease,
};
proj.SetProperty ("UseInterpreter", useInterpreter.ToString ());
proj.SetProperty ("PublishTrimmed", publishTrimmed.ToString ());
proj.SetProperty ("RunAOTCompilation", aot.ToString ());
var builder = CreateDotNetBuilder (proj);
Assert.IsTrue (builder.Build (), $"{proj.ProjectName} should succeed");
}

DotNetCLI CreateDotNetBuilder (string relativeProjectDir = null)
{
if (string.IsNullOrEmpty (relativeProjectDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.

<!-- Ahead-of-time compilation properties -->
<AotAssemblies Condition=" '$(AndroidEnableProfiledAot)' == 'True' ">True</AotAssemblies>
<AndroidAotMode Condition=" '$(AndroidUseInterpreter)' != 'False' ">Interpreter</AndroidAotMode>
<AndroidAotMode Condition=" '$(AotAssemblies)' != 'True' And '$(AndroidUseInterpreter)' == 'True' ">Interpreter</AndroidAotMode>
<AndroidAotMode Condition=" '$(AndroidAotMode)' == '' And '$(AotAssemblies)' == 'True' ">Normal</AndroidAotMode>
<AndroidAotMode Condition=" '$(AndroidAotMode)' == '' ">None</AndroidAotMode>
<AotAssemblies Condition=" '$(AndroidAotMode)' != '' And '$(AndroidAotMode)' != 'None' And '$(AndroidAotMode)' != 'Interpreter' ">True</AotAssemblies>
Expand Down

0 comments on commit 8fb948f

Please sign in to comment.