fc.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #! /usr/bin/env python
  2. # encoding: utf-8
  3. # DC 2008
  4. # Thomas Nagy 2016-2018 (ita)
  5. """
  6. Fortran support
  7. """
  8. from waflib import Utils, Task, Errors
  9. from waflib.Tools import ccroot, fc_config, fc_scan
  10. from waflib.TaskGen import extension
  11. from waflib.Configure import conf
  12. ccroot.USELIB_VARS['fc'] = set(['FCFLAGS', 'DEFINES', 'INCLUDES', 'FCPPFLAGS'])
  13. ccroot.USELIB_VARS['fcprogram_test'] = ccroot.USELIB_VARS['fcprogram'] = set(['LIB', 'STLIB', 'LIBPATH', 'STLIBPATH', 'LINKFLAGS', 'RPATH', 'LINKDEPS'])
  14. ccroot.USELIB_VARS['fcshlib'] = set(['LIB', 'STLIB', 'LIBPATH', 'STLIBPATH', 'LINKFLAGS', 'RPATH', 'LINKDEPS'])
  15. ccroot.USELIB_VARS['fcstlib'] = set(['ARFLAGS', 'LINKDEPS'])
  16. @extension('.f','.F','.f90','.F90','.for','.FOR','.f95','.F95','.f03','.F03','.f08','.F08')
  17. def fc_hook(self, node):
  18. "Binds the Fortran file extensions create :py:class:`waflib.Tools.fc.fc` instances"
  19. return self.create_compiled_task('fc', node)
  20. @conf
  21. def modfile(conf, name):
  22. """
  23. Turns a module name into the right module file name.
  24. Defaults to all lower case.
  25. """
  26. return {'lower' :name.lower() + '.mod',
  27. 'lower.MOD' :name.lower() + '.MOD',
  28. 'UPPER.mod' :name.upper() + '.mod',
  29. 'UPPER' :name.upper() + '.MOD'}[conf.env.FC_MOD_CAPITALIZATION or 'lower']
  30. def get_fortran_tasks(tsk):
  31. """
  32. Obtains all fortran tasks from the same build group. Those tasks must not have
  33. the attribute 'nomod' or 'mod_fortran_done'
  34. :return: a list of :py:class:`waflib.Tools.fc.fc` instances
  35. """
  36. bld = tsk.generator.bld
  37. tasks = bld.get_tasks_group(bld.get_group_idx(tsk.generator))
  38. return [x for x in tasks if isinstance(x, fc) and not getattr(x, 'nomod', None) and not getattr(x, 'mod_fortran_done', None)]
  39. class fc(Task.Task):
  40. """
  41. Fortran tasks can only run when all fortran tasks in a current task group are ready to be executed
  42. This may cause a deadlock if some fortran task is waiting for something that cannot happen (circular dependency)
  43. Should this ever happen, set the 'nomod=True' on those tasks instances to break the loop
  44. """
  45. color = 'GREEN'
  46. run_str = '${FC} ${FCFLAGS} ${FCINCPATH_ST:INCPATHS} ${FCDEFINES_ST:DEFINES} ${_FCMODOUTFLAGS} ${FC_TGT_F}${TGT[0].abspath()} ${FC_SRC_F}${SRC[0].abspath()} ${FCPPFLAGS}'
  47. vars = ["FORTRANMODPATHFLAG"]
  48. def scan(self):
  49. """Fortran dependency scanner"""
  50. tmp = fc_scan.fortran_parser(self.generator.includes_nodes)
  51. tmp.task = self
  52. tmp.start(self.inputs[0])
  53. return (tmp.nodes, tmp.names)
  54. def runnable_status(self):
  55. """
  56. Sets the mod file outputs and the dependencies on the mod files over all Fortran tasks
  57. executed by the main thread so there are no concurrency issues
  58. """
  59. if getattr(self, 'mod_fortran_done', None):
  60. return super(fc, self).runnable_status()
  61. # now, if we reach this part it is because this fortran task is the first in the list
  62. bld = self.generator.bld
  63. # obtain the fortran tasks
  64. lst = get_fortran_tasks(self)
  65. # disable this method for other tasks
  66. for tsk in lst:
  67. tsk.mod_fortran_done = True
  68. # wait for all the .f tasks to be ready for execution
  69. # and ensure that the scanners are called at least once
  70. for tsk in lst:
  71. ret = tsk.runnable_status()
  72. if ret == Task.ASK_LATER:
  73. # we have to wait for one of the other fortran tasks to be ready
  74. # this may deadlock if there are dependencies between fortran tasks
  75. # but this should not happen (we are setting them here!)
  76. for x in lst:
  77. x.mod_fortran_done = None
  78. return Task.ASK_LATER
  79. ins = Utils.defaultdict(set)
  80. outs = Utils.defaultdict(set)
  81. # the .mod files to create
  82. for tsk in lst:
  83. key = tsk.uid()
  84. for x in bld.raw_deps[key]:
  85. if x.startswith('MOD@'):
  86. name = bld.modfile(x.replace('MOD@', ''))
  87. node = bld.srcnode.find_or_declare(name)
  88. tsk.set_outputs(node)
  89. outs[node].add(tsk)
  90. # the .mod files to use
  91. for tsk in lst:
  92. key = tsk.uid()
  93. for x in bld.raw_deps[key]:
  94. if x.startswith('USE@'):
  95. name = bld.modfile(x.replace('USE@', ''))
  96. node = bld.srcnode.find_resource(name)
  97. if node and node not in tsk.outputs:
  98. if not node in bld.node_deps[key]:
  99. bld.node_deps[key].append(node)
  100. ins[node].add(tsk)
  101. # if the intersection matches, set the order
  102. for k in ins.keys():
  103. for a in ins[k]:
  104. a.run_after.update(outs[k])
  105. # the scanner cannot output nodes, so we have to set them
  106. # ourselves as task.dep_nodes (additional input nodes)
  107. tmp = []
  108. for t in outs[k]:
  109. tmp.extend(t.outputs)
  110. a.dep_nodes.extend(tmp)
  111. a.dep_nodes.sort(key=lambda x: x.abspath())
  112. # the task objects have changed: clear the signature cache
  113. for tsk in lst:
  114. try:
  115. delattr(tsk, 'cache_sig')
  116. except AttributeError:
  117. pass
  118. return super(fc, self).runnable_status()
  119. class fcprogram(ccroot.link_task):
  120. """Links Fortran programs"""
  121. color = 'YELLOW'
  122. run_str = '${FC} ${LINKFLAGS} ${FCLNK_SRC_F}${SRC} ${FCLNK_TGT_F}${TGT[0].abspath()} ${RPATH_ST:RPATH} ${FCSTLIB_MARKER} ${FCSTLIBPATH_ST:STLIBPATH} ${FCSTLIB_ST:STLIB} ${FCSHLIB_MARKER} ${FCLIBPATH_ST:LIBPATH} ${FCLIB_ST:LIB} ${LDFLAGS}'
  123. inst_to = '${BINDIR}'
  124. class fcshlib(fcprogram):
  125. """Links Fortran libraries"""
  126. inst_to = '${LIBDIR}'
  127. class fcstlib(ccroot.stlink_task):
  128. """Links Fortran static libraries (uses ar by default)"""
  129. pass # do not remove the pass statement
  130. class fcprogram_test(fcprogram):
  131. """Custom link task to obtain compiler outputs for Fortran configuration tests"""
  132. def runnable_status(self):
  133. """This task is always executed"""
  134. ret = super(fcprogram_test, self).runnable_status()
  135. if ret == Task.SKIP_ME:
  136. ret = Task.RUN_ME
  137. return ret
  138. def exec_command(self, cmd, **kw):
  139. """Stores the compiler std our/err onto the build context, to bld.out + bld.err"""
  140. bld = self.generator.bld
  141. kw['shell'] = isinstance(cmd, str)
  142. kw['stdout'] = kw['stderr'] = Utils.subprocess.PIPE
  143. kw['cwd'] = self.get_cwd()
  144. bld.out = bld.err = ''
  145. bld.to_log('command: %s\n' % cmd)
  146. kw['output'] = 0
  147. try:
  148. (bld.out, bld.err) = bld.cmd_and_log(cmd, **kw)
  149. except Errors.WafError:
  150. return -1
  151. if bld.out:
  152. bld.to_log('out: %s\n' % bld.out)
  153. if bld.err:
  154. bld.to_log('err: %s\n' % bld.err)