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