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