ifort.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # DC 2008
  4. # Thomas Nagy 2016-2018 (ita)
  5. import os, re, traceback
  6. from waflib import Utils, Logs, Errors
  7. from waflib.Tools import fc, fc_config, fc_scan, ar, ccroot
  8. from waflib.Configure import conf
  9. from waflib.TaskGen import after_method, feature
  10. @conf
  11. def find_ifort(conf):
  12. fc = conf.find_program('ifort', var='FC')
  13. conf.get_ifort_version(fc)
  14. conf.env.FC_NAME = 'IFORT'
  15. @conf
  16. def ifort_modifier_win32(self):
  17. v = self.env
  18. v.IFORT_WIN32 = True
  19. v.FCSTLIB_MARKER = ''
  20. v.FCSHLIB_MARKER = ''
  21. v.FCLIB_ST = v.FCSTLIB_ST = '%s.lib'
  22. v.FCLIBPATH_ST = v.STLIBPATH_ST = '/LIBPATH:%s'
  23. v.FCINCPATH_ST = '/I%s'
  24. v.FCDEFINES_ST = '/D%s'
  25. v.fcprogram_PATTERN = v.fcprogram_test_PATTERN = '%s.exe'
  26. v.fcshlib_PATTERN = '%s.dll'
  27. v.fcstlib_PATTERN = v.implib_PATTERN = '%s.lib'
  28. v.FCLNK_TGT_F = '/out:'
  29. v.FC_TGT_F = ['/c', '/o', '']
  30. v.FCFLAGS_fcshlib = ''
  31. v.LINKFLAGS_fcshlib = '/DLL'
  32. v.AR_TGT_F = '/out:'
  33. v.IMPLIB_ST = '/IMPLIB:%s'
  34. v.append_value('LINKFLAGS', '/subsystem:console')
  35. if v.IFORT_MANIFEST:
  36. v.append_value('LINKFLAGS', ['/MANIFEST'])
  37. @conf
  38. def ifort_modifier_darwin(conf):
  39. fc_config.fortran_modifier_darwin(conf)
  40. @conf
  41. def ifort_modifier_platform(conf):
  42. dest_os = conf.env.DEST_OS or Utils.unversioned_sys_platform()
  43. ifort_modifier_func = getattr(conf, 'ifort_modifier_' + dest_os, None)
  44. if ifort_modifier_func:
  45. ifort_modifier_func()
  46. @conf
  47. def get_ifort_version(conf, fc):
  48. """
  49. Detects the compiler version and sets ``conf.env.FC_VERSION``
  50. """
  51. version_re = re.compile(r"\bIntel\b.*\bVersion\s*(?P<major>\d*)\.(?P<minor>\d*)",re.I).search
  52. if Utils.is_win32:
  53. cmd = fc
  54. else:
  55. cmd = fc + ['-logo']
  56. out, err = fc_config.getoutput(conf, cmd, stdin=False)
  57. match = version_re(out) or version_re(err)
  58. if not match:
  59. conf.fatal('cannot determine ifort version.')
  60. k = match.groupdict()
  61. conf.env.FC_VERSION = (k['major'], k['minor'])
  62. def configure(conf):
  63. """
  64. Detects the Intel Fortran compilers
  65. """
  66. if Utils.is_win32:
  67. compiler, version, path, includes, libdirs, arch = conf.detect_ifort()
  68. v = conf.env
  69. v.DEST_CPU = arch
  70. v.PATH = path
  71. v.INCLUDES = includes
  72. v.LIBPATH = libdirs
  73. v.MSVC_COMPILER = compiler
  74. try:
  75. v.MSVC_VERSION = float(version)
  76. except ValueError:
  77. v.MSVC_VERSION = float(version[:-3])
  78. conf.find_ifort_win32()
  79. conf.ifort_modifier_win32()
  80. else:
  81. conf.find_ifort()
  82. conf.find_program('xiar', var='AR')
  83. conf.find_ar()
  84. conf.fc_flags()
  85. conf.fc_add_flags()
  86. conf.ifort_modifier_platform()
  87. all_ifort_platforms = [ ('intel64', 'amd64'), ('em64t', 'amd64'), ('ia32', 'x86'), ('Itanium', 'ia64')]
  88. """List of icl platforms"""
  89. @conf
  90. def gather_ifort_versions(conf, versions):
  91. """
  92. List compiler versions by looking up registry keys
  93. """
  94. version_pattern = re.compile('^...?.?\....?.?')
  95. try:
  96. all_versions = Utils.winreg.OpenKey(Utils.winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Wow6432node\\Intel\\Compilers\\Fortran')
  97. except OSError:
  98. try:
  99. all_versions = Utils.winreg.OpenKey(Utils.winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Intel\\Compilers\\Fortran')
  100. except OSError:
  101. return
  102. index = 0
  103. while 1:
  104. try:
  105. version = Utils.winreg.EnumKey(all_versions, index)
  106. except OSError:
  107. break
  108. index += 1
  109. if not version_pattern.match(version):
  110. continue
  111. targets = {}
  112. for target,arch in all_ifort_platforms:
  113. if target=='intel64':
  114. targetDir='EM64T_NATIVE'
  115. else:
  116. targetDir=target
  117. try:
  118. Utils.winreg.OpenKey(all_versions,version+'\\'+targetDir)
  119. icl_version=Utils.winreg.OpenKey(all_versions,version)
  120. path,type=Utils.winreg.QueryValueEx(icl_version,'ProductDir')
  121. except OSError:
  122. pass
  123. else:
  124. batch_file=os.path.join(path,'bin','ifortvars.bat')
  125. if os.path.isfile(batch_file):
  126. targets[target] = target_compiler(conf, 'intel', arch, version, target, batch_file)
  127. for target,arch in all_ifort_platforms:
  128. try:
  129. icl_version = Utils.winreg.OpenKey(all_versions, version+'\\'+target)
  130. path,type = Utils.winreg.QueryValueEx(icl_version,'ProductDir')
  131. except OSError:
  132. continue
  133. else:
  134. batch_file=os.path.join(path,'bin','ifortvars.bat')
  135. if os.path.isfile(batch_file):
  136. targets[target] = target_compiler(conf, 'intel', arch, version, target, batch_file)
  137. major = version[0:2]
  138. versions['intel ' + major] = targets
  139. @conf
  140. def setup_ifort(conf, versiondict):
  141. """
  142. Checks installed compilers and targets and returns the first combination from the user's
  143. options, env, or the global supported lists that checks.
  144. :param versiondict: dict(platform -> dict(architecture -> configuration))
  145. :type versiondict: dict(string -> dict(string -> target_compiler)
  146. :return: the compiler, revision, path, include dirs, library paths and target architecture
  147. :rtype: tuple of strings
  148. """
  149. platforms = Utils.to_list(conf.env.MSVC_TARGETS) or [i for i,j in all_ifort_platforms]
  150. desired_versions = conf.env.MSVC_VERSIONS or list(reversed(list(versiondict.keys())))
  151. for version in desired_versions:
  152. try:
  153. targets = versiondict[version]
  154. except KeyError:
  155. continue
  156. for arch in platforms:
  157. try:
  158. cfg = targets[arch]
  159. except KeyError:
  160. continue
  161. cfg.evaluate()
  162. if cfg.is_valid:
  163. compiler,revision = version.rsplit(' ', 1)
  164. return compiler,revision,cfg.bindirs,cfg.incdirs,cfg.libdirs,cfg.cpu
  165. conf.fatal('ifort: Impossible to find a valid architecture for building %r - %r' % (desired_versions, list(versiondict.keys())))
  166. @conf
  167. def get_ifort_version_win32(conf, compiler, version, target, vcvars):
  168. # FIXME hack
  169. try:
  170. conf.msvc_cnt += 1
  171. except AttributeError:
  172. conf.msvc_cnt = 1
  173. batfile = conf.bldnode.make_node('waf-print-msvc-%d.bat' % conf.msvc_cnt)
  174. batfile.write("""@echo off
  175. set INCLUDE=
  176. set LIB=
  177. call "%s" %s
  178. echo PATH=%%PATH%%
  179. echo INCLUDE=%%INCLUDE%%
  180. echo LIB=%%LIB%%;%%LIBPATH%%
  181. """ % (vcvars,target))
  182. sout = conf.cmd_and_log(['cmd.exe', '/E:on', '/V:on', '/C', batfile.abspath()])
  183. batfile.delete()
  184. lines = sout.splitlines()
  185. if not lines[0]:
  186. lines.pop(0)
  187. MSVC_PATH = MSVC_INCDIR = MSVC_LIBDIR = None
  188. for line in lines:
  189. if line.startswith('PATH='):
  190. path = line[5:]
  191. MSVC_PATH = path.split(';')
  192. elif line.startswith('INCLUDE='):
  193. MSVC_INCDIR = [i for i in line[8:].split(';') if i]
  194. elif line.startswith('LIB='):
  195. MSVC_LIBDIR = [i for i in line[4:].split(';') if i]
  196. if None in (MSVC_PATH, MSVC_INCDIR, MSVC_LIBDIR):
  197. conf.fatal('ifort: Could not find a valid architecture for building (get_ifort_version_win32)')
  198. # Check if the compiler is usable at all.
  199. # The detection may return 64-bit versions even on 32-bit systems, and these would fail to run.
  200. env = dict(os.environ)
  201. env.update(PATH = path)
  202. compiler_name, linker_name, lib_name = _get_prog_names(conf, compiler)
  203. fc = conf.find_program(compiler_name, path_list=MSVC_PATH)
  204. # delete CL if exists. because it could contain parameters which can change cl's behaviour rather catastrophically.
  205. if 'CL' in env:
  206. del(env['CL'])
  207. try:
  208. conf.cmd_and_log(fc + ['/help'], env=env)
  209. except UnicodeError:
  210. st = traceback.format_exc()
  211. if conf.logger:
  212. conf.logger.error(st)
  213. conf.fatal('ifort: Unicode error - check the code page?')
  214. except Exception as e:
  215. Logs.debug('ifort: get_ifort_version: %r %r %r -> failure %s', compiler, version, target, str(e))
  216. conf.fatal('ifort: cannot run the compiler in get_ifort_version (run with -v to display errors)')
  217. else:
  218. Logs.debug('ifort: get_ifort_version: %r %r %r -> OK', compiler, version, target)
  219. finally:
  220. conf.env[compiler_name] = ''
  221. return (MSVC_PATH, MSVC_INCDIR, MSVC_LIBDIR)
  222. class target_compiler(object):
  223. """
  224. Wraps a compiler configuration; call evaluate() to determine
  225. whether the configuration is usable.
  226. """
  227. def __init__(self, ctx, compiler, cpu, version, bat_target, bat, callback=None):
  228. """
  229. :param ctx: configuration context to use to eventually get the version environment
  230. :param compiler: compiler name
  231. :param cpu: target cpu
  232. :param version: compiler version number
  233. :param bat_target: ?
  234. :param bat: path to the batch file to run
  235. :param callback: optional function to take the realized environment variables tup and map it (e.g. to combine other constant paths)
  236. """
  237. self.conf = ctx
  238. self.name = None
  239. self.is_valid = False
  240. self.is_done = False
  241. self.compiler = compiler
  242. self.cpu = cpu
  243. self.version = version
  244. self.bat_target = bat_target
  245. self.bat = bat
  246. self.callback = callback
  247. def evaluate(self):
  248. if self.is_done:
  249. return
  250. self.is_done = True
  251. try:
  252. vs = self.conf.get_ifort_version_win32(self.compiler, self.version, self.bat_target, self.bat)
  253. except Errors.ConfigurationError:
  254. self.is_valid = False
  255. return
  256. if self.callback:
  257. vs = self.callback(self, vs)
  258. self.is_valid = True
  259. (self.bindirs, self.incdirs, self.libdirs) = vs
  260. def __str__(self):
  261. return str((self.bindirs, self.incdirs, self.libdirs))
  262. def __repr__(self):
  263. return repr((self.bindirs, self.incdirs, self.libdirs))
  264. @conf
  265. def detect_ifort(self):
  266. return self.setup_ifort(self.get_ifort_versions(False))
  267. @conf
  268. def get_ifort_versions(self, eval_and_save=True):
  269. """
  270. :return: platforms to compiler configurations
  271. :rtype: dict
  272. """
  273. dct = {}
  274. self.gather_ifort_versions(dct)
  275. return dct
  276. def _get_prog_names(self, compiler):
  277. if compiler=='intel':
  278. compiler_name = 'ifort'
  279. linker_name = 'XILINK'
  280. lib_name = 'XILIB'
  281. else:
  282. # assumes CL.exe
  283. compiler_name = 'CL'
  284. linker_name = 'LINK'
  285. lib_name = 'LIB'
  286. return compiler_name, linker_name, lib_name
  287. @conf
  288. def find_ifort_win32(conf):
  289. # the autodetection is supposed to be performed before entering in this method
  290. v = conf.env
  291. path = v.PATH
  292. compiler = v.MSVC_COMPILER
  293. version = v.MSVC_VERSION
  294. compiler_name, linker_name, lib_name = _get_prog_names(conf, compiler)
  295. v.IFORT_MANIFEST = (compiler == 'intel' and version >= 11)
  296. # compiler
  297. fc = conf.find_program(compiler_name, var='FC', path_list=path)
  298. # before setting anything, check if the compiler is really intel fortran
  299. env = dict(conf.environ)
  300. if path:
  301. env.update(PATH = ';'.join(path))
  302. if not conf.cmd_and_log(fc + ['/nologo', '/help'], env=env):
  303. conf.fatal('not intel fortran compiler could not be identified')
  304. v.FC_NAME = 'IFORT'
  305. if not v.LINK_FC:
  306. conf.find_program(linker_name, var='LINK_FC', path_list=path, mandatory=True)
  307. if not v.AR:
  308. conf.find_program(lib_name, path_list=path, var='AR', mandatory=True)
  309. v.ARFLAGS = ['/nologo']
  310. # manifest tool. Not required for VS 2003 and below. Must have for VS 2005 and later
  311. if v.IFORT_MANIFEST:
  312. conf.find_program('MT', path_list=path, var='MT')
  313. v.MTFLAGS = ['/nologo']
  314. try:
  315. conf.load('winres')
  316. except Errors.WafError:
  317. Logs.warn('Resource compiler not found. Compiling resource file is disabled')
  318. #######################################################################################################
  319. ##### conf above, build below
  320. @after_method('apply_link')
  321. @feature('fc')
  322. def apply_flags_ifort(self):
  323. """
  324. Adds additional flags implied by msvc, such as subsystems and pdb files::
  325. def build(bld):
  326. bld.stlib(source='main.c', target='bar', subsystem='gruik')
  327. """
  328. if not self.env.IFORT_WIN32 or not getattr(self, 'link_task', None):
  329. return
  330. is_static = isinstance(self.link_task, ccroot.stlink_task)
  331. subsystem = getattr(self, 'subsystem', '')
  332. if subsystem:
  333. subsystem = '/subsystem:%s' % subsystem
  334. flags = is_static and 'ARFLAGS' or 'LINKFLAGS'
  335. self.env.append_value(flags, subsystem)
  336. if not is_static:
  337. for f in self.env.LINKFLAGS:
  338. d = f.lower()
  339. if d[1:] == 'debug':
  340. pdbnode = self.link_task.outputs[0].change_ext('.pdb')
  341. self.link_task.outputs.append(pdbnode)
  342. if getattr(self, 'install_task', None):
  343. self.pdb_install_task = self.add_install_files(install_to=self.install_task.install_to, install_from=pdbnode)
  344. break
  345. @feature('fcprogram', 'fcshlib', 'fcprogram_test')
  346. @after_method('apply_link')
  347. def apply_manifest_ifort(self):
  348. """
  349. Enables manifest embedding in Fortran DLLs when using ifort on Windows
  350. See: http://msdn2.microsoft.com/en-us/library/ms235542(VS.80).aspx
  351. """
  352. if self.env.IFORT_WIN32 and getattr(self, 'link_task', None):
  353. # it seems ifort.exe cannot be called for linking
  354. self.link_task.env.FC = self.env.LINK_FC
  355. if self.env.IFORT_WIN32 and self.env.IFORT_MANIFEST and getattr(self, 'link_task', None):
  356. out_node = self.link_task.outputs[0]
  357. man_node = out_node.parent.find_or_declare(out_node.name + '.manifest')
  358. self.link_task.outputs.append(man_node)
  359. self.env.DO_MANIFEST = True