#!/bin/bash
# StarPU --- Runtime system for heterogeneous multicore architectures.
#
# Copyright (C) 2013-2020  Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
# Copyright (C) 2013       Thibaut Lambert
#
# StarPU is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at
# your option) any later version.
#
# StarPU is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the GNU Lesser General Public License in COPYING.LGPL for more details.
#
ROOT_DIR=$(dirname $0)

cat > ./mic-config.log << EOF
This file was created by StarPU mic-configure

 $ $0 $*
EOF

prefix="/usr/local"
coi_dir="/opt/intel/mic/coi"
scif_dir="/opt/intel/mic/scif"
mic_host="x86_64-k1om-linux"
declare -a host_params
declare -a mic_params
unset host_params
unset mic_params
native_mic=0
mpi=0
for arg in "$@"
do
	case $arg in
		--prefix=*)
			prefix="${arg#--prefix=}"
			;;
		--with-coi-dir=*)
			coi_dir="${arg#--with-coi-dir=}"
			;;
		--with-scif-dir=*)
			scif_dir="${arg#--with-scif-dir=}"
			;;
		--mic-host=*)
			mic_host="${arg#--mic-host=}"
			;;
	        --enable-native-mic)
		        native_mic=1
		        ;;
		--with-compiler=*)
			compiler="${arg#--with-compiler=}"
			;;
		--with-mic-param=*)
			mic_params+=("${arg#--with-mic-param=}")
			;;
		--with-host-param=*)
			host_params+=("${arg#--with-host-param=}")
			;;
		--with-mpi*)
			mpi=1
			;;
		--help)
			cat << EOF
mic-configure specific options:

  --with-coi-dir=DIR	Specify directory that contains
			device-linux-release/lib/libcoi_device and 
			host-linux-release/lib/libcoi_host and 
  --with-scif-dir=DIR	Specify directory that contains
			device-linux-release/lib/libscif_device and 
			host-linux-release/lib/libscif_host and 
  --mic-host=HOST	Specify the precise Phi host to build for
			(default: k1om)
  --with-compiler=[icc|gcc]
			Specify whether to build with icc or with gcc
  --enable-native-mic	Only build the Phi binaries
  --with-mic-param=--OPTION
			Pass --OPTION to the Phi configure script
  --with-host-param=--OPTION
			Pass --OPTION to the host configure script
EOF
			;;
	esac
done

if [ -z "$compiler" ]
then
    # Test gcc compiler
    x=$(type -t ${mic_host}-gcc)
    if [ -z "$x" ]
    then
	# Test icc compiler
	echo "int main(int argc, char **argv) { return 0; }" > /tmp/icc_$USER_$$.c
	icc -mmic /tmp/icc_$USER_$$.c > /dev/null 2>/tmp/icc_$USER_$$.err
	l=$(grep -c "invalid argument" /tmp/icc_$USER_$$.err)
	if [ "$l" != "0" ]
	then
	    echo "[error] no compiler found. please add path to either ${mic_host}-gcc or to an enabled mic icc compiler in your PATH"
	    exit 1
	else
	    compiler="icc"
	fi
    else
	compiler="gcc"
    fi
fi

dev_list="host mic"
if [ "$native_mic" -eq "1" ]
then
    dev_list="mic"
fi

# prepend mic_params with "--with-mpicc=mpicc -mmic", to allow possible override by the user
if [ $mpi = 1 ]
then
	mic_params=("--with-mpicc=mpicc -mmic" "${mic_params[@]}")
	mic_params=("--with-mpifort=mpifort -mmic" "${mic_params[@]}")
fi

