Gentoo Logo
Gentoo Spaceship




Note: Due to technical difficulties, the Archives are currently not up to date. GMANE provides an alternative service for most mailing lists.
c.f. bug 424647
List Archive: gentoo-amd64
Navigation:
Lists: gentoo-amd64: < Prev By Thread Next > < Prev By Date Next >
Headers:
To: gentoo-amd64@g.o
From: Samir Mishra <sqmishra@...>
Subject: Emerge Compilation Customization - /etc/portage/bashrc
Date: Tue, 04 Apr 2006 00:41:11 +0400
Hello all,

I've finally got my system compiled with GCC 4 and latest glibc. In the 
process, I have to apply a number of custom compiler and linking flags 
to different packages. I found having to use just /etc/portage/bashrc 
for this too limiting, but I love the ability to hook other scripts in 
using this mechanism. To help me with customizing flags on a per-package 
basis, I created a set of functions (or script) to  help me. They need 
to be sourced into /etc/portage/bashrc.

I'm not much of a programmer, but I'm hoping that someone else will also 
find these functions useful, develop them further and perhaps improve on 
the documentation. Most importantly, I'd appreciate constructive 
criticism on anything related to my approach here.

Thanks in advance.

SM

The documentation I have thus far, included in file --
# ------------------------------------------------------
# -----------------   USAGE NOTES   --------------------
# ------------------------------------------------------
# Set of functions that allow for per-package settings
# when sourced into /etc/portage/bashrc. This file reads
# the file(s), if they exist, in the following sequence -
#   1. /etc/portage/package.env
#   2. /etc/portage/$HOSTNAME/package.env
# These are default filenames and can be changed. These
# files have to follow a given format and are sourced by
# the function "fix_env $filename"
# Default layout of the above files is as follows -
#   1. no >=<! operators
#   2. Col 1 - category/packagename with version
#      and release optional
#   3. Col 2 - ACTION. One of (A,a)dd, (D,d)elete,
#      (N,n)ew or (R,r)eplace.
#   4. Col 3 - ENV_VARIABLE. Environment variable
#   5. Col 4 - VALUES. Values to add, change, replace
#      or create new variable.
#
# Additional package specific config files are then sources
# in the following sequence -
#   1. /etc/portage/env/$CATEGORY/${PN}.conf
#   2. /etc/portage/env/$CATEGORY/${PN}-${PV}.conf
#   3. /etc/portage/env/$CATEGORY/${PN}-${PV}-${PR}.conf
#   4. /etc/portage/env/$CATEGORY/${PN}-${PV}-${PR}.${HOST}.conf
# where default $HOST is the name of the host running the
# ebuild. The above need to be regular sourceable Bash
# script files.
#
# ------------------------------------------------------


#DUMMY FILE

        #DUMMY FILE

# 1. Test nonsense

sys-dummy1/somepkg-1.2.3-r1     Add     CFLAGS      -fweb ${PORTAGE_ROOTDIR}/log_to_another_dir #lets start a comment
sys-dummy1/somepkg-1.2.3        Add     CXXFLAGS    -fvisibility-inlines-hidden #lets start a comment
sys-dummy1/somepkg-1.2.3        Add     CXXFLAGS    -fvisibility-inlines-hidden $DELFLAG $HOSTNAME more_junk #lets start a comment
   sys-dummy1/somepkg-1.2.3       d     CFLAGS       $DELFLAG -ftracer
sys-dummy1/somepkg              New     PORT_LOGDIR ${PORTAGE_ROOTDIR}/log_to_another_dir $SHELL # another comment
sys-dummy1/somepkg-1.2.3        R       PORTAGE_TMPDIR  ${PORTAGE_ROOTDIR}/some_other_tmp_dir ${PORTAGE_ROOTDIR}/log_to_another_dir $DELFLAG
sys-dummy1/somepkg-1.2.3        R       RANDOMVAR  ${PORTAGE_ROOTDIR}/some_random_dir $DELFLAG

# No Effect - package revision version is incorrect
sys-dummy1/somepkg-1.2.3-r3     Add     CFLAGS      -fweb ${PORTAGE_ROOTDIR}/log_to_another_dir #lets start a comment


# 2. Sensible values
sys-dummy3/somepkg-1.2.3-r1     Add     CFLAGS      -fweb #lets start a comment
sys-dummy3/somepkg-1.2.3-r1     Add     CFLAGS      #lets start a comment
sys-dummy3/somepkg-1.2.3        Add     CXXFLAGS    -fvisibility-inlines-hidden #lets start a comment
   sys-dummy3/somepkg-1.2.3       d     CFLAGS      -fomit-frame-pointer -ftracer
sys-dummy3/somepkg              New     PORT_LOGDIR ${PORTAGE_ROOTDIR}/log_to_another_dir # another comment
sys-dummy3/somepkg-1.2.3        R       PORTAGE_TMPDIR  ${PORTAGE_ROOTDIR}/tmp
sys-dummy3/somepkg              R       RANDOMVAR  ${PORTAGE_ROOTDIR}/some_random_dir $DELFLAG

#------------------------------------------------------

ENV="package.env"
ENVDIR="/etc/portage"
HOST=${HOSTNAME}

#------------------------------------------------------

source ./bashrc.fun.sh

#------------------------------------------------------
# Test Variables & file definitions
#------------------------------------------------------
# These variables will override teh defaults for the system.
ENVFILE="./dummy.env"

# For unrealistic test values
CATEGORY="sys-dummy1"

# For realistic test values
#CATEGORY="sys-dummy3"

PN="somepkg"
PV="1.2.3"
PR="r1"

#------------------------------------------------------

CFLAGS="-march=k8 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"
LDFLAGS="-Wl,-O1"
PORTAGE_ROOTDIR="/usr/portage"
PORTAGE_TMPDIR=${PORTAGE_ROOTDIR}/tmp
PORTDIR=${PORTAGE_ROOTDIR}/ports
DISTDIR=${PORTAGE_ROOTDIR}/files/distfiles
PKGDIR=${PORTAGE_ROOTDIR}/files/packages
PORT_LOGDIR=${PORTAGE_ROOTDIR}/log

# environment variables used to affect all values in file
DELFLAG="-fomit-frame-pointer"

fix_env
get_pkg_conf

echo "CFLAGS = $CFLAGS"
echo "CXXFLAGS = $CXXFLAGS"
echo "LDFLAGS = $LDFLAGS"
echo "PORTAGE_ROOTDIR = $PORTAGE_ROOTDIR"
echo "PORTAGE_TMPDIR = $PORTAGE_TMPDIR"
echo "PORTDIR = $PORTDIR"
echo "DISTDIR = $DISTDIR"
echo "PKGDIR = $PKGDIR"
echo "PORT_LOGDIR = $PORT_LOGDIR"
echo "RANDOMVAR = $RANDOMVAR"
# ------------------------------------------------------
# Created: Samir Mishra (2006-04-04 12:27AM)
# No copyright, no guarantees, use at own risk. So far
# seems to work for me.
# ------------------------------------------------------

