1load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") 2load("@soong_injection//product_config:product_variable_constants.bzl", "product_var_constant_info") 3load( 4 "@soong_injection//product_config:soong_config_variables.bzl", 5 _soong_config_value_variables = "soong_config_value_variables", 6) 7 8_vars_to_labels = { 9 var.lower(): "//build/bazel/product_config/soong_config_variables:" + var.lower() 10 for var in _soong_config_value_variables 11} | { 12 var: "//build/bazel/product_config:" + var.lower() 13 for var, info in product_var_constant_info.items() 14 if info.selectable and var != "Debuggable" and var != "Eng" 15} 16 17def _product_variables_for_attributes_impl(ctx): 18 result = {} 19 20 def value_to_string(value): 21 typ = type(value) 22 if typ == "bool": 23 return "1" if value else "0" 24 elif typ == "list": 25 return ",".join(value) 26 elif typ == "int": 27 return str(value) 28 elif typ == "string": 29 return value 30 else: 31 fail("Unknown type") 32 33 for var in _vars_to_labels: 34 result[var] = value_to_string(getattr(ctx.attr, "_" + var)[BuildSettingInfo].value) 35 36 result["debuggable"] = value_to_string(ctx.attr._target_build_variant[BuildSettingInfo].value in ["userdebug", "eng"]) 37 result["eng"] = value_to_string(ctx.attr._target_build_variant[BuildSettingInfo].value == "eng") 38 39 return [platform_common.TemplateVariableInfo(result)] 40 41# Provides product variables for templated string replacement. 42product_variables_for_attributes = rule( 43 implementation = _product_variables_for_attributes_impl, 44 attrs = { 45 "_" + var: attr.label(default = label) 46 for var, label in _vars_to_labels.items() 47 } | { 48 "_target_build_variant": attr.label(default = "//build/bazel/product_config:target_build_variant"), 49 }, 50) 51