#!/bin/sh
GCC_SELECT_VERSION="1.3"
VERSION2=2.95.2
VERSION3=3.1
TEMPORARILY_SKIP=
VALIDATE_SYMLINKS=
LIBS_ARE_ALL_SYMLINKED=1
C_HDRS_ARE_SYMLINKED=1
c_hdrs="assert.h \
float.h \
inttypes.h \
machine/limits.h \
stdarg.h \
stdbool.h \
stdint.h \
varargs.h \
va-ppc.h"
switch_it()
{
local f who status argc=$ local n="\n`echo "usage: ${0 local usage="${0##*/} [-n] [-force] [2 | 3] [-h | --help] [-v | --version]$n [-root] [ -nc | --no-color]"
dashn=
switchto_cc=0
forced=
usr="/usr"
installing=
show_ver=
show_help=
root=
no_color=
while [ $ case $1 in
2 | -2)
if [ $switchto_cc -eq 3 ]; then
echo -e "usage: $usage"
echo " You cannot specify both a 2 and a 3!."
exit 1
fi
switchto_cc=2
shift
;;
3 | -3)
if [ $switchto_cc -eq 2 ]; then
echo -e "usage: $usage"
echo " You cannot specify both a 2 and a 3!."
exit 1
fi
switchto_cc=3
shift
;;
-force | --force)
forced=1
shift
;;
-h | --help)
show_help=1
shift
;;
-n)
dashn="echo -e ""\040"
shift
;;
--no-color | -nc)
no_color=1
shift
;;
-dstroot | --dstroot)
shift
usr="`echo "$1" | sed -e 's,/$,,'`"
if [ "${usr##*/}" != "usr" ]; then
usr="$usr/usr"
fi
installing=1
shift
;;
-root)
root=1
shift
;;
-v | --version)
echo "gcc_select v$GCC_SELECT_VERSION"
show_ver=1
shift
;;
--testing)
shift
do_testing "$1"
shift
;;
*)
echo -e "usage: $usage"
echo " Invalid argument ($1)."
exit 1
;;
esac
done
if false; then
echo "dashn = $dashn"
echo "switchto_cc = $switchto_cc"
echo "forced = $forced"
echo "root = $root"
echo "usr = $usr"
echo "show_ver = $show_ver"
echo "show_help = $show_help"
echo "installing = $installing"
echo "no_color = $no_color"
exit 0
fi
if [ "$show_help" ]; then
echo
echo -e "usage: $usage"
echo
echo "2 Select gcc 2.x as the default compiler."
echo "3 Select gcc 3.x as the default compiler."
echo "-force Ensure the links are correct for the specified version."
echo " even if the one specified is the current version."
echo "-h Display this help info."
echo "--help Same as -h."
echo "-n Show commands to do selection but do not execute them."
echo "-nc Suppress xterm color."
echo "--no-color Same as -nc."
echo "-root Skip 'root' check and assume you have root access."
echo "-v Display gcc_select version number."
echo "--version Same as -v."
echo
exit 0
fi
if [ "$no_color" ]; then
color_on=echo
color_off=echo
else
color_on="echo -e \\e[31m" color_off="echo -e \\e[0m"
fi
actual_ver="`cc -v 2>&1 | grep -i 'gcc version'`"
current_cc="`echo \"$actual_ver\" | sed -e 's/.*gcc version \([^ ]\).*/\1/'`"
if [ $current_cc -ne 2 -a $current_cc -ne 3 ]; then
echo "Error trying to determine current cc version (got $current_cc)"
exit 1
fi
if [ $argc -eq 0 ] || [ $argc -eq 1 -a "$show_ver" ]; then
echo "$actual_ver"
exit 0
fi
if [ "$switchto_cc" = "0" ]; then
echo -e "usage: $usage"
echo " You specify whether you want to switch to 2.x or 3.x."
exit 1
fi
if [ ! "$installing" ]; then
if [ ! "$forced" -a $switchto_cc -eq $current_cc ]; then
echo "You already are using gcc$current_cc as the default compiler."
exit 0
fi
if [ -e /usr/bin/gcc -a ! -L /usr/bin/gcc ] || \
[ -e /usr/bin/g++ -a ! -L /usr/bin/g++ ]; then
echo "You appear to already have a gcc and/or g++ in /usr/bin and they"
echo "are not sym links. If you want to switch to gcc$switch_to you need to"
echo "remove these files first."
exit 1
fi
if [ "$dashn" = "" -a ! "$root" ]; then
who="`whoami 2>/dev/null`"
if [ $? -ne 0 ]; then
who="`id -u 2>/dev/null`"
if [ $? -ne 0 ]; then
echo
echo "Cannot determine if you are running as root because the 'whoami'"
echo "command is not on your system or command path. If you think"
echo "you have root permission then you can bypass this root check by"
echo "specifying the -root option but the script will, of course,"
echo "still fail if you really don't have root access."
echo
exit 1
fi
if [ "$who" = "0" ]; then
who=root
fi
fi
if [ "$who" != "root" ]; then
$color_on
echo -e '*** GCC_SELECT MUST BE RUN AS ROOT. ***'
echo -e '*** NO CHANGES WERE MADE TO YOUR SETUP. ***'
$color_off
exit 1
fi
fi
if [ $current_cc -eq 2 ]; then
backup_gcc2
fi
validate_current_installation
if [ $? -ne 0 ]; then
cd "$cwd"
$color_on
echo -e '*** NO CHANGES WERE MADE TO YOUR SETUP. ***'
$color_off
exit 1
fi
validate_what_were_switching_to
if [ $? -ne 0 ]; then
cd "$cwd"
$color_on
echo -e '*** NO CHANGES WERE MADE TO YOUR SETUP. ***'
$color_off
exit 1
fi
fi
title "Commands to set the gcc$switchto_cc tools sym links..."
installdir "$usr/bin"
$dashn rm -f "$usr/bin/cc" "$usr/bin/gcc" "$usr/bin/g++" "$usr/bin/c++" \
"$usr/bin/c++filt" "$usr/bin/gcov"
if [ $? -ne 0 ]; then
echo "Something is wrong; couldn't rm the existing compiler sym links."
$color_on
echo -e '*** NO CHANGES WERE MADE TO YOUR SETUP. ***'
$color_off
exit 1
fi
status=0
for f in gcc g++ c++filt; do
$dashn ln -s $f$switchto_cc "$usr/bin/$f"
if [ $? -ne 0 ]; then
$((status++))
fi
done
$dashn ln -s gcc$switchto_cc "$usr/bin/cc"
if [ $? -ne 0 ]; then
$((status++))
fi
$dashn ln -s g++$switchto_cc "$usr/bin/c++"
if [ $? -ne 0 -o $status -ne 0 ]; then
echo "Something is wrong; couldn't install the gcc$switchto_cc sym links."
$color_on
echo -e '*** NO CHANGES WERE MADE TO YOUR SETUP. ***'
$color_off
fi
if [ $switchto_cc -eq 3 ]; then
$dashn ln -s gcov3 "$usr/bin/gcov"
if [ "$dashn" != "" ]; then
echo ' fi
fi
if [ ! "$TEMPORARILY_SKIP" ]; then
title "Commands to set the \"default\" sym links..."
installdir "$usr/include/gcc/darwin" "$usr/lib/gcc/darwin" \
"$usr/libexec/gcc/darwin/ppc" "$usr/libexec/gcc/darwin/i386"
set_default "$usr/include/gcc/darwin"
set_default "$usr/libexec/gcc/darwin/ppc"
set_default "$usr/lib/gcc/darwin"
if [ -d "$usr/libexec/gcc/darwin/i386" ]; then
set_default "$usr/libexec/gcc/darwin/i386"
fi
if [ ! "$installing" -a ! "$LIBS_ARE_ALL_SYMLINKED" ]; then
title "Commands to set up the libraries..."
else
title "Commands to set the libraries sym links..."
installdir "$usr/lib" "$usr/local/lib"
fi
$dashn rm -f "$usr/lib/libgcc.a" "$usr/lib/libcc_dynamic.a"
$dashn rm -f "$usr/lib/libcc.a" "$usr/lib/libgcc_static.a"
$dashn rm -f "$usr/lib/libsupc++.a" "$usr/lib/libstdc++.a"
$dashn rm -f "$usr/lib/libcc_kext.a" "$usr/local/lib/libcc_noc++.a"
if [ $switchto_cc -eq 2 ]; then
if [ ! "$installing" -a ! "$LIBS_ARE_ALL_SYMLINKED" ]; then
if [ -f /usr/lib/libgcc_static.a ]; then
$dashn mv /usr/lib/libgcc_static.a /usr/lib/gcc/darwin/$VERSION3
fi
if [ -f /usr/local/lib/libcc_noc++.a -a ! -L /usr/local/lib/libcc_noc++.a ]; then
$dashn mv /usr/local/lib/libcc_noc++.a /usr/lib/gcc/darwin/$VERSION3
fi
fi
$dashn ln -s gcc/darwin/default/libcc_dynamic.a "$usr/lib/libcc_dynamic.a"
$dashn ln -s gcc/darwin/default/libstdc++.a "$usr/lib/libstdc++.a"
if [ -e gcc/darwin/default/libcc_kext.a ]; then
$dashn ln -s gcc/darwin/default/libcc_kext.a "$usr/lib/libcc_kext.a"
fi
if [ "$LIBS_ARE_ALL_SYMLINKED" ]; then
if [ "$installing" ]; then
$dashn ln -s gcc/darwin/default/libcc.a "$usr/lib/libcc.a"
$dashn ln -s ../../lib/gcc/darwin/default/libcc_noc++.a "$usr/local/lib/libcc_noc++.a"
else
if [ -e "/usr/lib/gcc/darwin/default/libcc.a" ]; then
$dashn ln -s gcc/darwin/default/libcc.a "$usr/lib/libcc.a"
fi
if [ -d "$usr/local/lib" -a -e /usr/lib/gcc/darwin/default/libcc_noc++.a ]; then
$dashn ln -s ../../lib/gcc/darwin/default/libcc_noc++.a "$usr/local/lib/libcc_noc++.a"
fi
fi
elif [ ! "$installing" ]; then
if [ -f /usr/lib/gcc/darwin/default/libcc.a ]; then
$dashn mv /usr/lib/gcc/darwin/default/libcc.a /usr/lib/libcc.a
fi
if [ -f /usr/lib/gcc/darwin/default/libcc_noc++.a -a -d /usr/local/lib ]; then
$dashn mv /usr/lib/gcc/darwin/default/libcc_noc++.a /usr/local/lib/libcc_noc++.a
fi
fi
else if [ ! "$installing" -a ! "$LIBS_ARE_ALL_SYMLINKED" ]; then
if [ -f /usr/lib/libcc.a -a ! -L /usr/lib/libcc.a ]; then
$dashn mv /usr/lib/libcc.a /usr/lib/gcc/darwin/$VERSION2/
fi
if [ -f /usr/local/lib/libcc_noc++.a -a ! -L /usr/local/lib/libcc_noc++.a ]; then
$dashn mv /usr/local/lib/libcc_noc++.a /usr/lib/gcc/darwin/$VERSION2/
fi
fi
$dashn ln -s gcc/darwin/default/libcc_kext.a "$usr/lib/libcc_kext.a"
$dashn ln -s gcc/darwin/default/libgcc.a "$usr/lib/libgcc.a"
$dashn ln -s gcc/darwin/default/libgcc.a "$usr/lib/libcc_dynamic.a"
$dashn ln -s gcc/darwin/default/libstdc++.a "$usr/lib/libstdc++.a"
$dashn ln -s gcc/darwin/default/libsupc++.a "$usr/lib/libsupc++.a"
if [ "$LIBS_ARE_ALL_SYMLINKED" ]; then
if [ -f gcc/darwin/default/libgcc_static.a ]; then
$dashn ln -s gcc/darwin/default/libgcc_static.a "$usr/lib/libgcc_static.a"
fi
if [ -d "$usr/local/lib" -a -f ../../lib/gcc/darwin/default/libcc_noc++.a ]; then
$dashn ln -s ../../lib/gcc/darwin/default/libcc_noc++.a "$usr/local/lib/libcc_noc++.a"
fi
elif [ ! "$installing" ]; then
if [ ! -e /usr/lib/libgcc_static.a -a -f /usr/lib/gcc/darwin/default/libgcc_static.a ]; then
$dashn mv /usr/lib/gcc/darwin/default/libgcc_static.a /usr/lib/libgcc_static.a
fi
if [ -d /usr/local/lib ]; then
$dashn mv /usr/lib/gcc/darwin/default/libcc_noc++.a /usr/local/lib/libcc_noc++.a
fi
fi
fi
if [ "$C_HDRS_ARE_SYMLINKED" ]; then
title "Commands to set the C header sym links..."
installdir "$usr/include"
$dashn rm -f `echo $c_hdrs | sed -e 's,\([a-zA-Z./-]*\),'"$usr"'/include/\1,g'`
for f in $c_hdrs; do
if [ "$f" = "machine/limits.h" ]; then
installdir "$usr/include/machine"
$dashn ln -s ../gcc/darwin/default/machine/limits.h $usr/include/machine/limits.h
elif [ "$f" = "va-ppc.h" ]; then
if [ $switchto_cc -eq 2 ]; then
$dashn ln -s gcc/darwin/default/va-ppc.h $usr/include/va-ppc.h
fi
else
$dashn ln -s gcc/darwin/default/$f $usr/include/$f
fi
done
fi
fi
if [ ! "$installing" ]; then
if [ "$dashn" = "" ]; then
echo "Current default compiler is now gcc$switchto_cc."
else
echo
echo "Current default compiler would now be gcc$switchto_cc."
fi
else
if [ "$dashn" = "" ]; then
echo "All gcc$switchto_cc sym links are now installed in the \"$usr\""
else
echo
echo "All gcc$switchto_cc sym links would now be installed in the \"$usr\""
fi
fi
cd "$cwd"
}
backup_gcc2()
{
local f
if [ ! -x /usr/bin/gcc2 -a ! -L /usr/bin/cc ]; then
title "Commands to back up gcc2..."
for f in cc c++ c++filt; do
if [ -x /usr/bin/$f ] && [ ! -L /usr/bin/$f ]; then
if [ "$f" = "cc" ]; then
$dashn rm -f /usr/bin/gcc2 && \
$dashn cp -p /usr/bin/cc /usr/bin/gcc2
elif [ "$f" = "c++" ]; then
$dashn rm -f /usr/bin/g++2 && \
$dashn cp -p /usr/bin/c++ /usr/bin/g++2
else
$dashn rm -f /usr/bin/c++filt2 && \
$dashn cp -p /usr/bin/c++filt /usr/bin/c++filt2
fi
fi
done
fi
if [ ! -f /usr/lib/gcc/darwin/2.95.2/libcc.a -a \
-f /usr/lib/libcc.a -a ! -L /usr/lib/libcc.a ]; then
$dashn cp -p /usr/lib/libcc.a /usr/lib/gcc/darwin/2.95.2/libcc.a
fi
if [ ! -f /usr/lib/gcc/darwin/2.95.2/libcc_noc++.a -a\
-f /usr/local/lib/libcc_noc++.a -a ! -L /usr/local/lib/libcc_noc++.a ]; then
$dashn cp -p /usr/local/lib/libcc_noc++.a /usr/lib/gcc/darwin/2.95.2/libcc_noc++.a
fi
}
set_default()
{
$dashn rm -f "$1/default"
if [ "$switchto_cc" -eq 2 ]; then
$dashn ln -s $VERSION2 "$1/default"
else
$dashn ln -s $VERSION3 "$1/default"
fi
}
installdir()
{
if [ "$installing" ]; then
for dir; do
if [ ! -d "$dir" ]; then
$dashn mkdir -p "$dir"
fi
done
fi
}
validate_current_installation()
{
local f file libs list err=0 default actual_dir
local usr_local_lib_files2= local usr_local_lib_files3=
if [ "$TEMPORARILY_SKIP" ]; then
return 0
fi
if [ $current_cc -eq 2 ]; then
libs="/usr/lib/libcc_dynamic.a /usr/lib/libcc_kext.a /usr/lib/libstdc++.a"
if [ "$LIBS_ARE_ALL_SYMLINKED" ]; then
libs="$libs $usr_local_lib_files2"
fi
else
libs="/usr/lib/libgcc.a /usr/lib/libcc_kext.a /usr/lib/libsupc++.a /usr/lib/libstdc++.a"
if [ "$LIBS_ARE_ALL_SYMLINKED" ]; then
libs="$libs /usr/lib/libgcc_static.a $usr_local_lib_files3"
fi
fi
if [ "$VALIDATE_SYMLINKS" -a ! "$forced" ]; then
list=
for f in /usr/include/gcc/darwin \
/usr/libexec/gcc/darwin/ppc \
/usr/libexec/gcc/darwin/i386 \
/usr/lib/gcc/darwin; do
if [ ! -L "$f/default" ]; then
if [ "$f" != "/usr/libexec/gcc/darwin/i386" -o \
-d "/usr/libexec/gcc/darwin/i386" ]; then
list="$list $f"
fi
continue
fi
command cd "$f/default"
default="`/bin/pwd -P`"
command cd $f
if [ $current_cc -eq 2 ]; then
actual_dir=$VERSION2
else
actual_dir=$VERSION3
fi
if [ ! -d "$actual_dir" ]; then
list="$list $f"
continue
fi
command cd "$f/$actual_dir"
if [ "`/bin/pwd`" != "$default" ]; then
list="$list $f"
fi
done
if [ "$list" != "" ]; then
show_list "The \"default\" sym link in these directories are inconsistent:" "" \
$list
return 1
fi
list=
for f in $libs; do
if [ ! -L "$f" ]; then
list="$list $f"
fi
if [ "$f" = "/usr/lib/libgcc.a" -a ! -L "/usr/lib/libcc_dynamic.a" ]; then
list="$list $f"
fi
done
if [ "$list" != "" ]; then
show_list "The following libraries don't have sym links installed in /usr/lib:" \
"" $list
err=1
fi
fi
if [ $current_cc -eq 2 ]; then
if [ ! "$LIBS_ARE_ALL_SYMLINKED" ]; then
libs="$libs $usr_local_lib_files2"
fi
else
if [ ! "$LIBS_ARE_ALL_SYMLINKED" ]; then
libs="$libs /usr/lib/libgcc_static.a $usr_local_lib_files3"
fi
fi
list=
for f in $libs; do
file="${f##*/}"
if [ $current_cc -eq 2 ]; then
f="/usr/lib/gcc/darwin/$VERSION2/$file"
else
f="/usr/lib/gcc/darwin/$VERSION3/$file"
fi
if [ ! -f "$f" ]; then
if [ "$file" != "libcc_kext.a" -a \
"$file" != "libgcc_static.a" -a \
"$file" != "libcc_noc++.a" ]; then
if [ $current_cc -eq 3 ]; then
list="$list $f"
elif [ "$file" = "libcc.a" ]; then
if [ ! -f "/usr/lib/libcc.a" ]; then
list="$list $f"
fi
elif [ "$file" = "libcc_noc++.a" ]; then
if [ ! -f "/usr/local/lib/libcc_noc++.a" ]; then
list="$list $f"
fi
else
list="$list $f"
fi
fi
fi
done
if [ "$list" != "" ]; then
show_list "The following libraries are not installed:" "" $list
err=1
fi
if [ $err -ne 0 ]; then
echo
if [ "$dashn" = "" ]; then
echo "Cannot switch because the current installation is not what was expected."
else
echo "Wouldn't be able to switch anyway because the current installation was not"
echo "what was expected."
fi
fi
return $err
}
validate_what_were_switching_to()
{
local f libs list
list=
for f in gcc g++ c++filt; do
if [ ! -x /usr/bin/$f$switchto_cc ]; then
list="$list $f$switchto_cc"
fi
done
if [ $switchto_cc -eq 3 ]; then
for f in c++3 gcov3 cpp3; do
if [ "$f" = "c++3" ]; then
if [ ! -x /usr/bin/$f ] && [ ! -L /usr/bin/$f ]; then
list="$list $f"
fi
elif [ ! -x /usr/bin/$f ]; then
list="$list $f"
fi
done
fi
if [ ! "$TEMPORARILY_SKIP" ]; then
if [ $switchto_cc -eq 2 ]; then
command cd /usr/lib/gcc/darwin/$VERSION2
libs="libcc_dynamic.a libcc_kext.a libstdc++.a" else
command cd /usr/lib/gcc/darwin/$VERSION3
libs="libgcc.a libcc_kext.a libsupc++.a libstdc++.a libgcc_static.a" fi
for f in $libs; do
if [ ! -e $f ]; then
if [ "$f" != "libcc_kext.a" -a \
"$f" != "libgcc_static.a" -a \
"$f" != "libcc_noc++.a" ]; then
list="$list $f"
fi
fi
done
fi
if [ "$list" != "" ]; then
show_list "Master copies of the following files are missing:" \
"" $list
return 1
fi
return 0
}
show_list()
{
local info1="$1"
local info2="$2"
shift 2
if [ ${ echo
echo "$info1"
if [ "$info2" != "" ]; then
echo "$info2"
fi
for f; do
echo " $f"
done
fi
}
title()
{
if [ "$dashn" != "" ] && [ ${ echo
for line; do
echo "$line"
done
fi
}
do_testing()
{
case $1 in
move | save)
if [ -f /usr/lib/libcc.a -a ! -L /usr/lib/libcc.a ]; then
rm -f /usr/lib/gcc/darwin/2.95.2/Xlibcc.a
mv /usr/lib/libcc.a /usr/lib/gcc/darwin/2.95.2/Xlibcc.a
fi
if [ -f /usr/lib/gcc/darwin/2.95.2/libcc.a ]; then
rm -f /usr/lib/gcc/darwin/2.95.2/Xlibcc.a
mv /usr/lib/gcc/darwin/2.95.2/libcc.a /usr/lib/gcc/darwin/2.95.2/Xlibcc.a
fi
if [ -f /usr/local/lib/libcc_noc++.a -a ! -L /usr/local/lib/libcc_noc++.a ]; then
rm -f /usr/lib/gcc/darwin/2.95.2/Xlibcc_noc++.a
mv /usr/local/lib/libcc_noc++.a /usr/lib/gcc/darwin/2.95.2/Xlibcc_noc++.a
fi
if [ -f /usr/lib/gcc/darwin/2.95.2/libcc_noc++.a ]; then
rm -f /usr/lib/gcc/darwin/2.95.2/Xlibcc_noc++.a
mv /usr/lib/gcc/darwin/2.95.2/libcc_noc++.a /usr/lib/gcc/darwin/2.95.2/Xlibcc_noc++.a
fi
rm -f /usr/lib/libcc.a /usr/local/lib/libcc_noc++.a
exit 0
;;
restore)
if [ -f /usr/lib/gcc/darwin/2.95.2/Xlibcc.a ]; then
mv /usr/lib/gcc/darwin/2.95.2/Xlibcc.a /usr/lib/gcc/darwin/2.95.2/libcc.a
fi
if [ -f /usr/lib/gcc/darwin/2.95.2/Xlibcc_noc++.a ]; then
mv /usr/lib/gcc/darwin/2.95.2/Xlibcc_noc++.a /usr/lib/gcc/darwin/2.95.2/libcc_noc++.a
fi
actual_ver="`cc -v 2>&1 | grep -i 'gcc version'`"
current_cc="`echo \"$actual_ver\" | sed -e 's/.*gcc version \([^ ]\).*/\1/'`"
if [ $current_cc -eq 2 ]; then
rm -f /usr/lib/libcc.a /usr/local/lib/libcc_noc++.a
ln -s gcc/darwin/default/libcc.a "/usr/lib/libcc.a"
ln -s ../../lib/gcc/darwin/default/libcc_noc++.a "/usr/local/lib/libcc_noc++.a"
fi
exit 0
;;
*)
echo "Invalid testing option"
exit 1
;;
esac
}
switch_it "$@"