Invert has/hasq and use/useq implementations.
[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         use ${1}
132 }
133
134 usev() {
135         if use ${1}; then
136                 echo "${1#!}"
137                 return 0
138         fi
139         return 1
140 }
141
142 use() {
143         local u=$1
144         local found=0
145
146         # if we got something like '!flag', then invert the return value
147         if [[ ${u:0:1} == "!" ]] ; then
148                 u=${u:1}
149                 found=1
150         fi
151
152         if [[ $EBUILD_PHASE = depend ]] ; then
153                 # TODO: Add a registration interface for eclasses to register
154                 # any number of phase hooks, so that global scope eclass
155                 # initialization can by migrated to phase hooks in new EAPIs.
156                 # Example: add_phase_hook before pkg_setup $ECLASS_pre_pkg_setup
157                 #if [[ -n $EAPI ]] && ! hasq "$EAPI" 0 1 2 3 ; then
158                 #       die "use() called during invalid phase: $EBUILD_PHASE"
159                 #fi
160                 true
161
162         # Make sure we have this USE flag in IUSE
163         elif [[ -n $PORTAGE_IUSE && -n $EBUILD_PHASE ]] ; then
164                 [[ $u =~ $PORTAGE_IUSE ]] || \
165                         eqawarn "QA Notice: USE Flag '${u}' not" \
166                                 "in IUSE for ${CATEGORY}/${PF}"
167         fi
168
169         if hasq ${u} ${USE} ; then
170                 return ${found}
171         else
172                 return $((!found))
173         fi
174 }
175
176 # Return true if given package is installed. Otherwise return false.
177 # Takes single depend-type atoms.
178 has_version() {
179         if [ "${EBUILD_PHASE}" == "depend" ]; then
180                 die "portageq calls (has_version calls portageq) are not allowed in the global scope"
181         fi
182
183         if [[ -n $PORTAGE_IPC_DAEMON ]] ; then
184                 "$PORTAGE_BIN_PATH"/ebuild-ipc has_version "$ROOT" "$1"
185         else
186                 PYTHONPATH=${PORTAGE_PYM_PATH}${PYTHONPATH:+:}${PYTHONPATH} \
187                 "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}/portageq" has_version "${ROOT}" "$1"
188         fi
189         local retval=$?
190         case "${retval}" in
191                 0|1)
192                         return ${retval}
193                         ;;
194                 *)
195                         die "unexpected portageq exit code: ${retval}"
196                         ;;
197         esac
198 }
199
200 portageq() {
201         if [ "${EBUILD_PHASE}" == "depend" ]; then
202                 die "portageq calls are not allowed in the global scope"
203         fi
204
205         PYTHONPATH=${PORTAGE_PYM_PATH}${PYTHONPATH:+:}${PYTHONPATH} \
206         "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}/portageq" "$@"
207 }
208
209
210 # ----------------------------------------------------------------------------
211 # ----------------------------------------------------------------------------
212 # ----------------------------------------------------------------------------
213
214
215 # Returns the best/most-current match.
216 # Takes single depend-type atoms.
217 best_version() {
218         if [ "${EBUILD_PHASE}" == "depend" ]; then
219                 die "portageq calls (best_version calls portageq) are not allowed in the global scope"
220         fi
221
222         if [[ -n $PORTAGE_IPC_DAEMON ]] ; then
223                 "$PORTAGE_BIN_PATH"/ebuild-ipc best_version "$ROOT" "$1"
224         else
225                 PYTHONPATH=${PORTAGE_PYM_PATH}${PYTHONPATH:+:}${PYTHONPATH} \
226                 "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}/portageq" 'best_version' "${ROOT}" "$1"
227         fi
228         local retval=$?
229         case "${retval}" in
230                 0|1)
231                         return ${retval}
232                         ;;
233                 *)
234                         die "unexpected portageq exit code: ${retval}"
235                         ;;
236         esac
237 }
238
239 use_with() {
240         if [ -z "$1" ]; then
241                 echo "!!! use_with() called without a parameter." >&2
242                 echo "!!! use_with <USEFLAG> [<flagname> [value]]" >&2
243                 return 1
244         fi
245
246         if ! has "${EAPI:-0}" 0 1 2 3 ; then
247                 local UW_SUFFIX=${3+=$3}
248         else
249                 local UW_SUFFIX=${3:+=$3}
250         fi
251         local UWORD=${2:-$1}
252
253         if useq $1; then
254                 echo "--with-${UWORD}${UW_SUFFIX}"
255         else
256                 echo "--without-${UWORD}"
257         fi
258         return 0
259 }
260
261 use_enable() {
262         if [ -z "$1" ]; then
263                 echo "!!! use_enable() called without a parameter." >&2
264                 echo "!!! use_enable <USEFLAG> [<flagname> [value]]" >&2
265                 return 1
266         fi
267
268         if ! has "${EAPI:-0}" 0 1 2 3 ; then
269                 local UE_SUFFIX=${3+=$3}
270         else
271                 local UE_SUFFIX=${3:+=$3}
272         fi
273         local UWORD=${2:-$1}
274
275         if useq $1; then
276                 echo "--enable-${UWORD}${UE_SUFFIX}"
277         else
278                 echo "--disable-${UWORD}"
279         fi
280         return 0
281 }
282
283 register_die_hook() {
284         local x
285         for x in $* ; do
286                 hasq $x $EBUILD_DEATH_HOOKS || \
287                         export EBUILD_DEATH_HOOKS="$EBUILD_DEATH_HOOKS $x"
288         done
289 }
290
291 register_success_hook() {
292         local x
293         for x in $* ; do
294                 hasq $x $EBUILD_SUCCESS_HOOKS || \
295                         export EBUILD_SUCCESS_HOOKS="$EBUILD_SUCCESS_HOOKS $x"
296         done
297 }
298
299 # Ensure that $PWD is sane whenever possible, to protect against
300 # exploitation of insecure search path for python -c in ebuilds.
301 # See bug #239560.
302 if ! hasq "$EBUILD_PHASE" clean cleanrm depend help ; then
303         cd "$PORTAGE_BUILDDIR" || \
304                 die "PORTAGE_BUILDDIR does not exist: '$PORTAGE_BUILDDIR'"
305 fi
306
307 #if no perms are specified, dirs/files will have decent defaults
308 #(not secretive, but not stupid)
309 umask 022
310 export DESTTREE=/usr
311 export INSDESTTREE=""
312 export _E_EXEDESTTREE_=""
313 export _E_DOCDESTTREE_=""
314 export INSOPTIONS="-m0644"
315 export EXEOPTIONS="-m0755"
316 export LIBOPTIONS="-m0644"
317 export DIROPTIONS="-m0755"
318 export MOPREFIX=${PN}
319 declare -a PORTAGE_DOCOMPRESS=( /usr/share/{doc,info,man} )
320 declare -a PORTAGE_DOCOMPRESS_SKIP=( /usr/share/doc/${PF}/html )
321
322 # adds ".keep" files so that dirs aren't auto-cleaned
323 keepdir() {
324         dodir "$@"
325         local x
326         if [ "$1" == "-R" ] || [ "$1" == "-r" ]; then
327                 shift
328                 find "$@" -type d -printf "${D}%p/.keep_${CATEGORY}_${PN}-${SLOT}\n" \
329                         | tr "\n" "\0" | \
330                         while read -r -d $'\0' ; do
331                                 >> "$REPLY" || \
332                                         die "Failed to recursively create .keep files"
333                         done
334         else
335                 for x in "$@"; do
336                         >> "${D}${x}/.keep_${CATEGORY}_${PN}-${SLOT}" || \
337                                 die "Failed to create .keep in ${D}${x}"
338                 done
339         fi
340 }
341
342 unpack() {
343         local srcdir
344         local x
345         local y
346         local myfail
347         local eapi=${EAPI:-0}
348         [ -z "$*" ] && die "Nothing passed to the 'unpack' command"
349
350         for x in "$@"; do
351                 vecho ">>> Unpacking ${x} to ${PWD}"
352                 y=${x%.*}
353                 y=${y##*.}
354
355                 if [[ ${x} == "./"* ]] ; then
356                         srcdir=""
357                 elif [[ ${x} == ${DISTDIR%/}/* ]] ; then
358                         die "Arguments to unpack() cannot begin with \${DISTDIR}."
359                 elif [[ ${x} == "/"* ]] ; then
360                         die "Arguments to unpack() cannot be absolute"
361                 else
362                         srcdir="${DISTDIR}/"
363                 fi
364                 [[ ! -s ${srcdir}${x} ]] && die "${x} does not exist"
365
366                 _unpack_tar() {
367                         if [ "${y}" == "tar" ]; then
368                                 $1 -c -- "$srcdir$x" | tar xof -
369                                 assert_sigpipe_ok "$myfail"
370                         else
371                                 $1 -c -- "${srcdir}${x}" > ${x%.*} || die "$myfail"
372                         fi
373                 }
374
375                 myfail="failure unpacking ${x}"
376                 case "${x##*.}" in
377                         tar)
378                                 tar xof "$srcdir$x" || die "$myfail"
379                                 ;;
380                         tgz)
381                                 tar xozf "$srcdir$x" || die "$myfail"
382                                 ;;
383                         tbz|tbz2)
384                                 ${PORTAGE_BUNZIP2_COMMAND:-${PORTAGE_BZIP2_COMMAND} -d} -c -- "$srcdir$x" | tar xof -
385                                 assert_sigpipe_ok "$myfail"
386                                 ;;
387                         ZIP|zip|jar)
388                                 # unzip will interactively prompt under some error conditions,
389                                 # as reported in bug #336285
390                                 ( while true ; do echo n || break ; done ) | \
391                                 unzip -qo "${srcdir}${x}" || die "$myfail"
392                                 ;;
393                         gz|Z|z)
394                                 _unpack_tar "gzip -d"
395                                 ;;
396                         bz2|bz)
397                                 _unpack_tar "${PORTAGE_BUNZIP2_COMMAND:-${PORTAGE_BZIP2_COMMAND} -d}"
398                                 ;;
399                         7Z|7z)
400                                 local my_output
401                                 my_output="$(7z x -y "${srcdir}${x}")"
402                                 if [ $? -ne 0 ]; then
403                                         echo "${my_output}" >&2
404                                         die "$myfail"
405                                 fi
406                                 ;;
407                         RAR|rar)
408                                 unrar x -idq -o+ "${srcdir}${x}" || die "$myfail"
409                                 ;;
410                         LHa|LHA|lha|lzh)
411                                 lha xfq "${srcdir}${x}" || die "$myfail"
412                                 ;;
413                         a)
414                                 ar x "${srcdir}${x}" || die "$myfail"
415                                 ;;
416                         deb)
417                                 # Unpacking .deb archives can not always be done with
418                                 # `ar`.  For instance on AIX this doesn't work out.  If
419                                 # we have `deb2targz` installed, prefer it over `ar` for
420                                 # that reason.  We just make sure on AIX `deb2targz` is
421                                 # installed.
422                                 if type -P deb2targz > /dev/null; then
423                                         y=${x##*/}
424                                         local created_symlink=0
425                                         if [ ! "$srcdir$x" -ef "$y" ] ; then
426                                                 # deb2targz always extracts into the same directory as
427                                                 # the source file, so create a symlink in the current
428                                                 # working directory if necessary.
429                                                 ln -sf "$srcdir$x" "$y" || die "$myfail"
430                                                 created_symlink=1
431                                         fi
432                                         deb2targz "$y" || die "$myfail"
433                                         if [ $created_symlink = 1 ] ; then
434                                                 # Clean up the symlink so the ebuild
435                                                 # doesn't inadvertently install it.
436                                                 rm -f "$y"
437                                         fi
438                                         mv -f "${y%.deb}".tar.gz data.tar.gz || die "$myfail"
439                                 else
440                                         ar x "$srcdir$x" || die "$myfail"
441                                 fi
442                                 ;;
443                         lzma)
444                                 _unpack_tar "lzma -d"
445                                 ;;
446                         xz)
447                                 if hasq $eapi 0 1 2 ; then
448                                         vecho "unpack ${x}: file format not recognized. Ignoring."
449                                 else
450                                         _unpack_tar "xz -d"
451                                 fi
452                                 ;;
453                         *)
454                                 vecho "unpack ${x}: file format not recognized. Ignoring."
455                                 ;;
456                 esac
457         done
458         # Do not chmod '.' since it's probably ${WORKDIR} and PORTAGE_WORKDIR_MODE
459         # should be preserved.
460         find . -mindepth 1 -maxdepth 1 ! -type l -print0 | \
461                 ${XARGS} -0 chmod -fR a+rX,u+w,g-w,o-w
462 }
463
464 strip_duplicate_slashes() {
465         if [[ -n $1 ]] ; then
466                 local removed=$1
467                 while [[ ${removed} == *//* ]] ; do
468                         removed=${removed//\/\///}
469                 done
470                 echo ${removed}
471         fi
472 }
473
474 hasg() {
475     local x s=$1
476     shift
477     for x ; do [[ ${x} == ${s} ]] && echo "${x}" && return 0 ; done
478     return 1
479 }
480 hasgq() { hasg "$@" >/dev/null ; }
481 econf() {
482         local x
483
484         local phase_func=$(_ebuild_arg_to_phase "$EAPI" "$EBUILD_PHASE")
485         if [[ -n $phase_func ]] ; then
486                 if hasq "$EAPI" 0 1 ; then
487                         [[ $phase_func != src_compile ]] && \
488                                 eqawarn "QA Notice: econf called in" \
489                                         "$phase_func instead of src_compile"
490                 else
491                         [[ $phase_func != src_configure ]] && \
492                                 eqawarn "QA Notice: econf called in" \
493                                         "$phase_func instead of src_configure"
494                 fi
495         fi
496
497         : ${ECONF_SOURCE:=.}
498         if [ -x "${ECONF_SOURCE}/configure" ]; then
499                 if [[ -n $CONFIG_SHELL && \
500                         "$(head -n1 "$ECONF_SOURCE/configure")" =~ ^'#!'[[:space:]]*/bin/sh([[:space:]]|$) ]] ; then
501                         sed -e "1s:^#![[:space:]]*/bin/sh:#!$CONFIG_SHELL:" -i "$ECONF_SOURCE/configure" || \
502                                 die "Substition of shebang in '$ECONF_SOURCE/configure' failed"
503                 fi
504                 if [ -e /usr/share/gnuconfig/ ]; then
505                         find "${WORKDIR}" -type f '(' \
506                         -name config.guess -o -name config.sub ')' -print0 | \
507                         while read -r -d $'\0' x ; do
508                                 vecho " * econf: updating ${x/${WORKDIR}\/} with /usr/share/gnuconfig/${x##*/}"
509                                 cp -f /usr/share/gnuconfig/"${x##*/}" "${x}"
510                         done
511                 fi
512
513                 # EAPI=4 adds --disable-dependency-tracking to econf
514                 if ! hasq "$EAPI" 0 1 2 3 3_pre2 && \
515                         "${ECONF_SOURCE}/configure" --help 2>/dev/null | \
516                         grep -q disable-dependency-tracking ; then
517                         set -- --disable-dependency-tracking "$@"
518                 fi
519
520                 # if the profile defines a location to install libs to aside from default, pass it on.
521                 # if the ebuild passes in --libdir, they're responsible for the conf_libdir fun.
522                 local CONF_LIBDIR LIBDIR_VAR="LIBDIR_${ABI}"
523                 if [[ -n ${ABI} && -n ${!LIBDIR_VAR} ]] ; then
524                         CONF_LIBDIR=${!LIBDIR_VAR}
525                 fi
526                 if [[ -n ${CONF_LIBDIR} ]] && ! hasgq --libdir=\* "$@" ; then
527                         export CONF_PREFIX=$(hasg --exec-prefix=\* "$@")
528                         [[ -z ${CONF_PREFIX} ]] && CONF_PREFIX=$(hasg --prefix=\* "$@")
529                         : ${CONF_PREFIX:=/usr}
530                         CONF_PREFIX=${CONF_PREFIX#*=}
531                         [[ ${CONF_PREFIX} != /* ]] && CONF_PREFIX="/${CONF_PREFIX}"
532                         [[ ${CONF_LIBDIR} != /* ]] && CONF_LIBDIR="/${CONF_LIBDIR}"
533                         set -- --libdir="$(strip_duplicate_slashes ${CONF_PREFIX}${CONF_LIBDIR})" "$@"
534                 fi
535
536                 set -- \
537                         --prefix=/usr \
538                         ${CBUILD:+--build=${CBUILD}} \
539                         --host=${CHOST} \
540                         ${CTARGET:+--target=${CTARGET}} \
541                         --mandir=/usr/share/man \
542                         --infodir=/usr/share/info \
543                         --datadir=/usr/share \
544                         --sysconfdir=/etc \
545                         --localstatedir=/var/lib \
546                         "$@" \
547                         ${EXTRA_ECONF}
548                 vecho "${ECONF_SOURCE}/configure" "$@"
549
550                 if ! "${ECONF_SOURCE}/configure" "$@" ; then
551
552                         if [ -s config.log ]; then
553                                 echo
554                                 echo "!!! Please attach the following file when seeking support:"
555                                 echo "!!! ${PWD}/config.log"
556                         fi
557                         die "econf failed"
558                 fi
559         elif [ -f "${ECONF_SOURCE}/configure" ]; then
560                 die "configure is not executable"
561         else
562                 die "no configure script found"
563         fi
564 }
565
566 einstall() {
567         # CONF_PREFIX is only set if they didn't pass in libdir above.
568         local LOCAL_EXTRA_EINSTALL="${EXTRA_EINSTALL}"
569         LIBDIR_VAR="LIBDIR_${ABI}"
570         if [ -n "${ABI}" -a -n "${!LIBDIR_VAR}" ]; then
571                 CONF_LIBDIR="${!LIBDIR_VAR}"
572         fi
573         unset LIBDIR_VAR
574         if [ -n "${CONF_LIBDIR}" ] && [ "${CONF_PREFIX:+set}" = set ]; then
575                 EI_DESTLIBDIR="${D}/${CONF_PREFIX}/${CONF_LIBDIR}"
576                 EI_DESTLIBDIR="$(strip_duplicate_slashes ${EI_DESTLIBDIR})"
577                 LOCAL_EXTRA_EINSTALL="libdir=${EI_DESTLIBDIR} ${LOCAL_EXTRA_EINSTALL}"
578                 unset EI_DESTLIBDIR
579         fi
580
581         if [ -f ./[mM]akefile -o -f ./GNUmakefile ] ; then
582                 if [ "${PORTAGE_DEBUG}" == "1" ]; then
583                         ${MAKE:-make} -n prefix="${D}usr" \
584                                 datadir="${D}usr/share" \
585                                 infodir="${D}usr/share/info" \
586                                 localstatedir="${D}var/lib" \
587                                 mandir="${D}usr/share/man" \
588                                 sysconfdir="${D}etc" \
589                                 ${LOCAL_EXTRA_EINSTALL} \
590                                 ${MAKEOPTS} ${EXTRA_EMAKE} -j1 \
591                                 "$@" install
592                 fi
593                 ${MAKE:-make} prefix="${D}usr" \
594                         datadir="${D}usr/share" \
595                         infodir="${D}usr/share/info" \
596                         localstatedir="${D}var/lib" \
597                         mandir="${D}usr/share/man" \
598                         sysconfdir="${D}etc" \
599                         ${LOCAL_EXTRA_EINSTALL} \
600                         ${MAKEOPTS} ${EXTRA_EMAKE} -j1 \
601                         "$@" install || die "einstall failed"
602         else
603                 die "no Makefile found"
604         fi
605 }
606
607 _eapi0_pkg_nofetch() {
608         [ -z "${SRC_URI}" ] && return
609
610         elog "The following are listed in SRC_URI for ${PN}:"
611         local x
612         for x in $(echo ${SRC_URI}); do
613                 elog "   ${x}"
614         done
615 }
616
617 _eapi0_src_unpack() {
618         [[ -n ${A} ]] && unpack ${A}
619 }
620
621 _eapi0_src_compile() {
622         if [ -x ./configure ] ; then
623                 econf
624         fi
625         _eapi2_src_compile
626 }
627
628 _eapi0_src_test() {
629         # Since we don't want emake's automatic die
630         # support (EAPI 4 and later), and we also don't
631         # want the warning messages that it produces if
632         # we call it in 'nonfatal' mode, we use emake_cmd
633         # to emulate the desired parts of emake behavior.
634         local emake_cmd="${MAKE:-make} ${MAKEOPTS} ${EXTRA_EMAKE}"
635         if $emake_cmd -j1 check -n &> /dev/null; then
636                 vecho ">>> Test phase [check]: ${CATEGORY}/${PF}"
637                 if ! $emake_cmd -j1 check; then
638                         hasq test $FEATURES && die "Make check failed. See above for details."
639                         hasq test $FEATURES || eerror "Make check failed. See above for details."
640                 fi
641         elif $emake_cmd -j1 test -n &> /dev/null; then
642                 vecho ">>> Test phase [test]: ${CATEGORY}/${PF}"
643                 if ! $emake_cmd -j1 test; then
644                         hasq test $FEATURES && die "Make test failed. See above for details."
645                         hasq test $FEATURES || eerror "Make test failed. See above for details."
646                 fi
647         else
648                 vecho ">>> Test phase [none]: ${CATEGORY}/${PF}"
649         fi
650 }
651
652 _eapi1_src_compile() {
653         _eapi2_src_configure
654         _eapi2_src_compile
655 }
656
657 _eapi2_src_configure() {
658         if [[ -x ${ECONF_SOURCE:-.}/configure ]] ; then
659                 econf
660         fi
661 }
662
663 _eapi2_src_compile() {
664         if [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then
665                 emake || die "emake failed"
666         fi
667 }
668
669 _eapi4_src_install() {
670         if [[ -f Makefile || -f GNUmakefile || -f makefile ]] ; then
671                 emake DESTDIR="${D}" install
672         fi
673
674         if ! declare -p DOCS &>/dev/null ; then
675                 local d
676                 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
677                                 THANKS BUGS FAQ CREDITS CHANGELOG ; do
678                         [[ -s "${d}" ]] && dodoc "${d}"
679                 done
680         elif [[ $(declare -p DOCS) == "declare -a "* ]] ; then
681                 dodoc "${DOCS[@]}"
682         else
683                 dodoc ${DOCS}
684         fi
685 }
686
687 ebuild_phase() {
688         declare -F "$1" >/dev/null && qa_call $1
689 }
690
691 ebuild_phase_with_hooks() {
692         local x phase_name=${1}
693         for x in {pre_,,post_}${phase_name} ; do
694                 ebuild_phase ${x}
695         done
696 }
697
698 dyn_pretend() {
699         if [[ -e $PORTAGE_BUILDDIR/.pretended ]] ; then
700                 vecho ">>> It appears that '$PF' is already pretended; skipping."
701                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.pretended' to force pretend."
702                 return 0
703         fi
704         ebuild_phase pre_pkg_pretend
705         ebuild_phase pkg_pretend
706         >> "$PORTAGE_BUILDDIR/.pretended" || \
707                 die "Failed to create $PORTAGE_BUILDDIR/.pretended"
708         ebuild_phase post_pkg_pretend
709 }
710
711 dyn_setup() {
712         if [[ -e $PORTAGE_BUILDDIR/.setuped ]] ; then
713                 vecho ">>> It appears that '$PF' is already setup; skipping."
714                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.setuped' to force setup."
715                 return 0
716         fi
717         ebuild_phase pre_pkg_setup
718         ebuild_phase pkg_setup
719         >> "$PORTAGE_BUILDDIR/.setuped" || \
720                 die "Failed to create $PORTAGE_BUILDDIR/.setuped"
721         ebuild_phase post_pkg_setup
722 }
723
724 dyn_unpack() {
725         local newstuff="no"
726         if [ -e "${WORKDIR}" ]; then
727                 local x
728                 local checkme
729                 for x in $A ; do
730                         vecho ">>> Checking ${x}'s mtime..."
731                         if [ "${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/${x}" -nt "${WORKDIR}" ]; then
732                                 vecho ">>> ${x} has been updated; recreating WORKDIR..."
733                                 newstuff="yes"
734                                 break
735                         fi
736                 done
737                 if [ ! -f "${PORTAGE_BUILDDIR}/.unpacked" ] ; then
738                         vecho ">>> Not marked as unpacked; recreating WORKDIR..."
739                         newstuff="yes"
740                 fi
741         fi
742         if [ "${newstuff}" == "yes" ]; then
743                 # We don't necessarily have privileges to do a full dyn_clean here.
744                 rm -rf "${PORTAGE_BUILDDIR}"/{.setuped,.unpacked,.prepared,.configured,.compiled,.tested,.installed,.packaged,build-info}
745                 if ! hasq keepwork $FEATURES ; then
746                         rm -rf "${WORKDIR}"
747                 fi
748                 if [ -d "${T}" ] && \
749                         ! hasq keeptemp $FEATURES ; then
750                         rm -rf "${T}" && mkdir "${T}"
751                 fi
752         fi
753         if [ -e "${WORKDIR}" ]; then
754                 if [ "$newstuff" == "no" ]; then
755                         vecho ">>> WORKDIR is up-to-date, keeping..."
756                         return 0
757                 fi
758         fi
759
760         if [ ! -d "${WORKDIR}" ]; then
761                 install -m${PORTAGE_WORKDIR_MODE:-0700} -d "${WORKDIR}" || die "Failed to create dir '${WORKDIR}'"
762         fi
763         cd "${WORKDIR}" || die "Directory change failed: \`cd '${WORKDIR}'\`"
764         ebuild_phase pre_src_unpack
765         vecho ">>> Unpacking source..."
766         ebuild_phase src_unpack
767         >> "$PORTAGE_BUILDDIR/.unpacked" || \
768                 die "Failed to create $PORTAGE_BUILDDIR/.unpacked"
769         vecho ">>> Source unpacked in ${WORKDIR}"
770         ebuild_phase post_src_unpack
771 }
772
773 dyn_clean() {
774         if [ -z "${PORTAGE_BUILDDIR}" ]; then
775                 echo "Aborting clean phase because PORTAGE_BUILDDIR is unset!"
776                 return 1
777         elif [ ! -d "${PORTAGE_BUILDDIR}" ] ; then
778                 return 0
779         fi
780         if hasq chflags $FEATURES ; then
781                 chflags -R noschg,nouchg,nosappnd,nouappnd "${PORTAGE_BUILDDIR}"
782                 chflags -R nosunlnk,nouunlnk "${PORTAGE_BUILDDIR}" 2>/dev/null
783         fi
784
785         rm -rf "${PORTAGE_BUILDDIR}/image" "${PORTAGE_BUILDDIR}/homedir"
786         rm -f "${PORTAGE_BUILDDIR}/.installed"
787
788         if [[ $EMERGE_FROM = binary ]] || \
789                 ! hasq keeptemp $FEATURES && ! hasq keepwork $FEATURES ; then
790                 rm -rf "${T}"
791         fi
792
793         if [[ $EMERGE_FROM = binary ]] || ! hasq keepwork $FEATURES; then
794                 rm -f "$PORTAGE_BUILDDIR"/.{ebuild_changed,logid,pretended,setuped,unpacked,prepared} \
795                         "$PORTAGE_BUILDDIR"/.{configured,compiled,tested,packaged} \
796                         "$PORTAGE_BUILDDIR"/.die_hooks \
797                         "$PORTAGE_BUILDDIR"/.ipc_{in,out,lock} \
798                         "$PORTAGE_BUILDDIR"/.exit_status
799
800                 rm -rf "${PORTAGE_BUILDDIR}/build-info"
801                 rm -rf "${WORKDIR}"
802         fi
803
804         if [ -f "${PORTAGE_BUILDDIR}/.unpacked" ]; then
805                 find "${PORTAGE_BUILDDIR}" -type d ! -regex "^${WORKDIR}" | sort -r | tr "\n" "\0" | $XARGS -0 rmdir &>/dev/null
806         fi
807
808         # do not bind this to doebuild defined DISTDIR; don't trust doebuild, and if mistakes are made it'll
809         # result in it wiping the users distfiles directory (bad).
810         rm -rf "${PORTAGE_BUILDDIR}/distdir"
811
812         # Some kernels, such as Solaris, return EINVAL when an attempt
813         # is made to remove the current working directory.
814         cd "$PORTAGE_BUILDDIR"/../..
815         rmdir "$PORTAGE_BUILDDIR" 2>/dev/null
816
817         true
818 }
819
820 into() {
821         if [ "$1" == "/" ]; then
822                 export DESTTREE=""
823         else
824                 export DESTTREE=$1
825                 if [ ! -d "${D}${DESTTREE}" ]; then
826                         install -d "${D}${DESTTREE}"
827                         local ret=$?
828                         if [[ $ret -ne 0 ]] ; then
829                                 helpers_die "${FUNCNAME[0]} failed"
830                                 return $ret
831                         fi
832                 fi
833         fi
834 }
835
836 insinto() {
837         if [ "$1" == "/" ]; then
838                 export INSDESTTREE=""
839         else
840                 export INSDESTTREE=$1
841                 if [ ! -d "${D}${INSDESTTREE}" ]; then
842                         install -d "${D}${INSDESTTREE}"
843                         local ret=$?
844                         if [[ $ret -ne 0 ]] ; then
845                                 helpers_die "${FUNCNAME[0]} failed"
846                                 return $ret
847                         fi
848                 fi
849         fi
850 }
851
852 exeinto() {
853         if [ "$1" == "/" ]; then
854                 export _E_EXEDESTTREE_=""
855         else
856                 export _E_EXEDESTTREE_="$1"
857                 if [ ! -d "${D}${_E_EXEDESTTREE_}" ]; then
858                         install -d "${D}${_E_EXEDESTTREE_}"
859                         local ret=$?
860                         if [[ $ret -ne 0 ]] ; then
861                                 helpers_die "${FUNCNAME[0]} failed"
862                                 return $ret
863                         fi
864                 fi
865         fi
866 }
867
868 docinto() {
869         if [ "$1" == "/" ]; then
870                 export _E_DOCDESTTREE_=""
871         else
872                 export _E_DOCDESTTREE_="$1"
873                 if [ ! -d "${D}usr/share/doc/${PF}/${_E_DOCDESTTREE_}" ]; then
874                         install -d "${D}usr/share/doc/${PF}/${_E_DOCDESTTREE_}"
875                         local ret=$?
876                         if [[ $ret -ne 0 ]] ; then
877                                 helpers_die "${FUNCNAME[0]} failed"
878                                 return $ret
879                         fi
880                 fi
881         fi
882 }
883
884 insopts() {
885         export INSOPTIONS="$@"
886
887         # `install` should never be called with '-s' ...
888         hasq -s ${INSOPTIONS} && die "Never call insopts() with -s"
889 }
890
891 diropts() {
892         export DIROPTIONS="$@"
893 }
894
895 exeopts() {
896         export EXEOPTIONS="$@"
897
898         # `install` should never be called with '-s' ...
899         hasq -s ${EXEOPTIONS} && die "Never call exeopts() with -s"
900 }
901
902 libopts() {
903         export LIBOPTIONS="$@"
904
905         # `install` should never be called with '-s' ...
906         hasq -s ${LIBOPTIONS} && die "Never call libopts() with -s"
907 }
908
909 docompress() {
910         hasq "${EAPI}" 0 1 2 3 && die "'docompress' not supported in this EAPI"
911
912         local f g
913         if [[ $1 = "-x" ]]; then
914                 shift
915                 for f; do
916                         f=$(strip_duplicate_slashes "${f}"); f=${f%/}
917                         [[ ${f:0:1} = / ]] || f="/${f}"
918                         for g in "${PORTAGE_DOCOMPRESS_SKIP[@]}"; do
919                                 [[ ${f} = "${g}" ]] && continue 2
920                         done
921                         PORTAGE_DOCOMPRESS_SKIP[${#PORTAGE_DOCOMPRESS_SKIP[@]}]=${f}
922                 done
923         else
924                 for f; do
925                         f=$(strip_duplicate_slashes "${f}"); f=${f%/}
926                         [[ ${f:0:1} = / ]] || f="/${f}"
927                         for g in "${PORTAGE_DOCOMPRESS[@]}"; do
928                                 [[ ${f} = "${g}" ]] && continue 2
929                         done
930                         PORTAGE_DOCOMPRESS[${#PORTAGE_DOCOMPRESS[@]}]=${f}
931                 done
932         fi
933 }
934
935 abort_handler() {
936         local msg
937         if [ "$2" != "fail" ]; then
938                 msg="${EBUILD}: ${1} aborted; exiting."
939         else
940                 msg="${EBUILD}: ${1} failed; exiting."
941         fi
942         echo
943         echo "$msg"
944         echo
945         eval ${3}
946         #unset signal handler
947         trap - SIGINT SIGQUIT
948 }
949
950 abort_prepare() {
951         abort_handler src_prepare $1
952         rm -f "$PORTAGE_BUILDDIR/.prepared"
953         exit 1
954 }
955
956 abort_configure() {
957         abort_handler src_configure $1
958         rm -f "$PORTAGE_BUILDDIR/.configured"
959         exit 1
960 }
961
962 abort_compile() {
963         abort_handler "src_compile" $1
964         rm -f "${PORTAGE_BUILDDIR}/.compiled"
965         exit 1
966 }
967
968 abort_test() {
969         abort_handler "dyn_test" $1
970         rm -f "${PORTAGE_BUILDDIR}/.tested"
971         exit 1
972 }
973
974 abort_install() {
975         abort_handler "src_install" $1
976         rm -rf "${PORTAGE_BUILDDIR}/image"
977         exit 1
978 }
979
980 has_phase_defined_up_to() {
981         local phase
982         for phase in unpack prepare configure compile install; do
983                 has ${phase} ${DEFINED_PHASES} && return 0
984                 [[ ${phase} == $1 ]] && return 1
985         done
986         # We shouldn't actually get here
987         return 1
988 }
989
990 dyn_prepare() {
991
992         if [[ -e $PORTAGE_BUILDDIR/.prepared ]] ; then
993                 vecho ">>> It appears that '$PF' is already prepared; skipping."
994                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.prepared' to force prepare."
995                 return 0
996         fi
997
998         if [[ -d $S ]] ; then
999                 cd "${S}"
1000         elif hasq $EAPI 0 1 2 3 3_pre2 ; then
1001                 cd "${WORKDIR}"
1002         elif [[ -z ${A} ]] && ! has_phase_defined_up_to prepare; then
1003                 cd "${WORKDIR}"
1004         else
1005                 die "The source directory '${S}' doesn't exist"
1006         fi
1007
1008         trap abort_prepare SIGINT SIGQUIT
1009
1010         ebuild_phase pre_src_prepare
1011         vecho ">>> Preparing source in $PWD ..."
1012         ebuild_phase src_prepare
1013         >> "$PORTAGE_BUILDDIR/.prepared" || \
1014                 die "Failed to create $PORTAGE_BUILDDIR/.prepared"
1015         vecho ">>> Source prepared."
1016         ebuild_phase post_src_prepare
1017
1018         trap - SIGINT SIGQUIT
1019 }
1020
1021 dyn_configure() {
1022
1023         if [[ -e $PORTAGE_BUILDDIR/.configured ]] ; then
1024                 vecho ">>> It appears that '$PF' is already configured; skipping."
1025                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.configured' to force configuration."
1026                 return 0
1027         fi
1028
1029         if [[ -d $S ]] ; then
1030                 cd "${S}"
1031         elif hasq $EAPI 0 1 2 3 3_pre2 ; then
1032                 cd "${WORKDIR}"
1033         elif [[ -z ${A} ]] && ! has_phase_defined_up_to configure; then
1034                 cd "${WORKDIR}"
1035         else
1036                 die "The source directory '${S}' doesn't exist"
1037         fi
1038
1039         trap abort_configure SIGINT SIGQUIT
1040
1041         ebuild_phase pre_src_configure
1042
1043         vecho ">>> Configuring source in $PWD ..."
1044         ebuild_phase src_configure
1045         >> "$PORTAGE_BUILDDIR/.configured" || \
1046                 die "Failed to create $PORTAGE_BUILDDIR/.configured"
1047         vecho ">>> Source configured."
1048
1049         ebuild_phase post_src_configure
1050
1051         trap - SIGINT SIGQUIT
1052 }
1053
1054 dyn_compile() {
1055
1056         if [[ -e $PORTAGE_BUILDDIR/.compiled ]] ; then
1057                 vecho ">>> It appears that '${PF}' is already compiled; skipping."
1058                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.compiled' to force compilation."
1059                 return 0
1060         fi
1061
1062         if [[ -d $S ]] ; then
1063                 cd "${S}"
1064         elif hasq $EAPI 0 1 2 3 3_pre2 ; then
1065                 cd "${WORKDIR}"
1066         elif [[ -z ${A} ]] && ! has_phase_defined_up_to compile; then
1067                 cd "${WORKDIR}"
1068         else
1069                 die "The source directory '${S}' doesn't exist"
1070         fi
1071
1072         trap abort_compile SIGINT SIGQUIT
1073
1074         if hasq distcc $FEATURES && hasq distcc-pump $FEATURES ; then
1075                 if [[ -z $INCLUDE_SERVER_PORT ]] || [[ ! -w $INCLUDE_SERVER_PORT ]] ; then
1076                         eval $(pump --startup)
1077                         trap "pump --shutdown" EXIT
1078                 fi
1079         fi
1080
1081         ebuild_phase pre_src_compile
1082
1083         vecho ">>> Compiling source in $PWD ..."
1084         ebuild_phase src_compile
1085         >> "$PORTAGE_BUILDDIR/.compiled" || \
1086                 die "Failed to create $PORTAGE_BUILDDIR/.compiled"
1087         vecho ">>> Source compiled."
1088
1089         ebuild_phase post_src_compile
1090
1091         trap - SIGINT SIGQUIT
1092 }
1093
1094 dyn_test() {
1095
1096         if [[ -e $PORTAGE_BUILDDIR/.tested ]] ; then
1097                 vecho ">>> It appears that ${PN} has already been tested; skipping."
1098                 vecho ">>> Remove '${PORTAGE_BUILDDIR}/.tested' to force test."
1099                 return
1100         fi
1101
1102         if [ "${EBUILD_FORCE_TEST}" == "1" ] ; then
1103                 # If USE came from ${T}/environment then it might not have USE=test
1104                 # like it's supposed to here.
1105                 ! hasq test ${USE} && export USE="${USE} test"
1106         fi
1107
1108         trap "abort_test" SIGINT SIGQUIT
1109         if [ -d "${S}" ]; then
1110                 cd "${S}"
1111         else
1112                 cd "${WORKDIR}"
1113         fi
1114
1115         if ! hasq test $FEATURES && [ "${EBUILD_FORCE_TEST}" != "1" ]; then
1116                 vecho ">>> Test phase [not enabled]: ${CATEGORY}/${PF}"
1117         elif hasq test $RESTRICT; then
1118                 einfo "Skipping make test/check due to ebuild restriction."
1119                 vecho ">>> Test phase [explicitly disabled]: ${CATEGORY}/${PF}"
1120         else
1121                 local save_sp=${SANDBOX_PREDICT}
1122                 addpredict /
1123                 ebuild_phase pre_src_test
1124                 ebuild_phase src_test
1125                 >> "$PORTAGE_BUILDDIR/.tested" || \
1126                         die "Failed to create $PORTAGE_BUILDDIR/.tested"
1127                 ebuild_phase post_src_test
1128                 SANDBOX_PREDICT=${save_sp}
1129         fi
1130
1131         trap - SIGINT SIGQUIT
1132 }
1133
1134 dyn_install() {
1135         [ -z "$PORTAGE_BUILDDIR" ] && die "${FUNCNAME}: PORTAGE_BUILDDIR is unset"
1136         if hasq noauto $FEATURES ; then
1137                 rm -f "${PORTAGE_BUILDDIR}/.installed"
1138         elif [[ -e $PORTAGE_BUILDDIR/.installed ]] ; then
1139                 vecho ">>> It appears that '${PF}' is already installed; skipping."
1140                 vecho ">>> Remove '${PORTAGE_BUILDDIR}/.installed' to force install."
1141                 return 0
1142         fi
1143         trap "abort_install" SIGINT SIGQUIT
1144         ebuild_phase pre_src_install
1145         rm -rf "${PORTAGE_BUILDDIR}/image"
1146         mkdir "${PORTAGE_BUILDDIR}/image"
1147         if [[ -d $S ]] ; then
1148                 cd "${S}"
1149         elif hasq $EAPI 0 1 2 3 3_pre2 ; then
1150                 cd "${WORKDIR}"
1151         elif [[ -z ${A} ]] && ! has_phase_defined_up_to install; then
1152                 cd "${WORKDIR}"
1153         else
1154                 die "The source directory '${S}' doesn't exist"
1155         fi
1156
1157         vecho
1158         vecho ">>> Install ${PF} into ${D} category ${CATEGORY}"
1159         #our custom version of libtool uses $S and $D to fix
1160         #invalid paths in .la files
1161         export S D
1162
1163         # Reset exeinto(), docinto(), insinto(), and into() state variables
1164         # in case the user is running the install phase multiple times
1165         # consecutively via the ebuild command.
1166         export DESTTREE=/usr
1167         export INSDESTTREE=""
1168         export _E_EXEDESTTREE_=""
1169         export _E_DOCDESTTREE_=""
1170
1171         ebuild_phase src_install
1172         >> "$PORTAGE_BUILDDIR/.installed" || \
1173                 die "Failed to create $PORTAGE_BUILDDIR/.installed"
1174         vecho ">>> Completed installing ${PF} into ${D}"
1175         vecho
1176         ebuild_phase post_src_install
1177
1178         cd "${PORTAGE_BUILDDIR}"/build-info
1179         set -f
1180         local f x
1181         IFS=$' \t\n\r'
1182         for f in CATEGORY DEFINED_PHASES FEATURES INHERITED IUSE REQUIRED_USE \
1183                 PF PKGUSE SLOT KEYWORDS HOMEPAGE DESCRIPTION ; do
1184                 x=$(echo -n ${!f})
1185                 [[ -n $x ]] && echo "$x" > $f
1186         done
1187         if [[ $CATEGORY != virtual ]] ; then
1188                 for f in ASFLAGS CBUILD CC CFLAGS CHOST CTARGET CXX \
1189                         CXXFLAGS EXTRA_ECONF EXTRA_EINSTALL EXTRA_MAKE \
1190                         LDFLAGS LIBCFLAGS LIBCXXFLAGS ; do
1191                         x=$(echo -n ${!f})
1192                         [[ -n $x ]] && echo "$x" > $f
1193                 done
1194         fi
1195         echo "${USE}"       > USE
1196         echo "${EAPI:-0}"   > EAPI
1197         set +f
1198
1199         # local variables can leak into the saved environment.
1200         unset f
1201
1202         save_ebuild_env --exclude-init-phases | filter_readonly_variables \
1203                 --filter-path --filter-sandbox --allow-extra-vars > environment
1204         assert "save_ebuild_env failed"
1205
1206         ${PORTAGE_BZIP2_COMMAND} -f9 environment
1207
1208         cp "${EBUILD}" "${PF}.ebuild"
1209         [ -n "${PORTAGE_REPO_NAME}" ]  && echo "${PORTAGE_REPO_NAME}" > repository
1210         if hasq nostrip ${FEATURES} ${RESTRICT} || hasq strip ${RESTRICT}
1211         then
1212                 >> DEBUGBUILD
1213         fi
1214         trap - SIGINT SIGQUIT
1215 }
1216
1217 dyn_preinst() {
1218         if [ -z "${D}" ]; then
1219                 eerror "${FUNCNAME}: D is unset"
1220                 return 1
1221         fi
1222         ebuild_phase_with_hooks pkg_preinst
1223 }
1224
1225 dyn_help() {
1226         echo
1227         echo "Portage"
1228         echo "Copyright 1999-2010 Gentoo Foundation"
1229         echo
1230         echo "How to use the ebuild command:"
1231         echo
1232         echo "The first argument to ebuild should be an existing .ebuild file."
1233         echo
1234         echo "One or more of the following options can then be specified.  If more"
1235         echo "than one option is specified, each will be executed in order."
1236         echo
1237         echo "  help        : show this help screen"
1238         echo "  pretend     : execute package specific pretend actions"
1239         echo "  setup       : execute package specific setup actions"
1240         echo "  fetch       : download source archive(s) and patches"
1241         echo "  digest      : create a manifest file for the package"
1242         echo "  manifest    : create a manifest file for the package"
1243         echo "  unpack      : unpack sources (auto-dependencies if needed)"
1244         echo "  prepare     : prepare sources (auto-dependencies if needed)"
1245         echo "  configure   : configure sources (auto-fetch/unpack if needed)"
1246         echo "  compile     : compile sources (auto-fetch/unpack/configure if needed)"
1247         echo "  test        : test package (auto-fetch/unpack/configure/compile if needed)"
1248         echo "  preinst     : execute pre-install instructions"
1249         echo "  postinst    : execute post-install instructions"
1250         echo "  install     : install the package to the temporary install directory"
1251         echo "  qmerge      : merge image into live filesystem, recording files in db"
1252         echo "  merge       : do fetch, unpack, compile, install and qmerge"
1253         echo "  prerm       : execute pre-removal instructions"
1254         echo "  postrm      : execute post-removal instructions"
1255         echo "  unmerge     : remove package from live filesystem"
1256         echo "  config      : execute package specific configuration actions"
1257         echo "  package     : create a tarball package in ${PKGDIR}/All"
1258         echo "  rpm         : build a RedHat RPM package"
1259         echo "  clean       : clean up all source and temporary files"
1260         echo
1261         echo "The following settings will be used for the ebuild process:"
1262         echo
1263         echo "  package     : ${PF}"
1264         echo "  slot        : ${SLOT}"
1265         echo "  category    : ${CATEGORY}"
1266         echo "  description : ${DESCRIPTION}"
1267         echo "  system      : ${CHOST}"
1268         echo "  c flags     : ${CFLAGS}"
1269         echo "  c++ flags   : ${CXXFLAGS}"
1270         echo "  make flags  : ${MAKEOPTS}"
1271         echo -n "  build mode  : "
1272         if hasq nostrip ${FEATURES} ${RESTRICT} || hasq strip ${RESTRICT} ;
1273         then
1274                 echo "debug (large)"
1275         else
1276                 echo "production (stripped)"
1277         fi
1278         echo "  merge to    : ${ROOT}"
1279         echo
1280         if [ -n "$USE" ]; then
1281                 echo "Additionally, support for the following optional features will be enabled:"
1282                 echo
1283                 echo "  ${USE}"
1284         fi
1285         echo
1286 }
1287
1288 # debug-print() gets called from many places with verbose status information useful
1289 # for tracking down problems. The output is in $T/eclass-debug.log.
1290 # You can set ECLASS_DEBUG_OUTPUT to redirect the output somewhere else as well.
1291 # The special "on" setting echoes the information, mixing it with the rest of the
1292 # emerge output.
1293 # You can override the setting by exporting a new one from the console, or you can
1294 # set a new default in make.*. Here the default is "" or unset.
1295
1296 # in the future might use e* from /etc/init.d/functions.sh if i feel like it
1297 debug-print() {
1298         # if $T isn't defined, we're in dep calculation mode and
1299         # shouldn't do anything
1300         [[ $EBUILD_PHASE = depend || ! -d ${T} || ${#} -eq 0 ]] && return 0
1301
1302         if [[ ${ECLASS_DEBUG_OUTPUT} == on ]]; then
1303                 printf 'debug: %s\n' "${@}" >&2
1304         elif [[ -n ${ECLASS_DEBUG_OUTPUT} ]]; then
1305                 printf 'debug: %s\n' "${@}" >> "${ECLASS_DEBUG_OUTPUT}"
1306         fi
1307
1308         if [[ -w $T ]] ; then
1309                 # default target
1310                 printf '%s\n' "${@}" >> "${T}/eclass-debug.log"
1311                 # let the portage user own/write to this file
1312                 chgrp portage "${T}/eclass-debug.log" &>/dev/null
1313                 chmod g+w "${T}/eclass-debug.log" &>/dev/null
1314         fi
1315 }
1316
1317 # The following 2 functions are debug-print() wrappers
1318
1319 debug-print-function() {
1320         debug-print "${1}: entering function, parameters: ${*:2}"
1321 }
1322
1323 debug-print-section() {
1324         debug-print "now in section ${*}"
1325 }
1326
1327 # Sources all eclasses in parameters
1328 declare -ix ECLASS_DEPTH=0
1329 inherit() {
1330         ECLASS_DEPTH=$(($ECLASS_DEPTH + 1))
1331         if [[ ${ECLASS_DEPTH} > 1 ]]; then
1332                 debug-print "*** Multiple Inheritence (Level: ${ECLASS_DEPTH})"
1333         fi
1334
1335         if [[ -n $ECLASS && -n ${!__export_funcs_var} ]] ; then
1336                 echo "QA Notice: EXPORT_FUNCTIONS is called before inherit in" \
1337                         "$ECLASS.eclass. For compatibility with <=portage-2.1.6.7," \
1338                         "only call EXPORT_FUNCTIONS after inherit(s)." \
1339                         | fmt -w 75 | while read -r ; do eqawarn "$REPLY" ; done
1340         fi
1341
1342         local location
1343         local olocation
1344         local x
1345
1346         # These variables must be restored before returning.
1347         local PECLASS=$ECLASS
1348         local prev_export_funcs_var=$__export_funcs_var
1349
1350         local B_IUSE
1351         local B_REQUIRED_USE
1352         local B_DEPEND
1353         local B_RDEPEND
1354         local B_PDEPEND
1355         while [ "$1" ]; do
1356                 location="${ECLASSDIR}/${1}.eclass"
1357                 olocation=""
1358
1359                 export ECLASS="$1"
1360                 __export_funcs_var=__export_functions_$ECLASS_DEPTH
1361                 unset $__export_funcs_var
1362
1363                 if [ "${EBUILD_PHASE}" != "depend" ] && \
1364                         [[ ${EBUILD_PHASE} != *rm ]] && \
1365                         [[ ${EMERGE_FROM} != "binary" ]] ; then
1366                         # This is disabled in the *rm phases because they frequently give
1367                         # false alarms due to INHERITED in /var/db/pkg being outdated
1368                         # in comparison the the eclasses from the portage tree.
1369                         if ! hasq $ECLASS $INHERITED $__INHERITED_QA_CACHE ; then
1370                                 eqawarn "QA Notice: ECLASS '$ECLASS' inherited illegally in $CATEGORY/$PF $EBUILD_PHASE"
1371                         fi
1372                 fi
1373
1374                 # any future resolution code goes here
1375                 if [ -n "$PORTDIR_OVERLAY" ]; then
1376                         local overlay
1377                         for overlay in ${PORTDIR_OVERLAY}; do
1378                                 olocation="${overlay}/eclass/${1}.eclass"
1379                                 if [ -e "$olocation" ]; then
1380                                         location="${olocation}"
1381                                         debug-print "  eclass exists: ${location}"
1382                                 fi
1383                         done
1384                 fi
1385                 debug-print "inherit: $1 -> $location"
1386                 [ ! -e "$location" ] && die "${1}.eclass could not be found by inherit()"
1387
1388                 if [ "${location}" == "${olocation}" ] && \
1389                         ! hasq "${location}" ${EBUILD_OVERLAY_ECLASSES} ; then
1390                                 EBUILD_OVERLAY_ECLASSES="${EBUILD_OVERLAY_ECLASSES} ${location}"
1391                 fi
1392
1393                 #We need to back up the value of DEPEND and RDEPEND to B_DEPEND and B_RDEPEND
1394                 #(if set).. and then restore them after the inherit call.
1395
1396                 #turn off glob expansion
1397                 set -f
1398
1399                 # Retain the old data and restore it later.
1400                 unset B_IUSE B_REQUIRED_USE B_DEPEND B_RDEPEND B_PDEPEND
1401                 [ "${IUSE+set}"       = set ] && B_IUSE="${IUSE}"
1402                 [ "${REQUIRED_USE+set}" = set ] && B_REQUIRED_USE="${REQUIRED_USE}"
1403                 [ "${DEPEND+set}"     = set ] && B_DEPEND="${DEPEND}"
1404                 [ "${RDEPEND+set}"    = set ] && B_RDEPEND="${RDEPEND}"
1405                 [ "${PDEPEND+set}"    = set ] && B_PDEPEND="${PDEPEND}"
1406                 unset IUSE REQUIRED_USE DEPEND RDEPEND PDEPEND
1407                 #turn on glob expansion
1408                 set +f
1409
1410                 qa_source "$location" || die "died sourcing $location in inherit()"
1411                 
1412                 #turn off glob expansion
1413                 set -f
1414
1415                 # If each var has a value, append it to the global variable E_* to
1416                 # be applied after everything is finished. New incremental behavior.
1417                 [ "${IUSE+set}"       = set ] && export E_IUSE="${E_IUSE} ${IUSE}"
1418                 [ "${REQUIRED_USE+set}"       = set ] && export E_REQUIRED_USE="${E_REQUIRED_USE} ${REQUIRED_USE}"
1419                 [ "${DEPEND+set}"     = set ] && export E_DEPEND="${E_DEPEND} ${DEPEND}"
1420                 [ "${RDEPEND+set}"    = set ] && export E_RDEPEND="${E_RDEPEND} ${RDEPEND}"
1421                 [ "${PDEPEND+set}"    = set ] && export E_PDEPEND="${E_PDEPEND} ${PDEPEND}"
1422
1423                 [ "${B_IUSE+set}"     = set ] && IUSE="${B_IUSE}"
1424                 [ "${B_IUSE+set}"     = set ] || unset IUSE
1425                 
1426                 [ "${B_REQUIRED_USE+set}"     = set ] && REQUIRED_USE="${B_REQUIRED_USE}"
1427                 [ "${B_REQUIRED_USE+set}"     = set ] || unset REQUIRED_USE
1428
1429                 [ "${B_DEPEND+set}"   = set ] && DEPEND="${B_DEPEND}"
1430                 [ "${B_DEPEND+set}"   = set ] || unset DEPEND
1431
1432                 [ "${B_RDEPEND+set}"  = set ] && RDEPEND="${B_RDEPEND}"
1433                 [ "${B_RDEPEND+set}"  = set ] || unset RDEPEND
1434
1435                 [ "${B_PDEPEND+set}"  = set ] && PDEPEND="${B_PDEPEND}"
1436                 [ "${B_PDEPEND+set}"  = set ] || unset PDEPEND
1437
1438                 #turn on glob expansion
1439                 set +f
1440
1441                 if [[ -n ${!__export_funcs_var} ]] ; then
1442                         for x in ${!__export_funcs_var} ; do
1443                                 debug-print "EXPORT_FUNCTIONS: $x -> ${ECLASS}_$x"
1444                                 declare -F "${ECLASS}_$x" >/dev/null || \
1445                                         die "EXPORT_FUNCTIONS: ${ECLASS}_$x is not defined"
1446                                 eval "$x() { ${ECLASS}_$x \"\$@\" ; }" > /dev/null
1447                         done
1448                 fi
1449                 unset $__export_funcs_var
1450
1451                 hasq $1 $INHERITED || export INHERITED="$INHERITED $1"
1452
1453                 shift
1454         done
1455         ((--ECLASS_DEPTH)) # Returns 1 when ECLASS_DEPTH reaches 0.
1456         if (( ECLASS_DEPTH > 0 )) ; then
1457                 export ECLASS=$PECLASS
1458                 __export_funcs_var=$prev_export_funcs_var
1459         else
1460                 unset ECLASS __export_funcs_var
1461         fi
1462         return 0
1463 }
1464
1465 # Exports stub functions that call the eclass's functions, thereby making them default.
1466 # For example, if ECLASS="base" and you call "EXPORT_FUNCTIONS src_unpack", the following
1467 # code will be eval'd:
1468 # src_unpack() { base_src_unpack; }
1469 EXPORT_FUNCTIONS() {
1470         if [ -z "$ECLASS" ]; then
1471                 die "EXPORT_FUNCTIONS without a defined ECLASS"
1472         fi
1473         eval $__export_funcs_var+=\" $*\"
1474 }
1475
1476 # this is a function for removing any directory matching a passed in pattern from
1477 # PATH
1478 remove_path_entry() {
1479         save_IFS
1480         IFS=":"
1481         stripped_path="${PATH}"
1482         while [ -n "$1" ]; do
1483                 cur_path=""
1484                 for p in ${stripped_path}; do
1485                         if [ "${p/${1}}" == "${p}" ]; then
1486                                 cur_path="${cur_path}:${p}"
1487                         fi
1488                 done
1489                 stripped_path="${cur_path#:*}"
1490                 shift
1491         done
1492         restore_IFS
1493         PATH="${stripped_path}"
1494 }
1495
1496 # @FUNCTION: _ebuild_arg_to_phase
1497 # @DESCRIPTION:
1498 # Translate a known ebuild(1) argument into the precise
1499 # name of it's corresponding ebuild phase.
1500 _ebuild_arg_to_phase() {
1501         [ $# -ne 2 ] && die "expected exactly 2 args, got $#: $*"
1502         local eapi=$1
1503         local arg=$2
1504         local phase_func=""
1505
1506         case "$arg" in
1507                 pretend)
1508                         ! hasq $eapi 0 1 2 3 3_pre2 && \
1509                                 phase_func=pkg_pretend
1510                         ;;
1511                 setup)
1512                         phase_func=pkg_setup
1513                         ;;
1514                 nofetch)
1515                         phase_func=pkg_nofetch
1516                         ;;
1517                 unpack)
1518                         phase_func=src_unpack
1519                         ;;
1520                 prepare)
1521                         ! hasq $eapi 0 1 && \
1522                                 phase_func=src_prepare
1523                         ;;
1524                 configure)
1525                         ! hasq $eapi 0 1 && \
1526                                 phase_func=src_configure
1527                         ;;
1528                 compile)
1529                         phase_func=src_compile
1530                         ;;
1531                 test)
1532                         phase_func=src_test
1533                         ;;
1534                 install)
1535                         phase_func=src_install
1536                         ;;
1537                 preinst)
1538                         phase_func=pkg_preinst
1539                         ;;
1540                 postinst)
1541                         phase_func=pkg_postinst
1542                         ;;
1543                 prerm)
1544                         phase_func=pkg_prerm
1545                         ;;
1546                 postrm)
1547                         phase_func=pkg_postrm
1548                         ;;
1549         esac
1550
1551         [[ -z $phase_func ]] && return 1
1552         echo "$phase_func"
1553         return 0
1554 }
1555
1556 _ebuild_phase_funcs() {
1557         [ $# -ne 2 ] && die "expected exactly 2 args, got $#: $*"
1558         local eapi=$1
1559         local phase_func=$2
1560         local default_phases="pkg_nofetch src_unpack src_prepare src_configure
1561                 src_compile src_install src_test"
1562         local x y default_func=""
1563
1564         for x in pkg_nofetch src_unpack src_test ; do
1565                 declare -F $x >/dev/null || \
1566                         eval "$x() { _eapi0_$x \"\$@\" ; }"
1567         done
1568
1569         case $eapi in
1570
1571                 0|1)
1572
1573                         if ! declare -F src_compile >/dev/null ; then
1574                                 case $eapi in
1575                                         0)
1576                                                 src_compile() { _eapi0_src_compile "$@" ; }
1577                                                 ;;
1578                                         *)
1579                                                 src_compile() { _eapi1_src_compile "$@" ; }
1580                                                 ;;
1581                                 esac
1582                         fi
1583
1584                         for x in $default_phases ; do
1585                                 eval "default_$x() {
1586                                         die \"default_$x() is not supported with EAPI='$eapi' during phase $phase_func\"
1587                                 }"
1588                         done
1589
1590                         eval "default() {
1591                                 die \"default() is not supported with EAPI='$eapi' during phase $phase_func\"
1592                         }"
1593
1594                         ;;
1595
1596                 *)
1597
1598                         declare -F src_configure >/dev/null || \
1599                                 src_configure() { _eapi2_src_configure "$@" ; }
1600
1601                         declare -F src_compile >/dev/null || \
1602                                 src_compile() { _eapi2_src_compile "$@" ; }
1603
1604                         has $eapi 2 3 3_pre2 || declare -F src_install >/dev/null || \
1605                                 src_install() { _eapi4_src_install "$@" ; }
1606
1607                         if hasq $phase_func $default_phases ; then
1608
1609                                 _eapi2_pkg_nofetch   () { _eapi0_pkg_nofetch          "$@" ; }
1610                                 _eapi2_src_unpack    () { _eapi0_src_unpack           "$@" ; }
1611                                 _eapi2_src_prepare   () { true                             ; }
1612                                 _eapi2_src_test      () { _eapi0_src_test             "$@" ; }
1613                                 _eapi2_src_install   () { die "$FUNCNAME is not supported" ; }
1614
1615                                 for x in $default_phases ; do
1616                                         eval "default_$x() { _eapi2_$x \"\$@\" ; }"
1617                                 done
1618
1619                                 eval "default() { _eapi2_$phase_func \"\$@\" ; }"
1620
1621                                 case $eapi in
1622                                         2|3)
1623                                                 ;;
1624                                         *)
1625                                                 eval "default_src_install() { _eapi4_src_install \"\$@\" ; }"
1626                                                 [[ $phase_func = src_install ]] && \
1627                                                         eval "default() { _eapi4_$phase_func \"\$@\" ; }"
1628                                                 ;;
1629                                 esac
1630
1631                         else
1632
1633                                 for x in $default_phases ; do
1634                                         eval "default_$x() {
1635                                                 die \"default_$x() is not supported in phase $default_func\"
1636                                         }"
1637                                 done
1638
1639                                 eval "default() {
1640                                         die \"default() is not supported with EAPI='$eapi' during phase $phase_func\"
1641                                 }"
1642
1643                         fi
1644
1645                         ;;
1646         esac
1647 }
1648
1649 # Set given variables unless these variable have been already set (e.g. during emerge
1650 # invocation) to values different than values set in make.conf.
1651 set_unless_changed() {
1652         if [[ $# -lt 1 ]]; then
1653                 die "${FUNCNAME}() requires at least 1 argument: VARIABLE=VALUE"
1654         fi
1655
1656         local argument value variable
1657         for argument in "$@"; do
1658                 if [[ ${argument} != *=* ]]; then
1659                         die "${FUNCNAME}(): Argument '${argument}' has incorrect syntax"
1660                 fi
1661                 variable="${argument%%=*}"
1662                 value="${argument#*=}"
1663                 if eval "[[ \${${variable}} == \$(env -u ${variable} portageq envvar ${variable}) ]]"; then
1664                         eval "${variable}=\"\${value}\""
1665                 fi
1666         done
1667 }
1668
1669 # Unset given variables unless these variable have been set (e.g. during emerge
1670 # invocation) to values different than values set in make.conf.
1671 unset_unless_changed() {
1672         if [[ $# -lt 1 ]]; then
1673                 die "${FUNCNAME}() requires at least 1 argument: VARIABLE"
1674         fi
1675
1676         local variable
1677         for variable in "$@"; do
1678                 if eval "[[ \${${variable}} == \$(env -u ${variable} portageq envvar ${variable}) ]]"; then
1679                         unset ${variable}
1680                 fi
1681         done
1682 }
1683
1684 PORTAGE_BASHRCS_SOURCED=0
1685
1686 # @FUNCTION: source_all_bashrcs
1687 # @DESCRIPTION:
1688 # Source a relevant bashrc files and perform other miscellaneous
1689 # environment initialization when appropriate.
1690 #
1691 # If EAPI is set then define functions provided by the current EAPI:
1692 #
1693 #  * default_* aliases for the current EAPI phase functions
1694 #  * A "default" function which is an alias for the default phase
1695 #    function for the current phase.
1696 #
1697 source_all_bashrcs() {
1698         [[ $PORTAGE_BASHRCS_SOURCED = 1 ]] && return 0
1699         PORTAGE_BASHRCS_SOURCED=1
1700         local x
1701
1702         local OCC="${CC}" OCXX="${CXX}"
1703
1704         if [[ $EBUILD_PHASE != depend ]] ; then
1705                 # source the existing profile.bashrcs.
1706                 save_IFS
1707                 IFS=$'\n'
1708                 local path_array=($PROFILE_PATHS)
1709                 restore_IFS
1710                 for x in "${path_array[@]}" ; do
1711                         [ -f "$x/profile.bashrc" ] && qa_source "$x/profile.bashrc"
1712                 done
1713         fi
1714
1715         # We assume if people are changing shopts in their bashrc they do so at their
1716         # own peril.  This is the ONLY non-portage bit of code that can change shopts
1717         # without a QA violation.
1718         for x in "${PORTAGE_BASHRC}" "${PM_EBUILD_HOOK_DIR}"/${CATEGORY}/{${PN},${PN}:${SLOT},${P},${PF}}; do
1719                 if [ -r "${x}" ]; then
1720                         # If $- contains x, then tracing has already enabled elsewhere for some
1721                         # reason.  We preserve it's state so as not to interfere.
1722                         if [ "$PORTAGE_DEBUG" != "1" ] || [ "${-/x/}" != "$-" ]; then
1723                                 source "${x}"
1724                         else
1725                                 set -x
1726                                 source "${x}"
1727                                 set +x
1728                         fi
1729                 fi
1730         done
1731
1732         [ ! -z "${OCC}" ] && export CC="${OCC}"
1733         [ ! -z "${OCXX}" ] && export CXX="${OCXX}"
1734 }
1735
1736 # Hardcoded bash lists are needed for backward compatibility with
1737 # <portage-2.1.4 since they assume that a newly installed version
1738 # of ebuild.sh will work for pkg_postinst, pkg_prerm, and pkg_postrm
1739 # when portage is upgrading itself.
1740
1741 PORTAGE_READONLY_METADATA="DEFINED_PHASES DEPEND DESCRIPTION
1742         EAPI HOMEPAGE INHERITED IUSE REQUIRED_USE KEYWORDS LICENSE
1743         PDEPEND PROVIDE RDEPEND RESTRICT SLOT SRC_URI"
1744
1745 PORTAGE_READONLY_VARS="D EBUILD EBUILD_PHASE \
1746         EBUILD_SH_ARGS ECLASSDIR EMERGE_FROM FILESDIR MERGE_TYPE \
1747         PM_EBUILD_HOOK_DIR \
1748         PORTAGE_ACTUAL_DISTDIR PORTAGE_ARCHLIST PORTAGE_BASHRC  \
1749         PORTAGE_BINPKG_FILE PORTAGE_BINPKG_TAR_OPTS PORTAGE_BINPKG_TMPFILE \
1750         PORTAGE_BIN_PATH PORTAGE_BUILDDIR PORTAGE_BUNZIP2_COMMAND \
1751         PORTAGE_BZIP2_COMMAND PORTAGE_COLORMAP PORTAGE_CONFIGROOT \
1752         PORTAGE_DEBUG PORTAGE_DEPCACHEDIR PORTAGE_EBUILD_EXIT_FILE \
1753         PORTAGE_GID PORTAGE_GRPNAME PORTAGE_INST_GID PORTAGE_INST_UID \
1754         PORTAGE_IPC_DAEMON PORTAGE_IUSE PORTAGE_LOG_FILE \
1755         PORTAGE_MUTABLE_FILTERED_VARS PORTAGE_PYM_PATH PORTAGE_PYTHON \
1756         PORTAGE_READONLY_METADATA PORTAGE_READONLY_VARS \
1757         PORTAGE_REPO_NAME PORTAGE_RESTRICT PORTAGE_SANDBOX_COMPAT_LEVEL \
1758         PORTAGE_SAVED_READONLY_VARS PORTAGE_SIGPIPE_STATUS \
1759         PORTAGE_TMPDIR PORTAGE_UPDATE_ENV PORTAGE_USERNAME \
1760         PORTAGE_VERBOSE PORTAGE_WORKDIR_MODE PORTDIR PORTDIR_OVERLAY \
1761         PROFILE_PATHS REPLACING_VERSIONS REPLACED_BY_VERSION T WORKDIR"
1762
1763 PORTAGE_SAVED_READONLY_VARS="A CATEGORY P PF PN PR PV PVR"
1764
1765 # Variables that portage sets but doesn't mark readonly.
1766 # In order to prevent changed values from causing unexpected
1767 # interference, they are filtered out of the environment when
1768 # it is saved or loaded (any mutations do not persist).
1769 PORTAGE_MUTABLE_FILTERED_VARS="AA HOSTNAME"
1770
1771 # @FUNCTION: filter_readonly_variables
1772 # @DESCRIPTION: [--filter-sandbox] [--allow-extra-vars]
1773 # Read an environment from stdin and echo to stdout while filtering variables
1774 # with names that are known to cause interference:
1775 #
1776 #   * some specific variables for which bash does not allow assignment
1777 #   * some specific variables that affect portage or sandbox behavior
1778 #   * variable names that begin with a digit or that contain any
1779 #     non-alphanumeric characters that are not be supported by bash
1780 #
1781 # --filter-sandbox causes all SANDBOX_* variables to be filtered, which
1782 # is only desired in certain cases, such as during preprocessing or when
1783 # saving environment.bz2 for a binary or installed package.
1784 #
1785 # --filter-features causes the special FEATURES variable to be filtered.
1786 # Generally, we want it to persist between phases since the user might
1787 # want to modify it via bashrc to enable things like splitdebug and
1788 # installsources for specific packages. They should be able to modify it
1789 # in pre_pkg_setup() and have it persist all the way through the install
1790 # phase. However, if FEATURES exist inside environment.bz2 then they
1791 # should be overridden by current settings.
1792 #
1793 # --filter-locale causes locale related variables such as LANG and LC_*
1794 # variables to be filtered. These variables should persist between phases,
1795 # in case they are modified by the ebuild. However, the current user
1796 # settings should be used when loading the environment from a binary or
1797 # installed package.
1798 #
1799 # --filter-path causes the PATH variable to be filtered. This variable
1800 # should persist between phases, in case it is modified by the ebuild.
1801 # However, old settings should be overridden when loading the
1802 # environment from a binary or installed package.
1803 #
1804 # ---allow-extra-vars causes some extra vars to be allowd through, such
1805 # as ${PORTAGE_SAVED_READONLY_VARS} and ${PORTAGE_MUTABLE_FILTERED_VARS}.
1806 #
1807 # In bash-3.2_p20+ an attempt to assign BASH_*, FUNCNAME, GROUPS or any
1808 # readonly variable cause the shell to exit while executing the "source"
1809 # builtin command. To avoid this problem, this function filters those
1810 # variables out and discards them. See bug #190128.
1811 filter_readonly_variables() {
1812         local x filtered_vars
1813         local readonly_bash_vars="BASHOPTS BASHPID DIRSTACK EUID
1814                 FUNCNAME GROUPS PIPESTATUS PPID SHELLOPTS UID"
1815         local bash_misc_vars="BASH BASH_.* COMP_WORDBREAKS HISTCMD
1816                 HISTFILE HOSTNAME HOSTTYPE IFS LINENO MACHTYPE OLDPWD
1817                 OPTERR OPTIND OSTYPE POSIXLY_CORRECT PS4 PWD RANDOM
1818                 SECONDS SHELL SHLVL"
1819         local filtered_sandbox_vars="SANDBOX_ACTIVE SANDBOX_BASHRC
1820                 SANDBOX_DEBUG_LOG SANDBOX_DISABLED SANDBOX_LIB
1821                 SANDBOX_LOG SANDBOX_ON"
1822         local misc_garbage_vars="_portage_filter_opts"
1823         filtered_vars="$readonly_bash_vars $bash_misc_vars
1824                 $PORTAGE_READONLY_VARS $misc_garbage_vars"
1825
1826         # Don't filter/interfere with prefix variables unless they are
1827         # supported by the current EAPI.
1828         case "${EAPI:-0}" in
1829                 0|1|2)
1830                         ;;
1831                 *)
1832                         filtered_vars+=" ED EPREFIX EROOT"
1833                         ;;
1834         esac
1835
1836         if hasq --filter-sandbox $* ; then
1837                 filtered_vars="${filtered_vars} SANDBOX_.*"
1838         else
1839                 filtered_vars="${filtered_vars} ${filtered_sandbox_vars}"
1840         fi
1841         if hasq --filter-features $* ; then
1842                 filtered_vars="${filtered_vars} FEATURES PORTAGE_FEATURES"
1843         fi
1844         if hasq --filter-path $* ; then
1845                 filtered_vars+=" PATH"
1846         fi
1847         if hasq --filter-locale $* ; then
1848                 filtered_vars+=" LANG LC_ALL LC_COLLATE
1849                         LC_CTYPE LC_MESSAGES LC_MONETARY
1850                         LC_NUMERIC LC_PAPER LC_TIME"
1851         fi
1852         if ! hasq --allow-extra-vars $* ; then
1853                 filtered_vars="
1854                         ${filtered_vars}
1855                         ${PORTAGE_SAVED_READONLY_VARS}
1856                         ${PORTAGE_MUTABLE_FILTERED_VARS}
1857                 "
1858         fi
1859
1860         "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}"/filter-bash-environment.py "${filtered_vars}" || die "filter-bash-environment.py failed"
1861 }
1862
1863 # @FUNCTION: preprocess_ebuild_env
1864 # @DESCRIPTION:
1865 # Filter any readonly variables from ${T}/environment, source it, and then
1866 # save it via save_ebuild_env(). This process should be sufficient to prevent
1867 # any stale variables or functions from an arbitrary environment from
1868 # interfering with the current environment. This is useful when an existing
1869 # environment needs to be loaded from a binary or installed package.
1870 preprocess_ebuild_env() {
1871         local _portage_filter_opts="--filter-features --filter-locale --filter-path --filter-sandbox"
1872
1873         # If environment.raw is present, this is a signal from the python side,
1874         # indicating that the environment may contain stale FEATURES and
1875         # SANDBOX_{DENY,PREDICT,READ,WRITE} variables that should be filtered out.
1876         # Otherwise, we don't need to filter the environment.
1877         [ -f "${T}/environment.raw" ] || return 0
1878
1879         filter_readonly_variables $_portage_filter_opts < "${T}"/environment \
1880                 >> "$T/environment.filtered" || return $?
1881         unset _portage_filter_opts
1882         mv "${T}"/environment.filtered "${T}"/environment || return $?
1883         rm -f "${T}/environment.success" || return $?
1884         # WARNING: Code inside this subshell should avoid making assumptions
1885         # about variables or functions after source "${T}"/environment has been
1886         # called. Any variables that need to be relied upon should already be
1887         # filtered out above.
1888         (
1889                 export SANDBOX_ON=1
1890                 source "${T}/environment" || exit $?
1891                 # We have to temporarily disable sandbox since the
1892                 # SANDBOX_{DENY,READ,PREDICT,WRITE} values we've just loaded
1893                 # may be unusable (triggering in spurious sandbox violations)
1894                 # until we've merged them with our current values.
1895                 export SANDBOX_ON=0
1896
1897                 # It's remotely possible that save_ebuild_env() has been overridden
1898                 # by the above source command. To protect ourselves, we override it
1899                 # here with our own version. ${PORTAGE_BIN_PATH} is safe to use here
1900                 # because it's already filtered above.
1901                 source "${PORTAGE_BIN_PATH}/isolated-functions.sh" || exit $?
1902
1903                 # Rely on save_ebuild_env() to filter out any remaining variables
1904                 # and functions that could interfere with the current environment.
1905                 save_ebuild_env || exit $?
1906                 >> "$T/environment.success" || exit $?
1907         ) > "${T}/environment.filtered"
1908         local retval
1909         if [ -e "${T}/environment.success" ] ; then
1910                 filter_readonly_variables --filter-features < \
1911                         "${T}/environment.filtered" > "${T}/environment"
1912                 retval=$?
1913         else
1914                 retval=1
1915         fi
1916         rm -f "${T}"/environment.{filtered,raw,success}
1917         return ${retval}
1918 }
1919
1920 # === === === === === === === === === === === === === === === === === ===
1921 # === === === === === functions end, main part begins === === === === ===
1922 # === === === === === functions end, main part begins === === === === ===
1923 # === === === === === functions end, main part begins === === === === ===
1924 # === === === === === === === === === === === === === === === === === ===
1925
1926 export SANDBOX_ON="1"
1927 export S=${WORKDIR}/${P}
1928
1929 unset E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND
1930
1931 # Turn of extended glob matching so that g++ doesn't get incorrectly matched.
1932 shopt -u extglob
1933
1934 if [[ ${EBUILD_PHASE} == depend ]] ; then
1935         QA_INTERCEPTORS="awk bash cc egrep equery fgrep g++
1936                 gawk gcc grep javac java-config nawk perl
1937                 pkg-config python python-config sed"
1938 elif [[ ${EBUILD_PHASE} == clean* ]] ; then
1939         unset QA_INTERCEPTORS
1940 else
1941         QA_INTERCEPTORS="autoconf automake aclocal libtoolize"
1942 fi
1943 # level the QA interceptors if we're in depend
1944 if [[ -n ${QA_INTERCEPTORS} ]] ; then
1945         for BIN in ${QA_INTERCEPTORS}; do
1946                 BIN_PATH=$(type -Pf ${BIN})
1947                 if [ "$?" != "0" ]; then
1948                         BODY="echo \"*** missing command: ${BIN}\" >&2; return 127"
1949                 else
1950                         BODY="${BIN_PATH} \"\$@\"; return \$?"
1951                 fi
1952                 if [[ ${EBUILD_PHASE} == depend ]] ; then
1953                         FUNC_SRC="${BIN}() {
1954                                 if [ \$ECLASS_DEPTH -gt 0 ]; then
1955                                         eqawarn \"QA Notice: '${BIN}' called in global scope: eclass \${ECLASS}\"
1956                                 else
1957                                         eqawarn \"QA Notice: '${BIN}' called in global scope: \${CATEGORY}/\${PF}\"
1958                                 fi
1959                         ${BODY}
1960                         }"
1961                 elif hasq ${BIN} autoconf automake aclocal libtoolize ; then
1962                         FUNC_SRC="${BIN}() {
1963                                 if ! hasq \${FUNCNAME[1]} eautoreconf eaclocal _elibtoolize \\
1964                                         eautoheader eautoconf eautomake autotools_run_tool \\
1965                                         autotools_check_macro autotools_get_subdirs \\
1966                                         autotools_get_auxdir ; then
1967                                         eqawarn \"QA Notice: '${BIN}' called by \${FUNCNAME[1]}: \${CATEGORY}/\${PF}\"
1968                                         eqawarn \"Use autotools.eclass instead of calling '${BIN}' directly.\"
1969                                 fi
1970                         ${BODY}
1971                         }"
1972                 else
1973                         FUNC_SRC="${BIN}() {
1974                                 eqawarn \"QA Notice: '${BIN}' called by \${FUNCNAME[1]}: \${CATEGORY}/\${PF}\"
1975                         ${BODY}
1976                         }"
1977                 fi
1978                 eval "$FUNC_SRC" || echo "error creating QA interceptor ${BIN}" >&2
1979         done
1980         unset BIN_PATH BIN BODY FUNC_SRC
1981 fi
1982
1983 # Subshell/helper die support (must export for the die helper).
1984 export EBUILD_MASTER_PID=$BASHPID
1985 trap 'exit 1' SIGTERM
1986
1987 if ! hasq "$EBUILD_PHASE" clean cleanrm depend && \
1988         [ -f "${T}"/environment ] ; then
1989         # The environment may have been extracted from environment.bz2 or
1990         # may have come from another version of ebuild.sh or something.
1991         # In any case, preprocess it to prevent any potential interference.
1992         preprocess_ebuild_env || \
1993                 die "error processing environment"
1994         # Colon separated SANDBOX_* variables need to be cumulative.
1995         for x in SANDBOX_DENY SANDBOX_READ SANDBOX_PREDICT SANDBOX_WRITE ; do
1996                 export PORTAGE_${x}=${!x}
1997         done
1998         PORTAGE_SANDBOX_ON=${SANDBOX_ON}
1999         export SANDBOX_ON=1
2000         source "${T}"/environment || \
2001                 die "error sourcing environment"
2002         # We have to temporarily disable sandbox since the
2003         # SANDBOX_{DENY,READ,PREDICT,WRITE} values we've just loaded
2004         # may be unusable (triggering in spurious sandbox violations)
2005         # until we've merged them with our current values.
2006         export SANDBOX_ON=0
2007         for x in SANDBOX_DENY SANDBOX_PREDICT SANDBOX_READ SANDBOX_WRITE ; do
2008                 y="PORTAGE_${x}"
2009                 if [ -z "${!x}" ] ; then
2010                         export ${x}=${!y}
2011                 elif [ -n "${!y}" ] && [ "${!y}" != "${!x}" ] ; then
2012                         # filter out dupes
2013                         export ${x}=$(printf "${!y}:${!x}" | tr ":" "\0" | \
2014                                 sort -z -u | tr "\0" ":")
2015                 fi
2016                 export ${x}=${!x%:}
2017                 unset PORTAGE_${x}
2018         done
2019         unset x y
2020         export SANDBOX_ON=${PORTAGE_SANDBOX_ON}
2021         unset PORTAGE_SANDBOX_ON
2022         [[ -n $EAPI ]] || EAPI=0
2023 fi
2024
2025 if ! hasq "$EBUILD_PHASE" clean cleanrm ; then
2026         if [[ $EBUILD_PHASE = depend || ! -f $T/environment || \
2027                 -f $PORTAGE_BUILDDIR/.ebuild_changed ]] || \
2028                 hasq noauto $FEATURES ; then
2029                 # The bashrcs get an opportunity here to set aliases that will be expanded
2030                 # during sourcing of ebuilds and eclasses.
2031                 source_all_bashrcs
2032
2033                 # When EBUILD_PHASE != depend, INHERITED comes pre-initialized
2034                 # from cache. In order to make INHERITED content independent of
2035                 # EBUILD_PHASE during inherit() calls, we unset INHERITED after
2036                 # we make a backup copy for QA checks.
2037                 __INHERITED_QA_CACHE=$INHERITED
2038
2039                 # *DEPEND and IUSE will be set during the sourcing of the ebuild.
2040                 # In order to ensure correct interaction between ebuilds and
2041                 # eclasses, they need to be unset before this process of
2042                 # interaction begins.
2043                 unset DEPEND RDEPEND PDEPEND INHERITED IUSE REQUIRED_USE \
2044                         ECLASS E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND
2045
2046                 if [[ $PORTAGE_DEBUG != 1 || ${-/x/} != $- ]] ; then
2047                         source "$EBUILD" || die "error sourcing ebuild"
2048                 else
2049                         set -x
2050                         source "$EBUILD" || die "error sourcing ebuild"
2051                         set +x
2052                 fi
2053
2054                 if [[ "${EBUILD_PHASE}" != "depend" ]] ; then
2055                         RESTRICT=${PORTAGE_RESTRICT}
2056                         [[ -e $PORTAGE_BUILDDIR/.ebuild_changed ]] && \
2057                         rm "$PORTAGE_BUILDDIR/.ebuild_changed"
2058                 fi
2059
2060                 [[ -n $EAPI ]] || EAPI=0
2061
2062                 if has "$EAPI" 0 1 2 3 3_pre2 ; then
2063                         export RDEPEND=${RDEPEND-${DEPEND}}
2064                         debug-print "RDEPEND: not set... Setting to: ${DEPEND}"
2065                 fi
2066
2067                 # add in dependency info from eclasses
2068                 IUSE="${IUSE} ${E_IUSE}"
2069                 DEPEND="${DEPEND} ${E_DEPEND}"
2070                 RDEPEND="${RDEPEND} ${E_RDEPEND}"
2071                 PDEPEND="${PDEPEND} ${E_PDEPEND}"
2072                 REQUIRED_USE="${REQUIRED_USE} ${E_REQUIRED_USE}"
2073                 
2074                 unset ECLASS E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND \
2075                         __INHERITED_QA_CACHE
2076
2077                 # alphabetically ordered by $EBUILD_PHASE value
2078                 case "$EAPI" in
2079                         0|1)
2080                                 _valid_phases="src_compile pkg_config pkg_info src_install
2081                                         pkg_nofetch pkg_postinst pkg_postrm pkg_preinst pkg_prerm
2082                                         pkg_setup src_test src_unpack"
2083                                 ;;
2084                         2|3|3_pre2)
2085                                 _valid_phases="src_compile pkg_config src_configure pkg_info
2086                                         src_install pkg_nofetch pkg_postinst pkg_postrm pkg_preinst
2087                                         src_prepare pkg_prerm pkg_setup src_test src_unpack"
2088                                 ;;
2089                         *)
2090                                 _valid_phases="src_compile pkg_config src_configure pkg_info
2091                                         src_install pkg_nofetch pkg_postinst pkg_postrm pkg_preinst
2092                                         src_prepare pkg_prerm pkg_pretend pkg_setup src_test src_unpack"
2093                                 ;;
2094                 esac
2095
2096                 DEFINED_PHASES=
2097                 for _f in $_valid_phases ; do
2098                         if declare -F $_f >/dev/null ; then
2099                                 _f=${_f#pkg_}
2100                                 DEFINED_PHASES+=" ${_f#src_}"
2101                         fi
2102                 done
2103                 [[ -n $DEFINED_PHASES ]] || DEFINED_PHASES=-
2104
2105                 unset _f _valid_phases
2106
2107                 if [[ $EBUILD_PHASE != depend ]] ; then
2108
2109                         case "$EAPI" in
2110                                 0|1|2|3)
2111                                         _ebuild_helpers_path="$PORTAGE_BIN_PATH/ebuild-helpers"
2112                                         ;;
2113                                 *)
2114                                         _ebuild_helpers_path="$PORTAGE_BIN_PATH/ebuild-helpers/4:$PORTAGE_BIN_PATH/ebuild-helpers"
2115                                         ;;
2116                         esac
2117
2118                         PATH=$_ebuild_helpers_path:$PREROOTPATH${PREROOTPATH:+:}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${ROOTPATH:+:}$ROOTPATH
2119                         unset _ebuild_helpers_path
2120
2121                         # Use default ABI libdir in accordance with bug #355283.
2122                         x=LIBDIR_${DEFAULT_ABI}
2123                         [[ -n $DEFAULT_ABI && -n ${!x} ]] && x=${!x} || x=lib
2124
2125                         if hasq distcc $FEATURES ; then
2126                                 PATH="/usr/$x/distcc/bin:$PATH"
2127                                 [[ -n $DISTCC_LOG ]] && addwrite "${DISTCC_LOG%/*}"
2128                         fi
2129
2130                         if hasq ccache $FEATURES ; then
2131                                 PATH="/usr/$x/ccache/bin:$PATH"
2132
2133                                 if [[ -n $CCACHE_DIR ]] ; then
2134                                         addread "$CCACHE_DIR"
2135                                         addwrite "$CCACHE_DIR"
2136                                 fi
2137
2138                                 [[ -n $CCACHE_SIZE ]] && ccache -M $CCACHE_SIZE &> /dev/null
2139                         fi
2140
2141                         unset x
2142
2143                         if [[ -n $QA_PREBUILT ]] ; then
2144
2145                                 # these ones support fnmatch patterns
2146                                 QA_EXECSTACK+=" $QA_PREBUILT"
2147                                 QA_TEXTRELS+=" $QA_PREBUILT"
2148                                 QA_WX_LOAD+=" $QA_PREBUILT"
2149
2150                                 # these ones support regular expressions, so translate
2151                                 # fnmatch patterns to regular expressions
2152                                 for x in QA_DT_HASH QA_DT_NEEDED QA_PRESTRIPPED QA_SONAME ; do
2153                                         if [[ $(declare -p $x 2>/dev/null) = declare\ -a* ]] ; then
2154                                                 eval "$x=(\"\${$x[@]}\" ${QA_PREBUILT//\*/.*})"
2155                                         else
2156                                                 eval "$x+=\" ${QA_PREBUILT//\*/.*}\""
2157                                         fi
2158                                 done
2159
2160                                 unset x
2161                         fi
2162
2163                         # This needs to be exported since prepstrip is a separate shell script.
2164                         [[ -n $QA_PRESTRIPPED ]] && export QA_PRESTRIPPED
2165                         eval "[[ -n \$QA_PRESTRIPPED_${ARCH/-/_} ]] && \
2166                                 export QA_PRESTRIPPED_${ARCH/-/_}"
2167                 fi
2168         fi
2169 fi
2170
2171 # unset USE_EXPAND variables that contain only the special "*" token
2172 for x in ${USE_EXPAND} ; do
2173         [ "${!x}" == "*" ] && unset ${x}
2174 done
2175 unset x
2176
2177 if hasq nostrip ${FEATURES} ${RESTRICT} || hasq strip ${RESTRICT}
2178 then
2179         export DEBUGBUILD=1
2180 fi
2181
2182 #a reasonable default for $S
2183 [[ -z ${S} ]] && export S=${WORKDIR}/${P}
2184
2185 # Note: readonly variables interfere with preprocess_ebuild_env(), so
2186 # declare them only after it has already run.
2187 if [ "${EBUILD_PHASE}" != "depend" ] ; then
2188         declare -r $PORTAGE_READONLY_METADATA $PORTAGE_READONLY_VARS
2189         case "$EAPI" in
2190                 0|1|2)
2191                         ;;
2192                 *)
2193                         declare -r ED EPREFIX EROOT
2194                         ;;
2195         esac
2196 fi
2197
2198 ebuild_main() {
2199
2200         # Subshell/helper die support (must export for the die helper).
2201         # Since this function is typically executed in a subshell,
2202         # setup EBUILD_MASTER_PID to refer to the current $BASHPID,
2203         # which seems to give the best results when further
2204         # nested subshells call die.
2205         export EBUILD_MASTER_PID=$BASHPID
2206         trap 'exit 1' SIGTERM
2207
2208         if [[ $EBUILD_PHASE != depend ]] ; then
2209                 # Force configure scripts that automatically detect ccache to
2210                 # respect FEATURES="-ccache".
2211                 hasq ccache $FEATURES || export CCACHE_DISABLE=1
2212
2213                 local phase_func=$(_ebuild_arg_to_phase "$EAPI" "$EBUILD_PHASE")
2214                 [[ -n $phase_func ]] && _ebuild_phase_funcs "$EAPI" "$phase_func"
2215                 unset phase_func
2216         fi
2217
2218         source_all_bashrcs
2219
2220         case ${EBUILD_SH_ARGS} in
2221         nofetch)
2222                 ebuild_phase_with_hooks pkg_nofetch
2223                 ;;
2224         prerm|postrm|postinst|config|info)
2225                 if hasq "$EBUILD_SH_ARGS" config info && \
2226                         ! declare -F "pkg_$EBUILD_SH_ARGS" >/dev/null ; then
2227                         ewarn  "pkg_${EBUILD_SH_ARGS}() is not defined: '${EBUILD##*/}'"
2228                 fi
2229                 export SANDBOX_ON="0"
2230                 if [ "${PORTAGE_DEBUG}" != "1" ] || [ "${-/x/}" != "$-" ]; then
2231                         ebuild_phase_with_hooks pkg_${EBUILD_SH_ARGS}
2232                 else
2233                         set -x
2234                         ebuild_phase_with_hooks pkg_${EBUILD_SH_ARGS}
2235                         set +x
2236                 fi
2237                 if [[ $EBUILD_PHASE == postinst ]] && [[ -n $PORTAGE_UPDATE_ENV ]]; then
2238                         # Update environment.bz2 in case installation phases
2239                         # need to pass some variables to uninstallation phases.
2240                         save_ebuild_env --exclude-init-phases | \
2241                                 filter_readonly_variables --filter-path \
2242                                 --filter-sandbox --allow-extra-vars \
2243                                 | ${PORTAGE_BZIP2_COMMAND} -c -f9 > "$PORTAGE_UPDATE_ENV"
2244                         assert "save_ebuild_env failed"
2245                 fi
2246                 ;;
2247         unpack|prepare|configure|compile|test|clean|install)
2248                 if [[ ${SANDBOX_DISABLED:-0} = 0 ]] ; then
2249                         export SANDBOX_ON="1"
2250                 else
2251                         export SANDBOX_ON="0"
2252                 fi
2253
2254                 case "$EBUILD_SH_ARGS" in
2255                 configure|compile)
2256
2257                         local x
2258                         for x in ASFLAGS CCACHE_DIR CCACHE_SIZE \
2259                                 CFLAGS CXXFLAGS LDFLAGS LIBCFLAGS LIBCXXFLAGS ; do
2260                                 [[ ${!x+set} = set ]] && export $x
2261                         done
2262                         unset x
2263
2264                         hasq distcc $FEATURES && [[ -n $DISTCC_DIR ]] && \
2265                                 [[ ${SANDBOX_WRITE/$DISTCC_DIR} = $SANDBOX_WRITE ]] && \
2266                                 addwrite "$DISTCC_DIR"
2267
2268                         x=LIBDIR_$ABI
2269                         [ -z "$PKG_CONFIG_PATH" -a -n "$ABI" -a -n "${!x}" ] && \
2270                                 export PKG_CONFIG_PATH=/usr/${!x}/pkgconfig
2271
2272                         if hasq noauto $FEATURES && \
2273                                 [[ ! -f $PORTAGE_BUILDDIR/.unpacked ]] ; then
2274                                 echo
2275                                 echo "!!! We apparently haven't unpacked..." \
2276                                         "This is probably not what you"
2277                                 echo "!!! want to be doing... You are using" \
2278                                         "FEATURES=noauto so I'll assume"
2279                                 echo "!!! that you know what you are doing..." \
2280                                         "You have 5 seconds to abort..."
2281                                 echo
2282
2283                                 local x
2284                                 for x in 1 2 3 4 5 6 7 8; do
2285                                         LC_ALL=C sleep 0.25
2286                                 done
2287
2288                                 sleep 3
2289                         fi
2290
2291                         cd "$PORTAGE_BUILDDIR"
2292                         if [ ! -d build-info ] ; then
2293                                 mkdir build-info
2294                                 cp "$EBUILD" "build-info/$PF.ebuild"
2295                         fi
2296
2297                         #our custom version of libtool uses $S and $D to fix
2298                         #invalid paths in .la files
2299                         export S D
2300
2301                         ;;
2302                 esac
2303
2304                 if [ "${PORTAGE_DEBUG}" != "1" ] || [ "${-/x/}" != "$-" ]; then
2305                         dyn_${EBUILD_SH_ARGS}
2306                 else
2307                         set -x
2308                         dyn_${EBUILD_SH_ARGS}
2309                         set +x
2310                 fi
2311                 export SANDBOX_ON="0"
2312                 ;;
2313         help|pretend|setup|preinst)
2314                 #pkg_setup needs to be out of the sandbox for tmp file creation;
2315                 #for example, awking and piping a file in /tmp requires a temp file to be created
2316                 #in /etc.  If pkg_setup is in the sandbox, both our lilo and apache ebuilds break.
2317                 export SANDBOX_ON="0"
2318                 if [ "${PORTAGE_DEBUG}" != "1" ] || [ "${-/x/}" != "$-" ]; then
2319                         dyn_${EBUILD_SH_ARGS}
2320                 else
2321                         set -x
2322                         dyn_${EBUILD_SH_ARGS}
2323                         set +x
2324                 fi
2325                 ;;
2326         depend)
2327                 export SANDBOX_ON="0"
2328                 set -f
2329
2330                 if [ -n "${dbkey}" ] ; then
2331                         if [ ! -d "${dbkey%/*}" ]; then
2332                                 install -d -g ${PORTAGE_GID} -m2775 "${dbkey%/*}"
2333                         fi
2334                         # Make it group writable. 666&~002==664
2335                         umask 002
2336                 fi
2337
2338                 auxdbkeys="DEPEND RDEPEND SLOT SRC_URI RESTRICT HOMEPAGE LICENSE
2339                         DESCRIPTION KEYWORDS INHERITED IUSE REQUIRED_USE PDEPEND PROVIDE EAPI
2340                         PROPERTIES DEFINED_PHASES UNUSED_05 UNUSED_04
2341                         UNUSED_03 UNUSED_02 UNUSED_01"
2342
2343                 #the extra $(echo) commands remove newlines
2344                 [ -n "${EAPI}" ] || EAPI=0
2345
2346                 if [ -n "${dbkey}" ] ; then
2347                         > "${dbkey}"
2348                         for f in ${auxdbkeys} ; do
2349                                 echo $(echo ${!f}) >> "${dbkey}" || exit $?
2350                         done
2351                 else
2352                         for f in ${auxdbkeys} ; do
2353                                 echo $(echo ${!f}) 1>&9 || exit $?
2354                         done
2355                         exec 9>&-
2356                 fi
2357                 set +f
2358                 ;;
2359         _internal_test)
2360                 ;;
2361         *)
2362                 export SANDBOX_ON="1"
2363                 echo "Unrecognized EBUILD_SH_ARGS: '${EBUILD_SH_ARGS}'"
2364                 echo
2365                 dyn_help
2366                 exit 1
2367                 ;;
2368         esac
2369 }
2370
2371 if [[ -s $SANDBOX_LOG ]] ; then
2372         # We use SANDBOX_LOG to check for sandbox violations,
2373         # so we ensure that there can't be a stale log to
2374         # interfere with our logic.
2375         x=
2376         if [[ -n SANDBOX_ON ]] ; then
2377                 x=$SANDBOX_ON
2378                 export SANDBOX_ON=0
2379         fi
2380
2381         rm -f "$SANDBOX_LOG" || \
2382                 die "failed to remove stale sandbox log: '$SANDBOX_LOG'"
2383
2384         if [[ -n $x ]] ; then
2385                 export SANDBOX_ON=$x
2386         fi
2387         unset x
2388 fi
2389
2390 if [[ $EBUILD_PHASE = depend ]] ; then
2391         ebuild_main
2392 elif [[ -n $EBUILD_SH_ARGS ]] ; then
2393         (
2394                 # Don't allow subprocesses to inherit the pipe which
2395                 # emerge uses to monitor ebuild.sh.
2396                 exec 9>&-
2397
2398                 ebuild_main
2399
2400                 # Save the env only for relevant phases.
2401                 if ! hasq "$EBUILD_SH_ARGS" clean help info nofetch ; then
2402                         umask 002
2403                         save_ebuild_env | filter_readonly_variables \
2404                                 --filter-features > "$T/environment"
2405                         assert "save_ebuild_env failed"
2406                         chown portage:portage "$T/environment" &>/dev/null
2407                         chmod g+w "$T/environment" &>/dev/null
2408                 fi
2409                 [[ -n $PORTAGE_EBUILD_EXIT_FILE ]] && > "$PORTAGE_EBUILD_EXIT_FILE"
2410                 if [[ -n $PORTAGE_IPC_DAEMON ]] ; then
2411                         [[ ! -s $SANDBOX_LOG ]]
2412                         "$PORTAGE_BIN_PATH"/ebuild-ipc exit $?
2413                 fi
2414                 exit 0
2415         )
2416         exit $?
2417 fi
2418
2419 # Do not exit when ebuild.sh is sourced by other scripts.
2420 true