#!/bin/bash
set -x
set -e
set -o pipefail
set -u

date
echo "\$Id: llvm-clang-build.sh,v 1.38 2017/10/09 07:33:42 kenta Exp kenta $"

version=3.7.1
src=/mit/ghc/src/tarballs
unset PERL5LIB


jparallel_cmake=5
jparallel_llvm=1

basedir=/var/tmp/ken_ta/hdd/ghc/full-llvm-37-gcc7/do
installdir=$basedir/install
cmake_version=3.9.4

mygcc=/afs/sipb.mit.edu/project/gcc-6plus/
#still needed, or else uses usr/lib/stdc++ (llvm-tblgen)
libflag=-DCMAKE_EXE_LINKER_FLAGS=-Wl,-rpath,$mygcc/lib64
#libflag=
export PATH=$mygcc/bin:$PATH
export LD_RUN_PATH=$mygcc/lib64

gcc -v

mkdir -p $basedir
# some configure steps, e.g., subversion look at HOME dotfiles which does not work with no tokens
export HOME=$basedir
cd $basedir
rm -fr cmake-$cmake_version
rm -fr llvm-$version.src
rm -fr build
tar xf $src/cmake-$cmake_version.tar.gz
pushd cmake-$cmake_version
./bootstrap --prefix=$basedir/cmake
make -j$jparallel_cmake VERBOSE=1
make install
popd
date
export PATH=$basedir/cmake/bin:"$PATH"

which cmake
cmake --version

#export PATH=/afs/sipb.mit.edu/project/gcc-6plus/bin:"$PATH"
#which g++
#g++ --version

tar xf $src/llvm-$version.src.tar.xz
pushd llvm-$version.src/tools
tar xf $src/cfe-$version.src.tar.xz
popd
mkdir build
pushd build

#for libgo.so.11
#export LD_LIBRARY_PATH=$mygcc/lib64
which c++
c++ -v
#-DCMAKE_EXE_LINKER_FLAGS=-Wl,-rpath,/afs/sipb.mit.edu/project/gcc-6plus/lib
#having to set compiler manually, not finding it in the path, is new between of cmake 3.7- 3.9
cmake -DCMAKE_INSTALL_PREFIX:PATH=$installdir -DLLVM_TARGETS_TO_BUILD:STRING=X86 -DCMAKE_CXX_COMPILER=$mygcc/bin/g++ -DCMAKE_C_COMPILER=$mygcc/bin/gcc $libflag ../llvm-$version.src
make VERBOSE=1 -j$jparallel_llvm

make install
popd

function integrity() {
    pushd $installdir
    # for the logs, potentially to detect integrity loss
    ls -laR --time-style=+%s -b
    find . -type f -print0 | xargs -0 sha224sum
    popd
    true integrity check done
}

integrity

dirs
date
echo $0 all done
