Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Copyright (C) 2006 OpenedHand LTD |
| 2 | |
| 3 | # Point to an empty file so any user's custom settings don't break things |
| 4 | QUILTRCFILE ?= "${STAGING_ETCDIR_NATIVE}/quiltrc" |
| 5 | |
| 6 | PATCHDEPENDENCY = "${PATCHTOOL}-native:do_populate_sysroot" |
| 7 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 8 | # There is a bug in patch 2.7.3 and earlier where index lines |
| 9 | # in patches can change file modes when they shouldn't: |
| 10 | # http://git.savannah.gnu.org/cgit/patch.git/patch/?id=82b800c9552a088a241457948219d25ce0a407a4 |
| 11 | # This leaks into debug sources in particular. Add the dependency |
| 12 | # to target recipes to avoid this problem until we can rely on 2.7.4 or later. |
| 13 | PATCHDEPENDENCY_append_class-target = " patch-replacement-native:do_populate_sysroot" |
| 14 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | PATCH_GIT_USER_NAME ?= "OpenEmbedded" |
| 16 | PATCH_GIT_USER_EMAIL ?= "oe.patch@oe" |
| 17 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | inherit terminal |
| 19 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | python () { |
| 21 | if d.getVar('PATCHTOOL') == 'git' and d.getVar('PATCH_COMMIT_FUNCTIONS') == '1': |
| 22 | extratasks = bb.build.tasksbetween('do_unpack', 'do_patch', d) |
| 23 | try: |
| 24 | extratasks.remove('do_unpack') |
| 25 | except ValueError: |
| 26 | # For some recipes do_unpack doesn't exist, ignore it |
| 27 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | d.appendVarFlag('do_patch', 'prefuncs', ' patch_task_patch_prefunc') |
| 30 | for task in extratasks: |
| 31 | d.appendVarFlag(task, 'postfuncs', ' patch_task_postfunc') |
| 32 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | python patch_task_patch_prefunc() { |
| 35 | # Prefunc for do_patch |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 36 | srcsubdir = d.getVar('S') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 38 | workdir = os.path.abspath(d.getVar('WORKDIR')) |
| 39 | testsrcdir = os.path.abspath(srcsubdir) |
| 40 | if (testsrcdir + os.sep).startswith(workdir + os.sep): |
| 41 | # Double-check that either workdir or S or some directory in-between is a git repository |
| 42 | found = False |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 43 | while testsrcdir != workdir: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 44 | if os.path.exists(os.path.join(testsrcdir, '.git')): |
| 45 | found = True |
| 46 | break |
| 47 | if testsrcdir == workdir: |
| 48 | break |
| 49 | testsrcdir = os.path.dirname(testsrcdir) |
| 50 | if not found: |
| 51 | bb.fatal('PATCHTOOL = "git" set for source tree that is not a git repository. Refusing to continue as that may result in commits being made in your metadata repository.') |
| 52 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 53 | patchdir = os.path.join(srcsubdir, 'patches') |
| 54 | if os.path.exists(patchdir): |
| 55 | if os.listdir(patchdir): |
| 56 | d.setVar('PATCH_HAS_PATCHES_DIR', '1') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 58 | os.rmdir(patchdir) |
| 59 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | python patch_task_postfunc() { |
| 62 | # Prefunc for task functions between do_unpack and do_patch |
| 63 | import oe.patch |
| 64 | import shutil |
| 65 | func = d.getVar('BB_RUNTASK') |
| 66 | srcsubdir = d.getVar('S') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 68 | if os.path.exists(srcsubdir): |
| 69 | if func == 'do_patch': |
| 70 | haspatches = (d.getVar('PATCH_HAS_PATCHES_DIR') == '1') |
| 71 | patchdir = os.path.join(srcsubdir, 'patches') |
| 72 | if os.path.exists(patchdir): |
| 73 | shutil.rmtree(patchdir) |
| 74 | if haspatches: |
| 75 | stdout, _ = bb.process.run('git status --porcelain patches', cwd=srcsubdir) |
| 76 | if stdout: |
| 77 | bb.process.run('git checkout patches', cwd=srcsubdir) |
| 78 | stdout, _ = bb.process.run('git status --porcelain .', cwd=srcsubdir) |
| 79 | if stdout: |
| 80 | useroptions = [] |
| 81 | oe.patch.GitApplyTree.gitCommandUserOptions(useroptions, d=d) |
| 82 | 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) |
| 83 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 85 | def src_patches(d, all=False, expand=True): |
| 86 | import oe.patch |
| 87 | return oe.patch.src_patches(d, all, expand) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
| 89 | def should_apply(parm, d): |
| 90 | """Determine if we should apply the given patch""" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 91 | import oe.patch |
| 92 | return oe.patch.should_apply(parm, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
| 94 | should_apply[vardepsexclude] = "DATE SRCDATE" |
| 95 | |
| 96 | python patch_do_patch() { |
| 97 | import oe.patch |
| 98 | |
| 99 | patchsetmap = { |
| 100 | "patch": oe.patch.PatchTree, |
| 101 | "quilt": oe.patch.QuiltTree, |
| 102 | "git": oe.patch.GitApplyTree, |
| 103 | } |
| 104 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 105 | cls = patchsetmap[d.getVar('PATCHTOOL') or 'quilt'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | |
| 107 | resolvermap = { |
| 108 | "noop": oe.patch.NOOPResolver, |
| 109 | "user": oe.patch.UserResolver, |
| 110 | } |
| 111 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | rcls = resolvermap[d.getVar('PATCHRESOLVE') or 'user'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | |
| 114 | classes = {} |
| 115 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 116 | s = d.getVar('S') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 118 | os.putenv('PATH', d.getVar('PATH')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | |
| 120 | # We must use one TMPDIR per process so that the "patch" processes |
| 121 | # don't generate the same temp file name. |
| 122 | |
| 123 | import tempfile |
| 124 | process_tmpdir = tempfile.mkdtemp() |
| 125 | os.environ['TMPDIR'] = process_tmpdir |
| 126 | |
| 127 | for patch in src_patches(d): |
| 128 | _, _, local, _, _, parm = bb.fetch.decodeurl(patch) |
| 129 | |
| 130 | if "patchdir" in parm: |
| 131 | patchdir = parm["patchdir"] |
| 132 | if not os.path.isabs(patchdir): |
| 133 | patchdir = os.path.join(s, patchdir) |
| 134 | else: |
| 135 | patchdir = s |
| 136 | |
| 137 | if not patchdir in classes: |
| 138 | patchset = cls(patchdir, d) |
| 139 | resolver = rcls(patchset, oe_terminal) |
| 140 | classes[patchdir] = (patchset, resolver) |
| 141 | patchset.Clean() |
| 142 | else: |
| 143 | patchset, resolver = classes[patchdir] |
| 144 | |
| 145 | bb.note("Applying patch '%s' (%s)" % (parm['patchname'], oe.path.format_display(local, d))) |
| 146 | try: |
| 147 | patchset.Import({"file":local, "strippath": parm['striplevel']}, True) |
| 148 | except Exception as exc: |
| 149 | bb.utils.remove(process_tmpdir, True) |
| 150 | bb.fatal(str(exc)) |
| 151 | try: |
| 152 | resolver.Resolve() |
| 153 | except bb.BBHandledException as e: |
| 154 | bb.utils.remove(process_tmpdir, True) |
| 155 | bb.fatal(str(e)) |
| 156 | |
| 157 | bb.utils.remove(process_tmpdir, True) |
| 158 | del os.environ['TMPDIR'] |
| 159 | } |
| 160 | patch_do_patch[vardepsexclude] = "PATCHRESOLVE" |
| 161 | |
| 162 | addtask patch after do_unpack |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | do_patch[dirs] = "${WORKDIR}" |
| 164 | do_patch[depends] = "${PATCHDEPENDENCY}" |
| 165 | |
| 166 | EXPORT_FUNCTIONS do_patch |