|
1 | 1 | <Project>
|
2 | 2 | <PropertyGroup>
|
3 | 3 | <WitBindgenRuntime>native-aot</WitBindgenRuntime>
|
| 4 | + |
| 5 | + <!-- Keep this block all in sync manually, since URLs can be arbitrary --> |
| 6 | + <WasiSdkVersion>20.0</WasiSdkVersion> |
| 7 | + <WasiSdkUrl Condition="$([MSBuild]::IsOSPlatform('Windows'))">https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0.m-mingw.tar.gz</WasiSdkUrl> |
| 8 | + <WasiSdkUrl Condition="$([MSBuild]::IsOSPlatform('Linux'))">https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz</WasiSdkUrl> |
| 9 | + <WasiSdkUrl Condition="$([MSBuild]::IsOSPlatform('OSX'))">https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-macos.tar.gz</WasiSdkUrl> |
| 10 | + <WasiSdkRoot>$([System.IO.Path]::Combine("$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))", ".wasi-sdk", "wasi-sdk-$(WasiSdkVersion)"))</WasiSdkRoot> |
| 11 | + |
| 12 | + <EmSdkVersion>3.1.61</EmSdkVersion> |
| 13 | + <EmSdkUrl>https://github.com/emscripten-core/emsdk/archive/refs/tags/$(EmSdkVersion).zip</EmSdkUrl> |
| 14 | + <!-- Support bring your own emscripten if $(EMSDK) is already set--> |
| 15 | + <EmscriptenRoot Condition="'$(EMSDK)' == ''">$([System.IO.Path]::Combine("$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))", ".emsdk", "emsdk-$(EmSdkVersion)"))</EmscriptenRoot> |
| 16 | + <EmscriptenRoot Condition="'$(EMSDK)' != ''">$(EMSDK)</EmscriptenRoot> |
4 | 17 | </PropertyGroup>
|
5 | 18 |
|
| 19 | + <!-- |
| 20 | + MSBuild stuff to acquire the necessary SDKs (WASI SDK and Emscripten) automatically. It will take a few mins on the |
| 21 | + first build on a given machine, but after that should no-op. |
| 22 | + --> |
| 23 | + <Target Name="PrepareWasmSdks" BeforeTargets="CheckWasmSdks" DependsOnTargets="ObtainWasiSdk; ObtainEmscripten"> |
| 24 | + <PropertyGroup> |
| 25 | + <EmSdk>$(EmscriptenRoot)</EmSdk> |
| 26 | + <Wasicompiler>$(EmscriptenRoot)\upstream\emscripten\emcc.bat</Wasicompiler> |
| 27 | + <WASI_SDK_PATH>$(WasiSdkRoot)</WASI_SDK_PATH> |
| 28 | + </PropertyGroup> |
| 29 | + </Target> |
| 30 | + |
| 31 | + <Target Name="ObtainEmscripten" Condition="'$(EMSDK)' == '' AND !(Exists($(EmscriptenRoot)))"> |
| 32 | + <!-- |
| 33 | + This is not ideal because if your solution has multiple projects that use WasmComponent.Sdk, then if you |
| 34 | + build in parallel in CI where your machine doesn't already have wasi-sdk/emsdk, then it may try to download |
| 35 | + and extract the SDKs multiple times in parallel to the same disk location, which may cause it to fail. |
| 36 | + The only reason this doesn't happen in this repo is that it explicitly runs the PrepareWasmSdks task before |
| 37 | + building other projects. |
| 38 | +
|
| 39 | + For a proper fix, consider implementing an MSBuild task in C# that obtains wasi-sdk/emsdk, and uses a mutex |
| 40 | + so that only one flow executes at a time, with others blocking until it's done. |
| 41 | + --> |
| 42 | + |
| 43 | + <PropertyGroup> |
| 44 | + <EmSdkDownloadTempDir>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), $([System.IO.Path]::GetRandomFileName())))</EmSdkDownloadTempDir> |
| 45 | + </PropertyGroup> |
| 46 | + |
| 47 | + <MakeDir Directories="$(EmSdkDownloadTempDir)" /> |
| 48 | + <DownloadFile |
| 49 | + SourceUrl="$(EmSdkUrl)" |
| 50 | + DestinationFolder="$(EmSdkDownloadTempDir)"> |
| 51 | + <Output TaskParameter="DownloadedFile" ItemName="EmSdkDownloadTempFile" /> |
| 52 | + </DownloadFile> |
| 53 | + |
| 54 | + <!-- Windows 10+ has tar built in, so this should work cross-platform --> |
| 55 | + <Message Importance="high" Text="Extracting @(EmSdkDownloadTempFile) to $(EmscriptenRoot)..." /> |
| 56 | + <MakeDir Directories="$(EmscriptenRoot)" /> |
| 57 | + <Exec Command="tar -xf "@(EmSdkDownloadTempFile)" -C . --strip-components=1" WorkingDirectory="$(EmscriptenRoot)" /> |
| 58 | + <RemoveDir Directories="$(EmSdkDownloadTempDir)" /> |
| 59 | + |
| 60 | + <Exec Command="emsdk install $(EmSdkVersion)" WorkingDirectory="$(EmscriptenRoot)" /> |
| 61 | + <Exec Command="emsdk activate $(EmSdkVersion)" WorkingDirectory="$(EmscriptenRoot)" /> |
| 62 | + </Target> |
| 63 | + |
| 64 | + <Target Name="ObtainWasiSdk" Condition="!(Exists($(WasiSdkRoot)))"> |
| 65 | + <PropertyGroup> |
| 66 | + <WasiSdkDownloadTempDir>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), $([System.IO.Path]::GetRandomFileName())))</WasiSdkDownloadTempDir> |
| 67 | + </PropertyGroup> |
| 68 | + |
| 69 | + <MakeDir Directories="$(WasiSdkDownloadTempDir)" /> |
| 70 | + <DownloadFile |
| 71 | + SourceUrl="$(WasiSdkUrl)" |
| 72 | + DestinationFolder="$(WasiSdkDownloadTempDir)"> |
| 73 | + <Output TaskParameter="DownloadedFile" ItemName="WasiSdkDownloadTempFile" /> |
| 74 | + </DownloadFile> |
| 75 | + |
| 76 | + <!-- Windows 10+ has tar built in, so this should work cross-platform --> |
| 77 | + <Message Importance="high" Text="Extracting @(WasiSdkDownloadTempFile) to $(WasiSdkRoot)..." /> |
| 78 | + <MakeDir Directories="$(WasiSdkRoot)" /> |
| 79 | + <Exec Command="tar -xf "@(WasiSdkDownloadTempFile)" -C . --strip-components=1" WorkingDirectory="$(WasiSdkRoot)" /> |
| 80 | + <RemoveDir Directories="$(WasiSdkDownloadTempDir)" /> |
| 81 | + </Target> |
| 82 | + |
| 83 | + <!-- |
| 84 | + Following generats and compiles the wit code for the c# project |
| 85 | + --> |
6 | 86 | <Target Name="WitCompile_BeforeCsCompile" BeforeTargets="BeforeCompile"
|
7 | 87 | Condition="'$(Language)' == 'C#' AND '@(Wit)' != ''"
|
8 |
| - DependsOnTargets="WitCompile_GetDependencies; WitCompile_InvokeTool"> |
| 88 | + DependsOnTargets="PrepareWasmSdks; WitCompile_GetDependencies; WitCompile_InvokeTool"> |
9 | 89 | <ItemGroup>
|
10 | 90 | <Compile Include="$(WitGeneratedFilesRoot)**\*.cs" />
|
11 | 91 | <NativeObjects Include="$(WitGeneratedFilesRoot)**\*.o" />
|
|
40 | 120 | <ItemGroup>
|
41 | 121 | <CabiReAllocFiles Include="$(WitGeneratedFilesRoot)**\*World_cabi_realloc.c" />
|
42 | 122 | </ItemGroup>
|
43 |
| - <Message Importance="high" Text="building cabi... @(CabiReAllocFiles->'"%(FullPath)"', ' ')" /> |
44 |
| - <Exec WorkingDirectory="$(WitGeneratedFilesRoot)" Command="emcc.bat @(CabiReAllocFiles->'"%(FullPath)"', ' ') -c"/> |
| 123 | + <Message Importance="high" Text="building cabi_realloc files... @(CabiReAllocFiles->'"%(FullPath)"', ' ') with $(Wasicompiler)" /> |
| 124 | + <Exec WorkingDirectory="$(WitGeneratedFilesRoot)" Command=""$(Wasicompiler)" @(CabiReAllocFiles->'"%(FullPath)"', ' ') -c"/> |
45 | 125 |
|
46 | 126 | <ItemGroup>
|
47 | 127 | <WitGeneratedCsFiles Include="$(WitGeneratedFilesRoot)**\*.cs" />
|
|
0 commit comments