Created
September 5, 2012 18:17
-
-
Save rgov/3641765 to your computer and use it in GitHub Desktop.
Using ToT libclang from Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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