detect_host_arch.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. # Copyright 2014 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Outputs host CPU architecture in format recognized by gyp."""
  6. from __future__ import print_function
  7. import platform
  8. import re
  9. import sys
  10. def HostArch():
  11. """Returns the host architecture with a predictable string."""
  12. host_arch = platform.machine().lower()
  13. host_processor = platform.processor().lower()
  14. # Convert machine type to format recognized by gyp.
  15. if re.match(r'i.86', host_arch) or host_arch == 'i86pc':
  16. host_arch = 'x86'
  17. elif host_arch in ['x86_64', 'amd64']:
  18. host_arch = 'x64'
  19. elif host_arch.startswith('arm'):
  20. host_arch = 'arm'
  21. elif host_arch.startswith('aarch64'):
  22. host_arch = 'arm64'
  23. elif host_arch.startswith('mips64'):
  24. host_arch = 'mips64'
  25. elif host_arch.startswith('mips'):
  26. host_arch = 'mips'
  27. elif host_arch.startswith('ppc') or host_processor == 'powerpc':
  28. host_arch = 'ppc'
  29. elif host_arch.startswith('s390'):
  30. host_arch = 's390'
  31. # platform.machine is based on running kernel. It's possible to use 64-bit
  32. # kernel with 32-bit userland, e.g. to give linker slightly more memory.
  33. # Distinguish between different userland bitness by querying
  34. # the python binary.
  35. if host_arch == 'x64' and platform.architecture()[0] == '32bit':
  36. host_arch = 'x86'
  37. if host_arch == 'arm64' and platform.architecture()[0] == '32bit':
  38. host_arch = 'arm'
  39. return host_arch
  40. def DoMain(_):
  41. """Hook to be called from gyp without starting a separate python
  42. interpreter."""
  43. return HostArch()
  44. if __name__ == '__main__':
  45. print(DoMain([]))