poky/meta/classes/patch.bbclass
Jose Quaresma b332b342b4 patch.bbclass: when the patch fails show more info on the fatal error
There are situations when the user have the 'patchdir' defined
as a parameter on SRC_URI. However he doesn't know that with this
the patch is applied relatively to the receipe source dir 'S'.

- When user have 'patchdir' defined check if this directory exist.
- If the patch fails show addition info to the user:
  - Import: show the striplevel
  - Resolver: show the expanded 'patchdir' to the user.

The next example is from opencv in meta-oe layer, here the
patch is applied on the target directory ${WORKDIR}/git/contrib.

S = "${WORKDIR}/git"
SRCREV_FORMAT = "opencv_contrib"
SRC_URI = "git://github.com/opencv/opencv.git;name=opencv \
           git://github.com/opencv/opencv_contrib.git;destsuffix=contrib;name=contrib \
           file://0001-sfm-link-with-Glog_LIBS.patch;patchdir=../contrib \
           "

* When the patch fail there are no message that indicates the real reason.
  patchdir=../no-found-on-file-system

ERROR: opencv-4.5.2-r0 do_patch: Command Error: 'quilt --quiltrc /build/tmp/work/core2-64-poky-linux/opencv/4.5.2-r0/recipe-sysroot-native/etc/quiltrc push' exited with 0  Output:
stdout: Applying patch 0001-sfm-link-with-Glog_LIBS.patch
can't find file to patch at input line 37
Perhaps you used the wrong -p or --strip option?

* The check of the patchdir will add a new fatal error
  when the user specifies a wrong path than don't exist.
  patchdir=../no-found-on-file-system

ERROR: opencv-4.5.2-r0 do_patch: Target directory '/build/tmp/work/core2-64-poky-linux/opencv/4.5.2-r0/git/../no-found-on-file-system' not found, patchdir '../no-found-on-file-system' is incorrect in patch file '0001-sfm-link-with-Glog_LIBS.patch'

* When we can't aplly the patch but the patchdir exist,
  show the expanded patchdir on fatal error.
  patchdir=../git

ERROR: opencv-4.5.2-r0 do_patch: Applying patch '0001-sfm-link-with-Glog_LIBS.patch' on target directory '/build/tmp/work/core2-64-poky-linux/opencv/4.5.2-r0/git/../git'
Command Error: 'quilt --quiltrc /build/tmp/work/core2-64-poky-linux/opencv/4.5.2-r0/recipe-sysroot-native/etc/quiltrc push' exited with 0  Output:
stdout: Applying patch 0001-sfm-link-with-Glog_LIBS.patch
can't find file to patch at input line 37
Perhaps you used the wrong -p or --strip option?

(From OE-Core rev: caf21ee38f7a96af6c10e80f9422611e317b29d6)

Signed-off-by: Jose Quaresma <quaresma.jose@gmail.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
(cherry picked from commit c44bc7c0fb8b7c2e44dd93607a3bfd9733e1df80)
Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2021-10-29 11:17:26 +01:00

170 lines
6.0 KiB
Plaintext

