# load Intel compilers and mkl
source /theoryfs2/common/software/intel2015/bin/compilervars.sh intel64
# force static link to Intel mkl, except for openmp
MCONDA=${PREFIX}/lib
MKLROOT=/theoryfs2/common/software/intel2015/composer_xe_2015.3.187/mkl/lib/intel64
#LAPACK_INTERJECT="-Wl,--start-group ${MKLROOT}/libmkl_cdft_core.a ${MKLROOT}/libmkl_intel_lp64.a ${MKLROOT}/libmkl_core.a ${MKLROOT}/libmkl_intel_thread.a ${MKLROOT}/libmkl_blacs_intelmpi_lp64.a -Wl,--end-group -lpthread -liomp5 -lm -ldl"  #works2

#MKLROOT=/theoryfs2/common/software/intel2015/composer_xe_2015.3.187/mkl  # works2  # b4
#LAPACK_INTERJECT="-Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_cdft_core.a ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_core.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a ${MKLROOT}/lib/intel64/libmkl_blacs_intelmpi_lp64.a -Wl,--end-group -lpthread -liomp5 -lm -ldl"  #works2 # b4

#LAPACK_INTERJECT="${MKLROOT}/libmkl_intel_lp64.a;${MKLROOT}/libmkl_intel_thread.a;${MKLROOT}/libmkl_core.a;-liomp5;-lm"  # b5
#LAPACK_INTERJECT="${MKLROOT}/libmkl_intel_lp64.a ${MKLROOT}/libmkl_intel_thread.a ${MKLROOT}/libmkl_core.a -liomp5 -lm"
#LAPACK_INTERJECT="-Wl,--whole-archive;-Wl,--start-group;${MKLROOT}/libmkl_intel_lp64.a;${MKLROOT}/libmkl_intel_thread.a;${MKLROOT}/libmkl_core.a;${MKLROOT}/libmkl_cdft_core.a;${MKLROOT}/libmkl_blacs_lp64.a;-Wl,--end-group;-Wl,--no-whole-archive;${MCONDA}/libiomp5.so;-lm"
#LAPACK_INTERJECT="-Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_cdft_core.a ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_core.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a ${MKLROOT}/lib/intel64/libmkl_blacs_intelmpi_lp64.a -Wl,--end-group -lpthread -lm -ldl"  #works2
#LAPACK_INTERJECT="${MKLROOT}/libmkl_intel_lp64.so;${MKLROOT}/libmkl_intel_thread.so;${MKLROOT}/libmkl_core.so;-liomp5;-lm"  # works
LAPACK_INTERJECT="-L${MKLROOT};-lmkl_intel_lp64;-lmkl_intel_thread;-lmkl_core;-liomp5;-lm"  # works  # b6

# link against older libc for generic linux
TLIBC=/theoryfs2/ds/cdsgroup/psi4-compile/psi4cmake/psi4/glibc2.12rpm
#LIBC_INTERJECT="-L${TLIBC}/usr/lib64 ${TLIBC}/lib64/libpthread.so.0 ${TLIBC}/lib64/libc.so.6"
LIBC_INTERJECT="${MCONDA}/libgcc_s.so.1;-L${TLIBC}/usr/lib64;${TLIBC}/lib64/libpthread.so.0;${TLIBC}/lib64/libc.so.6"
# round off with pre-detected dependencies
HDF5_INTERJECT="${MCONDA}/libhdf5.so;${MCONDA}/libhdf5_hl.so;${MCONDA}/libhdf5.so;-lrt;-lz;-ldl;-lm"

mkdir build
cd build
cmake \
    -DCMAKE_CXX_COMPILER=icpc \
    -DCMAKE_C_COMPILER=icc \
    -DEXTRA_C_FLAGS=" " \
    -DEXTRA_CXX_FLAGS=" " \
    -DMKL=ON \
    -DBUILD_DOXYGEN=OFF \
    -DBUILD_SPHINX=OFF \
    -DENABLE_TESTS=OFF \
    -DENABLE_GENERIC=ON \
    -DENABLE_XHOST=OFF \
    -DLIBC_INTERJECT="${LIBC_INTERJECT}" \
    -DLAPACK_LIBRARIES="${LAPACK_INTERJECT}" \
    -DHDF5_LIBRARIES="${HDF5_INTERJECT}" \
    -DHDF5_INCLUDE_DIRS="${MCONDA}/../include" \
    -DCMAKE_INSTALL_PREFIX=${PREFIX} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    ${SRC_DIR}
make -j${CPU_COUNT} VERBOSE=1
make install

#    -DLAPACK_LIBRARIES="${LAPACK_INTERJECT}" \
#    -DHDF5_LIBRARIES="${HDF5_INTERJECT}" \
#    -DHDF5_INCLUDE_DIRS="${MCONDA}/../include" \
#    -DSTATIC_ONLY=ON \  # works2
#    -DLAPACK_LIBRARIES="${LIBC_INTERJECT} ${LAPACK_INTERJECT}" \

#    -DCMAKE_CXX_FLAGS=-Wl,-rpath,${PREFIX}/lib:/theoryfs2/ds/cdsgroup/miniconda/lib \
#    -DLAPACK_LIBRARIES="${LIBC_INTERJECT} ${LAPACK_INTERJECT}" \
#    -DBUILD_EXE=OFF \
#    -DENABLE_TESTS=ON \

