Merge "Fix prebuilt_apis creating of "latest" incompatibilities" am: b0d0710f67 am: ebf442153d

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1558554

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ib4c4293da87ba27b18d7cd4107f907f44db14ae9
This commit is contained in:
Anton Hansson 2021-01-28 17:00:51 +00:00 committed by Automerger Merge Worker
commit e879d64cf8
1 changed files with 81 additions and 20 deletions

View File

@ -15,12 +15,14 @@
package java package java
import ( import (
"fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/google/blueprint/proptools" "github.com/google/blueprint/proptools"
"android/soong/android" "android/soong/android"
"android/soong/genrule"
) )
func init() { func init() {
@ -35,6 +37,12 @@ type prebuiltApisProperties struct {
// list of api version directories // list of api version directories
Api_dirs []string Api_dirs []string
// The next API directory can optionally point to a directory where
// files incompatibility-tracking files are stored for the current
// "in progress" API. Each module present in one of the api_dirs will have
// a <module>-incompatibilities.api.<scope>.latest module created.
Next_api_dir *string
// The sdk_version of java_import modules generated based on jar files. // The sdk_version of java_import modules generated based on jar files.
// Defaults to "current" // Defaults to "current"
Imports_sdk_version *string Imports_sdk_version *string
@ -98,28 +106,46 @@ func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdk
mctx.CreateModule(ImportFactory, &props) mctx.CreateModule(ImportFactory, &props)
} }
func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) { func createFilegroup(mctx android.LoadHookContext, name string, path string) {
fgName := module + ".api." + scope + "." + apiver
filegroupProps := struct { filegroupProps := struct {
Name *string Name *string
Srcs []string Srcs []string
}{} }{}
filegroupProps.Name = proptools.StringPtr(fgName) filegroupProps.Name = proptools.StringPtr(name)
filegroupProps.Srcs = []string{path} filegroupProps.Srcs = []string{path}
mctx.CreateModule(android.FileGroupFactory, &filegroupProps) mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
} }
func createEmptyFile(mctx android.LoadHookContext, name string) {
props := struct {
Name *string
Cmd *string
Out []string
}{}
props.Name = proptools.StringPtr(name)
props.Out = []string{name}
props.Cmd = proptools.StringPtr("touch $(genDir)/" + name)
mctx.CreateModule(genrule.GenRuleFactory, &props)
}
func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string { func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string {
mydir := mctx.ModuleDir() + "/"
var files []string var files []string
for _, apiver := range p.properties.Api_dirs { for _, apiver := range p.properties.Api_dirs {
for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} { files = append(files, getPrebuiltFilesInSubdir(mctx, apiver, name)...)
vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil) }
if err != nil { return files
mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err) }
}
files = append(files, vfiles...) func getPrebuiltFilesInSubdir(mctx android.LoadHookContext, subdir string, name string) []string {
var files []string
dir := mctx.ModuleDir() + "/" + subdir
for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
glob := fmt.Sprintf("%s/%s/%s", dir, scope, name)
vfiles, err := mctx.GlobWithDeps(glob, nil)
if err != nil {
mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, dir+"/"+scope, err)
} }
files = append(files, vfiles...)
} }
return files return files
} }
@ -181,11 +207,14 @@ func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
// Create filegroups for all (<module>, <scope, <version>) triplets, // Create filegroups for all (<module>, <scope, <version>) triplets,
// and a "latest" filegroup variant for each (<module>, <scope>) pair // and a "latest" filegroup variant for each (<module>, <scope>) pair
moduleName := func(module, scope, version string) string {
return module + ".api." + scope + "." + version
}
m := make(map[string]latestApiInfo) m := make(map[string]latestApiInfo)
for _, f := range files { for _, f := range files {
localPath := strings.TrimPrefix(f, mydir) localPath := strings.TrimPrefix(f, mydir)
module, apiver, scope := parseApiFilePath(mctx, localPath) module, apiver, scope := parseApiFilePath(mctx, localPath)
createFilegroup(mctx, module, scope, apiver, localPath) createFilegroup(mctx, moduleName(module, scope, apiver), localPath)
version, err := strconv.Atoi(apiver) version, err := strconv.Atoi(apiver)
if err != nil { if err != nil {
@ -193,20 +222,52 @@ func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
return return
} }
key := module + "." + scope // Track latest version of each module/scope, except for incompatibilities
info, ok := m[key] if !strings.HasSuffix(module, "incompatibilities") {
if !ok { key := module + "." + scope
m[key] = latestApiInfo{module, scope, version, localPath} info, ok := m[key]
} else if version > info.version { if !ok {
info.version = version m[key] = latestApiInfo{module, scope, version, localPath}
info.path = localPath } else if version > info.version {
m[key] = info info.version = version
info.path = localPath
m[key] = info
}
} }
} }
// Sort the keys in order to make build.ninja stable // Sort the keys in order to make build.ninja stable
for _, k := range android.SortedStringKeys(m) { for _, k := range android.SortedStringKeys(m) {
info := m[k] info := m[k]
createFilegroup(mctx, info.module, info.scope, "latest", info.path) name := moduleName(info.module, info.scope, "latest")
createFilegroup(mctx, name, info.path)
}
// Create incompatibilities tracking files for all modules, if we have a "next" api.
if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" {
files := getPrebuiltFilesInSubdir(mctx, nextApiDir, "api/*incompatibilities.txt")
incompatibilities := make(map[string]bool)
for _, f := range files {
localPath := strings.TrimPrefix(f, mydir)
module, _, scope := parseApiFilePath(mctx, localPath)
// Figure out which module is referenced by this file. Special case for "android".
referencedModule := strings.TrimSuffix(module, "incompatibilities")
referencedModule = strings.TrimSuffix(referencedModule, "-")
if referencedModule == "" {
referencedModule = "android"
}
createFilegroup(mctx, moduleName(referencedModule+"-incompatibilities", scope, "latest"), localPath)
incompatibilities[referencedModule+"."+scope] = true
}
// Create empty incompatibilities files for remaining modules
for _, k := range android.SortedStringKeys(m) {
if _, ok := incompatibilities[k]; !ok {
createEmptyFile(mctx, moduleName(m[k].module+"-incompatibilities", m[k].scope, "latest"))
}
}
} }
} }