aboutsummaryrefslogtreecommitdiff
path: root/bindgen
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2020-12-23 14:37:41 +0100
committerAndre Weissflog <floooh@gmail.com>2020-12-23 14:37:41 +0100
commit36fdcc0257dd542065b4c246d620ca5493d65a4c (patch)
tree4d5f86c876fe59e7cfe09215d06e8da7cae1f2d1 /bindgen
parentd11e627dfab9740c12a1895e957410beb09b0da9 (diff)
bindgen updates
Diffstat (limited to 'bindgen')
-rw-r--r--bindgen/.gitignore4
-rw-r--r--bindgen/README.md22
-rw-r--r--bindgen/gen_all.py20
-rw-r--r--bindgen/gen_ir.py2
-rw-r--r--bindgen/gen_zig.py15
5 files changed, 46 insertions, 17 deletions
diff --git a/bindgen/.gitignore b/bindgen/.gitignore
index 69e0b805..1b8d7732 100644
--- a/bindgen/.gitignore
+++ b/bindgen/.gitignore
@@ -1,4 +1,4 @@
*.json
*.zig
-zig/
-
+__pycache__/
+sokol-zig/
diff --git a/bindgen/README.md b/bindgen/README.md
index 28f36741..faf6699c 100644
--- a/bindgen/README.md
+++ b/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
+...
+```
diff --git a/bindgen/gen_all.py b/bindgen/gen_all.py
index dff47187..0df517fb 100644
--- a/bindgen/gen_all.py
+++ b/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)
diff --git a/bindgen/gen_ir.py b/bindgen/gen_ir.py
index 36bebd87..165da199 100644
--- a/bindgen/gen_ir.py
+++ b/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 = {}
diff --git a/bindgen/gen_zig.py b/bindgen/gen_zig.py
index 88ce28dd..b605785a 100644
--- a/bindgen/gen_zig.py
+++ b/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)