Skip to content

Commit

Permalink
bindgen: Textwrap indent and Zig comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderArvidsson committed Jan 5, 2025
1 parent f80da36 commit 6d3258f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 6 additions & 5 deletions bindgen/gen_odin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
# Generate Odin bindings.
#-------------------------------------------------------------------------------
import textwrap
import gen_ir
import gen_util as util
import os, shutil, sys
Expand Down Expand Up @@ -150,13 +151,11 @@ def l(s):
out_lines += s + '\n'

def c(s, indent=""):
if not s:
return
if '\n' in s:
l(f'{indent}/*')
if indent:
# prefix all lines with indent
l(indent + indent.join(s.splitlines(True)))
else:
l(s)
l(textwrap.indent(textwrap.dedent(s), prefix=f" {indent}", predicate=lambda line: True))
l(f'{indent}*/')
else:
l(f'{indent}// {s.strip()}')
Expand Down Expand Up @@ -451,8 +450,10 @@ def gen_c_imports(inp, c_prefix, prefix):
l('')

def gen_consts(decl, prefix):
c(decl.get('comment'))
for item in decl['items']:
item_name = check_override(item['name'])
c(item.get('comment'))
l(f"{as_snake_case(item_name, prefix)} :: {item['value']}")
l('')

Expand Down
22 changes: 20 additions & 2 deletions bindgen/gen_zig.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#-------------------------------------------------------------------------------
import gen_ir
import os, shutil, sys
import textwrap

import gen_util as util

Expand Down Expand Up @@ -125,6 +126,12 @@ def l(s):
global out_lines
out_lines += s + '\n'

def c(s, indent=""):
if not s:
return
prefix = f"{indent}/// "
l(textwrap.indent(textwrap.dedent(s), prefix=prefix, predicate=lambda line: True))

def as_zig_prim_type(s):
return prim_types[s]

Expand Down Expand Up @@ -324,6 +331,7 @@ def funcdecl_result_zig(decl, prefix):
def gen_struct(decl, prefix):
struct_name = check_override(decl['name'])
zig_type = as_zig_struct_type(struct_name, prefix)
c(decl.get('comment'))
l(f"pub const {zig_type} = extern struct {{")
for field in decl['fields']:
field_name = check_override(field['name'])
Expand Down Expand Up @@ -382,14 +390,19 @@ def gen_struct(decl, prefix):
else:
sys.exit(f"ERROR gen_struct: {field_name}: {field_type};")
l("};")
l("")

def gen_consts(decl, prefix):
c(decl.get('comment'))
for item in decl['items']:
item_name = check_override(item['name'])
c(item.get('comment'))
l(f"pub const {util.as_lower_snake_case(item_name, prefix)} = {item['value']};")
l("")

def gen_enum(decl, prefix):
enum_name = check_override(decl['name'])
c(decl.get('comment'))
l(f"pub const {as_zig_enum_type(enum_name, prefix)} = enum(i32) {{")
for item in decl['items']:
item_name = as_enum_item_name(check_override(item['name']))
Expand All @@ -399,13 +412,17 @@ def gen_enum(decl, prefix):
else:
l(f" {item_name},")
l("};")
l("")

def gen_func_c(decl, prefix):
c(decl.get('comment'))
l(f"pub extern fn {decl['name']}({funcdecl_args_c(decl, prefix)}) {funcdecl_result_c(decl, prefix)};")
l('')

def gen_func_zig(decl, prefix):
c_func_name = decl['name']
zig_func_name = util.as_lower_camel_case(check_override(decl['name']), prefix)
c(decl.get('comment'))
if c_func_name in c_callbacks:
# a simple forwarded C callback function
l(f"pub const {zig_func_name} = {c_func_name};")
Expand Down Expand Up @@ -435,6 +452,7 @@ def gen_func_zig(decl, prefix):
s += ");"
l(s)
l("}")
l("")

def pre_parse(inp):
global struct_types
Expand Down Expand Up @@ -570,8 +588,8 @@ def gen(c_header_path, c_prefix, dep_c_prefixes):
print(f' {c_header_path} => {module_name}')
reset_globals()
shutil.copyfile(c_header_path, f'sokol-zig/src/sokol/c/{os.path.basename(c_header_path)}')
ir = gen_ir.gen(c_header_path, c_source_path, module_name, c_prefix, dep_c_prefixes)
gen_module(ir, dep_c_prefixes)
ir = gen_ir.gen(c_header_path, c_source_path, module_name, c_prefix, dep_c_prefixes, with_comments=True)
gen_module(ir, dep_c_prefixes,)
output_path = f"sokol-zig/src/sokol/{ir['module']}.zig"
with open(output_path, 'w', newline='\n') as f_outp:
f_outp.write(out_lines)

0 comments on commit 6d3258f

Please sign in to comment.