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 'mac-amd64': 'x86_64-apple-darwin', 14 'mac-arm64': 'arm64-apple-darwin', 15} 16PLATFORMS = PLATFORM_TO_TRIPLE.keys() 17 18 19class Target(object): 20 21 def __init__(self, api, os, arch): 22 self.m = api 23 self._os = os 24 self._arch = arch 25 26 @property 27 def is_win(self): 28 """Returns True iff the target platform is Windows.""" 29 return self.os == 'windows' 30 31 @property 32 def is_mac(self): 33 """Returns True iff the target platform is macOS.""" 34 return self.os == 'mac' 35 36 @property 37 def is_linux(self): 38 """Returns True iff the target platform is Linux.""" 39 return self.os == 'linux' 40 41 @property 42 def is_host(self): 43 """Returns True iff the target platform is host.""" 44 return self == self.m.host 45 46 @property 47 def os(self): 48 """Returns the target os name which will be in: 49 * windows 50 * mac 51 * linux 52 """ 53 return self._os 54 55 @property 56 def arch(self): 57 """Returns the current CPU architecture.""" 58 return self._arch 59 60 @property 61 def platform(self): 62 """Returns the target platform in the <os>-<arch> format.""" 63 return '%s-%s' % (self.os, self.arch) 64 65 @property 66 def triple(self): 67 """Returns the target triple.""" 68 return PLATFORM_TO_TRIPLE[self.platform] 69 70 def __str__(self): 71 return self.platform 72 73 def __eq__(self, other): 74 if isinstance(other, Target): 75 return self._os == other._os and self._arch == other._arch 76 return False 77 78 def __ne__(self, other): 79 return not self.__eq__(other) 80 81 82class TargetApi(recipe_api.RecipeApi): 83 84 def __call__(self, platform): 85 return Target(self, *platform.split('-', 2)) 86 87 @property 88 def host(self): 89 return Target(self, self.m.platform.name.replace('win', 'windows'), { 90 'intel': { 91 32: '386', 92 64: 'amd64', 93 }, 94 'arm': { 95 32: 'armv6', 96 64: 'arm64', 97 }, 98 }[self.m.platform.arch][self.m.platform.bits]) 99