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