device_host_converter.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. package java
  15. import (
  16. "fmt"
  17. "io"
  18. "android/soong/android"
  19. "android/soong/bazel"
  20. "android/soong/dexpreopt"
  21. "github.com/google/blueprint/proptools"
  22. )
  23. type DeviceHostConverter struct {
  24. android.ModuleBase
  25. android.DefaultableModuleBase
  26. android.BazelModuleBase
  27. properties DeviceHostConverterProperties
  28. headerJars android.Paths
  29. implementationJars android.Paths
  30. implementationAndResourceJars android.Paths
  31. resourceJars android.Paths
  32. srcJarArgs []string
  33. srcJarDeps android.Paths
  34. combinedHeaderJar android.Path
  35. combinedImplementationJar android.Path
  36. }
  37. type DeviceHostConverterProperties struct {
  38. // List of modules whose contents will be visible to modules that depend on this module.
  39. Libs []string
  40. }
  41. type DeviceForHost struct {
  42. DeviceHostConverter
  43. }
  44. // java_device_for_host makes the classes.jar output of a device java_library module available to host
  45. // java_library modules.
  46. //
  47. // It is rarely necessary, and its usage is restricted to a few allowed projects.
  48. func DeviceForHostFactory() android.Module {
  49. module := &DeviceForHost{}
  50. module.AddProperties(&module.properties)
  51. InitJavaModule(module, android.HostSupported)
  52. return module
  53. }
  54. type HostForDevice struct {
  55. DeviceHostConverter
  56. }
  57. // java_host_for_device makes the classes.jar output of a host java_library module available to device
  58. // java_library modules.
  59. //
  60. // It is rarely necessary, and its usage is restricted to a few allowed projects.
  61. func HostForDeviceFactory() android.Module {
  62. module := &HostForDevice{}
  63. module.AddProperties(&module.properties)
  64. InitJavaModule(module, android.DeviceSupported)
  65. android.InitBazelModule(module)
  66. return module
  67. }
  68. var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
  69. func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
  70. ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
  71. deviceHostConverterDepTag, d.properties.Libs...)
  72. }
  73. func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
  74. ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
  75. deviceHostConverterDepTag, d.properties.Libs...)
  76. }
  77. func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  78. if len(d.properties.Libs) < 1 {
  79. ctx.PropertyErrorf("libs", "at least one dependency is required")
  80. }
  81. ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
  82. if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
  83. dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
  84. d.headerJars = append(d.headerJars, dep.HeaderJars...)
  85. d.implementationJars = append(d.implementationJars, dep.ImplementationJars...)
  86. d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...)
  87. d.resourceJars = append(d.resourceJars, dep.ResourceJars...)
  88. d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...)
  89. d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...)
  90. } else {
  91. ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
  92. }
  93. })
  94. jarName := ctx.ModuleName() + ".jar"
  95. if len(d.implementationAndResourceJars) > 1 {
  96. outputFile := android.PathForModuleOut(ctx, "combined", jarName)
  97. TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
  98. android.OptionalPath{}, false, nil, nil)
  99. d.combinedImplementationJar = outputFile
  100. } else if len(d.implementationAndResourceJars) == 1 {
  101. d.combinedImplementationJar = d.implementationAndResourceJars[0]
  102. }
  103. if len(d.headerJars) > 1 {
  104. outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
  105. TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
  106. android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
  107. d.combinedHeaderJar = outputFile
  108. } else if len(d.headerJars) == 1 {
  109. d.combinedHeaderJar = d.headerJars[0]
  110. }
  111. ctx.SetProvider(JavaInfoProvider, JavaInfo{
  112. HeaderJars: d.headerJars,
  113. ImplementationAndResourcesJars: d.implementationAndResourceJars,
  114. ImplementationJars: d.implementationJars,
  115. ResourceJars: d.resourceJars,
  116. SrcJarArgs: d.srcJarArgs,
  117. SrcJarDeps: d.srcJarDeps,
  118. })
  119. }
  120. func (d *DeviceHostConverter) HeaderJars() android.Paths {
  121. return d.headerJars
  122. }
  123. func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
  124. return d.implementationAndResourceJars
  125. }
  126. func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
  127. return nil
  128. }
  129. func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
  130. return nil
  131. }
  132. func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
  133. return nil
  134. }
  135. func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
  136. return nil
  137. }
  138. func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
  139. return nil
  140. }
  141. func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
  142. return android.AndroidMkData{
  143. Class: "JAVA_LIBRARIES",
  144. OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
  145. // Make does not support Windows Java modules
  146. Disabled: d.Os() == android.Windows,
  147. Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
  148. Extra: []android.AndroidMkExtraFunc{
  149. func(w io.Writer, outputFile android.Path) {
  150. fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
  151. fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
  152. fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
  153. },
  154. },
  155. }
  156. }
  157. type bazelDeviceHostConverterAttributes struct {
  158. Exports bazel.LabelListAttribute
  159. }
  160. func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  161. ctx.CreateBazelTargetModule(
  162. bazel.BazelTargetModuleProperties{
  163. Rule_class: "java_host_for_device",
  164. Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl",
  165. },
  166. android.CommonAttributes{Name: d.Name()},
  167. &bazelDeviceHostConverterAttributes{
  168. Exports: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)),
  169. },
  170. )
  171. neverLinkAttrs := &javaLibraryAttributes{
  172. Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}),
  173. Neverlink: bazel.BoolAttribute{Value: proptools.BoolPtr(true)},
  174. javaCommonAttributes: &javaCommonAttributes{
  175. Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")},
  176. },
  177. }
  178. ctx.CreateBazelTargetModule(
  179. javaLibraryBazelTargetModuleProperties(),
  180. android.CommonAttributes{Name: d.Name() + "-neverlink"},
  181. neverLinkAttrs)
  182. }