blob: bbed42def4894e2c4af1726e375b6993a90edf7f [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080030 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010031 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010037 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080039 RegisterJavaSdkMemberTypes()
40}
41
Paul Duffin535e0a12021-03-30 23:34:32 +010042func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080043 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
44
45 ctx.RegisterModuleType("java_library", LibraryFactory)
46 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
47 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
48 ctx.RegisterModuleType("java_binary", BinaryFactory)
49 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
50 ctx.RegisterModuleType("java_test", TestFactory)
51 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
52 ctx.RegisterModuleType("java_test_host", TestHostFactory)
53 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
54 ctx.RegisterModuleType("java_import", ImportFactory)
55 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
56 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
57 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
58 ctx.RegisterModuleType("dex_import", DexImportFactory)
59
Martin Stjernholmcae43e12021-05-13 02:38:35 +010060 // This mutator registers dependencies on dex2oat for modules that should be
61 // dexpreopted. This is done late when the final variants have been
62 // established, to not get the dependencies split into the wrong variants and
63 // to support the checks in dexpreoptDisabled().
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080064 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
65 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
66 })
67
68 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
69 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
70}
71
72func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000073 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000074 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010075 android.RegisterSdkMemberType(javaLibsSdkMemberType)
76 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
77 android.RegisterSdkMemberType(javaTestSdkMemberType)
78}
79
80var (
81 // Supports adding java header libraries to module_exports and sdk.
82 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
83 android.SdkMemberTypeBase{
84 PropertyName: "java_header_libs",
85 SupportsSdk: true,
86 },
87 func(_ android.SdkMemberContext, j *Library) android.Path {
88 headerJars := j.HeaderJars()
89 if len(headerJars) != 1 {
90 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
91 }
92
93 return headerJars[0]
94 },
95 sdkSnapshotFilePathForJar,
96 copyEverythingToSnapshot,
97 }
Paul Duffin255f18e2019-12-13 11:22:16 +000098
Paul Duffin22ff0aa2021-02-04 11:15:34 +000099 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +0100100 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000101 implementationJars := j.ImplementationAndResourcesJars()
102 if len(implementationJars) != 1 {
103 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
104 }
105 return implementationJars[0]
106 }
107
Paul Duffin2da04242021-04-23 19:43:28 +0100108 // Supports adding java implementation libraries to module_exports but not sdk.
109 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000110 android.SdkMemberTypeBase{
111 PropertyName: "java_libs",
112 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000113 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000114 sdkSnapshotFilePathForJar,
115 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100116 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000117
Paul Duffin2da04242021-04-23 19:43:28 +0100118 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000119 //
120 // The build has some implicit dependencies (via the boot jars configuration) on a number of
121 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
122 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
123 // used outside those mainline modules.
124 //
125 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
126 // either java_libs, or java_header_libs would end up exporting more information than was strictly
127 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
128 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100129 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000130 android.SdkMemberTypeBase{
131 PropertyName: "java_boot_libs",
132 SupportsSdk: true,
133 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000134 // Temporarily export implementation classes jar for java_boot_libs as it is required for the
135 // hiddenapi processing.
136 // TODO(b/179354495): Revert once hiddenapi processing has been modularized.
137 exportImplementationClassesJar,
138 sdkSnapshotFilePathForJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000139 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100140 }
Paul Duffindb170e42020-12-08 17:48:25 +0000141
Paul Duffin2da04242021-04-23 19:43:28 +0100142 // Supports adding java test libraries to module_exports but not sdk.
143 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000144 SdkMemberTypeBase: android.SdkMemberTypeBase{
145 PropertyName: "java_tests",
146 },
Paul Duffin2da04242021-04-23 19:43:28 +0100147 }
148)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900149
Colin Crossdcf71b22021-02-01 13:59:03 -0800150// JavaInfo contains information about a java module for use by modules that depend on it.
151type JavaInfo struct {
152 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
153 // against this module. If empty, ImplementationJars should be used instead.
154 HeaderJars android.Paths
155
156 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
157 // in the module as well as any resources included in the module.
158 ImplementationAndResourcesJars android.Paths
159
160 // ImplementationJars is a list of jars that contain the implementations of classes in the
161 //module.
162 ImplementationJars android.Paths
163
164 // ResourceJars is a list of jars that contain the resources included in the module.
165 ResourceJars android.Paths
166
167 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
168 // depending on this module.
169 AidlIncludeDirs android.Paths
170
171 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
172 // module.
173 SrcJarArgs []string
174
175 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
176 SrcJarDeps android.Paths
177
178 // ExportedPlugins is a list of paths that should be used as annotation processors for any
179 // module that depends on this module.
180 ExportedPlugins android.Paths
181
182 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
183 // any module that depends on this module.
184 ExportedPluginClasses []string
185
186 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
187 // requiring disbling turbine for any modules that depend on it.
188 ExportedPluginDisableTurbine bool
189
190 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
191 // instrumented by jacoco.
192 JacocoReportClassesFile android.Path
193}
194
195var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
196
Colin Cross75ce9ec2021-02-26 16:20:32 -0800197// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
198// the sysprop implementation library.
199type SyspropPublicStubInfo struct {
200 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
201 // the sysprop implementation library.
202 JavaInfo JavaInfo
203}
204
205var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
206
Paul Duffin44b481b2020-06-17 16:59:43 +0100207// Methods that need to be implemented for a module that is added to apex java_libs property.
208type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700209 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100210 ImplementationAndResourcesJars() android.Paths
211}
212
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100213// Provides build path and install path to DEX jars.
214type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000215 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100216 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000217 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100218}
219
Jaewoong Jung26342642021-03-17 15:56:23 -0700220// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800221type xref interface {
222 XrefJavaFiles() android.Paths
223}
224
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800225func (j *Module) XrefJavaFiles() android.Paths {
226 return j.kytheFiles
227}
228
Colin Crossbe1da472017-07-07 15:59:46 -0700229type dependencyTag struct {
230 blueprint.BaseDependencyTag
231 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700232}
233
Colin Crosse9fe2942020-11-10 18:12:15 -0800234// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
235// dependency to be installed when the parent module is installed.
236type installDependencyTag struct {
237 blueprint.BaseDependencyTag
238 android.InstallAlwaysNeededDependencyTag
239 name string
240}
241
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100242type usesLibraryDependencyTag struct {
243 dependencyTag
244 sdkVersion int // SDK version in which the library appared as a standalone library.
245}
246
247func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
248 return usesLibraryDependencyTag{
249 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
250 sdkVersion: sdkVersion,
251 }
252}
253
Jiyong Park8be103b2019-11-08 15:53:48 +0900254func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700255 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900256}
257
Colin Crossbe1da472017-07-07 15:59:46 -0700258var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800259 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
260 staticLibTag = dependencyTag{name: "staticlib"}
261 libTag = dependencyTag{name: "javalib"}
262 java9LibTag = dependencyTag{name: "java9lib"}
263 pluginTag = dependencyTag{name: "plugin"}
264 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
265 exportedPluginTag = dependencyTag{name: "exported-plugin"}
266 bootClasspathTag = dependencyTag{name: "bootclasspath"}
267 systemModulesTag = dependencyTag{name: "system modules"}
268 frameworkResTag = dependencyTag{name: "framework-res"}
269 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
270 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
271 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
272 certificateTag = dependencyTag{name: "certificate"}
273 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
274 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
275 jniLibTag = dependencyTag{name: "jnilib"}
276 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
277 jniInstallTag = installDependencyTag{name: "jni install"}
278 binaryInstallTag = installDependencyTag{name: "binary install"}
279 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
280 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
281 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
282 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700283)
Colin Cross2fe66872015-03-30 17:20:39 -0700284
Jiyong Park83dc74b2020-01-14 18:38:44 +0900285func IsLibDepTag(depTag blueprint.DependencyTag) bool {
286 return depTag == libTag
287}
288
289func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
290 return depTag == staticLibTag
291}
292
Colin Crossfc3674a2017-09-18 17:41:52 -0700293type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100294 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700295
Colin Cross6cef4812019-10-17 14:23:50 -0700296 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
297 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100298
299 // The default system modules to use. Will be an empty string if no system
300 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700301 systemModules string
302
Pete Gilline3d44b22020-06-29 11:28:51 +0100303 // The modules that will be added to the classpath regardless of the Java language level targeted
304 classpath []string
305
Colin Cross6cef4812019-10-17 14:23:50 -0700306 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100307 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700308 java9Classpath []string
309
Colin Crossa97c5d32018-03-28 14:58:31 -0700310 frameworkResModule string
311
Colin Cross86a60ae2018-05-29 14:44:55 -0700312 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700313 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100314
315 noStandardLibs, noFrameworksLibs bool
316}
317
318func (s sdkDep) hasStandardLibs() bool {
319 return !s.noStandardLibs
320}
321
322func (s sdkDep) hasFrameworkLibs() bool {
323 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700324}
325
Colin Crossa4f08812018-10-02 22:03:40 -0700326type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700327 name string
328 path android.Path
329 target android.Target
330 coverageFile android.OptionalPath
331 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700332}
333
Jiyong Parkf1691d22021-03-29 20:11:58 +0900334func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700335 sdkDep := decodeSdkDep(ctx, sdkContext)
336 if sdkDep.useModule {
337 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
338 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
339 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
340 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
341 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
342 }
343 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
344 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
345 }
346 }
347 if sdkDep.systemModules != "" {
348 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
349 }
350}
351
Colin Cross32f676a2017-09-06 13:41:06 -0700352type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800353 classpath classpath
354 java9Classpath classpath
355 bootClasspath classpath
356 processorPath classpath
357 errorProneProcessorPath classpath
358 processorClasses []string
359 staticJars android.Paths
360 staticHeaderJars android.Paths
361 staticResourceJars android.Paths
362 aidlIncludeDirs android.Paths
363 srcs android.Paths
364 srcJars android.Paths
365 systemModules *systemModules
366 aidlPreprocess android.OptionalPath
367 kotlinStdlib android.Paths
368 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800369
370 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700371}
Colin Cross2fe66872015-03-30 17:20:39 -0700372
Colin Cross54250902017-12-05 09:28:08 -0800373func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
374 for _, f := range dep.Srcs() {
375 if f.Ext() != ".jar" {
376 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
377 ctx.OtherModuleName(dep.(blueprint.Module)))
378 }
379 }
380}
381
Jiyong Parkf1691d22021-03-29 20:11:58 +0900382func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700383 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700384 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700385 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900386 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700387 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700388 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700389 }
Nan Zhang357466b2018-04-17 17:38:36 -0700390}
391
Colin Cross1e743852019-10-28 11:37:20 -0700392type javaVersion int
393
394const (
395 JAVA_VERSION_UNSUPPORTED = 0
396 JAVA_VERSION_6 = 6
397 JAVA_VERSION_7 = 7
398 JAVA_VERSION_8 = 8
399 JAVA_VERSION_9 = 9
400)
401
402func (v javaVersion) String() string {
403 switch v {
404 case JAVA_VERSION_6:
405 return "1.6"
406 case JAVA_VERSION_7:
407 return "1.7"
408 case JAVA_VERSION_8:
409 return "1.8"
410 case JAVA_VERSION_9:
411 return "1.9"
412 default:
413 return "unsupported"
414 }
415}
416
417// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
418func (v javaVersion) usesJavaModules() bool {
419 return v >= 9
420}
421
422func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100423 switch javaVersion {
424 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700425 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100426 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700427 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100428 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700429 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100430 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700431 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100432 case "10", "11":
433 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700434 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100435 default:
436 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700437 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100438 }
439}
440
Colin Cross2fe66872015-03-30 17:20:39 -0700441//
442// Java libraries (.jar file)
443//
444
Colin Crossf506d872017-07-19 15:53:04 -0700445type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700446 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700447
448 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700449}
450
Jiyong Park45bf82e2020-12-15 22:29:02 +0900451var _ android.ApexModule = (*Library)(nil)
452
Paul Duffine739f1e2020-05-29 11:24:51 +0100453// Provides access to the list of permitted packages from updatable boot jars.
454type PermittedPackagesForUpdatableBootJars interface {
455 PermittedPackagesForUpdatableBootJars() []string
456}
457
458var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
459
460func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
461 return j.properties.Permitted_packages
462}
463
Colin Cross42be7612019-02-21 18:12:14 -0800464func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000465 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700466 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000467 return true
468 }
469
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000470 // Store uncompressed (and do not strip) dex files from boot class path jars.
471 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
472 return true
473 }
474
475 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -0800476 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000477 return true
478 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800479 if ctx.Config().UncompressPrivAppDex() &&
480 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
481 return true
482 }
483
Colin Cross2fc72f62018-12-21 12:59:54 -0800484 return false
485}
486
Colin Crossf506d872017-07-19 15:53:04 -0700487func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900488 j.sdkVersion = j.SdkVersion(ctx)
489 j.minSdkVersion = j.MinSdkVersion(ctx)
490
Colin Cross56a83212020-09-15 18:30:11 -0700491 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
492 if !apexInfo.IsForPlatform() {
493 j.hideApexVariantFromMake = true
494 }
495
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100496 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900497 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800498 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700499 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100500 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700501 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100502 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700503 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100504 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700505 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700506
bralee1fbf4402020-05-21 10:11:59 +0800507 // Collect the module directory for IDE info in java/jdeps.go.
508 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
509
Colin Cross56a83212020-09-15 18:30:11 -0700510 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900511 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700512 var extraInstallDeps android.Paths
513 if j.InstallMixin != nil {
514 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
515 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700516 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900517 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700518 }
Colin Crossb7a63242015-04-16 14:09:14 -0700519}
520
Colin Crossf506d872017-07-19 15:53:04 -0700521func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700522 j.deps(ctx)
523}
524
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000525const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000526 aidlIncludeDir = "aidl"
527 javaDir = "java"
528 jarFileSuffix = ".jar"
529 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000530)
531
Paul Duffina0dbf432019-12-05 11:25:53 +0000532// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000533func sdkSnapshotFilePathForJar(osPrefix, name string) string {
534 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000535}
536
Paul Duffina04c1072020-03-02 10:16:35 +0000537func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
538 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000539}
540
Paul Duffin13879572019-11-28 14:31:38 +0000541type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000542 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000543
544 // Function to retrieve the appropriate output jar (implementation or header) from
545 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000546 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
547
548 // Function to compute the snapshot relative path to which the named library's
549 // jar should be copied.
550 snapshotPathGetter func(osPrefix, name string) string
551
552 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
553 // files like aidl files should also be copied.
554 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000555}
556
Paul Duffindb170e42020-12-08 17:48:25 +0000557const (
558 onlyCopyJarToSnapshot = true
559 copyEverythingToSnapshot = false
560)
561
Paul Duffin13879572019-11-28 14:31:38 +0000562func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
563 mctx.AddVariationDependencies(nil, dependencyTag, names...)
564}
565
566func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
567 _, ok := module.(*Library)
568 return ok
569}
570
Paul Duffin3a4eb502020-03-19 16:11:18 +0000571func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
572 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000573}
Paul Duffina0dbf432019-12-05 11:25:53 +0000574
Paul Duffin14eb4672020-03-02 11:33:02 +0000575func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000576 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000577}
578
579type librarySdkMemberProperties struct {
580 android.SdkMemberPropertiesBase
581
Paul Duffin864e1b42020-05-06 10:23:19 +0100582 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000583 AidlIncludeDirs android.Paths
Paul Duffin7b3e10a2021-07-15 14:14:41 +0100584
585 // The list of permitted packages that need to be passed to the prebuilts as they are used to
586 // create the updatable-bcp-packages.txt file.
587 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000588}
589
Paul Duffin3a4eb502020-03-19 16:11:18 +0000590func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000591 j := variant.(*Library)
592
Paul Duffindb170e42020-12-08 17:48:25 +0000593 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
594
Paul Duffina551a1c2020-03-17 21:04:24 +0000595 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin7b3e10a2021-07-15 14:14:41 +0100596
597 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000598}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000599
Paul Duffin3a4eb502020-03-19 16:11:18 +0000600func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000601 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000602
Paul Duffindb170e42020-12-08 17:48:25 +0000603 memberType := ctx.MemberType().(*librarySdkMemberType)
604
Paul Duffina551a1c2020-03-17 21:04:24 +0000605 exportedJar := p.JarToExport
606 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000607 // Delegate the creation of the snapshot relative path to the member type.
608 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
609
610 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000611 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
612
Paul Duffina551a1c2020-03-17 21:04:24 +0000613 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
614 }
615
Paul Duffin7b3e10a2021-07-15 14:14:41 +0100616 if len(p.PermittedPackages) > 0 {
617 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
618 }
619
Paul Duffindb170e42020-12-08 17:48:25 +0000620 // Do not copy anything else to the snapshot.
621 if memberType.onlyCopyJarToSnapshot {
622 return
623 }
624
Paul Duffina551a1c2020-03-17 21:04:24 +0000625 aidlIncludeDirs := p.AidlIncludeDirs
626 if len(aidlIncludeDirs) != 0 {
627 sdkModuleContext := ctx.SdkModuleContext()
628 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000629 // TODO(jiyong): copy parcelable declarations only
630 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
631 for _, file := range aidlFiles {
632 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
633 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000634 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000635
Paul Duffina551a1c2020-03-17 21:04:24 +0000636 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000637 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000638}
639
Colin Cross1b16b0e2019-02-12 14:41:32 -0800640// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
641//
642// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
643// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
644// as a `static_libs` dependency of another module.
645//
646// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
647// a device.
648//
649// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
650// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700651func LibraryFactory() android.Module {
652 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700653
Colin Crossce6734e2020-06-15 16:09:53 -0700654 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700655
Paul Duffin3accbb52021-06-23 11:39:47 +0100656 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100657
Jiyong Park7f7766d2019-07-25 22:02:35 +0900658 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900659 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900660 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700661 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700662}
663
Colin Cross1b16b0e2019-02-12 14:41:32 -0800664// java_library_static is an obsolete alias for java_library.
665func LibraryStaticFactory() android.Module {
666 return LibraryFactory()
667}
668
669// java_library_host builds and links sources into a `.jar` file for the host.
670//
671// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
672// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700673func LibraryHostFactory() android.Module {
674 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700675
Colin Crossce6734e2020-06-15 16:09:53 -0700676 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700677
Colin Cross9ae1b922018-06-26 17:59:05 -0700678 module.Module.properties.Installable = proptools.BoolPtr(true)
679
Jiyong Park7f7766d2019-07-25 22:02:35 +0900680 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100681 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900682 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700683 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700684}
685
686//
Colin Crossb628ea52018-08-14 16:42:33 -0700687// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700688//
689
Dan Shi95d19422020-08-15 12:24:26 -0700690// Test option struct.
691type TestOptions struct {
692 // a list of extra test configuration files that should be installed with the module.
693 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800694
695 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
696 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700697}
698
Colin Cross05638fc2018-04-09 18:40:24 -0700699type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700700 // list of compatibility suites (for example "cts", "vts") that the module should be
701 // installed into.
702 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700703
704 // the name of the test configuration (for example "AndroidTest.xml") that should be
705 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800706 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700707
Jack He33338892018-09-19 02:21:28 -0700708 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
709 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800710 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700711
Colin Crossd96ca352018-08-10 16:06:24 -0700712 // list of files or filegroup modules that provide data that should be installed alongside
713 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900714 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700715
716 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
717 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
718 // explicitly.
719 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800720
721 // Add parameterized mainline modules to auto generated test config. The options will be
722 // handled by TradeFed to do downloading and installing the specified modules on the device.
723 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700724
725 // Test options.
726 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800727
728 // Names of modules containing JNI libraries that should be installed alongside the test.
729 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700730}
731
Liz Kammerdd849a82020-06-12 16:38:45 -0700732type hostTestProperties struct {
733 // list of native binary modules that should be installed alongside the test
734 Data_native_bins []string `android:"arch_variant"`
735}
736
Paul Duffin42df1442019-03-20 12:45:53 +0000737type testHelperLibraryProperties struct {
738 // list of compatibility suites (for example "cts", "vts") that the module should be
739 // installed into.
740 Test_suites []string `android:"arch_variant"`
741}
742
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000743type prebuiltTestProperties struct {
744 // list of compatibility suites (for example "cts", "vts") that the module should be
745 // installed into.
746 Test_suites []string `android:"arch_variant"`
747
748 // the name of the test configuration (for example "AndroidTest.xml") that should be
749 // installed with the module.
750 Test_config *string `android:"path,arch_variant"`
751}
752
Colin Cross05638fc2018-04-09 18:40:24 -0700753type Test struct {
754 Library
755
756 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700757
Dan Shi95d19422020-08-15 12:24:26 -0700758 testConfig android.Path
759 extraTestConfigs android.Paths
760 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700761}
762
Liz Kammerdd849a82020-06-12 16:38:45 -0700763type TestHost struct {
764 Test
765
766 testHostProperties hostTestProperties
767}
768
Paul Duffin42df1442019-03-20 12:45:53 +0000769type TestHelperLibrary struct {
770 Library
771
772 testHelperLibraryProperties testHelperLibraryProperties
773}
774
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000775type JavaTestImport struct {
776 Import
777
778 prebuiltTestProperties prebuiltTestProperties
779
780 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700781 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000782}
783
Liz Kammerdd849a82020-06-12 16:38:45 -0700784func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
785 if len(j.testHostProperties.Data_native_bins) > 0 {
786 for _, target := range ctx.MultiTargets() {
787 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
788 }
789 }
790
Colin Crossf8d9c492021-01-26 11:01:43 -0800791 if len(j.testProperties.Jni_libs) > 0 {
792 for _, target := range ctx.MultiTargets() {
793 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
794 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
795 }
796 }
797
Liz Kammerdd849a82020-06-12 16:38:45 -0700798 j.deps(ctx)
799}
800
Yuexi Ma627263f2021-03-04 13:47:56 -0800801func (j *TestHost) AddExtraResource(p android.Path) {
802 j.extraResources = append(j.extraResources, p)
803}
804
Colin Cross303e21f2018-08-07 16:49:25 -0700805func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000806 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
807 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700808 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000809 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
810 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700811 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800812 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700813
Colin Cross8a497952019-03-05 22:25:09 -0800814 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700815
Dan Shi95d19422020-08-15 12:24:26 -0700816 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
817
Liz Kammerdd849a82020-06-12 16:38:45 -0700818 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
819 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
820 })
821
Colin Crossf8d9c492021-01-26 11:01:43 -0800822 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
823 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
824 if sharedLibInfo.SharedLibrary != nil {
825 // Copy to an intermediate output directory to append "lib[64]" to the path,
826 // so that it's compatible with the default rpath values.
827 var relPath string
828 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
829 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
830 } else {
831 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
832 }
833 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
834 ctx.Build(pctx, android.BuildParams{
835 Rule: android.Cp,
836 Input: sharedLibInfo.SharedLibrary,
837 Output: relocatedLib,
838 })
839 j.data = append(j.data, relocatedLib)
840 } else {
841 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
842 }
843 })
844
Colin Cross303e21f2018-08-07 16:49:25 -0700845 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700846}
847
Paul Duffin42df1442019-03-20 12:45:53 +0000848func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
849 j.Library.GenerateAndroidBuildActions(ctx)
850}
851
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000852func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
853 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800854 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000855
856 j.Import.GenerateAndroidBuildActions(ctx)
857}
858
859type testSdkMemberType struct {
860 android.SdkMemberTypeBase
861}
862
863func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
864 mctx.AddVariationDependencies(nil, dependencyTag, names...)
865}
866
867func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
868 _, ok := module.(*Test)
869 return ok
870}
871
Paul Duffin3a4eb502020-03-19 16:11:18 +0000872func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
873 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000874}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000875
Paul Duffin14eb4672020-03-02 11:33:02 +0000876func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
877 return &testSdkMemberProperties{}
878}
879
880type testSdkMemberProperties struct {
881 android.SdkMemberPropertiesBase
882
Paul Duffina551a1c2020-03-17 21:04:24 +0000883 JarToExport android.Path
884 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000885}
886
Paul Duffin3a4eb502020-03-19 16:11:18 +0000887func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000888 test := variant.(*Test)
889
890 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000891 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000892 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000893 }
894
Paul Duffina551a1c2020-03-17 21:04:24 +0000895 p.JarToExport = implementationJars[0]
896 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000897}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000898
Paul Duffin3a4eb502020-03-19 16:11:18 +0000899func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000900 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000901
Paul Duffina551a1c2020-03-17 21:04:24 +0000902 exportedJar := p.JarToExport
903 if exportedJar != nil {
904 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
905 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000906
907 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000908 }
909
910 testConfig := p.TestConfig
911 if testConfig != nil {
912 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
913 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000914 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
915 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000916}
917
Colin Cross1b16b0e2019-02-12 14:41:32 -0800918// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
919// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
920//
921// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
922// compiled against the device bootclasspath.
923//
924// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
925// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700926func TestFactory() android.Module {
927 module := &Test{}
928
Colin Crossce6734e2020-06-15 16:09:53 -0700929 module.addHostAndDeviceProperties()
930 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700931
Colin Cross9ae1b922018-06-26 17:59:05 -0700932 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800933 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700934 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700935
Paul Duffinb6b89a42021-05-06 16:33:43 +0100936 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -0700937 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700938 return module
939}
940
Paul Duffin42df1442019-03-20 12:45:53 +0000941// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
942func TestHelperLibraryFactory() android.Module {
943 module := &TestHelperLibrary{}
944
Colin Crossce6734e2020-06-15 16:09:53 -0700945 module.addHostAndDeviceProperties()
946 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000947
Colin Cross9a4abed2019-04-24 13:19:28 -0700948 module.Module.properties.Installable = proptools.BoolPtr(true)
949 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700950 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700951
Paul Duffin42df1442019-03-20 12:45:53 +0000952 InitJavaModule(module, android.HostAndDeviceSupported)
953 return module
954}
955
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000956// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
957// and makes sure that it is added to the appropriate test suite.
958//
959// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
960// compiled against an Android classpath.
961//
962// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
963// for host modules.
964func JavaTestImportFactory() android.Module {
965 module := &JavaTestImport{}
966
967 module.AddProperties(
968 &module.Import.properties,
969 &module.prebuiltTestProperties)
970
971 module.Import.properties.Installable = proptools.BoolPtr(true)
972
973 android.InitPrebuiltModule(module, &module.properties.Jars)
974 android.InitApexModule(module)
975 android.InitSdkAwareModule(module)
976 InitJavaModule(module, android.HostAndDeviceSupported)
977 return module
978}
979
Colin Cross1b16b0e2019-02-12 14:41:32 -0800980// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
981// allow running the test with `atest` or a `TEST_MAPPING` file.
982//
983// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
984// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700985func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700986 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700987
Colin Crossce6734e2020-06-15 16:09:53 -0700988 module.addHostProperties()
989 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700990 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700991
Yuexi Ma627263f2021-03-04 13:47:56 -0800992 InitTestHost(
993 module,
994 proptools.BoolPtr(true),
995 nil,
996 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -0700997
Liz Kammerdd849a82020-06-12 16:38:45 -0700998 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +0000999
Colin Cross05638fc2018-04-09 18:40:24 -07001000 return module
1001}
1002
Yuexi Ma627263f2021-03-04 13:47:56 -08001003func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1004 th.properties.Installable = installable
1005 th.testProperties.Auto_gen_config = autoGenConfig
1006 th.testProperties.Test_suites = testSuites
1007}
1008
Colin Cross05638fc2018-04-09 18:40:24 -07001009//
Colin Cross2fe66872015-03-30 17:20:39 -07001010// Java Binaries (.jar file plus wrapper script)
1011//
1012
Colin Crossf506d872017-07-19 15:53:04 -07001013type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001014 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001015 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001016
1017 // Name of the class containing main to be inserted into the manifest as Main-Class.
1018 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001019
1020 // Names of modules containing JNI libraries that should be installed alongside the host
1021 // variant of the binary.
1022 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -07001023}
1024
Colin Crossf506d872017-07-19 15:53:04 -07001025type Binary struct {
1026 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001027
Colin Crossf506d872017-07-19 15:53:04 -07001028 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001029
Colin Cross6b4a32d2017-12-05 13:42:45 -08001030 isWrapperVariant bool
1031
Colin Crossc3315992017-12-08 19:12:36 -08001032 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001033 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001034}
1035
Alex Light24237172017-10-26 09:46:21 -07001036func (j *Binary) HostToolPath() android.OptionalPath {
1037 return android.OptionalPathForPath(j.binaryFile)
1038}
1039
Colin Crossf506d872017-07-19 15:53:04 -07001040func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001041 if ctx.Arch().ArchType == android.Common {
1042 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001043 if j.binaryProperties.Main_class != nil {
1044 if j.properties.Manifest != nil {
1045 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1046 }
1047 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1048 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1049 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1050 }
1051
Colin Cross6b4a32d2017-12-05 13:42:45 -08001052 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001053 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001054 // Handle the binary wrapper
1055 j.isWrapperVariant = true
1056
Colin Cross366938f2017-12-11 16:29:02 -08001057 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001058 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001059 } else {
1060 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1061 }
1062
Colin Crossc179ea62020-10-09 10:54:15 -07001063 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001064 // of the wrapper variant, which will include the common variant's jar file and any JNI
1065 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001066 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001067 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001068 }
Colin Cross2fe66872015-03-30 17:20:39 -07001069}
1070
Colin Crossf506d872017-07-19 15:53:04 -07001071func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001072 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001073 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001074 }
1075 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001076 // These dependencies ensure the host installation rules will install the jar file and
1077 // the jni libraries when the wrapper is installed.
1078 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1079 ctx.AddVariationDependencies(
1080 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1081 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001082 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001083}
1084
Colin Cross1b16b0e2019-02-12 14:41:32 -08001085// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1086// as well.
1087//
1088// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1089// compiled against the device bootclasspath.
1090//
1091// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1092// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001093func BinaryFactory() android.Module {
1094 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001095
Colin Crossce6734e2020-06-15 16:09:53 -07001096 module.addHostAndDeviceProperties()
1097 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001098
Colin Cross9ae1b922018-06-26 17:59:05 -07001099 module.Module.properties.Installable = proptools.BoolPtr(true)
1100
Colin Cross6b4a32d2017-12-05 13:42:45 -08001101 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1102 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001103 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001104}
1105
Colin Cross1b16b0e2019-02-12 14:41:32 -08001106// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1107//
1108// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1109// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001110func BinaryHostFactory() android.Module {
1111 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001112
Colin Crossce6734e2020-06-15 16:09:53 -07001113 module.addHostProperties()
1114 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001115
Colin Cross9ae1b922018-06-26 17:59:05 -07001116 module.Module.properties.Installable = proptools.BoolPtr(true)
1117
Colin Cross6b4a32d2017-12-05 13:42:45 -08001118 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1119 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001120 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001121}
1122
1123//
1124// Java prebuilts
1125//
1126
Colin Cross74d73e22017-08-02 11:05:49 -07001127type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001128 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001129
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001130 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1131 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001132 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001133
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001134 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1135 // specified.
1136 Min_sdk_version *string
1137
Colin Cross535e2cf2017-10-20 17:57:49 -07001138 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001139
Paul Duffin7b3e10a2021-07-15 14:14:41 +01001140 // If not empty, classes are restricted to the specified packages and their sub-packages.
1141 // This information is used to generate the updatable-bcp-packages.txt file.
1142 Permitted_packages []string
1143
Jiyong Park1be96912018-05-28 18:02:19 +09001144 // List of shared java libs that this module has dependencies to
1145 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001146
1147 // List of files to remove from the jar file(s)
1148 Exclude_files []string
1149
1150 // List of directories to remove from the jar file(s)
1151 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001152
1153 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001154 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001155
1156 // set the name of the output
1157 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001158
1159 Aidl struct {
1160 // directories that should be added as include directories for any aidl sources of modules
1161 // that depend on this module, as well as to aidl for this module.
1162 Export_include_dirs []string
1163 }
Colin Cross74d73e22017-08-02 11:05:49 -07001164}
1165
1166type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001167 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001168 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001169 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001170 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001171 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001172
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001173 // Functionality common to Module and Import.
1174 embeddableInModuleAndImport
1175
Liz Kammerd6c31d22020-08-05 15:40:41 -07001176 hiddenAPI
1177 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001178 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001179
Colin Cross74d73e22017-08-02 11:05:49 -07001180 properties ImportProperties
1181
Liz Kammerd6c31d22020-08-05 15:40:41 -07001182 // output file containing classes.dex and resources
1183 dexJarFile android.Path
1184
Colin Cross0a6e0072017-08-30 14:24:55 -07001185 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001186 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001187 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001188
1189 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001190
1191 sdkVersion android.SdkSpec
1192 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001193}
1194
Paul Duffindbcc2962021-07-15 13:35:26 +01001195var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1196
1197func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1198 return j.properties.Permitted_packages
1199}
1200
Jiyong Park92315372021-04-02 08:45:46 +09001201func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1202 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001203}
1204
Jiyong Parkf1691d22021-03-29 20:11:58 +09001205func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001206 return "none"
1207}
1208
Jiyong Park92315372021-04-02 08:45:46 +09001209func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001210 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001211 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001212 }
Jiyong Park92315372021-04-02 08:45:46 +09001213 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001214}
1215
Jiyong Park92315372021-04-02 08:45:46 +09001216func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1217 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001218}
1219
Colin Cross74d73e22017-08-02 11:05:49 -07001220func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001221 return &j.prebuilt
1222}
1223
Colin Cross74d73e22017-08-02 11:05:49 -07001224func (j *Import) PrebuiltSrcs() []string {
1225 return j.properties.Jars
1226}
1227
1228func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001229 return j.prebuilt.Name(j.ModuleBase.Name())
1230}
1231
Jiyong Park0b238752019-10-29 11:23:10 +09001232func (j *Import) Stem() string {
1233 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1234}
1235
Jiyong Park618922e2020-01-08 13:35:43 +09001236func (a *Import) JacocoReportClassesFile() android.Path {
1237 return nil
1238}
1239
Bill Peckhama41a6962021-01-11 10:58:54 -08001240func (j *Import) LintDepSets() LintDepSets {
1241 return LintDepSets{}
1242}
1243
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001244func (j *Import) getStrictUpdatabilityLinting() bool {
1245 return false
1246}
1247
1248func (j *Import) setStrictUpdatabilityLinting(bool) {
1249}
1250
Colin Cross74d73e22017-08-02 11:05:49 -07001251func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001252 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001253
1254 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001255 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001256 }
Colin Cross1e676be2016-10-12 14:38:15 -07001257}
1258
Colin Cross74d73e22017-08-02 11:05:49 -07001259func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001260 j.sdkVersion = j.SdkVersion(ctx)
1261 j.minSdkVersion = j.MinSdkVersion(ctx)
1262
Colin Cross56a83212020-09-15 18:30:11 -07001263 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1264 j.hideApexVariantFromMake = true
1265 }
1266
Colin Cross8a497952019-03-05 22:25:09 -08001267 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001268
Jiyong Park0b238752019-10-29 11:23:10 +09001269 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001270 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001271 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1272 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001273 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001274 inputFile := outputFile
1275 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1276 TransformJetifier(ctx, outputFile, inputFile)
1277 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001278 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001279 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001280
Liz Kammerd6c31d22020-08-05 15:40:41 -07001281 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001282 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001283
Jiyong Park1be96912018-05-28 18:02:19 +09001284 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001285 tag := ctx.OtherModuleDependencyTag(module)
1286
Colin Crossdcf71b22021-02-01 13:59:03 -08001287 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1288 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001289 switch tag {
1290 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001291 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001292 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001293 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001294 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001295 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001296 switch tag {
1297 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001298 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001299 }
1300 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001301
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001302 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001303
1304 // Save away the `deapexer` module on which this depends, if any.
1305 if tag == android.DeapexerTag {
Martin Stjernholm95994062021-06-30 16:35:07 +01001306 if deapexerModule != nil {
1307 ctx.ModuleErrorf("Ambiguous duplicate deapexer module dependencies %q and %q",
1308 deapexerModule.Name(), module.Name())
1309 }
Paul Duffin064b70c2020-11-02 17:32:38 +00001310 deapexerModule = module
1311 }
Jiyong Park1be96912018-05-28 18:02:19 +09001312 })
1313
Nan Zhang4973ecf2018-08-10 13:42:12 -07001314 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001315 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001316 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001317 }
Jiyong Park19604de2020-03-24 16:44:11 +09001318
1319 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001320
Paul Duffin064b70c2020-11-02 17:32:38 +00001321 if ctx.Device() {
1322 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1323 // obtained from the associated deapexer module.
1324 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1325 if ai.ForPrebuiltApex {
1326 if deapexerModule == nil {
1327 // This should never happen as a variant for a prebuilt_apex is only created if the
Paul Duffinb17d0442021-05-05 12:07:00 +01001328 // deapexer module has been configured to export the dex implementation jar for this module.
Paul Duffin064b70c2020-11-02 17:32:38 +00001329 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1330 j.Name(), ai.ApexVariationName)
Paul Duffinb17d0442021-05-05 12:07:00 +01001331 return
Paul Duffin064b70c2020-11-02 17:32:38 +00001332 }
1333
1334 // Get the path of the dex implementation jar from the `deapexer` module.
1335 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin034196d2021-06-17 15:59:07 +01001336 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Paul Duffin9d67ca62021-02-03 20:06:33 +00001337 j.dexJarFile = dexOutputPath
Paul Duffinf88ab952021-05-14 14:18:47 +01001338
1339 // Initialize the hiddenapi structure.
Paul Duffin6c6dde02021-05-14 15:52:25 +01001340 j.initHiddenAPI(ctx, dexOutputPath, outputFile, nil)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001341 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001342 // This should never happen as a variant for a prebuilt_apex is only created if the
1343 // prebuilt_apex has been configured to export the java library dex file.
1344 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1345 }
1346 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001347 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001348 if sdkDep.invalidVersion {
1349 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1350 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1351 } else if sdkDep.useFiles {
1352 // sdkDep.jar is actually equivalent to turbine header.jar.
1353 flags.classpath = append(flags.classpath, sdkDep.jars...)
1354 }
1355
1356 // Dex compilation
1357
1358 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1359 if j.dexProperties.Uncompress_dex == nil {
1360 // If the value was not force-set by the user, use reasonable default based on the module.
1361 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1362 }
1363 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1364
Paul Duffin612e6102021-02-02 13:38:13 +00001365 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001366 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001367 if ctx.Failed() {
1368 return
1369 }
1370
Paul Duffinf88ab952021-05-14 14:18:47 +01001371 // Initialize the hiddenapi structure.
Paul Duffin6c6dde02021-05-14 15:52:25 +01001372 j.initHiddenAPI(ctx, dexOutputFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001373
1374 // Encode hidden API flags in dex file.
Paul Duffin6c6dde02021-05-14 15:52:25 +01001375 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001376
1377 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001378 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001379 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001380
1381 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1382 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1383 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1384 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1385 AidlIncludeDirs: j.exportAidlIncludeDirs,
1386 })
Colin Cross2fe66872015-03-30 17:20:39 -07001387}
1388
Paul Duffinaa55f742020-10-06 17:20:13 +01001389func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1390 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001391 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001392 return android.Paths{j.combinedClasspathFile}, nil
1393 default:
1394 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1395 }
1396}
1397
1398var _ android.OutputFileProducer = (*Import)(nil)
1399
Nan Zhanged19fc32017-10-19 13:06:22 -07001400func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001401 if j.combinedClasspathFile == nil {
1402 return nil
1403 }
Colin Cross37f6d792018-07-12 12:28:41 -07001404 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001405}
1406
Colin Cross331a1212018-08-15 20:40:52 -07001407func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001408 if j.combinedClasspathFile == nil {
1409 return nil
1410 }
Colin Cross331a1212018-08-15 20:40:52 -07001411 return android.Paths{j.combinedClasspathFile}
1412}
1413
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001414func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001415 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001416}
1417
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001418func (j *Import) DexJarInstallPath() android.Path {
1419 return nil
1420}
1421
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001422func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1423 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001424}
1425
Jiyong Park45bf82e2020-12-15 22:29:02 +09001426var _ android.ApexModule = (*Import)(nil)
1427
1428// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001429func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001430 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001431}
1432
Jiyong Park45bf82e2020-12-15 22:29:02 +09001433// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001434func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1435 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001436 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001437 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001438 return fmt.Errorf("min_sdk_version is not specified")
1439 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001440 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001441 return nil
1442 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001443 ver, err := sdkSpec.EffectiveVersion(ctx)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001444 if err != nil {
1445 return err
1446 }
Jiyong Park54105c42021-03-31 18:17:53 +09001447 if ver.GreaterThan(sdkVersion) {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001448 return fmt.Errorf("newer SDK(%v)", ver)
1449 }
Jooyung Han749dc692020-04-15 11:03:39 +09001450 return nil
1451}
1452
Paul Duffin7db57e02021-06-17 14:56:05 +01001453// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1454// java_sdk_library_import with the specified base module name requires to be exported from a
1455// prebuilt_apex/apex_set.
Paul Duffin034196d2021-06-17 15:59:07 +01001456func requiredFilesFromPrebuiltApexForImport(name string) []string {
1457 // Add the dex implementation jar to the set of exported files.
1458 return []string{
1459 apexRootRelativePathToJavaLib(name),
Paul Duffin7db57e02021-06-17 14:56:05 +01001460 }
1461}
1462
Paul Duffin034196d2021-06-17 15:59:07 +01001463// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1464// the java library with the specified name.
1465func apexRootRelativePathToJavaLib(name string) string {
1466 return filepath.Join("javalib", name+".jar")
1467}
1468
Paul Duffin7db57e02021-06-17 14:56:05 +01001469var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1470
Paul Duffin034196d2021-06-17 15:59:07 +01001471func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffin7db57e02021-06-17 14:56:05 +01001472 name := j.BaseModuleName()
1473 return requiredFilesFromPrebuiltApexForImport(name)
1474}
1475
albaltai36ff7dc2018-12-25 14:35:23 +08001476// Add compile time check for interface implementation
1477var _ android.IDEInfo = (*Import)(nil)
1478var _ android.IDECustomizedModuleName = (*Import)(nil)
1479
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001480// Collect information for opening IDE project files in java/jdeps.go.
1481const (
1482 removedPrefix = "prebuilt_"
1483)
1484
1485func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1486 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1487}
1488
1489func (j *Import) IDECustomizedModuleName() string {
1490 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1491 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1492 // solution to get the Import name.
1493 name := j.Name()
1494 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001495 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001496 }
1497 return name
1498}
1499
Colin Cross74d73e22017-08-02 11:05:49 -07001500var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001501
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001502func (j *Import) IsInstallable() bool {
1503 return Bool(j.properties.Installable)
1504}
1505
1506var _ dexpreopterInterface = (*Import)(nil)
1507
Colin Cross1b16b0e2019-02-12 14:41:32 -08001508// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1509//
1510// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1511// compiled against an Android classpath.
1512//
1513// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1514// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001515func ImportFactory() android.Module {
1516 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001517
Liz Kammerd6c31d22020-08-05 15:40:41 -07001518 module.AddProperties(
1519 &module.properties,
1520 &module.dexer.dexProperties,
1521 )
Colin Cross74d73e22017-08-02 11:05:49 -07001522
Paul Duffin3accbb52021-06-23 11:39:47 +01001523 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001524
Liz Kammerd6c31d22020-08-05 15:40:41 -07001525 module.dexProperties.Optimize.EnabledByDefault = false
1526
Colin Cross74d73e22017-08-02 11:05:49 -07001527 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001528 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001529 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001530 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001531 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001532}
1533
Colin Cross1b16b0e2019-02-12 14:41:32 -08001534// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1535// module.
1536//
1537// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1538// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001539func ImportFactoryHost() android.Module {
1540 module := &Import{}
1541
1542 module.AddProperties(&module.properties)
1543
1544 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001545 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001546 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001547 return module
1548}
1549
Colin Cross42be7612019-02-21 18:12:14 -08001550// dex_import module
1551
1552type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001553 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001554
1555 // set the name of the output
1556 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001557}
1558
1559type DexImport struct {
1560 android.ModuleBase
1561 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001562 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001563 prebuilt android.Prebuilt
1564
1565 properties DexImportProperties
1566
Colin Crossb014f072021-02-26 14:54:36 -08001567 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001568
1569 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001570
1571 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001572}
1573
1574func (j *DexImport) Prebuilt() *android.Prebuilt {
1575 return &j.prebuilt
1576}
1577
1578func (j *DexImport) PrebuiltSrcs() []string {
1579 return j.properties.Jars
1580}
1581
1582func (j *DexImport) Name() string {
1583 return j.prebuilt.Name(j.ModuleBase.Name())
1584}
1585
Jiyong Park0b238752019-10-29 11:23:10 +09001586func (j *DexImport) Stem() string {
1587 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1588}
1589
Jiyong Park77acec62020-06-01 21:39:15 +09001590func (a *DexImport) JacocoReportClassesFile() android.Path {
1591 return nil
1592}
1593
Colin Cross08dca382020-07-21 20:31:17 -07001594func (a *DexImport) LintDepSets() LintDepSets {
1595 return LintDepSets{}
1596}
1597
Martin Stjernholm6d415272020-01-31 17:10:36 +00001598func (j *DexImport) IsInstallable() bool {
1599 return true
1600}
1601
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001602func (j *DexImport) getStrictUpdatabilityLinting() bool {
1603 return false
1604}
1605
1606func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1607}
1608
Colin Cross42be7612019-02-21 18:12:14 -08001609func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1610 if len(j.properties.Jars) != 1 {
1611 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1612 }
1613
Colin Cross56a83212020-09-15 18:30:11 -07001614 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1615 if !apexInfo.IsForPlatform() {
1616 j.hideApexVariantFromMake = true
1617 }
1618
Jiyong Park0b238752019-10-29 11:23:10 +09001619 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001620 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1621
1622 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1623 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1624
1625 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001626 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001627
1628 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1629 rule.Temporary(temporary)
1630
1631 // use zip2zip to uncompress classes*.dex files
1632 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001633 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001634 FlagWithInput("-i ", inputJar).
1635 FlagWithOutput("-o ", temporary).
1636 FlagWithArg("-0 ", "'classes*.dex'")
1637
1638 // use zipalign to align uncompressed classes*.dex files
1639 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001640 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001641 Flag("-f").
1642 Text("4").
1643 Input(temporary).
1644 Output(dexOutputFile)
1645
1646 rule.DeleteTemporaryFiles()
1647
Colin Crossf1a035e2020-11-16 17:32:30 -08001648 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001649 } else {
1650 ctx.Build(pctx, android.BuildParams{
1651 Rule: android.Cp,
1652 Input: inputJar,
1653 Output: dexOutputFile,
1654 })
1655 }
1656
1657 j.dexJarFile = dexOutputFile
1658
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001659 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001660
Colin Cross56a83212020-09-15 18:30:11 -07001661 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001662 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1663 j.Stem()+".jar", dexOutputFile)
1664 }
Colin Cross42be7612019-02-21 18:12:14 -08001665}
1666
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001667func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001668 return j.dexJarFile
1669}
1670
Jiyong Park45bf82e2020-12-15 22:29:02 +09001671var _ android.ApexModule = (*DexImport)(nil)
1672
1673// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001674func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1675 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001676 // we don't check prebuilt modules for sdk_version
1677 return nil
1678}
1679
Colin Cross42be7612019-02-21 18:12:14 -08001680// dex_import imports a `.jar` file containing classes.dex files.
1681//
1682// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1683// to the device.
1684func DexImportFactory() android.Module {
1685 module := &DexImport{}
1686
1687 module.AddProperties(&module.properties)
1688
1689 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001690 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001691 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001692 return module
1693}
1694
Colin Cross89536d42017-07-07 14:35:50 -07001695//
1696// Defaults
1697//
1698type Defaults struct {
1699 android.ModuleBase
1700 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001701 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001702}
1703
Colin Cross1b16b0e2019-02-12 14:41:32 -08001704// java_defaults provides a set of properties that can be inherited by other java or android modules.
1705//
1706// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1707// property in the defaults module that exists in the depending module will be prepended to the depending module's
1708// value for that property.
1709//
1710// Example:
1711//
1712// java_defaults {
1713// name: "example_defaults",
1714// srcs: ["common/**/*.java"],
1715// javacflags: ["-Xlint:all"],
1716// aaptflags: ["--auto-add-overlay"],
1717// }
1718//
1719// java_library {
1720// name: "example",
1721// defaults: ["example_defaults"],
1722// srcs: ["example/**/*.java"],
1723// }
1724//
1725// is functionally identical to:
1726//
1727// java_library {
1728// name: "example",
1729// srcs: [
1730// "common/**/*.java",
1731// "example/**/*.java",
1732// ],
1733// javacflags: ["-Xlint:all"],
1734// }
Paul Duffin47357662019-12-05 14:07:14 +00001735func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001736 module := &Defaults{}
1737
Colin Cross89536d42017-07-07 14:35:50 -07001738 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001739 &CommonProperties{},
1740 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001741 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001742 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001743 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001744 &aaptProperties{},
1745 &androidLibraryProperties{},
1746 &appProperties{},
1747 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001748 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001749 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001750 &ImportProperties{},
1751 &AARImportProperties{},
1752 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001753 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001754 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001755 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001756 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001757 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001758 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001759 )
1760
1761 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001762 return module
1763}
Nan Zhangea568a42017-11-08 21:20:04 -08001764
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001765func kytheExtractJavaFactory() android.Singleton {
1766 return &kytheExtractJavaSingleton{}
1767}
1768
1769type kytheExtractJavaSingleton struct {
1770}
1771
1772func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1773 var xrefTargets android.Paths
1774 ctx.VisitAllModules(func(module android.Module) {
1775 if javaModule, ok := module.(xref); ok {
1776 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1777 }
1778 })
1779 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1780 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001781 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001782 }
1783}
1784
Nan Zhangea568a42017-11-08 21:20:04 -08001785var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001786var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001787var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001788var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001789
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001790// Add class loader context (CLC) of a given dependency to the current CLC.
1791func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1792 clcMap dexpreopt.ClassLoaderContextMap) {
1793
1794 dep, ok := depModule.(UsesLibraryDependency)
1795 if !ok {
1796 return
1797 }
1798
1799 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1800 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1801 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1802 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1803 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1804 // the current CLC.
1805 var implicitSdkLib *string
1806 comp, isComp := depModule.(SdkLibraryComponentDependency)
1807 if isComp {
1808 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1809 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1810 }
1811 if implicitSdkLib == nil {
1812 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1813 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001814 }
1815 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001816
1817 depTag := ctx.OtherModuleDependencyTag(depModule)
1818 if depTag == libTag || depTag == usesLibTag {
1819 // Ok, propagate <uses-library> through non-static library dependencies.
1820 } else if depTag == staticLibTag {
1821 // Propagate <uses-library> through static library dependencies, unless it is a component
1822 // library (such as stubs). Component libraries have a dependency on their SDK library,
1823 // which should not be pulled just because of a static component library.
1824 if implicitSdkLib != nil {
1825 return
1826 }
1827 } else {
1828 // Don't propagate <uses-library> for other dependency tags.
1829 return
1830 }
1831
1832 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001833 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001834 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1835 } else {
1836 depName := ctx.OtherModuleName(depModule)
1837 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1838 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001839}