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="TARGET_CORE_JARS='core-oj core-libart' 39# HOST_CORE_JARS='core-oj-hostdex core-libart-hostdex'" 40# 41# Calling into soong repeatedly is very slow; whenever it needs to be done 42# more than once, the var_cache.py or var_cache.sh script should be used instead. 43# 44 45import os 46import subprocess 47import sys 48 49def get_build_var(name): 50 """ 51 Query soong build for a variable value and return it as a string. 52 53 Var lookup is cached, subsequent var lookups in any child process 54 (that includes a var-cache is free). The var must be in 'var_list' 55 to participate in the cache. 56 57 Example: 58 host_out = var_cache.get_build_var('HOST_OUT') 59 60 Note that build vars can often have spaces in them, 61 so the caller must take care to ensure space-correctness. 62 63 Raises KeyError if the variable name is not in 'var_list'. 64 """ 65 _populate() 66 _build_dict() 67 68 value = _var_cache_dict.get(name) 69 if value is None: 70 _debug(_var_cache_dict) 71 raise KeyError("The variable '%s' is not in 'var_list', can't lookup" %(name)) 72 73 return value 74 75_var_cache_dict = None 76_THIS_DIR = os.path.dirname(os.path.realpath(__file__)) 77_TOP = os.path.join(_THIS_DIR, "../../..") 78_VAR_LIST_PATH = os.path.join(_THIS_DIR, "var_list") 79_SOONG_UI_SCRIPT = os.path.join(_TOP, "build/soong/soong_ui.bash") 80_DEBUG = False 81 82def _populate(): 83 if os.environ.get('ART_TOOLS_BUILD_VAR_CACHE'): 84 return 85 86 _debug("Varcache missing (PY)... repopulate") 87 88 interesting_vars=[] 89 with open(_VAR_LIST_PATH) as f: 90 for line in f.readlines(): 91 line = line.strip() 92 if not line or line.startswith('#'): 93 continue 94 95 _debug(line) 96 97 interesting_vars.append(line) 98 99 _debug("Interesting vars: ", interesting_vars) 100 101 # Invoke soong exactly once for optimal performance. 102 var_values = subprocess.check_output([ 103 _SOONG_UI_SCRIPT, '--dumpvars-mode', '-vars', " ".join(interesting_vars)], 104 cwd=_TOP) 105 106 # Export the ART_TOOLS_BUILD_VAR_CACHE in the same format as soong_ui.bash --dumpvars-mode. 107 os.environb[b'ART_TOOLS_BUILD_VAR_CACHE'] = var_values 108 109 _debug("Soong output: ", var_values) 110 111def _build_dict(): 112 global _var_cache_dict 113 114 if _var_cache_dict: 115 return 116 117 _debug("_var_cache_build_dict()") 118 119 _var_cache_dict = {} 120 121 # Parse $ART_TOOLS_BUILD_VAR_CACHE, e.g. 122 # TARGET_CORE_JARS='core-oj core-libart conscrypt okhttp bouncycastle apache-xml' 123 # HOST_CORE_JARS='core-oj-hostdex core-libart-hostdex ...' 124 125 for line in os.environ['ART_TOOLS_BUILD_VAR_CACHE'].splitlines(): 126 _debug(line) 127 var_name, var_value = line.split("=") 128 var_value = var_value.strip("'") 129 130 _debug("Var name =", var_name) 131 _debug("Var value =", var_value) 132 133 _var_cache_dict[var_name] = var_value 134 135 _debug("Num entries in dict: ", len(_var_cache_dict)) 136 137def _debug(*args): 138 if _DEBUG: 139 print(*args, file=sys.stderr) 140 141# Below definitions are for interactive use only, e.g. 142# python -c 'import var_cache; var_cache._dump_cache()' 143 144def _dump_cache(): 145 _populate() 146 print(os.environ['ART_TOOLS_BUILD_VAR_CACHE']) 147 148#get_build_var("xyz") 149