Skip to content

Commit bbf0c6b

Browse files
authored
Merge pull request #71 from jsturtevant/wit-bindgen-additional-args
Provide way to pass wit-bindgen arguments
2 parents 8058b36 + dcf92b5 commit bbf0c6b

File tree

10 files changed

+110
-1
lines changed

10 files changed

+110
-1
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,26 @@ By default the project will find all wit files and execute wit-bindgen against e
232232
</ItemGroup>
233233
```
234234

235+
### Passing additional wit-bindgen args
236+
237+
[wit-bindgen](https://github.com/bytecodealliance/wit-bindgen/tree/main) for c# has some advanced settings that can be set by using `WitBindgenAddtionalArgs` property. A non-exhustive list of example args that might be useful are:
238+
239+
- `--features <featurename>` - turn on [wit features](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#feature-gates)
240+
- `--with-wit-results` - use [wit Result types](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md#wit-types) instead of generating exceptions
241+
- `--skip-support-files` - don't output files like `WasmImportLinkageAttribute`
242+
243+
Example:
244+
245+
```xml
246+
<PropertyGroup>
247+
<WitBindgenAddtionalArgs>--with-wit-results --features tls</WitBindgenAddtionalArgs>
248+
</PropertyGroup>
249+
```
250+
251+
To learn about addtional args run `wit-bindgen c-sharp -h`
252+
235253
### Referencing Wit Packages from OCI Registries
254+
236255
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).
237256

238257
```xml

src/WitBindgen/build/BytecodeAlliance.Componentize.DotNet.WitBindgen.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<WitBindgenRuntime>native-aot</WitBindgenRuntime>
4+
<WitBindgenAddtionalArgs></WitBindgenAddtionalArgs>
45

56
<!-- Keep this block all in sync manually, since URLs can be arbitrary -->
67
<WasiSdkVersion>24.0</WasiSdkVersion>
@@ -86,7 +87,7 @@
8687

8788
<RemoveDir Directories="$(WitGeneratedFilesRoot)" />
8889
<MakeDir Directories="$(WitGeneratedFilesRoot)" />
89-
<Exec Command="$(WitBindgenExe) c-sharp %(Wit.Identity) %(Wit.WitWorldArg) --runtime $(WitBindgenRuntime) --out-dir $(WitGeneratedFilesRoot)" />
90+
<Exec Command="$(WitBindgenExe) c-sharp %(Wit.Identity) %(Wit.WitWorldArg) --runtime $(WitBindgenRuntime) $(WitBindgenAddtionalArgs) --out-dir $(WitGeneratedFilesRoot)" />
9091
<WriteLinesToFile File="$(WitGeneratedFilesRoot)lastbuild.txt" Lines="" Overwrite="true" />
9192

9293
<ItemGroup>

test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using MyFuncsWorld;
22
using ProducerWorld;
3+
using MyResultsWorld;
4+
using MyWitResultsWorld;
35
using System;
46
using Xunit;
57

@@ -33,4 +35,16 @@ public void CanSpecifyWorld()
3335
{
3436
Assert.NotNull((Func<int>)SomeStuffImpl.GetNumber);
3537
}
38+
39+
[Fact]
40+
public void ShouldBeNormalResult()
41+
{
42+
Assert.NotNull((Func<float, float>)MyResultsWorldImpl.StringError);
43+
}
44+
45+
[Fact]
46+
public void ShouldBeNormalWitResult()
47+
{
48+
Assert.NotNull((Func<float, MyWitResultsWorld.Result<float, string>>)MyWitResultsWorldImpl.StringError);
49+
}
3650
}

test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
<ItemGroup>
2222
<ProjectReference Include="..\testapps\LibraryUsingWit\LibraryUsingWit.csproj" />
23+
<ProjectReference Include="..\testapps\LibraryWithExceptions\LibraryWitExceptions.csproj" />
24+
<ProjectReference Include="..\testapps\LibraryWithWitResultType\LibraryWithWitResultType.csproj" />
2325
</ItemGroup>
2426

2527
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
3+
<Import Project="..\..\..\..\src\WitBindgen\ImportInDev.proj" />
4+
5+
<PropertyGroup>
6+
<TargetFramework>net9.0</TargetFramework>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\..\..\src\WitBindgen\WitBindgen.csproj" />
13+
<PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM"/>
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<Wit Update="result.wit" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using MyResultsWorld;
2+
3+
public class MyResultsWorldImpl : IMyResultsWorld
4+
{
5+
public static float StringError(float a)
6+
{
7+
return a;
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:results;
2+
3+
world my-results {
4+
export string-error: func(a: f32) -> result<f32, string>;
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
3+
<Import Project="..\..\..\..\src\WitBindgen\ImportInDev.proj" />
4+
5+
<PropertyGroup>
6+
<TargetFramework>net9.0</TargetFramework>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<WitBindgenAddtionalArgs>--with-wit-results</WitBindgenAddtionalArgs>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\..\..\src\WitBindgen\WitBindgen.csproj" />
14+
<PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM"/>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Wit Update="result.wit" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// I don't think this namespace should be so different to the one in MyFuncsWorldImpl,
2+
// but currently that's what the codegen requires
3+
using MyWitResultsWorld;
4+
5+
6+
7+
public class MyWitResultsWorldImpl : IMyWitResultsWorld
8+
{
9+
public static Result<float, string> StringError(float a)
10+
{
11+
return Result<float, string>.Ok(a);
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:results;
2+
3+
world my-wit-results {
4+
export string-error: func(a: f32) -> result<f32, string>;
5+
}

0 commit comments

Comments
 (0)