1 # Lots of reusable functions. This file is sourced, not run.
3 function blank_tempdir
()
5 # sanity test: never rm -rf something we don't own.
7 touch -c "$1" || dienow
9 # Delete old directory, create new one.
11 mkdir
-p "$1" || dienow
14 # Figure out if we're using the stable or unstable versions of a package.
18 [ ! -z "$(echo ,"$USE_UNSTABLE", | grep ,"$1",)" ]
21 # Strip the version number off a tarball
26 [ $?
-ne 0 ] && dienow
28 if [ ! -z "$NO_CLEANUP" ]
30 echo "skip cleanup $@"
36 unstable
"$i" && i
="$PACKAGE"
42 # Give filename.tar.ext minus the version number.
46 echo "$1" |
sed -e 's/-*\(\([0-9\.]\)*\([_-]rc\)*\(-pre\)*\([0-9][a-zA-Z]\)*\)*\(\.tar\..z2*\)$/'"$2"'\6/'
49 # Given a filename.tar.ext, return the versino number.
53 echo "$1" |
sed -e 's/.*-\(\([0-9\.]\)*\([_-]rc\)*\(-pre\)*\([0-9][a-zA-Z]\)*\)*\(\.tar\..z2*\)$/'"$2"'\1/'
56 # Give package name, minus file's version number and archive extension.
60 noversion
$1 |
sed 's/\.tar\..z2*$//'
63 # output the sha1sum of a file
66 sha1sum "$@" |
awk '{print $1}'
69 # Extract tarball named in $1 and apply all relevant patches into
70 # "$BUILD/sources/$1". Record sha1sum of tarball and patch files in
71 # sha1-for-source.txt. Re-extract if tarball or patches change.
76 SRCTREE
="${BUILD}/sources"
77 SHA1FILE
="$(echo "${SRCTREE}/${PACKAGE}/sha1-for-source.txt
")"
79 # Sanity check: don't ever "rm -rf /". Just don't.
81 if [ -z "$PACKAGE" ] ||
[ -z "$SRCTREE" ]
86 # If the source tarball doesn't exist, but the extracted directory is there,
87 # assume everything's ok.
89 [ ! -e "$FILENAME" ] && [ -e "$SHA1FILE" ] && return 0
91 SHA1TAR
="$(sha1file "${SRCDIR}/${FILENAME}")"
93 # If it's already extracted and up to date (including patches), do nothing.
94 SHALIST
=$
(cat "$SHA1FILE" 2> /dev
/null
)
95 if [ ! -z "$SHALIST" ]
97 for i
in "$SHA1TAR" $
(sha1file
"${SOURCES}/patches/${PACKAGE}"-* 2>/dev
/null
)
99 # Is this sha1 in the file?
100 if [ -z "$(echo "$SHALIST" | sed -n "s
/$i/$i/p
" )" ]
106 SHALIST
="$(echo "$SHALIST" | sed "s
/$i//" )"
108 # If we matched all the sha1sums, nothing more to do.
109 [ -z "$SHALIST" ] && return 0
112 echo -n "Extracting '${PACKAGE}'"
113 # Delete the old tree (if any). Create new empty working directories.
114 rm -rf "${BUILD}/temp" "${SRCTREE}/${PACKAGE}" 2>/dev
/null
115 mkdir
-p "${BUILD}"/{temp
,sources
} || dienow
117 # Is it a bzip2 or gzip tarball?
119 [ "$FILENAME" != "${FILENAME/%\.tar\.bz2/}" ] && DECOMPRESS
="j"
120 [ "$FILENAME" != "${FILENAME/%\.tar\.gz/}" ] && DECOMPRESS
="z"
123 { tar -xv${DECOMPRESS} -f "${SRCDIR}/${FILENAME}" -C "${BUILD}/temp
" || dienow
126 mv "${BUILD}/temp/"* "${SRCTREE}/${PACKAGE}" &&
127 rmdir "${BUILD}/temp
" &&
128 echo "$SHA1TAR" > "$SHA1FILE"
130 [ $? -ne 0 ] && dienow
132 # Apply any patches to this package
134 ls "${SOURCES}/patches
/${PACKAGE}"-* 2> /dev/null | sort | while read i
139 (cd "${SRCTREE}/${PACKAGE}" && patch -p1 -i "$i") || dienow
140 sha1file "$i" >> "$SHA1FILE"
145 function try_checksum()
147 SUM="$
(sha1file
"$SRCDIR/$FILENAME" 2>/dev
/null
)"
148 if [ x"$SUM" == x"$SHA1" ] || [ -z "$SHA1" ] && [ -f "$SRCDIR/$FILENAME" ]
152 echo "No SHA1
for $FILENAME ($SUM)"
154 echo "Confirmed
$FILENAME"
157 # Preemptively extract source packages?
159 [ -z "$EXTRACT_ALL" ] && return 0
160 EXTRACT_ONLY=1 setupfor "$
(basename "$FILENAME")"
168 function try_download()
170 # Return success if we have a valid copy of the file
172 try_checksum && return 0
174 # If there's a corrupted file, delete it. In theory it would be nice
175 # to resume downloads, but wget creates "*.1" files instead.
177 rm "$SRCDIR/$FILENAME" 2> /dev/null
179 # If we have another source, try to download file.
183 wget -t 2 -T 20 -O "$SRCDIR/$FILENAME" "$1" ||
184 (rm "$SRCDIR/$FILENAME"; return 2)
185 touch -c "$SRCDIR/$FILENAME"
191 # Confirm a file matches sha1sum, else try to download it from mirror list.
195 FILENAME=`echo "$URL" | sed 's .*/ '`
196 [ -z "$RENAME" ] || FILENAME="$
(echo "$FILENAME" |
sed -r "$RENAME")"
197 ALTFILENAME=alt-"$
(noversion
"$FILENAME" -0)"
199 echo -ne "checking
$FILENAME\r"
201 # Update timestamps on both stable and unstable tarballs (if any)
202 # so cleanup_oldfiles doesn't delete them
203 touch -c "$SRCDIR"/{"$FILENAME","$ALTFILENAME"} 2>/dev/null
205 # Is the unstable version selected?
206 if unstable "$
(basename "$FILENAME")"
208 # Download new one as alt-packagename.tar.ext
209 FILENAME="$ALTFILENAME" SHA1= try_download "$UNSTABLE" ||
210 ([ ! -z "$PREFERRED_MIRROR" ] && SHA1= FILENAME="$ALTFILENAME" try_download "$PREFERRED_MIRROR/$ALTFILENAME")
214 # If environment variable specifies a preferred mirror, try that first.
216 if [ ! -z "$PREFERRED_MIRROR" ]
218 try_download "$PREFERRED_MIRROR/$FILENAME" && return 0
221 # Try standard locations
222 # Note: the URLs in mirror list cannot contain whitespace.
224 try_download "$URL" && return 0
225 for i in $MIRROR_LIST
227 try_download "$i/$FILENAME" && return 0
232 echo "Could not download
$FILENAME"
237 # Clean obsolete files out of the source directory
239 START_TIME=`date +%s`
241 function cleanup_oldfiles()
243 for i in "${SRCDIR}"/*
245 if [ -f "$i" ] && [ "$
(date +%s
-r "$i")" -lt "${START_TIME}" ]
247 echo Removing old file "$i"
253 # An exit function that works properly even from a subshell.
255 function actually_dienow()
257 echo -e "\n\e
[31mExiting due to errors
($PACKAGE)\e
[0m
"
261 trap actually_dienow SIGUSR1
270 # Turn a bunch of output lines into a much quieter series of periods.
272 function dotprogress()
287 # Extract package $1, use out-of-tree build directory $2 (or $1 if no $2)
288 # Use link directory $3 (or $1 if no $3)
292 export WRAPPY_LOGPATH="$WRAPPY_LOGDIR/cmdlines.
${STAGE_NAME}.setupfor
"
294 # Figure out whether we're using an unstable package.
297 unstable "$PACKAGE" && PACKAGE=alt-"$PACKAGE"
299 # Make sure the source is already extracted and up-to-date.
301 extract "${PACKAGE}-"*.tar* || exit 1
303 # If all we want to do is extract source, bail out now.
304 [ ! -z "$EXTRACT_ONLY" ] && return 0
311 unstable "$CURSRC" && CURSRC=alt-"$CURSRC"
313 export CURSRC="${WORK}/${CURSRC}"
315 [ -z "$SNAPSHOT_SYMLINK" ] && LINKTYPE="l
" || LINKTYPE="s
"
317 # Announce package, with easy-to-grep-for "===" marker.
319 echo "=== Building
$PACKAGE ($ARCH_NAME $STAGE_NAME)"
320 echo "Snapshot
'$PACKAGE'...
"
321 cd "${WORK}" || dienow
324 rm -rf "${CURSRC}" || dienow
326 mkdir -p "${CURSRC}" &&
327 cp -${LINKTYPE}fR "${SRCTREE}/$PACKAGE/"* "${CURSRC}"
329 [ $?
-ne 0 ] && dienow
331 # Do we have a separate working directory?
335 cd "$PACKAGE"* || dienow
337 mkdir
-p "$2" && cd "$2" || dienow
339 export WRAPPY_LOGPATH
="$WRAPPY_LOGDIR/cmdlines.${STAGE_NAME}.$1"
341 # Change window title bar to package now
342 [ -z "$NO_TITLE_BAR" ] &&
343 echo -en "\033]2;$ARCH_NAME $STAGE_NAME $PACKAGE\007"
346 # Figure out what version of a package we last built
348 function get_download_version
()
350 getversion $
(sed -n 's@URL=.*/\(.[^ ]*\).*@\1@p' "$TOP/download.sh" |
grep ${1}-)
353 # Filter out unnecessary noise
357 [ -z "$QUIET" ] && cat ||
grep "^==="
360 # Run a command either in foreground or background, depending on $FORK
372 # Kill a process and all its decendants
380 KIDS
="$KIDS $(pgrep -P$1)"
384 KIDS
="$(echo -n $KIDS)"
387 # Depth first kill avoids reparent_to_init hiding stuff.
389 kill $KIDS 2>/dev
/null