-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from iuribrindeiro/dotnet-platform-example
Dotnet platform example
- Loading branch information
Showing
12 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ examples/Tuples/main | |
roc_nightly/ | ||
|
||
# macOS directory attributes | ||
.DS_Store | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/expect | ||
|
||
# uncomment line below for debugging | ||
# exp_internal 1 | ||
|
||
set timeout 25 | ||
|
||
cd "./examples/DotNetPlatform/platform/" | ||
|
||
spawn dotnet run | ||
|
||
expect "Hello from .NET!\r\n" { | ||
expect "Hi from roc! (in a .NET platform) 🔥🦅🔥\r\n" { | ||
expect eof | ||
exit 0 | ||
} | ||
} | ||
|
||
puts stderr "\nError: output was different from expected value." | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# publish and debug artifacts | ||
bin | ||
obj | ||
platform/publish | ||
|
||
interop.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# DotNet Platform | ||
|
||
A minimal .NET platform | ||
|
||
## Full Code | ||
|
||
main.roc: | ||
```roc | ||
file:main.roc | ||
``` | ||
|
||
platform/main.roc: | ||
``` | ||
file:platform/main.roc | ||
``` | ||
|
||
platform/DotNetRocPlatform.csproj: | ||
``` | ||
file:platform/DotNetRocPlatform.csproj | ||
``` | ||
|
||
platform/Program.cs: | ||
``` | ||
file:platform/Program.cs | ||
``` | ||
|
||
## Build & Run | ||
|
||
1. Build the roc app that is using the dotnet platform: | ||
|
||
```cli | ||
cd examples/DotNetPlatform/ | ||
roc build main.roc --lib --output ./platform/interop | ||
``` | ||
> _use `arch -arm64` if you are running in a Apple Silicon mac._ | ||
This will produce a shared library file that we'll be able to import from a .NET context. | ||
|
||
To run: | ||
```cli | ||
cd platform | ||
dotnet run | ||
``` | ||
This should print "Hello from .NET". | ||
|
||
|
||
## Build & Run Binary | ||
|
||
If you want to build a binary for the app using native AOT: | ||
|
||
1. Publish the dotnet app | ||
```cli | ||
dotnet publish -c Release | ||
``` | ||
> _use `arch -arm64` if you are running in a Apple Silicon mac._ | ||
2. `cd` into the into the `publish` folder and run the binary: | ||
```cli | ||
./DotNetRocPlatform | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
app "dotnetapp" | ||
packages { platform: "./platform/main.roc" } | ||
imports [] | ||
provides [main] to platform | ||
|
||
main = "Hi from roc! (in a .NET platform) 🔥🦅🔥" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<PublishAot>true</PublishAot> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Update="interop.*"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using static System.Console; | ||
using static Platform; | ||
|
||
NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), CustomResolver); | ||
|
||
WriteLine("Hello from .NET!"); | ||
|
||
MainFromRoc(out var rocStr); | ||
|
||
WriteLine(rocStr); | ||
|
||
//Load native library even when the name doesn't exactly match the name of the library defined in `LibraryImport` | ||
//eg: `interop.so.1.0` instead of `interop.so` | ||
static IntPtr CustomResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) | ||
{ | ||
var libFile = Directory | ||
.EnumerateFiles(Directory.GetCurrentDirectory()) | ||
.FirstOrDefault(e => e.Contains(libraryName)); | ||
|
||
if (libFile != null) | ||
{ | ||
return NativeLibrary.Load(libFile, assembly, searchPath); | ||
} | ||
|
||
return IntPtr.Zero; | ||
} | ||
|
||
public static partial class Platform | ||
{ | ||
[LibraryImport("interop", EntryPoint = "roc__mainForHost_1_exposed_generic")] | ||
internal static partial void MainFromRoc(out RocStr rocStr); | ||
} | ||
|
||
public unsafe struct RocStr | ||
{ | ||
public byte* Bytes; | ||
public UIntPtr Len; | ||
public UIntPtr Capacity; | ||
|
||
public override string ToString() => Encoding.UTF8.GetString(Bytes, (int)Len.ToUInt32()); | ||
|
||
public static implicit operator string(RocStr rocStr) => rocStr.ToString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
platform "dotnetplatform" | ||
requires {} { main : Str } | ||
exposes [] | ||
packages {} | ||
imports [] | ||
provides [mainForHost] | ||
|
||
mainForHost : Str | ||
mainForHost = main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters