blob: 266b457bf71fde7c78e8af6924b27cbd30d9733b [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
Tom Andersonc31ae0b2018-02-06 14:48:56 -08002# 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 Andersonc31ae0b2018-02-06 14:48:56 -08005"""Outputs host CPU architecture in format recognized by gyp."""
6
Aravind Vasudevan8197d722023-11-30 23:29:09 +00007from functools import lru_cache
Tom Andersonc31ae0b2018-02-06 14:48:56 -08008import platform
9import re
Tom Andersonc31ae0b2018-02-06 14:48:56 -080010
Mike Frysinger124bb8e2023-09-06 05:48:55 +000011
Aravind Vasudevan8197d722023-11-30 23:29:09 +000012@lru_cache(maxsize=None)
Tom Andersonc31ae0b2018-02-06 14:48:56 -080013def HostArch():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000014 """Returns the host architecture with a predictable string."""
15 host_arch = platform.machine().lower()
16 host_processor = platform.processor().lower()
Tom Andersonc31ae0b2018-02-06 14:48:56 -080017
Mike Frysinger124bb8e2023-09-06 05:48:55 +000018 # Convert machine type to format recognized by gyp.
19 if re.match(r'i.86', host_arch) or host_arch == 'i86pc':
20 host_arch = 'x86'
21 elif host_arch in ['x86_64', 'amd64']:
22 host_arch = 'x64'
23 elif host_arch == 'arm64' or host_arch.startswith('aarch64'):
24 host_arch = 'arm64'
25 elif host_arch.startswith('arm'):
26 host_arch = 'arm'
27 elif host_arch.startswith('mips64'):
28 host_arch = 'mips64'
29 elif host_arch.startswith('mips'):
30 host_arch = 'mips'
31 elif host_arch.startswith('ppc') or host_processor == 'powerpc':
32 host_arch = 'ppc'
33 elif host_arch.startswith('s390'):
34 host_arch = 's390'
35 elif host_arch.startswith('riscv'):
36 host_arch = 'riscv64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080037
Mike Frysinger124bb8e2023-09-06 05:48:55 +000038 if host_arch == 'arm64':
39 host_platform = platform.architecture()
40 if len(host_platform) > 1:
41 if host_platform[1].lower() == 'windowspe':
42 # Special case for Windows on Arm: windows-386 packages no
43 # longer work so use the x64 emulation (this restricts us to
44 # Windows 11). Python 32-bit returns the host_arch as arm64,
45 # 64-bit does not.
46 return 'x64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080047
Mike Frysinger124bb8e2023-09-06 05:48:55 +000048 # platform.machine is based on running kernel. It's possible to use 64-bit
49 # kernel with 32-bit userland, e.g. to give linker slightly more memory.
50 # Distinguish between different userland bitness by querying
51 # the python binary.
52 if host_arch == 'x64' and platform.architecture()[0] == '32bit':
53 host_arch = 'x86'
54 if host_arch == 'arm64' and platform.architecture()[0] == '32bit':
55 host_arch = 'arm'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080056
Mike Frysinger124bb8e2023-09-06 05:48:55 +000057 return host_arch
58
Tom Andersonc31ae0b2018-02-06 14:48:56 -080059
60def DoMain(_):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000061 """Hook to be called from gyp without starting a separate python
Tom Andersonc31ae0b2018-02-06 14:48:56 -080062 interpreter."""
Mike Frysinger124bb8e2023-09-06 05:48:55 +000063 return HostArch()
64
Tom Andersonc31ae0b2018-02-06 14:48:56 -080065
66if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000067 print(DoMain([]))