• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from recipe_engine import recipe_api
6
7
8PLATFORM_TO_TRIPLE = {
9  'fuchsia-amd64': 'x86_64-fuchsia',
10  'fuchsia-arm64': 'aarch64-fuchsia',
11  'linux-amd64': 'x86_64-linux-gnu',
12  'linux-arm64': 'aarch64-linux-gnu',
13  'linux-riscv64': 'riscv64-linux-gnu',
14  'mac-amd64': 'x86_64-apple-darwin',
15  'mac-arm64': 'arm64-apple-darwin',
16}
17PLATFORMS = PLATFORM_TO_TRIPLE.keys()
18
19
20class Target(object):
21
22  def __init__(self, api, os, arch):
23    self.m = api
24    self._os = os
25    self._arch = arch
26
27  @property
28  def is_win(self):
29    """Returns True iff the target platform is Windows."""
30    return self.os == 'windows'
31
32  @property
33  def is_mac(self):
34    """Returns True iff the target platform is macOS."""
35    return self.os == 'mac'
36
37  @property
38  def is_linux(self):
39    """Returns True iff the target platform is Linux."""
40    return self.os == 'linux'
41
42  @property
43  def is_host(self):
44    """Returns True iff the target platform is host."""
45    return self == self.m.host
46
47  @property
48  def os(self):
49    """Returns the target os name which will be in:
50      * windows
51      * mac
52      * linux
53    """
54    return self._os
55
56  @property
57  def arch(self):
58    """Returns the current CPU architecture."""
59    return self._arch
60
61  @property
62  def platform(self):
63    """Returns the target platform in the <os>-<arch> format."""
64    return '%s-%s' % (self.os, self.arch)
65
66  @property
67  def triple(self):
68    """Returns the target triple."""
69    return PLATFORM_TO_TRIPLE[self.platform]
70
71  def __str__(self):
72    return self.platform
73
74  def __eq__(self, other):
75    if isinstance(other, Target):
76      return self._os == other._os and self._arch == other._arch
77    return False
78
79  def __ne__(self, other):
80    return not self.__eq__(other)
81
82
83class TargetApi(recipe_api.RecipeApi):
84
85  def __call__(self, platform):
86    return Target(self, *platform.split('-', 2))
87
88  @property
89  def host(self):
90    return Target(self, self.m.platform.name.replace('win', 'windows'), {
91        'intel': {
92            32: '386',
93            64: 'amd64',
94        },
95        'arm': {
96            32: 'armv6',
97            64: 'arm64',
98        },
99    }[self.m.platform.arch][self.m.platform.bits])
100