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