PageRenderTime 93ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/tools/build/v2/build/type.py

http://hadesmem.googlecode.com/
Python | 313 lines | 276 code | 10 blank | 27 comment | 6 complexity | a1ca69ce4b0f5c51ca12d0f318063934 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, Apache-2.0, LGPL-3.0
  1. # Status: ported.
  2. # Base revision: 45462.
  3. # Copyright (C) Vladimir Prus 2002. Permission to copy, use, modify, sell and
  4. # distribute this software is granted provided this copyright notice appears in
  5. # all copies. This software is provided "as is" without express or implied
  6. # warranty, and with no claim as to its suitability for any purpose.
  7. import re
  8. import os
  9. import os.path
  10. from b2.util.utility import replace_grist, os_name
  11. from b2.exceptions import *
  12. from b2.build import feature, property, scanner
  13. from b2.util import bjam_signature
  14. __re_hyphen = re.compile ('-')
  15. def __register_features ():
  16. """ Register features need by this module.
  17. """
  18. # The feature is optional so that it is never implicitly added.
  19. # It's used only for internal purposes, and in all cases we
  20. # want to explicitly use it.
  21. feature.feature ('target-type', [], ['composite', 'optional'])
  22. feature.feature ('main-target-type', [], ['optional', 'incidental'])
  23. feature.feature ('base-target-type', [], ['composite', 'optional', 'free'])
  24. def reset ():
  25. """ Clear the module state. This is mainly for testing purposes.
  26. Note that this must be called _after_ resetting the module 'feature'.
  27. """
  28. global __prefixes_suffixes, __suffixes_to_types, __types, __rule_names_to_types, __target_suffixes_cache
  29. __register_features ()
  30. # Stores suffixes for generated targets.
  31. __prefixes_suffixes = [property.PropertyMap(), property.PropertyMap()]
  32. # Maps suffixes to types
  33. __suffixes_to_types = {}
  34. # A map with all the registered types, indexed by the type name
  35. # Each entry is a dictionary with following values:
  36. # 'base': the name of base type or None if type has no base
  37. # 'derived': a list of names of type which derive from this one
  38. # 'scanner': the scanner class registered for this type, if any
  39. __types = {}
  40. # Caches suffixes for targets with certain properties.
  41. __target_suffixes_cache = {}
  42. reset ()
  43. @bjam_signature((["type"], ["suffixes", "*"], ["base_type", "?"]))
  44. def register (type, suffixes = [], base_type = None):
  45. """ Registers a target type, possibly derived from a 'base-type'.
  46. If 'suffixes' are provided, they list all the suffixes that mean a file is of 'type'.
  47. Also, the first element gives the suffix to be used when constructing and object of
  48. 'type'.
  49. type: a string
  50. suffixes: None or a sequence of strings
  51. base_type: None or a string
  52. """
  53. # Type names cannot contain hyphens, because when used as
  54. # feature-values they will be interpreted as composite features
  55. # which need to be decomposed.
  56. if __re_hyphen.search (type):
  57. raise BaseException ('type name "%s" contains a hyphen' % type)
  58. if __types.has_key (type):
  59. raise BaseException ('Type "%s" is already registered.' % type)
  60. entry = {}
  61. entry ['base'] = base_type
  62. entry ['derived'] = []
  63. entry ['scanner'] = None
  64. __types [type] = entry
  65. if base_type:
  66. __types [base_type]['derived'].append (type)
  67. if len (suffixes) > 0:
  68. # Generated targets of 'type' will use the first of 'suffixes'
  69. # (this may be overriden)
  70. set_generated_target_suffix (type, [], suffixes [0])
  71. # Specify mapping from suffixes to type
  72. register_suffixes (suffixes, type)
  73. feature.extend('target-type', [type])
  74. feature.extend('main-target-type', [type])
  75. feature.extend('base-target-type', [type])
  76. if base_type:
  77. feature.compose ('<target-type>' + type, replace_grist (base_type, '<base-target-type>'))
  78. feature.compose ('<base-target-type>' + type, '<base-target-type>' + base_type)
  79. import b2.build.generators as generators
  80. # Adding a new derived type affects generator selection so we need to
  81. # make the generator selection module update any of its cached
  82. # information related to a new derived type being defined.
  83. generators.update_cached_information_with_a_new_type(type)
  84. # FIXME: resolving recursive dependency.
  85. from b2.manager import get_manager
  86. get_manager().projects().project_rules().add_rule_for_type(type)
  87. # FIXME: quick hack.
  88. def type_from_rule_name(rule_name):
  89. return rule_name.upper().replace("-", "_")
  90. def register_suffixes (suffixes, type):
  91. """ Specifies that targets with suffix from 'suffixes' have the type 'type'.
  92. If a different type is already specified for any of syffixes, issues an error.
  93. """
  94. for s in suffixes:
  95. if __suffixes_to_types.has_key (s):
  96. old_type = __suffixes_to_types [s]
  97. if old_type != type:
  98. raise BaseException ('Attempting to specify type for suffix "%s"\nOld type: "%s", New type "%s"' % (s, old_type, type))
  99. else:
  100. __suffixes_to_types [s] = type
  101. def registered (type):
  102. """ Returns true iff type has been registered.
  103. """
  104. return __types.has_key (type)
  105. def validate (type):
  106. """ Issues an error if 'type' is unknown.
  107. """
  108. if not registered (type):
  109. raise BaseException ("Unknown target type '%s'" % type)
  110. def set_scanner (type, scanner):
  111. """ Sets a scanner class that will be used for this 'type'.
  112. """
  113. validate (type)
  114. __types [type]['scanner'] = scanner
  115. def get_scanner (type, prop_set):
  116. """ Returns a scanner instance appropriate to 'type' and 'property_set'.
  117. """
  118. if registered (type):
  119. scanner_type = __types [type]['scanner']
  120. if scanner_type:
  121. return scanner.get (scanner_type, prop_set.raw ())
  122. pass
  123. return None
  124. def base(type):
  125. """Returns a base type for the given type or nothing in case the given type is
  126. not derived."""
  127. return __types[type]['base']
  128. def all_bases (type):
  129. """ Returns type and all of its bases, in the order of their distance from type.
  130. """
  131. result = []
  132. while type:
  133. result.append (type)
  134. type = __types [type]['base']
  135. return result
  136. def all_derived (type):
  137. """ Returns type and all classes that derive from it, in the order of their distance from type.
  138. """
  139. result = [type]
  140. for d in __types [type]['derived']:
  141. result.extend (all_derived (d))
  142. return result
  143. def is_derived (type, base):
  144. """ Returns true if 'type' is 'base' or has 'base' as its direct or indirect base.
  145. """
  146. # TODO: this isn't very efficient, especially for bases close to type
  147. if base in all_bases (type):
  148. return True
  149. else:
  150. return False
  151. def is_subtype (type, base):
  152. """ Same as is_derived. Should be removed.
  153. """
  154. # TODO: remove this method
  155. return is_derived (type, base)
  156. @bjam_signature((["type"], ["properties", "*"], ["suffix"]))
  157. def set_generated_target_suffix (type, properties, suffix):
  158. """ Sets a target suffix that should be used when generating target
  159. of 'type' with the specified properties. Can be called with
  160. empty properties if no suffix for 'type' was specified yet.
  161. This does not automatically specify that files 'suffix' have
  162. 'type' --- two different types can use the same suffix for
  163. generating, but only one type should be auto-detected for
  164. a file with that suffix. User should explicitly specify which
  165. one.
  166. The 'suffix' parameter can be empty string ("") to indicate that
  167. no suffix should be used.
  168. """
  169. set_generated_target_ps(1, type, properties, suffix)
  170. def change_generated_target_suffix (type, properties, suffix):
  171. """ Change the suffix previously registered for this type/properties
  172. combination. If suffix is not yet specified, sets it.
  173. """
  174. change_generated_target_ps(1, type, properties, suffix)
  175. def generated_target_suffix(type, properties):
  176. return generated_target_ps(1, type, properties)
  177. # Sets a target prefix that should be used when generating targets of 'type'
  178. # with the specified properties. Can be called with empty properties if no
  179. # prefix for 'type' has been specified yet.
  180. #
  181. # The 'prefix' parameter can be empty string ("") to indicate that no prefix
  182. # should be used.
  183. #
  184. # Usage example: library names use the "lib" prefix on unix.
  185. @bjam_signature((["type"], ["properties", "*"], ["suffix"]))
  186. def set_generated_target_prefix(type, properties, prefix):
  187. set_generated_target_ps(0, type, properties, prefix)
  188. # Change the prefix previously registered for this type/properties combination.
  189. # If prefix is not yet specified, sets it.
  190. def change_generated_target_prefix(type, properties, prefix):
  191. change_generated_target_ps(0, type, properties, prefix)
  192. def generated_target_prefix(type, properties):
  193. return generated_target_ps(0, type, properties)
  194. def set_generated_target_ps(is_suffix, type, properties, val):
  195. properties.append ('<target-type>' + type)
  196. __prefixes_suffixes[is_suffix].insert (properties, val)
  197. def change_generated_target_ps(is_suffix, type, properties, val):
  198. properties.append ('<target-type>' + type)
  199. prev = __prefixes_suffixes[is_suffix].find_replace(properties, val)
  200. if not prev:
  201. set_generated_target_ps(is_suffix, type, properties, val)
  202. # Returns either prefix or suffix (as indicated by 'is_suffix') that should be used
  203. # when generating a target of 'type' with the specified properties.
  204. # If no prefix/suffix is specified for 'type', returns prefix/suffix for
  205. # base type, if any.
  206. def generated_target_ps_real(is_suffix, type, properties):
  207. result = ''
  208. found = False
  209. while type and not found:
  210. result = __prefixes_suffixes[is_suffix].find (['<target-type>' + type] + properties)
  211. # Note that if the string is empty (""), but not null, we consider
  212. # suffix found. Setting prefix or suffix to empty string is fine.
  213. if result is not None:
  214. found = True
  215. type = __types [type]['base']
  216. if not result:
  217. result = ''
  218. return result
  219. def generated_target_ps(is_suffix, type, prop_set):
  220. """ Returns suffix that should be used when generating target of 'type',
  221. with the specified properties. If not suffix were specified for
  222. 'type', returns suffix for base type, if any.
  223. """
  224. key = (is_suffix, type, prop_set)
  225. v = __target_suffixes_cache.get(key, None)
  226. if not v:
  227. v = generated_target_ps_real(is_suffix, type, prop_set.raw())
  228. __target_suffixes_cache [key] = v
  229. return v
  230. def type(filename):
  231. """ Returns file type given it's name. If there are several dots in filename,
  232. tries each suffix. E.g. for name of "file.so.1.2" suffixes "2", "1", and
  233. "so" will be tried.
  234. """
  235. while 1:
  236. filename, suffix = os.path.splitext (filename)
  237. if not suffix: return None
  238. suffix = suffix[1:]
  239. if __suffixes_to_types.has_key(suffix):
  240. return __suffixes_to_types[suffix]
  241. # NOTE: moved from tools/types/register
  242. def register_type (type, suffixes, base_type = None, os = []):
  243. """ Register the given type on the specified OSes, or on remaining OSes
  244. if os is not specified. This rule is injected into each of the type
  245. modules for the sake of convenience.
  246. """
  247. if registered (type):
  248. return
  249. if not os or os_name () in os:
  250. register (type, suffixes, base_type)