Add EPREFIX and ED support in all ebuild helpers.
[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 ROOTPATH=${ROOTPATH##:}
9 ROOTPATH=${ROOTPATH%%:}
10 PREROOTPATH=${PREROOTPATH##:}
11 PREROOTPATH=${PREROOTPATH%%:}
12 PATH=$PORTAGE_BIN_PATH/ebuild-helpers:$PREROOTPATH${PREROOTPATH:+:}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${ROOTPATH:+:}$ROOTPATH
13 export PATH
14
15 # Prevent aliases from causing portage to act inappropriately.
16 # Make sure it's before everything so we don't mess aliases that follow.
17 unalias -a
18
19 source "${PORTAGE_BIN_PATH}/isolated-functions.sh" || exit 1
20
21 if [[ $EBUILD_PHASE != depend ]] ; then
22         source "${PORTAGE_BIN_PATH}/phase-functions.sh" || die
23         source "${PORTAGE_BIN_PATH}/save-ebuild-env.sh" || die
24         source "${PORTAGE_BIN_PATH}/phase-helpers.sh" || die
25         source "${PORTAGE_BIN_PATH}/bashrc-functions.sh" || die
26 else
27         # These dummy functions are for things that are likely to be called
28         # in global scope, even though they are completely useless during
29         # the "depend" phase.
30         for x in diropts docompress exeopts get_KV insopts \
31                 keepdir KV_major KV_micro KV_minor KV_to_int \
32                 libopts register_die_hook register_success_hook \
33                 remove_path_entry set_unless_changed strip_duplicate_slashes \
34                 unset_unless_changed use_with use_enable ; do
35                 eval "${x}() { : ; }"
36         done
37         # These dummy functions return false, in order to ensure that
38         # `use multislot` is false for the "depend" phase.
39         for x in use useq usev ; do
40                 eval "${x}() { return 1; }"
41         done
42         # These functions die because calls to them during the "depend" phase
43         # are considered to be severe QA violations.
44         for x in best_version has_version portageq ; do
45                 eval "${x}() { die \"\${FUNCNAME} calls are not allowed in global scope\"; }"
46         done
47         unset x
48 fi
49
50 if [[ $PORTAGE_SANDBOX_COMPAT_LEVEL -lt 22 ]] ; then
51         # Ensure that /dev/std* streams have appropriate sandbox permission for
52         # bug #288863. This can be removed after sandbox is fixed and portage
53         # depends on the fixed version (sandbox-2.2 has the fix but it is
54         # currently unstable).
55         export SANDBOX_WRITE="${SANDBOX_WRITE:+${SANDBOX_WRITE}:}/dev/stdout:/dev/stderr"
56         export SANDBOX_READ="${SANDBOX_READ:+${SANDBOX_READ}:}/dev/stdin"
57 fi
58
59 # Don't use sandbox's BASH_ENV for new shells because it does
60 # 'source /etc/profile' which can interfere with the build
61 # environment by modifying our PATH.
62 unset BASH_ENV
63
64 # This is just a temporary workaround for portage-9999 users since
65 # earlier portage versions do not detect a version change in this case
66 # (9999 to 9999) and therefore they try execute an incompatible version of
67 # ebuild.sh during the upgrade.
68 export PORTAGE_BZIP2_COMMAND=${PORTAGE_BZIP2_COMMAND:-bzip2} 
69
70 # These two functions wrap sourcing and calling respectively.  At present they
71 # perform a qa check to make sure eclasses and ebuilds and profiles don't mess
72 # with shell opts (shopts).  Ebuilds/eclasses changing shopts should reset them 
73 # when they are done.
74
75 qa_source() {
76         local shopts=$(shopt) OLDIFS="$IFS"
77         local retval
78         source "$@"
79         retval=$?
80         set +e
81         [[ $shopts != $(shopt) ]] &&
82                 eqawarn "QA Notice: Global shell options changed and were not restored while sourcing '$*'"
83         [[ "$IFS" != "$OLDIFS" ]] &&
84                 eqawarn "QA Notice: Global IFS changed and was not restored while sourcing '$*'"
85         return $retval
86 }
87
88 qa_call() {
89         local shopts=$(shopt) OLDIFS="$IFS"
90         local retval
91         "$@"
92         retval=$?
93         set +e
94         [[ $shopts != $(shopt) ]] &&
95                 eqawarn "QA Notice: Global shell options changed and were not restored while calling '$*'"
96         [[ "$IFS" != "$OLDIFS" ]] &&
97                 eqawarn "QA Notice: Global IFS changed and was not restored while calling '$*'"
98         return $retval
99 }
100
101 EBUILD_SH_ARGS="$*"
102
103 shift $#
104
105 # Unset some variables that break things.
106 unset GZIP BZIP BZIP2 CDPATH GREP_OPTIONS GREP_COLOR GLOBIGNORE
107
108 [[ $PORTAGE_QUIET != "" ]] && export PORTAGE_QUIET
109
110 # sandbox support functions; defined prior to profile.bashrc srcing, since the profile might need to add a default exception (/usr/lib64/conftest fex)
111 _sb_append_var() {
112         local _v=$1 ; shift
113         local var="SANDBOX_${_v}"
114         [[ -z $1 || -n $2 ]] && die "Usage: add$(echo ${_v} | \
115                 LC_ALL=C tr [:upper:] [:lower:]) <colon-delimited list of paths>"
116         export ${var}="${!var:+${!var}:}$1"
117 }
118 # bash-4 version:
119 # local var="SANDBOX_${1^^}"
120 # addread() { _sb_append_var ${0#add} "$@" ; }
121 addread()    { _sb_append_var READ    "$@" ; }
122 addwrite()   { _sb_append_var WRITE   "$@" ; }
123 adddeny()    { _sb_append_var DENY    "$@" ; }
124 addpredict() { _sb_append_var PREDICT "$@" ; }
125
126 addwrite "${PORTAGE_TMPDIR}"
127 addread "/:${PORTAGE_TMPDIR}"
128 [[ -n ${PORTAGE_GPG_DIR} ]] && addpredict "${PORTAGE_GPG_DIR}"
129
130 # Avoid sandbox violations in temporary directories.
131 if [[ -w $T ]] ; then
132         export TEMP=$T
133         export TMP=$T
134         export TMPDIR=$T
135 elif [[ $SANDBOX_ON = 1 ]] ; then
136         for x in TEMP TMP TMPDIR ; do
137                 [[ -n ${!x} ]] && addwrite "${!x}"
138         done
139         unset x
140 fi
141
142 # the sandbox is disabled by default except when overridden in the relevant stages
143 export SANDBOX_ON=0
144
145 esyslog() {
146         # Custom version of esyslog() to take care of the "Red Star" bug.
147         # MUST follow functions.sh to override the "" parameter problem.
148         return 0
149 }
150
151 # Ensure that $PWD is sane whenever possible, to protect against
152 # exploitation of insecure search path for python -c in ebuilds.
153 # See bug #239560.
154 if ! has "$EBUILD_PHASE" clean cleanrm depend help ; then
155         cd "$PORTAGE_BUILDDIR" || \
156                 die "PORTAGE_BUILDDIR does not exist: '$PORTAGE_BUILDDIR'"
157 fi
158
159 #if no perms are specified, dirs/files will have decent defaults
160 #(not secretive, but not stupid)
161 umask 022
162
163 # debug-print() gets called from many places with verbose status information useful
164 # for tracking down problems. The output is in $T/eclass-debug.log.
165 # You can set ECLASS_DEBUG_OUTPUT to redirect the output somewhere else as well.
166 # The special "on" setting echoes the information, mixing it with the rest of the
167 # emerge output.
168 # You can override the setting by exporting a new one from the console, or you can
169 # set a new default in make.*. Here the default is "" or unset.
170
171 # in the future might use e* from /etc/init.d/functions.sh if i feel like it
172 debug-print() {
173         # if $T isn't defined, we're in dep calculation mode and
174         # shouldn't do anything
175         [[ $EBUILD_PHASE = depend || ! -d ${T} || ${#} -eq 0 ]] && return 0
176
177         if [[ ${ECLASS_DEBUG_OUTPUT} == on ]]; then
178                 printf 'debug: %s\n' "${@}" >&2
179         elif [[ -n ${ECLASS_DEBUG_OUTPUT} ]]; then
180                 printf 'debug: %s\n' "${@}" >> "${ECLASS_DEBUG_OUTPUT}"
181         fi
182
183         if [[ -w $T ]] ; then
184                 # default target
185                 printf '%s\n' "${@}" >> "${T}/eclass-debug.log"
186                 # let the portage user own/write to this file
187                 chgrp portage "${T}/eclass-debug.log" &>/dev/null
188                 chmod g+w "${T}/eclass-debug.log" &>/dev/null
189         fi
190 }
191
192 # The following 2 functions are debug-print() wrappers
193
194 debug-print-function() {
195         debug-print "${1}: entering function, parameters: ${*:2}"
196 }
197
198 debug-print-section() {
199         debug-print "now in section ${*}"
200 }
201
202 # Sources all eclasses in parameters
203 declare -ix ECLASS_DEPTH=0
204 inherit() {
205         ECLASS_DEPTH=$(($ECLASS_DEPTH + 1))
206         if [[ ${ECLASS_DEPTH} > 1 ]]; then
207                 debug-print "*** Multiple Inheritence (Level: ${ECLASS_DEPTH})"
208         fi
209
210         if [[ -n $ECLASS && -n ${!__export_funcs_var} ]] ; then
211                 echo "QA Notice: EXPORT_FUNCTIONS is called before inherit in" \
212                         "$ECLASS.eclass. For compatibility with <=portage-2.1.6.7," \
213                         "only call EXPORT_FUNCTIONS after inherit(s)." \
214                         | fmt -w 75 | while read -r ; do eqawarn "$REPLY" ; done
215         fi
216
217         local location
218         local olocation
219         local x
220
221         # These variables must be restored before returning.
222         local PECLASS=$ECLASS
223         local prev_export_funcs_var=$__export_funcs_var
224
225         local B_IUSE
226         local B_REQUIRED_USE
227         local B_DEPEND
228         local B_RDEPEND
229         local B_PDEPEND
230         while [ "$1" ]; do
231                 location="${ECLASSDIR}/${1}.eclass"
232                 olocation=""
233
234                 export ECLASS="$1"
235                 __export_funcs_var=__export_functions_$ECLASS_DEPTH
236                 unset $__export_funcs_var
237
238                 if [ "${EBUILD_PHASE}" != "depend" ] && \
239                         [[ ${EBUILD_PHASE} != *rm ]] && \
240                         [[ ${EMERGE_FROM} != "binary" ]] ; then
241                         # This is disabled in the *rm phases because they frequently give
242                         # false alarms due to INHERITED in /var/db/pkg being outdated
243                         # in comparison the the eclasses from the portage tree.
244                         if ! has $ECLASS $INHERITED $__INHERITED_QA_CACHE ; then
245                                 eqawarn "QA Notice: ECLASS '$ECLASS' inherited illegally in $CATEGORY/$PF $EBUILD_PHASE"
246                         fi
247                 fi
248
249                 # any future resolution code goes here
250                 if [ -n "$PORTDIR_OVERLAY" ]; then
251                         local overlay
252                         for overlay in ${PORTDIR_OVERLAY}; do
253                                 olocation="${overlay}/eclass/${1}.eclass"
254                                 if [ -e "$olocation" ]; then
255                                         location="${olocation}"
256                                         debug-print "  eclass exists: ${location}"
257                                 fi
258                         done
259                 fi
260                 debug-print "inherit: $1 -> $location"
261                 [ ! -e "$location" ] && die "${1}.eclass could not be found by inherit()"
262
263                 if [ "${location}" == "${olocation}" ] && \
264                         ! has "${location}" ${EBUILD_OVERLAY_ECLASSES} ; then
265                                 EBUILD_OVERLAY_ECLASSES="${EBUILD_OVERLAY_ECLASSES} ${location}"
266                 fi
267
268                 #We need to back up the value of DEPEND and RDEPEND to B_DEPEND and B_RDEPEND
269                 #(if set).. and then restore them after the inherit call.
270
271                 #turn off glob expansion
272                 set -f
273
274                 # Retain the old data and restore it later.
275                 unset B_IUSE B_REQUIRED_USE B_DEPEND B_RDEPEND B_PDEPEND
276                 [ "${IUSE+set}"       = set ] && B_IUSE="${IUSE}"
277                 [ "${REQUIRED_USE+set}" = set ] && B_REQUIRED_USE="${REQUIRED_USE}"
278                 [ "${DEPEND+set}"     = set ] && B_DEPEND="${DEPEND}"
279                 [ "${RDEPEND+set}"    = set ] && B_RDEPEND="${RDEPEND}"
280                 [ "${PDEPEND+set}"    = set ] && B_PDEPEND="${PDEPEND}"
281                 unset IUSE REQUIRED_USE DEPEND RDEPEND PDEPEND
282                 #turn on glob expansion
283                 set +f
284
285                 qa_source "$location" || die "died sourcing $location in inherit()"
286                 
287                 #turn off glob expansion
288                 set -f
289
290                 # If each var has a value, append it to the global variable E_* to
291                 # be applied after everything is finished. New incremental behavior.
292                 [ "${IUSE+set}"       = set ] && export E_IUSE="${E_IUSE} ${IUSE}"
293                 [ "${REQUIRED_USE+set}"       = set ] && export E_REQUIRED_USE="${E_REQUIRED_USE} ${REQUIRED_USE}"
294                 [ "${DEPEND+set}"     = set ] && export E_DEPEND="${E_DEPEND} ${DEPEND}"
295                 [ "${RDEPEND+set}"    = set ] && export E_RDEPEND="${E_RDEPEND} ${RDEPEND}"
296                 [ "${PDEPEND+set}"    = set ] && export E_PDEPEND="${E_PDEPEND} ${PDEPEND}"
297
298                 [ "${B_IUSE+set}"     = set ] && IUSE="${B_IUSE}"
299                 [ "${B_IUSE+set}"     = set ] || unset IUSE
300                 
301                 [ "${B_REQUIRED_USE+set}"     = set ] && REQUIRED_USE="${B_REQUIRED_USE}"
302                 [ "${B_REQUIRED_USE+set}"     = set ] || unset REQUIRED_USE
303
304                 [ "${B_DEPEND+set}"   = set ] && DEPEND="${B_DEPEND}"
305                 [ "${B_DEPEND+set}"   = set ] || unset DEPEND
306
307                 [ "${B_RDEPEND+set}"  = set ] && RDEPEND="${B_RDEPEND}"
308                 [ "${B_RDEPEND+set}"  = set ] || unset RDEPEND
309
310                 [ "${B_PDEPEND+set}"  = set ] && PDEPEND="${B_PDEPEND}"
311                 [ "${B_PDEPEND+set}"  = set ] || unset PDEPEND
312
313                 #turn on glob expansion
314                 set +f
315
316                 if [[ -n ${!__export_funcs_var} ]] ; then
317                         for x in ${!__export_funcs_var} ; do
318                                 debug-print "EXPORT_FUNCTIONS: $x -> ${ECLASS}_$x"
319                                 declare -F "${ECLASS}_$x" >/dev/null || \
320                                         die "EXPORT_FUNCTIONS: ${ECLASS}_$x is not defined"
321                                 eval "$x() { ${ECLASS}_$x \"\$@\" ; }" > /dev/null
322                         done
323                 fi
324                 unset $__export_funcs_var
325
326                 has $1 $INHERITED || export INHERITED="$INHERITED $1"
327
328                 shift
329         done
330         ((--ECLASS_DEPTH)) # Returns 1 when ECLASS_DEPTH reaches 0.
331         if (( ECLASS_DEPTH > 0 )) ; then
332                 export ECLASS=$PECLASS
333                 __export_funcs_var=$prev_export_funcs_var
334         else
335                 unset ECLASS __export_funcs_var
336         fi
337         return 0
338 }
339
340 # Exports stub functions that call the eclass's functions, thereby making them default.
341 # For example, if ECLASS="base" and you call "EXPORT_FUNCTIONS src_unpack", the following
342 # code will be eval'd:
343 # src_unpack() { base_src_unpack; }
344 EXPORT_FUNCTIONS() {
345         if [ -z "$ECLASS" ]; then
346                 die "EXPORT_FUNCTIONS without a defined ECLASS"
347         fi
348         eval $__export_funcs_var+=\" $*\"
349 }
350
351 PORTAGE_BASHRCS_SOURCED=0
352
353 # @FUNCTION: source_all_bashrcs
354 # @DESCRIPTION:
355 # Source a relevant bashrc files and perform other miscellaneous
356 # environment initialization when appropriate.
357 #
358 # If EAPI is set then define functions provided by the current EAPI:
359 #
360 #  * default_* aliases for the current EAPI phase functions
361 #  * A "default" function which is an alias for the default phase
362 #    function for the current phase.
363 #
364 source_all_bashrcs() {
365         [[ $PORTAGE_BASHRCS_SOURCED = 1 ]] && return 0
366         PORTAGE_BASHRCS_SOURCED=1
367         local x
368
369         local OCC="${CC}" OCXX="${CXX}"
370
371         if [[ $EBUILD_PHASE != depend ]] ; then
372                 # source the existing profile.bashrcs.
373                 save_IFS
374                 IFS=$'\n'
375                 local path_array=($PROFILE_PATHS)
376                 restore_IFS
377                 for x in "${path_array[@]}" ; do
378                         [ -f "$x/profile.bashrc" ] && qa_source "$x/profile.bashrc"
379                 done
380         fi
381
382         # We assume if people are changing shopts in their bashrc they do so at their
383         # own peril.  This is the ONLY non-portage bit of code that can change shopts
384         # without a QA violation.
385         for x in "${PORTAGE_BASHRC}" "${PM_EBUILD_HOOK_DIR}"/${CATEGORY}/{${PN},${PN}:${SLOT},${P},${PF}}; do
386                 if [ -r "${x}" ]; then
387                         # If $- contains x, then tracing has already enabled elsewhere for some
388                         # reason.  We preserve it's state so as not to interfere.
389                         if [ "$PORTAGE_DEBUG" != "1" ] || [ "${-/x/}" != "$-" ]; then
390                                 source "${x}"
391                         else
392                                 set -x
393                                 source "${x}"
394                                 set +x
395                         fi
396                 fi
397         done
398
399         [ ! -z "${OCC}" ] && export CC="${OCC}"
400         [ ! -z "${OCXX}" ] && export CXX="${OCXX}"
401 }
402
403 # === === === === === === === === === === === === === === === === === ===
404 # === === === === === functions end, main part begins === === === === ===
405 # === === === === === === === === === === === === === === === === === ===
406
407 export SANDBOX_ON="1"
408 export S=${WORKDIR}/${P}
409
410 # Turn of extended glob matching so that g++ doesn't get incorrectly matched.
411 shopt -u extglob
412
413 if [[ ${EBUILD_PHASE} == depend ]] ; then
414         QA_INTERCEPTORS="awk bash cc egrep equery fgrep g++
415                 gawk gcc grep javac java-config nawk perl
416                 pkg-config python python-config sed"
417 elif [[ ${EBUILD_PHASE} == clean* ]] ; then
418         unset QA_INTERCEPTORS
419 else
420         QA_INTERCEPTORS="autoconf automake aclocal libtoolize"
421 fi
422 # level the QA interceptors if we're in depend
423 if [[ -n ${QA_INTERCEPTORS} ]] ; then
424         for BIN in ${QA_INTERCEPTORS}; do
425                 BIN_PATH=$(type -Pf ${BIN})
426                 if [ "$?" != "0" ]; then
427                         BODY="echo \"*** missing command: ${BIN}\" >&2; return 127"
428                 else
429                         BODY="${BIN_PATH} \"\$@\"; return \$?"
430                 fi
431                 if [[ ${EBUILD_PHASE} == depend ]] ; then
432                         FUNC_SRC="${BIN}() {
433                                 if [ \$ECLASS_DEPTH -gt 0 ]; then
434                                         eqawarn \"QA Notice: '${BIN}' called in global scope: eclass \${ECLASS}\"
435                                 else
436                                         eqawarn \"QA Notice: '${BIN}' called in global scope: \${CATEGORY}/\${PF}\"
437                                 fi
438                         ${BODY}
439                         }"
440                 elif has ${BIN} autoconf automake aclocal libtoolize ; then
441                         FUNC_SRC="${BIN}() {
442                                 if ! has \${FUNCNAME[1]} eautoreconf eaclocal _elibtoolize \\
443                                         eautoheader eautoconf eautomake autotools_run_tool \\
444                                         autotools_check_macro autotools_get_subdirs \\
445                                         autotools_get_auxdir ; then
446                                         eqawarn \"QA Notice: '${BIN}' called by \${FUNCNAME[1]}: \${CATEGORY}/\${PF}\"
447                                         eqawarn \"Use autotools.eclass instead of calling '${BIN}' directly.\"
448                                 fi
449                         ${BODY}
450                         }"
451                 else
452                         FUNC_SRC="${BIN}() {
453                                 eqawarn \"QA Notice: '${BIN}' called by \${FUNCNAME[1]}: \${CATEGORY}/\${PF}\"
454                         ${BODY}
455                         }"
456                 fi
457                 eval "$FUNC_SRC" || echo "error creating QA interceptor ${BIN}" >&2
458         done
459         unset BIN_PATH BIN BODY FUNC_SRC
460 fi
461
462 # Subshell/helper die support (must export for the die helper).
463 export EBUILD_MASTER_PID=$BASHPID
464 trap 'exit 1' SIGTERM
465
466 if ! has "$EBUILD_PHASE" clean cleanrm depend && \
467         [ -f "${T}"/environment ] ; then
468         # The environment may have been extracted from environment.bz2 or
469         # may have come from another version of ebuild.sh or something.
470         # In any case, preprocess it to prevent any potential interference.
471         preprocess_ebuild_env || \
472                 die "error processing environment"
473         # Colon separated SANDBOX_* variables need to be cumulative.
474         for x in SANDBOX_DENY SANDBOX_READ SANDBOX_PREDICT SANDBOX_WRITE ; do
475                 export PORTAGE_${x}=${!x}
476         done
477         PORTAGE_SANDBOX_ON=${SANDBOX_ON}
478         export SANDBOX_ON=1
479         source "${T}"/environment || \
480                 die "error sourcing environment"
481         # We have to temporarily disable sandbox since the
482         # SANDBOX_{DENY,READ,PREDICT,WRITE} values we've just loaded
483         # may be unusable (triggering in spurious sandbox violations)
484         # until we've merged them with our current values.
485         export SANDBOX_ON=0
486         for x in SANDBOX_DENY SANDBOX_PREDICT SANDBOX_READ SANDBOX_WRITE ; do
487                 y="PORTAGE_${x}"
488                 if [ -z "${!x}" ] ; then
489                         export ${x}=${!y}
490                 elif [ -n "${!y}" ] && [ "${!y}" != "${!x}" ] ; then
491                         # filter out dupes
492                         export ${x}=$(printf "${!y}:${!x}" | tr ":" "\0" | \
493                                 sort -z -u | tr "\0" ":")
494                 fi
495                 export ${x}=${!x%:}
496                 unset PORTAGE_${x}
497         done
498         unset x y
499         export SANDBOX_ON=${PORTAGE_SANDBOX_ON}
500         unset PORTAGE_SANDBOX_ON
501         [[ -n $EAPI ]] || EAPI=0
502 fi
503
504 if ! has "$EBUILD_PHASE" clean cleanrm ; then
505         if [[ $EBUILD_PHASE = depend || ! -f $T/environment || \
506                 -f $PORTAGE_BUILDDIR/.ebuild_changed ]] || \
507                 has noauto $FEATURES ; then
508                 # The bashrcs get an opportunity here to set aliases that will be expanded
509                 # during sourcing of ebuilds and eclasses.
510                 source_all_bashrcs
511
512                 # When EBUILD_PHASE != depend, INHERITED comes pre-initialized
513                 # from cache. In order to make INHERITED content independent of
514                 # EBUILD_PHASE during inherit() calls, we unset INHERITED after
515                 # we make a backup copy for QA checks.
516                 __INHERITED_QA_CACHE=$INHERITED
517
518                 # *DEPEND and IUSE will be set during the sourcing of the ebuild.
519                 # In order to ensure correct interaction between ebuilds and
520                 # eclasses, they need to be unset before this process of
521                 # interaction begins.
522                 unset DEPEND RDEPEND PDEPEND INHERITED IUSE REQUIRED_USE \
523                         ECLASS E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND
524
525                 if [[ $PORTAGE_DEBUG != 1 || ${-/x/} != $- ]] ; then
526                         source "$EBUILD" || die "error sourcing ebuild"
527                 else
528                         set -x
529                         source "$EBUILD" || die "error sourcing ebuild"
530                         set +x
531                 fi
532
533                 if [[ "${EBUILD_PHASE}" != "depend" ]] ; then
534                         RESTRICT=${PORTAGE_RESTRICT}
535                         [[ -e $PORTAGE_BUILDDIR/.ebuild_changed ]] && \
536                         rm "$PORTAGE_BUILDDIR/.ebuild_changed"
537                 fi
538
539                 [[ -n $EAPI ]] || EAPI=0
540
541                 if has "$EAPI" 0 1 2 3 3_pre2 ; then
542                         export RDEPEND=${RDEPEND-${DEPEND}}
543                         debug-print "RDEPEND: not set... Setting to: ${DEPEND}"
544                 fi
545
546                 # add in dependency info from eclasses
547                 IUSE="${IUSE} ${E_IUSE}"
548                 DEPEND="${DEPEND} ${E_DEPEND}"
549                 RDEPEND="${RDEPEND} ${E_RDEPEND}"
550                 PDEPEND="${PDEPEND} ${E_PDEPEND}"
551                 REQUIRED_USE="${REQUIRED_USE} ${E_REQUIRED_USE}"
552                 
553                 unset ECLASS E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND \
554                         __INHERITED_QA_CACHE
555
556                 # alphabetically ordered by $EBUILD_PHASE value
557                 case "$EAPI" in
558                         0|1)
559                                 _valid_phases="src_compile pkg_config pkg_info src_install
560                                         pkg_nofetch pkg_postinst pkg_postrm pkg_preinst pkg_prerm
561                                         pkg_setup src_test src_unpack"
562                                 ;;
563                         2|3|3_pre2)
564                                 _valid_phases="src_compile pkg_config src_configure pkg_info
565                                         src_install pkg_nofetch pkg_postinst pkg_postrm pkg_preinst
566                                         src_prepare pkg_prerm pkg_setup src_test src_unpack"
567                                 ;;
568                         *)
569                                 _valid_phases="src_compile pkg_config src_configure pkg_info
570                                         src_install pkg_nofetch pkg_postinst pkg_postrm pkg_preinst
571                                         src_prepare pkg_prerm pkg_pretend pkg_setup src_test src_unpack"
572                                 ;;
573                 esac
574
575                 DEFINED_PHASES=
576                 for _f in $_valid_phases ; do
577                         if declare -F $_f >/dev/null ; then
578                                 _f=${_f#pkg_}
579                                 DEFINED_PHASES+=" ${_f#src_}"
580                         fi
581                 done
582                 [[ -n $DEFINED_PHASES ]] || DEFINED_PHASES=-
583
584                 unset _f _valid_phases
585
586                 if [[ $EBUILD_PHASE != depend ]] ; then
587
588                         case "$EAPI" in
589                                 0|1|2|3)
590                                         _ebuild_helpers_path="$PORTAGE_BIN_PATH/ebuild-helpers"
591                                         ;;
592                                 *)
593                                         _ebuild_helpers_path="$PORTAGE_BIN_PATH/ebuild-helpers/4:$PORTAGE_BIN_PATH/ebuild-helpers"
594                                         ;;
595                         esac
596
597                         PATH=$_ebuild_helpers_path:$PREROOTPATH${PREROOTPATH:+:}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${ROOTPATH:+:}$ROOTPATH
598                         unset _ebuild_helpers_path
599
600                         _eprefix=${EPREFIX}
601                         case "$EAPI" in 0|1|2) _eprefix= ;; esac
602                         # Use default ABI libdir in accordance with bug #355283.
603                         x=LIBDIR_${DEFAULT_ABI}
604                         [[ -n $DEFAULT_ABI && -n ${!x} ]] && x=${!x} || x=lib
605
606                         if has distcc $FEATURES ; then
607                                 PATH="${_eprefix}/usr/$x/distcc/bin:$PATH"
608                                 [[ -n $DISTCC_LOG ]] && addwrite "${DISTCC_LOG%/*}"
609                         fi
610
611                         if has ccache $FEATURES ; then
612                                 PATH="${_eprefix}/usr/$x/ccache/bin:$PATH"
613
614                                 if [[ -n $CCACHE_DIR ]] ; then
615                                         addread "$CCACHE_DIR"
616                                         addwrite "$CCACHE_DIR"
617                                 fi
618
619                                 [[ -n $CCACHE_SIZE ]] && ccache -M $CCACHE_SIZE &> /dev/null
620                         fi
621
622                         unset x _eprefix
623
624                         if [[ -n $QA_PREBUILT ]] ; then
625
626                                 # these ones support fnmatch patterns
627                                 QA_EXECSTACK+=" $QA_PREBUILT"
628                                 QA_TEXTRELS+=" $QA_PREBUILT"
629                                 QA_WX_LOAD+=" $QA_PREBUILT"
630
631                                 # these ones support regular expressions, so translate
632                                 # fnmatch patterns to regular expressions
633                                 for x in QA_DT_HASH QA_DT_NEEDED QA_PRESTRIPPED QA_SONAME ; do
634                                         if [[ $(declare -p $x 2>/dev/null) = declare\ -a* ]] ; then
635                                                 eval "$x=(\"\${$x[@]}\" ${QA_PREBUILT//\*/.*})"
636                                         else
637                                                 eval "$x+=\" ${QA_PREBUILT//\*/.*}\""
638                                         fi
639                                 done
640
641                                 unset x
642                         fi
643
644                         # This needs to be exported since prepstrip is a separate shell script.
645                         [[ -n $QA_PRESTRIPPED ]] && export QA_PRESTRIPPED
646                         eval "[[ -n \$QA_PRESTRIPPED_${ARCH/-/_} ]] && \
647                                 export QA_PRESTRIPPED_${ARCH/-/_}"
648                 fi
649         fi
650 fi
651
652 # unset USE_EXPAND variables that contain only the special "*" token
653 for x in ${USE_EXPAND} ; do
654         [ "${!x}" == "*" ] && unset ${x}
655 done
656 unset x
657
658 if has nostrip ${FEATURES} ${RESTRICT} || has strip ${RESTRICT}
659 then
660         export DEBUGBUILD=1
661 fi
662
663 if [[ $EBUILD_PHASE = depend ]] ; then
664         export SANDBOX_ON="0"
665         set -f
666
667         if [ -n "${dbkey}" ] ; then
668                 if [ ! -d "${dbkey%/*}" ]; then
669                         install -d -g ${PORTAGE_GID} -m2775 "${dbkey%/*}"
670                 fi
671                 # Make it group writable. 666&~002==664
672                 umask 002
673         fi
674
675         auxdbkeys="DEPEND RDEPEND SLOT SRC_URI RESTRICT HOMEPAGE LICENSE
676                 DESCRIPTION KEYWORDS INHERITED IUSE REQUIRED_USE PDEPEND PROVIDE EAPI
677                 PROPERTIES DEFINED_PHASES UNUSED_05 UNUSED_04
678                 UNUSED_03 UNUSED_02 UNUSED_01"
679
680         #the extra $(echo) commands remove newlines
681         [ -n "${EAPI}" ] || EAPI=0
682
683         if [ -n "${dbkey}" ] ; then
684                 > "${dbkey}"
685                 for f in ${auxdbkeys} ; do
686                         echo $(echo ${!f}) >> "${dbkey}" || exit $?
687                 done
688         else
689                 for f in ${auxdbkeys} ; do
690                         echo $(echo ${!f}) 1>&9 || exit $?
691                 done
692                 exec 9>&-
693         fi
694         set +f
695 else
696         # Note: readonly variables interfere with preprocess_ebuild_env(), so
697         # declare them only after it has already run.
698         declare -r $PORTAGE_READONLY_METADATA $PORTAGE_READONLY_VARS
699         case "$EAPI" in
700                 0|1|2)
701                         ;;
702                 *)
703                         declare -r ED EPREFIX EROOT
704                         ;;
705         esac
706
707         if [[ -n $EBUILD_SH_ARGS ]] ; then
708                 (
709                         # Don't allow subprocesses to inherit the pipe which
710                         # emerge uses to monitor ebuild.sh.
711                         exec 9>&-
712                         ebuild_main ${EBUILD_SH_ARGS}
713                         exit 0
714                 )
715                 exit $?
716         fi
717 fi
718
719 # Do not exit when ebuild.sh is sourced by other scripts.
720 true