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