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.

227 lines
8.1 KiB

6 years ago
  1. #
  2. # Copyright (c) 2001 - 2017 The SCons Foundation
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included
  13. # in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  16. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #
  23. __revision__ = "src/engine/SCons/PathList.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  24. __doc__ = """SCons.PathList
  25. A module for handling lists of directory paths (the sort of things
  26. that get set as CPPPATH, LIBPATH, etc.) with as much caching of data and
  27. efficiency as we can, while still keeping the evaluation delayed so that we
  28. Do the Right Thing (almost) regardless of how the variable is specified.
  29. """
  30. import os
  31. import SCons.Memoize
  32. import SCons.Node
  33. import SCons.Util
  34. #
  35. # Variables to specify the different types of entries in a PathList object:
  36. #
  37. TYPE_STRING_NO_SUBST = 0 # string with no '$'
  38. TYPE_STRING_SUBST = 1 # string containing '$'
  39. TYPE_OBJECT = 2 # other object
  40. def node_conv(obj):
  41. """
  42. This is the "string conversion" routine that we have our substitutions
  43. use to return Nodes, not strings. This relies on the fact that an
  44. EntryProxy object has a get() method that returns the underlying
  45. Node that it wraps, which is a bit of architectural dependence
  46. that we might need to break or modify in the future in response to
  47. additional requirements.
  48. """
  49. try:
  50. get = obj.get
  51. except AttributeError:
  52. if isinstance(obj, SCons.Node.Node) or SCons.Util.is_Sequence( obj ):
  53. result = obj
  54. else:
  55. result = str(obj)
  56. else:
  57. result = get()
  58. return result
  59. class _PathList(object):
  60. """
  61. An actual PathList object.
  62. """
  63. def __init__(self, pathlist):
  64. """
  65. Initializes a PathList object, canonicalizing the input and
  66. pre-processing it for quicker substitution later.
  67. The stored representation of the PathList is a list of tuples
  68. containing (type, value), where the "type" is one of the TYPE_*
  69. variables defined above. We distinguish between:
  70. strings that contain no '$' and therefore need no
  71. delayed-evaluation string substitution (we expect that there
  72. will be many of these and that we therefore get a pretty
  73. big win from avoiding string substitution)
  74. strings that contain '$' and therefore need substitution
  75. (the hard case is things like '${TARGET.dir}/include',
  76. which require re-evaluation for every target + source)
  77. other objects (which may be something like an EntryProxy
  78. that needs a method called to return a Node)
  79. Pre-identifying the type of each element in the PathList up-front
  80. and storing the type in the list of tuples is intended to reduce
  81. the amount of calculation when we actually do the substitution
  82. over and over for each target.
  83. """
  84. if SCons.Util.is_String(pathlist):
  85. pathlist = pathlist.split(os.pathsep)
  86. elif not SCons.Util.is_Sequence(pathlist):
  87. pathlist = [pathlist]
  88. pl = []
  89. for p in pathlist:
  90. try:
  91. found = '$' in p
  92. except (AttributeError, TypeError):
  93. type = TYPE_OBJECT
  94. else:
  95. if not found:
  96. type = TYPE_STRING_NO_SUBST
  97. else:
  98. type = TYPE_STRING_SUBST
  99. pl.append((type, p))
  100. self.pathlist = tuple(pl)
  101. def __len__(self): return len(self.pathlist)
  102. def __getitem__(self, i): return self.pathlist[i]
  103. def subst_path(self, env, target, source):
  104. """
  105. Performs construction variable substitution on a pre-digested
  106. PathList for a specific target and source.
  107. """
  108. result = []
  109. for type, value in self.pathlist:
  110. if type == TYPE_STRING_SUBST:
  111. value = env.subst(value, target=target, source=source,
  112. conv=node_conv)
  113. if SCons.Util.is_Sequence(value):
  114. result.extend(SCons.Util.flatten(value))
  115. elif value:
  116. result.append(value)
  117. elif type == TYPE_OBJECT:
  118. value = node_conv(value)
  119. if value:
  120. result.append(value)
  121. elif value:
  122. result.append(value)
  123. return tuple(result)
  124. class PathListCache(object):
  125. """
  126. A class to handle caching of PathList lookups.
  127. This class gets instantiated once and then deleted from the namespace,
  128. so it's used as a Singleton (although we don't enforce that in the
  129. usual Pythonic ways). We could have just made the cache a dictionary
  130. in the module namespace, but putting it in this class allows us to
  131. use the same Memoizer pattern that we use elsewhere to count cache
  132. hits and misses, which is very valuable.
  133. Lookup keys in the cache are computed by the _PathList_key() method.
  134. Cache lookup should be quick, so we don't spend cycles canonicalizing
  135. all forms of the same lookup key. For example, 'x:y' and ['x',
  136. 'y'] logically represent the same list, but we don't bother to
  137. split string representations and treat those two equivalently.
  138. (Note, however, that we do, treat lists and tuples the same.)
  139. The main type of duplication we're trying to catch will come from
  140. looking up the same path list from two different clones of the
  141. same construction environment. That is, given
  142. env2 = env1.Clone()
  143. both env1 and env2 will have the same CPPPATH value, and we can
  144. cheaply avoid re-parsing both values of CPPPATH by using the
  145. common value from this cache.
  146. """
  147. def __init__(self):
  148. self._memo = {}
  149. def _PathList_key(self, pathlist):
  150. """
  151. Returns the key for memoization of PathLists.
  152. Note that we want this to be pretty quick, so we don't completely
  153. canonicalize all forms of the same list. For example,
  154. 'dir1:$ROOT/dir2' and ['$ROOT/dir1', 'dir'] may logically
  155. represent the same list if you're executing from $ROOT, but
  156. we're not going to bother splitting strings into path elements,
  157. or massaging strings into Nodes, to identify that equivalence.
  158. We just want to eliminate obvious redundancy from the normal
  159. case of re-using exactly the same cloned value for a path.
  160. """
  161. if SCons.Util.is_Sequence(pathlist):
  162. pathlist = tuple(SCons.Util.flatten(pathlist))
  163. return pathlist
  164. @SCons.Memoize.CountDictCall(_PathList_key)
  165. def PathList(self, pathlist):
  166. """
  167. Returns the cached _PathList object for the specified pathlist,
  168. creating and caching a new object as necessary.
  169. """
  170. pathlist = self._PathList_key(pathlist)
  171. try:
  172. memo_dict = self._memo['PathList']
  173. except KeyError:
  174. memo_dict = {}
  175. self._memo['PathList'] = memo_dict
  176. else:
  177. try:
  178. return memo_dict[pathlist]
  179. except KeyError:
  180. pass
  181. result = _PathList(pathlist)
  182. memo_dict[pathlist] = result
  183. return result
  184. PathList = PathListCache().PathList
  185. del PathListCache
  186. # Local Variables:
  187. # tab-width:4
  188. # indent-tabs-mode:nil
  189. # End:
  190. # vim: set expandtab tabstop=4 shiftwidth=4: