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

# thornhump, 11.25 hr with j1
set -x
set -e
set -u
set -o pipefail

echo '$Id: 2017-go.sh,v 1.54 2019/09/20 01:32:11 kenta Exp $'

date
pwd
dirs
hostname
dpkg -l

#xz
gmp=6.1.2

#xz
mpfr=4.0.2

#gz
mpc=1.1.0
# http://www.multiprecision.org/mpc/ is down as of 2019-09-19

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

# xz
isl=0.21
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86724 bug for isl 0.20
# amazingly fixed in 7.4 but not in 8.2 because of weird chronology

#xz
gcc=9.2.0


locker=/afs/sipb/project/gcc-6plus/src
#minutes are what change the most between build attempts
datestring=$(date '+on-%Y%m%d%H-%M%S%z')

jparallel=1
toolsparallel=1
#install_location=/home/kenta/gcc/afs/sipb.mit.edu/project/gcc-6plus/.install/$datestring
install_location=/afs/sipb.mit.edu/project/gcc-6plus/.install/$datestring
if [ -e $install_location ]
then exit 1
fi
buildprefix=/tmp/gcc-6plus
if [ ! -e $buildprefix ]
then ln -s /home/kenta/gcc/gcc-6plus /tmp
    #alternatively mkdir /tmp/gcc-6plus, but that runs out of space on thornhump, needs 22GB
fi
buildroot=$buildprefix/$datestring

mkdir $buildroot

function untar_extra_libs () {
    tar xf $locker/tarballs/gmp-$gmp.tar.xz
    tar xf $locker/tarballs/mpfr-$mpfr.tar.xz
    tar xf $locker/tarballs/mpc-$mpc.tar.gz
    tar xf $locker/tarballs/isl-$isl.tar.xz
}

function symlink_extra_libs () {
    pushd gcc-$gcc
    ln -s ../gmp-$gmp gmp
    ln -s ../mpfr-$mpfr mpfr
    ln -s ../mpc-$mpc mpc
    ln -s ../isl-$isl isl
    popd
}

#avoid problems looking in places when tokens expired
unset PERL5LIB

ubuntu_gcc_install=$buildprefix/4.8-install
my_configure="--enable-threads=posix --with-tune=generic --with-system-zlib --with-multilib-list=m64,m32,mx32"
# caching this saves 40 minutes
if [ ! -e $ubuntu_gcc_install ]
then
    # we need to build a multilib capable, Ada capable gcc, which Ubuntu Trusty does not provide.
    ubuntu_gcc_version=4.8.4
    mkdir $buildroot/4.8-build
    pushd $buildroot/4.8-build
    # this is from a (weird) ubuntu package containing gcc source
    tar xf /usr/src/gcc-4.8/gcc-$ubuntu_gcc_version.tar.xz
    mkdir build
    pushd build
    export CC=gnatgcc
    ../gcc-$ubuntu_gcc_version/configure --prefix=$ubuntu_gcc_install --enable-languages=c++,c,ada --disable-host-shared $my_configure
    time make -j$jparallel
    make install
    popd
    unset CC
    popd
    pushd $buildprefix
    time tar cf 4.8-install.tar.lz -I 'plzip --best' 4.8-install
    # keep a copy of this to save time in the future
    popd
    date
fi
original_path=$PATH
export PATH=$ubuntu_gcc_install/bin:"$PATH"

for file in $locker/tarballs/gmp-$gmp.tar.xz $locker/tarballs/mpfr-$mpfr.tar.xz $locker/tarballs/mpc-$mpc.tar.gz $locker/tarballs/isl-$isl.tar.xz $locker/tarballs/gcc-$gcc.tar.xz
do sha224sum "$file"
done

#gccjit needs enable-host-shared
pushd $buildroot
mkdir gccjit
mkdir rest
pushd gccjit
tar xf $locker/tarballs/gcc-$gcc.tar.xz
pushd gcc-$gcc
#this patch is necessary or else complains about isl/.lib/libisl.a relocation R_X86_64_32 ... recompile with -fPIC
patch -b -p0 < $locker/Makefile.def.patch
autogen Makefile.def
popd

