From e136ddac77209c5630d334c6a780847b194a2e24 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Wed, 27 Sep 2023 14:10:33 -0700 Subject: [PATCH] Make platform_mappings file deterministic Fixes: 301473943 Test: presubmits Change-Id: I24a1fef87c1fb59b5aeb8ac9c05306cddee32399 --- bp2build/bp2build_product_config.go | 33 +++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/bp2build/bp2build_product_config.go b/bp2build/bp2build_product_config.go index b724f5783..2f9e9cc56 100644 --- a/bp2build/bp2build_product_config.go +++ b/bp2build/bp2build_product_config.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "reflect" + "sort" "strings" "android/soong/android" @@ -28,6 +29,22 @@ type bazelLabel struct { target string } +func (l *bazelLabel) Less(other *bazelLabel) bool { + if l.repo < other.repo { + return true + } + if l.repo > other.repo { + return false + } + if l.pkg < other.pkg { + return true + } + if l.pkg > other.pkg { + return false + } + return l.target < other.target +} + func (l *bazelLabel) String() string { return fmt.Sprintf("@%s//%s:%s", l.repo, l.pkg, l.target) } @@ -229,9 +246,16 @@ func platformMappingContent( mergedConvertedModulePathMap[k] = v } + productLabels := make([]bazelLabel, 0, len(productLabelToVariables)) + for k := range productLabelToVariables { + productLabels = append(productLabels, k) + } + sort.Slice(productLabels, func(i, j int) bool { + return productLabels[i].Less(&productLabels[j]) + }) result.WriteString("platforms:\n") - for productLabel, productVariables := range productLabelToVariables { - platformMappingSingleProduct(productLabel, productVariables, soongConfigDefinitions, mergedConvertedModulePathMap, &result) + for _, productLabel := range productLabels { + platformMappingSingleProduct(productLabel, productLabelToVariables[productLabel], soongConfigDefinitions, mergedConvertedModulePathMap, &result) } return result.String(), nil } @@ -328,8 +352,9 @@ func platformMappingSingleProduct( } } - for namespace, namespaceContents := range productVariables.VendorVars { - for variable, value := range namespaceContents { + for _, namespace := range android.SortedKeys(productVariables.VendorVars) { + for _, variable := range android.SortedKeys(productVariables.VendorVars[namespace]) { + value := productVariables.VendorVars[namespace][variable] key := namespace + "__" + variable _, hasBool := soongConfigDefinitions.BoolVars[key] _, hasString := soongConfigDefinitions.StringVars[key]