Skip to content

Commit a587494

Browse files
authored
Merge pull request #117 from Peefy/kcl-c-api-based-cbingen
feat: kcl capi based cbindgen
2 parents 065f222 + f048b9a commit a587494

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

c/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern crate cbindgen;
2+
3+
use std::path::Path;
4+
5+
fn main() {
6+
let header_file = Path::new("include").join("kcl_ffi.h");
7+
8+
cbindgen::generate(".")
9+
.expect("Unable to generate bindings")
10+
.write_to_file(header_file);
11+
}

c/cbindgen.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cpp_compat = true
2+
include_guard = "_KCL_FFI_H"
3+
language = "C"
4+
no_includes = true
5+
sys_includes = ["stdint.h", "stddef.h", "stdbool.h"]
6+
7+
[parse]
8+
include = ["kcl_ffi"]
9+
parse_deps = true

c/examples/exec_api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <kcl_lib.h>
22

3-
int exec_file(const char* file_str) {
3+
int exec_file(const char* file_str)
4+
{
45
uint8_t buffer[BUFFER_SIZE];
56
uint8_t result_buffer[BUFFER_SIZE];
67
size_t message_length;

c/examples/ping_api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <kcl_lib.h>
22

3-
int ping(const char* msg) {
3+
int ping(const char* msg)
4+
{
45
uint8_t buffer[BUFFER_SIZE];
56
uint8_t result_buffer[BUFFER_SIZE];
67
size_t message_length;

c/include/kcl_ffi.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef _KCL_FFI_H
2+
#define _KCL_FFI_H
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
#include <stdbool.h>
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif // __cplusplus
11+
12+
uintptr_t call_native(const uint8_t *name_ptr,
13+
uintptr_t name_len,
14+
const uint8_t *args_ptr,
15+
uintptr_t args_len,
16+
uint8_t *result_ptr);
17+
18+
#ifdef __cplusplus
19+
} // extern "C"
20+
#endif // __cplusplus
21+
22+
#endif /* _KCL_FFI_H */

c/include/kcl_lib.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extern "C" {
1414
#include <stddef.h>
1515
#include <stdbool.h>
1616

17+
#include "kcl_ffi.h"
18+
1719
#define BUFFER_SIZE 1024
1820

1921
struct Buffer {
@@ -77,12 +79,6 @@ bool check_error_prefix(uint8_t result_buffer[])
7779
return false;
7880
}
7981

80-
uintptr_t call_native(const uint8_t *name_ptr,
81-
uintptr_t name_len,
82-
const uint8_t *args_ptr,
83-
uintptr_t args_len,
84-
uint8_t *result_ptr);
85-
8682
#ifdef __cplusplus
8783
} /* extern "C" */
8884
#endif

0 commit comments

Comments
 (0)