blob: f9baa85e48705493bcce219ec9b31886cba45a87 [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
25}
26
27type GeneratedJavaLibraryCallbacks interface {
28 // Called from inside DepsMutator, gives a chance to AddDependencies
29 DepsMutator(module *GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext)
30
31 // Called from inside GenerateAndroidBuildActions. Add the build rules to
32 // make the srcjar, and return the path to it.
Joe Onorato6fe59eb2023-07-16 13:20:33 -070033 GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) android.Path
Joe Onorato175073c2023-06-01 14:42:59 -070034}
35
36// GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated
37// source code, including ones outside the java package to build jar files
38// from that generated source.
39//
40// To use GeneratedJavaLibraryModule, call GeneratedJavaLibraryModuleFactory with
41// a callback interface and a properties object to add to the module.
42//
43// These modules will have some properties blocked, and it will be an error if
44// modules attempt to set them. See the list of property names in GeneratedAndroidBuildActions
45// for the list of those properties.
46func GeneratedJavaLibraryModuleFactory(moduleName string, callbacks GeneratedJavaLibraryCallbacks, properties interface{}) android.Module {
47 module := &GeneratedJavaLibraryModule{
48 callbacks: callbacks,
49 moduleName: moduleName,
50 }
51 module.addHostAndDeviceProperties()
52 module.initModuleAndImport(module)
53 android.InitApexModule(module)
54 android.InitBazelModule(module)
55 InitJavaModule(module, android.HostAndDeviceSupported)
56 if properties != nil {
57 module.AddProperties(properties)
58 }
59 return module
60}
61
62func (module *GeneratedJavaLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
63 module.callbacks.DepsMutator(module, ctx)
64 module.Library.DepsMutator(ctx)
65}
66
67func checkPropertyEmpty(ctx android.ModuleContext, module *GeneratedJavaLibraryModule, name string, value []string) {
68 if len(value) != 0 {
69 ctx.PropertyErrorf(name, "%s not allowed on %s", name, module.moduleName)
70 }
71}
72
73func (module *GeneratedJavaLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
74 // These modules are all-generated, so disallow these properties to keep it simple.
75 // No additional sources
76 checkPropertyEmpty(ctx, module, "srcs", module.Library.properties.Srcs)
77 checkPropertyEmpty(ctx, module, "common_srcs", module.Library.properties.Common_srcs)
78 checkPropertyEmpty(ctx, module, "exclude_srcs", module.Library.properties.Exclude_srcs)
79 checkPropertyEmpty(ctx, module, "java_resource_dirs", module.Library.properties.Java_resource_dirs)
80 checkPropertyEmpty(ctx, module, "exclude_java_resource_dirs", module.Library.properties.Exclude_java_resource_dirs)
81 // No additional libraries. The generator should add anything necessary automatically
82 // by returning something from ____ (TODO: Additional libraries aren't needed now, so
83 // these are just blocked).
84 checkPropertyEmpty(ctx, module, "libs", module.Library.properties.Libs)
85 checkPropertyEmpty(ctx, module, "static_libs", module.Library.properties.Static_libs)
86 // Restrict these for no good reason other than to limit the surface area. If there's a
87 // good use case put them back.
88 checkPropertyEmpty(ctx, module, "plugins", module.Library.properties.Plugins)
89 checkPropertyEmpty(ctx, module, "exported_plugins", module.Library.properties.Exported_plugins)
90
Joe Onorato6fe59eb2023-07-16 13:20:33 -070091 srcJarPath := module.callbacks.GenerateSourceJarBuildActions(module, ctx)
Joe Onorato175073c2023-06-01 14:42:59 -070092 module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath)
93 module.Library.GenerateAndroidBuildActions(ctx)
94}