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

[release/6.0][wasm] Add support for native relinking after Build, and AOT after publish #58913

Merged
merged 48 commits into from
Sep 15, 2021

Conversation

radical
Copy link
Member

@radical radical commented Sep 9, 2021

Wasm app build can run in two scenarios:

  1. For build, eg. when running the app in VS, or dotnet build foo.csproj
  2. For Publish, eg, when publishing the app to a folder, or Azure

A dotnet wasm app has some native wasm files (dotnet.wasm, and dotnet.js). How these files are obtained, or generated:

  • Build

    • with no native libraries referenced (AOT setting is ignored here)
      • files from the runtime pack are used as-is
    • with native libraries referenced
      • dotnet.wasm is relinked with the native libraries
  • Publish

    • dotnet.wasm is relinked with the native libraries, and updated pinvoke/icalls from the trimmed assemblies
    • if RunAOTCompilation=true, then the relinking includes AOT'ed assemblies

Build

Implementation:

  • Target WasmBuildApp

  • runs after Build by default

    • which can be disabled by $(DisableAutoWasmBuildApp)
    • or the run-after target can be set via $(WasmBuildAppAfterThisTarget)
  • To run a custom target

    • before any of the wasm build targets, use $(WasmBuildAppDependsOn), and prepend your target name to that
    • after any of the wasm build targets, use AfterTargets="WasmBuildApp" on that target
  • Avoid depending on this target, because it is available only when the workload is installed. Use $(WasmNativeWorkload) to check if it is installed.

Publish

Implementation:

  • This part runs as a nested build using a MSBuild task, which means that the project gets reevaluated. So, if there were any changes made to items/properties in targets before this, then they won't be visible in the nested build.

  • By default WasmTriggerPublishApp runs after the Publish target, and that triggers the nested build

    • The nested build runs WasmNestedPublishApp, which causes Build, and Publish targets to be run
    • Because this causes Build to be run again, if you have any targets that get triggered by that, then they will be running twice.
      • But the original build run, and this publish run can be differentiated using $(WasmBuildingForPublish)
  • WasmTriggerPublishApp essentially just invokes the nested publish

    • This runs after Publish

      • which can be disabled by $(DisableAutoWasmPublishApp)
      • or the run-after target can be set via $(WasmTriggerPublishAppAfterThisTarget)
    • To influence the wasm build for publish, use WasmNestedPublishApp

      • To run a custom target before it, use $(WasmNestedPublishAppDependsOn)
      • to run a custom target after it, use AfterTargets="WasmNestedPublishApp"
    • If you want to dependsOn on this, then use DependsOnTargets="WasmTriggerPublishApp"

  • Fixes [net6.0][BlazorWasmSdk] build does not link native files #56783

  • Fixes If you build with trimming enabled, future builds with it disabled will produce incorrect output #53612

  • Fixes [wasm] Avoid unnecessary linking after Build, when publishing from VS #58973

  • Also fixes building for AOT with trimming disabled
    Fixes Cannot AOT compile with trimming disabled #53804

Renames the temporary folder used for native files built after *Build*
target, from `relink` to `for-build`, to be consistent with publish.
When publishing with AOT (`RunAOTCompilation=true`), the assemblies get
trimmed by the linker, and copied to a single `linked/` folder. Then
`MonoAOTCompiler` runs `mono-aot-cross` on those assemblies. Since, all
the assemblies have their dependencies in the same folder too,
`mono-aot-cross` can resolve them from there.

But when trimming is disabled (`PublishTrimmed=false`),
`MonoAOTCompiler` gets paths to assemblies from different folders. For
example, some might be from `obj`, nugets etc. Which means that
`mono-aot-cross` might not always be able to find dependencies for the
assemblies being AOTed.

To fix this, if all the input assemblies are not in the same directory,
then we copy them to a temporary one, and then run `mono-aot-cross` on
them.

Fixes dotnet#53804
When publishing with VS, it builds with `$(DeployOnBuild)=true`. That
causes `DotNetPublish` to run, which runs `Publish` after `Build`.

Wasm targets run `WasmTriggerPublishApp`, that then uses the MSBuild
task to invoke `WasmNestedPublishApp, which depends on `Publish`, which
depends on `Build`. And that causes `DotNetPublish` again, which results
in:

```
sdk/6.0.100-rc.2.21459.15/Sdks/Microsoft.NET.Sdk.Publish/targets/CopyTargets/Microsoft.NET.Sdk.Publish.CopyFiles.targets(80,5):

error MSB4006: There is a circular dependency in the target dependency graph involving target "Publish".
Since "_CopyAspNetCoreFilesToIntermediateOutputPath" has "DependsOn" dependence on "Publish", the circular is "Publish<-_CopyAspNetCoreFilesToIntermediateOutputPath<-CorePublish<-DotNetPublish<-_PublishBuildAlternative<-Publish".
```

Fix is to unset `DeployOnBuild` for the nested build
(`WasmNestedPublishApp`).
`DefaultTemplate_AOT_OnlyWithPublishCommandLine_Then_PublishNoAOT`

This test runs build thrice:
1. simple build (no relinking)
2. publish+aot
3. publish-no-aot (relinking)

This causes the publish folder to have more than one `dotnet*hash*js`
files, which breaks the test since we don't know which one is the
"current" one.
This reverts commit 48085f2.

- We want to revert this to a version that works with the currently
  available SDK, which doesn't have the ComputeBlazorPublishAsset fixes,
  or the new cache-busting changes.
@marek-safar marek-safar merged commit f38d58f into dotnet:release/6.0 Sep 15, 2021
radical added a commit to radical/runtime that referenced this pull request Sep 15, 2021
radical added a commit to radical/runtime that referenced this pull request Sep 15, 2021
radical added a commit to radical/runtime that referenced this pull request Sep 15, 2021
…ild, and AOT after publish (dotnet#58913)"

This reverts commit 00859ec.

This will be ported in a separate PR.
radical added a commit that referenced this pull request Sep 16, 2021
… AOT after publish (#58913)

Forward port #58913

(cherry picked from commit f38d58f)
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-Build-mono
Projects
None yet
3 participants