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