From ca91acb03bcea2ec14a49baa939dc37ad868b768 Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Mon, 27 Jun 2022 19:20:25 +0200 Subject: start with odin bindings --- bindgen/.gitignore | 1 + bindgen/gen_all.py | 10 ++++++- bindgen/gen_odin.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 bindgen/gen_odin.py (limited to 'bindgen') diff --git a/bindgen/.gitignore b/bindgen/.gitignore index 9dcbf6fe..412befc3 100644 --- a/bindgen/.gitignore +++ b/bindgen/.gitignore @@ -4,3 +4,4 @@ __pycache__/ sokol-nim/ sokol-zig/ +sokol-odin/ diff --git a/bindgen/gen_all.py b/bindgen/gen_all.py index 112ca8fc..2f514b3b 100644 --- a/bindgen/gen_all.py +++ b/bindgen/gen_all.py @@ -1,4 +1,4 @@ -import os, gen_nim, gen_zig +import os, gen_nim, gen_zig, gen_odin tasks = [ [ '../sokol_gfx.h', 'sg_', [] ], @@ -11,6 +11,14 @@ tasks = [ [ '../util/sokol_shape.h', 'sshape_', ['sg_'] ], ] +# Odin +gen_odin.prepare() +for task in tasks: + c_header_path = task[0] + main_prefix = task[1] + dep_prefixes = task[2] + gen_odin.gen(c_header_path, main_prefix, dep_prefixes) + # Nim gen_nim.prepare() for task in tasks: diff --git a/bindgen/gen_odin.py b/bindgen/gen_odin.py new file mode 100644 index 00000000..18a9ef32 --- /dev/null +++ b/bindgen/gen_odin.py @@ -0,0 +1,79 @@ +#------------------------------------------------------------------------------- +# gen_odin.py +# +# Generate Odin bindings. +#------------------------------------------------------------------------------- +import gen_ir +import re, os, shutil, sys + +module_root = 'sokol-odin/src/sokol' + +module_names = { + 'sg_': 'gfx', + 'sapp_': 'app', + 'sapp_sg': 'glue', + 'stm_': 'time', + 'saudio_': 'audio', + 'sgl_': 'gl', + 'sdtx_': 'debugtext', + 'sshape_': 'shape', +} + +c_source_names = { + 'sg_': 'sokol_gfx.c', + 'sapp_': 'sokol_app.c', + 'sapp_sg': 'sokol_glue.c', + 'stm_': 'sokol_time.c', + 'saudio_': 'sokol_audio.c', + 'sgl_': 'sokol_gl.c', + 'sdtx_': 'sokol_debugtext.c', + 'sshape_': 'sokol_shape.c', +} + +out_lines = '' + +def reset_globals(): + global out_lines + out_lines = '' + +def l(s): + global out_lines + out_lines += s + '\n' + +def get_odin_module_path(c_prefix): + return f'{module_root}/{module_names[c_prefix]}' + +def get_csource_path(c_prefix): + return f'{module_root}/c/{c_source_names[c_prefix]}' + +def make_odin_module_directory(c_prefix): + path = get_odin_module_path(c_prefix) + if not os.path.isdir(path): + os.makedirs(path) + +def gen_module(inp, dep_prefixes): + l('// machine generated, do not edit') + l('') + +def prepare(): + print('Generating Odin bindings:') + if not os.path.isdir(f'{module_root}/c'): + os.makedirs(f'{module_root}/c') + +def gen(c_header_path, c_prefix, dep_c_prefixes): + if not c_prefix in module_names: + print(f'warning: skipping generation for {c_prefix} prefix...') + return + reset_globals() + make_odin_module_directory(c_prefix) + print(f' {c_header_path} => {module_names[c_prefix]}') + shutil.copyfile(c_header_path, f'{module_root}/c/{os.path.basename(c_header_path)}') + csource_path = get_csource_path(c_prefix) + module_name = module_names[c_prefix] + ir = gen_ir.gen(c_header_path, csource_path, module_name, c_prefix, dep_c_prefixes) + gen_module(ir, dep_c_prefixes) + with open(f"{module_root}/{ir['module']}/{ir['module']}.odin", 'w', newline='\n') as f_outp: + f_outp.write(out_lines) + + + -- cgit v1.2.3