1# Copyright 2018, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15#
16# !!! Keep up-to-date with var_cache.sh
17#
18
19#
20# Provide a soong-build variable query mechanism that is cached
21# in the current process and any other subchild process that knows
22# how to parse the exported variable:
23#
24# export ART_TOOLS_BUILD_VAR_CACHE="..."
25#
26# Of the format:
27#
28#   <key1>='<value1>'\n
29#   <key2>='<value2>'\n
30#   ...
31#   <keyN>='<valueN>'
32#
33# Note: This is intentionally the same output format as
34#     build/soong/soong_ui.bash --dumpvars-mode --vars "key1 key2 ... keyN"
35#
36# For example, this would be a valid var-cache:
37#
38# export ART_TOOLS_BUILD_VAR_CACHE="ART_APEX_JARS='core-oj core-libart'"
39#
40# Calling into soong repeatedly is very slow; whenever it needs to be done
41# more than once, the var_cache.py or var_cache.sh script should be used instead.
42#
43
44import os
45import subprocess
46import sys
47
48def get_build_var(name):
49  """
50  Query soong build for a variable value and return it as a string.
51
52  Var lookup is cached, subsequent var lookups in any child process
53  (that includes a var-cache is free). The var must be in 'var_list'
54  to participate in the cache.
55
56  Example:
57     host_out = var_cache.get_build_var('HOST_OUT')
58
59  Note that build vars can often have spaces in them,
60  so the caller must take care to ensure space-correctness.
61
62  Raises KeyError if the variable name is not in 'var_list'.
63  """
64  _populate()
65  _build_dict()
66
67  value = _var_cache_dict.get(name)
68  if value is None:
69    _debug(_var_cache_dict)
70    raise KeyError("The variable '%s' is not in 'var_list', can't lookup" %(name))
71
72  return value
73
74_var_cache_dict = None
75_THIS_DIR = os.path.dirname(os.path.realpath(__file__))
76_TOP = os.path.join(_THIS_DIR, "../../..")
77_VAR_LIST_PATH = os.path.join(_THIS_DIR, "var_list")
78_SOONG_UI_SCRIPT = os.path.join(_TOP, "build/soong/soong_ui.bash")
79_DEBUG = False
80
81def _populate():
82  if os.environ.get('ART_TOOLS_BUILD_VAR_CACHE'):
83    return
84
85  _debug("Varcache missing (PY)... repopulate")
86
87  interesting_vars=[]
88  with open(_VAR_LIST_PATH) as f:
89    for line in f.readlines():
90      line = line.strip()
91      if not line or line.startswith('#'):
92        continue
93
94      _debug(line)
95
96      interesting_vars.append(line)
97
98  _debug("Interesting vars: ", interesting_vars)
99
100  # Invoke soong exactly once for optimal performance.
101  var_values = subprocess.check_output([
102      _SOONG_UI_SCRIPT, '--dumpvars-mode', '-vars', " ".join(interesting_vars)],
103      cwd=_TOP)
104
105  # Export the ART_TOOLS_BUILD_VAR_CACHE in the same format as soong_ui.bash --dumpvars-mode.
106  os.environb[b'ART_TOOLS_BUILD_VAR_CACHE'] = var_values
107
108  _debug("Soong output: ", var_values)
109
110def _build_dict():
111  global _var_cache_dict
112
113  if _var_cache_dict:
114    return
115
116  _debug("_var_cache_build_dict()")
117
118  _var_cache_dict = {}
119
120  # Parse $ART_TOOLS_BUILD_VAR_CACHE, e.g.
121  #   ART_APEX_JARS='core-oj core-libart conscrypt okhttp bouncycastle apache-xml'
122
123  for line in os.environ['ART_TOOLS_BUILD_VAR_CACHE'].splitlines():
124    _debug(line)
125    var_name, var_value = line.split("=")
126    var_value = var_value.strip("'")
127
128    _debug("Var name =", var_name)
129    _debug("Var value =", var_value)
130
131    _var_cache_dict[var_name] = var_value
132
133  _debug("Num entries in dict: ", len(_var_cache_dict))
134
135def _debug(*args):
136  if _DEBUG:
137    print(*args, file=sys.stderr)
138
139# Below definitions are for interactive use only, e.g.
140# python -c 'import var_cache; var_cache._dump_cache()'
141
142def _dump_cache():
143  _populate()
144  print(os.environ['ART_TOOLS_BUILD_VAR_CACHE'])
145
146#get_build_var("xyz")
147