product variables are consumed via soong injection

Currently the list of product variables and whether they are arch-variant is
hard-coded. Instead, we can generate this data from the Soong Product_variables
struct and use soong_injection to consume it in Bazel.

Bug: 209801976
Test: m bp2build && build/bazel/ci/mixed_libc.sh
Change-Id: I8a91030950407a10832765f15eb010899d73da26
This commit is contained in:
Sam Delmerico
2022-01-20 20:15:02 +00:00
parent 620dc3f4f9
commit 5c32bbf910

View File

@@ -24,6 +24,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
@@ -273,15 +274,45 @@ func saveToBazelConfigFile(config *productVariables, outDir string) error {
return fmt.Errorf("Could not create dir %s: %s", dir, err)
}
data, err := json.MarshalIndent(&config, "", " ")
nonArchVariantProductVariables := []string{}
archVariantProductVariables := []string{}
p := variableProperties{}
t := reflect.TypeOf(p.Product_variables)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
nonArchVariantProductVariables = append(nonArchVariantProductVariables, strings.ToLower(f.Name))
if proptools.HasTag(f, "android", "arch_variant") {
archVariantProductVariables = append(archVariantProductVariables, strings.ToLower(f.Name))
}
}
//TODO(b/216168792) should use common function to print Starlark code
nonArchVariantProductVariablesJson, err := json.MarshalIndent(&nonArchVariantProductVariables, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal product variable data: %s", err.Error())
}
//TODO(b/216168792) should use common function to print Starlark code
archVariantProductVariablesJson, err := json.MarshalIndent(&archVariantProductVariables, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal arch variant product variable data: %s", err.Error())
}
configJson, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal config data: %s", err.Error())
}
bzl := []string{
bazel.GeneratedBazelFileWarning,
fmt.Sprintf(`_product_vars = json.decode("""%s""")`, data),
"product_vars = _product_vars\n",
fmt.Sprintf(`_product_vars = json.decode("""%s""")`, configJson),
fmt.Sprintf(`_product_var_constraints = %s`, nonArchVariantProductVariablesJson),
fmt.Sprintf(`_arch_variant_product_var_constraints = %s`, archVariantProductVariablesJson),
"\n", `
product_vars = _product_vars
product_var_constraints = _product_var_constraints
arch_variant_product_var_constraints = _arch_variant_product_var_constraints
`,
}
err = ioutil.WriteFile(filepath.Join(dir, "product_variables.bzl"), []byte(strings.Join(bzl, "\n")), 0644)
if err != nil {