patch.bbclass 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (C) 2006 OpenedHand LTD
  2. # Point to an empty file so any user's custom settings don't break things
  3. QUILTRCFILE ?= "${STAGING_ETCDIR_NATIVE}/quiltrc"
  4. PATCHDEPENDENCY = "${PATCHTOOL}-native:do_populate_sysroot"
  5. PATCH_GIT_USER_NAME ?= "OpenEmbedded"
  6. PATCH_GIT_USER_EMAIL ?= "oe.patch@oe"
  7. inherit terminal
  8. python () {
  9. if d.getVar('PATCHTOOL', True) == 'git' and d.getVar('PATCH_COMMIT_FUNCTIONS', True) == '1':
  10. tasks = list(filter(lambda k: d.getVarFlag(k, "task", True), d.keys()))
  11. extratasks = []
  12. def follow_chain(task, endtask, chain=None):
  13. if not chain:
  14. chain = []
  15. chain.append(task)
  16. for othertask in tasks:
  17. if othertask == task:
  18. continue
  19. if task == endtask:
  20. for ctask in chain:
  21. if ctask not in extratasks:
  22. extratasks.append(ctask)
  23. else:
  24. deps = d.getVarFlag(othertask, 'deps', False)
  25. if task in deps:
  26. follow_chain(othertask, endtask, chain)
  27. chain.pop()
  28. follow_chain('do_unpack', 'do_patch')
  29. try:
  30. extratasks.remove('do_unpack')
  31. except ValueError:
  32. # For some recipes do_unpack doesn't exist, ignore it
  33. pass
  34. d.appendVarFlag('do_patch', 'prefuncs', ' patch_task_patch_prefunc')
  35. for task in extratasks:
  36. d.appendVarFlag(task, 'postfuncs', ' patch_task_postfunc')
  37. }
  38. python patch_task_patch_prefunc() {
  39. # Prefunc for do_patch
  40. func = d.getVar('BB_RUNTASK', True)
  41. srcsubdir = d.getVar('S', True)
  42. patchdir = os.path.join(srcsubdir, 'patches')
  43. if os.path.exists(patchdir):
  44. if os.listdir(patchdir):
  45. d.setVar('PATCH_HAS_PATCHES_DIR', '1')
  46. else:
  47. os.rmdir(patchdir)
  48. }
  49. python patch_task_postfunc() {
  50. # Prefunc for task functions between do_unpack and do_patch
  51. import oe.patch
  52. import shutil
  53. func = d.getVar('BB_RUNTASK', True)
  54. srcsubdir = d.getVar('S', True)
  55. if os.path.exists(srcsubdir):
  56. if func == 'do_patch':
  57. haspatches = (d.getVar('PATCH_HAS_PATCHES_DIR', True) == '1')
  58. patchdir = os.path.join(srcsubdir, 'patches')
  59. if os.path.exists(patchdir):
  60. shutil.rmtree(patchdir)
  61. if haspatches:
  62. stdout, _ = bb.process.run('git status --porcelain patches', cwd=srcsubdir)
  63. if stdout:
  64. bb.process.run('git checkout patches', cwd=srcsubdir)
  65. stdout, _ = bb.process.run('git status --porcelain .', cwd=srcsubdir)
  66. if stdout:
  67. useroptions = []
  68. oe.patch.GitApplyTree.gitCommandUserOptions(useroptions, d=d)
  69. bb.process.run('git add .; git %s commit -a -m "Committing changes from %s\n\n%s"' % (' '.join(useroptions), func, oe.patch.GitApplyTree.ignore_commit_prefix + ' - from %s' % func), cwd=srcsubdir)
  70. }
  71. def src_patches(d, all=False, expand=True):
  72. import oe.patch
  73. return oe.patch.src_patches(d, all, expand)
  74. def should_apply(parm, d):
  75. """Determine if we should apply the given patch"""
  76. import oe.patch
  77. return oe.patch.should_apply(parm, d)
  78. should_apply[vardepsexclude] = "DATE SRCDATE"
  79. python patch_do_patch() {
  80. import oe.patch
  81. patchsetmap = {
  82. "patch": oe.patch.PatchTree,
  83. "quilt": oe.patch.QuiltTree,
  84. "git": oe.patch.GitApplyTree,
  85. }
  86. cls = patchsetmap[d.getVar('PATCHTOOL', True) or 'quilt']
  87. resolvermap = {
  88. "noop": oe.patch.NOOPResolver,
  89. "user": oe.patch.UserResolver,
  90. }
  91. rcls = resolvermap[d.getVar('PATCHRESOLVE', True) or 'user']
  92. classes = {}
  93. s = d.getVar('S', True)
  94. os.putenv('PATH', d.getVar('PATH', True))
  95. # We must use one TMPDIR per process so that the "patch" processes
  96. # don't generate the same temp file name.
  97. import tempfile
  98. process_tmpdir = tempfile.mkdtemp()
  99. os.environ['TMPDIR'] = process_tmpdir
  100. for patch in src_patches(d):
  101. _, _, local, _, _, parm = bb.fetch.decodeurl(patch)
  102. if "patchdir" in parm:
  103. patchdir = parm["patchdir"]
  104. if not os.path.isabs(patchdir):
  105. patchdir = os.path.join(s, patchdir)
  106. else:
  107. patchdir = s
  108. if not patchdir in classes:
  109. patchset = cls(patchdir, d)
  110. resolver = rcls(patchset, oe_terminal)
  111. classes[patchdir] = (patchset, resolver)
  112. patchset.Clean()
  113. else:
  114. patchset, resolver = classes[patchdir]
  115. bb.note("Applying patch '%s' (%s)" % (parm['patchname'], oe.path.format_display(local, d)))
  116. try:
  117. patchset.Import({"file":local, "strippath": parm['striplevel']}, True)
  118. except Exception as exc:
  119. bb.utils.remove(process_tmpdir, True)
  120. bb.fatal(str(exc))
  121. try:
  122. resolver.Resolve()
  123. except bb.BBHandledException as e:
  124. bb.utils.remove(process_tmpdir, True)
  125. bb.fatal(str(e))
  126. bb.utils.remove(process_tmpdir, True)
  127. del os.environ['TMPDIR']
  128. }
  129. patch_do_patch[vardepsexclude] = "PATCHRESOLVE"
  130. addtask patch after do_unpack
  131. do_patch[dirs] = "${WORKDIR}"
  132. do_patch[depends] = "${PATCHDEPENDENCY}"
  133. EXPORT_FUNCTIONS do_patch