This commit is contained in:
nub31
2025-10-16 22:28:49 +02:00
parent 1edeb7253e
commit e887c638f6

View File

@@ -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}")