xref: /aosp_15_r20/external/libchrome/build/detect_host_arch.py (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker#!/usr/bin/env python
2*635a8641SAndroid Build Coastguard Worker# Copyright 2014 The Chromium Authors. All rights reserved.
3*635a8641SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*635a8641SAndroid Build Coastguard Worker# found in the LICENSE file.
5*635a8641SAndroid Build Coastguard Worker
6*635a8641SAndroid Build Coastguard Worker"""Outputs host CPU architecture in format recognized by gyp."""
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Workerimport platform
9*635a8641SAndroid Build Coastguard Workerimport re
10*635a8641SAndroid Build Coastguard Workerimport sys
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Worker
13*635a8641SAndroid Build Coastguard Workerdef HostArch():
14*635a8641SAndroid Build Coastguard Worker  """Returns the host architecture with a predictable string."""
15*635a8641SAndroid Build Coastguard Worker  host_arch = platform.machine()
16*635a8641SAndroid Build Coastguard Worker
17*635a8641SAndroid Build Coastguard Worker  # Convert machine type to format recognized by gyp.
18*635a8641SAndroid Build Coastguard Worker  if re.match(r'i.86', host_arch) or host_arch == 'i86pc':
19*635a8641SAndroid Build Coastguard Worker    host_arch = 'ia32'
20*635a8641SAndroid Build Coastguard Worker  elif host_arch in ['x86_64', 'amd64']:
21*635a8641SAndroid Build Coastguard Worker    host_arch = 'x64'
22*635a8641SAndroid Build Coastguard Worker  elif host_arch.startswith('arm'):
23*635a8641SAndroid Build Coastguard Worker    host_arch = 'arm'
24*635a8641SAndroid Build Coastguard Worker  elif host_arch.startswith('aarch64'):
25*635a8641SAndroid Build Coastguard Worker    host_arch = 'arm64'
26*635a8641SAndroid Build Coastguard Worker  elif host_arch.startswith('mips'):
27*635a8641SAndroid Build Coastguard Worker    host_arch = 'mips'
28*635a8641SAndroid Build Coastguard Worker  elif host_arch.startswith('ppc'):
29*635a8641SAndroid Build Coastguard Worker    host_arch = 'ppc'
30*635a8641SAndroid Build Coastguard Worker  elif host_arch.startswith('s390'):
31*635a8641SAndroid Build Coastguard Worker    host_arch = 's390'
32*635a8641SAndroid Build Coastguard Worker
33*635a8641SAndroid Build Coastguard Worker
34*635a8641SAndroid Build Coastguard Worker  # platform.machine is based on running kernel. It's possible to use 64-bit
35*635a8641SAndroid Build Coastguard Worker  # kernel with 32-bit userland, e.g. to give linker slightly more memory.
36*635a8641SAndroid Build Coastguard Worker  # Distinguish between different userland bitness by querying
37*635a8641SAndroid Build Coastguard Worker  # the python binary.
38*635a8641SAndroid Build Coastguard Worker  if host_arch == 'x64' and platform.architecture()[0] == '32bit':
39*635a8641SAndroid Build Coastguard Worker    host_arch = 'ia32'
40*635a8641SAndroid Build Coastguard Worker  if host_arch == 'arm64' and platform.architecture()[0] == '32bit':
41*635a8641SAndroid Build Coastguard Worker    host_arch = 'arm'
42*635a8641SAndroid Build Coastguard Worker
43*635a8641SAndroid Build Coastguard Worker  return host_arch
44*635a8641SAndroid Build Coastguard Worker
45*635a8641SAndroid Build Coastguard Workerdef DoMain(_):
46*635a8641SAndroid Build Coastguard Worker  """Hook to be called from gyp without starting a separate python
47*635a8641SAndroid Build Coastguard Worker  interpreter."""
48*635a8641SAndroid Build Coastguard Worker  return HostArch()
49*635a8641SAndroid Build Coastguard Worker
50*635a8641SAndroid Build Coastguard Workerif __name__ == '__main__':
51*635a8641SAndroid Build Coastguard Worker  print(DoMain([]))
52