Powered by
Movable Type 3.38 mod_perl/2

Linux dynamic environment variable setup

Like having different working directories for different compilers. That means, it is necessary to set up some environment variables to have PATH, LDFLAGS, CPPFLAGS or PKG_CONFIG_PATH point to the wanted libraries and binaries.

Furthermore, I like to put some “packages” in its own prefix (which most people will disagree), like

package1/lib
package1/bin
package1/include

So to keep this flexible, it would be nice to have it just include all directories that match. — Excluding the source ones, that is.

I wrote this down in some function, this might not be the best way to do it however ;-)

typeset -T -Ux PKG_CONFIG_PATH pkg_config_path
typeset -T -Ux LDFLAGS ldflags ' '
typeset -T -Ux CPPFLAGS cppflags ' '
setup_gcc440_world() {
	typeset world=$HOME/gcc440world
	typeset gcc_bin=$HOME/gcc/440/rel/bin
	if [[ $PWD/ == $world/* ]] {
		typeset -U newpath
		newpath=($gcc_bin $path)
		if [[ $newpath != $path ]] {
			path=( $gcc_bin $world/*/bin(/e_'(set -G; nobin=($REPLY/../*(^/)) && [[ ! ${#nobin} -gt 0 ]])'_) $path )
			rehash
			echo "Adjusted environment to gcc440world: "
			echo "	PATH: $path"
			pkg_config_path=( $world/*/lib/pkgconfig(/e_'(set -G; nobin=($REPLY/../../*(^/)) && [[ ! ${#nobin} -gt 0 ]])'_) $pkg_config_path )
			echo "	PKG_CONFIG_PATH: $pkg_config_path"
			typeset lib_dirs
			lib_dirs=( $world/*/lib(/e_'(set -G; nobin=($REPLY/../*(^/)) && [[ ! ${#nobin} -gt 0 ]])'_) )
			ldflags=( ${lib_dirs/#/-L} $ldflags )
			echo "	LDFLAGS: $ldflags"
			typeset inc_dirs
			inc_dirs=( $world/*/include(/e_'(set -G; nobin=($REPLY/../*(^/)) && [[ ! ${#nobin} -gt 0 ]])'_) )
			cppflags=( ${inc_dirs/#/-I} $cppflags )
			echo "	CPPFLAGS: $cppflags"
		}
	} \
	elif [[ $PWD/ != $world/* ]] {
		typeset -U newpath
		newpath=(${path:#$gcc_bin})
		if [[ $newpath != $path ]] {
			path=( ${newpath:#$world/*/bin} )
			rehash
			echo "Adjusted environment without gcc440world"
			echo "	PATH: $path"
			pkg_config_path=( ${pkg_config_path:#$world/*/lib/pkgconfig} )
			echo "	PKG_CONFIG_PATH: $pkg_config_path"
			ldflags=( ${ldflags:#-L$world/*/lib} )
			echo "	LDFLAGS: $ldflags"
			cppflags=( ${cppflags:#-I$world/*/include} )
			echo "	CPPFLAGS: $cppflags"
		}
	}
}
precmd() {
	setup_gcc440_world;
}


TrackBack URL for this entry:
http://anti.teamidiot.de/mt-tb/131