makepkg: allow to specify an alternative pacman command
[pacman-ng.git] / scripts / makepkg.sh.in
blobef884b7667dd292ff35ea8dc7297faf9c357aa9c
1 #!/bin/bash -e
3 # makepkg - make packages compatible for use with pacman
4 # @configure_input@
6 # Copyright (c) 2006-2009 Pacman Development Team <pacman-dev@archlinux.org>
7 # Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
8 # Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
9 # Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
10 # Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
11 # Copyright (c) 2006 by Alex Smith <alex@alex-smith.me.uk>
12 # Copyright (c) 2006 by Andras Voroskoi <voroskoi@frugalware.org>
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 # makepkg uses quite a few external programs during its execution. You
29 # need to have at least the following installed for makepkg to function:
30 # bsdtar (libarchive), bzip2, coreutils, fakeroot, find (findutils),
31 # getopt (util-linux), gettext, grep, gzip, openssl, sed, tput (ncurses)
33 # gettext initialization
34 export TEXTDOMAIN='pacman'
35 export TEXTDOMAINDIR='@localedir@'
37 # file -i does not work on Mac OSX unless legacy mode is set
38 export COMMAND_MODE='legacy'
40 myver='@PACKAGE_VERSION@'
41 confdir='@sysconfdir@'
42 BUILDSCRIPT='@BUILDSCRIPT@'
43 startdir="$PWD"
44 srcdir="$startdir/src"
45 pkgdir="$startdir/pkg"
47 packaging_options=('strip' 'docs' 'libtool' 'emptydirs' 'zipman' 'purge')
48 other_options=('ccache' 'distcc' 'makeflags' 'force')
49 splitpkg_overrides=('pkgver' 'pkgrel' 'pkgdesc' 'arch' 'license' 'groups' \
50 'depends' 'optdepends' 'provides' 'conflicts' 'replaces' \
51 'backup' 'options' 'install' 'changelog')
52 readonly -a packaging_options other_options splitpkg_overrides
54 # Options
55 ASROOT=0
56 CLEANUP=0
57 CLEANCACHE=0
58 DEP_BIN=0
59 FORCE=0
60 INFAKEROOT=0
61 GENINTEG=0
62 SKIPINTEG=0
63 INSTALL=0
64 NOBUILD=0
65 NODEPS=0
66 NOEXTRACT=0
67 RMDEPS=0
68 REPKG=0
69 LOGGING=0
70 SOURCEONLY=0
71 IGNOREARCH=0
72 HOLDVER=0
73 BUILDFUNC=0
74 PKGFUNC=0
75 SPLITPKG=0
76 PKGLIST=""
78 # Forces the pkgver of the current PKGBUILD. Used by the fakeroot call
79 # when dealing with svn/cvs/etc PKGBUILDs.
80 FORCE_VER=""
82 PACMAN_OPTS=
84 ### SUBROUTINES ###
86 plain() {
87 local mesg=$1; shift
88 printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
91 msg() {
92 local mesg=$1; shift
93 printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
96 msg2() {
97 local mesg=$1; shift
98 printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
101 warning() {
102 local mesg=$1; shift
103 printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
106 error() {
107 local mesg=$1; shift
108 printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
113 # Special exit call for traps, Don't print any error messages when inside,
114 # the fakeroot call, the error message will be printed by the main call.
116 trap_exit() {
117 if (( ! INFAKEROOT )); then
118 echo
119 error "$@"
121 exit 1
126 # Clean up function. Called automatically when the script exits.
128 clean_up() {
129 local EXIT_CODE=$?
131 if (( INFAKEROOT )); then
132 # Don't clean up when leaving fakeroot, we're not done yet.
133 return
136 if (( ! EXIT_CODE && CLEANUP )); then
137 # If it's a clean exit and -c/--clean has been passed...
138 msg "$(gettext "Cleaning up...")"
139 rm -rf "$pkgdir" "$srcdir"
140 if [[ -n $pkgbase ]]; then
141 # Can't do this unless the BUILDSCRIPT has been sourced.
142 if (( BUILDFUNC )); then
143 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-build.log"*
145 if (( PKGFUNC )); then
146 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-package.log"*
147 elif (( SPLITPKG )); then
148 for pkg in ${pkgname[@]}; do
149 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-package_${pkg}.log"*
150 done
153 # clean up dangling symlinks to packages
154 for pkg in ${pkgname[@]}; do
155 for file in ${pkg}-*-*-${CARCH}${PKGEXT}; do
156 if [[ -h $file && ! -e $file ]]; then
157 rm -f $file
159 done
160 done
164 remove_deps
169 # Signal Traps
171 set -E
172 trap 'clean_up' 0
173 trap 'trap_exit "$(gettext "TERM signal caught. Exiting...")"' TERM HUP QUIT
174 trap 'trap_exit "$(gettext "Aborted by user! Exiting...")"' INT
175 trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR
177 # a source entry can have two forms :
178 # 1) "filename::http://path/to/file"
179 # 2) "http://path/to/file"
181 # extract the filename from a source entry
182 get_filename() {
183 # if a filename is specified, use it
184 local filename="${1%%::*}"
185 # if it is just an URL, we only keep the last component
186 echo "${filename##*/}"
189 # extract the URL from a source entry
190 get_url() {
191 # strip an eventual filename
192 echo "${1#*::}"
196 # Checks to see if options are present in makepkg.conf or PKGBUILD;
197 # PKGBUILD options always take precedence.
199 # usage : check_option( $option )
200 # return : y - enabled
201 # n - disabled
202 # ? - not found
204 check_option() {
205 local ret=$(in_opt_array "$1" ${options[@]})
206 if [[ $ret != '?' ]]; then
207 echo $ret
208 return
211 # fall back to makepkg.conf options
212 ret=$(in_opt_array "$1" ${OPTIONS[@]})
213 if [[ $ret != '?' ]]; then
214 echo $ret
215 return
218 echo '?' # Not Found
223 # Check if option is present in BUILDENV
225 # usage : check_buildenv( $option )
226 # return : y - enabled
227 # n - disabled
228 # ? - not found
230 check_buildenv() {
231 echo $(in_opt_array "$1" ${BUILDENV[@]})
236 # usage : in_opt_array( $needle, $haystack )
237 # return : y - enabled
238 # n - disabled
239 # ? - not found
241 in_opt_array() {
242 local needle="${1,,}"; shift
244 local opt
245 for opt in "$@"; do
246 opt="${opt,,}"
247 if [[ $opt = $needle ]]; then
248 echo 'y' # Enabled
249 return
250 elif [[ $opt = "!$needle" ]]; then
251 echo 'n' # Disabled
252 return
254 done
256 echo '?' # Not Found
261 # usage : in_array( $needle, $haystack )
262 # return : 0 - found
263 # 1 - not found
265 in_array() {
266 local needle=$1; shift
267 [[ -z $1 ]] && return 1 # Not Found
268 local item
269 for item in "$@"; do
270 [[ $item = $needle ]] && return 0 # Found
271 done
272 return 1 # Not Found
275 get_downloadclient() {
276 # $1 = URL with valid protocol prefix
277 local url=$1
278 local proto="${url%%://*}"
280 # loop through DOWNLOAD_AGENTS variable looking for protocol
281 local i
282 for i in "${DLAGENTS[@]}"; do
283 local handler="${i%%::*}"
284 if [[ $proto = $handler ]]; then
285 agent="${i##*::}"
286 break
288 done
290 # if we didn't find an agent, return an error
291 if [[ -z $agent ]]; then
292 error "$(gettext "There is no agent set up to handle %s URLs. Check %s.")" "$proto" "$MAKEPKG_CONF"
293 plain "$(gettext "Aborting...")"
294 exit 1 # $E_CONFIG_ERROR
297 # ensure specified program is installed
298 local program="${agent%% *}"
299 if [[ ! -x $program ]]; then
300 local baseprog=$(basename $program)
301 error "$(gettext "The download program %s is not installed.")" "$baseprog"
302 plain "$(gettext "Aborting...")"
303 exit 1 # $E_MISSING_PROGRAM
306 echo "$agent"
309 download_file() {
310 # download command
311 local dlcmd=$1
312 # URL of the file
313 local url=$2
314 # destination file
315 local file=$3
316 # temporary download file, default to last component of the URL
317 local dlfile="${url##*/}"
319 # replace %o by the temporary dlfile if it exists
320 if echo "$dlcmd" | grep -q "%o" ; then
321 dlcmd=${dlcmd//\%o/\"$file.part\"}
322 dlfile="$file.part"
324 # add the URL, either in place of %u or at the end
325 if echo "$dlcmd" | grep -q "%u" ; then
326 dlcmd=${dlcmd//\%u/\"$url\"}
327 else
328 dlcmd="$dlcmd \"$url\""
331 local ret=0
332 eval "$dlcmd || ret=\$?"
333 if (( ret )); then
334 [[ ! -s $dlfile ]] && rm -f -- "$dlfile"
335 return $ret
338 # rename the temporary download file to the final destination
339 if [[ $dlfile != $file ]]; then
340 mv -f "$SRCDEST/$dlfile" "$SRCDEST/$file"
344 run_pacman() {
345 local ret=0
346 if (( ! ASROOT )) && [[ $1 != "-T" ]]; then
347 sudo $PACMAN $PACMAN_OPTS "$@" || ret=$?
348 else
349 $PACMAN $PACMAN_OPTS "$@" || ret=$?
351 return $ret
354 check_deps() {
355 (( $# > 0 )) || return
357 local ret=0
358 pmout=$(run_pacman -T "$@")
359 ret=$?
360 if (( ret == 127 )); then #unresolved deps
361 echo "$pmout"
362 elif (( ret )); then
363 error "$(gettext "'%s' returned a fatal error (%i): %s")" "$PACMAN" "$ret" "$pmout"
364 exit 1
368 handle_deps() {
369 local R_DEPS_SATISFIED=0
370 local R_DEPS_MISSING=1
372 (( $# == 0 )) && return $R_DEPS_SATISFIED
374 local deplist="$*"
376 if (( ! DEP_BIN )); then
377 return $R_DEPS_MISSING
380 if (( DEP_BIN )); then
381 # install missing deps from binary packages (using pacman -S)
382 msg "$(gettext "Installing missing dependencies...")"
384 if ! run_pacman -S --asdeps $deplist; then
385 error "$(gettext "'%s' failed to install missing dependencies.")" "$PACMAN"
386 exit 1 # TODO: error code
390 # we might need the new system environment
391 # avoid triggering the ERR trap
392 local restoretrap=$(trap -p ERR)
393 trap - ERR
394 source /etc/profile &>/dev/null
395 eval $restoretrap
397 return $R_DEPS_SATISFIED
400 resolve_deps() {
401 # $pkgdeps is a GLOBAL variable, used by remove_deps()
402 local R_DEPS_SATISFIED=0
403 local R_DEPS_MISSING=1
405 local deplist="$(check_deps $*)"
406 if [[ -z $deplist ]]; then
407 return $R_DEPS_SATISFIED
410 if handle_deps $deplist; then
411 pkgdeps="$pkgdeps $deplist"
412 # check deps again to make sure they were resolved
413 deplist="$(check_deps $*)"
414 [[ -z $deplist ]] && return $R_DEPS_SATISFIED
415 elif (( DEP_BIN )); then
416 error "$(gettext "Failed to install all missing dependencies.")"
419 msg "$(gettext "Missing Dependencies:")"
420 local dep
421 for dep in $deplist; do
422 msg2 "$dep"
423 done
425 return $R_DEPS_MISSING
428 # fix flyspray bug #5923
429 remove_deps() {
430 # $pkgdeps is a GLOBAL variable, set by resolve_deps()
431 (( ! RMDEPS )) && return
432 [[ -z $pkgdeps ]] && return
434 local dep depstrip deplist
435 deplist=""
436 for dep in $pkgdeps; do
437 depstrip="${dep%%[<=>]*}"
438 deplist="$deplist $depstrip"
439 done
441 msg "Removing installed dependencies..."
443 # exit cleanly on failure to remove deps as package has been built successfully
444 if ! run_pacman -Rns $deplist; 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 )); 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="${integ,,}"
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
526 local indent=''
527 for (( i = 0; i < ${#integ} + 6; i++ )); do
528 indent="$indent "
529 done
531 local netfile
532 for netfile in "${source[@]}"; do
533 local file="$(get_filename "$netfile")"
535 if [[ ! -f $file ]] ; then
536 if [[ ! -f $SRCDEST/$file ]] ; then
537 error "$(gettext "Unable to find source file %s to generate checksum.")" "$file"
538 plain "$(gettext "Aborting...")"
539 exit 1
540 else
541 file="$SRCDEST/$file"
545 local sum="$(openssl dgst -${integ} "$file")"
546 sum=${sum##* }
547 (( ct )) && echo -n "$indent"
548 echo -n "'$sum'"
549 ct=$(($ct+1))
550 (( $ct < $numsrc )) && echo
551 done
553 echo ")"
554 done
557 check_checksums() {
558 (( ! ${#source[@]} )) && 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[@]} == ${#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 )) ; then
591 local expectedsum="${integrity_sums[$idx],,}"
592 local realsum="$(openssl dgst -${integ} "$file")"
593 realsum="${realsum##* }"
594 if [[ $expectedsum = $realsum ]]; then
595 echo "$(gettext "Passed")" >&2
596 else
597 echo "$(gettext "FAILED")" >&2
598 errors=1
602 idx=$((idx + 1))
603 done
605 if (( errors )); then
606 error "$(gettext "One or more files did not pass the validity check!")"
607 exit 1 # TODO: error code
609 elif (( ${#integrity_sums[@]} )); then
610 error "$(gettext "Integrity checks (%s) differ in size from the source array.")" "$integ"
611 exit 1 # TODO: error code
613 done
615 if (( ! correlation )); then
616 if (( SKIPINTEG )); then
617 warning "$(gettext "Integrity checks are missing.")"
618 else
619 error "$(gettext "Integrity checks are missing.")"
620 exit 1 # TODO: error code
625 extract_sources() {
626 msg "$(gettext "Extracting Sources...")"
627 local netfile
628 for netfile in "${source[@]}"; do
629 file=$(get_filename "$netfile")
630 if in_array "$file" ${noextract[@]}; then
631 #skip source files in the noextract=() array
632 # these are marked explicitly to NOT be extracted
633 continue
636 if [[ ! -f $file ]] ; then
637 if [[ ! -f $SRCDEST/$file ]] ; then
638 error "$(gettext "Unable to find source file %s for extraction.")" "$file"
639 plain "$(gettext "Aborting...")"
640 exit 1
641 else
642 file="$SRCDEST/$file"
646 # fix flyspray #6246
647 local file_type=$(file -bizL "$file")
648 local ext=${file##*.}
649 local cmd=''
650 case "$file_type" in
651 *application/x-tar*|*application/zip*|*application/x-zip*|*application/x-cpio*)
652 cmd="bsdtar" ;;
653 *application/x-gzip*)
654 case "$ext" in
655 gz|z|Z) cmd="gzip" ;;
656 *) continue;;
657 esac ;;
658 *application/x-bzip*)
659 case "$ext" in
660 bz2|bz) cmd="bzip2" ;;
661 *) continue;;
662 esac ;;
663 *application/x-xz*)
664 case "$ext" in
665 xz) cmd="xz" ;;
666 *) continue;;
667 esac ;;
669 # Don't know what to use to extract this file,
670 # skip to the next file
671 continue;;
672 esac
674 local ret=0
675 msg2 "$(gettext "Extracting %s with %s")" "$file" "$cmd"
676 if [[ $cmd = bsdtar ]]; then
677 $cmd -xf "$file" || ret=?
678 else
679 rm -f "${file%.*}"
680 $cmd -dcf "$file" > "${file%.*}" || ret=?
682 if (( ret )); then
683 error "$(gettext "Failed to extract %s")" "$file"
684 plain "$(gettext "Aborting...")"
685 exit 1
687 done
689 if (( EUID == 0 )); then
690 # change perms of all source files to root user & root group
691 chown -R 0:0 "$srcdir"
695 error_function() {
696 if [[ -p $logpipe ]]; then
697 rm "$logpipe"
699 # first exit all subshells, then print the error
700 if (( ! BASH_SUBSHELL )); then
701 plain "$(gettext "Aborting...")"
702 remove_deps
704 exit 2 # $E_BUILD_FAILED
707 run_function() {
708 if [[ -z $1 ]]; then
709 return 1
711 pkgfunc="$1"
713 # clear user-specified makeflags if requested
714 if [[ $(check_option makeflags) = "n" ]]; then
715 MAKEFLAGS=""
718 msg "$(gettext "Starting %s()...")" "$pkgfunc"
719 cd "$srcdir"
721 # ensure all necessary build variables are exported
722 export CFLAGS CXXFLAGS LDFLAGS MAKEFLAGS CHOST
723 # save our shell options so pkgfunc() can't override what we need
724 local shellopts=$(shopt -p)
726 local ret=0
727 if (( LOGGING )); then
728 BUILDLOG="${startdir}/${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-$pkgfunc.log"
729 if [[ -f $BUILDLOG ]]; then
730 local i=1
731 while true; do
732 if [[ -f $BUILDLOG.$i ]]; then
733 i=$(($i +1))
734 else
735 break
737 done
738 mv "$BUILDLOG" "$BUILDLOG.$i"
741 # ensure overridden package variables survive tee with split packages
742 logpipe=$(mktemp -u "$startdir/logpipe.XXXXXXXX")
743 mknod "$logpipe" p
744 exec 3>&1
745 tee "$BUILDLOG" < "$logpipe" &
746 exec 1>"$logpipe" 2>"$logpipe"
747 restoretrap=$(trap -p ERR)
748 trap 'error_function' ERR
749 $pkgfunc 2>&1
750 eval $restoretrap
751 sync
752 exec 1>&3 2>&3 3>&-
753 rm "$logpipe"
754 else
755 restoretrap=$(trap -p ERR)
756 trap 'error_function' ERR
757 $pkgfunc 2>&1
758 eval $restoretrap
760 # reset our shell options
761 eval "$shellopts"
764 run_build() {
765 # use distcc if it is requested (check buildenv and PKGBUILD opts)
766 if [[ $(check_buildenv distcc) = "y" && $(check_option distcc) != "n" ]]; then
767 [[ -d /usr/lib/distcc/bin ]] && export PATH="/usr/lib/distcc/bin:$PATH"
768 export DISTCC_HOSTS
769 elif [[ $(check_option distcc) = "n" ]]; then
770 # if it is not wanted, clear the makeflags too
771 MAKEFLAGS=""
774 # use ccache if it is requested (check buildenv and PKGBUILD opts)
775 if [[ $(check_buildenv ccache) = "y" && $(check_option ccache) != "n" ]]; then
776 [[ -d /usr/lib/ccache/bin ]] && export PATH="/usr/lib/ccache/bin:$PATH"
779 run_function "build"
782 run_package() {
783 if [[ -z $1 ]]; then
784 pkgfunc="package"
785 else
786 pkgfunc="package_$1"
789 run_function "$pkgfunc"
792 tidy_install() {
793 cd "$pkgdir"
794 msg "$(gettext "Tidying install...")"
796 if [[ $(check_option docs) = "n" && -n ${DOC_DIRS[*]} ]]; then
797 msg2 "$(gettext "Removing doc files...")"
798 rm -rf ${DOC_DIRS[@]}
801 if [[ $(check_option purge) = "y" && -n ${PURGE_TARGETS[*]} ]]; then
802 msg2 "$(gettext "Purging other files...")"
803 local pt
804 for pt in "${PURGE_TARGETS[@]}"; do
805 if [[ ${pt} = ${pt//\/} ]]; then
806 find . -type f -name "${pt}" -exec rm -f -- '{}' \;
807 else
808 rm -f ${pt}
810 done
813 if [[ $(check_option zipman) = "y" && -n ${MAN_DIRS[*]} ]]; then
814 msg2 "$(gettext "Compressing man and info pages...")"
815 local manpage ext file link hardlinks hl
816 find ${MAN_DIRS[@]} -type f 2>/dev/null |
817 while read manpage ; do
818 # check file still exists (potentially compressed with hard link)
819 if [[ -f ${manpage} ]]; then
820 ext="${manpage##*.}"
821 file="${manpage##*/}"
822 if [[ $ext != gz && $ext != bz2 ]]; then
823 # update symlinks to this manpage
824 find ${MAN_DIRS[@]} -lname "$file" 2>/dev/null |
825 while read link ; do
826 rm -f "$link"
827 ln -sf "${file}.gz" "${link}.gz"
828 done
829 # find hard links and remove them
830 # the '|| true' part keeps the script from bailing if find returned an
831 # error, such as when one of the man directories doesn't exist
832 hardlinks="$(find ${MAN_DIRS[@]} \! -name "$file" -samefile "$manpage" 2>/dev/null)" || true
833 for hl in ${hardlinks}; do
834 rm -f "${hl}";
835 done
836 # compress the original
837 gzip -9 "$manpage"
838 # recreate hard links removed earlier
839 for hl in ${hardlinks}; do
840 ln "${manpage}.gz" "${hl}.gz"
841 chmod 644 ${hl}.gz
842 done
845 done
848 if [[ $(check_option strip) = y && -n ${STRIP_DIRS[*]} ]]; then
849 msg2 "$(gettext "Stripping debugging symbols from binaries and libraries...")"
850 local binary
851 find ${STRIP_DIRS[@]} -type f 2>/dev/null | while read binary ; do
852 case "$(file -biz "$binary")" in
853 *compressed-encoding*) # Skip compressed binaries
855 *application/x-sharedlib*) # Libraries (.so)
856 /usr/bin/strip -S "$binary";;
857 *application/x-archive*) # Libraries (.a)
858 /usr/bin/strip -S "$binary";;
859 *application/x-executable*) # Binaries
860 /usr/bin/strip "$binary";;
861 esac
862 done
865 if [[ $(check_option libtool) = "n" ]]; then
866 msg2 "$(gettext "Removing libtool .la files...")"
867 find . ! -type d -name "*.la" -exec rm -f -- '{}' \;
870 if [[ $(check_option emptydirs) = "n" ]]; then
871 msg2 "$(gettext "Removing empty directories...")"
872 find . -depth -type d -empty -delete
876 write_pkginfo() {
877 local builddate=$(date -u "+%s")
878 if [[ -n $PACKAGER ]]; then
879 local packager="$PACKAGER"
880 else
881 local packager="Unknown Packager"
883 local size="$(du -sk)"
884 size="$(( ${size%%[^0-9]*} * 1024 ))"
886 msg2 "$(gettext "Generating .PKGINFO file...")"
887 echo "# Generated by makepkg $myver" >.PKGINFO
888 if (( INFAKEROOT )); then
889 echo "# using $(fakeroot -v)" >>.PKGINFO
891 echo "# $(LC_ALL=C date -u)" >>.PKGINFO
892 echo "pkgname = $1" >>.PKGINFO
893 (( SPLITPKG )) && echo pkgbase = $pkgbase >>.PKGINFO
894 echo "pkgver = $pkgver-$pkgrel" >>.PKGINFO
895 echo "pkgdesc = $pkgdesc" >>.PKGINFO
896 echo "url = $url" >>.PKGINFO
897 echo "builddate = $builddate" >>.PKGINFO
898 echo "packager = $packager" >>.PKGINFO
899 echo "size = $size" >>.PKGINFO
900 echo "arch = $PKGARCH" >>.PKGINFO
901 if [[ $(check_option force) = "y" ]]; then
902 echo "force = true" >> .PKGINFO
905 local it
906 for it in "${license[@]}"; do
907 echo "license = $it" >>.PKGINFO
908 done
909 for it in "${replaces[@]}"; do
910 echo "replaces = $it" >>.PKGINFO
911 done
912 for it in "${groups[@]}"; do
913 echo "group = $it" >>.PKGINFO
914 done
915 for it in "${depends[@]}"; do
916 echo "depend = $it" >>.PKGINFO
917 done
918 for it in "${optdepends[@]}"; do
919 echo "optdepend = $it" >>.PKGINFO
920 done
921 for it in "${conflicts[@]}"; do
922 echo "conflict = $it" >>.PKGINFO
923 done
924 for it in "${provides[@]}"; do
925 echo "provides = $it" >>.PKGINFO
926 done
927 for it in "${backup[@]}"; do
928 echo "backup = $it" >>.PKGINFO
929 done
930 for it in "${packaging_options[@]}"; do
931 local ret="$(check_option $it)"
932 if [[ $ret != "?" ]]; then
933 if [[ $ret = y ]]; then
934 echo "makepkgopt = $it" >>.PKGINFO
935 else
936 echo "makepkgopt = !$it" >>.PKGINFO
939 done
941 # TODO maybe remove this at some point
942 # warn if license array is not present or empty
943 if [[ -z $license ]]; then
944 warning "$(gettext "Please add a license line to your %s!")" "$BUILDSCRIPT"
945 plain "$(gettext "Example for GPL\'ed software: license=('GPL').")"
949 check_package() {
950 cd "$pkgdir"
952 # check existence of backup files
953 local file
954 for file in "${backup[@]}"; do
955 if [[ ! -f $file ]]; then
956 warning "$(gettext "Invalid backup entry : %s")" "$file"
958 done
960 # check for references to the build directory
961 if grep -R "${srcdir}" "${pkgdir}" &>/dev/null; then
962 warning "$(gettext "Package contains reference to %s")" "\$srcdir"
966 create_package() {
967 if [[ ! -d $pkgdir ]]; then
968 error "$(gettext "Missing pkg/ directory.")"
969 plain "$(gettext "Aborting...")"
970 exit 1 # $E_MISSING_PKGDIR
973 check_package
975 cd "$pkgdir"
976 msg "$(gettext "Creating package...")"
978 if [[ -z $1 ]]; then
979 nameofpkg="$pkgname"
980 else
981 nameofpkg="$1"
984 if [[ $arch = "any" ]]; then
985 PKGARCH="any"
986 else
987 PKGARCH=$CARCH
990 write_pkginfo $nameofpkg
992 local comp_files=".PKGINFO"
994 # check for an install script
995 if [[ -n $install ]]; then
996 msg2 "$(gettext "Adding install script...")"
997 cp "$startdir/$install" .INSTALL
998 comp_files="$comp_files .INSTALL"
1001 # do we have a changelog?
1002 if [[ -n $changelog ]]; then
1003 msg2 "$(gettext "Adding package changelog...")"
1004 cp "$startdir/$changelog" .CHANGELOG
1005 comp_files="$comp_files .CHANGELOG"
1008 # tar it up
1009 msg2 "$(gettext "Compressing package...")"
1011 case "$PKGEXT" in
1012 *tar.gz) EXT=${PKGEXT%.gz} ;;
1013 *tar.bz2) EXT=${PKGEXT%.bz2} ;;
1014 *tar.xz) EXT=${PKGEXT%.xz} ;;
1015 *) warning "$(gettext "'%s' is not a valid archive extension.")" \
1016 "$PKGEXT" ; EXT=$PKGEXT ;;
1017 esac
1018 local tar_file="$PKGDEST/${nameofpkg}-${pkgver}-${pkgrel}-${PKGARCH}${EXT}"
1019 local pkg_file="$PKGDEST/${nameofpkg}-${pkgver}-${pkgrel}-${PKGARCH}${PKGEXT}"
1021 local ret=0
1023 # when fileglobbing, we want * in an empty directory to expand to
1024 # the null string rather than itself
1025 shopt -s nullglob
1026 bsdtar -cf - $comp_files * > "$tar_file" || ret=$?
1027 shopt -u nullglob
1029 if (( ! ret )); then
1030 case "$PKGEXT" in
1031 *tar.gz) gzip -f -n "$tar_file" ;;
1032 *tar.bz2) bzip2 -f "$tar_file" ;;
1033 *tar.xz) xz -z -f "$tar_file" ;;
1034 esac
1035 ret=$?
1038 if (( ret )); then
1039 error "$(gettext "Failed to create package file.")"
1040 exit 1 # TODO: error code
1043 if (( ! ret )) && [[ "$PKGDEST" != "${startdir}" ]]; then
1044 ln -sf "${pkg_file}" "${pkg_file/$PKGDEST/$startdir}"
1045 ret=$?
1048 if (( ret )); then
1049 warning "$(gettext "Failed to create symlink to package file.")"
1053 create_srcpackage() {
1054 cd "$startdir"
1056 # Get back to our src directory so we can begin with sources.
1057 mkdir -p "$srcdir"
1058 cd "$srcdir"
1059 download_sources
1060 # We can only check checksums if we have all files.
1061 check_checksums
1062 cd "$startdir"
1064 msg "$(gettext "Creating source package...")"
1065 local srclinks="$(mktemp -d "$startdir"/srclinks.XXXXXXXXX)"
1066 mkdir "${srclinks}"/${pkgbase}
1068 msg2 "$(gettext "Adding %s...")" "$BUILDSCRIPT"
1069 ln -s "${BUILDFILE}" "${srclinks}/${pkgbase}/${BUILDSCRIPT}"
1071 if [[ -n $install ]]; then
1072 if [[ -f $install ]]; then
1073 msg2 "$(gettext "Adding install script...")"
1074 ln -s "${startdir}/$install" "${srclinks}/${pkgbase}/"
1075 else
1076 error "$(gettext "Install scriptlet (%s) does not exist.")" "$install"
1080 if [[ -n $changelog ]]; then
1081 if [[ -f $changelog ]]; then
1082 msg2 "$(gettext "Adding package changelog...")"
1083 ln -s "${startdir}/$changelog" "${srclinks}/${pkgbase}/"
1084 else
1085 error "$(gettext "Changelog file (%s) does not exist.")" "$changelog"
1089 local netfile
1090 for netfile in "${source[@]}"; do
1091 local file=$(get_filename "$netfile")
1092 if [[ -f $netfile ]]; then
1093 msg2 "$(gettext "Adding %s...")" "$netfile"
1094 ln -s "${startdir}/$netfile" "${srclinks}/${pkgbase}"
1095 elif (( SOURCEONLY == 2 )) && [[ -f $SRCDEST/$file ]]; then
1096 msg2 "$(gettext "Adding %s...")" "$file"
1097 ln -s "$SRCDEST/$file" "${srclinks}/${pkgbase}/"
1099 done
1101 local TAR_OPT
1102 case "$SRCEXT" in
1103 *tar.gz) TAR_OPT="z" ;;
1104 *tar.bz2) TAR_OPT="j" ;;
1105 *tar.xz) TAR_OPT="J" ;;
1106 *) warning "$(gettext "'%s' is not a valid archive extension.")" \
1107 "$SRCEXT" ;;
1108 esac
1110 local pkg_file="$PKGDEST/${pkgbase}-${pkgver}-${pkgrel}${SRCEXT}"
1112 # tar it up
1113 msg2 "$(gettext "Compressing source package...")"
1114 cd "${srclinks}"
1115 if ! bsdtar -c${TAR_OPT}Lf "$pkg_file" ${pkgbase}; then
1116 error "$(gettext "Failed to create source package file.")"
1117 exit 1 # TODO: error code
1119 cd "${startdir}"
1120 rm -rf "${srclinks}"
1123 install_package() {
1124 (( ! INSTALL )) && return
1126 if (( ! SPLITPKG )); then
1127 msg "$(gettext "Installing package %s with %s -U...")" "$pkgname" "$PACMAN"
1128 else
1129 msg "$(gettext "Installing %s package group with %s -U...")" "$pkgbase" "$PACMAN"
1132 local pkglist
1133 for pkg in ${pkgname[@]}; do
1134 if [[ -f $PKGDEST/${pkg}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT} ]]; then
1135 pkglist="${pkglist} $PKGDEST/${pkg}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT}"
1136 else
1137 pkglist="${pkglist} $PKGDEST/${pkg}-${pkgver}-${pkgrel}-any${PKGEXT}"
1139 done
1141 if ! run_pacman -U $pkglist; then
1142 warning "$(gettext "Failed to install built package(s).")"
1143 return 0
1147 check_sanity() {
1148 # check for no-no's in the build script
1149 if [[ -z $pkgname ]]; then
1150 error "$(gettext "%s is not allowed to be empty.")" "pkgname"
1151 return 1
1153 if [[ -z $pkgver ]]; then
1154 error "$(gettext "%s is not allowed to be empty.")" "pkgver"
1155 return 1
1157 if [[ -z $pkgrel ]]; then
1158 error "$(gettext "%s is not allowed to be empty.")" "pkgrel"
1159 return 1
1162 local name
1163 for name in "${pkgname[@]}"; do
1164 if [[ ${name:0:1} = "-" ]]; then
1165 error "$(gettext "%s is not allowed to start with a hyphen.")" "pkgname"
1166 return 1
1168 done
1170 if [[ ${pkgbase:0:1} = "-" ]]; then
1171 error "$(gettext "%s is not allowed to start with a hyphen.")" "pkgbase"
1172 return 1
1174 if [[ $pkgver != ${pkgver//-/} ]]; then
1175 error "$(gettext "%s is not allowed to contain hyphens.")" "pkgver"
1176 return 1
1178 if [[ $pkgrel != ${pkgrel//-/} ]]; then
1179 error "$(gettext "%s is not allowed to contain hyphens.")" "pkgrel"
1180 return 1
1183 if [[ $arch != 'any' ]]; then
1184 if ! in_array $CARCH ${arch[@]}; then
1185 if (( ! IGNOREARCH )); then
1186 error "$(gettext "%s is not available for the '%s' architecture.")" "$pkgbase" "$CARCH"
1187 plain "$(gettext "Note that many packages may need a line added to their %s")" "$BUILDSCRIPT"
1188 plain "$(gettext "such as arch=('%s').")" "$CARCH"
1189 return 1
1194 local provide
1195 for provide in ${provides[@]}; do
1196 if [[ $provide != ${provide//</} || $provide != ${provide//>/} ]]; then
1197 error "$(gettext "Provides array cannot contain comparison (< or >) operators.")"
1198 return 1
1200 done
1202 local file
1203 for file in "${backup[@]}"; do
1204 if [[ ${file:0:1} = "/" ]]; then
1205 error "$(gettext "Invalid backup entry : %s")" "$file"
1206 return 1
1208 done
1210 local optdepend
1211 for optdepend in "${optdepends[@]}"; do
1212 pkg=${optdepend%%:*}
1213 if [[ ! $pkg =~ ^[[:alnum:]\>\<\=\.\+\_\-]*$ ]]; then
1214 error "$(gettext "Invalid syntax for optdepend : '%s'")" "$optdepend"
1216 done
1218 if [[ $install && ! -f $install ]]; then
1219 error "$(gettext "Install scriptlet (%s) does not exist.")" "$install"
1220 return 1
1223 if [[ -n $changelog && ! -f $changelog ]]; then
1224 error "$(gettext "Changelog file (%s) does not exist.")" "$changelog"
1225 return 1
1228 local valid_options=1
1229 local opt known kopt
1230 for opt in ${options[@]}; do
1231 known=0
1232 # check if option matches a known option or its inverse
1233 for kopt in ${packaging_options[@]} ${other_options[@]}; do
1234 if [[ ${opt} = ${kopt} || ${opt} = "!${kopt}" ]]; then
1235 known=1
1237 done
1238 if (( ! known )); then
1239 error "$(gettext "options array contains unknown option '%s'")" "$opt"
1240 valid_options=0
1242 done
1243 if (( ! valid_options )); then
1244 return 1
1247 if (( ${#pkgname[@]} > 1 )); then
1248 for pkg in ${pkgname[@]}; do
1249 if [ "$(type -t package_${pkg})" != "function" ]; then
1250 error "$(gettext "missing package function for split package '%s'")" "$pkg"
1251 return 1
1253 done
1256 if [[ -n "${PKGLIST[@]}" ]]; then
1257 for pkg in ${PKGLIST[@]}; do
1258 if ! in_array $pkg ${pkgname[@]}; then
1259 error "$(gettext "requested package %s is not provided in %s")" "$pkg" "$BUILDSCRIPT"
1260 return 1
1262 done
1265 return 0
1268 devel_check() {
1269 newpkgver=""
1271 # Do not update pkgver if --holdver is set, when building a source package,
1272 # when reading PKGBUILD from pipe (-f), or if we cannot write to the file (-w)
1273 if (( HOLDVER || SOURCEONLY )) \
1274 || [[ ! -f $BUILDFILE || ! -w $BUILDFILE ]]; then
1275 return
1278 if [[ -z $FORCE_VER ]]; then
1279 # Check if this is a svn/cvs/etc PKGBUILD; set $newpkgver if so.
1280 # This will only be used on the first call to makepkg; subsequent
1281 # calls to makepkg via fakeroot will explicitly pass the version
1282 # number to avoid having to determine the version number twice.
1283 # Also do a brief check to make sure we have the VCS tool available.
1284 oldpkgver=$pkgver
1285 if [[ -n ${_darcstrunk} && -n ${_darcsmod} ]] ; then
1286 [ $(type -p darcs) ] || return 0
1287 msg "$(gettext "Determining latest darcs revision...")"
1288 newpkgver=$(date +%Y%m%d)
1289 elif [[ -n ${_cvsroot} && -n ${_cvsmod} ]] ; then
1290 [ $(type -p cvs) ] || return 0
1291 msg "$(gettext "Determining latest cvs revision...")"
1292 newpkgver=$(date +%Y%m%d)
1293 elif [[ -n ${_gitroot} && -n ${_gitname} ]] ; then
1294 [ $(type -p git) ] || return 0
1295 msg "$(gettext "Determining latest git revision...")"
1296 newpkgver=$(date +%Y%m%d)
1297 elif [[ -n ${_svntrunk} && -n ${_svnmod} ]] ; then
1298 [ $(type -p svn) ] || return 0
1299 msg "$(gettext "Determining latest svn revision...")"
1300 newpkgver=$(LC_ALL=C svn info $_svntrunk | sed -n 's/^Last Changed Rev: \([0-9]*\)$/\1/p')
1301 elif [[ -n ${_bzrtrunk} && -n ${_bzrmod} ]] ; then
1302 [ $(type -p bzr) ] || return 0
1303 msg "$(gettext "Determining latest bzr revision...")"
1304 newpkgver=$(bzr revno ${_bzrtrunk})
1305 elif [[ -n ${_hgroot} && -n ${_hgrepo} ]] ; then
1306 [ $(type -p hg) ] || return 0
1307 msg "$(gettext "Determining latest hg revision...")"
1308 if [[ -d ./src/$_hgrepo ]] ; then
1309 cd ./src/$_hgrepo
1310 hg pull
1311 hg update
1312 else
1313 [[ ! -d ./src/ ]] && mkdir ./src/
1314 hg clone $_hgroot/$_hgrepo ./src/$_hgrepo
1315 cd ./src/$_hgrepo
1317 newpkgver=$(hg tip --template "{rev}")
1318 cd ../../
1321 if [[ -n $newpkgver ]]; then
1322 msg2 "$(gettext "Version found: %s")" "$newpkgver"
1325 else
1326 # Version number retrieved from fakeroot->makepkg argument
1327 newpkgver=$FORCE_VER
1331 devel_update() {
1332 # This is lame, but if we're wanting to use an updated pkgver for
1333 # retrieving svn/cvs/etc sources, we'll update the PKGBUILD with
1334 # the new pkgver and then re-source it. This is the most robust
1335 # method for dealing with PKGBUILDs that use, e.g.:
1337 # pkgver=23
1338 # ...
1339 # _foo=pkgver
1341 if [[ -n $newpkgver ]]; then
1342 if [[ $newpkgver != $pkgver ]]; then
1343 if [[ -f $BUILDFILE && -w $BUILDFILE ]]; then
1344 @SEDINPLACE@ "s/^pkgver=[^ ]*/pkgver=$newpkgver/" "$BUILDFILE"
1345 @SEDINPLACE@ "s/^pkgrel=[^ ]*/pkgrel=1/" "$BUILDFILE"
1346 source "$BUILDFILE"
1352 backup_package_variables() {
1353 for var in ${splitpkg_overrides[@]}; do
1354 indirect="${var}_backup"
1355 eval "${indirect}=(\"\${$var[@]}\")"
1356 done
1359 restore_package_variables() {
1360 for var in ${splitpkg_overrides[@]}; do
1361 indirect="${var}_backup"
1362 if [[ -n ${!indirect} ]]; then
1363 eval "${var}=(\"\${$indirect[@]}\")"
1364 else
1365 unset ${var}
1367 done
1370 # getopt like parser
1371 parse_options() {
1372 local short_options=$1; shift;
1373 local long_options=$1; shift;
1374 local ret=0;
1375 local unused_options=""
1377 while [[ -n $1 ]]; do
1378 if [[ ${1:0:2} = '--' ]]; then
1379 if [[ -n ${1:2} ]]; then
1380 local match=""
1381 for i in ${long_options//,/ }; do
1382 if [[ ${1:2} = ${i//:} ]]; then
1383 match=$i
1384 break
1386 done
1387 if [[ -n $match ]]; then
1388 if [[ ${1:2} = $match ]]; then
1389 printf ' %s' "$1"
1390 else
1391 if [[ -n $2 ]]; then
1392 printf ' %s' "$1"
1393 shift
1394 printf " '%s'" "$1"
1395 else
1396 echo "makepkg: option '$1' $(gettext "requires an argument")" >&2
1397 ret=1
1400 else
1401 echo "makepkg: $(gettext "unrecognized option") '$1'" >&2
1402 ret=1
1404 else
1405 shift
1406 break
1408 elif [[ ${1:0:1} = '-' ]]; then
1409 for ((i=1; i<${#1}; i++)); do
1410 if [[ $short_options =~ ${1:i:1} ]]; then
1411 if [[ $short_options =~ "${1:i:1}:" ]]; then
1412 if [[ -n ${1:$i+1} ]]; then
1413 printf ' -%s' "${1:i:1}"
1414 printf " '%s'" "${1:$i+1}"
1415 else
1416 if [[ -n $2 ]]; then
1417 printf ' -%s' "${1:i:1}"
1418 shift
1419 printf " '%s'" "${1}"
1420 else
1421 echo "makepkg: option $(gettext "requires an argument") -- '${1:i:1}'" >&2
1422 ret=1
1425 break
1426 else
1427 printf ' -%s' "${1:i:1}"
1429 else
1430 echo "makepkg: $(gettext "invalid option") -- '${1:i:1}'" >&2
1431 ret=1
1433 done
1434 else
1435 unused_options="${unused_options} '$1'"
1437 shift
1438 done
1440 printf " --"
1441 if [[ -n $unused_options ]]; then
1442 for i in ${unused_options[@]}; do
1443 printf ' %s' "$i"
1444 done
1446 if [[ -n $1 ]]; then
1447 while [[ -n $1 ]]; do
1448 printf " '%s'" "${1}"
1449 shift
1450 done
1452 printf "\n"
1454 return $ret
1457 usage() {
1458 printf "makepkg (pacman) %s\n" "$myver"
1459 echo
1460 printf "$(gettext "Usage: %s [options]")\n" "$0"
1461 echo
1462 echo "$(gettext "Options:")"
1463 printf "$(gettext " -A, --ignorearch Ignore incomplete arch field in %s")\n" "$BUILDSCRIPT"
1464 echo "$(gettext " -c, --clean Clean up work files after build")"
1465 echo "$(gettext " -C, --cleancache Clean up source files from the cache")"
1466 echo "$(gettext " -d, --nodeps Skip all dependency checks")"
1467 echo "$(gettext " -e, --noextract Do not extract source files (use existing src/ dir)")"
1468 echo "$(gettext " -f, --force Overwrite existing package")"
1469 echo "$(gettext " -g, --geninteg Generate integrity checks for source files")"
1470 echo "$(gettext " -h, --help This help")"
1471 echo "$(gettext " -i, --install Install package after successful build")"
1472 echo "$(gettext " -L, --log Log package build process")"
1473 echo "$(gettext " -m, --nocolor Disable colorized output messages")"
1474 echo "$(gettext " -o, --nobuild Download and extract files only")"
1475 printf "$(gettext " -p <file> Use an alternate build script (instead of '%s')")\n" "$BUILDSCRIPT"
1476 echo "$(gettext " -r, --rmdeps Remove installed dependencies after a successful build")"
1477 echo "$(gettext " -R, --repackage Repackage contents of the package without rebuilding")"
1478 echo "$(gettext " -s, --syncdeps Install missing dependencies with pacman")"
1479 echo "$(gettext " --allsource Generate a source-only tarball including downloaded sources")"
1480 echo "$(gettext " --asroot Allow makepkg to run as root user")"
1481 printf "$(gettext " --config <file> Use an alternate config file (instead of '%s')")\n" "$confdir/makepkg.conf"
1482 echo "$(gettext " --holdver Prevent automatic version bumping for development PKGBUILDs")"
1483 echo "$(gettext " --pkg <list> Only build listed packages from a split package")"
1484 echo "$(gettext " --skipinteg Do not fail when integrity checks are missing")"
1485 echo "$(gettext " --source Generate a source-only tarball without downloaded sources")"
1486 echo
1487 echo "$(gettext "These options can be passed to pacman:")"
1488 echo
1489 echo "$(gettext " --noconfirm Do not ask for confirmation when resolving dependencies")"
1490 echo "$(gettext " --noprogressbar Do not show a progress bar when downloading files")"
1491 echo
1492 printf "$(gettext "If -p is not specified, makepkg will look for '%s'")\n" "$BUILDSCRIPT"
1493 echo
1496 version() {
1497 printf "makepkg (pacman) %s\n" "$myver"
1498 printf "$(gettext "\
1499 Copyright (c) 2006-2009 Pacman Development Team <pacman-dev@archlinux.org>.\n\
1500 Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>.\n\n\
1501 This is free software; see the source for copying conditions.\n\
1502 There is NO WARRANTY, to the extent permitted by law.\n")"
1505 # PROGRAM START
1507 # determine whether we have gettext; make it a no-op if we do not
1508 if [ ! $(type -t gettext) ]; then
1509 gettext() {
1510 echo "$@"
1514 ARGLIST=("$@")
1516 # Parse Command Line Options.
1517 OPT_SHORT="AcCdefFghiLmop:rRsV"
1518 OPT_LONG="allsource,asroot,ignorearch,clean,cleancache,nodeps"
1519 OPT_LONG="$OPT_LONG,noextract,force,forcever:,geninteg,help,holdver"
1520 OPT_LONG="$OPT_LONG,install,log,nocolor,nobuild,pkg:,rmdeps,repackage,skipinteg"
1521 OPT_LONG="$OPT_LONG,source,syncdeps,version,config:"
1522 # Pacman Options
1523 OPT_LONG="$OPT_LONG,noconfirm,noprogressbar"
1524 OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@" || echo 'PARSE_OPTIONS FAILED')"
1525 if echo "$OPT_TEMP" | grep -q 'PARSE_OPTIONS FAILED'; then
1526 # This is a small hack to stop the script bailing with 'set -e'
1527 echo; usage; exit 1 # E_INVALID_OPTION;
1529 eval set -- "$OPT_TEMP"
1530 unset OPT_SHORT OPT_LONG OPT_TEMP
1532 while true; do
1533 case "$1" in
1534 # Pacman Options
1535 --noconfirm) PACMAN_OPTS="$PACMAN_OPTS --noconfirm" ;;
1536 --noprogressbar) PACMAN_OPTS="$PACMAN_OPTS --noprogressbar" ;;
1538 # Makepkg Options
1539 --allsource) SOURCEONLY=2 ;;
1540 --asroot) ASROOT=1 ;;
1541 -A|--ignorearch) IGNOREARCH=1 ;;
1542 -c|--clean) CLEANUP=1 ;;
1543 -C|--cleancache) CLEANCACHE=1 ;;
1544 --config) shift; MAKEPKG_CONF=$1 ;;
1545 -d|--nodeps) NODEPS=1 ;;
1546 -e|--noextract) NOEXTRACT=1 ;;
1547 -f|--force) FORCE=1 ;;
1548 #hidden opt used by fakeroot call for svn/cvs/etc PKGBUILDs to set pkgver
1549 --forcever) shift; FORCE_VER=$1;;
1550 -F) INFAKEROOT=1 ;;
1551 -g|--geninteg) GENINTEG=1 ;;
1552 --holdver) HOLDVER=1 ;;
1553 -i|--install) INSTALL=1 ;;
1554 -L|--log) LOGGING=1 ;;
1555 -m|--nocolor) USE_COLOR='n' ;;
1556 -o|--nobuild) NOBUILD=1 ;;
1557 -p) shift; BUILDFILE=$1 ;;
1558 --pkg) shift; PKGLIST=$1 ;;
1559 -r|--rmdeps) RMDEPS=1 ;;
1560 -R|--repackage) REPKG=1 ;;
1561 --skipinteg) SKIPINTEG=1 ;;
1562 --source) SOURCEONLY=1 ;;
1563 -s|--syncdeps) DEP_BIN=1 ;;
1565 -h|--help) usage; exit 0 ;; # E_OK
1566 -V|--version) version; exit 0 ;; # E_OK
1568 --) OPT_IND=0; shift; break;;
1569 *) usage; exit 1 ;; # E_INVALID_OPTION
1570 esac
1571 shift
1572 done
1574 #preserve environment variables
1575 _PKGDEST=${PKGDEST}
1576 _SRCDEST=${SRCDEST}
1578 # default config is makepkg.conf
1579 MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
1581 # Source the config file; fail if it is not found
1582 if [[ -r $MAKEPKG_CONF ]]; then
1583 source "$MAKEPKG_CONF"
1584 else
1585 error "$(gettext "%s not found.")" "$MAKEPKG_CONF"
1586 plain "$(gettext "Aborting...")"
1587 exit 1 # $E_CONFIG_ERROR
1590 # Source user-specific makepkg.conf overrides
1591 if [[ -r ~/.makepkg.conf ]]; then
1592 source ~/.makepkg.conf
1595 # set pacman command if not already defined
1596 PACMAN=${PACMAN:-pacman}
1598 # check if messages are to be printed using color
1599 unset ALL_OFF BOLD BLUE GREEN RED YELLOW
1600 if [[ -t 2 && ! $USE_COLOR = "n" && $(check_buildenv color) = "y" ]]; then
1601 ALL_OFF="$(tput sgr0)"
1602 BOLD="$(tput bold)"
1603 BLUE="${BOLD}$(tput setaf 4)"
1604 GREEN="${BOLD}$(tput setaf 2)"
1605 RED="${BOLD}$(tput setaf 1)"
1606 YELLOW="${BOLD}$(tput setaf 3)"
1608 readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
1610 # override settings with an environment variable for batch processing
1611 PKGDEST=${_PKGDEST:-$PKGDEST}
1612 PKGDEST=${PKGDEST:-$startdir} #default to $startdir if undefined
1613 SRCDEST=${_SRCDEST:-$SRCDEST}
1614 SRCDEST=${SRCDEST:-$startdir} #default to $startdir if undefined
1617 if (( HOLDVER )) && [[ -n $FORCE_VER ]]; then
1618 # The '\\0' is here to prevent gettext from thinking --holdver is an option
1619 error "$(gettext "\\0--holdver and --forcever cannot both be specified" )"
1620 exit 1
1623 if (( CLEANCACHE )); then
1624 #fix flyspray feature request #5223
1625 if [[ -n $SRCDEST && $SRCDEST != $startdir ]]; then
1626 msg "$(gettext "Cleaning up ALL files from %s.")" "$SRCDEST"
1627 echo -n "$(gettext " Are you sure you wish to do this? ")"
1628 echo -n "$(gettext "[y/N]")"
1629 read answer
1630 answer="${answer^^}"
1631 if [[ $answer = $(gettext YES) || $answer = $(gettext Y) ]]; then
1632 rm "$SRCDEST"/*
1633 if (( $? )); then
1634 error "$(gettext "Problem removing files; you may not have correct permissions in %s")" "$SRCDEST"
1635 exit 1
1636 else
1637 # removal worked
1638 msg "$(gettext "Source cache cleaned.")"
1639 exit 0
1641 else
1642 # answer = no
1643 msg "$(gettext "No files have been removed.")"
1644 exit 0
1646 else
1647 # $SRCDEST is $startdir, two possibilities
1648 error "$(gettext "Source destination must be defined in %s.")" "$MAKEPKG_CONF"
1649 plain "$(gettext "In addition, please run makepkg -C outside of your cache directory.")"
1650 exit 1
1654 if (( ! INFAKEROOT )); then
1655 if (( EUID == 0 && ! ASROOT )); then
1656 # Warn those who like to live dangerously.
1657 error "$(gettext "Running makepkg as root is a BAD idea and can cause")"
1658 plain "$(gettext "permanent, catastrophic damage to your system. If you")"
1659 plain "$(gettext "wish to run as root, please use the --asroot option.")"
1660 exit 1 # $E_USER_ABORT
1661 elif (( EUID > 0 && ASROOT )); then
1662 # Warn those who try to use the --asroot option when they are not root
1663 error "$(gettext "The --asroot option is meant for the root user only.")"
1664 plain "$(gettext "Please rerun makepkg without the --asroot flag.")"
1665 exit 1 # $E_USER_ABORT
1666 elif [[ $(check_buildenv fakeroot) = "y" ]] && (( EUID > 0 )); then
1667 if [ ! $(type -p fakeroot) ]; then
1668 error "$(gettext "Fakeroot must be installed if using the 'fakeroot' option")"
1669 plain "$(gettext "in the BUILDENV array in %s.")" "$MAKEPKG_CONF"
1670 exit 1
1672 elif (( EUID > 0 )); then
1673 warning "$(gettext "Running makepkg as an unprivileged user will result in non-root")"
1674 plain "$(gettext "ownership of the packaged files. Try using the fakeroot environment by")"
1675 plain "$(gettext "placing 'fakeroot' in the BUILDENV array in %s.")" "$MAKEPKG_CONF"
1676 sleep 1
1678 else
1679 if [[ -z $FAKEROOTKEY ]]; then
1680 error "$(gettext "Do not use the '-F' option. This option is only for use by makepkg.")"
1681 exit 1 # TODO: error code
1685 # check for sudo if we will need it during makepkg execution
1686 if (( ! ASROOT && ( DEP_BIN || RMDEPS || INSTALL ) )); then
1687 if [ ! "$(type -p sudo)" ]; then
1688 error "$(gettext "Cannot find the sudo binary! Is sudo installed?")"
1689 plain "$(gettext "Missing dependencies cannot be installed or removed as a normal user")"
1690 plain "$(gettext "without sudo; install and configure sudo to auto-resolve dependencies.")"
1691 exit 1
1695 unset pkgname pkgbase pkgver pkgrel pkgdesc url license groups provides
1696 unset md5sums replaces depends conflicts backup source install changelog build
1697 unset makedepends optdepends options noextract
1699 BUILDFILE=${BUILDFILE:-$BUILDSCRIPT}
1700 if [[ ! -f $BUILDFILE ]]; then
1701 if [[ -t 0 ]]; then
1702 error "$(gettext "%s does not exist.")" "$BUILDFILE"
1703 exit 1
1704 else
1705 # PKGBUILD passed through a pipe
1706 BUILDFILE=/dev/stdin
1707 source "$BUILDFILE"
1709 else
1710 crlftest=$(file "$BUILDFILE" | grep -F 'CRLF' || true)
1711 if [[ -n $crlftest ]]; then
1712 error "$(gettext "%s contains CRLF characters and cannot be sourced.")" "$BUILDFILE"
1713 exit 1
1716 if [[ ${BUILDFILE:0:1} != "/" ]]; then
1717 BUILDFILE="$startdir/$BUILDFILE"
1719 source "$BUILDFILE"
1722 if (( GENINTEG )); then
1723 mkdir -p "$srcdir"
1724 cd "$srcdir"
1725 download_sources
1726 generate_checksums
1727 exit 0 # $E_OK
1730 # check the PKGBUILD for some basic requirements
1731 check_sanity || exit 1
1733 # We need to run devel_update regardless of whether we are in the fakeroot
1734 # build process so that if the user runs makepkg --forcever manually, we
1735 # 1) output the correct pkgver, and 2) use the correct filename when
1736 # checking if the package file already exists - fixes FS #9194
1737 devel_check
1738 devel_update
1740 if (( ${#pkgname[@]} > 1 )); then
1741 SPLITPKG=1
1744 # test for available PKGBUILD functions
1745 # The exclamation mark is required here to avoid triggering the ERR trap when
1746 # a tested function does not exist.
1747 if [[ $(! type -t build) = "function" ]]; then
1748 BUILDFUNC=1
1750 if [ "$(type -t package)" = "function" ]; then
1751 PKGFUNC=1
1752 elif [ $SPLITPKG -eq 0 -a "$(type -t package_${pkgname})" = "function" ]; then
1753 SPLITPKG=1
1756 pkgbase=${pkgbase:-${pkgname[0]}}
1758 if [[ -n "${PKGLIST[@]}" ]]; then
1759 unset pkgname
1760 pkgname="${PKGLIST[@]}"
1763 if (( ! SPLITPKG )); then
1764 if [[ -f $PKGDEST/${pkgname}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT} \
1765 || -f $PKGDEST/${pkgname}-${pkgver}-${pkgrel}-any${PKGEXT} ]] \
1766 && ! (( FORCE || SOURCEONLY || NOBUILD )); then
1767 if (( INSTALL )); then
1768 warning "$(gettext "A package has already been built, installing existing package...")"
1769 install_package
1770 exit $?
1771 else
1772 error "$(gettext "A package has already been built. (use -f to overwrite)")"
1773 exit 1
1776 else
1777 allpkgbuilt=1
1778 somepkgbuilt=0
1779 for pkg in ${pkgname[@]}; do
1780 if [[ -f $PKGDEST/${pkg}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT} \
1781 || -f $PKGDEST/${pkg}-${pkgver}-${pkgrel}-any${PKGEXT} ]]; then
1782 somepkgbuilt=1
1783 else
1784 allpkgbuilt=0
1786 done
1787 if ! (( FORCE || SOURCEONLY || NOBUILD )); then
1788 if (( allpkgbuilt )); then
1789 if (( INSTALL )); then
1790 warning "$(gettext "The package group has already been built, installing existing packages...")"
1791 install_package
1792 exit $?
1793 else
1794 error "$(gettext "The package group has already been built. (use -f to overwrite)")"
1795 exit 1
1798 if (( somepkgbuilt )); then
1799 error "$(gettext "Part of the package group has already been built. (use -f to overwrite)")"
1800 exit 1
1803 unset allpkgbuilt somepkgbuilt
1806 # Run the bare minimum in fakeroot
1807 if (( INFAKEROOT )); then
1808 if (( ! SPLITPKG )); then
1809 if (( ! PKGFUNC )); then
1810 if (( BUILDFUNC && ! REPKG )); then
1811 run_build
1812 tidy_install
1814 else
1815 run_package
1816 tidy_install
1818 create_package
1819 else
1820 for pkg in ${pkgname[@]}; do
1821 pkgdir="$pkgdir/$pkg"
1822 mkdir -p "$pkgdir"
1823 backup_package_variables
1824 run_package $pkg
1825 tidy_install
1826 create_package $pkg
1827 restore_package_variables
1828 pkgdir="${pkgdir%/*}"
1829 done
1832 msg "$(gettext "Leaving fakeroot environment.")"
1833 exit 0 # $E_OK
1836 msg "$(gettext "Making package: %s")" "$pkgbase $pkgver-$pkgrel ($(date))"
1838 # if we are creating a source-only package, go no further
1839 if (( SOURCEONLY )); then
1840 if [[ -f $PKGDEST/${pkgbase}-${pkgver}-${pkgrel}${SRCEXT} ]] \
1841 && (( ! FORCE )); then
1842 error "$(gettext "A source package has already been built. (use -f to overwrite)")"
1843 exit 1
1845 create_srcpackage
1846 msg "$(gettext "Source package created: %s")" "$pkgbase ($(date))"
1847 exit 0
1850 # fix flyspray bug #5973
1851 if (( NODEPS || NOBUILD || REPKG )); then
1852 # no warning message needed for nobuild, repkg
1853 if (( NODEPS )); then
1854 warning "$(gettext "Skipping dependency checks.")"
1856 elif [ $(type -p "${PACMAN%% *}") ]; then
1857 unset pkgdeps # Set by resolve_deps() and used by remove_deps()
1858 deperr=0
1860 msg "$(gettext "Checking Runtime Dependencies...")"
1861 resolve_deps ${depends[@]} || deperr=1
1863 msg "$(gettext "Checking Buildtime Dependencies...")"
1864 resolve_deps ${makedepends[@]} || deperr=1
1866 if (( deperr )); then
1867 error "$(gettext "Could not resolve all dependencies.")"
1868 exit 1
1870 else
1871 warning "$(gettext "%s was not found in PATH; skipping dependency checks.")" "${PACMAN%% *}"
1874 # ensure we have a sane umask set
1875 umask 0022
1877 # get back to our src directory so we can begin with sources
1878 mkdir -p "$srcdir"
1879 cd "$srcdir"
1881 if (( NOEXTRACT )); then
1882 warning "$(gettext "Skipping source retrieval -- using existing src/ tree")"
1883 warning "$(gettext "Skipping source integrity checks -- using existing src/ tree")"
1884 warning "$(gettext "Skipping source extraction -- using existing src/ tree")"
1886 if (( NOEXTRACT )) && [[ -z $(ls "$srcdir" 2>/dev/null) ]]; then
1887 error "$(gettext "The source directory is empty, there is nothing to build!")"
1888 plain "$(gettext "Aborting...")"
1889 exit 1
1891 elif (( REPKG )); then
1892 if (( ! PKGFUNC && ! SPLITPKG )) \
1893 && [[ ! -d $pkgdir || -z $(ls "$pkgdir" 2>/dev/null) ]]; then
1894 error "$(gettext "The package directory is empty, there is nothing to repackage!")"
1895 plain "$(gettext "Aborting...")"
1896 exit 1
1898 else
1899 download_sources
1900 check_checksums
1901 extract_sources
1904 if (( NOBUILD )); then
1905 msg "$(gettext "Sources are ready.")"
1906 exit 0 #E_OK
1907 else
1908 # check for existing pkg directory; don't remove if we are repackaging
1909 if [[ -d $pkgdir ]] && (( ! REPKG || PKGFUNC || SPLITPKG )); then
1910 msg "$(gettext "Removing existing pkg/ directory...")"
1911 rm -rf "$pkgdir"
1913 mkdir -p "$pkgdir"
1914 cd "$startdir"
1916 # if we are root or if fakeroot is not enabled, then we don't use it
1917 if [[ $(check_buildenv fakeroot) != "y" ]] || (( EUID == 0 )); then
1918 if (( ! REPKG )); then
1919 devel_update
1920 (( BUILDFUNC )) && run_build
1922 if (( ! SPLITPKG )); then
1923 if (( PKGFUNC )); then
1924 run_package
1925 tidy_install
1926 elif (( ! REPKG )); then
1927 tidy_install
1929 create_package
1930 else
1931 for pkg in ${pkgname[@]}; do
1932 pkgdir="$pkgdir/$pkg"
1933 mkdir -p "$pkgdir"
1934 backup_package_variables
1935 run_package $pkg
1936 tidy_install
1937 create_package $pkg
1938 restore_package_variables
1939 pkgdir="${pkgdir%/*}"
1940 done
1942 else
1943 if (( ! REPKG && ( PKGFUNC || SPLITPKG ) )); then
1944 devel_update
1945 (( BUILDFUNC )) && run_build
1946 cd "$startdir"
1949 msg "$(gettext "Entering fakeroot environment...")"
1951 if [[ -n $newpkgver ]]; then
1952 fakeroot -- $0 --forcever $newpkgver -F "${ARGLIST[@]}" || exit $?
1953 else
1954 fakeroot -- $0 -F "${ARGLIST[@]}" || exit $?
1959 msg "$(gettext "Finished making: %s")" "$pkgbase $pkgver-$pkgrel ($(date))"
1961 install_package
1963 exit 0 #E_OK
1965 # vim: set ts=2 sw=2 noet: