From a0f9dd521e74ac272a330f0d28dda4a9d4639ec2 Mon Sep 17 00:00:00 2001 From: nub31 Date: Thu, 16 Oct 2025 22:28:49 +0200 Subject: [PATCH] ... --- bindings/generate.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/bindings/generate.py b/bindings/generate.py index 29029ec..f72f008 100644 --- a/bindings/generate.py +++ b/bindings/generate.py @@ -1,5 +1,5 @@ -import json import sys +import os import clang.cindex from clang.cindex import CursorKind, TypeKind, Type @@ -66,29 +66,35 @@ def map_type(clang_type: Type): if kind == TypeKind.RECORD: if not spelling.startswith("struct "): - name = clang_type.get_canonical().spelling - raise Exception(f"Unknown custom type: {name}") + raise Exception(f"Unknown custom type: {spelling}") return spelling.replace("struct ", "") - name = clang_type.get_canonical().spelling - raise Exception(f"Unresolved type: {name}") + raise Exception(f"Unresolved type: {spelling}") -filename = "../examples/raylib/raylib-5.5_linux_amd64/include/raylib.h" +if len(sys.argv) != 2: + print("Usage: python3 generate.py [path to header]", file=sys.stderr) + sys.exit(1) + +filename = sys.argv[1] index = clang.cindex.Index.create() -args = ["-x", "c"] - -tu = index.parse(filename, args=args) +tu = index.parse(filename, ["-x", "c"]) if tu.diagnostics: for diag in tu.diagnostics: if diag.severity >= clang.cindex.Diagnostic.Error: print(f"Error: {diag.spelling}", file=sys.stderr) +print(f'module "{os.path.basename(filename).split(".")[0]}"') +print() + for cursor in tu.cursor.walk_preorder(): + if cursor.location.file and cursor.location.file.name != filename: + continue + if cursor.kind == CursorKind.FUNCTION_DECL: name = cursor.spelling return_type = map_type(cursor.result_type) @@ -102,6 +108,7 @@ for cursor in tu.cursor.walk_preorder(): params_str = ", ".join(params) print(f'export extern "{name}" func {name}({params_str}): {return_type}') + elif cursor.kind == CursorKind.STRUCT_DECL: name = cursor.spelling print(f"export struct {name}")