untar_extra_libs
symlink_extra_libs

mkdir build
pushd build
which gcc
grep -h '^ *language *=' $(find ../gcc-$gcc -name config-lang.in) | sort -u
../gcc-$gcc/configure --prefix=$install_location/gcc --enable-languages=jit --enable-host-shared $my_configure
time make -j$jparallel
make install
popd
popd

#everything else gets built with disable-host-shared so we don't have to do LD_LIBRARY_PATH madness to reach our compiled shared libraries gmp mpfr mpc isl
#we deliberately install on top of the previous build, clobbering the old (shared-lib-dependent) files
date
pushd rest
tar xf $locker/tarballs/gcc-$gcc.tar.xz

untar_extra_libs
symlink_extra_libs

mkdir build
pushd build
which gcc
# gcc.gnu.org/install
# "all" does pick up new brig language
../gcc-$gcc/configure --prefix=$install_location/gcc --enable-languages=all,ada,go,obj-c++ --disable-host-shared $my_configure
# search log for The following languages will be built
# egrep 'language=|wing lang'

time make -j$jparallel
make install
popd
popd
popd

pushd $install_location
time tar cf $buildroot/partial.tar.lz -I 'plzip -1' .
popd

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

# we build these because libgccjit.so will need them
export PATH=$install_location/gcc/bin:$original_path
export LD_RUN_PATH=$install_location/gcc/lib64
which gcc
gcc -v

pushd $buildroot
untar_extra_libs
gmpinstall_location=$install_location/gmp
pushd gmp-$gmp
#avoid tuning exactly for this machine
CFLAGS='-O2 -pedantic -fomit-frame-pointer -m64' ./configure --prefix=$gmpinstall_location
CFLAGS='-O2 -pedantic -fomit-frame-pointer -m64' make -j$toolsparallel
CFLAGS='-O2 -pedantic -fomit-frame-pointer -m64' make check -j$toolsparallel
CFLAGS='-O2 -pedantic -fomit-frame-pointer -m64' make install
popd

pushd isl-$isl
#isl is too dumb to make this
mkdir $install_location/isl
./configure --prefix=$install_location/isl --with-gmp=$gmpinstall_location
make -j$toolsparallel
make install
popd

pushd mpfr-$mpfr
./configure --prefix=$install_location/mpfr --with-gmp=$gmpinstall_location
make -j$toolsparallel
make check -j$toolsparallel
make install
popd

pushd mpc-$mpc
./configure --prefix=$install_location/mpc --with-gmp=$gmpinstall_location --with-mpfr=$install_location/mpfr
make -j$toolsparallel
make check -j$toolsparallel
make install
popd

popd

realsuffix=.real
mkdir $install_location/selflaunch
cat > $install_location/selflaunch/selflaunch.sh <<EOF
#!/bin/bash
LD_LIBRARY_PATH=$install_location/gcc/lib64:"\$LD_LIBRARY_PATH" $install_location/gcc/bin/\$(basename "\$0")$realsuffix "\$@"
EOF
chmod a+x $install_location/selflaunch/selflaunch.sh

pushd $install_location/gcc/bin
for file in go gofmt
do mv $file $file$realsuffix
    ln -s ../../selflaunch/selflaunch.sh $file
done

# ubuntu 14.04 has these files.
for file in x86_64-pc-linux-gnu-*
do ln -s "$file" "${file/-pc-/-}"
done

#create suffixed versions
for file in *
do if [ \( "$file" = "${file%-$gcc}"  \) -a ! -e "$file"-$gcc -a  \( "$file" = "${file%$realsuffix}"  \) ]
   then ln -s "$file" "$file"-$gcc
   fi
done
for file in *$realsuffix
do ln -s "$file" ${file%$realsuffix}-$gcc$realsuffix
done
popd

mkdir $install_location/extralibs
pushd $install_location/extralibs
for file in gmp mpfr isl mpc
do ln -s ../$file/lib $file
done
popd

pushd $install_location
for file in gcc/*
do ln -s $file
done
popd

dirs
date
true $0 all done
