소스 검색

add sokol_log.h to zig bindings

Andre Weissflog 2 년 전
부모
커밋
ce08661a3f
2개의 변경된 파일35개의 추가작업 그리고 23개의 파일을 삭제
  1. 1 0
      bindgen/gen_all.py
  2. 34 23
      bindgen/gen_zig.py

+ 1 - 0
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_'] ],

+ 34 - 23
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