Skip to content

Instantly share code, notes, and snippets.

@trolldbois
Created January 17, 2018 02:30
Show Gist options
  • Save trolldbois/af7fee01b1afa13a74c196fefceeda93 to your computer and use it in GitHub Desktop.
Save trolldbois/af7fee01b1afa13a74c196fefceeda93 to your computer and use it in GitHub Desktop.
import ctypeslib
from ctypeslib.codegen import clangparser
from ctypeslib.codegen import codegenerator
def gen_python_bindings(outdir, path):
# TODO: Make it configurable
clang_opts = ["-I/usr/local/include/libr/", "-I/usr/local/include/"]
#
pyout = StringIO()
fname = os.path.splitext(os.path.basename(path))[0]
# Use clang_opts here
parser = clangparser.Clang_Parser(flags = clang_opts)
items = parser.parse(path)
if items is None:
print("Error parsing {0} file!\n".format(fname))
return False
gen = codegenerator.Generator(pyout)
# See ctypeslib/clang2py.py for more options
gen.generate(parser, items, flags=[], verbose=True)
outfname = outdir + "/" + fname + ".py"
with fopen(outfname, "w") as f:
f.write(pyout.getvalue())
pyout.close()
return True
pyout.close()
print("Cannot write {0}.py file!\n".format(fname))
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment