#!/bin/bash
# It is 2016.  Time for a new script.

#todo: go cannot find gcc/lib64
# need to special case in selflaunch

set -x
set -e
# no longer use this script, use 2017-go.sh
exit 1
date
#https://gcc.gnu.org/install/prerequisites.html

#xz
gmp=6.1.2

#xz
mpfr=3.1.5

#gz
mpc=1.0.3

# isl stands for Integer Set Library.  The version of fsf is older (0.16.1) than the one at inria (0.17.1), but we use the recommended fsf version.  There is no cryptographic signature.

#bz2
#isl=0.16.1

#lets try the inria version, dl bz2 version even though xz exists
isl=0.18

#bz2
gcc=7.1.0


# watch out of quota in /tarballs
tarballs=/afs/sipb/project/gcc-6plus/src/tarballs

#little-endian to make it easier to tab
# but too confusing
#datestring=$(date '+%S%M%H-%d%m-%Y%z')

#minutes are what change the most between build attempts
datestring=$(date '+go-%Y%m%d%H-%M%S%z')

buildroot=/var/tmp/.ethanhunt/gcc/build/$datestring

target=/hdd-scratch/kenta/gcc/afs/sipb.mit.edu/project/gcc-6plus/.install/$datestring

#export LD_RUN_PATH=/afs/sipb.mit.edu/project/gcc-6plus/lib
#needed for gnat
export PATH=/afs/sipb.mit.edu/project/gcc-6plus/bin:"$PATH"

echo PATH=$PATH
echo LD_RUN_PATH=$LD_RUN_PATH
echo LD_LIBRARY_PATH=$LD_LIBRARY_PATH

echo '$Id: 2016-go.sh,v 1.25 2017/10/06 04:57:39 kenta Exp $'
which gcc

if [ -e $buildroot ]
then exit 1
fi

# kenta has a local perl dir, which causes perl to fail
unset PERL5LIB

#if not afs
#kdestroy
#unlog
tokens

#do all of these first to make sure the files exist
mkdir -p $buildroot
cd $buildroot
for file in $tarballs/gmp-$gmp.tar.xz $tarballs/mpfr-$mpfr.tar.xz $tarballs/mpc-$mpc.tar.gz $tarballs/isl-$isl.tar.bz2 $tarballs/gcc-$gcc.tar.bz2
do sha224sum "$file"
    tar xf "$file"
done

# trigger building these automatically
pushd gcc-$gcc
ln -s ../gmp-$gmp gmp
ln -s ../mpfr-$mpfr mpfr
ln -s ../mpc-$mpc mpc
ln -s ../isl-$isl isl
popd

mkdir -p $target
pushd $target
mkdir selflaunch
cat > selflaunch/selflaunch.sh <<EOF
#!/bin/bash

# we need to set LD_LIBRARY_PATH because otherwise gcc only looks in
# system locations for the shared libraries it needs to function, most
# notably isl, which is a new requirement not present in gcc included
# with ubuntu 14.04.

#set -x
set -e

# skip gcc libraries because we assume gcc knows where to find them if
# it needs them.  this assumption has yet to be tested.

for i in gmp isl mpc mpfr
do export LD_LIBRARY_PATH=$target/\$i/lib:"\$LD_LIBRARY_PATH"
done

my_basename=\$(basename "\$0")

$target/gcc/bin/"\$my_basename" "\$@"
EOF

#might need to special-case go, easy to test by just invoking it

chmod a+x selflaunch/selflaunch.sh
popd

mkdir gcc-build
pushd gcc-build

#all is not all
#some of these cribbed from gcc -v
moreflags='--with-system-zlib --enable-threads=posix --enable-host-shared --with-tune=generic'

../gcc-$gcc/configure --prefix=$target/gcc $moreflags
nice -19 time make -j4
pushd ..
nice -19 time tar cf gcc-build.tar.bz2 -I pbzip2 gcc-build
popd
make install
popd

exit

pushd $target
pushd gcc/bin
ln -s gcc cc
popd

mkdir bin
pushd bin
for file in ../gcc/bin/*
do ln -s ../selflaunch/selflaunch.sh "$(basename "$file")"
done
popd

# for the logs, potentially to detect integrity loss
ls -laR --time-style=+%s -b
find . -type f -print0 | xargs -0 sha224sum

popd

dirs
date
# 5 hours on /scratch
