Skip to content

Instantly share code, notes, and snippets.

@mkotsbak
Forked from timothyklim/main.py
Last active September 22, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkotsbak/7ae7c5ec7b852fdabcb67fe913e54ab8 to your computer and use it in GitHub Desktop.
Save mkotsbak/7ae7c5ec7b852fdabcb67fe913e54ab8 to your computer and use it in GitHub Desktop.
import clang.cindex
import os, textwrap, StringIO
from clang.cindex import Config, Index, CursorKind
clib_scalanative_path = '/Users/timothyklim/Development/scala-native/clib/src/main/scala/scala/scalanative/libc'
scalatypes_map_dict = {
"int": "CInt"
"void": "Unit",
"void *": "Ptr[_]",
"char *": "CString",
"unsigned char": "CUnsignedChar",
"const char *": "CString",
"char": "CChar",
"int *": "Ptr[Int]"
}
def convert_to_scalanative(kind):
res = scalatypes_map_dict.get(kind)
if res is None:
print kind
raise "%s type not found" % kind
else:
return res
def look_header(path):
node = index.parse(path).cursor
name = os.path.basename(path).split('.')[0]
output = StringIO.StringIO()
header = """
package scala.scalanative
package libc
import native._
@extern object %s {
""" % name
output.write(textwrap.dedent(header).lstrip())
for c in node.get_children():
if not c.displayname.startswith("_") and c.displayname != "":
if c.kind == CursorKind.FUNCTION_DECL:
print([c.kind, c.spelling, c.displayname, c.type.get_result().spelling])
def_args = []
for a in c.get_arguments():
print ([a.displayname, a.type.spelling, a.type.get_result().spelling])
def_args.append("%s: %s" % (a.displayname.replace('_', ''), convert_to_scalanative(a.type.spelling)))
def_name = c.spelling
def_type = convert_to_scalanative(c.type.get_result().spelling)
output.write(" def %s(%s): %s = extern\n" % (def_name, ', '.join(def_args), def_type))
output.write("}\n")
contents = output.getvalue()
output.close()
f = open("%s/%s.scala" % (clib_scalanative_path, name), "w")
f.write(contents)
f.close()
return contents
Config.set_library_path("/usr/local/Cellar/llvm38/3.8.0/lib/llvm/lib")
index = Index.create()
include_path = '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include' # '/usr/local/Cellar/llvm38/3.8.0/lib/llvm/include/c++/v1'
# 'stdlib', 'string', 'float', 'complex'
# 'stdnoreturn', 'stdalign', 'stdarg', 'stdatomic', 'stdbool', 'threads', 'uchar'
# 'inttypes', 'iso646', 'limits', 'locale',
# 'math', 'setjmp', 'signal', 'stddef', 'stdint', 'string', 'tgmath', 'time', 'wchar', 'wctype'
for header in ['assert', 'ctype', 'errno', 'fenv']:
print 'processing header %s:' % header
look_header('%s/%s.h' % (include_path, header))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment