loading...

Build .NET AOT for WebAssembly in Visual Studio with Uno Platform

unoplatform profile image Uno Platform Originally published at platform.uno on ・4 min read

It’s been a long-time request: Build Uno Platform WebAssembly apps in Visual Studio on Windows using Ahead of Time (AOT) compilation.

Good news, it’s now available in preview in Uno dev branch! The Uno.Wasm.Bootstrap package 1.1-dev.416 package now provides the ability to create AOT, Mixed (AOT+Interpreter) and Interpreter (with Bitcode dependencies) builds on Windows 10, without leaving Visual Studio 2019. To our knowledge, Uno is the first one to provide this kind of functionality. If we are not – please let us know in the comments below; maybe we missed something.

This new Uno feature makes transparent use of the Windows Subsystem for Linux (WSL) to create the final binaries. You’ll only need to setup your WSL environment once using a simple script, and don’t worry if you forget to set it up, the bootstrapper will explain what to do.

Quick Start – Uno, Visual Studio, WASM, AOT

  1. Make sure WSL with Ubuntu 18.04 is installed
  2. Install the latest Uno Visual Studio templates
  3. Create a new Uno Cross Platform app project
  4. Update the Uno.Wasm.Bootstrap and Uno.Wasm.Bootstrap.Cli nuget packages to 1.1-dev.416 or later, using the Nuget Package manager (There’s no need to update other packages)
  5. In the Wasm project, add the following line in the first PropertyGroup :
<WasmShellMonoRuntimeExecutionMode>InterpreterAndAOT</WasmShellMonoRuntimeExecutionMode>
  1. Build once, you’ll get a message with a bash command to execute. (This script, for reference).
  2. Open a Windows Command line or PowerShell and paste the command there. It may take a few minutes as it installs Mono and .NET Core
  3. Build again. At this step, the build will install the required Emscripten version in the “obj” folder of your application. This may also take a few more minutes.
  4. Run the application without the debugger and you’ll get an AOT enabled build of your app

Two important notes on using AOT compilation:

  • Generating an AOT app always takes some time, which is why this is not enabled by default. You may need to enable is like this:
<WasmShellMonoRuntimeExecutionMode Condition="'$(Configuration)'=='Release'">InterpreterAndAOT</WasmShellMonoRuntimeExecutionMode>

This will enable AOT for release builds only and use the interpreter otherwise.

  • You may adjust the AOT process by balancing the use of the interpreter and AOT compiled code by using the MonoRuntimeMixModeExcludedAssembly Build time, package size , and runtime performance can be tuned balanced by adding or removing assemblies for the AOT generation.

More Technical Details

The dynamic linking feature (referencing a “.wasm” file, through P/Invoke in mono) was intended to be used for development purposes on Windows only as it gives decent build times.

This requirement is caused by the inability of the mono-wasm SDK to run on natively on Windows to do static linking (referencing an LLVM “.bc” bitcode file). This prevented developers using Visual Studio on Windows to build applications that make use of either AOT or that reference those libraries.

The deciding factor for enabling Windows support is the inability to use dynamic linking (the ability to reference “.wasm” files, through P/Invoke in mono) properly since the LLVM backend has been enabled in emscripten (1.39.1 and later). A chain of issues has made this process fragile and very difficult to work with. This also prevented the use of libraries such as libSkia or SQLite.

Windows 10 provides support for WSL, and a while back we provided some steps to build using AOT, but it still was an out of band manual process. Now with the updated bootstrapper, this process is transparent, given that you’ve setup your environment with the provided scripts.

This update also gives the ability to add some additional emscripten parameters, such as enabling back IDFBS which was disabled by the mono for some default size constraints. It also adds the ability to include other native libraries that may be referenced by external code, such as libtpng or libfreetype.

You can head over to the documentation for additional information.

In Closing

Features above are available in Uno ‘dev’ branch, which will eventually be merged into the ‘master’ branch. It is meant to those of you who are very eager to get started with it.

If you are new to the Uno Platform, you may want to run through the Getting Started tutorial. This will ensure you have the correct environment set up and that you have the latest release of Uno. Of course, we will be blogging more about AOT, Uno Platform and .NET / Visual Studio – so please stay tuned to our blogs and subscribe to Uno Platform blog RSS

The post Build .NET AOT for WebAssembly in Visual Studio with Uno Platform appeared first on Uno Platform.

Posted on by:

unoplatform profile

Uno Platform

@unoplatform

The only platform for building native mobile, desktop and WebAssembly with C#, XAML from a single codebase.

Discussion

markdown guide