Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 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. |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 5 | """Outputs host CPU architecture in format recognized by gyp.""" |
| 6 | |
| 7 | import platform |
| 8 | import re |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 9 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 10 | |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 11 | def HostArch(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 12 | """Returns the host architecture with a predictable string.""" |
| 13 | host_arch = platform.machine().lower() |
| 14 | host_processor = platform.processor().lower() |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 15 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 16 | # Convert machine type to format recognized by gyp. |
| 17 | if re.match(r'i.86', host_arch) or host_arch == 'i86pc': |
| 18 | host_arch = 'x86' |
| 19 | elif host_arch in ['x86_64', 'amd64']: |
| 20 | host_arch = 'x64' |
| 21 | elif host_arch == 'arm64' or host_arch.startswith('aarch64'): |
| 22 | host_arch = 'arm64' |
| 23 | elif host_arch.startswith('arm'): |
| 24 | host_arch = 'arm' |
| 25 | elif host_arch.startswith('mips64'): |
| 26 | host_arch = 'mips64' |
| 27 | elif host_arch.startswith('mips'): |
| 28 | host_arch = 'mips' |
| 29 | elif host_arch.startswith('ppc') or host_processor == 'powerpc': |
| 30 | host_arch = 'ppc' |
| 31 | elif host_arch.startswith('s390'): |
| 32 | host_arch = 's390' |
| 33 | elif host_arch.startswith('riscv'): |
| 34 | host_arch = 'riscv64' |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 35 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 36 | if host_arch == 'arm64': |
| 37 | host_platform = platform.architecture() |
| 38 | if len(host_platform) > 1: |
| 39 | if host_platform[1].lower() == 'windowspe': |
| 40 | # Special case for Windows on Arm: windows-386 packages no |
| 41 | # longer work so use the x64 emulation (this restricts us to |
| 42 | # Windows 11). Python 32-bit returns the host_arch as arm64, |
| 43 | # 64-bit does not. |
| 44 | return 'x64' |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 45 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 46 | # platform.machine is based on running kernel. It's possible to use 64-bit |
| 47 | # kernel with 32-bit userland, e.g. to give linker slightly more memory. |
| 48 | # Distinguish between different userland bitness by querying |
| 49 | # the python binary. |
| 50 | if host_arch == 'x64' and platform.architecture()[0] == '32bit': |
| 51 | host_arch = 'x86' |
| 52 | if host_arch == 'arm64' and platform.architecture()[0] == '32bit': |
| 53 | host_arch = 'arm' |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 54 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 55 | return host_arch |
| 56 | |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 57 | |
| 58 | def DoMain(_): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 59 | """Hook to be called from gyp without starting a separate python |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 60 | interpreter.""" |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 61 | return HostArch() |
| 62 | |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 63 | |
| 64 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 65 | print(DoMain([])) |