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