blob: 930bfd2670215b9ca22471dd27535931c38e8e50 [file] [log] [blame]
Joe Onorato175073c2023-06-01 14:42:59 -07001// 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
15package java
16
17import (
18 "android/soong/android"
19)
20
21type GeneratedJavaLibraryModule struct {
22 Library
23 callbacks GeneratedJavaLibraryCallbacks
24 moduleName string
Joe Onorato8f755852023-08-25 20:23:32 -070025
26 // true if we've already called DepsMutator. Can't call AddLibrary or AddSharedLibrary
27 // after DepsMutator.
28 depsMutatorDone bool
Joe Onorato175073c2023-06-01 14:42:59 -070029}
30
31type 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.
Joe Onorato6fe59eb2023-07-16 13:20:33 -070037 GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path
Yu Liuf2b94012023-09-19 15:09:10 -070038
39 Bp2build(ctx android.Bp2buildMutatorContext, module *GeneratedJavaLibraryModule)
Joe Onorato175073c2023-06-01 14:42:59 -070040}
41
42// GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated
43// source code, including ones outside the java package to build jar files
44// from that generated source.
45//
46// To use GeneratedJavaLibraryModule, call GeneratedJavaLibraryModuleFactory with
47// a callback interface and a properties object to add to the module.
48//
49// These modules will have some properties blocked, and it will be an error if
50// modules attempt to set them. See the list of property names in GeneratedAndroidBuildActions
51// for the list of those properties.
52func GeneratedJavaLibraryModuleFactory(moduleName string, callbacks GeneratedJavaLibraryCallbacks, properties interface{}) android.Module {
53 module := &GeneratedJavaLibraryModule{
54 callbacks: callbacks,
55 moduleName: moduleName,
56 }
57 module.addHostAndDeviceProperties()
58 module.initModuleAndImport(module)
59 android.InitApexModule(module)
60 android.InitBazelModule(module)
61 InitJavaModule(module, android.HostAndDeviceSupported)
62 if properties != nil {
63 module.AddProperties(properties)
64 }
65 return module
66}
67
Joe Onorato8f755852023-08-25 20:23:32 -070068// Add a java shared library as a dependency, as if they had said `libs: [ "name" ]`
69func (module *GeneratedJavaLibraryModule) AddSharedLibrary(name string) {
70 if module.depsMutatorDone {
71 panic("GeneratedJavaLibraryModule.AddLibrary called after DepsMutator")
72 }
73 module.Library.properties.Libs = append(module.Library.properties.Libs, name)
74}
75
76// Add a java shared library as a dependency, as if they had said `libs: [ "name" ]`
77func (module *GeneratedJavaLibraryModule) AddStaticLibrary(name string) {
78 if module.depsMutatorDone {
79 panic("GeneratedJavaLibraryModule.AddStaticLibrary called after DepsMutator")
80 }
81 module.Library.properties.Static_libs = append(module.Library.properties.Static_libs, name)
82}
83
Joe Onorato175073c2023-06-01 14:42:59 -070084func (module *GeneratedJavaLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
85 module.callbacks.DepsMutator(module, ctx)
Joe Onorato8f755852023-08-25 20:23:32 -070086 module.depsMutatorDone = true
Joe Onorato175073c2023-06-01 14:42:59 -070087 module.Library.DepsMutator(ctx)
88}
89
90func checkPropertyEmpty(ctx android.ModuleContext, module *GeneratedJavaLibraryModule, name string, value []string) {
91 if len(value) != 0 {
92 ctx.PropertyErrorf(name, "%s not allowed on %s", name, module.moduleName)
93 }
94}
95
96func (module *GeneratedJavaLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
97 // These modules are all-generated, so disallow these properties to keep it simple.
98 // No additional sources
99 checkPropertyEmpty(ctx, module, "srcs", module.Library.properties.Srcs)
100 checkPropertyEmpty(ctx, module, "common_srcs", module.Library.properties.Common_srcs)
101 checkPropertyEmpty(ctx, module, "exclude_srcs", module.Library.properties.Exclude_srcs)
102 checkPropertyEmpty(ctx, module, "java_resource_dirs", module.Library.properties.Java_resource_dirs)
103 checkPropertyEmpty(ctx, module, "exclude_java_resource_dirs", module.Library.properties.Exclude_java_resource_dirs)
Joe Onorato175073c2023-06-01 14:42:59 -0700104 // Restrict these for no good reason other than to limit the surface area. If there's a
105 // good use case put them back.
106 checkPropertyEmpty(ctx, module, "plugins", module.Library.properties.Plugins)
107 checkPropertyEmpty(ctx, module, "exported_plugins", module.Library.properties.Exported_plugins)
108
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700109 srcJarPath := module.callbacks.GenerateSourceJarBuildActions(module, ctx)
Joe Onorato175073c2023-06-01 14:42:59 -0700110 module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath)
111 module.Library.GenerateAndroidBuildActions(ctx)
112}
Yu Liuf2b94012023-09-19 15:09:10 -0700113
114func (module *GeneratedJavaLibraryModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
115 module.callbacks.Bp2build(ctx, module)
116}