Skip to content

Instantly share code, notes, and snippets.

@pfernique
Last active August 24, 2016 18:55
Show Gist options
  • Save pfernique/4c53fece95f1255ee6e35f41f0eafd9c to your computer and use it in GitHub Desktop.
Save pfernique/4c53fece95f1255ee6e35f41f0eafd9c to your computer and use it in GitHub Desktop.
import subprocess
if not subprocess.check_output(["llvm-config", "--version"]).strip() == '3.8.1':
raise Exception
subprocess.check_call(['clang++', '--version'])
if not subprocess.check_output(["llvm-config", "--has-rtti"]).strip() == 'YES':
raise Exception
import sys
from path import path
prefix = path(sys.prefix)
if not prefix == subprocess.check_output(["llvm-config", "--prefix"]).strip():
raise Exception
srcdir = path('PyClangLite')
if not srcdir.exists():
from git import Repo
repo = Repo.clone_from('https://github.com/StatisKit/PyClangLite.git', srcdir.relpath('.'))
srcdir = srcdir/'src'/'py'
for wrapper in srcdir.walkfiles('*.cpp'):
wrapper.unlink()
subprocess.check_output(['scons', 'cpp', '--prefix=' + prefix, '-C', srcdir.parent.parent.abspath()])
headers = [prefix/'include'/'clanglite'/'tool.h']
import autowig
import pickle
try:
asg = autowig.AbstractSemanticGraph()
autowig.parser.plugin = 'libclang'
asg = autowig.parser(asg, headers,
flags = ['-x', 'c++', '-std=c++11',
'-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS',
'-I' + str((prefix/'include').abspath())],
libpath = prefix/'lib'/'libclang.so',
bootstrap = False,
silent = True)
with open('AutoWIG/asg.pkl', 'w') as f:
pickle.dump(asg, f)
except:
with open('AutoWIG/asg.pkl', 'r') as f:
asg = pickle.load(f)
def clanglite_controller(asg):
from autowig.boost_python_generator import BoostPythonExportBasicFileProxy
BoostPythonExportBasicFileProxy.HELDTYPE = "namespace autowig { template<class T> using HeldType = T*; }"
for node in asg['::boost::python'].classes(nested = True):
node.is_copyable = True
for node in asg.classes():
node.boost_python_export = False
for node in asg.enumerations():
node.boost_python_export = False
for node in asg.enumerators():
if node.parent.boost_python_export:
node.boost_python_export = False
for node in asg.typedefs():
node.boost_python_export = False
from autowig.default_controller import refactoring
asg = refactoring(asg)
for fct in asg['::clanglite'].functions():
if not fct.localname == 'build_ast_from_code_with_args':
fct.parent = fct.parameters[0].qualified_type.desugared_type.unqualified_type
for node in asg.functions(free = True):
node.boost_python_export = False
for node in asg.variables(free = True):
node.boost_python_export = False
asg['class ::boost::python::api::object'].boost_python_export = True
asg['class ::boost::python::list'].boost_python_export = True
asg['class ::boost::python::str'].boost_python_export = True
subset = []
classes = [asg['class ::clang::QualType'],
asg['class ::clang::Type'],
asg['class ::clang::Decl']]
asg._nodes['class ::clang::QualType']['_is_abstract'] = False
asg['class ::clang::QualType'].is_copyable = True
subset += classes
for cls in classes:
subset += cls.subclasses(recursive=True)
subset.append(asg['class ::llvm::StringRef'])
asg._nodes['class ::llvm::StringRef']['_is_abstract'] = False
asg['class ::llvm::StringRef'].is_copyable = True
subset.append(asg['class ::clang::ASTUnit'])
subset.append(asg['class ::clang::ASTContext'])
subset.append(asg['class ::clang::SourceManager'])
for mtd in asg['class ::clang::ASTContext'].methods(pattern='.*getSourceManager.*'):
if mtd.return_type.globalname == 'class ::clang::SourceManager &':
mtd.boost_python_export = True
break
subset.append(asg['class ::clang::FileID'])
asg._nodes['class ::clang::FileID']['_is_abstract'] = False
asg['class ::clang::FileID'].is_copyable = True
subset.append(asg['class ::clang::SourceLocation'])
asg._nodes['class ::clang::SourceLocation']['_is_abstract'] = False
asg['class ::clang::SourceLocation'].is_copyable = True
subset.append(asg['class ::clang::CXXBaseSpecifier'])
subset.append(asg['class ::clang::DeclContext'])
subset.append(asg['class ::clang::TemplateArgument'])
asg._nodes['class ::clang::TemplateArgument']['_is_abstract'] = False
asg['class ::clang::TemplateArgument'].is_copyable = True
subset.append(asg['class ::clang::TemplateArgumentList'])
subset.append(asg['enum ::clang::Type::TypeClass'])
subset.append(asg['enum ::clang::AccessSpecifier'])
subset.append(asg['enum ::clang::LinkageSpecDecl::LanguageIDs'])
subset.append(asg['enum ::clang::BuiltinType::Kind'])
subset.append(asg['enum ::clang::TemplateArgument::ArgKind'])
subset.append(asg['enum ::clang::Decl::Kind'])
subset.extend(asg['::boost::python'].classes(nested = True))
subset.extend(asg['::boost::python'].enumerations(nested = True))
subset.extend(asg.nodes('::clanglite::build_ast_from_code_with_args'))
for node in subset:
node.boost_python_export = True
if autowig.parser.plugin == 'libclang':
for node in (asg.functions(pattern='.*(llvm|clang).*_(begin|end)')
+ asg.functions(pattern='.*(llvm|clang).*getNameAsString')
+ asg.nodes('::clang::NamedDecl::getQualifiedNameAsString')
+ asg.nodes('::clang::ObjCProtocolDecl::collectInheritedProtocolProperties')
+ asg.nodes('::clang::ASTUnit::LoadFromASTFile')
+ asg.nodes('::clang::ASTUnit::getCachedCompletionTypes')
+ asg.nodes('::clang::ASTUnit::getBufferForFile')
+ asg.nodes('::clang::CXXRecordDecl::getCaptureFields')
+ asg.nodes('::clang::ASTContext::SectionInfos')
+ asg.nodes('::clang::ASTContext::getAllocator')
+ asg.nodes('::clang::ASTContext::getObjCEncoding.*')
+ asg.nodes('::clang::ASTContext::getAllocator')
+ asg.nodes('::clang::QualType::getAsString')
+ asg.nodes('::clang::SourceLocation::printToString')
+ asg['class ::llvm::StringRef'].methods()):
node.boost_python_export = False
import sys
from path import path
for header in (path(sys.prefix)/'include'/'clang').walkfiles('*.h'):
asg[header.abspath()].is_external_dependency = False
return asg
autowig.controller['clanglite'] = clanglite_controller
autowig.controller.plugin = 'clanglite'
asg = autowig.controller(asg)
autowig.generator.plugin = 'boost_python_pattern'
wrappers = autowig.generator(asg,
module = srcdir/'_clanglite.cpp',
decorator = srcdir/'clanglite'/'_clanglite.py',
closure = False)
for wrapper in wrappers:
wrapper.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment