1// 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 ( 18 "fmt" 19 "path" 20 "strconv" 21 "strings" 22 23 "github.com/google/blueprint/proptools" 24 25 "android/soong/android" 26 "android/soong/genrule" 27) 28 29func init() { 30 RegisterPrebuiltApisBuildComponents(android.InitRegistrationContext) 31} 32 33func RegisterPrebuiltApisBuildComponents(ctx android.RegistrationContext) { 34 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) 35} 36 37type prebuiltApisProperties struct { 38 // list of api version directories 39 Api_dirs []string 40 41 // Directory containing finalized api txt files for extension versions. 42 // Extension versions higher than the base sdk extension version will 43 // be assumed to be finalized later than all Api_dirs. 44 Extensions_dir *string 45 46 // The next API directory can optionally point to a directory where 47 // files incompatibility-tracking files are stored for the current 48 // "in progress" API. Each module present in one of the api_dirs will have 49 // a <module>-incompatibilities.api.<scope>.latest module created. 50 Next_api_dir *string 51 52 // The sdk_version of java_import modules generated based on jar files. 53 // Defaults to "current" 54 Imports_sdk_version *string 55 56 // If set to true, compile dex for java_import modules. Defaults to false. 57 Imports_compile_dex *bool 58} 59 60type prebuiltApis struct { 61 android.ModuleBase 62 properties prebuiltApisProperties 63} 64 65func (module *prebuiltApis) GenerateAndroidBuildActions(ctx android.ModuleContext) { 66 // no need to implement 67} 68 69// parsePrebuiltPath parses the relevant variables out of a variety of paths, e.g. 70// <version>/<scope>/<module>.jar 71// <version>/<scope>/api/<module>.txt 72// extensions/<version>/<scope>/<module>.jar 73// extensions/<version>/<scope>/api/<module>.txt 74func parsePrebuiltPath(ctx android.LoadHookContext, p string) (module string, version string, scope string) { 75 elements := strings.Split(p, "/") 76 77 scopeIdx := len(elements) - 2 78 if elements[scopeIdx] == "api" { 79 scopeIdx-- 80 } 81 scope = elements[scopeIdx] 82 if scope != "core" && scope != "public" && scope != "system" && scope != "test" && scope != "module-lib" && scope != "system-server" { 83 ctx.ModuleErrorf("invalid scope %q found in path: %q", scope, p) 84 return 85 } 86 version = elements[scopeIdx-1] 87 88 module = strings.TrimSuffix(path.Base(p), path.Ext(p)) 89 return 90} 91 92// parseFinalizedPrebuiltPath is like parsePrebuiltPath, but verifies the version is numeric (a finalized version). 93func parseFinalizedPrebuiltPath(ctx android.LoadHookContext, p string) (module string, version int, scope string) { 94 module, v, scope := parsePrebuiltPath(ctx, p) 95 version, err := strconv.Atoi(v) 96 if err != nil { 97 ctx.ModuleErrorf("Found finalized API files in non-numeric dir '%v'", v) 98 return 99 } 100 return 101} 102 103func prebuiltApiModuleName(mctx android.LoadHookContext, module, scope, version string) string { 104 return fmt.Sprintf("%s_%s_%s_%s", mctx.ModuleName(), scope, version, module) 105} 106 107func createImport(mctx android.LoadHookContext, module, scope, version, path, sdkVersion string, compileDex bool) { 108 props := struct { 109 Name *string 110 Jars []string 111 Sdk_version *string 112 Installable *bool 113 Compile_dex *bool 114 }{} 115 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, module, scope, version)) 116 props.Jars = append(props.Jars, path) 117 props.Sdk_version = proptools.StringPtr(sdkVersion) 118 props.Installable = proptools.BoolPtr(false) 119 props.Compile_dex = proptools.BoolPtr(compileDex) 120 121 mctx.CreateModule(ImportFactory, &props) 122} 123 124func createApiModule(mctx android.LoadHookContext, name string, path string) { 125 genruleProps := struct { 126 Name *string 127 Srcs []string 128 Out []string 129 Cmd *string 130 }{} 131 genruleProps.Name = proptools.StringPtr(name) 132 genruleProps.Srcs = []string{path} 133 genruleProps.Out = []string{name} 134 genruleProps.Cmd = proptools.StringPtr("cp $(in) $(out)") 135 mctx.CreateModule(genrule.GenRuleFactory, &genruleProps) 136} 137 138func createEmptyFile(mctx android.LoadHookContext, name string) { 139 props := struct { 140 Name *string 141 Cmd *string 142 Out []string 143 }{} 144 props.Name = proptools.StringPtr(name) 145 props.Out = []string{name} 146 props.Cmd = proptools.StringPtr("touch $(genDir)/" + name) 147 mctx.CreateModule(genrule.GenRuleFactory, &props) 148} 149 150// globApiDirs collects all the files in all api_dirs and all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'. 151// <api-dir>/<scope>/<glob> for all api-dir and scope. 152func globApiDirs(mctx android.LoadHookContext, p *prebuiltApis, api_dir_glob string) []string { 153 var files []string 154 for _, apiver := range p.properties.Api_dirs { 155 files = append(files, globScopeDir(mctx, apiver, api_dir_glob)...) 156 } 157 return files 158} 159 160// globExtensionDirs collects all the files under the extension dir (for all versions and scopes) that match the given glob 161// <extension-dir>/<version>/<scope>/<glob> for all version and scope. 162func globExtensionDirs(mctx android.LoadHookContext, p *prebuiltApis, extension_dir_glob string) []string { 163 // <extensions-dir>/<num>/<extension-dir-glob> 164 return globScopeDir(mctx, *p.properties.Extensions_dir+"/*", extension_dir_glob) 165} 166 167// globScopeDir collects all the files in the given subdir across all scopes that match the given glob, e.g. '*.jar' or 'api/*.txt'. 168// <subdir>/<scope>/<glob> for all scope. 169func globScopeDir(mctx android.LoadHookContext, subdir string, subdir_glob string) []string { 170 var files []string 171 dir := mctx.ModuleDir() + "/" + subdir 172 for _, scope := range []string{"public", "system", "test", "core", "module-lib", "system-server"} { 173 glob := fmt.Sprintf("%s/%s/%s", dir, scope, subdir_glob) 174 vfiles, err := mctx.GlobWithDeps(glob, nil) 175 if err != nil { 176 mctx.ModuleErrorf("failed to glob %s files under %q: %s", subdir_glob, dir+"/"+scope, err) 177 } 178 files = append(files, vfiles...) 179 } 180 for i, f := range files { 181 files[i] = strings.TrimPrefix(f, mctx.ModuleDir()+"/") 182 } 183 return files 184} 185 186func prebuiltSdkStubs(mctx android.LoadHookContext, p *prebuiltApis) { 187 // <apiver>/<scope>/<module>.jar 188 files := globApiDirs(mctx, p, "*.jar") 189 190 sdkVersion := proptools.StringDefault(p.properties.Imports_sdk_version, "current") 191 compileDex := proptools.BoolDefault(p.properties.Imports_compile_dex, false) 192 193 for _, f := range files { 194 // create a Import module for each jar file 195 module, version, scope := parsePrebuiltPath(mctx, f) 196 createImport(mctx, module, scope, version, f, sdkVersion, compileDex) 197 198 if module == "core-for-system-modules" { 199 createSystemModules(mctx, version, scope) 200 } 201 } 202} 203 204func createSystemModules(mctx android.LoadHookContext, version, scope string) { 205 props := struct { 206 Name *string 207 Libs []string 208 }{} 209 props.Name = proptools.StringPtr(prebuiltApiModuleName(mctx, "system_modules", scope, version)) 210 props.Libs = append(props.Libs, prebuiltApiModuleName(mctx, "core-for-system-modules", scope, version)) 211 212 mctx.CreateModule(systemModulesImportFactory, &props) 213} 214 215func prebuiltApiFiles(mctx android.LoadHookContext, p *prebuiltApis) { 216 // <apiver>/<scope>/api/<module>.txt 217 apiLevelFiles := globApiDirs(mctx, p, "api/*.txt") 218 if len(apiLevelFiles) == 0 { 219 mctx.ModuleErrorf("no api file found under %q", mctx.ModuleDir()) 220 } 221 222 // Create modules for all (<module>, <scope, <version>) triplets, 223 apiModuleName := func(module, scope, version string) string { 224 return module + ".api." + scope + "." + version 225 } 226 for _, f := range apiLevelFiles { 227 module, version, scope := parseFinalizedPrebuiltPath(mctx, f) 228 createApiModule(mctx, apiModuleName(module, scope, strconv.Itoa(version)), f) 229 } 230 231 // Figure out the latest version of each module/scope 232 type latestApiInfo struct { 233 module, scope, path string 234 version int 235 } 236 237 getLatest := func(files []string) map[string]latestApiInfo { 238 m := make(map[string]latestApiInfo) 239 for _, f := range files { 240 module, version, scope := parseFinalizedPrebuiltPath(mctx, f) 241 if strings.HasSuffix(module, "incompatibilities") { 242 continue 243 } 244 key := module + "." + scope 245 info, exists := m[key] 246 if !exists || version > info.version { 247 m[key] = latestApiInfo{module, scope, f, version} 248 } 249 } 250 return m 251 } 252 253 latest := getLatest(apiLevelFiles) 254 if p.properties.Extensions_dir != nil { 255 extensionApiFiles := globExtensionDirs(mctx, p, "api/*.txt") 256 for k, v := range getLatest(extensionApiFiles) { 257 if v.version > mctx.Config().PlatformBaseSdkExtensionVersion() { 258 if _, exists := latest[k]; !exists { 259 mctx.ModuleErrorf("Module %v finalized for extension %d but never during an API level; likely error", v.module, v.version) 260 } 261 latest[k] = v 262 } 263 } 264 } 265 266 // Sort the keys in order to make build.ninja stable 267 for _, k := range android.SortedStringKeys(latest) { 268 info := latest[k] 269 name := apiModuleName(info.module, info.scope, "latest") 270 createApiModule(mctx, name, info.path) 271 } 272 273 // Create incompatibilities tracking files for all modules, if we have a "next" api. 274 incompatibilities := make(map[string]bool) 275 if nextApiDir := String(p.properties.Next_api_dir); nextApiDir != "" { 276 files := globScopeDir(mctx, nextApiDir, "api/*incompatibilities.txt") 277 for _, f := range files { 278 filename, _, scope := parsePrebuiltPath(mctx, f) 279 referencedModule := strings.TrimSuffix(filename, "-incompatibilities") 280 281 createApiModule(mctx, apiModuleName(referencedModule+"-incompatibilities", scope, "latest"), f) 282 283 incompatibilities[referencedModule+"."+scope] = true 284 } 285 } 286 // Create empty incompatibilities files for remaining modules 287 for _, k := range android.SortedStringKeys(latest) { 288 if _, ok := incompatibilities[k]; !ok { 289 createEmptyFile(mctx, apiModuleName(latest[k].module+"-incompatibilities", latest[k].scope, "latest")) 290 } 291 } 292} 293 294func createPrebuiltApiModules(mctx android.LoadHookContext) { 295 if p, ok := mctx.Module().(*prebuiltApis); ok { 296 prebuiltApiFiles(mctx, p) 297 prebuiltSdkStubs(mctx, p) 298 } 299} 300 301// prebuilt_apis is a meta-module that generates modules for all API txt files 302// found under the directory where the Android.bp is located. 303// Specifically, an API file located at ./<ver>/<scope>/api/<module>.txt 304// generates a module named <module>-api.<scope>.<ver>. 305// 306// It also creates <module>-api.<scope>.latest for the latest <ver>. 307// 308// Similarly, it generates a java_import for all API .jar files found under the 309// directory where the Android.bp is located. Specifically, an API file located 310// at ./<ver>/<scope>/api/<module>.jar generates a java_import module named 311// <prebuilt-api-module>_<scope>_<ver>_<module>, and for SDK versions >= 30 312// a java_system_modules module named 313// <prebuilt-api-module>_public_<ver>_system_modules 314func PrebuiltApisFactory() android.Module { 315 module := &prebuiltApis{} 316 module.AddProperties(&module.properties) 317 android.InitAndroidModule(module) 318 android.AddLoadHook(module, createPrebuiltApiModules) 319 return module 320} 321