blob: 5460dc99317f9256dc13ef1a1769c6c85d2bd78f [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,
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700146 // TODO: Not sure if aconfig flags that have been moved between device and host variants
147 // make sense.
Colin Crossdcf71b22021-02-01 13:59:03 -0800148 })
Colin Cross3d7c9822019-03-01 13:46:24 -0800149
Colin Crossdcf71b22021-02-01 13:59:03 -0800150}
Colin Cross3d7c9822019-03-01 13:46:24 -0800151
152func (d *DeviceHostConverter) HeaderJars() android.Paths {
153 return d.headerJars
154}
155
Colin Cross3d7c9822019-03-01 13:46:24 -0800156func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
157 return d.implementationAndResourceJars
158}
159
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000160func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
Colin Cross3d7c9822019-03-01 13:46:24 -0800161 return nil
162}
163
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100164func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
165 return nil
166}
167
Colin Cross3d7c9822019-03-01 13:46:24 -0800168func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
169 return nil
170}
171
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100172func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross3d7c9822019-03-01 13:46:24 -0800173 return nil
174}
Colin Cross28c3eb62019-04-23 22:04:46 -0700175
Jiyong Park618922e2020-01-08 13:35:43 +0900176func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
177 return nil
178}
179
Colin Cross28c3eb62019-04-23 22:04:46 -0700180func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
181 return android.AndroidMkData{
182 Class: "JAVA_LIBRARIES",
183 OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
Dan Willemsen9fe14102021-07-13 21:52:04 -0700184 // Make does not support Windows Java modules
185 Disabled: d.Os() == android.Windows,
186 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Colin Cross28c3eb62019-04-23 22:04:46 -0700187 Extra: []android.AndroidMkExtraFunc{
188 func(w io.Writer, outputFile android.Path) {
189 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
190 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
191 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
192 },
193 },
194 }
195}
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400196
197type bazelDeviceHostConverterAttributes struct {
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500198 Exports bazel.LabelListAttribute
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400199}
200
201func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
202 ctx.CreateBazelTargetModule(
203 bazel.BazelTargetModuleProperties{
204 Rule_class: "java_host_for_device",
205 Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl",
206 },
207 android.CommonAttributes{Name: d.Name()},
208 &bazelDeviceHostConverterAttributes{
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500209 Exports: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)),
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400210 },
211 )
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400212 neverLinkAttrs := &javaLibraryAttributes{
213 Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}),
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500214 Neverlink: bazel.BoolAttribute{Value: proptools.BoolPtr(true)},
215 javaCommonAttributes: &javaCommonAttributes{
216 Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")},
217 },
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400218 }
219 ctx.CreateBazelTargetModule(
220 javaLibraryBazelTargetModuleProperties(),
221 android.CommonAttributes{Name: d.Name() + "-neverlink"},
222 neverLinkAttrs)
223
224}