Description: Fix CPU type autodetection on little endian MIPS (mipsel)
 Linux Python will return "mips" instead of "mipsel"; "arch" returns
 "mips" as well. So if "mips" is reported, we'll ask the native compiler
 for its machine identifier.

 While we have compiled for mipsel a lot, most of that was cross
 compilation, in which case CPU autodetection is not used. The Debian
 builds however are native, which means that they have been broken for
 the last few years. The most obvious symtom is that all SHA1 sums
 mismatch with those found in hardware configs and software DB.
Origin: upstream <http://sourceforge.net/p/openmsx/openmsx/ci/92fc9edb1c17a3b58f7d26c2f498285abccd0dad/#diff-1>
Bug-Debian: https://bugs.debian.org/758822
Last-Update: 2014-10-05

--- openmsx-0.10.1.orig/build/detectsys.py
+++ openmsx-0.10.1/build/detectsys.py
@@ -2,6 +2,8 @@
 # Actually we rely on the Python "platform" module and map its output to names
 # that the openMSX build understands.
 
+from executils import captureStdout
+
 from platform import architecture, machine, system
 from subprocess import PIPE, STDOUT, Popen
 import sys
@@ -80,6 +82,15 @@ def detectOS():
 	else:
 		raise ValueError('Unsupported or unrecognised OS "%s"' % os)
 
+def getCompilerMachine():
+	# Note: Recent GCC and Clang versions support this option.
+	machine = captureStdout(sys.stderr, 'cc -dumpmachine')
+	if machine is not None:
+		machineParts = machine.split('-')
+		if len(machineParts) >= 3:
+			return machineParts[0], machineParts[2]
+	return None, None
+
 def detectMaemo5():
 	try:
 		proc = Popen(
@@ -96,6 +107,19 @@ def detectMaemo5():
 if __name__ == '__main__':
 	try:
 		hostCPU = detectCPU()
+		if hostCPU == 'mips':
+			# Little endian MIPS is reported as just "mips" by Linux Python.
+			compilerCPU, compilerOS = getCompilerMachine()
+			if compilerCPU == 'mips':
+				pass
+			elif compilerCPU == 'mipsel':
+				hostCPU = compilerCPU
+			else:
+				print >>sys.stderr, (
+						'Warning: Unabling to determine endianess; '
+						'compiling for big endian'
+						)
+
 		hostOS = detectOS()
 		if hostOS == 'mingw32' and hostCPU == 'x86_64':
 			# It is possible to run MinGW on 64-bit Windows, but producing
@@ -111,6 +135,7 @@ if __name__ == '__main__':
 			# Detect maemo5 environment, e.g. Nokia N900
 			if detectMaemo5():
 				hostOS = 'maemo5'
+
 		print '%s-%s' % (hostCPU, hostOS)
 	except ValueError, ex:
 		print >> sys.stderr, ex
