Skip to content

Instantly share code, notes, and snippets.

@Kronuz
Last active April 4, 2018 18:12
Show Gist options
  • Save Kronuz/a87d26f82b1956fa2eb85007542a006a to your computer and use it in GitHub Desktop.
Save Kronuz/a87d26f82b1956fa2eb85007542a006a to your computer and use it in GitHub Desktop.
Compiler selector for macOS
compiler() {
# After installing compilers in macOS:
# brew install gcc [email protected] gcc@5 gcc@6 [email protected] llvm@4 llvm@5
# Use compiler <compiler> to select it. Ex.:
# compiler llvm@5
compiler=$1
if [ -z "$compiler" ]; then
if [ -z "$COMPILER" ]; then
echo "* default"
ls -1 "/usr/local/opt/" | grep -E "gcc|llvm" | sed -e "s#^# #"
else
echo " default"
ls -1 "/usr/local/opt/" | grep -E "gcc|llvm" | sed -e "s#^# #" | sed -e "s# $COMPILER\$#* $COMPILER#"
fi
return 1
fi
case $1 in
gcc*)
if [ -d "/usr/local/opt/$compiler" ]; then
export COMPILER=$compiler
export COMPILER_PATH="/usr/local/opt/$compiler"
export PATH="$COMPILER_PATH/bin:$(echo "$PATH" | sed -e 's#:/usr/local/opt/[^/]*/bin##')"
export CC="$(ls $COMPILER_PATH/bin/gcc* | head -1)"
export CXX="$(ls $COMPILER_PATH/bin/g++* | head -1)"
export CPP="$(ls $COMPILER_PATH/bin/cpp* | head -1)"
export AR="$(ls $COMPILER_PATH/bin/gcc-ar* | head -1)"
export NM="$(ls $COMPILER_PATH/bin/gcc-nm* | head -1)"
export RANLIB="$(ls $COMPILER_PATH/bin/gcc-ranlib* | head -1)"
export CFLAGS="-I$COMPILER_PATH/include"
export CXXFLAGS="-I$COMPILER_PATH/include"
export CPPFLAGS="-I$COMPILER_PATH/include"
export LDFLAGS="-L$COMPILER_PATH/lib -Wl,-rpath,/usr/local/opt/$compiler/lib"
unset ASAN_SYMBOLIZER_PATH
unset MSAN_SYMBOLIZER_PATH
unset TSAN_SYMBOLIZER_PATH
unset UBSAN_SYMBOLIZER_PATH
alias CC=$CC
alias CXX=$CXX
alias CPP=$CPP
alias AR=$AR
alias NM=$NM
alias RANLIB=$RANLIB
else
echo "/usr/local/opt/$compiler not found!"
return 1
fi
;;
llvm*)
if [ -d "/usr/local/opt/$compiler" ]; then
export COMPILER=$compiler
export COMPILER_PATH="/usr/local/opt/$compiler"
export PATH="$COMPILER_PATH/bin:$(echo "$PATH" | sed -e 's#:/usr/local/opt/[^/]*/bin##')"
export CC="$(ls $COMPILER_PATH/bin/clang* | head -1)"
export CXX="$(ls $COMPILER_PATH/bin/clang++* | head -1)"
export CPP="$(ls $COMPILER_PATH/bin/clang-cpp* | head -1)"
export AR="$(ls $COMPILER_PATH/bin/llvm-ar* | head -1)"
export NM="$(ls $COMPILER_PATH/bin/llvm-nm* | head -1)"
export RANLIB="$(ls $COMPILER_PATH/bin/llvm-ranlib* | head -1)"
export CFLAGS="-I$COMPILER_PATH/include"
export CXXFLAGS="-I$COMPILER_PATH/include"
export CPPFLAGS="-I$COMPILER_PATH/include"
export LDFLAGS="-L$COMPILER_PATH/lib -Wl,-rpath,$COMPILER_PATH/lib"
export ASAN_SYMBOLIZER_PATH="$(ls $COMPILER_PATH/bin/llvm-symbolizer* | head -1)"
export MSAN_SYMBOLIZER_PATH="$(ls $COMPILER_PATH/bin/llvm-symbolizer* | head -1)"
export TSAN_SYMBOLIZER_PATH="$(ls $COMPILER_PATH/bin/llvm-symbolizer* | head -1)"
export UBSAN_SYMBOLIZER_PATH="$(ls $COMPILER_PATH/bin/llvm-symbolizer* | head -1)"
alias CC=$CC
alias CXX=$CXX
alias CPP=$CPP
alias AR=$AR
alias NM=$NM
alias RANLIB=$RANLIB
else
echo "/usr/local/opt/$compiler not found!"
return 1
fi
;;
default)
export PATH="$(echo "$PATH" | sed -e 's#:/usr/local/opt/[^/]*/bin##')"
unset COMPILER
unset COMPILER_PATH
unset CC
unset CXX
unset CPP
unset AR
unset NM
unset RANLIB
unset CFLAGS
unset CXXFLAGS
unset CPPFLAGS
unset LDFLAGS
unset ASAN_SYMBOLIZER_PATH
unset MSAN_SYMBOLIZER_PATH
unset TSAN_SYMBOLIZER_PATH
unset UBSAN_SYMBOLIZER_PATH
unalias CC
unalias CXX
unalias CPP
unalias AR
unalias NM
unalias RANLIB
;;
esac
if [ -z "$COMPILER" ]; then
echo "Using default compiler"
else
echo "Using $COMPILER compiler"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment