SteveSandersonMS / dotnet-wasi-sdk

Packages for building .NET projects as standalone WASI-compliant modules
518 stars 35 forks source link

How does the `native` "magic" works? #2

Closed flavio closed 2 years ago

flavio commented 2 years ago

I was a .NET developer in a previous life, this project got me really excited, kudos!

I'm trying to build the .NET guest glue for waPC, but I'm having some difficulties. The final Wasm module need to expose some functions, this is where I struggle.

Looking at the other code inside of this repository I have the feeling I should write some C code under the native directory, and then export it like doing inside of the Atmo code:

https://github.com/SteveSandersonMS/dotnet-wasi-sdk/blob/66fcdcd1ee60e9bbc2cd752736811cf142962e79/src/Wasi.AspNetCore.Server.Atmo/native/host_interop.c#L70-L71

I tried to mimic what you have done with the build targets, but my .c file seems to be ignored by dotnet build. I deliberately put syntax errors inside of it, but the whole build just works fine.

I was initially doing my experiment under Linux, but then I moved to Windows with Visual Studio 2022. I even tried to use msbuild but nothing changed.

Can you give a high level overview of the steps a .NET noob like me should do to configure everything? 🙏

Thanks a lot!

SteveSandersonMS commented 2 years ago

I tried to mimic what you have done with the build targets, but my .c file seems to be ignored by dotnet build. I deliberately put syntax errors inside of it, but the whole build just works fine.

One way or another, you need to add an entry to the WasiNativeFileReference msbuild itemgroup to have your .c file included in the compilation. In the sample packages I made, I've done it like this: https://github.com/SteveSandersonMS/dotnet-wasi-sdk/blob/main/src/Wasi.AspNetCore.Server.CustomHost/build/Wasi.AspNetCore.Server.CustomHost.targets#L4

Can you give a high level overview of the steps a .NET noob like me should do to configure everything?

I'd have to recommend copy-pasting a full project like Wasi.AspNetCore.Server.CustomHost and then adapting it. That will get you to a fully working state.

SteveSandersonMS commented 2 years ago

Note that longer term it's a goal to avoid having to write any .c, and being able to define the imports/exports directly in C# code.

SteveSandersonMS commented 2 years ago

I tried to mimic what you have done with the build targets, but my .c file seems to be ignored by dotnet build. I deliberately put syntax errors inside of it, but the whole build just works fine.

Just to clarify on this, NuGet is a bit weird about how custom targets work in packages vs projects. You'd probably find that if you published your package and consumed it as a .nupkg it would start working based on the build\* files. However, when consuming it from a <ProjectReference> you have to do some additional steps from the application project to make the .targets and .props get included. Example: https://github.com/SteveSandersonMS/dotnet-wasi-sdk/blob/main/samples/AspNetCoreOnCustomHost/AspNetCoreOnCustomHost.csproj#L20-L23

flavio commented 2 years ago

Thanks, I'll try again!

flavio commented 2 years ago

I managed to get that to work, the issue is that I didn't have the build/<file>.target included inside of my .csproj file :facepalm:

Adding something like:

<Import Project="build\WapcGuest.targets" />

managed to fix it.

This has to be done both by the main library and by the projects that live inside of the same solution.

For the ones interested in seeing that in action, you can find everything here: https://github.com/flavio/wapc-guest-dotnet

@SteveSandersonMS thanks a lot for this amazing project! :clap: :pray: