#!/bin/sh
# Copyright 2010-2012 FARGOS Development, LLC
# $Id$
originDir=`dirname $0`
targetDir=`pwd`
CMake_system=`echo $SB_TARGET_ARCH | awk -F. '{print $1}'`

case X${CXX}Y in
	XY) # nothing set
		CXX_path=`which g++`	# reasonable default
		;;
	X/*) # full path name
		CXX_path=${CXX}
		;;
	*) # anything else
		CXX_path=`which ${CXX}`
		;;
esac

case X${CC}Y in
	XY) # nothing set
		CC_path=`which gcc`	# reasonable default
		;;
	X/*) # full path name
		CC_path=${CC}
		;;
	*) # anything else
		CC_path=`which ${CC}`
		;;
esac

case X${AR_CMD}Y in
	XY) # nothing set
		AR_path=`which ar`	# reasonable default
		;;
	X/*) # full path name
		AR_path=${AR_CMD}
		;;
	*) # anything else
		AR_path=`which ${AR_CMD}`
		;;
esac

case X${RANLIB_CMD}Y in
	XY) # nothing set
		RANLIB_path=`which g++`	# reasonable default
		;;
	X/*) # full path name
		RANLIB_path=${RANLIB_CMD}
		;;
	*) # anything else
		RANLIB_path=`which ${RANLIB_CMD}`
		;;
esac

toolchainFilename="${targetDir}/toolchain.${SB_TARGET_ARCH}.cmake"


cat > $toolchainFilename <<endText
set(CMAKE_SYSTEM_NAME ${CMake_system})
set(CMAKE_SYSTEM_VERSION 1)

set(CMAKE_C_COMPILER ${CC_path})
set(CMAKE_CXX_COMPILER ${CXX_path})
#set(CMAKE_AR ${AR_path})
#set(CMAKE_RANLIB ${AR_path})

# search for programs in build directory NEVER
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# search for libraries and headers in the target directories ONLY
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

endText
if test "$CMake_system" = "Windows"
then
	RC_path=`which i686-pc-mingw32-windres`
	printf "set(CMAKE_RC_COMPILER %s)\n" ${RC_path} >> $toolchainFilename
#	LD_path=`which i686-pc-mingw32-ld`
#	printf "set(CMAKE_LINKER %s)\n" ${LD_path} >> $toolchainFilename
fi

$originDir/cmake.clean

cmake -G "Unix Makefiles" -D CMAKE_TOOLCHAIN_FILE=${toolchainFilename} ${targetDir}

