aa3c1281fdb172a217ed48bbcc756c766b758c25
[portage.git] / bin / ebuild.sh
1 #!/bin/bash
2 # Copyright 1999-2006 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Id$
5
6 PORTAGE_BIN_PATH="${PORTAGE_BIN_PATH:-/usr/lib/portage/bin}"
7 PORTAGE_PYM_PATH="${PORTAGE_PYM_PATH:-/usr/lib/portage/pym}"
8 declare -rx PORTAGE_BIN_PATH PORTAGE_PYM_PATH
9
10 SANDBOX_PREDICT="${SANDBOX_PREDICT}:/proc/self/maps:/dev/console:/dev/random"
11 export SANDBOX_PREDICT="${SANDBOX_PREDICT}:${PORTAGE_PYM_PATH}:${PORTAGE_DEPCACHEDIR}"
12 export SANDBOX_WRITE="${SANDBOX_WRITE}:/dev/shm:/dev/stdout:/dev/stderr:${PORTAGE_TMPDIR}"
13 export SANDBOX_READ="${SANDBOX_READ}:/dev/shm:/dev/stdin:${PORTAGE_TMPDIR}"
14
15 if [ ! -z "${PORTAGE_GPG_DIR}" ]; then
16         SANDBOX_PREDICT="${SANDBOX_PREDICT}:${PORTAGE_GPG_DIR}"
17 fi
18
19 declare -rx EBUILD_PHASE
20
21 # These two functions wrap sourcing and calling respectively.  At present they
22 # perform a qa check to make sure eclasses and ebuilds and profiles don't mess
23 # with shell opts (shopts).  Ebuilds/eclasses changing shopts should reset them 
24 # when they are done.  Note:  For now these shoudl always return success.
25
26 qa_source() {
27         local shopts=$(shopt) OLDIFS="$IFS"
28         source "$@" || return 1
29         [[ $shopts != $(shopt) ]] &&
30                 eqawarn "QA Notice: Global shell options changed and were not restored while sourcing '$*'"
31         [[ "$IFS" != "$OLDIFS" ]] &&
32                 eqawarn "QA Notice: Global IFS changed and was not restored while sourcing '$*'"
33         return 0
34 }
35
36 qa_call() {
37         local shopts=$(shopt) OLDIFS="$IFS"
38         "$@" || return 1
39         [[ $shopts != $(shopt) ]] &&
40                 eqawarn "QA Notice: Global shell options changed and were not restored while calling '$*'"
41         [[ "$IFS" != "$OLDIFS" ]] &&
42                 eqawarn "QA Notice: Global IFS changed and was not restored while calling '$*'"
43         return 0
44 }
45
46 if [ "$*" != "depend" ] && [ "$*" != "clean" ] && [ "$*" != "nofetch" ]; then
47         if [ -f "${T}/environment" ]; then
48                 qa_source "${T}/environment" &>/dev/null
49         fi
50 fi
51
52 # subshell die support
53 EBUILD_MASTER_PID=$$
54 trap 'exit 1' SIGTERM
55
56 EBUILD_SH_ARGS="$*"
57
58 shift $#
59
60 # Prevent aliases from causing portage to act inappropriately.
61 # Make sure it's before everything so we don't mess aliases that follow.
62 unalias -a
63
64 # Unset some variables that break things.
65 unset GZIP BZIP BZIP2 CDPATH GREP_OPTIONS GREP_COLOR GLOBIGNORE
66
67 # We need this next line for "die" and "assert". It expands
68 # It _must_ preceed all the calls to die and assert.
69 shopt -s expand_aliases
70 alias die='diefunc "$FUNCNAME" "$LINENO" "$?"'
71 alias assert='_pipestatus="${PIPESTATUS[*]}"; [[ "${_pipestatus// /}" -eq 0 ]] || diefunc "$FUNCNAME" "$LINENO" "$_pipestatus"'
72 alias save_IFS='[ "${IFS:-unset}" != "unset" ] && old_IFS="${IFS}"'
73 alias restore_IFS='if [ "${old_IFS:-unset}" != "unset" ]; then IFS="${old_IFS}"; unset old_IFS; else unset IFS; fi'
74
75 OCC="$CC"
76 OCXX="$CXX"
77
78 export PATH="/usr/local/sbin:/sbin:/usr/sbin:${PORTAGE_BIN_PATH}:/usr/local/bin:/bin:/usr/bin:${ROOTPATH}"
79 [ ! -z "$PREROOTPATH" ] && export PATH="${PREROOTPATH%%:}:$PATH"
80
81 source "${PORTAGE_BIN_PATH}/isolated-functions.sh"  &>/dev/null
82
83 [[ $PORTAGE_QUIET != "" ]] && export PORTAGE_QUIET
84
85 # the sandbox is disabled by default except when overridden in the relevant stages
86 export SANDBOX_ON="0"
87
88 # sandbox support functions; defined prior to profile.bashrc srcing, since the profile might need to add a default exception (/usr/lib64/conftest fex)
89 addread() {
90         [[ -z $1 || -n $2 ]] && die "Usage: addread <colon-delimited list of paths>"
91         export SANDBOX_READ="$SANDBOX_READ:$1"
92 }
93
94 addwrite() {
95         [[ -z $1 || -n $2 ]] && die "Usage: addwrite <colon-delimited list of paths>"
96         export SANDBOX_WRITE="$SANDBOX_WRITE:$1"
97 }
98
99 adddeny() {
100         [[ -z $1 || -n $2 ]] && die "Usage: adddeny <colon-delimited list of paths>"
101         export SANDBOX_DENY="$SANDBOX_DENY:$1"
102 }
103
104 addpredict() {
105         [[ -z $1 || -n $2 ]] && die "Usage: addpredict <colon-delimited list of paths>"
106         export SANDBOX_PREDICT="$SANDBOX_PREDICT:$1"
107 }
108
109 lchown() {
110         chown -h "$@"
111 }
112
113 lchgrp() {
114         chgrp -h "$@"
115 }
116
117 # source the existing profile.bashrc's.
118 save_IFS
119 IFS=$'\n'
120 for dir in ${PROFILE_PATHS}; do
121         # Must unset it so that it doesn't mess up assumptions in the RCs.
122         unset IFS
123         if [ -f "${dir}/profile.bashrc" ]; then
124                 qa_source "${dir}/profile.bashrc"
125         fi
126 done
127 restore_IFS
128
129 # We assume if people are changing shopts in their bashrc they do so at their
130 # own peril.  This is the ONLY non-portage bit of code that can change shopts
131 # without a QA violation.
132 if [ -f "${PORTAGE_BASHRC}" ]; then
133         # If $- contains x, then tracing has already enabled elsewhere for some
134         # reason.  We preserve it's state so as not to interfere.
135         if [ "$PORTAGE_DEBUG" != "1" ] || [ "${-/x/}" != "$-" ]; then
136                 source "${PORTAGE_BASHRC}"
137         else
138                 set -x
139                 source "${PORTAGE_BASHRC}"
140                 set +x
141         fi
142 fi
143
144 [ ! -z "$OCC" ] && export CC="$OCC"
145 [ ! -z "$OCXX" ] && export CXX="$OCXX"
146
147 esyslog() {
148         # Custom version of esyslog() to take care of the "Red Star" bug.
149         # MUST follow functions.sh to override the "" parameter problem.
150         return 0
151 }
152
153 use() {
154         useq ${1}
155 }
156
157 usev() {
158         if useq ${1}; then
159                 echo "${1}"
160                 return 0
161         fi
162         return 1
163 }
164
165 useq() {
166         local u=$1
167         local found=0
168
169         # if we got something like '!flag', then invert the return value
170         if [[ ${u:0:1} == "!" ]] ; then
171                 u=${u:1}
172                 found=1
173         fi
174
175         # Make sure we have this USE flag in IUSE
176         if ! hasq "${u}" ${IUSE} ${E_IUSE} && ! hasq "${u}" ${PORTAGE_ARCHLIST} selinux; then
177                 eqawarn "QA Notice: USE Flag '${u}' not in IUSE for ${CATEGORY}/${PF}"
178         fi
179
180         if hasq ${u} ${USE} ; then
181                 return ${found}
182         else
183                 return $((!found))
184         fi
185 }
186
187 has_version() {
188         if [ "${EBUILD_PHASE}" == "depend" ]; then
189                 die "portageq calls (has_version calls portageq) are not allowed in the global scope"
190         fi
191         # return shell-true/shell-false if exists.
192         # Takes single depend-type atoms.
193         if "${PORTAGE_BIN_PATH}/portageq" 'has_version' "${ROOT}" "$1"; then
194                 return 0
195         else
196                 return 1
197         fi
198 }
199
200 portageq() {
201         if [ "${EBUILD_PHASE}" == "depend" ]; then
202                 die "portageq calls are not allowed in the global scope"
203         fi
204         "${PORTAGE_BIN_PATH}/portageq" "$@"
205 }
206
207
208 # ----------------------------------------------------------------------------
209 # ----------------------------------------------------------------------------
210 # ----------------------------------------------------------------------------
211
212
213 best_version() {
214         if [ "${EBUILD_PHASE}" == "depend" ]; then
215                 die "portageq calls (best_version calls portageq) are not allowed in the global scope"
216         fi
217         # returns the best/most-current match.
218         # Takes single depend-type atoms.
219         "${PORTAGE_BIN_PATH}/portageq" 'best_version' "${ROOT}" "$1"
220 }
221
222 use_with() {
223         if [ -z "$1" ]; then
224                 echo "!!! use_with() called without a parameter." >&2
225                 echo "!!! use_with <USEFLAG> [<flagname> [value]]" >&2
226                 return 1
227         fi
228
229         local UW_SUFFIX=""
230         if [ ! -z "${3}" ]; then
231                 UW_SUFFIX="=${3}"
232         fi
233
234         local UWORD="$2"
235         if [ -z "${UWORD}" ]; then
236                 UWORD="$1"
237         fi
238
239         if useq $1; then
240                 echo "--with-${UWORD}${UW_SUFFIX}"
241         else
242                 echo "--without-${UWORD}"
243         fi
244         return 0
245 }
246
247 use_enable() {
248         if [ -z "$1" ]; then
249                 echo "!!! use_enable() called without a parameter." >&2
250                 echo "!!! use_enable <USEFLAG> [<flagname> [value]]" >&2
251                 return 1
252         fi
253
254         local UE_SUFFIX=""
255         if [ ! -z "${3}" ]; then
256                 UE_SUFFIX="=${3}"
257         fi
258
259         local UWORD="$2"
260         if [ -z "${UWORD}" ]; then
261                 UWORD="$1"
262         fi
263
264         if useq $1; then
265                 echo "--enable-${UWORD}${UE_SUFFIX}"
266         else
267                 echo "--disable-${UWORD}"
268         fi
269         return 0
270 }
271
272 register_die_hook() {
273         export EBUILD_DEATH_HOOKS="${EBUILD_DEATH_HOOKS} $*"
274 }
275
276 diefunc() {
277         local funcname="$1" lineno="$2" exitcode="$3"
278         shift 3
279         echo >&2
280         echo "!!! ERROR: $CATEGORY/$PF failed." >&2
281         dump_trace 2 1>&2
282         echo "  $(basename "${BASH_SOURCE[1]}"), line ${BASH_LINENO[0]}:   Called die" 1>&2
283         echo >&2
284         echo "!!! ${*:-(no error message)}" >&2
285         echo "!!! If you need support, post the topmost build error, and the call stack if relevant." >&2
286         echo "!!! A complete build log is located at '${PORTAGE_LOG_FILE}'." >&2
287         echo >&2
288         if [ -n "${EBUILD_OVERLAY_ECLASSES}" ] ; then
289                 echo "This ebuild used the following eclasses from overlays:" >&2
290                 echo >&2
291                 for x in ${EBUILD_OVERLAY_ECLASSES} ; do
292                         echo "  ${x}" >&2
293                 done
294                 echo >&2
295         fi
296
297         if [ "${EBUILD_PHASE/depend}" == "${EBUILD_PHASE}" ]; then
298                 local x
299                 for x in $EBUILD_DEATH_HOOKS; do
300                         ${x} "$@" >&2 1>&2
301                 done
302         fi
303
304         # subshell die support
305         kill -s SIGTERM ${EBUILD_MASTER_PID}
306         exit 1
307 }
308
309 shopt -s extdebug &> /dev/null
310
311 # usage- first arg is the number of funcs on the stack to ignore.
312 # defaults to 1 (ignoring dump_trace)
313 dump_trace() {
314         local funcname="" sourcefile="" lineno="" n e s="yes"
315
316         declare -i strip=1
317
318         if [[ -n $1 ]]; then
319                 strip=$(( $1 ))
320         fi
321         
322         echo "Call stack:"
323         for (( n = ${#FUNCNAME[@]} - 1, p = ${#BASH_ARGV[@]} ; n > $strip ; n-- )) ; do
324                 funcname=${FUNCNAME[${n} - 1]}
325                 sourcefile=$(basename ${BASH_SOURCE[${n}]})
326                 lineno=${BASH_LINENO[${n} - 1]}
327                 # Display function arguments
328                 args=
329                 if [[ -n "${BASH_ARGV[@]}" ]]; then
330                         for (( j = 1 ; j <= ${BASH_ARGC[${n} - 1]} ; ++j )); do
331                                 newarg=${BASH_ARGV[$(( p - j - 1 ))]}
332                                 args="${args:+${args} }'${newarg}'"
333                         done
334                         (( p -= ${BASH_ARGC[${n} - 1]} ))
335                 fi
336                 echo "  ${sourcefile}, line ${lineno}:   Called ${funcname}${args:+ ${args}}"
337         done
338 }
339
340
341
342 #if no perms are specified, dirs/files will have decent defaults
343 #(not secretive, but not stupid)
344 umask 022
345 export DESTTREE=/usr
346 export INSDESTTREE=""
347 export EXEDESTTREE=""
348 export DOCDESTTREE=""
349 export INSOPTIONS="-m0644"
350 export EXEOPTIONS="-m0755"
351 export LIBOPTIONS="-m0644"
352 export DIROPTIONS="-m0755"
353 export MOPREFIX=${PN}
354
355 check_KV() {
356         if [ -z "${KV}" ]; then
357                 eerror ""
358                 eerror "Could not determine your kernel version."
359                 eerror "Make sure that you have a /usr/src/linux symlink,"
360                 eerror "and that the indicated kernel has been configured."
361                 eerror "You can also simply run the following command"
362                 eerror "in the directory referenced by /usr/src/linux:"
363                 eerror " make include/linux/version.h"
364                 eerror ""
365                 die
366         fi
367 }
368
369 # adds ".keep" files so that dirs aren't auto-cleaned
370 keepdir() {
371         dodir "$@"
372         local x
373         if [ "$1" == "-R" ] || [ "$1" == "-r" ]; then
374                 shift
375                 find "$@" -type d -printf "${D}/%p/.keep_${CATEGORY}_${PN}-${SLOT}\n" | tr "\n" "\0" | $XARGS -0 -n100 touch || die "Failed to recursively create .keep files"
376         else
377                 for x in "$@"; do
378                         touch "${D}/${x}/.keep_${CATEGORY}_${PN}-${SLOT}" || die "Failed to create .keep in ${D}/${x}"
379                 done
380         fi
381 }
382
383 unpack() {
384         local x
385         local y
386         local myfail
387
388         [ -z "$*" ] && die "Nothing passed to the 'unpack' command"
389
390         for x in "$@"; do
391                 vecho ">>> Unpacking ${x} to ${PWD}"
392                 y=${x%.*}
393                 y=${y##*.}
394
395                 myfail="${x} does not exist"
396                 if [ "${x:0:2}" = "./" ] ; then
397                         srcdir=""
398                 else
399                         srcdir="${DISTDIR}/"
400                 fi
401                 [[ ${x} == ${DISTDIR}* ]] && \
402                         die "Arguments to unpack() should not begin with \${DISTDIR}."
403                 [ ! -s "${srcdir}${x}" ] && die "$myfail"
404
405                 myfail="failure unpacking ${x}"
406                 case "${x##*.}" in
407                         tar)
408                                 tar xof "${srcdir}${x}" || die "$myfail"
409                                 ;;
410                         tgz)
411                                 tar xozf "${srcdir}${x}" || die "$myfail"
412                                 ;;
413                         tbz|tbz2)
414                                 bzip2 -dc "${srcdir}${x}" | tar xof -
415                                 assert "$myfail"
416                                 ;;
417                         ZIP|zip|jar)
418                                 unzip -qo "${srcdir}${x}" || die "$myfail"
419                                 ;;
420                         gz|Z|z)
421                                 if [ "${y}" == "tar" ]; then
422                                         tar zoxf "${srcdir}${x}" || die "$myfail"
423                                 else
424                                         gzip -dc "${srcdir}${x}" > ${x%.*} || die "$myfail"
425                                 fi
426                                 ;;
427                         bz2|bz)
428                                 if [ "${y}" == "tar" ]; then
429                                         bzip2 -dc "${srcdir}${x}" | tar xof -
430                                         assert "$myfail"
431                                 else
432                                         bzip2 -dc "${srcdir}${x}" > ${x%.*} || die "$myfail"
433                                 fi
434                                 ;;
435                         7Z|7z)
436                                 local my_output
437                                 my_output="$(7z x -y "${srcdir}/${x}")"
438                                 if [ $? -ne 0 ]; then
439                                         echo "${my_output}" >&2
440                                         die "$myfail"
441                                 fi
442                                 ;;
443                         RAR|rar)
444                                 unrar x -idq -o+ "${srcdir}/${x}" || die "$myfail"
445                                 ;;
446                         LHa|LHA|lha|lzh)
447                                 lha xqf "${srcdir}/${x}" || die "$myfail"
448                                 ;;
449                         a|deb)
450                                 ar x "${srcdir}/${x}" || die "$myfail"
451                                 ;;
452                         *)
453                                 vecho "unpack ${x}: file format not recognized. Ignoring."
454                                 ;;
455                 esac
456         done
457         # Do not chmod '.' since it's probably ${WORKDIR} and PORTAGE_WORKDIR_MODE
458         # should be preserved.
459         find . -mindepth 1 -print0 | ${XARGS} -0 chmod -f a+rX,u+w,g-w,o-w
460 }
461
462 strip_duplicate_slashes() {
463         if [[ -n $1 ]] ; then
464                 local removed=$1
465                 while [[ ${removed} == *//* ]] ; do
466                         removed=${removed//\/\///}
467                 done
468                 echo ${removed}
469         fi
470 }
471
472 econf() {
473         local x
474         local LOCAL_EXTRA_ECONF="${EXTRA_ECONF}"
475
476         if [ -z "${ECONF_SOURCE}" ]; then
477                 ECONF_SOURCE="."
478         fi
479         if [ -x "${ECONF_SOURCE}/configure" ]; then
480                 if [ -e /usr/share/gnuconfig/ ]; then
481                         for x in $(find "${WORKDIR}" -type f '(' -name config.guess -o -name config.sub ')') ; do
482                                 vecho " * econf: updating ${x/${WORKDIR}\/} with /usr/share/gnuconfig/${x##*/}"
483                                 cp -f /usr/share/gnuconfig/${x##*/} ${x}
484                         done
485                 fi
486
487                 if [ ! -z "${CBUILD}" ]; then
488                         LOCAL_EXTRA_ECONF="--build=${CBUILD} ${LOCAL_EXTRA_ECONF}"
489                 fi
490
491                 if [ ! -z "${CTARGET}" ]; then
492                         LOCAL_EXTRA_ECONF="--target=${CTARGET} ${LOCAL_EXTRA_ECONF}"
493                 fi
494
495                 # if the profile defines a location to install libs to aside from default, pass it on.
496                 # if the ebuild passes in --libdir, they're responsible for the conf_libdir fun.
497                 LIBDIR_VAR="LIBDIR_${ABI}"
498                 if [ -n "${ABI}" -a -n "${!LIBDIR_VAR}" ]; then
499                         CONF_LIBDIR="${!LIBDIR_VAR}"
500                 fi
501                 unset LIBDIR_VAR
502                 if [ -n "${CONF_LIBDIR}" ] && [ "${*/--libdir}" == "$*" ]; then
503                         if [ "${*/--exec-prefix}" != "$*" ]; then
504                                 local args="$(echo $*)"
505                                 local -a pref=($(echo ${args/*--exec-prefix[= ]}))
506                                 CONF_PREFIX=${pref}
507                                 [ "${CONF_PREFIX:0:1}" != "/" ] && CONF_PREFIX="/${CONF_PREFIX}"
508                         elif [ "${*/--prefix}" != "$*" ]; then
509                                 local args="$(echo $*)"
510                                 local -a pref=($(echo ${args/*--prefix[= ]}))
511                                 CONF_PREFIX=${pref}
512                                 [ "${CONF_PREFIX:0:1}" != "/" ] && CONF_PREFIX="/${CONF_PREFIX}"
513                         else
514                                 CONF_PREFIX="/usr"
515                         fi
516                         export CONF_PREFIX
517                         [ "${CONF_LIBDIR:0:1}" != "/" ] && CONF_LIBDIR="/${CONF_LIBDIR}"
518
519                         CONF_LIBDIR_RESULT="$(strip_duplicate_slashes ${CONF_PREFIX}${CONF_LIBDIR})"
520
521                         LOCAL_EXTRA_ECONF="--libdir=${CONF_LIBDIR_RESULT} ${LOCAL_EXTRA_ECONF}"
522                 fi
523
524                 local TMP_CONFCACHE_DIR CONFCACHE_ARG
525                 if hasq confcache $FEATURES && ! hasq confcache $RESTRICT; then
526                         CONFCACHE="$(type -p confcache)"
527                         if [ -z "${CONFCACHE}" ]; then
528                                 ewarn "disabling confcache, binary cannot be found"
529                         else
530                                 CONFCACHE="${CONFCACHE/ /\ }"
531                                 TMP_CONFCACHE_DIR="${CONFCACHE:+${CONFCACHE_DIR:-${PORTAGE_TMPDIR}/confcache}}"
532                                 TMP_CONFCACHE_DIR="${TMP_CONFCACHE_DIR/ /\ }"
533                                 CONFCACHE_ARG="--confcache-dir"
534                                 local s
535                                 if [ -n "$CCACHE_DIR" ]; then
536                                         s="$CCACHE_DIR"
537                                 fi
538                                 if [ -n "$DISTCC_DIR" ]; then
539                                         s="${s:+${s}:}$DISTCC_DIR"
540                                 fi
541                                 if [ -n "$s" ]; then
542                                         CONFCACHE_ARG="--confcache-ignore $s $CONFCACHE_ARG"
543                                 fi
544                         fi
545                 else
546                         CONFCACHE=
547                 fi
548
549                 vecho ${CONFCACHE} ${CONFCACHE_ARG} ${TMP_CONFCACHE_DIR} "${ECONF_SOURCE}/configure" \
550                         --prefix=/usr \
551                         --host=${CHOST} \
552                         --mandir=/usr/share/man \
553                         --infodir=/usr/share/info \
554                         --datadir=/usr/share \
555                         --sysconfdir=/etc \
556                         --localstatedir=/var/lib \
557                         "$@" \
558                         ${LOCAL_EXTRA_ECONF}
559
560                 if ! ${CONFCACHE} ${CONFCACHE_ARG} ${TMP_CONFCACHE_DIR} "${ECONF_SOURCE}/configure" \
561                         --prefix=/usr \
562                         --host=${CHOST} \
563                         --mandir=/usr/share/man \
564                         --infodir=/usr/share/info \
565                         --datadir=/usr/share \
566                         --sysconfdir=/etc \
567                         --localstatedir=/var/lib \
568                         "$@"  \
569                         ${LOCAL_EXTRA_ECONF}; then
570
571                         if [ -s config.log ]; then
572                                 echo
573                                 echo "!!! Please attach the following file when filing a report to bugs.gentoo.org:"
574                                 echo "!!! ${PWD}/config.log"
575                         fi
576                         die "econf failed"
577                 fi
578         else
579                 die "no configure script found"
580         fi
581 }
582
583 einstall() {
584         # CONF_PREFIX is only set if they didn't pass in libdir above.
585         LIBDIR_VAR="LIBDIR_${ABI}"
586         if [ -n "${ABI}" -a -n "${!LIBDIR_VAR}" ]; then
587                 CONF_LIBDIR="${!LIBDIR_VAR}"
588         fi
589         unset LIBDIR_VAR
590         if [ -n "${CONF_LIBDIR}" ] && [ "${CONF_PREFIX:-unset}" != "unset" ]; then
591                 EI_DESTLIBDIR="${D}/${CONF_PREFIX}/${CONF_LIBDIR}"
592                 EI_DESTLIBDIR="$(strip_duplicate_slashes ${EI_DESTLIBDIR})"
593                 EXTRA_EINSTALL="libdir=${EI_DESTLIBDIR} ${EXTRA_EINSTALL}"
594                 unset EI_DESTLIBDIR
595         fi
596
597         if [ -f ./[mM]akefile -o -f ./GNUmakefile ] ; then
598                 if [ "${PORTAGE_DEBUG}" == "1" ]; then
599                         make -n prefix="${D}/usr" \
600                                 datadir="${D}/usr/share" \
601                                 infodir="${D}/usr/share/info" \
602                                 localstatedir="${D}/var/lib" \
603                                 mandir="${D}/usr/share/man" \
604                                 sysconfdir="${D}/etc" \
605                                 ${EXTRA_EINSTALL} \
606                                 "$@" install
607                 fi
608                 make prefix="${D}/usr" \
609                         datadir="${D}/usr/share" \
610                         infodir="${D}/usr/share/info" \
611                         localstatedir="${D}/var/lib" \
612                         mandir="${D}/usr/share/man" \
613                         sysconfdir="${D}/etc" \
614                         ${EXTRA_EINSTALL} \
615                         "$@" install || die "einstall failed"
616         else
617                 die "no Makefile found"
618         fi
619 }
620
621 pkg_setup() {
622         return
623 }
624
625 pkg_nofetch() {
626         [ -z "${SRC_URI}" ] && return
627
628         echo "!!! The following are listed in SRC_URI for ${PN}:"
629         local x
630         for x in $(echo ${SRC_URI}); do
631                 echo "!!!   ${x}"
632         done
633 }
634
635 src_unpack() {
636         [[ -n ${A} ]] && unpack ${A}
637 }
638
639 src_compile() {
640         if [ -x ./configure ]; then
641                 econf
642         fi
643         if [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then
644                 emake || die "emake failed"
645         fi
646 }
647
648 src_test() {
649         if emake -j1 check -n &> /dev/null; then
650                 vecho ">>> Test phase [check]: ${CATEGORY}/${PF}"
651                 if ! emake -j1 check; then
652                         hasq test $FEATURES && die "Make check failed. See above for details."
653                         hasq test $FEATURES || eerror "Make check failed. See above for details."
654                 fi
655         elif emake -j1 test -n &> /dev/null; then
656                 vecho ">>> Test phase [test]: ${CATEGORY}/${PF}"
657                 if ! emake -j1 test; then
658                         hasq test $FEATURES && die "Make test failed. See above for details."
659                         hasq test $FEATURES || eerror "Make test failed. See above for details."
660                 fi
661         else
662                 vecho ">>> Test phase [none]: ${CATEGORY}/${PF}"
663         fi
664 }
665
666 src_install() {
667         return
668 }
669
670 pkg_preinst() {
671         return
672 }
673
674 pkg_postinst() {
675         return
676 }
677
678 pkg_prerm() {
679         return
680 }
681
682 pkg_postrm() {
683         return
684 }
685
686 pkg_config() {
687         eerror "This ebuild does not have a config function."
688 }
689
690 # Used to generate the /lib/cpp and /usr/bin/cc wrappers
691 gen_wrapper() {
692         cat > "$1" <<-EOF
693         #!/bin/sh
694         exec $2 "\$@"
695         EOF
696         chmod 0755 "$1"
697 }
698
699 dyn_setup() {
700         [ "$(type -t pre_pkg_setup)" == "function" ] && qa_call pre_pkg_setup
701         qa_call pkg_setup
702         [ "$(type -t post_pkg_setup)" == "function" ] && qa_call post_pkg_setup
703 }
704
705 dyn_unpack() {
706         [ "$(type -t pre_src_unpack)" == "function" ] && qa_call pre_src_unpack
707         local newstuff="no"
708         if [ -e "${WORKDIR}" ]; then
709                 local x
710                 local checkme
711                 for x in ${AA}; do
712                         vecho ">>> Checking ${x}'s mtime..."
713                         if [ "${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/${x}" -nt "${WORKDIR}" ]; then
714                                 vecho ">>> ${x} has been updated; recreating WORKDIR..."
715                                 newstuff="yes"
716                                 break
717                         fi
718                 done
719                 if [ "${EBUILD}" -nt "${WORKDIR}" ] && ! hasq keepwork ${FEATURES} ; then
720                         vecho ">>> ${EBUILD} has been updated; recreating WORKDIR..."
721                         newstuff="yes"
722                 elif [ ! -f "${PORTAGE_BUILDDIR}/.unpacked" ]; then
723                         vecho ">>> Not marked as unpacked; recreating WORKDIR..."
724                         newstuff="yes"
725                 fi
726         fi
727         if [ "${newstuff}" == "yes" ]; then
728                 # We don't necessarily have privileges to do a full dyn_clean here.
729                 rm -rf "${WORKDIR}"
730                 if [ -d "${T}" ] && ! hasq keeptemp ${FEATURES} ; then
731                         rm -rf "${T}" && mkdir "${T}"
732                 else
733                         [ -e "${T}/environment" ] && \
734                                 mv "${T}/environment" "${T}/environment.keeptemp"
735                 fi
736         fi
737         if [ -e "${WORKDIR}" ]; then
738                 if [ "$newstuff" == "no" ]; then
739                         vecho ">>> WORKDIR is up-to-date, keeping..."
740                         [ "$(type -t post_src_unpack)" == "function" ] && qa_call post_src_unpack
741                         return 0
742                 fi
743         fi
744
745         if [ ! -d "${WORKDIR}" ]; then
746                 install -m${PORTAGE_WORKDIR_MODE:-0700} -d "${WORKDIR}" || die "Failed to create dir '${WORKDIR}'"
747         fi
748         cd "${WORKDIR}" || die "Directory change failed: \`cd '${WORKDIR}'\`"
749         vecho ">>> Unpacking source..."
750         qa_call src_unpack
751         touch "${PORTAGE_BUILDDIR}/.unpacked" || die "IO Failure -- Failed 'touch .unpacked' in ${PORTAGE_BUILDDIR}"
752         vecho ">>> Source unpacked."
753         cd "${PORTAGE_BUILDDIR}"
754
755         [ "$(type -t post_src_unpack)" == "function" ] && qa_call post_src_unpack
756 }
757
758 dyn_clean() {
759         if [ -z "${PORTAGE_BUILDDIR}" ]; then
760                 echo "Aborting clean phase because PORTAGE_BUILDDIR is unset!"
761                 return 1
762         fi
763
764         if type -p chflags &>/dev/null; then
765                 chflags -R noschg,nouchg,nosappnd,nouappnd "${PORTAGE_BUILDDIR}"
766                 chflags -R nosunlnk,nouunlnk "${PORTAGE_BUILDDIR}" 2>/dev/null
767         fi
768
769         rm -rf "${PORTAGE_BUILDDIR}/image" "${PORTAGE_BUILDDIR}/homedir"
770
771         if ! hasq keeptemp $FEATURES; then
772                 rm -rf "${T}"
773         else
774                 [ -e "${T}/environment" ] && mv "${T}/environment" "${T}/environment.keeptemp"
775         fi
776
777         if ! hasq keepwork $FEATURES; then
778                 rm -rf "${PORTAGE_BUILDDIR}/.logid"
779                 rm -rf "${PORTAGE_BUILDDIR}/.unpacked"
780                 rm -rf "${PORTAGE_BUILDDIR}/.compiled"
781                 rm -rf "${PORTAGE_BUILDDIR}/.tested"
782                 rm -rf "${PORTAGE_BUILDDIR}/.installed"
783                 rm -rf "${PORTAGE_BUILDDIR}/.packaged"
784                 rm -rf "${PORTAGE_BUILDDIR}/build-info"
785                 rm -rf "${WORKDIR}"
786         fi
787
788         if [ -f "${PORTAGE_BUILDDIR}/.unpacked" ]; then
789                 find "${PORTAGE_BUILDDIR}" -type d ! -regex "^${WORKDIR}" | sort -r | tr "\n" "\0" | $XARGS -0 rmdir &>/dev/null
790         fi
791
792         # do not bind this to doebuild defined DISTDIR; don't trust doebuild, and if mistakes are made it'll
793         # result in it wiping the users distfiles directory (bad).
794         rm -rf "${PORTAGE_BUILDDIR}/distdir"
795
796         if [ -z "$(find "${PORTAGE_BUILDDIR}" -mindepth 1 -maxdepth 1)" ]; then
797                 rmdir "${PORTAGE_BUILDDIR}"
798         fi
799
800         true
801 }
802
803 into() {
804         if [ "$1" == "/" ]; then
805                 export DESTTREE=""
806         else
807                 export DESTTREE=$1
808                 if [ ! -d "${D}${DESTTREE}" ]; then
809                         install -d "${D}${DESTTREE}"
810                 fi
811         fi
812 }
813
814 insinto() {
815         if [ "$1" == "/" ]; then
816                 export INSDESTTREE=""
817         else
818                 export INSDESTTREE=$1
819                 if [ ! -d "${D}${INSDESTTREE}" ]; then
820                         install -d "${D}${INSDESTTREE}"
821                 fi
822         fi
823 }
824
825 exeinto() {
826         if [ "$1" == "/" ]; then
827                 export EXEDESTTREE=""
828         else
829                 export EXEDESTTREE="$1"
830                 if [ ! -d "${D}${EXEDESTTREE}" ]; then
831                         install -d "${D}${EXEDESTTREE}"
832                 fi
833         fi
834 }
835
836 docinto() {
837         if [ "$1" == "/" ]; then
838                 export DOCDESTTREE=""
839         else
840                 export DOCDESTTREE="$1"
841                 if [ ! -d "${D}usr/share/doc/${PF}/${DOCDESTTREE}" ]; then
842                         install -d "${D}usr/share/doc/${PF}/${DOCDESTTREE}"
843                 fi
844         fi
845 }
846
847 insopts() {
848         export INSOPTIONS="$@"
849
850         # `install` should never be called with '-s' ...
851         hasq -s ${INSOPTIONS} && die "Never call insopts() with -s"
852 }
853
854 diropts() {
855         export DIROPTIONS="$@"
856 }
857
858 exeopts() {
859         export EXEOPTIONS="$@"
860
861         # `install` should never be called with '-s' ...
862         hasq -s ${EXEOPTIONS} && die "Never call exeopts() with -s"
863 }
864
865 libopts() {
866         export LIBOPTIONS="$@"
867
868         # `install` should never be called with '-s' ...
869         hasq -s ${LIBOPTIONS} && die "Never call libopts() with -s"
870 }
871
872 abort_handler() {
873         local msg
874         if [ "$2" != "fail" ]; then
875                 msg="${EBUILD}: ${1} aborted; exiting."
876         else
877                 msg="${EBUILD}: ${1} failed; exiting."
878         fi
879         echo
880         echo "$msg"
881         echo
882         eval ${3}
883         #unset signal handler
884         trap SIGINT SIGQUIT
885 }
886
887 abort_compile() {
888         abort_handler "src_compile" $1
889         rm -f "${PORTAGE_BUILDDIR}/.compiled"
890         exit 1
891 }
892
893 abort_test() {
894         abort_handler "dyn_test" $1
895         rm -f "${PORTAGE_BUILDDIR}/.tested"
896         exit 1
897 }
898
899 abort_install() {
900         abort_handler "src_install" $1
901         rm -rf "${PORTAGE_BUILDDIR}/image"
902         exit 1
903 }
904
905 dyn_compile() {
906         trap "abort_compile" SIGINT SIGQUIT
907
908         [ "$(type -t pre_src_compile)" == "function" ] && qa_call pre_src_compile
909
910         [ "${CFLAGS-unset}"      != "unset" ] && export CFLAGS
911         [ "${CXXFLAGS-unset}"    != "unset" ] && export CXXFLAGS
912         [ "${LIBCFLAGS-unset}"   != "unset" ] && export LIBCFLAGS
913         [ "${LIBCXXFLAGS-unset}" != "unset" ] && export LIBCXXFLAGS
914         [ "${LDFLAGS-unset}"     != "unset" ] && export LDFLAGS
915         [ "${ASFLAGS-unset}"     != "unset" ] && export ASFLAGS
916
917         [ "${CCACHE_DIR-unset}"  != "unset" ] && export CCACHE_DIR
918         [ "${CCACHE_SIZE-unset}" != "unset" ] && export CCACHE_SIZE
919
920         [ "${DISTCC_DIR-unset}"  == "unset" ] && export DISTCC_DIR="${PORTAGE_TMPDIR}/.distcc"
921         [ ! -z "${DISTCC_DIR}" ] && addwrite "${DISTCC_DIR}"
922
923         LIBDIR_VAR="LIBDIR_${ABI}"
924         if [ -z "${PKG_CONFIG_PATH}" -a -n "${ABI}" -a -n "${!LIBDIR_VAR}" ]; then
925                 export PKG_CONFIG_PATH="/usr/${!LIBDIR_VAR}/pkgconfig"
926         fi
927         unset LIBDIR_VAR
928
929         if hasq noauto $FEATURES && [ ! -f ${PORTAGE_BUILDDIR}/.unpacked ]; then
930                 echo
931                 echo "!!! We apparently haven't unpacked... This is probably not what you"
932                 echo "!!! want to be doing... You are using FEATURES=noauto so I'll assume"
933                 echo "!!! that you know what you are doing... You have 5 seconds to abort..."
934                 echo
935
936                 local x
937                 for x in 1 2 3 4 5 6 7 8; do
938                         echo -ne "\a"
939                         LC_ALL=C sleep 0.25
940                 done
941
942                 sleep 3
943         fi
944
945         local srcdir=${PORTAGE_BUILDDIR}
946         cd "${PORTAGE_BUILDDIR}"
947         if [ ! -e "build-info" ]; then
948                 mkdir build-info
949         fi
950         cp "${EBUILD}" "build-info/${PF}.ebuild"
951
952         if [[ ${PORTAGE_BUILDDIR}/.compiled -nt ${WORKDIR} ]] ; then
953                 vecho ">>> It appears that '${PF}' is already compiled; skipping."
954                 vecho ">>> Remove '${PORTAGE_BUILDDIR}/.compiled' to force compilation."
955                 trap SIGINT SIGQUIT
956                 [ "$(type -t post_src_compile)" == "function" ] && qa_call post_src_compile
957                 return
958         fi
959         if [ -d "${S}" ]; then
960                 srcdir=${S}
961                 cd "${S}"
962         fi
963         #our custom version of libtool uses $S and $D to fix
964         #invalid paths in .la files
965         export S D
966         #some packages use an alternative to $S to build in, cause
967         #our libtool to create problematic .la files
968         export PWORKDIR="$WORKDIR"
969         vecho ">>> Compiling source in ${srcdir} ..."
970         qa_call src_compile
971         vecho ">>> Source compiled."
972         #|| abort_compile "fail"
973         cd "${PORTAGE_BUILDDIR}"
974         touch .compiled
975         cd build-info
976
977         set -f
978         for f in ASFLAGS CATEGORY CBUILD CC CFLAGS CHOST CTARGET CXX \
979                 CXXFLAGS DEPEND EXTRA_ECONF EXTRA_EINSTALL EXTRA_MAKE \
980                 FEATURES INHERITED IUSE LDFLAGS LIBCFLAGS LIBCXXFLAGS \
981                 LICENSE PDEPEND PF PKGUSE PROVIDE RDEPEND RESTRICT SLOT \
982                 KEYWORDS HOMEPAGE SRC_URI DESCRIPTION; do
983                 [ -n "${!f}" ] && echo $(echo "${!f}" | tr '\n,\r,\t' ' , , ' | sed s/'  \+'/' '/g) > ${f}
984         done
985         echo "${USE}"           > USE
986         echo "${EAPI:-0}"       > EAPI
987         set +f
988         set                     >  environment
989         export -p | sed 's:declare -rx:declare -x:' >> environment
990         bzip2 -f9 environment
991
992         cp "${EBUILD}" "${PF}.ebuild"
993         if hasq nostrip ${FEATURES} ${RESTRICT} || hasq strip ${RESTRICT}
994         then
995                 touch DEBUGBUILD
996         fi
997
998         [ "$(type -t post_src_compile)" == "function" ] && qa_call post_src_compile
999
1000         trap SIGINT SIGQUIT
1001 }
1002
1003 dyn_test() {
1004         [ "${EBUILD_FORCE_TEST}" == "1" ] && rm -f "${PORTAGE_BUILDDIR}/.tested"
1005         [ "$(type -t pre_src_test)" == "function" ] && qa_call pre_src_test
1006         if [ "${PORTAGE_BUILDDIR}/.tested" -nt "${WORKDIR}" ]; then
1007                 vecho ">>> It appears that ${PN} has already been tested; skipping."
1008                 [ "$(type -t post_src_test)" == "function" ] && qa_call post_src_test
1009                 return
1010         fi
1011         trap "abort_test" SIGINT SIGQUIT
1012         if [ -d "${S}" ]; then
1013                 cd "${S}"
1014         fi
1015         if ! hasq test $FEATURES && [ "${EBUILD_FORCE_TEST}" != "1" ]; then
1016                 vecho ">>> Test phase [not enabled]: ${CATEGORY}/${PF}"
1017         elif ! hasq test ${USE} && [ "${EBUILD_FORCE_TEST}" != "1" ]; then
1018                 ewarn "Skipping make test/check since USE=test is masked."
1019                 vecho ">>> Test phase [explicitly disabled]: ${CATEGORY}/${PF}"
1020         elif hasq test $RESTRICT; then
1021                 ewarn "Skipping make test/check due to ebuild restriction."
1022                 vecho ">>> Test phase [explicitly disabled]: ${CATEGORY}/${PF}"
1023         else
1024                 addpredict /
1025                 qa_call src_test
1026                 SANDBOX_PREDICT="${SANDBOX_PREDICT%:/}"
1027         fi
1028
1029         cd "${PORTAGE_BUILDDIR}"
1030         touch .tested || die "Failed to 'touch .tested' in ${PORTAGE_BUILDDIR}"
1031         [ "$(type -t post_src_test)" == "function" ] && qa_call post_src_test
1032         trap SIGINT SIGQUIT
1033 }
1034
1035 dyn_install() {
1036         [ -z "$PORTAGE_BUILDDIR" ] && die "${FUNCNAME}: PORTAGE_BUILDDIR is unset"
1037         if hasq noauto $FEATURES ; then
1038                 rm -f "${PORTAGE_BUILDDIR}/.installed"
1039         elif [[ ${PORTAGE_BUILDDIR}/.installed -nt ${WORKDIR} ]] ; then
1040                 vecho ">>> It appears that '${PF}' is already installed; skipping."
1041                 vecho ">>> Remove '${PORTAGE_BUILDDIR}/.installed' to force install."
1042                 return 0
1043         fi
1044         trap "abort_install" SIGINT SIGQUIT
1045         [ "$(type -t pre_src_install)" == "function" ] && qa_call pre_src_install
1046         rm -rf "${PORTAGE_BUILDDIR}/image"
1047         mkdir "${PORTAGE_BUILDDIR}/image"
1048         if [ -d "${S}" ]; then
1049                 cd "${S}"
1050         fi
1051         vecho
1052         vecho ">>> Install ${PF} into ${D} category ${CATEGORY}"
1053         #our custom version of libtool uses $S and $D to fix
1054         #invalid paths in .la files
1055         export S D
1056         #some packages uses an alternative to $S to build in, cause
1057         #our libtool to create problematic .la files
1058         export PWORKDIR="$WORKDIR"
1059         qa_call src_install
1060         touch "${PORTAGE_BUILDDIR}/.installed"
1061         vecho ">>> Completed installing ${PF} into ${D}"
1062         vecho
1063         cd ${PORTAGE_BUILDDIR}
1064         [ "$(type -t post_src_install)" == "function" ] && qa_call post_src_install
1065         trap SIGINT SIGQUIT
1066 }
1067
1068 dyn_preinst() {
1069         if [ -z "$IMAGE" ]; then
1070                 eerror "${FUNCNAME}: IMAGE is unset"
1071                 return 1
1072         fi
1073
1074         [ "$(type -t pre_pkg_preinst)" == "function" ] && qa_call pre_pkg_preinst
1075
1076         declare -r D=${IMAGE}
1077         pkg_preinst
1078
1079         [ "$(type -t post_pkg_preinst)" == "function" ] && qa_call post_pkg_preinst
1080 }
1081
1082 dyn_help() {
1083         echo
1084         echo "Portage"
1085         echo "Copyright 1999-2006 Gentoo Foundation"
1086         echo
1087         echo "How to use the ebuild command:"
1088         echo
1089         echo "The first argument to ebuild should be an existing .ebuild file."
1090         echo
1091         echo "One or more of the following options can then be specified.  If more"
1092         echo "than one option is specified, each will be executed in order."
1093         echo
1094         echo "  help        : show this help screen"
1095         echo "  setup       : execute package specific setup actions"
1096         echo "  fetch       : download source archive(s) and patches"
1097         echo "  digest      : create a digest and a manifest file for the package"
1098         echo "  manifest    : create a manifest file for the package"
1099         echo "  unpack      : unpack/patch sources (auto-fetch if needed)"
1100         echo "  compile     : compile sources (auto-fetch/unpack if needed)"
1101         echo "  test        : test package (auto-fetch/unpack/compile if needed)"
1102         echo "  preinst     : execute pre-install instructions"
1103         echo "  postinst    : execute post-install instructions"
1104         echo "  install     : install the package to the temporary install directory"
1105         echo "  qmerge      : merge image into live filesystem, recording files in db"
1106         echo "  merge       : do fetch, unpack, compile, install and qmerge"
1107         echo "  prerm       : execute pre-removal instructions"
1108         echo "  postrm      : execute post-removal instructions"
1109         echo "  unmerge     : remove package from live filesystem"
1110         echo "  config      : execute package specific configuration actions"
1111         echo "  package     : create a tarball package in ${PKGDIR}/All"
1112         echo "  rpm         : build a RedHat RPM package"
1113         echo "  clean       : clean up all source and temporary files"
1114         echo
1115         echo "The following settings will be used for the ebuild process:"
1116         echo
1117         echo "  package     : ${PF}"
1118         echo "  slot        : ${SLOT}"
1119         echo "  category    : ${CATEGORY}"
1120         echo "  description : ${DESCRIPTION}"
1121         echo "  system      : ${CHOST}"
1122         echo "  c flags     : ${CFLAGS}"
1123         echo "  c++ flags   : ${CXXFLAGS}"
1124         echo "  make flags  : ${MAKEOPTS}"
1125         echo -n "  build mode  : "
1126         if hasq nostrip ${FEATURES} ${RESTRICT} || hasq strip ${RESTRICT} ;
1127         then
1128                 echo "debug (large)"
1129         else
1130                 echo "production (stripped)"
1131         fi
1132         echo "  merge to    : ${ROOT}"
1133         echo
1134         if [ -n "$USE" ]; then
1135                 echo "Additionally, support for the following optional features will be enabled:"
1136                 echo
1137                 echo "  ${USE}"
1138         fi
1139         echo
1140 }
1141
1142 # debug-print() gets called from many places with verbose status information useful
1143 # for tracking down problems. The output is in $T/eclass-debug.log.
1144 # You can set ECLASS_DEBUG_OUTPUT to redirect the output somewhere else as well.
1145 # The special "on" setting echoes the information, mixing it with the rest of the
1146 # emerge output.
1147 # You can override the setting by exporting a new one from the console, or you can
1148 # set a new default in make.*. Here the default is "" or unset.
1149
1150 # in the future might use e* from /etc/init.d/functions.sh if i feel like it
1151 debug-print() {
1152         # if $T isn't defined, we're in dep calculation mode and
1153         # shouldn't do anything
1154         [ ! -d "$T" ] && return 0
1155
1156         while [ "$1" ]; do
1157
1158                 # extra user-configurable targets
1159                 if [ "$ECLASS_DEBUG_OUTPUT" == "on" ]; then
1160                         echo "debug: $1"
1161                 elif [ -n "$ECLASS_DEBUG_OUTPUT" ]; then
1162                         echo "debug: $1" >> $ECLASS_DEBUG_OUTPUT
1163                 fi
1164
1165                 # default target
1166                 echo "$1" >> "${T}/eclass-debug.log"
1167                 # let the portage user own/write to this file
1168                 chmod g+w "${T}/eclass-debug.log" &>/dev/null
1169
1170                 shift
1171         done
1172 }
1173
1174 # The following 2 functions are debug-print() wrappers
1175
1176 debug-print-function() {
1177         str="$1: entering function"
1178         shift
1179         debug-print "$str, parameters: $*"
1180 }
1181
1182 debug-print-section() {
1183         debug-print "now in section $*"
1184 }
1185
1186 # Sources all eclasses in parameters
1187 declare -ix ECLASS_DEPTH=0
1188 inherit() {
1189         ECLASS_DEPTH=$(($ECLASS_DEPTH + 1))
1190         if [[ ${ECLASS_DEPTH} > 1 ]]; then
1191                 debug-print "*** Multiple Inheritence (Level: ${ECLASS_DEPTH})"
1192         fi
1193
1194         local location
1195         local olocation
1196         local PECLASS
1197
1198         local B_IUSE
1199         local B_DEPEND
1200         local B_RDEPEND
1201         local B_PDEPEND
1202         while [ "$1" ]; do
1203                 location="${ECLASSDIR}/${1}.eclass"
1204                 olocation=""
1205
1206                 # PECLASS is used to restore the ECLASS var after recursion.
1207                 PECLASS="$ECLASS"
1208                 export ECLASS="$1"
1209
1210                 if [ "${EBUILD_PHASE}" != "depend" ] && \
1211                         [[ ${EBUILD_PHASE} != *rm ]]; then
1212                         # This is disabled in the *rm phases because they frequently give
1213                         # false alarms due to INHERITED in /var/db/pkg being outdated
1214                         # in comparison the the eclasses from the portage tree.
1215                         if ! hasq $ECLASS $INHERITED; then
1216                                 eqawarn "QA Notice: ECLASS '$ECLASS' inherited illegally in $CATEGORY/$PF"
1217                         fi
1218                 fi
1219
1220                 # any future resolution code goes here
1221                 if [ -n "$PORTDIR_OVERLAY" ]; then
1222                         local overlay
1223                         for overlay in ${PORTDIR_OVERLAY}; do
1224                                 olocation="${overlay}/eclass/${1}.eclass"
1225                                 if [ -e "$olocation" ]; then
1226                                         location="${olocation}"
1227                                         debug-print "  eclass exists: ${location}"
1228                                 fi
1229                         done
1230                 fi
1231                 debug-print "inherit: $1 -> $location"
1232                 [ ! -e "$location" ] && die "${1}.eclass could not be found by inherit()"
1233
1234                 if [ "${location}" == "${olocation}" ] && \
1235                         ! hasq "${location}" ${EBUILD_OVERLAY_ECLASSES} ; then
1236                                 EBUILD_OVERLAY_ECLASSES="${EBUILD_OVERLAY_ECLASSES} ${location}"
1237                 fi
1238
1239                 #We need to back up the value of DEPEND and RDEPEND to B_DEPEND and B_RDEPEND
1240                 #(if set).. and then restore them after the inherit call.
1241
1242                 #turn off glob expansion
1243                 set -f
1244
1245                 # Retain the old data and restore it later.
1246                 unset B_IUSE B_DEPEND B_RDEPEND B_PDEPEND
1247                 [ "${IUSE-unset}"    != "unset" ] && B_IUSE="${IUSE}"
1248                 [ "${DEPEND-unset}"  != "unset" ] && B_DEPEND="${DEPEND}"
1249                 [ "${RDEPEND-unset}" != "unset" ] && B_RDEPEND="${RDEPEND}"
1250                 [ "${PDEPEND-unset}" != "unset" ] && B_PDEPEND="${PDEPEND}"
1251                 unset IUSE DEPEND RDEPEND PDEPEND
1252                 #turn on glob expansion
1253                 set +f
1254
1255                 qa_source "$location" || die "died sourcing $location in inherit()"
1256                 
1257                 #turn off glob expansion
1258                 set -f
1259
1260                 # If each var has a value, append it to the global variable E_* to
1261                 # be applied after everything is finished. New incremental behavior.
1262                 [ "${IUSE-unset}"    != "unset" ] && export E_IUSE="${E_IUSE} ${IUSE}"
1263                 [ "${DEPEND-unset}"  != "unset" ] && export E_DEPEND="${E_DEPEND} ${DEPEND}"
1264                 [ "${RDEPEND-unset}" != "unset" ] && export E_RDEPEND="${E_RDEPEND} ${RDEPEND}"
1265                 [ "${PDEPEND-unset}" != "unset" ] && export E_PDEPEND="${E_PDEPEND} ${PDEPEND}"
1266
1267                 [ "${B_IUSE-unset}"    != "unset" ] && IUSE="${B_IUSE}"
1268                 [ "${B_IUSE-unset}"    != "unset" ] || unset IUSE
1269
1270                 [ "${B_DEPEND-unset}"  != "unset" ] && DEPEND="${B_DEPEND}"
1271                 [ "${B_DEPEND-unset}"  != "unset" ] || unset DEPEND
1272
1273                 [ "${B_RDEPEND-unset}" != "unset" ] && RDEPEND="${B_RDEPEND}"
1274                 [ "${B_RDEPEND-unset}" != "unset" ] || unset RDEPEND
1275
1276                 [ "${B_PDEPEND-unset}" != "unset" ] && PDEPEND="${B_PDEPEND}"
1277                 [ "${B_PDEPEND-unset}" != "unset" ] || unset PDEPEND
1278
1279                 #turn on glob expansion
1280                 set +f
1281
1282                 hasq $1 $INHERITED || export INHERITED="$INHERITED $1"
1283
1284                 export ECLASS="$PECLASS"
1285
1286                 shift
1287         done
1288         ((--ECLASS_DEPTH)) # Returns 1 when ECLASS_DEPTH reaches 0.
1289         return 0
1290 }
1291
1292 # Exports stub functions that call the eclass's functions, thereby making them default.
1293 # For example, if ECLASS="base" and you call "EXPORT_FUNCTIONS src_unpack", the following
1294 # code will be eval'd:
1295 # src_unpack() { base_src_unpack; }
1296 EXPORT_FUNCTIONS() {
1297         if [ -z "$ECLASS" ]; then
1298                 echo "EXPORT_FUNCTIONS without a defined ECLASS" >&2
1299                 exit 1
1300         fi
1301         while [ "$1" ]; do
1302                 debug-print "EXPORT_FUNCTIONS: ${1} -> ${ECLASS}_${1}"
1303                 eval "$1() { ${ECLASS}_$1 "\$@" ; }" > /dev/null
1304                 shift
1305         done
1306 }
1307
1308 # adds all parameters to E_DEPEND and E_RDEPEND, which get added to DEPEND
1309 # and RDEPEND after the ebuild has been processed. This is important to
1310 # allow users to use DEPEND="foo" without frying dependencies added by an
1311 # earlier inherit. It also allows RDEPEND to work properly, since a lot
1312 # of ebuilds assume that an unset RDEPEND gets its value from DEPEND.
1313 # Without eclasses, this is true. But with them, the eclass may set
1314 # RDEPEND itself (or at least used to) which would prevent RDEPEND from
1315 # getting its value from DEPEND. This is a side-effect that made eclasses
1316 # have unreliable dependencies.
1317
1318 newdepend() {
1319         debug-print-function newdepend $*
1320         debug-print "newdepend: E_DEPEND=$E_DEPEND E_RDEPEND=$E_RDEPEND"
1321
1322         while [ -n "$1" ]; do
1323                 case $1 in
1324                 "/autotools")
1325                         do_newdepend DEPEND sys-devel/autoconf sys-devel/automake sys-devel/make
1326                         ;;
1327                 "/c")
1328                         do_newdepend DEPEND sys-devel/gcc virtual/libc
1329                         do_newdepend RDEPEND virtual/libc
1330                         ;;
1331                 *)
1332                         do_newdepend DEPEND $1
1333                         ;;
1334                 esac
1335                 shift
1336         done
1337 }
1338
1339 newrdepend() {
1340         debug-print-function newrdepend $*
1341         do_newdepend RDEPEND $1
1342 }
1343
1344 newpdepend() {
1345         debug-print-function newpdepend $*
1346         do_newdepend PDEPEND $1
1347 }
1348
1349 do_newdepend() {
1350         # This function does a generic change determining whether we're in an
1351         # eclass or not. If we are, we change the E_* variables for deps.
1352         debug-print-function do_newdepend $*
1353         [ -z "$1" ] && die "do_newdepend without arguments"
1354
1355         # Grab what we're affecting... Figure out if we're affecting eclasses.
1356         [[ ${ECLASS_DEPTH} > 0 ]] && TARGET="E_$1"
1357         [[ ${ECLASS_DEPTH} > 0 ]] || TARGET="$1"
1358         shift # $1 was a variable name.
1359
1360         while [ -n "$1" ]; do
1361                 # This bit of evil takes TARGET and uses it to evaluate down to a
1362                 # variable. This is a sneaky way to make this infinately expandable.
1363                 # The normal translation of this would look something like this:
1364                 # E_DEPEND="${E_DEPEND} $1"  ::::::  Cool, huh? :)
1365                 eval export ${TARGET}=\"\${${TARGET}} \$1\"
1366                 shift
1367         done
1368 }
1369
1370 # this is a function for removing any directory matching a passed in pattern from
1371 # PATH
1372 remove_path_entry() {
1373         save_IFS
1374         IFS=":"
1375         stripped_path="${PATH}"
1376         while [ -n "$1" ]; do
1377                 cur_path=""
1378                 for p in ${stripped_path}; do
1379                         if [ "${p/${1}}" == "${p}" ]; then
1380                                 cur_path="${cur_path}:${p}"
1381                         fi
1382                 done
1383                 stripped_path="${cur_path#:*}"
1384                 shift
1385         done
1386         restore_IFS
1387         PATH="${stripped_path}"
1388 }
1389
1390 # === === === === === === === === === === === === === === === === === ===
1391 # === === === === === functions end, main part begins === === === === ===
1392 # === === === === === functions end, main part begins === === === === ===
1393 # === === === === === functions end, main part begins === === === === ===
1394 # === === === === === === === === === === === === === === === === === ===
1395
1396 if [[ ${EBUILD_SH_ARGS} != "depend" ]] && [[ ${EBUILD_SH_ARGS}  != "clean" ]] && [[ ${EBUILD_SH_ARGS} != "setup" ]]; then
1397         cd ${PORTAGE_TMPDIR} &> /dev/null
1398         cd ${BUILD_PREFIX} &> /dev/null
1399
1400         if [ "$(id -nu)" == "portage" ] ; then
1401                 export USER=portage
1402         fi
1403
1404         if hasq distcc ${FEATURES} ; then
1405                 if [ -d /usr/lib/distcc/bin ]; then
1406                         #We can enable distributed compile support
1407                         if [ -z "${PATH/*distcc*/}" ]; then
1408                                 # Remove the other reference.
1409                                 remove_path_entry "distcc"
1410                         fi
1411                         export PATH="/usr/lib/distcc/bin:${PATH}"
1412                         [ ! -z "${DISTCC_LOG}" ] && addwrite "$(dirname ${DISTCC_LOG})"
1413                 elif which distcc &>/dev/null; then
1414                         if ! hasq distcc $CC; then
1415                                 export CC="distcc $CC"
1416                         fi
1417                         if ! hasq distcc $CXX; then
1418                                 export CXX="distcc $CXX"
1419                         fi
1420                 fi
1421         fi
1422
1423         if hasq ccache ${FEATURES} ; then
1424                 #We can enable compiler cache support
1425                 if [ -z "${PATH/*ccache*/}" ]; then
1426                         # Remove the other reference.
1427                         remove_path_entry "ccache"
1428                 fi
1429
1430                 if [ -d /usr/lib/ccache/bin ]; then
1431                         export PATH="/usr/lib/ccache/bin:${PATH}"
1432                 elif [ -d /usr/bin/ccache ]; then
1433                         export PATH="/usr/bin/ccache:${PATH}"
1434                 fi
1435
1436                 [ -z "${CCACHE_DIR}" ] && export CCACHE_DIR="/var/tmp/ccache"
1437
1438                 addread "${CCACHE_DIR}"
1439                 addwrite "${CCACHE_DIR}"
1440
1441                 [ -n "${CCACHE_SIZE}" ] && ccache -M ${CCACHE_SIZE} &> /dev/null
1442         else
1443                 # Force configure scripts that automatically detect ccache to respect
1444                 # FEATURES="-ccache"
1445                 export CCACHE_DISABLE=1
1446         fi
1447
1448         # XXX: Load up the helper functions.
1449 #       for X in /usr/lib/portage/bin/functions/*.sh; do
1450 #               source ${X} || die "Failed to source ${X}"
1451 #       done
1452
1453 else
1454
1455 killparent() {
1456         trap INT
1457         kill ${PORTAGE_MASTER_PID}
1458 }
1459 trap "killparent" INT
1460
1461 fi # "$*"!="depend" && "$*"!="clean" && "$*" != "setup"
1462
1463 export SANDBOX_ON="1"
1464 export S=${WORKDIR}/${P}
1465
1466 unset E_IUSE E_DEPEND E_RDEPEND E_PDEPEND
1467
1468 for x in T P PN PV PVR PR CATEGORY A EBUILD EMERGE_FROM O PPID FILESDIR PORTAGE_TMPDIR; do
1469         [[ ${!x-UNSET_VAR} != UNSET_VAR ]] && declare -r ${x}
1470 done
1471 # Need to be able to change D in dyn_preinst due to the IMAGE stuff
1472 [[ ${EBUILD_SH_ARGS} != "preinst" ]] && declare -r D
1473 unset x
1474
1475 # Turn of extended glob matching so that g++ doesn't get incorrectly matched.
1476 shopt -u extglob
1477
1478 QA_INTERCEPTORS="javac java-config python python-config perl grep egrep fgrep sed gcc g++ cc bash awk nawk gawk pkg-config"
1479 # level the QA interceptors if we're in depend
1480 if hasq "depend" "${EBUILD_SH_ARGS}"; then
1481         for BIN in ${QA_INTERCEPTORS}; do
1482                 BIN_PATH=$(type -pf ${BIN})
1483                 if [ "$?" != "0" ]; then
1484                         BODY="echo \"*** missing command: ${BIN}\" >&2; return 127"
1485                 else
1486                         BODY="${BIN_PATH} \"\$@\"; return \$?"
1487                 fi
1488                 FUNC_SRC="${BIN}() {
1489                 if [ \$ECLASS_DEPTH -gt 0 ]; then
1490                         eqawarn \"QA Notice: '${BIN}' called in global scope: eclass \${ECLASS}\"
1491                 else
1492                         eqawarn \"QA Notice: '${BIN}' called in global scope: \${CATEGORY}/\${PF}\"
1493                 fi
1494                 ${BODY}
1495                 }";
1496                 eval "$FUNC_SRC" || echo "error creating QA interceptor ${BIN}" >&2
1497         done
1498         unset BIN_PATH BIN BODY FUNC_SRC
1499 fi
1500
1501 # reset the EBUILD_DEATH_HOOKS so they don't multiple due to stable's re-sourcing of env.
1502 # this can be left out of ebd variants, since they're unaffected.
1503 unset EBUILD_DEATH_HOOKS
1504
1505 # *DEPEND and IUSE will be set during the sourcing of the ebuild.  In order to
1506 # ensure correct interaction between ebuilds and eclasses, they need to be
1507 # unset before this process of interaction begins.
1508 unset DEPEND RDEPEND PDEPEND IUSE
1509
1510 source ${EBUILD} || die "error sourcing ebuild"
1511 if ! hasq depend $EBUILD_PHASE; then
1512         RESTRICT="${PORTAGE_RESTRICT}"
1513         unset PORTAGE_RESTRICT
1514 fi
1515
1516 # Expand KEYWORDS
1517 # We need to turn off pathname expansion for -* in KEYWORDS and
1518 # we need to escape ~ to avoid tilde expansion
1519 set -f
1520 KEYWORDS=$(eval echo ${KEYWORDS//~/\\~})
1521 set +f
1522
1523 if hasq nostrip ${FEATURES} ${RESTRICT} || hasq strip ${RESTRICT}
1524 then
1525         export DEBUGBUILD=1
1526 fi
1527
1528 #a reasonable default for $S
1529 if [ "$S" = "" ]; then
1530         export S=${WORKDIR}/${P}
1531 fi
1532
1533 #wipe the interceptors.  we don't want saved.
1534 if hasq "depend" "${EBUILD_SH_ARGS}"; then
1535         unset -f $QA_INTERCEPTORS
1536         unset QA_INTERCEPTORS
1537 fi
1538
1539 #some users have $TMP/$TMPDIR to a custom dir in their home ...
1540 #this will cause sandbox errors with some ./configure
1541 #scripts, so set it to $T.
1542 export TMP="${T}"
1543 export TMPDIR="${T}"
1544
1545 # Note: this next line is not the same as export RDEPEND=${RDEPEND:-${DEPEND}}
1546 # That will test for unset *or* NULL ("").  We want just to set for unset...
1547
1548 #turn off glob expansion from here on in to prevent *'s and ? in the DEPEND
1549 #syntax from getting expanded :)
1550 #check eclass rdepends also.
1551 set -f
1552 if [ "${RDEPEND-unset}" == "unset" ] ; then
1553         export RDEPEND=${DEPEND}
1554         debug-print "RDEPEND: not set... Setting to: ${DEPEND}"
1555 fi
1556
1557 #add in dependency info from eclasses
1558 IUSE="$IUSE $E_IUSE"
1559 DEPEND="$DEPEND $E_DEPEND"
1560 RDEPEND="$RDEPEND $E_RDEPEND"
1561 PDEPEND="$PDEPEND $E_PDEPEND"
1562
1563 unset E_IUSE E_DEPEND E_RDEPEND E_PDEPEND
1564
1565 if [ "${EBUILD_PHASE}" != "depend" ]; then
1566         # Make IUSE defaults backward compatible with all the old shell code.
1567         iuse_temp=""
1568         for x in ${IUSE} ; do
1569                 if [[ ${x} == +* ]]; then
1570                         iuse_temp="${iuse_temp} ${x:1}"
1571                 else
1572                         iuse_temp="${iuse_temp} ${x}"
1573                 fi
1574         done
1575         export IUSE=${iuse_temp}
1576         unset iuse_temp
1577         # Lock the dbkey variables after the global phase
1578         declare -r DEPEND RDEPEND SLOT SRC_URI RESTRICT HOMEPAGE LICENSE DESCRIPTION
1579         declare -r KEYWORDS INHERITED IUSE PDEPEND PROVIDE
1580 fi
1581
1582 set +f
1583
1584 for myarg in ${EBUILD_SH_ARGS} ; do
1585         case $myarg in
1586         nofetch)
1587                 qa_call pkg_nofetch
1588                 exit 1
1589                 ;;
1590         prerm|postrm|postinst|config)
1591                 export SANDBOX_ON="0"
1592                 if [ "$PORTAGE_DEBUG" != "1" ]; then
1593                         [ "$(type -t pre_pkg_${myarg})" == "function" ] && qa_call pre_pkg_${myarg}
1594                         qa_call pkg_${myarg}
1595                         [ "$(type -t post_pkg_${myarg})" == "function" ] && qa_call post_pkg_${myarg}
1596                         #Allow non-zero return codes since they can be caused by &&
1597                 else
1598                         set -x
1599                         [ "$(type -t pre_pkg_${myarg})" == "function" ] && qa_call pre_pkg_${myarg}
1600                         qa_call pkg_${myarg}
1601                         [ "$(type -t post_pkg_${myarg})" == "function" ] && qa_call post_pkg_${myarg}
1602                         #Allow non-zero return codes since they can be caused by &&
1603                         set +x
1604                 fi
1605                 ;;
1606         unpack|compile|test|clean|install)
1607                 if [ "${SANDBOX_DISABLED="0"}" == "0" ]; then
1608                         export SANDBOX_ON="1"
1609                 else
1610                         export SANDBOX_ON="0"
1611                 fi
1612                 if [ "$PORTAGE_DEBUG" != "1" ]; then
1613                         dyn_${myarg}
1614                         #Allow non-zero return codes since they can be caused by &&
1615                 else
1616                         set -x
1617                         dyn_${myarg}
1618                         #Allow non-zero return codes since they can be caused by &&
1619                         set +x
1620                 fi
1621                 export SANDBOX_ON="0"
1622                 ;;
1623         help|setup|preinst)
1624                 #pkg_setup needs to be out of the sandbox for tmp file creation;
1625                 #for example, awking and piping a file in /tmp requires a temp file to be created
1626                 #in /etc.  If pkg_setup is in the sandbox, both our lilo and apache ebuilds break.
1627                 export SANDBOX_ON="0"
1628                 if [ "$PORTAGE_DEBUG" != "1" ]; then
1629                         dyn_${myarg}
1630                 else
1631                         set -x
1632                         dyn_${myarg}
1633                         set +x
1634                 fi
1635                 ;;
1636         depend)
1637                 export SANDBOX_ON="0"
1638                 set -f
1639
1640                 if [ -n "${dbkey}" ] ; then
1641                         if [ ! -d "${dbkey%/*}" ]; then
1642                                 install -d -g ${PORTAGE_GID} -m2775 "${dbkey%/*}"
1643                         fi
1644                         # Make it group writable. 666&~002==664
1645                         umask 002
1646                 fi
1647
1648                 auxdbkeys="DEPEND RDEPEND SLOT SRC_URI RESTRICT HOMEPAGE LICENSE
1649                         DESCRIPTION KEYWORDS INHERITED IUSE CDEPEND PDEPEND PROVIDE EAPI
1650                         UNUSED_01 UNUSED_02 UNUSED_03 UNUSED_04 UNUSED_05 UNUSED_06
1651                         UNUSED_07"
1652
1653                 #the extra $(echo) commands remove newlines
1654                 unset CDEPEND
1655                 [ -n "${EAPI}" ] || EAPI=0
1656                 if [ -n "${dbkey}" ] ; then
1657                         > "${dbkey}"
1658                         for f in ${auxdbkeys} ; do
1659                                 echo $(echo ${!f}) >> "${dbkey}" || exit $?
1660                         done
1661                 else
1662                         for f in ${auxdbkeys} ; do
1663                                 echo $(echo ${!f}) 1>&9 || exit $?
1664                         done
1665                         9>&-
1666                 fi
1667                 set +f
1668                 #make sure it is writable by our group:
1669                 exit 0
1670                 ;;
1671         *)
1672                 export SANDBOX_ON="1"
1673                 echo "Please specify a valid command."
1674                 echo
1675                 dyn_help
1676                 exit 1
1677                 ;;
1678         esac
1679
1680         #if [ $? -ne 0 ]; then
1681         #       exit 1
1682         #fi
1683 done
1684
1685 # Save the env only for relevant phases.
1686 if [ -n "${myarg}" ] && \
1687         [ "${myarg}" != "clean" ] && \
1688         [ "${myarg}" != "help" ] ; then
1689         # Do not save myarg in the env, or else the above [ -n "$myarg" ] test will
1690         # give a false positive when ebuild.sh is sourced.
1691         unset myarg
1692         # Save current environment and touch a success file. (echo for success)
1693         umask 002
1694         set | egrep -v "^SANDBOX_" > "${T}/environment" 2>/dev/null
1695         chown portage:portage "${T}/environment" &>/dev/null
1696         chmod g+w "${T}/environment" &>/dev/null
1697 fi
1698
1699 # Do not exit when ebuild.sh is sourced by other scripts.
1700 true