1*333d2b36SAndroid Build Coastguard Worker// Copyright 2023 Google Inc. All rights reserved. 2*333d2b36SAndroid Build Coastguard Worker// 3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*333d2b36SAndroid Build Coastguard Worker// 7*333d2b36SAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*333d2b36SAndroid Build Coastguard Worker// 9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*333d2b36SAndroid Build Coastguard Worker// limitations under the License. 14*333d2b36SAndroid Build Coastguard Worker 15*333d2b36SAndroid Build Coastguard Workerpackage java 16*333d2b36SAndroid Build Coastguard Worker 17*333d2b36SAndroid Build Coastguard Workerimport ( 18*333d2b36SAndroid Build Coastguard Worker "android/soong/android" 19*333d2b36SAndroid Build Coastguard Worker) 20*333d2b36SAndroid Build Coastguard Worker 21*333d2b36SAndroid Build Coastguard Workertype GeneratedJavaLibraryModule struct { 22*333d2b36SAndroid Build Coastguard Worker Library 23*333d2b36SAndroid Build Coastguard Worker callbacks GeneratedJavaLibraryCallbacks 24*333d2b36SAndroid Build Coastguard Worker moduleName string 25*333d2b36SAndroid Build Coastguard Worker 26*333d2b36SAndroid Build Coastguard Worker // true if we've already called DepsMutator. Can't call AddLibrary or AddSharedLibrary 27*333d2b36SAndroid Build Coastguard Worker // after DepsMutator. 28*333d2b36SAndroid Build Coastguard Worker depsMutatorDone bool 29*333d2b36SAndroid Build Coastguard Worker} 30*333d2b36SAndroid Build Coastguard Worker 31*333d2b36SAndroid Build Coastguard Workertype GeneratedJavaLibraryCallbacks interface { 32*333d2b36SAndroid Build Coastguard Worker // Called from inside DepsMutator, gives a chance to AddDependencies 33*333d2b36SAndroid Build Coastguard Worker DepsMutator(module *GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext) 34*333d2b36SAndroid Build Coastguard Worker 35*333d2b36SAndroid Build Coastguard Worker // Called from inside GenerateAndroidBuildActions. Add the build rules to 36*333d2b36SAndroid Build Coastguard Worker // make the srcjar, and return the path to it. 37*333d2b36SAndroid Build Coastguard Worker GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) (android.Path, android.Path) 38*333d2b36SAndroid Build Coastguard Worker} 39*333d2b36SAndroid Build Coastguard Worker 40*333d2b36SAndroid Build Coastguard Worker// GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated 41*333d2b36SAndroid Build Coastguard Worker// source code, including ones outside the java package to build jar files 42*333d2b36SAndroid Build Coastguard Worker// from that generated source. 43*333d2b36SAndroid Build Coastguard Worker// 44*333d2b36SAndroid Build Coastguard Worker// To use GeneratedJavaLibraryModule, call GeneratedJavaLibraryModuleFactory with 45*333d2b36SAndroid Build Coastguard Worker// a callback interface and a properties object to add to the module. 46*333d2b36SAndroid Build Coastguard Worker// 47*333d2b36SAndroid Build Coastguard Worker// These modules will have some properties blocked, and it will be an error if 48*333d2b36SAndroid Build Coastguard Worker// modules attempt to set them. See the list of property names in GeneratedAndroidBuildActions 49*333d2b36SAndroid Build Coastguard Worker// for the list of those properties. 50*333d2b36SAndroid Build Coastguard Workerfunc GeneratedJavaLibraryModuleFactory(moduleName string, callbacks GeneratedJavaLibraryCallbacks, properties interface{}) android.Module { 51*333d2b36SAndroid Build Coastguard Worker module := &GeneratedJavaLibraryModule{ 52*333d2b36SAndroid Build Coastguard Worker callbacks: callbacks, 53*333d2b36SAndroid Build Coastguard Worker moduleName: moduleName, 54*333d2b36SAndroid Build Coastguard Worker } 55*333d2b36SAndroid Build Coastguard Worker module.addHostAndDeviceProperties() 56*333d2b36SAndroid Build Coastguard Worker module.initModuleAndImport(module) 57*333d2b36SAndroid Build Coastguard Worker android.InitApexModule(module) 58*333d2b36SAndroid Build Coastguard Worker InitJavaModule(module, android.HostAndDeviceSupported) 59*333d2b36SAndroid Build Coastguard Worker if properties != nil { 60*333d2b36SAndroid Build Coastguard Worker module.AddProperties(properties) 61*333d2b36SAndroid Build Coastguard Worker } 62*333d2b36SAndroid Build Coastguard Worker return module 63*333d2b36SAndroid Build Coastguard Worker} 64*333d2b36SAndroid Build Coastguard Worker 65*333d2b36SAndroid Build Coastguard Worker// Add a java shared library as a dependency, as if they had said `libs: [ "name" ]` 66*333d2b36SAndroid Build Coastguard Workerfunc (module *GeneratedJavaLibraryModule) AddSharedLibrary(name string) { 67*333d2b36SAndroid Build Coastguard Worker if module.depsMutatorDone { 68*333d2b36SAndroid Build Coastguard Worker panic("GeneratedJavaLibraryModule.AddLibrary called after DepsMutator") 69*333d2b36SAndroid Build Coastguard Worker } 70*333d2b36SAndroid Build Coastguard Worker module.Library.properties.Libs = append(module.Library.properties.Libs, name) 71*333d2b36SAndroid Build Coastguard Worker} 72*333d2b36SAndroid Build Coastguard Worker 73*333d2b36SAndroid Build Coastguard Workerfunc (module *GeneratedJavaLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) { 74*333d2b36SAndroid Build Coastguard Worker module.callbacks.DepsMutator(module, ctx) 75*333d2b36SAndroid Build Coastguard Worker module.depsMutatorDone = true 76*333d2b36SAndroid Build Coastguard Worker module.Library.DepsMutator(ctx) 77*333d2b36SAndroid Build Coastguard Worker} 78*333d2b36SAndroid Build Coastguard Worker 79*333d2b36SAndroid Build Coastguard Workerfunc checkPropertyEmpty(ctx android.ModuleContext, module *GeneratedJavaLibraryModule, name string, value []string) { 80*333d2b36SAndroid Build Coastguard Worker if len(value) != 0 { 81*333d2b36SAndroid Build Coastguard Worker ctx.PropertyErrorf(name, "%s not allowed on %s", name, module.moduleName) 82*333d2b36SAndroid Build Coastguard Worker } 83*333d2b36SAndroid Build Coastguard Worker} 84*333d2b36SAndroid Build Coastguard Worker 85*333d2b36SAndroid Build Coastguard Workerfunc (module *GeneratedJavaLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { 86*333d2b36SAndroid Build Coastguard Worker // These modules are all-generated, so disallow these properties to keep it simple. 87*333d2b36SAndroid Build Coastguard Worker // No additional sources 88*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "srcs", module.Library.properties.Srcs) 89*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "common_srcs", module.Library.properties.Common_srcs) 90*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "exclude_srcs", module.Library.properties.Exclude_srcs) 91*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "java_resource_dirs", module.Library.properties.Java_resource_dirs) 92*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "exclude_java_resource_dirs", module.Library.properties.Exclude_java_resource_dirs) 93*333d2b36SAndroid Build Coastguard Worker // Restrict these for no good reason other than to limit the surface area. If there's a 94*333d2b36SAndroid Build Coastguard Worker // good use case put them back. 95*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "plugins", module.Library.properties.Plugins) 96*333d2b36SAndroid Build Coastguard Worker checkPropertyEmpty(ctx, module, "exported_plugins", module.Library.properties.Exported_plugins) 97*333d2b36SAndroid Build Coastguard Worker 98*333d2b36SAndroid Build Coastguard Worker srcJarPath, cacheOutputPath := module.callbacks.GenerateSourceJarBuildActions(module, ctx) 99*333d2b36SAndroid Build Coastguard Worker 100*333d2b36SAndroid Build Coastguard Worker module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath) 101*333d2b36SAndroid Build Coastguard Worker module.Library.properties.Aconfig_Cache_files = append(module.Library.properties.Aconfig_Cache_files, cacheOutputPath) 102*333d2b36SAndroid Build Coastguard Worker module.Library.GenerateAndroidBuildActions(ctx) 103*333d2b36SAndroid Build Coastguard Worker} 104*333d2b36SAndroid Build Coastguard Worker 105*333d2b36SAndroid Build Coastguard Worker// Add a rule to the jarjar renaming rules. See RepackageProviderData. 106*333d2b36SAndroid Build Coastguard Workerfunc (module *GeneratedJavaLibraryModule) AddJarJarRenameRule(original string, renamed string) { 107*333d2b36SAndroid Build Coastguard Worker module.addJarJarRenameRule(original, renamed) 108*333d2b36SAndroid Build Coastguard Worker} 109