blob: 656c866edf591c5ca3c56f495558c547fcd33648 [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"
Colin Cross3d7c9822019-03-01 13:46:24 -080024)
25
26type DeviceHostConverter struct {
27 android.ModuleBase
28 android.DefaultableModuleBase
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -040029 android.BazelModuleBase
Colin Cross3d7c9822019-03-01 13:46:24 -080030
31 properties DeviceHostConverterProperties
32
33 headerJars android.Paths
34 implementationJars android.Paths
35 implementationAndResourceJars android.Paths
36 resourceJars android.Paths
Colin Cross28c3eb62019-04-23 22:04:46 -070037
Colin Cross0c4ce212019-05-03 15:28:19 -070038 srcJarArgs []string
39 srcJarDeps android.Paths
40
Colin Cross28c3eb62019-04-23 22:04:46 -070041 combinedHeaderJar android.Path
42 combinedImplementationJar android.Path
Colin Cross3d7c9822019-03-01 13:46:24 -080043}
44
45type DeviceHostConverterProperties struct {
46 // List of modules whose contents will be visible to modules that depend on this module.
47 Libs []string
48}
49
50type DeviceForHost struct {
51 DeviceHostConverter
52}
53
54// java_device_for_host makes the classes.jar output of a device java_library module available to host
55// java_library modules.
56//
Colin Cross440e0d02020-06-11 11:32:11 -070057// It is rarely necessary, and its usage is restricted to a few allowed projects.
Colin Cross3d7c9822019-03-01 13:46:24 -080058func DeviceForHostFactory() android.Module {
59 module := &DeviceForHost{}
60
61 module.AddProperties(&module.properties)
62
63 InitJavaModule(module, android.HostSupported)
64 return module
65}
66
67type HostForDevice struct {
68 DeviceHostConverter
69}
70
71// java_host_for_device makes the classes.jar output of a host java_library module available to device
72// java_library modules.
73//
Colin Cross440e0d02020-06-11 11:32:11 -070074// It is rarely necessary, and its usage is restricted to a few allowed projects.
Colin Cross3d7c9822019-03-01 13:46:24 -080075func HostForDeviceFactory() android.Module {
76 module := &HostForDevice{}
77
78 module.AddProperties(&module.properties)
79
80 InitJavaModule(module, android.DeviceSupported)
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -040081 android.InitBazelModule(module)
Colin Cross3d7c9822019-03-01 13:46:24 -080082 return module
83}
84
85var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
86
87func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070088 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
89 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080090}
91
92func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070093 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
94 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080095}
96
97func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
98 if len(d.properties.Libs) < 1 {
99 ctx.PropertyErrorf("libs", "at least one dependency is required")
100 }
101
102 ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
Colin Crossdcf71b22021-02-01 13:59:03 -0800103 if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
104 dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
105 d.headerJars = append(d.headerJars, dep.HeaderJars...)
106 d.implementationJars = append(d.implementationJars, dep.ImplementationJars...)
107 d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...)
108 d.resourceJars = append(d.resourceJars, dep.ResourceJars...)
Colin Cross0c4ce212019-05-03 15:28:19 -0700109
Colin Crossdcf71b22021-02-01 13:59:03 -0800110 d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...)
111 d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...)
Colin Cross3d7c9822019-03-01 13:46:24 -0800112 } else {
113 ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
114 }
115 })
Colin Cross28c3eb62019-04-23 22:04:46 -0700116
117 jarName := ctx.ModuleName() + ".jar"
118
119 if len(d.implementationAndResourceJars) > 1 {
120 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
121 TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
122 android.OptionalPath{}, false, nil, nil)
123 d.combinedImplementationJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700124 } else if len(d.implementationAndResourceJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700125 d.combinedImplementationJar = d.implementationAndResourceJars[0]
126 }
127
128 if len(d.headerJars) > 1 {
129 outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
130 TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700131 android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross28c3eb62019-04-23 22:04:46 -0700132 d.combinedHeaderJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700133 } else if len(d.headerJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700134 d.combinedHeaderJar = d.headerJars[0]
135 }
136
Colin Crossdcf71b22021-02-01 13:59:03 -0800137 ctx.SetProvider(JavaInfoProvider, JavaInfo{
138 HeaderJars: d.headerJars,
139 ImplementationAndResourcesJars: d.implementationAndResourceJars,
140 ImplementationJars: d.implementationJars,
141 ResourceJars: d.resourceJars,
142 SrcJarArgs: d.srcJarArgs,
143 SrcJarDeps: d.srcJarDeps,
144 })
Colin Cross3d7c9822019-03-01 13:46:24 -0800145
Colin Crossdcf71b22021-02-01 13:59:03 -0800146}
Colin Cross3d7c9822019-03-01 13:46:24 -0800147
148func (d *DeviceHostConverter) HeaderJars() android.Paths {
149 return d.headerJars
150}
151
Colin Cross3d7c9822019-03-01 13:46:24 -0800152func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
153 return d.implementationAndResourceJars
154}
155
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000156func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
Colin Cross3d7c9822019-03-01 13:46:24 -0800157 return nil
158}
159
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100160func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
161 return nil
162}
163
Colin Cross3d7c9822019-03-01 13:46:24 -0800164func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
165 return nil
166}
167
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100168func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross3d7c9822019-03-01 13:46:24 -0800169 return nil
170}
Colin Cross28c3eb62019-04-23 22:04:46 -0700171
Jiyong Park618922e2020-01-08 13:35:43 +0900172func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
173 return nil
174}
175
Colin Cross28c3eb62019-04-23 22:04:46 -0700176func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
177 return android.AndroidMkData{
178 Class: "JAVA_LIBRARIES",
179 OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
Dan Willemsen9fe14102021-07-13 21:52:04 -0700180 // Make does not support Windows Java modules
181 Disabled: d.Os() == android.Windows,
182 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Colin Cross28c3eb62019-04-23 22:04:46 -0700183 Extra: []android.AndroidMkExtraFunc{
184 func(w io.Writer, outputFile android.Path) {
185 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
186 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
187 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
188 },
189 },
190 }
191}
Romain Jobredeauxe7370ea2023-04-04 10:30:14 -0400192
193type bazelDeviceHostConverterAttributes struct {
194 Deps bazel.LabelListAttribute
195}
196
197func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
198 ctx.CreateBazelTargetModule(
199 bazel.BazelTargetModuleProperties{
200 Rule_class: "java_host_for_device",
201 Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl",
202 },
203 android.CommonAttributes{Name: d.Name()},
204 &bazelDeviceHostConverterAttributes{
205 Deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)),
206 },
207 )
208 neverlinkProp := true
209 neverLinkAttrs := &javaLibraryAttributes{
210 Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}),
211 Neverlink: bazel.BoolAttribute{Value: &neverlinkProp},
212 }
213 ctx.CreateBazelTargetModule(
214 javaLibraryBazelTargetModuleProperties(),
215 android.CommonAttributes{Name: d.Name() + "-neverlink"},
216 neverLinkAttrs)
217
218}