blob: 9896b578ae4f2a009c36ef541484dc68fb3a444c [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"
Colin Crossf8d9c492021-01-26 11:01:43 -080030 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010031 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010037 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080039 RegisterJavaSdkMemberTypes()
40}
41
Paul Duffin535e0a12021-03-30 23:34:32 +010042func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080043 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
44
45 ctx.RegisterModuleType("java_library", LibraryFactory)
46 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
47 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
48 ctx.RegisterModuleType("java_binary", BinaryFactory)
49 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
50 ctx.RegisterModuleType("java_test", TestFactory)
51 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
52 ctx.RegisterModuleType("java_test_host", TestHostFactory)
53 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
54 ctx.RegisterModuleType("java_import", ImportFactory)
55 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
56 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
57 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
58 ctx.RegisterModuleType("dex_import", DexImportFactory)
59
60 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
61 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
62 })
63
64 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
65 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
66}
67
68func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000069 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000070 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010071 android.RegisterSdkMemberType(javaLibsSdkMemberType)
72 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
73 android.RegisterSdkMemberType(javaTestSdkMemberType)
74}
75
76var (
77 // Supports adding java header libraries to module_exports and sdk.
78 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
79 android.SdkMemberTypeBase{
80 PropertyName: "java_header_libs",
81 SupportsSdk: true,
82 },
83 func(_ android.SdkMemberContext, j *Library) android.Path {
84 headerJars := j.HeaderJars()
85 if len(headerJars) != 1 {
86 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
87 }
88
89 return headerJars[0]
90 },
91 sdkSnapshotFilePathForJar,
92 copyEverythingToSnapshot,
93 }
Paul Duffin255f18e2019-12-13 11:22:16 +000094
Paul Duffin22ff0aa2021-02-04 11:15:34 +000095 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +010096 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +000097 implementationJars := j.ImplementationAndResourcesJars()
98 if len(implementationJars) != 1 {
99 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
100 }
101 return implementationJars[0]
102 }
103
Paul Duffin2da04242021-04-23 19:43:28 +0100104 // Supports adding java implementation libraries to module_exports but not sdk.
105 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000106 android.SdkMemberTypeBase{
107 PropertyName: "java_libs",
108 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000109 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000110 sdkSnapshotFilePathForJar,
111 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100112 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000113
Paul Duffin2da04242021-04-23 19:43:28 +0100114 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000115 //
116 // The build has some implicit dependencies (via the boot jars configuration) on a number of
117 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
118 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
119 // used outside those mainline modules.
120 //
121 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
122 // either java_libs, or java_header_libs would end up exporting more information than was strictly
123 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
124 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100125 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000126 android.SdkMemberTypeBase{
127 PropertyName: "java_boot_libs",
128 SupportsSdk: true,
129 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000130 // Temporarily export implementation classes jar for java_boot_libs as it is required for the
131 // hiddenapi processing.
132 // TODO(b/179354495): Revert once hiddenapi processing has been modularized.
133 exportImplementationClassesJar,
134 sdkSnapshotFilePathForJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000135 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100136 }
Paul Duffindb170e42020-12-08 17:48:25 +0000137
Paul Duffin2da04242021-04-23 19:43:28 +0100138 // Supports adding java test libraries to module_exports but not sdk.
139 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000140 SdkMemberTypeBase: android.SdkMemberTypeBase{
141 PropertyName: "java_tests",
142 },
Paul Duffin2da04242021-04-23 19:43:28 +0100143 }
144)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900145
Colin Crossdcf71b22021-02-01 13:59:03 -0800146// JavaInfo contains information about a java module for use by modules that depend on it.
147type JavaInfo struct {
148 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
149 // against this module. If empty, ImplementationJars should be used instead.
150 HeaderJars android.Paths
151
152 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
153 // in the module as well as any resources included in the module.
154 ImplementationAndResourcesJars android.Paths
155
156 // ImplementationJars is a list of jars that contain the implementations of classes in the
157 //module.
158 ImplementationJars android.Paths
159
160 // ResourceJars is a list of jars that contain the resources included in the module.
161 ResourceJars android.Paths
162
163 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
164 // depending on this module.
165 AidlIncludeDirs android.Paths
166
167 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
168 // module.
169 SrcJarArgs []string
170
171 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
172 SrcJarDeps android.Paths
173
174 // ExportedPlugins is a list of paths that should be used as annotation processors for any
175 // module that depends on this module.
176 ExportedPlugins android.Paths
177
178 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
179 // any module that depends on this module.
180 ExportedPluginClasses []string
181
182 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
183 // requiring disbling turbine for any modules that depend on it.
184 ExportedPluginDisableTurbine bool
185
186 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
187 // instrumented by jacoco.
188 JacocoReportClassesFile android.Path
189}
190
191var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
192
Colin Cross75ce9ec2021-02-26 16:20:32 -0800193// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
194// the sysprop implementation library.
195type SyspropPublicStubInfo struct {
196 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
197 // the sysprop implementation library.
198 JavaInfo JavaInfo
199}
200
201var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
202
Paul Duffin44b481b2020-06-17 16:59:43 +0100203// Methods that need to be implemented for a module that is added to apex java_libs property.
204type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700205 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100206 ImplementationAndResourcesJars() android.Paths
207}
208
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100209// Provides build path and install path to DEX jars.
210type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000211 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100212 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000213 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100214}
215
Jaewoong Jung26342642021-03-17 15:56:23 -0700216// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800217type xref interface {
218 XrefJavaFiles() android.Paths
219}
220
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800221func (j *Module) XrefJavaFiles() android.Paths {
222 return j.kytheFiles
223}
224
Colin Crossbe1da472017-07-07 15:59:46 -0700225type dependencyTag struct {
226 blueprint.BaseDependencyTag
227 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700228}
229
Colin Crosse9fe2942020-11-10 18:12:15 -0800230// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
231// dependency to be installed when the parent module is installed.
232type installDependencyTag struct {
233 blueprint.BaseDependencyTag
234 android.InstallAlwaysNeededDependencyTag
235 name string
236}
237
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100238type usesLibraryDependencyTag struct {
239 dependencyTag
240 sdkVersion int // SDK version in which the library appared as a standalone library.
241}
242
243func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
244 return usesLibraryDependencyTag{
245 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
246 sdkVersion: sdkVersion,
247 }
248}
249
Jiyong Park8be103b2019-11-08 15:53:48 +0900250func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700251 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900252}
253
Colin Crossbe1da472017-07-07 15:59:46 -0700254var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800255 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
256 staticLibTag = dependencyTag{name: "staticlib"}
257 libTag = dependencyTag{name: "javalib"}
258 java9LibTag = dependencyTag{name: "java9lib"}
259 pluginTag = dependencyTag{name: "plugin"}
260 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
261 exportedPluginTag = dependencyTag{name: "exported-plugin"}
262 bootClasspathTag = dependencyTag{name: "bootclasspath"}
263 systemModulesTag = dependencyTag{name: "system modules"}
264 frameworkResTag = dependencyTag{name: "framework-res"}
265 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
266 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
267 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
268 certificateTag = dependencyTag{name: "certificate"}
269 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
270 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
271 jniLibTag = dependencyTag{name: "jnilib"}
272 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
273 jniInstallTag = installDependencyTag{name: "jni install"}
274 binaryInstallTag = installDependencyTag{name: "binary install"}
275 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
276 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
277 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
278 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700279)
Colin Cross2fe66872015-03-30 17:20:39 -0700280
Jiyong Park83dc74b2020-01-14 18:38:44 +0900281func IsLibDepTag(depTag blueprint.DependencyTag) bool {
282 return depTag == libTag
283}
284
285func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
286 return depTag == staticLibTag
287}
288
Colin Crossfc3674a2017-09-18 17:41:52 -0700289type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100290 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700291
Colin Cross6cef4812019-10-17 14:23:50 -0700292 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
293 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100294
295 // The default system modules to use. Will be an empty string if no system
296 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700297 systemModules string
298
Pete Gilline3d44b22020-06-29 11:28:51 +0100299 // The modules that will be added to the classpath regardless of the Java language level targeted
300 classpath []string
301
Colin Cross6cef4812019-10-17 14:23:50 -0700302 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100303 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700304 java9Classpath []string
305
Colin Crossa97c5d32018-03-28 14:58:31 -0700306 frameworkResModule string
307
Colin Cross86a60ae2018-05-29 14:44:55 -0700308 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700309 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100310
311 noStandardLibs, noFrameworksLibs bool
312}
313
314func (s sdkDep) hasStandardLibs() bool {
315 return !s.noStandardLibs
316}
317
318func (s sdkDep) hasFrameworkLibs() bool {
319 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700320}
321
Colin Crossa4f08812018-10-02 22:03:40 -0700322type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700323 name string
324 path android.Path
325 target android.Target
326 coverageFile android.OptionalPath
327 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700328}
329
Jiyong Parkf1691d22021-03-29 20:11:58 +0900330func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700331 sdkDep := decodeSdkDep(ctx, sdkContext)
332 if sdkDep.useModule {
333 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
334 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
335 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
336 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
337 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
338 }
339 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
340 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
341 }
342 }
343 if sdkDep.systemModules != "" {
344 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
345 }
346}
347
Colin Cross32f676a2017-09-06 13:41:06 -0700348type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800349 classpath classpath
350 java9Classpath classpath
351 bootClasspath classpath
352 processorPath classpath
353 errorProneProcessorPath classpath
354 processorClasses []string
355 staticJars android.Paths
356 staticHeaderJars android.Paths
357 staticResourceJars android.Paths
358 aidlIncludeDirs android.Paths
359 srcs android.Paths
360 srcJars android.Paths
361 systemModules *systemModules
362 aidlPreprocess android.OptionalPath
363 kotlinStdlib android.Paths
364 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800365
366 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700367}
Colin Cross2fe66872015-03-30 17:20:39 -0700368
Colin Cross54250902017-12-05 09:28:08 -0800369func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
370 for _, f := range dep.Srcs() {
371 if f.Ext() != ".jar" {
372 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
373 ctx.OtherModuleName(dep.(blueprint.Module)))
374 }
375 }
376}
377
Jiyong Parkf1691d22021-03-29 20:11:58 +0900378func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700379 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700380 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700381 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900382 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700383 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700384 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700385 }
Nan Zhang357466b2018-04-17 17:38:36 -0700386}
387
Colin Cross1e743852019-10-28 11:37:20 -0700388type javaVersion int
389
390const (
391 JAVA_VERSION_UNSUPPORTED = 0
392 JAVA_VERSION_6 = 6
393 JAVA_VERSION_7 = 7
394 JAVA_VERSION_8 = 8
395 JAVA_VERSION_9 = 9
396)
397
398func (v javaVersion) String() string {
399 switch v {
400 case JAVA_VERSION_6:
401 return "1.6"
402 case JAVA_VERSION_7:
403 return "1.7"
404 case JAVA_VERSION_8:
405 return "1.8"
406 case JAVA_VERSION_9:
407 return "1.9"
408 default:
409 return "unsupported"
410 }
411}
412
413// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
414func (v javaVersion) usesJavaModules() bool {
415 return v >= 9
416}
417
418func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100419 switch javaVersion {
420 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700421 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100422 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700423 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100424 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700425 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100426 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700427 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100428 case "10", "11":
429 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700430 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100431 default:
432 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700433 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100434 }
435}
436
Colin Cross2fe66872015-03-30 17:20:39 -0700437//
438// Java libraries (.jar file)
439//
440
Colin Crossf506d872017-07-19 15:53:04 -0700441type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700442 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700443
444 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700445}
446
Jiyong Park45bf82e2020-12-15 22:29:02 +0900447var _ android.ApexModule = (*Library)(nil)
448
Paul Duffine739f1e2020-05-29 11:24:51 +0100449// Provides access to the list of permitted packages from updatable boot jars.
450type PermittedPackagesForUpdatableBootJars interface {
451 PermittedPackagesForUpdatableBootJars() []string
452}
453
454var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
455
456func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
457 return j.properties.Permitted_packages
458}
459
Colin Cross42be7612019-02-21 18:12:14 -0800460func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000461 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700462 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000463 return true
464 }
465
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000466 // Store uncompressed (and do not strip) dex files from boot class path jars.
467 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
468 return true
469 }
470
471 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -0800472 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000473 return true
474 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800475 if ctx.Config().UncompressPrivAppDex() &&
476 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
477 return true
478 }
479
Colin Cross2fc72f62018-12-21 12:59:54 -0800480 return false
481}
482
Colin Crossf506d872017-07-19 15:53:04 -0700483func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin4103e922021-02-01 19:01:34 +0000484 // Initialize the hiddenapi structure. Pass in the configuration name rather than the module name
485 // so the hidden api will encode the <x>.impl java_ library created by java_sdk_library just as it
486 // would the <x> library if <x> was configured as a boot jar.
487 j.initHiddenAPI(ctx, j.ConfigurationName())
488
Jiyong Park92315372021-04-02 08:45:46 +0900489 j.sdkVersion = j.SdkVersion(ctx)
490 j.minSdkVersion = j.MinSdkVersion(ctx)
491
Colin Cross56a83212020-09-15 18:30:11 -0700492 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
493 if !apexInfo.IsForPlatform() {
494 j.hideApexVariantFromMake = true
495 }
496
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100497 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900498 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800499 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700500 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100501 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700502 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100503 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700504 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100505 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700506 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700507
bralee1fbf4402020-05-21 10:11:59 +0800508 // Collect the module directory for IDE info in java/jdeps.go.
509 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
510
Colin Cross56a83212020-09-15 18:30:11 -0700511 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900512 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700513 var extraInstallDeps android.Paths
514 if j.InstallMixin != nil {
515 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
516 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700517 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900518 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700519 }
Colin Crossb7a63242015-04-16 14:09:14 -0700520}
521
Colin Crossf506d872017-07-19 15:53:04 -0700522func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700523 j.deps(ctx)
524}
525
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000526const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000527 aidlIncludeDir = "aidl"
528 javaDir = "java"
529 jarFileSuffix = ".jar"
530 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000531)
532
Paul Duffina0dbf432019-12-05 11:25:53 +0000533// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000534func sdkSnapshotFilePathForJar(osPrefix, name string) string {
535 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000536}
537
Paul Duffina04c1072020-03-02 10:16:35 +0000538func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
539 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000540}
541
Paul Duffin13879572019-11-28 14:31:38 +0000542type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000543 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000544
545 // Function to retrieve the appropriate output jar (implementation or header) from
546 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000547 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
548
549 // Function to compute the snapshot relative path to which the named library's
550 // jar should be copied.
551 snapshotPathGetter func(osPrefix, name string) string
552
553 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
554 // files like aidl files should also be copied.
555 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000556}
557
Paul Duffindb170e42020-12-08 17:48:25 +0000558const (
559 onlyCopyJarToSnapshot = true
560 copyEverythingToSnapshot = false
561)
562
Paul Duffin13879572019-11-28 14:31:38 +0000563func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
564 mctx.AddVariationDependencies(nil, dependencyTag, names...)
565}
566
567func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
568 _, ok := module.(*Library)
569 return ok
570}
571
Paul Duffin3a4eb502020-03-19 16:11:18 +0000572func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
573 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000574}
Paul Duffina0dbf432019-12-05 11:25:53 +0000575
Paul Duffin14eb4672020-03-02 11:33:02 +0000576func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000577 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000578}
579
580type librarySdkMemberProperties struct {
581 android.SdkMemberPropertiesBase
582
Paul Duffin864e1b42020-05-06 10:23:19 +0100583 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000584 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +0000585}
586
Paul Duffin3a4eb502020-03-19 16:11:18 +0000587func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000588 j := variant.(*Library)
589
Paul Duffindb170e42020-12-08 17:48:25 +0000590 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
591
Paul Duffina551a1c2020-03-17 21:04:24 +0000592 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +0000593}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000594
Paul Duffin3a4eb502020-03-19 16:11:18 +0000595func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000596 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000597
Paul Duffindb170e42020-12-08 17:48:25 +0000598 memberType := ctx.MemberType().(*librarySdkMemberType)
599
Paul Duffina551a1c2020-03-17 21:04:24 +0000600 exportedJar := p.JarToExport
601 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000602 // Delegate the creation of the snapshot relative path to the member type.
603 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
604
605 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000606 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
607
Paul Duffina551a1c2020-03-17 21:04:24 +0000608 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
609 }
610
Paul Duffindb170e42020-12-08 17:48:25 +0000611 // Do not copy anything else to the snapshot.
612 if memberType.onlyCopyJarToSnapshot {
613 return
614 }
615
Paul Duffina551a1c2020-03-17 21:04:24 +0000616 aidlIncludeDirs := p.AidlIncludeDirs
617 if len(aidlIncludeDirs) != 0 {
618 sdkModuleContext := ctx.SdkModuleContext()
619 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000620 // TODO(jiyong): copy parcelable declarations only
621 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
622 for _, file := range aidlFiles {
623 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
624 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000625 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000626
Paul Duffina551a1c2020-03-17 21:04:24 +0000627 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000628 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000629}
630
Colin Cross1b16b0e2019-02-12 14:41:32 -0800631// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
632//
633// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
634// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
635// as a `static_libs` dependency of another module.
636//
637// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
638// a device.
639//
640// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
641// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700642func LibraryFactory() android.Module {
643 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700644
Colin Crossce6734e2020-06-15 16:09:53 -0700645 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700646
Paul Duffin859fe962020-05-15 10:20:31 +0100647 module.initModuleAndImport(&module.ModuleBase)
648
Jiyong Park7f7766d2019-07-25 22:02:35 +0900649 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900650 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900651 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700652 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700653}
654
Colin Cross1b16b0e2019-02-12 14:41:32 -0800655// java_library_static is an obsolete alias for java_library.
656func LibraryStaticFactory() android.Module {
657 return LibraryFactory()
658}
659
660// java_library_host builds and links sources into a `.jar` file for the host.
661//
662// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
663// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700664func LibraryHostFactory() android.Module {
665 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700666
Colin Crossce6734e2020-06-15 16:09:53 -0700667 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700668
Colin Cross9ae1b922018-06-26 17:59:05 -0700669 module.Module.properties.Installable = proptools.BoolPtr(true)
670
Jiyong Park7f7766d2019-07-25 22:02:35 +0900671 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900672 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700673 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700674}
675
676//
Colin Crossb628ea52018-08-14 16:42:33 -0700677// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700678//
679
Dan Shi95d19422020-08-15 12:24:26 -0700680// Test option struct.
681type TestOptions struct {
682 // a list of extra test configuration files that should be installed with the module.
683 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800684
685 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
686 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700687}
688
Colin Cross05638fc2018-04-09 18:40:24 -0700689type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700690 // list of compatibility suites (for example "cts", "vts") that the module should be
691 // installed into.
692 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700693
694 // the name of the test configuration (for example "AndroidTest.xml") that should be
695 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800696 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700697
Jack He33338892018-09-19 02:21:28 -0700698 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
699 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800700 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700701
Colin Crossd96ca352018-08-10 16:06:24 -0700702 // list of files or filegroup modules that provide data that should be installed alongside
703 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900704 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700705
706 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
707 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
708 // explicitly.
709 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800710
711 // Add parameterized mainline modules to auto generated test config. The options will be
712 // handled by TradeFed to do downloading and installing the specified modules on the device.
713 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700714
715 // Test options.
716 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800717
718 // Names of modules containing JNI libraries that should be installed alongside the test.
719 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700720}
721
Liz Kammerdd849a82020-06-12 16:38:45 -0700722type hostTestProperties struct {
723 // list of native binary modules that should be installed alongside the test
724 Data_native_bins []string `android:"arch_variant"`
725}
726
Paul Duffin42df1442019-03-20 12:45:53 +0000727type testHelperLibraryProperties struct {
728 // list of compatibility suites (for example "cts", "vts") that the module should be
729 // installed into.
730 Test_suites []string `android:"arch_variant"`
731}
732
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000733type prebuiltTestProperties struct {
734 // list of compatibility suites (for example "cts", "vts") that the module should be
735 // installed into.
736 Test_suites []string `android:"arch_variant"`
737
738 // the name of the test configuration (for example "AndroidTest.xml") that should be
739 // installed with the module.
740 Test_config *string `android:"path,arch_variant"`
741}
742
Colin Cross05638fc2018-04-09 18:40:24 -0700743type Test struct {
744 Library
745
746 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700747
Dan Shi95d19422020-08-15 12:24:26 -0700748 testConfig android.Path
749 extraTestConfigs android.Paths
750 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700751}
752
Liz Kammerdd849a82020-06-12 16:38:45 -0700753type TestHost struct {
754 Test
755
756 testHostProperties hostTestProperties
757}
758
Paul Duffin42df1442019-03-20 12:45:53 +0000759type TestHelperLibrary struct {
760 Library
761
762 testHelperLibraryProperties testHelperLibraryProperties
763}
764
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000765type JavaTestImport struct {
766 Import
767
768 prebuiltTestProperties prebuiltTestProperties
769
770 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700771 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000772}
773
Liz Kammerdd849a82020-06-12 16:38:45 -0700774func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
775 if len(j.testHostProperties.Data_native_bins) > 0 {
776 for _, target := range ctx.MultiTargets() {
777 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
778 }
779 }
780
Colin Crossf8d9c492021-01-26 11:01:43 -0800781 if len(j.testProperties.Jni_libs) > 0 {
782 for _, target := range ctx.MultiTargets() {
783 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
784 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
785 }
786 }
787
Liz Kammerdd849a82020-06-12 16:38:45 -0700788 j.deps(ctx)
789}
790
Yuexi Ma627263f2021-03-04 13:47:56 -0800791func (j *TestHost) AddExtraResource(p android.Path) {
792 j.extraResources = append(j.extraResources, p)
793}
794
Colin Cross303e21f2018-08-07 16:49:25 -0700795func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000796 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
797 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700798 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000799 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
800 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700801 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800802 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700803
Colin Cross8a497952019-03-05 22:25:09 -0800804 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700805
Dan Shi95d19422020-08-15 12:24:26 -0700806 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
807
Liz Kammerdd849a82020-06-12 16:38:45 -0700808 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
809 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
810 })
811
Colin Crossf8d9c492021-01-26 11:01:43 -0800812 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
813 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
814 if sharedLibInfo.SharedLibrary != nil {
815 // Copy to an intermediate output directory to append "lib[64]" to the path,
816 // so that it's compatible with the default rpath values.
817 var relPath string
818 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
819 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
820 } else {
821 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
822 }
823 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
824 ctx.Build(pctx, android.BuildParams{
825 Rule: android.Cp,
826 Input: sharedLibInfo.SharedLibrary,
827 Output: relocatedLib,
828 })
829 j.data = append(j.data, relocatedLib)
830 } else {
831 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
832 }
833 })
834
Colin Cross303e21f2018-08-07 16:49:25 -0700835 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700836}
837
Paul Duffin42df1442019-03-20 12:45:53 +0000838func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
839 j.Library.GenerateAndroidBuildActions(ctx)
840}
841
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000842func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
843 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800844 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000845
846 j.Import.GenerateAndroidBuildActions(ctx)
847}
848
849type testSdkMemberType struct {
850 android.SdkMemberTypeBase
851}
852
853func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
854 mctx.AddVariationDependencies(nil, dependencyTag, names...)
855}
856
857func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
858 _, ok := module.(*Test)
859 return ok
860}
861
Paul Duffin3a4eb502020-03-19 16:11:18 +0000862func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
863 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000864}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000865
Paul Duffin14eb4672020-03-02 11:33:02 +0000866func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
867 return &testSdkMemberProperties{}
868}
869
870type testSdkMemberProperties struct {
871 android.SdkMemberPropertiesBase
872
Paul Duffina551a1c2020-03-17 21:04:24 +0000873 JarToExport android.Path
874 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000875}
876
Paul Duffin3a4eb502020-03-19 16:11:18 +0000877func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000878 test := variant.(*Test)
879
880 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000881 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000882 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000883 }
884
Paul Duffina551a1c2020-03-17 21:04:24 +0000885 p.JarToExport = implementationJars[0]
886 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000887}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000888
Paul Duffin3a4eb502020-03-19 16:11:18 +0000889func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000890 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000891
Paul Duffina551a1c2020-03-17 21:04:24 +0000892 exportedJar := p.JarToExport
893 if exportedJar != nil {
894 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
895 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000896
897 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000898 }
899
900 testConfig := p.TestConfig
901 if testConfig != nil {
902 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
903 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000904 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
905 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000906}
907
Colin Cross1b16b0e2019-02-12 14:41:32 -0800908// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
909// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
910//
911// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
912// compiled against the device bootclasspath.
913//
914// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
915// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700916func TestFactory() android.Module {
917 module := &Test{}
918
Colin Crossce6734e2020-06-15 16:09:53 -0700919 module.addHostAndDeviceProperties()
920 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700921
Colin Cross9ae1b922018-06-26 17:59:05 -0700922 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800923 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700924 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700925
Colin Cross05638fc2018-04-09 18:40:24 -0700926 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700927 return module
928}
929
Paul Duffin42df1442019-03-20 12:45:53 +0000930// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
931func TestHelperLibraryFactory() android.Module {
932 module := &TestHelperLibrary{}
933
Colin Crossce6734e2020-06-15 16:09:53 -0700934 module.addHostAndDeviceProperties()
935 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000936
Colin Cross9a4abed2019-04-24 13:19:28 -0700937 module.Module.properties.Installable = proptools.BoolPtr(true)
938 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700939 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700940
Paul Duffin42df1442019-03-20 12:45:53 +0000941 InitJavaModule(module, android.HostAndDeviceSupported)
942 return module
943}
944
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000945// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
946// and makes sure that it is added to the appropriate test suite.
947//
948// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
949// compiled against an Android classpath.
950//
951// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
952// for host modules.
953func JavaTestImportFactory() android.Module {
954 module := &JavaTestImport{}
955
956 module.AddProperties(
957 &module.Import.properties,
958 &module.prebuiltTestProperties)
959
960 module.Import.properties.Installable = proptools.BoolPtr(true)
961
962 android.InitPrebuiltModule(module, &module.properties.Jars)
963 android.InitApexModule(module)
964 android.InitSdkAwareModule(module)
965 InitJavaModule(module, android.HostAndDeviceSupported)
966 return module
967}
968
Colin Cross1b16b0e2019-02-12 14:41:32 -0800969// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
970// allow running the test with `atest` or a `TEST_MAPPING` file.
971//
972// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
973// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700974func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700975 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700976
Colin Crossce6734e2020-06-15 16:09:53 -0700977 module.addHostProperties()
978 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700979 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700980
Yuexi Ma627263f2021-03-04 13:47:56 -0800981 InitTestHost(
982 module,
983 proptools.BoolPtr(true),
984 nil,
985 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -0700986
Liz Kammerdd849a82020-06-12 16:38:45 -0700987 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +0000988
Colin Cross05638fc2018-04-09 18:40:24 -0700989 return module
990}
991
Yuexi Ma627263f2021-03-04 13:47:56 -0800992func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
993 th.properties.Installable = installable
994 th.testProperties.Auto_gen_config = autoGenConfig
995 th.testProperties.Test_suites = testSuites
996}
997
Colin Cross05638fc2018-04-09 18:40:24 -0700998//
Colin Cross2fe66872015-03-30 17:20:39 -0700999// Java Binaries (.jar file plus wrapper script)
1000//
1001
Colin Crossf506d872017-07-19 15:53:04 -07001002type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001003 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001004 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001005
1006 // Name of the class containing main to be inserted into the manifest as Main-Class.
1007 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001008
1009 // Names of modules containing JNI libraries that should be installed alongside the host
1010 // variant of the binary.
1011 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -07001012}
1013
Colin Crossf506d872017-07-19 15:53:04 -07001014type Binary struct {
1015 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001016
Colin Crossf506d872017-07-19 15:53:04 -07001017 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001018
Colin Cross6b4a32d2017-12-05 13:42:45 -08001019 isWrapperVariant bool
1020
Colin Crossc3315992017-12-08 19:12:36 -08001021 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001022 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001023}
1024
Alex Light24237172017-10-26 09:46:21 -07001025func (j *Binary) HostToolPath() android.OptionalPath {
1026 return android.OptionalPathForPath(j.binaryFile)
1027}
1028
Colin Crossf506d872017-07-19 15:53:04 -07001029func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001030 if ctx.Arch().ArchType == android.Common {
1031 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001032 if j.binaryProperties.Main_class != nil {
1033 if j.properties.Manifest != nil {
1034 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1035 }
1036 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1037 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1038 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1039 }
1040
Colin Cross6b4a32d2017-12-05 13:42:45 -08001041 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001042 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001043 // Handle the binary wrapper
1044 j.isWrapperVariant = true
1045
Colin Cross366938f2017-12-11 16:29:02 -08001046 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001047 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001048 } else {
1049 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1050 }
1051
Colin Crossc179ea62020-10-09 10:54:15 -07001052 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001053 // of the wrapper variant, which will include the common variant's jar file and any JNI
1054 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001055 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001056 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001057 }
Colin Cross2fe66872015-03-30 17:20:39 -07001058}
1059
Colin Crossf506d872017-07-19 15:53:04 -07001060func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001061 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001062 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001063 }
1064 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001065 // These dependencies ensure the host installation rules will install the jar file and
1066 // the jni libraries when the wrapper is installed.
1067 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1068 ctx.AddVariationDependencies(
1069 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1070 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001071 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001072}
1073
Colin Cross1b16b0e2019-02-12 14:41:32 -08001074// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1075// as well.
1076//
1077// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1078// compiled against the device bootclasspath.
1079//
1080// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1081// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001082func BinaryFactory() android.Module {
1083 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001084
Colin Crossce6734e2020-06-15 16:09:53 -07001085 module.addHostAndDeviceProperties()
1086 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001087
Colin Cross9ae1b922018-06-26 17:59:05 -07001088 module.Module.properties.Installable = proptools.BoolPtr(true)
1089
Colin Cross6b4a32d2017-12-05 13:42:45 -08001090 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1091 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001092 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001093}
1094
Colin Cross1b16b0e2019-02-12 14:41:32 -08001095// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1096//
1097// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1098// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001099func BinaryHostFactory() android.Module {
1100 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001101
Colin Crossce6734e2020-06-15 16:09:53 -07001102 module.addHostProperties()
1103 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001104
Colin Cross9ae1b922018-06-26 17:59:05 -07001105 module.Module.properties.Installable = proptools.BoolPtr(true)
1106
Colin Cross6b4a32d2017-12-05 13:42:45 -08001107 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1108 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001109 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001110}
1111
1112//
1113// Java prebuilts
1114//
1115
Colin Cross74d73e22017-08-02 11:05:49 -07001116type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001117 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001118
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001119 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1120 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001121 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001122
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001123 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1124 // specified.
1125 Min_sdk_version *string
1126
Colin Cross535e2cf2017-10-20 17:57:49 -07001127 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001128
1129 // List of shared java libs that this module has dependencies to
1130 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001131
1132 // List of files to remove from the jar file(s)
1133 Exclude_files []string
1134
1135 // List of directories to remove from the jar file(s)
1136 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001137
1138 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001139 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001140
1141 // set the name of the output
1142 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001143
1144 Aidl struct {
1145 // directories that should be added as include directories for any aidl sources of modules
1146 // that depend on this module, as well as to aidl for this module.
1147 Export_include_dirs []string
1148 }
Colin Cross74d73e22017-08-02 11:05:49 -07001149}
1150
1151type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001152 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001153 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001154 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001155 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001156 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001157
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001158 // Functionality common to Module and Import.
1159 embeddableInModuleAndImport
1160
Liz Kammerd6c31d22020-08-05 15:40:41 -07001161 hiddenAPI
1162 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001163 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001164
Colin Cross74d73e22017-08-02 11:05:49 -07001165 properties ImportProperties
1166
Liz Kammerd6c31d22020-08-05 15:40:41 -07001167 // output file containing classes.dex and resources
1168 dexJarFile android.Path
1169
Colin Cross0a6e0072017-08-30 14:24:55 -07001170 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001171 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001172 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001173
1174 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001175
1176 sdkVersion android.SdkSpec
1177 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001178}
1179
Jiyong Park92315372021-04-02 08:45:46 +09001180func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1181 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001182}
1183
Jiyong Parkf1691d22021-03-29 20:11:58 +09001184func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001185 return "none"
1186}
1187
Jiyong Park92315372021-04-02 08:45:46 +09001188func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001189 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001190 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001191 }
Jiyong Park92315372021-04-02 08:45:46 +09001192 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001193}
1194
Jiyong Park92315372021-04-02 08:45:46 +09001195func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1196 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001197}
1198
Colin Cross74d73e22017-08-02 11:05:49 -07001199func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001200 return &j.prebuilt
1201}
1202
Colin Cross74d73e22017-08-02 11:05:49 -07001203func (j *Import) PrebuiltSrcs() []string {
1204 return j.properties.Jars
1205}
1206
1207func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001208 return j.prebuilt.Name(j.ModuleBase.Name())
1209}
1210
Jiyong Park0b238752019-10-29 11:23:10 +09001211func (j *Import) Stem() string {
1212 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1213}
1214
Jiyong Park618922e2020-01-08 13:35:43 +09001215func (a *Import) JacocoReportClassesFile() android.Path {
1216 return nil
1217}
1218
Bill Peckhama41a6962021-01-11 10:58:54 -08001219func (j *Import) LintDepSets() LintDepSets {
1220 return LintDepSets{}
1221}
1222
Colin Cross74d73e22017-08-02 11:05:49 -07001223func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001224 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001225
1226 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001227 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001228 }
Colin Cross1e676be2016-10-12 14:38:15 -07001229}
1230
Colin Cross74d73e22017-08-02 11:05:49 -07001231func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001232 j.sdkVersion = j.SdkVersion(ctx)
1233 j.minSdkVersion = j.MinSdkVersion(ctx)
1234
Paul Duffin4103e922021-02-01 19:01:34 +00001235 // Initialize the hiddenapi structure.
1236 j.initHiddenAPI(ctx, j.BaseModuleName())
1237
Colin Cross56a83212020-09-15 18:30:11 -07001238 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1239 j.hideApexVariantFromMake = true
1240 }
1241
Colin Cross8a497952019-03-05 22:25:09 -08001242 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001243
Jiyong Park0b238752019-10-29 11:23:10 +09001244 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001245 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001246 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1247 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001248 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001249 inputFile := outputFile
1250 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1251 TransformJetifier(ctx, outputFile, inputFile)
1252 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001253 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001254 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001255
Liz Kammerd6c31d22020-08-05 15:40:41 -07001256 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001257 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001258
Jiyong Park1be96912018-05-28 18:02:19 +09001259 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001260 tag := ctx.OtherModuleDependencyTag(module)
1261
Colin Crossdcf71b22021-02-01 13:59:03 -08001262 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1263 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001264 switch tag {
1265 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001266 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001267 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001268 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001269 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001270 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001271 switch tag {
1272 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001273 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001274 }
1275 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001276
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001277 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001278
1279 // Save away the `deapexer` module on which this depends, if any.
1280 if tag == android.DeapexerTag {
1281 deapexerModule = module
1282 }
Jiyong Park1be96912018-05-28 18:02:19 +09001283 })
1284
Nan Zhang4973ecf2018-08-10 13:42:12 -07001285 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001286 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001287 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001288 }
Jiyong Park19604de2020-03-24 16:44:11 +09001289
1290 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001291
Paul Duffin064b70c2020-11-02 17:32:38 +00001292 if ctx.Device() {
1293 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1294 // obtained from the associated deapexer module.
1295 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1296 if ai.ForPrebuiltApex {
1297 if deapexerModule == nil {
1298 // This should never happen as a variant for a prebuilt_apex is only created if the
Paul Duffinb17d0442021-05-05 12:07:00 +01001299 // deapexer module has been configured to export the dex implementation jar for this module.
Paul Duffin064b70c2020-11-02 17:32:38 +00001300 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1301 j.Name(), ai.ApexVariationName)
Paul Duffinb17d0442021-05-05 12:07:00 +01001302 return
Paul Duffin064b70c2020-11-02 17:32:38 +00001303 }
1304
1305 // Get the path of the dex implementation jar from the `deapexer` module.
1306 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001307 if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
1308 j.dexJarFile = dexOutputPath
Paul Duffinf75e5272021-02-09 14:34:25 +00001309 j.hiddenAPIExtractInformation(ctx, dexOutputPath, outputFile)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001310 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001311 // This should never happen as a variant for a prebuilt_apex is only created if the
1312 // prebuilt_apex has been configured to export the java library dex file.
1313 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1314 }
1315 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001316 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001317 if sdkDep.invalidVersion {
1318 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1319 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1320 } else if sdkDep.useFiles {
1321 // sdkDep.jar is actually equivalent to turbine header.jar.
1322 flags.classpath = append(flags.classpath, sdkDep.jars...)
1323 }
1324
1325 // Dex compilation
1326
1327 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1328 if j.dexProperties.Uncompress_dex == nil {
1329 // If the value was not force-set by the user, use reasonable default based on the module.
1330 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1331 }
1332 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1333
Paul Duffin612e6102021-02-02 13:38:13 +00001334 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001335 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001336 if ctx.Failed() {
1337 return
1338 }
1339
Paul Duffin064b70c2020-11-02 17:32:38 +00001340 // Hidden API CSV generation and dex encoding
Paul Duffinf75e5272021-02-09 14:34:25 +00001341 dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, outputFile,
Paul Duffin064b70c2020-11-02 17:32:38 +00001342 proptools.Bool(j.dexProperties.Uncompress_dex))
1343
1344 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001345 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001346 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001347
1348 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1349 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1350 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1351 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1352 AidlIncludeDirs: j.exportAidlIncludeDirs,
1353 })
Colin Cross2fe66872015-03-30 17:20:39 -07001354}
1355
Paul Duffinaa55f742020-10-06 17:20:13 +01001356func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1357 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001358 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001359 return android.Paths{j.combinedClasspathFile}, nil
1360 default:
1361 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1362 }
1363}
1364
1365var _ android.OutputFileProducer = (*Import)(nil)
1366
Nan Zhanged19fc32017-10-19 13:06:22 -07001367func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001368 if j.combinedClasspathFile == nil {
1369 return nil
1370 }
Colin Cross37f6d792018-07-12 12:28:41 -07001371 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001372}
1373
Colin Cross331a1212018-08-15 20:40:52 -07001374func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001375 if j.combinedClasspathFile == nil {
1376 return nil
1377 }
Colin Cross331a1212018-08-15 20:40:52 -07001378 return android.Paths{j.combinedClasspathFile}
1379}
1380
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001381func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001382 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001383}
1384
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001385func (j *Import) DexJarInstallPath() android.Path {
1386 return nil
1387}
1388
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001389func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1390 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001391}
1392
Jiyong Park45bf82e2020-12-15 22:29:02 +09001393var _ android.ApexModule = (*Import)(nil)
1394
1395// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001396func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001397 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001398}
1399
Jiyong Park45bf82e2020-12-15 22:29:02 +09001400// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001401func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1402 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001403 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001404 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001405 return fmt.Errorf("min_sdk_version is not specified")
1406 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001407 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001408 return nil
1409 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001410 ver, err := sdkSpec.EffectiveVersion(ctx)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001411 if err != nil {
1412 return err
1413 }
Jiyong Park54105c42021-03-31 18:17:53 +09001414 if ver.GreaterThan(sdkVersion) {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001415 return fmt.Errorf("newer SDK(%v)", ver)
1416 }
Jooyung Han749dc692020-04-15 11:03:39 +09001417 return nil
1418}
1419
albaltai36ff7dc2018-12-25 14:35:23 +08001420// Add compile time check for interface implementation
1421var _ android.IDEInfo = (*Import)(nil)
1422var _ android.IDECustomizedModuleName = (*Import)(nil)
1423
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001424// Collect information for opening IDE project files in java/jdeps.go.
1425const (
1426 removedPrefix = "prebuilt_"
1427)
1428
1429func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1430 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1431}
1432
1433func (j *Import) IDECustomizedModuleName() string {
1434 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1435 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1436 // solution to get the Import name.
1437 name := j.Name()
1438 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001439 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001440 }
1441 return name
1442}
1443
Colin Cross74d73e22017-08-02 11:05:49 -07001444var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001445
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001446func (j *Import) IsInstallable() bool {
1447 return Bool(j.properties.Installable)
1448}
1449
1450var _ dexpreopterInterface = (*Import)(nil)
1451
Colin Cross1b16b0e2019-02-12 14:41:32 -08001452// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1453//
1454// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1455// compiled against an Android classpath.
1456//
1457// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1458// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001459func ImportFactory() android.Module {
1460 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001461
Liz Kammerd6c31d22020-08-05 15:40:41 -07001462 module.AddProperties(
1463 &module.properties,
1464 &module.dexer.dexProperties,
1465 )
Colin Cross74d73e22017-08-02 11:05:49 -07001466
Paul Duffin859fe962020-05-15 10:20:31 +01001467 module.initModuleAndImport(&module.ModuleBase)
1468
Liz Kammerd6c31d22020-08-05 15:40:41 -07001469 module.dexProperties.Optimize.EnabledByDefault = false
1470
Colin Cross74d73e22017-08-02 11:05:49 -07001471 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001472 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001473 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001474 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001475 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001476}
1477
Colin Cross1b16b0e2019-02-12 14:41:32 -08001478// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1479// module.
1480//
1481// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1482// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001483func ImportFactoryHost() android.Module {
1484 module := &Import{}
1485
1486 module.AddProperties(&module.properties)
1487
1488 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001489 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001490 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001491 return module
1492}
1493
Colin Cross42be7612019-02-21 18:12:14 -08001494// dex_import module
1495
1496type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001497 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001498
1499 // set the name of the output
1500 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001501}
1502
1503type DexImport struct {
1504 android.ModuleBase
1505 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001506 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001507 prebuilt android.Prebuilt
1508
1509 properties DexImportProperties
1510
Colin Crossb014f072021-02-26 14:54:36 -08001511 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001512
1513 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001514
1515 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001516}
1517
1518func (j *DexImport) Prebuilt() *android.Prebuilt {
1519 return &j.prebuilt
1520}
1521
1522func (j *DexImport) PrebuiltSrcs() []string {
1523 return j.properties.Jars
1524}
1525
1526func (j *DexImport) Name() string {
1527 return j.prebuilt.Name(j.ModuleBase.Name())
1528}
1529
Jiyong Park0b238752019-10-29 11:23:10 +09001530func (j *DexImport) Stem() string {
1531 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1532}
1533
Jiyong Park77acec62020-06-01 21:39:15 +09001534func (a *DexImport) JacocoReportClassesFile() android.Path {
1535 return nil
1536}
1537
Colin Cross08dca382020-07-21 20:31:17 -07001538func (a *DexImport) LintDepSets() LintDepSets {
1539 return LintDepSets{}
1540}
1541
Martin Stjernholm6d415272020-01-31 17:10:36 +00001542func (j *DexImport) IsInstallable() bool {
1543 return true
1544}
1545
Colin Cross42be7612019-02-21 18:12:14 -08001546func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1547 if len(j.properties.Jars) != 1 {
1548 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1549 }
1550
Colin Cross56a83212020-09-15 18:30:11 -07001551 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1552 if !apexInfo.IsForPlatform() {
1553 j.hideApexVariantFromMake = true
1554 }
1555
Jiyong Park0b238752019-10-29 11:23:10 +09001556 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001557 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1558
1559 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1560 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1561
1562 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001563 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001564
1565 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1566 rule.Temporary(temporary)
1567
1568 // use zip2zip to uncompress classes*.dex files
1569 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001570 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001571 FlagWithInput("-i ", inputJar).
1572 FlagWithOutput("-o ", temporary).
1573 FlagWithArg("-0 ", "'classes*.dex'")
1574
1575 // use zipalign to align uncompressed classes*.dex files
1576 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001577 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001578 Flag("-f").
1579 Text("4").
1580 Input(temporary).
1581 Output(dexOutputFile)
1582
1583 rule.DeleteTemporaryFiles()
1584
Colin Crossf1a035e2020-11-16 17:32:30 -08001585 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001586 } else {
1587 ctx.Build(pctx, android.BuildParams{
1588 Rule: android.Cp,
1589 Input: inputJar,
1590 Output: dexOutputFile,
1591 })
1592 }
1593
1594 j.dexJarFile = dexOutputFile
1595
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001596 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001597
Colin Cross56a83212020-09-15 18:30:11 -07001598 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001599 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1600 j.Stem()+".jar", dexOutputFile)
1601 }
Colin Cross42be7612019-02-21 18:12:14 -08001602}
1603
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001604func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001605 return j.dexJarFile
1606}
1607
Jiyong Park45bf82e2020-12-15 22:29:02 +09001608var _ android.ApexModule = (*DexImport)(nil)
1609
1610// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001611func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1612 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001613 // we don't check prebuilt modules for sdk_version
1614 return nil
1615}
1616
Colin Cross42be7612019-02-21 18:12:14 -08001617// dex_import imports a `.jar` file containing classes.dex files.
1618//
1619// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1620// to the device.
1621func DexImportFactory() android.Module {
1622 module := &DexImport{}
1623
1624 module.AddProperties(&module.properties)
1625
1626 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001627 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001628 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001629 return module
1630}
1631
Colin Cross89536d42017-07-07 14:35:50 -07001632//
1633// Defaults
1634//
1635type Defaults struct {
1636 android.ModuleBase
1637 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001638 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001639}
1640
Colin Cross1b16b0e2019-02-12 14:41:32 -08001641// java_defaults provides a set of properties that can be inherited by other java or android modules.
1642//
1643// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1644// property in the defaults module that exists in the depending module will be prepended to the depending module's
1645// value for that property.
1646//
1647// Example:
1648//
1649// java_defaults {
1650// name: "example_defaults",
1651// srcs: ["common/**/*.java"],
1652// javacflags: ["-Xlint:all"],
1653// aaptflags: ["--auto-add-overlay"],
1654// }
1655//
1656// java_library {
1657// name: "example",
1658// defaults: ["example_defaults"],
1659// srcs: ["example/**/*.java"],
1660// }
1661//
1662// is functionally identical to:
1663//
1664// java_library {
1665// name: "example",
1666// srcs: [
1667// "common/**/*.java",
1668// "example/**/*.java",
1669// ],
1670// javacflags: ["-Xlint:all"],
1671// }
Paul Duffin47357662019-12-05 14:07:14 +00001672func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001673 module := &Defaults{}
1674
Colin Cross89536d42017-07-07 14:35:50 -07001675 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001676 &CommonProperties{},
1677 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001678 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001679 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001680 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001681 &aaptProperties{},
1682 &androidLibraryProperties{},
1683 &appProperties{},
1684 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001685 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001686 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001687 &ImportProperties{},
1688 &AARImportProperties{},
1689 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001690 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001691 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001692 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001693 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001694 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001695 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001696 )
1697
1698 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001699 return module
1700}
Nan Zhangea568a42017-11-08 21:20:04 -08001701
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001702func kytheExtractJavaFactory() android.Singleton {
1703 return &kytheExtractJavaSingleton{}
1704}
1705
1706type kytheExtractJavaSingleton struct {
1707}
1708
1709func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1710 var xrefTargets android.Paths
1711 ctx.VisitAllModules(func(module android.Module) {
1712 if javaModule, ok := module.(xref); ok {
1713 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1714 }
1715 })
1716 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1717 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001718 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001719 }
1720}
1721
Nan Zhangea568a42017-11-08 21:20:04 -08001722var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001723var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001724var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001725var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001726
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001727// Add class loader context (CLC) of a given dependency to the current CLC.
1728func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1729 clcMap dexpreopt.ClassLoaderContextMap) {
1730
1731 dep, ok := depModule.(UsesLibraryDependency)
1732 if !ok {
1733 return
1734 }
1735
1736 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1737 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1738 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1739 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1740 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1741 // the current CLC.
1742 var implicitSdkLib *string
1743 comp, isComp := depModule.(SdkLibraryComponentDependency)
1744 if isComp {
1745 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1746 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1747 }
1748 if implicitSdkLib == nil {
1749 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1750 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001751 }
1752 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001753
1754 depTag := ctx.OtherModuleDependencyTag(depModule)
1755 if depTag == libTag || depTag == usesLibTag {
1756 // Ok, propagate <uses-library> through non-static library dependencies.
1757 } else if depTag == staticLibTag {
1758 // Propagate <uses-library> through static library dependencies, unless it is a component
1759 // library (such as stubs). Component libraries have a dependency on their SDK library,
1760 // which should not be pulled just because of a static component library.
1761 if implicitSdkLib != nil {
1762 return
1763 }
1764 } else {
1765 // Don't propagate <uses-library> for other dependency tags.
1766 return
1767 }
1768
1769 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001770 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001771 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1772 } else {
1773 depName := ctx.OtherModuleName(depModule)
1774 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1775 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001776}