/etc/portage/bashrc

Synopsis
This is a global bashrc similar to the bashrc files under, except always sourced for every package. It can either be used to setup a global environment common to all ebuilds, or as an alternative to the  files allowing you to handle all the necessary conditional code manually.

It is important to note that this file will be sourced multiple times during the build, at least once per phase plus several other times. You must therefore control when your code is executed using e.g.,   (EAPI 5), or otherwise avoid using the global scope by only putting code into hook functions (e.g.  ). See: bug 250240.

Another important issue with the global bashrc is that it isn't sourced for the first time until after dependency resolution -- i.e. if run with, bashrc is sourced for the first time shortly after interactive confirmation. Therefore, it isn't a complete replacement for the global. Anything that must be in the environment immediately can't be set by bashrc such as USE flags, FEATURES, etc. The portage documentation doesn't provide a complete list of contradictions, so be careful.

Finally, Portage doesn't save the initial environment prior to being mangled by Portage's global make.defaults and make.globals. You therefore have no direct way of determining whether an environment variable was supplied by the user (e.g. CFLAGS), or set by portage, so you can't guarantee using the environment by your bashrc to control behavior.

Example
This is an example global bashrc that acts as a minimal loader for user-defined hooks. Many more sophisticated examples exist. It is primarily for setting up CFLAGS and to further define hook functions on a per-package basis. You define "flag groups" by adding to the  2D associative array within the   function. Calling this function with an array name and list of group names will create an associative array whose keys are set to the union of the flags in the given groups. You can then manipulate that array per-package by defining hook functions whose names are defined in the  array of the   function.


 * 1) !/usr/bin/env bash
 * 2) /etc/portage/bashrc
 * 3) Dan Douglas 

$EBUILD_PHASE != setup && return

setupFlags { if $FUNCNAME != "${FUNCNAME[1]}" ; then if -z $1 ; then eerror "${FUNCNAME}: Must pass at least a non-empty associative array name argument." return 1 else # Base CFLAG groups local -A groups=(				[base]='(-march=native -Ofast -mmmx -pipe)'				[graphite]='(-floop-interchange -floop-strip-mine -floop-block)'				[debug]='(-ggdb)'				[dragonegg]='(-fplugin=/usr/lib64/llvm/dragonegg.so -fplugin-arg-dragonegg-enable-gcc-optzns)'			)
 * 1) Set up the initial associative array of CFLAGS and CXXFLAGS.
 * 2) Takes an array name to assign and optionally the names of "flag groups" to include.
 * 3) setupFlags arrname groupname [ groupname ... ]

local x			for x in "${@:2}"; do				if ${groups[$x]:+false}; then eerror "${FUNCNAME}: Invalid flag group name: ${x}" return 1 else local -a "y=${groups[$x]}" 'z+=("${y[@]}")' fi done "$FUNCNAME" "$1" "$(printf '[%q]= ' "${z[@]}")" fi else unset -v x y z groups eval "${1}=(${2})" fi }

runHooks { local x	for x; do		 $x || return done
 * 1) runHooks "$CATEGORY" "$PN"

# Define mappings from package names to hook functions. local -A categoryDefs=(		[dev-db]='([sqlite]=noFast)'		[dev-util]='([valgrind]=noFast)'		[media-gfx]='([digikam]=dgKamFix)'		[sys-devel]='([binutils]=xdoBinutils)'	) ${1:+${categoryDefs[$1]:+"packageDefs=${categoryDefs[$1]}"}}

# If a hook is defined for the current package, run it. Otherwise setup # default flags. Remember if you want to use default flags as a base for a	# hook, you must run setupFlags within the hook. This allows using # different default flag groups as a base per-package. local -A flags if declare -f "${packageDefs[$2]:-:}" >/dev/null 2>&1; then ${2:+"${packageDefs[$2]:-false}"} || return ${2:+unset -f "${packageDefs[$2]}"} einfo "Hook function: ${packageDefs[$2]} -- finished successfully. :)"	else		# Each defined key of the "flags" array will become part of CFLAGS.		setupFlags flags base graphite debug || die 'setupFlags failed'		einfo "No hook function defined for ${1}/${2} -- used default flag groups. :)" fi export C{,XX}"FLAGS=${!flags[*]}" einfo "bashrc computed cflags: $(declare -p CFLAGS)" }

cleanup { unset -f setupFlags runHooks main cleanup }
 * 1) Clean up our mess.

main { (( BASH_VERSINFO >= 4 )) || die 'bashrc requires at least Bash 4. Upgrade!'

local -a shopts=(extglob lastpipe) local opt for opt in "${!shopts[@]}"; do		if shopt -q "${shopts[opt]}"; then unset -v 'shopts[opt]' else shopt -s "${shopts[opt]}" fi done (( ${#shopts[@]} )) && trap 'trap RETURN; shopt -u "${shopts[@]}"' RETURN

runHooks "$CATEGORY" "$PN" || die 'hook failed'

I_PROMISE_TO_SUPPLY_PATCHES_WITH_BUGS=1 cleanup }


 * 1) All package hooks below
 * 2) Some examples are given below.
 * 3) If you want to run a hook function for a particular package,
 * 4) the function and category/package names should be defined
 * 5) above in the "categoryDefs" array.
 * 1) above in the "categoryDefs" array.

dgKamFix { setupFlags flags base graphite debug || return export MYSQL_TOOLS_PATH=/usr/share/mysql/scripts }

doBinutils { setupFlags flags base graphite debug || return export EXTRA_ECONF=--enable-gold=default }

_doKsh { setupFlags flags base graphite debug || return pre_pkg_setup { local d='printf "<%s> " "${FUNCNAME[@]}"; echo; inherit flag-o-matic; trap -- DEBUG RETURN' local -a dcmd=( trap "$(printf %q "$d")" DEBUG ) trap "${dcmd[*]}" RETURN printf "<%s> " "${FUNCNAME[@]}"; echo }

pre_src_prepare { append-flags "-fplugin=/usr/$(get_libdir)/llvm/dragonegg.so" -fplugin-arg-dragonegg-enable-gcc-optzns einfo "${FUNCNAME}: applied - $_"

local x srcdir=/home/smorg/doc/programs/ast-open pushd "$srcdir" addwrite "$srcdir" git checkout 2012-05-15 for x in **/Mamfile; do			 -d ${WORKDIR}/${x%Mamfile} && cp "$x" "${WORKDIR}/${x%Mamfile}" done

git checkout master adddeny "$srcdir" popd } }

noFast { setupFlags flags base graphite debug || return unset -v 'flags[-Ofast]' flags[-O3]= }
 * 1) This demonstrates how to modify CFLAGS. It's as simple as setting up defaults
 * 2) by calling setupFlags, then modifying the returned associative array keys.

doChromium { setupFlags flags base graphite debug || return pre_src_configure { egyp { replace-flags -O2 -O3 append-flags -floop-interchange -floop-strip-mine -floop-block local -a cmd=( build/gyp_chromium --depth=. "$@" ) printf '<%q> ' "${cmd[@]}" >&2 "${cmd[@]}" }	} }

main "$@"


 * 1) vim: set fenc=utf-8 ff=unix ft=sh :