#!/bin/sh
#
# toolchain detection
#

C_COMPILERS="gcc clang suncc cc"
CPP_COMPILERS="g++ clang++ sunCC CC"
unset TOOLCHAIN_NAME
unset TOOLCHAIN_CC
unset TOOLCHAIN_CXX

check_c_compiler()
{
	cat > "$TEMP_DIR/test.c" << __EOF__
/* test file */
#include <stdio.h>
int main(int argc, char **argv) {
#if defined(__clang__)
	printf("clang\n");
#elif defined(__GNUC__)
	printf("gcc\n");
#elif defined(__sun)
	printf("suncc\n");
#else
	printf("unknown\n");
#endif
	return 0;
}
__EOF__
	rm -f "$TEMP_DIR/checkcc"
	$1 -o "$TEMP_DIR/checkcc" $CFLAGS $LDFLAGS "$TEMP_DIR/test.c" 2> /dev/null
}

check_cpp_compiler()
{
	cat > "$TEMP_DIR/test.cpp" << __EOF__
/* test file */
#include <iostream>
int main(int argc, char **argv) {
#if defined(__clang__)
	std::cout << "clang" << std::endl;
#elif defined(__GNUC__)
	std::cout << "gcc" << std::endl;
#elif defined(__sun)
	std::cout << "suncc" << std::endl;
#else
	std::cout << "cc" << std::endl;
#endif
	return 0;
}
__EOF__
	rm -f "$TEMP_DIR/checkcc"
	$1 -o "$TEMP_DIR/checkcc" $CXXFLAGS $LDFLAGS "$TEMP_DIR/test.cpp" 2> /dev/null
}

check_c_lib()
{
  if [ -z "$TOOLCHAIN_CC" ]; then
    return 1
  fi
	cat > "$TEMP_DIR/test.c" << __EOF__
/* libtest file */
int main(int argc, char **argv) {
	return 0;
}
__EOF__
  rm -f "$TEMP_DIR/checklib"
	$TOOLCHAIN_CC -o "$TEMP_DIR/checklib" $CFLAGS $LDFLAGS "-l$1" "$TEMP_DIR/test.c" 2> /dev/null
}

check_cpp_lib()
{
  if [ -z "$TOOLCHAIN_CXX" ]; then
    return 1
  fi
	cat > "$TEMP_DIR/test.cpp" << __EOF__
/* libtest file */
int main(int argc, char **argv) {
	return 0;
}
__EOF__
  rm -f "$TEMP_DIR/checklib"
	$TOOLCHAIN_CXX -o "$TEMP_DIR/checklib" $CXXFLAGS $LDFLAGS "-l$1" "$TEMP_DIR/test.cpp" 2> /dev/null
}

check_lib()
{
	if [ -n "$TOOLCHAIN_CC" ]; then
		check_c_lib "$1"
	elif  [ -n "$TOOLCHAIN_CXX" ]; then
	  check_cpp_lib "$1"
	fi
}

detect_c_compiler()
{
  if [ -n "$TOOLCHAIN_CC" ]; then
    return 0
  fi
  printf "detect C compiler... "
  if [ -n "$CC" ]; then
    if check_c_compiler "$CC"; then
      TOOLCHAIN_CC=$CC
      TOOLCHAIN_NAME=`"$TEMP_DIR/checkcc"`
      echo "$CC"
      return 0
    else
      echo "$CC is not a working C compiler"
      return 1
    fi
  else
    for COMP in $C_COMPILERS
    do
      if check_c_compiler "$COMP"; then
        TOOLCHAIN_CC=$COMP
        TOOLCHAIN_NAME=`"$TEMP_DIR/checkcc"`
        echo "$COMP"
        return 0
      fi
    done
    echo "not found"
    return 1
  fi
}

detect_cpp_compiler()
{
  if [ -n "$TOOLCHAIN_CXX" ]; then
    return 0
  fi
  printf "detect C++ compiler... "

  if [ -n "$CXX" ]; then
    if check_cpp_compiler "$CXX"; then
      TOOLCHAIN_CXX=$CXX
      TOOLCHAIN_NAME=`"$TEMP_DIR/checkcc"`
      echo "$CXX"
      return 0
    else
      echo "$CXX is not a working C++ compiler"
      return 1
    fi
  else
    for COMP in $CPP_COMPILERS
    do
      if check_cpp_compiler "$COMP"; then
        TOOLCHAIN_CXX=$COMP
        TOOLCHAIN_NAME=`"$TEMP_DIR/checkcc"`
        echo "$COMP"
        return 0
      fi
    done
    echo "${TOOLCHAIN_CXX:-"not found"}"
    return 1
  fi
}

write_toolchain_defaults()
{
  echo "# toolchain" >> "$1"
  if [ -n "$TOOLCHAIN_CC" ]; then
    echo "CC = ${TOOLCHAIN_CC}" >> "$1"
  fi
  if [ -n "$TOOLCHAIN_CXX" ]; then
    echo "CXX = ${TOOLCHAIN_CXX}" >> "$1"
  fi
  echo >> "$1"
  if [ -f "make/${TOOLCHAIN_NAME}.mk" ]; then
    cat "make/${TOOLCHAIN_NAME}.mk" >> "$1"
  else
    echo "!!! WARNING !!! Default toolchain flags not found. Configuration might be incomplete."
  fi
}
