blob: 2bbb5b10208bb908214bba193e48ab0f3e6e92f8 [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 Stjernholm0e4cceb2021-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 Duffin14eb4672020-03-02 11:33:02 +0000584}
585
Paul Duffin3a4eb502020-03-19 16:11:18 +0000586func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000587 j := variant.(*Library)
588
Paul Duffindb170e42020-12-08 17:48:25 +0000589 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
590
Paul Duffina551a1c2020-03-17 21:04:24 +0000591 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +0000592}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000593
Paul Duffin3a4eb502020-03-19 16:11:18 +0000594func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000595 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000596
Paul Duffindb170e42020-12-08 17:48:25 +0000597 memberType := ctx.MemberType().(*librarySdkMemberType)
598
Paul Duffina551a1c2020-03-17 21:04:24 +0000599 exportedJar := p.JarToExport
600 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000601 // Delegate the creation of the snapshot relative path to the member type.
602 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
603
604 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000605 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
606
Paul Duffina551a1c2020-03-17 21:04:24 +0000607 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
608 }
609
Paul Duffindb170e42020-12-08 17:48:25 +0000610 // Do not copy anything else to the snapshot.
611 if memberType.onlyCopyJarToSnapshot {
612 return
613 }
614
Paul Duffina551a1c2020-03-17 21:04:24 +0000615 aidlIncludeDirs := p.AidlIncludeDirs
616 if len(aidlIncludeDirs) != 0 {
617 sdkModuleContext := ctx.SdkModuleContext()
618 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000619 // TODO(jiyong): copy parcelable declarations only
620 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
621 for _, file := range aidlFiles {
622 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
623 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000624 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000625
Paul Duffina551a1c2020-03-17 21:04:24 +0000626 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000627 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000628}
629
Colin Cross1b16b0e2019-02-12 14:41:32 -0800630// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
631//
632// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
633// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
634// as a `static_libs` dependency of another module.
635//
636// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
637// a device.
638//
639// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
640// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700641func LibraryFactory() android.Module {
642 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700643
Colin Crossce6734e2020-06-15 16:09:53 -0700644 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700645
Paul Duffin859fe962020-05-15 10:20:31 +0100646 module.initModuleAndImport(&module.ModuleBase)
647
Jiyong Park7f7766d2019-07-25 22:02:35 +0900648 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900649 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900650 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700651 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700652}
653
Colin Cross1b16b0e2019-02-12 14:41:32 -0800654// java_library_static is an obsolete alias for java_library.
655func LibraryStaticFactory() android.Module {
656 return LibraryFactory()
657}
658
659// java_library_host builds and links sources into a `.jar` file for the host.
660//
661// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
662// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700663func LibraryHostFactory() android.Module {
664 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700665
Colin Crossce6734e2020-06-15 16:09:53 -0700666 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700667
Colin Cross9ae1b922018-06-26 17:59:05 -0700668 module.Module.properties.Installable = proptools.BoolPtr(true)
669
Jiyong Park7f7766d2019-07-25 22:02:35 +0900670 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100671 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900672 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700673 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700674}
675
676//
Colin Crossb628ea52018-08-14 16:42:33 -0700677// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700678//
679
Dan Shi95d19422020-08-15 12:24:26 -0700680// Test option struct.
681type TestOptions struct {
682 // a list of extra test configuration files that should be installed with the module.
683 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800684
685 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
686 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700687}
688
Colin Cross05638fc2018-04-09 18:40:24 -0700689type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700690 // list of compatibility suites (for example "cts", "vts") that the module should be
691 // installed into.
692 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700693
694 // the name of the test configuration (for example "AndroidTest.xml") that should be
695 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800696 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700697
Jack He33338892018-09-19 02:21:28 -0700698 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
699 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800700 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700701
Colin Crossd96ca352018-08-10 16:06:24 -0700702 // list of files or filegroup modules that provide data that should be installed alongside
703 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900704 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700705
706 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
707 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
708 // explicitly.
709 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800710
711 // Add parameterized mainline modules to auto generated test config. The options will be
712 // handled by TradeFed to do downloading and installing the specified modules on the device.
713 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700714
715 // Test options.
716 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800717
718 // Names of modules containing JNI libraries that should be installed alongside the test.
719 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700720}
721
Liz Kammerdd849a82020-06-12 16:38:45 -0700722type hostTestProperties struct {
723 // list of native binary modules that should be installed alongside the test
724 Data_native_bins []string `android:"arch_variant"`
725}
726
Paul Duffin42df1442019-03-20 12:45:53 +0000727type testHelperLibraryProperties struct {
728 // list of compatibility suites (for example "cts", "vts") that the module should be
729 // installed into.
730 Test_suites []string `android:"arch_variant"`
731}
732
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000733type prebuiltTestProperties struct {
734 // list of compatibility suites (for example "cts", "vts") that the module should be
735 // installed into.
736 Test_suites []string `android:"arch_variant"`
737
738 // the name of the test configuration (for example "AndroidTest.xml") that should be
739 // installed with the module.
740 Test_config *string `android:"path,arch_variant"`
741}
742
Colin Cross05638fc2018-04-09 18:40:24 -0700743type Test struct {
744 Library
745
746 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700747
Dan Shi95d19422020-08-15 12:24:26 -0700748 testConfig android.Path
749 extraTestConfigs android.Paths
750 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700751}
752
Liz Kammerdd849a82020-06-12 16:38:45 -0700753type TestHost struct {
754 Test
755
756 testHostProperties hostTestProperties
757}
758
Paul Duffin42df1442019-03-20 12:45:53 +0000759type TestHelperLibrary struct {
760 Library
761
762 testHelperLibraryProperties testHelperLibraryProperties
763}
764
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000765type JavaTestImport struct {
766 Import
767
768 prebuiltTestProperties prebuiltTestProperties
769
770 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700771 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000772}
773
Liz Kammerdd849a82020-06-12 16:38:45 -0700774func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
775 if len(j.testHostProperties.Data_native_bins) > 0 {
776 for _, target := range ctx.MultiTargets() {
777 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
778 }
779 }
780
Colin Crossf8d9c492021-01-26 11:01:43 -0800781 if len(j.testProperties.Jni_libs) > 0 {
782 for _, target := range ctx.MultiTargets() {
783 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
784 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
785 }
786 }
787
Liz Kammerdd849a82020-06-12 16:38:45 -0700788 j.deps(ctx)
789}
790
Yuexi Ma627263f2021-03-04 13:47:56 -0800791func (j *TestHost) AddExtraResource(p android.Path) {
792 j.extraResources = append(j.extraResources, p)
793}
794
Colin Cross303e21f2018-08-07 16:49:25 -0700795func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000796 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
797 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700798 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000799 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
800 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700801 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800802 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700803
Colin Cross8a497952019-03-05 22:25:09 -0800804 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700805
Dan Shi95d19422020-08-15 12:24:26 -0700806 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
807
Liz Kammerdd849a82020-06-12 16:38:45 -0700808 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
809 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
810 })
811
Colin Crossf8d9c492021-01-26 11:01:43 -0800812 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
813 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
814 if sharedLibInfo.SharedLibrary != nil {
815 // Copy to an intermediate output directory to append "lib[64]" to the path,
816 // so that it's compatible with the default rpath values.
817 var relPath string
818 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
819 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
820 } else {
821 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
822 }
823 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
824 ctx.Build(pctx, android.BuildParams{
825 Rule: android.Cp,
826 Input: sharedLibInfo.SharedLibrary,
827 Output: relocatedLib,
828 })
829 j.data = append(j.data, relocatedLib)
830 } else {
831 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
832 }
833 })
834
Colin Cross303e21f2018-08-07 16:49:25 -0700835 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700836}
837
Paul Duffin42df1442019-03-20 12:45:53 +0000838func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
839 j.Library.GenerateAndroidBuildActions(ctx)
840}
841
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000842func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
843 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800844 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000845
846 j.Import.GenerateAndroidBuildActions(ctx)
847}
848
849type testSdkMemberType struct {
850 android.SdkMemberTypeBase
851}
852
853func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
854 mctx.AddVariationDependencies(nil, dependencyTag, names...)
855}
856
857func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
858 _, ok := module.(*Test)
859 return ok
860}
861
Paul Duffin3a4eb502020-03-19 16:11:18 +0000862func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
863 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000864}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000865
Paul Duffin14eb4672020-03-02 11:33:02 +0000866func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
867 return &testSdkMemberProperties{}
868}
869
870type testSdkMemberProperties struct {
871 android.SdkMemberPropertiesBase
872
Paul Duffina551a1c2020-03-17 21:04:24 +0000873 JarToExport android.Path
874 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000875}
876
Paul Duffin3a4eb502020-03-19 16:11:18 +0000877func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000878 test := variant.(*Test)
879
880 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000881 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000882 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000883 }
884
Paul Duffina551a1c2020-03-17 21:04:24 +0000885 p.JarToExport = implementationJars[0]
886 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000887}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000888
Paul Duffin3a4eb502020-03-19 16:11:18 +0000889func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000890 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000891
Paul Duffina551a1c2020-03-17 21:04:24 +0000892 exportedJar := p.JarToExport
893 if exportedJar != nil {
894 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
895 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000896
897 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000898 }
899
900 testConfig := p.TestConfig
901 if testConfig != nil {
902 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
903 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000904 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
905 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000906}
907
Colin Cross1b16b0e2019-02-12 14:41:32 -0800908// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
909// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
910//
911// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
912// compiled against the device bootclasspath.
913//
914// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
915// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700916func TestFactory() android.Module {
917 module := &Test{}
918
Colin Crossce6734e2020-06-15 16:09:53 -0700919 module.addHostAndDeviceProperties()
920 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700921
Colin Cross9ae1b922018-06-26 17:59:05 -0700922 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800923 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700924 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700925
Paul Duffinb6b89a42021-05-06 16:33:43 +0100926 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -0700927 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700928 return module
929}
930
Paul Duffin42df1442019-03-20 12:45:53 +0000931// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
932func TestHelperLibraryFactory() android.Module {
933 module := &TestHelperLibrary{}
934
Colin Crossce6734e2020-06-15 16:09:53 -0700935 module.addHostAndDeviceProperties()
936 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000937
Colin Cross9a4abed2019-04-24 13:19:28 -0700938 module.Module.properties.Installable = proptools.BoolPtr(true)
939 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700940 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700941
Paul Duffin42df1442019-03-20 12:45:53 +0000942 InitJavaModule(module, android.HostAndDeviceSupported)
943 return module
944}
945
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000946// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
947// and makes sure that it is added to the appropriate test suite.
948//
949// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
950// compiled against an Android classpath.
951//
952// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
953// for host modules.
954func JavaTestImportFactory() android.Module {
955 module := &JavaTestImport{}
956
957 module.AddProperties(
958 &module.Import.properties,
959 &module.prebuiltTestProperties)
960
961 module.Import.properties.Installable = proptools.BoolPtr(true)
962
963 android.InitPrebuiltModule(module, &module.properties.Jars)
964 android.InitApexModule(module)
965 android.InitSdkAwareModule(module)
966 InitJavaModule(module, android.HostAndDeviceSupported)
967 return module
968}
969
Colin Cross1b16b0e2019-02-12 14:41:32 -0800970// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
971// allow running the test with `atest` or a `TEST_MAPPING` file.
972//
973// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
974// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700975func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700976 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700977
Colin Crossce6734e2020-06-15 16:09:53 -0700978 module.addHostProperties()
979 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700980 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700981
Yuexi Ma627263f2021-03-04 13:47:56 -0800982 InitTestHost(
983 module,
984 proptools.BoolPtr(true),
985 nil,
986 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -0700987
Liz Kammerdd849a82020-06-12 16:38:45 -0700988 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +0000989
Colin Cross05638fc2018-04-09 18:40:24 -0700990 return module
991}
992
Yuexi Ma627263f2021-03-04 13:47:56 -0800993func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
994 th.properties.Installable = installable
995 th.testProperties.Auto_gen_config = autoGenConfig
996 th.testProperties.Test_suites = testSuites
997}
998
Colin Cross05638fc2018-04-09 18:40:24 -0700999//
Colin Cross2fe66872015-03-30 17:20:39 -07001000// Java Binaries (.jar file plus wrapper script)
1001//
1002
Colin Crossf506d872017-07-19 15:53:04 -07001003type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001004 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001005 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001006
1007 // Name of the class containing main to be inserted into the manifest as Main-Class.
1008 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001009
1010 // Names of modules containing JNI libraries that should be installed alongside the host
1011 // variant of the binary.
1012 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -07001013}
1014
Colin Crossf506d872017-07-19 15:53:04 -07001015type Binary struct {
1016 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001017
Colin Crossf506d872017-07-19 15:53:04 -07001018 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001019
Colin Cross6b4a32d2017-12-05 13:42:45 -08001020 isWrapperVariant bool
1021
Colin Crossc3315992017-12-08 19:12:36 -08001022 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001023 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001024}
1025
Alex Light24237172017-10-26 09:46:21 -07001026func (j *Binary) HostToolPath() android.OptionalPath {
1027 return android.OptionalPathForPath(j.binaryFile)
1028}
1029
Colin Crossf506d872017-07-19 15:53:04 -07001030func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001031 if ctx.Arch().ArchType == android.Common {
1032 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001033 if j.binaryProperties.Main_class != nil {
1034 if j.properties.Manifest != nil {
1035 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1036 }
1037 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1038 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1039 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1040 }
1041
Colin Cross6b4a32d2017-12-05 13:42:45 -08001042 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001043 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001044 // Handle the binary wrapper
1045 j.isWrapperVariant = true
1046
Colin Cross366938f2017-12-11 16:29:02 -08001047 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001048 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001049 } else {
1050 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1051 }
1052
Colin Crossc179ea62020-10-09 10:54:15 -07001053 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001054 // of the wrapper variant, which will include the common variant's jar file and any JNI
1055 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001056 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001057 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001058 }
Colin Cross2fe66872015-03-30 17:20:39 -07001059}
1060
Colin Crossf506d872017-07-19 15:53:04 -07001061func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001062 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001063 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001064 }
1065 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001066 // These dependencies ensure the host installation rules will install the jar file and
1067 // the jni libraries when the wrapper is installed.
1068 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1069 ctx.AddVariationDependencies(
1070 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1071 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001072 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001073}
1074
Colin Cross1b16b0e2019-02-12 14:41:32 -08001075// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1076// as well.
1077//
1078// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1079// compiled against the device bootclasspath.
1080//
1081// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1082// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001083func BinaryFactory() android.Module {
1084 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001085
Colin Crossce6734e2020-06-15 16:09:53 -07001086 module.addHostAndDeviceProperties()
1087 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001088
Colin Cross9ae1b922018-06-26 17:59:05 -07001089 module.Module.properties.Installable = proptools.BoolPtr(true)
1090
Colin Cross6b4a32d2017-12-05 13:42:45 -08001091 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1092 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001093 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001094}
1095
Colin Cross1b16b0e2019-02-12 14:41:32 -08001096// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1097//
1098// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1099// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001100func BinaryHostFactory() android.Module {
1101 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001102
Colin Crossce6734e2020-06-15 16:09:53 -07001103 module.addHostProperties()
1104 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001105
Colin Cross9ae1b922018-06-26 17:59:05 -07001106 module.Module.properties.Installable = proptools.BoolPtr(true)
1107
Colin Cross6b4a32d2017-12-05 13:42:45 -08001108 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1109 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001110 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001111}
1112
1113//
1114// Java prebuilts
1115//
1116
Colin Cross74d73e22017-08-02 11:05:49 -07001117type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001118 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001119
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001120 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1121 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001122 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001123
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001124 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1125 // specified.
1126 Min_sdk_version *string
1127
Colin Cross535e2cf2017-10-20 17:57:49 -07001128 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001129
1130 // List of shared java libs that this module has dependencies to
1131 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001132
1133 // List of files to remove from the jar file(s)
1134 Exclude_files []string
1135
1136 // List of directories to remove from the jar file(s)
1137 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001138
1139 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001140 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001141
1142 // set the name of the output
1143 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001144
1145 Aidl struct {
1146 // directories that should be added as include directories for any aidl sources of modules
1147 // that depend on this module, as well as to aidl for this module.
1148 Export_include_dirs []string
1149 }
Colin Cross74d73e22017-08-02 11:05:49 -07001150}
1151
1152type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001153 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001154 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001155 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001156 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001157 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001158
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001159 // Functionality common to Module and Import.
1160 embeddableInModuleAndImport
1161
Liz Kammerd6c31d22020-08-05 15:40:41 -07001162 hiddenAPI
1163 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001164 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001165
Colin Cross74d73e22017-08-02 11:05:49 -07001166 properties ImportProperties
1167
Liz Kammerd6c31d22020-08-05 15:40:41 -07001168 // output file containing classes.dex and resources
1169 dexJarFile android.Path
1170
Colin Cross0a6e0072017-08-30 14:24:55 -07001171 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001172 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001173 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001174
1175 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001176
1177 sdkVersion android.SdkSpec
1178 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001179}
1180
Jiyong Park92315372021-04-02 08:45:46 +09001181func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1182 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001183}
1184
Jiyong Parkf1691d22021-03-29 20:11:58 +09001185func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001186 return "none"
1187}
1188
Jiyong Park92315372021-04-02 08:45:46 +09001189func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001190 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001191 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001192 }
Jiyong Park92315372021-04-02 08:45:46 +09001193 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001194}
1195
Jiyong Park92315372021-04-02 08:45:46 +09001196func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1197 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001198}
1199
Colin Cross74d73e22017-08-02 11:05:49 -07001200func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001201 return &j.prebuilt
1202}
1203
Colin Cross74d73e22017-08-02 11:05:49 -07001204func (j *Import) PrebuiltSrcs() []string {
1205 return j.properties.Jars
1206}
1207
1208func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001209 return j.prebuilt.Name(j.ModuleBase.Name())
1210}
1211
Jiyong Park0b238752019-10-29 11:23:10 +09001212func (j *Import) Stem() string {
1213 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1214}
1215
Jiyong Park618922e2020-01-08 13:35:43 +09001216func (a *Import) JacocoReportClassesFile() android.Path {
1217 return nil
1218}
1219
Bill Peckhama41a6962021-01-11 10:58:54 -08001220func (j *Import) LintDepSets() LintDepSets {
1221 return LintDepSets{}
1222}
1223
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001224func (j *Import) getStrictUpdatabilityLinting() bool {
1225 return false
1226}
1227
1228func (j *Import) setStrictUpdatabilityLinting(bool) {
1229}
1230
Colin Cross74d73e22017-08-02 11:05:49 -07001231func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001232 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001233
1234 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001235 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001236 }
Colin Cross1e676be2016-10-12 14:38:15 -07001237}
1238
Colin Cross74d73e22017-08-02 11:05:49 -07001239func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001240 j.sdkVersion = j.SdkVersion(ctx)
1241 j.minSdkVersion = j.MinSdkVersion(ctx)
1242
Colin Cross56a83212020-09-15 18:30:11 -07001243 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1244 j.hideApexVariantFromMake = true
1245 }
1246
Colin Cross8a497952019-03-05 22:25:09 -08001247 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001248
Jiyong Park0b238752019-10-29 11:23:10 +09001249 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001250 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001251 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1252 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001253 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001254 inputFile := outputFile
1255 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1256 TransformJetifier(ctx, outputFile, inputFile)
1257 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001258 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001259 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001260
Liz Kammerd6c31d22020-08-05 15:40:41 -07001261 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001262 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001263
Jiyong Park1be96912018-05-28 18:02:19 +09001264 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001265 tag := ctx.OtherModuleDependencyTag(module)
1266
Colin Crossdcf71b22021-02-01 13:59:03 -08001267 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1268 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001269 switch tag {
1270 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001271 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001272 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001273 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001274 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001275 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001276 switch tag {
1277 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001278 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001279 }
1280 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001281
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001282 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001283
1284 // Save away the `deapexer` module on which this depends, if any.
1285 if tag == android.DeapexerTag {
1286 deapexerModule = module
1287 }
Jiyong Park1be96912018-05-28 18:02:19 +09001288 })
1289
Nan Zhang4973ecf2018-08-10 13:42:12 -07001290 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001291 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001292 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001293 }
Jiyong Park19604de2020-03-24 16:44:11 +09001294
1295 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001296
Paul Duffin064b70c2020-11-02 17:32:38 +00001297 if ctx.Device() {
1298 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1299 // obtained from the associated deapexer module.
1300 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1301 if ai.ForPrebuiltApex {
1302 if deapexerModule == nil {
1303 // This should never happen as a variant for a prebuilt_apex is only created if the
Paul Duffinb17d0442021-05-05 12:07:00 +01001304 // deapexer module has been configured to export the dex implementation jar for this module.
Paul Duffin064b70c2020-11-02 17:32:38 +00001305 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1306 j.Name(), ai.ApexVariationName)
Paul Duffinb17d0442021-05-05 12:07:00 +01001307 return
Paul Duffin064b70c2020-11-02 17:32:38 +00001308 }
1309
1310 // Get the path of the dex implementation jar from the `deapexer` module.
1311 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001312 if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
1313 j.dexJarFile = dexOutputPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001314
1315 // Initialize the hiddenapi structure.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001316 j.initHiddenAPI(ctx, dexOutputPath, outputFile, nil)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001317 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001318 // This should never happen as a variant for a prebuilt_apex is only created if the
1319 // prebuilt_apex has been configured to export the java library dex file.
1320 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1321 }
1322 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001323 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001324 if sdkDep.invalidVersion {
1325 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1326 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1327 } else if sdkDep.useFiles {
1328 // sdkDep.jar is actually equivalent to turbine header.jar.
1329 flags.classpath = append(flags.classpath, sdkDep.jars...)
1330 }
1331
1332 // Dex compilation
1333
1334 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1335 if j.dexProperties.Uncompress_dex == nil {
1336 // If the value was not force-set by the user, use reasonable default based on the module.
1337 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1338 }
1339 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1340
Paul Duffin612e6102021-02-02 13:38:13 +00001341 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001342 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001343 if ctx.Failed() {
1344 return
1345 }
1346
Paul Duffin74d18d12021-05-14 14:18:47 +01001347 // Initialize the hiddenapi structure.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001348 j.initHiddenAPI(ctx, dexOutputFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001349
1350 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001351 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001352
1353 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001354 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001355 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001356
1357 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1358 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1359 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1360 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1361 AidlIncludeDirs: j.exportAidlIncludeDirs,
1362 })
Colin Cross2fe66872015-03-30 17:20:39 -07001363}
1364
Paul Duffinaa55f742020-10-06 17:20:13 +01001365func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1366 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001367 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001368 return android.Paths{j.combinedClasspathFile}, nil
1369 default:
1370 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1371 }
1372}
1373
1374var _ android.OutputFileProducer = (*Import)(nil)
1375
Nan Zhanged19fc32017-10-19 13:06:22 -07001376func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001377 if j.combinedClasspathFile == nil {
1378 return nil
1379 }
Colin Cross37f6d792018-07-12 12:28:41 -07001380 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001381}
1382
Colin Cross331a1212018-08-15 20:40:52 -07001383func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001384 if j.combinedClasspathFile == nil {
1385 return nil
1386 }
Colin Cross331a1212018-08-15 20:40:52 -07001387 return android.Paths{j.combinedClasspathFile}
1388}
1389
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001390func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001391 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001392}
1393
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001394func (j *Import) DexJarInstallPath() android.Path {
1395 return nil
1396}
1397
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001398func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1399 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001400}
1401
Jiyong Park45bf82e2020-12-15 22:29:02 +09001402var _ android.ApexModule = (*Import)(nil)
1403
1404// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001405func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001406 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001407}
1408
Jiyong Park45bf82e2020-12-15 22:29:02 +09001409// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001410func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1411 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001412 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001413 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001414 return fmt.Errorf("min_sdk_version is not specified")
1415 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001416 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001417 return nil
1418 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001419 ver, err := sdkSpec.EffectiveVersion(ctx)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001420 if err != nil {
1421 return err
1422 }
Jiyong Park54105c42021-03-31 18:17:53 +09001423 if ver.GreaterThan(sdkVersion) {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001424 return fmt.Errorf("newer SDK(%v)", ver)
1425 }
Jooyung Han749dc692020-04-15 11:03:39 +09001426 return nil
1427}
1428
albaltai36ff7dc2018-12-25 14:35:23 +08001429// Add compile time check for interface implementation
1430var _ android.IDEInfo = (*Import)(nil)
1431var _ android.IDECustomizedModuleName = (*Import)(nil)
1432
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001433// Collect information for opening IDE project files in java/jdeps.go.
1434const (
1435 removedPrefix = "prebuilt_"
1436)
1437
1438func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1439 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1440}
1441
1442func (j *Import) IDECustomizedModuleName() string {
1443 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1444 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1445 // solution to get the Import name.
1446 name := j.Name()
1447 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001448 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001449 }
1450 return name
1451}
1452
Colin Cross74d73e22017-08-02 11:05:49 -07001453var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001454
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001455func (j *Import) IsInstallable() bool {
1456 return Bool(j.properties.Installable)
1457}
1458
1459var _ dexpreopterInterface = (*Import)(nil)
1460
Colin Cross1b16b0e2019-02-12 14:41:32 -08001461// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1462//
1463// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1464// compiled against an Android classpath.
1465//
1466// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1467// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001468func ImportFactory() android.Module {
1469 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001470
Liz Kammerd6c31d22020-08-05 15:40:41 -07001471 module.AddProperties(
1472 &module.properties,
1473 &module.dexer.dexProperties,
1474 )
Colin Cross74d73e22017-08-02 11:05:49 -07001475
Paul Duffin859fe962020-05-15 10:20:31 +01001476 module.initModuleAndImport(&module.ModuleBase)
1477
Liz Kammerd6c31d22020-08-05 15:40:41 -07001478 module.dexProperties.Optimize.EnabledByDefault = false
1479
Colin Cross74d73e22017-08-02 11:05:49 -07001480 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001481 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001482 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001483 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001484 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001485}
1486
Colin Cross1b16b0e2019-02-12 14:41:32 -08001487// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1488// module.
1489//
1490// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1491// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001492func ImportFactoryHost() android.Module {
1493 module := &Import{}
1494
1495 module.AddProperties(&module.properties)
1496
1497 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001498 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001499 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001500 return module
1501}
1502
Colin Cross42be7612019-02-21 18:12:14 -08001503// dex_import module
1504
1505type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001506 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001507
1508 // set the name of the output
1509 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001510}
1511
1512type DexImport struct {
1513 android.ModuleBase
1514 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001515 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001516 prebuilt android.Prebuilt
1517
1518 properties DexImportProperties
1519
Colin Crossb014f072021-02-26 14:54:36 -08001520 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001521
1522 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001523
1524 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001525}
1526
1527func (j *DexImport) Prebuilt() *android.Prebuilt {
1528 return &j.prebuilt
1529}
1530
1531func (j *DexImport) PrebuiltSrcs() []string {
1532 return j.properties.Jars
1533}
1534
1535func (j *DexImport) Name() string {
1536 return j.prebuilt.Name(j.ModuleBase.Name())
1537}
1538
Jiyong Park0b238752019-10-29 11:23:10 +09001539func (j *DexImport) Stem() string {
1540 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1541}
1542
Jiyong Park77acec62020-06-01 21:39:15 +09001543func (a *DexImport) JacocoReportClassesFile() android.Path {
1544 return nil
1545}
1546
Colin Cross08dca382020-07-21 20:31:17 -07001547func (a *DexImport) LintDepSets() LintDepSets {
1548 return LintDepSets{}
1549}
1550
Martin Stjernholm6d415272020-01-31 17:10:36 +00001551func (j *DexImport) IsInstallable() bool {
1552 return true
1553}
1554
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001555func (j *DexImport) getStrictUpdatabilityLinting() bool {
1556 return false
1557}
1558
1559func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1560}
1561
Colin Cross42be7612019-02-21 18:12:14 -08001562func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1563 if len(j.properties.Jars) != 1 {
1564 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1565 }
1566
Colin Cross56a83212020-09-15 18:30:11 -07001567 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1568 if !apexInfo.IsForPlatform() {
1569 j.hideApexVariantFromMake = true
1570 }
1571
Jiyong Park0b238752019-10-29 11:23:10 +09001572 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001573 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1574
1575 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1576 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1577
1578 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001579 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001580
1581 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1582 rule.Temporary(temporary)
1583
1584 // use zip2zip to uncompress classes*.dex files
1585 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001586 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001587 FlagWithInput("-i ", inputJar).
1588 FlagWithOutput("-o ", temporary).
1589 FlagWithArg("-0 ", "'classes*.dex'")
1590
1591 // use zipalign to align uncompressed classes*.dex files
1592 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001593 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001594 Flag("-f").
1595 Text("4").
1596 Input(temporary).
1597 Output(dexOutputFile)
1598
1599 rule.DeleteTemporaryFiles()
1600
Colin Crossf1a035e2020-11-16 17:32:30 -08001601 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001602 } else {
1603 ctx.Build(pctx, android.BuildParams{
1604 Rule: android.Cp,
1605 Input: inputJar,
1606 Output: dexOutputFile,
1607 })
1608 }
1609
1610 j.dexJarFile = dexOutputFile
1611
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001612 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001613
Colin Cross56a83212020-09-15 18:30:11 -07001614 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001615 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1616 j.Stem()+".jar", dexOutputFile)
1617 }
Colin Cross42be7612019-02-21 18:12:14 -08001618}
1619
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001620func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001621 return j.dexJarFile
1622}
1623
Jiyong Park45bf82e2020-12-15 22:29:02 +09001624var _ android.ApexModule = (*DexImport)(nil)
1625
1626// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001627func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1628 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001629 // we don't check prebuilt modules for sdk_version
1630 return nil
1631}
1632
Colin Cross42be7612019-02-21 18:12:14 -08001633// dex_import imports a `.jar` file containing classes.dex files.
1634//
1635// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1636// to the device.
1637func DexImportFactory() android.Module {
1638 module := &DexImport{}
1639
1640 module.AddProperties(&module.properties)
1641
1642 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001643 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001644 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001645 return module
1646}
1647
Colin Cross89536d42017-07-07 14:35:50 -07001648//
1649// Defaults
1650//
1651type Defaults struct {
1652 android.ModuleBase
1653 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001654 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001655}
1656
Colin Cross1b16b0e2019-02-12 14:41:32 -08001657// java_defaults provides a set of properties that can be inherited by other java or android modules.
1658//
1659// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1660// property in the defaults module that exists in the depending module will be prepended to the depending module's
1661// value for that property.
1662//
1663// Example:
1664//
1665// java_defaults {
1666// name: "example_defaults",
1667// srcs: ["common/**/*.java"],
1668// javacflags: ["-Xlint:all"],
1669// aaptflags: ["--auto-add-overlay"],
1670// }
1671//
1672// java_library {
1673// name: "example",
1674// defaults: ["example_defaults"],
1675// srcs: ["example/**/*.java"],
1676// }
1677//
1678// is functionally identical to:
1679//
1680// java_library {
1681// name: "example",
1682// srcs: [
1683// "common/**/*.java",
1684// "example/**/*.java",
1685// ],
1686// javacflags: ["-Xlint:all"],
1687// }
Paul Duffin47357662019-12-05 14:07:14 +00001688func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001689 module := &Defaults{}
1690
Colin Cross89536d42017-07-07 14:35:50 -07001691 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001692 &CommonProperties{},
1693 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001694 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001695 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001696 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001697 &aaptProperties{},
1698 &androidLibraryProperties{},
1699 &appProperties{},
1700 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001701 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001702 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001703 &ImportProperties{},
1704 &AARImportProperties{},
1705 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001706 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001707 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001708 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001709 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001710 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001711 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001712 )
1713
1714 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001715 return module
1716}
Nan Zhangea568a42017-11-08 21:20:04 -08001717
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001718func kytheExtractJavaFactory() android.Singleton {
1719 return &kytheExtractJavaSingleton{}
1720}
1721
1722type kytheExtractJavaSingleton struct {
1723}
1724
1725func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1726 var xrefTargets android.Paths
1727 ctx.VisitAllModules(func(module android.Module) {
1728 if javaModule, ok := module.(xref); ok {
1729 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1730 }
1731 })
1732 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1733 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001734 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001735 }
1736}
1737
Nan Zhangea568a42017-11-08 21:20:04 -08001738var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001739var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001740var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001741var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001742
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001743// Add class loader context (CLC) of a given dependency to the current CLC.
1744func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1745 clcMap dexpreopt.ClassLoaderContextMap) {
1746
1747 dep, ok := depModule.(UsesLibraryDependency)
1748 if !ok {
1749 return
1750 }
1751
1752 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1753 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1754 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1755 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1756 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1757 // the current CLC.
1758 var implicitSdkLib *string
1759 comp, isComp := depModule.(SdkLibraryComponentDependency)
1760 if isComp {
1761 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1762 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1763 }
1764 if implicitSdkLib == nil {
1765 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1766 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001767 }
1768 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001769
1770 depTag := ctx.OtherModuleDependencyTag(depModule)
1771 if depTag == libTag || depTag == usesLibTag {
1772 // Ok, propagate <uses-library> through non-static library dependencies.
1773 } else if depTag == staticLibTag {
1774 // Propagate <uses-library> through static library dependencies, unless it is a component
1775 // library (such as stubs). Component libraries have a dependency on their SDK library,
1776 // which should not be pulled just because of a static component library.
1777 if implicitSdkLib != nil {
1778 return
1779 }
1780 } else {
1781 // Don't propagate <uses-library> for other dependency tags.
1782 return
1783 }
1784
1785 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001786 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001787 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1788 } else {
1789 depName := ctx.OtherModuleName(depModule)
1790 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1791 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001792}