blob: ac8337dc57d917c5168914fa7140f5842ceaccd2 [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
Liz Kammer2d2fd852020-08-12 14:42:30 -070037
38 // The sdk_version of java_import modules generated based on jar files.
39 // Defaults to "current"
40 Imports_sdk_version *string
Sundong Ahn27eecb92018-06-21 13:47:17 +090041}
42
Jiyong Park58c518b2018-05-12 22:29:12 +090043type prebuiltApis struct {
44 android.ModuleBase
Sundong Ahn27eecb92018-06-21 13:47:17 +090045 properties prebuiltApisProperties
Jiyong Park58c518b2018-05-12 22:29:12 +090046}
47
Jiyong Park58c518b2018-05-12 22:29:12 +090048func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) {
49 // no need to implement
50}
51
Paul Duffind4c03562020-04-09 17:15:44 +010052func parseJarPath(path string) (module string, apiver string, scope string) {
Sundong Ahna01c2a52018-06-07 21:42:16 +090053 elements := strings.Split(path, "/")
54
55 apiver = elements[0]
56 scope = elements[1]
Sundong Ahna01c2a52018-06-07 21:42:16 +090057
58 module = strings.TrimSuffix(elements[2], ".jar")
59 return
60}
61
Paul Duffind4c03562020-04-09 17:15:44 +010062func parseApiFilePath(ctx android.LoadHookContext, path string) (module string, apiver string, scope string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090063 elements := strings.Split(path, "/")
Sundong Ahn054b19a2018-10-19 13:46:09 +090064 apiver = elements[0]
Jiyong Park58c518b2018-05-12 22:29:12 +090065
66 scope = elements[1]
Anton Hansson8d239692020-05-01 18:37:15 +010067 if scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" {
Jiyong Park58c518b2018-05-12 22:29:12 +090068 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, path)
69 return
70 }
71
72 // elements[2] is string literal "api". skipping.
73 module = strings.TrimSuffix(elements[3], ".txt")
74 return
75}
76
Colin Cross17dec172020-05-14 18:05:32 -070077func prebuiltApiModuleName(mctx android.LoadHookContext, module string, scope string, apiver string) string {
78 return mctx.ModuleName() + "_" + scope + "_" + apiver + "_" + module
79}
80
Liz Kammer2d2fd852020-08-12 14:42:30 -070081func createImport(mctx android.LoadHookContext, module, scope, apiver, path, sdk_version string) {
Sundong Ahna01c2a52018-06-07 21:42:16 +090082 props := struct {
83 Name *string
84 Jars []string
85 Sdk_version *string
86 Installable *bool
87 }{}
Colin Cross17dec172020-05-14 18:05:32 -070088 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, apiver))
Sundong Ahna01c2a52018-06-07 21:42:16 +090089 props.Jars = append(props.Jars, path)
Liz Kammer2d2fd852020-08-12 14:42:30 -070090 props.Sdk_version = proptools.StringPtr(sdk_version)
Sundong Ahna01c2a52018-06-07 21:42:16 +090091 props.Installable = proptools.BoolPtr(false)
92
Colin Cross84dfc3d2019-09-25 11:33:01 -070093 mctx.CreateModule(ImportFactory, &props)
Sundong Ahna01c2a52018-06-07 21:42:16 +090094}
95
Paul Duffind4c03562020-04-09 17:15:44 +010096func createFilegroup(mctx android.LoadHookContext, module string, scope string, apiver string, path string) {
Jiyong Park58c518b2018-05-12 22:29:12 +090097 fgName := module + ".api." + scope + "." + apiver
98 filegroupProps := struct {
99 Name *string
100 Srcs []string
101 }{}
102 filegroupProps.Name = proptools.StringPtr(fgName)
103 filegroupProps.Srcs = []string{path}
Colin Cross84dfc3d2019-09-25 11:33:01 -0700104 mctx.CreateModule(android.FileGroupFactory, &filegroupProps)
Jiyong Park58c518b2018-05-12 22:29:12 +0900105}
106
Liz Kammer2d2fd852020-08-12 14:42:30 -0700107func getPrebuiltFiles(mctx android.LoadHookContext, p *prebuiltApis, name string) []string {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900108 mydir := mctx.ModuleDir() + "/"
Sundong Ahn27eecb92018-06-21 13:47:17 +0900109 var files []string
Liz Kammer2d2fd852020-08-12 14:42:30 -0700110 for _, apiver := range p.properties.Api_dirs {
Anton Hansson8d239692020-05-01 18:37:15 +0100111 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900112 vfiles, err := mctx.GlobWithDeps(mydir+apiver+"/"+scope+"/"+name, nil)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900113 if err != nil {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900114 mctx.ModuleErrorf("failed to glob %s files under %q: %s", name, mydir+apiver+"/"+scope, err)
Sundong Ahn27eecb92018-06-21 13:47:17 +0900115 }
116 files = append(files, vfiles...)
117 }
Sundong Ahna01c2a52018-06-07 21:42:16 +0900118 }
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900119 return files
120}
121
Liz Kammer2d2fd852020-08-12 14:42:30 -0700122func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900123 mydir := mctx.ModuleDir() + "/"
124 // <apiver>/<scope>/<module>.jar
Liz Kammer2d2fd852020-08-12 14:42:30 -0700125 files := getPrebuiltFiles(mctx, p, "*.jar")
126
127 sdk_version := proptools.StringDefault(p.properties.Imports_sdk_version, "current")
Sundong Ahna01c2a52018-06-07 21:42:16 +0900128
129 for _, f := range files {
130 // create a Import module for each jar file
131 localPath := strings.TrimPrefix(f, mydir)
Paul Duffind4c03562020-04-09 17:15:44 +0100132 module, apiver, scope := parseJarPath(localPath)
Liz Kammer2d2fd852020-08-12 14:42:30 -0700133 createImport(mctx, module, scope, apiver, localPath, sdk_version)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900134 }
135}
136
Colin Cross17dec172020-05-14 18:05:32 -0700137func createSystemModules(mctx android.LoadHookContext, apiver string) {
138 props := struct {
139 Name *string
140 Libs []string
141 }{}
142 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", "public", apiver))
143 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", "public", apiver))
144
145 mctx.CreateModule(SystemModulesFactory, &props)
146}
147
Liz Kammer2d2fd852020-08-12 14:42:30 -0700148func prebuiltSdkSystemModules(mctx android.LoadHookContext, p *prebuiltApis) {
149 for _, apiver := range p.properties.Api_dirs {
Colin Cross17dec172020-05-14 18:05:32 -0700150 jar := android.ExistentPathForSource(mctx,
151 mctx.ModuleDir(), apiver, "public", "core-for-system-modules.jar")
152 if jar.Valid() {
153 createSystemModules(mctx, apiver)
154 }
155 }
156}
157
Liz Kammer2d2fd852020-08-12 14:42:30 -0700158func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900159 mydir := mctx.ModuleDir() + "/"
160 // <apiver>/<scope>/api/<module>.txt
Liz Kammer2d2fd852020-08-12 14:42:30 -0700161 files := getPrebuiltFiles(mctx, p, "api/*.txt")
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900162
Sundong Ahna01c2a52018-06-07 21:42:16 +0900163 if len(files) == 0 {
164 mctx.ModuleErrorf("no api file found under %q", mydir)
165 }
166
167 // construct a map to find out the latest api file path
168 // for each (<module>, <scope>) pair.
169 type latestApiInfo struct {
170 module string
171 scope string
Sundong Ahn054b19a2018-10-19 13:46:09 +0900172 apiver string
Sundong Ahna01c2a52018-06-07 21:42:16 +0900173 path string
174 }
175 m := make(map[string]latestApiInfo)
176
177 for _, f := range files {
178 // create a filegroup for each api txt file
179 localPath := strings.TrimPrefix(f, mydir)
180 module, apiver, scope := parseApiFilePath(mctx, localPath)
Sundong Ahn054b19a2018-10-19 13:46:09 +0900181 createFilegroup(mctx, module, scope, apiver, localPath)
Sundong Ahna01c2a52018-06-07 21:42:16 +0900182
183 // find the latest apiver
184 key := module + "." + scope
185 info, ok := m[key]
186 if !ok {
187 m[key] = latestApiInfo{module, scope, apiver, localPath}
Sundong Ahn054b19a2018-10-19 13:46:09 +0900188 } else if len(apiver) > len(info.apiver) || (len(apiver) == len(info.apiver) &&
189 strings.Compare(apiver, info.apiver) > 0) {
Sundong Ahna01c2a52018-06-07 21:42:16 +0900190 info.apiver = apiver
191 info.path = localPath
Sundong Ahn8faab8a2019-02-14 11:49:24 +0900192 m[key] = info
Sundong Ahna01c2a52018-06-07 21:42:16 +0900193 }
194 }
195 // create filegroups for the latest version of (<module>, <scope>) pairs
196 // sort the keys in order to make build.ninja stable
197 keys := make([]string, 0, len(m))
198 for k := range m {
199 keys = append(keys, k)
200 }
201 sort.Strings(keys)
202 for _, k := range keys {
203 info := m[k]
204 createFilegroup(mctx, info.module, info.scope, "latest", info.path)
205 }
206}
207
Paul Duffind4c03562020-04-09 17:15:44 +0100208func createPrebuiltApiModules(mctx android.LoadHookContext) {
Liz Kammer2d2fd852020-08-12 14:42:30 -0700209 if p, ok := mctx.Module().(*prebuiltApis); ok {
210 prebuiltApiFiles(mctx, p)
211 prebuiltSdkStubs(mctx, p)
212 prebuiltSdkSystemModules(mctx, p)
Jiyong Park58c518b2018-05-12 22:29:12 +0900213 }
214}
215
Jaewoong Jung5fb5b2a2019-03-21 10:48:25 -0700216// prebuilt_apis is a meta-module that generates filegroup modules for all
217// API txt files found under the directory where the Android.bp is located.
218// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt
219// generates a filegroup module named <module>-api.<scope>.<ver>.
220//
221// It also creates <module>-api.<scope>.latest for the latest <ver>.
Paul Duffind4c03562020-04-09 17:15:44 +0100222//
223// Similarly, it generates a java_import for all API .jar files found under the
224// directory where the Android.bp is located. Specifically, an API file located
225// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named
Colin Cross17dec172020-05-14 18:05:32 -0700226// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30
227// a java_system_modules module named
228// <prebuilt-api-module>_public_<ver>_system_modules
Inseob Kimc0907f12019-02-08 21:00:45 +0900229func PrebuiltApisFactory() android.Module {
Jiyong Park58c518b2018-05-12 22:29:12 +0900230 module := &prebuiltApis{}
Sundong Ahn27eecb92018-06-21 13:47:17 +0900231 module.AddProperties(&module.properties)
Jiyong Park58c518b2018-05-12 22:29:12 +0900232 android.InitAndroidModule(module)
Paul Duffind4c03562020-04-09 17:15:44 +0100233 android.AddLoadHook(module, createPrebuiltApiModules)
Jiyong Park58c518b2018-05-12 22:29:12 +0900234 return module
235}