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