-echo -n "Checking for -lexpat... "
-echo "int main() { return 0; }" > build_check.c
-gcc build_check.c -lexpat -o fake > /dev/null 2> /dev/null
+if [ -z "$CC" ]
+then
+ CC=gcc
+fi
+
+if [ -z "$CXX" ]
+then
+ CXX=g++
+fi
+
+printf "Checking CC... "
+$CC --version > /dev/null 2> /dev/null
+if [ $? != 0 ]
+then
+ printf "FAIL!\n"
+ printf "$CC not found\n"
+ exit;
+fi
+printf "found\n"
+printf "Checking CXX... "
+$CXX --version > /dev/null 2> /dev/null
+if [ $? != 0 ]
+then
+ printf "FAIL!\n"
+ printf "$CXX not found\n"
+ exit;
+fi
+printf "found\n"
+
+printf "Checking endianess... "
+printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c
+$CC $CFLAGS $LDFLAGS build_check.c -o fake > /dev/null 2> /dev/null
+if [ $? != 0 ]
+then
+ printf "FAIL!\n"
+ printf "Endianess checking failed\n"
+ exit;
+else
+ ./fake
+ if [ $? = 1 ]
+ then
+ ARCH=le
+ CXXFLAGS="$CXXFLAGS -DARCH_LE"
+ CFLAGS="$CFLAGS -DARCH_LE"
+ printf "Little Endian\n"
+ else
+ ARCH=be
+ CXXFLAGS="$CXXFLAGS -DARCH_BE"
+ CFLAGS="$CFLAGS -DARCH_BE"
+ printf "Big Endian\n"
+ fi
+fi
+rm -f fake
+
+printf "Checking for -lexpat... "
+printf "int main() { return 0; }\n" > build_check.c
+$CC $CXXFLAGS $LDFLAGS build_check.c -lexpat -o fake > /dev/null 2> /dev/null