for arch in $dev_list #host mic
do
	# We call the configure script from a build directory further in the
	# arborescence

	case $ROOT_DIR in
		/*) command="${ROOT_DIR}/configure";;
		*) command="../${ROOT_DIR}/configure";;
	esac

	declare -a params
	params=("--prefix=$prefix/$arch" "--disable-fstack-protector-all")

	if [ "$arch" = mic ] ; then
		if [ $compiler = "icc" ] ; then
		    export CC="icc -mmic"
		    export CXX="icc -mmic"
		    export LD="icc -mmic"
		    export CXXLD="icc -mmic"
		    export F77="ifort -mmic"
		    export FC="ifort -mmic"
		else
		    # let configure auto-detect GNU cross-building tools
		    unset CC
		    unset CXX
		    unset LD
		    unset CXXLD
		    unset F77
		    unset FC
		    params+=(--disable-fortran)
		fi
	fi

	if [ "$native_mic" -eq "0" ]
	then
		params+=(--enable-mic "--with-coi-dir=$coi_dir" "--with-scif-dir=$scif_dir")
	fi

	if test x$arch = xmic ; then
	    params+=(--host=$mic_host --disable-build-doc)
	    if [ "$native_mic" -eq "1" ]
	    then
		params+=(--enable-maxcpus=250)
	    else
		params+=("--with-coi-lib-dir=$coi_dir/device-linux-release/lib" "--with-scif-lib-dir=$scif_dir/device-linux-release/lib")
	    fi
	else
	    params+=("--with-coi-lib-dir=$coi_dir/host-linux-release/lib" "--with-scif-lib-dir=$scif_dir/host-linux-release/lib")
	fi

	# If the build directory doesn't exist yet, create it
	if [ ! -d "build_${arch}" ] ; then
		mkdir "build_${arch}"
	fi

	cd "build_${arch}"

	if test x$arch = xmic ; then
		LIBRARY_PATH=$SINK_LIBRARY_PATH:$MIC_LIBRARY_PATH \
		INCLUDE=$SINK_INCLUDE \
		C_INCLUDE_PATH=$SINK_C_INCLUDE_PATH \
		CPLUS_INCLUDE_PATH=$SINK_CPLUS_INCLUDE_PATH \
		PKG_CONFIG_PATH=$SINK_PKG_CONFIG_PATH \
		$command "$@" "${params[@]}" "${mic_params[@]}"
		MIC_BUILD_ENV="\
LIBRARY_PATH=$SINK_LIBRARY_PATH:$MIC_LIBRARY_PATH \\
	INCLUDE=$SINK_INCLUDE \\
	C_INCLUDE_PATH=$SINK_C_INCLUDE_PATH \\
	CPLUS_INCLUDE_PATH=$SINK_CPLUS_INCLUDE_PATH \\
	PKG_CONFIG_PATH=$SINK_PKG_CONFIG_PATH \\\
"
	else
		$command "$@" "${params[@]}""${host_params[@]}"
	fi
	if [ "$?" != 0 ]
	then
		exit $?
	fi
	cd ..
done
if [ "$native_mic" -eq "1" ]
then
cat > Makefile << EOF
all:
	$MIC_BUILD_ENV
	\$(MAKE) \$(MFLAGS) -C build_mic

clean:
	\$(MAKE) \$(MFLAGS) -C build_mic clean

distclean: clean
	rm -f Makefile

check:
	$MIC_BUILD_ENV
	\$(MAKE) \$(MFLAGS) -C build_mic check

showfailed:
	@\$(MAKE) \$(MFLAGS) -C build_mic showfailed

showcheck:
	\$(MAKE) \$(MFLAGS) -C build_mic showcheck

install:
	$MIC_BUILD_ENV
	\$(MAKE) \$(MFLAGS) -C build_mic install
	ln -sf "${prefix}/mic/lib/pkgconfig/starpu-1.3.pc" "${prefix}/mic/lib/pkgconfig/starpu-1.3-mic.pc"
EOF
else
cat > Makefile << EOF
all:
	\$(MAKE) \$(MFLAGS) -C build_host
	$MIC_BUILD_ENV
	\$(MAKE) \$(MFLAGS) -C build_mic

clean:
	\$(MAKE) \$(MFLAGS) -C build_host clean
	\$(MAKE) \$(MFLAGS) -C build_mic clean

distclean: clean
	rm -f Makefile

check:
	\$(MAKE) \$(MFLAGS) -C build_host check
	$MIC_BUILD_ENV
	\$(MAKE) \$(MFLAGS) -C build_mic check ; \
	RET=\$\$? ; \
	STARPU_NCPUS=0 \$(MAKE) \$(MFLAGS) -C build_mic check && [ \$\$RET == 0 ]

showfailed:
	@\$(MAKE) \$(MFLAGS) -C build_host showfailed
	@\$(MAKE) \$(MFLAGS) -C build_mic showfailed

showcheck:
	\$(MAKE) \$(MFLAGS) -C build_host showcheck
	\$(MAKE) \$(MFLAGS) -C build_mic showcheck

install:
	\$(MAKE) \$(MFLAGS) -C build_host install
	$MIC_BUILD_ENV
	\$(MAKE) \$(MFLAGS) -C build_mic install
	ln -sf "${prefix}/mic/lib/pkgconfig/starpu-1.3.pc" "${prefix}/mic/lib/pkgconfig/starpu-1.3-mic.pc"
EOF
fi
