File: detectsys.py

package info (click to toggle)
openmsx 0.10.1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,628 kB
  • ctags: 19,723
  • sloc: cpp: 131,938; xml: 25,418; tcl: 15,394; python: 4,012; sh: 365; makefile: 26
file content (142 lines) | stat: -rw-r--r-- 4,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Detect the native CPU and OS.
# 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

def detectCPU():
	'''Detects the CPU family (not the CPU model) of the machine were are
	running on.
	Raises ValueError if no known CPU is detected.
	'''
	cpu = machine().lower()
	dashIndex = cpu.find('-')
	if dashIndex != -1:
		# Hurd returns "cputype-cpusubtype" instead of just "cputype".
		cpu = cpu[ : dashIndex]
	if cpu in ('x86_64', 'amd64'):
		return 'x86_64'
	elif cpu in ('x86', 'i386', 'i486', 'i586', 'i686'):
		return 'x86'
	elif cpu.startswith('ppc') or cpu.endswith('ppc') or cpu.startswith('power'):
		return 'ppc64' if cpu.endswith('64') else 'ppc'
	elif cpu.startswith('arm'):
		return 'arm'
	elif cpu == 'aarch64':
		return 'aarch64'
	elif cpu == 'aarch64_be':
		return 'aarch64_be'
	elif cpu.startswith('mips') or cpu == 'sgi':
		return 'mipsel' if cpu.endswith('el') else 'mips'
	elif cpu == 'm68k':
		return 'm68k'
	elif cpu == 'ia64':
		return 'ia64'
	elif cpu.startswith('alpha'):
		return 'alpha'
	elif cpu.startswith('hppa') or cpu.startswith('parisc'):
		return 'hppa'
	elif cpu.startswith('s390'):
		return 's390'
	elif cpu.startswith('sparc') or cpu.startswith('sun4u'):
		return 'sparc'
	elif cpu.startswith('sh'):
		return 'sheb' if cpu.endswith('eb') else 'sh'
	elif cpu == 'avr32':
		return 'avr32'
	elif cpu == '':
		# Python couldn't figure it out.
		os = system().lower()
		if os == 'windows':
			# Relatively safe bet.
			return 'x86'
		raise ValueError('Unable to detect CPU')
	else:
		raise ValueError('Unsupported or unrecognised CPU "%s"' % cpu)

def detectOS():
	'''Detects the operating system of the machine were are running on.
	Raises ValueError if no known OS is detected.
	'''
	os = system().lower()
	if os in (
		'linux', 'darwin', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'gnu'
		):
		return os
	elif os.startswith('gnu/'):
		# GNU userland on non-Hurd kernel, for example Debian GNU/kFreeBSD.
		# For openMSX the kernel is not really relevant, so treat it like
		# a generic GNU system.
		return 'gnu'
	elif os.startswith('mingw') or os == 'windows':
		return 'mingw32'
	elif os == 'sunos':
		return 'solaris'
	elif os == '':
		# Python couldn't figure it out.
		raise ValueError('Unable to detect OS')
	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(
			["pkg-config", "--silence-errors", "--modversion", "maemo-version"],
			stdin = None,
			stdout = PIPE,
			stderr = None);
	except OSError:
		return False

	stdoutdata, stderrdata = proc.communicate()
	return proc.returncode == 0 and stdoutdata.startswith("5.0")

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
			# 64-bit code is not supported yet.
			hostCPU = 'x86'
		elif hostOS == 'darwin' and hostCPU == 'x86':
			# If Python is 64-bit, both the CPU and OS support it, so we can
			# compile openMSX for x86-64. Compiling in 32-bit mode might seem
			# safer, but will fail if using MacPorts on a 64-bit capable system.
			if architecture()[0] == '64bit':
				hostCPU = 'x86_64'
		elif hostOS == 'linux' and hostCPU == 'arm':
			# Detect maemo5 environment, e.g. Nokia N900
			if detectMaemo5():
				hostOS = 'maemo5'

		print '%s-%s' % (hostCPU, hostOS)
	except ValueError, ex:
		print >> sys.stderr, ex
		sys.exit(1)