| 1 |
import os |
| 2 |
import sys |
| 3 |
import sysconfig |
| 4 |
|
| 5 |
try: |
| 6 |
basestring |
| 7 |
except NameError: |
| 8 |
# Python 3.x |
| 9 |
basestring = str |
| 10 |
|
| 11 |
def error(msg): |
| 12 |
from cffi._shimmed_dist_utils import DistutilsSetupError |
| 13 |
raise DistutilsSetupError(msg) |
| 14 |
|
| 15 |
|
| 16 |
def execfile(filename, glob): |
| 17 |
# We use execfile() (here rewritten for Python 3) instead of |
| 18 |
# __import__() to load the build script. The problem with |
| 19 |
# a normal import is that in some packages, the intermediate |
| 20 |
# __init__.py files may already try to import the file that |
| 21 |
# we are generating. |
| 22 |
with open(filename) as f: |
| 23 |
src = f.read() |
| 24 |
src += '\n' # Python 2.6 compatibility |
| 25 |
code = compile(src, filename, 'exec') |
| 26 |
exec(code, glob, glob) |
| 27 |
|
| 28 |
|
| 29 |
def add_cffi_module(dist, mod_spec): |
| 30 |
from cffi.api import FFI |
| 31 |
|
| 32 |
if not isinstance(mod_spec, basestring): |
| 33 |
error("argument to 'cffi_modules=...' must be a str or a list of str," |
| 34 |
" not %r" % (type(mod_spec).__name__,)) |
| 35 |
mod_spec = str(mod_spec) |
| 36 |
try: |
| 37 |
build_file_name, ffi_var_name = mod_spec.split(':') |
| 38 |
except ValueError: |
| 39 |
error("%r must be of the form 'path/build.py:ffi_variable'" % |
| 40 |
(mod_spec,)) |
| 41 |
if not os.path.exists(build_file_name): |
| 42 |
ext = '' |
| 43 |
rewritten = build_file_name.replace('.', '/') + '.py' |
| 44 |
if os.path.exists(rewritten): |
| 45 |
ext = ' (rewrite cffi_modules to [%r])' % ( |
| 46 |
rewritten + ':' + ffi_var_name,) |
| 47 |
error("%r does not name an existing file%s" % (build_file_name, ext)) |
| 48 |
|
| 49 |
mod_vars = {'__name__': '__cffi__', '__file__': build_file_name} |
| 50 |
execfile(build_file_name, mod_vars) |
| 51 |
|
| 52 |
try: |
| 53 |
ffi = mod_vars[ffi_var_name] |
| 54 |
except KeyError: |
| 55 |
error("%r: object %r not found in module" % (mod_spec, |
| 56 |
ffi_var_name)) |
| 57 |
if not isinstance(ffi, FFI): |
| 58 |
ffi = ffi() # maybe it's a function instead of directly an ffi |
| 59 |
if not isinstance(ffi, FFI): |
| 60 |
error("%r is not an FFI instance (got %r)" % (mod_spec, |
| 61 |
type(ffi).__name__)) |
| 62 |
if not hasattr(ffi, '_assigned_source'): |
| 63 |
error("%r: the set_source() method was not called" % (mod_spec,)) |
| 64 |
module_name, source, source_extension, kwds = ffi._assigned_source |
| 65 |
if ffi._windows_unicode: |
| 66 |
kwds = kwds.copy() |
| 67 |
ffi._apply_windows_unicode(kwds) |
| 68 |
|
| 69 |
if source is None: |
| 70 |
_add_py_module(dist, ffi, module_name) |
| 71 |
else: |
| 72 |
_add_c_module(dist, ffi, module_name, source, source_extension, kwds) |
| 73 |
|
| 74 |
def _set_py_limited_api(Extension, kwds): |
| 75 |
""" |
| 76 |
Add py_limited_api to kwds if setuptools >= 26 is in use. |
| 77 |
Do not alter the setting if it already exists. |
| 78 |
Setuptools takes care of ignoring the flag on Python 2 and PyPy. |
| 79 |
|
| 80 |
CPython itself should ignore the flag in a debugging version |
| 81 |
(by not listing .abi3.so in the extensions it supports), but |
| 82 |
it doesn't so far, creating troubles. That's why we check |
| 83 |
for "not hasattr(sys, 'gettotalrefcount')" (the 2.7 compatible equivalent |
| 84 |
of 'd' not in sys.abiflags). (http://bugs.python.org/issue28401) |
| 85 |
|
| 86 |
On Windows, with CPython <= 3.4, it's better not to use py_limited_api |
| 87 |
because virtualenv *still* doesn't copy PYTHON3.DLL on these versions. |
| 88 |
Recently (2020) we started shipping only >= 3.5 wheels, though. So |
| 89 |
we'll give it another try and set py_limited_api on Windows >= 3.5. |
| 90 |
""" |
| 91 |
from cffi._shimmed_dist_utils import log |
| 92 |
from cffi import recompiler |
| 93 |
|
| 94 |
if ('py_limited_api' not in kwds and not hasattr(sys, 'gettotalrefcount') |
| 95 |
and recompiler.USE_LIMITED_API): |
| 96 |
import setuptools |
| 97 |
try: |
| 98 |
setuptools_major_version = int(setuptools.__version__.partition('.')[0]) |
| 99 |
if setuptools_major_version >= 26: |
| 100 |
kwds['py_limited_api'] = True |
| 101 |
except ValueError: # certain development versions of setuptools |
| 102 |
# If we don't know the version number of setuptools, we |
| 103 |
# try to set 'py_limited_api' anyway. At worst, we get a |
| 104 |
# warning. |
| 105 |
kwds['py_limited_api'] = True |
| 106 |
|
| 107 |
if sysconfig.get_config_var("Py_GIL_DISABLED"): |
| 108 |
if sys.version_info < (3, 15): |
| 109 |
if kwds.get('py_limited_api'): |
| 110 |
log.info("Ignoring py_limited_api=True for free-threaded build.") |
| 111 |
kwds['py_limited_api'] = False |
| 112 |
else: |
| 113 |
kwds.setdefault("define_macros", []).append(("Py_TARGET_ABI3T", "0x030f0000")) |
| 114 |
|
| 115 |
if kwds.get('py_limited_api') is False: |
| 116 |
# avoid setting Py_LIMITED_API if py_limited_api=False |
| 117 |
# which _cffi_include.h does unless _CFFI_NO_LIMITED_API is defined |
| 118 |
kwds.setdefault("define_macros", []).append(("_CFFI_NO_LIMITED_API", None)) |
| 119 |
return kwds |
| 120 |
|
| 121 |
def _add_c_module(dist, ffi, module_name, source, source_extension, kwds): |
| 122 |
# We are a setuptools extension. Need this build_ext for py_limited_api. |
| 123 |
from setuptools.command.build_ext import build_ext |
| 124 |
from cffi._shimmed_dist_utils import Extension, log, mkpath |
| 125 |
from cffi import recompiler |
| 126 |
|
| 127 |
allsources = ['$PLACEHOLDER'] |
| 128 |
allsources.extend(kwds.pop('sources', [])) |
| 129 |
kwds = _set_py_limited_api(Extension, kwds) |
| 130 |
ext = Extension(name=module_name, sources=allsources, **kwds) |
| 131 |
|
| 132 |
def make_mod(tmpdir, pre_run=None): |
| 133 |
c_file = os.path.join(tmpdir, module_name + source_extension) |
| 134 |
log.info("generating cffi module %r" % c_file) |
| 135 |
mkpath(tmpdir) |
| 136 |
# a setuptools-only, API-only hook: called with the "ext" and "ffi" |
| 137 |
# arguments just before we turn the ffi into C code. To use it, |
| 138 |
# subclass the 'distutils.command.build_ext.build_ext' class and |
| 139 |
# add a method 'def pre_run(self, ext, ffi)'. |
| 140 |
if pre_run is not None: |
| 141 |
pre_run(ext, ffi) |
| 142 |
updated = recompiler.make_c_source(ffi, module_name, source, c_file) |
| 143 |
if not updated: |
| 144 |
log.info("already up-to-date") |
| 145 |
return c_file |
| 146 |
|
| 147 |
if dist.ext_modules is None: |
| 148 |
dist.ext_modules = [] |
| 149 |
dist.ext_modules.append(ext) |
| 150 |
|
| 151 |
base_class = dist.cmdclass.get('build_ext', build_ext) |
| 152 |
class build_ext_make_mod(base_class): |
| 153 |
def run(self): |
| 154 |
if ext.sources[0] == '$PLACEHOLDER': |
| 155 |
pre_run = getattr(self, 'pre_run', None) |
| 156 |
ext.sources[0] = make_mod(self.build_temp, pre_run) |
| 157 |
base_class.run(self) |
| 158 |
dist.cmdclass['build_ext'] = build_ext_make_mod |
| 159 |
# NB. multiple runs here will create multiple 'build_ext_make_mod' |
| 160 |
# classes. Even in this case the 'build_ext' command should be |
| 161 |
# run once; but just in case, the logic above does nothing if |
| 162 |
# called again. |
| 163 |
|
| 164 |
|
| 165 |
def _add_py_module(dist, ffi, module_name): |
| 166 |
from setuptools.command.build_py import build_py |
| 167 |
from setuptools.command.build_ext import build_ext |
| 168 |
from cffi._shimmed_dist_utils import log, mkpath |
| 169 |
from cffi import recompiler |
| 170 |
|
| 171 |
def generate_mod(py_file): |
| 172 |
log.info("generating cffi module %r" % py_file) |
| 173 |
mkpath(os.path.dirname(py_file)) |
| 174 |
updated = recompiler.make_py_source(ffi, module_name, py_file) |
| 175 |
if not updated: |
| 176 |
log.info("already up-to-date") |
| 177 |
|
| 178 |
base_class = dist.cmdclass.get('build_py', build_py) |
| 179 |
class build_py_make_mod(base_class): |
| 180 |
def run(self): |
| 181 |
base_class.run(self) |
| 182 |
module_path = module_name.split('.') |
| 183 |
module_path[-1] += '.py' |
| 184 |
generate_mod(os.path.join(self.build_lib, *module_path)) |
| 185 |
def get_source_files(self): |
| 186 |
# This is called from 'setup.py sdist' only. Exclude |
| 187 |
# the generate .py module in this case. |
| 188 |
saved_py_modules = self.py_modules |
| 189 |
try: |
| 190 |
if saved_py_modules: |
| 191 |
self.py_modules = [m for m in saved_py_modules |
| 192 |
if m != module_name] |
| 193 |
return base_class.get_source_files(self) |
| 194 |
finally: |
| 195 |
self.py_modules = saved_py_modules |
| 196 |
dist.cmdclass['build_py'] = build_py_make_mod |
| 197 |
|
| 198 |
# distutils and setuptools have no notion I could find of a |
| 199 |
# generated python module. If we don't add module_name to |
| 200 |
# dist.py_modules, then things mostly work but there are some |
| 201 |
# combination of options (--root and --record) that will miss |
| 202 |
# the module. So we add it here, which gives a few apparently |
| 203 |
# harmless warnings about not finding the file outside the |
| 204 |
# build directory. |
| 205 |
# Then we need to hack more in get_source_files(); see above. |
| 206 |
if dist.py_modules is None: |
| 207 |
dist.py_modules = [] |
| 208 |
dist.py_modules.append(module_name) |
| 209 |
|
| 210 |
# the following is only for "build_ext -i" |
| 211 |
base_class_2 = dist.cmdclass.get('build_ext', build_ext) |
| 212 |
class build_ext_make_mod(base_class_2): |
| 213 |
def run(self): |
| 214 |
base_class_2.run(self) |
| 215 |
if self.inplace: |
| 216 |
# from get_ext_fullpath() in distutils/command/build_ext.py |
| 217 |
module_path = module_name.split('.') |
| 218 |
package = '.'.join(module_path[:-1]) |
| 219 |
build_py = self.get_finalized_command('build_py') |
| 220 |
package_dir = build_py.get_package_dir(package) |
| 221 |
file_name = module_path[-1] + '.py' |
| 222 |
generate_mod(os.path.join(package_dir, file_name)) |
| 223 |
dist.cmdclass['build_ext'] = build_ext_make_mod |
| 224 |
|
| 225 |
def cffi_modules(dist, attr, value): |
| 226 |
assert attr == 'cffi_modules' |
| 227 |
if isinstance(value, basestring): |
| 228 |
value = [value] |
| 229 |
|
| 230 |
for cffi_module in value: |
| 231 |
add_cffi_module(dist, cffi_module) |