BBHandler.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. """
  5. class for handling .bb files
  6. Reads a .bb file and obtains its metadata
  7. """
  8. # Copyright (C) 2003, 2004 Chris Larson
  9. # Copyright (C) 2003, 2004 Phil Blundell
  10. #
  11. # SPDX-License-Identifier: GPL-2.0-only
  12. #
  13. import re, bb, os
  14. import logging
  15. import bb.build, bb.utils
  16. from bb import data
  17. from . import ConfHandler
  18. from .. import resolve_file, ast, logger, ParseError
  19. from .ConfHandler import include, init
  20. # For compatibility
  21. bb.deprecate_import(__name__, "bb.parse", ["vars_from_file"])
  22. __func_start_regexp__ = re.compile(r"(((?P<py>python)|(?P<fr>fakeroot))\s*)*(?P<func>[\w\.\-\+\{\}\$]+)?\s*\(\s*\)\s*{$" )
  23. __inherit_regexp__ = re.compile(r"inherit\s+(.+)" )
  24. __export_func_regexp__ = re.compile(r"EXPORT_FUNCTIONS\s+(.+)" )
  25. __addtask_regexp__ = re.compile(r"addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
  26. __deltask_regexp__ = re.compile(r"deltask\s+(?P<func>\w+)(?P<ignores>.*)")
  27. __addhandler_regexp__ = re.compile(r"addhandler\s+(.+)" )
  28. __def_regexp__ = re.compile(r"def\s+(\w+).*:" )
  29. __python_func_regexp__ = re.compile(r"(\s+.*)|(^$)|(^#)" )
  30. __python_tab_regexp__ = re.compile(r" *\t")
  31. __infunc__ = []
  32. __inpython__ = False
  33. __body__ = []
  34. __classname__ = ""
  35. cached_statements = {}
  36. def supports(fn, d):
  37. """Return True if fn has a supported extension"""
  38. return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
  39. def inherit(files, fn, lineno, d):
  40. __inherit_cache = d.getVar('__inherit_cache', False) or []
  41. files = d.expand(files).split()
  42. for file in files:
  43. if not os.path.isabs(file) and not file.endswith(".bbclass"):
  44. file = os.path.join('classes', '%s.bbclass' % file)
  45. if not os.path.isabs(file):
  46. bbpath = d.getVar("BBPATH")
  47. abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
  48. for af in attempts:
  49. if af != abs_fn:
  50. bb.parse.mark_dependency(d, af)
  51. if abs_fn:
  52. file = abs_fn
  53. if not file in __inherit_cache:
  54. logger.debug(1, "Inheriting %s (from %s:%d)" % (file, fn, lineno))
  55. __inherit_cache.append( file )
  56. d.setVar('__inherit_cache', __inherit_cache)
  57. include(fn, file, lineno, d, "inherit")
  58. __inherit_cache = d.getVar('__inherit_cache', False) or []
  59. def get_statements(filename, absolute_filename, base_name):
  60. global cached_statements
  61. try:
  62. return cached_statements[absolute_filename]
  63. except KeyError:
  64. with open(absolute_filename, 'r') as f:
  65. statements = ast.StatementGroup()
  66. lineno = 0
  67. while True:
  68. lineno = lineno + 1
  69. s = f.readline()
  70. if not s: break
  71. s = s.rstrip()
  72. feeder(lineno, s, filename, base_name, statements)
  73. if __inpython__:
  74. # add a blank line to close out any python definition
  75. feeder(lineno, "", filename, base_name, statements, eof=True)
  76. if filename.endswith(".bbclass") or filename.endswith(".inc"):
  77. cached_statements[absolute_filename] = statements
  78. return statements
  79. def handle(fn, d, include):
  80. global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
  81. __body__ = []
  82. __infunc__ = []
  83. __classname__ = ""
  84. __residue__ = []
  85. base_name = os.path.basename(fn)
  86. (root, ext) = os.path.splitext(base_name)
  87. init(d)
  88. if ext == ".bbclass":
  89. __classname__ = root
  90. __inherit_cache = d.getVar('__inherit_cache', False) or []
  91. if not fn in __inherit_cache:
  92. __inherit_cache.append(fn)
  93. d.setVar('__inherit_cache', __inherit_cache)
  94. if include != 0:
  95. oldfile = d.getVar('FILE', False)
  96. else:
  97. oldfile = None
  98. abs_fn = resolve_file(fn, d)
  99. # actual loading
  100. statements = get_statements(fn, abs_fn, base_name)
  101. # DONE WITH PARSING... time to evaluate
  102. if ext != ".bbclass" and abs_fn != oldfile:
  103. d.setVar('FILE', abs_fn)
  104. try:
  105. statements.eval(d)
  106. except bb.parse.SkipRecipe:
  107. d.setVar("__SKIPPED", True)
  108. if include == 0:
  109. return { "" : d }
  110. if __infunc__:
  111. raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
  112. if __residue__:
  113. raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
  114. if ext != ".bbclass" and include == 0:
  115. return ast.multi_finalize(fn, d)
  116. if ext != ".bbclass" and oldfile and abs_fn != oldfile:
  117. d.setVar("FILE", oldfile)
  118. return d
  119. def feeder(lineno, s, fn, root, statements, eof=False):
  120. global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __def_regexp__, __python_func_regexp__, __inpython__, __infunc__, __body__, bb, __residue__, __classname__
  121. # Check tabs in python functions:
  122. # - def py_funcname(): covered by __inpython__
  123. # - python(): covered by '__anonymous' == __infunc__[0]
  124. # - python funcname(): covered by __infunc__[3]
  125. if __inpython__ or (__infunc__ and ('__anonymous' == __infunc__[0] or __infunc__[3])):
  126. tab = __python_tab_regexp__.match(s)
  127. if tab:
  128. bb.warn('python should use 4 spaces indentation, but found tabs in %s, line %s' % (root, lineno))
  129. if __infunc__:
  130. if s == '}':
  131. __body__.append('')
  132. ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4])
  133. __infunc__ = []
  134. __body__ = []
  135. else:
  136. __body__.append(s)
  137. return
  138. if __inpython__:
  139. m = __python_func_regexp__.match(s)
  140. if m and not eof:
  141. __body__.append(s)
  142. return
  143. else:
  144. ast.handlePythonMethod(statements, fn, lineno, __inpython__,
  145. root, __body__)
  146. __body__ = []
  147. __inpython__ = False
  148. if eof:
  149. return
  150. if s and s[0] == '#':
  151. if len(__residue__) != 0 and __residue__[0][0] != "#":
  152. bb.fatal("There is a comment on line %s of file %s (%s) which is in the middle of a multiline expression.\nBitbake used to ignore these but no longer does so, please fix your metadata as errors are likely as a result of this change." % (lineno, fn, s))
  153. if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
  154. bb.fatal("There is a confusing multiline, partially commented expression on line %s of file %s (%s).\nPlease clarify whether this is all a comment or should be parsed." % (lineno, fn, s))
  155. if s and s[-1] == '\\':
  156. __residue__.append(s[:-1])
  157. return
  158. s = "".join(__residue__) + s
  159. __residue__ = []
  160. # Skip empty lines
  161. if s == '':
  162. return
  163. # Skip comments
  164. if s[0] == '#':
  165. return
  166. m = __func_start_regexp__.match(s)
  167. if m:
  168. __infunc__ = [m.group("func") or "__anonymous", fn, lineno, m.group("py") is not None, m.group("fr") is not None]
  169. return
  170. m = __def_regexp__.match(s)
  171. if m:
  172. __body__.append(s)
  173. __inpython__ = m.group(1)
  174. return
  175. m = __export_func_regexp__.match(s)
  176. if m:
  177. ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
  178. return
  179. m = __addtask_regexp__.match(s)
  180. if m:
  181. if len(m.group().split()) == 2:
  182. # Check and warn for "addtask task1 task2"
  183. m2 = re.match(r"addtask\s+(?P<func>\w+)(?P<ignores>.*)", s)
  184. if m2 and m2.group('ignores'):
  185. logger.warning('addtask ignored: "%s"' % m2.group('ignores'))
  186. # Check and warn for "addtask task1 before task2 before task3", the
  187. # similar to "after"
  188. taskexpression = s.split()
  189. for word in ('before', 'after'):
  190. if taskexpression.count(word) > 1:
  191. logger.warning("addtask contained multiple '%s' keywords, only one is supported" % word)
  192. ast.handleAddTask(statements, fn, lineno, m)
  193. return
  194. m = __deltask_regexp__.match(s)
  195. if m:
  196. # Check and warn "for deltask task1 task2"
  197. if m.group('ignores'):
  198. logger.warning('deltask ignored: "%s"' % m.group('ignores'))
  199. ast.handleDelTask(statements, fn, lineno, m)
  200. return
  201. m = __addhandler_regexp__.match(s)
  202. if m:
  203. ast.handleBBHandlers(statements, fn, lineno, m)
  204. return
  205. m = __inherit_regexp__.match(s)
  206. if m:
  207. ast.handleInherit(statements, fn, lineno, m)
  208. return
  209. return ConfHandler.feeder(lineno, s, fn, statements)
  210. # Add us to the handlers list
  211. from .. import handlers
  212. handlers.append({'supports': supports, 'handle': handle, 'init': init})
  213. del handlers