CRUX-ARM : Home

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