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