Skip to content

Instantly share code, notes, and snippets.

@jtacoma
Last active August 29, 2015 14:13
Show Gist options
  • Save jtacoma/f3860106ad4b810afda9 to your computer and use it in GitHub Desktop.
Save jtacoma/f3860106ad4b810afda9 to your computer and use it in GitHub Desktop.
pushenv

pushenv

pushenv helps update your Bash or Zsh environment for compilation and linking with dependencies that are built and installed to different paths.

Installation

source pushenv.sh

Usage

pushenv PATH...

Example

Suppose you want to experiment with some C or C++ library whose source is freely available as a tar ball with an autoconf build system. You want the benefits of installing it without the drawbacks of mixing an experimental library into your standard system paths.

tar xzf niftywidgets-0.1.2.tgz
cd niftywidgets-0.1.2
./configure --prefix $HOME/local/niftywidgets-0.1.2
make install

Whenever you want to work with this library, first push compiler, linker, and man page environment variables into your current environment.

pushenv ~/local/niftywidgets-0.1.2

Voila! Plain old gcc and pkg-config commands work without hassle and you can lookup niftywidgets documentation with man.

pushenv() {
# Path separator: a colon in Unix-like environments, a semi-
# colon in Windows environments.
SEP=":"
maybe() {
name=$1
part=$(readlink -m $2)
value=$3
if ! [[ -d $part ]]; then
# $part does not exist or is not a directory
true
elif [[ -z $value ]]; then
echo "export $name=$part"
export $name=$part
elif ! [[ $value == "$part"
|| $value == "$part$SEP"*
|| $value == *"$SEP$part$SEP"*
|| $value == *"$SEP$part" ]]
then
echo "export $name=$part$SEP$value"
export $name=$part$SEP$value
fi
}
for dir in "$@"
do
maybe PATH "$dir/bin" "$PATH"
maybe CPATH "$dir/include" "$CPATH"
maybe LD_LIBRARY_PATH "$dir/lib" "$LD_LIBRARY_PATH"
maybe LIBRARY_PATH "$dir/lib" "$LIBRARY_PATH"
maybe PKG_CONFIG_PATH "$dir/lib/pkgconfig" "$PKG_CONFIG_PATH"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment