ibm.py 3.5 KB

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