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

[NativeAOT] MSBuild-related logic to get projects to build #9583

Merged
merged 1 commit into from
Dec 11, 2024

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Dec 3, 2024

This is an initial step to get the NativeAOT build working the same as
iOS does. At a high level, the changes are:

  • Run ILLink with our existing trimmer/linker pipeline.

  • Temporarily change our RID to linux-bionic-arm64.

  • Pass the output to ILC, the NativeAOT compiler.

  • Turn the RID back to android-arm64.

A list of MSBuild/workload changes required so far:

  • $(UseMonoRuntime) is false when $(PublishAot) is true

  • Set $(_IsPublishing) as NativeAOT expects dotnet publish to be
    the only way NativeAOT is used. Setting this in our targets allows
    the proper *.NativeAOT.* packs to restore inside of a dotnet build.

  • $(SelfContained)=true, seems to be required for the
    linux-bionic-arm64 runtime packs to restore. All Android apps are
    self-contained, so this might be fine.

  • Create a new Microsoft.Android.Sdk.NativeAOT.targets file that
    uses $(_AndroidNdkDirectory) to set various properties for
    NativeAOT's build (ILC).

  • HACK: set $(_targetOS) to linux to make the NativeAOT build
    attempt to run. --targetos:android results in a build error that
    says Android is not supported.

  • Disable $(RunAOTCompilation), Mono's AOT compiler and the
    <RemoveRegisterAttribute/> MSBuild task.

  • Disable $(AndroidEnableMarshalMethods) to be re-enabled in a
    future PR.

A list of blockers or things that don't work yet:

  • NativeAOT apps won't launch yet (future changes for that)

  • The NativeAOT cross compiler doesn't run on Windows:

Microsoft.NETCore.Native.Publish.targets(61,5): Cross-OS native compilation is not supported.
Microsoft.NETCore.Native.Unix.targets(296,5): error : Platform linker ('C:\Android\android-sdk\ndk\26.3.11579264\toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang++' or 'gcc') not found in PATH. Ensure you have all the required prerequisites documented at https://aka.ms/nativeaot-prerequisites.
  • You can only target -r android-arm64 as there are no other
    linux-bionic-* runtime packs yet.

  • -p:PublishAotUsingRuntimePack=true is currently required, but I
    will investigate if we should just set this by default.

@jonathanpeppers jonathanpeppers mentioned this pull request Dec 3, 2024
@jonathanpeppers

This comment was marked as outdated.

@jonathanpeppers

This comment was marked as outdated.

@jonathanpeppers

This comment was marked as outdated.

jonathanpeppers added a commit that referenced this pull request Dec 6, 2024
Context: #9538
Context: #9583

Expanding upon #9538, there are more changes required to support
multiple runtime flavors in our build.

* Remove `$(MicrosoftAndroidArmPackDir)` and related properties, to
  rely on a new `@(AndroidAbiAndRuntimeFlavor)` item group instead.

* Exclude our Mono-related native libraries from NativeAOT runtime
  packs.

* Ensure that our runtime packs copy assemblies (`Mono.Android.dll`,
  etc.) to the appropriate NativeAOT-related directories.

* List our `Microsoft.Android.Runtime.NativeAOT.*` packs in the
  `WorkloadManifest.json`, so that `**FromWorkload**` can resolve the
  version number for them. They can still restore via NuGet.
jonathanpeppers added a commit that referenced this pull request Dec 6, 2024
Context: #9538
Context: #9583

Expanding upon #9538, there are more changes required to support
multiple runtime flavors in our build.

* Remove `$(MicrosoftAndroidArmPackDir)` and related properties, to
  rely on a new `@(AndroidAbiAndRuntimeFlavor)` item group instead.

* Exclude our Mono-related native libraries from NativeAOT runtime
  packs.

* Ensure that our runtime packs copy assemblies (`Mono.Android.dll`,
  etc.) to the appropriate NativeAOT-related directories.

* List our `Microsoft.Android.Runtime.NativeAOT.*` packs in the
  `WorkloadManifest.json`, so that `**FromWorkload**` can resolve the
  version number for them. They can still restore via NuGet.
jonathanpeppers added a commit that referenced this pull request Dec 9, 2024
Context: #9538
Context: #9583

Expanding upon #9538, there are more changes required to support
multiple runtime flavors in our build.

* Remove `$(MicrosoftAndroidArmPackDir)` and related properties, to
  rely on a new `@(AndroidAbiAndRuntimeFlavor)` item group instead.

* Exclude our Mono-related native libraries from NativeAOT runtime
  packs.

