detectsys.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Detect the native CPU and OS.
  2. # Actually we rely on the Python "platform" module and map its output to names
  3. # that the openMSX build understands.
  4. from executils import captureStdout
  5. from platform import architecture, machine, system
  6. from subprocess import PIPE, STDOUT, Popen
  7. import sys
  8. def detectCPU():
  9. '''Detects the CPU family (not the CPU model) of the machine were are
  10. running on.
  11. Raises ValueError if no known CPU is detected.
  12. '''
  13. cpu = machine().lower()
  14. dashIndex = cpu.find('-')
  15. if dashIndex != -1:
  16. # Hurd returns "cputype-cpusubtype" instead of just "cputype".
  17. cpu = cpu[ : dashIndex]
  18. if cpu in ('x86_64', 'amd64'):
  19. return 'x86_64'
  20. elif cpu in ('x86', 'i386', 'i486', 'i586', 'i686'):
  21. return 'x86'
  22. elif cpu.startswith('ppc') or cpu.endswith('ppc') or cpu.startswith('power'):
  23. return 'ppc64' if cpu.endswith('64') else 'ppc'
  24. elif cpu.startswith('arm'):
  25. return 'arm'
  26. elif cpu == 'aarch64':
  27. return 'aarch64'
  28. elif cpu == 'aarch64_be':
  29. return 'aarch64_be'
  30. elif cpu.startswith('mips') or cpu == 'sgi':
  31. return 'mipsel' if cpu.endswith('el') else 'mips'
  32. elif cpu == 'm68k':
  33. return 'm68k'
  34. elif cpu == 'ia64':
  35. return 'ia64'
  36. elif cpu.startswith('alpha'):
  37. return 'alpha'
  38. elif cpu.startswith('hppa') or cpu.startswith('parisc'):
  39. return 'hppa'
  40. elif cpu.startswith('s390'):
  41. return 's390'
  42. elif cpu.startswith('sparc') or cpu.startswith('sun4u'):
  43. return 'sparc'
  44. elif cpu.startswith('sh'):
  45. return 'sheb' if cpu.endswith('eb') else 'sh'
  46. elif cpu == 'avr32':
  47. return 'avr32'
  48. elif cpu == '':
  49. # Python couldn't figure it out.
  50. os = system().lower()
  51. if os == 'windows':
  52. # Relatively safe bet.
  53. return 'x86'
  54. raise ValueError('Unable to detect CPU')
  55. else:
  56. raise ValueError('Unsupported or unrecognised CPU "%s"' % cpu)
  57. def detectOS():
  58. '''Detects the operating system of the machine were are running on.
  59. Raises ValueError if no known OS is detected.
  60. '''
  61. os = system().lower()
  62. if os in (
  63. 'linux', 'darwin', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'gnu'
  64. ):
  65. return os
  66. elif os.startswith('gnu/'):
  67. # GNU userland on non-Hurd kernel, for example Debian GNU/kFreeBSD.
  68. # For openMSX the kernel is not really relevant, so treat it like
  69. # a generic GNU system.
  70. return 'gnu'
  71. elif os.startswith('mingw') or os == 'windows':
  72. return 'mingw-w64'
  73. elif os == 'sunos':
  74. return 'solaris'
  75. elif os == '':
  76. # Python couldn't figure it out.
  77. raise ValueError('Unable to detect OS')
  78. else:
  79. raise ValueError('Unsupported or unrecognised OS "%s"' % os)
  80. def getCompilerMachine():
  81. # Note: Recent GCC and Clang versions support this option.
  82. machine = captureStdout(sys.stderr, 'cc -dumpmachine')
  83. if machine is not None:
  84. machineParts = machine.split('-')
  85. if len(machineParts) >= 3:
  86. return machineParts[0], machineParts[2]
  87. return None, None
  88. if __name__ == '__main__':
  89. try:
  90. hostCPU = detectCPU()
  91. if hostCPU == 'mips':
  92. # Little endian MIPS is reported as just "mips" by Linux Python.
  93. compilerCPU, compilerOS = getCompilerMachine()
  94. if compilerCPU == 'mips':
  95. pass
  96. elif compilerCPU == 'mipsel':
  97. hostCPU = compilerCPU
  98. else:
  99. print >>sys.stderr, (
  100. 'Warning: Unabling to determine endianess; '
  101. 'compiling for big endian'
  102. )
  103. hostOS = detectOS()
  104. if hostOS == 'mingw32' and hostCPU == 'x86_64':
  105. # It is possible to run MinGW on 64-bit Windows, but producing
  106. # 64-bit code is not supported yet.
  107. hostCPU = 'x86'
  108. elif hostOS == 'darwin' and hostCPU == 'x86':
  109. # If Python is 64-bit, both the CPU and OS support it, so we can
  110. # compile openMSX for x86-64. Compiling in 32-bit mode might seem
  111. # safer, but will fail if using MacPorts on a 64-bit capable system.
  112. if architecture()[0] == '64bit':
  113. hostCPU = 'x86_64'
  114. print hostCPU, hostOS
  115. except ValueError, ex:
  116. print >> sys.stderr, ex
  117. sys.exit(1)