ibm.py 3.5 KB

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