* Ensure that our runtime packs copy assemblies (`Mono.Android.dll`,
  etc.) to the appropriate NativeAOT-related directories.

* List our `Microsoft.Android.Runtime.NativeAOT.*` packs in the
  `WorkloadManifest.json`, so that `**FromWorkload**` can resolve the
  version number for them. They can still restore via NuGet.
jonathanpeppers added a commit that referenced this pull request Dec 9, 2024
Context: #9538
Context: #9583

Expanding upon #9538, there are more changes required to support
multiple runtime flavors in our build.

* Remove `$(MicrosoftAndroidArmPackDir)` and related properties, to
  rely on a new `@(AndroidAbiAndRuntimeFlavor)` item group instead.

* Exclude our Mono-related native libraries from NativeAOT runtime
  packs.

* Ensure that our runtime packs copy assemblies (`Mono.Android.dll`,
  etc.) to the appropriate NativeAOT-related directories.

* List our `Microsoft.Android.Runtime.NativeAOT.*` packs in the
  `WorkloadManifest.json`, so that `**FromWorkload**` can resolve the
  version number for them. They can still restore via NuGet.
@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/nativeaot/builds branch from 508ba8e to 546c241 Compare December 10, 2024 15:07
This is an initial step to get the NativeAOT build working the same as
iOS does. At a high level, the changes are:

* Run `ILLink` with our existing trimmer/linker pipeline.

* Temporarily change our RID to `linux-bionic-arm64`.

* Pass the output to `ILC`, the NativeAOT compiler.

* Turn the RID back to `android-arm64`.

A list of MSBuild/workload changes required so far:

* `$(UseMonoRuntime)` is false when `$(PublishAot)` is true

* Set `$(_IsPublishing)` as NativeAOT expects `dotnet publish` to be
  the only way NativeAOT is used. Setting this in our targets allows
  the proper `*.NativeAOT.*` packs to restore inside of a `dotnet
  build`.

* `$(SelfContained)=true`, seems to be required for the
  `linux-bionic-arm64` runtime packs to restore. All Android apps are
  self-contained, so this might be fine.

* Create a new `Microsoft.Android.Sdk.NativeAOT.targets` file that
  uses `$(_AndroidNdkDirectory)` to set various properties for
  NativeAOT's build (ILC).

* `HACK:` set `$(_targetOS)` to `linux` to make the NativeAOT build
  attempt to run. `--targetos:android` results in a build error that
  says Android is not supported.

* Disable `$(RunAOTCompilation)`, Mono's AOT compiler and the
  `<RemoveRegisterAttribute/>` MSBuild task.

* Disable `$(AndroidEnableMarshalMethods)` to be re-enabled in a
  future PR.

A list of blockers or things that don't work yet:

* NativeAOT apps won't launch yet (future changes for that)

* The NativeAOT cross compiler doesn't run on Windows:

    Microsoft.NETCore.Native.Publish.targets(61,5): Cross-OS native compilation is not supported.
    Microsoft.NETCore.Native.Unix.targets(296,5): error : Platform linker ('C:\Android\android-sdk\ndk\26.3.11579264\toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang++' or 'gcc') not found in PATH. Ensure you have all the required prerequisites documented at https://aka.ms/nativeaot-prerequisites.

* You can only target `-r android-arm64` as there are no other
  `linux-bionic-*` runtime packs yet.

* `-p:PublishAotUsingRuntimePack=true` is currently required, but I
  will investigate if we should just set this by default.
@jonathanpeppers jonathanpeppers force-pushed the dev/peppers/nativeaot/builds branch from 546c241 to a35f711 Compare December 10, 2024 15:14
@jonathanpeppers jonathanpeppers marked this pull request as ready for review December 10, 2024 17:13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 6 changed files in this pull request and generated no suggestions.

Files not reviewed (5)
  • src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets: Language not supported
  • src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets: Language not supported
  • src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.NativeAOT.targets: Language not supported
  • src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets: Language not supported
  • src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets: Language not supported
Comments skipped due to low confidence (2)

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs:109

  • [nitpick] The method name 'NativeAOT' is not descriptive. Consider renaming it to 'BuildNativeAOTApplication' or something similar to better reflect its purpose.
public void NativeAOT ()

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs:113

// Ensure you have all the required prerequisites documented at https://aka.ms/nativeaot-prerequisites.
Copy link
Contributor

@dellis1972 dellis1972 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks reasonable

@jonpryor jonpryor merged commit 2fa7954 into main Dec 11, 2024
58 checks passed
@jonpryor jonpryor deleted the dev/peppers/nativeaot/builds branch December 11, 2024 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants