Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
submodules: 'true'
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.100-rtm.23516.5
- name: Restore dependencies
run: dotnet restore
- name: Prepare WASM SDKs
run: dotnet msbuild src/WasmComponent.Sdk/build/WasmComponent.Sdk.targets /t:PrepareWasmSdks
run: dotnet msbuild src/WitBindgen/build/WitBindgen.targets /t:PrepareWasmSdks
- name: Build
run: dotnet build --no-restore /p:BuildNumber=${{ github.run_number }}
- name: Test
Expand Down
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "modules/wit-bindgen"]
path = modules/wit-bindgen
url = https://github.com/jsturtevant/wit-bindgen.git
url = https://github.com/bytecodealliance/wit-bindgen.git
[submodule "modules/wasm-tools"]
path = modules/wasm-tools
url = https://github.com/dicej/wasm-tools.git
url = https://github.com/bytecodealliance/wasm-tools.git
64 changes: 58 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ Open an issue detailing the issue you've encountered or feature you would like t

Bug fixes and new features must be submitted using a pull request and pass CI to be included in the project.

## Building the project locally
## Building

Requires [.NET 8+](https://dotnet.microsoft.com/en-us/download)

```
dotnet msbuild src/WasmComponent.Sdk/build/WasmComponent.Sdk.targets /t:PrepareWasmSdks
git submodule update --init
## needed to avoid errors with multiple projects calling and downloading the sdks at same time (https://github.com/bytecodealliance/componentize-dotnet/issues/8)
dotnet msbuild src/WitBindgen/build/WitBindgen.targets /t:PrepareWasmSdks
dotnet build
```

If you are experiencing issues with values not being updated, try running `dotnet clean` and using the steps above

## Testing

Run the tests:
Expand All @@ -23,5 +21,59 @@ Run the tests:
dotnet test
```

> tip: If you've already built the project you can speed up the tests by running `dotnet test --no-build`

## Build Wasm tools locally

> requires [rust](https://www.rust-lang.org/tools/install)

The project is configured by default to pull tools such as [wit-bindgen](https://github.com/bytecodealliance/wit-bindgen) from their releases. It is possible to use custom builds of these tools via submodules:

```
## get submodules
git submodule update --int

## get latest code from configured branch
git submodule update --recursive --remote

## optional, change the branch for the project
cd modules/<project>
git checkout <branch>
```

Modify the [WasmComponentSdk.csproj](./src/WasmComponent.Sdk/WasmComponent.Sdk.csproj) to enable building from source:

```
<BuildWasmToolsLocally>true</BuildWasmToolsLocally>
```

Modify the [WitBindgen.csproj](./src/WitBindgen/WitBindgen.csproj) to enable building from source:

```
<BuildWitBindgenLocally>true</BuildWitBindgenLocally>
```

And then follow the [project build steps](#building).

### Debugging

Create a msbuild debug log:

```
dotnet build /bl
```

View the log with https://www.msbuildlog.com/.

Learn more at [trouble shooting techniques](https://learn.microsoft.com/en-us/visualstudio/ide/msbuild-logs?view=vs-2022) for msbuild.

## Getting help
While we work on improving the documentation for contributing, if you have any questions please drop a note in the [c# zulip chat](https://bytecodealliance.zulipchat.com/#narrow/stream/407028-C.23.2F.2Enet-collaboration).

> If you have any questions please drop a note in the [c# zulip chat](https://bytecodealliance.zulipchat.com/#narrow/stream/407028-C.23.2F.2Enet-collaboration).

This project uses MSbuild and .NET Project SDKS. Learn more about this tooling in:

- [MSbuild docs](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild?view=vs-2022)
- [.NET Project SDKS](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview)
- [Creating Reliable Builds](https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/msbuild-best-practices-for-creating-reliable-builds-part-1#id0090093)

2 changes: 1 addition & 1 deletion modules/wasm-tools
Submodule wasm-tools updated 4709 files
2 changes: 1 addition & 1 deletion modules/wit-bindgen
Submodule wit-bindgen updated 221 files
4 changes: 2 additions & 2 deletions samples/calculator/Adder/OperationsImpl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace wit_computer.Wit.exports.example.calculator.Operations;
namespace ComputerWorld.wit.exports.example.calculator;

public class OperationsImpl : Operations
public class OperationsImpl : IOperations
{
public static int Add(int left, int right)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

<Target Name="ComposeWasmComponent" AfterTargets="Build">
<PropertyGroup>
<EntrypointComponent>../CalculatorHost/bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/CalculatorHost.component.wasm</EntrypointComponent>
<DependencyComponent>../Adder/bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/Adder.component.wasm</DependencyComponent>
<EntrypointComponent>../CalculatorHost/bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/calculatorhost-component.wasm</EntrypointComponent>
<DependencyComponent>../Adder/bin/$(Configuration)/$(TargetFramework)/wasi-wasm/native/adder-component.wasm</DependencyComponent>
</PropertyGroup>

<MakeDir Directories="dist" />
Expand Down
2 changes: 1 addition & 1 deletion samples/calculator/CalculatorHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using wit_hostapp.Wit.imports.example.calculator.Operations;
using HostappWorld.wit.imports.example.calculator;

var left = 123;
var right = 456;
Expand Down
15 changes: 6 additions & 9 deletions src/WasmComponent.Sdk/WasmComponent.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

<!-- Things you might want to edit -->
<!-- Set BuildWasmToolsLocally to true if you want to build modules/wasm-tools locally and use its output -->
<BuildWasmToolsLocally>true</BuildWasmToolsLocally>
<PrebuiltWasmToolsVersion>1.0.51</PrebuiltWasmToolsVersion>
<PrebuiltWasmToolsBaseUrl>https://github.com/bytecodealliance/wasm-tools/releases/download/wasm-tools-$(PrebuiltWasmToolsVersion)/wasm-tools-$(PrebuiltWasmToolsVersion)</PrebuiltWasmToolsBaseUrl>
<WasmtimeVersionForWasiSnapshotPreview1Adapters>14.0.4</WasmtimeVersionForWasiSnapshotPreview1Adapters>
<BuildWasmToolsLocally>false</BuildWasmToolsLocally>
<PrebuiltWasmToolsVersion>1.209.1</PrebuiltWasmToolsVersion>
<PrebuiltWasmToolsBaseUrl>https://github.com/bytecodealliance/wasm-tools/releases/download/v$(PrebuiltWasmToolsVersion)/wasm-tools-$(PrebuiltWasmToolsVersion)</PrebuiltWasmToolsBaseUrl>
<WasmtimeVersionForWasiSnapshotPreview1Adapters>21.0.1</WasmtimeVersionForWasiSnapshotPreview1Adapters>

<WasmToolsModuleRoot>$(MSBuildThisFileDirectory)..\..\modules\wasm-tools\</WasmToolsModuleRoot>

Expand Down Expand Up @@ -68,13 +68,10 @@
<RemoveDir Directories="tools\temp" />
</Target>

<Target Name="DownloadWasiPreview1Adapters" Condition="!Exists('tools\wasi-wasm\lastbuild.txt')">
<Target Name="DownloadWasiPreview1Adapters" Inputs="$(MSBuildThisFileDirectory)tools\wasi-wasm\version" Outputs="$(MSBuildThisFileDirectory)tools\wasi-wasm\version" >
<DownloadFile SourceUrl="https://github.com/bytecodealliance/wasmtime/releases/download/v$(WasmtimeVersionForWasiSnapshotPreview1Adapters)/wasi_snapshot_preview1.command.wasm" DestinationFolder="tools\wasi-wasm" />
<DownloadFile SourceUrl="https://github.com/bytecodealliance/wasmtime/releases/download/v$(WasmtimeVersionForWasiSnapshotPreview1Adapters)/wasi_snapshot_preview1.reactor.wasm" DestinationFolder="tools\wasi-wasm" />
<WriteLinesToFile File="tools\wasi-wasm\lastbuild.txt" Lines="" Overwrite="true" />
<ItemGroup>
<FileWrites Include="tools\wasi-wasm\lastbuild.txt" />
</ItemGroup>
<WriteLinesToFile File="$(MSBuildThisFileDirectory)tools\wasi-wasm\version" Lines="$(WasmtimeVersionForWasiSnapshotPreview1Adapters)" Overwrite="true" WriteOnlyWhenDifferent="true" />
</Target>

<Target Name="PackTaskDependencies" BeforeTargets="GenerateNuspec">
Expand Down
85 changes: 5 additions & 80 deletions src/WasmComponent.Sdk/build/WasmComponent.Sdk.targets
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
<Project>
<!--
MSBuild stuff to acquire the necessary SDKs (WASI SDK and Emscripten) automatically. It will take a few mins on the
first build on a given machine, but after that should no-op.
-->

<PropertyGroup>
<!-- Keep this block all in sync manually, since URLs can be arbitrary -->
<WasiSdkVersion>20.0</WasiSdkVersion>
<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>
<WasiSdkUrl Condition="$([MSBuild]::IsOSPlatform('Linux'))">https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz</WasiSdkUrl>
<WasiSdkUrl Condition="$([MSBuild]::IsOSPlatform('OSX'))">https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-macos.tar.gz</WasiSdkUrl>
<WasiSdkRoot>$([System.IO.Path]::Combine("$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))", ".wasi-sdk", "wasi-sdk-$(WasiSdkVersion)"))</WasiSdkRoot>

<EmSdkVersion>3.1.23</EmSdkVersion>
<EmSdkUrl>https://github.com/emscripten-core/emsdk/archive/refs/tags/$(EmSdkVersion).zip</EmSdkUrl>
<!-- Support bring your own emscripten if $(EMSDK) is already set-->
<EmscriptenRoot Condition="'$(EMSDK)' == ''">$([System.IO.Path]::Combine("$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))", ".emsdk", "emsdk-$(EmSdkVersion)"))</EmscriptenRoot>
<EmscriptenRoot Condition="'$(EMSDK)' != ''">$(EMSDK)</EmscriptenRoot>
</PropertyGroup>

<Target Name="EmitWasmOnBuild" AfterTargets="CopyFilesToOutputDirectory" DependsOnTargets="LinkNativeLlvm; ConvertToWasmComponent"
Condition="'$(RuntimeIdentifier)' == 'wasi-wasm'">
<Message Importance="high" Text="$(ProjectName) -> $([System.IO.Path]::GetFullPath('$(NativeComponentBinary)'))" />
Expand All @@ -33,66 +13,11 @@
<WasiPreview1AdapterType>reactor</WasiPreview1AdapterType>
<WasiPreview1AdapterType Condition="'$(OutputType.ToLower())' == 'exe'">command</WasiPreview1AdapterType>
<WasiPreview1AdapterPath>$(MSBuildThisFileDirectory)../tools/wasi-wasm/wasi_snapshot_preview1.$(WasiPreview1AdapterType).wasm</WasiPreview1AdapterPath>
<NativeComponentBinary>$(NativeOutputPath)$(TargetName).component.wasm</NativeComponentBinary>
</PropertyGroup>
</Target>

<Target Name="PrepareWasmSdks" BeforeTargets="CheckWasmSdks" DependsOnTargets="ObtainWasiSdk; ObtainEmscripten">
<PropertyGroup>
<EmSdk>$(EmscriptenRoot)</EmSdk>
<WASI_SDK_PATH>$(WasiSdkRoot)</WASI_SDK_PATH>
</PropertyGroup>
</Target>

<Target Name="ObtainEmscripten" Condition="'$(EMSDK)' == '' AND !(Exists($(EmscriptenRoot)))">
<!--
This is not ideal because if your solution has multiple projects that use WasmComponent.Sdk, then if you
build in parallel in CI where your machine doesn't already have wasi-sdk/emsdk, then it may try to download
and extract the SDKs multiple times in parallel to the same disk location, which may cause it to fail.
The only reason this doesn't happen in this repo is that it explicitly runs the PrepareWasmSdks task before
building other projects.

For a proper fix, consider implementing an MSBuild task in C# that obtains wasi-sdk/emsdk, and uses a mutex
so that only one flow executes at a time, with others blocking until it's done.
-->

<PropertyGroup>
<EmSdkDownloadTempDir>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), $([System.IO.Path]::GetRandomFileName())))</EmSdkDownloadTempDir>
<!--
wasm compose requires kabab case (todo: revisit when move to wac https://github.com/bytecodealliance/componentize-dotnet/issues/5)
https://github.com/bytecodealliance/wasm-tools/issues/1440
-->
<NativeComponentBinary>$(NativeOutputPath)$(TargetName.ToLower())-component.wasm</NativeComponentBinary>
</PropertyGroup>

<MakeDir Directories="$(EmSdkDownloadTempDir)" />
<DownloadFile
SourceUrl="$(EmSdkUrl)"
DestinationFolder="$(EmSdkDownloadTempDir)">
<Output TaskParameter="DownloadedFile" ItemName="EmSdkDownloadTempFile" />
</DownloadFile>

<!-- Windows 10+ has tar built in, so this should work cross-platform -->
<Message Importance="high" Text="Extracting @(EmSdkDownloadTempFile) to $(EmscriptenRoot)..." />
<MakeDir Directories="$(EmscriptenRoot)" />
<Exec Command="tar -xf &quot;@(EmSdkDownloadTempFile)&quot; -C . --strip-components=1" WorkingDirectory="$(EmscriptenRoot)" />
<RemoveDir Directories="$(EmSdkDownloadTempDir)" />

<Exec Command="emsdk install $(EmSdkVersion)" WorkingDirectory="$(EmscriptenRoot)" />
<Exec Command="emsdk activate $(EmSdkVersion)" WorkingDirectory="$(EmscriptenRoot)" />
</Target>

<Target Name="ObtainWasiSdk" Condition="!(Exists($(WasiSdkRoot)))">
<PropertyGroup>
<WasiSdkDownloadTempDir>$([System.IO.Path]::Combine($([System.IO.Path]::GetTempPath()), $([System.IO.Path]::GetRandomFileName())))</WasiSdkDownloadTempDir>
</PropertyGroup>

<MakeDir Directories="$(WasiSdkDownloadTempDir)" />
<DownloadFile
SourceUrl="$(WasiSdkUrl)"
DestinationFolder="$(WasiSdkDownloadTempDir)">
<Output TaskParameter="DownloadedFile" ItemName="WasiSdkDownloadTempFile" />
</DownloadFile>

<!-- Windows 10+ has tar built in, so this should work cross-platform -->
<Message Importance="high" Text="Extracting @(WasiSdkDownloadTempFile) to $(WasiSdkRoot)..." />
<MakeDir Directories="$(WasiSdkRoot)" />
<Exec Command="tar -xf &quot;@(WasiSdkDownloadTempFile)&quot; -C . --strip-components=1" WorkingDirectory="$(WasiSdkRoot)" />
<RemoveDir Directories="$(WasiSdkDownloadTempDir)" />
</Target>
</Project>
14 changes: 9 additions & 5 deletions src/WitBindgen/WitBindgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

<!-- Things you might want to edit -->
<!-- Set BuildWitBindgenLocally to true if you want to build modules/wit-bindgen locally and use its output -->
<BuildWitBindgenLocally>true</BuildWitBindgenLocally>
<PrebuiltWitBindgenVersion>0.14.0</PrebuiltWitBindgenVersion>
<PrebuiltWitBindgenBaseUrl>https://github.com/bytecodealliance/wit-bindgen/releases/download/wit-bindgen-cli-$(PrebuiltWitBindgenVersion)/wit-bindgen-v$(PrebuiltWitBindgenVersion)</PrebuiltWitBindgenBaseUrl>

<BuildWitBindgenLocally>false</BuildWitBindgenLocally>
<PrebuiltWitBindgenVersion>0.26.0</PrebuiltWitBindgenVersion>
<PrebuiltWitBindgenBaseUrl>https://github.com/bytecodealliance/wit-bindgen/releases/download/v$(PrebuiltWitBindgenVersion)/wit-bindgen-$(PrebuiltWitBindgenVersion)</PrebuiltWitBindgenBaseUrl>
<WitBindgenModuleRoot>$(MSBuildThisFileDirectory)..\..\modules\wit-bindgen\</WitBindgenModuleRoot>

<!-- This is a marker file that lets the build scripts identity if the files need to be modified when updating versions -->
<CurrentWitBindgenVersion>$(MSBuildThisFileDirectory)tools\version-$(PrebuiltWitBindgenVersion)</CurrentWitBindgenVersion>

<!-- Don't pack any assemblies in lib/*/.dll.-->
<NoPackageAnalysis>true</NoPackageAnalysis>
<IncludeBuildOutput>false</IncludeBuildOutput>
Expand Down Expand Up @@ -50,8 +52,10 @@
</ItemGroup>
</Target>

<Target Name="DownloadNativeToolingCore" Inputs="@(PrebuiltWitBindgenOutputs)" Outputs="@(PrebuiltWitBindgenOutputs)">
<Target Name="DownloadNativeToolingCore" Condition="!Exists('$(CurrentWitBindgenVersion)')" Inputs="@(PrebuiltWitBindgenOutputs);$(CurrentWitBindgenVersion)" Outputs="@(PrebuiltWitBindgenOutputs);$(CurrentWitBindgenVersion)">
<RemoveDir Directories="$(MSBuildThisFileDirectory)tools" />
<DownloadFile SourceUrl="$(PrebuiltWitBindgenBaseUrl)-%(PrebuiltWitBindgenToolTarget.Identity)%(PrebuiltWitBindgenToolTarget.Ext)" DestinationFolder="tools\temp" DestinationFileName="%(PrebuiltWitBindgenToolTarget.Rid)%(PrebuiltWitBindgenToolTarget.Ext)" />
<WriteLinesToFile File="$(CurrentWitBindgenVersion)" Lines="$(PrebuiltWitBindgenVersion)" Overwrite="true" WriteOnlyWhenDifferent="true" />
<MakeDir Directories="tools\%(PrebuiltWitBindgenToolTarget.Rid)" />
<Exec Command="tar -xf &quot;temp/%(PrebuiltWitBindgenToolTarget.Rid)%(PrebuiltWitBindgenToolTarget.Ext)&quot; -C %(PrebuiltWitBindgenToolTarget.Rid) --strip-components=1" WorkingDirectory="tools" />
<RemoveDir Directories="tools\temp" />
Expand Down
Loading