2018-05-12 21:29:12 +08:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-02-08 20:00:45 +08:00
|
|
|
android.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
|
2018-05-12 21:29:12 +08:00
|
|
|
|
|
|
|
android.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
|
2019-02-08 20:00:45 +08:00
|
|
|
ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
|
2018-05-12 21:29:12 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:47:17 +08:00
|
|
|
type prebuiltApisProperties struct {
|
|
|
|
// list of api version directories
|
|
|
|
Api_dirs []string
|
|
|
|
}
|
|
|
|
|
2018-05-12 21:29:12 +08:00
|
|
|
type prebuiltApis struct {
|
|
|
|
android.ModuleBase
|
2018-06-21 12:47:17 +08:00
|
|
|
properties prebuiltApisProperties
|
2018-05-12 21:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
// no need to implement
|
|
|
|
}
|
|
|
|
|
2018-06-07 20:42:16 +08:00
|
|
|
func parseJarPath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
|
|
|
|
elements := strings.Split(path, "/")
|
|
|
|
|
|
|
|
apiver = elements[0]
|
|
|
|
scope = elements[1]
|
|
|
|
|
|
|
|
module = strings.TrimSuffix(elements[2], ".jar")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-19 12:46:09 +08:00
|
|
|
func parseApiFilePath(ctx android.BaseModuleContext, path string) (module string, apiver string, scope string) {
|
2018-05-12 21:29:12 +08:00
|
|
|
elements := strings.Split(path, "/")
|
2018-10-19 12:46:09 +08:00
|
|
|
apiver = elements[0]
|
2018-05-12 21:29:12 +08:00
|
|
|
|
|
|
|
scope = elements[1]
|
|
|
|
if scope != "public" && scope != "system" && scope != "test" {
|
|
|
|
ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// elements[2] is string literal "api". skipping.
|
|
|
|
module = strings.TrimSuffix(elements[3], ".txt")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-07 20:42:16 +08:00
|
|
|
func createImport(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
|
|
|
|
props := struct {
|
|
|
|
Name *string
|
|
|
|
Jars []string
|
|
|
|
Sdk_version *string
|
|
|
|
Installable *bool
|
|
|
|
}{}
|
2018-06-21 12:47:17 +08:00
|
|
|
props.Name = proptools.StringPtr(mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module)
|
2018-06-07 20:42:16 +08:00
|
|
|
props.Jars = append(props.Jars, path)
|
|
|
|
// TODO(hansson): change to scope after migration is done.
|
|
|
|
props.Sdk_version = proptools.StringPtr("current")
|
|
|
|
props.Installable = proptools.BoolPtr(false)
|
|
|
|
|
2019-09-26 02:33:01 +08:00
|
|
|
mctx.CreateModule(ImportFactory, &props)
|
2018-06-07 20:42:16 +08:00
|
|
|
}
|
|
|
|
|
2018-05-12 21:29:12 +08:00
|
|
|
func createFilegroup(mctx android.TopDownMutatorContext, module string, scope string, apiver string, path string) {
|
|
|
|
fgName := module + ".api." + scope + "." + apiver
|
|
|
|
filegroupProps := struct {
|
|
|
|
Name *string
|
|
|
|
Srcs []string
|
|
|
|
}{}
|
|
|
|
filegroupProps.Name = proptools.StringPtr(fgName)
|
|
|
|
filegroupProps.Srcs = []string{path}
|
2019-09-26 02:33:01 +08:00
|
|
|
mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
|
2018-05-12 21:29:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-14 10:49:24 +08:00
|
|
|
func getPrebuiltFiles(mctx android.TopDownMutatorContext, name string) []string {
|
2018-06-07 20:42:16 +08:00
|
|
|
mydir := mctx.ModuleDir() + "/"
|
2018-06-21 12:47:17 +08:00
|
|
|
var files []string
|
|
|
|
for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs {
|
|
|
|
for _, scope := range []string{"public", "system", "test", "core"} {
|
2019-02-14 10:49:24 +08:00
|
|
|
vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
|
2018-06-21 12:47:17 +08:00
|
|
|
if err != nil {
|
2019-02-14 10:49:24 +08:00
|
|
|
mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
|
2018-06-21 12:47:17 +08:00
|
|
|
}
|
|
|
|
files = append(files, vfiles...)
|
|
|
|
}
|
2018-06-07 20:42:16 +08:00
|
|
|
}
|
2019-02-14 10:49:24 +08:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
func prebuiltSdkStubs(mctx android.TopDownMutatorContext) {
|
|
|
|
mydir := mctx.ModuleDir() + "/"
|
|
|
|
// <apiver>/<scope>/<module>.jar
|
|
|
|
files := getPrebuiltFiles(mctx, "*.jar")
|
2018-05-12 21:29:12 +08:00
|
|
|
|
2018-06-07 20:42:16 +08:00
|
|
|
for _, f := range files {
|
|
|
|
// create a Import module for each jar file
|
|
|
|
localPath := strings.TrimPrefix(f, mydir)
|
|
|
|
module, apiver, scope := parseJarPath(mctx, localPath)
|
2018-06-21 12:47:17 +08:00
|
|
|
createImport(mctx, module, scope, apiver, localPath)
|
2018-06-07 20:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prebuiltApiFiles(mctx android.TopDownMutatorContext) {
|
|
|
|
mydir := mctx.ModuleDir() + "/"
|
|
|
|
// <apiver>/<scope>/api/<module>.txt
|
2019-02-14 10:49:24 +08:00
|
|
|
files := getPrebuiltFiles(mctx, "api/*.txt")
|
|
|
|
|
2018-06-07 20:42:16 +08:00
|
|
|
if len(files) == 0 {
|
|
|
|
mctx.ModuleErrorf("no api file found under %q", mydir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a map to find out the latest api file path
|
|
|
|
// for each (<module>, <scope>) pair.
|
|
|
|
type latestApiInfo struct {
|
|
|
|
module string
|
|
|
|
scope string
|
2018-10-19 12:46:09 +08:00
|
|
|
apiver string
|
2018-06-07 20:42:16 +08:00
|
|
|
path string
|
|
|
|
}
|
|
|
|
m := make(map[string]latestApiInfo)
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
// create a filegroup for each api txt file
|
|
|
|
localPath := strings.TrimPrefix(f, mydir)
|
|
|
|
module, apiver, scope := parseApiFilePath(mctx, localPath)
|
2018-10-19 12:46:09 +08:00
|
|
|
createFilegroup(mctx, module, scope, apiver, localPath)
|
2018-06-07 20:42:16 +08:00
|
|
|
|
|
|
|
// find the latest apiver
|
|
|
|
key := module + "." + scope
|
|
|
|
info, ok := m[key]
|
|
|
|
if !ok {
|
|
|
|
m[key] = latestApiInfo{module, scope, apiver, localPath}
|
2018-10-19 12:46:09 +08:00
|
|
|
} else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) &&
|
|
|
|
strings.Compare(apiver, info.apiver) > 0) {
|
2018-06-07 20:42:16 +08:00
|
|
|
info.apiver = apiver
|
|
|
|
info.path = localPath
|
2019-02-14 10:49:24 +08:00
|
|
|
m[key] = info
|
2018-05-12 21:29:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-06-07 20:42:16 +08:00
|
|
|
// create filegroups for the latest version of (<module>, <scope>) pairs
|
|
|
|
// sort the keys in order to make build.ninja stable
|
|
|
|
keys := make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
info := m[k]
|
|
|
|
createFilegroup(mctx, info.module, info.scope, "latest", info.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 20:00:45 +08:00
|
|
|
func PrebuiltApisMutator(mctx android.TopDownMutatorContext) {
|
2018-06-07 20:42:16 +08:00
|
|
|
if _, ok := mctx.Module().(*prebuiltApis); ok {
|
|
|
|
prebuiltApiFiles(mctx)
|
|
|
|
prebuiltSdkStubs(mctx)
|
|
|
|
}
|
2018-05-12 21:29:12 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 01:48:25 +08:00
|
|
|
// prebuilt_apis is a meta-module that generates filegroup modules for all
|
|
|
|
// API txt files found under the directory where the Android.bp is located.
|
|
|
|
// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
|
|
|
|
// generates a filegroup module named <module>-api.<scope>.<ver>.
|
|
|
|
//
|
|
|
|
// It also creates <module>-api.<scope>.latest for the latest <ver>.
|
2019-02-08 20:00:45 +08:00
|
|
|
func PrebuiltApisFactory() android.Module {
|
2018-05-12 21:29:12 +08:00
|
|
|
module := &prebuiltApis{}
|
2018-06-21 12:47:17 +08:00
|
|
|
module.AddProperties(&module.properties)
|
2018-05-12 21:29:12 +08:00
|
|
|
android.InitAndroidModule(module)
|
|
|
|
return module
|
|
|
|
}
|