1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: epatch.eclass
6 # base-system@gentoo.org
7 # @BLURB: easy patch application functions
9 # An eclass providing epatch and epatch_user functions to easily apply
10 # patches to ebuilds. Mostly superseded by eapply* in EAPI 6.
12 if [[ -z ${_EPATCH_ECLASS} ]]; then
18 die "${ECLASS}: banned in EAPI=${EAPI}; use eapply* instead";;
23 # @VARIABLE: EPATCH_SOURCE
25 # Default directory to search for patches.
26 EPATCH_SOURCE="${WORKDIR}/patch"
27 # @VARIABLE: EPATCH_SUFFIX
29 # Default extension for patches (do not prefix the period yourself).
30 EPATCH_SUFFIX="patch.bz2"
31 # @VARIABLE: EPATCH_OPTS
33 # Options to pass to patch. Meant for ebuild/package-specific tweaking
34 # such as forcing the patch level (-p#) or fuzz (-F#) factor. Note that
35 # for single patch tweaking, you can also pass flags directly to epatch.
37 # @VARIABLE: EPATCH_COMMON_OPTS
39 # Common options to pass to `patch`. You probably should never need to
40 # change these. If you do, please discuss it with base-system first to
43 # -g0 - keep RCS, ClearCase, Perforce and SCCS happy #24571
44 # --no-backup-if-mismatch - do not leave .orig files behind
45 # -E - automatically remove empty files
47 EPATCH_COMMON_OPTS="-g0 -E --no-backup-if-mismatch"
48 # @VARIABLE: EPATCH_EXCLUDE
50 # List of patches not to apply. Note this is only file names,
51 # and not the full path. Globs accepted.
53 # @VARIABLE: EPATCH_SINGLE_MSG
55 # Change the printed message for a single patch.
57 # @VARIABLE: EPATCH_MULTI_MSG
59 # Change the printed message for multiple patches.
60 EPATCH_MULTI_MSG="Applying various patches (bugfixes/updates) ..."
61 # @VARIABLE: EPATCH_FORCE
63 # Only require patches to match EPATCH_SUFFIX rather than the extended
66 # @VARIABLE: EPATCH_USER_EXCLUDE
69 # List of patches not to apply. Note this is only file names,
70 # and not the full path. Globs accepted.
73 # @USAGE: [options] [patches] [dirs of patches]
75 # epatch is designed to greatly simplify the application of patches. It can
76 # process patch files directly, or directories of patches. The patches may be
77 # compressed (bzip/gzip/etc...) or plain text. You generally need not specify
78 # the -p option as epatch will automatically attempt -p0 to -p4 until things
81 # If you do not specify any patches/dirs, then epatch will default to the
82 # directory specified by EPATCH_SOURCE.
84 # Any options specified that start with a dash will be passed down to patch
85 # for this specific invocation. As soon as an arg w/out a dash is found, then
86 # arg processing stops.
88 # When processing directories, epatch will apply all patches that match:
90 # if ${EPATCH_FORCE} != "yes"
91 # ??_${ARCH}_foo.${EPATCH_SUFFIX}
95 # The leading ?? are typically numbers used to force consistent patch ordering.
96 # The arch field is used to apply patches only for the host architecture with
97 # the special value of "all" means apply for everyone. Note that using values
98 # other than "all" is highly discouraged -- you should apply patches all the
99 # time and let architecture details be detected at configure/compile time.
101 # If EPATCH_SUFFIX is empty, then no period before it is implied when searching
102 # for patches to apply.
104 # Refer to the other EPATCH_xxx variables for more customization of behavior.
106 _epatch_draw_line() {
107 # create a line of same length as input string
108 [[ -z $1 ]] && set "$(printf "%65s" '')"
112 unset P4CONFIG P4PORT P4USER # keep perforce at bay #56402
114 # First process options. We localize the EPATCH_OPTS setting
115 # from above so that we can pass it on in the loop below with
116 # any additional values the user has specified.
117 local EPATCH_OPTS=( ${EPATCH_OPTS[*]} )
118 while [[ $# -gt 0 ]] ; do
120 -*) EPATCH_OPTS+=( "$1" ) ;;
126 # Let the rest of the code process one user arg at a time --
127 # each arg may expand into multiple patches, and each arg may
128 # need to start off with the default global EPATCH_xxx values
129 if [[ $# -gt 1 ]] ; then
137 local SINGLE_PATCH="no"
138 # no args means process ${EPATCH_SOURCE}
139 [[ $# -eq 0 ]] && set -- "${EPATCH_SOURCE}"
141 if [[ -f $1 ]] ; then
144 # Use the suffix from the single patch (localize it); the code
145 # below will find the suffix for us
146 local EPATCH_SUFFIX=$1
148 elif [[ -d $1 ]] ; then
149 # We have to force sorting to C so that the wildcard expansion is consistent #471666.
150 evar_push_set LC_COLLATE C
151 # Some people like to make dirs of patches w/out suffixes (vim).
152 set -- "$1"/*${EPATCH_SUFFIX:+."${EPATCH_SUFFIX}"}
155 elif [[ -f ${EPATCH_SOURCE}/$1 ]] ; then
156 # Re-use EPATCH_SOURCE as a search dir
157 epatch "${EPATCH_SOURCE}/$1"
161 # sanity check ... if it isn't a dir or file, wtf man ?
162 [[ $# -ne 0 ]] && EPATCH_SOURCE=$1
164 eerror "Cannot find \$EPATCH_SOURCE! Value for \$EPATCH_SOURCE is:"
166 eerror " ${EPATCH_SOURCE}"
167 eerror " ( ${EPATCH_SOURCE##*/} )"
169 die "Cannot find \$EPATCH_SOURCE!"
172 # Now that we know we're actually going to apply something, merge
173 # all of the patch options back in to a single variable for below.
174 EPATCH_OPTS="${EPATCH_COMMON_OPTS} ${EPATCH_OPTS[*]}"
177 case ${EPATCH_SUFFIX##*\.} in
178 xz) PIPE_CMD="xz -dc" ;;
179 lzma) PIPE_CMD="lzma -dc" ;;
180 bz2) PIPE_CMD="bzip2 -dc" ;;
181 gz|Z|z) PIPE_CMD="gzip -dc" ;;
182 ZIP|zip) PIPE_CMD="unzip -p" ;;
186 [[ ${SINGLE_PATCH} == "no" ]] && einfo "${EPATCH_MULTI_MSG}"
190 # If the patch dir given contains subdirs, or our EPATCH_SUFFIX
191 # didn't match anything, ignore continue on
192 [[ ! -f ${x} ]] && continue
194 local patchname=${x##*/}
196 # Apply single patches, or forced sets of patches, or
197 # patches with ARCH dependant names.
199 # Else, skip this input altogether
200 local a=${patchname#*_} # strip the ???_
201 a=${a%%_*} # strip the _foo.patch
202 if ! [[ ${SINGLE_PATCH} == "yes" || \
203 ${EPATCH_FORCE} == "yes" || \
210 # Let people filter things dynamically
211 if [[ -n ${EPATCH_EXCLUDE}${EPATCH_USER_EXCLUDE} ]] ; then
212 # let people use globs in the exclude
213 eshopts_push -o noglob
216 for ex in ${EPATCH_EXCLUDE} ; do
217 if [[ ${patchname} == ${ex} ]] ; then
218 einfo " Skipping ${patchname} due to EPATCH_EXCLUDE ..."
224 for ex in ${EPATCH_USER_EXCLUDE} ; do
225 if [[ ${patchname} == ${ex} ]] ; then
226 einfo " Skipping ${patchname} due to EPATCH_USER_EXCLUDE ..."
235 if [[ ${SINGLE_PATCH} == "yes" ]] ; then
236 if [[ -n ${EPATCH_SINGLE_MSG} ]] ; then
237 einfo "${EPATCH_SINGLE_MSG}"
239 einfo "Applying ${patchname} ..."
242 einfo " ${patchname} ..."
245 # Handle aliased patch command #404447 #461568
247 eval $(alias patch 2>/dev/null | sed 's:^alias ::')
249 # most of the time, there will only be one run per unique name,
250 # but if there are more, make sure we get unique log filenames
251 local STDERR_TARGET="${T}/${patchname}.out"
252 if [[ -e ${STDERR_TARGET} ]] ; then
253 STDERR_TARGET="${T}/${patchname}-$$.out"
256 printf "***** %s *****\nPWD: %s\nPATCH TOOL: %s -> %s\nVERSION INFO:\n%s\n\n" \
260 "$(type -P "${patch}")" \
261 "$(${patch} --version)" \
264 # Decompress the patch if need be
267 if [[ -n ${PIPE_CMD} ]] ; then
268 PATCH_TARGET="${T}/$$.patch"
269 echo "PIPE_COMMAND: ${PIPE_CMD} ${x} > ${PATCH_TARGET}" >> "${STDERR_TARGET}"
271 if ! (${PIPE_CMD} "${x}" > "${PATCH_TARGET}") >> "${STDERR_TARGET}" 2>&1 ; then
273 eerror "Could not extract patch!"
274 #die "Could not extract patch!"
282 # Check for absolute paths in patches. If sandbox is disabled,
283 # people could (accidently) patch files in the root filesystem.
284 # Or trigger other unpleasantries #237667. So disallow -p0 on
286 local abs_paths=$(egrep -n '^[-+]{3} /' "${PATCH_TARGET}" | awk '$2 != "/dev/null" { print }')
287 if [[ -n ${abs_paths} ]] ; then
289 printf "NOTE: skipping -p0 due to absolute paths in patch:\n%s\n" "${abs_paths}" >> "${STDERR_TARGET}"
291 # Similar reason, but with relative paths.
292 local rel_paths=$(egrep -n '^[-+]{3} [^ ]*[.][.]/' "${PATCH_TARGET}")
293 if [[ -n ${rel_paths} ]] ; then
295 eerror "Rejected Patch: ${patchname} !"
296 eerror " ( ${PATCH_TARGET} )"
298 eerror "Your patch uses relative paths '../':"
299 eerror "${rel_paths}"
301 die "you need to fix the relative paths in patch"
304 # Dynamically detect the correct -p# ... i'm lazy, so shoot me :/
306 while [[ ${count} -lt 5 ]] ; do
307 patch_cmd="${patch} -p${count} ${EPATCH_OPTS}"
309 # Generate some useful debug info ...
311 _epatch_draw_line "***** ${patchname} *****"
313 echo "PATCH COMMAND: ${patch_cmd} --dry-run -f < '${PATCH_TARGET}'"
315 _epatch_draw_line "***** ${patchname} *****"
316 ${patch_cmd} --dry-run -f < "${PATCH_TARGET}" 2>&1
319 echo "patch program exited with status ${ret}"
321 ) >> "${STDERR_TARGET}"
323 if [ $? -eq 0 ] ; then
325 _epatch_draw_line "***** ${patchname} *****"
327 echo "ACTUALLY APPLYING ${patchname} ..."
328 echo "PATCH COMMAND: ${patch_cmd} < '${PATCH_TARGET}'"
330 _epatch_draw_line "***** ${patchname} *****"
331 ${patch_cmd} < "${PATCH_TARGET}" 2>&1
334 echo "patch program exited with status ${ret}"
336 ) >> "${STDERR_TARGET}"
338 if [ $? -ne 0 ] ; then
340 eerror "A dry-run of patch command succeeded, but actually"
341 eerror "applying the patch failed!"
342 #die "Real world sux compared to the dreamworld!"
351 (( EPATCH_N_APPLIED_PATCHES++ ))
353 # if we had to decompress the patch, delete the temp one
354 if [[ -n ${PIPE_CMD} ]] ; then
355 rm -f "${PATCH_TARGET}"
358 if [[ ${count} -ge 5 ]] ; then
360 eerror "Failed Patch: ${patchname} !"
361 eerror " ( ${PATCH_TARGET} )"
363 eerror "Include in your bugreport the contents of:"
365 eerror " ${STDERR_TARGET}"
367 die "Failed Patch: ${patchname}!"
370 # if everything worked, delete the full debug patch log
371 rm -f "${STDERR_TARGET}"
373 # then log away the exact stuff for people to review later
374 cat <<-EOF >> "${T}/epatch.log"
383 [[ ${SINGLE_PATCH} == "no" ]] && einfo "Done with patching"
384 : # everything worked
390 # @VARIABLE: EPATCH_USER_SOURCE
392 # Location for user patches, see the epatch_user function.
393 # Should be set by the user. Don't set this in ebuilds.
394 : ${EPATCH_USER_SOURCE:=${PORTAGE_CONFIGROOT%/}/etc/portage/patches}
396 # @FUNCTION: epatch_user
399 # Applies user-provided patches to the source tree. The patches are
400 # taken from /etc/portage/patches/<CATEGORY>/<P-PR|P|PN>[:SLOT]/, where the first
401 # of these three directories to exist will be the one to use, ignoring
402 # any more general directories which might exist as well. They must end
403 # in ".patch" to be applied.
405 # User patches are intended for quick testing of patches without ebuild
406 # modifications, as well as for permanent customizations a user might
407 # desire. Obviously, there can be no official support for arbitrarily
408 # patched ebuilds. So whenever a build log in a bug report mentions that
409 # user patches were applied, the user should be asked to reproduce the
410 # problem without these.
412 # Not all ebuilds do call this function, so placing patches in the
413 # stated directory might or might not work, depending on the package and
414 # the eclasses it inherits and uses. It is safe to call the function
415 # repeatedly, so it is always possible to add a call at the ebuild
416 # level. The first call is the time when the patches will be
419 # Ideally, this function should be called after gentoo-specific patches
420 # have been applied, so that their code can be modified as well, but
421 # before calls to e.g. eautoreconf, as the user patches might affect
422 # autotool input files as well.
424 [[ $# -ne 0 ]] && die "epatch_user takes no options"
426 # Allow multiple calls to this function; ignore all but the first
427 local applied="${T}/epatch_user.log"
428 [[ -e ${applied} ]] && return 2
430 # don't clobber any EPATCH vars that the parent might want
431 local EPATCH_SOURCE check
432 for check in ${CATEGORY}/{${P}-${PR},${P},${PN}}{,:${SLOT%/*}}; do
433 EPATCH_SOURCE=${EPATCH_USER_SOURCE}/${CTARGET}/${check}
434 [[ -r ${EPATCH_SOURCE} ]] || EPATCH_SOURCE=${EPATCH_USER_SOURCE}/${CHOST}/${check}
435 [[ -r ${EPATCH_SOURCE} ]] || EPATCH_SOURCE=${EPATCH_USER_SOURCE}/${check}
436 if [[ -d ${EPATCH_SOURCE} ]] ; then
437 local old_n_applied_patches=${EPATCH_N_APPLIED_PATCHES:-0}
438 EPATCH_SOURCE=${EPATCH_SOURCE} \
439 EPATCH_SUFFIX="patch" \
441 EPATCH_MULTI_MSG="Applying user patches from ${EPATCH_SOURCE} ..." \
443 echo "${EPATCH_SOURCE}" > "${applied}"
444 if [[ ${old_n_applied_patches} -lt ${EPATCH_N_APPLIED_PATCHES} ]]; then
445 has epatch_user_death_notice ${EBUILD_DEATH_HOOKS} || \
446 EBUILD_DEATH_HOOKS+=" epatch_user_death_notice"
451 echo "none" > "${applied}"
455 # @FUNCTION: epatch_user_death_notice
458 # Include an explicit notice in the die message itself that user patches were
459 # applied to this build.
460 epatch_user_death_notice() {
461 ewarn "!!! User patches were applied to this build!"