File: build.py

package info (click to toggle)
mpi4py 2.0.0-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,680 kB
  • sloc: python: 15,291; ansic: 7,099; makefile: 719; f90: 158; sh: 156; cpp: 121
file content (54 lines) | stat: -rw-r--r-- 1,640 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
import os
import cffi

ffi = cffi.FFI()
with open("libmpi.c.in") as f:
    ffi.set_source("libmpi", f.read())
with open("libmpi.h") as f:
    ffi.cdef(f.read())

class mpicompiler(object):

    from cffi import ffiplatform

    def __init__(self, cc, ld=None):
        self.cc = cc
        self.ld = ld if ld else cc
        self.ffi_compile = self.ffiplatform.compile

    def __enter__(self):
        self.ffiplatform.compile = self.compile

    def __exit__(self, *args):
        self.ffiplatform.compile = self.ffi_compile

    def configure(self, compiler):
        from distutils.util import split_quoted
        from distutils.spawn import find_executable
        def fix_command(command, cmd):
            if not cmd: return
            cmd = split_quoted(cmd)
            exe = find_executable(cmd[0])
            if not exe: return
            command[0] = exe
            command += cmd[1:]
        fix_command(compiler.compiler_so, self.cc)
        fix_command(compiler.linker_so, self.ld)

    def compile(self, *args, **kargs):
        from distutils.command import build_ext
        customize_compiler_orig = build_ext.customize_compiler
        def customize_compiler(compiler):
            customize_compiler_orig(compiler)
            self.configure(compiler)
        build_ext.customize_compiler = customize_compiler
        try:
            return self.ffi_compile(*args, **kargs)
        finally:
            build_ext.customize_compiler = customize_compiler_orig

if __name__ == '__main__':
    cc = os.environ.get('MPICC', 'mpicc')
    ld = os.environ.get('MPILD')
    with mpicompiler(cc, ld):
        ffi.compile()