From 5c32bbf91017080958211eee6b55dfc87f4d971a Mon Sep 17 00:00:00 2001 From: Sam Delmerico Date: Thu, 20 Jan 2022 20:15:02 +0000 Subject: [PATCH] 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 --- android/config.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/android/config.go b/android/config.go index 4472036da..10e074cb1 100644 --- a/android/config.go +++ b/android/config.go @@ -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 {