blob: 5fe8814f595b2b931721cafe5644d7f3b5457732 [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 Parkf1691d22021-03-29 20:11:58 +0900359 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion())
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
Colin Cross56a83212020-09-15 18:30:11 -0700466 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
467 if !apexInfo.IsForPlatform() {
468 j.hideApexVariantFromMake = true
469 }
470
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100471 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900472 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800473 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700474 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100475 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700476 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100477 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700478 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100479 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700480 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700481
bralee1fbf4402020-05-21 10:11:59 +0800482 // Collect the module directory for IDE info in java/jdeps.go.
483 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
484
Colin Cross56a83212020-09-15 18:30:11 -0700485 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700487 var extraInstallDeps android.Paths
488 if j.InstallMixin != nil {
489 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
490 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700491 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900492 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700493 }
Colin Crossb7a63242015-04-16 14:09:14 -0700494}
495
Colin Crossf506d872017-07-19 15:53:04 -0700496func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700497 j.deps(ctx)
498}
499
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000500const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000501 aidlIncludeDir = "aidl"
502 javaDir = "java"
503 jarFileSuffix = ".jar"
504 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000505)
506
Paul Duffina0dbf432019-12-05 11:25:53 +0000507// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000508func sdkSnapshotFilePathForJar(osPrefix, name string) string {
509 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000510}
511
Paul Duffina04c1072020-03-02 10:16:35 +0000512func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
513 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000514}
515
Paul Duffin13879572019-11-28 14:31:38 +0000516type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000517 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000518
519 // Function to retrieve the appropriate output jar (implementation or header) from
520 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000521 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
522
523 // Function to compute the snapshot relative path to which the named library's
524 // jar should be copied.
525 snapshotPathGetter func(osPrefix, name string) string
526
527 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
528 // files like aidl files should also be copied.
529 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000530}
531
Paul Duffindb170e42020-12-08 17:48:25 +0000532const (
533 onlyCopyJarToSnapshot = true
534 copyEverythingToSnapshot = false
535)
536
Paul Duffin13879572019-11-28 14:31:38 +0000537func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
538 mctx.AddVariationDependencies(nil, dependencyTag, names...)
539}
540
541func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
542 _, ok := module.(*Library)
543 return ok
544}
545
Paul Duffin3a4eb502020-03-19 16:11:18 +0000546func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
547 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000548}
Paul Duffina0dbf432019-12-05 11:25:53 +0000549
Paul Duffin14eb4672020-03-02 11:33:02 +0000550func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000551 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000552}
553
554type librarySdkMemberProperties struct {
555 android.SdkMemberPropertiesBase
556
Paul Duffin864e1b42020-05-06 10:23:19 +0100557 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000558 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +0000559}
560
Paul Duffin3a4eb502020-03-19 16:11:18 +0000561func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000562 j := variant.(*Library)
563
Paul Duffindb170e42020-12-08 17:48:25 +0000564 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
565
Paul Duffina551a1c2020-03-17 21:04:24 +0000566 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +0000567}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000568
Paul Duffin3a4eb502020-03-19 16:11:18 +0000569func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000570 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000571
Paul Duffindb170e42020-12-08 17:48:25 +0000572 memberType := ctx.MemberType().(*librarySdkMemberType)
573
Paul Duffina551a1c2020-03-17 21:04:24 +0000574 exportedJar := p.JarToExport
575 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000576 // Delegate the creation of the snapshot relative path to the member type.
577 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
578
579 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000580 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
581
Paul Duffina551a1c2020-03-17 21:04:24 +0000582 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
583 }
584
Paul Duffindb170e42020-12-08 17:48:25 +0000585 // Do not copy anything else to the snapshot.
586 if memberType.onlyCopyJarToSnapshot {
587 return
588 }
589
Paul Duffina551a1c2020-03-17 21:04:24 +0000590 aidlIncludeDirs := p.AidlIncludeDirs
591 if len(aidlIncludeDirs) != 0 {
592 sdkModuleContext := ctx.SdkModuleContext()
593 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000594 // TODO(jiyong): copy parcelable declarations only
595 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
596 for _, file := range aidlFiles {
597 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
598 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000599 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000600
Paul Duffina551a1c2020-03-17 21:04:24 +0000601 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000602 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000603}
604
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000605var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
606 android.SdkMemberTypeBase{
607 PropertyName: "java_header_libs",
608 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000609 },
Paul Duffindb170e42020-12-08 17:48:25 +0000610 func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +0000611 headerJars := j.HeaderJars()
612 if len(headerJars) != 1 {
613 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
614 }
615
616 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000617 },
Paul Duffindb170e42020-12-08 17:48:25 +0000618 sdkSnapshotFilePathForJar,
619 copyEverythingToSnapshot,
Paul Duffina0dbf432019-12-05 11:25:53 +0000620}
621
Colin Cross1b16b0e2019-02-12 14:41:32 -0800622// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
623//
624// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
625// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
626// as a `static_libs` dependency of another module.
627//
628// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
629// a device.
630//
631// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
632// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700633func LibraryFactory() android.Module {
634 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700635
Colin Crossce6734e2020-06-15 16:09:53 -0700636 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700637
Paul Duffin859fe962020-05-15 10:20:31 +0100638 module.initModuleAndImport(&module.ModuleBase)
639
Jiyong Park7f7766d2019-07-25 22:02:35 +0900640 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900641 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900642 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700643 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700644}
645
Colin Cross1b16b0e2019-02-12 14:41:32 -0800646// java_library_static is an obsolete alias for java_library.
647func LibraryStaticFactory() android.Module {
648 return LibraryFactory()
649}
650
651// java_library_host builds and links sources into a `.jar` file for the host.
652//
653// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
654// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700655func LibraryHostFactory() android.Module {
656 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700657
Colin Crossce6734e2020-06-15 16:09:53 -0700658 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700659
Colin Cross9ae1b922018-06-26 17:59:05 -0700660 module.Module.properties.Installable = proptools.BoolPtr(true)
661
Jiyong Park7f7766d2019-07-25 22:02:35 +0900662 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900663 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700664 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700665}
666
667//
Colin Crossb628ea52018-08-14 16:42:33 -0700668// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700669//
670
Dan Shi95d19422020-08-15 12:24:26 -0700671// Test option struct.
672type TestOptions struct {
673 // a list of extra test configuration files that should be installed with the module.
674 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800675
676 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
677 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700678}
679
Colin Cross05638fc2018-04-09 18:40:24 -0700680type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700681 // list of compatibility suites (for example "cts", "vts") that the module should be
682 // installed into.
683 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700684
685 // the name of the test configuration (for example "AndroidTest.xml") that should be
686 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800687 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700688
Jack He33338892018-09-19 02:21:28 -0700689 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
690 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800691 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700692
Colin Crossd96ca352018-08-10 16:06:24 -0700693 // list of files or filegroup modules that provide data that should be installed alongside
694 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900695 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700696
697 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
698 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
699 // explicitly.
700 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800701
702 // Add parameterized mainline modules to auto generated test config. The options will be
703 // handled by TradeFed to do downloading and installing the specified modules on the device.
704 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700705
706 // Test options.
707 Test_options TestOptions
Colin Cross05638fc2018-04-09 18:40:24 -0700708}
709
Liz Kammerdd849a82020-06-12 16:38:45 -0700710type hostTestProperties struct {
711 // list of native binary modules that should be installed alongside the test
712 Data_native_bins []string `android:"arch_variant"`
713}
714
Paul Duffin42df1442019-03-20 12:45:53 +0000715type testHelperLibraryProperties struct {
716 // list of compatibility suites (for example "cts", "vts") that the module should be
717 // installed into.
718 Test_suites []string `android:"arch_variant"`
719}
720
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000721type prebuiltTestProperties struct {
722 // list of compatibility suites (for example "cts", "vts") that the module should be
723 // installed into.
724 Test_suites []string `android:"arch_variant"`
725
726 // the name of the test configuration (for example "AndroidTest.xml") that should be
727 // installed with the module.
728 Test_config *string `android:"path,arch_variant"`
729}
730
Colin Cross05638fc2018-04-09 18:40:24 -0700731type Test struct {
732 Library
733
734 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700735
Dan Shi95d19422020-08-15 12:24:26 -0700736 testConfig android.Path
737 extraTestConfigs android.Paths
738 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700739}
740
Liz Kammerdd849a82020-06-12 16:38:45 -0700741type TestHost struct {
742 Test
743
744 testHostProperties hostTestProperties
745}
746
Paul Duffin42df1442019-03-20 12:45:53 +0000747type TestHelperLibrary struct {
748 Library
749
750 testHelperLibraryProperties testHelperLibraryProperties
751}
752
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000753type JavaTestImport struct {
754 Import
755
756 prebuiltTestProperties prebuiltTestProperties
757
758 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700759 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000760}
761
Liz Kammerdd849a82020-06-12 16:38:45 -0700762func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
763 if len(j.testHostProperties.Data_native_bins) > 0 {
764 for _, target := range ctx.MultiTargets() {
765 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
766 }
767 }
768
769 j.deps(ctx)
770}
771
Yuexi Ma627263f2021-03-04 13:47:56 -0800772func (j *TestHost) AddExtraResource(p android.Path) {
773 j.extraResources = append(j.extraResources, p)
774}
775
Colin Cross303e21f2018-08-07 16:49:25 -0700776func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000777 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
778 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700779 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000780 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
781 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700782 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800783 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700784
Colin Cross8a497952019-03-05 22:25:09 -0800785 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700786
Dan Shi95d19422020-08-15 12:24:26 -0700787 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
788
Liz Kammerdd849a82020-06-12 16:38:45 -0700789 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
790 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
791 })
792
Colin Cross303e21f2018-08-07 16:49:25 -0700793 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700794}
795
Paul Duffin42df1442019-03-20 12:45:53 +0000796func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
797 j.Library.GenerateAndroidBuildActions(ctx)
798}
799
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000800func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
801 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800802 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000803
804 j.Import.GenerateAndroidBuildActions(ctx)
805}
806
807type testSdkMemberType struct {
808 android.SdkMemberTypeBase
809}
810
811func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
812 mctx.AddVariationDependencies(nil, dependencyTag, names...)
813}
814
815func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
816 _, ok := module.(*Test)
817 return ok
818}
819
Paul Duffin3a4eb502020-03-19 16:11:18 +0000820func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
821 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000822}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000823
Paul Duffin14eb4672020-03-02 11:33:02 +0000824func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
825 return &testSdkMemberProperties{}
826}
827
828type testSdkMemberProperties struct {
829 android.SdkMemberPropertiesBase
830
Paul Duffina551a1c2020-03-17 21:04:24 +0000831 JarToExport android.Path
832 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000833}
834
Paul Duffin3a4eb502020-03-19 16:11:18 +0000835func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000836 test := variant.(*Test)
837
838 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000839 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000840 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000841 }
842
Paul Duffina551a1c2020-03-17 21:04:24 +0000843 p.JarToExport = implementationJars[0]
844 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000845}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000846
Paul Duffin3a4eb502020-03-19 16:11:18 +0000847func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000848 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000849
Paul Duffina551a1c2020-03-17 21:04:24 +0000850 exportedJar := p.JarToExport
851 if exportedJar != nil {
852 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
853 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000854
855 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000856 }
857
858 testConfig := p.TestConfig
859 if testConfig != nil {
860 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
861 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000862 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
863 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000864}
865
Colin Cross1b16b0e2019-02-12 14:41:32 -0800866// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
867// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
868//
869// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
870// compiled against the device bootclasspath.
871//
872// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
873// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700874func TestFactory() android.Module {
875 module := &Test{}
876
Colin Crossce6734e2020-06-15 16:09:53 -0700877 module.addHostAndDeviceProperties()
878 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700879
Colin Cross9ae1b922018-06-26 17:59:05 -0700880 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800881 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700882 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700883
Colin Cross05638fc2018-04-09 18:40:24 -0700884 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700885 return module
886}
887
Paul Duffin42df1442019-03-20 12:45:53 +0000888// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
889func TestHelperLibraryFactory() android.Module {
890 module := &TestHelperLibrary{}
891
Colin Crossce6734e2020-06-15 16:09:53 -0700892 module.addHostAndDeviceProperties()
893 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000894
Colin Cross9a4abed2019-04-24 13:19:28 -0700895 module.Module.properties.Installable = proptools.BoolPtr(true)
896 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700897 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700898
Paul Duffin42df1442019-03-20 12:45:53 +0000899 InitJavaModule(module, android.HostAndDeviceSupported)
900 return module
901}
902
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000903// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
904// and makes sure that it is added to the appropriate test suite.
905//
906// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
907// compiled against an Android classpath.
908//
909// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
910// for host modules.
911func JavaTestImportFactory() android.Module {
912 module := &JavaTestImport{}
913
914 module.AddProperties(
915 &module.Import.properties,
916 &module.prebuiltTestProperties)
917
918 module.Import.properties.Installable = proptools.BoolPtr(true)
919
920 android.InitPrebuiltModule(module, &module.properties.Jars)
921 android.InitApexModule(module)
922 android.InitSdkAwareModule(module)
923 InitJavaModule(module, android.HostAndDeviceSupported)
924 return module
925}
926
Colin Cross1b16b0e2019-02-12 14:41:32 -0800927// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
928// allow running the test with `atest` or a `TEST_MAPPING` file.
929//
930// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
931// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700932func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700933 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700934
Colin Crossce6734e2020-06-15 16:09:53 -0700935 module.addHostProperties()
936 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700937 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700938
Yuexi Ma627263f2021-03-04 13:47:56 -0800939 InitTestHost(
940 module,
941 proptools.BoolPtr(true),
942 nil,
943 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -0700944
Liz Kammerdd849a82020-06-12 16:38:45 -0700945 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +0000946
Colin Cross05638fc2018-04-09 18:40:24 -0700947 return module
948}
949
Yuexi Ma627263f2021-03-04 13:47:56 -0800950func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
951 th.properties.Installable = installable
952 th.testProperties.Auto_gen_config = autoGenConfig
953 th.testProperties.Test_suites = testSuites
954}
955
Colin Cross05638fc2018-04-09 18:40:24 -0700956//
Colin Cross2fe66872015-03-30 17:20:39 -0700957// Java Binaries (.jar file plus wrapper script)
958//
959
Colin Crossf506d872017-07-19 15:53:04 -0700960type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700961 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800962 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -0700963
964 // Name of the class containing main to be inserted into the manifest as Main-Class.
965 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -0700966
967 // Names of modules containing JNI libraries that should be installed alongside the host
968 // variant of the binary.
969 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700970}
971
Colin Crossf506d872017-07-19 15:53:04 -0700972type Binary struct {
973 Library
Colin Cross2fe66872015-03-30 17:20:39 -0700974
Colin Crossf506d872017-07-19 15:53:04 -0700975 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -0700976
Colin Cross6b4a32d2017-12-05 13:42:45 -0800977 isWrapperVariant bool
978
Colin Crossc3315992017-12-08 19:12:36 -0800979 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -0700980 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -0700981}
982
Alex Light24237172017-10-26 09:46:21 -0700983func (j *Binary) HostToolPath() android.OptionalPath {
984 return android.OptionalPathForPath(j.binaryFile)
985}
986
Colin Crossf506d872017-07-19 15:53:04 -0700987func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800988 if ctx.Arch().ArchType == android.Common {
989 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -0700990 if j.binaryProperties.Main_class != nil {
991 if j.properties.Manifest != nil {
992 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
993 }
994 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
995 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
996 j.overrideManifest = android.OptionalPathForPath(manifestFile)
997 }
998
Colin Cross6b4a32d2017-12-05 13:42:45 -0800999 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001000 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001001 // Handle the binary wrapper
1002 j.isWrapperVariant = true
1003
Colin Cross366938f2017-12-11 16:29:02 -08001004 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001005 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001006 } else {
1007 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1008 }
1009
Colin Crossc179ea62020-10-09 10:54:15 -07001010 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001011 // of the wrapper variant, which will include the common variant's jar file and any JNI
1012 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001013 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001014 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001015 }
Colin Cross2fe66872015-03-30 17:20:39 -07001016}
1017
Colin Crossf506d872017-07-19 15:53:04 -07001018func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001019 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001020 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001021 }
1022 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001023 // These dependencies ensure the host installation rules will install the jar file and
1024 // the jni libraries when the wrapper is installed.
1025 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1026 ctx.AddVariationDependencies(
1027 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1028 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001029 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001030}
1031
Colin Cross1b16b0e2019-02-12 14:41:32 -08001032// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1033// as well.
1034//
1035// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1036// compiled against the device bootclasspath.
1037//
1038// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1039// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001040func BinaryFactory() android.Module {
1041 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001042
Colin Crossce6734e2020-06-15 16:09:53 -07001043 module.addHostAndDeviceProperties()
1044 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001045
Colin Cross9ae1b922018-06-26 17:59:05 -07001046 module.Module.properties.Installable = proptools.BoolPtr(true)
1047
Colin Cross6b4a32d2017-12-05 13:42:45 -08001048 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1049 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001050 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001051}
1052
Colin Cross1b16b0e2019-02-12 14:41:32 -08001053// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1054//
1055// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1056// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001057func BinaryHostFactory() android.Module {
1058 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001059
Colin Crossce6734e2020-06-15 16:09:53 -07001060 module.addHostProperties()
1061 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001062
Colin Cross9ae1b922018-06-26 17:59:05 -07001063 module.Module.properties.Installable = proptools.BoolPtr(true)
1064
Colin Cross6b4a32d2017-12-05 13:42:45 -08001065 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1066 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001067 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001068}
1069
1070//
1071// Java prebuilts
1072//
1073
Colin Cross74d73e22017-08-02 11:05:49 -07001074type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001075 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001076
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001077 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1078 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001079 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001080
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001081 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1082 // specified.
1083 Min_sdk_version *string
1084
Colin Cross535e2cf2017-10-20 17:57:49 -07001085 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001086
1087 // List of shared java libs that this module has dependencies to
1088 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001089
1090 // List of files to remove from the jar file(s)
1091 Exclude_files []string
1092
1093 // List of directories to remove from the jar file(s)
1094 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001095
1096 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001097 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001098
1099 // set the name of the output
1100 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001101
1102 Aidl struct {
1103 // directories that should be added as include directories for any aidl sources of modules
1104 // that depend on this module, as well as to aidl for this module.
1105 Export_include_dirs []string
1106 }
Colin Cross74d73e22017-08-02 11:05:49 -07001107}
1108
1109type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001110 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001111 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001112 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001113 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001114 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001115
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001116 // Functionality common to Module and Import.
1117 embeddableInModuleAndImport
1118
Liz Kammerd6c31d22020-08-05 15:40:41 -07001119 hiddenAPI
1120 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001121 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001122
Colin Cross74d73e22017-08-02 11:05:49 -07001123 properties ImportProperties
1124
Liz Kammerd6c31d22020-08-05 15:40:41 -07001125 // output file containing classes.dex and resources
1126 dexJarFile android.Path
1127
Colin Cross0a6e0072017-08-30 14:24:55 -07001128 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001129 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001130 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001131
1132 hideApexVariantFromMake bool
Colin Cross2fe66872015-03-30 17:20:39 -07001133}
1134
Jiyong Parkf1691d22021-03-29 20:11:58 +09001135func (j *Import) SdkVersion() android.SdkSpec {
1136 return android.SdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07001137}
1138
Liz Kammer2d2fd852020-08-12 14:42:30 -07001139func (j *Import) makeSdkVersion() string {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001140 return j.SdkVersion().Raw
Liz Kammer2d2fd852020-08-12 14:42:30 -07001141}
1142
Jiyong Parkf1691d22021-03-29 20:11:58 +09001143func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001144 return "none"
1145}
1146
Jiyong Parkf1691d22021-03-29 20:11:58 +09001147func (j *Import) MinSdkVersion() android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001148 if j.properties.Min_sdk_version != nil {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001149 return android.SdkSpecFrom(*j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001150 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001151 return j.SdkVersion()
Colin Cross83bb3162018-06-25 15:48:06 -07001152}
1153
Jiyong Parkf1691d22021-03-29 20:11:58 +09001154func (j *Import) TargetSdkVersion() android.SdkSpec {
1155 return j.SdkVersion()
Liz Kammerd6c31d22020-08-05 15:40:41 -07001156}
1157
Jiyong Parkf1691d22021-03-29 20:11:58 +09001158func (j *Import) MinSdkVersionString() string {
1159 return j.MinSdkVersion().Version.String()
Artur Satayev480e25b2020-04-27 18:53:18 +01001160}
1161
Colin Cross74d73e22017-08-02 11:05:49 -07001162func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001163 return &j.prebuilt
1164}
1165
Colin Cross74d73e22017-08-02 11:05:49 -07001166func (j *Import) PrebuiltSrcs() []string {
1167 return j.properties.Jars
1168}
1169
1170func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001171 return j.prebuilt.Name(j.ModuleBase.Name())
1172}
1173
Jiyong Park0b238752019-10-29 11:23:10 +09001174func (j *Import) Stem() string {
1175 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1176}
1177
Jiyong Park618922e2020-01-08 13:35:43 +09001178func (a *Import) JacocoReportClassesFile() android.Path {
1179 return nil
1180}
1181
Bill Peckhama41a6962021-01-11 10:58:54 -08001182func (j *Import) LintDepSets() LintDepSets {
1183 return LintDepSets{}
1184}
1185
Colin Cross74d73e22017-08-02 11:05:49 -07001186func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001187 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001188
1189 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001190 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001191 }
Colin Cross1e676be2016-10-12 14:38:15 -07001192}
1193
Colin Cross74d73e22017-08-02 11:05:49 -07001194func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin4103e922021-02-01 19:01:34 +00001195 // Initialize the hiddenapi structure.
1196 j.initHiddenAPI(ctx, j.BaseModuleName())
1197
Colin Cross56a83212020-09-15 18:30:11 -07001198 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1199 j.hideApexVariantFromMake = true
1200 }
1201
Colin Cross8a497952019-03-05 22:25:09 -08001202 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001203
Jiyong Park0b238752019-10-29 11:23:10 +09001204 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001205 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001206 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1207 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001208 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001209 inputFile := outputFile
1210 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1211 TransformJetifier(ctx, outputFile, inputFile)
1212 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001213 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001214 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001215
Liz Kammerd6c31d22020-08-05 15:40:41 -07001216 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001217 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001218
Jiyong Park1be96912018-05-28 18:02:19 +09001219 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001220 tag := ctx.OtherModuleDependencyTag(module)
1221
Colin Crossdcf71b22021-02-01 13:59:03 -08001222 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1223 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001224 switch tag {
1225 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001226 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001227 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001228 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001229 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001230 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001231 switch tag {
1232 case libTag:
Jiyong Parkf1691d22021-03-29 20:11:58 +09001233 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion())...)
Jiyong Park1be96912018-05-28 18:02:19 +09001234 }
1235 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001236
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001237 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001238
1239 // Save away the `deapexer` module on which this depends, if any.
1240 if tag == android.DeapexerTag {
1241 deapexerModule = module
1242 }
Jiyong Park1be96912018-05-28 18:02:19 +09001243 })
1244
Nan Zhang4973ecf2018-08-10 13:42:12 -07001245 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001246 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001247 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001248 }
Jiyong Park19604de2020-03-24 16:44:11 +09001249
1250 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001251
Paul Duffin064b70c2020-11-02 17:32:38 +00001252 if ctx.Device() {
1253 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1254 // obtained from the associated deapexer module.
1255 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1256 if ai.ForPrebuiltApex {
1257 if deapexerModule == nil {
1258 // This should never happen as a variant for a prebuilt_apex is only created if the
1259 // deapxer module has been configured to export the dex implementation jar for this module.
1260 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1261 j.Name(), ai.ApexVariationName)
1262 }
1263
1264 // Get the path of the dex implementation jar from the `deapexer` module.
1265 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001266 if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
1267 j.dexJarFile = dexOutputPath
Paul Duffinf75e5272021-02-09 14:34:25 +00001268 j.hiddenAPIExtractInformation(ctx, dexOutputPath, outputFile)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001269 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001270 // This should never happen as a variant for a prebuilt_apex is only created if the
1271 // prebuilt_apex has been configured to export the java library dex file.
1272 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1273 }
1274 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001275 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001276 if sdkDep.invalidVersion {
1277 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1278 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1279 } else if sdkDep.useFiles {
1280 // sdkDep.jar is actually equivalent to turbine header.jar.
1281 flags.classpath = append(flags.classpath, sdkDep.jars...)
1282 }
1283
1284 // Dex compilation
1285
1286 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1287 if j.dexProperties.Uncompress_dex == nil {
1288 // If the value was not force-set by the user, use reasonable default based on the module.
1289 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1290 }
1291 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1292
Paul Duffin612e6102021-02-02 13:38:13 +00001293 var dexOutputFile android.OutputPath
Jiyong Parkf1691d22021-03-29 20:11:58 +09001294 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001295 if ctx.Failed() {
1296 return
1297 }
1298
Paul Duffin064b70c2020-11-02 17:32:38 +00001299 // Hidden API CSV generation and dex encoding
Paul Duffinf75e5272021-02-09 14:34:25 +00001300 dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, outputFile,
Paul Duffin064b70c2020-11-02 17:32:38 +00001301 proptools.Bool(j.dexProperties.Uncompress_dex))
1302
1303 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001304 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001305 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001306
1307 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1308 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1309 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1310 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1311 AidlIncludeDirs: j.exportAidlIncludeDirs,
1312 })
Colin Cross2fe66872015-03-30 17:20:39 -07001313}
1314
Paul Duffinaa55f742020-10-06 17:20:13 +01001315func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1316 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001317 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001318 return android.Paths{j.combinedClasspathFile}, nil
1319 default:
1320 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1321 }
1322}
1323
1324var _ android.OutputFileProducer = (*Import)(nil)
1325
Nan Zhanged19fc32017-10-19 13:06:22 -07001326func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001327 if j.combinedClasspathFile == nil {
1328 return nil
1329 }
Colin Cross37f6d792018-07-12 12:28:41 -07001330 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001331}
1332
Colin Cross331a1212018-08-15 20:40:52 -07001333func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001334 if j.combinedClasspathFile == nil {
1335 return nil
1336 }
Colin Cross331a1212018-08-15 20:40:52 -07001337 return android.Paths{j.combinedClasspathFile}
1338}
1339
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001340func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001341 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001342}
1343
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001344func (j *Import) DexJarInstallPath() android.Path {
1345 return nil
1346}
1347
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001348func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1349 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001350}
1351
Jiyong Park45bf82e2020-12-15 22:29:02 +09001352var _ android.ApexModule = (*Import)(nil)
1353
1354// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001355func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001356 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001357}
1358
Jiyong Park45bf82e2020-12-15 22:29:02 +09001359// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001360func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1361 sdkVersion android.ApiLevel) error {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001362 sdkSpec := j.MinSdkVersion()
1363 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001364 return fmt.Errorf("min_sdk_version is not specified")
1365 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001366 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001367 return nil
1368 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001369 ver, err := sdkSpec.EffectiveVersion(ctx)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001370 if err != nil {
1371 return err
1372 }
1373 if ver.ApiLevel(ctx).GreaterThan(sdkVersion) {
1374 return fmt.Errorf("newer SDK(%v)", ver)
1375 }
Jooyung Han749dc692020-04-15 11:03:39 +09001376 return nil
1377}
1378
albaltai36ff7dc2018-12-25 14:35:23 +08001379// Add compile time check for interface implementation
1380var _ android.IDEInfo = (*Import)(nil)
1381var _ android.IDECustomizedModuleName = (*Import)(nil)
1382
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001383// Collect information for opening IDE project files in java/jdeps.go.
1384const (
1385 removedPrefix = "prebuilt_"
1386)
1387
1388func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1389 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1390}
1391
1392func (j *Import) IDECustomizedModuleName() string {
1393 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1394 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1395 // solution to get the Import name.
1396 name := j.Name()
1397 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001398 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001399 }
1400 return name
1401}
1402
Colin Cross74d73e22017-08-02 11:05:49 -07001403var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001404
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001405func (j *Import) IsInstallable() bool {
1406 return Bool(j.properties.Installable)
1407}
1408
1409var _ dexpreopterInterface = (*Import)(nil)
1410
Colin Cross1b16b0e2019-02-12 14:41:32 -08001411// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1412//
1413// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1414// compiled against an Android classpath.
1415//
1416// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1417// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001418func ImportFactory() android.Module {
1419 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001420
Liz Kammerd6c31d22020-08-05 15:40:41 -07001421 module.AddProperties(
1422 &module.properties,
1423 &module.dexer.dexProperties,
1424 )
Colin Cross74d73e22017-08-02 11:05:49 -07001425
Paul Duffin859fe962020-05-15 10:20:31 +01001426 module.initModuleAndImport(&module.ModuleBase)
1427
Liz Kammerd6c31d22020-08-05 15:40:41 -07001428 module.dexProperties.Optimize.EnabledByDefault = false
1429
Colin Cross74d73e22017-08-02 11:05:49 -07001430 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001431 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001432 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001433 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001434 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001435}
1436
Colin Cross1b16b0e2019-02-12 14:41:32 -08001437// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1438// module.
1439//
1440// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1441// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001442func ImportFactoryHost() android.Module {
1443 module := &Import{}
1444
1445 module.AddProperties(&module.properties)
1446
1447 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001448 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001449 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001450 return module
1451}
1452
Colin Cross42be7612019-02-21 18:12:14 -08001453// dex_import module
1454
1455type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001456 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001457
1458 // set the name of the output
1459 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001460}
1461
1462type DexImport struct {
1463 android.ModuleBase
1464 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001465 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001466 prebuilt android.Prebuilt
1467
1468 properties DexImportProperties
1469
Colin Crossb014f072021-02-26 14:54:36 -08001470 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001471
1472 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001473
1474 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001475}
1476
1477func (j *DexImport) Prebuilt() *android.Prebuilt {
1478 return &j.prebuilt
1479}
1480
1481func (j *DexImport) PrebuiltSrcs() []string {
1482 return j.properties.Jars
1483}
1484
1485func (j *DexImport) Name() string {
1486 return j.prebuilt.Name(j.ModuleBase.Name())
1487}
1488
Jiyong Park0b238752019-10-29 11:23:10 +09001489func (j *DexImport) Stem() string {
1490 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1491}
1492
Jiyong Park77acec62020-06-01 21:39:15 +09001493func (a *DexImport) JacocoReportClassesFile() android.Path {
1494 return nil
1495}
1496
Colin Cross08dca382020-07-21 20:31:17 -07001497func (a *DexImport) LintDepSets() LintDepSets {
1498 return LintDepSets{}
1499}
1500
Martin Stjernholm6d415272020-01-31 17:10:36 +00001501func (j *DexImport) IsInstallable() bool {
1502 return true
1503}
1504
Colin Cross42be7612019-02-21 18:12:14 -08001505func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1506 if len(j.properties.Jars) != 1 {
1507 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1508 }
1509
Colin Cross56a83212020-09-15 18:30:11 -07001510 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1511 if !apexInfo.IsForPlatform() {
1512 j.hideApexVariantFromMake = true
1513 }
1514
Jiyong Park0b238752019-10-29 11:23:10 +09001515 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001516 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1517
1518 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1519 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1520
1521 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001522 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001523
1524 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1525 rule.Temporary(temporary)
1526
1527 // use zip2zip to uncompress classes*.dex files
1528 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001529 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001530 FlagWithInput("-i ", inputJar).
1531 FlagWithOutput("-o ", temporary).
1532 FlagWithArg("-0 ", "'classes*.dex'")
1533
1534 // use zipalign to align uncompressed classes*.dex files
1535 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001536 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001537 Flag("-f").
1538 Text("4").
1539 Input(temporary).
1540 Output(dexOutputFile)
1541
1542 rule.DeleteTemporaryFiles()
1543
Colin Crossf1a035e2020-11-16 17:32:30 -08001544 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001545 } else {
1546 ctx.Build(pctx, android.BuildParams{
1547 Rule: android.Cp,
1548 Input: inputJar,
1549 Output: dexOutputFile,
1550 })
1551 }
1552
1553 j.dexJarFile = dexOutputFile
1554
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001555 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001556
Colin Cross56a83212020-09-15 18:30:11 -07001557 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001558 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1559 j.Stem()+".jar", dexOutputFile)
1560 }
Colin Cross42be7612019-02-21 18:12:14 -08001561}
1562
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001563func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001564 return j.dexJarFile
1565}
1566
Jiyong Park45bf82e2020-12-15 22:29:02 +09001567var _ android.ApexModule = (*DexImport)(nil)
1568
1569// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001570func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1571 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001572 // we don't check prebuilt modules for sdk_version
1573 return nil
1574}
1575
Colin Cross42be7612019-02-21 18:12:14 -08001576// dex_import imports a `.jar` file containing classes.dex files.
1577//
1578// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1579// to the device.
1580func DexImportFactory() android.Module {
1581 module := &DexImport{}
1582
1583 module.AddProperties(&module.properties)
1584
1585 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001586 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001587 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001588 return module
1589}
1590
Colin Cross89536d42017-07-07 14:35:50 -07001591//
1592// Defaults
1593//
1594type Defaults struct {
1595 android.ModuleBase
1596 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001597 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001598}
1599
Colin Cross1b16b0e2019-02-12 14:41:32 -08001600// java_defaults provides a set of properties that can be inherited by other java or android modules.
1601//
1602// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1603// property in the defaults module that exists in the depending module will be prepended to the depending module's
1604// value for that property.
1605//
1606// Example:
1607//
1608// java_defaults {
1609// name: "example_defaults",
1610// srcs: ["common/**/*.java"],
1611// javacflags: ["-Xlint:all"],
1612// aaptflags: ["--auto-add-overlay"],
1613// }
1614//
1615// java_library {
1616// name: "example",
1617// defaults: ["example_defaults"],
1618// srcs: ["example/**/*.java"],
1619// }
1620//
1621// is functionally identical to:
1622//
1623// java_library {
1624// name: "example",
1625// srcs: [
1626// "common/**/*.java",
1627// "example/**/*.java",
1628// ],
1629// javacflags: ["-Xlint:all"],
1630// }
Paul Duffin47357662019-12-05 14:07:14 +00001631func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001632 module := &Defaults{}
1633
Colin Cross89536d42017-07-07 14:35:50 -07001634 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001635 &CommonProperties{},
1636 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001637 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001638 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001639 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001640 &aaptProperties{},
1641 &androidLibraryProperties{},
1642 &appProperties{},
1643 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001644 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001645 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001646 &ImportProperties{},
1647 &AARImportProperties{},
1648 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001649 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001650 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001651 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001652 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001653 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001654 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001655 )
1656
1657 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001658 return module
1659}
Nan Zhangea568a42017-11-08 21:20:04 -08001660
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001661func kytheExtractJavaFactory() android.Singleton {
1662 return &kytheExtractJavaSingleton{}
1663}
1664
1665type kytheExtractJavaSingleton struct {
1666}
1667
1668func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1669 var xrefTargets android.Paths
1670 ctx.VisitAllModules(func(module android.Module) {
1671 if javaModule, ok := module.(xref); ok {
1672 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1673 }
1674 })
1675 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1676 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001677 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001678 }
1679}
1680
Nan Zhangea568a42017-11-08 21:20:04 -08001681var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001682var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001683var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001684var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001685
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001686// Add class loader context (CLC) of a given dependency to the current CLC.
1687func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1688 clcMap dexpreopt.ClassLoaderContextMap) {
1689
1690 dep, ok := depModule.(UsesLibraryDependency)
1691 if !ok {
1692 return
1693 }
1694
1695 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1696 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1697 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1698 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1699 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1700 // the current CLC.
1701 var implicitSdkLib *string
1702 comp, isComp := depModule.(SdkLibraryComponentDependency)
1703 if isComp {
1704 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1705 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1706 }
1707 if implicitSdkLib == nil {
1708 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1709 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001710 }
1711 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001712
1713 depTag := ctx.OtherModuleDependencyTag(depModule)
1714 if depTag == libTag || depTag == usesLibTag {
1715 // Ok, propagate <uses-library> through non-static library dependencies.
1716 } else if depTag == staticLibTag {
1717 // Propagate <uses-library> through static library dependencies, unless it is a component
1718 // library (such as stubs). Component libraries have a dependency on their SDK library,
1719 // which should not be pulled just because of a static component library.
1720 if implicitSdkLib != nil {
1721 return
1722 }
1723 } else {
1724 // Don't propagate <uses-library> for other dependency tags.
1725 return
1726 }
1727
1728 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001729 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001730 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1731 } else {
1732 depName := ctx.OtherModuleName(depModule)
1733 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1734 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001735}