Skip to content

Commit

Permalink
Working initial binding
Browse files Browse the repository at this point in the history
  • Loading branch information
bramhoven committed Dec 3, 2023
1 parent 46eb500 commit 902af2f
Show file tree
Hide file tree
Showing 23 changed files with 3,274 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/carton-bindings-csharp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ast/**/*
40 changes: 40 additions & 0 deletions source/carton-bindings-csharp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
These bindings will allow users to use Carton within C#.

The C# bindings are generated from Cartons' C bindings. (`carton-bindings-c`)

The project `Carton.Tests` can be referenced to see some examples of the usage.

## Details on generating bindings

### Dependencies

For generating the bindings we use the dotnet tool [c2cs](https://github.com/bottlenoselabs/c2cs/).
Install the these tools for generating the bindings:
```
$ dotnet tool install bottlenoselabs.c2cs.tool --global
$ dotnet tool install bottlenoselabs.castffi.tool -g
```

### Preparing c2cs

`c2cs` needs a little setup before it can generate the bindings correctly.
We will need to generate some config files.
These are `config-windows.json`, `config-linux.json` & `config-macos.json`. I have not yet been able to create the last one.
We then use the `castffi extract` command to extract the abstract syntax tree (ast).
```
$ castffi extract config-windows.json
$ castffi extract config-linux.json
$ castffi extract config-macos.json
```

We can then merge these seperate ast-files into a single cross platform ast using:
`castffi merge --inputDirectoryPath ./ast --outputFilePath cross-platform-ast.json`

Lastly we create the `c2cs` config file named `config-generate-cs.json`.

### Running c2cs

To run the tool we simply execute the following command:
`c2cs generate --config config-generate-cs.json`

This will have regenerated the binding file which you can find in `csharp/Carton/CartonBindings.gen.cs`.
9 changes: 9 additions & 0 deletions source/carton-bindings-csharp/config-generate-cs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"inputFilePath": "./cross-platform-ast.json",
"outputFileDirectory": "./csharp/Carton/Native",
"namespaceName": "Carton.Native",
"className": "CartonBindings",
"libraryName": "./libcarton_bindings_c",
"isEnabledFileScopedNamespace": false,
"isEnabledLibraryImport": true
}
10 changes: 10 additions & 0 deletions source/carton-bindings-csharp/config-linux.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"inputFilePath": "../carton-bindings-c/carton.h",
"userIncludeDirectories": [
"/usr/lib/clang/16/include/"
],
"platforms": {
"aarch64-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {}
}
}
8 changes: 8 additions & 0 deletions source/carton-bindings-csharp/config-windows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"inputFilePath": "../carton-bindings-c/carton.h",
"userIncludeDirectories": [],
"platforms": {
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
}
}
Loading

0 comments on commit 902af2f

Please sign in to comment.