1import os
2import re
3import sys
4import subprocess
5
6from numpy.distutils.fcompiler import FCompiler
7from numpy.distutils.exec_command import find_executable
8from numpy.distutils.misc_util import make_temp_file
9from distutils import log
10
11compilers = ['IBMFCompiler']
12
13class IBMFCompiler(FCompiler):
14    compiler_type = 'ibm'
15    description = 'IBM XL Fortran Compiler'
16    version_pattern =  r'(xlf\(1\)\s*|)IBM XL Fortran ((Advanced Edition |)Version |Enterprise Edition V|for AIX, V)(?P<version>[^\s*]*)'
17    #IBM XL Fortran Enterprise Edition V10.1 for AIX \nVersion: 10.01.0000.0004
18
19    executables = {
20        'version_cmd'  : ["<F77>", "-qversion"],
21        'compiler_f77' : ["xlf"],
22        'compiler_fix' : ["xlf90", "-qfixed"],
23        'compiler_f90' : ["xlf90"],
24        'linker_so'    : ["xlf95"],
25        'archiver'     : ["ar", "-cr"],
26        'ranlib'       : ["ranlib"]
27        }
28
29    def get_version(self,*args,**kwds):
30        version = FCompiler.get_version(self,*args,**kwds)
31
32        if version is None and sys.platform.startswith('aix'):
33            # use lslpp to find out xlf version
34            lslpp = find_executable('lslpp')
35            xlf = find_executable('xlf')
36            if os.path.exists(xlf) and os.path.exists(lslpp):
37                try:
38                    o = subprocess.check_output([lslpp, '-Lc', 'xlfcmp'])
39                except (OSError, subprocess.CalledProcessError):
40                    pass
41                else:
42                    m = re.search(r'xlfcmp:(?P<version>\d+([.]\d+)+)', o)
43                    if m: version = m.group('version')
44
45        xlf_dir = '/etc/opt/ibmcmp/xlf'
46        if version is None and os.path.isdir(xlf_dir):
47            # linux:
48            # If the output of xlf does not contain version info
49            # (that's the case with xlf 8.1, for instance) then
50            # let's try another method:
51            l = sorted(os.listdir(xlf_dir))
52            l.reverse()
53            l = [d for d in l if os.path.isfile(os.path.join(xlf_dir, d, 'xlf.cfg'))]
54            if l:
55                from distutils.version import LooseVersion
56                self.version = version = LooseVersion(l[0])
57        return version
58
59    def get_flags(self):
60        return ['-qextname']
61
62    def get_flags_debug(self):
63        return ['-g']
64
65    def get_flags_linker_so(self):
66        opt = []
67        if sys.platform=='darwin':
68            opt.append('-Wl,-bundle,-flat_namespace,-undefined,suppress')
69        else:
70            opt.append('-bshared')
71        version = self.get_version(ok_status=[0, 40])
72        if version is not None:
73            if sys.platform.startswith('aix'):
74                xlf_cfg = '/etc/xlf.cfg'
75            else:
76                xlf_cfg = '/etc/opt/ibmcmp/xlf/%s/xlf.cfg' % version
77            fo, new_cfg = make_temp_file(suffix='_xlf.cfg')
78            log.info('Creating '+new_cfg)
79            with open(xlf_cfg, 'r') as fi:
80                crt1_match = re.compile(r'\s*crt\s*[=]\s*(?P<path>.*)/crt1.o').match
81                for line in fi:
82                    m = crt1_match(line)
83                    if m:
84                        fo.write('crt = %s/bundle1.o\n' % (m.group('path')))
85                    else:
86                        fo.write(line)
87            fo.close()
88            opt.append('-F'+new_cfg)
89        return opt
90
91    def get_flags_opt(self):
92        return ['-O3']
93
94if __name__ == '__main__':
95    from numpy.distutils import customized_fcompiler
96    log.set_verbosity(2)
97    print(customized_fcompiler(compiler='ibm').get_version())
98