egencache: portage.util._argparse
[portage.git] / bin / phase-helpers.sh
1 #!/bin/bash
2 # Copyright 1999-2013 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 export DESTTREE=/usr
6 export INSDESTTREE=""
7 export _E_EXEDESTTREE_=""
8 export _E_DOCDESTTREE_=""
9 export INSOPTIONS="-m0644"
10 export EXEOPTIONS="-m0755"
11 export LIBOPTIONS="-m0644"
12 export DIROPTIONS="-m0755"
13 export MOPREFIX=${PN}
14 declare -a PORTAGE_DOCOMPRESS=( /usr/share/{doc,info,man} )
15 declare -a PORTAGE_DOCOMPRESS_SKIP=( /usr/share/doc/${PF}/html )
16
17 into() {
18         if [ "$1" == "/" ]; then
19                 export DESTTREE=""
20         else
21                 export DESTTREE=$1
22                 if ! ___eapi_has_prefix_variables; then
23                         local ED=${D}
24                 fi
25                 if [ ! -d "${ED}${DESTTREE}" ]; then
26                         install -d "${ED}${DESTTREE}"
27                         local ret=$?
28                         if [[ $ret -ne 0 ]] ; then
29                                 __helpers_die "${FUNCNAME[0]} failed"
30                                 return $ret
31                         fi
32                 fi
33         fi
34 }
35
36 insinto() {
37         if [ "$1" == "/" ]; then
38                 export INSDESTTREE=""
39         else
40                 export INSDESTTREE=$1
41                 if ! ___eapi_has_prefix_variables; then
42                         local ED=${D}
43                 fi
44                 if [ ! -d "${ED}${INSDESTTREE}" ]; then
45                         install -d "${ED}${INSDESTTREE}"
46                         local ret=$?
47                         if [[ $ret -ne 0 ]] ; then
48                                 __helpers_die "${FUNCNAME[0]} failed"
49                                 return $ret
50                         fi
51                 fi
52         fi
53 }
54
55 exeinto() {
56         if [ "$1" == "/" ]; then
57                 export _E_EXEDESTTREE_=""
58         else
59                 export _E_EXEDESTTREE_="$1"
60                 if ! ___eapi_has_prefix_variables; then
61                         local ED=${D}
62                 fi
63                 if [ ! -d "${ED}${_E_EXEDESTTREE_}" ]; then
64                         install -d "${ED}${_E_EXEDESTTREE_}"
65                         local ret=$?
66                         if [[ $ret -ne 0 ]] ; then
67                                 __helpers_die "${FUNCNAME[0]} failed"
68                                 return $ret
69                         fi
70                 fi
71         fi
72 }
73
74 docinto() {
75         if [ "$1" == "/" ]; then
76                 export _E_DOCDESTTREE_=""
77         else
78                 export _E_DOCDESTTREE_="$1"
79                 if ! ___eapi_has_prefix_variables; then
80                         local ED=${D}
81                 fi
82                 if [ ! -d "${ED}usr/share/doc/${PF}/${_E_DOCDESTTREE_}" ]; then
83                         install -d "${ED}usr/share/doc/${PF}/${_E_DOCDESTTREE_}"
84                         local ret=$?
85                         if [[ $ret -ne 0 ]] ; then
86                                 __helpers_die "${FUNCNAME[0]} failed"
87                                 return $ret
88                         fi
89                 fi
90         fi
91 }
92
93 insopts() {
94         export INSOPTIONS="$@"
95
96         # `install` should never be called with '-s' ...
97         has -s ${INSOPTIONS} && die "Never call insopts() with -s"
98 }
99
100 diropts() {
101         export DIROPTIONS="$@"
102 }
103
104 exeopts() {
105         export EXEOPTIONS="$@"
106
107         # `install` should never be called with '-s' ...
108         has -s ${EXEOPTIONS} && die "Never call exeopts() with -s"
109 }
110
111 libopts() {
112         export LIBOPTIONS="$@"
113
114         # `install` should never be called with '-s' ...
115         has -s ${LIBOPTIONS} && die "Never call libopts() with -s"
116 }
117
118 docompress() {
119         ___eapi_has_docompress || die "'docompress' not supported in this EAPI"
120
121         local f g
122         if [[ $1 = "-x" ]]; then
123                 shift
124                 for f; do
125                         f=$(__strip_duplicate_slashes "${f}"); f=${f%/}
126                         [[ ${f:0:1} = / ]] || f="/${f}"
127                         for g in "${PORTAGE_DOCOMPRESS_SKIP[@]}"; do
128                                 [[ ${f} = "${g}" ]] && continue 2
129                         done
130                         PORTAGE_DOCOMPRESS_SKIP[${#PORTAGE_DOCOMPRESS_SKIP[@]}]=${f}
131                 done
132         else
133                 for f; do
134                         f=$(__strip_duplicate_slashes "${f}"); f=${f%/}
135                         [[ ${f:0:1} = / ]] || f="/${f}"
136                         for g in "${PORTAGE_DOCOMPRESS[@]}"; do
137                                 [[ ${f} = "${g}" ]] && continue 2
138                         done
139                         PORTAGE_DOCOMPRESS[${#PORTAGE_DOCOMPRESS[@]}]=${f}
140                 done
141         fi
142 }
143
144 # adds ".keep" files so that dirs aren't auto-cleaned
145 keepdir() {
146         dodir "$@"
147         local x
148         if ! ___eapi_has_prefix_variables; then
149                 local ED=${D}
150         fi
151         if [ "$1" == "-R" ] || [ "$1" == "-r" ]; then
152                 shift
153                 find "$@" -type d -printf "${ED}%p/.keep_${CATEGORY}_${PN}-${SLOT%/*}\n" \
154                         | tr "\n" "\0" | \
155                         while read -r -d $'\0' ; do
156                                 >> "$REPLY" || \
157                                         die "Failed to recursively create .keep files"
158                         done
159         else
160                 for x in "$@"; do
161                         >> "${ED}${x}/.keep_${CATEGORY}_${PN}-${SLOT%/*}" || \
162                                 die "Failed to create .keep in ${ED}${x}"
163                 done
164         fi
165 }
166
167
168 useq() {
169         has $EBUILD_PHASE prerm postrm || eqawarn \
170                 "QA Notice: The 'useq' function is deprecated (replaced by 'use')"
171         use ${1}
172 }
173
174 usev() {
175         if use ${1}; then
176                 echo "${1#!}"
177                 return 0
178         fi
179         return 1
180 }
181
182 if ___eapi_has_usex; then
183         usex() {
184                 if use "$1"; then
185                         echo "${2-yes}$4"
186                 else
187                         echo "${3-no}$5"
188                 fi
189                 return 0
190         }
191 fi
192
193 use() {
194         local u=$1
195         local found=0
196
197         # if we got something like '!flag', then invert the return value
198         if [[ ${u:0:1} == "!" ]] ; then
199                 u=${u:1}
200                 found=1
201         fi
202
203         if [[ $EBUILD_PHASE = depend ]] ; then
204                 # TODO: Add a registration interface for eclasses to register
205                 # any number of phase hooks, so that global scope eclass
206                 # initialization can by migrated to phase hooks in new EAPIs.
207                 # Example: add_phase_hook before pkg_setup $ECLASS_pre_pkg_setup
208                 #if [[ -n $EAPI ]] && ! has "$EAPI" 0 1 2 3 ; then
209                 #       die "use() called during invalid phase: $EBUILD_PHASE"
210                 #fi
211                 true
212
213         # Make sure we have this USE flag in IUSE, but exempt binary
214         # packages for API consumers like Entropy which do not require
215         # a full profile with IUSE_IMPLICIT and stuff (see bug #456830).
216         elif [[ -n $PORTAGE_IUSE && -n $EBUILD_PHASE &&
217                 -n $PORTAGE_INTERNAL_CALLER ]] ; then
218                 if [[ ! $u =~ $PORTAGE_IUSE ]] ; then
219                         if [[ ! ${EAPI} =~ ^(0|1|2|3|4|4-python|4-slot-abi)$ ]] ; then
220                                 # This is only strict starting with EAPI 5, since implicit IUSE
221                                 # is not well defined for earlier EAPIs (see bug #449708).
222                                 die "USE Flag '${u}' not in IUSE for ${CATEGORY}/${PF}"
223                         fi
224                         eqawarn "QA Notice: USE Flag '${u}' not" \
225                                 "in IUSE for ${CATEGORY}/${PF}"
226                 fi
227         fi
228
229         local IFS=$' \t\n' prev_shopts=$- ret
230         set -f
231         if has ${u} ${USE} ; then
232                 ret=${found}
233         else
234                 ret=$((!found))
235         fi
236         [[ ${prev_shopts} == *f* ]] || set +f
237         return ${ret}
238 }
239
240 use_with() {
241         if [ -z "$1" ]; then
242                 echo "!!! use_with() called without a parameter." >&2
243                 echo "!!! use_with <USEFLAG> [<flagname> [value]]" >&2
244                 return 1
245         fi
246
247         if ___eapi_use_enable_and_use_with_support_empty_third_argument; then
248                 local UW_SUFFIX=${3+=$3}
249         else
250                 local UW_SUFFIX=${3:+=$3}
251         fi
252         local UWORD=${2:-$1}
253
254         if use $1; then
255                 echo "--with-${UWORD}${UW_SUFFIX}"
256         else
257                 echo "--without-${UWORD}"
258         fi
259         return 0
260 }
261
262 use_enable() {
263         if [ -z "$1" ]; then
264                 echo "!!! use_enable() called without a parameter." >&2
265                 echo "!!! use_enable <USEFLAG> [<flagname> [value]]" >&2
266                 return 1
267         fi
268
269         if ___eapi_use_enable_and_use_with_support_empty_third_argument; then
270                 local UE_SUFFIX=${3+=$3}
271         else
272                 local UE_SUFFIX=${3:+=$3}
273         fi
274         local UWORD=${2:-$1}
275
276         if use $1; then
277                 echo "--enable-${UWORD}${UE_SUFFIX}"
278         else
279                 echo "--disable-${UWORD}"
280         fi
281         return 0
282 }
283
284 unpack() {
285         local srcdir
286         local x
287         local y
288         local suffix
289         local myfail
290         local eapi=${EAPI:-0}
291         [ -z "$*" ] && die "Nothing passed to the 'unpack' command"
292
293         for x in "$@"; do
294                 __vecho ">>> Unpacking ${x} to ${PWD}"
295                 suffix=${x##*.}
296                 suffix=$(LC_ALL=C tr "[:upper:]" "[:lower:]" <<< "${suffix}")
297                 y=${x%.*}
298                 y=${y##*.}
299                 y=$(LC_ALL=C tr "[:upper:]" "[:lower:]" <<< "${y}")
300
301                 if [[ ${x} == "./"* ]] ; then
302                         srcdir=""
303                 elif [[ ${x} == ${DISTDIR%/}/* ]] ; then
304                         die "Arguments to unpack() cannot begin with \${DISTDIR}."
305                 elif [[ ${x} == "/"* ]] ; then
306                         die "Arguments to unpack() cannot be absolute"
307                 else
308                         srcdir="${DISTDIR}/"
309                 fi
310                 [[ ! -s ${srcdir}${x} ]] && die "${x} does not exist"
311
312                 __unpack_tar() {
313                         if [ "${y}" == "tar" ]; then
314                                 $1 -c -- "$srcdir$x" | tar xof -
315                                 __assert_sigpipe_ok "$myfail"
316                         else
317                                 local cwd_dest=${x##*/}
318                                 cwd_dest=${cwd_dest%.*}
319                                 $1 -c -- "${srcdir}${x}" > "${cwd_dest}" || die "$myfail"
320                         fi
321                 }
322
323                 myfail="failure unpacking ${x}"
324                 case "${suffix}" in
325                         tar)
326                                 tar xof "$srcdir$x" || die "$myfail"
327                                 ;;
328                         tgz)
329                                 tar xozf "$srcdir$x" || die "$myfail"
330                                 ;;
331                         tbz|tbz2)
332                                 ${PORTAGE_BUNZIP2_COMMAND:-${PORTAGE_BZIP2_COMMAND} -d} -c -- "$srcdir$x" | tar xof -
333                                 __assert_sigpipe_ok "$myfail"
334                                 ;;
335                         zip|jar)
336                                 # unzip will interactively prompt under some error conditions,
337                                 # as reported in bug #336285
338                                 ( set +x ; while true ; do echo n || break ; done ) | \
339                                 unzip -qo "${srcdir}${x}" || die "$myfail"
340                                 ;;
341                         gz|z)
342                                 __unpack_tar "gzip -d"
343                                 ;;
344                         bz2|bz)
345                                 __unpack_tar "${PORTAGE_BUNZIP2_COMMAND:-${PORTAGE_BZIP2_COMMAND} -d}"
346                                 ;;
347                         7z)
348                                 local my_output
349                                 my_output="$(7z x -y "${srcdir}${x}")"
350                                 if [ $? -ne 0 ]; then
351                                         echo "${my_output}" >&2
352                                         die "$myfail"
353                                 fi
354                                 ;;
355                         rar)
356                                 unrar x -idq -o+ "${srcdir}${x}" || die "$myfail"
357                                 ;;
358                         lha|lzh)
359                                 lha xfq "${srcdir}${x}" || die "$myfail"
360                                 ;;
361                         a)
362                                 ar x "${srcdir}${x}" || die "$myfail"
363                                 ;;
364                         deb)
365                                 # Unpacking .deb archives can not always be done with
366                                 # `ar`.  For instance on AIX this doesn't work out.  If
367                                 # we have `deb2targz` installed, prefer it over `ar` for
368                                 # that reason.  We just make sure on AIX `deb2targz` is
369                                 # installed.
370                                 if type -P deb2targz > /dev/null; then
371                                         y=${x##*/}
372                                         local created_symlink=0
373                                         if [ ! "$srcdir$x" -ef "$y" ] ; then
374                                                 # deb2targz always extracts into the same directory as
375                                                 # the source file, so create a symlink in the current
376                                                 # working directory if necessary.
377                                                 ln -sf "$srcdir$x" "$y" || die "$myfail"
378                                                 created_symlink=1
379                                         fi
380                                         deb2targz "$y" || die "$myfail"
381                                         if [ $created_symlink = 1 ] ; then
382                                                 # Clean up the symlink so the ebuild
383                                                 # doesn't inadvertently install it.
384                                                 rm -f "$y"
385                                         fi
386                                         mv -f "${y%.deb}".tar.gz data.tar.gz || die "$myfail"
387                                 else
388                                         ar x "$srcdir$x" || die "$myfail"
389                                 fi
390                                 ;;
391                         lzma)
392                                 __unpack_tar "lzma -d"
393                                 ;;
394                         xz)
395                                 if ___eapi_unpack_supports_xz; then
396                                         __unpack_tar "xz -d"
397                                 else
398                                         __vecho "unpack ${x}: file format not recognized. Ignoring."
399                                 fi
400                                 ;;
401                         *)
402                                 __vecho "unpack ${x}: file format not recognized. Ignoring."
403                                 ;;
404                 esac
405         done
406         # Do not chmod '.' since it's probably ${WORKDIR} and PORTAGE_WORKDIR_MODE
407         # should be preserved.
408         find . -mindepth 1 -maxdepth 1 ! -type l -print0 | \
409                 ${XARGS} -0 chmod -fR a+rX,u+w,g-w,o-w
410 }
411
412 econf() {
413         local x
414
415         if ! ___eapi_has_prefix_variables; then
416                 local EPREFIX=
417         fi
418
419         __hasg() {
420                 local x s=$1
421                 shift
422                 for x ; do [[ ${x} == ${s} ]] && echo "${x}" && return 0 ; done
423                 return 1
424         }
425
426         __hasgq() { __hasg "$@" >/dev/null ; }
427
428         local phase_func=$(__ebuild_arg_to_phase "$EBUILD_PHASE")
429         if [[ -n $phase_func ]] ; then
430                 if ! ___eapi_has_src_configure; then
431                         [[ $phase_func != src_compile ]] && \
432                                 eqawarn "QA Notice: econf called in" \
433                                         "$phase_func instead of src_compile"
434                 else
435                         [[ $phase_func != src_configure ]] && \
436                                 eqawarn "QA Notice: econf called in" \
437                                         "$phase_func instead of src_configure"
438                 fi
439         fi
440
441         : ${ECONF_SOURCE:=.}
442         if [ -x "${ECONF_SOURCE}/configure" ]; then
443                 if [[ -n $CONFIG_SHELL && \
444                         "$(head -n1 "$ECONF_SOURCE/configure")" =~ ^'#!'[[:space:]]*/bin/sh([[:space:]]|$) ]] ; then
445                         # preserve timestamp, see bug #440304
446                         touch -r "$ECONF_SOURCE/configure" "$ECONF_SOURCE/configure._portage_tmp_.$$" || die
447                         sed -e "1s:^#![[:space:]]*/bin/sh:#!$CONFIG_SHELL:" -i "$ECONF_SOURCE/configure" || \
448                                 die "Substition of shebang in '$ECONF_SOURCE/configure' failed"
449                         touch -r "$ECONF_SOURCE/configure._portage_tmp_.$$" "$ECONF_SOURCE/configure" || die
450                         rm -f "$ECONF_SOURCE/configure._portage_tmp_.$$"
451                 fi
452                 if [ -e "${EPREFIX}"/usr/share/gnuconfig/ ]; then
453                         find "${WORKDIR}" -type f '(' \
454                         -name config.guess -o -name config.sub ')' -print0 | \
455                         while read -r -d $'\0' x ; do
456                                 __vecho " * econf: updating ${x/${WORKDIR}\/} with ${EPREFIX}/usr/share/gnuconfig/${x##*/}"
457                                 cp -f "${EPREFIX}"/usr/share/gnuconfig/"${x##*/}" "${x}"
458                         done
459                 fi
460
461                 if ___eapi_econf_passes_--disable-dependency-tracking || ___eapi_econf_passes_--disable-silent-rules; then
462                         local conf_help=$("${ECONF_SOURCE}/configure" --help 2>/dev/null)
463
464                         if ___eapi_econf_passes_--disable-dependency-tracking; then
465                                 case "${conf_help}" in
466                                         *--disable-dependency-tracking*)
467                                                 set -- --disable-dependency-tracking "$@"
468                                                 ;;
469                                 esac
470                         fi
471
472                         if ___eapi_econf_passes_--disable-silent-rules; then
473                                 case "${conf_help}" in
474                                         *--disable-silent-rules*)
475                                                 set -- --disable-silent-rules "$@"
476                                                 ;;
477                                 esac
478                         fi
479                 fi
480
481                 # if the profile defines a location to install libs to aside from default, pass it on.
482                 # if the ebuild passes in --libdir, they're responsible for the conf_libdir fun.
483                 local CONF_LIBDIR LIBDIR_VAR="LIBDIR_${ABI}"
484                 if [[ -n ${ABI} && -n ${!LIBDIR_VAR} ]] ; then
485                         CONF_LIBDIR=${!LIBDIR_VAR}
486                 fi
487                 if [[ -n ${CONF_LIBDIR} ]] && ! __hasgq --libdir=\* "$@" ; then
488                         export CONF_PREFIX=$(__hasg --exec-prefix=\* "$@")
489                         [[ -z ${CONF_PREFIX} ]] && CONF_PREFIX=$(__hasg --prefix=\* "$@")
490                         : ${CONF_PREFIX:=${EPREFIX}/usr}
491                         CONF_PREFIX=${CONF_PREFIX#*=}
492                         [[ ${CONF_PREFIX} != /* ]] && CONF_PREFIX="/${CONF_PREFIX}"
493                         [[ ${CONF_LIBDIR} != /* ]] && CONF_LIBDIR="/${CONF_LIBDIR}"
494                         set -- --libdir="$(__strip_duplicate_slashes "${CONF_PREFIX}${CONF_LIBDIR}")" "$@"
495                 fi
496
497                 # Handle arguments containing quoted whitespace (see bug #457136).
498                 eval "local -a EXTRA_ECONF=(${EXTRA_ECONF})"
499
500                 set -- \
501                         --prefix="${EPREFIX}"/usr \
502                         ${CBUILD:+--build=${CBUILD}} \
503                         --host=${CHOST} \
504                         ${CTARGET:+--target=${CTARGET}} \
505                         --mandir="${EPREFIX}"/usr/share/man \
506                         --infodir="${EPREFIX}"/usr/share/info \
507                         --datadir="${EPREFIX}"/usr/share \
508                         --sysconfdir="${EPREFIX}"/etc \
509                         --localstatedir="${EPREFIX}"/var/lib \
510                         "$@" \
511                         "${EXTRA_ECONF[@]}"
512                 __vecho "${ECONF_SOURCE}/configure" "$@"
513
514                 if ! "${ECONF_SOURCE}/configure" "$@" ; then
515
516                         if [ -s config.log ]; then
517                                 echo
518                                 echo "!!! Please attach the following file when seeking support:"
519                                 echo "!!! ${PWD}/config.log"
520                         fi
521                         die "econf failed"
522                 fi
523         elif [ -f "${ECONF_SOURCE}/configure" ]; then
524                 die "configure is not executable"
525         else
526                 die "no configure script found"
527         fi
528 }
529
530 einstall() {
531         # CONF_PREFIX is only set if they didn't pass in libdir above.
532         local LOCAL_EXTRA_EINSTALL="${EXTRA_EINSTALL}"
533         if ! ___eapi_has_prefix_variables; then
534                 local ED=${D}
535         fi
536         LIBDIR_VAR="LIBDIR_${ABI}"
537         if [ -n "${ABI}" -a -n "${!LIBDIR_VAR}" ]; then
538                 CONF_LIBDIR="${!LIBDIR_VAR}"
539         fi
540         unset LIBDIR_VAR
541         if [ -n "${CONF_LIBDIR}" ] && [ "${CONF_PREFIX:+set}" = set ]; then
542                 EI_DESTLIBDIR="${D}/${CONF_PREFIX}/${CONF_LIBDIR}"
543                 EI_DESTLIBDIR="$(__strip_duplicate_slashes "${EI_DESTLIBDIR}")"
544                 LOCAL_EXTRA_EINSTALL="libdir=${EI_DESTLIBDIR} ${LOCAL_EXTRA_EINSTALL}"
545                 unset EI_DESTLIBDIR
546         fi
547
548         if [ -f ./[mM]akefile -o -f ./GNUmakefile ] ; then
549                 if [ "${PORTAGE_DEBUG}" == "1" ]; then
550                         ${MAKE:-make} -n prefix="${ED}usr" \
551                                 datadir="${ED}usr/share" \
552                                 infodir="${ED}usr/share/info" \
553                                 localstatedir="${ED}var/lib" \
554                                 mandir="${ED}usr/share/man" \
555                                 sysconfdir="${ED}etc" \
556                                 ${LOCAL_EXTRA_EINSTALL} \
557                                 ${MAKEOPTS} ${EXTRA_EMAKE} -j1 \
558                                 "$@" install
559                 fi
560                 ${MAKE:-make} prefix="${ED}usr" \
561                         datadir="${ED}usr/share" \
562                         infodir="${ED}usr/share/info" \
563                         localstatedir="${ED}var/lib" \
564                         mandir="${ED}usr/share/man" \
565                         sysconfdir="${ED}etc" \
566                         ${LOCAL_EXTRA_EINSTALL} \
567                         ${MAKEOPTS} ${EXTRA_EMAKE} -j1 \
568                         "$@" install || die "einstall failed"
569         else
570                 die "no Makefile found"
571         fi
572 }
573
574 __eapi0_pkg_nofetch() {
575         [ -z "${SRC_URI}" ] && return
576
577         elog "The following are listed in SRC_URI for ${PN}:"
578         local x
579         for x in $(echo ${SRC_URI}); do
580                 elog "   ${x}"
581         done
582 }
583
584 __eapi0_src_unpack() {
585         [[ -n ${A} ]] && unpack ${A}
586 }
587
588 __eapi0_src_compile() {
589         if [ -x ./configure ] ; then
590                 econf
591         fi
592         __eapi2_src_compile
593 }
594
595 __eapi0_src_test() {
596         # Since we don't want emake's automatic die
597         # support (EAPI 4 and later), and we also don't
598         # want the warning messages that it produces if
599         # we call it in 'nonfatal' mode, we use emake_cmd
600         # to emulate the desired parts of emake behavior.
601         local emake_cmd="${MAKE:-make} ${MAKEOPTS} ${EXTRA_EMAKE}"
602         local internal_opts=
603         if ___eapi_default_src_test_disables_parallel_jobs; then
604                 internal_opts+=" -j1"
605         fi
606         if $emake_cmd ${internal_opts} check -n &> /dev/null; then
607                 __vecho ">>> Test phase [check]: ${CATEGORY}/${PF}"
608                 $emake_cmd ${internal_opts} check || \
609                         die "Make check failed. See above for details."
610         elif $emake_cmd ${internal_opts} test -n &> /dev/null; then
611                 __vecho ">>> Test phase [test]: ${CATEGORY}/${PF}"
612                 $emake_cmd ${internal_opts} test || \
613                         die "Make test failed. See above for details."
614         else
615                 __vecho ">>> Test phase [none]: ${CATEGORY}/${PF}"
616         fi
617 }
618
619 __eapi1_src_compile() {
620         __eapi2_src_configure
621         __eapi2_src_compile
622 }
623
624 __eapi2_src_configure() {
625         if [[ -x ${ECONF_SOURCE:-.}/configure ]] ; then
626                 econf
627         fi
628 }
629
630 __eapi2_src_compile() {
631         if [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then
632                 emake || die "emake failed"
633         fi
634 }
635
636 __eapi4_src_install() {
637         if [[ -f Makefile || -f GNUmakefile || -f makefile ]] ; then
638                 emake DESTDIR="${D}" install
639         fi
640
641         if ! declare -p DOCS &>/dev/null ; then
642                 local d
643                 for d in README* ChangeLog AUTHORS NEWS TODO CHANGES \
644                                 THANKS BUGS FAQ CREDITS CHANGELOG ; do
645                         [[ -s "${d}" ]] && dodoc "${d}"
646                 done
647         elif [[ $(declare -p DOCS) == "declare -a "* ]] ; then
648                 dodoc "${DOCS[@]}"
649         else
650                 dodoc ${DOCS}
651         fi
652 }
653
654 # @FUNCTION: has_version
655 # @USAGE: [--host-root] <DEPEND ATOM>
656 # @DESCRIPTION:
657 # Return true if given package is installed. Otherwise return false.
658 # Callers may override the ROOT variable in order to match packages from an
659 # alternative ROOT.
660 has_version() {
661
662         local atom eroot host_root=false root=${ROOT}
663         if [[ $1 == --host-root ]] ; then
664                 host_root=true
665                 shift
666         fi
667         atom=$1
668         shift
669         [ $# -gt 0 ] && die "${FUNCNAME[0]}: unused argument(s): $*"
670
671         if ${host_root} ; then
672                 if ! ___eapi_best_version_and_has_version_support_--host-root; then
673                         die "${FUNCNAME[0]}: option --host-root is not supported with EAPI ${EAPI}"
674                 fi
675                 root=/
676         fi
677
678         if ___eapi_has_prefix_variables; then
679                 # [[ ${root} == / ]] would be ambiguous here,
680                 # since both prefixes can share root=/ while
681                 # having different EPREFIX offsets.
682                 if ${host_root} ; then
683                         eroot=${root%/}${PORTAGE_OVERRIDE_EPREFIX}/
684                 else
685                         eroot=${root%/}${EPREFIX}/
686                 fi
687         else
688                 eroot=${root}
689         fi
690         if [[ -n $PORTAGE_IPC_DAEMON ]] ; then
691                 "$PORTAGE_BIN_PATH"/ebuild-ipc has_version "${eroot}" "${atom}"
692         else
693                 "${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" has_version "${eroot}" "${atom}"
694         fi
695         local retval=$?
696         case "${retval}" in
697                 0|1)
698                         return ${retval}
699                         ;;
700                 2)
701                         die "${FUNCNAME[0]}: invalid atom: ${atom}"
702                         ;;
703                 *)
704                         if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
705                                 die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
706                         else
707                                 die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
708                         fi
709                         ;;
710         esac
711 }
712
713 # @FUNCTION: best_version
714 # @USAGE: [--host-root] <DEPEND ATOM>
715 # @DESCRIPTION:
716 # Returns the best/most-current match.
717 # Callers may override the ROOT variable in order to match packages from an
718 # alternative ROOT.
719 best_version() {
720
721         local atom eroot host_root=false root=${ROOT}
722         if [[ $1 == --host-root ]] ; then
723                 host_root=true
724                 shift
725         fi
726         atom=$1
727         shift
728         [ $# -gt 0 ] && die "${FUNCNAME[0]}: unused argument(s): $*"
729
730         if ${host_root} ; then
731                 if ! ___eapi_best_version_and_has_version_support_--host-root; then
732                         die "${FUNCNAME[0]}: option --host-root is not supported with EAPI ${EAPI}"
733                 fi
734                 root=/
735         fi
736
737         if ___eapi_has_prefix_variables; then
738                 # [[ ${root} == / ]] would be ambiguous here,
739                 # since both prefixes can share root=/ while
740                 # having different EPREFIX offsets.
741                 if ${host_root} ; then
742                         eroot=${root%/}${PORTAGE_OVERRIDE_EPREFIX}/
743                 else
744                         eroot=${root%/}${EPREFIX}/
745                 fi
746         else
747                 eroot=${root}
748         fi
749         if [[ -n $PORTAGE_IPC_DAEMON ]] ; then
750                 "$PORTAGE_BIN_PATH"/ebuild-ipc best_version "${eroot}" "${atom}"
751         else
752                 "${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" best_version "${eroot}" "${atom}"
753         fi
754         local retval=$?
755         case "${retval}" in
756                 0|1)
757                         return ${retval}
758                         ;;
759                 2)
760                         die "${FUNCNAME[0]}: invalid atom: ${atom}"
761                         ;;
762                 *)
763                         if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
764                                 die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
765                         else
766                                 die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
767                         fi
768                         ;;
769         esac
770 }
771
772 if ___eapi_has_master_repositories; then
773         master_repositories() {
774                 local output repository=$1 retval
775                 shift
776                 [[ $# -gt 0 ]] && die "${FUNCNAME[0]}: unused argument(s): $*"
777
778                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
779                         "${PORTAGE_BIN_PATH}/ebuild-ipc" master_repositories "${EROOT}" "${repository}"
780                 else
781                         output=$("${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" master_repositories "${EROOT}" "${repository}")
782                 fi
783                 retval=$?
784                 [[ -n ${output} ]] && echo "${output}"
785                 case "${retval}" in
786                         0|1)
787                                 return ${retval}
788                                 ;;
789                         2)
790                                 die "${FUNCNAME[0]}: invalid repository: ${repository}"
791                                 ;;
792                         *)
793                                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
794                                         die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
795                                 else
796                                         die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
797                                 fi
798                                 ;;
799                 esac
800         }
801 fi
802
803 if ___eapi_has_repository_path; then
804         repository_path() {
805                 local output repository=$1 retval
806                 shift
807                 [[ $# -gt 0 ]] && die "${FUNCNAME[0]}: unused argument(s): $*"
808
809                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
810                         "${PORTAGE_BIN_PATH}/ebuild-ipc" repository_path "${EROOT}" "${repository}"
811                 else
812                         output=$("${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" get_repo_path "${EROOT}" "${repository}")
813                 fi
814                 retval=$?
815                 [[ -n ${output} ]] && echo "${output}"
816                 case "${retval}" in
817                         0|1)
818                                 return ${retval}
819                                 ;;
820                         2)
821                                 die "${FUNCNAME[0]}: invalid repository: ${repository}"
822                                 ;;
823                         *)
824                                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
825                                         die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
826                                 else
827                                         die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
828                                 fi
829                                 ;;
830                 esac
831         }
832 fi
833
834 if ___eapi_has_available_eclasses; then
835         available_eclasses() {
836                 local output repository=${PORTAGE_REPO_NAME} retval
837                 [[ $# -gt 0 ]] && die "${FUNCNAME[0]}: unused argument(s): $*"
838
839                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
840                         "${PORTAGE_BIN_PATH}/ebuild-ipc" available_eclasses "${EROOT}" "${repository}"
841                 else
842                         output=$("${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" available_eclasses "${EROOT}" "${repository}")
843                 fi
844                 retval=$?
845                 [[ -n ${output} ]] && echo "${output}"
846                 case "${retval}" in
847                         0|1)
848                                 return ${retval}
849                                 ;;
850                         2)
851                                 die "${FUNCNAME[0]}: invalid repository: ${repository}"
852                                 ;;
853                         *)
854                                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
855                                         die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
856                                 else
857                                         die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
858                                 fi
859                                 ;;
860                 esac
861         }
862 fi
863
864 if ___eapi_has_eclass_path; then
865         eclass_path() {
866                 local eclass=$1 output repository=${PORTAGE_REPO_NAME} retval
867                 shift
868                 [[ $# -gt 0 ]] && die "${FUNCNAME[0]}: unused argument(s): $*"
869
870                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
871                         "${PORTAGE_BIN_PATH}/ebuild-ipc" eclass_path "${EROOT}" "${repository}" "${eclass}"
872                 else
873                         output=$("${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" eclass_path "${EROOT}" "${repository}" "${eclass}")
874                 fi
875                 retval=$?
876                 [[ -n ${output} ]] && echo "${output}"
877                 case "${retval}" in
878                         0|1)
879                                 return ${retval}
880                                 ;;
881                         2)
882                                 die "${FUNCNAME[0]}: invalid repository: ${repository}"
883                                 ;;
884                         *)
885                                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
886                                         die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
887                                 else
888                                         die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
889                                 fi
890                                 ;;
891                 esac
892         }
893 fi
894
895 if ___eapi_has_license_path; then
896         license_path() {
897                 local license=$1 output repository=${PORTAGE_REPO_NAME} retval
898                 shift
899                 [[ $# -gt 0 ]] && die "${FUNCNAME[0]}: unused argument(s): $*"
900
901                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
902                         "${PORTAGE_BIN_PATH}/ebuild-ipc" license_path "${EROOT}" "${repository}" "${license}"
903                 else
904                         output=$("${PORTAGE_BIN_PATH}/ebuild-helpers/portageq" license_path "${EROOT}" "${repository}" "${license}")
905                 fi
906                 retval=$?
907                 [[ -n ${output} ]] && echo "${output}"
908                 case "${retval}" in
909                         0|1)
910                                 return ${retval}
911                                 ;;
912                         2)
913                                 die "${FUNCNAME[0]}: invalid repository: ${repository}"
914                                 ;;
915                         *)
916                                 if [[ -n ${PORTAGE_IPC_DAEMON} ]]; then
917                                         die "${FUNCNAME[0]}: unexpected ebuild-ipc exit code: ${retval}"
918                                 else
919                                         die "${FUNCNAME[0]}: unexpected portageq exit code: ${retval}"
920                                 fi
921                                 ;;
922                 esac
923         }
924 fi
925
926 if ___eapi_has_package_manager_build_user; then
927         package_manager_build_user() {
928                 echo "${PORTAGE_BUILD_USER}"
929         }
930 fi
931
932 if ___eapi_has_package_manager_build_group; then
933         package_manager_build_group() {
934                 echo "${PORTAGE_BUILD_GROUP}"
935         }
936 fi