blob: e3e9721535fb2c04298e6e667feb3a02d7c8f6dc [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"}
328 staticLibTag = dependencyTag{name: "staticlib"}
Colin Cross65cb3142021-12-10 23:05:02 +0000329 libTag = dependencyTag{name: "javalib", runtimeLinked: true}
330 java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800331 pluginTag = dependencyTag{name: "plugin"}
332 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
333 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross65cb3142021-12-10 23:05:02 +0000334 bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true}
335 systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800336 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross65cb3142021-12-10 23:05:02 +0000337 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true}
338 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700339 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800340 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
341 certificateTag = dependencyTag{name: "certificate"}
342 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
343 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
Colin Cross65cb3142021-12-10 23:05:02 +0000344 jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800345 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
346 jniInstallTag = installDependencyTag{name: "jni install"}
347 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700348)
Colin Cross2fe66872015-03-30 17:20:39 -0700349
Jiyong Park83dc74b2020-01-14 18:38:44 +0900350func IsLibDepTag(depTag blueprint.DependencyTag) bool {
351 return depTag == libTag
352}
353
354func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
355 return depTag == staticLibTag
356}
357
Colin Crossfc3674a2017-09-18 17:41:52 -0700358type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100359 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700360
Colin Cross6cef4812019-10-17 14:23:50 -0700361 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
362 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100363
364 // The default system modules to use. Will be an empty string if no system
365 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700366 systemModules string
367
Pete Gilline3d44b22020-06-29 11:28:51 +0100368 // The modules that will be added to the classpath regardless of the Java language level targeted
369 classpath []string
370
Colin Cross6cef4812019-10-17 14:23:50 -0700371 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100372 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700373 java9Classpath []string
374
Colin Crossa97c5d32018-03-28 14:58:31 -0700375 frameworkResModule string
376
Colin Cross86a60ae2018-05-29 14:44:55 -0700377 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700378 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100379
380 noStandardLibs, noFrameworksLibs bool
381}
382
383func (s sdkDep) hasStandardLibs() bool {
384 return !s.noStandardLibs
385}
386
387func (s sdkDep) hasFrameworkLibs() bool {
388 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700389}
390
Colin Crossa4f08812018-10-02 22:03:40 -0700391type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700392 name string
393 path android.Path
394 target android.Target
395 coverageFile android.OptionalPath
396 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700397}
398
Jiyong Parkf1691d22021-03-29 20:11:58 +0900399func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700400 sdkDep := decodeSdkDep(ctx, sdkContext)
401 if sdkDep.useModule {
402 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
403 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
404 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
405 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
406 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
407 }
408 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
409 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
410 }
411 }
412 if sdkDep.systemModules != "" {
413 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
414 }
415}
416
Colin Cross32f676a2017-09-06 13:41:06 -0700417type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800418 classpath classpath
419 java9Classpath classpath
420 bootClasspath classpath
421 processorPath classpath
422 errorProneProcessorPath classpath
423 processorClasses []string
424 staticJars android.Paths
425 staticHeaderJars android.Paths
426 staticResourceJars android.Paths
427 aidlIncludeDirs android.Paths
428 srcs android.Paths
429 srcJars android.Paths
430 systemModules *systemModules
431 aidlPreprocess android.OptionalPath
432 kotlinStdlib android.Paths
433 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700434 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800435
436 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700437}
Colin Cross2fe66872015-03-30 17:20:39 -0700438
Colin Cross54250902017-12-05 09:28:08 -0800439func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
440 for _, f := range dep.Srcs() {
441 if f.Ext() != ".jar" {
442 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
443 ctx.OtherModuleName(dep.(blueprint.Module)))
444 }
445 }
446}
447
Jiyong Parkf1691d22021-03-29 20:11:58 +0900448func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700449 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700450 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700451 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900452 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700453 } else {
Sorin Basca171c2102022-01-12 16:04:50 +0000454 return JAVA_VERSION_11
Nan Zhang357466b2018-04-17 17:38:36 -0700455 }
Nan Zhang357466b2018-04-17 17:38:36 -0700456}
457
Colin Cross1e743852019-10-28 11:37:20 -0700458type javaVersion int
459
460const (
461 JAVA_VERSION_UNSUPPORTED = 0
462 JAVA_VERSION_6 = 6
463 JAVA_VERSION_7 = 7
464 JAVA_VERSION_8 = 8
465 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000466 JAVA_VERSION_11 = 11
Colin Cross1e743852019-10-28 11:37:20 -0700467)
468
469func (v javaVersion) String() string {
470 switch v {
471 case JAVA_VERSION_6:
472 return "1.6"
473 case JAVA_VERSION_7:
474 return "1.7"
475 case JAVA_VERSION_8:
476 return "1.8"
477 case JAVA_VERSION_9:
478 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000479 case JAVA_VERSION_11:
480 return "11"
Colin Cross1e743852019-10-28 11:37:20 -0700481 default:
482 return "unsupported"
483 }
484}
485
486// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
487func (v javaVersion) usesJavaModules() bool {
488 return v >= 9
489}
490
491func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100492 switch javaVersion {
493 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700494 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100495 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700496 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100497 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700498 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100499 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700500 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000501 case "11":
502 return JAVA_VERSION_11
503 case "10":
504 ctx.PropertyErrorf("java_version", "Java language levels 10 is not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700505 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100506 default:
507 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700508 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100509 }
510}
511
Colin Cross2fe66872015-03-30 17:20:39 -0700512//
513// Java libraries (.jar file)
514//
515
Colin Crossf506d872017-07-19 15:53:04 -0700516type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700517 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700518
519 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700520}
521
Jiyong Park45bf82e2020-12-15 22:29:02 +0900522var _ android.ApexModule = (*Library)(nil)
523
satayevd604b212021-07-21 14:23:52 +0100524// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100525type PermittedPackagesForUpdatableBootJars interface {
526 PermittedPackagesForUpdatableBootJars() []string
527}
528
529var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
530
531func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
532 return j.properties.Permitted_packages
533}
534
Colin Cross42be7612019-02-21 18:12:14 -0800535func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000536 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700537 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000538 return true
539 }
540
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000541 // Store uncompressed (and do not strip) dex files from boot class path jars.
542 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
543 return true
544 }
545
546 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000547 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000548 return true
549 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800550 if ctx.Config().UncompressPrivAppDex() &&
551 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
552 return true
553 }
554
Colin Cross2fc72f62018-12-21 12:59:54 -0800555 return false
556}
557
Jiakai Zhang22450f22021-10-11 03:05:20 +0000558// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
559func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
560 if dexer.dexProperties.Uncompress_dex == nil {
561 // If the value was not force-set by the user, use reasonable default based on the module.
562 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
563 }
564}
565
Colin Crossf506d872017-07-19 15:53:04 -0700566func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900567 j.sdkVersion = j.SdkVersion(ctx)
568 j.minSdkVersion = j.MinSdkVersion(ctx)
satayev0a420e72021-11-29 17:25:52 +0000569 j.maxSdkVersion = j.MaxSdkVersion(ctx)
Jiyong Park92315372021-04-02 08:45:46 +0900570
Colin Cross56a83212020-09-15 18:30:11 -0700571 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
572 if !apexInfo.IsForPlatform() {
573 j.hideApexVariantFromMake = true
574 }
575
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100576 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000577 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
578 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800579 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000580 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700581 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100582 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700583 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700584
bralee1fbf4402020-05-21 10:11:59 +0800585 // Collect the module directory for IDE info in java/jdeps.go.
586 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
587
Colin Cross56a83212020-09-15 18:30:11 -0700588 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900589 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700590 var extraInstallDeps android.Paths
591 if j.InstallMixin != nil {
592 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
593 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700594 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
595 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800596 j.hostdexInstallFile = ctx.InstallFile(
597 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700598 j.Stem()+"-hostdex.jar", j.outputFile)
599 }
600 var installDir android.InstallPath
601 if ctx.InstallInTestcases() {
602 var archDir string
603 if !ctx.Host() {
604 archDir = ctx.DeviceConfig().DeviceArch()
605 }
606 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
607 } else {
608 installDir = android.PathForModuleInstall(ctx, "framework")
609 }
610 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700611 }
Colin Crossb7a63242015-04-16 14:09:14 -0700612}
613
Colin Crossf506d872017-07-19 15:53:04 -0700614func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700615 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100616 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700617}
618
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000619const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000620 aidlIncludeDir = "aidl"
621 javaDir = "java"
622 jarFileSuffix = ".jar"
623 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000624)
625
Paul Duffina0dbf432019-12-05 11:25:53 +0000626// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000627func sdkSnapshotFilePathForJar(osPrefix, name string) string {
628 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000629}
630
Paul Duffina04c1072020-03-02 10:16:35 +0000631func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
632 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000633}
634
Paul Duffin13879572019-11-28 14:31:38 +0000635type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000636 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000637
638 // Function to retrieve the appropriate output jar (implementation or header) from
639 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000640 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
641
642 // Function to compute the snapshot relative path to which the named library's
643 // jar should be copied.
644 snapshotPathGetter func(osPrefix, name string) string
645
646 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
647 // files like aidl files should also be copied.
648 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000649}
650
Paul Duffindb170e42020-12-08 17:48:25 +0000651const (
652 onlyCopyJarToSnapshot = true
653 copyEverythingToSnapshot = false
654)
655
Paul Duffin296701e2021-07-14 10:29:36 +0100656func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
657 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000658}
659
660func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
661 _, ok := module.(*Library)
662 return ok
663}
664
Paul Duffin3a4eb502020-03-19 16:11:18 +0000665func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
666 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000667}
Paul Duffina0dbf432019-12-05 11:25:53 +0000668
Paul Duffin14eb4672020-03-02 11:33:02 +0000669func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000670 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000671}
672
673type librarySdkMemberProperties struct {
674 android.SdkMemberPropertiesBase
675
Paul Duffin864e1b42020-05-06 10:23:19 +0100676 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000677 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100678
679 // The list of permitted packages that need to be passed to the prebuilts as they are used to
680 // create the updatable-bcp-packages.txt file.
681 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000682}
683
Paul Duffin3a4eb502020-03-19 16:11:18 +0000684func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000685 j := variant.(*Library)
686
Paul Duffindb170e42020-12-08 17:48:25 +0000687 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
688
Paul Duffina551a1c2020-03-17 21:04:24 +0000689 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100690
691 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000692}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000693
Paul Duffin3a4eb502020-03-19 16:11:18 +0000694func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000695 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000696
Paul Duffindb170e42020-12-08 17:48:25 +0000697 memberType := ctx.MemberType().(*librarySdkMemberType)
698
Paul Duffina551a1c2020-03-17 21:04:24 +0000699 exportedJar := p.JarToExport
700 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000701 // Delegate the creation of the snapshot relative path to the member type.
702 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
703
704 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000705 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
706
Paul Duffina551a1c2020-03-17 21:04:24 +0000707 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
708 }
709
Paul Duffin869de142021-07-15 14:14:41 +0100710 if len(p.PermittedPackages) > 0 {
711 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
712 }
713
Paul Duffindb170e42020-12-08 17:48:25 +0000714 // Do not copy anything else to the snapshot.
715 if memberType.onlyCopyJarToSnapshot {
716 return
717 }
718
Paul Duffina551a1c2020-03-17 21:04:24 +0000719 aidlIncludeDirs := p.AidlIncludeDirs
720 if len(aidlIncludeDirs) != 0 {
721 sdkModuleContext := ctx.SdkModuleContext()
722 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000723 // TODO(jiyong): copy parcelable declarations only
724 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
725 for _, file := range aidlFiles {
726 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
727 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000728 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000729
Paul Duffina551a1c2020-03-17 21:04:24 +0000730 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000731 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000732}
733
Colin Cross1b16b0e2019-02-12 14:41:32 -0800734// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
735//
736// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
737// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
738// as a `static_libs` dependency of another module.
739//
740// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
741// a device.
742//
743// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
744// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700745func LibraryFactory() android.Module {
746 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700747
Colin Crossce6734e2020-06-15 16:09:53 -0700748 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700749
Paul Duffin71b33cc2021-06-23 11:39:47 +0100750 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100751
Jiyong Park7f7766d2019-07-25 22:02:35 +0900752 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900753 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800754 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900755 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700756 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700757}
758
Colin Cross1b16b0e2019-02-12 14:41:32 -0800759// java_library_static is an obsolete alias for java_library.
760func LibraryStaticFactory() android.Module {
761 return LibraryFactory()
762}
763
764// java_library_host builds and links sources into a `.jar` file for the host.
765//
766// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
767// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700768func LibraryHostFactory() android.Module {
769 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700770
Colin Crossce6734e2020-06-15 16:09:53 -0700771 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700772
Colin Cross9ae1b922018-06-26 17:59:05 -0700773 module.Module.properties.Installable = proptools.BoolPtr(true)
774
Jiyong Park7f7766d2019-07-25 22:02:35 +0900775 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100776 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800777 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900778 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700779 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700780}
781
782//
Colin Crossb628ea52018-08-14 16:42:33 -0700783// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700784//
785
Dan Shi95d19422020-08-15 12:24:26 -0700786// Test option struct.
787type TestOptions struct {
788 // a list of extra test configuration files that should be installed with the module.
789 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800790
791 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
792 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700793}
794
Colin Cross05638fc2018-04-09 18:40:24 -0700795type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700796 // list of compatibility suites (for example "cts", "vts") that the module should be
797 // installed into.
798 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700799
800 // the name of the test configuration (for example "AndroidTest.xml") that should be
801 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800802 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700803
Jack He33338892018-09-19 02:21:28 -0700804 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
805 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800806 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700807
Colin Crossd96ca352018-08-10 16:06:24 -0700808 // list of files or filegroup modules that provide data that should be installed alongside
809 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900810 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700811
812 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
813 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
814 // explicitly.
815 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800816
817 // Add parameterized mainline modules to auto generated test config. The options will be
818 // handled by TradeFed to do downloading and installing the specified modules on the device.
819 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700820
821 // Test options.
822 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800823
824 // Names of modules containing JNI libraries that should be installed alongside the test.
825 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700826
827 // Install the test into a folder named for the module in all test suites.
828 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700829}
830
Liz Kammerdd849a82020-06-12 16:38:45 -0700831type hostTestProperties struct {
832 // list of native binary modules that should be installed alongside the test
833 Data_native_bins []string `android:"arch_variant"`
834}
835
Paul Duffin42df1442019-03-20 12:45:53 +0000836type testHelperLibraryProperties struct {
837 // list of compatibility suites (for example "cts", "vts") that the module should be
838 // installed into.
839 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700840
841 // Install the test into a folder named for the module in all test suites.
842 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000843}
844
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000845type prebuiltTestProperties struct {
846 // list of compatibility suites (for example "cts", "vts") that the module should be
847 // installed into.
848 Test_suites []string `android:"arch_variant"`
849
850 // the name of the test configuration (for example "AndroidTest.xml") that should be
851 // installed with the module.
852 Test_config *string `android:"path,arch_variant"`
853}
854
Colin Cross05638fc2018-04-09 18:40:24 -0700855type Test struct {
856 Library
857
858 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700859
Dan Shi95d19422020-08-15 12:24:26 -0700860 testConfig android.Path
861 extraTestConfigs android.Paths
862 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700863}
864
Liz Kammerdd849a82020-06-12 16:38:45 -0700865type TestHost struct {
866 Test
867
868 testHostProperties hostTestProperties
869}
870
Paul Duffin42df1442019-03-20 12:45:53 +0000871type TestHelperLibrary struct {
872 Library
873
874 testHelperLibraryProperties testHelperLibraryProperties
875}
876
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000877type JavaTestImport struct {
878 Import
879
880 prebuiltTestProperties prebuiltTestProperties
881
882 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700883 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000884}
885
Colin Cross24cc4be62021-11-03 14:09:41 -0700886func (j *Test) InstallInTestcases() bool {
887 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
888 // testcases by base_rules.mk.
889 return !j.Host()
890}
891
892func (j *TestHelperLibrary) InstallInTestcases() bool {
893 return true
894}
895
896func (j *JavaTestImport) InstallInTestcases() bool {
897 return true
898}
899
Liz Kammerdd849a82020-06-12 16:38:45 -0700900func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
901 if len(j.testHostProperties.Data_native_bins) > 0 {
902 for _, target := range ctx.MultiTargets() {
903 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
904 }
905 }
906
Colin Crossf8d9c492021-01-26 11:01:43 -0800907 if len(j.testProperties.Jni_libs) > 0 {
908 for _, target := range ctx.MultiTargets() {
909 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
910 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
911 }
912 }
913
Liz Kammerdd849a82020-06-12 16:38:45 -0700914 j.deps(ctx)
915}
916
Yuexi Ma627263f2021-03-04 13:47:56 -0800917func (j *TestHost) AddExtraResource(p android.Path) {
918 j.extraResources = append(j.extraResources, p)
919}
920
Colin Cross303e21f2018-08-07 16:49:25 -0700921func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000922 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
923 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700924 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000925 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
926 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700927 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800928 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700929
Colin Cross8a497952019-03-05 22:25:09 -0800930 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700931
Dan Shi95d19422020-08-15 12:24:26 -0700932 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
933
Liz Kammerdd849a82020-06-12 16:38:45 -0700934 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
935 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
936 })
937
Colin Crossf8d9c492021-01-26 11:01:43 -0800938 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
939 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
940 if sharedLibInfo.SharedLibrary != nil {
941 // Copy to an intermediate output directory to append "lib[64]" to the path,
942 // so that it's compatible with the default rpath values.
943 var relPath string
944 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
945 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
946 } else {
947 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
948 }
949 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
950 ctx.Build(pctx, android.BuildParams{
951 Rule: android.Cp,
952 Input: sharedLibInfo.SharedLibrary,
953 Output: relocatedLib,
954 })
955 j.data = append(j.data, relocatedLib)
956 } else {
957 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
958 }
959 })
960
Colin Cross303e21f2018-08-07 16:49:25 -0700961 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700962}
963
Paul Duffin42df1442019-03-20 12:45:53 +0000964func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
965 j.Library.GenerateAndroidBuildActions(ctx)
966}
967
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000968func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
969 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800970 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000971
972 j.Import.GenerateAndroidBuildActions(ctx)
973}
974
975type testSdkMemberType struct {
976 android.SdkMemberTypeBase
977}
978
Paul Duffin296701e2021-07-14 10:29:36 +0100979func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
980 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000981}
982
983func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
984 _, ok := module.(*Test)
985 return ok
986}
987
Paul Duffin3a4eb502020-03-19 16:11:18 +0000988func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
989 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000990}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000991
Paul Duffin14eb4672020-03-02 11:33:02 +0000992func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
993 return &testSdkMemberProperties{}
994}
995
996type testSdkMemberProperties struct {
997 android.SdkMemberPropertiesBase
998
Paul Duffina551a1c2020-03-17 21:04:24 +0000999 JarToExport android.Path
1000 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00001001}
1002
Paul Duffin3a4eb502020-03-19 16:11:18 +00001003func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00001004 test := variant.(*Test)
1005
1006 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001007 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00001008 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001009 }
1010
Paul Duffina551a1c2020-03-17 21:04:24 +00001011 p.JarToExport = implementationJars[0]
1012 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00001013}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001014
Paul Duffin3a4eb502020-03-19 16:11:18 +00001015func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001016 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001017
Paul Duffina551a1c2020-03-17 21:04:24 +00001018 exportedJar := p.JarToExport
1019 if exportedJar != nil {
1020 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
1021 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001022
1023 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001024 }
1025
1026 testConfig := p.TestConfig
1027 if testConfig != nil {
1028 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1029 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001030 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1031 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001032}
1033
Colin Cross1b16b0e2019-02-12 14:41:32 -08001034// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1035// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1036//
1037// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1038// compiled against the device bootclasspath.
1039//
1040// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1041// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001042func TestFactory() android.Module {
1043 module := &Test{}
1044
Colin Crossce6734e2020-06-15 16:09:53 -07001045 module.addHostAndDeviceProperties()
1046 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001047
Colin Cross9ae1b922018-06-26 17:59:05 -07001048 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001049 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001050 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001051
Paul Duffinb6b89a42021-05-06 16:33:43 +01001052 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001053 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001054 return module
1055}
1056
Paul Duffin42df1442019-03-20 12:45:53 +00001057// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1058func TestHelperLibraryFactory() android.Module {
1059 module := &TestHelperLibrary{}
1060
Colin Crossce6734e2020-06-15 16:09:53 -07001061 module.addHostAndDeviceProperties()
1062 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001063
Colin Cross9a4abed2019-04-24 13:19:28 -07001064 module.Module.properties.Installable = proptools.BoolPtr(true)
1065 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001066 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001067
Paul Duffin42df1442019-03-20 12:45:53 +00001068 InitJavaModule(module, android.HostAndDeviceSupported)
1069 return module
1070}
1071
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001072// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1073// and makes sure that it is added to the appropriate test suite.
1074//
1075// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1076// compiled against an Android classpath.
1077//
1078// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1079// for host modules.
1080func JavaTestImportFactory() android.Module {
1081 module := &JavaTestImport{}
1082
1083 module.AddProperties(
1084 &module.Import.properties,
1085 &module.prebuiltTestProperties)
1086
1087 module.Import.properties.Installable = proptools.BoolPtr(true)
1088
1089 android.InitPrebuiltModule(module, &module.properties.Jars)
1090 android.InitApexModule(module)
1091 android.InitSdkAwareModule(module)
1092 InitJavaModule(module, android.HostAndDeviceSupported)
1093 return module
1094}
1095
Colin Cross1b16b0e2019-02-12 14:41:32 -08001096// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1097// allow running the test with `atest` or a `TEST_MAPPING` file.
1098//
1099// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1100// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001101func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001102 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001103
Colin Crossce6734e2020-06-15 16:09:53 -07001104 module.addHostProperties()
1105 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001106 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001107
Yuexi Ma627263f2021-03-04 13:47:56 -08001108 InitTestHost(
1109 module,
1110 proptools.BoolPtr(true),
1111 nil,
1112 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001113
Liz Kammerdd849a82020-06-12 16:38:45 -07001114 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001115
Colin Cross05638fc2018-04-09 18:40:24 -07001116 return module
1117}
1118
Yuexi Ma627263f2021-03-04 13:47:56 -08001119func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1120 th.properties.Installable = installable
1121 th.testProperties.Auto_gen_config = autoGenConfig
1122 th.testProperties.Test_suites = testSuites
1123}
1124
Colin Cross05638fc2018-04-09 18:40:24 -07001125//
Colin Cross2fe66872015-03-30 17:20:39 -07001126// Java Binaries (.jar file plus wrapper script)
1127//
1128
Colin Crossf506d872017-07-19 15:53:04 -07001129type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001130 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001131 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001132
1133 // Name of the class containing main to be inserted into the manifest as Main-Class.
1134 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001135
1136 // Names of modules containing JNI libraries that should be installed alongside the host
1137 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001138 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001139}
1140
Colin Crossf506d872017-07-19 15:53:04 -07001141type Binary struct {
1142 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001143
Colin Crossf506d872017-07-19 15:53:04 -07001144 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001145
Colin Cross6b4a32d2017-12-05 13:42:45 -08001146 isWrapperVariant bool
1147
Colin Crossc3315992017-12-08 19:12:36 -08001148 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001149 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001150}
1151
Alex Light24237172017-10-26 09:46:21 -07001152func (j *Binary) HostToolPath() android.OptionalPath {
1153 return android.OptionalPathForPath(j.binaryFile)
1154}
1155
Colin Crossf506d872017-07-19 15:53:04 -07001156func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001157 if ctx.Arch().ArchType == android.Common {
1158 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001159 if j.binaryProperties.Main_class != nil {
1160 if j.properties.Manifest != nil {
1161 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1162 }
1163 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1164 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1165 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1166 }
1167
Colin Cross6b4a32d2017-12-05 13:42:45 -08001168 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001169 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001170 // Handle the binary wrapper
1171 j.isWrapperVariant = true
1172
Colin Cross366938f2017-12-11 16:29:02 -08001173 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001174 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001175 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001176 if ctx.Windows() {
1177 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1178 }
1179
Colin Cross6b4a32d2017-12-05 13:42:45 -08001180 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1181 }
1182
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001183 ext := ""
1184 if ctx.Windows() {
1185 ext = ".bat"
1186 }
1187
Colin Crossc179ea62020-10-09 10:54:15 -07001188 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001189 // of the wrapper variant, which will include the common variant's jar file and any JNI
1190 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001191 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001192 ctx.ModuleName()+ext, j.wrapperFile)
1193 }
Colin Cross2fe66872015-03-30 17:20:39 -07001194}
1195
Colin Crossf506d872017-07-19 15:53:04 -07001196func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001197 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001198 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001199 }
1200 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001201 // These dependencies ensure the host installation rules will install the jar file and
1202 // the jni libraries when the wrapper is installed.
1203 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1204 ctx.AddVariationDependencies(
1205 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1206 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001207 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001208}
1209
Colin Cross1b16b0e2019-02-12 14:41:32 -08001210// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1211// as well.
1212//
1213// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1214// compiled against the device bootclasspath.
1215//
1216// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1217// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001218func BinaryFactory() android.Module {
1219 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001220
Colin Crossce6734e2020-06-15 16:09:53 -07001221 module.addHostAndDeviceProperties()
1222 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001223
Colin Cross9ae1b922018-06-26 17:59:05 -07001224 module.Module.properties.Installable = proptools.BoolPtr(true)
1225
Colin Cross6b4a32d2017-12-05 13:42:45 -08001226 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1227 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001228 android.InitBazelModule(module)
1229
Colin Cross36242852017-06-23 15:06:31 -07001230 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001231}
1232
Colin Cross1b16b0e2019-02-12 14:41:32 -08001233// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1234//
1235// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1236// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001237func BinaryHostFactory() android.Module {
1238 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001239
Colin Crossce6734e2020-06-15 16:09:53 -07001240 module.addHostProperties()
1241 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001242
Colin Cross9ae1b922018-06-26 17:59:05 -07001243 module.Module.properties.Installable = proptools.BoolPtr(true)
1244
Colin Cross6b4a32d2017-12-05 13:42:45 -08001245 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1246 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001247 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001248 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001249}
1250
1251//
1252// Java prebuilts
1253//
1254
Colin Cross74d73e22017-08-02 11:05:49 -07001255type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001256 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001257
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001258 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1259 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001260 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001261
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001262 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1263 // specified.
1264 Min_sdk_version *string
1265
Colin Cross535e2cf2017-10-20 17:57:49 -07001266 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001267
Paul Duffin869de142021-07-15 14:14:41 +01001268 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001269 Permitted_packages []string
1270
Jiyong Park1be96912018-05-28 18:02:19 +09001271 // List of shared java libs that this module has dependencies to
1272 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001273
1274 // List of files to remove from the jar file(s)
1275 Exclude_files []string
1276
1277 // List of directories to remove from the jar file(s)
1278 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001279
1280 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001281 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001282
1283 // set the name of the output
1284 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001285
1286 Aidl struct {
1287 // directories that should be added as include directories for any aidl sources of modules
1288 // that depend on this module, as well as to aidl for this module.
1289 Export_include_dirs []string
1290 }
Colin Cross74d73e22017-08-02 11:05:49 -07001291}
1292
1293type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001294 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001295 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001296 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001297 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001298 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001299
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001300 // Functionality common to Module and Import.
1301 embeddableInModuleAndImport
1302
Liz Kammerd6c31d22020-08-05 15:40:41 -07001303 hiddenAPI
1304 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001305 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001306
Colin Cross74d73e22017-08-02 11:05:49 -07001307 properties ImportProperties
1308
Liz Kammerd6c31d22020-08-05 15:40:41 -07001309 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001310 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001311 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001312
Colin Cross0a6e0072017-08-30 14:24:55 -07001313 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001314 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001315 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001316
1317 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001318
1319 sdkVersion android.SdkSpec
1320 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001321}
1322
Paul Duffin630b11e2021-07-15 13:35:26 +01001323var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1324
1325func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1326 return j.properties.Permitted_packages
1327}
1328
Jiyong Park92315372021-04-02 08:45:46 +09001329func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1330 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001331}
1332
Jiyong Parkf1691d22021-03-29 20:11:58 +09001333func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001334 return "none"
1335}
1336
Jiyong Park92315372021-04-02 08:45:46 +09001337func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001338 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001339 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001340 }
Jiyong Park92315372021-04-02 08:45:46 +09001341 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001342}
1343
Jiyong Park92315372021-04-02 08:45:46 +09001344func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1345 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001346}
1347
Colin Cross74d73e22017-08-02 11:05:49 -07001348func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001349 return &j.prebuilt
1350}
1351
Colin Cross74d73e22017-08-02 11:05:49 -07001352func (j *Import) PrebuiltSrcs() []string {
1353 return j.properties.Jars
1354}
1355
1356func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001357 return j.prebuilt.Name(j.ModuleBase.Name())
1358}
1359
Jiyong Park0b238752019-10-29 11:23:10 +09001360func (j *Import) Stem() string {
1361 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1362}
1363
Jiyong Park618922e2020-01-08 13:35:43 +09001364func (a *Import) JacocoReportClassesFile() android.Path {
1365 return nil
1366}
1367
Bill Peckhama41a6962021-01-11 10:58:54 -08001368func (j *Import) LintDepSets() LintDepSets {
1369 return LintDepSets{}
1370}
1371
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001372func (j *Import) getStrictUpdatabilityLinting() bool {
1373 return false
1374}
1375
1376func (j *Import) setStrictUpdatabilityLinting(bool) {
1377}
1378
Colin Cross74d73e22017-08-02 11:05:49 -07001379func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001380 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001381
1382 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001383 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001384 }
Colin Cross1e676be2016-10-12 14:38:15 -07001385}
1386
Colin Cross74d73e22017-08-02 11:05:49 -07001387func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001388 j.sdkVersion = j.SdkVersion(ctx)
1389 j.minSdkVersion = j.MinSdkVersion(ctx)
1390
Colin Cross56a83212020-09-15 18:30:11 -07001391 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1392 j.hideApexVariantFromMake = true
1393 }
1394
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001395 if ctx.Windows() {
1396 j.HideFromMake()
1397 }
1398
Colin Cross8a497952019-03-05 22:25:09 -08001399 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001400
Jiyong Park0b238752019-10-29 11:23:10 +09001401 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001402 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001403 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1404 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001405 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001406 inputFile := outputFile
1407 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1408 TransformJetifier(ctx, outputFile, inputFile)
1409 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001410 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001411 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001412
Liz Kammerd6c31d22020-08-05 15:40:41 -07001413 var flags javaBuilderFlags
1414
Jiyong Park1be96912018-05-28 18:02:19 +09001415 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001416 tag := ctx.OtherModuleDependencyTag(module)
1417
Colin Crossdcf71b22021-02-01 13:59:03 -08001418 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1419 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001420 switch tag {
1421 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001422 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001423 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001424 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001425 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001426 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001427 switch tag {
1428 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001429 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001430 }
1431 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001432
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001433 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001434 })
1435
Nan Zhang4973ecf2018-08-10 13:42:12 -07001436 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001437 var installDir android.InstallPath
1438 if ctx.InstallInTestcases() {
1439 var archDir string
1440 if !ctx.Host() {
1441 archDir = ctx.DeviceConfig().DeviceArch()
1442 }
1443 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1444 } else {
1445 installDir = android.PathForModuleInstall(ctx, "framework")
1446 }
1447 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001448 }
Jiyong Park19604de2020-03-24 16:44:11 +09001449
1450 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001451
Paul Duffin064b70c2020-11-02 17:32:38 +00001452 if ctx.Device() {
1453 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1454 // obtained from the associated deapexer module.
1455 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1456 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001457 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001458 di := android.FindDeapexerProviderForModule(ctx)
1459 if di == nil {
1460 return // An error has been reported by FindDeapexerProviderForModule.
1461 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001462 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001463 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1464 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001465 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1466 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001467
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001468 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001469 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001470 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1471 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001472
1473 // Initialize the hiddenapi structure.
1474 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001475 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001476 // This should never happen as a variant for a prebuilt_apex is only created if the
1477 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001478 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001479 }
1480 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001481 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001482 if sdkDep.invalidVersion {
1483 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1484 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1485 } else if sdkDep.useFiles {
1486 // sdkDep.jar is actually equivalent to turbine header.jar.
1487 flags.classpath = append(flags.classpath, sdkDep.jars...)
1488 }
1489
1490 // Dex compilation
1491
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001492 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1493 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001494 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001495 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1496
Paul Duffin612e6102021-02-02 13:38:13 +00001497 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001498 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001499 if ctx.Failed() {
1500 return
1501 }
1502
Paul Duffin74d18d12021-05-14 14:18:47 +01001503 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001504 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001505
1506 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001507 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001508
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001509 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001510 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001511 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001512 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001513
1514 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1515 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1516 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1517 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1518 AidlIncludeDirs: j.exportAidlIncludeDirs,
1519 })
Colin Cross2fe66872015-03-30 17:20:39 -07001520}
1521
Paul Duffinaa55f742020-10-06 17:20:13 +01001522func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1523 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001524 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001525 return android.Paths{j.combinedClasspathFile}, nil
1526 default:
1527 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1528 }
1529}
1530
1531var _ android.OutputFileProducer = (*Import)(nil)
1532
Nan Zhanged19fc32017-10-19 13:06:22 -07001533func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001534 if j.combinedClasspathFile == nil {
1535 return nil
1536 }
Colin Cross37f6d792018-07-12 12:28:41 -07001537 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001538}
1539
Colin Cross331a1212018-08-15 20:40:52 -07001540func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001541 if j.combinedClasspathFile == nil {
1542 return nil
1543 }
Colin Cross331a1212018-08-15 20:40:52 -07001544 return android.Paths{j.combinedClasspathFile}
1545}
1546
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001547func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001548 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001549}
1550
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001551func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001552 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001553}
1554
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001555func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1556 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001557}
1558
Jiyong Park45bf82e2020-12-15 22:29:02 +09001559var _ android.ApexModule = (*Import)(nil)
1560
1561// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001562func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001563 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001564}
1565
Jiyong Park45bf82e2020-12-15 22:29:02 +09001566// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001567func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1568 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001569 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001570 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001571 return fmt.Errorf("min_sdk_version is not specified")
1572 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001573 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001574 return nil
1575 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001576 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1577 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001578 }
Jooyung Han749dc692020-04-15 11:03:39 +09001579 return nil
1580}
1581
Paul Duffinfef55002021-06-17 14:56:05 +01001582// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1583// java_sdk_library_import with the specified base module name requires to be exported from a
1584// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001585func requiredFilesFromPrebuiltApexForImport(name string) []string {
1586 // Add the dex implementation jar to the set of exported files.
1587 return []string{
1588 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001589 }
1590}
1591
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001592// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1593// the java library with the specified name.
1594func apexRootRelativePathToJavaLib(name string) string {
1595 return filepath.Join("javalib", name+".jar")
1596}
1597
Paul Duffinfef55002021-06-17 14:56:05 +01001598var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1599
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001600func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001601 name := j.BaseModuleName()
1602 return requiredFilesFromPrebuiltApexForImport(name)
1603}
1604
albaltai36ff7dc2018-12-25 14:35:23 +08001605// Add compile time check for interface implementation
1606var _ android.IDEInfo = (*Import)(nil)
1607var _ android.IDECustomizedModuleName = (*Import)(nil)
1608
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001609// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001610
1611func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1612 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1613}
1614
1615func (j *Import) IDECustomizedModuleName() string {
1616 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1617 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1618 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001619 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001620}
1621
Colin Cross74d73e22017-08-02 11:05:49 -07001622var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001623
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001624func (j *Import) IsInstallable() bool {
1625 return Bool(j.properties.Installable)
1626}
1627
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001628var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001629
Colin Cross1b16b0e2019-02-12 14:41:32 -08001630// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1631//
1632// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1633// compiled against an Android classpath.
1634//
1635// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1636// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001637func ImportFactory() android.Module {
1638 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001639
Liz Kammerd6c31d22020-08-05 15:40:41 -07001640 module.AddProperties(
1641 &module.properties,
1642 &module.dexer.dexProperties,
1643 )
Colin Cross74d73e22017-08-02 11:05:49 -07001644
Paul Duffin71b33cc2021-06-23 11:39:47 +01001645 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001646
Liz Kammerd6c31d22020-08-05 15:40:41 -07001647 module.dexProperties.Optimize.EnabledByDefault = false
1648
Colin Cross74d73e22017-08-02 11:05:49 -07001649 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001650 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001651 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001652 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001653 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001654}
1655
Colin Cross1b16b0e2019-02-12 14:41:32 -08001656// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1657// module.
1658//
1659// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1660// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001661func ImportFactoryHost() android.Module {
1662 module := &Import{}
1663
1664 module.AddProperties(&module.properties)
1665
1666 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001667 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001668 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001669 return module
1670}
1671
Colin Cross42be7612019-02-21 18:12:14 -08001672// dex_import module
1673
1674type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001675 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001676
1677 // set the name of the output
1678 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001679}
1680
1681type DexImport struct {
1682 android.ModuleBase
1683 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001684 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001685 prebuilt android.Prebuilt
1686
1687 properties DexImportProperties
1688
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001689 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001690
1691 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001692
1693 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001694}
1695
1696func (j *DexImport) Prebuilt() *android.Prebuilt {
1697 return &j.prebuilt
1698}
1699
1700func (j *DexImport) PrebuiltSrcs() []string {
1701 return j.properties.Jars
1702}
1703
1704func (j *DexImport) Name() string {
1705 return j.prebuilt.Name(j.ModuleBase.Name())
1706}
1707
Jiyong Park0b238752019-10-29 11:23:10 +09001708func (j *DexImport) Stem() string {
1709 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1710}
1711
Jiyong Park77acec62020-06-01 21:39:15 +09001712func (a *DexImport) JacocoReportClassesFile() android.Path {
1713 return nil
1714}
1715
Colin Cross08dca382020-07-21 20:31:17 -07001716func (a *DexImport) LintDepSets() LintDepSets {
1717 return LintDepSets{}
1718}
1719
Martin Stjernholm6d415272020-01-31 17:10:36 +00001720func (j *DexImport) IsInstallable() bool {
1721 return true
1722}
1723
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001724func (j *DexImport) getStrictUpdatabilityLinting() bool {
1725 return false
1726}
1727
1728func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1729}
1730
Colin Cross42be7612019-02-21 18:12:14 -08001731func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1732 if len(j.properties.Jars) != 1 {
1733 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1734 }
1735
Colin Cross56a83212020-09-15 18:30:11 -07001736 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1737 if !apexInfo.IsForPlatform() {
1738 j.hideApexVariantFromMake = true
1739 }
1740
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001741 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1742 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001743 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1744
1745 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1746 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1747
1748 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001749 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001750
1751 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1752 rule.Temporary(temporary)
1753
1754 // use zip2zip to uncompress classes*.dex files
1755 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001756 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001757 FlagWithInput("-i ", inputJar).
1758 FlagWithOutput("-o ", temporary).
1759 FlagWithArg("-0 ", "'classes*.dex'")
1760
1761 // use zipalign to align uncompressed classes*.dex files
1762 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001763 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001764 Flag("-f").
1765 Text("4").
1766 Input(temporary).
1767 Output(dexOutputFile)
1768
1769 rule.DeleteTemporaryFiles()
1770
Colin Crossf1a035e2020-11-16 17:32:30 -08001771 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001772 } else {
1773 ctx.Build(pctx, android.BuildParams{
1774 Rule: android.Cp,
1775 Input: inputJar,
1776 Output: dexOutputFile,
1777 })
1778 }
1779
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001780 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001781
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001782 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001783
Colin Cross56a83212020-09-15 18:30:11 -07001784 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001785 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1786 j.Stem()+".jar", dexOutputFile)
1787 }
Colin Cross42be7612019-02-21 18:12:14 -08001788}
1789
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001790func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001791 return j.dexJarFile
1792}
1793
Jiyong Park45bf82e2020-12-15 22:29:02 +09001794var _ android.ApexModule = (*DexImport)(nil)
1795
1796// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001797func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1798 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001799 // we don't check prebuilt modules for sdk_version
1800 return nil
1801}
1802
Colin Cross42be7612019-02-21 18:12:14 -08001803// dex_import imports a `.jar` file containing classes.dex files.
1804//
1805// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1806// to the device.
1807func DexImportFactory() android.Module {
1808 module := &DexImport{}
1809
1810 module.AddProperties(&module.properties)
1811
1812 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001813 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001814 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001815 return module
1816}
1817
Colin Cross89536d42017-07-07 14:35:50 -07001818//
1819// Defaults
1820//
1821type Defaults struct {
1822 android.ModuleBase
1823 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001824 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001825}
1826
Colin Cross1b16b0e2019-02-12 14:41:32 -08001827// java_defaults provides a set of properties that can be inherited by other java or android modules.
1828//
1829// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1830// property in the defaults module that exists in the depending module will be prepended to the depending module's
1831// value for that property.
1832//
1833// Example:
1834//
1835// java_defaults {
1836// name: "example_defaults",
1837// srcs: ["common/**/*.java"],
1838// javacflags: ["-Xlint:all"],
1839// aaptflags: ["--auto-add-overlay"],
1840// }
1841//
1842// java_library {
1843// name: "example",
1844// defaults: ["example_defaults"],
1845// srcs: ["example/**/*.java"],
1846// }
1847//
1848// is functionally identical to:
1849//
1850// java_library {
1851// name: "example",
1852// srcs: [
1853// "common/**/*.java",
1854// "example/**/*.java",
1855// ],
1856// javacflags: ["-Xlint:all"],
1857// }
Paul Duffin47357662019-12-05 14:07:14 +00001858func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001859 module := &Defaults{}
1860
Colin Cross89536d42017-07-07 14:35:50 -07001861 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001862 &CommonProperties{},
1863 &DeviceProperties{},
Jooyung Han01d80d82022-01-08 12:16:32 +09001864 &OverridableDeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001865 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001866 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001867 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001868 &aaptProperties{},
1869 &androidLibraryProperties{},
1870 &appProperties{},
1871 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001872 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001873 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001874 &ImportProperties{},
1875 &AARImportProperties{},
1876 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001877 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001878 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001879 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001880 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001881 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001882 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001883 )
1884
1885 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001886 return module
1887}
Nan Zhangea568a42017-11-08 21:20:04 -08001888
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001889func kytheExtractJavaFactory() android.Singleton {
1890 return &kytheExtractJavaSingleton{}
1891}
1892
1893type kytheExtractJavaSingleton struct {
1894}
1895
1896func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1897 var xrefTargets android.Paths
1898 ctx.VisitAllModules(func(module android.Module) {
1899 if javaModule, ok := module.(xref); ok {
1900 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1901 }
1902 })
1903 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1904 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001905 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001906 }
1907}
1908
Nan Zhangea568a42017-11-08 21:20:04 -08001909var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001910var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001911var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001912var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001913
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001914// Add class loader context (CLC) of a given dependency to the current CLC.
1915func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1916 clcMap dexpreopt.ClassLoaderContextMap) {
1917
1918 dep, ok := depModule.(UsesLibraryDependency)
1919 if !ok {
1920 return
1921 }
1922
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001923 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1924
1925 var sdkLib *string
1926 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1927 // A shared SDK library. This should be added as a top-level CLC element.
1928 sdkLib = &depName
1929 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1930 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1931 // property. This should be handled in the same way as a shared SDK library.
1932 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001933 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001934
1935 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001936 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001937 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001938 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1939 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1940 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1941 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001942 } else if depTag == staticLibTag {
1943 // Propagate <uses-library> through static library dependencies, unless it is a component
1944 // library (such as stubs). Component libraries have a dependency on their SDK library,
1945 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001946 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001947 return
1948 }
1949 } else {
1950 // Don't propagate <uses-library> for other dependency tags.
1951 return
1952 }
1953
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001954 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1955 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1956 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1957 // from its CLC should be added to the current CLC.
1958 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001959 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001960 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001961 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001962 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1963 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001964}
Wei Libafb6d62021-12-10 03:14:59 -08001965
1966type javaLibraryAttributes struct {
1967 Srcs bazel.LabelListAttribute
1968 Deps bazel.LabelListAttribute
1969 Javacopts bazel.StringListAttribute
1970}
1971
1972func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
1973 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
1974 attrs := &javaLibraryAttributes{
1975 Srcs: srcs,
1976 }
1977
1978 if m.properties.Javacflags != nil {
1979 attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
1980 }
1981
1982 if m.properties.Libs != nil {
1983 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.properties.Libs))
1984 }
1985
1986 props := bazel.BazelTargetModuleProperties{
1987 Rule_class: "java_library",
1988 Bzl_load_location: "//build/bazel/rules/java:library.bzl",
1989 }
1990
1991 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
1992}
1993
1994type javaBinaryHostAttributes struct {
1995 Srcs bazel.LabelListAttribute
1996 Deps bazel.LabelListAttribute
1997 Main_class string
1998 Jvm_flags bazel.StringListAttribute
Sam Delmerico4e272292022-01-06 20:03:51 +00001999 Javacopts bazel.StringListAttribute
Wei Libafb6d62021-12-10 03:14:59 -08002000}
2001
2002// JavaBinaryHostBp2Build is for java_binary_host bp2build.
2003func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
2004 mainClass := ""
2005 if m.binaryProperties.Main_class != nil {
2006 mainClass = *m.binaryProperties.Main_class
2007 }
2008 if m.properties.Manifest != nil {
2009 mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String())
2010 if err != nil {
2011 return
2012 }
2013 mainClass = mainClassInManifest
2014 }
2015 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
2016 attrs := &javaBinaryHostAttributes{
2017 Srcs: srcs,
2018 Main_class: mainClass,
2019 }
2020
Sam Delmerico4e272292022-01-06 20:03:51 +00002021 if m.properties.Javacflags != nil {
2022 attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
2023 }
2024
Wei Libafb6d62021-12-10 03:14:59 -08002025 // Attribute deps
2026 deps := []string{}
2027 if m.properties.Static_libs != nil {
2028 deps = append(deps, m.properties.Static_libs...)
2029 }
2030 if m.binaryProperties.Jni_libs != nil {
2031 deps = append(deps, m.binaryProperties.Jni_libs...)
2032 }
2033 if len(deps) > 0 {
2034 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, deps))
2035 }
2036
2037 // Attribute jvm_flags
2038 if m.binaryProperties.Jni_libs != nil {
2039 jniLibPackages := map[string]bool{}
2040 for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes {
2041 jniLibPackage := jniLibLabel.Label
2042 indexOfColon := strings.Index(jniLibLabel.Label, ":")
2043 if indexOfColon > 0 {
2044 // JNI lib from other package
2045 jniLibPackage = jniLibLabel.Label[2:indexOfColon]
2046 } else if indexOfColon == 0 {
2047 // JNI lib in the same package of java_binary
2048 packageOfCurrentModule := m.GetBazelLabel(ctx, m)
2049 jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")]
2050 }
2051 if _, inMap := jniLibPackages[jniLibPackage]; !inMap {
2052 jniLibPackages[jniLibPackage] = true
2053 }
2054 }
2055 jniLibPaths := []string{}
2056 for jniLibPackage, _ := range jniLibPackages {
2057 // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH
2058 jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage)
2059 }
2060 attrs.Jvm_flags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")})
2061 }
2062
2063 props := bazel.BazelTargetModuleProperties{
2064 Rule_class: "java_binary",
2065 }
2066
2067 // Create the BazelTargetModule.
2068 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
2069}