blob: 7669d9ec887fc4a654b990ea3c03f58f36a51a76 [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
7import platform
8import re
Tom Andersonc31ae0b2018-02-06 14:48:56 -08009
Mike Frysinger124bb8e2023-09-06 05:48:55 +000010
Tom Andersonc31ae0b2018-02-06 14:48:56 -080011def HostArch():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000012 """Returns the host architecture with a predictable string."""
13 host_arch = platform.machine().lower()
14 host_processor = platform.processor().lower()
Tom Andersonc31ae0b2018-02-06 14:48:56 -080015
Mike Frysinger124bb8e2023-09-06 05:48:55 +000016 # 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 Andersonc31ae0b2018-02-06 14:48:56 -080035
Mike Frysinger124bb8e2023-09-06 05:48:55 +000036 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 Andersonc31ae0b2018-02-06 14:48:56 -080045
Mike Frysinger124bb8e2023-09-06 05:48:55 +000046 # 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 Andersonc31ae0b2018-02-06 14:48:56 -080054
Mike Frysinger124bb8e2023-09-06 05:48:55 +000055 return host_arch
56
Tom Andersonc31ae0b2018-02-06 14:48:56 -080057
58def DoMain(_):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000059 """Hook to be called from gyp without starting a separate python
Tom Andersonc31ae0b2018-02-06 14:48:56 -080060 interpreter."""
Mike Frysinger124bb8e2023-09-06 05:48:55 +000061 return HostArch()
62
Tom Andersonc31ae0b2018-02-06 14:48:56 -080063
64if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000065 print(DoMain([]))