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