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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin/
obj/
.vs/
artifacts/
test/WasmComponentSdkTest/testapps/OciWit/wit
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ By default the project will find all wit files and execute wit-bindgen against e
</ItemGroup>
```

### Referencing Wit Packages from OCI Registries
Wit can be packaged into [OCI Artifacts](https://tag-runtime.cncf.io/wgs/wasm/deliverables/wasm-oci-artifact/) which can be pushed to OCI registries. To import a WIT definition from an OCI registry specify the Registry on the `Wit` Element. This will pull a WASM component binary that contains the WIT definition. wit-bindgen can use the binary format directly to generate the bindings. To view the WIT directly use [`wasm-tools component wit wit/wit.wasm`](https://github.com/bytecodealliance/wasm-tools).

```xml
<ItemGroup>
<Wit Remove="**\*.wit" />
<Wit Include="wit/wit.wasm" World="command" Registry="ghcr.io/webassembly/wasi/cli:0.2.0" />
</ItemGroup>
```


### WIT strings and memory

The calculator example above works easily because it doesn't need to allocate memory dynamically. Once you start working with strings, you must add an extra line to the `<PropertyGroup>` in your _host_ `.csproj` file (that is, the application that's _importing_ the interface):
Expand Down
30 changes: 19 additions & 11 deletions src/WitBindgen/WitBindgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<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>
<CurrentWitBindgenVersion>$(MSBuildThisFileDirectory)tools\version-wit-bindgen-$(PrebuiltWitBindgenVersion)</CurrentWitBindgenVersion>

<PrebuildWkgVersion>dev</PrebuildWkgVersion>
<PrebuildWkgBaseUrl>https://github.com/bytecodealliance/wasm-pkg-tools/releases/download/$(PrebuildWkgVersion)/wkg</PrebuildWkgBaseUrl>
<CurrentWkgVersion>$(MSBuildThisFileDirectory)tools\version-wkg-$(PrebuildWkgVersion)</CurrentWkgVersion>

<!-- Don't pack any assemblies in lib/*/.dll.-->
<NoPackageAnalysis>true</NoPackageAnalysis>
Expand Down Expand Up @@ -54,24 +58,28 @@

<Target Name="GetDownloadNativeToolingDependencies">
<ItemGroup>
<PrebuiltWitBindgenToolTarget Include="aarch64-linux" Rid="linux-arm64" Ext=".tar.gz" />
<PrebuiltWitBindgenToolTarget Include="aarch64-macos" Rid="osx-arm64" Ext=".tar.gz" />
<PrebuiltWitBindgenToolTarget Include="x86_64-linux" Rid="linux-x64" Ext=".tar.gz" />
<PrebuiltWitBindgenToolTarget Include="x86_64-macos" Rid="osx-x64" Ext=".tar.gz" />
<PrebuiltWitBindgenToolTarget Include="x86_64-windows" Rid="win-x64" Ext=".zip" ExeExt=".exe" />
<PrebuiltToolTarget Include="aarch64-linux" Rid="linux-arm64" Ext=".tar.gz" wkg="aarch64-unknown-linux-gnu" />
<PrebuiltToolTarget Include="aarch64-macos" Rid="osx-arm64" Ext=".tar.gz" wkg="aarch64-apple-darwin"/>
<PrebuiltToolTarget Include="x86_64-linux" Rid="linux-x64" Ext=".tar.gz" wkg="x86_64-unknown-linux-gnu"/>
<PrebuiltToolTarget Include="x86_64-macos" Rid="osx-x64" Ext=".tar.gz" wkg="x86_64-apple-darwin"/>
<PrebuiltToolTarget Include="x86_64-windows" Rid="win-x64" Ext=".zip" ExeExt=".exe" wkg="x86_64-pc-windows-gnu" />
</ItemGroup>
<ItemGroup>
<PrebuiltWitBindgenOutputs Include="tools\%(PrebuiltWitBindgenToolTarget.Rid)\wit-bindgen%(PrebuiltWitBindgenToolTarget.ExeExt)" />
<PrebuiltWitBindgenOutputs Include="tools\%(PrebuiltToolTarget.Rid)\wit-bindgen%(PrebuiltToolTarget.ExeExt)" />
<PrebuiltWkgOutputs Include="tools\%(PrebuiltToolTarget.Rid)\wkg%(PrebuiltToolTarget.ExeExt)" />
</ItemGroup>
</Target>

<Target Name="DownloadNativeToolingCore" Condition="!Exists('$(CurrentWitBindgenVersion)')" Inputs="@(PrebuiltWitBindgenOutputs);$(CurrentWitBindgenVersion)" Outputs="@(PrebuiltWitBindgenOutputs);$(CurrentWitBindgenVersion)">
<Target Name="DownloadNativeToolingCore" Condition="!Exists('$(CurrentWitBindgenVersion)') Or !Exists('$(CurrentWkgVersion)')" Inputs="@(PrebuiltWitBindgenOutputs);$(CurrentWitBindgenVersion);$(CurrentWkgVersion)" Outputs="@(PrebuiltWitBindgenOutputs);$(CurrentWitBindgenVersion);$(CurrentWkgVersion)">
<RemoveDir Directories="$(MSBuildThisFileDirectory)tools" />
<DownloadFile SourceUrl="$(PrebuiltWitBindgenBaseUrl)-%(PrebuiltWitBindgenToolTarget.Identity)%(PrebuiltWitBindgenToolTarget.Ext)" DestinationFolder="tools\temp" DestinationFileName="%(PrebuiltWitBindgenToolTarget.Rid)%(PrebuiltWitBindgenToolTarget.Ext)" />
<MakeDir Directories="tools\%(PrebuiltToolTarget.Rid)" />
<DownloadFile SourceUrl="$(PrebuiltWitBindgenBaseUrl)-%(PrebuiltToolTarget.Identity)%(PrebuiltToolTarget.Ext)" DestinationFolder="tools\temp" DestinationFileName="%(PrebuiltToolTarget.Rid)%(PrebuiltToolTarget.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" />
<Exec Command="tar -xf &quot;temp/%(PrebuiltToolTarget.Rid)%(PrebuiltToolTarget.Ext)&quot; -C %(PrebuiltToolTarget.Rid) --strip-components=1" WorkingDirectory="tools" />
<RemoveDir Directories="tools\temp" />

<DownloadFile SourceUrl="$(PrebuildWkgBaseUrl)-%(PrebuiltToolTarget.wkg)" DestinationFolder="tools\%(PrebuiltToolTarget.Rid)" DestinationFileName="wkg%(PrebuiltToolTarget.ExeExt)" />
<WriteLinesToFile File="$(CurrentWkgVersion)" Lines="$(PrebuildWkgVersion)" Overwrite="true" WriteOnlyWhenDifferent="true" />
</Target>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<WitBindgenToolTarget>$(WitBindgenToolTarget)-$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLower())</WitBindgenToolTarget>
<WitBindgenExe>$(MSBuildThisFileDirectory)..\tools\$(WitBindgenToolTarget)\wit-bindgen</WitBindgenExe>
<WitBindgenExe Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(WitBindgenExe).exe</WitBindgenExe>
<WkgExe>$(MSBuildThisFileDirectory)..\tools\$(WitBindgenToolTarget)\wkg</WkgExe>
<WkgExe Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(WkgExe).exe</WkgExe>

<!-- We have to add this to the application's project because the codegen uses unsafe in marshalling -->
<!-- An alternative would be to emit code that uses some kind of Wit.Runtime project that deals with the unsafe aspects -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<Project>
<PropertyGroup>
<WitBindgenRuntime>native-aot</WitBindgenRuntime>

Expand Down Expand Up @@ -55,6 +55,15 @@
</ItemGroup>
</Target>

<Target Name="PullWitOciFiles" Inputs="@(Wit)" Outputs="@(Wit)" BeforeTargets="WitCompile_GetDependencies">
<PropertyGroup>
<WitOciFile>@(Wit)</WitOciFile>
</PropertyGroup>
<Message Condition="'%(Wit.Registry)' != ''" Importance="high" Text="Pulling OCI %(Wit.Registry) to %(Wit.RelativeDir)..." />
<MakeDir Condition="'%(Wit.Registry)' != '' AND !Exists('%(Wit.RelativeDir)')" Directories="%(Wit.RelativeDir)" />
<Exec Condition="'%(Wit.Registry)' != ''" Command="$(WkgExe) oci pull %(Wit.Registry) --output $(WitOciFile)" />
</Target>

<Target Name="WitCompile_GetDependencies">
<PropertyGroup>
<WitGeneratedFilesRoot Condition="'$(WitGeneratedFilesRoot)' == ''">$(IntermediateOutputPath)wit_bindgen\</WitGeneratedFilesRoot>
Expand Down Expand Up @@ -82,8 +91,8 @@
<!-- Need to compile cabi from c https://github.com/bytecodealliance/wit-bindgen/pull/791-->
<ItemGroup>
<CabiReAllocFiles Include="$(WitGeneratedFilesRoot)**\*World_cabi_realloc.c" />
</ItemGroup>
<Message Importance="high" Text="building cabi_realloc files... @(CabiReAllocFiles->'&quot;%(FullPath)&quot;', ' ') with $(Wasicompiler)" />
</ItemGroup>
<Message Importance="high" Text="building cabi_realloc files... @(CabiReAllocFiles->'&quot;%(FullPath)&quot;', ' ') with $(Wasicompiler)" />
<Exec WorkingDirectory="$(WitGeneratedFilesRoot)" Command="&quot;$(Wasicompiler)&quot; @(CabiReAllocFiles->'&quot;%(FullPath)&quot;', ' ') -c"/>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions src/WitBindgen/build/Wit.CSharp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@
PersistenceStyle="Attribute" HasConfigurationCondition="false" />
</StringProperty.DataSource>
</StringProperty>

<StringProperty Name="Registry" DisplayName="Registry" Category="Wit" Description="Selects the package to pull from an OCI registry">
<StringProperty.DataSource>
<DataSource ItemType="Wit" SourceOfDefaultValue="AfterContext"
PersistenceStyle="Attribute" HasConfigurationCondition="false" />
</StringProperty.DataSource>
</StringProperty>
</Rule>
</ProjectSchemaDefinitions>
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public void CanComposeImportWithExport()
Assert.Contains("123 + 456 = 579", stdout);
}

[Fact]
public void CanBuildAppFromOci()
{
var composed = FindModulePath("../testapps/OciWit", "ociwit-component.wasm");
var stdout = ExecuteCommandComponent(composed);
Assert.StartsWith("Oci is awesome!", stdout);
}

private static string ExecuteCommandComponent(string componentFilePath)
{
var startInfo = new ProcessStartInfo(WasmtimeExePath, $"-W component-model {componentFilePath}") { RedirectStandardOutput = true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<ItemGroup>
<ProjectReference Include="..\testapps\SimpleConsumer\SimpleConsumer.csproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\testapps\AppWithWitFolder\AppWithWitFolder.csproj" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\testapps\OciWit\OciWit.csproj" ReferenceOutputAssembly="false"/>
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions test/WasmComponentSdkTest/testapps/OciWit/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CommandWorld.wit.exports.wasi.cli.v0_2_0;

public class RunImpl : IRun
{
public static void Run()
{
Console.WriteLine("Oci is awesome!");
}
}
23 changes: 23 additions & 0 deletions test/WasmComponentSdkTest/testapps/OciWit/OciWit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
<Import Project="..\..\..\..\src\WasmComponent.Sdk\ImportInDev.proj" />

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<InvariantGlobalization>true</InvariantGlobalization>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\WasmComponent.Sdk\WasmComponent.Sdk.csproj" />
</ItemGroup>

<ItemGroup>
<Wit Remove="**\*.wit" />
<Wit Include="wit/wit.wasm" World="command" Registry="ghcr.io/webassembly/wasi/cli:0.2.0" />
</ItemGroup>

</Project>