Debian Codegraph @

lib_pypy/cffi/ffiplatform.py

raw symbols SCIP
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 oldir = os.getcwd()
40 # Shorten the names of the sources for msvc's long file names
41 os.chdir(tmpdir)
42 ext.sources = [os.path.relpath(os.path.join(oldir, x)) for x in ext.sources]
43 try:
44 options['debug'] = ('ffiplatform', debug)
45 options['force'] = ('ffiplatform', True)
46 options['build_lib'] = ('ffiplatform', ".")
47 options['build_temp'] = ('ffiplatform', ".")
48 old_level = set_threshold(0) or 0
49 try:
50 set_verbosity(compiler_verbose)
51 dist.run_command('build_ext')
52 cmd_obj = dist.get_command_obj('build_ext')
53 [soname] = cmd_obj.get_outputs()
54 finally:
55 set_threshold(old_level)
56 except (CompileError, LinkError) as e:
57 raise VerificationError('%s: %s' % (e.__class__.__name__, e))
58 finally:
59 os.chdir(oldir)
60 # Since we shortened the file names, make sure the soname is correct
61 return os.path.join(tmpdir, soname)
62
63
64 try:
65 from os.path import samefile
66 except ImportError:
67 def samefile(f1, f2):
68 return os.path.abspath(f1) == os.path.abspath(f2)
69
70 def maybe_relative_path(path):
71 if not os.path.isabs(path):
72 return path # already relative
73 dir = path
74 names = []
75 while True:
76 prevdir = dir
77 dir, name = os.path.split(prevdir)
78 if dir == prevdir or not dir:
79 return path # failed to make it relative
80 names.append(name)
81 try:
82 if samefile(dir, os.curdir):
83 names.reverse()
84 return os.path.join(*names)
85 except OSError:
86 pass
87
88 # ____________________________________________________________
89
90 try:
91 int_or_long = (int, long)
92 import cStringIO
93 except NameError:
94 int_or_long = int # Python 3
95 import io as cStringIO
96
97 def _flatten(x, f):
98 if isinstance(x, str):
99 f.write('%ds%s' % (len(x), x))
100 elif isinstance(x, dict):
101 keys = sorted(x.keys())
102 f.write('%dd' % len(keys))
103 for key in keys:
104 _flatten(key, f)
105 _flatten(x[key], f)
106 elif isinstance(x, (list, tuple)):
107 f.write('%dl' % len(x))
108 for value in x:
109 _flatten(value, f)
110 elif isinstance(x, int_or_long):
111 f.write('%di' % (x,))
112 else:
113 raise TypeError(
114 "the keywords to verify() contains unsupported object %r" % (x,))
115
116 def flatten(x):
117 f = cStringIO.StringIO()
118 _flatten(x, f)
119 return f.getvalue()