Explorar el Código

bindgen updates

Andre Weissflog hace 4 años
padre
commit
36fdcc0257
Se han modificado 5 ficheros con 46 adiciones y 17 borrados
  1. 2 2
      bindgen/.gitignore
  2. 21 1
      bindgen/README.md
  3. 14 6
      bindgen/gen_all.py
  4. 1 1
      bindgen/gen_ir.py
  5. 8 7
      bindgen/gen_zig.py

+ 2 - 2
bindgen/.gitignore

@@ -1,4 +1,4 @@
 *.json
 *.zig
-zig/
-
+__pycache__/
+sokol-zig/

+ 21 - 1
bindgen/README.md

@@ -1 +1,21 @@
-WIP!
+## Language Binding Generation Scripts
+
+### Zig
+
+To update the Zig bindings:
+
+```
+> cd sokol/bindgen
+> git clone https://github.com/floooh/sokol-zig
+> python3 gen_all.py
+```
+
+Test and run samples:
+
+```
+> cd sokol/bindgen/sokol-zig
+> zig build run-clear
+> zig build run-triangle
+> zig build run-cube
+...
+```

+ 14 - 6
bindgen/gen_all.py

@@ -1,9 +1,17 @@
 import gen_ir, gen_zig
 
-def gen_bindings(c_header_path, c_prefix, module_name):
-    print(f'> {c_header_path}')
-    ir = gen_ir.gen_ir(c_header_path, module_name, c_prefix)
-    gen_zig.gen_zig(ir)
+tasks = [
+    [ '../sokol_gfx.h', 'sg_', 'gfx' ],
+    [ '../sokol_app.h', 'sapp_', 'app' ]
+]
 
-gen_bindings('../sokol_gfx.h', 'sg_', 'gfx')
-gen_bindings('../sokol_app.h', 'sapp_', 'app')
+# Zig
+print('> generating Zig bindings...')
+gen_zig.prepare()
+for task in tasks:
+    c_header_path = task[0]
+    c_prefix = task[1]
+    module_name = task[2]
+    print(f'  {c_header_path} => {module_name}.zig')
+    ir = gen_ir.gen(c_header_path, module_name, c_prefix)
+    gen_zig.gen(c_header_path, ir)

+ 1 - 1
bindgen/gen_ir.py

@@ -88,7 +88,7 @@ def parse_decl(decl):
 def clang(header_path):
     return subprocess.check_output(['clang', '-Xclang', '-ast-dump=json', header_path])
 
-def gen_ir(header_path, module, prefix):
+def gen(header_path, module, prefix):
     ast = clang(header_path)
     inp = json.loads(ast)
     outp = {}

+ 8 - 7
bindgen/gen_zig.py

@@ -6,7 +6,7 @@
 #   - functions are camelCase
 #   - otherwise snake_case
 #-------------------------------------------------------------------------------
-import json, re, os
+import json, re, os, shutil
 
 struct_types = []
 enum_types = []
@@ -404,13 +404,14 @@ def gen_module(inp):
             gen_func_c(decl, prefix)
             gen_func_zig(decl, prefix)
 
-def gen_zig(input_ir):
-    if not os.path.isdir('zig/'):
-        os.mkdir('zig')
-    if not os.path.isdir('zig/sokol'):
-        os.mkdir('zig/sokol')
+def prepare():
+    if not os.path.isdir('sokol-zig/src/sokol'):
+        os.makedirs('sokol-zig/src/sokol')
+
+def gen(c_header_path, input_ir):
     reset_globals()
     gen_module(input_ir)
-    output_path = f"zig/sokol/{input_ir['module']}.zig"
+    shutil.copyfile(c_header_path, f'sokol-zig/src/sokol/{os.path.basename(c_header_path)}')
+    output_path = f"sokol-zig/src/sokol/{input_ir['module']}.zig"
     with open(output_path, 'w', newline='\n') as f_outp:
         f_outp.write(out_lines)