CRUX-ARM : Home

Home :: Documentation :: Download :: Development :: Community :: Ports :: Packages :: Bugs :: Links :: About :: Donors
Also accept *.tar.lzma source archives.
[pkgutils-cross.git] / pkgmk.in
CommitLineData
9ac667e6
SR
1#!/bin/bash
2#
3# pkgutils
4#
5# Copyright (c) 2000-2005 Per Liden
6fe55c25 6# Copyright (c) 2006-2007 by CRUX team (http://crux.nu)
9ac667e6
SR
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21# USA.
22#
23
24info() {
25 echo "=======> $1"
26}
27
28warning() {
e55714dd 29 info "WARNING: $1" >&2
9ac667e6
SR
30}
31
32error() {
e55714dd 33 info "ERROR: $1" >&2
9ac667e6
SR
34}
35
36get_filename() {
7b3f9929
JD
37 if [[ $1 =~ ^(http|https|ftp|file)://.*/(.+) ]]; then
38 echo "$PKGMK_SOURCE_DIR/${BASH_REMATCH[2]}"
39 else
40 echo $1
9ac667e6 41 fi
9ac667e6
SR
42}
43
2dee8e17
MH
44get_basename() {
45 local FILE="`echo $1 | sed 's|^.*://.*/||g'`"
46 echo $FILE
47}
48
9ac667e6
SR
49check_pkgfile() {
50 if [ ! "$name" ]; then
51 error "Variable 'name' not specified in $PKGMK_PKGFILE."
52 exit 1
53 elif [ ! "$version" ]; then
54 error "Variable 'version' not specified in $PKGMK_PKGFILE."
55 exit 1
56 elif [ ! "$release" ]; then
57 error "Variable 'release' not specified in $PKGMK_PKGFILE."
58 exit 1
59 elif [ "`type -t build`" != "function" ]; then
60 error "Function 'build' not specified in $PKGMK_PKGFILE."
61 exit 1
62 fi
63}
64
65check_directory() {
66 if [ ! -d $1 ]; then
67 error "Directory '$1' does not exist."
68 exit 1
69 elif [ ! -w $1 ]; then
70 error "Directory '$1' not writable."
71 exit 1
72 elif [ ! -x $1 ] || [ ! -r $1 ]; then
73 error "Directory '$1' not readable."
74 exit 1
75 fi
76}
77
de428c29
JD
78check_file() {
79 if [ -e $1 ] && [ ! -w $1 ]; then
80 error "File '$1' is not writable."
81 exit 1
82 fi
83}
84
9ac667e6
SR
85download_file() {
86 info "Downloading '$1'."
87
88 if [ ! "`type -p wget`" ]; then
89 error "Command 'wget' not found."
90 exit 1
91 fi
92
93 LOCAL_FILENAME=`get_filename $1`
94 LOCAL_FILENAME_PARTIAL="$LOCAL_FILENAME.partial"
2dee8e17
MH
95 DOWNLOAD_OPTS="--passive-ftp --no-directories --tries=3 --waitretry=3 \
96 --directory-prefix=$PKGMK_SOURCE_DIR \
765b5014 97 --output-document=$LOCAL_FILENAME_PARTIAL --no-check-certificate"
9ac667e6
SR
98
99 if [ -f "$LOCAL_FILENAME_PARTIAL" ]; then
100 info "Partial download found, trying to resume"
101 RESUME_CMD="-c"
102 fi
103
2dee8e17
MH
104 error=1
105
106 BASENAME=`get_basename $1`
107 for REPO in ${PKGMK_SOURCE_MIRRORS[@]}; do
108 REPO="`echo $REPO | sed 's|/$||'`"
51c534c2 109 wget $RESUME_CMD $DOWNLOAD_OPTS $PKGMK_WGET_OPTS $REPO/$BASENAME
9ac667e6 110 error=$?
2dee8e17 111 if [ $error == 0 ]; then
9ac667e6
SR
112 break
113 fi
114 done
2dee8e17
MH
115
116 if [ $error != 0 ]; then
117 while true; do
51c534c2 118 wget $RESUME_CMD $DOWNLOAD_OPTS $PKGMK_WGET_OPTS $1
2dee8e17
MH
119 error=$?
120 if [ $error != 0 ] && [ "$RESUME_CMD" ]; then
121 info "Partial download failed, restarting"
122 rm -f "$LOCAL_FILENAME_PARTIAL"
123 RESUME_CMD=""
124 else
125 break
126 fi
127 done
128 fi
9ac667e6
SR
129
130 if [ $error != 0 ]; then
131 error "Downloading '$1' failed."
132 exit 1
133 fi
134
135 mv -f "$LOCAL_FILENAME_PARTIAL" "$LOCAL_FILENAME"
136}
137
138download_source() {
139 local FILE LOCAL_FILENAME
140
141 for FILE in ${source[@]}; do
142 LOCAL_FILENAME=`get_filename $FILE`
143 if [ ! -e $LOCAL_FILENAME ]; then
144 if [ "$LOCAL_FILENAME" = "$FILE" ]; then
145 error "Source file '$LOCAL_FILENAME' not found (can not be downloaded, URL not specified)."
146 exit 1
147 else
148 if [ "$PKGMK_DOWNLOAD" = "yes" ]; then
149 download_file $FILE
150 else
151 error "Source file '$LOCAL_FILENAME' not found (use option -d to download)."
152 exit 1
153 fi
154 fi
155 fi
156 done
157}
158
159unpack_source() {
160 local FILE LOCAL_FILENAME COMMAND
161
162 for FILE in ${source[@]}; do
163 LOCAL_FILENAME=`get_filename $FILE`
164 case $LOCAL_FILENAME in
e6c32c41 165 *.tar.gz|*.tar.Z|*.tgz|*.tar.bz2|*.tar.xz|*.tar.lzma|*.zip)
698c42e2 166 COMMAND="bsdtar -p -o -C $SRC -xf $LOCAL_FILENAME" ;;
9ac667e6
SR
167 *)
168 COMMAND="cp $LOCAL_FILENAME $SRC" ;;
169 esac
170
171 echo "$COMMAND"
172
173 $COMMAND
174
175 if [ $? != 0 ]; then
176 if [ "$PKGMK_KEEP_WORK" = "no" ]; then
177 rm -rf $PKGMK_WORK_DIR
178 fi
179 error "Building '$TARGET' failed."
180 exit 1
181 fi
182 done
183}
184
185make_md5sum() {
186 local FILE LOCAL_FILENAMES
187
188 if [ "$source" ]; then
189 for FILE in ${source[@]}; do
190 LOCAL_FILENAMES="$LOCAL_FILENAMES `get_filename $FILE`"
191 done
192
193 md5sum $LOCAL_FILENAMES | sed -e 's| .*/| |' | sort -k 2
194 fi
195}
196
197make_footprint() {
198 pkginfo --footprint $TARGET | \
199 sed "s|\tlib/modules/`uname -r`/|\tlib/modules/<kernel-version>/|g" | \
200 sort -k 3
201}
202
203check_md5sum() {
204 local FILE="$PKGMK_WORK_DIR/.tmp"
205
206 cd $PKGMK_ROOT
207
208 if [ -f $PKGMK_MD5SUM ]; then
209 make_md5sum > $FILE.md5sum
210 sort -k 2 $PKGMK_MD5SUM > $FILE.md5sum.orig
211 diff -w -t -U 0 $FILE.md5sum.orig $FILE.md5sum | \
212 sed '/^@@/d' | \
213 sed '/^+++/d' | \
214 sed '/^---/d' | \
215 sed 's/^+/NEW /g' | \
216 sed 's/^-/MISSING /g' > $FILE.md5sum.diff
217 if [ -s $FILE.md5sum.diff ]; then
218 error "Md5sum mismatch found:"
e55714dd 219 cat $FILE.md5sum.diff >&2
9ac667e6
SR
220
221 if [ "$PKGMK_KEEP_WORK" = "no" ]; then
222 rm -rf $PKGMK_WORK_DIR
223 fi
224
225 if [ "$PKGMK_CHECK_MD5SUM" = "yes" ]; then
226 error "Md5sum not ok."
227 exit 1
228 fi
229
230 error "Building '$TARGET' failed."
231 exit 1
232 fi
233 else
234 if [ "$PKGMK_CHECK_MD5SUM" = "yes" ]; then
235 if [ "$PKGMK_KEEP_WORK" = "no" ]; then
236 rm -rf $PKGMK_WORK_DIR
237 fi
238 info "Md5sum not found."
239 exit 1
240 fi
241
242 warning "Md5sum not found, creating new."
243 make_md5sum > $PKGMK_MD5SUM
244 fi
245
246 if [ "$PKGMK_CHECK_MD5SUM" = "yes" ]; then
247 if [ "$PKGMK_KEEP_WORK" = "no" ]; then
248 rm -rf $PKGMK_WORK_DIR
249 fi
250 info "Md5sum ok."
251 exit 0
252 fi
253}
254
255strip_files() {
256 local FILE FILTER
257
258 cd $PKG
259
260 if [ -f $PKGMK_ROOT/$PKGMK_NOSTRIP ]; then
261 FILTER="grep -v -f $PKGMK_ROOT/$PKGMK_NOSTRIP"
262 else
263 FILTER="cat"
264 fi
265
266 find . -type f -printf "%P\n" | $FILTER | while read FILE; do
b0bbde50
SR
267 case $(file -b "$FILE") in
268 *ELF*executable*not\ stripped)
9ac667e6 269 strip --strip-all "$FILE"
b0bbde50
SR
270 ;;
271 *ELF*shared\ object*not\ stripped)
9ac667e6 272 strip --strip-unneeded "$FILE"
b0bbde50
SR
273 ;;
274 current\ ar\ archive)
9ac667e6 275 strip --strip-debug "$FILE"
b0bbde50 276 esac
9ac667e6
SR
277 done
278}
279
280compress_manpages() {
281 local FILE DIR TARGET
282
283 cd $PKG
284
285 find . -type f -path "*/man/man*/*" | while read FILE; do
286 if [ "$FILE" = "${FILE%%.gz}" ]; then
287 gzip -9 "$FILE"
288 fi
289 done
290
291 find . -type l -path "*/man/man*/*" | while read FILE; do
292 TARGET=`readlink -n "$FILE"`
293 TARGET="${TARGET##*/}"
294 TARGET="${TARGET%%.gz}.gz"
295 rm -f "$FILE"
296 FILE="${FILE%%.gz}.gz"
297 DIR=`dirname "$FILE"`
298
299 if [ -e "$DIR/$TARGET" ]; then
300 ln -sf "$TARGET" "$FILE"
301 fi
302 done
303}
304
305check_footprint() {
306 local FILE="$PKGMK_WORK_DIR/.tmp"
307
308 cd $PKGMK_ROOT
309
310 if [ -f $TARGET ]; then
311 make_footprint > $FILE.footprint
312 if [ -f $PKGMK_FOOTPRINT ]; then
313 sort -k 3 $PKGMK_FOOTPRINT > $FILE.footprint.orig
314 diff -w -t -U 0 $FILE.footprint.orig $FILE.footprint | \
315 sed '/^@@/d' | \
316 sed '/^+++/d' | \
317 sed '/^---/d' | \
318 sed 's/^+/NEW /g' | \
319 sed 's/^-/MISSING /g' > $FILE.footprint.diff
320 if [ -s $FILE.footprint.diff ]; then
321 error "Footprint mismatch found:"
e55714dd 322 cat $FILE.footprint.diff >&2
9ac667e6
SR
323 BUILD_SUCCESSFUL="no"
324 fi
325 else
326 warning "Footprint not found, creating new."
327 mv $FILE.footprint $PKGMK_FOOTPRINT
328 fi
329 else
330 error "Package '$TARGET' was not found."
331 BUILD_SUCCESSFUL="no"
332 fi
333}
334
92dbaab6 335make_work_dir() {
9ac667e6
SR
336 export PKG="$PKGMK_WORK_DIR/pkg"
337 export SRC="$PKGMK_WORK_DIR/src"
338 umask 022
339
340 cd $PKGMK_ROOT
92dbaab6 341 remove_work_dir
9ac667e6 342 mkdir -p $SRC $PKG
92dbaab6 343
9ac667e6
SR
344 if [ "$PKGMK_IGNORE_MD5SUM" = "no" ]; then
345 check_md5sum
346 fi
92dbaab6
JW
347}
348
349remove_work_dir() {
350 rm -rf $PKGMK_WORK_DIR
351}
352
353
354build_package() {
355 local BUILD_SUCCESSFUL="no"
356
de428c29 357 check_file "$TARGET"
92dbaab6 358 make_work_dir
9ac667e6
SR
359
360 if [ "$UID" != "0" ]; then
361 warning "Packages should be built as root."
362 fi
363
364 info "Building '$TARGET'."
365
366 unpack_source
367
368 cd $SRC
369 (set -e -x ; build)
370
371 if [ $? = 0 ]; then
372 if [ "$PKGMK_NO_STRIP" = "no" ]; then
373 strip_files
374 fi
375
376 compress_manpages
377
378 cd $PKG
379 info "Build result:"
380 tar czvvf $TARGET *
381
382 if [ $? = 0 ]; then
383 BUILD_SUCCESSFUL="yes"
384
385 if [ "$PKGMK_IGNORE_FOOTPRINT" = "yes" ]; then
386 warning "Footprint ignored."
387 else
388 check_footprint
389 fi
390 fi
391 fi
392
393 if [ "$PKGMK_KEEP_WORK" = "no" ]; then
92dbaab6 394 remove_work_dir
9ac667e6
SR
395 fi
396
397 if [ "$BUILD_SUCCESSFUL" = "yes" ]; then
398 info "Building '$TARGET' succeeded."
399 else
400 if [ -f $TARGET ]; then
401 touch -r $PKGMK_ROOT/$PKGMK_PKGFILE $TARGET &> /dev/null
402 fi
403 error "Building '$TARGET' failed."
404 exit 1
405 fi
406}
407
408install_package() {
409 local COMMAND
410
411 info "Installing '$TARGET'."
412
413 if [ "$PKGMK_INSTALL" = "install" ]; then
414 COMMAND="pkgadd $TARGET"
415 else
416 COMMAND="pkgadd -u $TARGET"
417 fi
418
419 cd $PKGMK_ROOT
420 echo "$COMMAND"
421 $COMMAND
422
423 if [ $? = 0 ]; then
424 info "Installing '$TARGET' succeeded."
425 else
426 error "Installing '$TARGET' failed."
427 exit 1
428 fi
429}
430
431recursive() {
432 local ARGS FILE DIR
433
434 ARGS=`echo "$@" | sed -e "s/--recursive//g" -e "s/-r//g"`
435
436 for FILE in `find $PKGMK_ROOT -name $PKGMK_PKGFILE | sort`; do
437 DIR="`dirname $FILE`/"
438 if [ -d $DIR ]; then
439 info "Entering directory '$DIR'."
440 (cd $DIR && $PKGMK_COMMAND $ARGS)
441 info "Leaving directory '$DIR'."
442 fi
443 done
444}
445
446clean() {
447 local FILE LOCAL_FILENAME
448
449 if [ -f $TARGET ]; then
450 info "Removing $TARGET"
451 rm -f $TARGET
452 fi
453
454 for FILE in ${source[@]}; do
455 LOCAL_FILENAME=`get_filename $FILE`
456 if [ -e $LOCAL_FILENAME ] && [ "$LOCAL_FILENAME" != "$FILE" ]; then
457 info "Removing $LOCAL_FILENAME"
458 rm -f $LOCAL_FILENAME
459 fi
460 done
461}
462
463update_footprint() {
464 if [ ! -f $TARGET ]; then
465 error "Unable to update footprint. File '$TARGET' not found."
466 exit 1
467 fi
468
de428c29 469 check_file "$PKGMK_FOOTPRINT"
9ac667e6
SR
470 make_footprint > $PKGMK_FOOTPRINT
471 touch $TARGET
472
473 info "Footprint updated."
474}
475
476build_needed() {
477 local FILE RESULT
478
479 RESULT="yes"
480 if [ -f $TARGET ]; then
481 RESULT="no"
482 for FILE in $PKGMK_PKGFILE ${source[@]}; do
483 FILE=`get_filename $FILE`
484 if [ ! -e $FILE ] || [ ! $TARGET -nt $FILE ]; then
485 RESULT="yes"
486 break
487 fi
488 done
489 fi
490
491 echo $RESULT
492}
493
494interrupted() {
495 echo ""
496 error "Interrupted."
497
498 if [ "$PKGMK_KEEP_WORK" = "no" ]; then
499 rm -rf $PKGMK_WORK_DIR
500 fi
501
502 exit 1
503}
504
505print_help() {
506 echo "usage: `basename $PKGMK_COMMAND` [options]"
507 echo "options:"
508 echo " -i, --install build and install package"
509 echo " -u, --upgrade build and install package (as upgrade)"
510 echo " -r, --recursive search for and build packages recursively"
511 echo " -d, --download download missing source file(s)"
512 echo " -do, --download-only do not build, only download missing source file(s)"
92dbaab6 513 echo " -eo, --extract-only do not build, only extract source file(s)"
9ac667e6
SR
514 echo " -utd, --up-to-date do not build, only check if package is up to date"
515 echo " -uf, --update-footprint update footprint using result from last build"
516 echo " -if, --ignore-footprint build package without checking footprint"
517 echo " -um, --update-md5sum update md5sum"
518 echo " -im, --ignore-md5sum build package without checking md5sum"
519 echo " -cm, --check-md5sum do not build, only check md5sum"
520 echo " -ns, --no-strip do not strip executable binaries or libraries"
521 echo " -f, --force build package even if it appears to be up to date"
522 echo " -c, --clean remove package and downloaded files"
523 echo " -kw, --keep-work keep temporary working directory"
524 echo " -cf, --config-file <file> use alternative configuration file"
525 echo " -v, --version print version and exit "
526 echo " -h, --help print help and exit"
527}
528
529parse_options() {
530 while [ "$1" ]; do
531 case $1 in
532 -i|--install)
533 PKGMK_INSTALL="install" ;;
534 -u|--upgrade)
535 PKGMK_INSTALL="upgrade" ;;
536 -r|--recursive)
537 PKGMK_RECURSIVE="yes" ;;
538 -d|--download)
539 PKGMK_DOWNLOAD="yes" ;;
540 -do|--download-only)
541 PKGMK_DOWNLOAD="yes"
542 PKGMK_DOWNLOAD_ONLY="yes" ;;
92dbaab6
JW
543 -eo|--extract-only)
544 PKGMK_EXTRACT_ONLY="yes" ;;
9ac667e6
SR
545 -utd|--up-to-date)
546 PKGMK_UP_TO_DATE="yes" ;;
547 -uf|--update-footprint)
548 PKGMK_UPDATE_FOOTPRINT="yes" ;;
549 -if|--ignore-footprint)
550 PKGMK_IGNORE_FOOTPRINT="yes" ;;
551 -um|--update-md5sum)
552 PKGMK_UPDATE_MD5SUM="yes" ;;
553 -im|--ignore-md5sum)
554 PKGMK_IGNORE_MD5SUM="yes" ;;
555 -cm|--check-md5sum)
556 PKGMK_CHECK_MD5SUM="yes" ;;
557 -ns|--no-strip)
558 PKGMK_NO_STRIP="yes" ;;
559 -f|--force)
560 PKGMK_FORCE="yes" ;;
561 -c|--clean)
562 PKGMK_CLEAN="yes" ;;
563 -kw|--keep-work)
564 PKGMK_KEEP_WORK="yes" ;;
565 -cf|--config-file)
566 if [ ! "$2" ]; then
567 echo "`basename $PKGMK_COMMAND`: option $1 requires an argument"
568 exit 1
569 fi
570 PKGMK_CONFFILE="$2"
571 shift ;;
572 -v|--version)
573 echo "`basename $PKGMK_COMMAND` (pkgutils) $PKGMK_VERSION"
574 exit 0 ;;
575 -h|--help)
576 print_help
577 exit 0 ;;
578 *)
579 echo "`basename $PKGMK_COMMAND`: invalid option $1"
580 exit 1 ;;
581 esac
582 shift
583 done
584}
585
586main() {
587 local FILE TARGET
588
589 parse_options "$@"
590
591 if [ "$PKGMK_RECURSIVE" = "yes" ]; then
592 recursive "$@"
593 exit 0
594 fi
595
596 for FILE in $PKGMK_PKGFILE $PKGMK_CONFFILE; do
597 if [ ! -f $FILE ]; then
598 error "File '$FILE' not found."
599 exit 1
600 fi
601 . $FILE
602 done
603
604 check_directory "$PKGMK_SOURCE_DIR"
605 check_directory "$PKGMK_PACKAGE_DIR"
606 check_directory "`dirname $PKGMK_WORK_DIR`"
607
608 check_pkgfile
609
610 TARGET="$PKGMK_PACKAGE_DIR/$name#$version-$release.pkg.tar.gz"
611
612 if [ "$PKGMK_CLEAN" = "yes" ]; then
613 clean
614 exit 0
615 fi
616
617 if [ "$PKGMK_UPDATE_FOOTPRINT" = "yes" ]; then
618 update_footprint
619 exit 0
620 fi
621
622 if [ "$PKGMK_UPDATE_MD5SUM" = "yes" ]; then
623 download_source
de428c29 624 check_file "$PKGMK_MD5SUM"
9ac667e6
SR
625 make_md5sum > $PKGMK_MD5SUM
626 info "Md5sum updated."
627 exit 0
628 fi
629
630 if [ "$PKGMK_DOWNLOAD_ONLY" = "yes" ]; then
631 download_source
632 exit 0
633 fi
634
92dbaab6
JW
635 if [ "$PKGMK_EXTRACT_ONLY" = "yes" ]; then
636 download_source
637 make_work_dir
638 info "Extracting sources of package '$name-$version'."
639 unpack_source
640 exit 0
641 fi
642
9ac667e6
SR
643 if [ "$PKGMK_UP_TO_DATE" = "yes" ]; then
644 if [ "`build_needed`" = "yes" ]; then
645 info "Package '$TARGET' is not up to date."
646 else
647 info "Package '$TARGET' is up to date."
648 fi
649 exit 0
650 fi
651
652 if [ "`build_needed`" = "no" ] && [ "$PKGMK_FORCE" = "no" ] && [ "$PKGMK_CHECK_MD5SUM" = "no" ]; then
653 info "Package '$TARGET' is up to date."
654 else
655 download_source
656 build_package
657 fi
658
659 if [ "$PKGMK_INSTALL" != "no" ]; then
660 install_package
661 fi
662
663 exit 0
664}
665
666trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
667
668export LC_ALL=POSIX
669
670readonly PKGMK_VERSION="#VERSION#"
671readonly PKGMK_COMMAND="$0"
672readonly PKGMK_ROOT="$PWD"
673
674PKGMK_CONFFILE="/etc/pkgmk.conf"
675PKGMK_PKGFILE="Pkgfile"
676PKGMK_FOOTPRINT=".footprint"
677PKGMK_MD5SUM=".md5sum"
678PKGMK_NOSTRIP=".nostrip"
679
2dee8e17 680PKGMK_SOURCE_MIRRORS=()
9ac667e6
SR
681PKGMK_SOURCE_DIR="$PWD"
682PKGMK_PACKAGE_DIR="$PWD"
683PKGMK_WORK_DIR="$PWD/work"
684
685PKGMK_INSTALL="no"
686PKGMK_RECURSIVE="no"
687PKGMK_DOWNLOAD="no"
688PKGMK_DOWNLOAD_ONLY="no"
92dbaab6 689PKGMK_EXTRACT_ONLY="no"
9ac667e6
SR
690PKGMK_UP_TO_DATE="no"
691PKGMK_UPDATE_FOOTPRINT="no"
692PKGMK_IGNORE_FOOTPRINT="no"
693PKGMK_FORCE="no"
694PKGMK_KEEP_WORK="no"
695PKGMK_UPDATE_MD5SUM="no"
696PKGMK_IGNORE_MD5SUM="no"
697PKGMK_CHECK_MD5SUM="no"
698PKGMK_NO_STRIP="no"
699PKGMK_CLEAN="no"
700
701main "$@"
702
703# End of file