# Copyright (C) 2006 OpenedHand LTD
# Point to an empty file so any user's custom settings don't break things
QUILTRCFILE ?= "${STAGING_ETCDIR_NATIVE}/quiltrc"
PATCHDEPENDENCY = "${PATCHTOOL}-native:do_populate_sysroot"
# There is a bug in patch 2.7.3 and earlier where index lines
# in patches can change file modes when they shouldn't:
# http://git.savannah.gnu.org/cgit/patch.git/patch/?id=82b800c9552a088a241457948219d25ce0a407a4
# This leaks into debug sources in particular. Add the dependency
# to target recipes to avoid this problem until we can rely on 2.7.4 or later.
PATCHDEPENDENCY:append:class-target = " patch-replacement-native:do_populate_sysroot"
PATCH_GIT_USER_NAME ?= "OpenEmbedded"
PATCH_GIT_USER_EMAIL ?= "oe.patch@oe"
inherit terminal
python () {
if d.getVar('PATCHTOOL') == 'git' and d.getVar('PATCH_COMMIT_FUNCTIONS') == '1':
extratasks = bb.build.tasksbetween('do_unpack', 'do_patch', d)
try:
extratasks.remove('do_unpack')
except ValueError:
# For some recipes do_unpack doesn't exist, ignore it
pass
d.appendVarFlag('do_patch', 'prefuncs', ' patch_task_patch_prefunc')
for task in extratasks:
d.appendVarFlag(task, 'postfuncs', ' patch_task_postfunc')
}
python patch_task_patch_prefunc() {
# Prefunc for do_patch
srcsubdir = d.getVar('S')
workdir = os.path.abspath(d.getVar('WORKDIR'))
testsrcdir = os.path.abspath(srcsubdir)
if (testsrcdir + os.sep).startswith(workdir + os.sep):
# Double-check that either workdir or S or some directory in-between is a git repository
found = False
while testsrcdir != workdir:
if os.path.exists(os.path.join(testsrcdir, '.git')):
found = True
break
if testsrcdir == workdir:
break
testsrcdir = os.path.dirname(testsrcdir)
if not found:
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.')
patchdir = os.path.join(srcsubdir, 'patches')
if os.path.exists(patchdir):
if os.listdir(patchdir):
d.setVar('PATCH_HAS_PATCHES_DIR', '1')
else:
os.rmdir(patchdir)
}
python patch_task_postfunc() {
# Prefunc for task functions between do_unpack and do_patch
import oe.patch
import shutil
func = d.getVar('BB_RUNTASK')
srcsubdir = d.getVar('S')
if os.path.exists(srcsubdir):
if func == 'do_patch':
haspatches = (d.getVar('PATCH_HAS_PATCHES_DIR') == '1')
patchdir = os.path.join(srcsubdir, 'patches')
if os.path.exists(patchdir):
shutil.rmtree(patchdir)
if haspatches:
stdout, _ = bb.process.run('git status --porcelain patches', cwd=srcsubdir)
if stdout:
bb.process.run('git checkout patches', cwd=srcsubdir)
stdout, _ = bb.process.run('git status --porcelain .', cwd=srcsubdir)
if stdout:
useroptions = []
oe.patch.GitApplyTree.gitCommandUserOptions(useroptions, d=d)
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)
}
def src_patches(d, all=False, expand=True):
import oe.patch
return oe.patch.src_patches(d, all, expand)
def should_apply(parm, d):
"""Determine if we should apply the given patch"""
import oe.patch
return oe.patch.should_apply(parm, d)
should_apply[vardepsexclude] = "DATE SRCDATE"
python patch_do_patch() {
import oe.patch
patchsetmap = {
"patch": oe.patch.PatchTree,
"quilt": oe.patch.QuiltTree,
"git": oe.patch.GitApplyTree,
}
cls = patchsetmap[d.getVar('PATCHTOOL') or 'quilt']
resolvermap = {
"noop": oe.patch.NOOPResolver,
"user": oe.patch.UserResolver,
}
rcls = resolvermap[d.getVar('PATCHRESOLVE') or 'user']
classes = {}
s = d.getVar('S')
os.putenv('PATH', d.getVar('PATH'))
# We must use one TMPDIR per process so that the "patch" processes
# don't generate the same temp file name.
import tempfile
process_tmpdir = tempfile.mkdtemp()
os.environ['TMPDIR'] = process_tmpdir
for patch in src_patches(d):
_, _, local, _, _, parm = bb.fetch.decodeurl(patch)
if "patchdir" in parm:
patchdir = parm["patchdir"]
if not os.path.isabs(patchdir):
patchdir = os.path.join(s, patchdir)
if not os.path.isdir(patchdir):
bb.fatal("Target directory '%s' not found, patchdir '%s' is incorrect in patch file '%s'" %
(patchdir, parm["patchdir"], parm['patchname']))
else:
patchdir = s
if not patchdir in classes:
patchset = cls(patchdir, d)
resolver = rcls(patchset, oe_terminal)
classes[patchdir] = (patchset, resolver)
patchset.Clean()
else:
patchset, resolver = classes[patchdir]
bb.note("Applying patch '%s' (%s)" % (parm['patchname'], oe.path.format_display(local, d)))
try:
patchset.Import({"file":local, "strippath": parm['striplevel']}, True)
except Exception as exc:
bb.utils.remove(process_tmpdir, True)
bb.fatal("Importing patch '%s' with striplevel '%s'\n%s" % (parm['patchname'], parm['striplevel'], str(exc)))
try:
resolver.Resolve()
except bb.BBHandledException as e:
bb.utils.remove(process_tmpdir, True)
bb.fatal("Applying patch '%s' on target directory '%s'\n%s" % (parm['patchname'], patchdir, str(e)))
bb.utils.remove(process_tmpdir, True)
del os.environ['TMPDIR']
}
patch_do_patch[vardepsexclude] = "PATCHRESOLVE"
addtask patch after do_unpack
do_patch[dirs] = "${WORKDIR}"
do_patch[depends] = "${PATCHDEPENDENCY}"
EXPORT_FUNCTIONS do_patch