blob: 3581040f81173bc1d988b4fae372e80d85e316bc [file] [log] [blame]
Colin Cross3d7c9822019-03-01 13:46:24 -08001// 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
15package java
16
17import (
Colin Cross28c3eb62019-04-23 22:04:46 -070018 "fmt"
19 "io"
Colin Cross3d7c9822019-03-01 13:46:24 -080020
Colin Cross28c3eb62019-04-23 22:04:46 -070021 "android/soong/android"
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -040022 "android/soong/bazel"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010023 "android/soong/dexpreopt"
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050024
25 "github.com/google/blueprint/proptools"
Colin Cross3d7c9822019-03-01 13:46:24 -080026)
27
28type DeviceHostConverter struct {
29 android.ModuleBase
30 android.DefaultableModuleBase
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -040031 android.BazelModuleBase
Colin Cross3d7c9822019-03-01 13:46:24 -080032
33 properties DeviceHostConverterProperties
34
35 headerJars android.Paths
36 implementationJars android.Paths
37 implementationAndResourceJars android.Paths
38 resourceJars android.Paths
Colin Cross28c3eb62019-04-23 22:04:46 -070039
Colin Cross0c4ce212019-05-03 15:28:19 -070040 srcJarArgs []string
41 srcJarDeps android.Paths
42
Colin Cross28c3eb62019-04-23 22:04:46 -070043 combinedHeaderJar android.Path
44 combinedImplementationJar android.Path
Colin Cross3d7c9822019-03-01 13:46:24 -080045}
46
47type DeviceHostConverterProperties struct {
48 // List of modules whose contents will be visible to modules that depend on this module.
49 Libs []string
50}
51
52type DeviceForHost struct {
53 DeviceHostConverter
54}
55
56// java_device_for_host makes the classes.jar output of a device java_library module available to host
57// java_library modules.
58//
Colin Cross440e0d02020-06-11 11:32:11 -070059// It is rarely necessary, and its usage is restricted to a few allowed projects.
Colin Cross3d7c9822019-03-01 13:46:24 -080060func DeviceForHostFactory() android.Module {
61 module := &DeviceForHost{}
62
63 module.AddProperties(&module.properties)
64
65 InitJavaModule(module, android.HostSupported)
66 return module
67}
68
69type HostForDevice struct {
70 DeviceHostConverter
71}
72
73// java_host_for_device makes the classes.jar output of a host java_library module available to device
74// java_library modules.
75//
Colin Cross440e0d02020-06-11 11:32:11 -070076// It is rarely necessary, and its usage is restricted to a few allowed projects.
Colin Cross3d7c9822019-03-01 13:46:24 -080077func HostForDeviceFactory() android.Module {
78 module := &HostForDevice{}
79
80 module.AddProperties(&module.properties)
81
82 InitJavaModule(module, android.DeviceSupported)
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -040083 android.InitBazelModule(module)
Colin Cross3d7c9822019-03-01 13:46:24 -080084 return module
85}
86
87var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
88
89func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070090 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
91 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080092}
93
94func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070095 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
96 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080097}
98
99func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
100 if len(d.properties.Libs) < 1 {
101 ctx.PropertyErrorf("libs", "at least one dependency is required")
102 }
103
104 ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800105 if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
106 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
107 d.headerJars = append(d.headerJars, dep.HeaderJars...)
108 d.implementationJars = append(d.implementationJars, dep.ImplementationJars...)
109 d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...)
110 d.resourceJars = append(d.resourceJars, dep.ResourceJars...)
Colin Cross0c4ce212019-05-03 15:28:19 -0700111
Colin Crossdcf71b22021-02-01 13:59:03 -0800112 d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...)
113 d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...)
Colin Cross3d7c9822019-03-01 13:46:24 -0800114 } else {
115 ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
116 }
117 })
Colin Cross28c3eb62019-04-23 22:04:46 -0700118
119 jarName := ctx.ModuleName() + ".jar"
120
121 if len(d.implementationAndResourceJars) > 1 {
122 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
123 TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
124 android.OptionalPath{}, false, nil, nil)
125 d.combinedImplementationJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700126 } else if len(d.implementationAndResourceJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700127 d.combinedImplementationJar = d.implementationAndResourceJars[0]
128 }
129
130 if len(d.headerJars) > 1 {
131 outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
132 TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700133 android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross28c3eb62019-04-23 22:04:46 -0700134 d.combinedHeaderJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700135 } else if len(d.headerJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700136 d.combinedHeaderJar = d.headerJars[0]
137 }
138
Colin Crossdcf71b22021-02-01 13:59:03 -0800139 ctx.SetProvider(JavaInfoProvider, JavaInfo{
140 HeaderJars: d.headerJars,
141 ImplementationAndResourcesJars: d.implementationAndResourceJars,
142 ImplementationJars: d.implementationJars,
143 ResourceJars: d.resourceJars,
144 SrcJarArgs: d.srcJarArgs,
145 SrcJarDeps: d.srcJarDeps,
146 })
Colin Cross3d7c9822019-03-01 13:46:24 -0800147
Colin Crossdcf71b22021-02-01 13:59:03 -0800148}
Colin Cross3d7c9822019-03-01 13:46:24 -0800149
150func (d *DeviceHostConverter) HeaderJars() android.Paths {
151 return d.headerJars
152}
153
Colin Cross3d7c9822019-03-01 13:46:24 -0800154func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
155 return d.implementationAndResourceJars
156}
157
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000158func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
Colin Cross3d7c9822019-03-01 13:46:24 -0800159 return nil
160}
161
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100162func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
163 return nil
164}
165
Colin Cross3d7c9822019-03-01 13:46:24 -0800166func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
167 return nil
168}
169
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100170func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross3d7c9822019-03-01 13:46:24 -0800171 return nil
172}
Colin Cross28c3eb62019-04-23 22:04:46 -0700173
Jiyong Park618922e2020-01-08 13:35:43 +0900174func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
175 return nil
176}
177
Colin Cross28c3eb62019-04-23 22:04:46 -0700178func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
179 return android.AndroidMkData{
180 Class: "JAVA_LIBRARIES",
181 OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
Dan Willemsen9fe14102021-07-13 21:52:04 -0700182 // Make does not support Windows Java modules
183 Disabled: d.Os() == android.Windows,
184 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Colin Cross28c3eb62019-04-23 22:04:46 -0700185 Extra: []android.AndroidMkExtraFunc{
186 func(w io.Writer, outputFile android.Path) {
187 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
188 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
189 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
190 },
191 },
192 }
193}
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400194
195type bazelDeviceHostConverterAttributes struct {
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500196 Exports bazel.LabelListAttribute
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400197}
198
199func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
200 ctx.CreateBazelTargetModule(
201 bazel.BazelTargetModuleProperties{
202 Rule_class: "java_host_for_device",
203 Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl",
204 },
205 android.CommonAttributes{Name: d.Name()},
206 &bazelDeviceHostConverterAttributes{
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500207 Exports: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)),
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400208 },
209 )
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400210 neverLinkAttrs := &javaLibraryAttributes{
211 Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}),
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500212 Neverlink: bazel.BoolAttribute{Value: proptools.BoolPtr(true)},
213 javaCommonAttributes: &javaCommonAttributes{
214 Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")},
215 },
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400216 }
217 ctx.CreateBazelTargetModule(
218 javaLibraryBazelTargetModuleProperties(),
219 android.CommonAttributes{Name: d.Name() + "-neverlink"},
220 neverLinkAttrs)
221
222}