blob: 4294800fe639534847a0c3ca390c4246fc0ffdf5 [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.
5
6"""Outputs host CPU architecture in format recognized by gyp."""
7
Raul Tambreb946b232019-03-26 14:48:46 +00008from __future__ import print_function
9
Tom Andersonc31ae0b2018-02-06 14:48:56 -080010import platform
11import re
Tom Andersonc31ae0b2018-02-06 14:48:56 -080012
Tom Andersonc31ae0b2018-02-06 14:48:56 -080013def HostArch():
14 """Returns the host architecture with a predictable string."""
Edward Lemur4ac892e2018-10-18 00:41:56 +000015 host_arch = platform.machine().lower()
Milad Farazmand973788f2019-03-11 19:44:11 +000016 host_processor = platform.processor().lower()
Tom Andersonc31ae0b2018-02-06 14:48:56 -080017
18 # 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'
Nico Weberd4da7ca2021-03-22 18:32:24 +000023 elif host_arch == 'arm64' or host_arch.startswith('aarch64'):
24 host_arch = 'arm64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080025 elif host_arch.startswith('arm'):
26 host_arch = 'arm'
Wang Qing254538b2018-07-26 02:23:53 +000027 elif host_arch.startswith('mips64'):
28 host_arch = 'mips64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080029 elif host_arch.startswith('mips'):
30 host_arch = 'mips'
Milad Farazmand973788f2019-03-11 19:44:11 +000031 elif host_arch.startswith('ppc') or host_processor == 'powerpc':
Tom Andersonc31ae0b2018-02-06 14:48:56 -080032 host_arch = 'ppc'
33 elif host_arch.startswith('s390'):
34 host_arch = 's390'
Rebecca Chang Swee Funeb161622022-06-08 20:05:03 +000035 elif host_arch.startswith('riscv'):
36 host_arch = 'riscv64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080037
Richard Townsend1b4407e2023-01-20 14:49:42 +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 longer work
43 # so use the x64 emulation (this restricts us to Windows 11). Python
44 # 32-bit returns the host_arch as arm64, 64-bit does not.
45 return 'x64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080046
47 # platform.machine is based on running kernel. It's possible to use 64-bit
48 # kernel with 32-bit userland, e.g. to give linker slightly more memory.
49 # Distinguish between different userland bitness by querying
50 # the python binary.
51 if host_arch == 'x64' and platform.architecture()[0] == '32bit':
52 host_arch = 'x86'
53 if host_arch == 'arm64' and platform.architecture()[0] == '32bit':
54 host_arch = 'arm'
55
56 return host_arch
57
58def DoMain(_):
59 """Hook to be called from gyp without starting a separate python
60 interpreter."""
61 return HostArch()
62
63if __name__ == '__main__':
Raul Tambreb946b232019-03-26 14:48:46 +000064 print(DoMain([]))