makepkg: Document package splitting
[pacman-ng.git] / scripts / makepkg.sh.in
blobfefcc73a04055992433ca075e59f492a79adc6de
1 #!/bin/bash -e
3 # makepkg - make packages compatable for use with pacman
4 # @configure_input@
6 # Copyright (c) 2002-2008 by Judd Vinet <jvinet@zeroflux.org>
7 # Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
8 # Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 # Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
10 # Copyright (c) 2006 by Alex Smith <alex@alex-smith.me.uk>
11 # Copyright (c) 2006 by Andras Voroskoi <voroskoi@frugalware.org>
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 # makepkg uses quite a few external programs during its execution. You
28 # need to have at least the following installed for makepkg to function:
29 # awk, bsdtar (libarchive), bzip2, coreutils, fakeroot, find (findutils),
30 # getopt (util-linux), gettext, grep, gzip, openssl, sed
32 # gettext initialization
33 export TEXTDOMAIN='pacman'
34 export TEXTDOMAINDIR='@localedir@'
36 # file -i does not work on Mac OSX unless legacy mode is set
37 export COMMAND_MODE='legacy'
39 myver='@PACKAGE_VERSION@'
40 confdir='@sysconfdir@'
41 BUILDSCRIPT='@BUILDSCRIPT@'
42 startdir="$PWD"
43 srcdir="$startdir/src"
44 pkgdir="$startdir/pkg"
46 packaging_options=('strip' 'docs' 'libtool' 'emptydirs' 'zipman' 'purge')
47 other_options=('ccache' 'distcc' 'makeflags' 'force')
48 splitpkg_overrides=('pkgdesc' 'license' 'groups' 'depends' 'optdepends' 'provides' \
49 'conflicts' 'replaces' 'backup' 'options' 'install')
50 readonly -a packaging_options other_options splitpkg_overrides
52 # Options
53 ASROOT=0
54 CLEANUP=0
55 CLEANCACHE=0
56 DEP_BIN=0
57 FORCE=0
58 INFAKEROOT=0
59 GENINTEG=0
60 INSTALL=0
61 NOBUILD=0
62 NODEPS=0
63 NOEXTRACT=0
64 RMDEPS=0
65 REPKG=0
66 LOGGING=0
67 SOURCEONLY=0
68 IGNOREARCH=0
69 HOLDVER=0
70 PKGFUNC=0
71 SPLITPKG=0
72 COLORMSG=0
74 # Forces the pkgver of the current PKGBUILD. Used by the fakeroot call
75 # when dealing with svn/cvs/etc PKGBUILDs.
76 FORCE_VER=""
78 PACMAN_OPTS=
80 ### SUBROUTINES ###
82 plain() {
83 local mesg=$1; shift
84 if [ $COLORMSG -eq 1 ]; then
85 printf "\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
86 else
87 printf " ${mesg}\n" "$@" >&2
91 msg() {
92 local mesg=$1; shift
93 if [ $COLORMSG -eq 1 ]; then
94 printf "\033[1;32m==>\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
95 else
96 printf "==> ${mesg}\n" "$@" >&2
100 msg2() {
101 local mesg=$1; shift
102 if [ $COLORMSG -eq 1 ]; then
103 printf "\033[1;34m ->\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
104 else
105 printf " -> ${mesg}\n" "$@" >&2
109 warning() {
110 local mesg=$1; shift
111 if [ $COLORMSG -eq 1 ]; then
112 printf "\033[1;33m==> $(gettext "WARNING:")\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
113 else
114 printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
118 error() {
119 local mesg=$1; shift
120 if [ $COLORMSG -eq 1 ]; then
121 printf "\033[1;31m==> $(gettext "ERROR:")\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
122 else
123 printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
129 # Special exit call for traps, Don't print any error messages when inside,
130 # the fakeroot call, the error message will be printed by the main call.
132 trap_exit() {
133 if [ "$INFAKEROOT" -eq 0 ]; then
134 echo
135 error "$@"
137 exit 1
142 # Clean up function. Called automatically when the script exits.
144 clean_up() {
145 local EXIT_CODE=$?
147 if [ "$INFAKEROOT" -eq 1 ]; then
148 # Don't clean up when leaving fakeroot, we're not done yet.
149 return
152 if [ $EXIT_CODE -eq 0 -a "$CLEANUP" -eq 1 ]; then
153 # If it's a clean exit and -c/--clean has been passed...
154 msg "$(gettext "Cleaning up...")"
155 rm -rf "$pkgdir" "$srcdir"
156 if [ -n "$pkgname" ]; then
157 # Can't do this unless the BUILDSCRIPT has been sourced.
158 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-build.log"*
159 if [ "$PKGFUNC" -eq 1 ]; then
160 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-package.log"*
161 elif [ "$SPLITPKG" -eq 1 ]; then
162 for pkg in ${pkgname[@]}; do
163 rm -f "${pkg}-${pkgver}-${pkgrel}-${CARCH}-package.log"*
164 done
169 remove_deps
174 # Signal Traps
176 trap 'clean_up' 0
177 trap 'trap_exit "$(gettext "TERM signal caught. Exiting...")"' TERM HUP QUIT
178 trap 'trap_exit "$(gettext "Aborted by user! Exiting...")"' INT
179 trap 'trap_exit "$(gettext "An unknown error has occured. Exiting...")"' ERR
181 # a source entry can have two forms :
182 # 1) "filename::http://path/to/file"
183 # 2) "http://path/to/file"
185 # extract the filename from a source entry
186 get_filename() {
187 # if a filename is specified, use it
188 local filename=$(echo $1 | sed 's|::.*||')
189 # if it is just an url, we only keep the last component
190 echo "$filename" | sed 's|^.*://.*/||g'
193 # extract the url from a source entry
194 get_url() {
195 # strip an eventual filename
196 echo $1 | sed 's|.*::||'
200 # Checks to see if options are present in makepkg.conf or PKGBUILD;
201 # PKGBUILD options always take precedence.
203 # usage : check_option( $option )
204 # return : y - enabled
205 # n - disabled
206 # ? - not found
208 check_option() {
209 local ret=$(in_opt_array "$1" ${options[@]})
210 if [ "$ret" != '?' ]; then
211 echo $ret
212 return
215 # fall back to makepkg.conf options
216 ret=$(in_opt_array "$1" ${OPTIONS[@]})
217 if [ "$ret" != '?' ]; then
218 echo $ret
219 return
222 echo '?' # Not Found
227 # Check if option is present in BUILDENV
229 # usage : check_buildenv( $option )
230 # return : y - enabled
231 # n - disabled
232 # ? - not found
234 check_buildenv() {
235 echo $(in_opt_array "$1" ${BUILDENV[@]})
240 # usage : in_opt_array( $needle, $haystack )
241 # return : y - enabled
242 # n - disabled
243 # ? - not found
245 in_opt_array() {
246 local needle=$(echo $1 | tr '[:upper:]' '[:lower:]'); shift
248 local opt
249 for opt in "$@"; do
250 opt=$(echo $opt | tr '[:upper:]' '[:lower:]')
251 if [ "$opt" = "$needle" ]; then
252 echo 'y' # Enabled
253 return
254 elif [ "$opt" = "!$needle" ]; then
255 echo 'n' # Disabled
256 return
258 done
260 echo '?' # Not Found
265 # usage : in_array( $needle, $haystack )
266 # return : 0 - found
267 # 1 - not found
269 in_array() {
270 local needle=$1; shift
271 [ -z "$1" ] && return 1 # Not Found
272 local item
273 for item in "$@"; do
274 [ "$item" = "$needle" ] && return 0 # Found
275 done
276 return 1 # Not Found
279 get_downloadclient() {
280 # $1 = url with valid protocol prefix
281 local url=$1
282 local proto=$(echo "$url" | sed 's|://.*||')
284 # loop through DOWNLOAD_AGENTS variable looking for protocol
285 local i
286 for i in "${DLAGENTS[@]}"; do
287 local handler=$(echo $i | sed 's|::.*||')
288 if [ "$proto" = "$handler" ]; then
289 agent=$(echo $i | sed 's|^.*::||')
290 break
292 done
294 # if we didn't find an agent, return an error
295 if [ -z "$agent" ]; then
296 error "$(gettext "There is no agent set up to handle %s URLs. Check %s.")" "$proto" "$MAKEPKG_CONF"
297 plain "$(gettext "Aborting...")"
298 exit 1 # $E_CONFIG_ERROR
301 # ensure specified program is installed
302 local program="$(echo $agent | awk '{print $1 }')"
303 if [ ! -x "$program" ]; then
304 local baseprog=$(basename $program)
305 error "$(gettext "The download program %s is not installed.")" "$baseprog"
306 plain "$(gettext "Aborting...")"
307 exit 1 # $E_MISSING_PROGRAM
310 echo "$agent"
313 download_file() {
314 # download command
315 local dlcmd=$1
316 # url of the file
317 local url=$2
318 # destination file
319 local file=$3
320 # temporary download file, default to last component of the url
321 local dlfile=$(echo "$url" | sed 's|^.*://.*/||g')
323 # replace %o by the temporary dlfile if it exists
324 if echo "$dlcmd" | grep -q "%o" ; then
325 dlcmd=${dlcmd//\%o/$file.part}
326 dlfile="$file.part"
328 # add the url, either in place of %u or at the end
329 if echo "$dlcmd" | grep -q "%u" ; then
330 dlcmd=${dlcmd//\%u/$url}
331 else
332 dlcmd="$dlcmd $url"
335 $dlcmd || return $?
337 # rename the temporary download file to the final destination
338 if [ "$dlfile" != "$file" ]; then
339 mv -f "$SRCDEST/$dlfile" "$SRCDEST/$file"
343 check_deps() {
344 [ $# -gt 0 ] || return
346 pmout=$(pacman $PACMAN_OPTS -T "$@")
347 ret=$?
348 if [ $ret -eq 127 ]; then #unresolved deps
349 echo "$pmout"
350 elif [ $ret -ne 0 ]; then
351 error "$(gettext "Pacman returned a fatal error (%i): %s")" "$ret" "$pmout"
352 exit 1
356 handledeps() {
357 local R_DEPS_SATISFIED=0
358 local R_DEPS_MISSING=1
360 [ $# -eq 0 ] && return $R_DEPS_SATISFIED
362 local deplist="$*"
364 if [ "$DEP_BIN" -eq 0 ]; then
365 return $R_DEPS_MISSING
368 if [ "$DEP_BIN" -eq 1 ]; then
369 # install missing deps from binary packages (using pacman -S)
370 msg "$(gettext "Installing missing dependencies...")"
371 local ret=0
373 if [ "$ASROOT" -eq 0 ]; then
374 sudo pacman $PACMAN_OPTS -S --asdeps $deplist || ret=$?
375 else
376 pacman $PACMAN_OPTS -S --asdeps $deplist || ret=$?
379 if [ $ret -ne 0 ]; then
380 error "$(gettext "Pacman failed to install missing dependencies.")"
381 exit 1 # TODO: error code
385 # we might need the new system environment
386 # set -e can cause problems during sourcing profile scripts
387 set +e
388 source /etc/profile &>/dev/null
389 set -e
391 return $R_DEPS_SATISFIED
394 resolve_deps() {
395 # $pkgdeps is a GLOBAL variable, used by remove_deps()
396 local R_DEPS_SATISFIED=0
397 local R_DEPS_MISSING=1
399 local deplist="$(check_deps $*)"
400 if [ -z "$deplist" ]; then
401 return $R_DEPS_SATISFIED
404 if handledeps $deplist; then
405 pkgdeps="$pkgdeps $deplist"
406 # check deps again to make sure they were resolved
407 deplist="$(check_deps $*)"
408 [ -z "$deplist" ] && return $R_DEPS_SATISFIED
409 elif [ "$DEP_BIN" -eq 1 ]; then
410 error "$(gettext "Failed to install all missing dependencies.")"
413 msg "$(gettext "Missing Dependencies:")"
414 local dep
415 for dep in $deplist; do
416 msg2 "$dep"
417 done
419 return $R_DEPS_MISSING
422 # fix flyspray bug #5923
423 remove_deps() {
424 # $pkgdeps is a GLOBAL variable, set by resolve_deps()
425 [ "$RMDEPS" -eq 0 ] && return
426 [ -z "$pkgdeps" ] && return
428 local dep depstrip deplist
429 deplist=""
430 for dep in $pkgdeps; do
431 depstrip=$(echo $dep | sed -e 's|=.*$||' -e 's|>.*$||' -e 's|<.*$||')
432 deplist="$deplist $depstrip"
433 done
435 msg "Removing installed dependencies..."
436 local ret=0
437 if [ "$ASROOT" -eq 0 ]; then
438 sudo pacman $PACMAN_OPTS -Rns $deplist || ret=$?
439 else
440 pacman $PACMAN_OPTS -Rns $deplist || ret=$?
443 # Fixes FS#10039 - exit cleanly as package has built successfully
444 if [ $ret -ne 0 ]; then
445 warning "$(gettext "Failed to remove installed dependencies.")"
446 return 0
450 download_sources() {
451 msg "$(gettext "Retrieving Sources...")"
453 if [ ! -w "$SRCDEST" ] ; then
454 error "$(gettext "You do not have write permission to store downloads in %s.")" "$SRCDEST"
455 plain "$(gettext "Aborting...")"
456 exit 1
459 pushd "$SRCDEST" &>/dev/null
461 local netfile
462 for netfile in "${source[@]}"; do
463 local file=$(get_filename "$netfile")
464 local url=$(get_url "$netfile")
465 if [ -f "$startdir/$file" ]; then
466 msg2 "$(gettext "Found %s in build dir")" "$file"
467 rm -f "$srcdir/$file"
468 ln -s "$startdir/$file" "$srcdir/"
469 continue
470 elif [ -f "$SRCDEST/$file" ]; then
471 msg2 "$(gettext "Using cached copy of %s")" "$file"
472 rm -f "$srcdir/$file"
473 ln -s "$SRCDEST/$file" "$srcdir/"
474 continue
477 # if we get here, check to make sure it was a URL, else fail
478 if [ "$file" = "$url" ]; then
479 error "$(gettext "%s was not found in the build directory and is not a URL.")" "$file"
480 exit 1 # $E_MISSING_FILE
483 # find the client we should use for this URL
484 local dlclient=$(get_downloadclient "$url") || exit $?
486 msg2 "$(gettext "Downloading %s...")" "$file"
487 # fix flyspray bug #3289
488 local ret=0
489 download_file "$dlclient" "$url" "$file" || ret=$?
490 if [ $ret -gt 0 ]; then
491 error "$(gettext "Failure while downloading %s")" "$file"
492 plain "$(gettext "Aborting...")"
493 exit 1
495 rm -f "$srcdir/$file"
496 ln -s "$SRCDEST/$file" "$srcdir/"
497 done
499 popd &>/dev/null
502 generate_checksums() {
503 msg "$(gettext "Generating checksums for source files...")"
504 plain ""
506 if [ ! $(type -p openssl) ]; then
507 error "$(gettext "Cannot find openssl.")"
508 exit 1 # $E_MISSING_PROGRAM
511 local integ
512 for integ in ${INTEGRITY_CHECK[@]}; do
513 integ="$(echo $integ | tr '[:upper:]' '[:lower:]')"
514 case "$integ" in
515 md5|sha1|sha256|sha384|sha512) : ;;
517 error "$(gettext "Invalid integrity algorithm '%s' specified.")" "$integ"
518 exit 1;; # $E_CONFIG_ERROR
519 esac
521 local ct=0
522 local numsrc=${#source[@]}
523 echo -n "${integ}sums=("
525 local i=0;
526 local indent=''
527 while [ $i -lt $((${#integ}+6)) ]; do
528 indent="$indent "
529 i=$(($i+1))
530 done
532 local netfile
533 for netfile in "${source[@]}"; do
534 local file="$(get_filename "$netfile")"
536 if [ ! -f "$file" ] ; then
537 if [ ! -f "$SRCDEST/$file" ] ; then
538 error "$(gettext "Unable to find source file %s to generate checksum.")" "$file"
539 plain "$(gettext "Aborting...")"
540 exit 1
541 else
542 file="$SRCDEST/$file"
546 local sum="$(openssl dgst -${integ} "$file" | awk '{print $NF}')"
547 [ $ct -gt 0 ] && echo -n "$indent"
548 echo -n "'$sum'"
549 ct=$(($ct+1))
550 [ $ct -lt $numsrc ] && echo
551 done
553 echo ")"
554 done
557 check_checksums() {
558 [ ${#source[@]} -eq 0 ] && return 0
560 if [ ! $(type -p openssl) ]; then
561 error "$(gettext "Cannot find openssl.")"
562 exit 1 # $E_MISSING_PROGRAM
565 local correlation=0
566 local integ required
567 for integ in md5 sha1 sha256 sha384 sha512; do
568 local integrity_sums=($(eval echo "\${${integ}sums[@]}"))
569 if [ ${#integrity_sums[@]} -eq ${#source[@]} ]; then
570 msg "$(gettext "Validating source files with %s...")" "${integ}sums"
571 correlation=1
572 local errors=0
573 local idx=0
574 local file
575 for file in "${source[@]}"; do
576 local found=1
577 file="$(get_filename "$file")"
578 echo -n " $file ... " >&2
580 if [ ! -f "$file" ] ; then
581 if [ ! -f "$SRCDEST/$file" ] ; then
582 echo "$(gettext "NOT FOUND")" >&2
583 errors=1
584 found=0
585 else
586 file="$SRCDEST/$file"
590 if [ $found -gt 0 ] ; then
591 local expectedsum="$(echo ${integrity_sums[$idx]} | tr '[A-F]' '[a-f]')"
592 local realsum="$(openssl dgst -${integ} "$file" | awk '{print $NF}')"
593 if [ "$expectedsum" = "$realsum" ]; then
594 echo "$(gettext "Passed")" >&2
595 else
596 echo "$(gettext "FAILED")" >&2
597 errors=1
601 idx=$((idx + 1))
602 done
604 if [ $errors -gt 0 ]; then
605 error "$(gettext "One or more files did not pass the validity check!")"
606 exit 1 # TODO: error code
608 elif [ ${#integrity_sums[@]} -gt 0 ]; then
609 error "$(gettext "Integrity checks (%s) differ in size from the source array.")" "$integ"
610 exit 1 # TODO: error code
612 done
614 if [ $correlation -eq 0 ]; then
615 error "$(gettext "Integrity checks are missing.")"
616 exit 1 # TODO: error code
620 extract_sources() {
621 msg "$(gettext "Extracting Sources...")"
622 local netfile
623 for netfile in "${source[@]}"; do
624 file=$(get_filename "$netfile")
625 if in_array "$file" ${noextract[@]}; then
626 #skip source files in the noextract=() array
627 # these are marked explicitly to NOT be extracted
628 continue
631 if [ ! -f "$file" ] ; then
632 if [ ! -f "$SRCDEST/$file" ] ; then
633 error "$(gettext "Unable to find source file %s for extraction.")" "$file"
634 plain "$(gettext "Aborting...")"
635 exit 1
636 else
637 file="$SRCDEST/$file"
641 # fix flyspray #6246
642 local file_type=$(file -bizL "$file")
643 local cmd=''
644 case "$file_type" in
645 *application/x-tar*|*application/zip*|*application/x-zip*|*application/x-cpio*)
646 cmd="bsdtar -x -f $file" ;;
647 *application/x-gzip*)
648 cmd="gunzip -d -f $file" ;;
649 *application/x-bzip*)
650 cmd="bunzip2 -f $file" ;;
652 # Don't know what to use to extract this file,
653 # skip to the next file
654 continue;;
655 esac
657 local ret=0
658 msg2 "$cmd"
659 $cmd || ret=$?
660 if [ $ret -ne 0 ]; then
661 error "$(gettext "Failed to extract %s")" "$file"
662 plain "$(gettext "Aborting...")"
663 exit 1
665 done
667 if [ $EUID -eq 0 ]; then
668 # change perms of all source files to root user & root group
669 chown -R 0:0 "$srcdir"
673 run_build() {
674 # use distcc if it is requested (check buildenv and PKGBUILD opts)
675 if [ "$(check_buildenv distcc)" = "y" -a "$(check_option distcc)" != "n" ]; then
676 [ -d /usr/lib/distcc/bin ] && export PATH="/usr/lib/distcc/bin:$PATH"
677 export DISTCC_HOSTS
678 elif [ "$(check_option distcc)" = "n" ]; then
679 # if it is not wanted, clear the makeflags too
680 MAKEFLAGS=""
683 # use ccache if it is requested (check buildenv and PKGBUILD opts)
684 if [ "$(check_buildenv ccache)" = "y" -a "$(check_option ccache)" != "n" ]; then
685 [ -d /usr/lib/ccache/bin ] && export PATH="/usr/lib/ccache/bin:$PATH"
688 # clear user-specified makeflags if requested
689 if [ "$(check_option makeflags)" = "n" ]; then
690 MAKEFLAGS=""
693 msg "$(gettext "Starting build()...")"
694 cd "$srcdir"
696 # ensure all necessary build variables are exported
697 export CFLAGS CXXFLAGS MAKEFLAGS LDFLAGS CHOST
698 # save our shell options so build() can't override what we need
699 local shellopts=$(shopt -p)
701 local ret=0
702 if [ "$LOGGING" -eq 1 ]; then
703 BUILDLOG="${startdir}/${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-build.log"
704 if [ -f "$BUILDLOG" ]; then
705 local i=1
706 while true; do
707 if [ -f "$BUILDLOG.$i" ]; then
708 i=$(($i +1))
709 else
710 break
712 done
713 mv "$BUILDLOG" "$BUILDLOG.$i"
716 build 2>&1 | tee "$BUILDLOG"; ret=${PIPESTATUS[0]}
717 else
718 build 2>&1 || ret=$?
720 # reset our shell options
721 eval "$shellopts"
723 if [ $ret -gt 0 ]; then
724 error "$(gettext "Build Failed.")"
725 plain "$(gettext "Aborting...")"
726 remove_deps
727 exit 2 # $E_BUILD_FAILED
731 run_package() {
732 if [ -z "$1" ]; then
733 pkgfunc="package"
734 nameofpkg="$pkgname"
735 else
736 pkgfunc="package_$1"
737 nameofpkg="$1"
740 # clear user-specified makeflags if requested
741 if [ "$(check_option makeflags)" = "n" ]; then
742 MAKEFLAGS=""
745 msg "$(gettext "Starting %s()...")" "$pkgfunc"
746 cd "$srcdir"
748 # ensure all necessary build variables are exported
749 export CFLAGS CXXFLAGS LDFLAGS MAKEFLAGS CHOST
751 local ret=0
752 if [ "$LOGGING" -eq 1 ]; then
753 BUILDLOG="${startdir}/${nameofpkg}-${pkgver}-${pkgrel}-${CARCH}-package.log"
754 if [ -f "$BUILDLOG" ]; then
755 local i=1
756 while true; do
757 if [ -f "$BUILDLOG.$i" ]; then
758 i=$(($i +1))
759 else
760 break
762 done
763 mv "$BUILDLOG" "$BUILDLOG.$i"
766 # ensure overridden package variables suvrive tee with split packages
767 logpipe=$(mktemp -u "$startdir/logpipe.XXXXXXXX")
768 mknod "$logpipe" p
769 exec 3>&1
770 tee "$BUILDLOG" < "$logpipe" &
771 exec 1>"$logpipe" 2>"$logpipe"
772 $pkgfunc 2>&1 || ret=$?
773 sync
774 exec 1>&3 2>&3 3>&-
775 rm "$logpipe"
776 else
777 $pkgfunc 2>&1 || ret=$?
780 if [ $ret -gt 0 ]; then
781 error "$(gettext "Packaging Failed.")"
782 plain "$(gettext "Aborting...")"
783 remove_deps
784 exit 2 # $E_BUILD_FAILED
788 tidy_install() {
789 cd "$pkgdir"
790 msg "$(gettext "Tidying install...")"
792 if [ "$(check_option docs)" = "n" -a -n "${DOC_DIRS[*]}" ]; then
793 msg2 "$(gettext "Removing doc files...")"
794 rm -rf ${DOC_DIRS[@]}
797 if [ "$(check_option purge)" = "y" -a -n "${PURGE_TARGETS[*]}" ]; then
798 msg2 "$(gettext "Purging other files...")"
799 local pt
800 for pt in "${PURGE_TARGETS[@]}"; do
801 if [ "${pt}" = "${pt//\/}" ]; then
802 find . -type f -name "${pt}" -exec rm -f -- '{}' \;
803 else
804 rm -f ${pt}
806 done
809 if [ "$(check_option zipman)" = "y" -a -n "${MAN_DIRS[*]}" ]; then
810 msg2 "$(gettext "Compressing man and info pages...")"
811 local manpage ext file link hardlinks hl
812 find ${MAN_DIRS[@]} -type f 2>/dev/null |
813 while read manpage ; do
814 # check file still exists (potentially compressed with hard link)
815 if [ -f ${manpage} ]; then
816 ext="${manpage##*.}"
817 file="${manpage##*/}"
818 if [ "$ext" != "gz" -a "$ext" != "bz2" ]; then
819 # update symlinks to this manpage
820 find ${MAN_DIRS[@]} -lname "$file" 2>/dev/null |
821 while read link ; do
822 rm -f "$link"
823 ln -sf "${file}.gz" "${link}.gz"
824 done
825 # find hard links and remove them
826 # the '|| true' part keeps the script from bailing if find returned an
827 # error, such as when one of the man directories doesn't exist
828 hardlinks="$(find ${MAN_DIRS[@]} \! -name "$file" -samefile "$manpage" 2>/dev/null)" || true
829 for hl in ${hardlinks}; do
830 rm -f "${hl}";
831 done
832 # compress the original
833 gzip -9 "$manpage"
834 # recreate hard links removed earlier
835 for hl in ${hardlinks}; do
836 ln "${manpage}.gz" "${hl}.gz"
837 chmod 644 ${hl}.gz
838 done
841 done
844 if [ "$(check_option strip)" = "y" -a -n "${STRIP_DIRS[*]}" ]; then
845 msg2 "$(gettext "Stripping debugging symbols from binaries and libraries...")"
846 local binary
847 find ${STRIP_DIRS[@]} -type f 2>/dev/null | while read binary ; do
848 case "$(file -biz "$binary")" in
849 *compressed-encoding*) # Skip compressed binarys
851 *application/x-sharedlib*) # Libraries (.so)
852 /usr/bin/strip -S "$binary";;
853 *application/x-archive*) # Libraries (.a)
854 /usr/bin/strip -S "$binary";;
855 *application/x-executable*) # Binaries
856 /usr/bin/strip "$binary";;
857 esac
858 done
861 if [ "$(check_option libtool)" = "n" ]; then
862 msg2 "$(gettext "Removing libtool .la files...")"
863 find . ! -type d -name "*.la" -exec rm -f -- '{}' \;
866 if [ "$(check_option emptydirs)" = "n" ]; then
867 msg2 "$(gettext "Removing empty directories...")"
868 find . -depth -type d -empty -delete
872 create_package() {
873 if [ -z "$1" ]; then
874 nameofpkg="$pkgname"
875 else
876 nameofpkg="$1"
879 if [ ! -d "$pkgdir" ]; then
880 error "$(gettext "Missing pkg/ directory.")"
881 plain "$(gettext "Aborting...")"
882 exit 1 # $E_MISSING_PKGDIR
885 cd "$pkgdir"
886 msg "$(gettext "Creating package...")"
888 local builddate=$(date -u "+%s")
889 if [ -n "$PACKAGER" ]; then
890 local packager="$PACKAGER"
891 else
892 local packager="Unknown Packager"
894 local size=$(du -sk | awk '{print $1 * 1024}')
896 # write the .PKGINFO file
897 msg2 "$(gettext "Generating .PKGINFO file...")"
898 echo "# Generated by makepkg $myver" >.PKGINFO
899 if [ "$INFAKEROOT" -eq 1 ]; then
900 echo "# using $(fakeroot -v)" >>.PKGINFO
902 echo "# $(LC_ALL=C date -u)" >>.PKGINFO
903 echo "pkgname = $nameofpkg" >>.PKGINFO
904 echo "pkgver = $pkgver-$pkgrel" >>.PKGINFO
905 echo "pkgdesc = $pkgdesc" >>.PKGINFO
906 echo "url = $url" >>.PKGINFO
907 echo "builddate = $builddate" >>.PKGINFO
908 echo "packager = $packager" >>.PKGINFO
909 echo "size = $size" >>.PKGINFO
910 if [ -n "$CARCH" ]; then
911 echo "arch = $CARCH" >>.PKGINFO
913 if [ "$(check_option force)" = "y" ]; then
914 echo "force = true" >> .PKGINFO
917 local it
918 for it in "${license[@]}"; do
919 echo "license = $it" >>.PKGINFO
920 done
921 for it in "${replaces[@]}"; do
922 echo "replaces = $it" >>.PKGINFO
923 done
924 for it in "${groups[@]}"; do
925 echo "group = $it" >>.PKGINFO
926 done
927 for it in "${depends[@]}"; do
928 echo "depend = $it" >>.PKGINFO
929 done
930 for it in "${optdepends[@]}"; do
931 echo "optdepend = $it" >>.PKGINFO
932 done
933 for it in "${conflicts[@]}"; do
934 echo "conflict = $it" >>.PKGINFO
935 done
936 for it in "${provides[@]}"; do
937 echo "provides = $it" >>.PKGINFO
938 done
939 for it in "${backup[@]}"; do
940 echo "backup = $it" >>.PKGINFO
941 done
942 for it in "${packaging_options[@]}"; do
943 local ret="$(check_option $it)"
944 if [ "$ret" != "?" ]; then
945 if [ "$ret" = "y" ]; then
946 echo "makepkgopt = $it" >>.PKGINFO
947 else
948 echo "makepkgopt = !$it" >>.PKGINFO
951 done
953 # TODO maybe remove this at some point
954 # warn if license array is not present or empty
955 if [ -z "$license" ]; then
956 warning "$(gettext "Please add a license line to your %s!")" "$BUILDSCRIPT"
957 plain "$(gettext "Example for GPL\'ed software: license=('GPL').")"
960 local comp_files=".PKGINFO"
962 # check for an install script
963 if [ -n "$install" ]; then
964 msg2 "$(gettext "Adding install script...")"
965 cp "$startdir/$install" .INSTALL
966 comp_files="$comp_files .INSTALL"
969 # do we have a changelog?
970 if [ -f "$startdir/ChangeLog" ]; then
971 msg2 "$(gettext "Adding package changelog...")"
972 cp "$startdir/ChangeLog" .CHANGELOG
973 comp_files="$comp_files .CHANGELOG"
976 # tar it up
977 msg2 "$(gettext "Compressing package...")"
979 case "$PKGEXT" in
980 *tar.gz) EXT=${PKGEXT%.gz} ;;
981 *tar.bz2) EXT=${PKGEXT%.bz2} ;;
982 *) warning "$(gettext "'%s' is not a valid archive extension.")" \
983 "$PKGEXT" ; EXT=$PKGEXT ;;
984 esac
985 local pkg_file="$PKGDEST/${nameofpkg}-${pkgver}-${pkgrel}-${CARCH}${EXT}"
987 local ret=0
989 # when fileglobbing, we want * in an empty directory to expand to
990 # the null string rather than itself
991 shopt -s nullglob
992 bsdtar -cf - $comp_files * > "$pkg_file" || ret=$?
993 shopt -u nullglob
995 if [ $ret -eq 0 ]; then
996 case "$PKGEXT" in
997 *tar.gz) gzip -f -n "$pkg_file" ;;
998 *tar.bz2) bzip2 -f "$pkg_file" ;;
999 esac
1000 ret=$?
1003 if [ $ret -ne 0 ]; then
1004 error "$(gettext "Failed to create package file.")"
1005 exit 1 # TODO: error code
1009 create_srcpackage() {
1010 cd "$startdir"
1011 if [ "$SOURCEONLY" -eq 2 ]; then
1012 # get back to our src directory so we can begin with sources
1013 mkdir -p "$srcdir"
1014 cd "$srcdir"
1015 download_sources
1016 # we can only check checksums if we have all files
1017 check_checksums
1018 cd "$startdir"
1020 msg "$(gettext "Creating source package...")"
1021 local srclinks="$(mktemp -d "$startdir"/srclinks.XXXXXXXXX)"
1022 mkdir "${srclinks}"/${pkgbase}
1024 msg2 "$(gettext "Adding %s...")" "$BUILDSCRIPT"
1025 ln -s "${startdir}/${BUILDSCRIPT}" "${srclinks}/${pkgbase}/"
1027 if [ -n "$install" ]; then
1028 if [ -f $install ]; then
1029 msg2 "$(gettext "Adding install script...")"
1030 ln -s "${startdir}/$install" "${srclinks}/${pkgbase}/"
1031 else
1032 error "$(gettext "Install script %s not found.")" "$install"
1036 if [ -f ChangeLog ]; then
1037 msg2 "$(gettext "Adding %s...")" "ChangeLog"
1038 ln -s "${startdir}/ChangeLog" "${srclinks}/${pkgbase}"
1041 local netfile
1042 for netfile in "${source[@]}"; do
1043 local file=$(get_filename "$netfile")
1044 if [ -f "$netfile" ]; then
1045 msg2 "$(gettext "Adding %s...")" "$netfile"
1046 ln -s "${startdir}/$netfile" "${srclinks}/${pkgbase}"
1047 elif [ "$SOURCEONLY" -eq 2 -a -f "$SRCDEST/$file" ]; then
1048 msg2 "$(gettext "Adding %s...")" "$file"
1049 ln -s "$SRCDEST/$file" "${srclinks}/${pkgbase}/"
1051 done
1053 local TAR_OPT
1054 case "$SRCEXT" in
1055 *tar.gz) TAR_OPT="z" ;;
1056 *tar.bz2) TAR_OPT="j" ;;
1057 *) warning "$(gettext "'%s' is not a valid archive extension.")" \
1058 "$SRCEXT" ;;
1059 esac
1061 local pkg_file="$PKGDEST/${pkgbase}-${pkgver}-${pkgrel}${SRCEXT}"
1063 # tar it up
1064 msg2 "$(gettext "Compressing source package...")"
1065 cd "${srclinks}"
1066 if ! bsdtar -c${TAR_OPT}Lf "$pkg_file" ${pkgbase}; then
1067 error "$(gettext "Failed to create source package file.")"
1068 exit 1 # TODO: error code
1070 cd "${startdir}"
1071 rm -rf "${srclinks}"
1074 install_package() {
1075 [ "$INSTALL" -eq 0 ] && return
1077 if [ "$SPLITPKG" -eq 0 ]; then
1078 msg "$(gettext "Installing package ${pkgname} with pacman -U...")"
1079 else
1080 msg "$(gettext "Installing ${pkgbase} package group with pacman -U...")"
1083 local pkglist
1084 for pkg in ${pkgname[@]}; do
1085 pkglist="${pkglist} $PKGDEST/${pkg}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT}"
1086 done
1088 local ret=0
1089 if [ "$ASROOT" -eq 0 ]; then
1090 sudo pacman $PACMAN_OPTS -U ${pkglist} || ret=$?
1091 else
1092 pacman $PACMAN_OPTS -U ${pkglist} || ret=$?
1095 if [ $ret -ne 0 ]; then
1096 warning "$(gettext "Failed to install built package(s).")"
1097 return 0
1101 devel_check() {
1102 newpkgver=""
1104 # Do not update pkgver if --holdver is set, when building a source package,
1105 # or when reading PKGBUILD from pipe
1106 if [ "$HOLDVER" -eq 1 -o "$SOURCEONLY" -ne 0 -o ! -f "./$BUILDSCRIPT" ]; then
1107 return
1110 if [ -z "$FORCE_VER" ]; then
1111 # Check if this is a svn/cvs/etc PKGBUILD; set $newpkgver if so.
1112 # This will only be used on the first call to makepkg; subsequent
1113 # calls to makepkg via fakeroot will explicitly pass the version
1114 # number to avoid having to determine the version number twice.
1115 # Also do a brief check to make sure we have the VCS tool available.
1116 oldpkgver=$pkgver
1117 if [ -n "${_darcstrunk}" -a -n "${_darcsmod}" ] ; then
1118 [ $(type -p darcs) ] || return 0
1119 msg "$(gettext "Determining latest darcs revision...")"
1120 newpkgver=$(date +%Y%m%d)
1121 elif [ -n "${_cvsroot}" -a -n "${_cvsmod}" ] ; then
1122 [ $(type -p cvs) ] || return 0
1123 msg "$(gettext "Determining latest cvs revision...")"
1124 newpkgver=$(date +%Y%m%d)
1125 elif [ -n "${_gitroot}" -a -n "${_gitname}" ] ; then
1126 [ $(type -p git) ] || return 0
1127 msg "$(gettext "Determining latest git revision...")"
1128 newpkgver=$(date +%Y%m%d)
1129 elif [ -n "${_svntrunk}" -a -n "${_svnmod}" ] ; then
1130 [ $(type -p svn) ] || return 0
1131 msg "$(gettext "Determining latest svn revision...")"
1132 newpkgver=$(LC_ALL=C svn info $_svntrunk | sed -n 's/^Last Changed Rev: \([0-9]*\)$/\1/p')
1133 elif [ -n "${_bzrtrunk}" -a -n "${_bzrmod}" ] ; then
1134 [ $(type -p bzr) ] || return 0
1135 msg "$(gettext "Determining latest bzr revision...")"
1136 newpkgver=$(bzr revno ${_bzrtrunk})
1137 elif [ -n "${_hgroot}" -a -n "${_hgrepo}" ] ; then
1138 [ $(type -p hg) ] || return 0
1139 msg "$(gettext "Determining latest hg revision...")"
1140 if [ -d ./src/$_hgrepo ] ; then
1141 cd ./src/$_hgrepo
1142 hg pull
1143 hg update
1144 else
1145 [[ ! -d ./src/ ]] && mkdir ./src/
1146 hg clone $_hgroot/$_hgrepo ./src/$_hgrepo
1147 cd ./src/$_hgrepo
1149 newpkgver=$(hg tip --template "{rev}")
1150 cd ../../
1153 if [ -n "$newpkgver" ]; then
1154 msg2 "$(gettext "Version found: %s")" "$newpkgver"
1157 else
1158 # Version number retrieved from fakeroot->makepkg argument
1159 newpkgver=$FORCE_VER
1163 devel_update() {
1164 # This is lame, but if we're wanting to use an updated pkgver for
1165 # retrieving svn/cvs/etc sources, we'll update the PKGBUILD with
1166 # the new pkgver and then re-source it. This is the most robust
1167 # method for dealing with PKGBUILDs that use, e.g.:
1169 # pkgver=23
1170 # ...
1171 # _foo=pkgver
1173 if [ -n "$newpkgver" ]; then
1174 if [ "$newpkgver" != "$pkgver" ]; then
1175 if [ -f "./$BUILDSCRIPT" ]; then
1176 @SEDINPLACE@ "s/^pkgver=[^ ]*/pkgver=$newpkgver/" "./$BUILDSCRIPT"
1177 @SEDINPLACE@ "s/^pkgrel=[^ ]*/pkgrel=1/" "./$BUILDSCRIPT"
1178 source "$BUILDSCRIPT"
1184 backup_package_variables() {
1185 for var in ${splitpkg_overrides[@]}; do
1186 indirect="${var}_backup"
1187 eval "${indirect}=\"${!var}\""
1188 done
1191 restore_package_variables() {
1192 for var in ${splitpkg_overrides[@]}; do
1193 indirect="${var}_backup"
1194 if [ -n "${!indirect}" ]; then
1195 eval "${var}=\"${!indirect}\""
1196 else
1197 unset ${var}
1199 done
1202 # getopt like parser
1203 parse_options() {
1204 local short_options=$1; shift;
1205 local long_options=$1; shift;
1206 local ret=0;
1207 local unused_options=""
1209 while [ -n "$1" ]; do
1210 if [ ${1:0:2} = '--' ]; then
1211 if [ -n "${1:2}" ]; then
1212 local match=""
1213 for i in ${long_options//,/ }; do
1214 if [ ${1:2} = ${i//:} ]; then
1215 match=$i
1216 break
1218 done
1219 if [ -n "$match" ]; then
1220 if [ ${1:2} = $match ]; then
1221 printf ' %s' "$1"
1222 else
1223 if [ -n "$2" ]; then
1224 printf ' %s' "$1"
1225 shift
1226 printf " '%s'" "$1"
1227 else
1228 echo "makepkg: option '$1' $(gettext "requires an argument")" >&2
1229 ret=1
1232 else
1233 echo "makepkg: $(gettext "unrecognized option") '$1'" >&2
1234 ret=1
1236 else
1237 shift
1238 break
1240 elif [ ${1:0:1} = '-' ]; then
1241 for ((i=1; i<${#1}; i++)); do
1242 if [[ "$short_options" =~ "${1:i:1}" ]]; then
1243 if [[ "$short_options" =~ "${1:i:1}:" ]]; then
1244 if [ -n "${1:$i+1}" ]; then
1245 printf ' -%s' "${1:i:1}"
1246 printf " '%s'" "${1:$i+1}"
1247 else
1248 if [ -n "$2" ]; then
1249 printf ' -%s' "${1:i:1}"
1250 shift
1251 printf " '%s'" "${1}"
1252 else
1253 echo "makepkg: option $(gettext "requires an argument") -- '${1:i:1}'" >&2
1254 ret=1
1257 break
1258 else
1259 printf ' -%s' "${1:i:1}"
1261 else
1262 echo "makepkg: $(gettext "invalid option") -- '${1:i:1}'" >&2
1263 ret=1
1265 done
1266 else
1267 unused_options="${unused_options} '$1'"
1269 shift
1270 done
1272 printf " --"
1273 if [ -n "$unused_options" ]; then
1274 for i in ${unused_options[@]}; do
1275 printf ' %s' "$i"
1276 done
1278 if [ -n "$1" ]; then
1279 while [ -n "$1" ]; do
1280 printf " '%s'" "${1}"
1281 shift
1282 done
1284 printf "\n"
1286 return $ret
1289 usage() {
1290 printf "makepkg (pacman) %s\n" "$myver"
1291 echo
1292 printf "$(gettext "Usage: %s [options]")\n" "$0"
1293 echo
1294 echo "$(gettext "Options:")"
1295 printf "$(gettext " -A, --ignorearch Ignore incomplete arch field in %s")\n" "$BUILDSCRIPT"
1296 echo "$(gettext " -c, --clean Clean up work files after build")"
1297 echo "$(gettext " -C, --cleancache Clean up source files from the cache")"
1298 printf "$(gettext " --config <config> Use an alternate config file (instead of '%s')")\n" "$confdir/makepkg.conf"
1299 echo "$(gettext " -d, --nodeps Skip all dependency checks")"
1300 echo "$(gettext " -e, --noextract Do not extract source files (use existing src/ dir)")"
1301 echo "$(gettext " -f, --force Overwrite existing package")"
1302 echo "$(gettext " -g, --geninteg Generate integrity checks for source files")"
1303 echo "$(gettext " -h, --help This help")"
1304 echo "$(gettext " -i, --install Install package after successful build")"
1305 echo "$(gettext " -L, --log Log package build process")"
1306 echo "$(gettext " -m, --nocolor Disable colorized output messages")"
1307 echo "$(gettext " -o, --nobuild Download and extract files only")"
1308 printf "$(gettext " -p <buildscript> Use an alternate build script (instead of '%s')")\n" "$BUILDSCRIPT"
1309 echo "$(gettext " -r, --rmdeps Remove installed dependencies after a successful build")"
1310 # fix flyspray feature request #2978
1311 echo "$(gettext " -R, --repackage Repackage contents of the package without rebuilding")"
1312 echo "$(gettext " -s, --syncdeps Install missing dependencies with pacman")"
1313 echo "$(gettext " --allsource Generate a source-only tarball including downloaded sources")"
1314 echo "$(gettext " --asroot Allow makepkg to run as root user")"
1315 echo "$(gettext " --holdver Prevent automatic version bumping for development PKGBUILDs")"
1316 echo "$(gettext " --source Generate a source-only tarball without downloaded sources")"
1317 echo
1318 echo "$(gettext "These options can be passed to pacman:")"
1319 echo
1320 echo "$(gettext " --noconfirm Do not ask for confirmation when resolving dependencies")"
1321 echo "$(gettext " --noprogressbar Do not show a progress bar when downloading files")"
1322 echo
1323 printf "$(gettext "If -p is not specified, makepkg will look for '%s'")\n" "$BUILDSCRIPT"
1324 echo
1327 version() {
1328 printf "makepkg (pacman) %s\n" "$myver"
1329 printf "$(gettext "\
1330 Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.org>.\n\n\
1331 This is free software; see the source for copying conditions.\n\
1332 There is NO WARRANTY, to the extent permitted by law.\n")"
1335 # PROGRAM START
1337 # determine whether we have gettext; make it a no-op if we do not
1338 if [ ! $(type -t gettext) ]; then
1339 gettext() {
1340 echo "$@"
1344 ARGLIST=$@
1346 # Parse Command Line Options.
1347 OPT_SHORT="AbcCdefFghiLmop:rRsV"
1348 OPT_LONG="allsource,asroot,ignorearch,builddeps,clean,cleancache,nodeps"
1349 OPT_LONG="$OPT_LONG,noextract,force,forcever:,geninteg,help,holdver"
1350 OPT_LONG="$OPT_LONG,install,log,nocolor,nobuild,rmdeps,repackage,source"
1351 OPT_LONG="$OPT_LONG,syncdeps,version,config:"
1352 # Pacman Options
1353 OPT_LONG="$OPT_LONG,noconfirm,noprogressbar"
1354 OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@" || echo 'PARSE_OPTIONS FAILED')"
1355 if echo "$OPT_TEMP" | grep -q 'PARSE_OPTIONS FAILED'; then
1356 # This is a small hack to stop the script bailing with 'set -e'
1357 echo; usage; exit 1 # E_INVALID_OPTION;
1359 eval set -- "$OPT_TEMP"
1360 unset OPT_SHORT OPT_LONG OPT_TEMP
1362 while true; do
1363 case "$1" in
1364 # Pacman Options
1365 --noconfirm) PACMAN_OPTS="$PACMAN_OPTS --noconfirm" ;;
1366 --noprogressbar) PACMAN_OPTS="$PACMAN_OPTS --noprogressbar" ;;
1368 # Makepkg Options
1369 --allsource) SOURCEONLY=2 ;;
1370 --asroot) ASROOT=1 ;;
1371 -A|--ignorearch) IGNOREARCH=1 ;;
1372 -c|--clean) CLEANUP=1 ;;
1373 -C|--cleancache) CLEANCACHE=1 ;;
1374 --config) shift; MAKEPKG_CONF=$1 ;;
1375 -d|--nodeps) NODEPS=1 ;;
1376 -e|--noextract) NOEXTRACT=1 ;;
1377 -f|--force) FORCE=1 ;;
1378 #hidden opt used by fakeroot call for svn/cvs/etc PKGBUILDs to set pkgver
1379 --forcever) shift; FORCE_VER=$1;;
1380 -F) INFAKEROOT=1 ;;
1381 -g|--geninteg) GENINTEG=1 ;;
1382 --holdver) HOLDVER=1 ;;
1383 -i|--install) INSTALL=1 ;;
1384 -L|--log) LOGGING=1 ;;
1385 -m|--nocolor) USE_COLOR='n' ;;
1386 -o|--nobuild) NOBUILD=1 ;;
1387 -p) shift; BUILDSCRIPT=$1 ;;
1388 -r|--rmdeps) RMDEPS=1 ;;
1389 -R|--repackage) REPKG=1 ;;
1390 --source) SOURCEONLY=1 ;;
1391 -s|--syncdeps) DEP_BIN=1 ;;
1393 -h|--help) usage; exit 0 ;; # E_OK
1394 -V|--version) version; exit 0 ;; # E_OK
1396 --) OPT_IND=0; shift; break;;
1397 *) usage; exit 1 ;; # E_INVALID_OPTION
1398 esac
1399 shift
1400 done
1402 #preserve environment variables
1403 _PKGDEST=${PKGDEST}
1404 _SRCDEST=${SRCDEST}
1406 # default config is makepkg.conf
1407 MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
1409 # Source the config file; fail if it is not found
1410 if [ -r "$MAKEPKG_CONF" ]; then
1411 source "$MAKEPKG_CONF"
1412 else
1413 error "$(gettext "%s not found.")" "$MAKEPKG_CONF"
1414 plain "$(gettext "Aborting...")"
1415 exit 1 # $E_CONFIG_ERROR
1418 # Source user-specific makepkg.conf overrides
1419 if [ -r ~/.makepkg.conf ]; then
1420 source ~/.makepkg.conf
1423 # check if messages are to be printed using color
1424 if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
1425 COLORMSG=1
1428 # override settings with an environment variable for batch processing
1429 PKGDEST=${_PKGDEST:-$PKGDEST}
1430 PKGDEST=${PKGDEST:-$startdir} #default to $startdir if undefined
1431 SRCDEST=${_SRCDEST:-$SRCDEST}
1432 SRCDEST=${SRCDEST:-$startdir} #default to $startdir if undefined
1435 if [ "$HOLDVER" -eq 1 -a -n "$FORCE_VER" ]; then
1436 # The '\\0' is here to prevent gettext from thinking --holdver is an option
1437 error "$(gettext "\\0--holdver and --forcever cannot both be specified" )"
1438 exit 1
1441 if [ "$CLEANCACHE" -eq 1 ]; then
1442 #fix flyspray feature request #5223
1443 if [ -n "$SRCDEST" -a "$SRCDEST" != "$startdir" ]; then
1444 msg "$(gettext "Cleaning up ALL files from %s.")" "$SRCDEST"
1445 echo -n "$(gettext " Are you sure you wish to do this? ")"
1446 echo -n "$(gettext "[Y/n]")"
1447 read answer
1448 answer=$(echo $answer | tr '[:lower:]' '[:upper:]')
1449 if [ "$answer" = "$(gettext "YES")" -o "$answer" = "$(gettext "Y")" ]; then
1450 rm "$SRCDEST"/*
1451 if [ $? -ne 0 ]; then
1452 error "$(gettext "Problem removing files; you may not have correct permissions in %s")" "$SRCDEST"
1453 exit 1
1454 else
1455 # removal worked
1456 msg "$(gettext "Source cache cleaned.")"
1457 exit 0
1459 else
1460 # answer = no
1461 msg "$(gettext "No files have been removed.")"
1462 exit 0
1464 else
1465 # $SRCDEST is $startdir, two possibilities
1466 error "$(gettext "Source destination must be defined in %s.")" "$MAKEPKG_CONF"
1467 plain "$(gettext "In addition, please run makepkg -C outside of your cache directory.")"
1468 exit 1
1472 if [ -z "$BUILDSCRIPT" ]; then
1473 error "$(gettext "BUILDSCRIPT is undefined! Ensure you have updated %s.")" "$MAKEPKG_CONF"
1474 exit 1
1477 if [ "$INFAKEROOT" -eq 0 ]; then
1478 if [ $EUID -eq 0 -a "$ASROOT" -eq 0 ]; then
1479 # Warn those who like to live dangerously.
1480 error "$(gettext "Running makepkg as root is a BAD idea and can cause")"
1481 plain "$(gettext "permanent, catastrophic damage to your system. If you")"
1482 plain "$(gettext "wish to run as root, please use the --asroot option.")"
1483 exit 1 # $E_USER_ABORT
1484 elif [ $EUID -gt 0 -a "$ASROOT" -eq 1 ]; then
1485 # Warn those who try to use the --asroot option when they are not root
1486 error "$(gettext "The --asroot option is meant for the root user only.")"
1487 plain "$(gettext "Please rerun makepkg without the --asroot flag.")"
1488 exit 1 # $E_USER_ABORT
1489 elif [ "$(check_buildenv fakeroot)" = "y" -a $EUID -gt 0 ]; then
1490 if [ ! $(type -p fakeroot) ]; then
1491 error "$(gettext "Fakeroot must be installed if using the 'fakeroot' option")"
1492 plain "$(gettext "in the BUILDENV array in %s.")" "$MAKEPKG_CONF"
1493 exit 1
1495 elif [ $EUID -gt 0 ]; then
1496 warning "$(gettext "Running makepkg as an unprivileged user will result in non-root")"
1497 plain "$(gettext "ownership of the packaged files. Try using the fakeroot environment by")"
1498 plain "$(gettext "placing 'fakeroot' in the BUILDENV array in %s.")" "$MAKEPKG_CONF"
1499 sleep 1
1501 else
1502 if [ -z "$FAKEROOTKEY" ]; then
1503 error "$(gettext "Do not use the '-F' option. This option is only for use by makepkg.")"
1504 exit 1 # TODO: error code
1508 # check for sudo if we will need it during makepkg execution
1509 if [ "$ASROOT" -eq 0 \
1510 -a \( "$DEP_BIN" -eq 1 -o "$RMDEPS" -eq 1 -o "$INSTALL" -eq 1 \) ]; then
1511 if [ ! "$(type -p sudo)" ]; then
1512 error "$(gettext "Cannot find the sudo binary! Is sudo installed?")"
1513 plain "$(gettext "Missing dependencies cannot be installed or removed as a normal user")"
1514 plain "$(gettext "without sudo; install and configure sudo to auto-resolve dependencies.")"
1515 exit 1
1519 unset pkgname pkgbase pkgver pkgrel pkgdesc url license groups provides
1520 unset md5sums replaces depends conflicts backup source install build
1521 unset makedepends optdepends options noextract
1523 if [ ! -f "$BUILDSCRIPT" ]; then
1524 if [ -t 0 ]; then
1525 error "$(gettext "%s does not exist.")" "$BUILDSCRIPT"
1526 exit 1
1527 else
1528 # PKGBUILD passed through a pipe
1529 BUILDSCRIPT=/dev/stdin
1531 else
1532 crlftest=$(file $BUILDSCRIPT | grep -F 'CRLF' || true)
1533 if [ -n "$crlftest" ]; then
1534 error "$(gettext "%s contains CRLF characters and cannot be sourced.")" "$BUILDSCRIPT"
1535 exit 1
1539 source "$BUILDSCRIPT"
1541 if [ "$GENINTEG" -eq 1 ]; then
1542 mkdir -p "$srcdir"
1543 cd "$srcdir"
1544 download_sources
1545 generate_checksums
1546 exit 0 # $E_OK
1549 if [ "$(type -t package)" = "function" ]; then
1550 PKGFUNC=1
1553 # check for no-no's in the build script
1554 if [ -z "$pkgname" ]; then
1555 error "$(gettext "%s is not allowed to be empty.")" "pkgname"
1556 exit 1
1558 if [ -z "$pkgver" ]; then
1559 error "$(gettext "%s is not allowed to be empty.")" "pkgver"
1560 exit 1
1562 if [ -z "$pkgrel" ]; then
1563 error "$(gettext "%s is not allowed to be empty.")" "pkgrel"
1564 exit 1
1566 if [ "$pkgver" != "${pkgver//-/}" ]; then
1567 error "$(gettext "%s is not allowed to contain hyphens.")" "pkgver"
1568 exit 1
1570 if [ "$pkgrel" != "${pkgrel//-/}" ]; then
1571 error "$(gettext "%s is not allowed to contain hyphens.")" "pkgrel"
1572 exit 1
1575 if [ "$arch" = 'any' ]; then
1576 CARCH='any'
1579 pkgbase=${pkgbase:-${pkgname[0]}}
1581 if ! in_array $CARCH ${arch[@]}; then
1582 if [ "$IGNOREARCH" -eq 0 ]; then
1583 error "$(gettext "%s is not available for the '%s' architecture.")" "$pkgbase" "$CARCH"
1584 plain "$(gettext "Note that many packages may need a line added to their %s")" "$BUILDSCRIPT"
1585 plain "$(gettext "such as arch=('%s').")" "$CARCH"
1586 exit 1
1590 for provide in ${provides[@]}; do
1591 if [ $provide != ${provide//</} -o $provide != ${provide//>/} ]; then
1592 error "$(gettext "Provides array cannot contain comparison (< or >) operators.")"
1593 exit 1
1595 done
1596 unset provide
1598 if [ "$install" -a ! -f "$install" ]; then
1599 error "$(gettext "Install scriptlet (%s) does not exist.")" "$install"
1600 exit 1
1603 valid_options=1
1604 for opt in ${options[@]}; do
1605 known=0
1606 # check if option matches a known option or its inverse
1607 for kopt in ${packaging_options[@]} ${other_options[@]}; do
1608 if [ "${opt}" = "${kopt}" -o "${opt}" = "!${kopt}" ]; then
1609 known=1
1611 done
1612 if [ $known -eq 0 ]; then
1613 error "$(gettext "options array contains unknown option '%s'")" "$opt"
1614 valid_options=0
1616 done
1617 if [ $valid_options -eq 0 ]; then
1618 exit 1
1620 unset valid_options opt known kopt
1622 # We need to run devel_update regardless of whether we are in the fakeroot
1623 # build process so that if the user runs makepkg --forcever manually, we
1624 # 1) output the correct pkgver, and 2) use the correct filename when
1625 # checking if the package file already exists - fixes FS #9194
1626 devel_check
1627 devel_update
1629 if [ "${#pkgname[@]}" -gt "1" ]; then
1630 SPLITPKG=1
1633 if [ -f "$PKGDEST/${pkgname}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT}" \
1634 -a "$FORCE" -eq 0 -a "$SOURCEONLY" -eq 0 -a "$NOBUILD" -eq 0 ]; then
1635 if [ "$INSTALL" -eq 1 ]; then
1636 warning "$(gettext "A package has already been built, installing existing package...")"
1637 install_package
1638 exit $?
1639 else
1640 error "$(gettext "A package has already been built. (use -f to overwrite)")"
1641 exit 1
1645 # Run the bare minimum in fakeroot
1646 if [ "$INFAKEROOT" -eq 1 ]; then
1647 if [ "$SPLITPKG" -eq 0 ]; then
1648 if [ "$PKGFUNC" -eq 0 ]; then
1649 if [ "$REPKG" -eq 0 ]; then
1650 run_build
1651 tidy_install
1653 else
1654 run_package
1655 tidy_install
1657 create_package
1658 else
1659 for pkg in ${pkgname[@]}; do
1660 pkgdir="$pkgdir/$pkg"
1661 mkdir -p "$pkgdir"
1662 backup_package_variables
1663 run_package $pkg
1664 tidy_install
1665 create_package $pkg
1666 restore_package_variables
1667 pkgdir="${pkgdir%/*}"
1668 done
1671 msg "$(gettext "Leaving fakeroot environment.")"
1672 exit 0 # $E_OK
1675 msg "$(gettext "Making package: %s")" "$pkgbase $pkgver-$pkgrel $CARCH ($(date))"
1677 # if we are creating a source-only package, go no further
1678 if [ "$SOURCEONLY" -ne 0 ]; then
1679 if [ -f "$PKGDEST/${pkgbase}-${pkgver}-${pkgrel}${SRCEXT}" \
1680 -a "$FORCE" -eq 0 ]; then
1681 error "$(gettext "A package has already been built. (use -f to overwrite)")"
1682 exit 1
1684 create_srcpackage
1685 msg "$(gettext "Source package created: %s")" "$pkgbase ($(date))"
1686 exit 0
1689 # fix flyspray bug #5973
1690 if [ "$NODEPS" -eq 1 -o "$NOBUILD" -eq 1 -o "$REPKG" -eq 1 ]; then
1691 # no warning message needed for nobuild, repkg
1692 if [ "$NODEPS" -eq 1 ]; then
1693 warning "$(gettext "Skipping dependency checks.")"
1695 elif [ $(type -p pacman) ]; then
1696 unset pkgdeps # Set by resolve_deps() and used by remove_deps()
1697 deperr=0
1699 msg "$(gettext "Checking Runtime Dependencies...")"
1700 resolve_deps ${depends[@]} || deperr=1
1702 msg "$(gettext "Checking Buildtime Dependencies...")"
1703 resolve_deps ${makedepends[@]} || deperr=1
1705 if [ $deperr -eq 1 ]; then
1706 error "$(gettext "Could not resolve all dependencies.")"
1707 exit 1
1709 else
1710 warning "$(gettext "pacman was not found in PATH; skipping dependency checks.")"
1713 # ensure we have a sane umask set
1714 umask 0022
1716 # get back to our src directory so we can begin with sources
1717 mkdir -p "$srcdir"
1718 cd "$srcdir"
1720 if [ "$NOEXTRACT" -eq 1 ]; then
1721 warning "$(gettext "Skipping source retrieval -- using existing src/ tree")"
1722 warning "$(gettext "Skipping source integrity checks -- using existing src/ tree")"
1723 warning "$(gettext "Skipping source extraction -- using existing src/ tree")"
1725 if [ "$NOEXTRACT" -eq 1 -a -z "$(ls "$srcdir" 2>/dev/null)" ]; then
1726 error "$(gettext "The source directory is empty, there is nothing to build!")"
1727 plain "$(gettext "Aborting...")"
1728 exit 1
1730 elif [ "$REPKG" -eq 1 ]; then
1731 if [ "$PKGFUNC" -eq 0 -a "$SPLITPKG" -eq 0 \
1732 -a \( ! -d "$pkgdir" -o -z "$(ls "$pkgdir" 2>/dev/null)" \) ]; then
1733 error "$(gettext "The package directory is empty, there is nothing to repackage!")"
1734 plain "$(gettext "Aborting...")"
1735 exit 1
1737 else
1738 download_sources
1739 check_checksums
1740 extract_sources
1743 if [ "$NOBUILD" -eq 1 ]; then
1744 msg "$(gettext "Sources are ready.")"
1745 exit 0 #E_OK
1746 else
1747 # check for existing pkg directory; don't remove if we are repackaging
1748 if [ -d "$pkgdir" \
1749 -a \( "$REPKG" -eq 0 -o "$PKGFUNC" -eq 1 -o "$SPLITPKG" -eq 1 \) ]; then
1750 msg "$(gettext "Removing existing pkg/ directory...")"
1751 rm -rf "$pkgdir"
1753 mkdir -p "$pkgdir"
1754 cd "$startdir"
1756 # if we are root or if fakeroot is not enabled, then we don't use it
1757 if [ "$(check_buildenv fakeroot)" != "y" -o $EUID -eq 0 ]; then
1758 if [ "$REPKG" -eq 0 ]; then
1759 devel_update
1760 run_build
1762 if [ "$SPLITPKG" -eq 0 ]; then
1763 if [ "$PKGFUNC" -eq 1 ]; then
1764 run_package
1765 tidy_install
1766 elif [ "$REPKG" -eq 0 ]; then
1767 tidy_install
1769 create_package
1770 else
1771 for pkg in ${pkgname[@]}; do
1772 pkgdir="$pkgdir/$pkg"
1773 mkdir -p "$pkgdir"
1774 backup_package_variables
1775 run_package $pkg
1776 tidy_install
1777 create_package $pkg
1778 restore_package_variables
1779 pkgdir="${pkgdir%/*}"
1780 done
1782 else
1783 if [ "$REPKG" -eq 0 -a \( "$PKGFUNC" -eq 1 -o "$SPLITPKG" -eq 1 \) ]; then
1784 devel_update
1785 run_build
1786 cd "$startdir"
1789 msg "$(gettext "Entering fakeroot environment...")"
1791 if [ -n "$newpkgver" ]; then
1792 fakeroot -- $0 --forcever $newpkgver -F $ARGLIST || exit $?
1793 else
1794 fakeroot -- $0 -F $ARGLIST || exit $?
1799 msg "$(gettext "Finished making: %s")" "$pkgbase $pkgver-$pkgrel $CARCH ($(date))"
1801 install_package
1803 exit 0 #E_OK
1805 # vim: set ts=2 sw=2 noet: