blob: 9b4a005f0437bb1f6d4897f67cfa143f204bdcd2 [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"
Colin Cross2fe66872015-03-30 17:20:39 -070027 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080031 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010032 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070033 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070034 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070035)
36
Colin Cross463a90e2015-06-17 14:20:06 -070037func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010038 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000039
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080040 RegisterJavaSdkMemberTypes()
41}
42
Paul Duffin535e0a12021-03-30 23:34:32 +010043func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080044 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
45
46 ctx.RegisterModuleType("java_library", LibraryFactory)
47 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
48 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
49 ctx.RegisterModuleType("java_binary", BinaryFactory)
50 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
51 ctx.RegisterModuleType("java_test", TestFactory)
52 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
53 ctx.RegisterModuleType("java_test_host", TestHostFactory)
54 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
55 ctx.RegisterModuleType("java_import", ImportFactory)
56 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
57 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
58 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
59 ctx.RegisterModuleType("dex_import", DexImportFactory)
60
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +010061 // This mutator registers dependencies on dex2oat for modules that should be
62 // dexpreopted. This is done late when the final variants have been
63 // established, to not get the dependencies split into the wrong variants and
64 // to support the checks in dexpreoptDisabled().
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080065 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
66 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
67 })
68
69 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
70 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
71}
72
73func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000074 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000075 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010076 android.RegisterSdkMemberType(javaLibsSdkMemberType)
77 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
Jiakai Zhangea180332021-09-26 08:58:02 +000078 android.RegisterSdkMemberType(javaSystemserverLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010079 android.RegisterSdkMemberType(javaTestSdkMemberType)
80}
81
82var (
83 // Supports adding java header libraries to module_exports and sdk.
84 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
85 android.SdkMemberTypeBase{
86 PropertyName: "java_header_libs",
87 SupportsSdk: true,
88 },
89 func(_ android.SdkMemberContext, j *Library) android.Path {
90 headerJars := j.HeaderJars()
91 if len(headerJars) != 1 {
92 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
93 }
94
95 return headerJars[0]
96 },
97 sdkSnapshotFilePathForJar,
98 copyEverythingToSnapshot,
99 }
Paul Duffin255f18e2019-12-13 11:22:16 +0000100
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000101 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +0100102 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000103 implementationJars := j.ImplementationAndResourcesJars()
104 if len(implementationJars) != 1 {
105 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
106 }
107 return implementationJars[0]
108 }
109
Paul Duffin2da04242021-04-23 19:43:28 +0100110 // Supports adding java implementation libraries to module_exports but not sdk.
111 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000112 android.SdkMemberTypeBase{
113 PropertyName: "java_libs",
114 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000115 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000116 sdkSnapshotFilePathForJar,
117 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100118 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000119
Paul Duffin2da04242021-04-23 19:43:28 +0100120 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000121 //
122 // The build has some implicit dependencies (via the boot jars configuration) on a number of
123 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
124 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
125 // used outside those mainline modules.
126 //
127 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
128 // either java_libs, or java_header_libs would end up exporting more information than was strictly
129 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
130 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100131 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000132 android.SdkMemberTypeBase{
133 PropertyName: "java_boot_libs",
134 SupportsSdk: true,
135 },
Paul Duffin5c211452021-07-15 12:42:44 +0100136 func(ctx android.SdkMemberContext, j *Library) android.Path {
137 // Java boot libs are only provided in the SDK to provide access to their dex implementation
138 // jar for use by dexpreopting and boot jars package check. They do not need to provide an
139 // actual implementation jar but the java_import will need a file that exists so just copy an
140 // empty file. Any attempt to use that file as a jar will cause a build error.
141 return ctx.SnapshotBuilder().EmptyFile()
142 },
143 func(osPrefix, name string) string {
144 // Create a special name for the implementation jar to try and provide some useful information
145 // to a developer that attempts to compile against this.
146 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
147 return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
148 },
Paul Duffindb170e42020-12-08 17:48:25 +0000149 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100150 }
Paul Duffindb170e42020-12-08 17:48:25 +0000151
Jiakai Zhangea180332021-09-26 08:58:02 +0000152 // Supports adding java systemserver libraries to module_exports and sdk.
153 //
154 // The build has some implicit dependencies (via the systemserver jars configuration) on a number
155 // of modules that are part of the java systemserver classpath and which are provided by mainline
156 // modules but which are not otherwise used outside those mainline modules.
157 //
158 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
159 // either java_libs, or java_header_libs would end up exporting more information than was strictly
160 // necessary. The java_systemserver_libs property to allow those modules to be exported as part of
161 // the sdk/module_exports without exposing any unnecessary information.
162 javaSystemserverLibsSdkMemberType = &librarySdkMemberType{
163 android.SdkMemberTypeBase{
164 PropertyName: "java_systemserver_libs",
165 SupportsSdk: true,
166 },
167 func(ctx android.SdkMemberContext, j *Library) android.Path {
168 // Java systemserver libs are only provided in the SDK to provide access to their dex
169 // implementation jar for use by dexpreopting. They do not need to provide an actual
170 // implementation jar but the java_import will need a file that exists so just copy an empty
171 // file. Any attempt to use that file as a jar will cause a build error.
172 return ctx.SnapshotBuilder().EmptyFile()
173 },
174 func(osPrefix, name string) string {
175 // Create a special name for the implementation jar to try and provide some useful information
176 // to a developer that attempts to compile against this.
177 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
178 return filepath.Join(osPrefix, "java_systemserver_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
179 },
180 onlyCopyJarToSnapshot,
181 }
182
Paul Duffin2da04242021-04-23 19:43:28 +0100183 // Supports adding java test libraries to module_exports but not sdk.
184 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000185 SdkMemberTypeBase: android.SdkMemberTypeBase{
186 PropertyName: "java_tests",
187 },
Paul Duffin2da04242021-04-23 19:43:28 +0100188 }
189)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900190
Colin Crossdcf71b22021-02-01 13:59:03 -0800191// JavaInfo contains information about a java module for use by modules that depend on it.
192type JavaInfo struct {
193 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
194 // against this module. If empty, ImplementationJars should be used instead.
195 HeaderJars android.Paths
196
197 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
198 // in the module as well as any resources included in the module.
199 ImplementationAndResourcesJars android.Paths
200
201 // ImplementationJars is a list of jars that contain the implementations of classes in the
202 //module.
203 ImplementationJars android.Paths
204
205 // ResourceJars is a list of jars that contain the resources included in the module.
206 ResourceJars android.Paths
207
208 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
209 // depending on this module.
210 AidlIncludeDirs android.Paths
211
212 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
213 // module.
214 SrcJarArgs []string
215
216 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
217 SrcJarDeps android.Paths
218
219 // ExportedPlugins is a list of paths that should be used as annotation processors for any
220 // module that depends on this module.
221 ExportedPlugins android.Paths
222
223 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
224 // any module that depends on this module.
225 ExportedPluginClasses []string
226
227 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
228 // requiring disbling turbine for any modules that depend on it.
229 ExportedPluginDisableTurbine bool
230
231 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
232 // instrumented by jacoco.
233 JacocoReportClassesFile android.Path
234}
235
236var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
237
Colin Cross75ce9ec2021-02-26 16:20:32 -0800238// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
239// the sysprop implementation library.
240type SyspropPublicStubInfo struct {
241 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
242 // the sysprop implementation library.
243 JavaInfo JavaInfo
244}
245
246var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
247
Paul Duffin44b481b2020-06-17 16:59:43 +0100248// Methods that need to be implemented for a module that is added to apex java_libs property.
249type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700250 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100251 ImplementationAndResourcesJars() android.Paths
252}
253
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100254// Provides build path and install path to DEX jars.
255type UsesLibraryDependency interface {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100256 DexJarBuildPath() OptionalDexJarPath
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100257 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000258 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100259}
260
Jaewoong Jung26342642021-03-17 15:56:23 -0700261// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800262type xref interface {
263 XrefJavaFiles() android.Paths
264}
265
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800266func (j *Module) XrefJavaFiles() android.Paths {
267 return j.kytheFiles
268}
269
Colin Crossbe1da472017-07-07 15:59:46 -0700270type dependencyTag struct {
271 blueprint.BaseDependencyTag
272 name string
Colin Cross65cb3142021-12-10 23:05:02 +0000273
274 // True if the dependency is relinked at runtime.
275 runtimeLinked bool
Colin Cross2fe66872015-03-30 17:20:39 -0700276}
277
Colin Crosse9fe2942020-11-10 18:12:15 -0800278// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
279// dependency to be installed when the parent module is installed.
280type installDependencyTag struct {
281 blueprint.BaseDependencyTag
282 android.InstallAlwaysNeededDependencyTag
283 name string
284}
285
Colin Cross65cb3142021-12-10 23:05:02 +0000286func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
287 if d.runtimeLinked {
288 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
289 }
290 return nil
291}
292
293var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
294
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100295type usesLibraryDependencyTag struct {
296 dependencyTag
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100297
298 // SDK version in which the library appared as a standalone library.
299 sdkVersion int
300
301 // If the dependency is optional or required.
302 optional bool
303
304 // Whether this is an implicit dependency inferred by Soong, or an explicit one added via
305 // `uses_libs`/`optional_uses_libs` properties.
306 implicit bool
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100307}
308
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100309func makeUsesLibraryDependencyTag(sdkVersion int, optional bool, implicit bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100310 return usesLibraryDependencyTag{
Colin Cross65cb3142021-12-10 23:05:02 +0000311 dependencyTag: dependencyTag{
312 name: fmt.Sprintf("uses-library-%d", sdkVersion),
313 runtimeLinked: true,
314 },
315 sdkVersion: sdkVersion,
316 optional: optional,
317 implicit: implicit,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100318 }
319}
320
Jiyong Park8be103b2019-11-08 15:53:48 +0900321func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700322 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900323}
324
Colin Crossbe1da472017-07-07 15:59:46 -0700325var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800326 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
327 staticLibTag = dependencyTag{name: "staticlib"}
Colin Cross65cb3142021-12-10 23:05:02 +0000328 libTag = dependencyTag{name: "javalib", runtimeLinked: true}
329 java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800330 pluginTag = dependencyTag{name: "plugin"}
331 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
332 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross65cb3142021-12-10 23:05:02 +0000333 bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true}
334 systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800335 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross65cb3142021-12-10 23:05:02 +0000336 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true}
337 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700338 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800339 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
340 certificateTag = dependencyTag{name: "certificate"}
341 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
342 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
Colin Cross65cb3142021-12-10 23:05:02 +0000343 jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800344 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
345 jniInstallTag = installDependencyTag{name: "jni install"}
346 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700347)
Colin Cross2fe66872015-03-30 17:20:39 -0700348
Jiyong Park83dc74b2020-01-14 18:38:44 +0900349func IsLibDepTag(depTag blueprint.DependencyTag) bool {
350 return depTag == libTag
351}
352
353func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
354 return depTag == staticLibTag
355}
356
Colin Crossfc3674a2017-09-18 17:41:52 -0700357type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100358 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700359
Colin Cross6cef4812019-10-17 14:23:50 -0700360 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
361 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100362
363 // The default system modules to use. Will be an empty string if no system
364 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700365 systemModules string
366
Pete Gilline3d44b22020-06-29 11:28:51 +0100367 // The modules that will be added to the classpath regardless of the Java language level targeted
368 classpath []string
369
Colin Cross6cef4812019-10-17 14:23:50 -0700370 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100371 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700372 java9Classpath []string
373
Colin Crossa97c5d32018-03-28 14:58:31 -0700374 frameworkResModule string
375
Colin Cross86a60ae2018-05-29 14:44:55 -0700376 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700377 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100378
379 noStandardLibs, noFrameworksLibs bool
380}
381
382func (s sdkDep) hasStandardLibs() bool {
383 return !s.noStandardLibs
384}
385
386func (s sdkDep) hasFrameworkLibs() bool {
387 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700388}
389
Colin Crossa4f08812018-10-02 22:03:40 -0700390type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700391 name string
392 path android.Path
393 target android.Target
394 coverageFile android.OptionalPath
395 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700396}
397
Jiyong Parkf1691d22021-03-29 20:11:58 +0900398func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700399 sdkDep := decodeSdkDep(ctx, sdkContext)
400 if sdkDep.useModule {
401 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
402 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
403 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
404 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
405 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
406 }
407 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
408 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
409 }
410 }
411 if sdkDep.systemModules != "" {
412 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
413 }
414}
415
Colin Cross32f676a2017-09-06 13:41:06 -0700416type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800417 classpath classpath
418 java9Classpath classpath
419 bootClasspath classpath
420 processorPath classpath
421 errorProneProcessorPath classpath
422 processorClasses []string
423 staticJars android.Paths
424 staticHeaderJars android.Paths
425 staticResourceJars android.Paths
426 aidlIncludeDirs android.Paths
427 srcs android.Paths
428 srcJars android.Paths
429 systemModules *systemModules
430 aidlPreprocess android.OptionalPath
431 kotlinStdlib android.Paths
432 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700433 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800434
435 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700436}
Colin Cross2fe66872015-03-30 17:20:39 -0700437
Colin Cross54250902017-12-05 09:28:08 -0800438func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
439 for _, f := range dep.Srcs() {
440 if f.Ext() != ".jar" {
441 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
442 ctx.OtherModuleName(dep.(blueprint.Module)))
443 }
444 }
445}
446
Jiyong Parkf1691d22021-03-29 20:11:58 +0900447func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700448 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700449 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700450 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900451 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Sorin Basca9347ae32021-12-20 11:51:24 +0000452 } else if ctx.Config().TargetsJava11() {
Sorin Basca8ef3e6f2021-11-26 17:27:24 +0000453 // Temporary experimental flag to be able to try and build with
454 // java version 11 options. The flag, if used, just sets Java
455 // 11 as the default version, leaving any components that
456 // target an older version intact.
457 return JAVA_VERSION_11
Nan Zhang357466b2018-04-17 17:38:36 -0700458 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700459 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700460 }
Nan Zhang357466b2018-04-17 17:38:36 -0700461}
462
Colin Cross1e743852019-10-28 11:37:20 -0700463type javaVersion int
464
465const (
466 JAVA_VERSION_UNSUPPORTED = 0
467 JAVA_VERSION_6 = 6
468 JAVA_VERSION_7 = 7
469 JAVA_VERSION_8 = 8
470 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000471 JAVA_VERSION_11 = 11
Colin Cross1e743852019-10-28 11:37:20 -0700472)
473
474func (v javaVersion) String() string {
475 switch v {
476 case JAVA_VERSION_6:
477 return "1.6"
478 case JAVA_VERSION_7:
479 return "1.7"
480 case JAVA_VERSION_8:
481 return "1.8"
482 case JAVA_VERSION_9:
483 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000484 case JAVA_VERSION_11:
485 return "11"
Colin Cross1e743852019-10-28 11:37:20 -0700486 default:
487 return "unsupported"
488 }
489}
490
491// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
492func (v javaVersion) usesJavaModules() bool {
493 return v >= 9
494}
495
496func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100497 switch javaVersion {
498 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700499 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100500 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700501 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100502 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700503 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100504 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700505 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000506 case "11":
507 return JAVA_VERSION_11
508 case "10":
509 ctx.PropertyErrorf("java_version", "Java language levels 10 is not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700510 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100511 default:
512 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700513 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100514 }
515}
516
Colin Cross2fe66872015-03-30 17:20:39 -0700517//
518// Java libraries (.jar file)
519//
520
Colin Crossf506d872017-07-19 15:53:04 -0700521type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700522 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700523
524 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700525}
526
Jiyong Park45bf82e2020-12-15 22:29:02 +0900527var _ android.ApexModule = (*Library)(nil)
528
satayevd604b212021-07-21 14:23:52 +0100529// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100530type PermittedPackagesForUpdatableBootJars interface {
531 PermittedPackagesForUpdatableBootJars() []string
532}
533
534var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
535
536func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
537 return j.properties.Permitted_packages
538}
539
Colin Cross42be7612019-02-21 18:12:14 -0800540func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000541 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700542 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000543 return true
544 }
545
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000546 // Store uncompressed (and do not strip) dex files from boot class path jars.
547 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
548 return true
549 }
550
551 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000552 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000553 return true
554 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800555 if ctx.Config().UncompressPrivAppDex() &&
556 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
557 return true
558 }
559
Colin Cross2fc72f62018-12-21 12:59:54 -0800560 return false
561}
562
Jiakai Zhang22450f22021-10-11 03:05:20 +0000563// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
564func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
565 if dexer.dexProperties.Uncompress_dex == nil {
566 // If the value was not force-set by the user, use reasonable default based on the module.
567 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
568 }
569}
570
Colin Crossf506d872017-07-19 15:53:04 -0700571func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900572 j.sdkVersion = j.SdkVersion(ctx)
573 j.minSdkVersion = j.MinSdkVersion(ctx)
satayev0a420e72021-11-29 17:25:52 +0000574 j.maxSdkVersion = j.MaxSdkVersion(ctx)
Jiyong Park92315372021-04-02 08:45:46 +0900575
Colin Cross56a83212020-09-15 18:30:11 -0700576 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
577 if !apexInfo.IsForPlatform() {
578 j.hideApexVariantFromMake = true
579 }
580
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100581 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000582 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
583 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800584 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000585 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700586 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100587 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700588 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700589
bralee1fbf4402020-05-21 10:11:59 +0800590 // Collect the module directory for IDE info in java/jdeps.go.
591 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
592
Colin Cross56a83212020-09-15 18:30:11 -0700593 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900594 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700595 var extraInstallDeps android.Paths
596 if j.InstallMixin != nil {
597 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
598 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700599 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
600 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800601 j.hostdexInstallFile = ctx.InstallFile(
602 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700603 j.Stem()+"-hostdex.jar", j.outputFile)
604 }
605 var installDir android.InstallPath
606 if ctx.InstallInTestcases() {
607 var archDir string
608 if !ctx.Host() {
609 archDir = ctx.DeviceConfig().DeviceArch()
610 }
611 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
612 } else {
613 installDir = android.PathForModuleInstall(ctx, "framework")
614 }
615 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700616 }
Colin Crossb7a63242015-04-16 14:09:14 -0700617}
618
Colin Crossf506d872017-07-19 15:53:04 -0700619func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700620 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100621 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700622}
623
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000624const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000625 aidlIncludeDir = "aidl"
626 javaDir = "java"
627 jarFileSuffix = ".jar"
628 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000629)
630
Paul Duffina0dbf432019-12-05 11:25:53 +0000631// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000632func sdkSnapshotFilePathForJar(osPrefix, name string) string {
633 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000634}
635
Paul Duffina04c1072020-03-02 10:16:35 +0000636func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
637 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000638}
639
Paul Duffin13879572019-11-28 14:31:38 +0000640type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000641 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000642
643 // Function to retrieve the appropriate output jar (implementation or header) from
644 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000645 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
646
647 // Function to compute the snapshot relative path to which the named library's
648 // jar should be copied.
649 snapshotPathGetter func(osPrefix, name string) string
650
651 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
652 // files like aidl files should also be copied.
653 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000654}
655
Paul Duffindb170e42020-12-08 17:48:25 +0000656const (
657 onlyCopyJarToSnapshot = true
658 copyEverythingToSnapshot = false
659)
660
Paul Duffin296701e2021-07-14 10:29:36 +0100661func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
662 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000663}
664
665func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
666 _, ok := module.(*Library)
667 return ok
668}
669
Paul Duffin3a4eb502020-03-19 16:11:18 +0000670func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
671 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000672}
Paul Duffina0dbf432019-12-05 11:25:53 +0000673
Paul Duffin14eb4672020-03-02 11:33:02 +0000674func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000675 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000676}
677
678type librarySdkMemberProperties struct {
679 android.SdkMemberPropertiesBase
680
Paul Duffin864e1b42020-05-06 10:23:19 +0100681 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000682 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100683
684 // The list of permitted packages that need to be passed to the prebuilts as they are used to
685 // create the updatable-bcp-packages.txt file.
686 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000687}
688
Paul Duffin3a4eb502020-03-19 16:11:18 +0000689func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000690 j := variant.(*Library)
691
Paul Duffindb170e42020-12-08 17:48:25 +0000692 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
693
Paul Duffina551a1c2020-03-17 21:04:24 +0000694 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100695
696 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000697}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000698
Paul Duffin3a4eb502020-03-19 16:11:18 +0000699func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000700 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000701
Paul Duffindb170e42020-12-08 17:48:25 +0000702 memberType := ctx.MemberType().(*librarySdkMemberType)
703
Paul Duffina551a1c2020-03-17 21:04:24 +0000704 exportedJar := p.JarToExport
705 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000706 // Delegate the creation of the snapshot relative path to the member type.
707 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
708
709 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000710 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
711
Paul Duffina551a1c2020-03-17 21:04:24 +0000712 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
713 }
714
Paul Duffin869de142021-07-15 14:14:41 +0100715 if len(p.PermittedPackages) > 0 {
716 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
717 }
718
Paul Duffindb170e42020-12-08 17:48:25 +0000719 // Do not copy anything else to the snapshot.
720 if memberType.onlyCopyJarToSnapshot {
721 return
722 }
723
Paul Duffina551a1c2020-03-17 21:04:24 +0000724 aidlIncludeDirs := p.AidlIncludeDirs
725 if len(aidlIncludeDirs) != 0 {
726 sdkModuleContext := ctx.SdkModuleContext()
727 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000728 // TODO(jiyong): copy parcelable declarations only
729 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
730 for _, file := range aidlFiles {
731 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
732 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000733 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000734
Paul Duffina551a1c2020-03-17 21:04:24 +0000735 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000736 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000737}
738
Colin Cross1b16b0e2019-02-12 14:41:32 -0800739// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
740//
741// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
742// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
743// as a `static_libs` dependency of another module.
744//
745// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
746// a device.
747//
748// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
749// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700750func LibraryFactory() android.Module {
751 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700752
Colin Crossce6734e2020-06-15 16:09:53 -0700753 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700754
Paul Duffin71b33cc2021-06-23 11:39:47 +0100755 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100756
Jiyong Park7f7766d2019-07-25 22:02:35 +0900757 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900758 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800759 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900760 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700761 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700762}
763
Colin Cross1b16b0e2019-02-12 14:41:32 -0800764// java_library_static is an obsolete alias for java_library.
765func LibraryStaticFactory() android.Module {
766 return LibraryFactory()
767}
768
769// java_library_host builds and links sources into a `.jar` file for the host.
770//
771// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
772// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700773func LibraryHostFactory() android.Module {
774 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700775
Colin Crossce6734e2020-06-15 16:09:53 -0700776 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700777
Colin Cross9ae1b922018-06-26 17:59:05 -0700778 module.Module.properties.Installable = proptools.BoolPtr(true)
779
Jiyong Park7f7766d2019-07-25 22:02:35 +0900780 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100781 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800782 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900783 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700784 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700785}
786
787//
Colin Crossb628ea52018-08-14 16:42:33 -0700788// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700789//
790
Dan Shi95d19422020-08-15 12:24:26 -0700791// Test option struct.
792type TestOptions struct {
793 // a list of extra test configuration files that should be installed with the module.
794 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800795
796 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
797 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700798}
799
Colin Cross05638fc2018-04-09 18:40:24 -0700800type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700801 // list of compatibility suites (for example "cts", "vts") that the module should be
802 // installed into.
803 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700804
805 // the name of the test configuration (for example "AndroidTest.xml") that should be
806 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800807 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700808
Jack He33338892018-09-19 02:21:28 -0700809 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
810 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800811 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700812
Colin Crossd96ca352018-08-10 16:06:24 -0700813 // list of files or filegroup modules that provide data that should be installed alongside
814 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900815 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700816
817 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
818 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
819 // explicitly.
820 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800821
822 // Add parameterized mainline modules to auto generated test config. The options will be
823 // handled by TradeFed to do downloading and installing the specified modules on the device.
824 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700825
826 // Test options.
827 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800828
829 // Names of modules containing JNI libraries that should be installed alongside the test.
830 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700831
832 // Install the test into a folder named for the module in all test suites.
833 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700834}
835
Liz Kammerdd849a82020-06-12 16:38:45 -0700836type hostTestProperties struct {
837 // list of native binary modules that should be installed alongside the test
838 Data_native_bins []string `android:"arch_variant"`
839}
840
Paul Duffin42df1442019-03-20 12:45:53 +0000841type testHelperLibraryProperties struct {
842 // list of compatibility suites (for example "cts", "vts") that the module should be
843 // installed into.
844 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700845
846 // Install the test into a folder named for the module in all test suites.
847 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000848}
849
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000850type prebuiltTestProperties struct {
851 // list of compatibility suites (for example "cts", "vts") that the module should be
852 // installed into.
853 Test_suites []string `android:"arch_variant"`
854
855 // the name of the test configuration (for example "AndroidTest.xml") that should be
856 // installed with the module.
857 Test_config *string `android:"path,arch_variant"`
858}
859
Colin Cross05638fc2018-04-09 18:40:24 -0700860type Test struct {
861 Library
862
863 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700864
Dan Shi95d19422020-08-15 12:24:26 -0700865 testConfig android.Path
866 extraTestConfigs android.Paths
867 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700868}
869
Liz Kammerdd849a82020-06-12 16:38:45 -0700870type TestHost struct {
871 Test
872
873 testHostProperties hostTestProperties
874}
875
Paul Duffin42df1442019-03-20 12:45:53 +0000876type TestHelperLibrary struct {
877 Library
878
879 testHelperLibraryProperties testHelperLibraryProperties
880}
881
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000882type JavaTestImport struct {
883 Import
884
885 prebuiltTestProperties prebuiltTestProperties
886
887 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700888 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000889}
890
Colin Cross24cc4be62021-11-03 14:09:41 -0700891func (j *Test) InstallInTestcases() bool {
892 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
893 // testcases by base_rules.mk.
894 return !j.Host()
895}
896
897func (j *TestHelperLibrary) InstallInTestcases() bool {
898 return true
899}
900
901func (j *JavaTestImport) InstallInTestcases() bool {
902 return true
903}
904
Liz Kammerdd849a82020-06-12 16:38:45 -0700905func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
906 if len(j.testHostProperties.Data_native_bins) > 0 {
907 for _, target := range ctx.MultiTargets() {
908 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
909 }
910 }
911
Colin Crossf8d9c492021-01-26 11:01:43 -0800912 if len(j.testProperties.Jni_libs) > 0 {
913 for _, target := range ctx.MultiTargets() {
914 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
915 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
916 }
917 }
918
Liz Kammerdd849a82020-06-12 16:38:45 -0700919 j.deps(ctx)
920}
921
Yuexi Ma627263f2021-03-04 13:47:56 -0800922func (j *TestHost) AddExtraResource(p android.Path) {
923 j.extraResources = append(j.extraResources, p)
924}
925
Colin Cross303e21f2018-08-07 16:49:25 -0700926func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000927 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
928 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700929 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000930 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
931 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700932 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800933 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700934
Colin Cross8a497952019-03-05 22:25:09 -0800935 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700936
Dan Shi95d19422020-08-15 12:24:26 -0700937 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
938
Liz Kammerdd849a82020-06-12 16:38:45 -0700939 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
940 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
941 })
942
Colin Crossf8d9c492021-01-26 11:01:43 -0800943 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
944 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
945 if sharedLibInfo.SharedLibrary != nil {
946 // Copy to an intermediate output directory to append "lib[64]" to the path,
947 // so that it's compatible with the default rpath values.
948 var relPath string
949 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
950 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
951 } else {
952 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
953 }
954 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
955 ctx.Build(pctx, android.BuildParams{
956 Rule: android.Cp,
957 Input: sharedLibInfo.SharedLibrary,
958 Output: relocatedLib,
959 })
960 j.data = append(j.data, relocatedLib)
961 } else {
962 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
963 }
964 })
965
Colin Cross303e21f2018-08-07 16:49:25 -0700966 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700967}
968
Paul Duffin42df1442019-03-20 12:45:53 +0000969func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
970 j.Library.GenerateAndroidBuildActions(ctx)
971}
972
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000973func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
974 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800975 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000976
977 j.Import.GenerateAndroidBuildActions(ctx)
978}
979
980type testSdkMemberType struct {
981 android.SdkMemberTypeBase
982}
983
Paul Duffin296701e2021-07-14 10:29:36 +0100984func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
985 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000986}
987
988func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
989 _, ok := module.(*Test)
990 return ok
991}
992
Paul Duffin3a4eb502020-03-19 16:11:18 +0000993func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
994 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000995}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000996
Paul Duffin14eb4672020-03-02 11:33:02 +0000997func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
998 return &testSdkMemberProperties{}
999}
1000
1001type testSdkMemberProperties struct {
1002 android.SdkMemberPropertiesBase
1003
Paul Duffina551a1c2020-03-17 21:04:24 +00001004 JarToExport android.Path
1005 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00001006}
1007
Paul Duffin3a4eb502020-03-19 16:11:18 +00001008func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00001009 test := variant.(*Test)
1010
1011 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001012 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00001013 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001014 }
1015
Paul Duffina551a1c2020-03-17 21:04:24 +00001016 p.JarToExport = implementationJars[0]
1017 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00001018}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001019
Paul Duffin3a4eb502020-03-19 16:11:18 +00001020func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001021 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001022
Paul Duffina551a1c2020-03-17 21:04:24 +00001023 exportedJar := p.JarToExport
1024 if exportedJar != nil {
1025 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
1026 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001027
1028 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001029 }
1030
1031 testConfig := p.TestConfig
1032 if testConfig != nil {
1033 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1034 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001035 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1036 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001037}
1038
Colin Cross1b16b0e2019-02-12 14:41:32 -08001039// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1040// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1041//
1042// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1043// compiled against the device bootclasspath.
1044//
1045// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1046// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001047func TestFactory() android.Module {
1048 module := &Test{}
1049
Colin Crossce6734e2020-06-15 16:09:53 -07001050 module.addHostAndDeviceProperties()
1051 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001052
Colin Cross9ae1b922018-06-26 17:59:05 -07001053 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001054 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001055 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001056
Paul Duffinb6b89a42021-05-06 16:33:43 +01001057 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001058 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001059 return module
1060}
1061
Paul Duffin42df1442019-03-20 12:45:53 +00001062// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1063func TestHelperLibraryFactory() android.Module {
1064 module := &TestHelperLibrary{}
1065
Colin Crossce6734e2020-06-15 16:09:53 -07001066 module.addHostAndDeviceProperties()
1067 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001068
Colin Cross9a4abed2019-04-24 13:19:28 -07001069 module.Module.properties.Installable = proptools.BoolPtr(true)
1070 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001071 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001072
Paul Duffin42df1442019-03-20 12:45:53 +00001073 InitJavaModule(module, android.HostAndDeviceSupported)
1074 return module
1075}
1076
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001077// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1078// and makes sure that it is added to the appropriate test suite.
1079//
1080// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1081// compiled against an Android classpath.
1082//
1083// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1084// for host modules.
1085func JavaTestImportFactory() android.Module {
1086 module := &JavaTestImport{}
1087
1088 module.AddProperties(
1089 &module.Import.properties,
1090 &module.prebuiltTestProperties)
1091
1092 module.Import.properties.Installable = proptools.BoolPtr(true)
1093
1094 android.InitPrebuiltModule(module, &module.properties.Jars)
1095 android.InitApexModule(module)
1096 android.InitSdkAwareModule(module)
1097 InitJavaModule(module, android.HostAndDeviceSupported)
1098 return module
1099}
1100
Colin Cross1b16b0e2019-02-12 14:41:32 -08001101// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1102// allow running the test with `atest` or a `TEST_MAPPING` file.
1103//
1104// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1105// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001106func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001107 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001108
Colin Crossce6734e2020-06-15 16:09:53 -07001109 module.addHostProperties()
1110 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001111 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001112
Yuexi Ma627263f2021-03-04 13:47:56 -08001113 InitTestHost(
1114 module,
1115 proptools.BoolPtr(true),
1116 nil,
1117 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001118
Liz Kammerdd849a82020-06-12 16:38:45 -07001119 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001120
Colin Cross05638fc2018-04-09 18:40:24 -07001121 return module
1122}
1123
Yuexi Ma627263f2021-03-04 13:47:56 -08001124func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1125 th.properties.Installable = installable
1126 th.testProperties.Auto_gen_config = autoGenConfig
1127 th.testProperties.Test_suites = testSuites
1128}
1129
Colin Cross05638fc2018-04-09 18:40:24 -07001130//
Colin Cross2fe66872015-03-30 17:20:39 -07001131// Java Binaries (.jar file plus wrapper script)
1132//
1133
Colin Crossf506d872017-07-19 15:53:04 -07001134type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001135 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001136 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001137
1138 // Name of the class containing main to be inserted into the manifest as Main-Class.
1139 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001140
1141 // Names of modules containing JNI libraries that should be installed alongside the host
1142 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001143 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001144}
1145
Colin Crossf506d872017-07-19 15:53:04 -07001146type Binary struct {
1147 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001148
Colin Crossf506d872017-07-19 15:53:04 -07001149 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001150
Colin Cross6b4a32d2017-12-05 13:42:45 -08001151 isWrapperVariant bool
1152
Colin Crossc3315992017-12-08 19:12:36 -08001153 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001154 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001155}
1156
Alex Light24237172017-10-26 09:46:21 -07001157func (j *Binary) HostToolPath() android.OptionalPath {
1158 return android.OptionalPathForPath(j.binaryFile)
1159}
1160
Colin Crossf506d872017-07-19 15:53:04 -07001161func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001162 if ctx.Arch().ArchType == android.Common {
1163 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001164 if j.binaryProperties.Main_class != nil {
1165 if j.properties.Manifest != nil {
1166 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1167 }
1168 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1169 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1170 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1171 }
1172
Colin Cross6b4a32d2017-12-05 13:42:45 -08001173 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001174 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001175 // Handle the binary wrapper
1176 j.isWrapperVariant = true
1177
Colin Cross366938f2017-12-11 16:29:02 -08001178 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001179 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001180 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001181 if ctx.Windows() {
1182 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1183 }
1184
Colin Cross6b4a32d2017-12-05 13:42:45 -08001185 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1186 }
1187
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001188 ext := ""
1189 if ctx.Windows() {
1190 ext = ".bat"
1191 }
1192
Colin Crossc179ea62020-10-09 10:54:15 -07001193 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001194 // of the wrapper variant, which will include the common variant's jar file and any JNI
1195 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001196 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001197 ctx.ModuleName()+ext, j.wrapperFile)
1198 }
Colin Cross2fe66872015-03-30 17:20:39 -07001199}
1200
Colin Crossf506d872017-07-19 15:53:04 -07001201func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001202 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001203 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001204 }
1205 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001206 // These dependencies ensure the host installation rules will install the jar file and
1207 // the jni libraries when the wrapper is installed.
1208 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1209 ctx.AddVariationDependencies(
1210 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1211 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001212 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001213}
1214
Colin Cross1b16b0e2019-02-12 14:41:32 -08001215// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1216// as well.
1217//
1218// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1219// compiled against the device bootclasspath.
1220//
1221// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1222// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001223func BinaryFactory() android.Module {
1224 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001225
Colin Crossce6734e2020-06-15 16:09:53 -07001226 module.addHostAndDeviceProperties()
1227 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001228
Colin Cross9ae1b922018-06-26 17:59:05 -07001229 module.Module.properties.Installable = proptools.BoolPtr(true)
1230
Colin Cross6b4a32d2017-12-05 13:42:45 -08001231 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1232 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001233 android.InitBazelModule(module)
1234
Colin Cross36242852017-06-23 15:06:31 -07001235 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001236}
1237
Colin Cross1b16b0e2019-02-12 14:41:32 -08001238// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1239//
1240// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1241// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001242func BinaryHostFactory() android.Module {
1243 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001244
Colin Crossce6734e2020-06-15 16:09:53 -07001245 module.addHostProperties()
1246 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001247
Colin Cross9ae1b922018-06-26 17:59:05 -07001248 module.Module.properties.Installable = proptools.BoolPtr(true)
1249
Colin Cross6b4a32d2017-12-05 13:42:45 -08001250 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1251 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001252 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001253 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001254}
1255
1256//
1257// Java prebuilts
1258//
1259
Colin Cross74d73e22017-08-02 11:05:49 -07001260type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001261 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001262
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001263 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1264 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001265 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001266
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001267 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1268 // specified.
1269 Min_sdk_version *string
1270
Colin Cross535e2cf2017-10-20 17:57:49 -07001271 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001272
Paul Duffin869de142021-07-15 14:14:41 +01001273 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001274 Permitted_packages []string
1275
Jiyong Park1be96912018-05-28 18:02:19 +09001276 // List of shared java libs that this module has dependencies to
1277 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001278
1279 // List of files to remove from the jar file(s)
1280 Exclude_files []string
1281
1282 // List of directories to remove from the jar file(s)
1283 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001284
1285 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001286 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001287
1288 // set the name of the output
1289 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001290
1291 Aidl struct {
1292 // directories that should be added as include directories for any aidl sources of modules
1293 // that depend on this module, as well as to aidl for this module.
1294 Export_include_dirs []string
1295 }
Colin Cross74d73e22017-08-02 11:05:49 -07001296}
1297
1298type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001299 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001300 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001301 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001302 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001303 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001304
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001305 // Functionality common to Module and Import.
1306 embeddableInModuleAndImport
1307
Liz Kammerd6c31d22020-08-05 15:40:41 -07001308 hiddenAPI
1309 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001310 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001311
Colin Cross74d73e22017-08-02 11:05:49 -07001312 properties ImportProperties
1313
Liz Kammerd6c31d22020-08-05 15:40:41 -07001314 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001315 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001316 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001317
Colin Cross0a6e0072017-08-30 14:24:55 -07001318 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001319 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001320 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001321
1322 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001323
1324 sdkVersion android.SdkSpec
1325 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001326}
1327
Paul Duffin630b11e2021-07-15 13:35:26 +01001328var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1329
1330func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1331 return j.properties.Permitted_packages
1332}
1333
Jiyong Park92315372021-04-02 08:45:46 +09001334func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1335 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001336}
1337
Jiyong Parkf1691d22021-03-29 20:11:58 +09001338func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001339 return "none"
1340}
1341
Jiyong Park92315372021-04-02 08:45:46 +09001342func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001343 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001344 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001345 }
Jiyong Park92315372021-04-02 08:45:46 +09001346 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001347}
1348
Jiyong Park92315372021-04-02 08:45:46 +09001349func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1350 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001351}
1352
Colin Cross74d73e22017-08-02 11:05:49 -07001353func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001354 return &j.prebuilt
1355}
1356
Colin Cross74d73e22017-08-02 11:05:49 -07001357func (j *Import) PrebuiltSrcs() []string {
1358 return j.properties.Jars
1359}
1360
1361func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001362 return j.prebuilt.Name(j.ModuleBase.Name())
1363}
1364
Jiyong Park0b238752019-10-29 11:23:10 +09001365func (j *Import) Stem() string {
1366 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1367}
1368
Jiyong Park618922e2020-01-08 13:35:43 +09001369func (a *Import) JacocoReportClassesFile() android.Path {
1370 return nil
1371}
1372
Bill Peckhama41a6962021-01-11 10:58:54 -08001373func (j *Import) LintDepSets() LintDepSets {
1374 return LintDepSets{}
1375}
1376
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001377func (j *Import) getStrictUpdatabilityLinting() bool {
1378 return false
1379}
1380
1381func (j *Import) setStrictUpdatabilityLinting(bool) {
1382}
1383
Colin Cross74d73e22017-08-02 11:05:49 -07001384func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001385 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001386
1387 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001388 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001389 }
Colin Cross1e676be2016-10-12 14:38:15 -07001390}
1391
Colin Cross74d73e22017-08-02 11:05:49 -07001392func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001393 j.sdkVersion = j.SdkVersion(ctx)
1394 j.minSdkVersion = j.MinSdkVersion(ctx)
1395
Colin Cross56a83212020-09-15 18:30:11 -07001396 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1397 j.hideApexVariantFromMake = true
1398 }
1399
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001400 if ctx.Windows() {
1401 j.HideFromMake()
1402 }
1403
Colin Cross8a497952019-03-05 22:25:09 -08001404 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001405
Jiyong Park0b238752019-10-29 11:23:10 +09001406 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001407 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001408 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1409 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001410 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001411 inputFile := outputFile
1412 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1413 TransformJetifier(ctx, outputFile, inputFile)
1414 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001415 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001416 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001417
Liz Kammerd6c31d22020-08-05 15:40:41 -07001418 var flags javaBuilderFlags
1419
Jiyong Park1be96912018-05-28 18:02:19 +09001420 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001421 tag := ctx.OtherModuleDependencyTag(module)
1422
Colin Crossdcf71b22021-02-01 13:59:03 -08001423 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1424 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001425 switch tag {
1426 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001427 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001428 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001429 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001430 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001431 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001432 switch tag {
1433 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001434 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001435 }
1436 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001437
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001438 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001439 })
1440
Nan Zhang4973ecf2018-08-10 13:42:12 -07001441 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001442 var installDir android.InstallPath
1443 if ctx.InstallInTestcases() {
1444 var archDir string
1445 if !ctx.Host() {
1446 archDir = ctx.DeviceConfig().DeviceArch()
1447 }
1448 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1449 } else {
1450 installDir = android.PathForModuleInstall(ctx, "framework")
1451 }
1452 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001453 }
Jiyong Park19604de2020-03-24 16:44:11 +09001454
1455 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001456
Paul Duffin064b70c2020-11-02 17:32:38 +00001457 if ctx.Device() {
1458 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1459 // obtained from the associated deapexer module.
1460 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1461 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001462 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001463 di := android.FindDeapexerProviderForModule(ctx)
1464 if di == nil {
1465 return // An error has been reported by FindDeapexerProviderForModule.
1466 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001467 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001468 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1469 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001470 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1471 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001472
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001473 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001474 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001475 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1476 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001477
1478 // Initialize the hiddenapi structure.
1479 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001480 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001481 // This should never happen as a variant for a prebuilt_apex is only created if the
1482 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001483 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001484 }
1485 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001486 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001487 if sdkDep.invalidVersion {
1488 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1489 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1490 } else if sdkDep.useFiles {
1491 // sdkDep.jar is actually equivalent to turbine header.jar.
1492 flags.classpath = append(flags.classpath, sdkDep.jars...)
1493 }
1494
1495 // Dex compilation
1496
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001497 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1498 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001499 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001500 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1501
Paul Duffin612e6102021-02-02 13:38:13 +00001502 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001503 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001504 if ctx.Failed() {
1505 return
1506 }
1507
Paul Duffin74d18d12021-05-14 14:18:47 +01001508 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001509 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001510
1511 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001512 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001513
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001514 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001515 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001516 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001517 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001518
1519 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1520 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1521 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1522 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1523 AidlIncludeDirs: j.exportAidlIncludeDirs,
1524 })
Colin Cross2fe66872015-03-30 17:20:39 -07001525}
1526
Paul Duffinaa55f742020-10-06 17:20:13 +01001527func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1528 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001529 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001530 return android.Paths{j.combinedClasspathFile}, nil
1531 default:
1532 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1533 }
1534}
1535
1536var _ android.OutputFileProducer = (*Import)(nil)
1537
Nan Zhanged19fc32017-10-19 13:06:22 -07001538func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001539 if j.combinedClasspathFile == nil {
1540 return nil
1541 }
Colin Cross37f6d792018-07-12 12:28:41 -07001542 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001543}
1544
Colin Cross331a1212018-08-15 20:40:52 -07001545func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001546 if j.combinedClasspathFile == nil {
1547 return nil
1548 }
Colin Cross331a1212018-08-15 20:40:52 -07001549 return android.Paths{j.combinedClasspathFile}
1550}
1551
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001552func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001553 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001554}
1555
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001556func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001557 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001558}
1559
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001560func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1561 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001562}
1563
Jiyong Park45bf82e2020-12-15 22:29:02 +09001564var _ android.ApexModule = (*Import)(nil)
1565
1566// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001567func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001568 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001569}
1570
Jiyong Park45bf82e2020-12-15 22:29:02 +09001571// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001572func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1573 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001574 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001575 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001576 return fmt.Errorf("min_sdk_version is not specified")
1577 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001578 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001579 return nil
1580 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001581 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1582 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001583 }
Jooyung Han749dc692020-04-15 11:03:39 +09001584 return nil
1585}
1586
Paul Duffinfef55002021-06-17 14:56:05 +01001587// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1588// java_sdk_library_import with the specified base module name requires to be exported from a
1589// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001590func requiredFilesFromPrebuiltApexForImport(name string) []string {
1591 // Add the dex implementation jar to the set of exported files.
1592 return []string{
1593 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001594 }
1595}
1596
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001597// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1598// the java library with the specified name.
1599func apexRootRelativePathToJavaLib(name string) string {
1600 return filepath.Join("javalib", name+".jar")
1601}
1602
Paul Duffinfef55002021-06-17 14:56:05 +01001603var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1604
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001605func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001606 name := j.BaseModuleName()
1607 return requiredFilesFromPrebuiltApexForImport(name)
1608}
1609
albaltai36ff7dc2018-12-25 14:35:23 +08001610// Add compile time check for interface implementation
1611var _ android.IDEInfo = (*Import)(nil)
1612var _ android.IDECustomizedModuleName = (*Import)(nil)
1613
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001614// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001615
1616func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1617 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1618}
1619
1620func (j *Import) IDECustomizedModuleName() string {
1621 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1622 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1623 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001624 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001625}
1626
Colin Cross74d73e22017-08-02 11:05:49 -07001627var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001628
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001629func (j *Import) IsInstallable() bool {
1630 return Bool(j.properties.Installable)
1631}
1632
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001633var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001634
Colin Cross1b16b0e2019-02-12 14:41:32 -08001635// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1636//
1637// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1638// compiled against an Android classpath.
1639//
1640// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1641// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001642func ImportFactory() android.Module {
1643 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001644
Liz Kammerd6c31d22020-08-05 15:40:41 -07001645 module.AddProperties(
1646 &module.properties,
1647 &module.dexer.dexProperties,
1648 )
Colin Cross74d73e22017-08-02 11:05:49 -07001649
Paul Duffin71b33cc2021-06-23 11:39:47 +01001650 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001651
Liz Kammerd6c31d22020-08-05 15:40:41 -07001652 module.dexProperties.Optimize.EnabledByDefault = false
1653
Colin Cross74d73e22017-08-02 11:05:49 -07001654 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001655 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001656 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001657 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001658 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001659}
1660
Colin Cross1b16b0e2019-02-12 14:41:32 -08001661// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1662// module.
1663//
1664// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1665// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001666func ImportFactoryHost() android.Module {
1667 module := &Import{}
1668
1669 module.AddProperties(&module.properties)
1670
1671 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001672 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001673 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001674 return module
1675}
1676
Colin Cross42be7612019-02-21 18:12:14 -08001677// dex_import module
1678
1679type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001680 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001681
1682 // set the name of the output
1683 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001684}
1685
1686type DexImport struct {
1687 android.ModuleBase
1688 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001689 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001690 prebuilt android.Prebuilt
1691
1692 properties DexImportProperties
1693
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001694 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001695
1696 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001697
1698 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001699}
1700
1701func (j *DexImport) Prebuilt() *android.Prebuilt {
1702 return &j.prebuilt
1703}
1704
1705func (j *DexImport) PrebuiltSrcs() []string {
1706 return j.properties.Jars
1707}
1708
1709func (j *DexImport) Name() string {
1710 return j.prebuilt.Name(j.ModuleBase.Name())
1711}
1712
Jiyong Park0b238752019-10-29 11:23:10 +09001713func (j *DexImport) Stem() string {
1714 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1715}
1716
Jiyong Park77acec62020-06-01 21:39:15 +09001717func (a *DexImport) JacocoReportClassesFile() android.Path {
1718 return nil
1719}
1720
Colin Cross08dca382020-07-21 20:31:17 -07001721func (a *DexImport) LintDepSets() LintDepSets {
1722 return LintDepSets{}
1723}
1724
Martin Stjernholm6d415272020-01-31 17:10:36 +00001725func (j *DexImport) IsInstallable() bool {
1726 return true
1727}
1728
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001729func (j *DexImport) getStrictUpdatabilityLinting() bool {
1730 return false
1731}
1732
1733func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1734}
1735
Colin Cross42be7612019-02-21 18:12:14 -08001736func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1737 if len(j.properties.Jars) != 1 {
1738 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1739 }
1740
Colin Cross56a83212020-09-15 18:30:11 -07001741 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1742 if !apexInfo.IsForPlatform() {
1743 j.hideApexVariantFromMake = true
1744 }
1745
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001746 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1747 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001748 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1749
1750 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1751 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1752
1753 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001754 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001755
1756 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1757 rule.Temporary(temporary)
1758
1759 // use zip2zip to uncompress classes*.dex files
1760 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001761 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001762 FlagWithInput("-i ", inputJar).
1763 FlagWithOutput("-o ", temporary).
1764 FlagWithArg("-0 ", "'classes*.dex'")
1765
1766 // use zipalign to align uncompressed classes*.dex files
1767 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001768 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001769 Flag("-f").
1770 Text("4").
1771 Input(temporary).
1772 Output(dexOutputFile)
1773
1774 rule.DeleteTemporaryFiles()
1775
Colin Crossf1a035e2020-11-16 17:32:30 -08001776 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001777 } else {
1778 ctx.Build(pctx, android.BuildParams{
1779 Rule: android.Cp,
1780 Input: inputJar,
1781 Output: dexOutputFile,
1782 })
1783 }
1784
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001785 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001786
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001787 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001788
Colin Cross56a83212020-09-15 18:30:11 -07001789 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001790 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1791 j.Stem()+".jar", dexOutputFile)
1792 }
Colin Cross42be7612019-02-21 18:12:14 -08001793}
1794
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001795func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001796 return j.dexJarFile
1797}
1798
Jiyong Park45bf82e2020-12-15 22:29:02 +09001799var _ android.ApexModule = (*DexImport)(nil)
1800
1801// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001802func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1803 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001804 // we don't check prebuilt modules for sdk_version
1805 return nil
1806}
1807
Colin Cross42be7612019-02-21 18:12:14 -08001808// dex_import imports a `.jar` file containing classes.dex files.
1809//
1810// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1811// to the device.
1812func DexImportFactory() android.Module {
1813 module := &DexImport{}
1814
1815 module.AddProperties(&module.properties)
1816
1817 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001818 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001819 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001820 return module
1821}
1822
Colin Cross89536d42017-07-07 14:35:50 -07001823//
1824// Defaults
1825//
1826type Defaults struct {
1827 android.ModuleBase
1828 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001829 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001830}
1831
Colin Cross1b16b0e2019-02-12 14:41:32 -08001832// java_defaults provides a set of properties that can be inherited by other java or android modules.
1833//
1834// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1835// property in the defaults module that exists in the depending module will be prepended to the depending module's
1836// value for that property.
1837//
1838// Example:
1839//
1840// java_defaults {
1841// name: "example_defaults",
1842// srcs: ["common/**/*.java"],
1843// javacflags: ["-Xlint:all"],
1844// aaptflags: ["--auto-add-overlay"],
1845// }
1846//
1847// java_library {
1848// name: "example",
1849// defaults: ["example_defaults"],
1850// srcs: ["example/**/*.java"],
1851// }
1852//
1853// is functionally identical to:
1854//
1855// java_library {
1856// name: "example",
1857// srcs: [
1858// "common/**/*.java",
1859// "example/**/*.java",
1860// ],
1861// javacflags: ["-Xlint:all"],
1862// }
Paul Duffin47357662019-12-05 14:07:14 +00001863func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001864 module := &Defaults{}
1865
Colin Cross89536d42017-07-07 14:35:50 -07001866 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001867 &CommonProperties{},
1868 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001869 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001870 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001871 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001872 &aaptProperties{},
1873 &androidLibraryProperties{},
1874 &appProperties{},
1875 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001876 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001877 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001878 &ImportProperties{},
1879 &AARImportProperties{},
1880 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001881 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001882 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001883 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001884 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001885 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001886 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001887 )
1888
1889 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001890 return module
1891}
Nan Zhangea568a42017-11-08 21:20:04 -08001892
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001893func kytheExtractJavaFactory() android.Singleton {
1894 return &kytheExtractJavaSingleton{}
1895}
1896
1897type kytheExtractJavaSingleton struct {
1898}
1899
1900func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1901 var xrefTargets android.Paths
1902 ctx.VisitAllModules(func(module android.Module) {
1903 if javaModule, ok := module.(xref); ok {
1904 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1905 }
1906 })
1907 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1908 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001909 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001910 }
1911}
1912
Nan Zhangea568a42017-11-08 21:20:04 -08001913var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001914var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001915var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001916var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001917
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001918// Add class loader context (CLC) of a given dependency to the current CLC.
1919func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1920 clcMap dexpreopt.ClassLoaderContextMap) {
1921
1922 dep, ok := depModule.(UsesLibraryDependency)
1923 if !ok {
1924 return
1925 }
1926
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001927 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1928
1929 var sdkLib *string
1930 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1931 // A shared SDK library. This should be added as a top-level CLC element.
1932 sdkLib = &depName
1933 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1934 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1935 // property. This should be handled in the same way as a shared SDK library.
1936 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001937 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001938
1939 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001940 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001941 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001942 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1943 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1944 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1945 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001946 } else if depTag == staticLibTag {
1947 // Propagate <uses-library> through static library dependencies, unless it is a component
1948 // library (such as stubs). Component libraries have a dependency on their SDK library,
1949 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001950 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001951 return
1952 }
1953 } else {
1954 // Don't propagate <uses-library> for other dependency tags.
1955 return
1956 }
1957
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001958 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1959 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1960 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1961 // from its CLC should be added to the current CLC.
1962 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001963 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001964 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001965 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001966 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1967 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001968}
Wei Libafb6d62021-12-10 03:14:59 -08001969
1970type javaLibraryAttributes struct {
1971 Srcs bazel.LabelListAttribute
1972 Deps bazel.LabelListAttribute
1973 Javacopts bazel.StringListAttribute
1974}
1975
1976func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
1977 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
1978 attrs := &javaLibraryAttributes{
1979 Srcs: srcs,
1980 }
1981
1982 if m.properties.Javacflags != nil {
1983 attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
1984 }
1985
1986 if m.properties.Libs != nil {
1987 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.properties.Libs))
1988 }
1989
1990 props := bazel.BazelTargetModuleProperties{
1991 Rule_class: "java_library",
1992 Bzl_load_location: "//build/bazel/rules/java:library.bzl",
1993 }
1994
1995 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
1996}
1997
1998type javaBinaryHostAttributes struct {
1999 Srcs bazel.LabelListAttribute
2000 Deps bazel.LabelListAttribute
2001 Main_class string
2002 Jvm_flags bazel.StringListAttribute
2003}
2004
2005// JavaBinaryHostBp2Build is for java_binary_host bp2build.
2006func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
2007 mainClass := ""
2008 if m.binaryProperties.Main_class != nil {
2009 mainClass = *m.binaryProperties.Main_class
2010 }
2011 if m.properties.Manifest != nil {
2012 mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String())
2013 if err != nil {
2014 return
2015 }
2016 mainClass = mainClassInManifest
2017 }
2018 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
2019 attrs := &javaBinaryHostAttributes{
2020 Srcs: srcs,
2021 Main_class: mainClass,
2022 }
2023
2024 // Attribute deps
2025 deps := []string{}
2026 if m.properties.Static_libs != nil {
2027 deps = append(deps, m.properties.Static_libs...)
2028 }
2029 if m.binaryProperties.Jni_libs != nil {
2030 deps = append(deps, m.binaryProperties.Jni_libs...)
2031 }
2032 if len(deps) > 0 {
2033 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, deps))
2034 }
2035
2036 // Attribute jvm_flags
2037 if m.binaryProperties.Jni_libs != nil {
2038 jniLibPackages := map[string]bool{}
2039 for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes {
2040 jniLibPackage := jniLibLabel.Label
2041 indexOfColon := strings.Index(jniLibLabel.Label, ":")
2042 if indexOfColon > 0 {
2043 // JNI lib from other package
2044 jniLibPackage = jniLibLabel.Label[2:indexOfColon]
2045 } else if indexOfColon == 0 {
2046 // JNI lib in the same package of java_binary
2047 packageOfCurrentModule := m.GetBazelLabel(ctx, m)
2048 jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")]
2049 }
2050 if _, inMap := jniLibPackages[jniLibPackage]; !inMap {
2051 jniLibPackages[jniLibPackage] = true
2052 }
2053 }
2054 jniLibPaths := []string{}
2055 for jniLibPackage, _ := range jniLibPackages {
2056 // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH
2057 jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage)
2058 }
2059 attrs.Jvm_flags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")})
2060 }
2061
2062 props := bazel.BazelTargetModuleProperties{
2063 Rule_class: "java_binary",
2064 }
2065
2066 // Create the BazelTargetModule.
2067 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
2068}