...
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import json
|
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import clang.cindex
|
import clang.cindex
|
||||||
from clang.cindex import CursorKind, TypeKind, Type
|
from clang.cindex import CursorKind, TypeKind, Type
|
||||||
|
|
||||||
@@ -66,29 +66,35 @@ def map_type(clang_type: Type):
|
|||||||
|
|
||||||
if kind == TypeKind.RECORD:
|
if kind == TypeKind.RECORD:
|
||||||
if not spelling.startswith("struct "):
|
if not spelling.startswith("struct "):
|
||||||
name = clang_type.get_canonical().spelling
|
raise Exception(f"Unknown custom type: {spelling}")
|
||||||
raise Exception(f"Unknown custom type: {name}")
|
|
||||||
|
|
||||||
return spelling.replace("struct ", "")
|
return spelling.replace("struct ", "")
|
||||||
|
|
||||||
name = clang_type.get_canonical().spelling
|
raise Exception(f"Unresolved type: {spelling}")
|
||||||
raise Exception(f"Unresolved type: {name}")
|
|
||||||
|
|
||||||
|
|
||||||
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()
|
index = clang.cindex.Index.create()
|
||||||
|
|
||||||
args = ["-x", "c"]
|
tu = index.parse(filename, ["-x", "c"])
|
||||||
|
|
||||||
tu = index.parse(filename, args=args)
|
|
||||||
|
|
||||||
if tu.diagnostics:
|
if tu.diagnostics:
|
||||||
for diag in tu.diagnostics:
|
for diag in tu.diagnostics:
|
||||||
if diag.severity >= clang.cindex.Diagnostic.Error:
|
if diag.severity >= clang.cindex.Diagnostic.Error:
|
||||||
print(f"Error: {diag.spelling}", file=sys.stderr)
|
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():
|
for cursor in tu.cursor.walk_preorder():
|
||||||
|
if cursor.location.file and cursor.location.file.name != filename:
|
||||||
|
continue
|
||||||
|
|
||||||
if cursor.kind == CursorKind.FUNCTION_DECL:
|
if cursor.kind == CursorKind.FUNCTION_DECL:
|
||||||
name = cursor.spelling
|
name = cursor.spelling
|
||||||
return_type = map_type(cursor.result_type)
|
return_type = map_type(cursor.result_type)
|
||||||
@@ -102,6 +108,7 @@ for cursor in tu.cursor.walk_preorder():
|
|||||||
params_str = ", ".join(params)
|
params_str = ", ".join(params)
|
||||||
|
|
||||||
print(f'export extern "{name}" func {name}({params_str}): {return_type}')
|
print(f'export extern "{name}" func {name}({params_str}): {return_type}')
|
||||||
|
|
||||||
elif cursor.kind == CursorKind.STRUCT_DECL:
|
elif cursor.kind == CursorKind.STRUCT_DECL:
|
||||||
name = cursor.spelling
|
name = cursor.spelling
|
||||||
print(f"export struct {name}")
|
print(f"export struct {name}")
|
||||||
|
|||||||
Reference in New Issue
Block a user