Skip to content

Instantly share code, notes, and snippets.

@rgov
Created September 5, 2012 18:17
Show Gist options
  • Select an option

  • Save rgov/3641765 to your computer and use it in GitHub Desktop.

Select an option

Save rgov/3641765 to your computer and use it in GitHub Desktop.
Using ToT libclang from Python
import os, sys
import subprocess
import ctypes, ctypes.util
llvmconfig = '/Users/rzg/Projects/llvm/build/Release+Asserts/bin/llvm-config'
def getllvmconfig(flag):
return subprocess.check_output([ llvmconfig, '--' + flag ]).rstrip()
# Patch ctypes.cdll.LoadLibrary to respect search paths.
real_LoadLibrary = ctypes.cdll.LoadLibrary
def my_LoadLibrary(lib):
if not lib.startswith('/'):
return real_LoadLibrary(ctypes.util.find_library(lib))
else:
return real_LoadLibrary(lib)
ctypes.cdll.LoadLibrary = my_LoadLibrary
# Import the Clang python bindings.
libpath = getllvmconfig('libdir')
if 'DYLD_LIBRARY_PATH' in os.environ:
libpath += ':' + os.environ['DYLD_LIBRARY_PATH']
os.environ['DYLD_LIBRARY_PATH'] = libpath
sys.path.append(getllvmconfig('src-root') + '/tools/clang/bindings/python')
import clang.cindex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment