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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ By default the project will find all wit files and execute wit-bindgen against e
</ItemGroup>
```

### Configuring location of generaged wit files

By default the wit files are generated under the itermediate path `$(IntermediateOutputPath)wit_bindgen` which is by default at a location like `\obj\Debug\net9.0\wit_bindgen`. This means you can't really see the files and they will be ignored and not checked in to source by default (assuming you have a standard .net `.gitignore`) but intillisense will work.

If you would like to have the files in a more discoverable place and potentially check the source files in you can configure the output location with a folder name and location of your choice, for example the following will create a folder `genererated/wit/` at the root of your project and put all the files in there.

```xml
<PropertyGroup>
<WitGeneratedFilesRoot>generated/wit</WitGeneratedFilesRoot>
</PropertyGroup>
```

### Passing additional wit-bindgen args

[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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
Condition="'$(Language)' == 'C#' AND '@(Wit)' != ''"
DependsOnTargets="PrepareWasmSdks; WitCompile_GetDependencies; WitCompile_InvokeTool">
<ItemGroup>
<Compile Include="$(WitGeneratedFilesRoot)**\*.cs" />
<Compile Include="$(WitGeneratedFilesRoot)**\*.cs" KeepDuplicates="false" />
</ItemGroup>
</Target>

Expand All @@ -75,6 +75,7 @@
<Target Name="WitCompile_InvokeTool" Inputs="@(Wit);$(MSBuildProjectFile)" Outputs="@(WitGeneratedCsFiles);$(WitGeneratedFilesRoot)lastbuild.txt">
<PropertyGroup>
<WitGeneratedFilesRoot Condition="'$(WitGeneratedFilesRoot)' == ''">$(IntermediateOutputPath)wit_bindgen\</WitGeneratedFilesRoot>
<WitGeneratedFilesRoot Condition="'$(WitGeneratedFilesRoot)' != '' AND !$(WitGeneratedFilesRoot.EndsWith('\'))">$(WitGeneratedFilesRoot)\</WitGeneratedFilesRoot>
</PropertyGroup>
<ItemGroup>
<WitGeneratedCsFiles Remove="@(WitGeneratedCsFiles)" />
Expand Down
11 changes: 6 additions & 5 deletions test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using MyFuncsWorld;
using ProducerWorld;
using MyResultsWorld;
using MyWitResultsWorld;
using System;
using Xunit;

namespace WitBindgenTest;
Expand Down Expand Up @@ -47,4 +42,10 @@ public void ShouldBeNormalWitResult()
{
Assert.NotNull((Func<float, MyWitResultsWorld.Result<float, string>>)MyWitResultsWorldImpl.StringError);
}

[Fact]
public void CanChangeWitouputLocation()
{
Assert.NotNull((Func<int>)MySimpleWorldImpl.GetNumber);
}
}
1 change: 1 addition & 0 deletions test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ProjectReference Include="..\testapps\LibraryUsingWit\LibraryUsingWit.csproj" />
<ProjectReference Include="..\testapps\LibraryWithExceptions\LibraryWitExceptions.csproj" />
<ProjectReference Include="..\testapps\LibraryWithWitResultType\LibraryWithWitResultType.csproj" />
<ProjectReference Include="..\testapps\LibraryWithCustomGeneratedFilesLocation\LibraryWithCustomGeneratedFilesLocation.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MySimpleWorld;

public class MySimpleWorldImpl : IMySimpleWorld
{
public static int GetNumber()
{
return 123;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Only needed when referencing the dependencies as projects. For package references, these are imported automatically. -->
<Import Project="..\..\..\..\src\WitBindgen\ImportInDev.proj" />

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WitGeneratedFilesRoot>generated/wit</WitGeneratedFilesRoot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\WitBindgen\WitBindgen.csproj" />
<PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM"/>
</ItemGroup>

<ItemGroup>
<Wit Update="result.wit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Generated by `wit-bindgen` 0.40.0. DO NOT EDIT!
// <auto-generated />
#nullable enable
using System.Runtime.InteropServices;
namespace MySimpleWorld {

public interface IMySimpleWorld {
static abstract int GetNumber();

}

public readonly struct None {}

[StructLayout(LayoutKind.Sequential)]
public readonly struct Result<TOk, TErr>
{
public readonly byte Tag;
private readonly object value;

private Result(byte tag, object value)
{
Tag = tag;
this.value = value;
}

public static Result<TOk, TErr> Ok(TOk ok)
{
return new Result<TOk, TErr>(Tags.Ok, ok!);
}

public static Result<TOk, TErr> Err(TErr err)
{
return new Result<TOk, TErr>(Tags.Err, err!);
}

public bool IsOk => Tag == Tags.Ok;
public bool IsErr => Tag == Tags.Err;

public TOk AsOk
{
get
{
if (Tag == Tags.Ok)
{
return (TOk)value;
}

throw new ArgumentException("expected k, got " + Tag);
}
}

public TErr AsErr
{
get
{
if (Tag == Tags.Err)
{
return (TErr)value;
}

throw new ArgumentException("expected Err, got " + Tag);
}
}

public class Tags
{
public const byte Ok = 0;
public const byte Err = 1;
}
}

namespace exports {
using System.Runtime.InteropServices;
public static class MySimpleWorld
{

[UnmanagedCallersOnly(EntryPoint = "get-number")]
public static unsafe int wasmExportGetNumber() {

int ret;
ret = MySimpleWorldImpl.GetNumber();
return ret;

}

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test:simple;

world my-simple {
export get-number: func() -> s32;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated by `wit-bindgen` 0.40.0. DO NOT EDIT!
// <auto-generated />
#nullable enable

#if !NET9_0_OR_GREATER
// temporarily add this attribute until it is available in dotnet 9
namespace System.Runtime.InteropServices
{
internal partial class WasmImportLinkageAttribute : Attribute {}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test:simple;

world my-simple {
export get-number: func() -> s32;
}