| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 |  | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 17 | import ( | 
|  | 18 | "android/soong/android" | 
|  | 19 | "android/soong/bazel" | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 20 | ) | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 21 |  | 
|  | 22 | func init() { | 
| Paul Duffin | c029c43 | 2021-03-22 15:00:28 +0000 | [diff] [blame] | 23 | registerJavaPluginBuildComponents(android.InitRegistrationContext) | 
|  | 24 | } | 
|  | 25 |  | 
|  | 26 | func registerJavaPluginBuildComponents(ctx android.RegistrationContext) { | 
|  | 27 | ctx.RegisterModuleType("java_plugin", PluginFactory) | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 28 | } | 
|  | 29 |  | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 30 | func PluginFactory() android.Module { | 
|  | 31 | module := &Plugin{} | 
|  | 32 |  | 
| Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 33 | module.addHostProperties() | 
|  | 34 | module.AddProperties(&module.pluginProperties) | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | InitJavaModule(module, android.HostSupported) | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 37 |  | 
|  | 38 | android.InitBazelModule(module) | 
|  | 39 |  | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 40 | return module | 
|  | 41 | } | 
|  | 42 |  | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 43 | // Plugin describes a java_plugin module, a host java library that will be used by javac as an annotation processor. | 
| Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 44 | type Plugin struct { | 
|  | 45 | Library | 
|  | 46 |  | 
|  | 47 | pluginProperties PluginProperties | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | type PluginProperties struct { | 
|  | 51 | // The optional name of the class that javac will use to run the annotation processor. | 
|  | 52 | Processor_class *string | 
|  | 53 |  | 
|  | 54 | // If true, assume the annotation processor will generate classes that are referenced from outside the module. | 
|  | 55 | // This necessitates disabling the turbine optimization on modules that use this plugin, which will reduce | 
|  | 56 | // parallelism and cause more recompilation for modules that depend on modules that use this plugin. | 
|  | 57 | Generates_api *bool | 
|  | 58 | } | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 59 |  | 
|  | 60 | type pluginAttributes struct { | 
| Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 61 | *javaCommonAttributes | 
|  | 62 | Deps            bazel.LabelListAttribute | 
|  | 63 | Processor_class *string | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | // ConvertWithBp2build is used to convert android_app to Bazel. | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 67 | func (p *Plugin) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { | 
| Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 68 | pluginName := p.Name() | 
| Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 69 | commonAttrs, bp2BuildInfo, supported := p.convertLibraryAttrsBp2Build(ctx) | 
|  | 70 | if !supported { | 
|  | 71 | return | 
|  | 72 | } | 
| Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 73 | depLabels := bp2BuildInfo.DepLabels | 
| Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 74 |  | 
|  | 75 | deps := depLabels.Deps | 
|  | 76 | deps.Append(depLabels.StaticDeps) | 
|  | 77 |  | 
|  | 78 | var processorClass *string | 
|  | 79 | if p.pluginProperties.Processor_class != nil { | 
|  | 80 | processorClass = p.pluginProperties.Processor_class | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 83 | attrs := &pluginAttributes{ | 
|  | 84 | javaCommonAttributes: commonAttrs, | 
|  | 85 | Deps:                 deps, | 
|  | 86 | Processor_class:      processorClass, | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 89 | props := bazel.BazelTargetModuleProperties{ | 
|  | 90 | Rule_class: "java_plugin", | 
|  | 91 | } | 
| Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 92 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: pluginName}, attrs) | 
| Sam Delmerico | c06ea03 | 2022-01-28 21:11:03 +0000 | [diff] [blame] | 93 | } |