blob: 56e6e6a383f663e56794d997ca0e37cf2841146d [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
13
14def HostArch():
15 """Returns the host architecture with a predictable string."""
Edward Lemur4ac892e2018-10-18 00:41:56 +000016 host_arch = platform.machine().lower()
Milad Farazmand973788f2019-03-11 19:44:11 +000017 host_processor = platform.processor().lower()
Tom Andersonc31ae0b2018-02-06 14:48:56 -080018
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 Weberd4da7ca2021-03-22 18:32:24 +000024 elif host_arch == 'arm64' or host_arch.startswith('aarch64'):
25 host_arch = 'arm64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080026 elif host_arch.startswith('arm'):
27 host_arch = 'arm'
Wang Qing254538b2018-07-26 02:23:53 +000028 elif host_arch.startswith('mips64'):
29 host_arch = 'mips64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080030 elif host_arch.startswith('mips'):
31 host_arch = 'mips'
Milad Farazmand973788f2019-03-11 19:44:11 +000032 elif host_arch.startswith('ppc') or host_processor == 'powerpc':
Tom Andersonc31ae0b2018-02-06 14:48:56 -080033 host_arch = 'ppc'
34 elif host_arch.startswith('s390'):
35 host_arch = 's390'
Rebecca Chang Swee Funeb161622022-06-08 20:05:03 +000036 elif host_arch.startswith('riscv'):
37 host_arch = 'riscv64'
Tom Andersonc31ae0b2018-02-06 14:48:56 -080038
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
51def DoMain(_):
52 """Hook to be called from gyp without starting a separate python
53 interpreter."""
54 return HostArch()
55
56if __name__ == '__main__':
Raul Tambreb946b232019-03-26 14:48:46 +000057 print(DoMain([]))