detectsys.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 platform import machine, python_version, system
  5. import sys
  6. def detectCPU():
  7. '''Detects the CPU family (not the CPU model) of the machine were are
  8. running on.
  9. Raises ValueError if no known CPU is detected.
  10. '''
  11. cpu = machine().lower()
  12. dashIndex = cpu.find('-')
  13. if dashIndex != -1:
  14. # Hurd returns "cputype-cpusubtype" instead of just "cputype".
  15. cpu = cpu[ : dashIndex]
  16. if cpu in ('x86_64', 'amd64'):
  17. return 'x86_64'
  18. elif cpu in ('x86', 'i386', 'i486', 'i586', 'i686'):
  19. return 'x86'
  20. elif cpu.startswith('ppc') or cpu.endswith('ppc') or cpu.startswith('power'):
  21. return 'ppc64' if cpu.endswith('64') else 'ppc'
  22. elif cpu.startswith('arm'):
  23. return 'arm'
  24. elif cpu == 'aarch64':
  25. return 'aarch64'
  26. elif cpu == 'aarch64_be':
  27. return 'aarch64_be'
  28. elif cpu.startswith('mips') or cpu == 'sgi':
  29. return 'mipsel' if cpu.endswith('el') else 'mips'
  30. elif cpu == 'm68k':
  31. return 'm68k'
  32. elif cpu == 'ia64':
  33. return 'ia64'
  34. elif cpu.startswith('alpha'):
  35. return 'alpha'
  36. elif cpu.startswith('hppa') or cpu.startswith('parisc'):
  37. return 'hppa'
  38. elif cpu.startswith('s390'):
  39. return 's390'
  40. elif cpu.startswith('sparc') or cpu.startswith('sun4u'):
  41. return 'sparc'
  42. elif cpu.startswith('sh'):
  43. return 'sheb' if cpu.endswith('eb') else 'sh'
  44. elif cpu == 'avr32':
  45. return 'avr32'
  46. elif cpu == '':
  47. # Python couldn't figure it out.
  48. os = system().lower()
  49. if os == 'windows':
  50. # Relatively safe bet.
  51. return 'x86'
  52. raise ValueError('Unable to detect CPU')
  53. else:
  54. raise ValueError('Unsupported or unrecognised CPU "%s"' % cpu)
  55. def detectOS():
  56. '''Detects the operating system of the machine were are running on.
  57. Raises ValueError if no known OS is detected.
  58. '''
  59. os = system().lower()
  60. if os in ('linux', 'darwin', 'freebsd', 'netbsd', 'openbsd', 'gnu'):
  61. return os
  62. elif os.startswith('gnu/'):
  63. # GNU userland on non-Hurd kernel, for example Debian GNU/kFreeBSD.
  64. # For openMSX the kernel is not really relevant, so treat it like
  65. # a generic GNU system.
  66. return 'gnu'
  67. elif os.startswith('mingw') or os == 'windows':
  68. return 'mingw32'
  69. elif os == 'sunos':
  70. return 'solaris'
  71. elif os == '':
  72. # Python couldn't figure it out.
  73. raise ValueError('Unable to detect OS')
  74. else:
  75. raise ValueError('Unsupported or unrecognised OS "%s"' % os)
  76. if __name__ == '__main__':
  77. try:
  78. print >> sys.stderr, ' Using Python %s native system detection...' % (
  79. python_version()
  80. )
  81. hostCPU = detectCPU()
  82. hostOS = detectOS()
  83. if hostOS == 'mingw32' and hostCPU == 'x86_64':
  84. # It is possible to run MinGW on 64-bit Windows, but producing
  85. # 64-bit code is not supported yet.
  86. hostCPU = 'x86'
  87. print >> sys.stderr, ' Detected system: %s-%s' % (hostCPU, hostOS)
  88. print 'OPENMSX_TARGET_CPU=%s' % hostCPU
  89. print 'OPENMSX_TARGET_OS=%s' % hostOS
  90. except ValueError, ex:
  91. print >> sys.stderr, ex
  92. sys.exit(1)