makepkg: fix pkgdesc restoration with split packaging
[pacman-ng.git] / scripts / makepkg.sh.in
blob8aa6cfccb76a4b4e75113c893325cdae7d41977b
1 #!/bin/bash -e
3 # makepkg - make packages compatable 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 # awk, bsdtar (libarchive), bzip2, coreutils, fakeroot, find (findutils),
31 # getopt (util-linux), gettext, grep, gzip, openssl, sed
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=('pkgdesc' 'license' 'groups' 'depends' 'optdepends' 'provides' \
50 'conflicts' 'replaces' 'backup' 'options' 'install')
51 readonly -a packaging_options other_options splitpkg_overrides
53 # Options
54 ASROOT=0
55 CLEANUP=0
56 CLEANCACHE=0
57 DEP_BIN=0
58 FORCE=0
59 INFAKEROOT=0
60 GENINTEG=0
61 INSTALL=0
62 NOBUILD=0
63 NODEPS=0
64 NOEXTRACT=0
65 RMDEPS=0
66 REPKG=0
67 LOGGING=0
68 SOURCEONLY=0
69 IGNOREARCH=0
70 HOLDVER=0
71 PKGFUNC=0
72 SPLITPKG=0
73 COLORMSG=0
75 # Forces the pkgver of the current PKGBUILD. Used by the fakeroot call
76 # when dealing with svn/cvs/etc PKGBUILDs.
77 FORCE_VER=""
79 PACMAN_OPTS=
81 ### SUBROUTINES ###
83 plain() {
84 local mesg=$1; shift
85 if [ $COLORMSG -eq 1 ]; then
86 printf "\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
87 else
88 printf " ${mesg}\n" "$@" >&2
92 msg() {
93 local mesg=$1; shift
94 if [ $COLORMSG -eq 1 ]; then
95 printf "\033[1;32m==>\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
96 else
97 printf "==> ${mesg}\n" "$@" >&2
101 msg2() {
102 local mesg=$1; shift
103 if [ $COLORMSG -eq 1 ]; then
104 printf "\033[1;34m ->\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
105 else
106 printf " -> ${mesg}\n" "$@" >&2
110 warning() {
111 local mesg=$1; shift
112 if [ $COLORMSG -eq 1 ]; then
113 printf "\033[1;33m==> $(gettext "WARNING:")\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
114 else
115 printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
119 error() {
120 local mesg=$1; shift
121 if [ $COLORMSG -eq 1 ]; then
122 printf "\033[1;31m==> $(gettext "ERROR:")\033[1;0m\033[1;1m ${mesg}\033[1;0m\n" "$@" >&2
123 else
124 printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
130 # Special exit call for traps, Don't print any error messages when inside,
131 # the fakeroot call, the error message will be printed by the main call.
133 trap_exit() {
134 if [ "$INFAKEROOT" -eq 0 ]; then
135 echo
136 error "$@"
138 exit 1
143 # Clean up function. Called automatically when the script exits.
145 clean_up() {
146 local EXIT_CODE=$?
148 if [ "$INFAKEROOT" -eq 1 ]; then
149 # Don't clean up when leaving fakeroot, we're not done yet.
150 return
153 if [ $EXIT_CODE -eq 0 -a "$CLEANUP" -eq 1 ]; then
154 # If it's a clean exit and -c/--clean has been passed...
155 msg "$(gettext "Cleaning up...")"
156 rm -rf "$pkgdir" "$srcdir"
157 if [ -n "$pkgname" ]; then
158 # Can't do this unless the BUILDSCRIPT has been sourced.
159 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-build.log"*
160 if [ "$PKGFUNC" -eq 1 ]; then
161 rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-package.log"*
162 elif [ "$SPLITPKG" -eq 1 ]; then
163 for pkg in ${pkgname[@]}; do
164 rm -f "${pkg}-${pkgver}-${pkgrel}-${CARCH}-package.log"*
165 done
170 remove_deps
175 # Signal Traps
177 trap 'clean_up' 0
178 trap 'trap_exit "$(gettext "TERM signal caught. Exiting...")"' TERM HUP QUIT
179 trap 'trap_exit "$(gettext "Aborted by user! Exiting...")"' INT
180 trap 'trap_exit "$(gettext "An unknown error has occured. Exiting...")"' ERR
182 # a source entry can have two forms :
183 # 1) "filename::http://path/to/file"
184 # 2) "http://path/to/file"
186 # extract the filename from a source entry
187 get_filename() {
188 # if a filename is specified, use it
189 local filename=$(echo $1 | sed 's|::.*||')
190 # if it is just an url, we only keep the last component
191 echo "$filename" | sed 's|^.*://.*/||g'
194 # extract the url from a source entry
195 get_url() {
196 # strip an eventual filename
197 echo $1 | sed 's|.*::||'
201 # Checks to see if options are present in makepkg.conf or PKGBUILD;
202 # PKGBUILD options always take precedence.
204 # usage : check_option( $option )
205 # return : y - enabled
206 # n - disabled
207 # ? - not found
209 check_option() {
210 local ret=$(in_opt_array "$1" ${options[@]})
211 if [ "$ret" != '?' ]; then
212 echo $ret
213 return
216 # fall back to makepkg.conf options
217 ret=$(in_opt_array "$1" ${OPTIONS[@]})
218 if [ "$ret" != '?' ]; then
219 echo $ret
220 return
223 echo '?' # Not Found
228 # Check if option is present in BUILDENV
230 # usage : check_buildenv( $option )
231 # return : y - enabled
232 # n - disabled
233 # ? - not found
235 check_buildenv() {
236 echo $(in_opt_array "$1" ${BUILDENV[@]})
241 # usage : in_opt_array( $needle, $haystack )
242 # return : y - enabled
243 # n - disabled
244 # ? - not found
246 in_opt_array() {
247 local needle=$(echo $1 | tr '[:upper:]' '[:lower:]'); shift
249 local opt
250 for opt in "$@"; do
251 opt=$(echo $opt | tr '[:upper:]' '[:lower:]')
252 if [ "$opt" = "$needle" ]; then
253 echo 'y' # Enabled
254 return
255 elif [ "$opt" = "!$needle" ]; then
256 echo 'n' # Disabled
257 return
259 done
261 echo '?' # Not Found
266 # usage : in_array( $needle, $haystack )
267 # return : 0 - found
268 # 1 - not found
270 in_array() {
271 local needle=$1; shift
272 [ -z "$1" ] && return 1 # Not Found
273 local item
274 for item in "$@"; do
275 [ "$item" = "$needle" ] && return 0 # Found
276 done
277 return 1 # Not Found
280 get_downloadclient() {
281 # $1 = url with valid protocol prefix
282 local url=$1
283 local proto=$(echo "$url" | sed 's|://.*||')
285 # loop through DOWNLOAD_AGENTS variable looking for protocol
286 local i
287 for i in "${DLAGENTS[@]}"; do
288 local handler=$(echo $i | sed 's|::.*||')
289 if [ "$proto" = "$handler" ]; then
290 agent=$(echo $i | sed 's|^.*::||')
291 break
293 done
295 # if we didn't find an agent, return an error
296 if [ -z "$agent" ]; then
297 error "$(gettext "There is no agent set up to handle %s URLs. Check %s.")" "$proto" "$MAKEPKG_CONF"
298 plain "$(gettext "Aborting...")"
299 exit 1 # $E_CONFIG_ERROR
302 # ensure specified program is installed
303 local program="$(echo $agent | awk '{print $1 }')"
304 if [ ! -x "$program" ]; then
305 local baseprog=$(basename $program)
306 error "$(gettext "The download program %s is not installed.")" "$baseprog"
307 plain "$(gettext "Aborting...")"
308 exit 1 # $E_MISSING_PROGRAM
311 echo "$agent"
314 download_file() {
315 # download command
316 local dlcmd=$1
317 # url of the file
318 local url=$2
319 # destination file
320 local file=$3
321 # temporary download file, default to last component of the url
322 local dlfile=$(echo "$url" | sed 's|^.*://.*/||g')
324 # replace %o by the temporary dlfile if it exists
325 if echo "$dlcmd" | grep -q "%o" ; then
326 dlcmd=${dlcmd//\%o/$file.part}
327 dlfile="$file.part"
329 # add the url, either in place of %u or at the end
330 if echo "$dlcmd" | grep -q "%u" ; then
331 dlcmd=${dlcmd//\%u/$url}
332 else
333 dlcmd="$dlcmd $url"
336 $dlcmd || return $?
338 # rename the temporary download file to the final destination
339 if [ "$dlfile" != "$file" ]; then
340 mv -f "$SRCDEST/$dlfile" "$SRCDEST/$file"
344 check_deps() {
345 [ $# -gt 0 ] || return
347 pmout=$(pacman $PACMAN_OPTS -T "$@")
348 ret=$?
349 if [ $ret -eq 127 ]; then #unresolved deps
350 echo "$pmout"
351 elif [ $ret -ne 0 ]; then
352 error "$(gettext "Pacman returned a fatal error (%i): %s")" "$ret" "$pmout"
353 exit 1
357 handledeps() {
358 local R_DEPS_SATISFIED=0
359 local R_DEPS_MISSING=1
361 [ $# -eq 0 ] && return $R_DEPS_SATISFIED
363 local deplist="$*"
365 if [ "$DEP_BIN" -eq 0 ]; then
366 return $R_DEPS_MISSING
369 if [ "$DEP_BIN" -eq 1 ]; then
370 # install missing deps from binary packages (using pacman -S)
371 msg "$(gettext "Installing missing dependencies...")"
372 local ret=0
374 if [ "$ASROOT" -eq 0 ]; then
375 sudo pacman $PACMAN_OPTS -S --asdeps $deplist || ret=$?
376 else
377 pacman $PACMAN_OPTS -S --asdeps $deplist || ret=$?
380 if [ $ret -ne 0 ]; then
381 error "$(gettext "Pacman failed to install missing dependencies.")"
382 exit 1 # TODO: error code
386 # we might need the new system environment
387 # set -e can cause problems during sourcing profile scripts
388 set +e
389 source /etc/profile &>/dev/null
390 set -e
392 return $R_DEPS_SATISFIED
395 resolve_deps() {
396 # $pkgdeps is a GLOBAL variable, used by remove_deps()
397 local R_DEPS_SATISFIED=0
398 local R_DEPS_MISSING=1
400 local deplist="$(check_deps $*)"
401 if [ -z "$deplist" ]; then
402 return $R_DEPS_SATISFIED
405 if handledeps $deplist; then
406 pkgdeps="$pkgdeps $deplist"
407 # check deps again to make sure they were resolved
408 deplist="$(check_deps $*)"
409 [ -z "$deplist" ] && return $R_DEPS_SATISFIED
410 elif [ "$DEP_BIN" -eq 1 ]; then
411 error "$(gettext "Failed to install all missing dependencies.")"
414 msg "$(gettext "Missing Dependencies:")"
415 local dep
416 for dep in $deplist; do
417 msg2 "$dep"
418 done
420 return $R_DEPS_MISSING
423 # fix flyspray bug #5923
424 remove_deps() {
425 # $pkgdeps is a GLOBAL variable, set by resolve_deps()
426 [ "$RMDEPS" -eq 0 ] && return
427 [ -z "$pkgdeps" ] && return
429 local dep depstrip deplist
430 deplist=""
431 for dep in $pkgdeps; do
432 depstrip=$(echo $dep | sed -e 's|=.*$||' -e 's|>.*$||' -e 's|<.*$||')
433 deplist="$deplist $depstrip"
434 done
436 msg "Removing installed dependencies..."
437 local ret=0
438 if [ "$ASROOT" -eq 0 ]; then
439 sudo pacman $PACMAN_OPTS -Rns $deplist || ret=$?
440 else
441 pacman $PACMAN_OPTS -Rns $deplist || ret=$?
444 # Fixes FS#10039 - exit cleanly as package has built successfully
445 if [ $ret -ne 0 ]; then
446 warning "$(gettext "Failed to remove installed dependencies.")"
447 return 0
451 download_sources() {
452 msg "$(gettext "Retrieving Sources...")"
454 if [ ! -w "$SRCDEST" ] ; then
455 error "$(gettext "You do not have write permission to store downloads in %s.")" "$SRCDEST"
456 plain "$(gettext "Aborting...")"
457 exit 1
460 pushd "$SRCDEST" &>/dev/null
462 local netfile
463 for netfile in "${source[@]}"; do
464 local file=$(get_filename "$netfile")
465 local url=$(get_url "$netfile")
466 if [ -f "$startdir/$file" ]; then
467 msg2 "$(gettext "Found %s in build dir")" "$file"
468 rm -f "$srcdir/$file"
469 ln -s "$startdir/$file" "$srcdir/"
470 continue
471 elif [ -f "$SRCDEST/$file" ]; then
472 msg2 "$(gettext "Using cached copy of %s")" "$file"
473 rm -f "$srcdir/$file"
474 ln -s "$SRCDEST/$file" "$srcdir/"
475 continue
478 # if we get here, check to make sure it was a URL, else fail
479 if [ "$file" = "$url" ]; then
480 error "$(gettext "%s was not found in the build directory and is not a URL.")" "$file"
481 exit 1 # $E_MISSING_FILE
484 # find the client we should use for this URL
485 local dlclient=$(get_downloadclient "$url") || exit $?
487 msg2 "$(gettext "Downloading %s...")" "$file"
488 # fix flyspray bug #3289
489 local ret=0
490 download_file "$dlclient" "$url" "$file" || ret=$?
491 if [ $ret -gt 0 ]; then
492 error "$(gettext "Failure while downloading %s")" "$file"
493 plain "$(gettext "Aborting...")"
494 exit 1
496 rm -f "$srcdir/$file"
497 ln -s "$SRCDEST/$file" "$srcdir/"
498 done
500 popd &>/dev/null
503 generate_checksums() {
504 msg "$(gettext "Generating checksums for source files...")"
505 plain ""
507 if [ ! $(type -p openssl) ]; then
508 error "$(gettext "Cannot find openssl.")"
509 exit 1 # $E_MISSING_PROGRAM
512 local integ
513 for integ in ${INTEGRITY_CHECK[@]}; do
514 integ="$(echo $integ | tr '[:upper:]' '[:lower:]')"
515 case "$integ" in
516 md5|sha1|sha256|sha384|sha512) : ;;
518 error "$(gettext "Invalid integrity algorithm '%s' specified.")" "$integ"
519 exit 1;; # $E_CONFIG_ERROR
520 esac
522 local ct=0
523 local numsrc=${#source[@]}
524 echo -n "${integ}sums=("
526 local i=0;
527 local indent=''
528 while [ $i -lt $((${#integ}+6)) ]; do
529 indent="$indent "
530 i=$(($i+1))
531 done
533 local netfile
534 for netfile in "${source[@]}"; do
535 local file="$(get_filename "$netfile")"
537 if [ ! -f "$file" ] ; then
538 if [ ! -f "$SRCDEST/$file" ] ; then
539 error "$(gettext "Unable to find source file %s to generate checksum.")" "$file"
540 plain "$(gettext "Aborting...")"
541 exit 1
542 else
543 file="$SRCDEST/$file"
547 local sum="$(openssl dgst -${integ} "$file" | awk '{print $NF}')"
548 [ $ct -gt 0 ] && echo -n "$indent"
549 echo -n "'$sum'"
550 ct=$(($ct+1))
551 [ $ct -lt $numsrc ] && echo
552 done
554 echo ")"
555 done
558 check_checksums() {
559 [ ${#source[@]} -eq 0 ] && return 0
561 if [ ! $(type -p openssl) ]; then
562 error "$(gettext "Cannot find openssl.")"
563 exit 1 # $E_MISSING_PROGRAM
566 local correlation=0
567 local integ required
568 for integ in md5 sha1 sha256 sha384 sha512; do
569 local integrity_sums=($(eval echo "\${${integ}sums[@]}"))
570 if [ ${#integrity_sums[@]} -eq ${#source[@]} ]; then
571 msg "$(gettext "Validating source files with %s...")" "${integ}sums"
572 correlation=1
573 local errors=0
574 local idx=0
575 local file
576 for file in "${source[@]}"; do
577 local found=1
578 file="$(get_filename "$file")"
579 echo -n " $file ... " >&2
581 if [ ! -f "$file" ] ; then
582 if [ ! -f "$SRCDEST/$file" ] ; then
583 echo "$(gettext "NOT FOUND")" >&2
584 errors=1
585 found=0
586 else
587 file="$SRCDEST/$file"
591 if [ $found -gt 0 ] ; then
592 local expectedsum="$(echo ${integrity_sums[$idx]} | tr '[A-F]' '[a-f]')"
593 local realsum="$(openssl dgst -${integ} "$file" | awk '{print $NF}')"
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 -gt 0 ]; then
606 error "$(gettext "One or more files did not pass the validity check!")"
607 exit 1 # TODO: error code
609 elif [ ${#integrity_sums[@]} -gt 0 ]; 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 -eq 0 ]; then
616 error "$(gettext "Integrity checks are missing.")"
617 exit 1 # TODO: error code
621 extract_sources() {
622 msg "$(gettext "Extracting Sources...")"
623 local netfile
624 for netfile in "${source[@]}"; do
625 file=$(get_filename "$netfile")
626 if in_array "$file" ${noextract[@]}; then
627 #skip source files in the noextract=() array
628 # these are marked explicitly to NOT be extracted
629 continue
632 if [ ! -f "$file" ] ; then
633 if [ ! -f "$SRCDEST/$file" ] ; then
634 error "$(gettext "Unable to find source file %s for extraction.")" "$file"
635 plain "$(gettext "Aborting...")"
636 exit 1
637 else
638 file="$SRCDEST/$file"
642 # fix flyspray #6246
643 local file_type=$(file -bizL "$file")
644 local cmd=''
645 case "$file_type" in
646 *application/x-tar*|*application/zip*|*application/x-zip*|*application/x-cpio*)
647 cmd="bsdtar -x -f $file" ;;
648 *application/x-gzip*)
649 cmd="gunzip -d -f $file" ;;
650 *application/x-bzip*)
651 cmd="bunzip2 -f $file" ;;
652 *application/x-xz*)
653 cmd="xz -d -f $file" ;;
655 # Don't know what to use to extract this file,
656 # skip to the next file
657 continue;;
658 esac
660 local ret=0
661 msg2 "$cmd"
662 $cmd || ret=$?
663 if [ $ret -ne 0 ]; then
664 error "$(gettext "Failed to extract %s")" "$file"
665 plain "$(gettext "Aborting...")"
666 exit 1
668 done
670 if [ $EUID -eq 0 ]; then
671 # change perms of all source files to root user & root group
672 chown -R 0:0 "$srcdir"
676 run_build() {
677 # use distcc if it is requested (check buildenv and PKGBUILD opts)
678 if [ "$(check_buildenv distcc)" = "y" -a "$(check_option distcc)" != "n" ]; then
679 [ -d /usr/lib/distcc/bin ] && export PATH="/usr/lib/distcc/bin:$PATH"
680 export DISTCC_HOSTS
681 elif [ "$(check_option distcc)" = "n" ]; then
682 # if it is not wanted, clear the makeflags too
683 MAKEFLAGS=""
686 # use ccache if it is requested (check buildenv and PKGBUILD opts)
687 if [ "$(check_buildenv ccache)" = "y" -a "$(check_option ccache)" != "n" ]; then
688 [ -d /usr/lib/ccache/bin ] && export PATH="/usr/lib/ccache/bin:$PATH"
691 # clear user-specified makeflags if requested
692 if [ "$(check_option makeflags)" = "n" ]; then
693 MAKEFLAGS=""
696 msg "$(gettext "Starting build()...")"
697 cd "$srcdir"
699 # ensure all necessary build variables are exported
700 export CFLAGS CXXFLAGS MAKEFLAGS LDFLAGS CHOST
701 # save our shell options so build() can't override what we need
702 local shellopts=$(shopt -p)
704 local ret=0
705 if [ "$LOGGING" -eq 1 ]; then
706 BUILDLOG="${startdir}/${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-build.log"
707 if [ -f "$BUILDLOG" ]; then
708 local i=1
709 while true; do
710 if [ -f "$BUILDLOG.$i" ]; then
711 i=$(($i +1))
712 else
713 break
715 done
716 mv "$BUILDLOG" "$BUILDLOG.$i"
719 build 2>&1 | tee "$BUILDLOG"; ret=${PIPESTATUS[0]}
720 else
721 # piping to cat ensures makepkg fails on any build error
722 build 2>&1 | cat -; ret=${PIPESTATUS[0]}
725 # reset our shell options
726 eval "$shellopts"
728 if [ $ret -gt 0 ]; then
729 error "$(gettext "Build Failed.")"
730 plain "$(gettext "Aborting...")"
731 remove_deps
732 exit 2 # $E_BUILD_FAILED
736 run_package() {
737 if [ -z "$1" ]; then
738 pkgfunc="package"
739 nameofpkg="$pkgname"
740 else
741 pkgfunc="package_$1"
742 nameofpkg="$1"
745 # clear user-specified makeflags if requested
746 if [ "$(check_option makeflags)" = "n" ]; then
747 MAKEFLAGS=""
750 msg "$(gettext "Starting %s()...")" "$pkgfunc"
751 cd "$srcdir"
753 # ensure all necessary build variables are exported
754 export CFLAGS CXXFLAGS LDFLAGS MAKEFLAGS CHOST
756 local ret=0
757 if [ "$LOGGING" -eq 1 ]; then
758 BUILDLOG="${startdir}/${nameofpkg}-${pkgver}-${pkgrel}-${CARCH}-package.log"
759 if [ -f "$BUILDLOG" ]; then
760 local i=1
761 while true; do
762 if [ -f "$BUILDLOG.$i" ]; then
763 i=$(($i +1))
764 else
765 break
767 done
768 mv "$BUILDLOG" "$BUILDLOG.$i"
771 # ensure overridden package variables suvrive tee with split packages
772 logpipe=$(mktemp -u "$startdir/logpipe.XXXXXXXX")
773 mknod "$logpipe" p
774 exec 3>&1
775 tee "$BUILDLOG" < "$logpipe" &
776 exec 1>"$logpipe" 2>"$logpipe"
777 $pkgfunc 2>&1 | cat -; ret=${PIPESTATUS[0]}
778 sync
779 exec 1>&3 2>&3 3>&-
780 rm "$logpipe"
781 else
782 $pkgfunc 2>&1 | cat -; ret=${PIPESTATUS[0]}
785 if [ $ret -gt 0 ]; then
786 error "$(gettext "Packaging Failed.")"
787 plain "$(gettext "Aborting...")"
788 remove_deps
789 exit 2 # $E_BUILD_FAILED
793 tidy_install() {
794 cd "$pkgdir"
795 msg "$(gettext "Tidying install...")"
797 if [ "$(check_option docs)" = "n" -a -n "${DOC_DIRS[*]}" ]; then
798 msg2 "$(gettext "Removing doc files...")"
799 rm -rf ${DOC_DIRS[@]}
802 if [ "$(check_option purge)" = "y" -a -n "${PURGE_TARGETS[*]}" ]; then
803 msg2 "$(gettext "Purging other files...")"
804 local pt
805 for pt in "${PURGE_TARGETS[@]}"; do
806 if [ "${pt}" = "${pt//\/}" ]; then
807 find . -type f -name "${pt}" -exec rm -f -- '{}' \;
808 else
809 rm -f ${pt}
811 done
814 if [ "$(check_option zipman)" = "y" -a -n "${MAN_DIRS[*]}" ]; then
815 msg2 "$(gettext "Compressing man and info pages...")"
816 local manpage ext file link hardlinks hl
817 find ${MAN_DIRS[@]} -type f 2>/dev/null |
818 while read manpage ; do
819 # check file still exists (potentially compressed with hard link)
820 if [ -f ${manpage} ]; then
821 ext="${manpage##*.}"
822 file="${manpage##*/}"
823 if [ "$ext" != "gz" -a "$ext" != "bz2" ]; then
824 # update symlinks to this manpage
825 find ${MAN_DIRS[@]} -lname "$file" 2>/dev/null |
826 while read link ; do
827 rm -f "$link"
828 ln -sf "${file}.gz" "${link}.gz"
829 done
830 # find hard links and remove them
831 # the '|| true' part keeps the script from bailing if find returned an
832 # error, such as when one of the man directories doesn't exist
833 hardlinks="$(find ${MAN_DIRS[@]} \! -name "$file" -samefile "$manpage" 2>/dev/null)" || true
834 for hl in ${hardlinks}; do
835 rm -f "${hl}";
836 done
837 # compress the original
838 gzip -9 "$manpage"
839 # recreate hard links removed earlier
840 for hl in ${hardlinks}; do
841 ln "${manpage}.gz" "${hl}.gz"
842 chmod 644 ${hl}.gz
843 done
846 done
849 if [ "$(check_option strip)" = "y" -a -n "${STRIP_DIRS[*]}" ]; then
850 msg2 "$(gettext "Stripping debugging symbols from binaries and libraries...")"
851 local binary
852 find ${STRIP_DIRS[@]} -type f 2>/dev/null | while read binary ; do
853 case "$(file -biz "$binary")" in
854 *compressed-encoding*) # Skip compressed binarys
856 *application/x-sharedlib*) # Libraries (.so)
857 /usr/bin/strip -S "$binary";;
858 *application/x-archive*) # Libraries (.a)
859 /usr/bin/strip -S "$binary";;
860 *application/x-executable*) # Binaries
861 /usr/bin/strip "$binary";;
862 esac
863 done
866 if [ "$(check_option libtool)" = "n" ]; then
867 msg2 "$(gettext "Removing libtool .la files...")"
868 find . ! -type d -name "*.la" -exec rm -f -- '{}' \;
871 if [ "$(check_option emptydirs)" = "n" ]; then
872 msg2 "$(gettext "Removing empty directories...")"
873 find . -depth -type d -empty -delete
877 write_pkginfo() {
878 local builddate=$(date -u "+%s")
879 if [ -n "$PACKAGER" ]; then
880 local packager="$PACKAGER"
881 else
882 local packager="Unknown Packager"
884 local size=$(du -sk | awk '{print $1 * 1024}')
886 msg2 "$(gettext "Generating .PKGINFO file...")"
887 echo "# Generated by makepkg $myver" >.PKGINFO
888 if [ "$INFAKEROOT" -eq 1 ]; then
889 echo "# using $(fakeroot -v)" >>.PKGINFO
891 echo "# $(LC_ALL=C date -u)" >>.PKGINFO
892 echo "pkgname = $1" >>.PKGINFO
893 echo "pkgver = $pkgver-$pkgrel" >>.PKGINFO
894 echo "pkgdesc = $pkgdesc" >>.PKGINFO
895 echo "url = $url" >>.PKGINFO
896 echo "builddate = $builddate" >>.PKGINFO
897 echo "packager = $packager" >>.PKGINFO
898 echo "size = $size" >>.PKGINFO
899 if [ -n "$CARCH" ]; then
900 echo "arch = $CARCH" >>.PKGINFO
902 if [ "$(check_option force)" = "y" ]; then
903 echo "force = true" >> .PKGINFO
906 local it
907 for it in "${license[@]}"; do
908 echo "license = $it" >>.PKGINFO
909 done
910 for it in "${replaces[@]}"; do
911 echo "replaces = $it" >>.PKGINFO
912 done
913 for it in "${groups[@]}"; do
914 echo "group = $it" >>.PKGINFO
915 done
916 for it in "${depends[@]}"; do
917 echo "depend = $it" >>.PKGINFO
918 done
919 for it in "${optdepends[@]}"; do
920 echo "optdepend = $it" >>.PKGINFO
921 done
922 for it in "${conflicts[@]}"; do
923 echo "conflict = $it" >>.PKGINFO
924 done
925 for it in "${provides[@]}"; do
926 echo "provides = $it" >>.PKGINFO
927 done
928 for it in "${backup[@]}"; do
929 echo "backup = $it" >>.PKGINFO
930 done
931 for it in "${packaging_options[@]}"; do
932 local ret="$(check_option $it)"
933 if [ "$ret" != "?" ]; then
934 if [ "$ret" = "y" ]; then
935 echo "makepkgopt = $it" >>.PKGINFO
936 else
937 echo "makepkgopt = !$it" >>.PKGINFO
940 done
942 # TODO maybe remove this at some point
943 # warn if license array is not present or empty
944 if [ -z "$license" ]; then
945 warning "$(gettext "Please add a license line to your %s!")" "$BUILDSCRIPT"
946 plain "$(gettext "Example for GPL\'ed software: license=('GPL').")"
950 create_package() {
951 if [ ! -d "$pkgdir" ]; then
952 error "$(gettext "Missing pkg/ directory.")"
953 plain "$(gettext "Aborting...")"
954 exit 1 # $E_MISSING_PKGDIR
957 if [ -z "$1" ]; then
958 nameofpkg="$pkgname"
959 else
960 nameofpkg="$1"
963 write_pkginfo $nameofpkg
965 cd "$pkgdir"
966 msg "$(gettext "Creating package...")"
968 local comp_files=".PKGINFO"
970 # check for an install script
971 if [ -n "$install" ]; then
972 msg2 "$(gettext "Adding install script...")"
973 cp "$startdir/$install" .INSTALL
974 comp_files="$comp_files .INSTALL"
977 # do we have a changelog?
978 if [ -f "$startdir/ChangeLog" ]; then
979 msg2 "$(gettext "Adding package changelog...")"
980 cp "$startdir/ChangeLog" .CHANGELOG
981 comp_files="$comp_files .CHANGELOG"
984 # tar it up
985 msg2 "$(gettext "Compressing package...")"
987 case "$PKGEXT" in
988 *tar.gz) EXT=${PKGEXT%.gz} ;;
989 *tar.bz2) EXT=${PKGEXT%.bz2} ;;
990 *tar.xz) EXT=${PKGEXT%.xz} ;;
991 *) warning "$(gettext "'%s' is not a valid archive extension.")" \
992 "$PKGEXT" ; EXT=$PKGEXT ;;
993 esac
994 local pkg_file="$PKGDEST/${nameofpkg}-${pkgver}-${pkgrel}-${CARCH}${EXT}"
996 local ret=0
998 # when fileglobbing, we want * in an empty directory to expand to
999 # the null string rather than itself
1000 shopt -s nullglob
1001 bsdtar -cf - $comp_files * > "$pkg_file" || ret=$?
1002 shopt -u nullglob
1004 if [ $ret -eq 0 ]; then
1005 case "$PKGEXT" in
1006 *tar.gz) gzip -f -n "$pkg_file" ;;
1007 *tar.bz2) bzip2 -f "$pkg_file" ;;
1008 *tar.xz) xz -z -f "$pkg_file" ;;
1009 esac
1010 ret=$?
1013 if [ $ret -ne 0 ]; then
1014 error "$(gettext "Failed to create package file.")"
1015 exit 1 # TODO: error code
1019 create_srcpackage() {
1020 cd "$startdir"
1022 # Get back to our src directory so we can begin with sources.
1023 mkdir -p "$srcdir"
1024 cd "$srcdir"
1025 download_sources
1026 # We can only check checksums if we have all files.
1027 check_checksums
1028 cd "$startdir"
1030 msg "$(gettext "Creating source package...")"
1031 local srclinks="$(mktemp -d "$startdir"/srclinks.XXXXXXXXX)"
1032 mkdir "${srclinks}"/${pkgbase}
1034 msg2 "$(gettext "Adding %s...")" "$BUILDSCRIPT"
1035 ln -s "${startdir}/${BUILDSCRIPT}" "${srclinks}/${pkgbase}/"
1037 if [ -n "$install" ]; then
1038 if [ -f $install ]; then
1039 msg2 "$(gettext "Adding install script...")"
1040 ln -s "${startdir}/$install" "${srclinks}/${pkgbase}/"
1041 else
1042 error "$(gettext "Install script %s not found.")" "$install"
1046 if [ -f ChangeLog ]; then
1047 msg2 "$(gettext "Adding %s...")" "ChangeLog"
1048 ln -s "${startdir}/ChangeLog" "${srclinks}/${pkgbase}"
1051 local netfile
1052 for netfile in "${source[@]}"; do
1053 local file=$(get_filename "$netfile")
1054 if [ -f "$netfile" ]; then
1055 msg2 "$(gettext "Adding %s...")" "$netfile"
1056 ln -s "${startdir}/$netfile" "${srclinks}/${pkgbase}"
1057 elif [ "$SOURCEONLY" -eq 2 -a -f "$SRCDEST/$file" ]; then
1058 msg2 "$(gettext "Adding %s...")" "$file"
1059 ln -s "$SRCDEST/$file" "${srclinks}/${pkgbase}/"
1061 done
1063 local TAR_OPT
1064 case "$SRCEXT" in
1065 *tar.gz) TAR_OPT="z" ;;
1066 *tar.bz2) TAR_OPT="j" ;;
1067 *tar.xz) TAR_OPT="J" ;;
1068 *) warning "$(gettext "'%s' is not a valid archive extension.")" \
1069 "$SRCEXT" ;;
1070 esac
1072 local pkg_file="$PKGDEST/${pkgbase}-${pkgver}-${pkgrel}${SRCEXT}"
1074 # tar it up
1075 msg2 "$(gettext "Compressing source package...")"
1076 cd "${srclinks}"
1077 if ! bsdtar -c${TAR_OPT}Lf "$pkg_file" ${pkgbase}; then
1078 error "$(gettext "Failed to create source package file.")"
1079 exit 1 # TODO: error code
1081 cd "${startdir}"
1082 rm -rf "${srclinks}"
1085 install_package() {
1086 [ "$INSTALL" -eq 0 ] && return
1088 if [ "$SPLITPKG" -eq 0 ]; then
1089 msg "$(gettext "Installing package ${pkgname} with pacman -U...")"
1090 else
1091 msg "$(gettext "Installing ${pkgbase} package group with pacman -U...")"
1094 local pkglist
1095 for pkg in ${pkgname[@]}; do
1096 pkglist="${pkglist} $PKGDEST/${pkg}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT}"
1097 done
1099 local ret=0
1100 if [ "$ASROOT" -eq 0 ]; then
1101 sudo pacman $PACMAN_OPTS -U ${pkglist} || ret=$?
1102 else
1103 pacman $PACMAN_OPTS -U ${pkglist} || ret=$?
1106 if [ $ret -ne 0 ]; then
1107 warning "$(gettext "Failed to install built package(s).")"
1108 return 0
1112 check_sanity() {
1113 # check for no-no's in the build script
1114 if [ -z "$pkgname" ]; then
1115 error "$(gettext "%s is not allowed to be empty.")" "pkgname"
1116 return 1
1118 if [ -z "$pkgver" ]; then
1119 error "$(gettext "%s is not allowed to be empty.")" "pkgver"
1120 return 1
1122 if [ -z "$pkgrel" ]; then
1123 error "$(gettext "%s is not allowed to be empty.")" "pkgrel"
1124 return 1
1126 if [ "$pkgver" != "${pkgver//-/}" ]; then
1127 error "$(gettext "%s is not allowed to contain hyphens.")" "pkgver"
1128 return 1
1130 if [ "$pkgrel" != "${pkgrel//-/}" ]; then
1131 error "$(gettext "%s is not allowed to contain hyphens.")" "pkgrel"
1132 return 1
1135 if [ "$arch" = 'any' ]; then
1136 CARCH='any'
1139 pkgbase=${pkgbase:-${pkgname[0]}}
1141 if ! in_array $CARCH ${arch[@]}; then
1142 if [ "$IGNOREARCH" -eq 0 ]; then
1143 error "$(gettext "%s is not available for the '%s' architecture.")" "$pkgbase" "$CARCH"
1144 plain "$(gettext "Note that many packages may need a line added to their %s")" "$BUILDSCRIPT"
1145 plain "$(gettext "such as arch=('%s').")" "$CARCH"
1146 return 1
1150 local provide
1151 for provide in ${provides[@]}; do
1152 if [ $provide != ${provide//</} -o $provide != ${provide//>/} ]; then
1153 error "$(gettext "Provides array cannot contain comparison (< or >) operators.")"
1154 return 1
1156 done
1158 if [ "$install" -a ! -f "$install" ]; then
1159 error "$(gettext "Install scriptlet (%s) does not exist.")" "$install"
1160 return 1
1163 local valid_options=1
1164 local opt known kopt
1165 for opt in ${options[@]}; do
1166 known=0
1167 # check if option matches a known option or its inverse
1168 for kopt in ${packaging_options[@]} ${other_options[@]}; do
1169 if [ "${opt}" = "${kopt}" -o "${opt}" = "!${kopt}" ]; then
1170 known=1
1172 done
1173 if [ $known -eq 0 ]; then
1174 error "$(gettext "options array contains unknown option '%s'")" "$opt"
1175 valid_options=0
1177 done
1178 if [ $valid_options -eq 0 ]; then
1179 return 1
1182 return 0
1185 devel_check() {
1186 newpkgver=""
1188 # Do not update pkgver if --holdver is set, when building a source package,
1189 # or when reading PKGBUILD from pipe
1190 if [ "$HOLDVER" -eq 1 -o "$SOURCEONLY" -ne 0 -o ! -f "./$BUILDSCRIPT" ]; then
1191 return
1194 if [ -z "$FORCE_VER" ]; then
1195 # Check if this is a svn/cvs/etc PKGBUILD; set $newpkgver if so.
1196 # This will only be used on the first call to makepkg; subsequent
1197 # calls to makepkg via fakeroot will explicitly pass the version
1198 # number to avoid having to determine the version number twice.
1199 # Also do a brief check to make sure we have the VCS tool available.
1200 oldpkgver=$pkgver
1201 if [ -n "${_darcstrunk}" -a -n "${_darcsmod}" ] ; then
1202 [ $(type -p darcs) ] || return 0
1203 msg "$(gettext "Determining latest darcs revision...")"
1204 newpkgver=$(date +%Y%m%d)
1205 elif [ -n "${_cvsroot}" -a -n "${_cvsmod}" ] ; then
1206 [ $(type -p cvs) ] || return 0
1207 msg "$(gettext "Determining latest cvs revision...")"
1208 newpkgver=$(date +%Y%m%d)
1209 elif [ -n "${_gitroot}" -a -n "${_gitname}" ] ; then
1210 [ $(type -p git) ] || return 0
1211 msg "$(gettext "Determining latest git revision...")"
1212 newpkgver=$(date +%Y%m%d)
1213 elif [ -n "${_svntrunk}" -a -n "${_svnmod}" ] ; then
1214 [ $(type -p svn) ] || return 0
1215 msg "$(gettext "Determining latest svn revision...")"
1216 newpkgver=$(LC_ALL=C svn info $_svntrunk | sed -n 's/^Last Changed Rev: \([0-9]*\)$/\1/p')
1217 elif [ -n "${_bzrtrunk}" -a -n "${_bzrmod}" ] ; then
1218 [ $(type -p bzr) ] || return 0
1219 msg "$(gettext "Determining latest bzr revision...")"
1220 newpkgver=$(bzr revno ${_bzrtrunk})
1221 elif [ -n "${_hgroot}" -a -n "${_hgrepo}" ] ; then
1222 [ $(type -p hg) ] || return 0
1223 msg "$(gettext "Determining latest hg revision...")"
1224 if [ -d ./src/$_hgrepo ] ; then
1225 cd ./src/$_hgrepo
1226 hg pull
1227 hg update
1228 else
1229 [[ ! -d ./src/ ]] && mkdir ./src/
1230 hg clone $_hgroot/$_hgrepo ./src/$_hgrepo
1231 cd ./src/$_hgrepo
1233 newpkgver=$(hg tip --template "{rev}")
1234 cd ../../
1237 if [ -n "$newpkgver" ]; then
1238 msg2 "$(gettext "Version found: %s")" "$newpkgver"
1241 else
1242 # Version number retrieved from fakeroot->makepkg argument
1243 newpkgver=$FORCE_VER
1247 devel_update() {
1248 # This is lame, but if we're wanting to use an updated pkgver for
1249 # retrieving svn/cvs/etc sources, we'll update the PKGBUILD with
1250 # the new pkgver and then re-source it. This is the most robust
1251 # method for dealing with PKGBUILDs that use, e.g.:
1253 # pkgver=23
1254 # ...
1255 # _foo=pkgver
1257 if [ -n "$newpkgver" ]; then
1258 if [ "$newpkgver" != "$pkgver" ]; then
1259 if [ -f "./$BUILDSCRIPT" ]; then
1260 @SEDINPLACE@ "s/^pkgver=[^ ]*/pkgver=$newpkgver/" "./$BUILDSCRIPT"
1261 @SEDINPLACE@ "s/^pkgrel=[^ ]*/pkgrel=1/" "./$BUILDSCRIPT"
1262 source "$BUILDSCRIPT"
1268 backup_package_variables() {
1269 for var in ${splitpkg_overrides[@]}; do
1270 indirect="${var}_backup"
1271 eval "${indirect}=(\${$var[@]})"
1272 done
1275 restore_package_variables() {
1276 for var in ${splitpkg_overrides[@]}; do
1277 indirect="${var}_backup"
1278 if [ -n "${!indirect}" ]; then
1279 eval "${var}=(\${$indirect[@]})"
1280 else
1281 unset ${var}
1283 done
1285 # pkgdesc gets restored as an array - convert back to a string
1286 local pkgdesc_backup="${pkgdesc[@]}"
1287 unset pkgdesc
1288 pkgdesc=${pkgdesc_backup}
1291 # getopt like parser
1292 parse_options() {
1293 local short_options=$1; shift;
1294 local long_options=$1; shift;
1295 local ret=0;
1296 local unused_options=""
1298 while [ -n "$1" ]; do
1299 if [ ${1:0:2} = '--' ]; then
1300 if [ -n "${1:2}" ]; then
1301 local match=""
1302 for i in ${long_options//,/ }; do
1303 if [ ${1:2} = ${i//:} ]; then
1304 match=$i
1305 break
1307 done
1308 if [ -n "$match" ]; then
1309 if [ ${1:2} = $match ]; then
1310 printf ' %s' "$1"
1311 else
1312 if [ -n "$2" ]; then
1313 printf ' %s' "$1"
1314 shift
1315 printf " '%s'" "$1"
1316 else
1317 echo "makepkg: option '$1' $(gettext "requires an argument")" >&2
1318 ret=1
1321 else
1322 echo "makepkg: $(gettext "unrecognized option") '$1'" >&2
1323 ret=1
1325 else
1326 shift
1327 break
1329 elif [ ${1:0:1} = '-' ]; then
1330 for ((i=1; i<${#1}; i++)); do
1331 if [[ "$short_options" =~ "${1:i:1}" ]]; then
1332 if [[ "$short_options" =~ "${1:i:1}:" ]]; then
1333 if [ -n "${1:$i+1}" ]; then
1334 printf ' -%s' "${1:i:1}"
1335 printf " '%s'" "${1:$i+1}"
1336 else
1337 if [ -n "$2" ]; then
1338 printf ' -%s' "${1:i:1}"
1339 shift
1340 printf " '%s'" "${1}"
1341 else
1342 echo "makepkg: option $(gettext "requires an argument") -- '${1:i:1}'" >&2
1343 ret=1
1346 break
1347 else
1348 printf ' -%s' "${1:i:1}"
1350 else
1351 echo "makepkg: $(gettext "invalid option") -- '${1:i:1}'" >&2
1352 ret=1
1354 done
1355 else
1356 unused_options="${unused_options} '$1'"
1358 shift
1359 done
1361 printf " --"
1362 if [ -n "$unused_options" ]; then
1363 for i in ${unused_options[@]}; do
1364 printf ' %s' "$i"
1365 done
1367 if [ -n "$1" ]; then
1368 while [ -n "$1" ]; do
1369 printf " '%s'" "${1}"
1370 shift
1371 done
1373 printf "\n"
1375 return $ret
1378 usage() {
1379 printf "makepkg (pacman) %s\n" "$myver"
1380 echo
1381 printf "$(gettext "Usage: %s [options]")\n" "$0"
1382 echo
1383 echo "$(gettext "Options:")"
1384 printf "$(gettext " -A, --ignorearch Ignore incomplete arch field in %s")\n" "$BUILDSCRIPT"
1385 echo "$(gettext " -c, --clean Clean up work files after build")"
1386 echo "$(gettext " -C, --cleancache Clean up source files from the cache")"
1387 printf "$(gettext " --config <config> Use an alternate config file (instead of '%s')")\n" "$confdir/makepkg.conf"
1388 echo "$(gettext " -d, --nodeps Skip all dependency checks")"
1389 echo "$(gettext " -e, --noextract Do not extract source files (use existing src/ dir)")"
1390 echo "$(gettext " -f, --force Overwrite existing package")"
1391 echo "$(gettext " -g, --geninteg Generate integrity checks for source files")"
1392 echo "$(gettext " -h, --help This help")"
1393 echo "$(gettext " -i, --install Install package after successful build")"
1394 echo "$(gettext " -L, --log Log package build process")"
1395 echo "$(gettext " -m, --nocolor Disable colorized output messages")"
1396 echo "$(gettext " -o, --nobuild Download and extract files only")"
1397 printf "$(gettext " -p <buildscript> Use an alternate build script (instead of '%s')")\n" "$BUILDSCRIPT"
1398 echo "$(gettext " -r, --rmdeps Remove installed dependencies after a successful build")"
1399 # fix flyspray feature request #2978
1400 echo "$(gettext " -R, --repackage Repackage contents of the package without rebuilding")"
1401 echo "$(gettext " -s, --syncdeps Install missing dependencies with pacman")"
1402 echo "$(gettext " --allsource Generate a source-only tarball including downloaded sources")"
1403 echo "$(gettext " --asroot Allow makepkg to run as root user")"
1404 echo "$(gettext " --holdver Prevent automatic version bumping for development PKGBUILDs")"
1405 echo "$(gettext " --source Generate a source-only tarball without downloaded sources")"
1406 echo
1407 echo "$(gettext "These options can be passed to pacman:")"
1408 echo
1409 echo "$(gettext " --noconfirm Do not ask for confirmation when resolving dependencies")"
1410 echo "$(gettext " --noprogressbar Do not show a progress bar when downloading files")"
1411 echo
1412 printf "$(gettext "If -p is not specified, makepkg will look for '%s'")\n" "$BUILDSCRIPT"
1413 echo
1416 version() {
1417 printf "makepkg (pacman) %s\n" "$myver"
1418 printf "$(gettext "\
1419 Copyright (c) 2006-2009 Pacman Development Team <pacman-dev@archlinux.org>.\n\
1420 Copyright (C) 2002-2006 Judd Vinet <jvinet@zeroflux.org>.\n\n\
1421 This is free software; see the source for copying conditions.\n\
1422 There is NO WARRANTY, to the extent permitted by law.\n")"
1425 # PROGRAM START
1427 # determine whether we have gettext; make it a no-op if we do not
1428 if [ ! $(type -t gettext) ]; then
1429 gettext() {
1430 echo "$@"
1434 ARGLIST=$@
1436 # Parse Command Line Options.
1437 OPT_SHORT="AbcCdefFghiLmop:rRsV"
1438 OPT_LONG="allsource,asroot,ignorearch,builddeps,clean,cleancache,nodeps"
1439 OPT_LONG="$OPT_LONG,noextract,force,forcever:,geninteg,help,holdver"
1440 OPT_LONG="$OPT_LONG,install,log,nocolor,nobuild,rmdeps,repackage,source"
1441 OPT_LONG="$OPT_LONG,syncdeps,version,config:"
1442 # Pacman Options
1443 OPT_LONG="$OPT_LONG,noconfirm,noprogressbar"
1444 OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@" || echo 'PARSE_OPTIONS FAILED')"
1445 if echo "$OPT_TEMP" | grep -q 'PARSE_OPTIONS FAILED'; then
1446 # This is a small hack to stop the script bailing with 'set -e'
1447 echo; usage; exit 1 # E_INVALID_OPTION;
1449 eval set -- "$OPT_TEMP"
1450 unset OPT_SHORT OPT_LONG OPT_TEMP
1452 while true; do
1453 case "$1" in
1454 # Pacman Options
1455 --noconfirm) PACMAN_OPTS="$PACMAN_OPTS --noconfirm" ;;
1456 --noprogressbar) PACMAN_OPTS="$PACMAN_OPTS --noprogressbar" ;;
1458 # Makepkg Options
1459 --allsource) SOURCEONLY=2 ;;
1460 --asroot) ASROOT=1 ;;
1461 -A|--ignorearch) IGNOREARCH=1 ;;
1462 -c|--clean) CLEANUP=1 ;;
1463 -C|--cleancache) CLEANCACHE=1 ;;
1464 --config) shift; MAKEPKG_CONF=$1 ;;
1465 -d|--nodeps) NODEPS=1 ;;
1466 -e|--noextract) NOEXTRACT=1 ;;
1467 -f|--force) FORCE=1 ;;
1468 #hidden opt used by fakeroot call for svn/cvs/etc PKGBUILDs to set pkgver
1469 --forcever) shift; FORCE_VER=$1;;
1470 -F) INFAKEROOT=1 ;;
1471 -g|--geninteg) GENINTEG=1 ;;
1472 --holdver) HOLDVER=1 ;;
1473 -i|--install) INSTALL=1 ;;
1474 -L|--log) LOGGING=1 ;;
1475 -m|--nocolor) USE_COLOR='n' ;;
1476 -o|--nobuild) NOBUILD=1 ;;
1477 -p) shift; BUILDSCRIPT=$1 ;;
1478 -r|--rmdeps) RMDEPS=1 ;;
1479 -R|--repackage) REPKG=1 ;;
1480 --source) SOURCEONLY=1 ;;
1481 -s|--syncdeps) DEP_BIN=1 ;;
1483 -h|--help) usage; exit 0 ;; # E_OK
1484 -V|--version) version; exit 0 ;; # E_OK
1486 --) OPT_IND=0; shift; break;;
1487 *) usage; exit 1 ;; # E_INVALID_OPTION
1488 esac
1489 shift
1490 done
1492 #preserve environment variables
1493 _PKGDEST=${PKGDEST}
1494 _SRCDEST=${SRCDEST}
1496 # default config is makepkg.conf
1497 MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
1499 # Source the config file; fail if it is not found
1500 if [ -r "$MAKEPKG_CONF" ]; then
1501 source "$MAKEPKG_CONF"
1502 else
1503 error "$(gettext "%s not found.")" "$MAKEPKG_CONF"
1504 plain "$(gettext "Aborting...")"
1505 exit 1 # $E_CONFIG_ERROR
1508 # Source user-specific makepkg.conf overrides
1509 if [ -r ~/.makepkg.conf ]; then
1510 source ~/.makepkg.conf
1513 # check if messages are to be printed using color
1514 if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
1515 COLORMSG=1
1518 # override settings with an environment variable for batch processing
1519 PKGDEST=${_PKGDEST:-$PKGDEST}
1520 PKGDEST=${PKGDEST:-$startdir} #default to $startdir if undefined
1521 SRCDEST=${_SRCDEST:-$SRCDEST}
1522 SRCDEST=${SRCDEST:-$startdir} #default to $startdir if undefined
1525 if [ "$HOLDVER" -eq 1 -a -n "$FORCE_VER" ]; then
1526 # The '\\0' is here to prevent gettext from thinking --holdver is an option
1527 error "$(gettext "\\0--holdver and --forcever cannot both be specified" )"
1528 exit 1
1531 if [ "$CLEANCACHE" -eq 1 ]; then
1532 #fix flyspray feature request #5223
1533 if [ -n "$SRCDEST" -a "$SRCDEST" != "$startdir" ]; then
1534 msg "$(gettext "Cleaning up ALL files from %s.")" "$SRCDEST"
1535 echo -n "$(gettext " Are you sure you wish to do this? ")"
1536 echo -n "$(gettext "[Y/n]")"
1537 read answer
1538 answer=$(echo $answer | tr '[:lower:]' '[:upper:]')
1539 if [ "$answer" = "$(gettext "YES")" -o "$answer" = "$(gettext "Y")" ]; then
1540 rm "$SRCDEST"/*
1541 if [ $? -ne 0 ]; then
1542 error "$(gettext "Problem removing files; you may not have correct permissions in %s")" "$SRCDEST"
1543 exit 1
1544 else
1545 # removal worked
1546 msg "$(gettext "Source cache cleaned.")"
1547 exit 0
1549 else
1550 # answer = no
1551 msg "$(gettext "No files have been removed.")"
1552 exit 0
1554 else
1555 # $SRCDEST is $startdir, two possibilities
1556 error "$(gettext "Source destination must be defined in %s.")" "$MAKEPKG_CONF"
1557 plain "$(gettext "In addition, please run makepkg -C outside of your cache directory.")"
1558 exit 1
1562 if [ -z "$BUILDSCRIPT" ]; then
1563 error "$(gettext "BUILDSCRIPT is undefined! Ensure you have updated %s.")" "$MAKEPKG_CONF"
1564 exit 1
1567 if [ "$INFAKEROOT" -eq 0 ]; then
1568 if [ $EUID -eq 0 -a "$ASROOT" -eq 0 ]; then
1569 # Warn those who like to live dangerously.
1570 error "$(gettext "Running makepkg as root is a BAD idea and can cause")"
1571 plain "$(gettext "permanent, catastrophic damage to your system. If you")"
1572 plain "$(gettext "wish to run as root, please use the --asroot option.")"
1573 exit 1 # $E_USER_ABORT
1574 elif [ $EUID -gt 0 -a "$ASROOT" -eq 1 ]; then
1575 # Warn those who try to use the --asroot option when they are not root
1576 error "$(gettext "The --asroot option is meant for the root user only.")"
1577 plain "$(gettext "Please rerun makepkg without the --asroot flag.")"
1578 exit 1 # $E_USER_ABORT
1579 elif [ "$(check_buildenv fakeroot)" = "y" -a $EUID -gt 0 ]; then
1580 if [ ! $(type -p fakeroot) ]; then
1581 error "$(gettext "Fakeroot must be installed if using the 'fakeroot' option")"
1582 plain "$(gettext "in the BUILDENV array in %s.")" "$MAKEPKG_CONF"
1583 exit 1
1585 elif [ $EUID -gt 0 ]; then
1586 warning "$(gettext "Running makepkg as an unprivileged user will result in non-root")"
1587 plain "$(gettext "ownership of the packaged files. Try using the fakeroot environment by")"
1588 plain "$(gettext "placing 'fakeroot' in the BUILDENV array in %s.")" "$MAKEPKG_CONF"
1589 sleep 1
1591 else
1592 if [ -z "$FAKEROOTKEY" ]; then
1593 error "$(gettext "Do not use the '-F' option. This option is only for use by makepkg.")"
1594 exit 1 # TODO: error code
1598 # check for sudo if we will need it during makepkg execution
1599 if [ "$ASROOT" -eq 0 \
1600 -a \( "$DEP_BIN" -eq 1 -o "$RMDEPS" -eq 1 -o "$INSTALL" -eq 1 \) ]; then
1601 if [ ! "$(type -p sudo)" ]; then
1602 error "$(gettext "Cannot find the sudo binary! Is sudo installed?")"
1603 plain "$(gettext "Missing dependencies cannot be installed or removed as a normal user")"
1604 plain "$(gettext "without sudo; install and configure sudo to auto-resolve dependencies.")"
1605 exit 1
1609 unset pkgname pkgbase pkgver pkgrel pkgdesc url license groups provides
1610 unset md5sums replaces depends conflicts backup source install build
1611 unset makedepends optdepends options noextract
1613 if [ ! -f "$BUILDSCRIPT" ]; then
1614 if [ -t 0 ]; then
1615 error "$(gettext "%s does not exist.")" "$BUILDSCRIPT"
1616 exit 1
1617 else
1618 # PKGBUILD passed through a pipe
1619 BUILDSCRIPT=/dev/stdin
1620 source "$BUILDSCRIPT"
1622 else
1623 crlftest=$(file $BUILDSCRIPT | grep -F 'CRLF' || true)
1624 if [ -n "$crlftest" ]; then
1625 error "$(gettext "%s contains CRLF characters and cannot be sourced.")" "$BUILDSCRIPT"
1626 exit 1
1629 source ./"$BUILDSCRIPT"
1632 if [ "$GENINTEG" -eq 1 ]; then
1633 mkdir -p "$srcdir"
1634 cd "$srcdir"
1635 download_sources
1636 generate_checksums
1637 exit 0 # $E_OK
1640 if [ "$(type -t package)" = "function" ]; then
1641 PKGFUNC=1
1644 # check the PKGBUILD for some basic requirements
1645 check_sanity || exit 1
1647 # We need to run devel_update regardless of whether we are in the fakeroot
1648 # build process so that if the user runs makepkg --forcever manually, we
1649 # 1) output the correct pkgver, and 2) use the correct filename when
1650 # checking if the package file already exists - fixes FS #9194
1651 devel_check
1652 devel_update
1654 if [ "${#pkgname[@]}" -gt "1" ]; then
1655 SPLITPKG=1
1658 if [ "$SPLITPKG" -eq 0 ]; then
1659 if [ -f "$PKGDEST/${pkgname}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT}" \
1660 -a "$FORCE" -eq 0 -a "$SOURCEONLY" -eq 0 -a "$NOBUILD" -eq 0 ]; then
1661 if [ "$INSTALL" -eq 1 ]; then
1662 warning "$(gettext "A package has already been built, installing existing package...")"
1663 install_package
1664 exit $?
1665 else
1666 error "$(gettext "A package has already been built. (use -f to overwrite)")"
1667 exit 1
1670 else
1671 allpkgbuilt=1
1672 somepkgbuilt=0
1673 for pkg in ${pkgname[@]}; do
1674 if [ -f "$PKGDEST/${pkg}-${pkgver}-${pkgrel}-${CARCH}${PKGEXT}" ]; then
1675 somepkgbuilt=1
1676 else
1677 allpkgbuilt=0
1679 done
1680 if [ "$FORCE" -eq 0 -a "$SOURCEONLY" -eq 0 -a "$NOBUILD" -eq 0 ]; then
1681 if [ "$allpkgbuilt" -eq 1 ]; then
1682 if [ "$INSTALL" -eq 1 ]; then
1683 warning "$(gettext "The package group has already been built, installing existing packages...")"
1684 install_package
1685 exit $?
1686 else
1687 error "$(gettext "The package group has already been built. (use -f to overwrite)")"
1688 exit 1
1691 if [ "$somepkgbuilt" -eq 1 ]; then
1692 error "$(gettext "Part of the package group has already been built. (use -f to overwrite)")"
1693 exit 1
1696 unset allpkgbuilt somepkgbuilt
1699 # Run the bare minimum in fakeroot
1700 if [ "$INFAKEROOT" -eq 1 ]; then
1701 if [ "$SPLITPKG" -eq 0 ]; then
1702 if [ "$PKGFUNC" -eq 0 ]; then
1703 if [ "$REPKG" -eq 0 ]; then
1704 run_build
1705 tidy_install
1707 else
1708 run_package
1709 tidy_install
1711 create_package
1712 else
1713 for pkg in ${pkgname[@]}; do
1714 pkgdir="$pkgdir/$pkg"
1715 mkdir -p "$pkgdir"
1716 backup_package_variables
1717 run_package $pkg
1718 tidy_install
1719 create_package $pkg
1720 restore_package_variables
1721 pkgdir="${pkgdir%/*}"
1722 done
1725 msg "$(gettext "Leaving fakeroot environment.")"
1726 exit 0 # $E_OK
1729 msg "$(gettext "Making package: %s")" "$pkgbase $pkgver-$pkgrel $CARCH ($(date))"
1731 # if we are creating a source-only package, go no further
1732 if [ "$SOURCEONLY" -ne 0 ]; then
1733 if [ -f "$PKGDEST/${pkgbase}-${pkgver}-${pkgrel}${SRCEXT}" \
1734 -a "$FORCE" -eq 0 ]; then
1735 error "$(gettext "A package has already been built. (use -f to overwrite)")"
1736 exit 1
1738 create_srcpackage
1739 msg "$(gettext "Source package created: %s")" "$pkgbase ($(date))"
1740 exit 0
1743 # fix flyspray bug #5973
1744 if [ "$NODEPS" -eq 1 -o "$NOBUILD" -eq 1 -o "$REPKG" -eq 1 ]; then
1745 # no warning message needed for nobuild, repkg
1746 if [ "$NODEPS" -eq 1 ]; then
1747 warning "$(gettext "Skipping dependency checks.")"
1749 elif [ $(type -p pacman) ]; then
1750 unset pkgdeps # Set by resolve_deps() and used by remove_deps()
1751 deperr=0
1753 msg "$(gettext "Checking Runtime Dependencies...")"
1754 resolve_deps ${depends[@]} || deperr=1
1756 msg "$(gettext "Checking Buildtime Dependencies...")"
1757 resolve_deps ${makedepends[@]} || deperr=1
1759 if [ $deperr -eq 1 ]; then
1760 error "$(gettext "Could not resolve all dependencies.")"
1761 exit 1
1763 else
1764 warning "$(gettext "pacman was not found in PATH; skipping dependency checks.")"
1767 # ensure we have a sane umask set
1768 umask 0022
1770 # get back to our src directory so we can begin with sources
1771 mkdir -p "$srcdir"
1772 cd "$srcdir"
1774 if [ "$NOEXTRACT" -eq 1 ]; then
1775 warning "$(gettext "Skipping source retrieval -- using existing src/ tree")"
1776 warning "$(gettext "Skipping source integrity checks -- using existing src/ tree")"
1777 warning "$(gettext "Skipping source extraction -- using existing src/ tree")"
1779 if [ "$NOEXTRACT" -eq 1 -a -z "$(ls "$srcdir" 2>/dev/null)" ]; then
1780 error "$(gettext "The source directory is empty, there is nothing to build!")"
1781 plain "$(gettext "Aborting...")"
1782 exit 1
1784 elif [ "$REPKG" -eq 1 ]; then
1785 if [ "$PKGFUNC" -eq 0 -a "$SPLITPKG" -eq 0 \
1786 -a \( ! -d "$pkgdir" -o -z "$(ls "$pkgdir" 2>/dev/null)" \) ]; then
1787 error "$(gettext "The package directory is empty, there is nothing to repackage!")"
1788 plain "$(gettext "Aborting...")"
1789 exit 1
1791 else
1792 download_sources
1793 check_checksums
1794 extract_sources
1797 if [ "$NOBUILD" -eq 1 ]; then
1798 msg "$(gettext "Sources are ready.")"
1799 exit 0 #E_OK
1800 else
1801 # check for existing pkg directory; don't remove if we are repackaging
1802 if [ -d "$pkgdir" \
1803 -a \( "$REPKG" -eq 0 -o "$PKGFUNC" -eq 1 -o "$SPLITPKG" -eq 1 \) ]; then
1804 msg "$(gettext "Removing existing pkg/ directory...")"
1805 rm -rf "$pkgdir"
1807 mkdir -p "$pkgdir"
1808 cd "$startdir"
1810 # if we are root or if fakeroot is not enabled, then we don't use it
1811 if [ "$(check_buildenv fakeroot)" != "y" -o $EUID -eq 0 ]; then
1812 if [ "$REPKG" -eq 0 ]; then
1813 devel_update
1814 run_build
1816 if [ "$SPLITPKG" -eq 0 ]; then
1817 if [ "$PKGFUNC" -eq 1 ]; then
1818 run_package
1819 tidy_install
1820 elif [ "$REPKG" -eq 0 ]; then
1821 tidy_install
1823 create_package
1824 else
1825 for pkg in ${pkgname[@]}; do
1826 pkgdir="$pkgdir/$pkg"
1827 mkdir -p "$pkgdir"
1828 backup_package_variables
1829 run_package $pkg
1830 tidy_install
1831 create_package $pkg
1832 restore_package_variables
1833 pkgdir="${pkgdir%/*}"
1834 done
1836 else
1837 if [ "$REPKG" -eq 0 -a \( "$PKGFUNC" -eq 1 -o "$SPLITPKG" -eq 1 \) ]; then
1838 devel_update
1839 run_build
1840 cd "$startdir"
1843 msg "$(gettext "Entering fakeroot environment...")"
1845 if [ -n "$newpkgver" ]; then
1846 fakeroot -- $0 --forcever $newpkgver -F $ARGLIST || exit $?
1847 else
1848 fakeroot -- $0 -F $ARGLIST || exit $?
1853 msg "$(gettext "Finished making: %s")" "$pkgbase $pkgver-$pkgrel $CARCH ($(date))"
1855 install_package
1857 exit 0 #E_OK
1859 # vim: set ts=2 sw=2 noet: