blob: adb0c5644c73abdd08cf7c8969eb93459341d8d0 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross2fe66872015-03-30 17:20:39 -070024 "strings"
25
26 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070027 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070028
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080030 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010031 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010037 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080039 RegisterJavaSdkMemberTypes()
40}
41
Paul Duffin535e0a12021-03-30 23:34:32 +010042func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080043 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
44
45 ctx.RegisterModuleType("java_library", LibraryFactory)
46 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
47 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
48 ctx.RegisterModuleType("java_binary", BinaryFactory)
49 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
50 ctx.RegisterModuleType("java_test", TestFactory)
51 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
52 ctx.RegisterModuleType("java_test_host", TestHostFactory)
53 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
54 ctx.RegisterModuleType("java_import", ImportFactory)
55 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
56 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
57 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
58 ctx.RegisterModuleType("dex_import", DexImportFactory)
59
60 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
61 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
62 })
63
64 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
65 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
66}
67
68func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000069 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000070 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000071
Paul Duffin22ff0aa2021-02-04 11:15:34 +000072 // Export implementation classes jar as part of the sdk.
73 exportImplementationClassesJar := func(_ android.SdkMemberContext, j *Library) android.Path {
74 implementationJars := j.ImplementationAndResourcesJars()
75 if len(implementationJars) != 1 {
76 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
77 }
78 return implementationJars[0]
79 }
80
Paul Duffindb170e42020-12-08 17:48:25 +000081 // Register java implementation libraries for use only in module_exports (not sdk).
Paul Duffinf5c0a9c2020-02-28 14:39:53 +000082 android.RegisterSdkMemberType(&librarySdkMemberType{
83 android.SdkMemberTypeBase{
84 PropertyName: "java_libs",
85 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +000086 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +000087 sdkSnapshotFilePathForJar,
88 copyEverythingToSnapshot,
Paul Duffin255f18e2019-12-13 11:22:16 +000089 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000090
Paul Duffindb170e42020-12-08 17:48:25 +000091 // Register java boot libraries for use in sdk.
92 //
93 // The build has some implicit dependencies (via the boot jars configuration) on a number of
94 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
95 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
96 // used outside those mainline modules.
97 //
98 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
99 // either java_libs, or java_header_libs would end up exporting more information than was strictly
100 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
101 // sdk/module_exports without exposing any unnecessary information.
102 android.RegisterSdkMemberType(&librarySdkMemberType{
103 android.SdkMemberTypeBase{
104 PropertyName: "java_boot_libs",
105 SupportsSdk: true,
106 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000107 // Temporarily export implementation classes jar for java_boot_libs as it is required for the
108 // hiddenapi processing.
109 // TODO(b/179354495): Revert once hiddenapi processing has been modularized.
110 exportImplementationClassesJar,
111 sdkSnapshotFilePathForJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000112 onlyCopyJarToSnapshot,
113 })
114
115 // Register java test libraries for use only in module_exports (not sdk).
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000116 android.RegisterSdkMemberType(&testSdkMemberType{
117 SdkMemberTypeBase: android.SdkMemberTypeBase{
118 PropertyName: "java_tests",
119 },
120 })
Colin Cross463a90e2015-06-17 14:20:06 -0700121
Jeongik Cha538c0d02019-07-11 15:54:27 +0900122}
123
Colin Crossdcf71b22021-02-01 13:59:03 -0800124// JavaInfo contains information about a java module for use by modules that depend on it.
125type JavaInfo struct {
126 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
127 // against this module. If empty, ImplementationJars should be used instead.
128 HeaderJars android.Paths
129
130 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
131 // in the module as well as any resources included in the module.
132 ImplementationAndResourcesJars android.Paths
133
134 // ImplementationJars is a list of jars that contain the implementations of classes in the
135 //module.
136 ImplementationJars android.Paths
137
138 // ResourceJars is a list of jars that contain the resources included in the module.
139 ResourceJars android.Paths
140
141 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
142 // depending on this module.
143 AidlIncludeDirs android.Paths
144
145 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
146 // module.
147 SrcJarArgs []string
148
149 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
150 SrcJarDeps android.Paths
151
152 // ExportedPlugins is a list of paths that should be used as annotation processors for any
153 // module that depends on this module.
154 ExportedPlugins android.Paths
155
156 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
157 // any module that depends on this module.
158 ExportedPluginClasses []string
159
160 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
161 // requiring disbling turbine for any modules that depend on it.
162 ExportedPluginDisableTurbine bool
163
164 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
165 // instrumented by jacoco.
166 JacocoReportClassesFile android.Path
167}
168
169var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
170
Colin Cross75ce9ec2021-02-26 16:20:32 -0800171// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
172// the sysprop implementation library.
173type SyspropPublicStubInfo struct {
174 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
175 // the sysprop implementation library.
176 JavaInfo JavaInfo
177}
178
179var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
180
Paul Duffin44b481b2020-06-17 16:59:43 +0100181// Methods that need to be implemented for a module that is added to apex java_libs property.
182type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700183 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100184 ImplementationAndResourcesJars() android.Paths
185}
186
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100187// Provides build path and install path to DEX jars.
188type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000189 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100190 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000191 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100192}
193
Jaewoong Jung26342642021-03-17 15:56:23 -0700194// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800195type xref interface {
196 XrefJavaFiles() android.Paths
197}
198
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800199func (j *Module) XrefJavaFiles() android.Paths {
200 return j.kytheFiles
201}
202
Colin Crossbe1da472017-07-07 15:59:46 -0700203type dependencyTag struct {
204 blueprint.BaseDependencyTag
205 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700206}
207
Colin Crosse9fe2942020-11-10 18:12:15 -0800208// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
209// dependency to be installed when the parent module is installed.
210type installDependencyTag struct {
211 blueprint.BaseDependencyTag
212 android.InstallAlwaysNeededDependencyTag
213 name string
214}
215
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100216type usesLibraryDependencyTag struct {
217 dependencyTag
218 sdkVersion int // SDK version in which the library appared as a standalone library.
219}
220
221func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
222 return usesLibraryDependencyTag{
223 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
224 sdkVersion: sdkVersion,
225 }
226}
227
Jiyong Park8be103b2019-11-08 15:53:48 +0900228func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700229 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900230}
231
Colin Crossbe1da472017-07-07 15:59:46 -0700232var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800233 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
234 staticLibTag = dependencyTag{name: "staticlib"}
235 libTag = dependencyTag{name: "javalib"}
236 java9LibTag = dependencyTag{name: "java9lib"}
237 pluginTag = dependencyTag{name: "plugin"}
238 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
239 exportedPluginTag = dependencyTag{name: "exported-plugin"}
240 bootClasspathTag = dependencyTag{name: "bootclasspath"}
241 systemModulesTag = dependencyTag{name: "system modules"}
242 frameworkResTag = dependencyTag{name: "framework-res"}
243 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
244 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
245 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
246 certificateTag = dependencyTag{name: "certificate"}
247 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
248 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
249 jniLibTag = dependencyTag{name: "jnilib"}
250 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
251 jniInstallTag = installDependencyTag{name: "jni install"}
252 binaryInstallTag = installDependencyTag{name: "binary install"}
253 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
254 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
255 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
256 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700257)
Colin Cross2fe66872015-03-30 17:20:39 -0700258
Jiyong Park83dc74b2020-01-14 18:38:44 +0900259func IsLibDepTag(depTag blueprint.DependencyTag) bool {
260 return depTag == libTag
261}
262
263func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
264 return depTag == staticLibTag
265}
266
Colin Crossfc3674a2017-09-18 17:41:52 -0700267type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100268 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700269
Colin Cross6cef4812019-10-17 14:23:50 -0700270 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
271 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100272
273 // The default system modules to use. Will be an empty string if no system
274 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700275 systemModules string
276
Pete Gilline3d44b22020-06-29 11:28:51 +0100277 // The modules that will be added to the classpath regardless of the Java language level targeted
278 classpath []string
279
Colin Cross6cef4812019-10-17 14:23:50 -0700280 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100281 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700282 java9Classpath []string
283
Colin Crossa97c5d32018-03-28 14:58:31 -0700284 frameworkResModule string
285
Colin Cross86a60ae2018-05-29 14:44:55 -0700286 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700287 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100288
289 noStandardLibs, noFrameworksLibs bool
290}
291
292func (s sdkDep) hasStandardLibs() bool {
293 return !s.noStandardLibs
294}
295
296func (s sdkDep) hasFrameworkLibs() bool {
297 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700298}
299
Colin Crossa4f08812018-10-02 22:03:40 -0700300type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700301 name string
302 path android.Path
303 target android.Target
304 coverageFile android.OptionalPath
305 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700306}
307
Jiyong Parkf1691d22021-03-29 20:11:58 +0900308func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700309 sdkDep := decodeSdkDep(ctx, sdkContext)
310 if sdkDep.useModule {
311 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
312 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
313 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
314 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
315 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
316 }
317 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
318 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
319 }
320 }
321 if sdkDep.systemModules != "" {
322 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
323 }
324}
325
Colin Cross32f676a2017-09-06 13:41:06 -0700326type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800327 classpath classpath
328 java9Classpath classpath
329 bootClasspath classpath
330 processorPath classpath
331 errorProneProcessorPath classpath
332 processorClasses []string
333 staticJars android.Paths
334 staticHeaderJars android.Paths
335 staticResourceJars android.Paths
336 aidlIncludeDirs android.Paths
337 srcs android.Paths
338 srcJars android.Paths
339 systemModules *systemModules
340 aidlPreprocess android.OptionalPath
341 kotlinStdlib android.Paths
342 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800343
344 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700345}
Colin Cross2fe66872015-03-30 17:20:39 -0700346
Colin Cross54250902017-12-05 09:28:08 -0800347func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
348 for _, f := range dep.Srcs() {
349 if f.Ext() != ".jar" {
350 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
351 ctx.OtherModuleName(dep.(blueprint.Module)))
352 }
353 }
354}
355
Jiyong Parkf1691d22021-03-29 20:11:58 +0900356func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700357 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700358 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700359 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900360 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700361 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700362 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700363 }
Nan Zhang357466b2018-04-17 17:38:36 -0700364}
365
Colin Cross1e743852019-10-28 11:37:20 -0700366type javaVersion int
367
368const (
369 JAVA_VERSION_UNSUPPORTED = 0
370 JAVA_VERSION_6 = 6
371 JAVA_VERSION_7 = 7
372 JAVA_VERSION_8 = 8
373 JAVA_VERSION_9 = 9
374)
375
376func (v javaVersion) String() string {
377 switch v {
378 case JAVA_VERSION_6:
379 return "1.6"
380 case JAVA_VERSION_7:
381 return "1.7"
382 case JAVA_VERSION_8:
383 return "1.8"
384 case JAVA_VERSION_9:
385 return "1.9"
386 default:
387 return "unsupported"
388 }
389}
390
391// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
392func (v javaVersion) usesJavaModules() bool {
393 return v >= 9
394}
395
396func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100397 switch javaVersion {
398 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700399 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100400 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700401 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100402 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700403 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100404 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700405 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100406 case "10", "11":
407 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700408 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100409 default:
410 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700411 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100412 }
413}
414
Colin Cross2fe66872015-03-30 17:20:39 -0700415//
416// Java libraries (.jar file)
417//
418
Colin Crossf506d872017-07-19 15:53:04 -0700419type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700420 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700421
422 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700423}
424
Jiyong Park45bf82e2020-12-15 22:29:02 +0900425var _ android.ApexModule = (*Library)(nil)
426
Paul Duffine739f1e2020-05-29 11:24:51 +0100427// Provides access to the list of permitted packages from updatable boot jars.
428type PermittedPackagesForUpdatableBootJars interface {
429 PermittedPackagesForUpdatableBootJars() []string
430}
431
432var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
433
434func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
435 return j.properties.Permitted_packages
436}
437
Colin Cross42be7612019-02-21 18:12:14 -0800438func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000439 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700440 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000441 return true
442 }
443
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000444 // Store uncompressed (and do not strip) dex files from boot class path jars.
445 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
446 return true
447 }
448
449 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -0800450 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000451 return true
452 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800453 if ctx.Config().UncompressPrivAppDex() &&
454 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
455 return true
456 }
457
Colin Cross2fc72f62018-12-21 12:59:54 -0800458 return false
459}
460
Colin Crossf506d872017-07-19 15:53:04 -0700461func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin4103e922021-02-01 19:01:34 +0000462 // Initialize the hiddenapi structure. Pass in the configuration name rather than the module name
463 // so the hidden api will encode the <x>.impl java_ library created by java_sdk_library just as it
464 // would the <x> library if <x> was configured as a boot jar.
465 j.initHiddenAPI(ctx, j.ConfigurationName())
466
Jiyong Park92315372021-04-02 08:45:46 +0900467 j.sdkVersion = j.SdkVersion(ctx)
468 j.minSdkVersion = j.MinSdkVersion(ctx)
469
Colin Cross56a83212020-09-15 18:30:11 -0700470 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
471 if !apexInfo.IsForPlatform() {
472 j.hideApexVariantFromMake = true
473 }
474
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100475 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900476 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800477 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700478 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100479 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700480 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100481 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700482 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100483 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700484 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700485
bralee1fbf4402020-05-21 10:11:59 +0800486 // Collect the module directory for IDE info in java/jdeps.go.
487 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
488
Colin Cross56a83212020-09-15 18:30:11 -0700489 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900490 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700491 var extraInstallDeps android.Paths
492 if j.InstallMixin != nil {
493 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
494 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700495 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900496 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700497 }
Colin Crossb7a63242015-04-16 14:09:14 -0700498}
499
Colin Crossf506d872017-07-19 15:53:04 -0700500func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700501 j.deps(ctx)
502}
503
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000504const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000505 aidlIncludeDir = "aidl"
506 javaDir = "java"
507 jarFileSuffix = ".jar"
508 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000509)
510
Paul Duffina0dbf432019-12-05 11:25:53 +0000511// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000512func sdkSnapshotFilePathForJar(osPrefix, name string) string {
513 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000514}
515
Paul Duffina04c1072020-03-02 10:16:35 +0000516func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
517 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000518}
519
Paul Duffin13879572019-11-28 14:31:38 +0000520type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000521 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000522
523 // Function to retrieve the appropriate output jar (implementation or header) from
524 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000525 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
526
527 // Function to compute the snapshot relative path to which the named library's
528 // jar should be copied.
529 snapshotPathGetter func(osPrefix, name string) string
530
531 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
532 // files like aidl files should also be copied.
533 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000534}
535
Paul Duffindb170e42020-12-08 17:48:25 +0000536const (
537 onlyCopyJarToSnapshot = true
538 copyEverythingToSnapshot = false
539)
540
Paul Duffin13879572019-11-28 14:31:38 +0000541func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
542 mctx.AddVariationDependencies(nil, dependencyTag, names...)
543}
544
545func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
546 _, ok := module.(*Library)
547 return ok
548}
549
Paul Duffin3a4eb502020-03-19 16:11:18 +0000550func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
551 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000552}
Paul Duffina0dbf432019-12-05 11:25:53 +0000553
Paul Duffin14eb4672020-03-02 11:33:02 +0000554func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000555 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000556}
557
558type librarySdkMemberProperties struct {
559 android.SdkMemberPropertiesBase
560
Paul Duffin864e1b42020-05-06 10:23:19 +0100561 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000562 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +0000563}
564
Paul Duffin3a4eb502020-03-19 16:11:18 +0000565func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000566 j := variant.(*Library)
567
Paul Duffindb170e42020-12-08 17:48:25 +0000568 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
569
Paul Duffina551a1c2020-03-17 21:04:24 +0000570 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +0000571}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000572
Paul Duffin3a4eb502020-03-19 16:11:18 +0000573func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000574 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000575
Paul Duffindb170e42020-12-08 17:48:25 +0000576 memberType := ctx.MemberType().(*librarySdkMemberType)
577
Paul Duffina551a1c2020-03-17 21:04:24 +0000578 exportedJar := p.JarToExport
579 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000580 // Delegate the creation of the snapshot relative path to the member type.
581 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
582
583 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000584 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
585
Paul Duffina551a1c2020-03-17 21:04:24 +0000586 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
587 }
588
Paul Duffindb170e42020-12-08 17:48:25 +0000589 // Do not copy anything else to the snapshot.
590 if memberType.onlyCopyJarToSnapshot {
591 return
592 }
593
Paul Duffina551a1c2020-03-17 21:04:24 +0000594 aidlIncludeDirs := p.AidlIncludeDirs
595 if len(aidlIncludeDirs) != 0 {
596 sdkModuleContext := ctx.SdkModuleContext()
597 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000598 // TODO(jiyong): copy parcelable declarations only
599 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
600 for _, file := range aidlFiles {
601 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
602 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000603 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000604
Paul Duffina551a1c2020-03-17 21:04:24 +0000605 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000606 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000607}
608
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000609var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
610 android.SdkMemberTypeBase{
611 PropertyName: "java_header_libs",
612 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +0000613 },
Paul Duffindb170e42020-12-08 17:48:25 +0000614 func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +0000615 headerJars := j.HeaderJars()
616 if len(headerJars) != 1 {
617 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
618 }
619
620 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000621 },
Paul Duffindb170e42020-12-08 17:48:25 +0000622 sdkSnapshotFilePathForJar,
623 copyEverythingToSnapshot,
Paul Duffina0dbf432019-12-05 11:25:53 +0000624}
625
Colin Cross1b16b0e2019-02-12 14:41:32 -0800626// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
627//
628// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
629// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
630// as a `static_libs` dependency of another module.
631//
632// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
633// a device.
634//
635// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
636// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700637func LibraryFactory() android.Module {
638 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700639
Colin Crossce6734e2020-06-15 16:09:53 -0700640 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700641
Paul Duffin859fe962020-05-15 10:20:31 +0100642 module.initModuleAndImport(&module.ModuleBase)
643
Jiyong Park7f7766d2019-07-25 22:02:35 +0900644 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900645 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900646 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700647 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700648}
649
Colin Cross1b16b0e2019-02-12 14:41:32 -0800650// java_library_static is an obsolete alias for java_library.
651func LibraryStaticFactory() android.Module {
652 return LibraryFactory()
653}
654
655// java_library_host builds and links sources into a `.jar` file for the host.
656//
657// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
658// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700659func LibraryHostFactory() android.Module {
660 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700661
Colin Crossce6734e2020-06-15 16:09:53 -0700662 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700663
Colin Cross9ae1b922018-06-26 17:59:05 -0700664 module.Module.properties.Installable = proptools.BoolPtr(true)
665
Jiyong Park7f7766d2019-07-25 22:02:35 +0900666 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900667 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700668 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700669}
670
671//
Colin Crossb628ea52018-08-14 16:42:33 -0700672// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700673//
674
Dan Shi95d19422020-08-15 12:24:26 -0700675// Test option struct.
676type TestOptions struct {
677 // a list of extra test configuration files that should be installed with the module.
678 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800679
680 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
681 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700682}
683
Colin Cross05638fc2018-04-09 18:40:24 -0700684type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700685 // list of compatibility suites (for example "cts", "vts") that the module should be
686 // installed into.
687 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700688
689 // the name of the test configuration (for example "AndroidTest.xml") that should be
690 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800691 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700692
Jack He33338892018-09-19 02:21:28 -0700693 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
694 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800695 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700696
Colin Crossd96ca352018-08-10 16:06:24 -0700697 // list of files or filegroup modules that provide data that should be installed alongside
698 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900699 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700700
701 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
702 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
703 // explicitly.
704 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800705
706 // Add parameterized mainline modules to auto generated test config. The options will be
707 // handled by TradeFed to do downloading and installing the specified modules on the device.
708 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700709
710 // Test options.
711 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800712
713 // Names of modules containing JNI libraries that should be installed alongside the test.
714 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700715}
716
Liz Kammerdd849a82020-06-12 16:38:45 -0700717type hostTestProperties struct {
718 // list of native binary modules that should be installed alongside the test
719 Data_native_bins []string `android:"arch_variant"`
720}
721
Paul Duffin42df1442019-03-20 12:45:53 +0000722type testHelperLibraryProperties struct {
723 // list of compatibility suites (for example "cts", "vts") that the module should be
724 // installed into.
725 Test_suites []string `android:"arch_variant"`
726}
727
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000728type prebuiltTestProperties struct {
729 // list of compatibility suites (for example "cts", "vts") that the module should be
730 // installed into.
731 Test_suites []string `android:"arch_variant"`
732
733 // the name of the test configuration (for example "AndroidTest.xml") that should be
734 // installed with the module.
735 Test_config *string `android:"path,arch_variant"`
736}
737
Colin Cross05638fc2018-04-09 18:40:24 -0700738type Test struct {
739 Library
740
741 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700742
Dan Shi95d19422020-08-15 12:24:26 -0700743 testConfig android.Path
744 extraTestConfigs android.Paths
745 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700746}
747
Liz Kammerdd849a82020-06-12 16:38:45 -0700748type TestHost struct {
749 Test
750
751 testHostProperties hostTestProperties
752}
753
Paul Duffin42df1442019-03-20 12:45:53 +0000754type TestHelperLibrary struct {
755 Library
756
757 testHelperLibraryProperties testHelperLibraryProperties
758}
759
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000760type JavaTestImport struct {
761 Import
762
763 prebuiltTestProperties prebuiltTestProperties
764
765 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700766 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000767}
768
Liz Kammerdd849a82020-06-12 16:38:45 -0700769func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
770 if len(j.testHostProperties.Data_native_bins) > 0 {
771 for _, target := range ctx.MultiTargets() {
772 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
773 }
774 }
775
Colin Crossf8d9c492021-01-26 11:01:43 -0800776 if len(j.testProperties.Jni_libs) > 0 {
777 for _, target := range ctx.MultiTargets() {
778 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
779 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
780 }
781 }
782
Liz Kammerdd849a82020-06-12 16:38:45 -0700783 j.deps(ctx)
784}
785
Yuexi Ma627263f2021-03-04 13:47:56 -0800786func (j *TestHost) AddExtraResource(p android.Path) {
787 j.extraResources = append(j.extraResources, p)
788}
789
Colin Cross303e21f2018-08-07 16:49:25 -0700790func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000791 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
792 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700793 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000794 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
795 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700796 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800797 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700798
Colin Cross8a497952019-03-05 22:25:09 -0800799 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700800
Dan Shi95d19422020-08-15 12:24:26 -0700801 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
802
Liz Kammerdd849a82020-06-12 16:38:45 -0700803 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
804 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
805 })
806
Colin Crossf8d9c492021-01-26 11:01:43 -0800807 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
808 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
809 if sharedLibInfo.SharedLibrary != nil {
810 // Copy to an intermediate output directory to append "lib[64]" to the path,
811 // so that it's compatible with the default rpath values.
812 var relPath string
813 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
814 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
815 } else {
816 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
817 }
818 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
819 ctx.Build(pctx, android.BuildParams{
820 Rule: android.Cp,
821 Input: sharedLibInfo.SharedLibrary,
822 Output: relocatedLib,
823 })
824 j.data = append(j.data, relocatedLib)
825 } else {
826 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
827 }
828 })
829
Colin Cross303e21f2018-08-07 16:49:25 -0700830 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700831}
832
Paul Duffin42df1442019-03-20 12:45:53 +0000833func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
834 j.Library.GenerateAndroidBuildActions(ctx)
835}
836
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000837func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
838 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800839 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000840
841 j.Import.GenerateAndroidBuildActions(ctx)
842}
843
844type testSdkMemberType struct {
845 android.SdkMemberTypeBase
846}
847
848func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
849 mctx.AddVariationDependencies(nil, dependencyTag, names...)
850}
851
852func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
853 _, ok := module.(*Test)
854 return ok
855}
856
Paul Duffin3a4eb502020-03-19 16:11:18 +0000857func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
858 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000859}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000860
Paul Duffin14eb4672020-03-02 11:33:02 +0000861func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
862 return &testSdkMemberProperties{}
863}
864
865type testSdkMemberProperties struct {
866 android.SdkMemberPropertiesBase
867
Paul Duffina551a1c2020-03-17 21:04:24 +0000868 JarToExport android.Path
869 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000870}
871
Paul Duffin3a4eb502020-03-19 16:11:18 +0000872func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000873 test := variant.(*Test)
874
875 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000876 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000877 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000878 }
879
Paul Duffina551a1c2020-03-17 21:04:24 +0000880 p.JarToExport = implementationJars[0]
881 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000882}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000883
Paul Duffin3a4eb502020-03-19 16:11:18 +0000884func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000885 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000886
Paul Duffina551a1c2020-03-17 21:04:24 +0000887 exportedJar := p.JarToExport
888 if exportedJar != nil {
889 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
890 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000891
892 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000893 }
894
895 testConfig := p.TestConfig
896 if testConfig != nil {
897 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
898 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000899 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
900 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000901}
902
Colin Cross1b16b0e2019-02-12 14:41:32 -0800903// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
904// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
905//
906// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
907// compiled against the device bootclasspath.
908//
909// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
910// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700911func TestFactory() android.Module {
912 module := &Test{}
913
Colin Crossce6734e2020-06-15 16:09:53 -0700914 module.addHostAndDeviceProperties()
915 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700916
Colin Cross9ae1b922018-06-26 17:59:05 -0700917 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800918 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700919 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700920
Colin Cross05638fc2018-04-09 18:40:24 -0700921 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700922 return module
923}
924
Paul Duffin42df1442019-03-20 12:45:53 +0000925// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
926func TestHelperLibraryFactory() android.Module {
927 module := &TestHelperLibrary{}
928
Colin Crossce6734e2020-06-15 16:09:53 -0700929 module.addHostAndDeviceProperties()
930 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000931
Colin Cross9a4abed2019-04-24 13:19:28 -0700932 module.Module.properties.Installable = proptools.BoolPtr(true)
933 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700934 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700935
Paul Duffin42df1442019-03-20 12:45:53 +0000936 InitJavaModule(module, android.HostAndDeviceSupported)
937 return module
938}
939
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000940// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
941// and makes sure that it is added to the appropriate test suite.
942//
943// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
944// compiled against an Android classpath.
945//
946// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
947// for host modules.
948func JavaTestImportFactory() android.Module {
949 module := &JavaTestImport{}
950
951 module.AddProperties(
952 &module.Import.properties,
953 &module.prebuiltTestProperties)
954
955 module.Import.properties.Installable = proptools.BoolPtr(true)
956
957 android.InitPrebuiltModule(module, &module.properties.Jars)
958 android.InitApexModule(module)
959 android.InitSdkAwareModule(module)
960 InitJavaModule(module, android.HostAndDeviceSupported)
961 return module
962}
963
Colin Cross1b16b0e2019-02-12 14:41:32 -0800964// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
965// allow running the test with `atest` or a `TEST_MAPPING` file.
966//
967// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
968// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700969func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700970 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700971
Colin Crossce6734e2020-06-15 16:09:53 -0700972 module.addHostProperties()
973 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700974 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700975
Yuexi Ma627263f2021-03-04 13:47:56 -0800976 InitTestHost(
977 module,
978 proptools.BoolPtr(true),
979 nil,
980 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -0700981
Liz Kammerdd849a82020-06-12 16:38:45 -0700982 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +0000983
Colin Cross05638fc2018-04-09 18:40:24 -0700984 return module
985}
986
Yuexi Ma627263f2021-03-04 13:47:56 -0800987func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
988 th.properties.Installable = installable
989 th.testProperties.Auto_gen_config = autoGenConfig
990 th.testProperties.Test_suites = testSuites
991}
992
Colin Cross05638fc2018-04-09 18:40:24 -0700993//
Colin Cross2fe66872015-03-30 17:20:39 -0700994// Java Binaries (.jar file plus wrapper script)
995//
996
Colin Crossf506d872017-07-19 15:53:04 -0700997type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700998 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800999 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001000
1001 // Name of the class containing main to be inserted into the manifest as Main-Class.
1002 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001003
1004 // Names of modules containing JNI libraries that should be installed alongside the host
1005 // variant of the binary.
1006 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -07001007}
1008
Colin Crossf506d872017-07-19 15:53:04 -07001009type Binary struct {
1010 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001011
Colin Crossf506d872017-07-19 15:53:04 -07001012 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001013
Colin Cross6b4a32d2017-12-05 13:42:45 -08001014 isWrapperVariant bool
1015
Colin Crossc3315992017-12-08 19:12:36 -08001016 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001017 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001018}
1019
Alex Light24237172017-10-26 09:46:21 -07001020func (j *Binary) HostToolPath() android.OptionalPath {
1021 return android.OptionalPathForPath(j.binaryFile)
1022}
1023
Colin Crossf506d872017-07-19 15:53:04 -07001024func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001025 if ctx.Arch().ArchType == android.Common {
1026 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001027 if j.binaryProperties.Main_class != nil {
1028 if j.properties.Manifest != nil {
1029 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1030 }
1031 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1032 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1033 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1034 }
1035
Colin Cross6b4a32d2017-12-05 13:42:45 -08001036 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001037 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001038 // Handle the binary wrapper
1039 j.isWrapperVariant = true
1040
Colin Cross366938f2017-12-11 16:29:02 -08001041 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001042 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001043 } else {
1044 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1045 }
1046
Colin Crossc179ea62020-10-09 10:54:15 -07001047 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001048 // of the wrapper variant, which will include the common variant's jar file and any JNI
1049 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001050 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001051 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001052 }
Colin Cross2fe66872015-03-30 17:20:39 -07001053}
1054
Colin Crossf506d872017-07-19 15:53:04 -07001055func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001056 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001057 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001058 }
1059 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001060 // These dependencies ensure the host installation rules will install the jar file and
1061 // the jni libraries when the wrapper is installed.
1062 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1063 ctx.AddVariationDependencies(
1064 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1065 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001066 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001067}
1068
Colin Cross1b16b0e2019-02-12 14:41:32 -08001069// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1070// as well.
1071//
1072// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1073// compiled against the device bootclasspath.
1074//
1075// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1076// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001077func BinaryFactory() android.Module {
1078 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001079
Colin Crossce6734e2020-06-15 16:09:53 -07001080 module.addHostAndDeviceProperties()
1081 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001082
Colin Cross9ae1b922018-06-26 17:59:05 -07001083 module.Module.properties.Installable = proptools.BoolPtr(true)
1084
Colin Cross6b4a32d2017-12-05 13:42:45 -08001085 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1086 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001087 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001088}
1089
Colin Cross1b16b0e2019-02-12 14:41:32 -08001090// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1091//
1092// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1093// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001094func BinaryHostFactory() android.Module {
1095 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001096
Colin Crossce6734e2020-06-15 16:09:53 -07001097 module.addHostProperties()
1098 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001099
Colin Cross9ae1b922018-06-26 17:59:05 -07001100 module.Module.properties.Installable = proptools.BoolPtr(true)
1101
Colin Cross6b4a32d2017-12-05 13:42:45 -08001102 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1103 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001104 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001105}
1106
1107//
1108// Java prebuilts
1109//
1110
Colin Cross74d73e22017-08-02 11:05:49 -07001111type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001112 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001113
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001114 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1115 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001116 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001117
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001118 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1119 // specified.
1120 Min_sdk_version *string
1121
Colin Cross535e2cf2017-10-20 17:57:49 -07001122 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001123
1124 // List of shared java libs that this module has dependencies to
1125 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001126
1127 // List of files to remove from the jar file(s)
1128 Exclude_files []string
1129
1130 // List of directories to remove from the jar file(s)
1131 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001132
1133 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001134 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001135
1136 // set the name of the output
1137 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001138
1139 Aidl struct {
1140 // directories that should be added as include directories for any aidl sources of modules
1141 // that depend on this module, as well as to aidl for this module.
1142 Export_include_dirs []string
1143 }
Colin Cross74d73e22017-08-02 11:05:49 -07001144}
1145
1146type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001147 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001148 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001149 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001150 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001151 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001152
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001153 // Functionality common to Module and Import.
1154 embeddableInModuleAndImport
1155
Liz Kammerd6c31d22020-08-05 15:40:41 -07001156 hiddenAPI
1157 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001158 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001159
Colin Cross74d73e22017-08-02 11:05:49 -07001160 properties ImportProperties
1161
Liz Kammerd6c31d22020-08-05 15:40:41 -07001162 // output file containing classes.dex and resources
1163 dexJarFile android.Path
1164
Colin Cross0a6e0072017-08-30 14:24:55 -07001165 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001166 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001167 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001168
1169 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001170
1171 sdkVersion android.SdkSpec
1172 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001173}
1174
Jiyong Park92315372021-04-02 08:45:46 +09001175func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1176 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001177}
1178
Jiyong Parkf1691d22021-03-29 20:11:58 +09001179func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001180 return "none"
1181}
1182
Jiyong Park92315372021-04-02 08:45:46 +09001183func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001184 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001185 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001186 }
Jiyong Park92315372021-04-02 08:45:46 +09001187 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001188}
1189
Jiyong Park92315372021-04-02 08:45:46 +09001190func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1191 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001192}
1193
Colin Cross74d73e22017-08-02 11:05:49 -07001194func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001195 return &j.prebuilt
1196}
1197
Colin Cross74d73e22017-08-02 11:05:49 -07001198func (j *Import) PrebuiltSrcs() []string {
1199 return j.properties.Jars
1200}
1201
1202func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001203 return j.prebuilt.Name(j.ModuleBase.Name())
1204}
1205
Jiyong Park0b238752019-10-29 11:23:10 +09001206func (j *Import) Stem() string {
1207 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1208}
1209
Jiyong Park618922e2020-01-08 13:35:43 +09001210func (a *Import) JacocoReportClassesFile() android.Path {
1211 return nil
1212}
1213
Bill Peckhama41a6962021-01-11 10:58:54 -08001214func (j *Import) LintDepSets() LintDepSets {
1215 return LintDepSets{}
1216}
1217
Colin Cross74d73e22017-08-02 11:05:49 -07001218func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001219 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001220
1221 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001222 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001223 }
Colin Cross1e676be2016-10-12 14:38:15 -07001224}
1225
Colin Cross74d73e22017-08-02 11:05:49 -07001226func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001227 j.sdkVersion = j.SdkVersion(ctx)
1228 j.minSdkVersion = j.MinSdkVersion(ctx)
1229
Paul Duffin4103e922021-02-01 19:01:34 +00001230 // Initialize the hiddenapi structure.
1231 j.initHiddenAPI(ctx, j.BaseModuleName())
1232
Colin Cross56a83212020-09-15 18:30:11 -07001233 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1234 j.hideApexVariantFromMake = true
1235 }
1236
Colin Cross8a497952019-03-05 22:25:09 -08001237 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001238
Jiyong Park0b238752019-10-29 11:23:10 +09001239 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001240 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001241 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1242 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001243 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001244 inputFile := outputFile
1245 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1246 TransformJetifier(ctx, outputFile, inputFile)
1247 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001248 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001249 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001250
Liz Kammerd6c31d22020-08-05 15:40:41 -07001251 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001252 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001253
Jiyong Park1be96912018-05-28 18:02:19 +09001254 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001255 tag := ctx.OtherModuleDependencyTag(module)
1256
Colin Crossdcf71b22021-02-01 13:59:03 -08001257 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1258 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001259 switch tag {
1260 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001261 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001262 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001263 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001264 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001265 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001266 switch tag {
1267 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001268 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001269 }
1270 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001271
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001272 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001273
1274 // Save away the `deapexer` module on which this depends, if any.
1275 if tag == android.DeapexerTag {
1276 deapexerModule = module
1277 }
Jiyong Park1be96912018-05-28 18:02:19 +09001278 })
1279
Nan Zhang4973ecf2018-08-10 13:42:12 -07001280 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001281 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001282 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001283 }
Jiyong Park19604de2020-03-24 16:44:11 +09001284
1285 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001286
Paul Duffin064b70c2020-11-02 17:32:38 +00001287 if ctx.Device() {
1288 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1289 // obtained from the associated deapexer module.
1290 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1291 if ai.ForPrebuiltApex {
1292 if deapexerModule == nil {
1293 // This should never happen as a variant for a prebuilt_apex is only created if the
1294 // deapxer module has been configured to export the dex implementation jar for this module.
1295 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1296 j.Name(), ai.ApexVariationName)
1297 }
1298
1299 // Get the path of the dex implementation jar from the `deapexer` module.
1300 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001301 if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
1302 j.dexJarFile = dexOutputPath
Paul Duffinf75e5272021-02-09 14:34:25 +00001303 j.hiddenAPIExtractInformation(ctx, dexOutputPath, outputFile)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001304 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001305 // This should never happen as a variant for a prebuilt_apex is only created if the
1306 // prebuilt_apex has been configured to export the java library dex file.
1307 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1308 }
1309 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001310 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001311 if sdkDep.invalidVersion {
1312 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1313 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1314 } else if sdkDep.useFiles {
1315 // sdkDep.jar is actually equivalent to turbine header.jar.
1316 flags.classpath = append(flags.classpath, sdkDep.jars...)
1317 }
1318
1319 // Dex compilation
1320
1321 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1322 if j.dexProperties.Uncompress_dex == nil {
1323 // If the value was not force-set by the user, use reasonable default based on the module.
1324 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1325 }
1326 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1327
Paul Duffin612e6102021-02-02 13:38:13 +00001328 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001329 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001330 if ctx.Failed() {
1331 return
1332 }
1333
Paul Duffin064b70c2020-11-02 17:32:38 +00001334 // Hidden API CSV generation and dex encoding
Paul Duffinf75e5272021-02-09 14:34:25 +00001335 dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, outputFile,
Paul Duffin064b70c2020-11-02 17:32:38 +00001336 proptools.Bool(j.dexProperties.Uncompress_dex))
1337
1338 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07001339 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001340 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001341
1342 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1343 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1344 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1345 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1346 AidlIncludeDirs: j.exportAidlIncludeDirs,
1347 })
Colin Cross2fe66872015-03-30 17:20:39 -07001348}
1349
Paul Duffinaa55f742020-10-06 17:20:13 +01001350func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1351 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001352 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001353 return android.Paths{j.combinedClasspathFile}, nil
1354 default:
1355 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1356 }
1357}
1358
1359var _ android.OutputFileProducer = (*Import)(nil)
1360
Nan Zhanged19fc32017-10-19 13:06:22 -07001361func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001362 if j.combinedClasspathFile == nil {
1363 return nil
1364 }
Colin Cross37f6d792018-07-12 12:28:41 -07001365 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001366}
1367
Colin Cross331a1212018-08-15 20:40:52 -07001368func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001369 if j.combinedClasspathFile == nil {
1370 return nil
1371 }
Colin Cross331a1212018-08-15 20:40:52 -07001372 return android.Paths{j.combinedClasspathFile}
1373}
1374
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001375func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001376 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001377}
1378
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001379func (j *Import) DexJarInstallPath() android.Path {
1380 return nil
1381}
1382
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001383func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1384 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001385}
1386
Jiyong Park45bf82e2020-12-15 22:29:02 +09001387var _ android.ApexModule = (*Import)(nil)
1388
1389// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001390func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001391 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001392}
1393
Jiyong Park45bf82e2020-12-15 22:29:02 +09001394// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001395func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1396 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001397 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001398 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001399 return fmt.Errorf("min_sdk_version is not specified")
1400 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001401 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001402 return nil
1403 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001404 ver, err := sdkSpec.EffectiveVersion(ctx)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001405 if err != nil {
1406 return err
1407 }
Jiyong Park54105c42021-03-31 18:17:53 +09001408 if ver.GreaterThan(sdkVersion) {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001409 return fmt.Errorf("newer SDK(%v)", ver)
1410 }
Jooyung Han749dc692020-04-15 11:03:39 +09001411 return nil
1412}
1413
albaltai36ff7dc2018-12-25 14:35:23 +08001414// Add compile time check for interface implementation
1415var _ android.IDEInfo = (*Import)(nil)
1416var _ android.IDECustomizedModuleName = (*Import)(nil)
1417
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001418// Collect information for opening IDE project files in java/jdeps.go.
1419const (
1420 removedPrefix = "prebuilt_"
1421)
1422
1423func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1424 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1425}
1426
1427func (j *Import) IDECustomizedModuleName() string {
1428 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1429 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1430 // solution to get the Import name.
1431 name := j.Name()
1432 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001433 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001434 }
1435 return name
1436}
1437
Colin Cross74d73e22017-08-02 11:05:49 -07001438var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001439
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001440func (j *Import) IsInstallable() bool {
1441 return Bool(j.properties.Installable)
1442}
1443
1444var _ dexpreopterInterface = (*Import)(nil)
1445
Colin Cross1b16b0e2019-02-12 14:41:32 -08001446// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1447//
1448// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1449// compiled against an Android classpath.
1450//
1451// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1452// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001453func ImportFactory() android.Module {
1454 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001455
Liz Kammerd6c31d22020-08-05 15:40:41 -07001456 module.AddProperties(
1457 &module.properties,
1458 &module.dexer.dexProperties,
1459 )
Colin Cross74d73e22017-08-02 11:05:49 -07001460
Paul Duffin859fe962020-05-15 10:20:31 +01001461 module.initModuleAndImport(&module.ModuleBase)
1462
Liz Kammerd6c31d22020-08-05 15:40:41 -07001463 module.dexProperties.Optimize.EnabledByDefault = false
1464
Colin Cross74d73e22017-08-02 11:05:49 -07001465 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001466 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001467 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001468 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001469 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001470}
1471
Colin Cross1b16b0e2019-02-12 14:41:32 -08001472// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1473// module.
1474//
1475// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1476// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001477func ImportFactoryHost() android.Module {
1478 module := &Import{}
1479
1480 module.AddProperties(&module.properties)
1481
1482 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001483 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001484 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001485 return module
1486}
1487
Colin Cross42be7612019-02-21 18:12:14 -08001488// dex_import module
1489
1490type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001491 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001492
1493 // set the name of the output
1494 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001495}
1496
1497type DexImport struct {
1498 android.ModuleBase
1499 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001500 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001501 prebuilt android.Prebuilt
1502
1503 properties DexImportProperties
1504
Colin Crossb014f072021-02-26 14:54:36 -08001505 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001506
1507 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001508
1509 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001510}
1511
1512func (j *DexImport) Prebuilt() *android.Prebuilt {
1513 return &j.prebuilt
1514}
1515
1516func (j *DexImport) PrebuiltSrcs() []string {
1517 return j.properties.Jars
1518}
1519
1520func (j *DexImport) Name() string {
1521 return j.prebuilt.Name(j.ModuleBase.Name())
1522}
1523
Jiyong Park0b238752019-10-29 11:23:10 +09001524func (j *DexImport) Stem() string {
1525 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1526}
1527
Jiyong Park77acec62020-06-01 21:39:15 +09001528func (a *DexImport) JacocoReportClassesFile() android.Path {
1529 return nil
1530}
1531
Colin Cross08dca382020-07-21 20:31:17 -07001532func (a *DexImport) LintDepSets() LintDepSets {
1533 return LintDepSets{}
1534}
1535
Martin Stjernholm6d415272020-01-31 17:10:36 +00001536func (j *DexImport) IsInstallable() bool {
1537 return true
1538}
1539
Colin Cross42be7612019-02-21 18:12:14 -08001540func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1541 if len(j.properties.Jars) != 1 {
1542 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1543 }
1544
Colin Cross56a83212020-09-15 18:30:11 -07001545 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1546 if !apexInfo.IsForPlatform() {
1547 j.hideApexVariantFromMake = true
1548 }
1549
Jiyong Park0b238752019-10-29 11:23:10 +09001550 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001551 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1552
1553 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1554 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1555
1556 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001557 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001558
1559 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1560 rule.Temporary(temporary)
1561
1562 // use zip2zip to uncompress classes*.dex files
1563 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001564 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001565 FlagWithInput("-i ", inputJar).
1566 FlagWithOutput("-o ", temporary).
1567 FlagWithArg("-0 ", "'classes*.dex'")
1568
1569 // use zipalign to align uncompressed classes*.dex files
1570 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001571 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001572 Flag("-f").
1573 Text("4").
1574 Input(temporary).
1575 Output(dexOutputFile)
1576
1577 rule.DeleteTemporaryFiles()
1578
Colin Crossf1a035e2020-11-16 17:32:30 -08001579 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001580 } else {
1581 ctx.Build(pctx, android.BuildParams{
1582 Rule: android.Cp,
1583 Input: inputJar,
1584 Output: dexOutputFile,
1585 })
1586 }
1587
1588 j.dexJarFile = dexOutputFile
1589
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001590 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001591
Colin Cross56a83212020-09-15 18:30:11 -07001592 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001593 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1594 j.Stem()+".jar", dexOutputFile)
1595 }
Colin Cross42be7612019-02-21 18:12:14 -08001596}
1597
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001598func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001599 return j.dexJarFile
1600}
1601
Jiyong Park45bf82e2020-12-15 22:29:02 +09001602var _ android.ApexModule = (*DexImport)(nil)
1603
1604// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001605func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1606 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001607 // we don't check prebuilt modules for sdk_version
1608 return nil
1609}
1610
Colin Cross42be7612019-02-21 18:12:14 -08001611// dex_import imports a `.jar` file containing classes.dex files.
1612//
1613// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1614// to the device.
1615func DexImportFactory() android.Module {
1616 module := &DexImport{}
1617
1618 module.AddProperties(&module.properties)
1619
1620 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001621 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001622 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001623 return module
1624}
1625
Colin Cross89536d42017-07-07 14:35:50 -07001626//
1627// Defaults
1628//
1629type Defaults struct {
1630 android.ModuleBase
1631 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001632 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001633}
1634
Colin Cross1b16b0e2019-02-12 14:41:32 -08001635// java_defaults provides a set of properties that can be inherited by other java or android modules.
1636//
1637// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1638// property in the defaults module that exists in the depending module will be prepended to the depending module's
1639// value for that property.
1640//
1641// Example:
1642//
1643// java_defaults {
1644// name: "example_defaults",
1645// srcs: ["common/**/*.java"],
1646// javacflags: ["-Xlint:all"],
1647// aaptflags: ["--auto-add-overlay"],
1648// }
1649//
1650// java_library {
1651// name: "example",
1652// defaults: ["example_defaults"],
1653// srcs: ["example/**/*.java"],
1654// }
1655//
1656// is functionally identical to:
1657//
1658// java_library {
1659// name: "example",
1660// srcs: [
1661// "common/**/*.java",
1662// "example/**/*.java",
1663// ],
1664// javacflags: ["-Xlint:all"],
1665// }
Paul Duffin47357662019-12-05 14:07:14 +00001666func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001667 module := &Defaults{}
1668
Colin Cross89536d42017-07-07 14:35:50 -07001669 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001670 &CommonProperties{},
1671 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001672 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001673 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001674 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001675 &aaptProperties{},
1676 &androidLibraryProperties{},
1677 &appProperties{},
1678 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001679 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001680 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001681 &ImportProperties{},
1682 &AARImportProperties{},
1683 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001684 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001685 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001686 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001687 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001688 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001689 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001690 )
1691
1692 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001693 return module
1694}
Nan Zhangea568a42017-11-08 21:20:04 -08001695
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001696func kytheExtractJavaFactory() android.Singleton {
1697 return &kytheExtractJavaSingleton{}
1698}
1699
1700type kytheExtractJavaSingleton struct {
1701}
1702
1703func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1704 var xrefTargets android.Paths
1705 ctx.VisitAllModules(func(module android.Module) {
1706 if javaModule, ok := module.(xref); ok {
1707 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1708 }
1709 })
1710 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1711 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001712 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001713 }
1714}
1715
Nan Zhangea568a42017-11-08 21:20:04 -08001716var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001717var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001718var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001719var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001720
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001721// Add class loader context (CLC) of a given dependency to the current CLC.
1722func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1723 clcMap dexpreopt.ClassLoaderContextMap) {
1724
1725 dep, ok := depModule.(UsesLibraryDependency)
1726 if !ok {
1727 return
1728 }
1729
1730 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1731 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1732 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1733 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1734 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1735 // the current CLC.
1736 var implicitSdkLib *string
1737 comp, isComp := depModule.(SdkLibraryComponentDependency)
1738 if isComp {
1739 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1740 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1741 }
1742 if implicitSdkLib == nil {
1743 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1744 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001745 }
1746 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001747
1748 depTag := ctx.OtherModuleDependencyTag(depModule)
1749 if depTag == libTag || depTag == usesLibTag {
1750 // Ok, propagate <uses-library> through non-static library dependencies.
1751 } else if depTag == staticLibTag {
1752 // Propagate <uses-library> through static library dependencies, unless it is a component
1753 // library (such as stubs). Component libraries have a dependency on their SDK library,
1754 // which should not be pulled just because of a static component library.
1755 if implicitSdkLib != nil {
1756 return
1757 }
1758 } else {
1759 // Don't propagate <uses-library> for other dependency tags.
1760 return
1761 }
1762
1763 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001764 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001765 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1766 } else {
1767 depName := ctx.OtherModuleName(depModule)
1768 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1769 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001770}