generated_java_library.go 4.1 KB

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