2018-05-12 15:29:12 +02: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 (
|
2021-01-22 16:05:04 +01:00
|
|
|
"strconv"
|
2018-05-12 15:29:12 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
2020-05-15 03:05:32 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
2018-05-12 15:29:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-12-19 12:25:19 +01:00
|
|
|
RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext)
|
|
|
|
}
|
2018-05-12 15:29:12 +02:00
|
|
|
|
2019-12-19 12:25:19 +01:00
|
|
|
func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
|
2018-05-12 15:29:12 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 06:47:17 +02:00
|
|
|
type prebuiltApisProperties struct {
|
|
|
|
// list of api version directories
|
|
|
|
Api_dirs []string
|
2020-08-12 23:42:30 +02:00
|
|
|
|
|
|
|
// The sdk_version of java_import modules generated based on jar files.
|
|
|
|
// Defaults to "current"
|
|
|
|
Imports_sdk_version *string
|
2020-09-02 17:37:49 +02:00
|
|
|
|
|
|
|
// If set to true, compile dex for java_import modules. Defaults to false.
|
|
|
|
Imports_compile_dex *bool
|
2018-06-21 06:47:17 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:29:12 +02:00
|
|
|
type prebuiltApis struct {
|
|
|
|
android.ModuleBase
|
2018-06-21 06:47:17 +02:00
|
|
|
properties prebuiltApisProperties
|
2018-05-12 15:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
// no need to implement
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:15:44 +02:00
|
|
|
func parseJarPath(path string) (module string, apiver string, scope string) {
|
2018-06-07 14:42:16 +02:00
|
|
|
elements := strings.Split(path, "/")
|
|
|
|
|
|
|
|
apiver = elements[0]
|
|
|
|
scope = elements[1]
|
|
|
|
|
|
|
|
module = strings.TrimSuffix(elements[2], ".jar")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:15:44 +02:00
|
|
|
func parseApiFilePath(ctx android.LoadHookContext, path string) (module string, apiver string, scope string) {
|
2018-05-12 15:29:12 +02:00
|
|
|
elements := strings.Split(path, "/")
|
2018-10-19 06:46:09 +02:00
|
|
|
apiver = elements[0]
|
2018-05-12 15:29:12 +02:00
|
|
|
|
|
|
|
scope = elements[1]
|
2020-05-01 19:37:15 +02:00
|
|
|
if scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" {
|
2018-05-12 15:29:12 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:05:32 +02:00
|
|
|
func prebuiltApiModuleName(mctx android.LoadHookContext, module string, scope string, apiver string) string {
|
|
|
|
return mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module
|
|
|
|
}
|
|
|
|
|
2020-09-02 17:37:49 +02:00
|
|
|
func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdkVersion string, compileDex bool) {
|
2018-06-07 14:42:16 +02:00
|
|
|
props := struct {
|
|
|
|
Name *string
|
|
|
|
Jars []string
|
|
|
|
Sdk_version *string
|
|
|
|
Installable *bool
|
2020-09-02 17:37:49 +02:00
|
|
|
Compile_dex *bool
|
2018-06-07 14:42:16 +02:00
|
|
|
}{}
|
2020-05-15 03:05:32 +02:00
|
|
|
props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, apiver))
|
2018-06-07 14:42:16 +02:00
|
|
|
props.Jars = append(props.Jars, path)
|
2020-09-02 17:37:49 +02:00
|
|
|
props.Sdk_version = proptools.StringPtr(sdkVersion)
|
2018-06-07 14:42:16 +02:00
|
|
|
props.Installable = proptools.BoolPtr(false)
|
2020-09-02 17:37:49 +02:00
|
|
|
props.Compile_dex = proptools.BoolPtr(compileDex)
|
2018-06-07 14:42:16 +02:00
|
|
|
|
2019-09-25 20:33:01 +02:00
|
|
|
mctx.CreateModule(ImportFactory, &props)
|
2018-06-07 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 18:15:44 +02:00
|
|
|
func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) {
|
2018-05-12 15:29:12 +02:00
|
|
|
fgName := module + ".api." + scope + "." + apiver
|
|
|
|
filegroupProps := struct {
|
|
|
|
Name *string
|
|
|
|
Srcs []string
|
|
|
|
}{}
|
|
|
|
filegroupProps.Name = proptools.StringPtr(fgName)
|
|
|
|
filegroupProps.Srcs = []string{path}
|
2019-09-25 20:33:01 +02:00
|
|
|
mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
|
2018-05-12 15:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 23:42:30 +02:00
|
|
|
func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string {
|
2018-06-07 14:42:16 +02:00
|
|
|
mydir := mctx.ModuleDir() + "/"
|
2018-06-21 06:47:17 +02:00
|
|
|
var files []string
|
2020-08-12 23:42:30 +02:00
|
|
|
for _, apiver := range p.properties.Api_dirs {
|
2020-05-01 19:37:15 +02:00
|
|
|
for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
|
2019-02-14 03:49:24 +01:00
|
|
|
vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
|
2018-06-21 06:47:17 +02:00
|
|
|
if err != nil {
|
2019-02-14 03:49:24 +01:00
|
|
|
mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
|
2018-06-21 06:47:17 +02:00
|
|
|
}
|
|
|
|
files = append(files, vfiles...)
|
|
|
|
}
|
2018-06-07 14:42:16 +02:00
|
|
|
}
|
2019-02-14 03:49:24 +01:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-08-12 23:42:30 +02:00
|
|
|
func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
|
2019-02-14 03:49:24 +01:00
|
|
|
mydir := mctx.ModuleDir() + "/"
|
|
|
|
// <apiver>/<scope>/<module>.jar
|
2020-08-12 23:42:30 +02:00
|
|
|
files := getPrebuiltFiles(mctx, p, "*.jar")
|
|
|
|
|
2020-09-02 17:37:49 +02:00
|
|
|
sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
|
|
|
|
compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false)
|
2018-05-12 15:29:12 +02:00
|
|
|
|
2018-06-07 14:42:16 +02:00
|
|
|
for _, f := range files {
|
|
|
|
// create a Import module for each jar file
|
|
|
|
localPath := strings.TrimPrefix(f, mydir)
|
2020-04-09 18:15:44 +02:00
|
|
|
module, apiver, scope := parseJarPath(localPath)
|
2020-09-02 17:37:49 +02:00
|
|
|
createImport(mctx, module, scope, apiver, localPath, sdkVersion, compileDex)
|
2018-06-07 14:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:05:32 +02:00
|
|
|
func createSystemModules(mctx android.LoadHookContext, apiver string) {
|
|
|
|
props := struct {
|
|
|
|
Name *string
|
|
|
|
Libs []string
|
|
|
|
}{}
|
|
|
|
props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", "public", apiver))
|
|
|
|
props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", "public", apiver))
|
|
|
|
|
|
|
|
mctx.CreateModule(SystemModulesFactory, &props)
|
|
|
|
}
|
|
|
|
|
2020-08-12 23:42:30 +02:00
|
|
|
func prebuiltSdkSystemModules(mctx android.LoadHookContext, p *prebuiltApis) {
|
|
|
|
for _, apiver := range p.properties.Api_dirs {
|
2020-05-15 03:05:32 +02:00
|
|
|
jar := android.ExistentPathForSource(mctx,
|
|
|
|
mctx.ModuleDir(), apiver, "public", "core-for-system-modules.jar")
|
|
|
|
if jar.Valid() {
|
|
|
|
createSystemModules(mctx, apiver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 23:42:30 +02:00
|
|
|
func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
|
2018-06-07 14:42:16 +02:00
|
|
|
mydir := mctx.ModuleDir() + "/"
|
|
|
|
// <apiver>/<scope>/api/<module>.txt
|
2020-08-12 23:42:30 +02:00
|
|
|
files := getPrebuiltFiles(mctx, p, "api/*.txt")
|
2019-02-14 03:49:24 +01:00
|
|
|
|
2018-06-07 14:42:16 +02: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 {
|
2021-01-22 16:05:04 +01:00
|
|
|
module string
|
|
|
|
scope string
|
|
|
|
version int
|
|
|
|
path string
|
2018-06-07 14:42:16 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 16:05:04 +01:00
|
|
|
// Create filegroups for all (<module>, <scope, <version>) triplets,
|
|
|
|
// and a "latest" filegroup variant for each (<module>, <scope>) pair
|
|
|
|
m := make(map[string]latestApiInfo)
|
2018-06-07 14:42:16 +02:00
|
|
|
for _, f := range files {
|
|
|
|
localPath := strings.TrimPrefix(f, mydir)
|
|
|
|
module, apiver, scope := parseApiFilePath(mctx, localPath)
|
2018-10-19 06:46:09 +02:00
|
|
|
createFilegroup(mctx, module, scope, apiver, localPath)
|
2018-06-07 14:42:16 +02:00
|
|
|
|
2021-01-22 16:05:04 +01:00
|
|
|
version, err := strconv.Atoi(apiver)
|
|
|
|
if err != nil {
|
|
|
|
mctx.ModuleErrorf("Found finalized API files in non-numeric dir %v", apiver)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:42:16 +02:00
|
|
|
key := module + "." + scope
|
|
|
|
info, ok := m[key]
|
|
|
|
if !ok {
|
2021-01-22 16:05:04 +01:00
|
|
|
m[key] = latestApiInfo{module, scope, version, localPath}
|
|
|
|
} else if version > info.version {
|
|
|
|
info.version = version
|
2018-06-07 14:42:16 +02:00
|
|
|
info.path = localPath
|
2019-02-14 03:49:24 +01:00
|
|
|
m[key] = info
|
2018-05-12 15:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 16:05:04 +01:00
|
|
|
// Sort the keys in order to make build.ninja stable
|
|
|
|
for _, k := range android.SortedStringKeys(m) {
|
2018-06-07 14:42:16 +02:00
|
|
|
info := m[k]
|
|
|
|
createFilegroup(mctx, info.module, info.scope, "latest", info.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:15:44 +02:00
|
|
|
func createPrebuiltApiModules(mctx android.LoadHookContext) {
|
2020-08-12 23:42:30 +02:00
|
|
|
if p, ok := mctx.Module().(*prebuiltApis); ok {
|
|
|
|
prebuiltApiFiles(mctx, p)
|
|
|
|
prebuiltSdkStubs(mctx, p)
|
|
|
|
prebuiltSdkSystemModules(mctx, p)
|
2018-06-07 14:42:16 +02:00
|
|
|
}
|
2018-05-12 15:29:12 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 18:48:25 +01: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>.
|
2020-04-09 18:15:44 +02:00
|
|
|
//
|
|
|
|
// Similarly, it generates a java_import for all API .jar files found under the
|
|
|
|
// directory where the Android.bp is located. Specifically, an API file located
|
|
|
|
// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
|
2020-05-15 03:05:32 +02:00
|
|
|
// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
|
|
|
|
// a java_system_modules module named
|
|
|
|
// <prebuilt-api-module>_public_<ver>_system_modules
|
2019-02-08 13:00:45 +01:00
|
|
|
func PrebuiltApisFactory() android.Module {
|
2018-05-12 15:29:12 +02:00
|
|
|
module := &prebuiltApis{}
|
2018-06-21 06:47:17 +02:00
|
|
|
module.AddProperties(&module.properties)
|
2018-05-12 15:29:12 +02:00
|
|
|
android.InitAndroidModule(module)
|
2020-04-09 18:15:44 +02:00
|
|
|
android.AddLoadHook(module, createPrebuiltApiModules)
|
2018-05-12 15:29:12 +02:00
|
|
|
return module
|
|
|
|
}
|