summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2023-02-11 16:02:38 +0100
committerAndre Weissflog <floooh@gmail.com>2023-02-11 16:02:38 +0100
commitce08661a3fc55fb0b834e4d51b49222aa0175b39 (patch)
treeafb69786f1e4932d3e217ee2698a9d33ea711159
parentd65c4cd7ea368eb8cb097be89d632dbae0da66d1 (diff)
add sokol_log.h to zig bindings
-rw-r--r--bindgen/gen_all.py1
-rw-r--r--bindgen/gen_zig.py57
2 files changed, 35 insertions, 23 deletions
diff --git a/bindgen/gen_all.py b/bindgen/gen_all.py
index 2f514b3b..6cb5cd6b 100644
--- a/bindgen/gen_all.py
+++ b/bindgen/gen_all.py
@@ -1,6 +1,7 @@
import os, gen_nim, gen_zig, gen_odin
tasks = [
+ [ '../sokol_log.h', 'slog_', [] ],
[ '../sokol_gfx.h', 'sg_', [] ],
[ '../sokol_app.h', 'sapp_', [] ],
[ '../sokol_glue.h', 'sapp_sg', ['sg_'] ],
diff --git a/bindgen/gen_zig.py b/bindgen/gen_zig.py
index 79f599c1..a8768ca3 100644
--- a/bindgen/gen_zig.py
+++ b/bindgen/gen_zig.py
@@ -12,6 +12,7 @@ import os, shutil, sys
import gen_util as util
module_names = {
+ 'slog_': 'log',
'sg_': 'gfx',
'sapp_': 'app',
'stm_': 'time',
@@ -22,6 +23,7 @@ module_names = {
}
c_source_paths = {
+ 'slog_': 'sokol-zig/src/sokol/c/sokol_log.c',
'sg_': 'sokol-zig/src/sokol/c/sokol_gfx.c',
'sapp_': 'sokol-zig/src/sokol/c/sokol_app.c',
'stm_': 'sokol-zig/src/sokol/c/sokol_time.c',
@@ -38,6 +40,11 @@ ignores = [
'sg_trace_hooks',
]
+# functions that need to be exposed as 'raw' C callbacks without a Zig wrapper function
+c_callbacks = [
+ 'slog_func'
+]
+
# NOTE: syntax for function results: "func_name.RESULT"
overrides = {
'sgl_error': 'sgl_get_error', # 'error' is reserved in Zig
@@ -391,31 +398,35 @@ def gen_func_c(decl, prefix):
def gen_func_zig(decl, prefix):
c_func_name = decl['name']
zig_func_name = util.as_lower_camel_case(check_override(decl['name']), prefix)
- zig_res_type = funcdecl_result_zig(decl, prefix)
- l(f"pub fn {zig_func_name}({funcdecl_args_zig(decl, prefix)}) {zig_res_type} {{")
- if is_zig_string(zig_res_type):
- # special case: convert C string to Zig string slice
- s = f" return cStrToZig({c_func_name}("
- elif zig_res_type != 'void':
- s = f" return {c_func_name}("
+ if c_func_name in c_callbacks:
+ # a simple forwarded C callback function
+ l(f"pub const {zig_func_name} = {c_func_name};")
else:
- s = f" {c_func_name}("
- for i, param_decl in enumerate(decl['params']):
- if i > 0:
- s += ", "
- arg_name = param_decl['name']
- arg_type = param_decl['type']
- if is_const_struct_ptr(arg_type):
- s += f"&{arg_name}"
- elif util.is_string_ptr(arg_type):
- s += f"@ptrCast([*c]const u8,{arg_name})"
+ zig_res_type = funcdecl_result_zig(decl, prefix)
+ l(f"pub fn {zig_func_name}({funcdecl_args_zig(decl, prefix)}) {zig_res_type} {{")
+ if is_zig_string(zig_res_type):
+ # special case: convert C string to Zig string slice
+ s = f" return cStrToZig({c_func_name}("
+ elif zig_res_type != 'void':
+ s = f" return {c_func_name}("
else:
- s += arg_name
- if is_zig_string(zig_res_type):
- s += ")"
- s += ");"
- l(s)
- l("}")
+ s = f" {c_func_name}("
+ for i, param_decl in enumerate(decl['params']):
+ if i > 0:
+ s += ", "
+ arg_name = param_decl['name']
+ arg_type = param_decl['type']
+ if is_const_struct_ptr(arg_type):
+ s += f"&{arg_name}"
+ elif util.is_string_ptr(arg_type):
+ s += f"@ptrCast([*c]const u8,{arg_name})"
+ else:
+ s += arg_name
+ if is_zig_string(zig_res_type):
+ s += ")"
+ s += ");"
+ l(s)
+ l("}")
def pre_parse(inp):
global struct_types