You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
14 KiB

6 years ago
  1. """SCons.Scanner.Fortran
  2. This module implements the dependency scanner for Fortran code.
  3. """
  4. #
  5. # Copyright (c) 2001 - 2017 The SCons Foundation
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. __revision__ = "src/engine/SCons/Scanner/Fortran.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  26. import re
  27. import SCons.Node
  28. import SCons.Node.FS
  29. import SCons.Scanner
  30. import SCons.Util
  31. import SCons.Warnings
  32. class F90Scanner(SCons.Scanner.Classic):
  33. """
  34. A Classic Scanner subclass for Fortran source files which takes
  35. into account both USE and INCLUDE statements. This scanner will
  36. work for both F77 and F90 (and beyond) compilers.
  37. Currently, this scanner assumes that the include files do not contain
  38. USE statements. To enable the ability to deal with USE statements
  39. in include files, add logic right after the module names are found
  40. to loop over each include file, search for and locate each USE
  41. statement, and append each module name to the list of dependencies.
  42. Caching the search results in a common dictionary somewhere so that
  43. the same include file is not searched multiple times would be a
  44. smart thing to do.
  45. """
  46. def __init__(self, name, suffixes, path_variable,
  47. use_regex, incl_regex, def_regex, *args, **kw):
  48. self.cre_use = re.compile(use_regex, re.M)
  49. self.cre_incl = re.compile(incl_regex, re.M)
  50. self.cre_def = re.compile(def_regex, re.M)
  51. def _scan(node, env, path, self=self):
  52. node = node.rfile()
  53. if not node.exists():
  54. return []
  55. return self.scan(node, env, path)
  56. kw['function'] = _scan
  57. kw['path_function'] = SCons.Scanner.FindPathDirs(path_variable)
  58. kw['recursive'] = 1
  59. kw['skeys'] = suffixes
  60. kw['name'] = name
  61. SCons.Scanner.Current.__init__(self, *args, **kw)
  62. def scan(self, node, env, path=()):
  63. # cache the includes list in node so we only scan it once:
  64. if node.includes != None:
  65. mods_and_includes = node.includes
  66. else:
  67. # retrieve all included filenames
  68. includes = self.cre_incl.findall(node.get_text_contents())
  69. # retrieve all USE'd module names
  70. modules = self.cre_use.findall(node.get_text_contents())
  71. # retrieve all defined module names
  72. defmodules = self.cre_def.findall(node.get_text_contents())
  73. # Remove all USE'd module names that are defined in the same file
  74. # (case-insensitively)
  75. d = {}
  76. for m in defmodules:
  77. d[m.lower()] = 1
  78. modules = [m for m in modules if m.lower() not in d]
  79. # Convert module name to a .mod filename
  80. suffix = env.subst('$FORTRANMODSUFFIX')
  81. modules = [x.lower() + suffix for x in modules]
  82. # Remove unique items from the list
  83. mods_and_includes = SCons.Util.unique(includes+modules)
  84. node.includes = mods_and_includes
  85. # This is a hand-coded DSU (decorate-sort-undecorate, or
  86. # Schwartzian transform) pattern. The sort key is the raw name
  87. # of the file as specifed on the USE or INCLUDE line, which lets
  88. # us keep the sort order constant regardless of whether the file
  89. # is actually found in a Repository or locally.
  90. nodes = []
  91. source_dir = node.get_dir()
  92. if callable(path):
  93. path = path()
  94. for dep in mods_and_includes:
  95. n, i = self.find_include(dep, source_dir, path)
  96. if n is None:
  97. SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
  98. "No dependency generated for file: %s (referenced by: %s) -- file not found" % (i, node))
  99. else:
  100. sortkey = self.sort_key(dep)
  101. nodes.append((sortkey, n))
  102. return [pair[1] for pair in sorted(nodes)]
  103. def FortranScan(path_variable="FORTRANPATH"):
  104. """Return a prototype Scanner instance for scanning source files
  105. for Fortran USE & INCLUDE statements"""
  106. # The USE statement regex matches the following:
  107. #
  108. # USE module_name
  109. # USE :: module_name
  110. # USE, INTRINSIC :: module_name
  111. # USE, NON_INTRINSIC :: module_name
  112. #
  113. # Limitations
  114. #
  115. # -- While the regex can handle multiple USE statements on one line,
  116. # it cannot properly handle them if they are commented out.
  117. # In either of the following cases:
  118. #
  119. # ! USE mod_a ; USE mod_b [entire line is commented out]
  120. # USE mod_a ! ; USE mod_b [in-line comment of second USE statement]
  121. #
  122. # the second module name (mod_b) will be picked up as a dependency
  123. # even though it should be ignored. The only way I can see
  124. # to rectify this would be to modify the scanner to eliminate
  125. # the call to re.findall, read in the contents of the file,
  126. # treating the comment character as an end-of-line character
  127. # in addition to the normal linefeed, loop over each line,
  128. # weeding out the comments, and looking for the USE statements.
  129. # One advantage to this is that the regex passed to the scanner
  130. # would no longer need to match a semicolon.
  131. #
  132. # -- I question whether or not we need to detect dependencies to
  133. # INTRINSIC modules because these are built-in to the compiler.
  134. # If we consider them a dependency, will SCons look for them, not
  135. # find them, and kill the build? Or will we there be standard
  136. # compiler-specific directories we will need to point to so the
  137. # compiler and SCons can locate the proper object and mod files?
  138. # Here is a breakdown of the regex:
  139. #
  140. # (?i) : regex is case insensitive
  141. # ^ : start of line
  142. # (?: : group a collection of regex symbols without saving the match as a "group"
  143. # ^|; : matches either the start of the line or a semicolon - semicolon
  144. # ) : end the unsaved grouping
  145. # \s* : any amount of white space
  146. # USE : match the string USE, case insensitive
  147. # (?: : group a collection of regex symbols without saving the match as a "group"
  148. # \s+| : match one or more whitespace OR .... (the next entire grouped set of regex symbols)
  149. # (?: : group a collection of regex symbols without saving the match as a "group"
  150. # (?: : establish another unsaved grouping of regex symbols
  151. # \s* : any amount of white space
  152. # , : match a comma
  153. # \s* : any amount of white space
  154. # (?:NON_)? : optionally match the prefix NON_, case insensitive
  155. # INTRINSIC : match the string INTRINSIC, case insensitive
  156. # )? : optionally match the ", INTRINSIC/NON_INTRINSIC" grouped expression
  157. # \s* : any amount of white space
  158. # :: : match a double colon that must appear after the INTRINSIC/NON_INTRINSIC attribute
  159. # ) : end the unsaved grouping
  160. # ) : end the unsaved grouping
  161. # \s* : match any amount of white space
  162. # (\w+) : match the module name that is being USE'd
  163. #
  164. #
  165. use_regex = "(?i)(?:^|;)\s*USE(?:\s+|(?:(?:\s*,\s*(?:NON_)?INTRINSIC)?\s*::))\s*(\w+)"
  166. # The INCLUDE statement regex matches the following:
  167. #
  168. # INCLUDE 'some_Text'
  169. # INCLUDE "some_Text"
  170. # INCLUDE "some_Text" ; INCLUDE "some_Text"
  171. # INCLUDE kind_"some_Text"
  172. # INCLUDE kind_'some_Text"
  173. #
  174. # where some_Text can include any alphanumeric and/or special character
  175. # as defined by the Fortran 2003 standard.
  176. #
  177. # Limitations:
  178. #
  179. # -- The Fortran standard dictates that a " or ' in the INCLUDE'd
  180. # string must be represented as a "" or '', if the quotes that wrap
  181. # the entire string are either a ' or ", respectively. While the
  182. # regular expression below can detect the ' or " characters just fine,
  183. # the scanning logic, presently is unable to detect them and reduce
  184. # them to a single instance. This probably isn't an issue since,
  185. # in practice, ' or " are not generally used in filenames.
  186. #
  187. # -- This regex will not properly deal with multiple INCLUDE statements
  188. # when the entire line has been commented out, ala
  189. #
  190. # ! INCLUDE 'some_file' ; INCLUDE 'some_file'
  191. #
  192. # In such cases, it will properly ignore the first INCLUDE file,
  193. # but will actually still pick up the second. Interestingly enough,
  194. # the regex will properly deal with these cases:
  195. #
  196. # INCLUDE 'some_file'
  197. # INCLUDE 'some_file' !; INCLUDE 'some_file'
  198. #
  199. # To get around the above limitation, the FORTRAN programmer could
  200. # simply comment each INCLUDE statement separately, like this
  201. #
  202. # ! INCLUDE 'some_file' !; INCLUDE 'some_file'
  203. #
  204. # The way I see it, the only way to get around this limitation would
  205. # be to modify the scanning logic to replace the calls to re.findall
  206. # with a custom loop that processes each line separately, throwing
  207. # away fully commented out lines before attempting to match against
  208. # the INCLUDE syntax.
  209. #
  210. # Here is a breakdown of the regex:
  211. #
  212. # (?i) : regex is case insensitive
  213. # (?: : begin a non-saving group that matches the following:
  214. # ^ : either the start of the line
  215. # | : or
  216. # ['">]\s*; : a semicolon that follows a single quote,
  217. # double quote or greater than symbol (with any
  218. # amount of whitespace in between). This will
  219. # allow the regex to match multiple INCLUDE
  220. # statements per line (although it also requires
  221. # the positive lookahead assertion that is
  222. # used below). It will even properly deal with
  223. # (i.e. ignore) cases in which the additional
  224. # INCLUDES are part of an in-line comment, ala
  225. # " INCLUDE 'someFile' ! ; INCLUDE 'someFile2' "
  226. # ) : end of non-saving group
  227. # \s* : any amount of white space
  228. # INCLUDE : match the string INCLUDE, case insensitive
  229. # \s+ : match one or more white space characters
  230. # (?\w+_)? : match the optional "kind-param _" prefix allowed by the standard
  231. # [<"'] : match the include delimiter - an apostrophe, double quote, or less than symbol
  232. # (.+?) : match one or more characters that make up
  233. # the included path and file name and save it
  234. # in a group. The Fortran standard allows for
  235. # any non-control character to be used. The dot
  236. # operator will pick up any character, including
  237. # control codes, but I can't conceive of anyone
  238. # putting control codes in their file names.
  239. # The question mark indicates it is non-greedy so
  240. # that regex will match only up to the next quote,
  241. # double quote, or greater than symbol
  242. # (?=["'>]) : positive lookahead assertion to match the include
  243. # delimiter - an apostrophe, double quote, or
  244. # greater than symbol. This level of complexity
  245. # is required so that the include delimiter is
  246. # not consumed by the match, thus allowing the
  247. # sub-regex discussed above to uniquely match a
  248. # set of semicolon-separated INCLUDE statements
  249. # (as allowed by the F2003 standard)
  250. include_regex = """(?i)(?:^|['">]\s*;)\s*INCLUDE\s+(?:\w+_)?[<"'](.+?)(?=["'>])"""
  251. # The MODULE statement regex finds module definitions by matching
  252. # the following:
  253. #
  254. # MODULE module_name
  255. #
  256. # but *not* the following:
  257. #
  258. # MODULE PROCEDURE procedure_name
  259. #
  260. # Here is a breakdown of the regex:
  261. #
  262. # (?i) : regex is case insensitive
  263. # ^\s* : any amount of white space
  264. # MODULE : match the string MODULE, case insensitive
  265. # \s+ : match one or more white space characters
  266. # (?!PROCEDURE) : but *don't* match if the next word matches
  267. # PROCEDURE (negative lookahead assertion),
  268. # case insensitive
  269. # (\w+) : match one or more alphanumeric characters
  270. # that make up the defined module name and
  271. # save it in a group
  272. def_regex = """(?i)^\s*MODULE\s+(?!PROCEDURE)(\w+)"""
  273. scanner = F90Scanner("FortranScan",
  274. "$FORTRANSUFFIXES",
  275. path_variable,
  276. use_regex,
  277. include_regex,
  278. def_regex)
  279. return scanner
  280. # Local Variables:
  281. # tab-width:4
  282. # indent-tabs-mode:nil
  283. # End:
  284. # vim: set expandtab tabstop=4 shiftwidth=4: