Use -mtune=arm920t instead of -mtune=arm9tdmi
[qi-bootmenu-system.git] / sources / functions-fwl.sh
blobeac39308e774702da533f65763d12f3348f57832
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.
6 [ -z "$1" ] && dienow
7 touch -c "$1" || dienow
9 # Delete old directory, create new one.
10 rm -rf "$1"
11 mkdir -p "$1" || dienow
14 # Figure out if we're using the stable or unstable versions of a package.
16 function unstable()
18 [ ! -z "$(echo ,"$USE_UNSTABLE", | grep ,"$1",)" ]
21 # Strip the version number off a tarball
23 function cleanup()
26 [ $? -ne 0 ] && dienow
28 if [ ! -z "$NO_CLEANUP" ]
29 then
30 echo "skip cleanup $@"
31 return
34 for i in "$@"
36 unstable "$i" && i="$PACKAGE"
37 echo "cleanup $i"
38 rm -rf "$i" || dienow
39 done
42 # Give filename.tar.ext minus the version number.
44 function noversion()
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.
51 function getversion()
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.
58 function basename()
60 noversion $1 | sed 's/\.tar\..z2*$//'
63 # output the sha1sum of a file
64 function sha1file()
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.
73 function extract()
75 FILENAME="$1"
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" ]
82 then
83 dienow
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" ]
96 then
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" )" ]
101 then
102 SHALIST=missing
103 break
105 # Remove it
106 SHALIST="$(echo "$SHALIST" | sed "s/$i//" )"
107 done
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?
118 DECOMPRESS=""
119 [ "$FILENAME" != "${FILENAME/%\.tar\.bz2/}" ] && DECOMPRESS="j"
120 [ "$FILENAME" != "${FILENAME/%\.tar\.gz/}" ] && DECOMPRESS="z"
122 cd "${WORK}" &&
123 { tar -xv${DECOMPRESS} -f "${SRCDIR}/${FILENAME}" -C "${BUILD}/temp" || dienow
124 } | dotprogress
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
136 if [ -f "$i" ]
137 then
138 echo "Applying $i"
139 (cd "${SRCTREE}/${PACKAGE}" && patch -p1 -i "$i") || dienow
140 sha1file "$i" >> "$SHA1FILE"
142 done
145 function try_checksum()
147 SUM="$(sha1file "$SRCDIR/$FILENAME" 2>/dev/null)"
148 if [ x"$SUM" == x"$SHA1" ] || [ -z "$SHA1" ] && [ -f "$SRCDIR/$FILENAME" ]
149 then
150 if [ -z "$SHA1" ]
151 then
152 echo "No SHA1 for $FILENAME ($SUM)"
153 else
154 echo "Confirmed $FILENAME"
157 # Preemptively extract source packages?
159 [ -z "$EXTRACT_ALL" ] && return 0
160 EXTRACT_ONLY=1 setupfor "$(basename "$FILENAME")"
161 return $?
164 return 1
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.
181 if [ -n "$1" ]
182 then
183 wget -t 2 -T 60 -O "$SRCDIR/$FILENAME" "$1" ||
184 (rm "$SRCDIR/$FILENAME"; return 2)
185 touch -c "$SRCDIR/$FILENAME"
188 try_checksum
191 # Confirm a file matches sha1sum, else try to download it from mirror list.
193 function download()
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")"
207 then
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")
211 return $?
214 # If environment variable specifies a preferred mirror, try that first.
216 if [ ! -z "$PREFERRED_MIRROR" ]
217 then
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
228 done
230 # Return failure.
232 echo "Could not download $FILENAME"
233 echo -en "\e[0m"
234 return 1
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}" ]
246 then
247 echo Removing old file "$i"
248 rm -rf "$i"
250 done
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"
258 exit 1
261 trap actually_dienow SIGUSR1
262 TOPSHELL=$$
264 function dienow()
266 kill -USR1 $TOPSHELL
267 exit 1
270 # Turn a bunch of output lines into a much quieter series of periods.
272 function dotprogress()
275 while read i
277 x=$[$x + 1]
278 if [[ "$x" -eq 25 ]]
279 then
281 echo -n .
283 done
284 echo
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)
290 function setupfor()
292 export WRAPPY_LOGPATH="$WRAPPY_LOGDIR/cmdlines.${STAGE_NAME}.setupfor"
294 # Figure out whether we're using an unstable package.
296 PACKAGE="$1"
297 unstable "$PACKAGE" && PACKAGE=alt-"$PACKAGE"
299 # Make sure the source is already extracted and up-to-date.
300 cd "${SRCDIR}" &&
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
306 # Set CURSRC
307 CURSRC="$PACKAGE"
308 if [ ! -z "$3" ]
309 then
310 CURSRC="$3"
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
322 if [ $# -lt 3 ]
323 then
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?
333 if [ -z "$2" ]
334 then
335 cd "$PACKAGE"* || dienow
336 else
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
355 maybe_quiet()
357 [ -z "$QUIET" ] && cat || grep "^==="
360 # Run a command either in foreground or background, depending on $FORK
362 maybe_fork()
364 if [ -z "$FORK" ]
365 then
366 eval "$*"
367 else
368 eval "$*" &
372 # Kill a process and all its decendants
374 function killtree()
376 local KIDS=""
378 while [ $# -ne 0 ]
380 KIDS="$KIDS $(pgrep -P$1)"
381 shift
382 done
384 KIDS="$(echo -n $KIDS)"
385 if [ ! -z "$KIDS" ]
386 then
387 # Depth first kill avoids reparent_to_init hiding stuff.
388 killtree $KIDS
389 kill $KIDS 2>/dev/null