device_host_converter.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/dexpreopt"
  20. )
  21. type DeviceHostConverter struct {
  22. android.ModuleBase
  23. android.DefaultableModuleBase
  24. properties DeviceHostConverterProperties
  25. headerJars android.Paths
  26. implementationJars android.Paths
  27. implementationAndResourceJars android.Paths
  28. resourceJars android.Paths
  29. srcJarArgs []string
  30. srcJarDeps android.Paths
  31. combinedHeaderJar android.Path
  32. combinedImplementationJar android.Path
  33. }
  34. type DeviceHostConverterProperties struct {
  35. // List of modules whose contents will be visible to modules that depend on this module.
  36. Libs []string
  37. }
  38. type DeviceForHost struct {
  39. DeviceHostConverter
  40. }
  41. // java_device_for_host makes the classes.jar output of a device java_library module available to host
  42. // java_library modules.
  43. //
  44. // It is rarely necessary, and its usage is restricted to a few allowed projects.
  45. func DeviceForHostFactory() android.Module {
  46. module := &DeviceForHost{}
  47. module.AddProperties(&module.properties)
  48. InitJavaModule(module, android.HostSupported)
  49. return module
  50. }
  51. type HostForDevice struct {
  52. DeviceHostConverter
  53. }
  54. // java_host_for_device makes the classes.jar output of a host java_library module available to device
  55. // java_library modules.
  56. //
  57. // It is rarely necessary, and its usage is restricted to a few allowed projects.
  58. func HostForDeviceFactory() android.Module {
  59. module := &HostForDevice{}
  60. module.AddProperties(&module.properties)
  61. InitJavaModule(module, android.DeviceSupported)
  62. return module
  63. }
  64. var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
  65. func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
  66. ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
  67. deviceHostConverterDepTag, d.properties.Libs...)
  68. }
  69. func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
  70. ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
  71. deviceHostConverterDepTag, d.properties.Libs...)
  72. }
  73. func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  74. if len(d.properties.Libs) < 1 {
  75. ctx.PropertyErrorf("libs", "at least one dependency is required")
  76. }
  77. ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
  78. if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
  79. dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
  80. d.headerJars = append(d.headerJars, dep.HeaderJars...)
  81. d.implementationJars = append(d.implementationJars, dep.ImplementationJars...)
  82. d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...)
  83. d.resourceJars = append(d.resourceJars, dep.ResourceJars...)
  84. d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...)
  85. d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...)
  86. } else {
  87. ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
  88. }
  89. })
  90. jarName := ctx.ModuleName() + ".jar"
  91. if len(d.implementationAndResourceJars) > 1 {
  92. outputFile := android.PathForModuleOut(ctx, "combined", jarName)
  93. TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
  94. android.OptionalPath{}, false, nil, nil)
  95. d.combinedImplementationJar = outputFile
  96. } else {
  97. d.combinedImplementationJar = d.implementationAndResourceJars[0]
  98. }
  99. if len(d.headerJars) > 1 {
  100. outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
  101. TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
  102. android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
  103. d.combinedHeaderJar = outputFile
  104. } else {
  105. d.combinedHeaderJar = d.headerJars[0]
  106. }
  107. ctx.SetProvider(JavaInfoProvider, JavaInfo{
  108. HeaderJars: d.headerJars,
  109. ImplementationAndResourcesJars: d.implementationAndResourceJars,
  110. ImplementationJars: d.implementationJars,
  111. ResourceJars: d.resourceJars,
  112. SrcJarArgs: d.srcJarArgs,
  113. SrcJarDeps: d.srcJarDeps,
  114. })
  115. }
  116. func (d *DeviceHostConverter) HeaderJars() android.Paths {
  117. return d.headerJars
  118. }
  119. func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
  120. return d.implementationAndResourceJars
  121. }
  122. func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
  123. return nil
  124. }
  125. func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
  126. return nil
  127. }
  128. func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
  129. return nil
  130. }
  131. func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
  132. return nil
  133. }
  134. func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
  135. return nil
  136. }
  137. func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
  138. return android.AndroidMkData{
  139. Class: "JAVA_LIBRARIES",
  140. OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
  141. Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
  142. Extra: []android.AndroidMkExtraFunc{
  143. func(w io.Writer, outputFile android.Path) {
  144. fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
  145. fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
  146. fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
  147. },
  148. },
  149. }
  150. }