Skip to content

Commit

Permalink
c2ffi (#161)
Browse files Browse the repository at this point in the history
* First pass in converting to using `c2ffi`

* Changes and fixes for `c2ffi`
  • Loading branch information
lithiumtoast committed Apr 14, 2024
1 parent c8c468a commit 5a9a557
Show file tree
Hide file tree
Showing 168 changed files with 3,143 additions and 1,408 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
if: matrix.platform.os == 'windows-latest'
run: |
source "${{ github.workspace }}\.github\scripts\install-clang-x64-windows.sh"
- name: "Install Clang: Linux"
if: matrix.platform.os == 'ubuntu-latest'
run: |
Expand All @@ -39,9 +39,9 @@ jobs:
with:
dotnet-version: '8.x'

- name: "Install CAstFfi"
- name: "Install c2ffi"
shell: bash
run: dotnet tool install --global bottlenoselabs.CAstFfi.Tool
run: dotnet tool install --global bottlenoselabs.c2ffi.tool

- name: "Test .NET solution"
run: |
Expand Down
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ lib/
*.dylib
*.so

# C2CS artifacts
ast/
# c2ffi artifacts
src/c/tests/**/ffi/
src/c/tests/**/ffi-x/

# c2cs artifacts
src/c/tests/**/cs

# macOS
.DS_Store
14 changes: 7 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- [Latest release of `C2CS`](#latest-release-of-c2cs)
- [Latest pre-release of `C2CS`](#latest-pre-release-of-c2cs)
- [How to use `C2CS`](#how-to-use-c2cs)
- [Installing and using `CAstFfi`](#installing-and-using-castffi)
- [Installing and using `c2ffi`](#installing-and-using-c2ffi)
- [Execute `c2cs`](#execute-c2cs)
- [How to use `C2CS.Runtime`](#how-to-use-c2csruntime)
- [Building `C2CS` from source](#building-c2cs-from-source)
Expand Down Expand Up @@ -51,17 +51,17 @@ dotnet nuget locals all --clear

## How to use `C2CS`

To generate C# bindings for a C library you need to install and use the `CAstFfi` tool and setup a couple configuration files. See the [`helloworld`](../src/cs/examples/helloworld/) example projects for an example.
To generate C# bindings for a C library you need to install and use the `c2ffi` tool and setup a couple configuration files. See the [`helloworld`](../src/cs/examples/helloworld/) example projects for an example.

### Installing and using `CAstFfi`
### Installing and using `c2ffi`

See the auxiliary project `Getting Started` section: https://github.com/bottlenoselabs/CAstFfi#getting-started.
See the auxiliary project `Getting Started` section: https://github.com/bottlenoselabs/c2ffi#getting-started.

You should extract all the platform specific abstract syntax trees you wish to have as target platforms. See the [`helloworld-compile-c-library-and-generate-bindings`](../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/) for example configuration files for Windows, macOS, and Linux platforms.
You should extract all the platform specific FFIs you wish to have as target platforms. See the [`helloworld-compile-c-library-and-generate-bindings`](../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/) for example configuration files for Windows, macOS, and Linux platforms.

Once all the platform abstract syntax trees are extracted to a directory, merge them together into a cross-platform abstract syntax tree using `CAstFfi merge` option.
Once all the platform FFIs are extracted to a directory, merge them together into a cross-platform FFI using `c2ffi merge` option.

Once you have a cross-platform abstract syntax tree, you are ready to use `c2cs`.
Once you have a cross-platform FFI `.json` file, you are ready to use `c2cs`.

### Execute `c2cs`

Expand Down
4 changes: 2 additions & 2 deletions docs/SUPPORTED-PLATFORMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- [Problem](#problem)
- [Solution](#solution)
- [Calling C platform code from your .NET application](#calling-c-platform-code-from-your-net-application)
- [Dynamic loading & dynamic linking](#dynamic-loading--dynamic-linking)
- [Dynamic loading \& dynamic linking](#dynamic-loading--dynamic-linking)
- [Dynamic loading](#dynamic-loading)
- [Dynamic linking](#dynamic-linking)
- [Static linking](#static-linking)
Expand Down Expand Up @@ -58,7 +58,7 @@ While Clang is free and open-source, an annoying problem is that some target pla

`C2CS` the main flow has evolved into two programs to overcome this problem.

1. `ast`: (NOTE: Moved to https://github.com/bottlenoselabs/CAstFfi !) Reading C code and extracting information of the target platform for generating C# code later in form a C abstract syntax tree `.json` file. The extracted information _**does not contain any source code**_; it is purely meta data about the bitwidth and names of functions, structs, enums, typedefs, etc.
1. `ast`: (NOTE: Moved to https://github.com/bottlenoselabs/c2ffi !) Reading C code and extracting information of the target platform for generating C# code later in from a C ffi `.json` file. The extracted information _**does not contain any source code**_; it is purely meta data about the bitwidth and names of functions, structs, enums, typedefs, etc.
2. `cs`: (NOTE: now just the main `c2cs` program!) Reading one or more previously generated C abstract syntax tree `.json` files to generate C# code for the specified target platforms.

This allows the `ast` program of `C2CS` to run and extract the information for the target platforms with restrictive system headers. The resulting `.json` files of the C abstract syntax trees can be moved to any operating system to generate the C# code for multiple target platforms at once using the `cs` program of `C2CS`.
Expand Down
52 changes: 41 additions & 11 deletions src/c/production/ffi_helper/include/ffi_helper.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Provides macros, types, and functions that make P/Invoke with C# easier.
// Provides macros, types, and functions that make FFI easier.

#pragma once
#include <string.h>

#define FFI_COMPILER_CLANG __clang__

#if defined(__APPLE__) && __has_include("TargetConditionals.h")
#include <TargetConditionals.h>
Expand Down Expand Up @@ -30,7 +32,8 @@
#define FFI_TARGET_ENV_GNU defined(__GNUC__)
#endif

#if FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_GNU
#if FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_GNU // Windows (GNU)

#if FFI_TARGET_CPU_X64
#define FFI_PLATFORM_NAME "x86_64-pc-windows-gnu"
#elif FFI_TARGET_CPU_X86
Expand All @@ -39,8 +42,11 @@
#define FFI_PLATFORM_NAME "aarch64-pc-windows-gnu"
#else
#error "Unknown computer architecture for Windows (GNU)."
#define FFI_PLATFORM_NAME 0
#endif
#elif FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_MSVC

#elif FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_MSVC // Windows (Microsoft Visual C++)

#if FFI_TARGET_CPU_X64
#define FFI_PLATFORM_NAME "x86_64-pc-windows-msvc"
#elif FFI_TARGET_CPU_X86
Expand All @@ -49,8 +55,11 @@
#define FFI_PLATFORM_NAME "aarch64-pc-windows-msvc"
#else
#error "Unknown computer architecture for Windows (Microsoft Visual C++)."
#define FFI_PLATFORM_NAME 0
#endif
#elif FFI_TARGET_OS_LINUX

#elif FFI_TARGET_OS_LINUX // Linux

#if FFI_TARGET_CPU_X64
#define FFI_PLATFORM_NAME "x86_64-unknown-linux-gnu"
#elif FFI_TARGET_CPU_X86
Expand All @@ -59,8 +68,11 @@
#define FFI_PLATFORM_NAME "aarch64-unknown-linux-gnu"
#else
#error "Unknown computer architecture for Linux."
#define FFI_PLATFORM_NAME 0
#endif
#elif FFI_TARGET_OS_MACOS

#elif FFI_TARGET_OS_MACOS // macOS

#if FFI_TARGET_CPU_X64
#define FFI_PLATFORM_NAME "x86_64-apple-darwin"
#elif FFI_TARGET_CPU_X86
Expand All @@ -69,8 +81,11 @@
#define FFI_PLATFORM_NAME "aarch64-apple-darwin"
#else
#error "Unknown computer architecture for macOS."
#define FFI_PLATFORM_NAME 0
#endif
#elif FFI_TARGET_OS_IOS

#elif FFI_TARGET_OS_IOS // iOS

#if FFI_TARGET_CPU_X64
#define FFI_PLATFORM_NAME "x86_64-apple-ios"
#elif FFI_TARGET_CPU_X86
Expand All @@ -79,13 +94,28 @@
#define FFI_PLATFORM_NAME "aarch64-apple-ios"
#else
#error "Unknown computer architecture for iOS."
#define FFI_PLATFORM_NAME 0
#endif
#else

#else // Unknown

#error "Unknown computer architecture for unknown platform name."
#define FFI_PLATFORM_NAME 0

#endif

#if FFI_TARGET_OS_WINDOWS
#define FFI_API_DECL __declspec(dllexport)
#if FFI_COMPILER_CLANG
#define FFI_API_DECL __declspec(dllexport) __attribute__ ((visibility("default")))
#else
#define FFI_API_DECL __declspec(dllexport)
#endif
#else
#define FFI_API_DECL extern
#endif
#define FFI_API_DECL extern __attribute__ ((visibility("default")))
#endif

// Returns the current platform name.
FFI_API_DECL const char* ffi_get_platform_name()
{
return FFI_PLATFORM_NAME;
}
5 changes: 0 additions & 5 deletions src/c/tests/README.md

This file was deleted.

8 changes: 0 additions & 8 deletions src/c/tests/_container_library/build.sh

This file was deleted.

6 changes: 0 additions & 6 deletions src/c/tests/_container_library/main.c

This file was deleted.

21 changes: 0 additions & 21 deletions src/c/tests/enums/EnumForceSInt16.h

This file was deleted.

21 changes: 0 additions & 21 deletions src/c/tests/enums/EnumForceSInt32.h

This file was deleted.

21 changes: 0 additions & 21 deletions src/c/tests/enums/EnumForceSInt8.h

This file was deleted.

21 changes: 0 additions & 21 deletions src/c/tests/enums/EnumForceUInt16.h

This file was deleted.

21 changes: 0 additions & 21 deletions src/c/tests/enums/EnumForceUInt32.h

This file was deleted.

21 changes: 0 additions & 21 deletions src/c/tests/enums/EnumForceUInt8.h

This file was deleted.

8 changes: 0 additions & 8 deletions src/c/tests/enums/_index.h

This file was deleted.

27 changes: 27 additions & 0 deletions src/c/tests/enums/enum_uint8/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoredIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
Loading

0 comments on commit 5a9a557

Please sign in to comment.