1 #===- common.py - Python LLVM Bindings -----------------------*- python -*--===#
3 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 # See https://llvm.org/LICENSE.txt for license information.
5 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 #===------------------------------------------------------------------------===#
9 from ctypes
import POINTER
10 from ctypes
import c_void_p
11 from ctypes
import cdll
16 # LLVM_VERSION: sync with PACKAGE_VERSION in autoconf/configure.ac and CMakeLists.txt
17 # but leave out the 'svn' suffix.
25 c_object_p
= POINTER(c_void_p
)
27 class LLVMObject(object):
28 """Base class for objects that are backed by an LLVM data structure.
30 This class should never be instantiated outside of this package.
32 def __init__(self
, ptr
, ownable
=True, disposer
=None):
33 assert isinstance(ptr
, c_object_p
)
35 self
._ptr
= self
._as
_parameter
_ = ptr
37 self
._self
_owned
= True
38 self
._ownable
= ownable
39 self
._disposer
= disposer
41 self
._owned
_objects
= []
43 def take_ownership(self
, obj
):
44 """Take ownership of another object.
46 When you take ownership of another object, you are responsible for
47 destroying that object. In addition, a reference to that object is
48 placed inside this object so the Python garbage collector will not
49 collect the object while it is still alive in libLLVM.
51 This method should likely only be called from within modules inside
54 assert isinstance(obj
, LLVMObject
)
56 self
._owned
_objects
.append(obj
)
57 obj
._self
_owned
= False
60 """ctypes function that converts this object to a function parameter."""
61 return self
._as
_parameter
_
64 if not hasattr(self
, '_self_owned') or not hasattr(self
, '_disposer'):
67 if self
._self
_owned
and self
._disposer
:
70 class CachedProperty(object):
71 """Decorator that caches the result of a property lookup.
73 This is a useful replacement for @property. It is recommended to use this
74 decorator on properties that invoke C API calls for which the result of the
75 call will be idempotent.
77 def __init__(self
, wrapped
):
78 self
.wrapped
= wrapped
80 self
.__doc
__ = wrapped
.__doc
__
81 except: # pragma: no cover
84 def __get__(self
, instance
, instance_type
=None):
88 value
= self
.wrapped(instance
)
89 setattr(instance
, self
.wrapped
.__name
__, value
)
94 """Obtain a reference to the llvm library."""
96 # On Linux, ctypes.cdll.LoadLibrary() respects LD_LIBRARY_PATH
97 # while ctypes.util.find_library() doesn't.
98 # See http://docs.python.org/2/library/ctypes.html#finding-shared-libraries
100 # To make it possible to run the unit tests without installing the LLVM shared
101 # library into a default linker search path. Always Try ctypes.cdll.LoadLibrary()
102 # with all possible library names first, then try ctypes.util.find_library().
104 names
= ['LLVM-' + LLVM_VERSION
, 'LLVM-' + LLVM_VERSION
+ 'svn']
105 t
= platform
.system()
107 pfx
, ext
= 'lib', '.dylib'
109 pfx
, ext
= '', '.dll'
111 pfx
, ext
= 'lib', '.so'
115 lib
= cdll
.LoadLibrary(pfx
+ i
+ ext
)
122 t
= ctypes
.util
.find_library(i
)
124 return cdll
.LoadLibrary(t
)
125 raise Exception('LLVM shared library not found!')