2302910653990d919a57c9a51908a427b3786ee1
[portage.git] / bin / phase-functions.sh
1 #!/bin/bash
2 # Copyright 1999-2011 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Hardcoded bash lists are needed for backward compatibility with
6 # <portage-2.1.4 since they assume that a newly installed version
7 # of ebuild.sh will work for pkg_postinst, pkg_prerm, and pkg_postrm
8 # when portage is upgrading itself.
9
10 PORTAGE_READONLY_METADATA="DEFINED_PHASES DEPEND DESCRIPTION
11         EAPI HOMEPAGE INHERITED IUSE REQUIRED_USE KEYWORDS LICENSE
12         PDEPEND PROVIDE RDEPEND RESTRICT SLOT SRC_URI"
13
14 PORTAGE_READONLY_VARS="D EBUILD EBUILD_PHASE \
15         EBUILD_SH_ARGS ECLASSDIR EMERGE_FROM FILESDIR MERGE_TYPE \
16         PM_EBUILD_HOOK_DIR \
17         PORTAGE_ACTUAL_DISTDIR PORTAGE_ARCHLIST PORTAGE_BASHRC  \
18         PORTAGE_BINPKG_FILE PORTAGE_BINPKG_TAR_OPTS PORTAGE_BINPKG_TMPFILE \
19         PORTAGE_BIN_PATH PORTAGE_BUILDDIR PORTAGE_BUNZIP2_COMMAND \
20         PORTAGE_BZIP2_COMMAND PORTAGE_COLORMAP PORTAGE_CONFIGROOT \
21         PORTAGE_DEBUG PORTAGE_DEPCACHEDIR PORTAGE_EBUILD_EXIT_FILE \
22         PORTAGE_GID PORTAGE_GRPNAME PORTAGE_INST_GID PORTAGE_INST_UID \
23         PORTAGE_IPC_DAEMON PORTAGE_IUSE PORTAGE_LOG_FILE \
24         PORTAGE_MUTABLE_FILTERED_VARS PORTAGE_PYM_PATH PORTAGE_PYTHON \
25         PORTAGE_READONLY_METADATA PORTAGE_READONLY_VARS \
26         PORTAGE_REPO_NAME PORTAGE_RESTRICT PORTAGE_SANDBOX_COMPAT_LEVEL \
27         PORTAGE_SAVED_READONLY_VARS PORTAGE_SIGPIPE_STATUS \
28         PORTAGE_TMPDIR PORTAGE_UPDATE_ENV PORTAGE_USERNAME \
29         PORTAGE_VERBOSE PORTAGE_WORKDIR_MODE PORTDIR PORTDIR_OVERLAY \
30         PROFILE_PATHS REPLACING_VERSIONS REPLACED_BY_VERSION T WORKDIR"
31
32 PORTAGE_SAVED_READONLY_VARS="A CATEGORY P PF PN PR PV PVR"
33
34 # Variables that portage sets but doesn't mark readonly.
35 # In order to prevent changed values from causing unexpected
36 # interference, they are filtered out of the environment when
37 # it is saved or loaded (any mutations do not persist).
38 PORTAGE_MUTABLE_FILTERED_VARS="AA HOSTNAME"
39
40 # @FUNCTION: filter_readonly_variables
41 # @DESCRIPTION: [--filter-sandbox] [--allow-extra-vars]
42 # Read an environment from stdin and echo to stdout while filtering variables
43 # with names that are known to cause interference:
44 #
45 #   * some specific variables for which bash does not allow assignment
46 #   * some specific variables that affect portage or sandbox behavior
47 #   * variable names that begin with a digit or that contain any
48 #     non-alphanumeric characters that are not be supported by bash
49 #
50 # --filter-sandbox causes all SANDBOX_* variables to be filtered, which
51 # is only desired in certain cases, such as during preprocessing or when
52 # saving environment.bz2 for a binary or installed package.
53 #
54 # --filter-features causes the special FEATURES variable to be filtered.
55 # Generally, we want it to persist between phases since the user might
56 # want to modify it via bashrc to enable things like splitdebug and
57 # installsources for specific packages. They should be able to modify it
58 # in pre_pkg_setup() and have it persist all the way through the install
59 # phase. However, if FEATURES exist inside environment.bz2 then they
60 # should be overridden by current settings.
61 #
62 # --filter-locale causes locale related variables such as LANG and LC_*
63 # variables to be filtered. These variables should persist between phases,
64 # in case they are modified by the ebuild. However, the current user
65 # settings should be used when loading the environment from a binary or
66 # installed package.
67 #
68 # --filter-path causes the PATH variable to be filtered. This variable
69 # should persist between phases, in case it is modified by the ebuild.
70 # However, old settings should be overridden when loading the
71 # environment from a binary or installed package.
72 #
73 # ---allow-extra-vars causes some extra vars to be allowd through, such
74 # as ${PORTAGE_SAVED_READONLY_VARS} and ${PORTAGE_MUTABLE_FILTERED_VARS}.
75 #
76 # In bash-3.2_p20+ an attempt to assign BASH_*, FUNCNAME, GROUPS or any
77 # readonly variable cause the shell to exit while executing the "source"
78 # builtin command. To avoid this problem, this function filters those
79 # variables out and discards them. See bug #190128.
80 filter_readonly_variables() {
81         local x filtered_vars
82         local readonly_bash_vars="BASHOPTS BASHPID DIRSTACK EUID
83                 FUNCNAME GROUPS PIPESTATUS PPID SHELLOPTS UID"
84         local bash_misc_vars="BASH BASH_.* COMP_WORDBREAKS HISTCMD
85                 HISTFILE HOSTNAME HOSTTYPE IFS LINENO MACHTYPE OLDPWD
86                 OPTERR OPTIND OSTYPE POSIXLY_CORRECT PS4 PWD RANDOM
87                 SECONDS SHELL SHLVL _"
88         local filtered_sandbox_vars="SANDBOX_ACTIVE SANDBOX_BASHRC
89                 SANDBOX_DEBUG_LOG SANDBOX_DISABLED SANDBOX_LIB
90                 SANDBOX_LOG SANDBOX_ON"
91         local misc_garbage_vars="_portage_filter_opts"
92         filtered_vars="$readonly_bash_vars $bash_misc_vars
93                 $PORTAGE_READONLY_VARS $misc_garbage_vars"
94
95         # Don't filter/interfere with prefix variables unless they are
96         # supported by the current EAPI.
97         case "${EAPI:-0}" in
98                 0|1|2)
99                         [[ " ${USE} " == *" prefix "* ]] && \
100                                 filtered_vars+=" ED EPREFIX EROOT"
101                         ;;
102                 *)
103                         filtered_vars+=" ED EPREFIX EROOT"
104                         ;;
105         esac
106
107         if has --filter-sandbox $* ; then
108                 filtered_vars="${filtered_vars} SANDBOX_.*"
109         else
110                 filtered_vars="${filtered_vars} ${filtered_sandbox_vars}"
111         fi
112         if has --filter-features $* ; then
113                 filtered_vars="${filtered_vars} FEATURES PORTAGE_FEATURES"
114         fi
115         if has --filter-path $* ; then
116                 filtered_vars+=" PATH"
117         fi
118         if has --filter-locale $* ; then
119                 filtered_vars+=" LANG LC_ALL LC_COLLATE
120                         LC_CTYPE LC_MESSAGES LC_MONETARY
121                         LC_NUMERIC LC_PAPER LC_TIME"
122         fi
123         if ! has --allow-extra-vars $* ; then
124                 filtered_vars="
125                         ${filtered_vars}
126                         ${PORTAGE_SAVED_READONLY_VARS}
127                         ${PORTAGE_MUTABLE_FILTERED_VARS}
128                 "
129         fi
130
131         "${PORTAGE_PYTHON:-/usr/bin/python}" "${PORTAGE_BIN_PATH}"/filter-bash-environment.py "${filtered_vars}" || die "filter-bash-environment.py failed"
132 }
133
134 # @FUNCTION: preprocess_ebuild_env
135 # @DESCRIPTION:
136 # Filter any readonly variables from ${T}/environment, source it, and then
137 # save it via save_ebuild_env(). This process should be sufficient to prevent
138 # any stale variables or functions from an arbitrary environment from
139 # interfering with the current environment. This is useful when an existing
140 # environment needs to be loaded from a binary or installed package.
141 preprocess_ebuild_env() {
142         local _portage_filter_opts="--filter-features --filter-locale --filter-path --filter-sandbox"
143
144         # If environment.raw is present, this is a signal from the python side,
145         # indicating that the environment may contain stale FEATURES and
146         # SANDBOX_{DENY,PREDICT,READ,WRITE} variables that should be filtered out.
147         # Otherwise, we don't need to filter the environment.
148         [ -f "${T}/environment.raw" ] || return 0
149
150         filter_readonly_variables $_portage_filter_opts < "${T}"/environment \
151                 >> "$T/environment.filtered" || return $?
152         unset _portage_filter_opts
153         mv "${T}"/environment.filtered "${T}"/environment || return $?
154         rm -f "${T}/environment.success" || return $?
155         # WARNING: Code inside this subshell should avoid making assumptions
156         # about variables or functions after source "${T}"/environment has been
157         # called. Any variables that need to be relied upon should already be
158         # filtered out above.
159         (
160                 export SANDBOX_ON=1
161                 source "${T}/environment" || exit $?
162                 # We have to temporarily disable sandbox since the
163                 # SANDBOX_{DENY,READ,PREDICT,WRITE} values we've just loaded
164                 # may be unusable (triggering in spurious sandbox violations)
165                 # until we've merged them with our current values.
166                 export SANDBOX_ON=0
167
168                 # It's remotely possible that save_ebuild_env() has been overridden
169                 # by the above source command. To protect ourselves, we override it
170                 # here with our own version. ${PORTAGE_BIN_PATH} is safe to use here
171                 # because it's already filtered above.
172                 source "${PORTAGE_BIN_PATH}/save-ebuild-env.sh" || exit $?
173
174                 # Rely on save_ebuild_env() to filter out any remaining variables
175                 # and functions that could interfere with the current environment.
176                 save_ebuild_env || exit $?
177                 >> "$T/environment.success" || exit $?
178         ) > "${T}/environment.filtered"
179         local retval
180         if [ -e "${T}/environment.success" ] ; then
181                 filter_readonly_variables --filter-features < \
182                         "${T}/environment.filtered" > "${T}/environment"
183                 retval=$?
184         else
185                 retval=1
186         fi
187         rm -f "${T}"/environment.{filtered,raw,success}
188         return ${retval}
189 }
190
191 ebuild_phase() {
192         declare -F "$1" >/dev/null && qa_call $1
193 }
194
195 ebuild_phase_with_hooks() {
196         local x phase_name=${1}
197         for x in {pre_,,post_}${phase_name} ; do
198                 ebuild_phase ${x}
199         done
200 }
201
202 dyn_pretend() {
203         if [[ -e $PORTAGE_BUILDDIR/.pretended ]] ; then
204                 vecho ">>> It appears that '$PF' is already pretended; skipping."
205                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.pretended' to force pretend."
206                 return 0
207         fi
208         ebuild_phase pre_pkg_pretend
209         ebuild_phase pkg_pretend
210         >> "$PORTAGE_BUILDDIR/.pretended" || \
211                 die "Failed to create $PORTAGE_BUILDDIR/.pretended"
212         ebuild_phase post_pkg_pretend
213 }
214
215 dyn_setup() {
216         if [[ -e $PORTAGE_BUILDDIR/.setuped ]] ; then
217                 vecho ">>> It appears that '$PF' is already setup; skipping."
218                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.setuped' to force setup."
219                 return 0
220         fi
221         ebuild_phase pre_pkg_setup
222         ebuild_phase pkg_setup
223         >> "$PORTAGE_BUILDDIR/.setuped" || \
224                 die "Failed to create $PORTAGE_BUILDDIR/.setuped"
225         ebuild_phase post_pkg_setup
226 }
227
228 dyn_unpack() {
229         if [[ -f ${PORTAGE_BUILDDIR}/.unpacked ]] ; then
230                 vecho ">>> WORKDIR is up-to-date, keeping..."
231                 return 0
232         fi
233         if [ ! -d "${WORKDIR}" ]; then
234                 install -m${PORTAGE_WORKDIR_MODE:-0700} -d "${WORKDIR}" || die "Failed to create dir '${WORKDIR}'"
235         fi
236         cd "${WORKDIR}" || die "Directory change failed: \`cd '${WORKDIR}'\`"
237         ebuild_phase pre_src_unpack
238         vecho ">>> Unpacking source..."
239         ebuild_phase src_unpack
240         >> "$PORTAGE_BUILDDIR/.unpacked" || \
241                 die "Failed to create $PORTAGE_BUILDDIR/.unpacked"
242         vecho ">>> Source unpacked in ${WORKDIR}"
243         ebuild_phase post_src_unpack
244 }
245
246 dyn_clean() {
247         if [ -z "${PORTAGE_BUILDDIR}" ]; then
248                 echo "Aborting clean phase because PORTAGE_BUILDDIR is unset!"
249                 return 1
250         elif [ ! -d "${PORTAGE_BUILDDIR}" ] ; then
251                 return 0
252         fi
253         if has chflags $FEATURES ; then
254                 chflags -R noschg,nouchg,nosappnd,nouappnd "${PORTAGE_BUILDDIR}"
255                 chflags -R nosunlnk,nouunlnk "${PORTAGE_BUILDDIR}" 2>/dev/null
256         fi
257
258         rm -rf "${PORTAGE_BUILDDIR}/image" "${PORTAGE_BUILDDIR}/homedir"
259         rm -f "${PORTAGE_BUILDDIR}/.installed"
260
261         if [[ $EMERGE_FROM = binary ]] || \
262                 ! has keeptemp $FEATURES && ! has keepwork $FEATURES ; then
263                 rm -rf "${T}"
264         fi
265
266         if [[ $EMERGE_FROM = binary ]] || ! has keepwork $FEATURES; then
267                 rm -f "$PORTAGE_BUILDDIR"/.{ebuild_changed,logid,pretended,setuped,unpacked,prepared} \
268                         "$PORTAGE_BUILDDIR"/.{configured,compiled,tested,packaged} \
269                         "$PORTAGE_BUILDDIR"/.die_hooks \
270                         "$PORTAGE_BUILDDIR"/.ipc_{in,out,lock} \
271                         "$PORTAGE_BUILDDIR"/.exit_status
272
273                 rm -rf "${PORTAGE_BUILDDIR}/build-info"
274                 rm -rf "${WORKDIR}"
275         fi
276
277         if [ -f "${PORTAGE_BUILDDIR}/.unpacked" ]; then
278                 find "${PORTAGE_BUILDDIR}" -type d ! -regex "^${WORKDIR}" | sort -r | tr "\n" "\0" | $XARGS -0 rmdir &>/dev/null
279         fi
280
281         # do not bind this to doebuild defined DISTDIR; don't trust doebuild, and if mistakes are made it'll
282         # result in it wiping the users distfiles directory (bad).
283         rm -rf "${PORTAGE_BUILDDIR}/distdir"
284
285         # Some kernels, such as Solaris, return EINVAL when an attempt
286         # is made to remove the current working directory.
287         cd "$PORTAGE_BUILDDIR"/../..
288         rmdir "$PORTAGE_BUILDDIR" 2>/dev/null
289
290         true
291 }
292
293 abort_handler() {
294         local msg
295         if [ "$2" != "fail" ]; then
296                 msg="${EBUILD}: ${1} aborted; exiting."
297         else
298                 msg="${EBUILD}: ${1} failed; exiting."
299         fi
300         echo
301         echo "$msg"
302         echo
303         eval ${3}
304         #unset signal handler
305         trap - SIGINT SIGQUIT
306 }
307
308 abort_prepare() {
309         abort_handler src_prepare $1
310         rm -f "$PORTAGE_BUILDDIR/.prepared"
311         exit 1
312 }
313
314 abort_configure() {
315         abort_handler src_configure $1
316         rm -f "$PORTAGE_BUILDDIR/.configured"
317         exit 1
318 }
319
320 abort_compile() {
321         abort_handler "src_compile" $1
322         rm -f "${PORTAGE_BUILDDIR}/.compiled"
323         exit 1
324 }
325
326 abort_test() {
327         abort_handler "dyn_test" $1
328         rm -f "${PORTAGE_BUILDDIR}/.tested"
329         exit 1
330 }
331
332 abort_install() {
333         abort_handler "src_install" $1
334         rm -rf "${PORTAGE_BUILDDIR}/image"
335         exit 1
336 }
337
338 has_phase_defined_up_to() {
339         local phase
340         for phase in unpack prepare configure compile install; do
341                 has ${phase} ${DEFINED_PHASES} && return 0
342                 [[ ${phase} == $1 ]] && return 1
343         done
344         # We shouldn't actually get here
345         return 1
346 }
347
348 dyn_prepare() {
349
350         if [[ -e $PORTAGE_BUILDDIR/.prepared ]] ; then
351                 vecho ">>> It appears that '$PF' is already prepared; skipping."
352                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.prepared' to force prepare."
353                 return 0
354         fi
355
356         if [[ -d $S ]] ; then
357                 cd "${S}"
358         elif has $EAPI 0 1 2 3 3_pre2 ; then
359                 cd "${WORKDIR}"
360         elif [[ -z ${A} ]] && ! has_phase_defined_up_to prepare; then
361                 cd "${WORKDIR}"
362         else
363                 die "The source directory '${S}' doesn't exist"
364         fi
365
366         trap abort_prepare SIGINT SIGQUIT
367
368         ebuild_phase pre_src_prepare
369         vecho ">>> Preparing source in $PWD ..."
370         ebuild_phase src_prepare
371         >> "$PORTAGE_BUILDDIR/.prepared" || \
372                 die "Failed to create $PORTAGE_BUILDDIR/.prepared"
373         vecho ">>> Source prepared."
374         ebuild_phase post_src_prepare
375
376         trap - SIGINT SIGQUIT
377 }
378
379 dyn_configure() {
380
381         if [[ -e $PORTAGE_BUILDDIR/.configured ]] ; then
382                 vecho ">>> It appears that '$PF' is already configured; skipping."
383                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.configured' to force configuration."
384                 return 0
385         fi
386
387         if [[ -d $S ]] ; then
388                 cd "${S}"
389         elif has $EAPI 0 1 2 3 3_pre2 ; then
390                 cd "${WORKDIR}"
391         elif [[ -z ${A} ]] && ! has_phase_defined_up_to configure; then
392                 cd "${WORKDIR}"
393         else
394                 die "The source directory '${S}' doesn't exist"
395         fi
396
397         trap abort_configure SIGINT SIGQUIT
398
399         ebuild_phase pre_src_configure
400
401         vecho ">>> Configuring source in $PWD ..."
402         ebuild_phase src_configure
403         >> "$PORTAGE_BUILDDIR/.configured" || \
404                 die "Failed to create $PORTAGE_BUILDDIR/.configured"
405         vecho ">>> Source configured."
406
407         ebuild_phase post_src_configure
408
409         trap - SIGINT SIGQUIT
410 }
411
412 dyn_compile() {
413
414         if [[ -e $PORTAGE_BUILDDIR/.compiled ]] ; then
415                 vecho ">>> It appears that '${PF}' is already compiled; skipping."
416                 vecho ">>> Remove '$PORTAGE_BUILDDIR/.compiled' to force compilation."
417                 return 0
418         fi
419
420         if [[ -d $S ]] ; then
421                 cd "${S}"
422         elif has $EAPI 0 1 2 3 3_pre2 ; then
423                 cd "${WORKDIR}"
424         elif [[ -z ${A} ]] && ! has_phase_defined_up_to compile; then
425                 cd "${WORKDIR}"
426         else
427                 die "The source directory '${S}' doesn't exist"
428         fi
429
430         trap abort_compile SIGINT SIGQUIT
431
432         if has distcc $FEATURES && has distcc-pump $FEATURES ; then
433                 if [[ -z $INCLUDE_SERVER_PORT ]] || [[ ! -w $INCLUDE_SERVER_PORT ]] ; then
434                         eval $(pump --startup)
435                         trap "pump --shutdown" EXIT
436                 fi
437         fi
438
439         ebuild_phase pre_src_compile
440
441         vecho ">>> Compiling source in $PWD ..."
442         ebuild_phase src_compile
443         >> "$PORTAGE_BUILDDIR/.compiled" || \
444                 die "Failed to create $PORTAGE_BUILDDIR/.compiled"
445         vecho ">>> Source compiled."
446
447         ebuild_phase post_src_compile
448
449         trap - SIGINT SIGQUIT
450 }
451
452 dyn_test() {
453
454         if [[ -e $PORTAGE_BUILDDIR/.tested ]] ; then
455                 vecho ">>> It appears that ${PN} has already been tested; skipping."
456                 vecho ">>> Remove '${PORTAGE_BUILDDIR}/.tested' to force test."
457                 return
458         fi
459
460         if [ "${EBUILD_FORCE_TEST}" == "1" ] ; then
461                 # If USE came from ${T}/environment then it might not have USE=test
462                 # like it's supposed to here.
463                 ! has test ${USE} && export USE="${USE} test"
464         fi
465
466         trap "abort_test" SIGINT SIGQUIT
467         if [ -d "${S}" ]; then
468                 cd "${S}"
469         else
470                 cd "${WORKDIR}"
471         fi
472
473         if ! has test $FEATURES && [ "${EBUILD_FORCE_TEST}" != "1" ]; then
474                 vecho ">>> Test phase [not enabled]: ${CATEGORY}/${PF}"
475         elif has test $RESTRICT; then
476                 einfo "Skipping make test/check due to ebuild restriction."
477                 vecho ">>> Test phase [explicitly disabled]: ${CATEGORY}/${PF}"
478         else
479                 local save_sp=${SANDBOX_PREDICT}
480                 addpredict /
481                 ebuild_phase pre_src_test
482                 ebuild_phase src_test
483                 >> "$PORTAGE_BUILDDIR/.tested" || \
484                         die "Failed to create $PORTAGE_BUILDDIR/.tested"
485                 ebuild_phase post_src_test
486                 SANDBOX_PREDICT=${save_sp}
487         fi
488
489         trap - SIGINT SIGQUIT
490 }
491
492 dyn_install() {
493         [ -z "$PORTAGE_BUILDDIR" ] && die "${FUNCNAME}: PORTAGE_BUILDDIR is unset"
494         if has noauto $FEATURES ; then
495                 rm -f "${PORTAGE_BUILDDIR}/.installed"
496         elif [[ -e $PORTAGE_BUILDDIR/.installed ]] ; then
497                 vecho ">>> It appears that '${PF}' is already installed; skipping."
498                 vecho ">>> Remove '${PORTAGE_BUILDDIR}/.installed' to force install."
499                 return 0
500         fi
501         trap "abort_install" SIGINT SIGQUIT
502         ebuild_phase pre_src_install
503
504         _x=${ED}
505         [[ " ${USE} " == *" prefix "* ]] || \
506                 case "$EAPI" in 0|1|2) _x=${D} ;; esac
507         rm -rf "${D}"
508         mkdir -p "${_x}"
509         unset _x
510
511         if [[ -d $S ]] ; then
512                 cd "${S}"
513         elif has $EAPI 0 1 2 3 3_pre2 ; then
514                 cd "${WORKDIR}"
515         elif [[ -z ${A} ]] && ! has_phase_defined_up_to install; then
516                 cd "${WORKDIR}"
517         else
518                 die "The source directory '${S}' doesn't exist"
519         fi
520
521         vecho
522         vecho ">>> Install ${PF} into ${D} category ${CATEGORY}"
523         #our custom version of libtool uses $S and $D to fix
524         #invalid paths in .la files
525         export S D
526
527         # Reset exeinto(), docinto(), insinto(), and into() state variables
528         # in case the user is running the install phase multiple times
529         # consecutively via the ebuild command.
530         export DESTTREE=/usr
531         export INSDESTTREE=""
532         export _E_EXEDESTTREE_=""
533         export _E_DOCDESTTREE_=""
534
535         ebuild_phase src_install
536         >> "$PORTAGE_BUILDDIR/.installed" || \
537                 die "Failed to create $PORTAGE_BUILDDIR/.installed"
538         vecho ">>> Completed installing ${PF} into ${D}"
539         vecho
540         ebuild_phase post_src_install
541
542         cd "${PORTAGE_BUILDDIR}"/build-info
543         set -f
544         local f x
545         IFS=$' \t\n\r'
546         for f in CATEGORY DEFINED_PHASES FEATURES INHERITED IUSE \
547                 PF PKGUSE SLOT KEYWORDS HOMEPAGE DESCRIPTION ; do
548                 x=$(echo -n ${!f})
549                 [[ -n $x ]] && echo "$x" > $f
550         done
551         if [[ $CATEGORY != virtual ]] ; then
552                 for f in ASFLAGS CBUILD CC CFLAGS CHOST CTARGET CXX \
553                         CXXFLAGS EXTRA_ECONF EXTRA_EINSTALL EXTRA_MAKE \
554                         LDFLAGS LIBCFLAGS LIBCXXFLAGS ; do
555                         x=$(echo -n ${!f})
556                         [[ -n $x ]] && echo "$x" > $f
557                 done
558         fi
559         echo "${USE}"       > USE
560         echo "${EAPI:-0}"   > EAPI
561
562         # Save EPREFIX, since it makes it easy to use chpathtool to
563         # adjust the content of a binary package so that it will
564         # work in a different EPREFIX from the one is was built for.
565         case "${EAPI:-0}" in
566                 0|1|2)
567                         [[ " ${USE} " == *" prefix "* ]] && \
568                                 [ -n "${EPREFIX}" ] && echo "${EPREFIX}" > EPREFIX
569                         ;;
570                 *)
571                         [ -n "${EPREFIX}" ] && echo "${EPREFIX}" > EPREFIX
572                         ;;
573         esac
574
575         set +f
576
577         # local variables can leak into the saved environment.
578         unset f
579
580         save_ebuild_env --exclude-init-phases | filter_readonly_variables \
581                 --filter-path --filter-sandbox --allow-extra-vars > environment
582         assert "save_ebuild_env failed"
583
584         ${PORTAGE_BZIP2_COMMAND} -f9 environment
585
586         cp "${EBUILD}" "${PF}.ebuild"
587         [ -n "${PORTAGE_REPO_NAME}" ]  && echo "${PORTAGE_REPO_NAME}" > repository
588         if has nostrip ${FEATURES} ${RESTRICT} || has strip ${RESTRICT}
589         then
590                 >> DEBUGBUILD
591         fi
592         trap - SIGINT SIGQUIT
593 }
594
595 dyn_preinst() {
596         if [ -z "${D}" ]; then
597                 eerror "${FUNCNAME}: D is unset"
598                 return 1
599         fi
600         ebuild_phase_with_hooks pkg_preinst
601 }
602
603 dyn_help() {
604         echo
605         echo "Portage"
606         echo "Copyright 1999-2010 Gentoo Foundation"
607         echo
608         echo "How to use the ebuild command:"
609         echo
610         echo "The first argument to ebuild should be an existing .ebuild file."
611         echo
612         echo "One or more of the following options can then be specified.  If more"
613         echo "than one option is specified, each will be executed in order."
614         echo
615         echo "  help        : show this help screen"
616         echo "  pretend     : execute package specific pretend actions"
617         echo "  setup       : execute package specific setup actions"
618         echo "  fetch       : download source archive(s) and patches"
619         echo "  digest      : create a manifest file for the package"
620         echo "  manifest    : create a manifest file for the package"
621         echo "  unpack      : unpack sources (auto-dependencies if needed)"
622         echo "  prepare     : prepare sources (auto-dependencies if needed)"
623         echo "  configure   : configure sources (auto-fetch/unpack if needed)"
624         echo "  compile     : compile sources (auto-fetch/unpack/configure if needed)"
625         echo "  test        : test package (auto-fetch/unpack/configure/compile if needed)"
626         echo "  preinst     : execute pre-install instructions"
627         echo "  postinst    : execute post-install instructions"
628         echo "  install     : install the package to the temporary install directory"
629         echo "  qmerge      : merge image into live filesystem, recording files in db"
630         echo "  merge       : do fetch, unpack, compile, install and qmerge"
631         echo "  prerm       : execute pre-removal instructions"
632         echo "  postrm      : execute post-removal instructions"
633         echo "  unmerge     : remove package from live filesystem"
634         echo "  config      : execute package specific configuration actions"
635         echo "  package     : create a tarball package in ${PKGDIR}/All"
636         echo "  rpm         : build a RedHat RPM package"
637         echo "  clean       : clean up all source and temporary files"
638         echo
639         echo "The following settings will be used for the ebuild process:"
640         echo
641         echo "  package     : ${PF}"
642         echo "  slot        : ${SLOT}"
643         echo "  category    : ${CATEGORY}"
644         echo "  description : ${DESCRIPTION}"
645         echo "  system      : ${CHOST}"
646         echo "  c flags     : ${CFLAGS}"
647         echo "  c++ flags   : ${CXXFLAGS}"
648         echo "  make flags  : ${MAKEOPTS}"
649         echo -n "  build mode  : "
650         if has nostrip ${FEATURES} ${RESTRICT} || has strip ${RESTRICT} ;
651         then
652                 echo "debug (large)"
653         else
654                 echo "production (stripped)"
655         fi
656         echo "  merge to    : ${ROOT}"
657         echo
658         if [ -n "$USE" ]; then
659                 echo "Additionally, support for the following optional features will be enabled:"
660                 echo
661                 echo "  ${USE}"
662         fi
663         echo
664 }
665
666 # @FUNCTION: _ebuild_arg_to_phase
667 # @DESCRIPTION:
668 # Translate a known ebuild(1) argument into the precise
669 # name of it's corresponding ebuild phase.
670 _ebuild_arg_to_phase() {
671         [ $# -ne 2 ] && die "expected exactly 2 args, got $#: $*"
672         local eapi=$1
673         local arg=$2
674         local phase_func=""
675
676         case "$arg" in
677                 pretend)
678                         ! has $eapi 0 1 2 3 3_pre2 && \
679                                 phase_func=pkg_pretend
680                         ;;
681                 setup)
682                         phase_func=pkg_setup
683                         ;;
684                 nofetch)
685                         phase_func=pkg_nofetch
686                         ;;
687                 unpack)
688                         phase_func=src_unpack
689                         ;;
690                 prepare)
691                         ! has $eapi 0 1 && \
692                                 phase_func=src_prepare
693                         ;;
694                 configure)
695                         ! has $eapi 0 1 && \
696                                 phase_func=src_configure
697                         ;;
698                 compile)
699                         phase_func=src_compile
700                         ;;
701                 test)
702                         phase_func=src_test
703                         ;;
704                 install)
705                         phase_func=src_install
706                         ;;
707                 preinst)
708                         phase_func=pkg_preinst
709                         ;;
710                 postinst)
711                         phase_func=pkg_postinst
712                         ;;
713                 prerm)
714                         phase_func=pkg_prerm
715                         ;;
716                 postrm)
717                         phase_func=pkg_postrm
718                         ;;
719         esac
720
721         [[ -z $phase_func ]] && return 1
722         echo "$phase_func"
723         return 0
724 }
725
726 _ebuild_phase_funcs() {
727         [ $# -ne 2 ] && die "expected exactly 2 args, got $#: $*"
728         local eapi=$1
729         local phase_func=$2
730         local default_phases="pkg_nofetch src_unpack src_prepare src_configure
731                 src_compile src_install src_test"
732         local x y default_func=""
733
734         for x in pkg_nofetch src_unpack src_test ; do
735                 declare -F $x >/dev/null || \
736                         eval "$x() { _eapi0_$x \"\$@\" ; }"
737         done
738
739         case $eapi in
740
741                 0|1)
742
743                         if ! declare -F src_compile >/dev/null ; then
744                                 case $eapi in
745                                         0)
746                                                 src_compile() { _eapi0_src_compile "$@" ; }
747                                                 ;;
748                                         *)
749                                                 src_compile() { _eapi1_src_compile "$@" ; }
750                                                 ;;
751                                 esac
752                         fi
753
754                         for x in $default_phases ; do
755                                 eval "default_$x() {
756                                         die \"default_$x() is not supported with EAPI='$eapi' during phase $phase_func\"
757                                 }"
758                         done
759
760                         eval "default() {
761                                 die \"default() is not supported with EAPI='$eapi' during phase $phase_func\"
762                         }"
763
764                         ;;
765
766                 *)
767
768                         declare -F src_configure >/dev/null || \
769                                 src_configure() { _eapi2_src_configure "$@" ; }
770
771                         declare -F src_compile >/dev/null || \
772                                 src_compile() { _eapi2_src_compile "$@" ; }
773
774                         has $eapi 2 3 3_pre2 || declare -F src_install >/dev/null || \
775                                 src_install() { _eapi4_src_install "$@" ; }
776
777                         if has $phase_func $default_phases ; then
778
779                                 _eapi2_pkg_nofetch   () { _eapi0_pkg_nofetch          "$@" ; }
780                                 _eapi2_src_unpack    () { _eapi0_src_unpack           "$@" ; }
781                                 _eapi2_src_prepare   () { true                             ; }
782                                 _eapi2_src_test      () { _eapi0_src_test             "$@" ; }
783                                 _eapi2_src_install   () { die "$FUNCNAME is not supported" ; }
784
785                                 for x in $default_phases ; do
786                                         eval "default_$x() { _eapi2_$x \"\$@\" ; }"
787                                 done
788
789                                 eval "default() { _eapi2_$phase_func \"\$@\" ; }"
790
791                                 case $eapi in
792                                         2|3)
793                                                 ;;
794                                         *)
795                                                 eval "default_src_install() { _eapi4_src_install \"\$@\" ; }"
796                                                 [[ $phase_func = src_install ]] && \
797                                                         eval "default() { _eapi4_$phase_func \"\$@\" ; }"
798                                                 ;;
799                                 esac
800
801                         else
802
803                                 for x in $default_phases ; do
804                                         eval "default_$x() {
805                                                 die \"default_$x() is not supported in phase $default_func\"
806                                         }"
807                                 done
808
809                                 eval "default() {
810                                         die \"default() is not supported with EAPI='$eapi' during phase $phase_func\"
811                                 }"
812
813                         fi
814
815                         ;;
816         esac
817 }
818
819 ebuild_main() {
820
821         # Subshell/helper die support (must export for the die helper).
822         # Since this function is typically executed in a subshell,
823         # setup EBUILD_MASTER_PID to refer to the current $BASHPID,
824         # which seems to give the best results when further
825         # nested subshells call die.
826         export EBUILD_MASTER_PID=$BASHPID
827         trap 'exit 1' SIGTERM
828
829         #a reasonable default for $S
830         [[ -z ${S} ]] && export S=${WORKDIR}/${P}
831
832         if [[ -s $SANDBOX_LOG ]] ; then
833                 # We use SANDBOX_LOG to check for sandbox violations,
834                 # so we ensure that there can't be a stale log to
835                 # interfere with our logic.
836                 local x=
837                 if [[ -n SANDBOX_ON ]] ; then
838                         x=$SANDBOX_ON
839                         export SANDBOX_ON=0
840                 fi
841
842                 rm -f "$SANDBOX_LOG" || \
843                         die "failed to remove stale sandbox log: '$SANDBOX_LOG'"
844
845                 if [[ -n $x ]] ; then
846                         export SANDBOX_ON=$x
847                 fi
848                 unset x
849         fi
850
851         # Force configure scripts that automatically detect ccache to
852         # respect FEATURES="-ccache".
853         has ccache $FEATURES || export CCACHE_DISABLE=1
854
855         local phase_func=$(_ebuild_arg_to_phase "$EAPI" "$EBUILD_PHASE")
856         [[ -n $phase_func ]] && _ebuild_phase_funcs "$EAPI" "$phase_func"
857         unset phase_func
858
859         source_all_bashrcs
860
861         case ${1} in
862         nofetch)
863                 ebuild_phase_with_hooks pkg_nofetch
864                 ;;
865         prerm|postrm|postinst|config|info)
866                 if has "${1}" config info && \
867                         ! declare -F "pkg_${1}" >/dev/null ; then
868                         ewarn  "pkg_${1}() is not defined: '${EBUILD##*/}'"
869                 fi
870                 export SANDBOX_ON="0"
871                 if [ "${PORTAGE_DEBUG}" != "1" ] || [ "${-/x/}" != "$-" ]; then
872                         ebuild_phase_with_hooks pkg_${1}
873                 else
874                         set -x
875                         ebuild_phase_with_hooks pkg_${1}
876                         set +x
877                 fi
878                 if [[ $EBUILD_PHASE == postinst ]] && [[ -n $PORTAGE_UPDATE_ENV ]]; then
879                         # Update environment.bz2 in case installation phases
880                         # need to pass some variables to uninstallation phases.
881                         save_ebuild_env --exclude-init-phases | \
882                                 filter_readonly_variables --filter-path \
883                                 --filter-sandbox --allow-extra-vars \
884                                 | ${PORTAGE_BZIP2_COMMAND} -c -f9 > "$PORTAGE_UPDATE_ENV"
885                         assert "save_ebuild_env failed"
886                 fi
887                 ;;
888         unpack|prepare|configure|compile|test|clean|install)
889                 if [[ ${SANDBOX_DISABLED:-0} = 0 ]] ; then
890                         export SANDBOX_ON="1"
891                 else
892                         export SANDBOX_ON="0"
893                 fi
894
895                 case "${1}" in
896                 configure|compile)
897
898                         local x
899                         for x in ASFLAGS CCACHE_DIR CCACHE_SIZE \
900                                 CFLAGS CXXFLAGS LDFLAGS LIBCFLAGS LIBCXXFLAGS ; do
901                                 [[ ${!x+set} = set ]] && export $x
902                         done
903                         unset x
904
905                         has distcc $FEATURES && [[ -n $DISTCC_DIR ]] && \
906                                 [[ ${SANDBOX_WRITE/$DISTCC_DIR} = $SANDBOX_WRITE ]] && \
907                                 addwrite "$DISTCC_DIR"
908
909                         x=LIBDIR_$ABI
910                         [ -z "$PKG_CONFIG_PATH" -a -n "$ABI" -a -n "${!x}" ] && \
911                                 export PKG_CONFIG_PATH=/usr/${!x}/pkgconfig
912
913                         if has noauto $FEATURES && \
914                                 [[ ! -f $PORTAGE_BUILDDIR/.unpacked ]] ; then
915                                 echo
916                                 echo "!!! We apparently haven't unpacked..." \
917                                         "This is probably not what you"
918                                 echo "!!! want to be doing... You are using" \
919                                         "FEATURES=noauto so I'll assume"
920                                 echo "!!! that you know what you are doing..." \
921                                         "You have 5 seconds to abort..."
922                                 echo
923
924                                 local x
925                                 for x in 1 2 3 4 5 6 7 8; do
926                                         LC_ALL=C sleep 0.25
927                                 done
928
929                                 sleep 3
930                         fi
931
932                         cd "$PORTAGE_BUILDDIR"
933                         if [ ! -d build-info ] ; then
934                                 mkdir build-info
935                                 cp "$EBUILD" "build-info/$PF.ebuild"
936                         fi
937
938                         #our custom version of libtool uses $S and $D to fix
939                         #invalid paths in .la files
940                         export S D
941
942                         ;;
943                 esac
944
945                 if [ "${PORTAGE_DEBUG}" != "1" ] || [ "${-/x/}" != "$-" ]; then
946                         dyn_${1}
947                 else
948                         set -x
949                         dyn_${1}
950                         set +x
951                 fi
952                 export SANDBOX_ON="0"
953                 ;;
954         help|pretend|setup|preinst)
955                 #pkg_setup needs to be out of the sandbox for tmp file creation;
956                 #for example, awking and piping a file in /tmp requires a temp file to be created
957                 #in /etc.  If pkg_setup is in the sandbox, both our lilo and apache ebuilds break.
958                 export SANDBOX_ON="0"
959                 if [ "${PORTAGE_DEBUG}" != "1" ] || [ "${-/x/}" != "$-" ]; then
960                         dyn_${1}
961                 else
962                         set -x
963                         dyn_${1}
964                         set +x
965                 fi
966                 ;;
967         _internal_test)
968                 ;;
969         *)
970                 export SANDBOX_ON="1"
971                 echo "Unrecognized arg '${1}'"
972                 echo
973                 dyn_help
974                 exit 1
975                 ;;
976         esac
977
978         # Save the env only for relevant phases.
979         if ! has "${1}" clean help info nofetch ; then
980                 umask 002
981                 save_ebuild_env | filter_readonly_variables \
982                         --filter-features > "$T/environment"
983                 assert "save_ebuild_env failed"
984                 chown portage:portage "$T/environment" &>/dev/null
985                 chmod g+w "$T/environment" &>/dev/null
986         fi
987         [[ -n $PORTAGE_EBUILD_EXIT_FILE ]] && > "$PORTAGE_EBUILD_EXIT_FILE"
988         if [[ -n $PORTAGE_IPC_DAEMON ]] ; then
989                 [[ ! -s $SANDBOX_LOG ]]
990                 "$PORTAGE_BIN_PATH"/ebuild-ipc exit $?
991         fi
992 }