# Uno.Wasm.Bootstrap [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/unoplatform/Uno.Wasm.Bootstrap) Uno.Wasm.Bootstrap provides a simple way to package C# .NET code, and run it from a compatible browser environment. It is a standalone .NET Web Assembly (WASM) sdk bootstrapper taking the form of a nuget package. Installing it on a .NET 5 project or .NET Standard 2.0 library with an entry point allows to publish it as part of a WASM distribution folder, along with CSS, Javascript and content files. This package only provides the bootstrapping features to run a .NET assembly and write to the javascript console, through `Console.WriteLine`. This package is based on the excellent work from @praeclarum's [OOui Wasm MSBuild task](https://github.com/praeclarum/Ooui). ## How to use the package with .NET 5 and later * Create a .NET 5 Console Application, and update it with the following basic definition: ```xml Exe net5.0 true ``` * Add a main entry point: ```csharp class Program { static void Main(string[] args) { Console.WriteLine("Hello from C#!"); } } ``` * In Visual Studio 2019, press `Ctrl+F5` to start without the debugger (this will create the `launchSettings.json` needed below for debugging) * A browser window will appear with your application * The output of the Console.WriteLine will appear in the javascript debugging console ### How to use the Visual Studio 2019 Debugger Starting from **Visual Studio 2019 16.6**, it is possible to debug a WebAssembly app. To enable the debugging, add the following line to your `launchSettings.json` file: ```json "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}" ``` in every profile section of the file, below each `"launchBrowser": true,` line. Press `F5` to start debugging. ### Alternate deployment methods Install the `[dotnet serve](https://github.com/natemcmaster/dotnet-serve)` tool: ``` dotnet tool install -g dotnet-serve ``` Once installed, launch the server by using the following command: ```bash cd MyApp.Wasm dotnet serve -d bin\Debug\net5.0\dist -p 8000 ``` You application will be available `http://localhost:8000`. ### Upgrading from previous versions of the Uno.Wasm.Bootstrap package Previously, the suggested project structure was a .NET Standard 2.0 project using the non-web projects SDK. To enable debugging and easier deployment, the support for `Microsoft.NET.Sdk.Web` has been added. To upgrade a project from 1.1 to 1.2: - If you had a `` line, remove it - Add the `` item in the same item group as the other nuget packages. To upgrade a project from 1.0 to 1.1: - Change `Microsoft.NET.Sdk` to `Microsoft.NET.Sdk.Web` in the Sdk attribute of your project - Add the `` item in the same item group as the other nuget packages. ## Linker configuration The .NET tooling uses the [ILLinker](https://github.com/mono/linker/tree/master/), and can be configured using a linker directives file. The Bootstrapper searches for a file placed in an ItemGroup named `LinkerDescriptor`. See examples below. ### Configuration file (commonly named `LinkerConfig.xml`) ```xml ``` The documentation for this file [can be found here](https://github.com/mono/linker/blob/master/docs/data-formats.md#xml-examples). ### Reference in project file It is also possible to provide the linker file as an embedded resource, which is useful when creating libraries. The linker step will discover those files and apply the configuration. ``` xml $(AssemblyName).xml ``` The Linker can be disabled completely by setting the `WasmShellILLinkerEnabled` property to false. This property has no effect when building with AOT enabled. ### .NET 5 Feature Linker Configuration The bootstrapper supports the [feature switches configuration](https://github.com/dotnet/runtime/blob/master/docs/workflow/trimming/feature-switches.md) provided by .NET 5. By default, some features are linked out as those are not likely to be used in a WebAssembly context: - `EventSourceSupport` - `EnableUnsafeUTF7Encoding` - `HttpActivityPropagationSupport` If you need to enable any of those features, you can set the following in your csproj first `PropertyGroup`: ```xml true ``` ## Publishing the build results The easiest way to publish the build results is to use the Visual Studio publish menu on your project. This will allow to use all the features provided by the standard experience, as described in the [Deploy to Azure App Service](https://docs.microsoft.com/en-us/visualstudio/deployment/quickstart-deploy-to-azure?view=vs-2017). The publication of the application must be done in .NET Framework hosting (and not .NET Core), as the app uses the `web.config` file for the server configuration, and to enable the use of pre-compression. For deeper integration in the publishing pipeline, the `WasmShellOutputPackagePath` property is defined by the bootstrapper after the `BuildDist` target, which contains the path to the generated `package_XXX` content. ## Serve the Wasm app through Windows Linux Subsystem Using Windows 10, serving the app through a small Web Server is done through WSL. Here's how to install it: - Search for Ubuntu in the Windows Store: https://www.microsoft.com/en-us/search/result.aspx?q=ubuntu - Install Ubuntu 18.04 or later, and follow the instructions during the first run - If you have another distribution installed make sure that the 18.04 is the default using `wslconfig /s "Ubuntu-20.04"`. You can list your active distributions with `wslconfig /l` - Note that WSL 2 is considerably slower than WSL 1 for the boostrapper scenario. You will need to set your distribution to version 1 using `wsl --set-version "Ubuntu-20.04" 1`. - Once you've built your project, you should see a path to the project dll - In the Ubuntu shell, type ``cd `wslpath "[the_path_to_your_bin_folder]\dist"` `` - Type `python3 server.py` - If this command does not exist, run the following `sudo apt-get install python3` - Using your favorite browser, navigate to `http://localhost:8000` ## .NET for WebAssembly Debugger Support Debugging is supported through the integration of a .NET Core CLI component, which acts as a static files server, as well as a debugger proxy for Chrome (other browsers are not supported). ### Enable the Debugger support In order to debug an **Uno.Wasm.Boostrap** enabled project, the Mono runtime debugger must be enabled: ```xml true ``` Debug symbols need to be emitted and be of the type `portable`: ```xml portable true ``` Finally, the `DEBUG` constant must be defined ```xml $(DefineConstants);TRACE;DEBUG ``` Doing so will enable the deployment of `pdb` files to the browser, and allow for the mono debugger proxy to use them. For the time being, you will also need to make sure that mscorlib is disabled in the Linker configuration file: ```xml ``` .NET for WebAssembly now has integrated **preliminary** support for in-browser debugging. Refer to [this document for up-to-date information](https://github.com/mono/mono/tree/master/sdks/wasm#debugging) on how to set up the debugging. ### How to use the Browser debugger The boostrapper also supports debugging directly in the browser debugging tools. In Visual Studio: - Make your project the startup project (right-click **set as startup**) - In the debugging toolbar: - Select **IIS Express** as the debugging target - Select **Chrome** as the Web Browser - Make sure script debugging is disabled - Start the debugging session using F5 (or Start Debug) - Once your application has started, press `Alt+Shift+D` - Follow the instructions on the web page - You may need to refresh the original tab if you want to debug the entry point (Main) of your application. ### Debugger troubleshooting The debugger is still under development, and here are a few things to look for: - Breakpoints set sometimes disappear when the debugged page is reloaded - If none of your assemblies appear in the debugger window, it's generally caused by the debugger caching previously loaded files. Make sure to hit Ctrl+Shit+R to force reload the debugged page. ## Runtime Execution Modes The mono for WebAssembly runtime provides three execution modes, Interpreter, AOT (Ahead of Time) and Mixed Mode Interpreter/AOT. The execution mode can be set as follows: ```xml Interpreter ``` The possible values are: - `Interpreter` (the default mode) - `InterpreterAndAOT` - `FullAOT` To setup your machine to use AOT modes on Windows, you will need to install [Python from Windows Store](https://www.microsoft.com/store/productId/9P7QFQMJRFP7), or manually through [Python's official site](https://www.python.org/downloads/). ### Interpreter mode This mode is the slowest of all three, but allows for great flexibility and debugging, as well as an efficient payload size. The linker mode can also be completely disabled for troubleshooting, as this will not impact the wasm payload size. ### Full AOT Mode This mode generates WebAssembly binary for all the referenced assemblies and provides the fastest code execution, but also generates the largest payload. This mode will not allow the execution of code that was not known at compile time (e.g. dynamically generated assemblies or loaded through `Assembly.LoadFrom`). > The FullAOT mode currently fails at runtime using net5 or net6 because of [this issue](https://github.com/dotnet/runtime/issues/50609). ### Mixed Interpreter and AOT Mode This mode enable AOT compilation for most of the assemblies, with [some specific exceptions](https://github.com/dotnet/runtime/issues/50609). This mode is generally prefered to FullAOT as it allows to load arbitrary assemblies and execute their code through the interpreter. ## Profile Guided AOT This mode allows for the AOT engine to selectively optimize methods to WebAssembly, and keep the rest as interpreted. This gives a very good balance when choosing between performance and payload size. It also has the advantage of reducing the build time, as less code needs to be compiled down to WebAssembly. This feature is used in two passes: - The first pass needs the creation of a profiled interpreter build, which records any methods invoked during the profiling session. - The second pass rebuilds the application using the Mixed AOT/Interpreter mode augmented by the recording created during the first pass. This mode gives very good results, where the RayTracer sample of this repository goes from an uncomressed size of 5.5MB to 2.9MB. To create a profiled build: - In your Wasm csproj, add the following: ```xml true ``` - In your `LinkerConfig.xml` file, add the following: ```xml ``` - Run the application once, without the debugger (e.g. Ctrl+F5) - Navigate throughout the application in high usage places. - Once done, either: - Press the `Alt+Shift+P` key sequence - Launch App.saveProfile() - Download the `aot.profile` file next to the csproj file - Comment the `WasmShellGenerateAOTProfile` line - Add the following lines: ```xml ``` - Make sure that Mixed mode is enabled: ```xml InterpreterAndAOT ``` - Build you application again Note that the AOT profile is a snapshot of the current set of assemblies and methods in your application. If that set changes significantly, you'll need to re-create the AOT profile to get optimal results. ### AOT Profile method exclusion The generated profile contains all the methods found to be executed during the profiling session, but some methods may still need to be manually excluded for some reasons (e.g. runtime or compile time errors). The `WasmShellAOTProfileExcludedMethods` property specifies a semi-colon separated list of regular expressions to exclude methods from the profile: ```xml Class1\.Method1;Class2\.OtherMethod $(WasmShellAOTProfileExcludedMethods);Class3.* ``` The `MixedModeExcludedAssembly` is also used to filter the profile for assemblies, see below for more information. Dumping the whole list of original and filtered list is possible by adding: ```xml true ``` This will generate files named `AOTProfileDump.*.txt` in the `obj` folder for inspection. ### Mixed AOT/Interpreter Mode This modes allows for the WebAssembly generation of parts of the referenced assemblies, and falls back to the interpreter for code that was excluded or not known at build time. This allows for a fine balance between payload size and execution performance. At this time, it is only possible to exclude assemblies from being compiled to WebAssembly through the use of this item group: ```xml ``` Adding assemblies to this list will exclude them from being compiled to WebAssembly. ### AOT Debugging and mono tracing (.NET 5 only) When running with PG-AOT/FullAOT, exceptions generally do not provide stack traces, as WebAssembly as of the MVP does not yet support stack walking. For the time being, it's still possible view browser stack traces in the log by enabling mono tracing. First, you'll need to add the following class to your app: ```csharp static class MonoInternals { [DllImport("__Native")] internal static extern void mono_trace_enable(int enable); [DllImport("__Native")] internal static extern int mono_trace_set_options(string options); } ``` Then in the `Main` of your application, add the following: ```csharp MonoInternals.mono_trace_enable(1); MonoInternals.mono_trace_set_options("E:all"); ``` This will enable the tracing of all application exceptions (caught or not), along with the associated native host stack traces. You can find the documentation for [`mono_trace_set_options` parameter here](https://www.mono-project.com/docs/debug+profile/debug/#tracing-program-execution). ## Required configuration for static linking on macOS - macOS 10.15 or later - [Homebrew](https://brew.sh) - [.NET SDK 5.0](https://dotnet.microsoft.com/download/dotnet/5.0) for net5 builds - A recent build of [Mono MDK - Stable](https://www.mono-project.com/download/stable/#download-mac) - ninja: `brew install ninja` We recommend that you install newer versions of Uno.Wasm.Bootstrap as it provides a better out-of-the-box experience(e.g. Emscripten is automatically installed). Please note that `AOT` and `Mixed Mode` are not supported yet. ## Required configuration for AOT, Mixed Mode or static linking on Linux - Ubuntu 18.04+ or a [container](https://hub.docker.com/r/unoplatform/wasm-build) - A [stable build of mono](https://www.mono-project.com/download/stable/#download-lin) - A [.NET SDK](https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu) >= 3.1 - ninja: `apt-get install ninja-build` We recommend that you install newer versions of Uno.Wasm.Bootstrap as it provides a better out-of-the-box experience(e.g. Emscripten is automatically installed). The easiest is to build using the environment provided by the [unoplatform/wasm-build docker image](https://hub.docker.com/r/unoplatform/wasm-build). ## Required configuration for AOT, Mixed Mode or external bitcode support compilation on Windows 10 ### Native windows tooling This is the default mode on Windows. It requires installing [Python from Windows Store](https://www.microsoft.com/store/productId/9P7QFQMJRFP7), or manually through [Python's official site](https://www.python.org/downloads/). This mode is compatible with CI servers which have Python installed by default, such as [Azure Devops Hosted Agents](https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops). ### Using Windows Subsystem for Linux This mode can be enabled by adding this property to the `csproj`: ```xml false ``` Requirements: - A Windows 10 machine with [WSL 1 or 2 with Ubuntu 18.04](https://docs.microsoft.com/en-us/windows/wsl/install-win10) installed. - A [stable build of mono](https://www.mono-project.com/download/stable/#download-lin) - A [.NET SDK](https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu) >= 3.1 - ninja: `apt-get install ninja-build` in WSL If you have another distribution installed make sure that the 18.04 is the default using `wslconfig /s "Ubuntu-20.04"`. You can list your active distributions with `wslconfig /l` Note that WSL 2 is considerably slower than WSL 1 for the boostrapper scenario. You will need to set your distribution to version 1 using `wsl --set-version "Ubuntu-20.04" 1`. During the first use of WSL, if the environment is not properly setup, you will be guided to run the [`dotnet-setup.sh`](/src/Uno.Wasm.Bootstrap/build/scripts/dotnet-setup.sh) script that will install Mono, .NET Core and some additional dependencies. The emscripten installation is automatically done as part of the build. The boostrapper uses its own installation of emscripten, installed by default in `$HOME/.uno/emsdk` in the WSL filesystem. This can be globally overriden by setting the `WASMSHELL_WSLEMSDK` environment variable. #### Special considerations for CI servers (GitHub Actions, Azure Devops) When building an application on Windows based CI servers, WSL is generally not enabled in base images. This can cause builds to fail if they require the use of static linking and/or AOT. In order to work around this issue, the following property can be set: ```xml true ``` **It is important to note that generating a build this way, on a Windows CI server, without WSL enabled will generate an interpreter only build, and generate an invalid package if static linking was to be required.** When using GitHub actions: ```xml true ``` When using Azure Devops: ```xml true ``` ## Debugging and contributing to the Uno WebAssembly Bootstrapper The [src/Uno.Wasm.Bootstrap.sln](src/Uno.Wasm.Bootstrap.sln) solution is a good way to build the bootstrapper itself, as well as sample solutions that validate the different features of the bootstrapper. ### Debugging in Visual Studio for Windows - Select a sample application, such as the `Uno.Wasm.Sample` project, and press `Ctrl+F5` or run **without debugger**. - The bootstrapper will be built as part of the process, and will generate a new webassembly site layout. - Once the application has built, it will run in the selected browser in the Visual Studio debug location toolbar Some tips: - If you make modifications to the `Uno.Wasm.Bootstrap`, you may have to terminate all `msbuild.exe` processes, as they may lock files of that project. - If you make modifications to the `Uno.Wasm.Bootstrap.Cli` project, you may have to terminate the `dotnet.exe` processes that link to your solution's subfolders, as they may lock files of that project. Once the processes have been terminated, restart your build. Debugging the bootstrapper task can be done by adding a `Debugger.Launch()` statement in the `Run` method of `ShellTask.cs`. ### Testing the bootstrapper through GitPod You can also make contributions through GitPod, and validate that your changes are appropriate. Building and debugging samples is done through the command line. 1. Build a sample using : ``` cd src/Uno.Wasm.Sample msbuild /r /bl ``` 1. Start the web server to serve the sample on port 8000: ``` cd bin/Debug/net5.0/dist python3 server.py ``` 1. The GitPod IDE will open a preview window with the content of the site. You may need to open the browser debugger window to see the results of the sample's execution. Click on the button below to try this out! [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/unoplatform/Uno.Wasm.Bootstrap) ## Features ### WebAssembly Module Linking support #### Static Linking overview Statically linking Emscripten LLVM Bitcode (`.bc` and `.a` files) files to mono is supported on both Windows 10 and Linux. To build on Windows please refer to the AOT environment setup instructions. This linking type embeds the `.bc` or `.a` files with the rest of the WebAssembly modules, and uses _normal_ webassembly function invocations that are faster than with dynamic linking. Any `.bc` or `.a` file placed as `content` in the built project will be statically linked to the currently running application. This allowing for p/invoke to be functional when resolving methods from the loaded module. If you have a `.bc` or a `.a` file you don't want to be include in the linking, you may add the `UnoAotCompile="false"` metadata that way: ``` xml ``` #### Static Linking multi-version support As emscripten's ABI is not guaranteed to be compatible between versions, it may also be required to include multiple versions of the same LLVM binaries, compiled against different versions of LLVM. In order to enable this scenario, the Uno Bootstrapper supports adding .bc files by convention. If the bitcode file to be added is named `libTest.bc`, the following structure can be used in your project: - `libTest.bc/2.0.6/libTest.bc` - `libTest.bc/2.0.9/libTest.bc` In this case, based on the emscripten version used by the mono runtime, the bootstrapper will choose the closest matching version. #### Static Linking additional emcc flags Static linking may also require some additional emscripten flags, for instance when using libpng. In such a case, add the following to your project: ```xml ``` For more information, see the `Uno.Wasm.StaticLinking.Aot` sample side module build script. ### Static linking additional P/Invoke libraries When building applications, in some cases, NuGet provided libraries may use native dependencies that are emscripten provided libraries, such as `libc`. In such cases, the boostrapper allows for providing a set of known P/Invoke libraries as follows: ```xml ``` It's important to note that providing additional libraries this way implies that all the imported functions will have to be available during emcc link operation. Any missing function will result in a missing symbol error. ### Additional C/C++ files The bootstrapper provides the ability to include additional C/C++ files to the final package generation. This feature can be used to include additional source files for native operations that may be more difficult to perform from managed C# code, but can also be used to override some weak aliases with [ASAN](https://emscripten.org/docs/debugging/Sanitizers.html). #### Usage ```xml ``` The file is provided as-is to `emcc` and its resulting object file is linked with the rest of the compilation. This feature is meant to be used for small additions of native code. If more is needed (e.g. adding header directories, defines, options, etc...) it is best to use the emcc tooling directly. #### Example Here's an example of file: ```cpp #include #include #include #define WASM_EXPORT __attribute__((visibility("default"))) extern "C" { WASM_EXPORT int additional_native_add(int a, int b); } WASM_EXPORT int additional_native_add(int a, int b) { printf("test_add(%d, %d)\r\n", a, b); return a + b; } ``` which can be used in C# as follows: ```csharp static class AdditionalImportTest { [DllImport("__Native")] internal static extern int additional_native_add(int left, int right); } ``` #### Emscripten Linker optimizations flags When building with AOT or using static linking of bitcode files, the emscripten linker step is enabled and runs optimizations on the generated code. These steps can be very expensive depending on the final binary size, and disabling those optimizations can be useful to improve the development loop. To control those optimizations, use the following msbuild property: ```xml false ``` This flag is automatically set to `false` when running in a configuration named `Debug`. The optimization level can be adjusted with the following: ```xml Level3 ``` Allowed values are: - `None` (`-O0`) - `Level1` (`-O1`) - `Level2` (`-O2`) - `Level3` (`-O3`) - `Maximum` (`-Oz`) - Any other value will be passed onto emcripten without modifications The default value is `Level3`. #### Invoking emscripten and Mono/.NET 5 native functions In order to invoke emscripten and mono native functions, the bootstrapper exposes the special library name `__Native`. For instance, the following enables the mono internal tracing: ``` static class MonoInternals { [DllImport("__Native")] internal static extern void mono_trace_enable(int enable); } ``` Or use emscripten functions: ``` [DllImport("__Internal_emscripten")] public static extern void emscripten_console_log(string str); ``` ### Threads support > .NET 5/6 for WebAssembly does not support Threading (as of 2021-01-01), enabling this option will result in build errors (missing `dotnet.worker.js` file) Mono now supports the ability to create threads, in browsers that support it (Chrome 79+, Edge 81+). Threads are backed by [`atomics` and WebWorkers](https://emscripten.org/docs/porting/pthreads.html). To enable the support, add the following configuration: ```xml threads-release ``` Note that executing javascript in the context of a thread stays in the worked that is assigned to the thread, thus modifying the DOM from that context will do nothing. To update the UI, execution will need to go back to the main thread, generally by using a mecanism similar to `System.Threading.Timer` which uses `setTimeout` so execute on the main thread. ### Pre-Compression support Pre-compression has two modes: - In-place, where Brotli compressed files are placed next to original files - Legacy, where a `web.config` file url rewriter rule is used The parameters for the pre-compression are as follows: - `WasmShellGenerateCompressedFiles` which can be `true` or `false`. This property is ignored when building `MonoRuntimeDebuggerEnabled` is set to `true`, and `true` by default when the `Configuration` property is set to `Release` - `WasmShellCompressedExtension` is an item group which specifies which files to compress. By default `wasm`, `clr`, `js`, `css` and `html files are pre-compressed. More files can be added as follows: ```xml ``` - `WasmShellBrotliCompressionQuality` which controls the compression quality used to pre-compress the files. The default value is 7. - `WasmShellCompressionLayoutMode` which can be set to `InPlace` or `Legacy`. If not set and for backward compatility reasons, `Legacy` is automatically selected if a `web.config` file is detected in the layout, and contains the `_compressed_br` string. #### Support for in-place compression This mode is to be preferred for web servers that support `accept-encoding` header file rewriting. In the case of [**Azure Static WebApps**](https://docs.microsoft.com/en-us/azure/static-web-apps/get-started-portal), if a file next to the original one is suffixed with `.br`, and the client requested for brotli compressed files, the in-place compressed file will be served. #### Support for IIS / Azure Webapp GZip/Brotli pre-compression The IIS compression support has too many knobs for the size of generated WebAssembly files, which makes the serving of static files inefficient. The Bootstrapper tooling will generate two folders `_compressed_gz` and `_compressed_br` which contain compressed versions of the main files. A set IIS rewriting rules are used to redirect the queries to the requested pre-compressed files, with a preference for Brotli. When building an application, place [the following file](src/Uno.Wasm.Sample/wwwroot/web.config) in the `wwwroot` folder to automatically enable the use of pre-compressed files. Note that the pre-compressed files are optional, and if the rewriting rules are removed or not used (because the site is served without IIS), the original files are available at their normal locations. ### Node.js support The bootstrapper supports having a project loaded as part of a node application. To do so: - Create a Wasm bootstrapper project, named `MyApp.Wasm` - Create a Node.js TypeScript project in Visual Studio, named `MyApp.Runner` - In boostrapper project, add the following : ```xml ../MyApp.Runner/app node ``` - In the `app.ts`, add the following: ```js require("./app/mono"); ``` Run the application and the main method of the `MyApp.Wasm` will be executed. The parameters of the node command line are provided to the app's main method when running the app as follows: ``` node app param1 param2 ``` An example of the node.js support is available in the `Uno.Wasm.Node.Sample` and `Uno.Wasm.Node.Sample.Runner.njsproj` projects. ### Browser Embedded mode By default, the project is launched with a HTML page (`index.html`). This mode is used for SPAs (Single Page Applications), but does not allow embedding into an existing webpage or application. It is possible to use the Browser Embedded mode to allow the launching using JavaScript instead. 1. Add this line in your project file: ``` xml BrowserEmbedded ``` The `embedded.js` file will be generated instead of `index.html`, containing the required code to launch the application. 2. In the HTML where you want to host the application, add the following: Using HTML: ``` html