1#!/usr/bin/env python
2# 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
8from __future__ import print_function
9
10import platform
11import re
12import sys
13
14
15def HostArch():
16  """Returns the host architecture with a predictable string."""
17  host_arch = platform.machine()
18
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 = 'ia32'
22  elif host_arch in ['x86_64', 'amd64']:
23    host_arch = 'x64'
24  elif host_arch.startswith('arm64'):
25    host_arch = 'arm64'
26  elif host_arch.startswith('arm'):
27    host_arch = 'arm'
28  elif host_arch.startswith('aarch64'):
29    host_arch = 'arm64'
30  elif host_arch.startswith('mips64'):
31    host_arch = 'mips64'
32  elif host_arch.startswith('mips'):
33    host_arch = 'mips'
34  elif host_arch.startswith('ppc'):
35    host_arch = 'ppc'
36  elif host_arch.startswith('s390'):
37    host_arch = 's390'
38
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 = 'ia32'
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__':
57  print(DoMain([]))
58