| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 1 | // Copyright 2023 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 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | ) | 
|  | 20 |  | 
|  | 21 | type GeneratedJavaLibraryModule struct { | 
|  | 22 | Library | 
|  | 23 | callbacks  GeneratedJavaLibraryCallbacks | 
|  | 24 | moduleName string | 
| Joe Onorato | 8f75585 | 2023-08-25 20:23:32 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | // true if we've already called DepsMutator. Can't call AddLibrary or AddSharedLibrary | 
|  | 27 | // after DepsMutator. | 
|  | 28 | depsMutatorDone bool | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
|  | 31 | type GeneratedJavaLibraryCallbacks interface { | 
|  | 32 | // Called from inside DepsMutator, gives a chance to AddDependencies | 
|  | 33 | DepsMutator(module *GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) | 
|  | 34 |  | 
|  | 35 | // Called from inside GenerateAndroidBuildActions. Add the build rules to | 
|  | 36 | // make the srcjar, and return the path to it. | 
| Jihoon Kang | 3921f0b | 2024-03-12 23:51:37 +0000 | [diff] [blame] | 37 | GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) (android.Path, android.Path) | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
|  | 40 | // GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated | 
|  | 41 | // source code, including ones outside the java package to build jar files | 
|  | 42 | // from that generated source. | 
|  | 43 | // | 
|  | 44 | // To use GeneratedJavaLibraryModule, call GeneratedJavaLibraryModuleFactory with | 
|  | 45 | // a callback interface and a properties object to add to the module. | 
|  | 46 | // | 
|  | 47 | // These modules will have some properties blocked, and it will be an error if | 
|  | 48 | // modules attempt to set them. See the list of property names in GeneratedAndroidBuildActions | 
|  | 49 | // for the list of those properties. | 
|  | 50 | func GeneratedJavaLibraryModuleFactory(moduleName string, callbacks GeneratedJavaLibraryCallbacks, properties interface{}) android.Module { | 
|  | 51 | module := &GeneratedJavaLibraryModule{ | 
|  | 52 | callbacks:  callbacks, | 
|  | 53 | moduleName: moduleName, | 
|  | 54 | } | 
|  | 55 | module.addHostAndDeviceProperties() | 
|  | 56 | module.initModuleAndImport(module) | 
|  | 57 | android.InitApexModule(module) | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 58 | InitJavaModule(module, android.HostAndDeviceSupported) | 
|  | 59 | if properties != nil { | 
|  | 60 | module.AddProperties(properties) | 
|  | 61 | } | 
|  | 62 | return module | 
|  | 63 | } | 
|  | 64 |  | 
| Joe Onorato | 8f75585 | 2023-08-25 20:23:32 -0700 | [diff] [blame] | 65 | // Add a java shared library as a dependency, as if they had said `libs: [ "name" ]` | 
|  | 66 | func (module *GeneratedJavaLibraryModule) AddSharedLibrary(name string) { | 
|  | 67 | if module.depsMutatorDone { | 
|  | 68 | panic("GeneratedJavaLibraryModule.AddLibrary called after DepsMutator") | 
|  | 69 | } | 
|  | 70 | module.Library.properties.Libs = append(module.Library.properties.Libs, name) | 
|  | 71 | } | 
|  | 72 |  | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 73 | func (module *GeneratedJavaLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 74 | module.callbacks.DepsMutator(module, ctx) | 
| Joe Onorato | 8f75585 | 2023-08-25 20:23:32 -0700 | [diff] [blame] | 75 | module.depsMutatorDone = true | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 76 | module.Library.DepsMutator(ctx) | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | func checkPropertyEmpty(ctx android.ModuleContext, module *GeneratedJavaLibraryModule, name string, value []string) { | 
|  | 80 | if len(value) != 0 { | 
|  | 81 | ctx.PropertyErrorf(name, "%s not allowed on %s", name, module.moduleName) | 
|  | 82 | } | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | func (module *GeneratedJavaLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 86 | // These modules are all-generated, so disallow these properties to keep it simple. | 
|  | 87 | // No additional sources | 
|  | 88 | checkPropertyEmpty(ctx, module, "srcs", module.Library.properties.Srcs) | 
|  | 89 | checkPropertyEmpty(ctx, module, "common_srcs", module.Library.properties.Common_srcs) | 
|  | 90 | checkPropertyEmpty(ctx, module, "exclude_srcs", module.Library.properties.Exclude_srcs) | 
|  | 91 | checkPropertyEmpty(ctx, module, "java_resource_dirs", module.Library.properties.Java_resource_dirs) | 
|  | 92 | checkPropertyEmpty(ctx, module, "exclude_java_resource_dirs", module.Library.properties.Exclude_java_resource_dirs) | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 93 | // Restrict these for no good reason other than to limit the surface area. If there's a | 
|  | 94 | // good use case put them back. | 
|  | 95 | checkPropertyEmpty(ctx, module, "plugins", module.Library.properties.Plugins) | 
|  | 96 | checkPropertyEmpty(ctx, module, "exported_plugins", module.Library.properties.Exported_plugins) | 
|  | 97 |  | 
| Jihoon Kang | 3921f0b | 2024-03-12 23:51:37 +0000 | [diff] [blame] | 98 | srcJarPath, cacheOutputPath := module.callbacks.GenerateSourceJarBuildActions(module, ctx) | 
|  | 99 |  | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 100 | module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath) | 
| Jihoon Kang | 3921f0b | 2024-03-12 23:51:37 +0000 | [diff] [blame] | 101 | module.Library.properties.Aconfig_Cache_files = append(module.Library.properties.Aconfig_Cache_files, cacheOutputPath) | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 102 | module.Library.GenerateAndroidBuildActions(ctx) | 
|  | 103 | } | 
| Joe Onorato | 349ae8d | 2024-02-05 22:46:00 +0000 | [diff] [blame] | 104 |  | 
|  | 105 | // Add a rule to the jarjar renaming rules.  See RepackageProviderData. | 
|  | 106 | func (module *GeneratedJavaLibraryModule) AddJarJarRenameRule(original string, renamed string) { | 
|  | 107 | module.addJarJarRenameRule(original, renamed) | 
|  | 108 | } |