#!/bin/sh
#######################################################
# Modify following variables to yours
TARBALL_PATH=/home/nick/work-gcc/tarball
PATCHES_PATH=/home/nick/work-gcc/patches
LINUX_HEADERS_PATH=/home/nick/work-gcc/linux-headers-2.6.24.3
INSTALL_PATH=/opt/mipseltools-gcc412-lnx26
BUILD_PATH=`pwd`

######################################################
BINUTILS_VER=binutils-2.17
GCC_VER=gcc-4.1.2
GLIBC_VER=glibc-2.3.6
GLIBC_PORTS_VER=glibc-ports-2.3.6
GLIBC_LINUXTHREADS_VER=glibc-linuxthreads-2.3.6
GDB_VER=gdb-6.0
unset CFLAGS
unset CXXFLAGS
export PATH=${INSTALL_PATH}/bin:$PATH
echo "----------------------------------"
echo "@@ Building binutils ..."
echo "----------------------------------"
# prepare binutils source
cd ${BUILD_PATH}
rm -rf ${BINUTILS_VER} binutils-build
tar jxf ${TARBALL_PATH}/${BINUTILS_VER}.tar.bz2
# configure and build binutils
mkdir -v binutils-build
cd binutils-build
../${BINUTILS_VER}/configure --target=mipsel-linux
--prefix=${INSTALL_PATH}
make CFLAGS="-O2"
# install binutils
make install
echo "----------------------------------"
echo "@@ Building bootstrap gcc ..."
echo "----------------------------------"
# prepare gcc source
cd ${BUILD_PATH}
rm -rf ${GCC_VER} gcc-build
tar jxf ${TARBALL_PATH}/${GCC_VER}.tar.bz2
cd ${BUILD_PATH}/${GCC_VER}/libiberty


##########################################################

cat strsignal.c | sed -e 's/#ifndef HAVE_PSIGNAL/#if 0/g' >junk.c
cp -f strsignal.c strsignal.c.fixed; mv -f junk.c strsignal.c
# configure and build gcc
cd ${BUILD_PATH}
mkdir -v gcc-build
cd gcc-build
../${GCC_VER}/configure --target=mipsel-linux \
      --host=i686-pc-linux-gnu --prefix=${INSTALL_PATH} \
      --disable-shared --disable-threads --disable-multilib \
      --enable-languages=c
make CFLAGS="-O2" all-gcc
# install gcc
make install-gcc
echo "----------------------------------"
echo "@@ Building glibc ..."
echo "----------------------------------"
# prepare glibc source
cd ${BUILD_PATH}
rm -rf ${GLIBC_VER} glibc-build
tar jxf ${TARBALL_PATH}/${GLIBC_VER}.tar.bz2
cd ${GLIBC_VER}
tar jxf ${TARBALL_PATH}/${GLIBC_LINUXTHREADS_VER}.tar.bz2
tar jxf ${TARBALL_PATH}/${GLIBC_PORTS_VER}.tar.bz2
mv ${GLIBC_PORTS_VER} ports
# apply patch
patch -Np1 -i ${PATCHES_PATH}/${GLIBC_VER}-jz.patch
# remove nptl
rm -rf nptl nptl_db
# configure and build glibc
cd ${BUILD_PATH}
mkdir -v glibc-build
cd glibc-build
export CC="mipsel-linux-gcc"
export libc_cv_forced_unwind=yes
export libc_cv_c_cleanup=yes



############################################
../${GLIBC_VER}/configure \
       --host=mips-linux \
       --build=i686-pc-linux-gnu \
       --enable-add-ons \
       --enable-shared \
       --with-cpu=mips32 \
       --prefix=/usr \
       --with-headers=${LINUX_HEADERS_PATH}
make CFLAGS="-O2"
# install glibc
export GLIBC_INSTALL=${BUILD_PATH}/glibc-inst
cd $BUILD_PATH
rm -rf glibc-inst
mkdir -v glibc-inst
cd glibc-build
make install_root=${GLIBC_INSTALL} install
cd ${GLIBC_INSTALL}; tar zcf $BUILD_PATH/glibc-build/glibc-lib.tgz lib
cd ${INSTALL_PATH}; tar zxf $BUILD_PATH/glibc-build/glibc-lib.tgz

##################
# nick fix
####################
rm -rf ${GLIBC_INSTALL}/usr
mkdir -v ${GLIBC_INSTALL}/usr
#######################
#########################
cd ${GLIBC_INSTALL}/usr; tar cfz $BUILD_PATH/glibc-build/glibc-usr.tgz
.
cd ${INSTALL_PATH}/mipsel-linux; tar xzf
$BUILD_PATH/glibc-build/glibc-usr.tgz
tar xfz $BUILD_PATH/glibc-build/glibc-lib.tgz
# install linux kernel headers
cd ${LINUX_HEADERS_PATH}
cp -afr {asm,asm-mips,asm-generic,linux,mtd,scsi,sound}
${INSTALL_PATH}/mipsel-linux/include
# fixed libc.so and libpthread.so
sed -i -e 's/\/usr\/lib\///g'
${INSTALL_PATH}/mipsel-linux/lib/libpthread.so
sed -i -e 's/\/usr\/lib\///g' ${INSTALL_PATH}/mipsel-linux/lib/libc.so
sed -i -e 's/\/lib\///g'
${INSTALL_PATH}/mipsel-linux/lib/libpthread.so
sed -i -e 's/\/lib\///g' ${INSTALL_PATH}/mipsel-linux/lib/libc.so

####################################################

# install localedata
cd ${BUILD_PATH}/glibc-build
cp -v ${PATCHES_PATH}/${GLIBC_VER}-localedata-Makefile
${BUILD_PATH}/${GLIBC_VER}/localedata/Makefile
cp -v ${PATCHES_PATH}/${GLIBC_VER}-localedata-SUPPORTED
${BUILD_PATH}/${GLIBC_VER}/localedata/SUPPORTED
make localedata/install-locales install_root=${INSTALL_PATH}
echo "----------------------------------"
echo "@@ Building final gcc ..."
echo "----------------------------------"
# prepare gcc source
cd $BUILD_PATH
rm -rf ${GCC_VER} gcc-build
tar jxf ${TARBALL_PATH}/${GCC_VER}.tar.bz2
cd ${BUILD_PATH}/${GCC_VER}/libiberty
cat strsignal.c | sed -e 's/#ifndef HAVE_PSIGNAL/#if 0/g' >junk.c
cp -f strsignal.c strsignal.c.fixed; mv -f junk.c strsignal.c
# apply patches
cd ${BUILD_PATH}/${GCC_VER}
patch -p0 < ${PATCHES_PATH}/gcc-4.1_bug27067.patch
# configure and build gcc
cd ${BUILD_PATH}
mkdir -v gcc-build
cd gcc-build
export CC="gcc"
../${GCC_VER}/configure --target=mipsel-linux \
      --host=i686-pc-linux-gnu --prefix=${INSTALL_PATH} \
      --disable-multilib --enable-shared --enable-languages=c,c++ \
      --with-headers=${INSTALL_PATH}/mipsel-linux/include
make CFLAGS="-O2"
# install gcc
make install
# remove sys-include
rm -rf ${INSTALL_PATH}/mipsel-linux/sys-include

#######################################################

cd $BUILD_PATH
# fix symlink
echo "Fix symlink ..."
cd ${INSTALL_PATH}/mipsel-linux/lib
rm libanl.so
ln -s libanl.so.1 libanl.so
rm libBrokenLocale.so
ln -s libBrokenLocale.so.1 libBrokenLocale.so
rm libcrypt.so
ln -s libcrypt.so.1 libcrypt.so
rm libdl.so
ln -s libdl.so.2 libdl.so
rm libm.so
ln -s libm.so.6 libm.so
rm libnsl.so
ln -s libnsl.so.1 libnsl.so
rm libnss_compat.so
ln -s libnss_compat.so.2 libnss_compat.so
rm libnss_dns.so
ln -s libnss_dns.so.2 libnss_dns.so
rm libnss_files.so
ln -s libnss_files.so.2 libnss_files.so
rm libnss_hesiod.so
ln -s libnss_hesiod.so.2 libnss_hesiod.so
rm libnss_nisplus.so
ln -s libnss_nisplus.so.2 libnss_nisplus.so
rm libnss_nis.so
ln -s libnss_nis.so.2 libnss_nis.so

##################################################

rm libresolv.so
ln -s libresolv.so.2 libresolv.so
rm librt.so
ln -s librt.so.1 librt.so
rm libthread_db.so
ln -s libthread_db.so.1 libthread_db.so
rm libutil.so
ln -s libutil.so.1 libutil.so
# install mxu_as and jz_mxu.h
cp -v ${PATCHES_PATH}/mxu_as ${INSTALL_PATH}/bin
chmod +x ${INSTALL_PATH}/bin/mxu_as
cp -v ${PATCHES_PATH}/jz_mxu.h ${INSTALL_PATH}/mipsel-linux/include
echo "----------------------------------"
echo "@@ Building GDB ..."
echo "----------------------------------"
cd $BUILD_PATH
rm -rf ${GDB_VER} gdb-build
tar jxf ${TARBALL_PATH}/${GDB_VER}.tar.bz2
mkdir -v gdb-build
cd gdb-build
export CC="gcc"
../${GDB_VER}/configure --target=mipsel-linux
--prefix=${INSTALL_PATH}
make
make install
echo "@@ Building binutils, glibc, gcc and gdb OK"

##################################################


