#!/bin/sh
#
# toolchain detection
#

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

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 << "unknown" << 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_lib()
{
	if [ -z "$TOOLCHAIN_LD" ]; 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_LD -o "$TEMP_DIR/checklib" $CFLAGS $LDFLAGS "-l$1" "$TEMP_DIR/test.c" 2> /dev/null
}

printf "detect C compiler... "

if [ -n "$CC" ]; then
  if check_c_compiler "$CC"; then
    TOOLCHAIN_CC=$CC
    TOOLCHAIN_NAME=$("$TEMP_DIR/checkcc")
    echo $CC
  else
    echo "$CC is not a working C compiler"
    TOOLCHAIN_ERROR="required C compiler not found"
  fi
else
  for COMP in $C_COMPILERS
  do
    if check_c_compiler "$COMP"; then
      TOOLCHAIN_NAME=$("$TEMP_DIR/checkcc")
      TOOLCHAIN_CC=$COMP
      break
    fi
  done
  echo "${TOOLCHAIN_CC-"not found"}"
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
  else
    echo "$CXX is not a working C++ compiler"
    TOOLCHAIN_ERROR="required C++ compiler not found"
  fi
else
  for COMP in $CPP_COMPILERS
  do
    if check_cpp_compiler "$COMP"; then
      TOOLCHAIN_NAME=$("$TEMP_DIR/checkcc")
      TOOLCHAIN_CXX=$COMP
      break
    fi
  done
  echo "${TOOLCHAIN_CXX-"not found"}"
fi

printf "detect linker... "
TOOLCHAIN_LD=$TOOLCHAIN_CC
echo "$TOOLCHAIN_LD"

if [ -z "$TOOLCHAIN_NAME" ]; then
  TOOLCHAIN_ERROR="no build tools detected"
else
	cat >> "$TEMP_DIR/config.mk" << __EOF__

# toolchain
__EOF__
	echo "CC = ${TOOLCHAIN_CC}" >> "$TEMP_DIR/config.mk"
	if [ -n "$TOOLCHAIN_CXX" ]; then
		echo "CXX = ${TOOLCHAIN_CXX}" >> "$TEMP_DIR/config.mk"
	fi
	echo "LD = ${TOOLCHAIN_LD}" >> "$TEMP_DIR/config.mk"
	echo >> "$TEMP_DIR/config.mk"
	
	if [ -f "make/${TOOLCHAIN_NAME}.mk" ]; then
		cat "make/${TOOLCHAIN_NAME}.mk" >> "$TEMP_DIR/config.mk"
	else
		cat >> "$TEMP_DIR/config.mk" << __EOF__
CFLAGS =
DEBUG_FLAGS = -g
RELEASE_FLAGS = -O3 -DNDEBUG
LDFLAGS =

SHLIB_CFLAGS = -fPIC
SHLIB_LDFLAGS = -shared
__EOF__
	fi
fi