# ------------------------------------------------------
# -----------------   USAGE NOTES   --------------------
# ------------------------------------------------------
# Set of functions that allow for per-package settings
# when sourced into /etc/portage/bashrc. This file reads
# the file(s), if they exist, in the following sequence -
#   1. /etc/portage/package.env
#   2. /etc/portage/$HOSTNAME/package.env
# These are default filenames and can be changed. These
# files have to follow a given format and are sourced by
# the function "fix_env $filename"
# Default layout of the above files is as follows -
#   1. no >=<! operators
#   2. Col 1 - category/packagename with version
#      and release optional
#   3. Col 2 - ACTION. One of (A,a)dd, (D,d)elete,
#      (N,n)ew or (R,r)eplace.
#   4. Col 3 - ENV_VARIABLE. Environment variable
#   5. Col 4 - VALUES. Values to add, change, replace
#      or create new variable.
#
# Additinal package specific config files are then sources
# in the following sequence -
#   1. /etc/portage/env/$CATEGORY/${PN}.conf
#   2. /etc/portage/env/$CATEGORY/${PN}-${PN}.conf
#   3. /etc/portage/env/$CATEGORY/${PN}-${PN}-${PR}.conf
#   4. /etc/portage/env/$CATEGORY/${PN}-${PN}-${PR}.${HOST}.conf
# where default $HOST is the name of the host running the
# ebuild. The above are regular sourceable Bash script
# files.
#
# ------------------------------------------------------

ENVFILE="${ENVDIR}/${ENV}"
ENVHOSTFILE="${ENVDIR}/${HOST}/${ENV}"
PKGENV="${ENVDIR}/env"

# ------------------------------------------------------


add_env() {
    # add elements to pre-existing environment variable
    # ensuring to skip duplicates
    local evar xval newval count item skip oitem
    evar="$1"
    eval xval="\$${evar}"
    shift
    xval=( $xval )
    newval=( $* )
    count=${#xval[@]}
    for item in ${newval[@]}; do
        skip=0
        for oitem in ${xval[@]}; do
            if [[ "$item" == "$oitem" ]]; then
                skip=1
            fi
        done
        if [[ "$skip" == "0" ]]; then
            xval[$count]=${item}
            let count=$count+1
        fi
    done;
    xval="`echo ${xval[@]}`"
    eval xval=\""$xval"\"
    export ${evar}="$xval"
}


del_env() {
    # delete elements of environment variable using
    # piecewise comparisons
    local evar xval newval x
    evar="$1"
    shift
    eval xval="\$${evar}"
    xval="`echo ${xval[@]}`"
    newval=( $* )
    for x in ${newval[@]}; do
        eval x=\""$x"\"
        xval="${xval/$x/}"
    done
    eval xval=\"`echo ${xval}`\"
    export ${evar}="$xval"
}


new_env() {
    # replace value associated with pre-existing environment
    # variable with new value, or if a global variable does not
    # exist, create it.
    local evar xval
    evar="$1"
    shift
    xval=( "$*" )
    eval xval=\""${xval}"\"
    export ${evar}="$xval"
}


get_pkg_conf() {
    # for sourcing package specific and host specific config files
    local conf
    for conf in ${PN}.conf ${PN}-${PV}.conf ${PN}-${PV}-${PR}.conf ${PN}-${PV}-${PR}.${HOST}.conf; do
        if [[ -f ${PKGENV}/${CATEGORY}/${conf} ]]; then
            source ${PKGENV}/${CATEGORY}/${conf}
        fi
    done
}


eecho() {
	echo -ne '\e[1;34m>\e[1;36m>\e[1;35m>\e[0m ' || echo -n ">>> "
	echo "$*"
}


process_directive() {
    local action
    action=$2
    shift 2
    case "${action:0:1}" in
        #covers 'Add'
        "A" | "a" )
            add_env $*
        ;;
        #covers 'Delete', 'Del'
        "D" | "d" )
            del_env $*
        ;;
        #covers 'Replace', 'New'
        "R" | "r" | "N" | "n" )
            new_env $*
        ;;
    esac
}

