/[gentoo-x86]/eclass/eutils.eclass
Gentoo

Diff of /eclass/eutils.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.324 Revision 1.325
1# Copyright 1999-2009 Gentoo Foundation 1# Copyright 1999-2009 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.324 2010/01/03 19:57:10 zmedico Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.325 2010/01/09 20:06:24 vapier Exp $
4 4
5# @ECLASS: eutils.eclass 5# @ECLASS: eutils.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# base-system@gentoo.org 7# base-system@gentoo.org
8# @BLURB: many extra (but common) functions that are used in ebuilds 8# @BLURB: many extra (but common) functions that are used in ebuilds
112 unset __ESHOPTS_SAVE__[$i] 112 unset __ESHOPTS_SAVE__[$i]
113 set +$- || die "eshopts_pop: sanity: invalid shell settings: $-" 113 set +$- || die "eshopts_pop: sanity: invalid shell settings: $-"
114 set -${s} || die "eshopts_pop: sanity: unable to restore saved shell settings: ${s}" 114 set -${s} || die "eshopts_pop: sanity: unable to restore saved shell settings: ${s}"
115} 115}
116 116
117# Default directory where patches are located 117# @VARIABLE: EPATCH_SOURCE
118# @DESCRIPTION:
119# Default directory to search for patches.
118EPATCH_SOURCE="${WORKDIR}/patch" 120EPATCH_SOURCE="${WORKDIR}/patch"
119# Default extension for patches 121# @VARIABLE: EPATCH_SUFFIX
122# @DESCRIPTION:
123# Default extension for patches (do not prefix the period yourself).
120EPATCH_SUFFIX="patch.bz2" 124EPATCH_SUFFIX="patch.bz2"
125# @VARIABLE: EPATCH_OPTS
126# @DESCRIPTION:
121# Default options for patch 127# Default options for patch:
128# @CODE
122# Set -g0 to keep RCS, ClearCase, Perforce and SCCS happy. Bug #24571 129# -g0 - keep RCS, ClearCase, Perforce and SCCS happy #24571
123# Set --no-backup-if-mismatch so we don't leave '.orig' files behind. 130# --no-backup-if-mismatch - do not leave .orig files behind
124# Set -E to automatically remove empty files. 131# -E - automatically remove empty files
132# @CODE
125EPATCH_OPTS="-g0 -E --no-backup-if-mismatch" 133EPATCH_OPTS="-g0 -E --no-backup-if-mismatch"
134# @VARIABLE: EPATCH_EXCLUDE
135# @DESCRIPTION:
126# List of patches not to apply. Note this is only file names, 136# List of patches not to apply. Note this is only file names,
127# and not the full path .. 137# and not the full path. Globs accepted.
128EPATCH_EXCLUDE="" 138EPATCH_EXCLUDE=""
139# @VARIABLE: EPATCH_SINGLE_MSG
140# @DESCRIPTION:
129# Change the printed message for a single patch. 141# Change the printed message for a single patch.
130EPATCH_SINGLE_MSG="" 142EPATCH_SINGLE_MSG=""
143# @VARIABLE: EPATCH_MULTI_MSG
144# @DESCRIPTION:
131# Change the printed message for multiple patches. 145# Change the printed message for multiple patches.
132EPATCH_MULTI_MSG="Applying various patches (bugfixes/updates) ..." 146EPATCH_MULTI_MSG="Applying various patches (bugfixes/updates) ..."
133# Force applying bulk patches even if not following the style: 147# @VARIABLE: EPATCH_FORCE
134# 148# @DESCRIPTION:
135# ??_${ARCH}_foo.${EPATCH_SUFFIX} 149# Only require patches to match EPATCH_SUFFIX rather than the extended
136# 150# arch naming style.
137EPATCH_FORCE="no" 151EPATCH_FORCE="no"
138 152
139# This function is for bulk patching, or in theory for just one 153# @FUNCTION: epatch
140# or two patches. 154# @USAGE: [patches] [dirs of patches]
155# @DESCRIPTION:
156# epatch is designed to greatly simplify the application of patches. It can
157# process patch files directly, or directories of patches. The patches may be
158# compressed (bzip/gzip/etc...) or plain text. You generally need not specify
159# the -p option as epatch will automatically attempt -p0 to -p5 until things
160# apply successfully.
141# 161#
142# It should work with .bz2, .gz, .zip and plain text patches. 162# If you do not specify any options, then epatch will default to the directory
143# Currently all patches should be the same format. 163# specified by EPATCH_SOURCE.
144# 164#
145# You do not have to specify '-p' option to patch, as it will 165# When processing directories, epatch will apply all patches that match:
146# try with -p0 to -p5 until it succeed, or fail at -p5. 166# @CODE
147# 167# ${EPATCH_FORCE} == "yes"
148# Above EPATCH_* variables can be used to control various defaults,
149# bug they should be left as is to ensure an ebuild can rely on
150# them for.
151#
152# Patches are applied in current directory.
153#
154# Bulk Patches should preferably have the form of:
155#
156# ??_${ARCH}_foo.${EPATCH_SUFFIX} 168# ??_${ARCH}_foo.${EPATCH_SUFFIX}
169# else
170# *.${EPATCH_SUFFIX}
171# @CODE
172# The leading ?? are typically numbers used to force consistent patch ordering.
173# The arch field is used to apply patches only for the host architecture with
174# the special value of "all" means apply for everyone. Note that using values
175# other than "all" is highly discouraged -- you should apply patches all the
176# time and let architecture details be detected at configure/compile time.
157# 177#
158# For example: 178# If EPATCH_SUFFIX is empty, then no period before it is implied when searching
179# for patches to apply.
159# 180#
160# 01_all_misc-fix.patch.bz2 181# Refer to the other EPATCH_xxx variables for more customization of behavior.
161# 02_sparc_another-fix.patch.bz2
162#
163# This ensures that there are a set order, and you can have ARCH
164# specific patches.
165#
166# If you however give an argument to epatch(), it will treat it as a
167# single patch that need to be applied if its a file. If on the other
168# hand its a directory, it will set EPATCH_SOURCE to this.
169#
170# <azarah@gentoo.org> (10 Nov 2002)
171#
172epatch() { 182epatch() {
173 _epatch_draw_line() { 183 _epatch_draw_line() {
184 # create a line of same length as input string
174 [[ -z $1 ]] && set "$(printf "%65s" '')" 185 [[ -z $1 ]] && set "$(printf "%65s" '')"
175 echo "${1//?/=}" 186 echo "${1//?/=}"
176 } 187 }
177 _epatch_assert() { local _pipestatus=${PIPESTATUS[*]}; [[ ${_pipestatus// /} -eq 0 ]] ; }
178 local PIPE_CMD=""
179 local STDERR_TARGET="${T}/$$.out"
180 local PATCH_TARGET="${T}/$$.patch"
181 local PATCH_SUFFIX=""
182 local SINGLE_PATCH="no"
183 local x=""
184 188
185 unset P4CONFIG P4PORT P4USER # keep perforce at bay #56402 189 unset P4CONFIG P4PORT P4USER # keep perforce at bay #56402
186 190
187 if [ "$#" -gt 1 ] 191 # Let the rest of the code process one user arg at a time --
188 then 192 # each arg may expand into multiple patches, and each arg may
193 # need to start off with the default global EPATCH_xxx values
194 if [[ $# -gt 1 ]] ; then
189 local m="" 195 local m
190 for m in "$@" ; do 196 for m in "$@" ; do
191 epatch "${m}" 197 epatch "${m}"
192 done 198 done
193 return 0 199 return 0
194 fi 200 fi
195 201
196 if [ -n "$1" -a -f "$1" ] 202 local SINGLE_PATCH="no"
197 then 203 # no args means process ${EPATCH_SOURCE}
204 [[ $# -eq 0 ]] && set -- "${EPATCH_SOURCE}"
205
206 if [[ -f $1 ]] ; then
198 SINGLE_PATCH="yes" 207 SINGLE_PATCH="yes"
199 208 set -- "$1"
200 local EPATCH_SOURCE="$1" 209 # Use the suffix from the single patch (localize it); the code
210 # below will find the suffix for us
201 local EPATCH_SUFFIX="${1##*\.}" 211 local EPATCH_SUFFIX=$1
202 212
203 elif [ -n "$1" -a -d "$1" ] 213 elif [[ -d $1 ]] ; then
204 then 214 # Some people like to make dirs of patches w/out suffixes (vim)
205 # Allow no extension if EPATCH_FORCE=yes ... used by vim for example ... 215 set -- "$1"/*${EPATCH_SUFFIX:+."${EPATCH_SUFFIX}"}
206 if [ "${EPATCH_FORCE}" = "yes" ] && [ -z "${EPATCH_SUFFIX}" ] 216
217 else
218 # sanity check ... if it isn't a dir or file, wtf man ?
219 [[ $# -ne 0 ]] && EPATCH_SOURCE=$1
220 echo
221 eerror "Cannot find \$EPATCH_SOURCE! Value for \$EPATCH_SOURCE is:"
222 eerror
223 eerror " ${EPATCH_SOURCE}"
224 eerror " ( ${EPATCH_SOURCE##*/} )"
225 echo
226 die "Cannot find \$EPATCH_SOURCE!"
227 fi
228
229 local PIPE_CMD
230 case ${EPATCH_SUFFIX##*\.} in
231 xz) PIPE_CMD="xz -dc" ;;
232 lzma) PIPE_CMD="lzma -dc" ;;
233 bz2) PIPE_CMD="bzip2 -dc" ;;
234 gz|Z|z) PIPE_CMD="gzip -dc" ;;
235 ZIP|zip) PIPE_CMD="unzip -p" ;;
236 *) ;;
237 esac
238
239 [[ ${SINGLE_PATCH} == "no" ]] && einfo "${EPATCH_MULTI_MSG}"
240
241 local x
242 for x in "$@" ; do
243 # If the patch dir given contains subdirs, or our EPATCH_SUFFIX
244 # didn't match anything, ignore continue on
245 [[ ! -f ${x} ]] && continue
246
247 local patchname=${x##*/}
248
249 # Apply single patches, or forced sets of patches, or
250 # patches with ARCH dependant names.
251 # ???_arch_foo.patch
252 # Else, skip this input altogether
253 local a=${patchname#*_} # strip the ???_
254 a=${a%%_*} # strip the _foo.patch
255 if ! [[ ${SINGLE_PATCH} == "yes" || \
256 ${EPATCH_FORCE} == "yes" || \
257 ${a} == all || \
258 ${a} == ${ARCH} ]]
207 then 259 then
208 local EPATCH_SOURCE="$1/*" 260 continue
209 else
210 local EPATCH_SOURCE="$1/*.${EPATCH_SUFFIX}"
211 fi
212 else
213 if [[ ! -d ${EPATCH_SOURCE} ]] || [[ -n $1 ]] ; then
214 if [ -n "$1" -a "${EPATCH_SOURCE}" = "${WORKDIR}/patch" ]
215 then
216 EPATCH_SOURCE="$1"
217 fi 261 fi
218 262
219 echo 263 # Let people filter things dynamically
220 eerror "Cannot find \$EPATCH_SOURCE! Value for \$EPATCH_SOURCE is:"
221 eerror
222 eerror " ${EPATCH_SOURCE}"
223 eerror " ( ${EPATCH_SOURCE##*/} )"
224 echo
225 die "Cannot find \$EPATCH_SOURCE!"
226 fi
227
228 local EPATCH_SOURCE="${EPATCH_SOURCE}/*.${EPATCH_SUFFIX}"
229 fi
230
231 case ${EPATCH_SUFFIX##*\.} in
232 xz)
233 PIPE_CMD="xz -dc"
234 PATCH_SUFFIX="xz"
235 ;;
236 lzma)
237 PIPE_CMD="lzma -dc"
238 PATCH_SUFFIX="lzma"
239 ;;
240 bz2)
241 PIPE_CMD="bzip2 -dc"
242 PATCH_SUFFIX="bz2"
243 ;;
244 gz|Z|z)
245 PIPE_CMD="gzip -dc"
246 PATCH_SUFFIX="gz"
247 ;;
248 ZIP|zip)
249 PIPE_CMD="unzip -p"
250 PATCH_SUFFIX="zip"
251 ;;
252 *)
253 PIPE_CMD="cat"
254 PATCH_SUFFIX="patch"
255 ;;
256 esac
257
258 if [ "${SINGLE_PATCH}" = "no" ]
259 then
260 einfo "${EPATCH_MULTI_MSG}"
261 fi
262 for x in ${EPATCH_SOURCE}
263 do
264 # New ARCH dependant patch naming scheme ...
265 #
266 # ???_arch_foo.patch
267 #
268 if [ -f ${x} ] && \
269 ([ "${SINGLE_PATCH}" = "yes" -o "${x/_all_}" != "${x}" -o "${x/_${ARCH}_}" != "${x}" ] || \
270 [ "${EPATCH_FORCE}" = "yes" ])
271 then
272 local count=0
273 local popts="${EPATCH_OPTS}"
274 local patchname=${x##*/}
275
276 if [ -n "${EPATCH_EXCLUDE}" ] 264 if [[ -n ${EPATCH_EXCLUDE} ]] ; then
277 then 265 # let people use globs in the exclude
278 if [ "${EPATCH_EXCLUDE/${patchname}}" != "${EPATCH_EXCLUDE}" ] 266 eshopts_push -o noglob
279 then 267
268 local ex
269 for ex in ${EPATCH_EXCLUDE} ; do
270 if [[ ${patchname} == ${ex} ]] ; then
271 eshopts_pop
280 continue 272 continue
281 fi 273 fi
274 done
275 eshopts_pop
276 fi
277
278 if [[ ${SINGLE_PATCH} == "yes" ]] ; then
279 if [[ -n ${EPATCH_SINGLE_MSG} ]] ; then
280 einfo "${EPATCH_SINGLE_MSG}"
281 else
282 einfo "Applying ${patchname} ..."
282 fi 283 fi
283
284 if [ "${SINGLE_PATCH}" = "yes" ]
285 then
286 if [ -n "${EPATCH_SINGLE_MSG}" ]
287 then
288 einfo "${EPATCH_SINGLE_MSG}"
289 else 284 else
290 einfo "Applying ${patchname} ..." 285 einfo " ${patchname} ..."
286 fi
287
288 # most of the time, there will only be one run per unique name,
289 # but if there are more, make sure we get unique log filenames
290 local STDERR_TARGET="${T}/${patchname}.out"
291 if [[ -e ${STDERR_TARGET} ]] ; then
292 STDERR_TARGET="${T}/${patchname}-$$.out"
293 fi
294
295 printf "***** %s *****\n\n" "${patchname}" > "${STDERR_TARGET}"
296
297 # Decompress the patch if need be
298 local count=0
299 local PATCH_TARGET
300 if [[ -n ${PIPE_CMD} ]] ; then
301 PATCH_TARGET="${T}/$$.patch"
302 echo "PIPE_COMMAND: ${PIPE_CMD} ${x} > ${PATCH_TARGET}" >> "${STDERR_TARGET}"
303
304 if ! (${PIPE_CMD} "${x}" > "${PATCH_TARGET}") >> "${STDERR_TARGET}" 2>&1 ; then
305 echo
306 eerror "Could not extract patch!"
307 #die "Could not extract patch!"
308 count=5
309 break
310 fi
311 else
312 PATCH_TARGET=${x}
313 fi
314
315 # Check for absolute paths in patches. If sandbox is disabled,
316 # people could (accidently) patch files in the root filesystem.
317 # Or trigger other unpleasantries #237667. So disallow -p0 on
318 # such patches.
319 local abs_paths=$(egrep -n '^[-+]{3} /' "${PATCH_TARGET}" | awk '$2 != "/dev/null" { print }')
320 if [[ -n ${abs_paths} ]] ; then
321 count=1
322 printf "NOTE: skipping -p0 due to absolute paths in patch:\n%s\n" "${abs_paths}" >> "${STDERR_TARGET}"
323 fi
324
325 # Dynamically detect the correct -p# ... i'm lazy, so shoot me :/
326 while [[ ${count} -lt 5 ]] ; do
327 # Generate some useful debug info ...
328 (
329 _epatch_draw_line "***** ${patchname} *****"
330 echo
331 echo "PATCH COMMAND: patch -p${count} ${EPATCH_OPTS} < '${PATCH_TARGET}'"
332 echo
333 _epatch_draw_line "***** ${patchname} *****"
334 ) >> "${STDERR_TARGET}"
335
336 if (patch -p${count} ${EPATCH_OPTS} --dry-run -f < "${PATCH_TARGET}") >> "${STDERR_TARGET}" 2>&1 ; then
337 (
338 _epatch_draw_line "***** ${patchname} *****"
339 echo
340 echo "ACTUALLY APPLYING ${patchname} ..."
341 echo
342 _epatch_draw_line "***** ${patchname} *****"
343 patch -p${count} ${EPATCH_OPTS} < "${PATCH_TARGET}" 2>&1
344 ) >> "${STDERR_TARGET}"
345
346 if [ $? -ne 0 ] ; then
347 echo
348 eerror "A dry-run of patch command succeeded, but actually"
349 eerror "applying the patch failed!"
350 #die "Real world sux compared to the dreamworld!"
351 count=5
291 fi 352 fi
292 else 353 break
293 einfo " ${patchname} ..."
294 fi 354 fi
295 355
296 echo "***** ${patchname} *****" > ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/} 356 : $(( count++ ))
297 echo >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
298
299 # Decompress the patch if need be
300 if [[ ${PATCH_SUFFIX} != "patch" ]] ; then
301 echo -n "PIPE_COMMAND: " >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
302 echo "${PIPE_CMD} ${x} > ${PATCH_TARGET}" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
303
304 if ! (${PIPE_CMD} ${x} > ${PATCH_TARGET}) >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/} 2>&1 ; then
305 echo
306 eerror "Could not extract patch!"
307 #die "Could not extract patch!"
308 count=5
309 break
310 fi
311 else
312 PATCH_TARGET="${x}"
313 fi
314
315 # Check for absolute paths in patches. If sandbox is disabled,
316 # people could (accidently) patch files in the root filesystem.
317 # Or trigger other unpleasantries #237667. So disallow -p0 on
318 # such patches.
319 local abs_paths=$(egrep -n '^[-+]{3} /' "${PATCH_TARGET}" | awk '$2 != "/dev/null" { print }')
320 if [[ -n ${abs_paths} ]] ; then
321 count=1
322 echo "NOTE: skipping -p0 due to absolute paths in patch:" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
323 echo "${abs_paths}" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
324 fi
325
326 # Allow for prefix to differ ... im lazy, so shoot me :/
327 while [ "${count}" -lt 5 ]
328 do
329 # Generate some useful debug info ...
330 _epatch_draw_line "***** ${patchname} *****" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
331 echo >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
332
333 echo -n "PATCH COMMAND: " >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
334 echo "patch -p${count} ${popts} < ${PATCH_TARGET}" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
335
336 echo >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
337 _epatch_draw_line "***** ${patchname} *****" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
338
339 if (cat ${PATCH_TARGET} | patch -p${count} ${popts} --dry-run -f ; _epatch_assert) >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/} 2>&1
340 then
341 _epatch_draw_line "***** ${patchname} *****" > ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real
342 echo >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real
343 echo "ACTUALLY APPLYING ${patchname} ..." >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real
344 echo >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real
345 _epatch_draw_line "***** ${patchname} *****" >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real
346
347 cat ${PATCH_TARGET} | patch -p${count} ${popts} >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real 2>&1
348 _epatch_assert
349
350 if [ "$?" -ne 0 ]
351 then
352 cat ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real >> ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}
353 echo
354 eerror "A dry-run of patch command succeeded, but actually"
355 eerror "applying the patch failed!"
356 #die "Real world sux compared to the dreamworld!"
357 count=5
358 fi
359
360 rm -f ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}.real
361
362 break
363 fi
364
365 count=$((count + 1))
366 done 357 done
367 358
368 if [ "${PATCH_SUFFIX}" != "patch" ] 359 # if we had to decompress the patch, delete the temp one
369 then 360 if [[ -n ${PIPE_CMD} ]] ; then
370 rm -f ${PATCH_TARGET} 361 rm -f "${PATCH_TARGET}"
371 fi 362 fi
372 363
373 if [ "${count}" -eq 5 ] 364 if [[ ${count} -ge 5 ]] ; then
374 then
375 echo 365 echo
376 eerror "Failed Patch: ${patchname} !" 366 eerror "Failed Patch: ${patchname} !"
377 eerror " ( ${PATCH_TARGET} )" 367 eerror " ( ${PATCH_TARGET} )"
378 eerror 368 eerror
379 eerror "Include in your bugreport the contents of:" 369 eerror "Include in your bugreport the contents of:"
380 eerror 370 eerror
381 eerror " ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/}" 371 eerror " ${STDERR_TARGET}"
382 echo 372 echo
383 die "Failed Patch: ${patchname}!" 373 die "Failed Patch: ${patchname}!"
384 fi 374 fi
385 375
386 rm -f ${STDERR_TARGET%/*}/${patchname}-${STDERR_TARGET##*/} 376 # if everything worked, delete the patch log
387 377 rm -f "${STDERR_TARGET}"
388 eend 0 378 eend 0
389 fi
390 done 379 done
391 if [ "${SINGLE_PATCH}" = "no" ] 380
392 then 381 [[ ${SINGLE_PATCH} == "no" ]] && einfo "Done with patching"
393 einfo "Done with patching" 382 : # everything worked
394 fi
395} 383}
396epatch_user() { 384epatch_user() {
397 [[ $# -ne 0 ]] && die "epatch_user takes no options" 385 [[ $# -ne 0 ]] && die "epatch_user takes no options"
398 386
399 # don't clobber any EPATCH vars that the parent might want 387 # don't clobber any EPATCH vars that the parent might want

Legend:
Removed from v.1.324  
changed lines
  Added in v.1.325

  ViewVC Help
Powered by ViewVC 1.1.20