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