detectsys.py 3.8 KB

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