blob: 1714152075b36899e7b129d9a5a02cd3f25c2a91 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 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
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Wei Libafb6d62021-12-10 03:14:59 -080024 "strings"
Colin Cross2fe66872015-03-30 17:20:39 -070025
Wei Libafb6d62021-12-10 03:14:59 -080026 "android/soong/bazel"
Sam Delmerico4e272292022-01-06 20:03:51 +000027
Colin Cross2fe66872015-03-30 17:20:39 -070028 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080032 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010033 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070034 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070035 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070036)
37
Colin Cross463a90e2015-06-17 14:20:06 -070038func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010039 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000040
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080041 RegisterJavaSdkMemberTypes()
42}
43
Paul Duffin535e0a12021-03-30 23:34:32 +010044func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080045 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
46
47 ctx.RegisterModuleType("java_library", LibraryFactory)
48 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
49 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
50 ctx.RegisterModuleType("java_binary", BinaryFactory)
51 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
52 ctx.RegisterModuleType("java_test", TestFactory)
53 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
54 ctx.RegisterModuleType("java_test_host", TestHostFactory)
55 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
56 ctx.RegisterModuleType("java_import", ImportFactory)
57 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
58 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
59 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
60 ctx.RegisterModuleType("dex_import", DexImportFactory)
61
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +010062 // This mutator registers dependencies on dex2oat for modules that should be
63 // dexpreopted. This is done late when the final variants have been
64 // established, to not get the dependencies split into the wrong variants and
65 // to support the checks in dexpreoptDisabled().
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080066 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
67 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
68 })
69
70 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
71 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
72}
73
74func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000075 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000076 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010077 android.RegisterSdkMemberType(javaLibsSdkMemberType)
78 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
Jiakai Zhangea180332021-09-26 08:58:02 +000079 android.RegisterSdkMemberType(javaSystemserverLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010080 android.RegisterSdkMemberType(javaTestSdkMemberType)
81}
82
83var (
84 // Supports adding java header libraries to module_exports and sdk.
85 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
86 android.SdkMemberTypeBase{
87 PropertyName: "java_header_libs",
88 SupportsSdk: true,
89 },
90 func(_ android.SdkMemberContext, j *Library) android.Path {
91 headerJars := j.HeaderJars()
92 if len(headerJars) != 1 {
93 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
94 }
95
96 return headerJars[0]
97 },
98 sdkSnapshotFilePathForJar,
99 copyEverythingToSnapshot,
100 }
Paul Duffin255f18e2019-12-13 11:22:16 +0000101
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000102 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +0100103 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000104 implementationJars := j.ImplementationAndResourcesJars()
105 if len(implementationJars) != 1 {
106 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
107 }
108 return implementationJars[0]
109 }
110
Paul Duffin2da04242021-04-23 19:43:28 +0100111 // Supports adding java implementation libraries to module_exports but not sdk.
112 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000113 android.SdkMemberTypeBase{
114 PropertyName: "java_libs",
115 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000116 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000117 sdkSnapshotFilePathForJar,
118 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100119 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000120
Paul Duffin2da04242021-04-23 19:43:28 +0100121 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000122 //
123 // The build has some implicit dependencies (via the boot jars configuration) on a number of
124 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
125 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
126 // used outside those mainline modules.
127 //
128 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
129 // either java_libs, or java_header_libs would end up exporting more information than was strictly
130 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
131 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100132 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000133 android.SdkMemberTypeBase{
134 PropertyName: "java_boot_libs",
135 SupportsSdk: true,
136 },
Paul Duffin5c211452021-07-15 12:42:44 +0100137 func(ctx android.SdkMemberContext, j *Library) android.Path {
138 // Java boot libs are only provided in the SDK to provide access to their dex implementation
139 // jar for use by dexpreopting and boot jars package check. They do not need to provide an
140 // actual implementation jar but the java_import will need a file that exists so just copy an
141 // empty file. Any attempt to use that file as a jar will cause a build error.
142 return ctx.SnapshotBuilder().EmptyFile()
143 },
144 func(osPrefix, name string) string {
145 // Create a special name for the implementation jar to try and provide some useful information
146 // to a developer that attempts to compile against this.
147 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
148 return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
149 },
Paul Duffindb170e42020-12-08 17:48:25 +0000150 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100151 }
Paul Duffindb170e42020-12-08 17:48:25 +0000152
Jiakai Zhangea180332021-09-26 08:58:02 +0000153 // Supports adding java systemserver libraries to module_exports and sdk.
154 //
155 // The build has some implicit dependencies (via the systemserver jars configuration) on a number
156 // of modules that are part of the java systemserver classpath and which are provided by mainline
157 // modules but which are not otherwise used outside those mainline modules.
158 //
159 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
160 // either java_libs, or java_header_libs would end up exporting more information than was strictly
161 // necessary. The java_systemserver_libs property to allow those modules to be exported as part of
162 // the sdk/module_exports without exposing any unnecessary information.
163 javaSystemserverLibsSdkMemberType = &librarySdkMemberType{
164 android.SdkMemberTypeBase{
165 PropertyName: "java_systemserver_libs",
166 SupportsSdk: true,
167 },
168 func(ctx android.SdkMemberContext, j *Library) android.Path {
169 // Java systemserver libs are only provided in the SDK to provide access to their dex
170 // implementation jar for use by dexpreopting. They do not need to provide an actual
171 // implementation jar but the java_import will need a file that exists so just copy an empty
172 // file. Any attempt to use that file as a jar will cause a build error.
173 return ctx.SnapshotBuilder().EmptyFile()
174 },
175 func(osPrefix, name string) string {
176 // Create a special name for the implementation jar to try and provide some useful information
177 // to a developer that attempts to compile against this.
178 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
179 return filepath.Join(osPrefix, "java_systemserver_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
180 },
181 onlyCopyJarToSnapshot,
182 }
183
Paul Duffin2da04242021-04-23 19:43:28 +0100184 // Supports adding java test libraries to module_exports but not sdk.
185 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000186 SdkMemberTypeBase: android.SdkMemberTypeBase{
187 PropertyName: "java_tests",
188 },
Paul Duffin2da04242021-04-23 19:43:28 +0100189 }
190)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900191
Colin Crossdcf71b22021-02-01 13:59:03 -0800192// JavaInfo contains information about a java module for use by modules that depend on it.
193type JavaInfo struct {
194 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
195 // against this module. If empty, ImplementationJars should be used instead.
196 HeaderJars android.Paths
197
198 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
199 // in the module as well as any resources included in the module.
200 ImplementationAndResourcesJars android.Paths
201
202 // ImplementationJars is a list of jars that contain the implementations of classes in the
203 //module.
204 ImplementationJars android.Paths
205
206 // ResourceJars is a list of jars that contain the resources included in the module.
207 ResourceJars android.Paths
208
209 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
210 // depending on this module.
211 AidlIncludeDirs android.Paths
212
213 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
214 // module.
215 SrcJarArgs []string
216
217 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
218 SrcJarDeps android.Paths
219
220 // ExportedPlugins is a list of paths that should be used as annotation processors for any
221 // module that depends on this module.
222 ExportedPlugins android.Paths
223
224 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
225 // any module that depends on this module.
226 ExportedPluginClasses []string
227
228 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
229 // requiring disbling turbine for any modules that depend on it.
230 ExportedPluginDisableTurbine bool
231
232 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
233 // instrumented by jacoco.
234 JacocoReportClassesFile android.Path
235}
236
237var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
238
Colin Cross75ce9ec2021-02-26 16:20:32 -0800239// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
240// the sysprop implementation library.
241type SyspropPublicStubInfo struct {
242 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
243 // the sysprop implementation library.
244 JavaInfo JavaInfo
245}
246
247var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
248
Paul Duffin44b481b2020-06-17 16:59:43 +0100249// Methods that need to be implemented for a module that is added to apex java_libs property.
250type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700251 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100252 ImplementationAndResourcesJars() android.Paths
253}
254
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100255// Provides build path and install path to DEX jars.
256type UsesLibraryDependency interface {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100257 DexJarBuildPath() OptionalDexJarPath
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100258 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000259 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100260}
261
Jaewoong Jung26342642021-03-17 15:56:23 -0700262// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800263type xref interface {
264 XrefJavaFiles() android.Paths
265}
266
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800267func (j *Module) XrefJavaFiles() android.Paths {
268 return j.kytheFiles
269}
270
Colin Crossbe1da472017-07-07 15:59:46 -0700271type dependencyTag struct {
272 blueprint.BaseDependencyTag
273 name string
Colin Cross65cb3142021-12-10 23:05:02 +0000274
275 // True if the dependency is relinked at runtime.
276 runtimeLinked bool
Colin Cross2fe66872015-03-30 17:20:39 -0700277}
278
Colin Crosse9fe2942020-11-10 18:12:15 -0800279// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
280// dependency to be installed when the parent module is installed.
281type installDependencyTag struct {
282 blueprint.BaseDependencyTag
283 android.InstallAlwaysNeededDependencyTag
284 name string
285}
286
Colin Cross65cb3142021-12-10 23:05:02 +0000287func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
288 if d.runtimeLinked {
289 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
290 }
291 return nil
292}
293
294var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
295
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100296type usesLibraryDependencyTag struct {
297 dependencyTag
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100298
299 // SDK version in which the library appared as a standalone library.
300 sdkVersion int
301
302 // If the dependency is optional or required.
303 optional bool
304
305 // Whether this is an implicit dependency inferred by Soong, or an explicit one added via
306 // `uses_libs`/`optional_uses_libs` properties.
307 implicit bool
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100308}
309
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100310func makeUsesLibraryDependencyTag(sdkVersion int, optional bool, implicit bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100311 return usesLibraryDependencyTag{
Colin Cross65cb3142021-12-10 23:05:02 +0000312 dependencyTag: dependencyTag{
313 name: fmt.Sprintf("uses-library-%d", sdkVersion),
314 runtimeLinked: true,
315 },
316 sdkVersion: sdkVersion,
317 optional: optional,
318 implicit: implicit,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100319 }
320}
321
Jiyong Park8be103b2019-11-08 15:53:48 +0900322func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700323 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900324}
325
Colin Crossbe1da472017-07-07 15:59:46 -0700326var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800327 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
Sam Delmericob3342ce2022-01-20 21:10:28 +0000328 dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800329 staticLibTag = dependencyTag{name: "staticlib"}
Colin Cross65cb3142021-12-10 23:05:02 +0000330 libTag = dependencyTag{name: "javalib", runtimeLinked: true}
331 java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800332 pluginTag = dependencyTag{name: "plugin"}
333 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
334 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross65cb3142021-12-10 23:05:02 +0000335 bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true}
336 systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800337 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross65cb3142021-12-10 23:05:02 +0000338 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true}
339 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700340 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800341 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
342 certificateTag = dependencyTag{name: "certificate"}
343 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
344 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
Colin Cross65cb3142021-12-10 23:05:02 +0000345 jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800346 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
347 jniInstallTag = installDependencyTag{name: "jni install"}
348 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700349)
Colin Cross2fe66872015-03-30 17:20:39 -0700350
Jiyong Park83dc74b2020-01-14 18:38:44 +0900351func IsLibDepTag(depTag blueprint.DependencyTag) bool {
352 return depTag == libTag
353}
354
355func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
356 return depTag == staticLibTag
357}
358
Colin Crossfc3674a2017-09-18 17:41:52 -0700359type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100360 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700361
Colin Cross6cef4812019-10-17 14:23:50 -0700362 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
363 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100364
365 // The default system modules to use. Will be an empty string if no system
366 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700367 systemModules string
368
Pete Gilline3d44b22020-06-29 11:28:51 +0100369 // The modules that will be added to the classpath regardless of the Java language level targeted
370 classpath []string
371
Colin Cross6cef4812019-10-17 14:23:50 -0700372 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100373 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700374 java9Classpath []string
375
Colin Crossa97c5d32018-03-28 14:58:31 -0700376 frameworkResModule string
377
Colin Cross86a60ae2018-05-29 14:44:55 -0700378 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700379 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100380
381 noStandardLibs, noFrameworksLibs bool
382}
383
384func (s sdkDep) hasStandardLibs() bool {
385 return !s.noStandardLibs
386}
387
388func (s sdkDep) hasFrameworkLibs() bool {
389 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700390}
391
Colin Crossa4f08812018-10-02 22:03:40 -0700392type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700393 name string
394 path android.Path
395 target android.Target
396 coverageFile android.OptionalPath
397 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700398}
399
Jiyong Parkf1691d22021-03-29 20:11:58 +0900400func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700401 sdkDep := decodeSdkDep(ctx, sdkContext)
402 if sdkDep.useModule {
403 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
404 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
405 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
406 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
407 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
408 }
409 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
410 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
411 }
412 }
413 if sdkDep.systemModules != "" {
414 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
415 }
416}
417
Colin Cross32f676a2017-09-06 13:41:06 -0700418type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800419 classpath classpath
420 java9Classpath classpath
421 bootClasspath classpath
422 processorPath classpath
423 errorProneProcessorPath classpath
424 processorClasses []string
425 staticJars android.Paths
426 staticHeaderJars android.Paths
427 staticResourceJars android.Paths
428 aidlIncludeDirs android.Paths
429 srcs android.Paths
430 srcJars android.Paths
431 systemModules *systemModules
432 aidlPreprocess android.OptionalPath
433 kotlinStdlib android.Paths
434 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700435 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800436
437 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700438}
Colin Cross2fe66872015-03-30 17:20:39 -0700439
Colin Cross54250902017-12-05 09:28:08 -0800440func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
441 for _, f := range dep.Srcs() {
442 if f.Ext() != ".jar" {
443 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
444 ctx.OtherModuleName(dep.(blueprint.Module)))
445 }
446 }
447}
448
Jiyong Parkf1691d22021-03-29 20:11:58 +0900449func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700450 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700451 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700452 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900453 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Sorin Basca8d3e0bb2022-01-20 15:21:51 +0000454 } else {
Sorin Basca18ecf612022-01-23 09:01:07 +0000455 return JAVA_VERSION_11
Nan Zhang357466b2018-04-17 17:38:36 -0700456 }
Nan Zhang357466b2018-04-17 17:38:36 -0700457}
458
Colin Cross1e743852019-10-28 11:37:20 -0700459type javaVersion int
460
461const (
462 JAVA_VERSION_UNSUPPORTED = 0
463 JAVA_VERSION_6 = 6
464 JAVA_VERSION_7 = 7
465 JAVA_VERSION_8 = 8
466 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000467 JAVA_VERSION_11 = 11
Colin Cross1e743852019-10-28 11:37:20 -0700468)
469
470func (v javaVersion) String() string {
471 switch v {
472 case JAVA_VERSION_6:
473 return "1.6"
474 case JAVA_VERSION_7:
475 return "1.7"
476 case JAVA_VERSION_8:
477 return "1.8"
478 case JAVA_VERSION_9:
479 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000480 case JAVA_VERSION_11:
481 return "11"
Colin Cross1e743852019-10-28 11:37:20 -0700482 default:
483 return "unsupported"
484 }
485}
486
487// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
488func (v javaVersion) usesJavaModules() bool {
489 return v >= 9
490}
491
492func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100493 switch javaVersion {
494 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700495 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100496 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700497 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100498 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700499 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100500 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700501 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000502 case "11":
503 return JAVA_VERSION_11
504 case "10":
505 ctx.PropertyErrorf("java_version", "Java language levels 10 is not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700506 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100507 default:
508 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700509 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100510 }
511}
512
Colin Cross2fe66872015-03-30 17:20:39 -0700513//
514// Java libraries (.jar file)
515//
516
Colin Crossf506d872017-07-19 15:53:04 -0700517type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700518 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700519
520 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700521}
522
Jiyong Park45bf82e2020-12-15 22:29:02 +0900523var _ android.ApexModule = (*Library)(nil)
524
satayevd604b212021-07-21 14:23:52 +0100525// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100526type PermittedPackagesForUpdatableBootJars interface {
527 PermittedPackagesForUpdatableBootJars() []string
528}
529
530var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
531
532func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
533 return j.properties.Permitted_packages
534}
535
Colin Cross42be7612019-02-21 18:12:14 -0800536func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000537 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700538 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000539 return true
540 }
541
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000542 // Store uncompressed (and do not strip) dex files from boot class path jars.
543 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
544 return true
545 }
546
547 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000548 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000549 return true
550 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800551 if ctx.Config().UncompressPrivAppDex() &&
552 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
553 return true
554 }
555
Colin Cross2fc72f62018-12-21 12:59:54 -0800556 return false
557}
558
Jiakai Zhang22450f22021-10-11 03:05:20 +0000559// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
560func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
561 if dexer.dexProperties.Uncompress_dex == nil {
562 // If the value was not force-set by the user, use reasonable default based on the module.
563 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
564 }
565}
566
Colin Crossf506d872017-07-19 15:53:04 -0700567func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900568 j.sdkVersion = j.SdkVersion(ctx)
569 j.minSdkVersion = j.MinSdkVersion(ctx)
satayev0a420e72021-11-29 17:25:52 +0000570 j.maxSdkVersion = j.MaxSdkVersion(ctx)
Jiyong Park92315372021-04-02 08:45:46 +0900571
Colin Cross56a83212020-09-15 18:30:11 -0700572 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
573 if !apexInfo.IsForPlatform() {
574 j.hideApexVariantFromMake = true
575 }
576
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100577 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000578 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
579 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800580 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000581 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700582 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100583 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700584 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700585
bralee1fbf4402020-05-21 10:11:59 +0800586 // Collect the module directory for IDE info in java/jdeps.go.
587 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
588
Colin Cross56a83212020-09-15 18:30:11 -0700589 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900590 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700591 var extraInstallDeps android.Paths
592 if j.InstallMixin != nil {
593 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
594 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700595 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
596 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800597 j.hostdexInstallFile = ctx.InstallFile(
598 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700599 j.Stem()+"-hostdex.jar", j.outputFile)
600 }
601 var installDir android.InstallPath
602 if ctx.InstallInTestcases() {
603 var archDir string
604 if !ctx.Host() {
605 archDir = ctx.DeviceConfig().DeviceArch()
606 }
607 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
608 } else {
609 installDir = android.PathForModuleInstall(ctx, "framework")
610 }
611 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700612 }
Colin Crossb7a63242015-04-16 14:09:14 -0700613}
614
Colin Crossf506d872017-07-19 15:53:04 -0700615func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700616 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100617 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700618}
619
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000620const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000621 aidlIncludeDir = "aidl"
622 javaDir = "java"
623 jarFileSuffix = ".jar"
624 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000625)
626
Paul Duffina0dbf432019-12-05 11:25:53 +0000627// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000628func sdkSnapshotFilePathForJar(osPrefix, name string) string {
629 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000630}
631
Paul Duffina04c1072020-03-02 10:16:35 +0000632func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
633 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000634}
635
Paul Duffin13879572019-11-28 14:31:38 +0000636type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000637 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000638
639 // Function to retrieve the appropriate output jar (implementation or header) from
640 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000641 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
642
643 // Function to compute the snapshot relative path to which the named library's
644 // jar should be copied.
645 snapshotPathGetter func(osPrefix, name string) string
646
647 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
648 // files like aidl files should also be copied.
649 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000650}
651
Paul Duffindb170e42020-12-08 17:48:25 +0000652const (
653 onlyCopyJarToSnapshot = true
654 copyEverythingToSnapshot = false
655)
656
Paul Duffin296701e2021-07-14 10:29:36 +0100657func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
658 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000659}
660
661func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
662 _, ok := module.(*Library)
663 return ok
664}
665
Paul Duffin3a4eb502020-03-19 16:11:18 +0000666func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
667 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000668}
Paul Duffina0dbf432019-12-05 11:25:53 +0000669
Paul Duffin14eb4672020-03-02 11:33:02 +0000670func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000671 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000672}
673
674type librarySdkMemberProperties struct {
675 android.SdkMemberPropertiesBase
676
Paul Duffin864e1b42020-05-06 10:23:19 +0100677 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000678 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100679
680 // The list of permitted packages that need to be passed to the prebuilts as they are used to
681 // create the updatable-bcp-packages.txt file.
682 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000683}
684
Paul Duffin3a4eb502020-03-19 16:11:18 +0000685func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000686 j := variant.(*Library)
687
Paul Duffindb170e42020-12-08 17:48:25 +0000688 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
689
Paul Duffina551a1c2020-03-17 21:04:24 +0000690 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100691
692 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000693}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000694
Paul Duffin3a4eb502020-03-19 16:11:18 +0000695func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000696 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000697
Paul Duffindb170e42020-12-08 17:48:25 +0000698 memberType := ctx.MemberType().(*librarySdkMemberType)
699
Paul Duffina551a1c2020-03-17 21:04:24 +0000700 exportedJar := p.JarToExport
701 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000702 // Delegate the creation of the snapshot relative path to the member type.
703 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
704
705 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000706 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
707
Paul Duffina551a1c2020-03-17 21:04:24 +0000708 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
709 }
710
Paul Duffin869de142021-07-15 14:14:41 +0100711 if len(p.PermittedPackages) > 0 {
712 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
713 }
714
Paul Duffindb170e42020-12-08 17:48:25 +0000715 // Do not copy anything else to the snapshot.
716 if memberType.onlyCopyJarToSnapshot {
717 return
718 }
719
Paul Duffina551a1c2020-03-17 21:04:24 +0000720 aidlIncludeDirs := p.AidlIncludeDirs
721 if len(aidlIncludeDirs) != 0 {
722 sdkModuleContext := ctx.SdkModuleContext()
723 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000724 // TODO(jiyong): copy parcelable declarations only
725 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
726 for _, file := range aidlFiles {
727 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
728 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000729 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000730
Paul Duffina551a1c2020-03-17 21:04:24 +0000731 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000732 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000733}
734
Colin Cross1b16b0e2019-02-12 14:41:32 -0800735// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
736//
737// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
738// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
739// as a `static_libs` dependency of another module.
740//
741// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
742// a device.
743//
744// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
745// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700746func LibraryFactory() android.Module {
747 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700748
Colin Crossce6734e2020-06-15 16:09:53 -0700749 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700750
Paul Duffin71b33cc2021-06-23 11:39:47 +0100751 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100752
Jiyong Park7f7766d2019-07-25 22:02:35 +0900753 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900754 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800755 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900756 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700757 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700758}
759
Colin Cross1b16b0e2019-02-12 14:41:32 -0800760// java_library_static is an obsolete alias for java_library.
761func LibraryStaticFactory() android.Module {
762 return LibraryFactory()
763}
764
765// java_library_host builds and links sources into a `.jar` file for the host.
766//
767// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
768// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700769func LibraryHostFactory() android.Module {
770 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700771
Colin Crossce6734e2020-06-15 16:09:53 -0700772 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700773
Colin Cross9ae1b922018-06-26 17:59:05 -0700774 module.Module.properties.Installable = proptools.BoolPtr(true)
775
Jiyong Park7f7766d2019-07-25 22:02:35 +0900776 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100777 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800778 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900779 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700780 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700781}
782
783//
Colin Crossb628ea52018-08-14 16:42:33 -0700784// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700785//
786
Dan Shi95d19422020-08-15 12:24:26 -0700787// Test option struct.
788type TestOptions struct {
789 // a list of extra test configuration files that should be installed with the module.
790 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800791
792 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
793 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700794}
795
Colin Cross05638fc2018-04-09 18:40:24 -0700796type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700797 // list of compatibility suites (for example "cts", "vts") that the module should be
798 // installed into.
799 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700800
801 // the name of the test configuration (for example "AndroidTest.xml") that should be
802 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800803 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700804
Jack He33338892018-09-19 02:21:28 -0700805 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
806 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800807 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700808
Colin Crossd96ca352018-08-10 16:06:24 -0700809 // list of files or filegroup modules that provide data that should be installed alongside
810 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900811 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700812
813 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
814 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
815 // explicitly.
816 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800817
818 // Add parameterized mainline modules to auto generated test config. The options will be
819 // handled by TradeFed to do downloading and installing the specified modules on the device.
820 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700821
822 // Test options.
823 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800824
825 // Names of modules containing JNI libraries that should be installed alongside the test.
826 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700827
828 // Install the test into a folder named for the module in all test suites.
829 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700830}
831
Liz Kammerdd849a82020-06-12 16:38:45 -0700832type hostTestProperties struct {
833 // list of native binary modules that should be installed alongside the test
834 Data_native_bins []string `android:"arch_variant"`
Sam Delmericob3342ce2022-01-20 21:10:28 +0000835
836 // list of device binary modules that should be installed alongside the test
837 Data_device_bins []string `android:"arch_variant"`
Liz Kammerdd849a82020-06-12 16:38:45 -0700838}
839
Paul Duffin42df1442019-03-20 12:45:53 +0000840type testHelperLibraryProperties struct {
841 // list of compatibility suites (for example "cts", "vts") that the module should be
842 // installed into.
843 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700844
845 // Install the test into a folder named for the module in all test suites.
846 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000847}
848
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000849type prebuiltTestProperties struct {
850 // list of compatibility suites (for example "cts", "vts") that the module should be
851 // installed into.
852 Test_suites []string `android:"arch_variant"`
853
854 // the name of the test configuration (for example "AndroidTest.xml") that should be
855 // installed with the module.
856 Test_config *string `android:"path,arch_variant"`
857}
858
Colin Cross05638fc2018-04-09 18:40:24 -0700859type Test struct {
860 Library
861
862 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700863
Dan Shi95d19422020-08-15 12:24:26 -0700864 testConfig android.Path
865 extraTestConfigs android.Paths
866 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700867}
868
Liz Kammerdd849a82020-06-12 16:38:45 -0700869type TestHost struct {
870 Test
871
872 testHostProperties hostTestProperties
873}
874
Paul Duffin42df1442019-03-20 12:45:53 +0000875type TestHelperLibrary struct {
876 Library
877
878 testHelperLibraryProperties testHelperLibraryProperties
879}
880
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000881type JavaTestImport struct {
882 Import
883
884 prebuiltTestProperties prebuiltTestProperties
885
886 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700887 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000888}
889
Colin Cross24cc4be62021-11-03 14:09:41 -0700890func (j *Test) InstallInTestcases() bool {
891 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
892 // testcases by base_rules.mk.
893 return !j.Host()
894}
895
896func (j *TestHelperLibrary) InstallInTestcases() bool {
897 return true
898}
899
900func (j *JavaTestImport) InstallInTestcases() bool {
901 return true
902}
903
Liz Kammerdd849a82020-06-12 16:38:45 -0700904func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
905 if len(j.testHostProperties.Data_native_bins) > 0 {
906 for _, target := range ctx.MultiTargets() {
907 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
908 }
909 }
910
Sam Delmericob3342ce2022-01-20 21:10:28 +0000911 if len(j.testHostProperties.Data_device_bins) > 0 {
912 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
913 ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins...)
914 }
915
Colin Crossf8d9c492021-01-26 11:01:43 -0800916 if len(j.testProperties.Jni_libs) > 0 {
917 for _, target := range ctx.MultiTargets() {
918 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
919 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
920 }
921 }
922
Liz Kammerdd849a82020-06-12 16:38:45 -0700923 j.deps(ctx)
924}
925
Yuexi Ma627263f2021-03-04 13:47:56 -0800926func (j *TestHost) AddExtraResource(p android.Path) {
927 j.extraResources = append(j.extraResources, p)
928}
929
Sam Delmericob3342ce2022-01-20 21:10:28 +0000930func (j *TestHost) GenerateAndroidBuildActions(ctx android.ModuleContext) {
931 var configs []tradefed.Config
932 if len(j.testHostProperties.Data_device_bins) > 0 {
933 // add Tradefed configuration to push device bins to device for testing
934 remoteDir := filepath.Join("/data/local/tests/unrestricted/", j.Name())
935 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
936 for _, bin := range j.testHostProperties.Data_device_bins {
937 fullPath := filepath.Join(remoteDir, bin)
938 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: fullPath})
939 }
940 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
941 }
942
943 j.Test.generateAndroidBuildActionsWithConfig(ctx, configs)
944}
945
Colin Cross303e21f2018-08-07 16:49:25 -0700946func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Sam Delmericob3342ce2022-01-20 21:10:28 +0000947 j.generateAndroidBuildActionsWithConfig(ctx, nil)
948}
949
950func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, configs []tradefed.Config) {
Julien Desprezb2166612021-03-05 18:08:36 +0000951 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
952 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700953 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000954 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
955 }
Sam Delmericob3342ce2022-01-20 21:10:28 +0000956
Dan Shi6ffaaa82019-09-26 11:41:36 -0700957 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Sam Delmericob3342ce2022-01-20 21:10:28 +0000958 j.testProperties.Test_suites, configs, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700959
Colin Cross8a497952019-03-05 22:25:09 -0800960 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700961
Dan Shi95d19422020-08-15 12:24:26 -0700962 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
963
Liz Kammerdd849a82020-06-12 16:38:45 -0700964 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
965 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
966 })
967
Sam Delmericob3342ce2022-01-20 21:10:28 +0000968 ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) {
969 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
970 })
971
Colin Crossf8d9c492021-01-26 11:01:43 -0800972 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
973 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
974 if sharedLibInfo.SharedLibrary != nil {
975 // Copy to an intermediate output directory to append "lib[64]" to the path,
976 // so that it's compatible with the default rpath values.
977 var relPath string
978 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
979 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
980 } else {
981 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
982 }
983 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
984 ctx.Build(pctx, android.BuildParams{
985 Rule: android.Cp,
986 Input: sharedLibInfo.SharedLibrary,
987 Output: relocatedLib,
988 })
989 j.data = append(j.data, relocatedLib)
990 } else {
991 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
992 }
993 })
994
Colin Cross303e21f2018-08-07 16:49:25 -0700995 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700996}
997
Paul Duffin42df1442019-03-20 12:45:53 +0000998func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
999 j.Library.GenerateAndroidBuildActions(ctx)
1000}
1001
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001002func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1003 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Sam Delmericob3342ce2022-01-20 21:10:28 +00001004 j.prebuiltTestProperties.Test_suites, nil, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001005
1006 j.Import.GenerateAndroidBuildActions(ctx)
1007}
1008
1009type testSdkMemberType struct {
1010 android.SdkMemberTypeBase
1011}
1012
Paul Duffin296701e2021-07-14 10:29:36 +01001013func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
1014 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001015}
1016
1017func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
1018 _, ok := module.(*Test)
1019 return ok
1020}
1021
Paul Duffin3a4eb502020-03-19 16:11:18 +00001022func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1023 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00001024}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001025
Paul Duffin14eb4672020-03-02 11:33:02 +00001026func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1027 return &testSdkMemberProperties{}
1028}
1029
1030type testSdkMemberProperties struct {
1031 android.SdkMemberPropertiesBase
1032
Paul Duffina551a1c2020-03-17 21:04:24 +00001033 JarToExport android.Path
1034 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00001035}
1036
Paul Duffin3a4eb502020-03-19 16:11:18 +00001037func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00001038 test := variant.(*Test)
1039
1040 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001041 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00001042 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001043 }
1044
Paul Duffina551a1c2020-03-17 21:04:24 +00001045 p.JarToExport = implementationJars[0]
1046 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00001047}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001048
Paul Duffin3a4eb502020-03-19 16:11:18 +00001049func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001050 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001051
Paul Duffina551a1c2020-03-17 21:04:24 +00001052 exportedJar := p.JarToExport
1053 if exportedJar != nil {
1054 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
1055 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001056
1057 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001058 }
1059
1060 testConfig := p.TestConfig
1061 if testConfig != nil {
1062 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1063 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001064 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1065 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001066}
1067
Colin Cross1b16b0e2019-02-12 14:41:32 -08001068// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1069// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1070//
1071// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1072// compiled against the device bootclasspath.
1073//
1074// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1075// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001076func TestFactory() android.Module {
1077 module := &Test{}
1078
Colin Crossce6734e2020-06-15 16:09:53 -07001079 module.addHostAndDeviceProperties()
1080 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001081
Colin Cross9ae1b922018-06-26 17:59:05 -07001082 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001083 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001084 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001085
Paul Duffinb6b89a42021-05-06 16:33:43 +01001086 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001087 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001088 return module
1089}
1090
Paul Duffin42df1442019-03-20 12:45:53 +00001091// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1092func TestHelperLibraryFactory() android.Module {
1093 module := &TestHelperLibrary{}
1094
Colin Crossce6734e2020-06-15 16:09:53 -07001095 module.addHostAndDeviceProperties()
1096 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001097
Colin Cross9a4abed2019-04-24 13:19:28 -07001098 module.Module.properties.Installable = proptools.BoolPtr(true)
1099 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001100 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001101
Paul Duffin42df1442019-03-20 12:45:53 +00001102 InitJavaModule(module, android.HostAndDeviceSupported)
1103 return module
1104}
1105
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001106// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1107// and makes sure that it is added to the appropriate test suite.
1108//
1109// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1110// compiled against an Android classpath.
1111//
1112// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1113// for host modules.
1114func JavaTestImportFactory() android.Module {
1115 module := &JavaTestImport{}
1116
1117 module.AddProperties(
1118 &module.Import.properties,
1119 &module.prebuiltTestProperties)
1120
1121 module.Import.properties.Installable = proptools.BoolPtr(true)
1122
1123 android.InitPrebuiltModule(module, &module.properties.Jars)
1124 android.InitApexModule(module)
1125 android.InitSdkAwareModule(module)
1126 InitJavaModule(module, android.HostAndDeviceSupported)
1127 return module
1128}
1129
Colin Cross1b16b0e2019-02-12 14:41:32 -08001130// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1131// allow running the test with `atest` or a `TEST_MAPPING` file.
1132//
1133// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1134// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001135func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001136 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001137
Colin Crossce6734e2020-06-15 16:09:53 -07001138 module.addHostProperties()
1139 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001140 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001141
Yuexi Ma627263f2021-03-04 13:47:56 -08001142 InitTestHost(
1143 module,
1144 proptools.BoolPtr(true),
1145 nil,
1146 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001147
Liz Kammerdd849a82020-06-12 16:38:45 -07001148 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001149
Colin Cross05638fc2018-04-09 18:40:24 -07001150 return module
1151}
1152
Yuexi Ma627263f2021-03-04 13:47:56 -08001153func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1154 th.properties.Installable = installable
1155 th.testProperties.Auto_gen_config = autoGenConfig
1156 th.testProperties.Test_suites = testSuites
1157}
1158
Colin Cross05638fc2018-04-09 18:40:24 -07001159//
Colin Cross2fe66872015-03-30 17:20:39 -07001160// Java Binaries (.jar file plus wrapper script)
1161//
1162
Colin Crossf506d872017-07-19 15:53:04 -07001163type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001164 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001165 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001166
1167 // Name of the class containing main to be inserted into the manifest as Main-Class.
1168 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001169
1170 // Names of modules containing JNI libraries that should be installed alongside the host
1171 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001172 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001173}
1174
Colin Crossf506d872017-07-19 15:53:04 -07001175type Binary struct {
1176 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001177
Colin Crossf506d872017-07-19 15:53:04 -07001178 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001179
Colin Cross6b4a32d2017-12-05 13:42:45 -08001180 isWrapperVariant bool
1181
Colin Crossc3315992017-12-08 19:12:36 -08001182 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001183 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001184}
1185
Alex Light24237172017-10-26 09:46:21 -07001186func (j *Binary) HostToolPath() android.OptionalPath {
1187 return android.OptionalPathForPath(j.binaryFile)
1188}
1189
Colin Crossf506d872017-07-19 15:53:04 -07001190func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001191 if ctx.Arch().ArchType == android.Common {
1192 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001193 if j.binaryProperties.Main_class != nil {
1194 if j.properties.Manifest != nil {
1195 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1196 }
1197 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1198 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1199 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1200 }
1201
Colin Cross6b4a32d2017-12-05 13:42:45 -08001202 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001203 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001204 // Handle the binary wrapper
1205 j.isWrapperVariant = true
1206
Colin Cross366938f2017-12-11 16:29:02 -08001207 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001208 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001209 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001210 if ctx.Windows() {
1211 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1212 }
1213
Colin Cross6b4a32d2017-12-05 13:42:45 -08001214 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1215 }
1216
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001217 ext := ""
1218 if ctx.Windows() {
1219 ext = ".bat"
1220 }
1221
Colin Crossc179ea62020-10-09 10:54:15 -07001222 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001223 // of the wrapper variant, which will include the common variant's jar file and any JNI
1224 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001225 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001226 ctx.ModuleName()+ext, j.wrapperFile)
1227 }
Colin Cross2fe66872015-03-30 17:20:39 -07001228}
1229
Colin Crossf506d872017-07-19 15:53:04 -07001230func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001231 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001232 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001233 }
1234 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001235 // These dependencies ensure the host installation rules will install the jar file and
1236 // the jni libraries when the wrapper is installed.
1237 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1238 ctx.AddVariationDependencies(
1239 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1240 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001241 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001242}
1243
Colin Cross1b16b0e2019-02-12 14:41:32 -08001244// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1245// as well.
1246//
1247// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1248// compiled against the device bootclasspath.
1249//
1250// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1251// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001252func BinaryFactory() android.Module {
1253 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001254
Colin Crossce6734e2020-06-15 16:09:53 -07001255 module.addHostAndDeviceProperties()
1256 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001257
Colin Cross9ae1b922018-06-26 17:59:05 -07001258 module.Module.properties.Installable = proptools.BoolPtr(true)
1259
Colin Cross6b4a32d2017-12-05 13:42:45 -08001260 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1261 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001262 android.InitBazelModule(module)
1263
Colin Cross36242852017-06-23 15:06:31 -07001264 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001265}
1266
Colin Cross1b16b0e2019-02-12 14:41:32 -08001267// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1268//
1269// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1270// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001271func BinaryHostFactory() android.Module {
1272 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001273
Colin Crossce6734e2020-06-15 16:09:53 -07001274 module.addHostProperties()
1275 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001276
Colin Cross9ae1b922018-06-26 17:59:05 -07001277 module.Module.properties.Installable = proptools.BoolPtr(true)
1278
Colin Cross6b4a32d2017-12-05 13:42:45 -08001279 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1280 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001281 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001282 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001283}
1284
1285//
1286// Java prebuilts
1287//
1288
Colin Cross74d73e22017-08-02 11:05:49 -07001289type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001290 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001291
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001292 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1293 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001294 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001295
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001296 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1297 // specified.
1298 Min_sdk_version *string
1299
Colin Cross535e2cf2017-10-20 17:57:49 -07001300 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001301
Paul Duffin869de142021-07-15 14:14:41 +01001302 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001303 Permitted_packages []string
1304
Jiyong Park1be96912018-05-28 18:02:19 +09001305 // List of shared java libs that this module has dependencies to
1306 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001307
1308 // List of files to remove from the jar file(s)
1309 Exclude_files []string
1310
1311 // List of directories to remove from the jar file(s)
1312 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001313
1314 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001315 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001316
1317 // set the name of the output
1318 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001319
1320 Aidl struct {
1321 // directories that should be added as include directories for any aidl sources of modules
1322 // that depend on this module, as well as to aidl for this module.
1323 Export_include_dirs []string
1324 }
Colin Cross74d73e22017-08-02 11:05:49 -07001325}
1326
1327type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001328 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001329 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001330 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001331 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001332 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001333
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001334 // Functionality common to Module and Import.
1335 embeddableInModuleAndImport
1336
Liz Kammerd6c31d22020-08-05 15:40:41 -07001337 hiddenAPI
1338 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001339 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001340
Colin Cross74d73e22017-08-02 11:05:49 -07001341 properties ImportProperties
1342
Liz Kammerd6c31d22020-08-05 15:40:41 -07001343 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001344 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001345 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001346
Colin Cross0a6e0072017-08-30 14:24:55 -07001347 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001348 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001349 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001350
1351 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001352
1353 sdkVersion android.SdkSpec
1354 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001355}
1356
Paul Duffin630b11e2021-07-15 13:35:26 +01001357var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1358
1359func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1360 return j.properties.Permitted_packages
1361}
1362
Jiyong Park92315372021-04-02 08:45:46 +09001363func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1364 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001365}
1366
Jiyong Parkf1691d22021-03-29 20:11:58 +09001367func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001368 return "none"
1369}
1370
Jiyong Park92315372021-04-02 08:45:46 +09001371func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001372 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001373 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001374 }
Jiyong Park92315372021-04-02 08:45:46 +09001375 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001376}
1377
Jiyong Park92315372021-04-02 08:45:46 +09001378func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1379 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001380}
1381
Colin Cross74d73e22017-08-02 11:05:49 -07001382func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001383 return &j.prebuilt
1384}
1385
Colin Cross74d73e22017-08-02 11:05:49 -07001386func (j *Import) PrebuiltSrcs() []string {
1387 return j.properties.Jars
1388}
1389
1390func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001391 return j.prebuilt.Name(j.ModuleBase.Name())
1392}
1393
Jiyong Park0b238752019-10-29 11:23:10 +09001394func (j *Import) Stem() string {
1395 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1396}
1397
Jiyong Park618922e2020-01-08 13:35:43 +09001398func (a *Import) JacocoReportClassesFile() android.Path {
1399 return nil
1400}
1401
Bill Peckhama41a6962021-01-11 10:58:54 -08001402func (j *Import) LintDepSets() LintDepSets {
1403 return LintDepSets{}
1404}
1405
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001406func (j *Import) getStrictUpdatabilityLinting() bool {
1407 return false
1408}
1409
1410func (j *Import) setStrictUpdatabilityLinting(bool) {
1411}
1412
Colin Cross74d73e22017-08-02 11:05:49 -07001413func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001414 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001415
1416 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001417 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001418 }
Colin Cross1e676be2016-10-12 14:38:15 -07001419}
1420
Colin Cross74d73e22017-08-02 11:05:49 -07001421func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001422 j.sdkVersion = j.SdkVersion(ctx)
1423 j.minSdkVersion = j.MinSdkVersion(ctx)
1424
Colin Cross56a83212020-09-15 18:30:11 -07001425 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1426 j.hideApexVariantFromMake = true
1427 }
1428
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001429 if ctx.Windows() {
1430 j.HideFromMake()
1431 }
1432
Colin Cross8a497952019-03-05 22:25:09 -08001433 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001434
Jiyong Park0b238752019-10-29 11:23:10 +09001435 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001436 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001437 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1438 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001439 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001440 inputFile := outputFile
1441 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1442 TransformJetifier(ctx, outputFile, inputFile)
1443 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001444 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001445 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001446
Liz Kammerd6c31d22020-08-05 15:40:41 -07001447 var flags javaBuilderFlags
1448
Jiyong Park1be96912018-05-28 18:02:19 +09001449 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001450 tag := ctx.OtherModuleDependencyTag(module)
1451
Colin Crossdcf71b22021-02-01 13:59:03 -08001452 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1453 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001454 switch tag {
1455 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001456 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001457 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001458 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001459 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001460 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001461 switch tag {
1462 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001463 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001464 }
1465 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001466
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001467 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001468 })
1469
Nan Zhang4973ecf2018-08-10 13:42:12 -07001470 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001471 var installDir android.InstallPath
1472 if ctx.InstallInTestcases() {
1473 var archDir string
1474 if !ctx.Host() {
1475 archDir = ctx.DeviceConfig().DeviceArch()
1476 }
1477 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1478 } else {
1479 installDir = android.PathForModuleInstall(ctx, "framework")
1480 }
1481 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001482 }
Jiyong Park19604de2020-03-24 16:44:11 +09001483
1484 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001485
Paul Duffin064b70c2020-11-02 17:32:38 +00001486 if ctx.Device() {
1487 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1488 // obtained from the associated deapexer module.
1489 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1490 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001491 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001492 di := android.FindDeapexerProviderForModule(ctx)
1493 if di == nil {
1494 return // An error has been reported by FindDeapexerProviderForModule.
1495 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001496 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001497 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1498 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001499 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1500 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001501
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001502 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001503 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001504 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1505 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001506
1507 // Initialize the hiddenapi structure.
1508 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001509 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001510 // This should never happen as a variant for a prebuilt_apex is only created if the
1511 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001512 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001513 }
1514 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001515 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001516 if sdkDep.invalidVersion {
1517 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1518 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1519 } else if sdkDep.useFiles {
1520 // sdkDep.jar is actually equivalent to turbine header.jar.
1521 flags.classpath = append(flags.classpath, sdkDep.jars...)
1522 }
1523
1524 // Dex compilation
1525
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001526 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1527 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001528 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001529 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1530
Paul Duffin612e6102021-02-02 13:38:13 +00001531 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001532 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001533 if ctx.Failed() {
1534 return
1535 }
1536
Paul Duffin74d18d12021-05-14 14:18:47 +01001537 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001538 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001539
1540 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001541 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001542
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001543 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001544 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001545 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001546 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001547
1548 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1549 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1550 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1551 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1552 AidlIncludeDirs: j.exportAidlIncludeDirs,
1553 })
Colin Cross2fe66872015-03-30 17:20:39 -07001554}
1555
Paul Duffinaa55f742020-10-06 17:20:13 +01001556func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1557 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001558 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001559 return android.Paths{j.combinedClasspathFile}, nil
1560 default:
1561 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1562 }
1563}
1564
1565var _ android.OutputFileProducer = (*Import)(nil)
1566
Nan Zhanged19fc32017-10-19 13:06:22 -07001567func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001568 if j.combinedClasspathFile == nil {
1569 return nil
1570 }
Colin Cross37f6d792018-07-12 12:28:41 -07001571 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001572}
1573
Colin Cross331a1212018-08-15 20:40:52 -07001574func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001575 if j.combinedClasspathFile == nil {
1576 return nil
1577 }
Colin Cross331a1212018-08-15 20:40:52 -07001578 return android.Paths{j.combinedClasspathFile}
1579}
1580
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001581func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001582 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001583}
1584
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001585func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001586 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001587}
1588
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001589func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1590 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001591}
1592
Jiyong Park45bf82e2020-12-15 22:29:02 +09001593var _ android.ApexModule = (*Import)(nil)
1594
1595// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001596func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001597 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001598}
1599
Jiyong Park45bf82e2020-12-15 22:29:02 +09001600// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001601func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1602 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001603 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001604 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001605 return fmt.Errorf("min_sdk_version is not specified")
1606 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001607 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001608 return nil
1609 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001610 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1611 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001612 }
Jooyung Han749dc692020-04-15 11:03:39 +09001613 return nil
1614}
1615
Paul Duffinfef55002021-06-17 14:56:05 +01001616// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1617// java_sdk_library_import with the specified base module name requires to be exported from a
1618// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001619func requiredFilesFromPrebuiltApexForImport(name string) []string {
1620 // Add the dex implementation jar to the set of exported files.
1621 return []string{
1622 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001623 }
1624}
1625
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001626// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1627// the java library with the specified name.
1628func apexRootRelativePathToJavaLib(name string) string {
1629 return filepath.Join("javalib", name+".jar")
1630}
1631
Paul Duffinfef55002021-06-17 14:56:05 +01001632var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1633
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001634func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001635 name := j.BaseModuleName()
1636 return requiredFilesFromPrebuiltApexForImport(name)
1637}
1638
albaltai36ff7dc2018-12-25 14:35:23 +08001639// Add compile time check for interface implementation
1640var _ android.IDEInfo = (*Import)(nil)
1641var _ android.IDECustomizedModuleName = (*Import)(nil)
1642
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001643// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001644
1645func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1646 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1647}
1648
1649func (j *Import) IDECustomizedModuleName() string {
1650 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1651 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1652 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001653 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001654}
1655
Colin Cross74d73e22017-08-02 11:05:49 -07001656var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001657
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001658func (j *Import) IsInstallable() bool {
1659 return Bool(j.properties.Installable)
1660}
1661
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001662var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001663
Colin Cross1b16b0e2019-02-12 14:41:32 -08001664// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1665//
1666// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1667// compiled against an Android classpath.
1668//
1669// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1670// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001671func ImportFactory() android.Module {
1672 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001673
Liz Kammerd6c31d22020-08-05 15:40:41 -07001674 module.AddProperties(
1675 &module.properties,
1676 &module.dexer.dexProperties,
1677 )
Colin Cross74d73e22017-08-02 11:05:49 -07001678
Paul Duffin71b33cc2021-06-23 11:39:47 +01001679 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001680
Liz Kammerd6c31d22020-08-05 15:40:41 -07001681 module.dexProperties.Optimize.EnabledByDefault = false
1682
Colin Cross74d73e22017-08-02 11:05:49 -07001683 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001684 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001685 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001686 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001687 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001688}
1689
Colin Cross1b16b0e2019-02-12 14:41:32 -08001690// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1691// module.
1692//
1693// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1694// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001695func ImportFactoryHost() android.Module {
1696 module := &Import{}
1697
1698 module.AddProperties(&module.properties)
1699
1700 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001701 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001702 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001703 return module
1704}
1705
Colin Cross42be7612019-02-21 18:12:14 -08001706// dex_import module
1707
1708type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001709 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001710
1711 // set the name of the output
1712 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001713}
1714
1715type DexImport struct {
1716 android.ModuleBase
1717 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001718 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001719 prebuilt android.Prebuilt
1720
1721 properties DexImportProperties
1722
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001723 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001724
1725 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001726
1727 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001728}
1729
1730func (j *DexImport) Prebuilt() *android.Prebuilt {
1731 return &j.prebuilt
1732}
1733
1734func (j *DexImport) PrebuiltSrcs() []string {
1735 return j.properties.Jars
1736}
1737
1738func (j *DexImport) Name() string {
1739 return j.prebuilt.Name(j.ModuleBase.Name())
1740}
1741
Jiyong Park0b238752019-10-29 11:23:10 +09001742func (j *DexImport) Stem() string {
1743 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1744}
1745
Jiyong Park77acec62020-06-01 21:39:15 +09001746func (a *DexImport) JacocoReportClassesFile() android.Path {
1747 return nil
1748}
1749
Colin Cross08dca382020-07-21 20:31:17 -07001750func (a *DexImport) LintDepSets() LintDepSets {
1751 return LintDepSets{}
1752}
1753
Martin Stjernholm6d415272020-01-31 17:10:36 +00001754func (j *DexImport) IsInstallable() bool {
1755 return true
1756}
1757
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001758func (j *DexImport) getStrictUpdatabilityLinting() bool {
1759 return false
1760}
1761
1762func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1763}
1764
Colin Cross42be7612019-02-21 18:12:14 -08001765func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1766 if len(j.properties.Jars) != 1 {
1767 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1768 }
1769
Colin Cross56a83212020-09-15 18:30:11 -07001770 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1771 if !apexInfo.IsForPlatform() {
1772 j.hideApexVariantFromMake = true
1773 }
1774
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001775 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1776 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001777 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1778
1779 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1780 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1781
1782 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001783 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001784
1785 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1786 rule.Temporary(temporary)
1787
1788 // use zip2zip to uncompress classes*.dex files
1789 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001790 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001791 FlagWithInput("-i ", inputJar).
1792 FlagWithOutput("-o ", temporary).
1793 FlagWithArg("-0 ", "'classes*.dex'")
1794
1795 // use zipalign to align uncompressed classes*.dex files
1796 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001797 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001798 Flag("-f").
1799 Text("4").
1800 Input(temporary).
1801 Output(dexOutputFile)
1802
1803 rule.DeleteTemporaryFiles()
1804
Colin Crossf1a035e2020-11-16 17:32:30 -08001805 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001806 } else {
1807 ctx.Build(pctx, android.BuildParams{
1808 Rule: android.Cp,
1809 Input: inputJar,
1810 Output: dexOutputFile,
1811 })
1812 }
1813
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001814 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001815
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001816 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001817
Colin Cross56a83212020-09-15 18:30:11 -07001818 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001819 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1820 j.Stem()+".jar", dexOutputFile)
1821 }
Colin Cross42be7612019-02-21 18:12:14 -08001822}
1823
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001824func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001825 return j.dexJarFile
1826}
1827
Jiyong Park45bf82e2020-12-15 22:29:02 +09001828var _ android.ApexModule = (*DexImport)(nil)
1829
1830// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001831func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1832 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001833 // we don't check prebuilt modules for sdk_version
1834 return nil
1835}
1836
Colin Cross42be7612019-02-21 18:12:14 -08001837// dex_import imports a `.jar` file containing classes.dex files.
1838//
1839// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1840// to the device.
1841func DexImportFactory() android.Module {
1842 module := &DexImport{}
1843
1844 module.AddProperties(&module.properties)
1845
1846 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001847 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001848 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001849 return module
1850}
1851
Colin Cross89536d42017-07-07 14:35:50 -07001852//
1853// Defaults
1854//
1855type Defaults struct {
1856 android.ModuleBase
1857 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001858 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001859}
1860
Colin Cross1b16b0e2019-02-12 14:41:32 -08001861// java_defaults provides a set of properties that can be inherited by other java or android modules.
1862//
1863// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1864// property in the defaults module that exists in the depending module will be prepended to the depending module's
1865// value for that property.
1866//
1867// Example:
1868//
1869// java_defaults {
1870// name: "example_defaults",
1871// srcs: ["common/**/*.java"],
1872// javacflags: ["-Xlint:all"],
1873// aaptflags: ["--auto-add-overlay"],
1874// }
1875//
1876// java_library {
1877// name: "example",
1878// defaults: ["example_defaults"],
1879// srcs: ["example/**/*.java"],
1880// }
1881//
1882// is functionally identical to:
1883//
1884// java_library {
1885// name: "example",
1886// srcs: [
1887// "common/**/*.java",
1888// "example/**/*.java",
1889// ],
1890// javacflags: ["-Xlint:all"],
1891// }
Paul Duffin47357662019-12-05 14:07:14 +00001892func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001893 module := &Defaults{}
1894
Colin Cross89536d42017-07-07 14:35:50 -07001895 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001896 &CommonProperties{},
1897 &DeviceProperties{},
Jooyung Han01d80d82022-01-08 12:16:32 +09001898 &OverridableDeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001899 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001900 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001901 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001902 &aaptProperties{},
1903 &androidLibraryProperties{},
1904 &appProperties{},
1905 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001906 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001907 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001908 &ImportProperties{},
1909 &AARImportProperties{},
1910 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001911 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001912 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001913 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001914 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001915 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001916 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001917 )
1918
1919 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001920 return module
1921}
Nan Zhangea568a42017-11-08 21:20:04 -08001922
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001923func kytheExtractJavaFactory() android.Singleton {
1924 return &kytheExtractJavaSingleton{}
1925}
1926
1927type kytheExtractJavaSingleton struct {
1928}
1929
1930func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1931 var xrefTargets android.Paths
1932 ctx.VisitAllModules(func(module android.Module) {
1933 if javaModule, ok := module.(xref); ok {
1934 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1935 }
1936 })
1937 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1938 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001939 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001940 }
1941}
1942
Nan Zhangea568a42017-11-08 21:20:04 -08001943var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001944var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001945var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001946var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001947
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001948// Add class loader context (CLC) of a given dependency to the current CLC.
1949func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1950 clcMap dexpreopt.ClassLoaderContextMap) {
1951
1952 dep, ok := depModule.(UsesLibraryDependency)
1953 if !ok {
1954 return
1955 }
1956
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001957 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1958
1959 var sdkLib *string
1960 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1961 // A shared SDK library. This should be added as a top-level CLC element.
1962 sdkLib = &depName
1963 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1964 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1965 // property. This should be handled in the same way as a shared SDK library.
1966 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001967 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001968
1969 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001970 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001971 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001972 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1973 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1974 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1975 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001976 } else if depTag == staticLibTag {
1977 // Propagate <uses-library> through static library dependencies, unless it is a component
1978 // library (such as stubs). Component libraries have a dependency on their SDK library,
1979 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001980 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001981 return
1982 }
1983 } else {
1984 // Don't propagate <uses-library> for other dependency tags.
1985 return
1986 }
1987
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001988 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1989 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1990 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1991 // from its CLC should be added to the current CLC.
1992 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001993 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001994 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001995 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001996 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1997 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001998}
Wei Libafb6d62021-12-10 03:14:59 -08001999
2000type javaLibraryAttributes struct {
2001 Srcs bazel.LabelListAttribute
2002 Deps bazel.LabelListAttribute
2003 Javacopts bazel.StringListAttribute
2004}
2005
2006func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
2007 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
2008 attrs := &javaLibraryAttributes{
2009 Srcs: srcs,
2010 }
2011
2012 if m.properties.Javacflags != nil {
2013 attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
2014 }
2015
2016 if m.properties.Libs != nil {
2017 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.properties.Libs))
2018 }
2019
2020 props := bazel.BazelTargetModuleProperties{
2021 Rule_class: "java_library",
2022 Bzl_load_location: "//build/bazel/rules/java:library.bzl",
2023 }
2024
2025 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
2026}
2027
2028type javaBinaryHostAttributes struct {
2029 Srcs bazel.LabelListAttribute
2030 Deps bazel.LabelListAttribute
2031 Main_class string
2032 Jvm_flags bazel.StringListAttribute
Sam Delmerico4e272292022-01-06 20:03:51 +00002033 Javacopts bazel.StringListAttribute
Wei Libafb6d62021-12-10 03:14:59 -08002034}
2035
2036// JavaBinaryHostBp2Build is for java_binary_host bp2build.
2037func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
2038 mainClass := ""
2039 if m.binaryProperties.Main_class != nil {
2040 mainClass = *m.binaryProperties.Main_class
2041 }
2042 if m.properties.Manifest != nil {
2043 mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String())
2044 if err != nil {
2045 return
2046 }
2047 mainClass = mainClassInManifest
2048 }
2049 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
2050 attrs := &javaBinaryHostAttributes{
2051 Srcs: srcs,
2052 Main_class: mainClass,
2053 }
2054
Sam Delmerico4e272292022-01-06 20:03:51 +00002055 if m.properties.Javacflags != nil {
2056 attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
2057 }
2058
Wei Libafb6d62021-12-10 03:14:59 -08002059 // Attribute deps
2060 deps := []string{}
2061 if m.properties.Static_libs != nil {
2062 deps = append(deps, m.properties.Static_libs...)
2063 }
2064 if m.binaryProperties.Jni_libs != nil {
2065 deps = append(deps, m.binaryProperties.Jni_libs...)
2066 }
2067 if len(deps) > 0 {
2068 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, deps))
2069 }
2070
2071 // Attribute jvm_flags
2072 if m.binaryProperties.Jni_libs != nil {
2073 jniLibPackages := map[string]bool{}
2074 for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes {
2075 jniLibPackage := jniLibLabel.Label
2076 indexOfColon := strings.Index(jniLibLabel.Label, ":")
2077 if indexOfColon > 0 {
2078 // JNI lib from other package
2079 jniLibPackage = jniLibLabel.Label[2:indexOfColon]
2080 } else if indexOfColon == 0 {
2081 // JNI lib in the same package of java_binary
2082 packageOfCurrentModule := m.GetBazelLabel(ctx, m)
2083 jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")]
2084 }
2085 if _, inMap := jniLibPackages[jniLibPackage]; !inMap {
2086 jniLibPackages[jniLibPackage] = true
2087 }
2088 }
2089 jniLibPaths := []string{}
2090 for jniLibPackage, _ := range jniLibPackages {
2091 // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH
2092 jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage)
2093 }
2094 attrs.Jvm_flags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")})
2095 }
2096
2097 props := bazel.BazelTargetModuleProperties{
2098 Rule_class: "java_binary",
2099 }
2100
2101 // Create the BazelTargetModule.
2102 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
2103}