Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / toolchain.py
1 # Copyright (c) 2012 The Chromium OS 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
5 """Utilities for managing the toolchains in the chroot."""
6
7 import copy
8 import json
9 import os
10
11 from chromite.buildbot import constants
12 from chromite.buildbot import portage_utilities
13 from chromite.lib import cros_build_lib
14 from chromite.lib import gs
15 from chromite.lib import osutils
16
17 if cros_build_lib.IsInsideChroot():
18   # Only import portage after we've checked that we're inside the chroot.
19   # Outside may not have portage, in which case the above may not happen.
20   # We'll check in main() if the operation needs portage.
21
22   # pylint: disable=F0401
23   import portage
24
25
26 def GetHostTuple():
27   """Returns compiler tuple for the host system."""
28   # pylint: disable=E1101
29   return portage.settings['CHOST']
30
31
32 def GetTuplesForOverlays(overlays):
33   """Returns a set of tuples for a given set of overlays."""
34   targets = {}
35   default_settings = {
36       'sdk'      : True,
37       'crossdev' : '',
38       'default'  : False,
39   }
40
41   for overlay in overlays:
42     config = os.path.join(overlay, 'toolchain.conf')
43     if os.path.exists(config):
44       first_target = None
45       seen_default = False
46
47       for line in osutils.ReadFile(config).splitlines():
48         # Split by hash sign so that comments are ignored.
49         # Then split the line to get the tuple and its options.
50         line = line.split('#', 1)[0].split()
51
52         if len(line) > 0:
53           target = line[0]
54           if not first_target:
55             first_target = target
56           if target not in targets:
57             targets[target] = copy.copy(default_settings)
58           if len(line) > 1:
59             targets[target].update(json.loads(' '.join(line[1:])))
60             if targets[target]['default']:
61               seen_default = True
62
63       # If the user has not explicitly marked a toolchain as default,
64       # automatically select the first tuple that we saw in the conf.
65       if not seen_default and first_target:
66         targets[first_target]['default'] = True
67
68   return targets
69
70
71 # Tree interface functions. They help with retrieving data about the current
72 # state of the tree:
73 def GetAllTargets():
74   """Get the complete list of targets.
75
76   Returns:
77     The list of cross targets for the current tree
78   """
79   targets = GetToolchainsForBoard('all')
80
81   # Remove the host target as that is not a cross-target. Replace with 'host'.
82   del targets[GetHostTuple()]
83   return targets
84
85
86 def GetToolchainsForBoard(board, buildroot=constants.SOURCE_ROOT):
87   """Get a list of toolchain tuples for a given board name
88
89   Returns:
90     The list of toolchain tuples for the given board
91   """
92   overlays = portage_utilities.FindOverlays(
93       constants.BOTH_OVERLAYS, None if board in ('all', 'sdk') else board,
94       buildroot=buildroot)
95   toolchains = GetTuplesForOverlays(overlays)
96   if board == 'sdk':
97     toolchains = FilterToolchains(toolchains, 'sdk', True)
98   return toolchains
99
100
101 def FilterToolchains(targets, key, value):
102   """Filter out targets based on their attributes.
103
104   Args:
105     targets: dict of toolchains
106     key: metadata to examine
107     value: expected value for metadata
108
109   Returns:
110     dict where all targets whose metadata |key| does not match |value|
111     have been deleted
112   """
113   return dict((k, v) for k, v in targets.iteritems() if v[key] == value)
114
115
116 def GetSdkURL(for_gsutil=False, suburl=''):
117   """Construct a Google Storage URL for accessing SDK related archives
118
119   Args:
120     for_gsutil: Do you want a URL for passing to `gsutil`?
121     suburl: A url fragment to tack onto the end
122
123   Returns:
124     The fully constructed URL
125   """
126   return gs.GetGsURL(constants.SDK_GS_BUCKET, for_gsutil=for_gsutil,
127                      suburl=suburl)