CRUX-ARM : Home

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