| 1 |
import sys, os |
| 2 |
from .error import VerificationError |
| 3 |
|
| 4 |
|
| 5 |
LIST_OF_FILE_NAMES = ['sources', 'include_dirs', 'library_dirs', |
| 6 |
'extra_objects', 'depends'] |
| 7 |
|
| 8 |
def get_extension(srcfilename, modname, sources=(), **kwds): |
| 9 |
from cffi._shimmed_dist_utils import Extension |
| 10 |
allsources = [srcfilename] |
| 11 |
for src in sources: |
| 12 |
allsources.append(os.path.normpath(src)) |
| 13 |
return Extension(name=modname, sources=allsources, **kwds) |
| 14 |
|
| 15 |
def compile(tmpdir, ext, compiler_verbose=0, debug=None): |
| 16 |
"""Compile a C extension module using distutils.""" |
| 17 |
|
| 18 |
saved_environ = os.environ.copy() |
| 19 |
try: |
| 20 |
outputfilename = _build(tmpdir, ext, compiler_verbose, debug) |
| 21 |
outputfilename = os.path.abspath(outputfilename) |
| 22 |
finally: |
| 23 |
# workaround for a distutils bugs where some env vars can |
| 24 |
# become longer and longer every time it is used |
| 25 |
for key, value in saved_environ.items(): |
| 26 |
if os.environ.get(key) != value: |
| 27 |
os.environ[key] = value |
| 28 |
return outputfilename |
| 29 |
|
| 30 |
def _build(tmpdir, ext, compiler_verbose=0, debug=None): |
| 31 |
# XXX compact but horrible :-( |
| 32 |
from cffi._shimmed_dist_utils import Distribution, CompileError, LinkError, set_threshold, set_verbosity |
| 33 |
|
| 34 |
dist = Distribution({'ext_modules': [ext]}) |
| 35 |
dist.parse_config_files() |
| 36 |
options = dist.get_option_dict('build_ext') |
| 37 |
if debug is None: |
| 38 |
debug = sys.flags.debug |
| 39 |
options['debug'] = ('ffiplatform', debug) |
| 40 |
options['force'] = ('ffiplatform', True) |
| 41 |
options['build_lib'] = ('ffiplatform', tmpdir) |
| 42 |
options['build_temp'] = ('ffiplatform', tmpdir) |
| 43 |
# |
| 44 |
try: |
| 45 |
old_level = set_threshold(0) or 0 |
| 46 |
try: |
| 47 |
set_verbosity(compiler_verbose) |
| 48 |
dist.run_command('build_ext') |
| 49 |
cmd_obj = dist.get_command_obj('build_ext') |
| 50 |
[soname] = cmd_obj.get_outputs() |
| 51 |
finally: |
| 52 |
set_threshold(old_level) |
| 53 |
except (CompileError, LinkError) as e: |
| 54 |
raise VerificationError('%s: %s' % (e.__class__.__name__, e)) |
| 55 |
# |
| 56 |
return soname |
| 57 |
|
| 58 |
try: |
| 59 |
from os.path import samefile |
| 60 |
except ImportError: |
| 61 |
def samefile(f1, f2): |
| 62 |
return os.path.abspath(f1) == os.path.abspath(f2) |
| 63 |
|
| 64 |
def maybe_relative_path(path): |
| 65 |
if not os.path.isabs(path): |
| 66 |
return path # already relative |
| 67 |
dir = path |
| 68 |
names = [] |
| 69 |
while True: |
| 70 |
prevdir = dir |
| 71 |
dir, name = os.path.split(prevdir) |
| 72 |
if dir == prevdir or not dir: |
| 73 |
return path # failed to make it relative |
| 74 |
names.append(name) |
| 75 |
try: |
| 76 |
if samefile(dir, os.curdir): |
| 77 |
names.reverse() |
| 78 |
return os.path.join(*names) |
| 79 |
except OSError: |
| 80 |
pass |
| 81 |
|
| 82 |
# ____________________________________________________________ |
| 83 |
|
| 84 |
try: |
| 85 |
int_or_long = (int, long) |
| 86 |
import cStringIO |
| 87 |
except NameError: |
| 88 |
int_or_long = int # Python 3 |
| 89 |
import io as cStringIO |
| 90 |
|
| 91 |
def _flatten(x, f): |
| 92 |
if isinstance(x, str): |
| 93 |
f.write('%ds%s' % (len(x), x)) |
| 94 |
elif isinstance(x, dict): |
| 95 |
keys = sorted(x.keys()) |
| 96 |
f.write('%dd' % len(keys)) |
| 97 |
for key in keys: |
| 98 |
_flatten(key, f) |
| 99 |
_flatten(x[key], f) |
| 100 |
elif isinstance(x, (list, tuple)): |
| 101 |
f.write('%dl' % len(x)) |
| 102 |
for value in x: |
| 103 |
_flatten(value, f) |
| 104 |
elif isinstance(x, int_or_long): |
| 105 |
f.write('%di' % (x,)) |
| 106 |
else: |
| 107 |
raise TypeError( |
| 108 |
"the keywords to verify() contains unsupported object %r" % (x,)) |
| 109 |
|
| 110 |
def flatten(x): |
| 111 |
f = cStringIO.StringIO() |
| 112 |
_flatten(x, f) |
| 113 |
return f.getvalue() |