blob: 3f4e3cda081f2efacc9b9f6925342b5f8c839b03 [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 Cross3d7c9822019-03-01 13:46:24 -080023)
24
25type DeviceHostConverter struct {
26 android.ModuleBase
27 android.DefaultableModuleBase
28
29 properties DeviceHostConverterProperties
30
31 headerJars android.Paths
32 implementationJars android.Paths
33 implementationAndResourceJars android.Paths
34 resourceJars android.Paths
Colin Cross28c3eb62019-04-23 22:04:46 -070035
Colin Cross0c4ce212019-05-03 15:28:19 -070036 srcJarArgs []string
37 srcJarDeps android.Paths
38
Colin Cross28c3eb62019-04-23 22:04:46 -070039 combinedHeaderJar android.Path
40 combinedImplementationJar android.Path
Colin Cross3d7c9822019-03-01 13:46:24 -080041}
42
43type DeviceHostConverterProperties struct {
44 // List of modules whose contents will be visible to modules that depend on this module.
45 Libs []string
46}
47
48type DeviceForHost struct {
49 DeviceHostConverter
50}
51
52// java_device_for_host makes the classes.jar output of a device java_library module available to host
53// java_library modules.
54//
Colin Cross440e0d02020-06-11 11:32:11 -070055// It is rarely necessary, and its usage is restricted to a few allowed projects.
Colin Cross3d7c9822019-03-01 13:46:24 -080056func DeviceForHostFactory() android.Module {
57 module := &DeviceForHost{}
58
59 module.AddProperties(&module.properties)
60
61 InitJavaModule(module, android.HostSupported)
62 return module
63}
64
65type HostForDevice struct {
66 DeviceHostConverter
67}
68
69// java_host_for_device makes the classes.jar output of a host java_library module available to device
70// java_library modules.
71//
Colin Cross440e0d02020-06-11 11:32:11 -070072// It is rarely necessary, and its usage is restricted to a few allowed projects.
Colin Cross3d7c9822019-03-01 13:46:24 -080073func HostForDeviceFactory() android.Module {
74 module := &HostForDevice{}
75
76 module.AddProperties(&module.properties)
77
78 InitJavaModule(module, android.DeviceSupported)
79 return module
80}
81
82var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
83
84func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070085 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
86 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080087}
88
89func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross0f7d2ef2019-10-16 11:03:10 -070090 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
91 deviceHostConverterDepTag, d.properties.Libs...)
Colin Cross3d7c9822019-03-01 13:46:24 -080092}
93
94func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
95 if len(d.properties.Libs) < 1 {
96 ctx.PropertyErrorf("libs", "at least one dependency is required")
97 }
98
Colin Crossc9b4f6b2024-07-26 15:25:46 -070099 var transitiveHeaderJars []*android.DepSet[android.Path]
100 var transitiveImplementationJars []*android.DepSet[android.Path]
101 var transitiveResourceJars []*android.DepSet[android.Path]
102
Colin Cross3d7c9822019-03-01 13:46:24 -0800103 ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
Colin Cross313aa542023-12-13 13:47:44 -0800104 if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -0800105 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 Crossc9b4f6b2024-07-26 15:25:46 -0700112
113 if dep.TransitiveStaticLibsHeaderJars != nil {
114 transitiveHeaderJars = append(transitiveHeaderJars, dep.TransitiveStaticLibsHeaderJars)
115 }
116 if dep.TransitiveStaticLibsImplementationJars != nil {
117 transitiveImplementationJars = append(transitiveImplementationJars, dep.TransitiveStaticLibsImplementationJars)
118 }
119 if dep.TransitiveStaticLibsResourceJars != nil {
120 transitiveResourceJars = append(transitiveResourceJars, dep.TransitiveStaticLibsResourceJars)
121 }
Colin Cross3d7c9822019-03-01 13:46:24 -0800122 } else {
123 ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
124 }
125 })
Colin Cross28c3eb62019-04-23 22:04:46 -0700126
127 jarName := ctx.ModuleName() + ".jar"
128
129 if len(d.implementationAndResourceJars) > 1 {
130 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
131 TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
132 android.OptionalPath{}, false, nil, nil)
133 d.combinedImplementationJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700134 } else if len(d.implementationAndResourceJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700135 d.combinedImplementationJar = d.implementationAndResourceJars[0]
136 }
137
138 if len(d.headerJars) > 1 {
139 outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
140 TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
Colin Cross6c6e6cd2019-05-08 14:30:12 -0700141 android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross28c3eb62019-04-23 22:04:46 -0700142 d.combinedHeaderJar = outputFile
Dan Willemsen9fe14102021-07-13 21:52:04 -0700143 } else if len(d.headerJars) == 1 {
Colin Cross28c3eb62019-04-23 22:04:46 -0700144 d.combinedHeaderJar = d.headerJars[0]
145 }
146
Colin Cross7727c7f2024-07-18 15:36:32 -0700147 android.SetProvider(ctx, JavaInfoProvider, &JavaInfo{
Colin Crossc9b4f6b2024-07-26 15:25:46 -0700148 HeaderJars: d.headerJars,
149 LocalHeaderJars: d.headerJars,
150 TransitiveStaticLibsHeaderJars: android.NewDepSet(android.PREORDER, nil, transitiveHeaderJars),
151 TransitiveStaticLibsImplementationJars: android.NewDepSet(android.PREORDER, nil, transitiveImplementationJars),
152 TransitiveStaticLibsResourceJars: android.NewDepSet(android.PREORDER, nil, transitiveResourceJars),
153 ImplementationAndResourcesJars: d.implementationAndResourceJars,
154 ImplementationJars: d.implementationJars,
155 ResourceJars: d.resourceJars,
156 SrcJarArgs: d.srcJarArgs,
157 SrcJarDeps: d.srcJarDeps,
158 StubsLinkType: Implementation,
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700159 // TODO: Not sure if aconfig flags that have been moved between device and host variants
160 // make sense.
Colin Crossdcf71b22021-02-01 13:59:03 -0800161 })
Colin Cross3d7c9822019-03-01 13:46:24 -0800162
Colin Crossdcf71b22021-02-01 13:59:03 -0800163}
Colin Cross3d7c9822019-03-01 13:46:24 -0800164
165func (d *DeviceHostConverter) HeaderJars() android.Paths {
166 return d.headerJars
167}
168
Colin Cross3d7c9822019-03-01 13:46:24 -0800169func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
170 return d.implementationAndResourceJars
171}
172
Spandan Das59a4a2b2024-01-09 21:35:56 +0000173func (d *DeviceHostConverter) DexJarBuildPath(ctx android.ModuleErrorfContext) android.Path {
Colin Cross3d7c9822019-03-01 13:46:24 -0800174 return nil
175}
176
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100177func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
178 return nil
179}
180
Colin Cross3d7c9822019-03-01 13:46:24 -0800181func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
182 return nil
183}
184
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100185func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
Colin Cross3d7c9822019-03-01 13:46:24 -0800186 return nil
187}
Colin Cross28c3eb62019-04-23 22:04:46 -0700188
Jiyong Park618922e2020-01-08 13:35:43 +0900189func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
190 return nil
191}
192
Colin Cross28c3eb62019-04-23 22:04:46 -0700193func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
194 return android.AndroidMkData{
195 Class: "JAVA_LIBRARIES",
196 OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
Dan Willemsen9fe14102021-07-13 21:52:04 -0700197 // Make does not support Windows Java modules
198 Disabled: d.Os() == android.Windows,
199 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
Colin Cross28c3eb62019-04-23 22:04:46 -0700200 Extra: []android.AndroidMkExtraFunc{
201 func(w io.Writer, outputFile android.Path) {
202 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
203 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
204 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
205 },
206 },
207 }
208}
Spandan Dasf5ee86c2024-08-08 21:11:42 +0000209
210// implement the following interface for IDE completion.
211var _ android.IDEInfo = (*DeviceHostConverter)(nil)
212
Cole Faustb36d31d2024-08-27 16:04:28 -0700213func (d *DeviceHostConverter) IDEInfo(ctx android.BaseModuleContext, ideInfo *android.IdeInfo) {
Spandan Dasf5ee86c2024-08-08 21:11:42 +0000214 ideInfo.Deps = append(ideInfo.Deps, d.properties.Libs...)
215 ideInfo.Libs = append(ideInfo.Libs, d.properties.Libs...)
216}