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. |
| 5 | |
| 6 | """Outputs host CPU architecture in format recognized by gyp.""" |
| 7 | |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 10 | import platform |
| 11 | import re |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def HostArch(): |
| 15 | """Returns the host architecture with a predictable string.""" |
Edward Lemur | 4ac892e | 2018-10-18 00:41:56 +0000 | [diff] [blame] | 16 | host_arch = platform.machine().lower() |
Milad Farazmand | 973788f | 2019-03-11 19:44:11 +0000 | [diff] [blame] | 17 | host_processor = platform.processor().lower() |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 18 | |
| 19 | # Convert machine type to format recognized by gyp. |
| 20 | if re.match(r'i.86', host_arch) or host_arch == 'i86pc': |
| 21 | host_arch = 'x86' |
| 22 | elif host_arch in ['x86_64', 'amd64']: |
| 23 | host_arch = 'x64' |
Nico Weber | d4da7ca | 2021-03-22 18:32:24 +0000 | [diff] [blame] | 24 | elif host_arch == 'arm64' or host_arch.startswith('aarch64'): |
| 25 | host_arch = 'arm64' |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 26 | elif host_arch.startswith('arm'): |
| 27 | host_arch = 'arm' |
Wang Qing | 254538b | 2018-07-26 02:23:53 +0000 | [diff] [blame] | 28 | elif host_arch.startswith('mips64'): |
| 29 | host_arch = 'mips64' |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 30 | elif host_arch.startswith('mips'): |
| 31 | host_arch = 'mips' |
Milad Farazmand | 973788f | 2019-03-11 19:44:11 +0000 | [diff] [blame] | 32 | elif host_arch.startswith('ppc') or host_processor == 'powerpc': |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 33 | host_arch = 'ppc' |
| 34 | elif host_arch.startswith('s390'): |
| 35 | host_arch = 's390' |
Rebecca Chang Swee Fun | eb16162 | 2022-06-08 20:05:03 +0000 | [diff] [blame] | 36 | elif host_arch.startswith('riscv'): |
| 37 | host_arch = 'riscv64' |
Tom Anderson | c31ae0b | 2018-02-06 14:48:56 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | # platform.machine is based on running kernel. It's possible to use 64-bit |
| 41 | # kernel with 32-bit userland, e.g. to give linker slightly more memory. |
| 42 | # Distinguish between different userland bitness by querying |
| 43 | # the python binary. |
| 44 | if host_arch == 'x64' and platform.architecture()[0] == '32bit': |
| 45 | host_arch = 'x86' |
| 46 | if host_arch == 'arm64' and platform.architecture()[0] == '32bit': |
| 47 | host_arch = 'arm' |
| 48 | |
| 49 | return host_arch |
| 50 | |
| 51 | def DoMain(_): |
| 52 | """Hook to be called from gyp without starting a separate python |
| 53 | interpreter.""" |
| 54 | return HostArch() |
| 55 | |
| 56 | if __name__ == '__main__': |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 57 | print(DoMain([])) |