File: cbuild.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (342 lines) | stat: -rw-r--r-- 13,497 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import py
import sys, os, subprocess

from rpython.translator.platform import host
from rpython.translator import cdir
from rpython.tool.udir import udir


class ExternalCompilationInfo(object):

    _ATTRIBUTES = ['pre_include_bits', 'includes', 'include_dirs',
                   'post_include_bits', 'libraries', 'library_dirs',
                   'separate_module_sources', 'separate_module_files',
                   'compile_extra', 'link_extra',
                   'frameworks', 'link_files', 'testonly_libraries']
    _DUPLICATES_OK = ['compile_extra', 'link_extra']
    _EXTRA_ATTRIBUTES = ['use_cpp_linker', 'platform']

    def __init__(self,
                 pre_include_bits        = [],
                 includes                = [],
                 include_dirs            = [],
                 post_include_bits       = [],
                 libraries               = [],
                 library_dirs            = [],
                 separate_module_sources = [],
                 separate_module_files   = [],
                 compile_extra           = [],
                 link_extra              = [],
                 frameworks              = [],
                 link_files              = [],
                 testonly_libraries      = [],
                 use_cpp_linker          = False,
                 platform                = None):
        """
        pre_include_bits: list of pieces of text that should be put at the top
        of the generated .c files, before any #include.  They shouldn't
        contain an #include themselves.  (Duplicate pieces are removed.)

        includes: list of .h file names to be #include'd from the
        generated .c files.

        include_dirs: list of dir names that is passed to the C compiler

        post_include_bits: list of pieces of text that should be put at the top
        of the generated .c files, after the #includes.  (Duplicate pieces are
        removed.)

        libraries: list of library names that is passed to the linker

        library_dirs: list of dir names that is passed to the linker

        separate_module_sources: list of multiline strings that are
        each written to a .c file and compiled separately and linked
        later on.  (If function prototypes are needed for other .c files
        to access this, they can be put in post_include_bits.)

        separate_module_files: list of .c file names that are compiled
        separately and linked later on.  (If an .h file is needed for
        other .c files to access this, it can be put in includes.)

        (export_symbols: killed; you need, depending on the case, to
        add the RPY_EXTERN or RPY_EXPORTED macro just before the
        declaration of each function in the C header file, as explained
        in translator/c/src/precommondefs.h; or you need the decorator
        @rlib.entrypoint.export_symbol)

        compile_extra: list of parameters which will be directly passed to
        the compiler

        link_extra: list of parameters which will be directly passed to
        the linker

        frameworks: list of Mac OS X frameworks which should passed to the
        linker. Use this instead of the 'libraries' parameter if you want to
        link to a framework bundle. Not suitable for unix-like .dylib
        installations.

        link_files: list of file names which will be directly passed to the
        linker

        testonly_libraries: list of libraries that are searched for during
        testing only, by ll2ctypes.  Useful to search for a name in a dynamic
        library during testing but use the static library for compilation.

        use_cpp_linker: a flag to tell if g++ should be used instead of gcc
        when linking (a bit custom so far)

        platform: an object that can identify the platform
        """
        for name in self._ATTRIBUTES:
            value = locals()[name]
            assert isinstance(value, (list, tuple))
            setattr(self, name, tuple(value))
        self.use_cpp_linker = use_cpp_linker
        self._platform = platform

    @property
    def platform(self):
        if self._platform is None:
            from rpython.translator.platform import platform
            return platform
        return self._platform

    @classmethod
    def from_compiler_flags(cls, flags):
        """Returns a new ExternalCompilationInfo instance by parsing
        the string 'flags', which is in the typical Unix compiler flags
        format."""
        pre_include_bits = []
        include_dirs = []
        compile_extra = []
        for arg in flags.split():
            if arg.startswith('-I'):
                include_dirs.append(arg[2:])
            elif arg.startswith('-D'):
                macro = arg[2:]
                if '=' in macro:
                    macro, value = macro.split('=')
                else:
                    value = '1'
                pre_include_bits.append('#define %s %s' % (macro, value))
            elif arg.startswith('-L') or arg.startswith('-l'):
                raise ValueError('linker flag found in compiler options: %r'
                                 % (arg,))
            else:
                compile_extra.append(arg)
        return cls(pre_include_bits=pre_include_bits,
                   include_dirs=include_dirs,
                   compile_extra=compile_extra)

    @classmethod
    def from_linker_flags(cls, flags):
        """Returns a new ExternalCompilationInfo instance by parsing
        the string 'flags', which is in the typical Unix linker flags
        format."""
        libraries = []
        library_dirs = []
        link_extra = []
        for arg in flags.split():
            if arg.startswith('-L'):
                library_dirs.append(arg[2:])
            elif arg.startswith('-l'):
                libraries.append(arg[2:])
            elif arg.startswith('-I') or arg.startswith('-D'):
                raise ValueError('compiler flag found in linker options: %r'
                                 % (arg,))
            else:
                link_extra.append(arg)
        return cls(libraries=libraries,
                   library_dirs=library_dirs,
                   link_extra=link_extra)

    @classmethod
    def from_config_tool(cls, execonfigtool):
        """Returns a new ExternalCompilationInfo instance by executing
        the 'execonfigtool' with --cflags and --libs arguments."""
        path = py.path.local.sysfind(execonfigtool)
        if not path:
            raise ImportError("cannot find %r" % (execonfigtool,))
            # we raise ImportError to be nice to the pypy.config.pypyoption
            # logic of skipping modules depending on non-installed libs
        return cls._run_config_tool('"%s"' % (str(path),))

    @classmethod
    def from_pkg_config(cls, pkgname):
        """Returns a new ExternalCompilationInfo instance by executing
        'pkg-config <pkgname>' with --cflags and --libs arguments."""
        assert isinstance(pkgname, str)
        try:
            popen = subprocess.Popen(['pkg-config', pkgname, '--exists'])
            result = popen.wait()
        except OSError:
            result = -1
        if result != 0:
            raise ImportError("failed: 'pkg-config %s --exists'" % pkgname)
        return cls._run_config_tool('pkg-config "%s"' % pkgname)

    @classmethod
    def _run_config_tool(cls, command):
        cflags = py.process.cmdexec('%s --cflags' % command)
        eci1 = cls.from_compiler_flags(cflags)
        libs = py.process.cmdexec('%s --libs' % command)
        eci2 = cls.from_linker_flags(libs)
        return eci1.merge(eci2)

    def _value(self):
        return tuple([getattr(self, x)
                          for x in self._ATTRIBUTES + self._EXTRA_ATTRIBUTES])

    def __hash__(self):
        return hash(self._value())

    def __eq__(self, other):
        return self.__class__ is other.__class__ and \
               self._value() == other._value()

    def __ne__(self, other):
        return not self == other

    def __repr__(self):
        info = []
        for attr in self._ATTRIBUTES + self._EXTRA_ATTRIBUTES:
            val = getattr(self, attr)
            info.append("%s=%s" % (attr, repr(val)))
        return "<ExternalCompilationInfo (%s)>" % ", ".join(info)

    def merge(self, *others):
        def unique_elements(l):
            seen = set()
            new_objs = []
            for obj in l:
                if obj not in seen:
                    new_objs.append(obj)
                    seen.add(obj)
            return new_objs
        others = unique_elements(list(others))

        attrs = {}
        for name in self._ATTRIBUTES:
            if name in self._DUPLICATES_OK:
                s = []
                for i in [self] + others:
                    s += getattr(i, name)
                attrs[name] = s
            else:
                s = set()
                attr = []
                for one in [self] + others:
                    for elem in getattr(one, name):
                        if elem not in s:
                            s.add(elem)
                            attr.append(elem)
                attrs[name] = attr
        use_cpp_linker = self.use_cpp_linker
        for other in others:
            use_cpp_linker = use_cpp_linker or other.use_cpp_linker
        attrs['use_cpp_linker'] = use_cpp_linker
        for other in others:
            if other.platform != self.platform:
                raise Exception("Mixing ECI for different platforms %s and %s"%
                                (other.platform, self.platform))
        attrs['platform'] = self.platform
        return ExternalCompilationInfo(**attrs)

    def write_c_header(self, fileobj):
        f = open(os.path.join(cdir, 'src', 'precommondefs.h'))
        fileobj.write(f.read())
        f.close()
        print >> fileobj
        for piece in self.pre_include_bits:
            print >> fileobj, piece
        for path in self.includes:
            print >> fileobj, '#include <%s>' % (path,)
        for piece in self.post_include_bits:
            print >> fileobj, piece

    def _copy_attributes(self):
        d = {}
        for attr in self._ATTRIBUTES + self._EXTRA_ATTRIBUTES:
            d[attr] = getattr(self, attr)
        return d

    def convert_sources_to_files(self, cache_dir=None):
        if not self.separate_module_sources:
            return self
        if cache_dir is None:
            cache_dir = udir.join('module_cache').ensure(dir=1)
        num = 0
        files = []
        for source in self.separate_module_sources:
            while 1:
                filename = cache_dir.join('module_%d.c' % num)
                num += 1
                if not filename.check():
                    break
            f = filename.open("w")
            self.write_c_header(f)
            source = str(source)
            f.write(source)
            if not source.endswith('\n'):
                f.write('\n')
            f.close()
            files.append(str(filename))
        d = self._copy_attributes()
        d['separate_module_sources'] = ()
        d['separate_module_files'] += tuple(files)
        return ExternalCompilationInfo(**d)

    def get_module_files(self):
        d = self._copy_attributes()
        files = d['separate_module_files']
        d['separate_module_files'] = ()
        return files, ExternalCompilationInfo(**d)

    def compile_shared_lib(self, outputfilename=None, ignore_a_files=False,
                           debug_mode=True, defines=[]):
        self = self.convert_sources_to_files()
        if ignore_a_files:
            if not [fn for fn in self.link_files if fn.endswith('.a')]:
                ignore_a_files = False    # there are none
        if not self.separate_module_files and not ignore_a_files:
            return self    # xxx there was some condition about win32 here
        else:
            #basepath = py.path.local(self.separate_module_files[0]).dirpath()
            basepath = udir.join('shared_cache')
        if outputfilename is None:
            # find more or less unique name there
            pth = basepath.join('externmod').new(ext=host.so_ext)
            num = 0
            while pth.check():
                pth = basepath.join(
                    'externmod_%d' % (num,)).new(ext=host.so_ext)
                num += 1
            basepath.ensure(dir=1)
            outputfilename = str(pth.dirpath().join(pth.purebasename))

        d = self._copy_attributes()
        if ignore_a_files:
            d['link_files'] = [fn for fn in d['link_files']
                                  if not fn.endswith('.a')]
        if debug_mode and sys.platform != 'win32':
            d['compile_extra'] = d['compile_extra'] + ('-g', '-O0')
        d['compile_extra'] = d['compile_extra'] + (
            '-DRPY_EXTERN=RPY_EXPORTED',)
        for define in defines:
            d['compile_extra'] += ('-D%s' % define,)
        self = ExternalCompilationInfo(**d)

        lib = str(host.compile([], self, outputfilename=outputfilename,
                               standalone=False))
        d = self._copy_attributes()
        d['libraries'] += (lib,)
        d['separate_module_files'] = ()
        d['separate_module_sources'] = ()
        return ExternalCompilationInfo(**d)

    def copy_without(self, *names):
        d = self._copy_attributes()
        for name in names:
            del d[name]
        return ExternalCompilationInfo(**d)