Clean up sortedKeys function

This introduces a generic function SortedStringKeys which can be used to
get a slice of sorted string keys for all kinds of maps having string
keys.

Bug: N/A
Test: m
Change-Id: I542194c68984d909b7ad1dbf060d4d3a98f0ef23
This commit is contained in:
Inseob Kim 2019-06-08 15:47:51 +09:00
parent 4cb61bed13
commit 1a365c6a7f
4 changed files with 15 additions and 29 deletions

View file

@ -18,7 +18,6 @@ import (
"fmt"
"path"
"path/filepath"
"sort"
"strings"
"text/scanner"
@ -1628,17 +1627,8 @@ func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
return
}
sortedKeys := func(m map[string]Paths) []string {
s := make([]string, 0, len(m))
for k := range m {
s = append(s, k)
}
sort.Strings(s)
return s
}
// Ensure ancestor directories are in modulesInDir
dirs := sortedKeys(modulesInDir)
dirs := SortedStringKeys(modulesInDir)
for _, dir := range dirs {
dir := parentDir(dir)
for dir != "." && dir != "/" {
@ -1651,7 +1641,6 @@ func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
}
// Make directories build their direct subdirectories
dirs = sortedKeys(modulesInDir)
for _, dir := range dirs {
p := parentDir(dir)
if p != "." && p != "/" {
@ -1708,8 +1697,7 @@ func (c *buildTargetSingleton) GenerateBuildActions(ctx SingletonContext) {
}
// Wrap those into host|host-cross|target phony rules
osClasses := sortedKeys(osClass)
for _, class := range osClasses {
for _, class := range SortedStringKeys(osClass) {
ctx.Build(pctx, BuildParams{
Rule: blueprint.Phony,
Output: PathForPhony(ctx, class),

View file

@ -16,6 +16,7 @@ package android
import (
"fmt"
"reflect"
"regexp"
"runtime"
"sort"
@ -77,10 +78,15 @@ func JoinWithSuffix(strs []string, suffix string, separator string) string {
return string(ret)
}
func sortedKeys(m map[string][]string) []string {
s := make([]string, 0, len(m))
for k := range m {
s = append(s, k)
func SortedStringKeys(m interface{}) []string {
v := reflect.ValueOf(m)
if v.Kind() != reflect.Map {
panic(fmt.Sprintf("%#v is not a map", m))
}
keys := v.MapKeys()
s := make([]string, 0, len(keys))
for _, key := range keys {
s = append(s, key.String())
}
sort.Strings(s)
return s

View file

@ -30,6 +30,7 @@ blueprint_go_binary {
"androidmk-parser",
"blueprint-parser",
"bpfix-lib",
"soong-android",
],
}

View file

@ -15,9 +15,9 @@
package main
import (
"android/soong/android"
mkparser "android/soong/androidmk/parser"
"fmt"
"sort"
"strings"
bpparser "github.com/google/blueprint/parser"
@ -335,15 +335,6 @@ func classifyLocalOrGlobalPath(value bpparser.Expression) (string, bpparser.Expr
}
}
func sortedMapKeys(inputMap map[string]string) (sortedKeys []string) {
keys := make([]string, 0, len(inputMap))
for key := range inputMap {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
// splitAndAssign splits a Make list into components and then
// creates the corresponding variable assignments.
func splitAndAssign(ctx variableAssignmentContext, splitFunc listSplitFunc, namesByClassification map[string]string) error {
@ -357,7 +348,7 @@ func splitAndAssign(ctx variableAssignmentContext, splitFunc listSplitFunc, name
return err
}
for _, nameClassification := range sortedMapKeys(namesByClassification) {
for _, nameClassification := range android.SortedStringKeys(namesByClassification) {
name := namesByClassification[nameClassification]
if component, ok := lists[nameClassification]; ok && !emptyList(component) {
err = setVariable(ctx.file, ctx.append, ctx.prefix, name, component, true)