blob: f77c694ba3fc5ef916eadabeae63dbb500b6fa72 [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 Cross24cc4be62021-11-03 14:09:41 -0700270func (j *Module) InstallBypassMake() bool { return true }
271
Colin Crossbe1da472017-07-07 15:59:46 -0700272type dependencyTag struct {
273 blueprint.BaseDependencyTag
274 name string
Colin Cross65cb3142021-12-10 23:05:02 +0000275
276 // True if the dependency is relinked at runtime.
277 runtimeLinked bool
Colin Cross2fe66872015-03-30 17:20:39 -0700278}
279
Colin Crosse9fe2942020-11-10 18:12:15 -0800280// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
281// dependency to be installed when the parent module is installed.
282type installDependencyTag struct {
283 blueprint.BaseDependencyTag
284 android.InstallAlwaysNeededDependencyTag
285 name string
286}
287
Colin Cross65cb3142021-12-10 23:05:02 +0000288func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
289 if d.runtimeLinked {
290 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
291 }
292 return nil
293}
294
295var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
296
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100297type usesLibraryDependencyTag struct {
298 dependencyTag
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100299
300 // SDK version in which the library appared as a standalone library.
301 sdkVersion int
302
303 // If the dependency is optional or required.
304 optional bool
305
306 // Whether this is an implicit dependency inferred by Soong, or an explicit one added via
307 // `uses_libs`/`optional_uses_libs` properties.
308 implicit bool
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100309}
310
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100311func makeUsesLibraryDependencyTag(sdkVersion int, optional bool, implicit bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100312 return usesLibraryDependencyTag{
Colin Cross65cb3142021-12-10 23:05:02 +0000313 dependencyTag: dependencyTag{
314 name: fmt.Sprintf("uses-library-%d", sdkVersion),
315 runtimeLinked: true,
316 },
317 sdkVersion: sdkVersion,
318 optional: optional,
319 implicit: implicit,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100320 }
321}
322
Jiyong Park8be103b2019-11-08 15:53:48 +0900323func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700324 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900325}
326
Colin Crossbe1da472017-07-07 15:59:46 -0700327var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800328 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
329 staticLibTag = dependencyTag{name: "staticlib"}
Colin Cross65cb3142021-12-10 23:05:02 +0000330 libTag = dependencyTag{name: "javalib", runtimeLinked: true}
331 java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800332 pluginTag = dependencyTag{name: "plugin"}
333 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
334 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross65cb3142021-12-10 23:05:02 +0000335 bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true}
336 systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800337 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Cross65cb3142021-12-10 23:05:02 +0000338 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true}
339 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700340 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800341 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
342 certificateTag = dependencyTag{name: "certificate"}
343 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
344 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
Colin Cross65cb3142021-12-10 23:05:02 +0000345 jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800346 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
347 jniInstallTag = installDependencyTag{name: "jni install"}
348 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700349)
Colin Cross2fe66872015-03-30 17:20:39 -0700350
Jiyong Park83dc74b2020-01-14 18:38:44 +0900351func IsLibDepTag(depTag blueprint.DependencyTag) bool {
352 return depTag == libTag
353}
354
355func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
356 return depTag == staticLibTag
357}
358
Colin Crossfc3674a2017-09-18 17:41:52 -0700359type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100360 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700361
Colin Cross6cef4812019-10-17 14:23:50 -0700362 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
363 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100364
365 // The default system modules to use. Will be an empty string if no system
366 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700367 systemModules string
368
Pete Gilline3d44b22020-06-29 11:28:51 +0100369 // The modules that will be added to the classpath regardless of the Java language level targeted
370 classpath []string
371
Colin Cross6cef4812019-10-17 14:23:50 -0700372 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100373 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700374 java9Classpath []string
375
Colin Crossa97c5d32018-03-28 14:58:31 -0700376 frameworkResModule string
377
Colin Cross86a60ae2018-05-29 14:44:55 -0700378 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700379 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100380
381 noStandardLibs, noFrameworksLibs bool
382}
383
384func (s sdkDep) hasStandardLibs() bool {
385 return !s.noStandardLibs
386}
387
388func (s sdkDep) hasFrameworkLibs() bool {
389 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700390}
391
Colin Crossa4f08812018-10-02 22:03:40 -0700392type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700393 name string
394 path android.Path
395 target android.Target
396 coverageFile android.OptionalPath
397 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700398}
399
Jiyong Parkf1691d22021-03-29 20:11:58 +0900400func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700401 sdkDep := decodeSdkDep(ctx, sdkContext)
402 if sdkDep.useModule {
403 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
404 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
405 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
406 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
407 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
408 }
409 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
410 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
411 }
412 }
413 if sdkDep.systemModules != "" {
414 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
415 }
416}
417
Colin Cross32f676a2017-09-06 13:41:06 -0700418type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800419 classpath classpath
420 java9Classpath classpath
421 bootClasspath classpath
422 processorPath classpath
423 errorProneProcessorPath classpath
424 processorClasses []string
425 staticJars android.Paths
426 staticHeaderJars android.Paths
427 staticResourceJars android.Paths
428 aidlIncludeDirs android.Paths
429 srcs android.Paths
430 srcJars android.Paths
431 systemModules *systemModules
432 aidlPreprocess android.OptionalPath
433 kotlinStdlib android.Paths
434 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700435 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800436
437 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700438}
Colin Cross2fe66872015-03-30 17:20:39 -0700439
Colin Cross54250902017-12-05 09:28:08 -0800440func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
441 for _, f := range dep.Srcs() {
442 if f.Ext() != ".jar" {
443 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
444 ctx.OtherModuleName(dep.(blueprint.Module)))
445 }
446 }
447}
448
Jiyong Parkf1691d22021-03-29 20:11:58 +0900449func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700450 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700451 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700452 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900453 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Sorin Basca8ef3e6f2021-11-26 17:27:24 +0000454 } else if ctx.Config().IsEnvTrue("EXPERIMENTAL_TARGET_JAVA_VERSION_11") {
455 // Temporary experimental flag to be able to try and build with
456 // java version 11 options. The flag, if used, just sets Java
457 // 11 as the default version, leaving any components that
458 // target an older version intact.
459 return JAVA_VERSION_11
Nan Zhang357466b2018-04-17 17:38:36 -0700460 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700461 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700462 }
Nan Zhang357466b2018-04-17 17:38:36 -0700463}
464
Colin Cross1e743852019-10-28 11:37:20 -0700465type javaVersion int
466
467const (
468 JAVA_VERSION_UNSUPPORTED = 0
469 JAVA_VERSION_6 = 6
470 JAVA_VERSION_7 = 7
471 JAVA_VERSION_8 = 8
472 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000473 JAVA_VERSION_11 = 11
Colin Cross1e743852019-10-28 11:37:20 -0700474)
475
476func (v javaVersion) String() string {
477 switch v {
478 case JAVA_VERSION_6:
479 return "1.6"
480 case JAVA_VERSION_7:
481 return "1.7"
482 case JAVA_VERSION_8:
483 return "1.8"
484 case JAVA_VERSION_9:
485 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000486 case JAVA_VERSION_11:
487 return "11"
Colin Cross1e743852019-10-28 11:37:20 -0700488 default:
489 return "unsupported"
490 }
491}
492
493// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
494func (v javaVersion) usesJavaModules() bool {
495 return v >= 9
496}
497
498func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100499 switch javaVersion {
500 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700501 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100502 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700503 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100504 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700505 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100506 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700507 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000508 case "11":
509 return JAVA_VERSION_11
510 case "10":
511 ctx.PropertyErrorf("java_version", "Java language levels 10 is not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700512 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100513 default:
514 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700515 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100516 }
517}
518
Colin Cross2fe66872015-03-30 17:20:39 -0700519//
520// Java libraries (.jar file)
521//
522
Colin Crossf506d872017-07-19 15:53:04 -0700523type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700524 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700525
526 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700527}
528
Jiyong Park45bf82e2020-12-15 22:29:02 +0900529var _ android.ApexModule = (*Library)(nil)
530
satayevd604b212021-07-21 14:23:52 +0100531// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100532type PermittedPackagesForUpdatableBootJars interface {
533 PermittedPackagesForUpdatableBootJars() []string
534}
535
536var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
537
538func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
539 return j.properties.Permitted_packages
540}
541
Colin Cross42be7612019-02-21 18:12:14 -0800542func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000543 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700544 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000545 return true
546 }
547
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000548 // Store uncompressed (and do not strip) dex files from boot class path jars.
549 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
550 return true
551 }
552
553 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000554 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000555 return true
556 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800557 if ctx.Config().UncompressPrivAppDex() &&
558 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
559 return true
560 }
561
Colin Cross2fc72f62018-12-21 12:59:54 -0800562 return false
563}
564
Jiakai Zhang22450f22021-10-11 03:05:20 +0000565// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
566func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
567 if dexer.dexProperties.Uncompress_dex == nil {
568 // If the value was not force-set by the user, use reasonable default based on the module.
569 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
570 }
571}
572
Colin Crossf506d872017-07-19 15:53:04 -0700573func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900574 j.sdkVersion = j.SdkVersion(ctx)
575 j.minSdkVersion = j.MinSdkVersion(ctx)
satayev0a420e72021-11-29 17:25:52 +0000576 j.maxSdkVersion = j.MaxSdkVersion(ctx)
Jiyong Park92315372021-04-02 08:45:46 +0900577
Colin Cross56a83212020-09-15 18:30:11 -0700578 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
579 if !apexInfo.IsForPlatform() {
580 j.hideApexVariantFromMake = true
581 }
582
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100583 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000584 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
585 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800586 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000587 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700588 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100589 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700590 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700591
bralee1fbf4402020-05-21 10:11:59 +0800592 // Collect the module directory for IDE info in java/jdeps.go.
593 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
594
Colin Cross56a83212020-09-15 18:30:11 -0700595 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900596 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700597 var extraInstallDeps android.Paths
598 if j.InstallMixin != nil {
599 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
600 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700601 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
602 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800603 j.hostdexInstallFile = ctx.InstallFile(
604 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700605 j.Stem()+"-hostdex.jar", j.outputFile)
606 }
607 var installDir android.InstallPath
608 if ctx.InstallInTestcases() {
609 var archDir string
610 if !ctx.Host() {
611 archDir = ctx.DeviceConfig().DeviceArch()
612 }
613 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
614 } else {
615 installDir = android.PathForModuleInstall(ctx, "framework")
616 }
617 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700618 }
Colin Crossb7a63242015-04-16 14:09:14 -0700619}
620
Colin Crossf506d872017-07-19 15:53:04 -0700621func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700622 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100623 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700624}
625
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000626const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000627 aidlIncludeDir = "aidl"
628 javaDir = "java"
629 jarFileSuffix = ".jar"
630 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000631)
632
Paul Duffina0dbf432019-12-05 11:25:53 +0000633// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000634func sdkSnapshotFilePathForJar(osPrefix, name string) string {
635 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000636}
637
Paul Duffina04c1072020-03-02 10:16:35 +0000638func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
639 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000640}
641
Paul Duffin13879572019-11-28 14:31:38 +0000642type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000643 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000644
645 // Function to retrieve the appropriate output jar (implementation or header) from
646 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000647 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
648
649 // Function to compute the snapshot relative path to which the named library's
650 // jar should be copied.
651 snapshotPathGetter func(osPrefix, name string) string
652
653 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
654 // files like aidl files should also be copied.
655 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000656}
657
Paul Duffindb170e42020-12-08 17:48:25 +0000658const (
659 onlyCopyJarToSnapshot = true
660 copyEverythingToSnapshot = false
661)
662
Paul Duffin296701e2021-07-14 10:29:36 +0100663func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
664 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000665}
666
667func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
668 _, ok := module.(*Library)
669 return ok
670}
671
Paul Duffin3a4eb502020-03-19 16:11:18 +0000672func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
673 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000674}
Paul Duffina0dbf432019-12-05 11:25:53 +0000675
Paul Duffin14eb4672020-03-02 11:33:02 +0000676func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000677 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000678}
679
680type librarySdkMemberProperties struct {
681 android.SdkMemberPropertiesBase
682
Paul Duffin864e1b42020-05-06 10:23:19 +0100683 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000684 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100685
686 // The list of permitted packages that need to be passed to the prebuilts as they are used to
687 // create the updatable-bcp-packages.txt file.
688 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000689}
690
Paul Duffin3a4eb502020-03-19 16:11:18 +0000691func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000692 j := variant.(*Library)
693
Paul Duffindb170e42020-12-08 17:48:25 +0000694 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
695
Paul Duffina551a1c2020-03-17 21:04:24 +0000696 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100697
698 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000699}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000700
Paul Duffin3a4eb502020-03-19 16:11:18 +0000701func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000702 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000703
Paul Duffindb170e42020-12-08 17:48:25 +0000704 memberType := ctx.MemberType().(*librarySdkMemberType)
705
Paul Duffina551a1c2020-03-17 21:04:24 +0000706 exportedJar := p.JarToExport
707 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000708 // Delegate the creation of the snapshot relative path to the member type.
709 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
710
711 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000712 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
713
Paul Duffina551a1c2020-03-17 21:04:24 +0000714 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
715 }
716
Paul Duffin869de142021-07-15 14:14:41 +0100717 if len(p.PermittedPackages) > 0 {
718 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
719 }
720
Paul Duffindb170e42020-12-08 17:48:25 +0000721 // Do not copy anything else to the snapshot.
722 if memberType.onlyCopyJarToSnapshot {
723 return
724 }
725
Paul Duffina551a1c2020-03-17 21:04:24 +0000726 aidlIncludeDirs := p.AidlIncludeDirs
727 if len(aidlIncludeDirs) != 0 {
728 sdkModuleContext := ctx.SdkModuleContext()
729 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000730 // TODO(jiyong): copy parcelable declarations only
731 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
732 for _, file := range aidlFiles {
733 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
734 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000735 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000736
Paul Duffina551a1c2020-03-17 21:04:24 +0000737 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000738 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000739}
740
Colin Cross1b16b0e2019-02-12 14:41:32 -0800741// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
742//
743// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
744// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
745// as a `static_libs` dependency of another module.
746//
747// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
748// a device.
749//
750// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
751// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700752func LibraryFactory() android.Module {
753 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700754
Colin Crossce6734e2020-06-15 16:09:53 -0700755 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700756
Paul Duffin71b33cc2021-06-23 11:39:47 +0100757 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100758
Jiyong Park7f7766d2019-07-25 22:02:35 +0900759 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900760 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800761 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900762 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700763 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700764}
765
Colin Cross1b16b0e2019-02-12 14:41:32 -0800766// java_library_static is an obsolete alias for java_library.
767func LibraryStaticFactory() android.Module {
768 return LibraryFactory()
769}
770
771// java_library_host builds and links sources into a `.jar` file for the host.
772//
773// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
774// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700775func LibraryHostFactory() android.Module {
776 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700777
Colin Crossce6734e2020-06-15 16:09:53 -0700778 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700779
Colin Cross9ae1b922018-06-26 17:59:05 -0700780 module.Module.properties.Installable = proptools.BoolPtr(true)
781
Jiyong Park7f7766d2019-07-25 22:02:35 +0900782 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100783 android.InitSdkAwareModule(module)
Wei Libafb6d62021-12-10 03:14:59 -0800784 android.InitBazelModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900785 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700786 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700787}
788
789//
Colin Crossb628ea52018-08-14 16:42:33 -0700790// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700791//
792
Dan Shi95d19422020-08-15 12:24:26 -0700793// Test option struct.
794type TestOptions struct {
795 // a list of extra test configuration files that should be installed with the module.
796 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800797
798 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
799 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700800}
801
Colin Cross05638fc2018-04-09 18:40:24 -0700802type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700803 // list of compatibility suites (for example "cts", "vts") that the module should be
804 // installed into.
805 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700806
807 // the name of the test configuration (for example "AndroidTest.xml") that should be
808 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800809 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700810
Jack He33338892018-09-19 02:21:28 -0700811 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
812 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800813 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700814
Colin Crossd96ca352018-08-10 16:06:24 -0700815 // list of files or filegroup modules that provide data that should be installed alongside
816 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900817 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700818
819 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
820 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
821 // explicitly.
822 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800823
824 // Add parameterized mainline modules to auto generated test config. The options will be
825 // handled by TradeFed to do downloading and installing the specified modules on the device.
826 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700827
828 // Test options.
829 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800830
831 // Names of modules containing JNI libraries that should be installed alongside the test.
832 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700833
834 // Install the test into a folder named for the module in all test suites.
835 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700836}
837
Liz Kammerdd849a82020-06-12 16:38:45 -0700838type hostTestProperties struct {
839 // list of native binary modules that should be installed alongside the test
840 Data_native_bins []string `android:"arch_variant"`
841}
842
Paul Duffin42df1442019-03-20 12:45:53 +0000843type testHelperLibraryProperties struct {
844 // list of compatibility suites (for example "cts", "vts") that the module should be
845 // installed into.
846 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700847
848 // Install the test into a folder named for the module in all test suites.
849 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000850}
851
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000852type prebuiltTestProperties struct {
853 // list of compatibility suites (for example "cts", "vts") that the module should be
854 // installed into.
855 Test_suites []string `android:"arch_variant"`
856
857 // the name of the test configuration (for example "AndroidTest.xml") that should be
858 // installed with the module.
859 Test_config *string `android:"path,arch_variant"`
860}
861
Colin Cross05638fc2018-04-09 18:40:24 -0700862type Test struct {
863 Library
864
865 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700866
Dan Shi95d19422020-08-15 12:24:26 -0700867 testConfig android.Path
868 extraTestConfigs android.Paths
869 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700870}
871
Liz Kammerdd849a82020-06-12 16:38:45 -0700872type TestHost struct {
873 Test
874
875 testHostProperties hostTestProperties
876}
877
Paul Duffin42df1442019-03-20 12:45:53 +0000878type TestHelperLibrary struct {
879 Library
880
881 testHelperLibraryProperties testHelperLibraryProperties
882}
883
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000884type JavaTestImport struct {
885 Import
886
887 prebuiltTestProperties prebuiltTestProperties
888
889 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700890 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000891}
892
Colin Cross24cc4be62021-11-03 14:09:41 -0700893func (j *Test) InstallInTestcases() bool {
894 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
895 // testcases by base_rules.mk.
896 return !j.Host()
897}
898
899func (j *TestHelperLibrary) InstallInTestcases() bool {
900 return true
901}
902
903func (j *JavaTestImport) InstallInTestcases() bool {
904 return true
905}
906
Liz Kammerdd849a82020-06-12 16:38:45 -0700907func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
908 if len(j.testHostProperties.Data_native_bins) > 0 {
909 for _, target := range ctx.MultiTargets() {
910 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
911 }
912 }
913
Colin Crossf8d9c492021-01-26 11:01:43 -0800914 if len(j.testProperties.Jni_libs) > 0 {
915 for _, target := range ctx.MultiTargets() {
916 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
917 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
918 }
919 }
920
Liz Kammerdd849a82020-06-12 16:38:45 -0700921 j.deps(ctx)
922}
923
Yuexi Ma627263f2021-03-04 13:47:56 -0800924func (j *TestHost) AddExtraResource(p android.Path) {
925 j.extraResources = append(j.extraResources, p)
926}
927
Colin Cross303e21f2018-08-07 16:49:25 -0700928func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000929 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
930 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700931 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000932 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
933 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700934 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800935 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700936
Colin Cross8a497952019-03-05 22:25:09 -0800937 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700938
Dan Shi95d19422020-08-15 12:24:26 -0700939 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
940
Liz Kammerdd849a82020-06-12 16:38:45 -0700941 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
942 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
943 })
944
Colin Crossf8d9c492021-01-26 11:01:43 -0800945 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
946 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
947 if sharedLibInfo.SharedLibrary != nil {
948 // Copy to an intermediate output directory to append "lib[64]" to the path,
949 // so that it's compatible with the default rpath values.
950 var relPath string
951 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
952 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
953 } else {
954 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
955 }
956 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
957 ctx.Build(pctx, android.BuildParams{
958 Rule: android.Cp,
959 Input: sharedLibInfo.SharedLibrary,
960 Output: relocatedLib,
961 })
962 j.data = append(j.data, relocatedLib)
963 } else {
964 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
965 }
966 })
967
Colin Cross303e21f2018-08-07 16:49:25 -0700968 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700969}
970
Paul Duffin42df1442019-03-20 12:45:53 +0000971func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
972 j.Library.GenerateAndroidBuildActions(ctx)
973}
974
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000975func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
976 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800977 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000978
979 j.Import.GenerateAndroidBuildActions(ctx)
980}
981
982type testSdkMemberType struct {
983 android.SdkMemberTypeBase
984}
985
Paul Duffin296701e2021-07-14 10:29:36 +0100986func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
987 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000988}
989
990func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
991 _, ok := module.(*Test)
992 return ok
993}
994
Paul Duffin3a4eb502020-03-19 16:11:18 +0000995func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
996 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000997}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000998
Paul Duffin14eb4672020-03-02 11:33:02 +0000999func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1000 return &testSdkMemberProperties{}
1001}
1002
1003type testSdkMemberProperties struct {
1004 android.SdkMemberPropertiesBase
1005
Paul Duffina551a1c2020-03-17 21:04:24 +00001006 JarToExport android.Path
1007 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00001008}
1009
Paul Duffin3a4eb502020-03-19 16:11:18 +00001010func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00001011 test := variant.(*Test)
1012
1013 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001014 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00001015 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001016 }
1017
Paul Duffina551a1c2020-03-17 21:04:24 +00001018 p.JarToExport = implementationJars[0]
1019 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00001020}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001021
Paul Duffin3a4eb502020-03-19 16:11:18 +00001022func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001023 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001024
Paul Duffina551a1c2020-03-17 21:04:24 +00001025 exportedJar := p.JarToExport
1026 if exportedJar != nil {
1027 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
1028 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001029
1030 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001031 }
1032
1033 testConfig := p.TestConfig
1034 if testConfig != nil {
1035 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1036 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001037 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1038 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001039}
1040
Colin Cross1b16b0e2019-02-12 14:41:32 -08001041// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1042// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1043//
1044// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1045// compiled against the device bootclasspath.
1046//
1047// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1048// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001049func TestFactory() android.Module {
1050 module := &Test{}
1051
Colin Crossce6734e2020-06-15 16:09:53 -07001052 module.addHostAndDeviceProperties()
1053 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001054
Colin Cross9ae1b922018-06-26 17:59:05 -07001055 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001056 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001057 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001058
Paul Duffinb6b89a42021-05-06 16:33:43 +01001059 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001060 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001061 return module
1062}
1063
Paul Duffin42df1442019-03-20 12:45:53 +00001064// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1065func TestHelperLibraryFactory() android.Module {
1066 module := &TestHelperLibrary{}
1067
Colin Crossce6734e2020-06-15 16:09:53 -07001068 module.addHostAndDeviceProperties()
1069 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001070
Colin Cross9a4abed2019-04-24 13:19:28 -07001071 module.Module.properties.Installable = proptools.BoolPtr(true)
1072 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001073 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001074
Paul Duffin42df1442019-03-20 12:45:53 +00001075 InitJavaModule(module, android.HostAndDeviceSupported)
1076 return module
1077}
1078
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001079// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1080// and makes sure that it is added to the appropriate test suite.
1081//
1082// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1083// compiled against an Android classpath.
1084//
1085// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1086// for host modules.
1087func JavaTestImportFactory() android.Module {
1088 module := &JavaTestImport{}
1089
1090 module.AddProperties(
1091 &module.Import.properties,
1092 &module.prebuiltTestProperties)
1093
1094 module.Import.properties.Installable = proptools.BoolPtr(true)
1095
1096 android.InitPrebuiltModule(module, &module.properties.Jars)
1097 android.InitApexModule(module)
1098 android.InitSdkAwareModule(module)
1099 InitJavaModule(module, android.HostAndDeviceSupported)
1100 return module
1101}
1102
Colin Cross1b16b0e2019-02-12 14:41:32 -08001103// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1104// allow running the test with `atest` or a `TEST_MAPPING` file.
1105//
1106// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1107// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001108func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001109 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001110
Colin Crossce6734e2020-06-15 16:09:53 -07001111 module.addHostProperties()
1112 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001113 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001114
Yuexi Ma627263f2021-03-04 13:47:56 -08001115 InitTestHost(
1116 module,
1117 proptools.BoolPtr(true),
1118 nil,
1119 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001120
Liz Kammerdd849a82020-06-12 16:38:45 -07001121 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001122
Colin Cross05638fc2018-04-09 18:40:24 -07001123 return module
1124}
1125
Yuexi Ma627263f2021-03-04 13:47:56 -08001126func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1127 th.properties.Installable = installable
1128 th.testProperties.Auto_gen_config = autoGenConfig
1129 th.testProperties.Test_suites = testSuites
1130}
1131
Colin Cross05638fc2018-04-09 18:40:24 -07001132//
Colin Cross2fe66872015-03-30 17:20:39 -07001133// Java Binaries (.jar file plus wrapper script)
1134//
1135
Colin Crossf506d872017-07-19 15:53:04 -07001136type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001137 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001138 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001139
1140 // Name of the class containing main to be inserted into the manifest as Main-Class.
1141 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001142
1143 // Names of modules containing JNI libraries that should be installed alongside the host
1144 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001145 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001146}
1147
Colin Crossf506d872017-07-19 15:53:04 -07001148type Binary struct {
1149 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001150
Colin Crossf506d872017-07-19 15:53:04 -07001151 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001152
Colin Cross6b4a32d2017-12-05 13:42:45 -08001153 isWrapperVariant bool
1154
Colin Crossc3315992017-12-08 19:12:36 -08001155 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001156 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001157}
1158
Alex Light24237172017-10-26 09:46:21 -07001159func (j *Binary) HostToolPath() android.OptionalPath {
1160 return android.OptionalPathForPath(j.binaryFile)
1161}
1162
Colin Crossf506d872017-07-19 15:53:04 -07001163func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001164 if ctx.Arch().ArchType == android.Common {
1165 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001166 if j.binaryProperties.Main_class != nil {
1167 if j.properties.Manifest != nil {
1168 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1169 }
1170 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1171 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1172 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1173 }
1174
Colin Cross6b4a32d2017-12-05 13:42:45 -08001175 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001176 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001177 // Handle the binary wrapper
1178 j.isWrapperVariant = true
1179
Colin Cross366938f2017-12-11 16:29:02 -08001180 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001181 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001182 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001183 if ctx.Windows() {
1184 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1185 }
1186
Colin Cross6b4a32d2017-12-05 13:42:45 -08001187 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1188 }
1189
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001190 ext := ""
1191 if ctx.Windows() {
1192 ext = ".bat"
1193 }
1194
Colin Crossc179ea62020-10-09 10:54:15 -07001195 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001196 // of the wrapper variant, which will include the common variant's jar file and any JNI
1197 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001198 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001199 ctx.ModuleName()+ext, j.wrapperFile)
1200 }
Colin Cross2fe66872015-03-30 17:20:39 -07001201}
1202
Colin Crossf506d872017-07-19 15:53:04 -07001203func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001204 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001205 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001206 }
1207 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001208 // These dependencies ensure the host installation rules will install the jar file and
1209 // the jni libraries when the wrapper is installed.
1210 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1211 ctx.AddVariationDependencies(
1212 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1213 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001214 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001215}
1216
Colin Cross1b16b0e2019-02-12 14:41:32 -08001217// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1218// as well.
1219//
1220// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1221// compiled against the device bootclasspath.
1222//
1223// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1224// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001225func BinaryFactory() android.Module {
1226 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001227
Colin Crossce6734e2020-06-15 16:09:53 -07001228 module.addHostAndDeviceProperties()
1229 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001230
Colin Cross9ae1b922018-06-26 17:59:05 -07001231 module.Module.properties.Installable = proptools.BoolPtr(true)
1232
Colin Cross6b4a32d2017-12-05 13:42:45 -08001233 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1234 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001235 android.InitBazelModule(module)
1236
Colin Cross36242852017-06-23 15:06:31 -07001237 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001238}
1239
Colin Cross1b16b0e2019-02-12 14:41:32 -08001240// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1241//
1242// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1243// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001244func BinaryHostFactory() android.Module {
1245 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001246
Colin Crossce6734e2020-06-15 16:09:53 -07001247 module.addHostProperties()
1248 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001249
Colin Cross9ae1b922018-06-26 17:59:05 -07001250 module.Module.properties.Installable = proptools.BoolPtr(true)
1251
Colin Cross6b4a32d2017-12-05 13:42:45 -08001252 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1253 android.InitDefaultableModule(module)
Wei Libafb6d62021-12-10 03:14:59 -08001254 android.InitBazelModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001255 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001256}
1257
1258//
1259// Java prebuilts
1260//
1261
Colin Cross74d73e22017-08-02 11:05:49 -07001262type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001263 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001264
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001265 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1266 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001267 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001268
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001269 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1270 // specified.
1271 Min_sdk_version *string
1272
Colin Cross535e2cf2017-10-20 17:57:49 -07001273 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001274
Paul Duffin869de142021-07-15 14:14:41 +01001275 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001276 Permitted_packages []string
1277
Jiyong Park1be96912018-05-28 18:02:19 +09001278 // List of shared java libs that this module has dependencies to
1279 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001280
1281 // List of files to remove from the jar file(s)
1282 Exclude_files []string
1283
1284 // List of directories to remove from the jar file(s)
1285 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001286
1287 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001288 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001289
1290 // set the name of the output
1291 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001292
1293 Aidl struct {
1294 // directories that should be added as include directories for any aidl sources of modules
1295 // that depend on this module, as well as to aidl for this module.
1296 Export_include_dirs []string
1297 }
Colin Cross74d73e22017-08-02 11:05:49 -07001298}
1299
1300type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001301 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001302 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001303 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001304 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001305 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001306
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001307 // Functionality common to Module and Import.
1308 embeddableInModuleAndImport
1309
Liz Kammerd6c31d22020-08-05 15:40:41 -07001310 hiddenAPI
1311 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001312 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001313
Colin Cross74d73e22017-08-02 11:05:49 -07001314 properties ImportProperties
1315
Liz Kammerd6c31d22020-08-05 15:40:41 -07001316 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001317 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001318 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001319
Colin Cross0a6e0072017-08-30 14:24:55 -07001320 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001321 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001322 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001323
1324 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001325
1326 sdkVersion android.SdkSpec
1327 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001328}
1329
Paul Duffin630b11e2021-07-15 13:35:26 +01001330var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1331
1332func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1333 return j.properties.Permitted_packages
1334}
1335
Jiyong Park92315372021-04-02 08:45:46 +09001336func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1337 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001338}
1339
Jiyong Parkf1691d22021-03-29 20:11:58 +09001340func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001341 return "none"
1342}
1343
Jiyong Park92315372021-04-02 08:45:46 +09001344func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001345 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001346 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001347 }
Jiyong Park92315372021-04-02 08:45:46 +09001348 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001349}
1350
Jiyong Park92315372021-04-02 08:45:46 +09001351func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1352 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001353}
1354
Colin Cross74d73e22017-08-02 11:05:49 -07001355func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001356 return &j.prebuilt
1357}
1358
Colin Cross74d73e22017-08-02 11:05:49 -07001359func (j *Import) PrebuiltSrcs() []string {
1360 return j.properties.Jars
1361}
1362
1363func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001364 return j.prebuilt.Name(j.ModuleBase.Name())
1365}
1366
Jiyong Park0b238752019-10-29 11:23:10 +09001367func (j *Import) Stem() string {
1368 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1369}
1370
Jiyong Park618922e2020-01-08 13:35:43 +09001371func (a *Import) JacocoReportClassesFile() android.Path {
1372 return nil
1373}
1374
Bill Peckhama41a6962021-01-11 10:58:54 -08001375func (j *Import) LintDepSets() LintDepSets {
1376 return LintDepSets{}
1377}
1378
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001379func (j *Import) getStrictUpdatabilityLinting() bool {
1380 return false
1381}
1382
1383func (j *Import) setStrictUpdatabilityLinting(bool) {
1384}
1385
Colin Cross74d73e22017-08-02 11:05:49 -07001386func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001387 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001388
1389 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001390 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001391 }
Colin Cross1e676be2016-10-12 14:38:15 -07001392}
1393
Colin Cross74d73e22017-08-02 11:05:49 -07001394func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001395 j.sdkVersion = j.SdkVersion(ctx)
1396 j.minSdkVersion = j.MinSdkVersion(ctx)
1397
Colin Cross56a83212020-09-15 18:30:11 -07001398 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1399 j.hideApexVariantFromMake = true
1400 }
1401
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001402 if ctx.Windows() {
1403 j.HideFromMake()
1404 }
1405
Colin Cross8a497952019-03-05 22:25:09 -08001406 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001407
Jiyong Park0b238752019-10-29 11:23:10 +09001408 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001409 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001410 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1411 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001412 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001413 inputFile := outputFile
1414 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1415 TransformJetifier(ctx, outputFile, inputFile)
1416 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001417 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001418 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001419
Liz Kammerd6c31d22020-08-05 15:40:41 -07001420 var flags javaBuilderFlags
1421
Jiyong Park1be96912018-05-28 18:02:19 +09001422 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001423 tag := ctx.OtherModuleDependencyTag(module)
1424
Colin Crossdcf71b22021-02-01 13:59:03 -08001425 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1426 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001427 switch tag {
1428 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001429 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001430 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001431 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001432 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001433 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001434 switch tag {
1435 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001436 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001437 }
1438 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001439
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001440 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001441 })
1442
Nan Zhang4973ecf2018-08-10 13:42:12 -07001443 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001444 var installDir android.InstallPath
1445 if ctx.InstallInTestcases() {
1446 var archDir string
1447 if !ctx.Host() {
1448 archDir = ctx.DeviceConfig().DeviceArch()
1449 }
1450 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1451 } else {
1452 installDir = android.PathForModuleInstall(ctx, "framework")
1453 }
1454 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001455 }
Jiyong Park19604de2020-03-24 16:44:11 +09001456
1457 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001458
Paul Duffin064b70c2020-11-02 17:32:38 +00001459 if ctx.Device() {
1460 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1461 // obtained from the associated deapexer module.
1462 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1463 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001464 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001465 di := android.FindDeapexerProviderForModule(ctx)
1466 if di == nil {
1467 return // An error has been reported by FindDeapexerProviderForModule.
1468 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001469 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001470 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1471 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001472 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1473 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001474
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001475 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001476 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001477 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1478 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001479
1480 // Initialize the hiddenapi structure.
1481 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001482 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001483 // This should never happen as a variant for a prebuilt_apex is only created if the
1484 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001485 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001486 }
1487 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001488 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001489 if sdkDep.invalidVersion {
1490 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1491 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1492 } else if sdkDep.useFiles {
1493 // sdkDep.jar is actually equivalent to turbine header.jar.
1494 flags.classpath = append(flags.classpath, sdkDep.jars...)
1495 }
1496
1497 // Dex compilation
1498
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001499 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1500 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001501 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001502 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1503
Paul Duffin612e6102021-02-02 13:38:13 +00001504 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001505 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001506 if ctx.Failed() {
1507 return
1508 }
1509
Paul Duffin74d18d12021-05-14 14:18:47 +01001510 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001511 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001512
1513 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001514 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001515
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001516 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001517 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001518 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001519 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001520
1521 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1522 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1523 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1524 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1525 AidlIncludeDirs: j.exportAidlIncludeDirs,
1526 })
Colin Cross2fe66872015-03-30 17:20:39 -07001527}
1528
Paul Duffinaa55f742020-10-06 17:20:13 +01001529func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1530 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001531 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001532 return android.Paths{j.combinedClasspathFile}, nil
1533 default:
1534 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1535 }
1536}
1537
1538var _ android.OutputFileProducer = (*Import)(nil)
1539
Nan Zhanged19fc32017-10-19 13:06:22 -07001540func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001541 if j.combinedClasspathFile == nil {
1542 return nil
1543 }
Colin Cross37f6d792018-07-12 12:28:41 -07001544 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001545}
1546
Colin Cross331a1212018-08-15 20:40:52 -07001547func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001548 if j.combinedClasspathFile == nil {
1549 return nil
1550 }
Colin Cross331a1212018-08-15 20:40:52 -07001551 return android.Paths{j.combinedClasspathFile}
1552}
1553
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001554func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001555 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001556}
1557
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001558func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001559 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001560}
1561
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001562func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1563 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001564}
1565
Jiyong Park45bf82e2020-12-15 22:29:02 +09001566var _ android.ApexModule = (*Import)(nil)
1567
1568// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001569func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001570 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001571}
1572
Jiyong Park45bf82e2020-12-15 22:29:02 +09001573// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001574func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1575 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001576 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001577 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001578 return fmt.Errorf("min_sdk_version is not specified")
1579 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001580 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001581 return nil
1582 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001583 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1584 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001585 }
Jooyung Han749dc692020-04-15 11:03:39 +09001586 return nil
1587}
1588
Paul Duffinfef55002021-06-17 14:56:05 +01001589// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1590// java_sdk_library_import with the specified base module name requires to be exported from a
1591// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001592func requiredFilesFromPrebuiltApexForImport(name string) []string {
1593 // Add the dex implementation jar to the set of exported files.
1594 return []string{
1595 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001596 }
1597}
1598
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001599// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1600// the java library with the specified name.
1601func apexRootRelativePathToJavaLib(name string) string {
1602 return filepath.Join("javalib", name+".jar")
1603}
1604
Paul Duffinfef55002021-06-17 14:56:05 +01001605var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1606
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001607func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001608 name := j.BaseModuleName()
1609 return requiredFilesFromPrebuiltApexForImport(name)
1610}
1611
albaltai36ff7dc2018-12-25 14:35:23 +08001612// Add compile time check for interface implementation
1613var _ android.IDEInfo = (*Import)(nil)
1614var _ android.IDECustomizedModuleName = (*Import)(nil)
1615
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001616// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001617
1618func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1619 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1620}
1621
1622func (j *Import) IDECustomizedModuleName() string {
1623 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1624 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1625 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001626 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001627}
1628
Colin Cross74d73e22017-08-02 11:05:49 -07001629var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001630
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001631func (j *Import) IsInstallable() bool {
1632 return Bool(j.properties.Installable)
1633}
1634
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001635var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001636
Colin Cross1b16b0e2019-02-12 14:41:32 -08001637// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1638//
1639// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1640// compiled against an Android classpath.
1641//
1642// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1643// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001644func ImportFactory() android.Module {
1645 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001646
Liz Kammerd6c31d22020-08-05 15:40:41 -07001647 module.AddProperties(
1648 &module.properties,
1649 &module.dexer.dexProperties,
1650 )
Colin Cross74d73e22017-08-02 11:05:49 -07001651
Paul Duffin71b33cc2021-06-23 11:39:47 +01001652 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001653
Liz Kammerd6c31d22020-08-05 15:40:41 -07001654 module.dexProperties.Optimize.EnabledByDefault = false
1655
Colin Cross74d73e22017-08-02 11:05:49 -07001656 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001657 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001658 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001659 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001660 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001661}
1662
Colin Cross1b16b0e2019-02-12 14:41:32 -08001663// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1664// module.
1665//
1666// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1667// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001668func ImportFactoryHost() android.Module {
1669 module := &Import{}
1670
1671 module.AddProperties(&module.properties)
1672
1673 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001674 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001675 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001676 return module
1677}
1678
Colin Cross42be7612019-02-21 18:12:14 -08001679// dex_import module
1680
1681type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001682 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001683
1684 // set the name of the output
1685 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001686}
1687
1688type DexImport struct {
1689 android.ModuleBase
1690 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001691 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001692 prebuilt android.Prebuilt
1693
1694 properties DexImportProperties
1695
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001696 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001697
1698 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001699
1700 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001701}
1702
1703func (j *DexImport) Prebuilt() *android.Prebuilt {
1704 return &j.prebuilt
1705}
1706
1707func (j *DexImport) PrebuiltSrcs() []string {
1708 return j.properties.Jars
1709}
1710
1711func (j *DexImport) Name() string {
1712 return j.prebuilt.Name(j.ModuleBase.Name())
1713}
1714
Jiyong Park0b238752019-10-29 11:23:10 +09001715func (j *DexImport) Stem() string {
1716 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1717}
1718
Jiyong Park77acec62020-06-01 21:39:15 +09001719func (a *DexImport) JacocoReportClassesFile() android.Path {
1720 return nil
1721}
1722
Colin Cross08dca382020-07-21 20:31:17 -07001723func (a *DexImport) LintDepSets() LintDepSets {
1724 return LintDepSets{}
1725}
1726
Martin Stjernholm6d415272020-01-31 17:10:36 +00001727func (j *DexImport) IsInstallable() bool {
1728 return true
1729}
1730
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001731func (j *DexImport) getStrictUpdatabilityLinting() bool {
1732 return false
1733}
1734
1735func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1736}
1737
Colin Cross42be7612019-02-21 18:12:14 -08001738func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1739 if len(j.properties.Jars) != 1 {
1740 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1741 }
1742
Colin Cross56a83212020-09-15 18:30:11 -07001743 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1744 if !apexInfo.IsForPlatform() {
1745 j.hideApexVariantFromMake = true
1746 }
1747
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001748 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1749 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001750 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1751
1752 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1753 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1754
1755 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001756 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001757
1758 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1759 rule.Temporary(temporary)
1760
1761 // use zip2zip to uncompress classes*.dex files
1762 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001763 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001764 FlagWithInput("-i ", inputJar).
1765 FlagWithOutput("-o ", temporary).
1766 FlagWithArg("-0 ", "'classes*.dex'")
1767
1768 // use zipalign to align uncompressed classes*.dex files
1769 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001770 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001771 Flag("-f").
1772 Text("4").
1773 Input(temporary).
1774 Output(dexOutputFile)
1775
1776 rule.DeleteTemporaryFiles()
1777
Colin Crossf1a035e2020-11-16 17:32:30 -08001778 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001779 } else {
1780 ctx.Build(pctx, android.BuildParams{
1781 Rule: android.Cp,
1782 Input: inputJar,
1783 Output: dexOutputFile,
1784 })
1785 }
1786
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001787 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001788
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001789 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001790
Colin Cross56a83212020-09-15 18:30:11 -07001791 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001792 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1793 j.Stem()+".jar", dexOutputFile)
1794 }
Colin Cross42be7612019-02-21 18:12:14 -08001795}
1796
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001797func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001798 return j.dexJarFile
1799}
1800
Jiyong Park45bf82e2020-12-15 22:29:02 +09001801var _ android.ApexModule = (*DexImport)(nil)
1802
1803// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001804func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1805 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001806 // we don't check prebuilt modules for sdk_version
1807 return nil
1808}
1809
Colin Cross42be7612019-02-21 18:12:14 -08001810// dex_import imports a `.jar` file containing classes.dex files.
1811//
1812// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1813// to the device.
1814func DexImportFactory() android.Module {
1815 module := &DexImport{}
1816
1817 module.AddProperties(&module.properties)
1818
1819 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001820 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001821 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001822 return module
1823}
1824
Colin Cross89536d42017-07-07 14:35:50 -07001825//
1826// Defaults
1827//
1828type Defaults struct {
1829 android.ModuleBase
1830 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001831 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001832}
1833
Colin Cross1b16b0e2019-02-12 14:41:32 -08001834// java_defaults provides a set of properties that can be inherited by other java or android modules.
1835//
1836// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1837// property in the defaults module that exists in the depending module will be prepended to the depending module's
1838// value for that property.
1839//
1840// Example:
1841//
1842// java_defaults {
1843// name: "example_defaults",
1844// srcs: ["common/**/*.java"],
1845// javacflags: ["-Xlint:all"],
1846// aaptflags: ["--auto-add-overlay"],
1847// }
1848//
1849// java_library {
1850// name: "example",
1851// defaults: ["example_defaults"],
1852// srcs: ["example/**/*.java"],
1853// }
1854//
1855// is functionally identical to:
1856//
1857// java_library {
1858// name: "example",
1859// srcs: [
1860// "common/**/*.java",
1861// "example/**/*.java",
1862// ],
1863// javacflags: ["-Xlint:all"],
1864// }
Paul Duffin47357662019-12-05 14:07:14 +00001865func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001866 module := &Defaults{}
1867
Colin Cross89536d42017-07-07 14:35:50 -07001868 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001869 &CommonProperties{},
1870 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001871 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001872 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001873 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001874 &aaptProperties{},
1875 &androidLibraryProperties{},
1876 &appProperties{},
1877 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001878 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001879 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001880 &ImportProperties{},
1881 &AARImportProperties{},
1882 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001883 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001884 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001885 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001886 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001887 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001888 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001889 )
1890
1891 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001892 return module
1893}
Nan Zhangea568a42017-11-08 21:20:04 -08001894
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001895func kytheExtractJavaFactory() android.Singleton {
1896 return &kytheExtractJavaSingleton{}
1897}
1898
1899type kytheExtractJavaSingleton struct {
1900}
1901
1902func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1903 var xrefTargets android.Paths
1904 ctx.VisitAllModules(func(module android.Module) {
1905 if javaModule, ok := module.(xref); ok {
1906 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1907 }
1908 })
1909 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1910 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001911 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001912 }
1913}
1914
Nan Zhangea568a42017-11-08 21:20:04 -08001915var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001916var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001917var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001918var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001919
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001920// Add class loader context (CLC) of a given dependency to the current CLC.
1921func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1922 clcMap dexpreopt.ClassLoaderContextMap) {
1923
1924 dep, ok := depModule.(UsesLibraryDependency)
1925 if !ok {
1926 return
1927 }
1928
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001929 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1930
1931 var sdkLib *string
1932 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1933 // A shared SDK library. This should be added as a top-level CLC element.
1934 sdkLib = &depName
1935 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1936 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1937 // property. This should be handled in the same way as a shared SDK library.
1938 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001939 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001940
1941 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001942 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001943 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001944 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1945 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1946 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1947 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001948 } else if depTag == staticLibTag {
1949 // Propagate <uses-library> through static library dependencies, unless it is a component
1950 // library (such as stubs). Component libraries have a dependency on their SDK library,
1951 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001952 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001953 return
1954 }
1955 } else {
1956 // Don't propagate <uses-library> for other dependency tags.
1957 return
1958 }
1959
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001960 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1961 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1962 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1963 // from its CLC should be added to the current CLC.
1964 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001965 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001966 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001967 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001968 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1969 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001970}
Wei Libafb6d62021-12-10 03:14:59 -08001971
1972type javaLibraryAttributes struct {
1973 Srcs bazel.LabelListAttribute
1974 Deps bazel.LabelListAttribute
1975 Javacopts bazel.StringListAttribute
1976}
1977
1978func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
1979 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
1980 attrs := &javaLibraryAttributes{
1981 Srcs: srcs,
1982 }
1983
1984 if m.properties.Javacflags != nil {
1985 attrs.Javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags)
1986 }
1987
1988 if m.properties.Libs != nil {
1989 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.properties.Libs))
1990 }
1991
1992 props := bazel.BazelTargetModuleProperties{
1993 Rule_class: "java_library",
1994 Bzl_load_location: "//build/bazel/rules/java:library.bzl",
1995 }
1996
1997 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
1998}
1999
2000type javaBinaryHostAttributes struct {
2001 Srcs bazel.LabelListAttribute
2002 Deps bazel.LabelListAttribute
2003 Main_class string
2004 Jvm_flags bazel.StringListAttribute
2005}
2006
2007// JavaBinaryHostBp2Build is for java_binary_host bp2build.
2008func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
2009 mainClass := ""
2010 if m.binaryProperties.Main_class != nil {
2011 mainClass = *m.binaryProperties.Main_class
2012 }
2013 if m.properties.Manifest != nil {
2014 mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String())
2015 if err != nil {
2016 return
2017 }
2018 mainClass = mainClassInManifest
2019 }
2020 srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs))
2021 attrs := &javaBinaryHostAttributes{
2022 Srcs: srcs,
2023 Main_class: mainClass,
2024 }
2025
2026 // Attribute deps
2027 deps := []string{}
2028 if m.properties.Static_libs != nil {
2029 deps = append(deps, m.properties.Static_libs...)
2030 }
2031 if m.binaryProperties.Jni_libs != nil {
2032 deps = append(deps, m.binaryProperties.Jni_libs...)
2033 }
2034 if len(deps) > 0 {
2035 attrs.Deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, deps))
2036 }
2037
2038 // Attribute jvm_flags
2039 if m.binaryProperties.Jni_libs != nil {
2040 jniLibPackages := map[string]bool{}
2041 for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes {
2042 jniLibPackage := jniLibLabel.Label
2043 indexOfColon := strings.Index(jniLibLabel.Label, ":")
2044 if indexOfColon > 0 {
2045 // JNI lib from other package
2046 jniLibPackage = jniLibLabel.Label[2:indexOfColon]
2047 } else if indexOfColon == 0 {
2048 // JNI lib in the same package of java_binary
2049 packageOfCurrentModule := m.GetBazelLabel(ctx, m)
2050 jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")]
2051 }
2052 if _, inMap := jniLibPackages[jniLibPackage]; !inMap {
2053 jniLibPackages[jniLibPackage] = true
2054 }
2055 }
2056 jniLibPaths := []string{}
2057 for jniLibPackage, _ := range jniLibPackages {
2058 // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH
2059 jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage)
2060 }
2061 attrs.Jvm_flags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")})
2062 }
2063
2064 props := bazel.BazelTargetModuleProperties{
2065 Rule_class: "java_binary",
2066 }
2067
2068 // Create the BazelTargetModule.
2069 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
2070}