blob: ee4f2eb0bdfe11e00418cad6259850faebc42550 [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"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010030 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010036 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000037
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080038 RegisterJavaSdkMemberTypes()
39}
40
Paul Duffin535e0a12021-03-30 23:34:32 +010041func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080042 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
43
44 ctx.RegisterModuleType("java_library", LibraryFactory)
45 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
46 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
47 ctx.RegisterModuleType("java_binary", BinaryFactory)
48 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
49 ctx.RegisterModuleType("java_test", TestFactory)
50 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
51 ctx.RegisterModuleType("java_test_host", TestHostFactory)
52 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
53 ctx.RegisterModuleType("java_import", ImportFactory)
54 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
55 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
56 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
57 ctx.RegisterModuleType("dex_import", DexImportFactory)
58
59 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
60 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
61 })
62
63 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
64 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
65}
66
67func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000068 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000069 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000070
Paul Duffin22ff0aa2021-02-04 11:15:34 +000071 // Export implementation classes jar as part of the sdk.
72 exportImplementationClassesJar := func(_ android.SdkMemberContext, j *Library) android.Path {
73 implementationJars := j.ImplementationAndResourcesJars()
74 if len(implementationJars) != 1 {
75 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
76 }
77 return implementationJars[0]
78 }
79
Paul Duffindb170e42020-12-08 17:48:25 +000080 // Register java implementation libraries for use only in module_exports (not sdk).
Paul Duffinf5c0a9c2020-02-28 14:39:53 +000081 android.RegisterSdkMemberType(&librarySdkMemberType{
82 android.SdkMemberTypeBase{
83 PropertyName: "java_libs",
84 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +000085 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +000086 sdkSnapshotFilePathForJar,
87 copyEverythingToSnapshot,
Paul Duffin255f18e2019-12-13 11:22:16 +000088 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000089
Paul Duffindb170e42020-12-08 17:48:25 +000090 // Register java boot libraries for use in sdk.
91 //
92 // The build has some implicit dependencies (via the boot jars configuration) on a number of
93 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
94 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
95 // used outside those mainline modules.
96 //
97 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
98 // either java_libs, or java_header_libs would end up exporting more information than was strictly
99 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
100 // sdk/module_exports without exposing any unnecessary information.
101 android.RegisterSdkMemberType(&librarySdkMemberType{
102 android.SdkMemberTypeBase{
103 PropertyName: "java_boot_libs",
104 SupportsSdk: true,
105 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000106 // Temporarily export implementation classes jar for java_boot_libs as it is required for the
107 // hiddenapi processing.
108 // TODO(b/179354495): Revert once hiddenapi processing has been modularized.
109 exportImplementationClassesJar,
110 sdkSnapshotFilePathForJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000111 onlyCopyJarToSnapshot,
112 })
113
114 // Register java test libraries for use only in module_exports (not sdk).
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000115 android.RegisterSdkMemberType(&testSdkMemberType{
116 SdkMemberTypeBase: android.SdkMemberTypeBase{
117 PropertyName: "java_tests",
118 },
119 })
Colin Cross463a90e2015-06-17 14:20:06 -0700120
Jeongik Cha538c0d02019-07-11 15:54:27 +0900121}
122
Colin Crossdcf71b22021-02-01 13:59:03 -0800123// JavaInfo contains information about a java module for use by modules that depend on it.
124type JavaInfo struct {
125 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
126 // against this module. If empty, ImplementationJars should be used instead.
127 HeaderJars android.Paths
128
129 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
130 // in the module as well as any resources included in the module.
131 ImplementationAndResourcesJars android.Paths
132
133 // ImplementationJars is a list of jars that contain the implementations of classes in the
134 //module.
135 ImplementationJars android.Paths
136
137 // ResourceJars is a list of jars that contain the resources included in the module.
138 ResourceJars android.Paths
139
140 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
141 // depending on this module.
142 AidlIncludeDirs android.Paths
143
144 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
145 // module.
146 SrcJarArgs []string
147
148 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
149 SrcJarDeps android.Paths
150
151 // ExportedPlugins is a list of paths that should be used as annotation processors for any
152 // module that depends on this module.
153 ExportedPlugins android.Paths
154
155 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
156 // any module that depends on this module.
157 ExportedPluginClasses []string
158
159 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
160 // requiring disbling turbine for any modules that depend on it.
161 ExportedPluginDisableTurbine bool
162
163 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
164 // instrumented by jacoco.
165 JacocoReportClassesFile android.Path
166}
167
168var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
169
Colin Cross75ce9ec2021-02-26 16:20:32 -0800170// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
171// the sysprop implementation library.
172type SyspropPublicStubInfo struct {
173 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
174 // the sysprop implementation library.
175 JavaInfo JavaInfo
176}
177
178var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
179
Paul Duffin44b481b2020-06-17 16:59:43 +0100180// Methods that need to be implemented for a module that is added to apex java_libs property.
181type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700182 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100183 ImplementationAndResourcesJars() android.Paths
184}
185
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100186// Provides build path and install path to DEX jars.
187type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000188 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100189 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000190 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100191}
192
Jaewoong Jung26342642021-03-17 15:56:23 -0700193// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800194type xref interface {
195 XrefJavaFiles() android.Paths
196}
197
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800198func (j *Module) XrefJavaFiles() android.Paths {
199 return j.kytheFiles
200}
201
Colin Crossbe1da472017-07-07 15:59:46 -0700202type dependencyTag struct {
203 blueprint.BaseDependencyTag
204 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700205}
206
Colin Crosse9fe2942020-11-10 18:12:15 -0800207// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
208// dependency to be installed when the parent module is installed.
209type installDependencyTag struct {
210 blueprint.BaseDependencyTag
211 android.InstallAlwaysNeededDependencyTag
212 name string
213}
214
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100215type usesLibraryDependencyTag struct {
216 dependencyTag
217 sdkVersion int // SDK version in which the library appared as a standalone library.
218}
219
220func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
221 return usesLibraryDependencyTag{
222 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
223 sdkVersion: sdkVersion,
224 }
225}
226
Jiyong Park8be103b2019-11-08 15:53:48 +0900227func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700228 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900229}
230
Colin Crossbe1da472017-07-07 15:59:46 -0700231var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800232 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
233 staticLibTag = dependencyTag{name: "staticlib"}
234 libTag = dependencyTag{name: "javalib"}
235 java9LibTag = dependencyTag{name: "java9lib"}
236 pluginTag = dependencyTag{name: "plugin"}
237 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
238 exportedPluginTag = dependencyTag{name: "exported-plugin"}
239 bootClasspathTag = dependencyTag{name: "bootclasspath"}
240 systemModulesTag = dependencyTag{name: "system modules"}
241 frameworkResTag = dependencyTag{name: "framework-res"}
242 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
243 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
244 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
245 certificateTag = dependencyTag{name: "certificate"}
246 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
247 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
248 jniLibTag = dependencyTag{name: "jnilib"}
249 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
250 jniInstallTag = installDependencyTag{name: "jni install"}
251 binaryInstallTag = installDependencyTag{name: "binary install"}
252 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
253 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
254 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
255 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700256)
Colin Cross2fe66872015-03-30 17:20:39 -0700257
Jiyong Park83dc74b2020-01-14 18:38:44 +0900258func IsLibDepTag(depTag blueprint.DependencyTag) bool {
259 return depTag == libTag
260}
261
262func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
263 return depTag == staticLibTag
264}
265
Colin Crossfc3674a2017-09-18 17:41:52 -0700266type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100267 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700268
Colin Cross6cef4812019-10-17 14:23:50 -0700269 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
270 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100271
272 // The default system modules to use. Will be an empty string if no system
273 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700274 systemModules string
275
Pete Gilline3d44b22020-06-29 11:28:51 +0100276 // The modules that will be added to the classpath regardless of the Java language level targeted
277 classpath []string
278
Colin Cross6cef4812019-10-17 14:23:50 -0700279 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100280 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700281 java9Classpath []string
282
Colin Crossa97c5d32018-03-28 14:58:31 -0700283 frameworkResModule string
284
Colin Cross86a60ae2018-05-29 14:44:55 -0700285 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700286 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100287
288 noStandardLibs, noFrameworksLibs bool
289}
290
291func (s sdkDep) hasStandardLibs() bool {
292 return !s.noStandardLibs
293}
294
295func (s sdkDep) hasFrameworkLibs() bool {
296 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700297}
298
Colin Crossa4f08812018-10-02 22:03:40 -0700299type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700300 name string
301 path android.Path
302 target android.Target
303 coverageFile android.OptionalPath
304 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700305}
306
Jiyong Parkf1691d22021-03-29 20:11:58 +0900307func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700308 sdkDep := decodeSdkDep(ctx, sdkContext)
309 if sdkDep.useModule {
310 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
311 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
312 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
313 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
314 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
315 }
316 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
317 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
318 }
319 }
320 if sdkDep.systemModules != "" {
321 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
322 }
323}
324
Colin Cross32f676a2017-09-06 13:41:06 -0700325type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800326 classpath classpath
327 java9Classpath classpath
328 bootClasspath classpath
329 processorPath classpath
330 errorProneProcessorPath classpath
331 processorClasses []string
332 staticJars android.Paths
333 staticHeaderJars android.Paths
334 staticResourceJars android.Paths
335 aidlIncludeDirs android.Paths
336 srcs android.Paths
337 srcJars android.Paths
338 systemModules *systemModules
339 aidlPreprocess android.OptionalPath
340 kotlinStdlib android.Paths
341 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800342
343 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700344}
Colin Cross2fe66872015-03-30 17:20:39 -0700345
Colin Cross54250902017-12-05 09:28:08 -0800346func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
347 for _, f := range dep.Srcs() {
348 if f.Ext() != ".jar" {
349 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
350 ctx.OtherModuleName(dep.(blueprint.Module)))
351 }
352 }
353}
354
Jiyong Parkf1691d22021-03-29 20:11:58 +0900355func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700356 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700357 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700358 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900359 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700360 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700361 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700362 }
Nan Zhang357466b2018-04-17 17:38:36 -0700363}
364
Colin Cross1e743852019-10-28 11:37:20 -0700365type javaVersion int
366
367const (
368 JAVA_VERSION_UNSUPPORTED = 0
369 JAVA_VERSION_6 = 6
370 JAVA_VERSION_7 = 7
371 JAVA_VERSION_8 = 8
372 JAVA_VERSION_9 = 9
373)
374
375func (v javaVersion) String() string {
376 switch v {
377 case JAVA_VERSION_6:
378 return "1.6"
379 case JAVA_VERSION_7:
380 return "1.7"
381 case JAVA_VERSION_8:
382 return "1.8"
383 case JAVA_VERSION_9:
384 return "1.9"
385 default:
386 return "unsupported"
387 }
388}
389
390// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
391func (v javaVersion) usesJavaModules() bool {
392 return v >= 9
393}
394
395func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100396 switch javaVersion {
397 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700398 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100399 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700400 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100401 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700402 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100403 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700404 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100405 case "10", "11":
406 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700407 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100408 default:
409 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700410 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100411 }
412}
413
Colin Cross2fe66872015-03-30 17:20:39 -0700414//
415// Java libraries (.jar file)
416//
417
Colin Crossf506d872017-07-19 15:53:04 -0700418type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700419 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700420
421 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700422}
423
Jiyong Park45bf82e2020-12-15 22:29:02 +0900424var _ android.ApexModule = (*Library)(nil)
425
Paul Duffine739f1e2020-05-29 11:24:51 +0100426// Provides access to the list of permitted packages from updatable boot jars.
427type PermittedPackagesForUpdatableBootJars interface {
428 PermittedPackagesForUpdatableBootJars() []string
429}
430
431var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
432
433func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
434 return j.properties.Permitted_packages
435}
436
Colin Cross42be7612019-02-21 18:12:14 -0800437func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000438 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700439 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000440 return true
441 }
442
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000443 // Store uncompressed (and do not strip) dex files from boot class path jars.
444 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
445 return true
446 }
447
448 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -0800449 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000450 return true
451 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800452 if ctx.Config().UncompressPrivAppDex() &&
453 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
454 return true
455 }
456
Colin Cross2fc72f62018-12-21 12:59:54 -0800457 return false
458}
459
Colin Crossf506d872017-07-19 15:53:04 -0700460func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin4103e922021-02-01 19:01:34 +0000461 // Initialize the hiddenapi structure. Pass in the configuration name rather than the module name
462 // so the hidden api will encode the <x>.impl java_ library created by java_sdk_library just as it
463 // would the <x> library if <x> was configured as a boot jar.
464 j.initHiddenAPI(ctx, j.ConfigurationName())
465
Jiyong Park92315372021-04-02 08:45:46 +0900466 j.sdkVersion = j.SdkVersion(ctx)
467 j.minSdkVersion = j.MinSdkVersion(ctx)
468
Colin Cross56a83212020-09-15 18:30:11 -0700469 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
470 if !apexInfo.IsForPlatform() {
471 j.hideApexVariantFromMake = true
472 }
473
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100474 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900475 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800476 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700477 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100478 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700479 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100480 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700481 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100482 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700483 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700484
bralee1fbf4402020-05-21 10:11:59 +0800485 // Collect the module directory for IDE info in java/jdeps.go.
486 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
487
Colin Cross56a83212020-09-15 18:30:11 -0700488 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700490 var extraInstallDeps android.Paths
491 if j.InstallMixin != nil {
492 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
493 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700494 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900495 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700496 }
Colin Crossb7a63242015-04-16 14:09:14 -0700497}
498
Colin Crossf506d872017-07-19 15:53:04 -0700499func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700500 j.deps(ctx)
501}
502
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000503const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000504 aidlIncludeDir = "aidl"
505 javaDir = "java"
506 jarFileSuffix = ".jar"
507 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000508)
509
Paul Duffina0dbf432019-12-05 11:25:53 +0000510// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000511func sdkSnapshotFilePathForJar(osPrefix, name string) string {
512 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000513}
514
Paul Duffina04c1072020-03-02 10:16:35 +0000515func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
516 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000517}
518
Paul Duffin13879572019-11-28 14:31:38 +0000519type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000520 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000521
522 // Function to retrieve the appropriate output jar (implementation or header) from
523 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000524 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
525
526 // Function to compute the snapshot relative path to which the named library's
527 // jar should be copied.
528 snapshotPathGetter func(osPrefix, name string) string
529
530 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
531 // files like aidl files should also be copied.
532 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000533}
534
Paul Duffindb170e42020-12-08 17:48:25 +0000535const (
536 onlyCopyJarToSnapshot = true
537 copyEverythingToSnapshot = false
538)
539
Paul Duffin13879572019-11-28 14:31:38 +0000540func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
541 mctx.AddVariationDependencies(nil, dependencyTag, names...)
542}
543
544func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
545 _, ok := module.(*Library)
546 return ok
547}
548
Paul Duffin3a4eb502020-03-19 16:11:18 +0000549func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
550 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000551}
Paul Duffina0dbf432019-12-05 11:25:53 +0000552
Paul Duffin14eb4672020-03-02 11:33:02 +0000553func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000554 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000555}
556
557type librarySdkMemberProperties struct {
558 android.SdkMemberPropertiesBase
559
Paul Duffin864e1b42020-05-06 10:23:19 +0100560 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000561 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +0000562}
563
Paul Duffin3a4eb502020-03-19 16:11:18 +0000564func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000565 j := variant.(*Library)
566
Paul Duffindb170e42020-12-08 17:48:25 +0000567 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
568
Paul Duffina551a1c2020-03-17 21:04:24 +0000569 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +0000570}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000571
Paul Duffin3a4eb502020-03-19 16:11:18 +0000572func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000573 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000574
Paul Duffindb170e42020-12-08 17:48:25 +0000575 memberType := ctx.MemberType().(*librarySdkMemberType)
576
Paul Duffina551a1c2020-03-17 21:04:24 +0000577 exportedJar := p.JarToExport
578 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000579 // Delegate the creation of the snapshot relative path to the member type.
580 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
581
582 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000583 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
584
Paul Duffina551a1c2020-03-17 21:04:24 +0000585 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
586 }
587
Paul Duffindb170e42020-12-08 17:48:25 +0000588 // Do not copy anything else to the snapshot.
589 if memberType.onlyCopyJarToSnapshot {
590 return
591 }
592
Paul Duffina551a1c2020-03-17 21:04:24 +0000593 aidlIncludeDirs := p.AidlIncludeDirs
594 if len(aidlIncludeDirs) != 0 {
595 sdkModuleContext := ctx.SdkModuleContext()
596 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000597 // TODO(jiyong): copy parcelable declarations only
598 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
599 for _, file := range aidlFiles {
600 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
601 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000602 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000603
Paul Duffina551a1c2020-03-17 21:04:24 +0000604 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000605 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000606}
607
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000608var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
609 android.SdkMemberTypeBase{
610 PropertyName: "java_header_libs",
611 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000612 },
Paul Duffindb170e42020-12-08 17:48:25 +0000613 func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +0000614 headerJars := j.HeaderJars()
615 if len(headerJars) != 1 {
616 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
617 }
618
619 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000620 },
Paul Duffindb170e42020-12-08 17:48:25 +0000621 sdkSnapshotFilePathForJar,
622 copyEverythingToSnapshot,
Paul Duffina0dbf432019-12-05 11:25:53 +0000623}
624
Colin Cross1b16b0e2019-02-12 14:41:32 -0800625// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
626//
627// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
628// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
629// as a `static_libs` dependency of another module.
630//
631// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
632// a device.
633//
634// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
635// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700636func LibraryFactory() android.Module {
637 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700638
Colin Crossce6734e2020-06-15 16:09:53 -0700639 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700640
Paul Duffin859fe962020-05-15 10:20:31 +0100641 module.initModuleAndImport(&module.ModuleBase)
642
Jiyong Park7f7766d2019-07-25 22:02:35 +0900643 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900644 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900645 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700646 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700647}
648
Colin Cross1b16b0e2019-02-12 14:41:32 -0800649// java_library_static is an obsolete alias for java_library.
650func LibraryStaticFactory() android.Module {
651 return LibraryFactory()
652}
653
654// java_library_host builds and links sources into a `.jar` file for the host.
655//
656// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
657// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700658func LibraryHostFactory() android.Module {
659 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700660
Colin Crossce6734e2020-06-15 16:09:53 -0700661 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700662
Colin Cross9ae1b922018-06-26 17:59:05 -0700663 module.Module.properties.Installable = proptools.BoolPtr(true)
664
Jiyong Park7f7766d2019-07-25 22:02:35 +0900665 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900666 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700667 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700668}
669
670//
Colin Crossb628ea52018-08-14 16:42:33 -0700671// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700672//
673
Dan Shi95d19422020-08-15 12:24:26 -0700674// Test option struct.
675type TestOptions struct {
676 // a list of extra test configuration files that should be installed with the module.
677 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800678
679 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
680 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700681}
682
Colin Cross05638fc2018-04-09 18:40:24 -0700683type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700684 // list of compatibility suites (for example "cts", "vts") that the module should be
685 // installed into.
686 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700687
688 // the name of the test configuration (for example "AndroidTest.xml") that should be
689 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800690 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700691
Jack He33338892018-09-19 02:21:28 -0700692 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
693 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800694 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700695
Colin Crossd96ca352018-08-10 16:06:24 -0700696 // list of files or filegroup modules that provide data that should be installed alongside
697 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900698 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700699
700 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
701 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
702 // explicitly.
703 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800704
705 // Add parameterized mainline modules to auto generated test config. The options will be
706 // handled by TradeFed to do downloading and installing the specified modules on the device.
707 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700708
709 // Test options.
710 Test_options TestOptions
Colin Cross05638fc2018-04-09 18:40:24 -0700711}
712
Liz Kammerdd849a82020-06-12 16:38:45 -0700713type hostTestProperties struct {
714 // list of native binary modules that should be installed alongside the test
715 Data_native_bins []string `android:"arch_variant"`
716}
717
Paul Duffin42df1442019-03-20 12:45:53 +0000718type testHelperLibraryProperties struct {
719 // list of compatibility suites (for example "cts", "vts") that the module should be
720 // installed into.
721 Test_suites []string `android:"arch_variant"`
722}
723
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000724type prebuiltTestProperties struct {
725 // list of compatibility suites (for example "cts", "vts") that the module should be
726 // installed into.
727 Test_suites []string `android:"arch_variant"`
728
729 // the name of the test configuration (for example "AndroidTest.xml") that should be
730 // installed with the module.
731 Test_config *string `android:"path,arch_variant"`
732}
733
Colin Cross05638fc2018-04-09 18:40:24 -0700734type Test struct {
735 Library
736
737 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700738
Dan Shi95d19422020-08-15 12:24:26 -0700739 testConfig android.Path
740 extraTestConfigs android.Paths
741 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700742}
743
Liz Kammerdd849a82020-06-12 16:38:45 -0700744type TestHost struct {
745 Test
746
747 testHostProperties hostTestProperties
748}
749
Paul Duffin42df1442019-03-20 12:45:53 +0000750type TestHelperLibrary struct {
751 Library
752
753 testHelperLibraryProperties testHelperLibraryProperties
754}
755
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000756type JavaTestImport struct {
757 Import
758
759 prebuiltTestProperties prebuiltTestProperties
760
761 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700762 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000763}
764
Liz Kammerdd849a82020-06-12 16:38:45 -0700765func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
766 if len(j.testHostProperties.Data_native_bins) > 0 {
767 for _, target := range ctx.MultiTargets() {
768 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
769 }
770 }
771
772 j.deps(ctx)
773}
774
Yuexi Ma627263f2021-03-04 13:47:56 -0800775func (j *TestHost) AddExtraResource(p android.Path) {
776 j.extraResources = append(j.extraResources, p)
777}
778
Colin Cross303e21f2018-08-07 16:49:25 -0700779func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000780 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
781 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700782 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000783 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
784 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700785 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800786 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700787
Colin Cross8a497952019-03-05 22:25:09 -0800788 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700789
Dan Shi95d19422020-08-15 12:24:26 -0700790 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
791
Liz Kammerdd849a82020-06-12 16:38:45 -0700792 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
793 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
794 })
795
Colin Cross303e21f2018-08-07 16:49:25 -0700796 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700797}
798
Paul Duffin42df1442019-03-20 12:45:53 +0000799func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
800 j.Library.GenerateAndroidBuildActions(ctx)
801}
802
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000803func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
804 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800805 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000806
807 j.Import.GenerateAndroidBuildActions(ctx)
808}
809
810type testSdkMemberType struct {
811 android.SdkMemberTypeBase
812}
813
814func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
815 mctx.AddVariationDependencies(nil, dependencyTag, names...)
816}
817
818func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
819 _, ok := module.(*Test)
820 return ok
821}
822
Paul Duffin3a4eb502020-03-19 16:11:18 +0000823func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
824 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000825}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000826
Paul Duffin14eb4672020-03-02 11:33:02 +0000827func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
828 return &testSdkMemberProperties{}
829}
830
831type testSdkMemberProperties struct {
832 android.SdkMemberPropertiesBase
833
Paul Duffina551a1c2020-03-17 21:04:24 +0000834 JarToExport android.Path
835 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000836}
837
Paul Duffin3a4eb502020-03-19 16:11:18 +0000838func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000839 test := variant.(*Test)
840
841 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000842 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000843 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000844 }
845
Paul Duffina551a1c2020-03-17 21:04:24 +0000846 p.JarToExport = implementationJars[0]
847 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000848}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000849
Paul Duffin3a4eb502020-03-19 16:11:18 +0000850func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000851 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000852
Paul Duffina551a1c2020-03-17 21:04:24 +0000853 exportedJar := p.JarToExport
854 if exportedJar != nil {
855 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
856 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000857
858 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000859 }
860
861 testConfig := p.TestConfig
862 if testConfig != nil {
863 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
864 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000865 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
866 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000867}
868
Colin Cross1b16b0e2019-02-12 14:41:32 -0800869// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
870// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
871//
872// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
873// compiled against the device bootclasspath.
874//
875// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
876// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700877func TestFactory() android.Module {
878 module := &Test{}
879
Colin Crossce6734e2020-06-15 16:09:53 -0700880 module.addHostAndDeviceProperties()
881 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700882
Colin Cross9ae1b922018-06-26 17:59:05 -0700883 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800884 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700885 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700886
Colin Cross05638fc2018-04-09 18:40:24 -0700887 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700888 return module
889}
890
Paul Duffin42df1442019-03-20 12:45:53 +0000891// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
892func TestHelperLibraryFactory() android.Module {
893 module := &TestHelperLibrary{}
894
Colin Crossce6734e2020-06-15 16:09:53 -0700895 module.addHostAndDeviceProperties()
896 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000897
Colin Cross9a4abed2019-04-24 13:19:28 -0700898 module.Module.properties.Installable = proptools.BoolPtr(true)
899 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700900 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700901
Paul Duffin42df1442019-03-20 12:45:53 +0000902 InitJavaModule(module, android.HostAndDeviceSupported)
903 return module
904}
905
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000906// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
907// and makes sure that it is added to the appropriate test suite.
908//
909// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
910// compiled against an Android classpath.
911//
912// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
913// for host modules.
914func JavaTestImportFactory() android.Module {
915 module := &JavaTestImport{}
916
917 module.AddProperties(
918 &module.Import.properties,
919 &module.prebuiltTestProperties)
920
921 module.Import.properties.Installable = proptools.BoolPtr(true)
922
923 android.InitPrebuiltModule(module, &module.properties.Jars)
924 android.InitApexModule(module)
925 android.InitSdkAwareModule(module)
926 InitJavaModule(module, android.HostAndDeviceSupported)
927 return module
928}
929
Colin Cross1b16b0e2019-02-12 14:41:32 -0800930// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
931// allow running the test with `atest` or a `TEST_MAPPING` file.
932//
933// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
934// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700935func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700936 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700937
Colin Crossce6734e2020-06-15 16:09:53 -0700938 module.addHostProperties()
939 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700940 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700941
Yuexi Ma627263f2021-03-04 13:47:56 -0800942 InitTestHost(
943 module,
944 proptools.BoolPtr(true),
945 nil,
946 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -0700947
Liz Kammerdd849a82020-06-12 16:38:45 -0700948 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +0000949
Colin Cross05638fc2018-04-09 18:40:24 -0700950 return module
951}
952
Yuexi Ma627263f2021-03-04 13:47:56 -0800953func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
954 th.properties.Installable = installable
955 th.testProperties.Auto_gen_config = autoGenConfig
956 th.testProperties.Test_suites = testSuites
957}
958
Colin Cross05638fc2018-04-09 18:40:24 -0700959//
Colin Cross2fe66872015-03-30 17:20:39 -0700960// Java Binaries (.jar file plus wrapper script)
961//
962
Colin Crossf506d872017-07-19 15:53:04 -0700963type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700964 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800965 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -0700966
967 // Name of the class containing main to be inserted into the manifest as Main-Class.
968 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -0700969
970 // Names of modules containing JNI libraries that should be installed alongside the host
971 // variant of the binary.
972 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700973}
974
Colin Crossf506d872017-07-19 15:53:04 -0700975type Binary struct {
976 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700977
Colin Crossf506d872017-07-19 15:53:04 -0700978 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700979
Colin Cross6b4a32d2017-12-05 13:42:45 -0800980 isWrapperVariant bool
981
Colin Crossc3315992017-12-08 19:12:36 -0800982 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700983 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -0700984}
985
Alex Light24237172017-10-26 09:46:21 -0700986func (j *Binary) HostToolPath() android.OptionalPath {
987 return android.OptionalPathForPath(j.binaryFile)
988}
989
Colin Crossf506d872017-07-19 15:53:04 -0700990func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800991 if ctx.Arch().ArchType == android.Common {
992 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -0700993 if j.binaryProperties.Main_class != nil {
994 if j.properties.Manifest != nil {
995 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
996 }
997 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
998 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
999 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1000 }
1001
Colin Cross6b4a32d2017-12-05 13:42:45 -08001002 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001003 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001004 // Handle the binary wrapper
1005 j.isWrapperVariant = true
1006
Colin Cross366938f2017-12-11 16:29:02 -08001007 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001008 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001009 } else {
1010 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1011 }
1012
Colin Crossc179ea62020-10-09 10:54:15 -07001013 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001014 // of the wrapper variant, which will include the common variant's jar file and any JNI
1015 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001016 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001017 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001018 }
Colin Cross2fe66872015-03-30 17:20:39 -07001019}
1020
Colin Crossf506d872017-07-19 15:53:04 -07001021func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001022 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001023 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001024 }
1025 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001026 // These dependencies ensure the host installation rules will install the jar file and
1027 // the jni libraries when the wrapper is installed.
1028 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1029 ctx.AddVariationDependencies(
1030 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1031 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001032 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001033}
1034
Colin Cross1b16b0e2019-02-12 14:41:32 -08001035// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1036// as well.
1037//
1038// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1039// compiled against the device bootclasspath.
1040//
1041// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1042// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001043func BinaryFactory() android.Module {
1044 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001045
Colin Crossce6734e2020-06-15 16:09:53 -07001046 module.addHostAndDeviceProperties()
1047 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001048
Colin Cross9ae1b922018-06-26 17:59:05 -07001049 module.Module.properties.Installable = proptools.BoolPtr(true)
1050
Colin Cross6b4a32d2017-12-05 13:42:45 -08001051 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1052 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001053 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001054}
1055
Colin Cross1b16b0e2019-02-12 14:41:32 -08001056// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1057//
1058// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1059// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001060func BinaryHostFactory() android.Module {
1061 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001062
Colin Crossce6734e2020-06-15 16:09:53 -07001063 module.addHostProperties()
1064 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001065
Colin Cross9ae1b922018-06-26 17:59:05 -07001066 module.Module.properties.Installable = proptools.BoolPtr(true)
1067
Colin Cross6b4a32d2017-12-05 13:42:45 -08001068 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1069 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001070 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001071}
1072
1073//
1074// Java prebuilts
1075//
1076
Colin Cross74d73e22017-08-02 11:05:49 -07001077type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001078 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001079
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001080 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1081 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001082 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001083
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001084 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1085 // specified.
1086 Min_sdk_version *string
1087
Colin Cross535e2cf2017-10-20 17:57:49 -07001088 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001089
1090 // List of shared java libs that this module has dependencies to
1091 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001092
1093 // List of files to remove from the jar file(s)
1094 Exclude_files []string
1095
1096 // List of directories to remove from the jar file(s)
1097 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001098
1099 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001100 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001101
1102 // set the name of the output
1103 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001104
1105 Aidl struct {
1106 // directories that should be added as include directories for any aidl sources of modules
1107 // that depend on this module, as well as to aidl for this module.
1108 Export_include_dirs []string
1109 }
Colin Cross74d73e22017-08-02 11:05:49 -07001110}
1111
1112type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001113 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001114 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001115 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001116 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001117 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001118
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001119 // Functionality common to Module and Import.
1120 embeddableInModuleAndImport
1121
Liz Kammerd6c31d22020-08-05 15:40:41 -07001122 hiddenAPI
1123 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001124 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001125
Colin Cross74d73e22017-08-02 11:05:49 -07001126 properties ImportProperties
1127
Liz Kammerd6c31d22020-08-05 15:40:41 -07001128 // output file containing classes.dex and resources
1129 dexJarFile android.Path
1130
Colin Cross0a6e0072017-08-30 14:24:55 -07001131 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001132 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001133 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001134
1135 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001136
1137 sdkVersion android.SdkSpec
1138 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001139}
1140
Jiyong Park92315372021-04-02 08:45:46 +09001141func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1142 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001143}
1144
Jiyong Parkf1691d22021-03-29 20:11:58 +09001145func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001146 return "none"
1147}
1148
Jiyong Park92315372021-04-02 08:45:46 +09001149func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001150 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001151 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001152 }
Jiyong Park92315372021-04-02 08:45:46 +09001153 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001154}
1155
Jiyong Park92315372021-04-02 08:45:46 +09001156func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1157 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001158}
1159
Colin Cross74d73e22017-08-02 11:05:49 -07001160func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001161 return &j.prebuilt
1162}
1163
Colin Cross74d73e22017-08-02 11:05:49 -07001164func (j *Import) PrebuiltSrcs() []string {
1165 return j.properties.Jars
1166}
1167
1168func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001169 return j.prebuilt.Name(j.ModuleBase.Name())
1170}
1171
Jiyong Park0b238752019-10-29 11:23:10 +09001172func (j *Import) Stem() string {
1173 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1174}
1175
Jiyong Park618922e2020-01-08 13:35:43 +09001176func (a *Import) JacocoReportClassesFile() android.Path {
1177 return nil
1178}
1179
Bill Peckhama41a6962021-01-11 10:58:54 -08001180func (j *Import) LintDepSets() LintDepSets {
1181 return LintDepSets{}
1182}
1183
Colin Cross74d73e22017-08-02 11:05:49 -07001184func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001185 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001186
1187 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001188 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001189 }
Colin Cross1e676be2016-10-12 14:38:15 -07001190}
1191
Colin Cross74d73e22017-08-02 11:05:49 -07001192func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001193 j.sdkVersion = j.SdkVersion(ctx)
1194 j.minSdkVersion = j.MinSdkVersion(ctx)
1195
Paul Duffin4103e922021-02-01 19:01:34 +00001196 // Initialize the hiddenapi structure.
1197 j.initHiddenAPI(ctx, j.BaseModuleName())
1198
Colin Cross56a83212020-09-15 18:30:11 -07001199 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1200 j.hideApexVariantFromMake = true
1201 }
1202
Colin Cross8a497952019-03-05 22:25:09 -08001203 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001204
Jiyong Park0b238752019-10-29 11:23:10 +09001205 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001206 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001207 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1208 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001209 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001210 inputFile := outputFile
1211 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1212 TransformJetifier(ctx, outputFile, inputFile)
1213 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001214 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001215 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001216
Liz Kammerd6c31d22020-08-05 15:40:41 -07001217 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001218 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001219
Jiyong Park1be96912018-05-28 18:02:19 +09001220 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001221 tag := ctx.OtherModuleDependencyTag(module)
1222
Colin Crossdcf71b22021-02-01 13:59:03 -08001223 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1224 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001225 switch tag {
1226 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001227 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001228 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001229 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001230 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001231 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001232 switch tag {
1233 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001234 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001235 }
1236 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001237
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001238 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001239
1240 // Save away the `deapexer` module on which this depends, if any.
1241 if tag == android.DeapexerTag {
1242 deapexerModule = module
1243 }
Jiyong Park1be96912018-05-28 18:02:19 +09001244 })
1245
Nan Zhang4973ecf2018-08-10 13:42:12 -07001246 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001247 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001248 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001249 }
Jiyong Park19604de2020-03-24 16:44:11 +09001250
1251 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001252
Paul Duffin064b70c2020-11-02 17:32:38 +00001253 if ctx.Device() {
1254 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1255 // obtained from the associated deapexer module.
1256 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1257 if ai.ForPrebuiltApex {
1258 if deapexerModule == nil {
1259 // This should never happen as a variant for a prebuilt_apex is only created if the
1260 // deapxer module has been configured to export the dex implementation jar for this module.
1261 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1262 j.Name(), ai.ApexVariationName)
1263 }
1264
1265 // Get the path of the dex implementation jar from the `deapexer` module.
1266 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001267 if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
1268 j.dexJarFile = dexOutputPath
Paul Duffinf75e5272021-02-09 14:34:25 +00001269 j.hiddenAPIExtractInformation(ctx, dexOutputPath, outputFile)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001270 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001271 // This should never happen as a variant for a prebuilt_apex is only created if the
1272 // prebuilt_apex has been configured to export the java library dex file.
1273 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1274 }
1275 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001276 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001277 if sdkDep.invalidVersion {
1278 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1279 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1280 } else if sdkDep.useFiles {
1281 // sdkDep.jar is actually equivalent to turbine header.jar.
1282 flags.classpath = append(flags.classpath, sdkDep.jars...)
1283 }
1284
1285 // Dex compilation
1286
1287 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1288 if j.dexProperties.Uncompress_dex == nil {
1289 // If the value was not force-set by the user, use reasonable default based on the module.
1290 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1291 }
1292 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1293
Paul Duffin612e6102021-02-02 13:38:13 +00001294 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001295 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001296 if ctx.Failed() {
1297 return
1298 }
1299
Paul Duffin064b70c2020-11-02 17:32:38 +00001300 // Hidden API CSV generation and dex encoding
Paul Duffinf75e5272021-02-09 14:34:25 +00001301 dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, outputFile,
Paul Duffin064b70c2020-11-02 17:32:38 +00001302 proptools.Bool(j.dexProperties.Uncompress_dex))
1303
1304 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001305 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001306 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001307
1308 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1309 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1310 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1311 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1312 AidlIncludeDirs: j.exportAidlIncludeDirs,
1313 })
Colin Cross2fe66872015-03-30 17:20:39 -07001314}
1315
Paul Duffinaa55f742020-10-06 17:20:13 +01001316func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1317 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001318 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001319 return android.Paths{j.combinedClasspathFile}, nil
1320 default:
1321 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1322 }
1323}
1324
1325var _ android.OutputFileProducer = (*Import)(nil)
1326
Nan Zhanged19fc32017-10-19 13:06:22 -07001327func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001328 if j.combinedClasspathFile == nil {
1329 return nil
1330 }
Colin Cross37f6d792018-07-12 12:28:41 -07001331 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001332}
1333
Colin Cross331a1212018-08-15 20:40:52 -07001334func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001335 if j.combinedClasspathFile == nil {
1336 return nil
1337 }
Colin Cross331a1212018-08-15 20:40:52 -07001338 return android.Paths{j.combinedClasspathFile}
1339}
1340
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001341func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001342 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001343}
1344
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001345func (j *Import) DexJarInstallPath() android.Path {
1346 return nil
1347}
1348
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001349func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1350 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001351}
1352
Jiyong Park45bf82e2020-12-15 22:29:02 +09001353var _ android.ApexModule = (*Import)(nil)
1354
1355// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001356func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001357 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001358}
1359
Jiyong Park45bf82e2020-12-15 22:29:02 +09001360// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001361func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1362 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001363 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001364 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001365 return fmt.Errorf("min_sdk_version is not specified")
1366 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001367 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001368 return nil
1369 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001370 ver, err := sdkSpec.EffectiveVersion(ctx)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001371 if err != nil {
1372 return err
1373 }
Jiyong Park54105c42021-03-31 18:17:53 +09001374 if ver.GreaterThan(sdkVersion) {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001375 return fmt.Errorf("newer SDK(%v)", ver)
1376 }
Jooyung Han749dc692020-04-15 11:03:39 +09001377 return nil
1378}
1379
albaltai36ff7dc2018-12-25 14:35:23 +08001380// Add compile time check for interface implementation
1381var _ android.IDEInfo = (*Import)(nil)
1382var _ android.IDECustomizedModuleName = (*Import)(nil)
1383
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001384// Collect information for opening IDE project files in java/jdeps.go.
1385const (
1386 removedPrefix = "prebuilt_"
1387)
1388
1389func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1390 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1391}
1392
1393func (j *Import) IDECustomizedModuleName() string {
1394 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1395 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1396 // solution to get the Import name.
1397 name := j.Name()
1398 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001399 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001400 }
1401 return name
1402}
1403
Colin Cross74d73e22017-08-02 11:05:49 -07001404var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001405
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001406func (j *Import) IsInstallable() bool {
1407 return Bool(j.properties.Installable)
1408}
1409
1410var _ dexpreopterInterface = (*Import)(nil)
1411
Colin Cross1b16b0e2019-02-12 14:41:32 -08001412// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1413//
1414// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1415// compiled against an Android classpath.
1416//
1417// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1418// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001419func ImportFactory() android.Module {
1420 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001421
Liz Kammerd6c31d22020-08-05 15:40:41 -07001422 module.AddProperties(
1423 &module.properties,
1424 &module.dexer.dexProperties,
1425 )
Colin Cross74d73e22017-08-02 11:05:49 -07001426
Paul Duffin859fe962020-05-15 10:20:31 +01001427 module.initModuleAndImport(&module.ModuleBase)
1428
Liz Kammerd6c31d22020-08-05 15:40:41 -07001429 module.dexProperties.Optimize.EnabledByDefault = false
1430
Colin Cross74d73e22017-08-02 11:05:49 -07001431 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001432 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001433 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001434 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001435 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001436}
1437
Colin Cross1b16b0e2019-02-12 14:41:32 -08001438// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1439// module.
1440//
1441// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1442// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001443func ImportFactoryHost() android.Module {
1444 module := &Import{}
1445
1446 module.AddProperties(&module.properties)
1447
1448 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001449 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001450 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001451 return module
1452}
1453
Colin Cross42be7612019-02-21 18:12:14 -08001454// dex_import module
1455
1456type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001457 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001458
1459 // set the name of the output
1460 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001461}
1462
1463type DexImport struct {
1464 android.ModuleBase
1465 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001466 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001467 prebuilt android.Prebuilt
1468
1469 properties DexImportProperties
1470
Colin Crossb014f072021-02-26 14:54:36 -08001471 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001472
1473 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001474
1475 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001476}
1477
1478func (j *DexImport) Prebuilt() *android.Prebuilt {
1479 return &j.prebuilt
1480}
1481
1482func (j *DexImport) PrebuiltSrcs() []string {
1483 return j.properties.Jars
1484}
1485
1486func (j *DexImport) Name() string {
1487 return j.prebuilt.Name(j.ModuleBase.Name())
1488}
1489
Jiyong Park0b238752019-10-29 11:23:10 +09001490func (j *DexImport) Stem() string {
1491 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1492}
1493
Jiyong Park77acec62020-06-01 21:39:15 +09001494func (a *DexImport) JacocoReportClassesFile() android.Path {
1495 return nil
1496}
1497
Colin Cross08dca382020-07-21 20:31:17 -07001498func (a *DexImport) LintDepSets() LintDepSets {
1499 return LintDepSets{}
1500}
1501
Martin Stjernholm6d415272020-01-31 17:10:36 +00001502func (j *DexImport) IsInstallable() bool {
1503 return true
1504}
1505
Colin Cross42be7612019-02-21 18:12:14 -08001506func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1507 if len(j.properties.Jars) != 1 {
1508 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1509 }
1510
Colin Cross56a83212020-09-15 18:30:11 -07001511 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1512 if !apexInfo.IsForPlatform() {
1513 j.hideApexVariantFromMake = true
1514 }
1515
Jiyong Park0b238752019-10-29 11:23:10 +09001516 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001517 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1518
1519 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1520 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1521
1522 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001523 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001524
1525 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1526 rule.Temporary(temporary)
1527
1528 // use zip2zip to uncompress classes*.dex files
1529 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001530 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001531 FlagWithInput("-i ", inputJar).
1532 FlagWithOutput("-o ", temporary).
1533 FlagWithArg("-0 ", "'classes*.dex'")
1534
1535 // use zipalign to align uncompressed classes*.dex files
1536 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001537 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001538 Flag("-f").
1539 Text("4").
1540 Input(temporary).
1541 Output(dexOutputFile)
1542
1543 rule.DeleteTemporaryFiles()
1544
Colin Crossf1a035e2020-11-16 17:32:30 -08001545 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001546 } else {
1547 ctx.Build(pctx, android.BuildParams{
1548 Rule: android.Cp,
1549 Input: inputJar,
1550 Output: dexOutputFile,
1551 })
1552 }
1553
1554 j.dexJarFile = dexOutputFile
1555
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001556 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001557
Colin Cross56a83212020-09-15 18:30:11 -07001558 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001559 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1560 j.Stem()+".jar", dexOutputFile)
1561 }
Colin Cross42be7612019-02-21 18:12:14 -08001562}
1563
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001564func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001565 return j.dexJarFile
1566}
1567
Jiyong Park45bf82e2020-12-15 22:29:02 +09001568var _ android.ApexModule = (*DexImport)(nil)
1569
1570// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001571func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1572 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001573 // we don't check prebuilt modules for sdk_version
1574 return nil
1575}
1576
Colin Cross42be7612019-02-21 18:12:14 -08001577// dex_import imports a `.jar` file containing classes.dex files.
1578//
1579// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1580// to the device.
1581func DexImportFactory() android.Module {
1582 module := &DexImport{}
1583
1584 module.AddProperties(&module.properties)
1585
1586 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001587 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001588 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001589 return module
1590}
1591
Colin Cross89536d42017-07-07 14:35:50 -07001592//
1593// Defaults
1594//
1595type Defaults struct {
1596 android.ModuleBase
1597 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001598 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001599}
1600
Colin Cross1b16b0e2019-02-12 14:41:32 -08001601// java_defaults provides a set of properties that can be inherited by other java or android modules.
1602//
1603// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1604// property in the defaults module that exists in the depending module will be prepended to the depending module's
1605// value for that property.
1606//
1607// Example:
1608//
1609// java_defaults {
1610// name: "example_defaults",
1611// srcs: ["common/**/*.java"],
1612// javacflags: ["-Xlint:all"],
1613// aaptflags: ["--auto-add-overlay"],
1614// }
1615//
1616// java_library {
1617// name: "example",
1618// defaults: ["example_defaults"],
1619// srcs: ["example/**/*.java"],
1620// }
1621//
1622// is functionally identical to:
1623//
1624// java_library {
1625// name: "example",
1626// srcs: [
1627// "common/**/*.java",
1628// "example/**/*.java",
1629// ],
1630// javacflags: ["-Xlint:all"],
1631// }
Paul Duffin47357662019-12-05 14:07:14 +00001632func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001633 module := &Defaults{}
1634
Colin Cross89536d42017-07-07 14:35:50 -07001635 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001636 &CommonProperties{},
1637 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001638 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001639 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001640 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001641 &aaptProperties{},
1642 &androidLibraryProperties{},
1643 &appProperties{},
1644 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001645 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001646 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001647 &ImportProperties{},
1648 &AARImportProperties{},
1649 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001650 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001651 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001652 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001653 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001654 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001655 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001656 )
1657
1658 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001659 return module
1660}
Nan Zhangea568a42017-11-08 21:20:04 -08001661
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001662func kytheExtractJavaFactory() android.Singleton {
1663 return &kytheExtractJavaSingleton{}
1664}
1665
1666type kytheExtractJavaSingleton struct {
1667}
1668
1669func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1670 var xrefTargets android.Paths
1671 ctx.VisitAllModules(func(module android.Module) {
1672 if javaModule, ok := module.(xref); ok {
1673 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1674 }
1675 })
1676 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1677 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001678 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001679 }
1680}
1681
Nan Zhangea568a42017-11-08 21:20:04 -08001682var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001683var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001684var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001685var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001686
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001687// Add class loader context (CLC) of a given dependency to the current CLC.
1688func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1689 clcMap dexpreopt.ClassLoaderContextMap) {
1690
1691 dep, ok := depModule.(UsesLibraryDependency)
1692 if !ok {
1693 return
1694 }
1695
1696 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1697 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1698 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1699 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1700 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1701 // the current CLC.
1702 var implicitSdkLib *string
1703 comp, isComp := depModule.(SdkLibraryComponentDependency)
1704 if isComp {
1705 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1706 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1707 }
1708 if implicitSdkLib == nil {
1709 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1710 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001711 }
1712 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001713
1714 depTag := ctx.OtherModuleDependencyTag(depModule)
1715 if depTag == libTag || depTag == usesLibTag {
1716 // Ok, propagate <uses-library> through non-static library dependencies.
1717 } else if depTag == staticLibTag {
1718 // Propagate <uses-library> through static library dependencies, unless it is a component
1719 // library (such as stubs). Component libraries have a dependency on their SDK library,
1720 // which should not be pulled just because of a static component library.
1721 if implicitSdkLib != nil {
1722 return
1723 }
1724 } else {
1725 // Don't propagate <uses-library> for other dependency tags.
1726 return
1727 }
1728
1729 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001730 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001731 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1732 } else {
1733 depName := ctx.OtherModuleName(depModule)
1734 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1735 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001736}