Skip to content

Commit 4315c0c

Browse files
authored
Merge pull request #72 from jsturtevant/wit-files
Update docs for generated files and make expereince a bit smoother
2 parents bbf0c6b + 81dd50b commit 4315c0c

12 files changed

+161
-6
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ By default the project will find all wit files and execute wit-bindgen against e
232232
</ItemGroup>
233233
```
234234

235+
### Configuring location of generaged wit files
236+
237+
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.
238+
239+
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.
240+
241+
```xml
242+
<PropertyGroup>
243+
<WitGeneratedFilesRoot>generated/wit</WitGeneratedFilesRoot>
244+
</PropertyGroup>
245+
```
246+
235247
### Passing additional wit-bindgen args
236248

237249
[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:

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
Condition="'$(Language)' == 'C#' AND '@(Wit)' != ''"
4949
DependsOnTargets="PrepareWasmSdks; WitCompile_GetDependencies; WitCompile_InvokeTool">
5050
<ItemGroup>
51-
<Compile Include="$(WitGeneratedFilesRoot)**\*.cs" />
51+
<Compile Include="$(WitGeneratedFilesRoot)**\*.cs" KeepDuplicates="false" />
5252
</ItemGroup>
5353
</Target>
5454

@@ -75,6 +75,7 @@
7575
<Target Name="WitCompile_InvokeTool" Inputs="@(Wit);$(MSBuildProjectFile)" Outputs="@(WitGeneratedCsFiles);$(WitGeneratedFilesRoot)lastbuild.txt">
7676
<PropertyGroup>
7777
<WitGeneratedFilesRoot Condition="'$(WitGeneratedFilesRoot)' == ''">$(IntermediateOutputPath)wit_bindgen\</WitGeneratedFilesRoot>
78+
<WitGeneratedFilesRoot Condition="'$(WitGeneratedFilesRoot)' != '' AND !$(WitGeneratedFilesRoot.EndsWith('\'))">$(WitGeneratedFilesRoot)\</WitGeneratedFilesRoot>
7879
</PropertyGroup>
7980
<ItemGroup>
8081
<WitGeneratedCsFiles Remove="@(WitGeneratedCsFiles)" />

test/WitBindgenTest/WitBindgenTest/CodeGenerationTest.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
using MyFuncsWorld;
2-
using ProducerWorld;
3-
using MyResultsWorld;
4-
using MyWitResultsWorld;
5-
using System;
61
using Xunit;
72

83
namespace WitBindgenTest;
@@ -47,4 +42,10 @@ public void ShouldBeNormalWitResult()
4742
{
4843
Assert.NotNull((Func<float, MyWitResultsWorld.Result<float, string>>)MyWitResultsWorldImpl.StringError);
4944
}
45+
46+
[Fact]
47+
public void CanChangeWitouputLocation()
48+
{
49+
Assert.NotNull((Func<int>)MySimpleWorldImpl.GetNumber);
50+
}
5051
}

test/WitBindgenTest/WitBindgenTest/WitBindgenTest.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<ProjectReference Include="..\testapps\LibraryUsingWit\LibraryUsingWit.csproj" />
2323
<ProjectReference Include="..\testapps\LibraryWithExceptions\LibraryWitExceptions.csproj" />
2424
<ProjectReference Include="..\testapps\LibraryWithWitResultType\LibraryWithWitResultType.csproj" />
25+
<ProjectReference Include="..\testapps\LibraryWithCustomGeneratedFilesLocation\LibraryWithCustomGeneratedFilesLocation.csproj" />
2526
</ItemGroup>
2627

2728
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using MySimpleWorld;
2+
3+
public class MySimpleWorldImpl : IMySimpleWorld
4+
{
5+
public static int GetNumber()
6+
{
7+
return 123;
8+
}
9+
}
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+
<WitGeneratedFilesRoot>generated/wit</WitGeneratedFilesRoot>
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Generated by `wit-bindgen` 0.40.0. DO NOT EDIT!
2+
// <auto-generated />
3+
#nullable enable
4+
using System.Runtime.InteropServices;
5+
namespace MySimpleWorld {
6+
7+
public interface IMySimpleWorld {
8+
static abstract int GetNumber();
9+
10+
}
11+
12+
public readonly struct None {}
13+
14+
[StructLayout(LayoutKind.Sequential)]
15+
public readonly struct Result<TOk, TErr>
16+
{
17+
public readonly byte Tag;
18+
private readonly object value;
19+
20+
private Result(byte tag, object value)
21+
{
22+
Tag = tag;
23+
this.value = value;
24+
}
25+
26+
public static Result<TOk, TErr> Ok(TOk ok)
27+
{
28+
return new Result<TOk, TErr>(Tags.Ok, ok!);
29+
}
30+
31+
public static Result<TOk, TErr> Err(TErr err)
32+
{
33+
return new Result<TOk, TErr>(Tags.Err, err!);
34+
}
35+
36+
public bool IsOk => Tag == Tags.Ok;
37+
public bool IsErr => Tag == Tags.Err;
38+
39+
public TOk AsOk
40+
{
41+
get
42+
{
43+
if (Tag == Tags.Ok)
44+
{
45+
return (TOk)value;
46+
}
47+
48+
throw new ArgumentException("expected k, got " + Tag);
49+
}
50+
}
51+
52+
public TErr AsErr
53+
{
54+
get
55+
{
56+
if (Tag == Tags.Err)
57+
{
58+
return (TErr)value;
59+
}
60+
61+
throw new ArgumentException("expected Err, got " + Tag);
62+
}
63+
}
64+
65+
public class Tags
66+
{
67+
public const byte Ok = 0;
68+
public const byte Err = 1;
69+
}
70+
}
71+
72+
namespace exports {
73+
using System.Runtime.InteropServices;
74+
public static class MySimpleWorld
75+
{
76+
77+
[UnmanagedCallersOnly(EntryPoint = "get-number")]
78+
public static unsafe int wasmExportGetNumber() {
79+
80+
int ret;
81+
ret = MySimpleWorldImpl.GetNumber();
82+
return ret;
83+
84+
}
85+
86+
}
87+
}
88+
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:simple;
2+
3+
world my-simple {
4+
export get-number: func() -> s32;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated by `wit-bindgen` 0.40.0. DO NOT EDIT!
2+
// <auto-generated />
3+
#nullable enable
4+
5+
#if !NET9_0_OR_GREATER
6+
// temporarily add this attribute until it is available in dotnet 9
7+
namespace System.Runtime.InteropServices
8+
{
9+
internal partial class WasmImportLinkageAttribute : Attribute {}
10+
}
11+
#endif

test/WitBindgenTest/testapps/LibraryWithCustomGeneratedFilesLocation/generated/wit/lastbuild.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test:simple;
2+
3+
world my-simple {
4+
export get-number: func() -> s32;
5+
}

test/WitBindgenTest/testapps/LibraryWithCustomGeneratedFilesLocation/wit-bindgen-generatedlastbuild.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)