ebuild.sh: move hasg* funcs into econf
[portage.git] / bin / ebuild.sh
1 #!/bin/bash
2 # Copyright 1999-2011 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 PORTAGE_BIN_PATH="${PORTAGE_BIN_PATH:-/usr/lib/portage/bin}"
6 PORTAGE_PYM_PATH="${PORTAGE_PYM_PATH:-/usr/lib/portage/pym}"
7
8 if [[ $PORTAGE_SANDBOX_COMPAT_LEVEL -lt 22 ]] ; then
9         # Ensure that /dev/std* streams have appropriate sandbox permission for
10         # bug #288863. This can be removed after sandbox is fixed and portage
11         # depends on the fixed version (sandbox-2.2 has the fix but it is
12         # currently unstable).
13         export SANDBOX_WRITE="${SANDBOX_WRITE:+${SANDBOX_WRITE}:}/dev/stdout:/dev/stderr"
14         export SANDBOX_READ="${SANDBOX_READ:+${SANDBOX_READ}:}/dev/stdin"
15 fi
16
17 # Don't use sandbox's BASH_ENV for new shells because it does
18 # 'source /etc/profile' which can interfere with the build
19 # environment by modifying our PATH.
20 unset BASH_ENV
21
22 ROOTPATH=${ROOTPATH##:}
23 ROOTPATH=${ROOTPATH%%:}
24 PREROOTPATH=${PREROOTPATH##:}
25 PREROOTPATH=${PREROOTPATH%%:}
26 PATH=$PORTAGE_BIN_PATH/ebuild-helpers:$PREROOTPATH${PREROOTPATH:+:}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${ROOTPATH:+:}$ROOTPATH
27 export PATH
28
29 # This is just a temporary workaround for portage-9999 users since
30 # earlier portage versions do not detect a version change in this case
31 # (9999 to 9999) and therefore they try execute an incompatible version of
32 # ebuild.sh during the upgrade.
33 export PORTAGE_BZIP2_COMMAND=${PORTAGE_BZIP2_COMMAND:-bzip2} 
34
35 # These two functions wrap sourcing and calling respectively.  At present they
36 # perform a qa check to make sure eclasses and ebuilds and profiles don't mess
37 # with shell opts (shopts).  Ebuilds/eclasses changing shopts should reset them 
38 # when they are done.
39
40 qa_source() {
41         local shopts=$(shopt) OLDIFS="$IFS"
42         local retval
43         source "$@"
44         retval=$?
45         set +e
46         [[ $shopts != $(shopt) ]] &&
47                 eqawarn "QA Notice: Global shell options changed and were not restored while sourcing '$*'"
48         [[ "$IFS" != "$OLDIFS" ]] &&
49                 eqawarn "QA Notice: Global IFS changed and was not restored while sourcing '$*'"
50         return $retval
51 }
52
53 qa_call() {
54         local shopts=$(shopt) OLDIFS="$IFS"
55         local retval
56         "$@"
57         retval=$?
58         set +e
59         [[ $shopts != $(shopt) ]] &&
60                 eqawarn "QA Notice: Global shell options changed and were not restored while calling '$*'"
61         [[ "$IFS" != "$OLDIFS" ]] &&
62                 eqawarn "QA Notice: Global IFS changed and was not restored while calling '$*'"
63         return $retval
64 }
65
66 EBUILD_SH_ARGS="$*"
67
68 shift $#
69
70 # Prevent aliases from causing portage to act inappropriately.
71 # Make sure it's before everything so we don't mess aliases that follow.
72 unalias -a
73
74 # Unset some variables that break things.
75 unset GZIP BZIP BZIP2 CDPATH GREP_OPTIONS GREP_COLOR GLOBIGNORE
76
77 source "${PORTAGE_BIN_PATH}/isolated-functions.sh"  &>/dev/null
78
79 [[ $PORTAGE_QUIET != "" ]] && export PORTAGE_QUIET
80
81 # sandbox support functions; defined prior to profile.bashrc srcing, since the profile might need to add a default exception (/usr/lib64/conftest fex)
82 _sb_append_var() {
83         local _v=$1 ; shift
84         local var="SANDBOX_${_v}"
85         [[ -z $1 || -n $2 ]] && die "Usage: add$(echo ${_v} | \
86                 LC_ALL=C tr [:upper:] [:lower:]) <colon-delimited list of paths>"
87         export ${var}="${!var:+${!var}:}$1"
88 }
89 # bash-4 version:
90 # local var="SANDBOX_${1^^}"
91 # addread() { _sb_append_var ${0#add} "$@" ; }
92 addread()    { _sb_append_var READ    "$@" ; }
93 addwrite()   { _sb_append_var WRITE   "$@" ; }
94 adddeny()    { _sb_append_var DENY    "$@" ; }
95 addpredict() { _sb_append_var PREDICT "$@" ; }
96
97 addwrite "${PORTAGE_TMPDIR}"
98 addread "/:${PORTAGE_TMPDIR}"
99 [[ -n ${PORTAGE_GPG_DIR} ]] && addpredict "${PORTAGE_GPG_DIR}"
100
101 # Avoid sandbox violations in temporary directories.
102 if [[ -w $T ]] ; then
103         export TEMP=$T
104         export TMP=$T
105         export TMPDIR=$T
106 elif [[ $SANDBOX_ON = 1 ]] ; then
107         for x in TEMP TMP TMPDIR ; do
108                 [[ -n ${!x} ]] && addwrite "${!x}"
109         done
110         unset x
111 fi
112
113 # the sandbox is disabled by default except when overridden in the relevant stages
114 export SANDBOX_ON=0
115
116 esyslog() {
117         # Custom version of esyslog() to take care of the "Red Star" bug.
118         # MUST follow functions.sh to override the "" parameter problem.
119         return 0
120 }
121
122 portageq() {
123         if [ "${EBUILD_PHASE}" == "depend" ]; then
124                 die "portageq calls are not allowed in the global scope"
125         fi
126
127         PYTHONPATH=${PORTAGE_PYM_PATH}${PYTHONPATH:+:}${PYTHONPATH} \
128         "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}/portageq" "$@"
129 }
130
131 register_die_hook() {
132         local x
133         for x in $* ; do
134                 has $x $EBUILD_DEATH_HOOKS || \
135                         export EBUILD_DEATH_HOOKS="$EBUILD_DEATH_HOOKS $x"
136         done
137 }
138
139 register_success_hook() {
140         local x
141         for x in $* ; do
142                 has $x $EBUILD_SUCCESS_HOOKS || \
143                         export EBUILD_SUCCESS_HOOKS="$EBUILD_SUCCESS_HOOKS $x"
144         done
145 }
146
147 # Ensure that $PWD is sane whenever possible, to protect against
148 # exploitation of insecure search path for python -c in ebuilds.
149 # See bug #239560.
150 if ! has "$EBUILD_PHASE" clean cleanrm depend help ; then
151         cd "$PORTAGE_BUILDDIR" || \
152                 die "PORTAGE_BUILDDIR does not exist: '$PORTAGE_BUILDDIR'"
153 fi
154
155 #if no perms are specified, dirs/files will have decent defaults
156 #(not secretive, but not stupid)
157 umask 022
158
159 strip_duplicate_slashes() {
160         if [[ -n $1 ]] ; then
161                 local removed=$1
162                 while [[ ${removed} == *//* ]] ; do
163                         removed=${removed//\/\///}
164                 done
165                 echo ${removed}
166         fi
167 }
168
169 # debug-print() gets called from many places with verbose status information useful
170 # for tracking down problems. The output is in $T/eclass-debug.log.
171 # You can set ECLASS_DEBUG_OUTPUT to redirect the output somewhere else as well.
172 # The special "on" setting echoes the information, mixing it with the rest of the
173 # emerge output.
174 # You can override the setting by exporting a new one from the console, or you can
175 # set a new default in make.*. Here the default is "" or unset.
176
177 # in the future might use e* from /etc/init.d/functions.sh if i feel like it
178 debug-print() {
179         # if $T isn't defined, we're in dep calculation mode and
180         # shouldn't do anything
181         [[ $EBUILD_PHASE = depend || ! -d ${T} || ${#} -eq 0 ]] && return 0
182
183         if [[ ${ECLASS_DEBUG_OUTPUT} == on ]]; then
184                 printf 'debug: %s\n' "${@}" >&2
185         elif [[ -n ${ECLASS_DEBUG_OUTPUT} ]]; then
186                 printf 'debug: %s\n' "${@}" >> "${ECLASS_DEBUG_OUTPUT}"
187         fi
188
189         if [[ -w $T ]] ; then
190                 # default target
191                 printf '%s\n' "${@}" >> "${T}/eclass-debug.log"
192                 # let the portage user own/write to this file
193                 chgrp portage "${T}/eclass-debug.log" &>/dev/null
194                 chmod g+w "${T}/eclass-debug.log" &>/dev/null
195         fi
196 }
197
198 # The following 2 functions are debug-print() wrappers
199
200 debug-print-function() {
201         debug-print "${1}: entering function, parameters: ${*:2}"
202 }
203
204 debug-print-section() {
205         debug-print "now in section ${*}"
206 }
207
208 # Sources all eclasses in parameters
209 declare -ix ECLASS_DEPTH=0
210 inherit() {
211         ECLASS_DEPTH=$(($ECLASS_DEPTH + 1))
212         if [[ ${ECLASS_DEPTH} > 1 ]]; then
213                 debug-print "*** Multiple Inheritence (Level: ${ECLASS_DEPTH})"
214         fi
215
216         if [[ -n $ECLASS && -n ${!__export_funcs_var} ]] ; then
217                 echo "QA Notice: EXPORT_FUNCTIONS is called before inherit in" \
218                         "$ECLASS.eclass. For compatibility with <=portage-2.1.6.7," \
219                         "only call EXPORT_FUNCTIONS after inherit(s)." \
220                         | fmt -w 75 | while read -r ; do eqawarn "$REPLY" ; done
221         fi
222
223         local location
224         local olocation
225         local x
226
227         # These variables must be restored before returning.
228         local PECLASS=$ECLASS
229         local prev_export_funcs_var=$__export_funcs_var
230
231         local B_IUSE
232         local B_REQUIRED_USE
233         local B_DEPEND
234         local B_RDEPEND
235         local B_PDEPEND
236         while [ "$1" ]; do
237                 location="${ECLASSDIR}/${1}.eclass"
238                 olocation=""
239
240                 export ECLASS="$1"
241                 __export_funcs_var=__export_functions_$ECLASS_DEPTH
242                 unset $__export_funcs_var
243
244                 if [ "${EBUILD_PHASE}" != "depend" ] && \
245                         [[ ${EBUILD_PHASE} != *rm ]] && \
246                         [[ ${EMERGE_FROM} != "binary" ]] ; then
247                         # This is disabled in the *rm phases because they frequently give
248                         # false alarms due to INHERITED in /var/db/pkg being outdated
249                         # in comparison the the eclasses from the portage tree.
250                         if ! has $ECLASS $INHERITED $__INHERITED_QA_CACHE ; then
251                                 eqawarn "QA Notice: ECLASS '$ECLASS' inherited illegally in $CATEGORY/$PF $EBUILD_PHASE"
252                         fi
253                 fi
254
255                 # any future resolution code goes here
256                 if [ -n "$PORTDIR_OVERLAY" ]; then
257                         local overlay
258                         for overlay in ${PORTDIR_OVERLAY}; do
259                                 olocation="${overlay}/eclass/${1}.eclass"
260                                 if [ -e "$olocation" ]; then
261                                         location="${olocation}"
262                                         debug-print "  eclass exists: ${location}"
263                                 fi
264                         done
265                 fi
266                 debug-print "inherit: $1 -> $location"
267                 [ ! -e "$location" ] && die "${1}.eclass could not be found by inherit()"
268
269                 if [ "${location}" == "${olocation}" ] && \
270                         ! has "${location}" ${EBUILD_OVERLAY_ECLASSES} ; then
271                                 EBUILD_OVERLAY_ECLASSES="${EBUILD_OVERLAY_ECLASSES} ${location}"
272                 fi
273
274                 #We need to back up the value of DEPEND and RDEPEND to B_DEPEND and B_RDEPEND
275                 #(if set).. and then restore them after the inherit call.
276
277                 #turn off glob expansion
278                 set -f
279
280                 # Retain the old data and restore it later.
281                 unset B_IUSE B_REQUIRED_USE B_DEPEND B_RDEPEND B_PDEPEND
282                 [ "${IUSE+set}"       = set ] && B_IUSE="${IUSE}"
283                 [ "${REQUIRED_USE+set}" = set ] && B_REQUIRED_USE="${REQUIRED_USE}"
284                 [ "${DEPEND+set}"     = set ] && B_DEPEND="${DEPEND}"
285                 [ "${RDEPEND+set}"    = set ] && B_RDEPEND="${RDEPEND}"
286                 [ "${PDEPEND+set}"    = set ] && B_PDEPEND="${PDEPEND}"
287                 unset IUSE REQUIRED_USE DEPEND RDEPEND PDEPEND
288                 #turn on glob expansion
289                 set +f
290
291                 qa_source "$location" || die "died sourcing $location in inherit()"
292                 
293                 #turn off glob expansion
294                 set -f
295
296                 # If each var has a value, append it to the global variable E_* to
297                 # be applied after everything is finished. New incremental behavior.
298                 [ "${IUSE+set}"       = set ] && export E_IUSE="${E_IUSE} ${IUSE}"
299                 [ "${REQUIRED_USE+set}"       = set ] && export E_REQUIRED_USE="${E_REQUIRED_USE} ${REQUIRED_USE}"
300                 [ "${DEPEND+set}"     = set ] && export E_DEPEND="${E_DEPEND} ${DEPEND}"
301                 [ "${RDEPEND+set}"    = set ] && export E_RDEPEND="${E_RDEPEND} ${RDEPEND}"
302                 [ "${PDEPEND+set}"    = set ] && export E_PDEPEND="${E_PDEPEND} ${PDEPEND}"
303
304                 [ "${B_IUSE+set}"     = set ] && IUSE="${B_IUSE}"
305                 [ "${B_IUSE+set}"     = set ] || unset IUSE
306                 
307                 [ "${B_REQUIRED_USE+set}"     = set ] && REQUIRED_USE="${B_REQUIRED_USE}"
308                 [ "${B_REQUIRED_USE+set}"     = set ] || unset REQUIRED_USE
309
310                 [ "${B_DEPEND+set}"   = set ] && DEPEND="${B_DEPEND}"
311                 [ "${B_DEPEND+set}"   = set ] || unset DEPEND
312
313                 [ "${B_RDEPEND+set}"  = set ] && RDEPEND="${B_RDEPEND}"
314                 [ "${B_RDEPEND+set}"  = set ] || unset RDEPEND
315
316                 [ "${B_PDEPEND+set}"  = set ] && PDEPEND="${B_PDEPEND}"
317                 [ "${B_PDEPEND+set}"  = set ] || unset PDEPEND
318
319                 #turn on glob expansion
320                 set +f
321
322                 if [[ -n ${!__export_funcs_var} ]] ; then
323                         for x in ${!__export_funcs_var} ; do
324                                 debug-print "EXPORT_FUNCTIONS: $x -> ${ECLASS}_$x"
325                                 declare -F "${ECLASS}_$x" >/dev/null || \
326                                         die "EXPORT_FUNCTIONS: ${ECLASS}_$x is not defined"
327                                 eval "$x() { ${ECLASS}_$x \"\$@\" ; }" > /dev/null
328                         done
329                 fi
330                 unset $__export_funcs_var
331
332                 has $1 $INHERITED || export INHERITED="$INHERITED $1"
333
334                 shift
335         done
336         ((--ECLASS_DEPTH)) # Returns 1 when ECLASS_DEPTH reaches 0.
337         if (( ECLASS_DEPTH > 0 )) ; then
338                 export ECLASS=$PECLASS
339                 __export_funcs_var=$prev_export_funcs_var
340         else
341                 unset ECLASS __export_funcs_var
342         fi
343         return 0
344 }
345
346 # Exports stub functions that call the eclass's functions, thereby making them default.
347 # For example, if ECLASS="base" and you call "EXPORT_FUNCTIONS src_unpack", the following
348 # code will be eval'd:
349 # src_unpack() { base_src_unpack; }
350 EXPORT_FUNCTIONS() {
351         if [ -z "$ECLASS" ]; then
352                 die "EXPORT_FUNCTIONS without a defined ECLASS"
353         fi
354         eval $__export_funcs_var+=\" $*\"
355 }
356
357 # this is a function for removing any directory matching a passed in pattern from
358 # PATH
359 remove_path_entry() {
360         save_IFS
361         IFS=":"
362         stripped_path="${PATH}"
363         while [ -n "$1" ]; do
364                 cur_path=""
365                 for p in ${stripped_path}; do
366                         if [ "${p/${1}}" == "${p}" ]; then
367                                 cur_path="${cur_path}:${p}"
368                         fi
369                 done
370                 stripped_path="${cur_path#:*}"
371                 shift
372         done
373         restore_IFS
374         PATH="${stripped_path}"
375 }
376
377 # Set given variables unless these variable have been already set (e.g. during emerge
378 # invocation) to values different than values set in make.conf.
379 set_unless_changed() {
380         if [[ $# -lt 1 ]]; then
381                 die "${FUNCNAME}() requires at least 1 argument: VARIABLE=VALUE"
382         fi
383
384         local argument value variable
385         for argument in "$@"; do
386                 if [[ ${argument} != *=* ]]; then
387                         die "${FUNCNAME}(): Argument '${argument}' has incorrect syntax"
388                 fi
389                 variable="${argument%%=*}"
390                 value="${argument#*=}"
391                 if eval "[[ \${${variable}} == \$(env -u ${variable} portageq envvar ${variable}) ]]"; then
392                         eval "${variable}=\"\${value}\""
393                 fi
394         done
395 }
396
397 # Unset given variables unless these variable have been set (e.g. during emerge
398 # invocation) to values different than values set in make.conf.
399 unset_unless_changed() {
400         if [[ $# -lt 1 ]]; then
401                 die "${FUNCNAME}() requires at least 1 argument: VARIABLE"
402         fi
403
404         local variable
405         for variable in "$@"; do
406                 if eval "[[ \${${variable}} == \$(env -u ${variable} portageq envvar ${variable}) ]]"; then
407                         unset ${variable}
408                 fi
409         done
410 }
411
412 PORTAGE_BASHRCS_SOURCED=0
413
414 # @FUNCTION: source_all_bashrcs
415 # @DESCRIPTION:
416 # Source a relevant bashrc files and perform other miscellaneous
417 # environment initialization when appropriate.
418 #
419 # If EAPI is set then define functions provided by the current EAPI:
420 #
421 #  * default_* aliases for the current EAPI phase functions
422 #  * A "default" function which is an alias for the default phase
423 #    function for the current phase.
424 #
425 source_all_bashrcs() {
426         [[ $PORTAGE_BASHRCS_SOURCED = 1 ]] && return 0
427         PORTAGE_BASHRCS_SOURCED=1
428         local x
429
430         local OCC="${CC}" OCXX="${CXX}"
431
432         if [[ $EBUILD_PHASE != depend ]] ; then
433                 # source the existing profile.bashrcs.
434                 save_IFS
435                 IFS=$'\n'
436                 local path_array=($PROFILE_PATHS)
437                 restore_IFS
438                 for x in "${path_array[@]}" ; do
439                         [ -f "$x/profile.bashrc" ] && qa_source "$x/profile.bashrc"
440                 done
441         fi
442
443         # We assume if people are changing shopts in their bashrc they do so at their
444         # own peril.  This is the ONLY non-portage bit of code that can change shopts
445         # without a QA violation.
446         for x in "${PORTAGE_BASHRC}" "${PM_EBUILD_HOOK_DIR}"/${CATEGORY}/{${PN},${PN}:${SLOT},${P},${PF}}; do
447                 if [ -r "${x}" ]; then
448                         # If $- contains x, then tracing has already enabled elsewhere for some
449                         # reason.  We preserve it's state so as not to interfere.
450                         if [ "$PORTAGE_DEBUG" != "1" ] || [ "${-/x/}" != "$-" ]; then
451                                 source "${x}"
452                         else
453                                 set -x
454                                 source "${x}"
455                                 set +x
456                         fi
457                 fi
458         done
459
460         [ ! -z "${OCC}" ] && export CC="${OCC}"
461         [ ! -z "${OCXX}" ] && export CXX="${OCXX}"
462 }
463
464 # Hardcoded bash lists are needed for backward compatibility with
465 # <portage-2.1.4 since they assume that a newly installed version
466 # of ebuild.sh will work for pkg_postinst, pkg_prerm, and pkg_postrm
467 # when portage is upgrading itself.
468
469 PORTAGE_READONLY_METADATA="DEFINED_PHASES DEPEND DESCRIPTION
470         EAPI HOMEPAGE INHERITED IUSE REQUIRED_USE KEYWORDS LICENSE
471         PDEPEND PROVIDE RDEPEND RESTRICT SLOT SRC_URI"
472
473 PORTAGE_READONLY_VARS="D EBUILD EBUILD_PHASE \
474         EBUILD_SH_ARGS ECLASSDIR EMERGE_FROM FILESDIR MERGE_TYPE \
475         PM_EBUILD_HOOK_DIR \
476         PORTAGE_ACTUAL_DISTDIR PORTAGE_ARCHLIST PORTAGE_BASHRC  \
477         PORTAGE_BINPKG_FILE PORTAGE_BINPKG_TAR_OPTS PORTAGE_BINPKG_TMPFILE \
478         PORTAGE_BIN_PATH PORTAGE_BUILDDIR PORTAGE_BUNZIP2_COMMAND \
479         PORTAGE_BZIP2_COMMAND PORTAGE_COLORMAP PORTAGE_CONFIGROOT \
480         PORTAGE_DEBUG PORTAGE_DEPCACHEDIR PORTAGE_EBUILD_EXIT_FILE \
481         PORTAGE_GID PORTAGE_GRPNAME PORTAGE_INST_GID PORTAGE_INST_UID \
482         PORTAGE_IPC_DAEMON PORTAGE_IUSE PORTAGE_LOG_FILE \
483         PORTAGE_MUTABLE_FILTERED_VARS PORTAGE_PYM_PATH PORTAGE_PYTHON \
484         PORTAGE_READONLY_METADATA PORTAGE_READONLY_VARS \
485         PORTAGE_REPO_NAME PORTAGE_RESTRICT PORTAGE_SANDBOX_COMPAT_LEVEL \
486         PORTAGE_SAVED_READONLY_VARS PORTAGE_SIGPIPE_STATUS \
487         PORTAGE_TMPDIR PORTAGE_UPDATE_ENV PORTAGE_USERNAME \
488         PORTAGE_VERBOSE PORTAGE_WORKDIR_MODE PORTDIR PORTDIR_OVERLAY \
489         PROFILE_PATHS REPLACING_VERSIONS REPLACED_BY_VERSION T WORKDIR"
490
491 PORTAGE_SAVED_READONLY_VARS="A CATEGORY P PF PN PR PV PVR"
492
493 # Variables that portage sets but doesn't mark readonly.
494 # In order to prevent changed values from causing unexpected
495 # interference, they are filtered out of the environment when
496 # it is saved or loaded (any mutations do not persist).
497 PORTAGE_MUTABLE_FILTERED_VARS="AA HOSTNAME"
498
499 # @FUNCTION: filter_readonly_variables
500 # @DESCRIPTION: [--filter-sandbox] [--allow-extra-vars]
501 # Read an environment from stdin and echo to stdout while filtering variables
502 # with names that are known to cause interference:
503 #
504 #   * some specific variables for which bash does not allow assignment
505 #   * some specific variables that affect portage or sandbox behavior
506 #   * variable names that begin with a digit or that contain any
507 #     non-alphanumeric characters that are not be supported by bash
508 #
509 # --filter-sandbox causes all SANDBOX_* variables to be filtered, which
510 # is only desired in certain cases, such as during preprocessing or when
511 # saving environment.bz2 for a binary or installed package.
512 #
513 # --filter-features causes the special FEATURES variable to be filtered.
514 # Generally, we want it to persist between phases since the user might
515 # want to modify it via bashrc to enable things like splitdebug and
516 # installsources for specific packages. They should be able to modify it
517 # in pre_pkg_setup() and have it persist all the way through the install
518 # phase. However, if FEATURES exist inside environment.bz2 then they
519 # should be overridden by current settings.
520 #
521 # --filter-locale causes locale related variables such as LANG and LC_*
522 # variables to be filtered. These variables should persist between phases,
523 # in case they are modified by the ebuild. However, the current user
524 # settings should be used when loading the environment from a binary or
525 # installed package.
526 #
527 # --filter-path causes the PATH variable to be filtered. This variable
528 # should persist between phases, in case it is modified by the ebuild.
529 # However, old settings should be overridden when loading the
530 # environment from a binary or installed package.
531 #
532 # ---allow-extra-vars causes some extra vars to be allowd through, such
533 # as ${PORTAGE_SAVED_READONLY_VARS} and ${PORTAGE_MUTABLE_FILTERED_VARS}.
534 #
535 # In bash-3.2_p20+ an attempt to assign BASH_*, FUNCNAME, GROUPS or any
536 # readonly variable cause the shell to exit while executing the "source"
537 # builtin command. To avoid this problem, this function filters those
538 # variables out and discards them. See bug #190128.
539 filter_readonly_variables() {
540         local x filtered_vars
541         local readonly_bash_vars="BASHOPTS BASHPID DIRSTACK EUID
542                 FUNCNAME GROUPS PIPESTATUS PPID SHELLOPTS UID"
543         local bash_misc_vars="BASH BASH_.* COMP_WORDBREAKS HISTCMD
544                 HISTFILE HOSTNAME HOSTTYPE IFS LINENO MACHTYPE OLDPWD
545                 OPTERR OPTIND OSTYPE POSIXLY_CORRECT PS4 PWD RANDOM
546                 SECONDS SHELL SHLVL"
547         local filtered_sandbox_vars="SANDBOX_ACTIVE SANDBOX_BASHRC
548                 SANDBOX_DEBUG_LOG SANDBOX_DISABLED SANDBOX_LIB
549                 SANDBOX_LOG SANDBOX_ON"
550         local misc_garbage_vars="_portage_filter_opts"
551         filtered_vars="$readonly_bash_vars $bash_misc_vars
552                 $PORTAGE_READONLY_VARS $misc_garbage_vars"
553
554         # Don't filter/interfere with prefix variables unless they are
555         # supported by the current EAPI.
556         case "${EAPI:-0}" in
557                 0|1|2)
558                         ;;
559                 *)
560                         filtered_vars+=" ED EPREFIX EROOT"
561                         ;;
562         esac
563
564         if has --filter-sandbox $* ; then
565                 filtered_vars="${filtered_vars} SANDBOX_.*"
566         else
567                 filtered_vars="${filtered_vars} ${filtered_sandbox_vars}"
568         fi
569         if has --filter-features $* ; then
570                 filtered_vars="${filtered_vars} FEATURES PORTAGE_FEATURES"
571         fi
572         if has --filter-path $* ; then
573                 filtered_vars+=" PATH"
574         fi
575         if has --filter-locale $* ; then
576                 filtered_vars+=" LANG LC_ALL LC_COLLATE
577                         LC_CTYPE LC_MESSAGES LC_MONETARY
578                         LC_NUMERIC LC_PAPER LC_TIME"
579         fi
580         if ! has --allow-extra-vars $* ; then
581                 filtered_vars="
582                         ${filtered_vars}
583                         ${PORTAGE_SAVED_READONLY_VARS}
584                         ${PORTAGE_MUTABLE_FILTERED_VARS}
585                 "
586         fi
587
588         "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}"/filter-bash-environment.py "${filtered_vars}" || die "filter-bash-environment.py failed"
589 }
590
591 # @FUNCTION: preprocess_ebuild_env
592 # @DESCRIPTION:
593 # Filter any readonly variables from ${T}/environment, source it, and then
594 # save it via save_ebuild_env(). This process should be sufficient to prevent
595 # any stale variables or functions from an arbitrary environment from
596 # interfering with the current environment. This is useful when an existing
597 # environment needs to be loaded from a binary or installed package.
598 preprocess_ebuild_env() {
599         local _portage_filter_opts="--filter-features --filter-locale --filter-path --filter-sandbox"
600
601         # If environment.raw is present, this is a signal from the python side,
602         # indicating that the environment may contain stale FEATURES and
603         # SANDBOX_{DENY,PREDICT,READ,WRITE} variables that should be filtered out.
604         # Otherwise, we don't need to filter the environment.
605         [ -f "${T}/environment.raw" ] || return 0
606
607         filter_readonly_variables $_portage_filter_opts < "${T}"/environment \
608                 >> "$T/environment.filtered" || return $?
609         unset _portage_filter_opts
610         mv "${T}"/environment.filtered "${T}"/environment || return $?
611         rm -f "${T}/environment.success" || return $?
612         # WARNING: Code inside this subshell should avoid making assumptions
613         # about variables or functions after source "${T}"/environment has been
614         # called. Any variables that need to be relied upon should already be
615         # filtered out above.
616         (
617                 export SANDBOX_ON=1
618                 source "${T}/environment" || exit $?
619                 # We have to temporarily disable sandbox since the
620                 # SANDBOX_{DENY,READ,PREDICT,WRITE} values we've just loaded
621                 # may be unusable (triggering in spurious sandbox violations)
622                 # until we've merged them with our current values.
623                 export SANDBOX_ON=0
624
625                 # It's remotely possible that save_ebuild_env() has been overridden
626                 # by the above source command. To protect ourselves, we override it
627                 # here with our own version. ${PORTAGE_BIN_PATH} is safe to use here
628                 # because it's already filtered above.
629                 source "${PORTAGE_BIN_PATH}/isolated-functions.sh" || exit $?
630
631                 # Rely on save_ebuild_env() to filter out any remaining variables
632                 # and functions that could interfere with the current environment.
633                 save_ebuild_env || exit $?
634                 >> "$T/environment.success" || exit $?
635         ) > "${T}/environment.filtered"
636         local retval
637         if [ -e "${T}/environment.success" ] ; then
638                 filter_readonly_variables --filter-features < \
639                         "${T}/environment.filtered" > "${T}/environment"
640                 retval=$?
641         else
642                 retval=1
643         fi
644         rm -f "${T}"/environment.{filtered,raw,success}
645         return ${retval}
646 }
647
648 # === === === === === === === === === === === === === === === === === ===
649 # === === === === === functions end, main part begins === === === === ===
650 # === === === === === functions end, main part begins === === === === ===
651 # === === === === === functions end, main part begins === === === === ===
652 # === === === === === === === === === === === === === === === === === ===
653
654 export SANDBOX_ON="1"
655 export S=${WORKDIR}/${P}
656
657 unset E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND
658
659 # Turn of extended glob matching so that g++ doesn't get incorrectly matched.
660 shopt -u extglob
661
662 if [[ ${EBUILD_PHASE} == depend ]] ; then
663         QA_INTERCEPTORS="awk bash cc egrep equery fgrep g++
664                 gawk gcc grep javac java-config nawk perl
665                 pkg-config python python-config sed"
666 elif [[ ${EBUILD_PHASE} == clean* ]] ; then
667         unset QA_INTERCEPTORS
668 else
669         QA_INTERCEPTORS="autoconf automake aclocal libtoolize"
670 fi
671 # level the QA interceptors if we're in depend
672 if [[ -n ${QA_INTERCEPTORS} ]] ; then
673         for BIN in ${QA_INTERCEPTORS}; do
674                 BIN_PATH=$(type -Pf ${BIN})
675                 if [ "$?" != "0" ]; then
676                         BODY="echo \"*** missing command: ${BIN}\" >&2; return 127"
677                 else
678                         BODY="${BIN_PATH} \"\$@\"; return \$?"
679                 fi
680                 if [[ ${EBUILD_PHASE} == depend ]] ; then
681                         FUNC_SRC="${BIN}() {
682                                 if [ \$ECLASS_DEPTH -gt 0 ]; then
683                                         eqawarn \"QA Notice: '${BIN}' called in global scope: eclass \${ECLASS}\"
684                                 else
685                                         eqawarn \"QA Notice: '${BIN}' called in global scope: \${CATEGORY}/\${PF}\"
686                                 fi
687                         ${BODY}
688                         }"
689                 elif has ${BIN} autoconf automake aclocal libtoolize ; then
690                         FUNC_SRC="${BIN}() {
691                                 if ! has \${FUNCNAME[1]} eautoreconf eaclocal _elibtoolize \\
692                                         eautoheader eautoconf eautomake autotools_run_tool \\
693                                         autotools_check_macro autotools_get_subdirs \\
694                                         autotools_get_auxdir ; then
695                                         eqawarn \"QA Notice: '${BIN}' called by \${FUNCNAME[1]}: \${CATEGORY}/\${PF}\"
696                                         eqawarn \"Use autotools.eclass instead of calling '${BIN}' directly.\"
697                                 fi
698                         ${BODY}
699                         }"
700                 else
701                         FUNC_SRC="${BIN}() {
702                                 eqawarn \"QA Notice: '${BIN}' called by \${FUNCNAME[1]}: \${CATEGORY}/\${PF}\"
703                         ${BODY}
704                         }"
705                 fi
706                 eval "$FUNC_SRC" || echo "error creating QA interceptor ${BIN}" >&2
707         done
708         unset BIN_PATH BIN BODY FUNC_SRC
709 fi
710
711 # Subshell/helper die support (must export for the die helper).
712 export EBUILD_MASTER_PID=$BASHPID
713 trap 'exit 1' SIGTERM
714
715 if [[ $EBUILD_PHASE != depend ]] ; then
716         source "${PORTAGE_BIN_PATH}/phase-functions.sh"
717         source "${PORTAGE_BIN_PATH}/phase-helpers.sh"
718 else
719         # These dummy functions are for things that are likely to be called
720         # in global scope, even though they are completely useless during
721         # the "depend" phase.
722         for x in diropts docompress exeopts insopts \
723                 keepdir libopts use useq usev use_with use_enable ; do
724                 eval "${x}() { : ; }"
725         done
726         # These functions die because calls to them during the "depend" phase
727         # are considered to be severe QA violations.
728         for x in best_version has_version ; do
729                 eval "${x}() { die \"\${FUNCNAME} calls are not allowed in global scope\"; }"
730         done
731         unset x
732 fi
733
734 if ! has "$EBUILD_PHASE" clean cleanrm depend && \
735         [ -f "${T}"/environment ] ; then
736         # The environment may have been extracted from environment.bz2 or
737         # may have come from another version of ebuild.sh or something.
738         # In any case, preprocess it to prevent any potential interference.
739         preprocess_ebuild_env || \
740                 die "error processing environment"
741         # Colon separated SANDBOX_* variables need to be cumulative.
742         for x in SANDBOX_DENY SANDBOX_READ SANDBOX_PREDICT SANDBOX_WRITE ; do
743                 export PORTAGE_${x}=${!x}
744         done
745         PORTAGE_SANDBOX_ON=${SANDBOX_ON}
746         export SANDBOX_ON=1
747         source "${T}"/environment || \
748                 die "error sourcing environment"
749         # We have to temporarily disable sandbox since the
750         # SANDBOX_{DENY,READ,PREDICT,WRITE} values we've just loaded
751         # may be unusable (triggering in spurious sandbox violations)
752         # until we've merged them with our current values.
753         export SANDBOX_ON=0
754         for x in SANDBOX_DENY SANDBOX_PREDICT SANDBOX_READ SANDBOX_WRITE ; do
755                 y="PORTAGE_${x}"
756                 if [ -z "${!x}" ] ; then
757                         export ${x}=${!y}
758                 elif [ -n "${!y}" ] && [ "${!y}" != "${!x}" ] ; then
759                         # filter out dupes
760                         export ${x}=$(printf "${!y}:${!x}" | tr ":" "\0" | \
761                                 sort -z -u | tr "\0" ":")
762                 fi
763                 export ${x}=${!x%:}
764                 unset PORTAGE_${x}
765         done
766         unset x y
767         export SANDBOX_ON=${PORTAGE_SANDBOX_ON}
768         unset PORTAGE_SANDBOX_ON
769         [[ -n $EAPI ]] || EAPI=0
770 fi
771
772 if ! has "$EBUILD_PHASE" clean cleanrm ; then
773         if [[ $EBUILD_PHASE = depend || ! -f $T/environment || \
774                 -f $PORTAGE_BUILDDIR/.ebuild_changed ]] || \
775                 has noauto $FEATURES ; then
776                 # The bashrcs get an opportunity here to set aliases that will be expanded
777                 # during sourcing of ebuilds and eclasses.
778                 source_all_bashrcs
779
780                 # When EBUILD_PHASE != depend, INHERITED comes pre-initialized
781                 # from cache. In order to make INHERITED content independent of
782                 # EBUILD_PHASE during inherit() calls, we unset INHERITED after
783                 # we make a backup copy for QA checks.
784                 __INHERITED_QA_CACHE=$INHERITED
785
786                 # *DEPEND and IUSE will be set during the sourcing of the ebuild.
787                 # In order to ensure correct interaction between ebuilds and
788                 # eclasses, they need to be unset before this process of
789                 # interaction begins.
790                 unset DEPEND RDEPEND PDEPEND INHERITED IUSE REQUIRED_USE \
791                         ECLASS E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND
792
793                 if [[ $PORTAGE_DEBUG != 1 || ${-/x/} != $- ]] ; then
794                         source "$EBUILD" || die "error sourcing ebuild"
795                 else
796                         set -x
797                         source "$EBUILD" || die "error sourcing ebuild"
798                         set +x
799                 fi
800
801                 if [[ "${EBUILD_PHASE}" != "depend" ]] ; then
802                         RESTRICT=${PORTAGE_RESTRICT}
803                         [[ -e $PORTAGE_BUILDDIR/.ebuild_changed ]] && \
804                         rm "$PORTAGE_BUILDDIR/.ebuild_changed"
805                 fi
806
807                 [[ -n $EAPI ]] || EAPI=0
808
809                 if has "$EAPI" 0 1 2 3 3_pre2 ; then
810                         export RDEPEND=${RDEPEND-${DEPEND}}
811                         debug-print "RDEPEND: not set... Setting to: ${DEPEND}"
812                 fi
813
814                 # add in dependency info from eclasses
815                 IUSE="${IUSE} ${E_IUSE}"
816                 DEPEND="${DEPEND} ${E_DEPEND}"
817                 RDEPEND="${RDEPEND} ${E_RDEPEND}"
818                 PDEPEND="${PDEPEND} ${E_PDEPEND}"
819                 REQUIRED_USE="${REQUIRED_USE} ${E_REQUIRED_USE}"
820                 
821                 unset ECLASS E_IUSE E_REQUIRED_USE E_DEPEND E_RDEPEND E_PDEPEND \
822                         __INHERITED_QA_CACHE
823
824                 # alphabetically ordered by $EBUILD_PHASE value
825                 case "$EAPI" in
826                         0|1)
827                                 _valid_phases="src_compile pkg_config pkg_info src_install
828                                         pkg_nofetch pkg_postinst pkg_postrm pkg_preinst pkg_prerm
829                                         pkg_setup src_test src_unpack"
830                                 ;;
831                         2|3|3_pre2)
832                                 _valid_phases="src_compile pkg_config src_configure pkg_info
833                                         src_install pkg_nofetch pkg_postinst pkg_postrm pkg_preinst
834                                         src_prepare pkg_prerm pkg_setup src_test src_unpack"
835                                 ;;
836                         *)
837                                 _valid_phases="src_compile pkg_config src_configure pkg_info
838                                         src_install pkg_nofetch pkg_postinst pkg_postrm pkg_preinst
839                                         src_prepare pkg_prerm pkg_pretend pkg_setup src_test src_unpack"
840                                 ;;
841                 esac
842
843                 DEFINED_PHASES=
844                 for _f in $_valid_phases ; do
845                         if declare -F $_f >/dev/null ; then
846                                 _f=${_f#pkg_}
847                                 DEFINED_PHASES+=" ${_f#src_}"
848                         fi
849                 done
850                 [[ -n $DEFINED_PHASES ]] || DEFINED_PHASES=-
851
852                 unset _f _valid_phases
853
854                 if [[ $EBUILD_PHASE != depend ]] ; then
855
856                         case "$EAPI" in
857                                 0|1|2|3)
858                                         _ebuild_helpers_path="$PORTAGE_BIN_PATH/ebuild-helpers"
859                                         ;;
860                                 *)
861                                         _ebuild_helpers_path="$PORTAGE_BIN_PATH/ebuild-helpers/4:$PORTAGE_BIN_PATH/ebuild-helpers"
862                                         ;;
863                         esac
864
865                         PATH=$_ebuild_helpers_path:$PREROOTPATH${PREROOTPATH:+:}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin${ROOTPATH:+:}$ROOTPATH
866                         unset _ebuild_helpers_path
867
868                         # Use default ABI libdir in accordance with bug #355283.
869                         x=LIBDIR_${DEFAULT_ABI}
870                         [[ -n $DEFAULT_ABI && -n ${!x} ]] && x=${!x} || x=lib
871
872                         if has distcc $FEATURES ; then
873                                 PATH="/usr/$x/distcc/bin:$PATH"
874                                 [[ -n $DISTCC_LOG ]] && addwrite "${DISTCC_LOG%/*}"
875                         fi
876
877                         if has ccache $FEATURES ; then
878                                 PATH="/usr/$x/ccache/bin:$PATH"
879
880                                 if [[ -n $CCACHE_DIR ]] ; then
881                                         addread "$CCACHE_DIR"
882                                         addwrite "$CCACHE_DIR"
883                                 fi
884
885                                 [[ -n $CCACHE_SIZE ]] && ccache -M $CCACHE_SIZE &> /dev/null
886                         fi
887
888                         unset x
889
890                         if [[ -n $QA_PREBUILT ]] ; then
891
892                                 # these ones support fnmatch patterns
893                                 QA_EXECSTACK+=" $QA_PREBUILT"
894                                 QA_TEXTRELS+=" $QA_PREBUILT"
895                                 QA_WX_LOAD+=" $QA_PREBUILT"
896
897                                 # these ones support regular expressions, so translate
898                                 # fnmatch patterns to regular expressions
899                                 for x in QA_DT_HASH QA_DT_NEEDED QA_PRESTRIPPED QA_SONAME ; do
900                                         if [[ $(declare -p $x 2>/dev/null) = declare\ -a* ]] ; then
901                                                 eval "$x=(\"\${$x[@]}\" ${QA_PREBUILT//\*/.*})"
902                                         else
903                                                 eval "$x+=\" ${QA_PREBUILT//\*/.*}\""
904                                         fi
905                                 done
906
907                                 unset x
908                         fi
909
910                         # This needs to be exported since prepstrip is a separate shell script.
911                         [[ -n $QA_PRESTRIPPED ]] && export QA_PRESTRIPPED
912                         eval "[[ -n \$QA_PRESTRIPPED_${ARCH/-/_} ]] && \
913                                 export QA_PRESTRIPPED_${ARCH/-/_}"
914                 fi
915         fi
916 fi
917
918 # unset USE_EXPAND variables that contain only the special "*" token
919 for x in ${USE_EXPAND} ; do
920         [ "${!x}" == "*" ] && unset ${x}
921 done
922 unset x
923
924 if has nostrip ${FEATURES} ${RESTRICT} || has strip ${RESTRICT}
925 then
926         export DEBUGBUILD=1
927 fi
928
929 if [[ $EBUILD_PHASE = depend ]] ; then
930         export SANDBOX_ON="0"
931         set -f
932
933         if [ -n "${dbkey}" ] ; then
934                 if [ ! -d "${dbkey%/*}" ]; then
935                         install -d -g ${PORTAGE_GID} -m2775 "${dbkey%/*}"
936                 fi
937                 # Make it group writable. 666&~002==664
938                 umask 002
939         fi
940
941         auxdbkeys="DEPEND RDEPEND SLOT SRC_URI RESTRICT HOMEPAGE LICENSE
942                 DESCRIPTION KEYWORDS INHERITED IUSE REQUIRED_USE PDEPEND PROVIDE EAPI
943                 PROPERTIES DEFINED_PHASES UNUSED_05 UNUSED_04
944                 UNUSED_03 UNUSED_02 UNUSED_01"
945
946         #the extra $(echo) commands remove newlines
947         [ -n "${EAPI}" ] || EAPI=0
948
949         if [ -n "${dbkey}" ] ; then
950                 > "${dbkey}"
951                 for f in ${auxdbkeys} ; do
952                         echo $(echo ${!f}) >> "${dbkey}" || exit $?
953                 done
954         else
955                 for f in ${auxdbkeys} ; do
956                         echo $(echo ${!f}) 1>&9 || exit $?
957                 done
958                 exec 9>&-
959         fi
960         set +f
961 else
962         # Note: readonly variables interfere with preprocess_ebuild_env(), so
963         # declare them only after it has already run.
964         declare -r $PORTAGE_READONLY_METADATA $PORTAGE_READONLY_VARS
965         case "$EAPI" in
966                 0|1|2)
967                         ;;
968                 *)
969                         declare -r ED EPREFIX EROOT
970                         ;;
971         esac
972
973         if [[ -n $EBUILD_SH_ARGS ]] ; then
974                 (
975                         # Don't allow subprocesses to inherit the pipe which
976                         # emerge uses to monitor ebuild.sh.
977                         exec 9>&-
978                         ebuild_main ${EBUILD_SH_ARGS}
979                         exit 0
980                 )
981                 exit $?
982         fi
983 fi
984
985 # Do not exit when ebuild.sh is sourced by other scripts.
986 true