detect_host_arch.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  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 functools import lru_cache
  7. import platform
  8. import re
  9. @lru_cache(maxsize=None)
  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 == 'arm64' or host_arch.startswith('aarch64'):
  20. host_arch = 'arm64'
  21. elif host_arch.startswith('arm'):
  22. host_arch = 'arm'
  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. elif host_arch.startswith('riscv'):
  32. host_arch = 'riscv64'
  33. elif platform.system() == 'OS/390':
  34. host_arch = 's390x'
  35. if host_arch == 'arm64':
  36. host_platform = platform.architecture()
  37. if len(host_platform) > 1:
  38. if host_platform[1].lower() == 'windowspe':
  39. # Special case for Windows on Arm: windows-386 packages no
  40. # longer work so use the x64 emulation (this restricts us to
  41. # Windows 11). Python 32-bit returns the host_arch as arm64,
  42. # 64-bit does not.
  43. return 'x64'
  44. # platform.machine is based on running kernel. It's possible to use 64-bit
  45. # kernel with 32-bit userland, e.g. to give linker slightly more memory.
  46. # Distinguish between different userland bitness by querying
  47. # the python binary.
  48. if host_arch == 'x64' and platform.architecture()[0] == '32bit':
  49. host_arch = 'x86'
  50. if host_arch == 'arm64' and platform.architecture()[0] == '32bit':
  51. host_arch = 'arm'
  52. return host_arch
  53. def DoMain(_):
  54. """Hook to be called from gyp without starting a separate python
  55. interpreter."""
  56. return HostArch()
  57. if __name__ == '__main__':
  58. print(DoMain([]))