blob: 0770f7ffb78a48d60cc6a17a52757ac9cc459776 [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
25 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070026 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080029 "android/soong/cc"
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
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +010059 // This mutator registers dependencies on dex2oat for modules that should be
60 // dexpreopted. This is done late when the final variants have been
61 // established, to not get the dependencies split into the wrong variants and
62 // to support the checks in dexpreoptDisabled().
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080063 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
64 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
65 })
66
67 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
68 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
69}
70
71func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000072 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000073 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010074 android.RegisterSdkMemberType(javaLibsSdkMemberType)
75 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
76 android.RegisterSdkMemberType(javaTestSdkMemberType)
77}
78
79var (
80 // Supports adding java header libraries to module_exports and sdk.
81 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
82 android.SdkMemberTypeBase{
83 PropertyName: "java_header_libs",
84 SupportsSdk: true,
85 },
86 func(_ android.SdkMemberContext, j *Library) android.Path {
87 headerJars := j.HeaderJars()
88 if len(headerJars) != 1 {
89 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
90 }
91
92 return headerJars[0]
93 },
94 sdkSnapshotFilePathForJar,
95 copyEverythingToSnapshot,
96 }
Paul Duffin255f18e2019-12-13 11:22:16 +000097
Paul Duffin22ff0aa2021-02-04 11:15:34 +000098 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +010099 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000100 implementationJars := j.ImplementationAndResourcesJars()
101 if len(implementationJars) != 1 {
102 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
103 }
104 return implementationJars[0]
105 }
106
Paul Duffin2da04242021-04-23 19:43:28 +0100107 // Supports adding java implementation libraries to module_exports but not sdk.
108 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000109 android.SdkMemberTypeBase{
110 PropertyName: "java_libs",
111 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000112 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000113 sdkSnapshotFilePathForJar,
114 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100115 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000116
Paul Duffin2da04242021-04-23 19:43:28 +0100117 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000118 //
119 // The build has some implicit dependencies (via the boot jars configuration) on a number of
120 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
121 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
122 // used outside those mainline modules.
123 //
124 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
125 // either java_libs, or java_header_libs would end up exporting more information than was strictly
126 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
127 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100128 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000129 android.SdkMemberTypeBase{
130 PropertyName: "java_boot_libs",
131 SupportsSdk: true,
132 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000133 // Temporarily export implementation classes jar for java_boot_libs as it is required for the
134 // hiddenapi processing.
135 // TODO(b/179354495): Revert once hiddenapi processing has been modularized.
136 exportImplementationClassesJar,
137 sdkSnapshotFilePathForJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000138 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100139 }
Paul Duffindb170e42020-12-08 17:48:25 +0000140
Paul Duffin2da04242021-04-23 19:43:28 +0100141 // Supports adding java test libraries to module_exports but not sdk.
142 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000143 SdkMemberTypeBase: android.SdkMemberTypeBase{
144 PropertyName: "java_tests",
145 },
Paul Duffin2da04242021-04-23 19:43:28 +0100146 }
147)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148
Colin Crossdcf71b22021-02-01 13:59:03 -0800149// JavaInfo contains information about a java module for use by modules that depend on it.
150type JavaInfo struct {
151 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
152 // against this module. If empty, ImplementationJars should be used instead.
153 HeaderJars android.Paths
154
155 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
156 // in the module as well as any resources included in the module.
157 ImplementationAndResourcesJars android.Paths
158
159 // ImplementationJars is a list of jars that contain the implementations of classes in the
160 //module.
161 ImplementationJars android.Paths
162
163 // ResourceJars is a list of jars that contain the resources included in the module.
164 ResourceJars android.Paths
165
166 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
167 // depending on this module.
168 AidlIncludeDirs android.Paths
169
170 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
171 // module.
172 SrcJarArgs []string
173
174 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
175 SrcJarDeps android.Paths
176
177 // ExportedPlugins is a list of paths that should be used as annotation processors for any
178 // module that depends on this module.
179 ExportedPlugins android.Paths
180
181 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
182 // any module that depends on this module.
183 ExportedPluginClasses []string
184
185 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
186 // requiring disbling turbine for any modules that depend on it.
187 ExportedPluginDisableTurbine bool
188
189 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
190 // instrumented by jacoco.
191 JacocoReportClassesFile android.Path
192}
193
194var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
195
Colin Cross75ce9ec2021-02-26 16:20:32 -0800196// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
197// the sysprop implementation library.
198type SyspropPublicStubInfo struct {
199 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
200 // the sysprop implementation library.
201 JavaInfo JavaInfo
202}
203
204var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
205
Paul Duffin44b481b2020-06-17 16:59:43 +0100206// Methods that need to be implemented for a module that is added to apex java_libs property.
207type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700208 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100209 ImplementationAndResourcesJars() android.Paths
210}
211
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100212// Provides build path and install path to DEX jars.
213type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000214 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100215 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000216 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100217}
218
Jaewoong Jung26342642021-03-17 15:56:23 -0700219// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800220type xref interface {
221 XrefJavaFiles() android.Paths
222}
223
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800224func (j *Module) XrefJavaFiles() android.Paths {
225 return j.kytheFiles
226}
227
Colin Crossbe1da472017-07-07 15:59:46 -0700228type dependencyTag struct {
229 blueprint.BaseDependencyTag
230 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700231}
232
Colin Crosse9fe2942020-11-10 18:12:15 -0800233// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
234// dependency to be installed when the parent module is installed.
235type installDependencyTag struct {
236 blueprint.BaseDependencyTag
237 android.InstallAlwaysNeededDependencyTag
238 name string
239}
240
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100241type usesLibraryDependencyTag struct {
242 dependencyTag
243 sdkVersion int // SDK version in which the library appared as a standalone library.
244}
245
246func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
247 return usesLibraryDependencyTag{
248 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
249 sdkVersion: sdkVersion,
250 }
251}
252
Jiyong Park8be103b2019-11-08 15:53:48 +0900253func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700254 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900255}
256
Colin Crossbe1da472017-07-07 15:59:46 -0700257var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800258 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
259 staticLibTag = dependencyTag{name: "staticlib"}
260 libTag = dependencyTag{name: "javalib"}
261 java9LibTag = dependencyTag{name: "java9lib"}
262 pluginTag = dependencyTag{name: "plugin"}
263 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
264 exportedPluginTag = dependencyTag{name: "exported-plugin"}
265 bootClasspathTag = dependencyTag{name: "bootclasspath"}
266 systemModulesTag = dependencyTag{name: "system modules"}
267 frameworkResTag = dependencyTag{name: "framework-res"}
268 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
269 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
270 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
271 certificateTag = dependencyTag{name: "certificate"}
272 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
273 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
274 jniLibTag = dependencyTag{name: "jnilib"}
275 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
276 jniInstallTag = installDependencyTag{name: "jni install"}
277 binaryInstallTag = installDependencyTag{name: "binary install"}
278 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
279 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
280 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
281 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700282)
Colin Cross2fe66872015-03-30 17:20:39 -0700283
Jiyong Park83dc74b2020-01-14 18:38:44 +0900284func IsLibDepTag(depTag blueprint.DependencyTag) bool {
285 return depTag == libTag
286}
287
288func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
289 return depTag == staticLibTag
290}
291
Colin Crossfc3674a2017-09-18 17:41:52 -0700292type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100293 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700294
Colin Cross6cef4812019-10-17 14:23:50 -0700295 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
296 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100297
298 // The default system modules to use. Will be an empty string if no system
299 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700300 systemModules string
301
Pete Gilline3d44b22020-06-29 11:28:51 +0100302 // The modules that will be added to the classpath regardless of the Java language level targeted
303 classpath []string
304
Colin Cross6cef4812019-10-17 14:23:50 -0700305 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100306 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700307 java9Classpath []string
308
Colin Crossa97c5d32018-03-28 14:58:31 -0700309 frameworkResModule string
310
Colin Cross86a60ae2018-05-29 14:44:55 -0700311 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700312 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100313
314 noStandardLibs, noFrameworksLibs bool
315}
316
317func (s sdkDep) hasStandardLibs() bool {
318 return !s.noStandardLibs
319}
320
321func (s sdkDep) hasFrameworkLibs() bool {
322 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700323}
324
Colin Crossa4f08812018-10-02 22:03:40 -0700325type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700326 name string
327 path android.Path
328 target android.Target
329 coverageFile android.OptionalPath
330 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700331}
332
Jiyong Parkf1691d22021-03-29 20:11:58 +0900333func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700334 sdkDep := decodeSdkDep(ctx, sdkContext)
335 if sdkDep.useModule {
336 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
337 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
338 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
339 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
340 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
341 }
342 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
343 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
344 }
345 }
346 if sdkDep.systemModules != "" {
347 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
348 }
349}
350
Colin Cross32f676a2017-09-06 13:41:06 -0700351type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800352 classpath classpath
353 java9Classpath classpath
354 bootClasspath classpath
355 processorPath classpath
356 errorProneProcessorPath classpath
357 processorClasses []string
358 staticJars android.Paths
359 staticHeaderJars android.Paths
360 staticResourceJars android.Paths
361 aidlIncludeDirs android.Paths
362 srcs android.Paths
363 srcJars android.Paths
364 systemModules *systemModules
365 aidlPreprocess android.OptionalPath
366 kotlinStdlib android.Paths
367 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800368
369 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700370}
Colin Cross2fe66872015-03-30 17:20:39 -0700371
Colin Cross54250902017-12-05 09:28:08 -0800372func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
373 for _, f := range dep.Srcs() {
374 if f.Ext() != ".jar" {
375 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
376 ctx.OtherModuleName(dep.(blueprint.Module)))
377 }
378 }
379}
380
Jiyong Parkf1691d22021-03-29 20:11:58 +0900381func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700382 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700383 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700384 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900385 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700386 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700387 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700388 }
Nan Zhang357466b2018-04-17 17:38:36 -0700389}
390
Colin Cross1e743852019-10-28 11:37:20 -0700391type javaVersion int
392
393const (
394 JAVA_VERSION_UNSUPPORTED = 0
395 JAVA_VERSION_6 = 6
396 JAVA_VERSION_7 = 7
397 JAVA_VERSION_8 = 8
398 JAVA_VERSION_9 = 9
399)
400
401func (v javaVersion) String() string {
402 switch v {
403 case JAVA_VERSION_6:
404 return "1.6"
405 case JAVA_VERSION_7:
406 return "1.7"
407 case JAVA_VERSION_8:
408 return "1.8"
409 case JAVA_VERSION_9:
410 return "1.9"
411 default:
412 return "unsupported"
413 }
414}
415
416// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
417func (v javaVersion) usesJavaModules() bool {
418 return v >= 9
419}
420
421func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100422 switch javaVersion {
423 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700424 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100425 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700426 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100427 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700428 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100429 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700430 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100431 case "10", "11":
432 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700433 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100434 default:
435 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700436 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100437 }
438}
439
Colin Cross2fe66872015-03-30 17:20:39 -0700440//
441// Java libraries (.jar file)
442//
443
Colin Crossf506d872017-07-19 15:53:04 -0700444type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700445 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700446
447 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700448}
449
Jiyong Park45bf82e2020-12-15 22:29:02 +0900450var _ android.ApexModule = (*Library)(nil)
451
Paul Duffine739f1e2020-05-29 11:24:51 +0100452// Provides access to the list of permitted packages from updatable boot jars.
453type PermittedPackagesForUpdatableBootJars interface {
454 PermittedPackagesForUpdatableBootJars() []string
455}
456
457var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
458
459func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
460 return j.properties.Permitted_packages
461}
462
Colin Cross42be7612019-02-21 18:12:14 -0800463func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000464 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700465 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000466 return true
467 }
468
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000469 // Store uncompressed (and do not strip) dex files from boot class path jars.
470 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
471 return true
472 }
473
474 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -0800475 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000476 return true
477 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800478 if ctx.Config().UncompressPrivAppDex() &&
479 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
480 return true
481 }
482
Colin Cross2fc72f62018-12-21 12:59:54 -0800483 return false
484}
485
Colin Crossf506d872017-07-19 15:53:04 -0700486func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900487 j.sdkVersion = j.SdkVersion(ctx)
488 j.minSdkVersion = j.MinSdkVersion(ctx)
489
Colin Cross56a83212020-09-15 18:30:11 -0700490 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
491 if !apexInfo.IsForPlatform() {
492 j.hideApexVariantFromMake = true
493 }
494
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100495 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900496 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800497 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700498 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100499 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700500 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100501 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700502 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100503 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700504 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700505
bralee1fbf4402020-05-21 10:11:59 +0800506 // Collect the module directory for IDE info in java/jdeps.go.
507 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
508
Colin Cross56a83212020-09-15 18:30:11 -0700509 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700511 var extraInstallDeps android.Paths
512 if j.InstallMixin != nil {
513 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
514 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700515 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900516 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700517 }
Colin Crossb7a63242015-04-16 14:09:14 -0700518}
519
Colin Crossf506d872017-07-19 15:53:04 -0700520func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700521 j.deps(ctx)
522}
523
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000524const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000525 aidlIncludeDir = "aidl"
526 javaDir = "java"
527 jarFileSuffix = ".jar"
528 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000529)
530
Paul Duffina0dbf432019-12-05 11:25:53 +0000531// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000532func sdkSnapshotFilePathForJar(osPrefix, name string) string {
533 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000534}
535
Paul Duffina04c1072020-03-02 10:16:35 +0000536func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
537 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000538}
539
Paul Duffin13879572019-11-28 14:31:38 +0000540type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000541 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000542
543 // Function to retrieve the appropriate output jar (implementation or header) from
544 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000545 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
546
547 // Function to compute the snapshot relative path to which the named library's
548 // jar should be copied.
549 snapshotPathGetter func(osPrefix, name string) string
550
551 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
552 // files like aidl files should also be copied.
553 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000554}
555
Paul Duffindb170e42020-12-08 17:48:25 +0000556const (
557 onlyCopyJarToSnapshot = true
558 copyEverythingToSnapshot = false
559)
560
Paul Duffin13879572019-11-28 14:31:38 +0000561func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
562 mctx.AddVariationDependencies(nil, dependencyTag, names...)
563}
564
565func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
566 _, ok := module.(*Library)
567 return ok
568}
569
Paul Duffin3a4eb502020-03-19 16:11:18 +0000570func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
571 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000572}
Paul Duffina0dbf432019-12-05 11:25:53 +0000573
Paul Duffin14eb4672020-03-02 11:33:02 +0000574func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000575 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000576}
577
578type librarySdkMemberProperties struct {
579 android.SdkMemberPropertiesBase
580
Paul Duffin864e1b42020-05-06 10:23:19 +0100581 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000582 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +0000583}
584
Paul Duffin3a4eb502020-03-19 16:11:18 +0000585func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000586 j := variant.(*Library)
587
Paul Duffindb170e42020-12-08 17:48:25 +0000588 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
589
Paul Duffina551a1c2020-03-17 21:04:24 +0000590 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +0000591}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000592
Paul Duffin3a4eb502020-03-19 16:11:18 +0000593func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000594 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000595
Paul Duffindb170e42020-12-08 17:48:25 +0000596 memberType := ctx.MemberType().(*librarySdkMemberType)
597
Paul Duffina551a1c2020-03-17 21:04:24 +0000598 exportedJar := p.JarToExport
599 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000600 // Delegate the creation of the snapshot relative path to the member type.
601 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
602
603 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000604 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
605
Paul Duffina551a1c2020-03-17 21:04:24 +0000606 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
607 }
608
Paul Duffindb170e42020-12-08 17:48:25 +0000609 // Do not copy anything else to the snapshot.
610 if memberType.onlyCopyJarToSnapshot {
611 return
612 }
613
Paul Duffina551a1c2020-03-17 21:04:24 +0000614 aidlIncludeDirs := p.AidlIncludeDirs
615 if len(aidlIncludeDirs) != 0 {
616 sdkModuleContext := ctx.SdkModuleContext()
617 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000618 // TODO(jiyong): copy parcelable declarations only
619 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
620 for _, file := range aidlFiles {
621 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
622 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000623 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000624
Paul Duffina551a1c2020-03-17 21:04:24 +0000625 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000626 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000627}
628
Colin Cross1b16b0e2019-02-12 14:41:32 -0800629// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
630//
631// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
632// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
633// as a `static_libs` dependency of another module.
634//
635// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
636// a device.
637//
638// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
639// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700640func LibraryFactory() android.Module {
641 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700642
Colin Crossce6734e2020-06-15 16:09:53 -0700643 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700644
Paul Duffin71b33cc2021-06-23 11:39:47 +0100645 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100646
Jiyong Park7f7766d2019-07-25 22:02:35 +0900647 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900648 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900649 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700650 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700651}
652
Colin Cross1b16b0e2019-02-12 14:41:32 -0800653// java_library_static is an obsolete alias for java_library.
654func LibraryStaticFactory() android.Module {
655 return LibraryFactory()
656}
657
658// java_library_host builds and links sources into a `.jar` file for the host.
659//
660// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
661// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700662func LibraryHostFactory() android.Module {
663 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700664
Colin Crossce6734e2020-06-15 16:09:53 -0700665 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700666
Colin Cross9ae1b922018-06-26 17:59:05 -0700667 module.Module.properties.Installable = proptools.BoolPtr(true)
668
Jiyong Park7f7766d2019-07-25 22:02:35 +0900669 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100670 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900671 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700672 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700673}
674
675//
Colin Crossb628ea52018-08-14 16:42:33 -0700676// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700677//
678
Dan Shi95d19422020-08-15 12:24:26 -0700679// Test option struct.
680type TestOptions struct {
681 // a list of extra test configuration files that should be installed with the module.
682 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800683
684 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
685 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700686}
687
Colin Cross05638fc2018-04-09 18:40:24 -0700688type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700689 // list of compatibility suites (for example "cts", "vts") that the module should be
690 // installed into.
691 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700692
693 // the name of the test configuration (for example "AndroidTest.xml") that should be
694 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800695 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700696
Jack He33338892018-09-19 02:21:28 -0700697 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
698 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800699 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700700
Colin Crossd96ca352018-08-10 16:06:24 -0700701 // list of files or filegroup modules that provide data that should be installed alongside
702 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900703 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700704
705 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
706 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
707 // explicitly.
708 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800709
710 // Add parameterized mainline modules to auto generated test config. The options will be
711 // handled by TradeFed to do downloading and installing the specified modules on the device.
712 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700713
714 // Test options.
715 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800716
717 // Names of modules containing JNI libraries that should be installed alongside the test.
718 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700719}
720
Liz Kammerdd849a82020-06-12 16:38:45 -0700721type hostTestProperties struct {
722 // list of native binary modules that should be installed alongside the test
723 Data_native_bins []string `android:"arch_variant"`
724}
725
Paul Duffin42df1442019-03-20 12:45:53 +0000726type testHelperLibraryProperties struct {
727 // list of compatibility suites (for example "cts", "vts") that the module should be
728 // installed into.
729 Test_suites []string `android:"arch_variant"`
730}
731
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000732type prebuiltTestProperties struct {
733 // list of compatibility suites (for example "cts", "vts") that the module should be
734 // installed into.
735 Test_suites []string `android:"arch_variant"`
736
737 // the name of the test configuration (for example "AndroidTest.xml") that should be
738 // installed with the module.
739 Test_config *string `android:"path,arch_variant"`
740}
741
Colin Cross05638fc2018-04-09 18:40:24 -0700742type Test struct {
743 Library
744
745 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700746
Dan Shi95d19422020-08-15 12:24:26 -0700747 testConfig android.Path
748 extraTestConfigs android.Paths
749 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700750}
751
Liz Kammerdd849a82020-06-12 16:38:45 -0700752type TestHost struct {
753 Test
754
755 testHostProperties hostTestProperties
756}
757
Paul Duffin42df1442019-03-20 12:45:53 +0000758type TestHelperLibrary struct {
759 Library
760
761 testHelperLibraryProperties testHelperLibraryProperties
762}
763
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000764type JavaTestImport struct {
765 Import
766
767 prebuiltTestProperties prebuiltTestProperties
768
769 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700770 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000771}
772
Liz Kammerdd849a82020-06-12 16:38:45 -0700773func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
774 if len(j.testHostProperties.Data_native_bins) > 0 {
775 for _, target := range ctx.MultiTargets() {
776 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
777 }
778 }
779
Colin Crossf8d9c492021-01-26 11:01:43 -0800780 if len(j.testProperties.Jni_libs) > 0 {
781 for _, target := range ctx.MultiTargets() {
782 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
783 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
784 }
785 }
786
Liz Kammerdd849a82020-06-12 16:38:45 -0700787 j.deps(ctx)
788}
789
Yuexi Ma627263f2021-03-04 13:47:56 -0800790func (j *TestHost) AddExtraResource(p android.Path) {
791 j.extraResources = append(j.extraResources, p)
792}
793
Colin Cross303e21f2018-08-07 16:49:25 -0700794func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000795 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
796 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700797 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000798 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
799 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700800 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800801 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700802
Colin Cross8a497952019-03-05 22:25:09 -0800803 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700804
Dan Shi95d19422020-08-15 12:24:26 -0700805 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
806
Liz Kammerdd849a82020-06-12 16:38:45 -0700807 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
808 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
809 })
810
Colin Crossf8d9c492021-01-26 11:01:43 -0800811 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
812 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
813 if sharedLibInfo.SharedLibrary != nil {
814 // Copy to an intermediate output directory to append "lib[64]" to the path,
815 // so that it's compatible with the default rpath values.
816 var relPath string
817 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
818 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
819 } else {
820 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
821 }
822 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
823 ctx.Build(pctx, android.BuildParams{
824 Rule: android.Cp,
825 Input: sharedLibInfo.SharedLibrary,
826 Output: relocatedLib,
827 })
828 j.data = append(j.data, relocatedLib)
829 } else {
830 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
831 }
832 })
833
Colin Cross303e21f2018-08-07 16:49:25 -0700834 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700835}
836
Paul Duffin42df1442019-03-20 12:45:53 +0000837func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
838 j.Library.GenerateAndroidBuildActions(ctx)
839}
840
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000841func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
842 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800843 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000844
845 j.Import.GenerateAndroidBuildActions(ctx)
846}
847
848type testSdkMemberType struct {
849 android.SdkMemberTypeBase
850}
851
852func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
853 mctx.AddVariationDependencies(nil, dependencyTag, names...)
854}
855
856func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
857 _, ok := module.(*Test)
858 return ok
859}
860
Paul Duffin3a4eb502020-03-19 16:11:18 +0000861func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
862 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000863}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000864
Paul Duffin14eb4672020-03-02 11:33:02 +0000865func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
866 return &testSdkMemberProperties{}
867}
868
869type testSdkMemberProperties struct {
870 android.SdkMemberPropertiesBase
871
Paul Duffina551a1c2020-03-17 21:04:24 +0000872 JarToExport android.Path
873 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000874}
875
Paul Duffin3a4eb502020-03-19 16:11:18 +0000876func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000877 test := variant.(*Test)
878
879 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000880 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000881 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000882 }
883
Paul Duffina551a1c2020-03-17 21:04:24 +0000884 p.JarToExport = implementationJars[0]
885 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000886}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000887
Paul Duffin3a4eb502020-03-19 16:11:18 +0000888func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000889 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000890
Paul Duffina551a1c2020-03-17 21:04:24 +0000891 exportedJar := p.JarToExport
892 if exportedJar != nil {
893 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
894 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000895
896 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000897 }
898
899 testConfig := p.TestConfig
900 if testConfig != nil {
901 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
902 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000903 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
904 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000905}
906
Colin Cross1b16b0e2019-02-12 14:41:32 -0800907// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
908// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
909//
910// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
911// compiled against the device bootclasspath.
912//
913// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
914// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700915func TestFactory() android.Module {
916 module := &Test{}
917
Colin Crossce6734e2020-06-15 16:09:53 -0700918 module.addHostAndDeviceProperties()
919 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700920
Colin Cross9ae1b922018-06-26 17:59:05 -0700921 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800922 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700923 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700924
Paul Duffinb6b89a42021-05-06 16:33:43 +0100925 android.InitSdkAwareModule(module)
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
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001223func (j *Import) getStrictUpdatabilityLinting() bool {
1224 return false
1225}
1226
1227func (j *Import) setStrictUpdatabilityLinting(bool) {
1228}
1229
Colin Cross74d73e22017-08-02 11:05:49 -07001230func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001231 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001232
1233 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001234 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001235 }
Colin Cross1e676be2016-10-12 14:38:15 -07001236}
1237
Colin Cross74d73e22017-08-02 11:05:49 -07001238func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001239 j.sdkVersion = j.SdkVersion(ctx)
1240 j.minSdkVersion = j.MinSdkVersion(ctx)
1241
Colin Cross56a83212020-09-15 18:30:11 -07001242 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1243 j.hideApexVariantFromMake = true
1244 }
1245
Colin Cross8a497952019-03-05 22:25:09 -08001246 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001247
Jiyong Park0b238752019-10-29 11:23:10 +09001248 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001249 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001250 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1251 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001252 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001253 inputFile := outputFile
1254 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1255 TransformJetifier(ctx, outputFile, inputFile)
1256 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001257 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001258 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001259
Liz Kammerd6c31d22020-08-05 15:40:41 -07001260 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001261 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001262
Jiyong Park1be96912018-05-28 18:02:19 +09001263 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001264 tag := ctx.OtherModuleDependencyTag(module)
1265
Colin Crossdcf71b22021-02-01 13:59:03 -08001266 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1267 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001268 switch tag {
1269 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001270 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001271 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001272 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001273 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001274 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001275 switch tag {
1276 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001277 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001278 }
1279 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001280
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001281 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001282
1283 // Save away the `deapexer` module on which this depends, if any.
1284 if tag == android.DeapexerTag {
1285 deapexerModule = module
1286 }
Jiyong Park1be96912018-05-28 18:02:19 +09001287 })
1288
Nan Zhang4973ecf2018-08-10 13:42:12 -07001289 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001290 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001291 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001292 }
Jiyong Park19604de2020-03-24 16:44:11 +09001293
1294 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001295
Paul Duffin064b70c2020-11-02 17:32:38 +00001296 if ctx.Device() {
1297 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1298 // obtained from the associated deapexer module.
1299 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1300 if ai.ForPrebuiltApex {
1301 if deapexerModule == nil {
1302 // This should never happen as a variant for a prebuilt_apex is only created if the
Paul Duffinb17d0442021-05-05 12:07:00 +01001303 // deapexer module has been configured to export the dex implementation jar for this module.
Paul Duffin064b70c2020-11-02 17:32:38 +00001304 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1305 j.Name(), ai.ApexVariationName)
Paul Duffinb17d0442021-05-05 12:07:00 +01001306 return
Paul Duffin064b70c2020-11-02 17:32:38 +00001307 }
1308
1309 // Get the path of the dex implementation jar from the `deapexer` module.
1310 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001311 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Paul Duffin9d67ca62021-02-03 20:06:33 +00001312 j.dexJarFile = dexOutputPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001313
1314 // Initialize the hiddenapi structure.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001315 j.initHiddenAPI(ctx, dexOutputPath, outputFile, nil)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001316 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001317 // This should never happen as a variant for a prebuilt_apex is only created if the
1318 // prebuilt_apex has been configured to export the java library dex file.
1319 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1320 }
1321 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001322 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001323 if sdkDep.invalidVersion {
1324 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1325 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1326 } else if sdkDep.useFiles {
1327 // sdkDep.jar is actually equivalent to turbine header.jar.
1328 flags.classpath = append(flags.classpath, sdkDep.jars...)
1329 }
1330
1331 // Dex compilation
1332
1333 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1334 if j.dexProperties.Uncompress_dex == nil {
1335 // If the value was not force-set by the user, use reasonable default based on the module.
1336 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1337 }
1338 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1339
Paul Duffin612e6102021-02-02 13:38:13 +00001340 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001341 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001342 if ctx.Failed() {
1343 return
1344 }
1345
Paul Duffin74d18d12021-05-14 14:18:47 +01001346 // Initialize the hiddenapi structure.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001347 j.initHiddenAPI(ctx, dexOutputFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001348
1349 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001350 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001351
1352 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001353 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001354 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001355
1356 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1357 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1358 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1359 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1360 AidlIncludeDirs: j.exportAidlIncludeDirs,
1361 })
Colin Cross2fe66872015-03-30 17:20:39 -07001362}
1363
Paul Duffinaa55f742020-10-06 17:20:13 +01001364func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1365 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001366 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001367 return android.Paths{j.combinedClasspathFile}, nil
1368 default:
1369 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1370 }
1371}
1372
1373var _ android.OutputFileProducer = (*Import)(nil)
1374
Nan Zhanged19fc32017-10-19 13:06:22 -07001375func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001376 if j.combinedClasspathFile == nil {
1377 return nil
1378 }
Colin Cross37f6d792018-07-12 12:28:41 -07001379 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001380}
1381
Colin Cross331a1212018-08-15 20:40:52 -07001382func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001383 if j.combinedClasspathFile == nil {
1384 return nil
1385 }
Colin Cross331a1212018-08-15 20:40:52 -07001386 return android.Paths{j.combinedClasspathFile}
1387}
1388
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001389func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001390 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001391}
1392
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001393func (j *Import) DexJarInstallPath() android.Path {
1394 return nil
1395}
1396
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001397func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1398 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001399}
1400
Jiyong Park45bf82e2020-12-15 22:29:02 +09001401var _ android.ApexModule = (*Import)(nil)
1402
1403// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001404func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001405 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001406}
1407
Jiyong Park45bf82e2020-12-15 22:29:02 +09001408// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001409func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1410 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001411 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001412 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001413 return fmt.Errorf("min_sdk_version is not specified")
1414 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001415 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001416 return nil
1417 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001418 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1419 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001420 }
Jooyung Han749dc692020-04-15 11:03:39 +09001421 return nil
1422}
1423
Paul Duffinfef55002021-06-17 14:56:05 +01001424// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1425// java_sdk_library_import with the specified base module name requires to be exported from a
1426// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001427func requiredFilesFromPrebuiltApexForImport(name string) []string {
1428 // Add the dex implementation jar to the set of exported files.
1429 return []string{
1430 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001431 }
1432}
1433
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001434// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1435// the java library with the specified name.
1436func apexRootRelativePathToJavaLib(name string) string {
1437 return filepath.Join("javalib", name+".jar")
1438}
1439
Paul Duffinfef55002021-06-17 14:56:05 +01001440var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1441
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001442func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001443 name := j.BaseModuleName()
1444 return requiredFilesFromPrebuiltApexForImport(name)
1445}
1446
albaltai36ff7dc2018-12-25 14:35:23 +08001447// Add compile time check for interface implementation
1448var _ android.IDEInfo = (*Import)(nil)
1449var _ android.IDECustomizedModuleName = (*Import)(nil)
1450
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001451// Collect information for opening IDE project files in java/jdeps.go.
1452const (
1453 removedPrefix = "prebuilt_"
1454)
1455
1456func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1457 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1458}
1459
1460func (j *Import) IDECustomizedModuleName() string {
1461 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1462 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1463 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001464 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001465}
1466
Colin Cross74d73e22017-08-02 11:05:49 -07001467var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001468
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001469func (j *Import) IsInstallable() bool {
1470 return Bool(j.properties.Installable)
1471}
1472
1473var _ dexpreopterInterface = (*Import)(nil)
1474
Colin Cross1b16b0e2019-02-12 14:41:32 -08001475// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1476//
1477// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1478// compiled against an Android classpath.
1479//
1480// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1481// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001482func ImportFactory() android.Module {
1483 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001484
Liz Kammerd6c31d22020-08-05 15:40:41 -07001485 module.AddProperties(
1486 &module.properties,
1487 &module.dexer.dexProperties,
1488 )
Colin Cross74d73e22017-08-02 11:05:49 -07001489
Paul Duffin71b33cc2021-06-23 11:39:47 +01001490 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001491
Liz Kammerd6c31d22020-08-05 15:40:41 -07001492 module.dexProperties.Optimize.EnabledByDefault = false
1493
Colin Cross74d73e22017-08-02 11:05:49 -07001494 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001495 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001496 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001497 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001498 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001499}
1500
Colin Cross1b16b0e2019-02-12 14:41:32 -08001501// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1502// module.
1503//
1504// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1505// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001506func ImportFactoryHost() android.Module {
1507 module := &Import{}
1508
1509 module.AddProperties(&module.properties)
1510
1511 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001512 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001513 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001514 return module
1515}
1516
Colin Cross42be7612019-02-21 18:12:14 -08001517// dex_import module
1518
1519type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001520 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001521
1522 // set the name of the output
1523 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001524}
1525
1526type DexImport struct {
1527 android.ModuleBase
1528 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001529 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001530 prebuilt android.Prebuilt
1531
1532 properties DexImportProperties
1533
Colin Crossb014f072021-02-26 14:54:36 -08001534 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001535
1536 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001537
1538 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001539}
1540
1541func (j *DexImport) Prebuilt() *android.Prebuilt {
1542 return &j.prebuilt
1543}
1544
1545func (j *DexImport) PrebuiltSrcs() []string {
1546 return j.properties.Jars
1547}
1548
1549func (j *DexImport) Name() string {
1550 return j.prebuilt.Name(j.ModuleBase.Name())
1551}
1552
Jiyong Park0b238752019-10-29 11:23:10 +09001553func (j *DexImport) Stem() string {
1554 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1555}
1556
Jiyong Park77acec62020-06-01 21:39:15 +09001557func (a *DexImport) JacocoReportClassesFile() android.Path {
1558 return nil
1559}
1560
Colin Cross08dca382020-07-21 20:31:17 -07001561func (a *DexImport) LintDepSets() LintDepSets {
1562 return LintDepSets{}
1563}
1564
Martin Stjernholm6d415272020-01-31 17:10:36 +00001565func (j *DexImport) IsInstallable() bool {
1566 return true
1567}
1568
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001569func (j *DexImport) getStrictUpdatabilityLinting() bool {
1570 return false
1571}
1572
1573func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1574}
1575
Colin Cross42be7612019-02-21 18:12:14 -08001576func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1577 if len(j.properties.Jars) != 1 {
1578 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1579 }
1580
Colin Cross56a83212020-09-15 18:30:11 -07001581 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1582 if !apexInfo.IsForPlatform() {
1583 j.hideApexVariantFromMake = true
1584 }
1585
Jiyong Park0b238752019-10-29 11:23:10 +09001586 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001587 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1588
1589 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1590 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1591
1592 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001593 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001594
1595 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1596 rule.Temporary(temporary)
1597
1598 // use zip2zip to uncompress classes*.dex files
1599 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001600 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001601 FlagWithInput("-i ", inputJar).
1602 FlagWithOutput("-o ", temporary).
1603 FlagWithArg("-0 ", "'classes*.dex'")
1604
1605 // use zipalign to align uncompressed classes*.dex files
1606 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001607 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001608 Flag("-f").
1609 Text("4").
1610 Input(temporary).
1611 Output(dexOutputFile)
1612
1613 rule.DeleteTemporaryFiles()
1614
Colin Crossf1a035e2020-11-16 17:32:30 -08001615 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001616 } else {
1617 ctx.Build(pctx, android.BuildParams{
1618 Rule: android.Cp,
1619 Input: inputJar,
1620 Output: dexOutputFile,
1621 })
1622 }
1623
1624 j.dexJarFile = dexOutputFile
1625
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001626 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001627
Colin Cross56a83212020-09-15 18:30:11 -07001628 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001629 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1630 j.Stem()+".jar", dexOutputFile)
1631 }
Colin Cross42be7612019-02-21 18:12:14 -08001632}
1633
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001634func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001635 return j.dexJarFile
1636}
1637
Jiyong Park45bf82e2020-12-15 22:29:02 +09001638var _ android.ApexModule = (*DexImport)(nil)
1639
1640// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001641func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1642 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001643 // we don't check prebuilt modules for sdk_version
1644 return nil
1645}
1646
Colin Cross42be7612019-02-21 18:12:14 -08001647// dex_import imports a `.jar` file containing classes.dex files.
1648//
1649// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1650// to the device.
1651func DexImportFactory() android.Module {
1652 module := &DexImport{}
1653
1654 module.AddProperties(&module.properties)
1655
1656 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001657 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001658 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001659 return module
1660}
1661
Colin Cross89536d42017-07-07 14:35:50 -07001662//
1663// Defaults
1664//
1665type Defaults struct {
1666 android.ModuleBase
1667 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001668 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001669}
1670
Colin Cross1b16b0e2019-02-12 14:41:32 -08001671// java_defaults provides a set of properties that can be inherited by other java or android modules.
1672//
1673// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1674// property in the defaults module that exists in the depending module will be prepended to the depending module's
1675// value for that property.
1676//
1677// Example:
1678//
1679// java_defaults {
1680// name: "example_defaults",
1681// srcs: ["common/**/*.java"],
1682// javacflags: ["-Xlint:all"],
1683// aaptflags: ["--auto-add-overlay"],
1684// }
1685//
1686// java_library {
1687// name: "example",
1688// defaults: ["example_defaults"],
1689// srcs: ["example/**/*.java"],
1690// }
1691//
1692// is functionally identical to:
1693//
1694// java_library {
1695// name: "example",
1696// srcs: [
1697// "common/**/*.java",
1698// "example/**/*.java",
1699// ],
1700// javacflags: ["-Xlint:all"],
1701// }
Paul Duffin47357662019-12-05 14:07:14 +00001702func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001703 module := &Defaults{}
1704
Colin Cross89536d42017-07-07 14:35:50 -07001705 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001706 &CommonProperties{},
1707 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001708 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001709 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001710 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001711 &aaptProperties{},
1712 &androidLibraryProperties{},
1713 &appProperties{},
1714 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001715 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001716 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001717 &ImportProperties{},
1718 &AARImportProperties{},
1719 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001720 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001721 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001722 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001723 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001724 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001725 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001726 )
1727
1728 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001729 return module
1730}
Nan Zhangea568a42017-11-08 21:20:04 -08001731
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001732func kytheExtractJavaFactory() android.Singleton {
1733 return &kytheExtractJavaSingleton{}
1734}
1735
1736type kytheExtractJavaSingleton struct {
1737}
1738
1739func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1740 var xrefTargets android.Paths
1741 ctx.VisitAllModules(func(module android.Module) {
1742 if javaModule, ok := module.(xref); ok {
1743 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1744 }
1745 })
1746 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1747 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001748 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001749 }
1750}
1751
Nan Zhangea568a42017-11-08 21:20:04 -08001752var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001753var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001754var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001755var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001756
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001757// Add class loader context (CLC) of a given dependency to the current CLC.
1758func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1759 clcMap dexpreopt.ClassLoaderContextMap) {
1760
1761 dep, ok := depModule.(UsesLibraryDependency)
1762 if !ok {
1763 return
1764 }
1765
1766 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1767 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1768 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1769 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1770 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1771 // the current CLC.
1772 var implicitSdkLib *string
1773 comp, isComp := depModule.(SdkLibraryComponentDependency)
1774 if isComp {
1775 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1776 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1777 }
1778 if implicitSdkLib == nil {
1779 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1780 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001781 }
1782 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001783
1784 depTag := ctx.OtherModuleDependencyTag(depModule)
1785 if depTag == libTag || depTag == usesLibTag {
1786 // Ok, propagate <uses-library> through non-static library dependencies.
1787 } else if depTag == staticLibTag {
1788 // Propagate <uses-library> through static library dependencies, unless it is a component
1789 // library (such as stubs). Component libraries have a dependency on their SDK library,
1790 // which should not be pulled just because of a static component library.
1791 if implicitSdkLib != nil {
1792 return
1793 }
1794 } else {
1795 // Don't propagate <uses-library> for other dependency tags.
1796 return
1797 }
1798
1799 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001800 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001801 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1802 } else {
1803 depName := ctx.OtherModuleName(depModule)
1804 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1805 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001806}