load_env() {
    # Not to be used directly. Sources one file at a
    # time sequentially.
	local target flags flag i EnvF item parm count j
	EnvF=$1
	if [ ! -r ${EnvF} ]; then
	    eecho "File does not exist: ${EnvF}"
	    return 1
	fi
	j=1
	while read -a target; do
        let count=0
        unset parm
        for item in ${target[@]}; do
            if [[ "${item:0:1}" != "#" ]]; then
                parm[$count]=$item
            else
                break
            fi
	        let count=$count+1
        done
 		# valid syntax---
 		# no >=<! operators
		# category/packagename  ACTION ENV_VARIABLE VALUES
		if [[ "${parm[0]}" == "${CATEGORY}/${PN}" || "${parm[0]}" == "${CATEGORY}/${PN}-${PV}" || "${parm[0]}" == "${CATEGORY}/${PN}-${PV}-${PR}" ]]; then
            if [[ "$count" -lt "4" ]]; then
                eecho "Incorrect number of parameters; Line ${j}: ${parm[0]}"
                unset parm
                return 0
            else
                eecho "Processing environment directives for package: ${parm[0]}"
    	        process_directive ${parm[@]}
            fi
		fi
	    let j=$j+1
	done < ${EnvF}
}


fix_env() {
	## need bash >= 3
	if [ "${BASH_VERSINFO[0]}" -le 2 ]; then
		eecho "Need bash3 for this script to work"
		return 0
	fi
	## source file with changes to environment
	## do nothing if file does not exist or is not readable
	if [[ "$#" -gt "0" && -r $1 ]]; then
	    load_env $1
	    return 0
	fi
    if [ -r ${ENVFILE} ]; then
        load_env ${ENVFILE}
    fi
    if [ -r ${ENVHOSTFILE} ]; then
        load_env ${ENVHOSTFILE}
    fi
}

#!/bin/bash
#------------------------------------------------------

ENV="package.env"
ENVDIR="/etc/portage"
HOST=${HOSTNAME}

#------------------------------------------------------

source ./bashrc.fun.sh

#------------------------------------------------------
# Test Variables & file definitions
#------------------------------------------------------
# These variables will override teh defaults for the system.
ENVFILE="./dummy.env"

# For unrealistic test values
CATEGORY="sys-dummy1"

# For realistic test values
#CATEGORY="sys-dummy3"

PN="somepkg"
PV="1.2.3"
PR="r1"

#------------------------------------------------------

CFLAGS="-march=k8 -O2 -pipe -fomit-frame-pointer"
CXXFLAGS="${CFLAGS}"
LDFLAGS="-Wl,-O1"
PORTAGE_ROOTDIR="/usr/portage"
PORTAGE_TMPDIR=${PORTAGE_ROOTDIR}/tmp
PORTDIR=${PORTAGE_ROOTDIR}/ports
DISTDIR=${PORTAGE_ROOTDIR}/files/distfiles
PKGDIR=${PORTAGE_ROOTDIR}/files/packages
PORT_LOGDIR=${PORTAGE_ROOTDIR}/log

# environment variables used to affect all values in file
DELFLAG="-fomit-frame-pointer"

fix_env
get_pkg_conf

echo "CFLAGS = $CFLAGS"
echo "CXXFLAGS = $CXXFLAGS"
echo "LDFLAGS = $LDFLAGS"
echo "PORTAGE_ROOTDIR = $PORTAGE_ROOTDIR"
echo "PORTAGE_TMPDIR = $PORTAGE_TMPDIR"
echo "PORTDIR = $PORTDIR"
echo "DISTDIR = $DISTDIR"
echo "PKGDIR = $PKGDIR"
echo "PORT_LOGDIR = $PORT_LOGDIR"
echo "RANDOMVAR = $RANDOMVAR"
Navigation:
Lists: gentoo-amd64: < Prev By Thread Next > < Prev By Date Next >
Previous by thread:
Successful
Next by thread:
nfs mount has now become read-only
Previous by date:
Re: Thunderbird Address book problems
Next by date:
editor, shell, and gnome configuration (was: Successful)


Updated Jun 17, 2009

Summary: Archive of the gentoo-amd64 mailing list.

Donate to support our development efforts.

Copyright 2001-2013 Gentoo Foundation, Inc. Questions, Comments? Contact us.