blob: b10e6c7fefb9c720a92b90bc93ee685ce1573363 [file] [log] [blame]
Jiyong Park58c518b2018-05-12 22:29:12 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Jiyong Park58c518b2018-05-12 22:29:12 +090018 "sort"
Jiyong Park58c518b2018-05-12 22:29:12 +090019 "strings"
20
21 "github.com/google/blueprint/proptools"
Colin Cross17dec172020-05-14 18:05:32 -070022
23 "android/soong/android"
Jiyong Park58c518b2018-05-12 22:29:12 +090024)
25
Jiyong Park58c518b2018-05-12 22:29:12 +090026func init() {
Paul Duffina48f7582019-12-19 11:25:19 +000027 RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext)
28}
Jiyong Park58c518b2018-05-12 22:29:12 +090029
Paul Duffina48f7582019-12-19 11:25:19 +000030func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) {
31 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Jiyong Park58c518b2018-05-12 22:29:12 +090032}
33
Sundong Ahn27eecb92018-06-21 13:47:17 +090034type prebuiltApisProperties struct {
35 // list of api version directories
36 Api_dirs []string
37}
38
Jiyong Park58c518b2018-05-12 22:29:12 +090039type prebuiltApis struct {
40 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090041 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090042}
43
Jiyong Park58c518b2018-05-12 22:29:12 +090044func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
45 // no need to implement
46}
47
Paul Duffind4c03562020-04-09 17:15:44 +010048func parseJarPath(path string) (module string, apiver string, scope string) {
Sundong Ahna01c2a52018-06-07 21:42:16 +090049 elements := strings.Split(path, "/")
50
51 apiver = elements[0]
52 scope = elements[1]
Sundong Ahna01c2a52018-06-07 21:42:16 +090053
54 module = strings.TrimSuffix(elements[2], ".jar")
55 return
56}
57
Paul Duffind4c03562020-04-09 17:15:44 +010058func parseApiFilePath(ctx android.LoadHookContext, path string) (module string, apiver string, scope string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090059 elements := strings.Split(path, "/")
Sundong Ahn054b19a2018-10-19 13:46:09 +090060 apiver = elements[0]
Jiyong Park58c518b2018-05-12 22:29:12 +090061
62 scope = elements[1]
Anton Hansson8d239692020-05-01 18:37:15 +010063 if scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" {
Jiyong Park58c518b2018-05-12 22:29:12 +090064 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
65 return
66 }
67
68 // elements[2] is string literal "api". skipping.
69 module = strings.TrimSuffix(elements[3], ".txt")
70 return
71}
72
Colin Cross17dec172020-05-14 18:05:32 -070073func prebuiltApiModuleName(mctx android.LoadHookContext, module string, scope string, apiver string) string {
74 return mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module
75}
76
Paul Duffind4c03562020-04-09 17:15:44 +010077func createImport(mctx android.LoadHookContext, module string, scope string, apiver string, path string) {
Sundong Ahna01c2a52018-06-07 21:42:16 +090078 props := struct {
79 Name *string
80 Jars []string
81 Sdk_version *string
82 Installable *bool
83 }{}
Colin Cross17dec172020-05-14 18:05:32 -070084 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, apiver))
Sundong Ahna01c2a52018-06-07 21:42:16 +090085 props.Jars = append(props.Jars, path)
Liz Kammerf971af72020-07-13 15:36:20 -070086 props.Sdk_version = proptools.StringPtr(scope)
Sundong Ahna01c2a52018-06-07 21:42:16 +090087 props.Installable = proptools.BoolPtr(false)
88
Colin Cross84dfc3d2019-09-25 11:33:01 -070089 mctx.CreateModule(ImportFactory, &props)
Sundong Ahna01c2a52018-06-07 21:42:16 +090090}
91
Paul Duffind4c03562020-04-09 17:15:44 +010092func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090093 fgName := module + ".api." + scope + "." + apiver
94 filegroupProps := struct {
95 Name *string
96 Srcs []string
97 }{}
98 filegroupProps.Name = proptools.StringPtr(fgName)
99 filegroupProps.Srcs = []string{path}
Colin Cross84dfc3d2019-09-25 11:33:01 -0700100 mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900101}
102
Paul Duffind4c03562020-04-09 17:15:44 +0100103func getPrebuiltFiles(mctx android.LoadHookContext, name string) []string {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900104 mydir := mctx.ModuleDir() + "/"
Sundong Ahn27eecb92018-06-21 13:47:17 +0900105 var files []string
106 for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs {
Anton Hansson8d239692020-05-01 18:37:15 +0100107 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900108 vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900109 if err != nil {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900110 mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900111 }
112 files = append(files, vfiles...)
113 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900114 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900115 return files
116}
117
Paul Duffind4c03562020-04-09 17:15:44 +0100118func prebuiltSdkStubs(mctx android.LoadHookContext) {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900119 mydir := mctx.ModuleDir() + "/"
120 // <apiver>/<scope>/<module>.jar
121 files := getPrebuiltFiles(mctx, "*.jar")
Sundong Ahna01c2a52018-06-07 21:42:16 +0900122
123 for _, f := range files {
124 // create a Import module for each jar file
125 localPath := strings.TrimPrefix(f, mydir)
Paul Duffind4c03562020-04-09 17:15:44 +0100126 module, apiver, scope := parseJarPath(localPath)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900127 createImport(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900128 }
129}
130
Colin Cross17dec172020-05-14 18:05:32 -0700131func createSystemModules(mctx android.LoadHookContext, apiver string) {
132 props := struct {
133 Name *string
134 Libs []string
135 }{}
136 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", "public", apiver))
137 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", "public", apiver))
138
139 mctx.CreateModule(SystemModulesFactory, &props)
140}
141
142func prebuiltSdkSystemModules(mctx android.LoadHookContext) {
143 for _, apiver := range mctx.Module().(*prebuiltApis).properties.Api_dirs {
144 jar := android.ExistentPathForSource(mctx,
145 mctx.ModuleDir(), apiver, "public", "core-for-system-modules.jar")
146 if jar.Valid() {
147 createSystemModules(mctx, apiver)
148 }
149 }
150}
151
Paul Duffind4c03562020-04-09 17:15:44 +0100152func prebuiltApiFiles(mctx android.LoadHookContext) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900153 mydir := mctx.ModuleDir() + "/"
154 // <apiver>/<scope>/api/<module>.txt
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900155 files := getPrebuiltFiles(mctx, "api/*.txt")
156
Sundong Ahna01c2a52018-06-07 21:42:16 +0900157 if len(files) == 0 {
158 mctx.ModuleErrorf("no api file found under %q", mydir)
159 }
160
161 // construct a map to find out the latest api file path
162 // for each (<module>, <scope>) pair.
163 type latestApiInfo struct {
164 module string
165 scope string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900166 apiver string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900167 path string
168 }
169 m := make(map[string]latestApiInfo)
170
171 for _, f := range files {
172 // create a filegroup for each api txt file
173 localPath := strings.TrimPrefix(f, mydir)
174 module, apiver, scope := parseApiFilePath(mctx, localPath)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900175 createFilegroup(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900176
177 // find the latest apiver
178 key := module + "." + scope
179 info, ok := m[key]
180 if !ok {
181 m[key] = latestApiInfo{module, scope, apiver, localPath}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900182 } else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) &&
183 strings.Compare(apiver, info.apiver) > 0) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900184 info.apiver = apiver
185 info.path = localPath
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900186 m[key] = info
Sundong Ahna01c2a52018-06-07 21:42:16 +0900187 }
188 }
189 // create filegroups for the latest version of (<module>, <scope>) pairs
190 // sort the keys in order to make build.ninja stable
191 keys := make([]string, 0, len(m))
192 for k := range m {
193 keys = append(keys, k)
194 }
195 sort.Strings(keys)
196 for _, k := range keys {
197 info := m[k]
198 createFilegroup(mctx, info.module, info.scope, "latest", info.path)
199 }
200}
201
Paul Duffind4c03562020-04-09 17:15:44 +0100202func createPrebuiltApiModules(mctx android.LoadHookContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +0900203 if _, ok := mctx.Module().(*prebuiltApis); ok {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900204 prebuiltApiFiles(mctx)
205 prebuiltSdkStubs(mctx)
Colin Cross17dec172020-05-14 18:05:32 -0700206 prebuiltSdkSystemModules(mctx)
Jiyong Park58c518b2018-05-12 22:29:12 +0900207 }
208}
209
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700210// prebuilt_apis is a meta-module that generates filegroup modules for all
211// API txt files found under the directory where the Android.bp is located.
212// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
213// generates a filegroup module named <module>-api.<scope>.<ver>.
214//
215// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100216//
217// Similarly, it generates a java_import for all API .jar files found under the
218// directory where the Android.bp is located. Specifically, an API file located
219// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700220// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
221// a java_system_modules module named
222// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900223func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900224 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900225 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900226 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100227 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900228 return module
229}