blob: bfacea6da40cc0ca40e5166d1e0c145bc0ccd143 [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"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010022 "android/soong/dexpreopt"
Colin Crossa14fb6a2024-10-23 16:57:06 -070023
24 "github.com/google/blueprint/depset"
Colin Cross3d7c9822019-03-01 13:46:24 -080025)
26
27type DeviceHostConverter struct {
28 android.ModuleBase
29 android.DefaultableModuleBase
30
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)
81 return module
82}
83
84var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
85
86func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070087 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
88 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080089}
90
91func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070092 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
93 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080094}
95
96func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
97 if len(d.properties.Libs) < 1 {
98 ctx.PropertyErrorf("libs", "at least one dependency is required")
99 }
100
Colin Crossa14fb6a2024-10-23 16:57:06 -0700101 var transitiveHeaderJars []depset.DepSet[android.Path]
102 var transitiveImplementationJars []depset.DepSet[android.Path]
103 var transitiveResourceJars []depset.DepSet[android.Path]
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700104
Colin Cross3d7c9822019-03-01 13:46:24 -0800105 ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800106 if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800107 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 Crossc9b4f6b2024-07-26 15:25:46 -0700114
Colin Crossa14fb6a2024-10-23 16:57:06 -0700115 transitiveHeaderJars = append(transitiveHeaderJars, dep.TransitiveStaticLibsHeaderJars)
116 transitiveImplementationJars = append(transitiveImplementationJars, dep.TransitiveStaticLibsImplementationJars)
117 transitiveResourceJars = append(transitiveResourceJars, dep.TransitiveStaticLibsResourceJars)
Colin Cross3d7c9822019-03-01 13:46:24 -0800118 } else {
119 ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
120 }
121 })
Colin Cross28c3eb62019-04-23 22:04:46 -0700122
123 jarName := ctx.ModuleName() + ".jar"
124
125 if len(d.implementationAndResourceJars) > 1 {
126 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
127 TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
128 android.OptionalPath{}, false, nil, nil)
129 d.combinedImplementationJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700130 } else if len(d.implementationAndResourceJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700131 d.combinedImplementationJar = d.implementationAndResourceJars[0]
132 }
133
134 if len(d.headerJars) > 1 {
135 outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
136 TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700137 android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross28c3eb62019-04-23 22:04:46 -0700138 d.combinedHeaderJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700139 } else if len(d.headerJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700140 d.combinedHeaderJar = d.headerJars[0]
141 }
142
Colin Cross7727c7f2024-07-18 15:36:32 -0700143 android.SetProvider(ctx, JavaInfoProvider, &JavaInfo{
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700144 HeaderJars: d.headerJars,
145 LocalHeaderJars: d.headerJars,
Colin Crossa14fb6a2024-10-23 16:57:06 -0700146 TransitiveStaticLibsHeaderJars: depset.New(depset.PREORDER, nil, transitiveHeaderJars),
147 TransitiveStaticLibsImplementationJars: depset.New(depset.PREORDER, nil, transitiveImplementationJars),
148 TransitiveStaticLibsResourceJars: depset.New(depset.PREORDER, nil, transitiveResourceJars),
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700149 ImplementationAndResourcesJars: d.implementationAndResourceJars,
150 ImplementationJars: d.implementationJars,
151 ResourceJars: d.resourceJars,
152 SrcJarArgs: d.srcJarArgs,
153 SrcJarDeps: d.srcJarDeps,
154 StubsLinkType: Implementation,
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700155 // TODO: Not sure if aconfig flags that have been moved between device and host variants
156 // make sense.
Colin Crossdcf71b22021-02-01 13:59:03 -0800157 })
Colin Cross3d7c9822019-03-01 13:46:24 -0800158
Colin Crossdcf71b22021-02-01 13:59:03 -0800159}
Colin Cross3d7c9822019-03-01 13:46:24 -0800160
161func (d *DeviceHostConverter) HeaderJars() android.Paths {
162 return d.headerJars
163}
164
Colin Cross3d7c9822019-03-01 13:46:24 -0800165func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
166 return d.implementationAndResourceJars
167}
168
Spandan Das59a4a2b2024-01-09 21:35:56 +0000169func (d *DeviceHostConverter) DexJarBuildPath(ctx android.ModuleErrorfContext) android.Path {
Colin Cross3d7c9822019-03-01 13:46:24 -0800170 return nil
171}
172
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100173func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
174 return nil
175}
176
Colin Cross3d7c9822019-03-01 13:46:24 -0800177func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
178 return nil
179}
180
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100181func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross3d7c9822019-03-01 13:46:24 -0800182 return nil
183}
Colin Cross28c3eb62019-04-23 22:04:46 -0700184
Jiyong Park618922e2020-01-08 13:35:43 +0900185func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
186 return nil
187}
188
Colin Cross28c3eb62019-04-23 22:04:46 -0700189func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
190 return android.AndroidMkData{
191 Class: "JAVA_LIBRARIES",
192 OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
Dan Willemsen9fe14102021-07-13 21:52:04 -0700193 // Make does not support Windows Java modules
194 Disabled: d.Os() == android.Windows,
195 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Colin Cross28c3eb62019-04-23 22:04:46 -0700196 Extra: []android.AndroidMkExtraFunc{
197 func(w io.Writer, outputFile android.Path) {
198 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
199 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
200 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
201 },
202 },
203 }
204}
Spandan Dasf5ee86c2024-08-08 21:11:42 +0000205
206// implement the following interface for IDE completion.
207var _ android.IDEInfo = (*DeviceHostConverter)(nil)
208
Cole Faustb36d31d2024-08-27 16:04:28 -0700209func (d *DeviceHostConverter) IDEInfo(ctx android.BaseModuleContext, ideInfo *android.IdeInfo) {
Spandan Dasf5ee86c2024-08-08 21:11:42 +0000210 ideInfo.Deps = append(ideInfo.Deps, d.properties.Libs...)
211 ideInfo.Libs = append(ideInfo.Libs, d.properties.Libs...)
212}