blob: e20ef9e0676eab329543db505d82c78c508bc736 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross2fe66872015-03-30 17:20:39 -070024
25 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070026 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossf8d9c492021-01-26 11:01:43 -080029 "android/soong/cc"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010030 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Paul Duffin535e0a12021-03-30 23:34:32 +010036 registerJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000037
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080038 RegisterJavaSdkMemberTypes()
39}
40
Paul Duffin535e0a12021-03-30 23:34:32 +010041func registerJavaBuildComponents(ctx android.RegistrationContext) {
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080042 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
43
44 ctx.RegisterModuleType("java_library", LibraryFactory)
45 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
46 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
47 ctx.RegisterModuleType("java_binary", BinaryFactory)
48 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
49 ctx.RegisterModuleType("java_test", TestFactory)
50 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
51 ctx.RegisterModuleType("java_test_host", TestHostFactory)
52 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
53 ctx.RegisterModuleType("java_import", ImportFactory)
54 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
55 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
56 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
57 ctx.RegisterModuleType("dex_import", DexImportFactory)
58
Martin Stjernholm0e4cceb2021-05-13 02:38:35 +010059 // This mutator registers dependencies on dex2oat for modules that should be
60 // dexpreopted. This is done late when the final variants have been
61 // established, to not get the dependencies split into the wrong variants and
62 // to support the checks in dexpreoptDisabled().
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080063 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
64 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
65 })
66
67 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
68 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
69}
70
71func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000072 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000073 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010074 android.RegisterSdkMemberType(javaLibsSdkMemberType)
75 android.RegisterSdkMemberType(javaBootLibsSdkMemberType)
76 android.RegisterSdkMemberType(javaTestSdkMemberType)
77}
78
79var (
80 // Supports adding java header libraries to module_exports and sdk.
81 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
82 android.SdkMemberTypeBase{
83 PropertyName: "java_header_libs",
84 SupportsSdk: true,
85 },
86 func(_ android.SdkMemberContext, j *Library) android.Path {
87 headerJars := j.HeaderJars()
88 if len(headerJars) != 1 {
89 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
90 }
91
92 return headerJars[0]
93 },
94 sdkSnapshotFilePathForJar,
95 copyEverythingToSnapshot,
96 }
Paul Duffin255f18e2019-12-13 11:22:16 +000097
Paul Duffin22ff0aa2021-02-04 11:15:34 +000098 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +010099 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000100 implementationJars := j.ImplementationAndResourcesJars()
101 if len(implementationJars) != 1 {
102 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
103 }
104 return implementationJars[0]
105 }
106
Paul Duffin2da04242021-04-23 19:43:28 +0100107 // Supports adding java implementation libraries to module_exports but not sdk.
108 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000109 android.SdkMemberTypeBase{
110 PropertyName: "java_libs",
111 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000112 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000113 sdkSnapshotFilePathForJar,
114 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100115 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000116
Paul Duffin2da04242021-04-23 19:43:28 +0100117 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000118 //
119 // The build has some implicit dependencies (via the boot jars configuration) on a number of
120 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
121 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
122 // used outside those mainline modules.
123 //
124 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
125 // either java_libs, or java_header_libs would end up exporting more information than was strictly
126 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
127 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100128 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000129 android.SdkMemberTypeBase{
130 PropertyName: "java_boot_libs",
131 SupportsSdk: true,
132 },
Paul Duffin5c211452021-07-15 12:42:44 +0100133 func(ctx android.SdkMemberContext, j *Library) android.Path {
134 // Java boot libs are only provided in the SDK to provide access to their dex implementation
135 // jar for use by dexpreopting and boot jars package check. They do not need to provide an
136 // actual implementation jar but the java_import will need a file that exists so just copy an
137 // empty file. Any attempt to use that file as a jar will cause a build error.
138 return ctx.SnapshotBuilder().EmptyFile()
139 },
140 func(osPrefix, name string) string {
141 // Create a special name for the implementation jar to try and provide some useful information
142 // to a developer that attempts to compile against this.
143 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
144 return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
145 },
Paul Duffindb170e42020-12-08 17:48:25 +0000146 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100147 }
Paul Duffindb170e42020-12-08 17:48:25 +0000148
Paul Duffin2da04242021-04-23 19:43:28 +0100149 // Supports adding java test libraries to module_exports but not sdk.
150 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000151 SdkMemberTypeBase: android.SdkMemberTypeBase{
152 PropertyName: "java_tests",
153 },
Paul Duffin2da04242021-04-23 19:43:28 +0100154 }
155)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900156
Colin Crossdcf71b22021-02-01 13:59:03 -0800157// JavaInfo contains information about a java module for use by modules that depend on it.
158type JavaInfo struct {
159 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
160 // against this module. If empty, ImplementationJars should be used instead.
161 HeaderJars android.Paths
162
163 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
164 // in the module as well as any resources included in the module.
165 ImplementationAndResourcesJars android.Paths
166
167 // ImplementationJars is a list of jars that contain the implementations of classes in the
168 //module.
169 ImplementationJars android.Paths
170
171 // ResourceJars is a list of jars that contain the resources included in the module.
172 ResourceJars android.Paths
173
174 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
175 // depending on this module.
176 AidlIncludeDirs android.Paths
177
178 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
179 // module.
180 SrcJarArgs []string
181
182 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
183 SrcJarDeps android.Paths
184
185 // ExportedPlugins is a list of paths that should be used as annotation processors for any
186 // module that depends on this module.
187 ExportedPlugins android.Paths
188
189 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
190 // any module that depends on this module.
191 ExportedPluginClasses []string
192
193 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
194 // requiring disbling turbine for any modules that depend on it.
195 ExportedPluginDisableTurbine bool
196
197 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
198 // instrumented by jacoco.
199 JacocoReportClassesFile android.Path
200}
201
202var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
203
Colin Cross75ce9ec2021-02-26 16:20:32 -0800204// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
205// the sysprop implementation library.
206type SyspropPublicStubInfo struct {
207 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
208 // the sysprop implementation library.
209 JavaInfo JavaInfo
210}
211
212var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
213
Paul Duffin44b481b2020-06-17 16:59:43 +0100214// Methods that need to be implemented for a module that is added to apex java_libs property.
215type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700216 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100217 ImplementationAndResourcesJars() android.Paths
218}
219
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100220// Provides build path and install path to DEX jars.
221type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000222 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100223 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000224 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100225}
226
Jaewoong Jung26342642021-03-17 15:56:23 -0700227// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800228type xref interface {
229 XrefJavaFiles() android.Paths
230}
231
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800232func (j *Module) XrefJavaFiles() android.Paths {
233 return j.kytheFiles
234}
235
Colin Crossbe1da472017-07-07 15:59:46 -0700236type dependencyTag struct {
237 blueprint.BaseDependencyTag
238 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700239}
240
Colin Crosse9fe2942020-11-10 18:12:15 -0800241// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
242// dependency to be installed when the parent module is installed.
243type installDependencyTag struct {
244 blueprint.BaseDependencyTag
245 android.InstallAlwaysNeededDependencyTag
246 name string
247}
248
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100249type usesLibraryDependencyTag struct {
250 dependencyTag
251 sdkVersion int // SDK version in which the library appared as a standalone library.
252}
253
254func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
255 return usesLibraryDependencyTag{
256 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
257 sdkVersion: sdkVersion,
258 }
259}
260
Jiyong Park8be103b2019-11-08 15:53:48 +0900261func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700262 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900263}
264
Colin Crossbe1da472017-07-07 15:59:46 -0700265var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800266 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
267 staticLibTag = dependencyTag{name: "staticlib"}
268 libTag = dependencyTag{name: "javalib"}
269 java9LibTag = dependencyTag{name: "java9lib"}
270 pluginTag = dependencyTag{name: "plugin"}
271 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
272 exportedPluginTag = dependencyTag{name: "exported-plugin"}
273 bootClasspathTag = dependencyTag{name: "bootclasspath"}
274 systemModulesTag = dependencyTag{name: "system modules"}
275 frameworkResTag = dependencyTag{name: "framework-res"}
276 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
277 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
278 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
279 certificateTag = dependencyTag{name: "certificate"}
280 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
281 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
282 jniLibTag = dependencyTag{name: "jnilib"}
283 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
284 jniInstallTag = installDependencyTag{name: "jni install"}
285 binaryInstallTag = installDependencyTag{name: "binary install"}
286 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
287 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
288 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
289 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700290)
Colin Cross2fe66872015-03-30 17:20:39 -0700291
Jiyong Park83dc74b2020-01-14 18:38:44 +0900292func IsLibDepTag(depTag blueprint.DependencyTag) bool {
293 return depTag == libTag
294}
295
296func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
297 return depTag == staticLibTag
298}
299
Colin Crossfc3674a2017-09-18 17:41:52 -0700300type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100301 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700302
Colin Cross6cef4812019-10-17 14:23:50 -0700303 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
304 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100305
306 // The default system modules to use. Will be an empty string if no system
307 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700308 systemModules string
309
Pete Gilline3d44b22020-06-29 11:28:51 +0100310 // The modules that will be added to the classpath regardless of the Java language level targeted
311 classpath []string
312
Colin Cross6cef4812019-10-17 14:23:50 -0700313 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100314 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700315 java9Classpath []string
316
Colin Crossa97c5d32018-03-28 14:58:31 -0700317 frameworkResModule string
318
Colin Cross86a60ae2018-05-29 14:44:55 -0700319 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700320 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100321
322 noStandardLibs, noFrameworksLibs bool
323}
324
325func (s sdkDep) hasStandardLibs() bool {
326 return !s.noStandardLibs
327}
328
329func (s sdkDep) hasFrameworkLibs() bool {
330 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700331}
332
Colin Crossa4f08812018-10-02 22:03:40 -0700333type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700334 name string
335 path android.Path
336 target android.Target
337 coverageFile android.OptionalPath
338 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700339}
340
Jiyong Parkf1691d22021-03-29 20:11:58 +0900341func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700342 sdkDep := decodeSdkDep(ctx, sdkContext)
343 if sdkDep.useModule {
344 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
345 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
346 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
347 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
348 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
349 }
350 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
351 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
352 }
353 }
354 if sdkDep.systemModules != "" {
355 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
356 }
357}
358
Colin Cross32f676a2017-09-06 13:41:06 -0700359type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800360 classpath classpath
361 java9Classpath classpath
362 bootClasspath classpath
363 processorPath classpath
364 errorProneProcessorPath classpath
365 processorClasses []string
366 staticJars android.Paths
367 staticHeaderJars android.Paths
368 staticResourceJars android.Paths
369 aidlIncludeDirs android.Paths
370 srcs android.Paths
371 srcJars android.Paths
372 systemModules *systemModules
373 aidlPreprocess android.OptionalPath
374 kotlinStdlib android.Paths
375 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800376
377 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700378}
Colin Cross2fe66872015-03-30 17:20:39 -0700379
Colin Cross54250902017-12-05 09:28:08 -0800380func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
381 for _, f := range dep.Srcs() {
382 if f.Ext() != ".jar" {
383 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
384 ctx.OtherModuleName(dep.(blueprint.Module)))
385 }
386 }
387}
388
Jiyong Parkf1691d22021-03-29 20:11:58 +0900389func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700390 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700391 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700392 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900393 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700394 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700395 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700396 }
Nan Zhang357466b2018-04-17 17:38:36 -0700397}
398
Colin Cross1e743852019-10-28 11:37:20 -0700399type javaVersion int
400
401const (
402 JAVA_VERSION_UNSUPPORTED = 0
403 JAVA_VERSION_6 = 6
404 JAVA_VERSION_7 = 7
405 JAVA_VERSION_8 = 8
406 JAVA_VERSION_9 = 9
407)
408
409func (v javaVersion) String() string {
410 switch v {
411 case JAVA_VERSION_6:
412 return "1.6"
413 case JAVA_VERSION_7:
414 return "1.7"
415 case JAVA_VERSION_8:
416 return "1.8"
417 case JAVA_VERSION_9:
418 return "1.9"
419 default:
420 return "unsupported"
421 }
422}
423
424// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
425func (v javaVersion) usesJavaModules() bool {
426 return v >= 9
427}
428
429func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100430 switch javaVersion {
431 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700432 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100433 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700434 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100435 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700436 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100437 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700438 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100439 case "10", "11":
440 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700441 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100442 default:
443 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700444 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100445 }
446}
447
Colin Cross2fe66872015-03-30 17:20:39 -0700448//
449// Java libraries (.jar file)
450//
451
Colin Crossf506d872017-07-19 15:53:04 -0700452type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700453 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700454
455 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700456}
457
Jiyong Park45bf82e2020-12-15 22:29:02 +0900458var _ android.ApexModule = (*Library)(nil)
459
Paul Duffine739f1e2020-05-29 11:24:51 +0100460// Provides access to the list of permitted packages from updatable boot jars.
461type PermittedPackagesForUpdatableBootJars interface {
462 PermittedPackagesForUpdatableBootJars() []string
463}
464
465var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
466
467func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
468 return j.properties.Permitted_packages
469}
470
Colin Cross42be7612019-02-21 18:12:14 -0800471func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000472 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700473 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000474 return true
475 }
476
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000477 // Store uncompressed (and do not strip) dex files from boot class path jars.
478 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
479 return true
480 }
481
482 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -0800483 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000484 return true
485 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800486 if ctx.Config().UncompressPrivAppDex() &&
487 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
488 return true
489 }
490
Colin Cross2fc72f62018-12-21 12:59:54 -0800491 return false
492}
493
Colin Crossf506d872017-07-19 15:53:04 -0700494func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900495 j.sdkVersion = j.SdkVersion(ctx)
496 j.minSdkVersion = j.MinSdkVersion(ctx)
497
Colin Cross56a83212020-09-15 18:30:11 -0700498 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
499 if !apexInfo.IsForPlatform() {
500 j.hideApexVariantFromMake = true
501 }
502
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100503 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +0900504 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -0800505 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700506 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100507 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700508 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100509 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700510 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100511 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700512 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700513
bralee1fbf4402020-05-21 10:11:59 +0800514 // Collect the module directory for IDE info in java/jdeps.go.
515 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
516
Colin Cross56a83212020-09-15 18:30:11 -0700517 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900518 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700519 var extraInstallDeps android.Paths
520 if j.InstallMixin != nil {
521 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
522 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700523 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900524 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700525 }
Colin Crossb7a63242015-04-16 14:09:14 -0700526}
527
Colin Crossf506d872017-07-19 15:53:04 -0700528func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700529 j.deps(ctx)
530}
531
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000532const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000533 aidlIncludeDir = "aidl"
534 javaDir = "java"
535 jarFileSuffix = ".jar"
536 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000537)
538
Paul Duffina0dbf432019-12-05 11:25:53 +0000539// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000540func sdkSnapshotFilePathForJar(osPrefix, name string) string {
541 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000542}
543
Paul Duffina04c1072020-03-02 10:16:35 +0000544func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
545 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000546}
547
Paul Duffin13879572019-11-28 14:31:38 +0000548type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000549 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000550
551 // Function to retrieve the appropriate output jar (implementation or header) from
552 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000553 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
554
555 // Function to compute the snapshot relative path to which the named library's
556 // jar should be copied.
557 snapshotPathGetter func(osPrefix, name string) string
558
559 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
560 // files like aidl files should also be copied.
561 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000562}
563
Paul Duffindb170e42020-12-08 17:48:25 +0000564const (
565 onlyCopyJarToSnapshot = true
566 copyEverythingToSnapshot = false
567)
568
Paul Duffin13879572019-11-28 14:31:38 +0000569func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
570 mctx.AddVariationDependencies(nil, dependencyTag, names...)
571}
572
573func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
574 _, ok := module.(*Library)
575 return ok
576}
577
Paul Duffin3a4eb502020-03-19 16:11:18 +0000578func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
579 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000580}
Paul Duffina0dbf432019-12-05 11:25:53 +0000581
Paul Duffin14eb4672020-03-02 11:33:02 +0000582func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000583 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000584}
585
586type librarySdkMemberProperties struct {
587 android.SdkMemberPropertiesBase
588
Paul Duffin864e1b42020-05-06 10:23:19 +0100589 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000590 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100591
592 // The list of permitted packages that need to be passed to the prebuilts as they are used to
593 // create the updatable-bcp-packages.txt file.
594 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000595}
596
Paul Duffin3a4eb502020-03-19 16:11:18 +0000597func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000598 j := variant.(*Library)
599
Paul Duffindb170e42020-12-08 17:48:25 +0000600 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
601
Paul Duffina551a1c2020-03-17 21:04:24 +0000602 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100603
604 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000605}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000606
Paul Duffin3a4eb502020-03-19 16:11:18 +0000607func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000608 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000609
Paul Duffindb170e42020-12-08 17:48:25 +0000610 memberType := ctx.MemberType().(*librarySdkMemberType)
611
Paul Duffina551a1c2020-03-17 21:04:24 +0000612 exportedJar := p.JarToExport
613 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000614 // Delegate the creation of the snapshot relative path to the member type.
615 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
616
617 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000618 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
619
Paul Duffina551a1c2020-03-17 21:04:24 +0000620 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
621 }
622
Paul Duffin869de142021-07-15 14:14:41 +0100623 if len(p.PermittedPackages) > 0 {
624 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
625 }
626
Paul Duffindb170e42020-12-08 17:48:25 +0000627 // Do not copy anything else to the snapshot.
628 if memberType.onlyCopyJarToSnapshot {
629 return
630 }
631
Paul Duffina551a1c2020-03-17 21:04:24 +0000632 aidlIncludeDirs := p.AidlIncludeDirs
633 if len(aidlIncludeDirs) != 0 {
634 sdkModuleContext := ctx.SdkModuleContext()
635 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000636 // TODO(jiyong): copy parcelable declarations only
637 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
638 for _, file := range aidlFiles {
639 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
640 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000641 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000642
Paul Duffina551a1c2020-03-17 21:04:24 +0000643 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000644 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000645}
646
Colin Cross1b16b0e2019-02-12 14:41:32 -0800647// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
648//
649// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
650// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
651// as a `static_libs` dependency of another module.
652//
653// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
654// a device.
655//
656// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
657// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700658func LibraryFactory() android.Module {
659 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700660
Colin Crossce6734e2020-06-15 16:09:53 -0700661 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700662
Paul Duffin71b33cc2021-06-23 11:39:47 +0100663 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100664
Jiyong Park7f7766d2019-07-25 22:02:35 +0900665 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900666 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900667 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700668 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700669}
670
Colin Cross1b16b0e2019-02-12 14:41:32 -0800671// java_library_static is an obsolete alias for java_library.
672func LibraryStaticFactory() android.Module {
673 return LibraryFactory()
674}
675
676// java_library_host builds and links sources into a `.jar` file for the host.
677//
678// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
679// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700680func LibraryHostFactory() android.Module {
681 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700682
Colin Crossce6734e2020-06-15 16:09:53 -0700683 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700684
Colin Cross9ae1b922018-06-26 17:59:05 -0700685 module.Module.properties.Installable = proptools.BoolPtr(true)
686
Jiyong Park7f7766d2019-07-25 22:02:35 +0900687 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100688 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900689 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700690 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700691}
692
693//
Colin Crossb628ea52018-08-14 16:42:33 -0700694// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700695//
696
Dan Shi95d19422020-08-15 12:24:26 -0700697// Test option struct.
698type TestOptions struct {
699 // a list of extra test configuration files that should be installed with the module.
700 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800701
702 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
703 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700704}
705
Colin Cross05638fc2018-04-09 18:40:24 -0700706type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700707 // list of compatibility suites (for example "cts", "vts") that the module should be
708 // installed into.
709 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700710
711 // the name of the test configuration (for example "AndroidTest.xml") that should be
712 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800713 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700714
Jack He33338892018-09-19 02:21:28 -0700715 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
716 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800717 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700718
Colin Crossd96ca352018-08-10 16:06:24 -0700719 // list of files or filegroup modules that provide data that should be installed alongside
720 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900721 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700722
723 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
724 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
725 // explicitly.
726 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800727
728 // Add parameterized mainline modules to auto generated test config. The options will be
729 // handled by TradeFed to do downloading and installing the specified modules on the device.
730 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700731
732 // Test options.
733 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800734
735 // Names of modules containing JNI libraries that should be installed alongside the test.
736 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700737}
738
Liz Kammerdd849a82020-06-12 16:38:45 -0700739type hostTestProperties struct {
740 // list of native binary modules that should be installed alongside the test
741 Data_native_bins []string `android:"arch_variant"`
742}
743
Paul Duffin42df1442019-03-20 12:45:53 +0000744type testHelperLibraryProperties struct {
745 // list of compatibility suites (for example "cts", "vts") that the module should be
746 // installed into.
747 Test_suites []string `android:"arch_variant"`
748}
749
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000750type prebuiltTestProperties struct {
751 // list of compatibility suites (for example "cts", "vts") that the module should be
752 // installed into.
753 Test_suites []string `android:"arch_variant"`
754
755 // the name of the test configuration (for example "AndroidTest.xml") that should be
756 // installed with the module.
757 Test_config *string `android:"path,arch_variant"`
758}
759
Colin Cross05638fc2018-04-09 18:40:24 -0700760type Test struct {
761 Library
762
763 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700764
Dan Shi95d19422020-08-15 12:24:26 -0700765 testConfig android.Path
766 extraTestConfigs android.Paths
767 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700768}
769
Liz Kammerdd849a82020-06-12 16:38:45 -0700770type TestHost struct {
771 Test
772
773 testHostProperties hostTestProperties
774}
775
Paul Duffin42df1442019-03-20 12:45:53 +0000776type TestHelperLibrary struct {
777 Library
778
779 testHelperLibraryProperties testHelperLibraryProperties
780}
781
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000782type JavaTestImport struct {
783 Import
784
785 prebuiltTestProperties prebuiltTestProperties
786
787 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700788 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000789}
790
Liz Kammerdd849a82020-06-12 16:38:45 -0700791func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
792 if len(j.testHostProperties.Data_native_bins) > 0 {
793 for _, target := range ctx.MultiTargets() {
794 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
795 }
796 }
797
Colin Crossf8d9c492021-01-26 11:01:43 -0800798 if len(j.testProperties.Jni_libs) > 0 {
799 for _, target := range ctx.MultiTargets() {
800 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
801 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
802 }
803 }
804
Liz Kammerdd849a82020-06-12 16:38:45 -0700805 j.deps(ctx)
806}
807
Yuexi Ma627263f2021-03-04 13:47:56 -0800808func (j *TestHost) AddExtraResource(p android.Path) {
809 j.extraResources = append(j.extraResources, p)
810}
811
Colin Cross303e21f2018-08-07 16:49:25 -0700812func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000813 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
814 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700815 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000816 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
817 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700818 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800819 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700820
Colin Cross8a497952019-03-05 22:25:09 -0800821 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700822
Dan Shi95d19422020-08-15 12:24:26 -0700823 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
824
Liz Kammerdd849a82020-06-12 16:38:45 -0700825 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
826 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
827 })
828
Colin Crossf8d9c492021-01-26 11:01:43 -0800829 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
830 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
831 if sharedLibInfo.SharedLibrary != nil {
832 // Copy to an intermediate output directory to append "lib[64]" to the path,
833 // so that it's compatible with the default rpath values.
834 var relPath string
835 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
836 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
837 } else {
838 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
839 }
840 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
841 ctx.Build(pctx, android.BuildParams{
842 Rule: android.Cp,
843 Input: sharedLibInfo.SharedLibrary,
844 Output: relocatedLib,
845 })
846 j.data = append(j.data, relocatedLib)
847 } else {
848 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
849 }
850 })
851
Colin Cross303e21f2018-08-07 16:49:25 -0700852 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700853}
854
Paul Duffin42df1442019-03-20 12:45:53 +0000855func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
856 j.Library.GenerateAndroidBuildActions(ctx)
857}
858
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000859func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
860 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800861 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000862
863 j.Import.GenerateAndroidBuildActions(ctx)
864}
865
866type testSdkMemberType struct {
867 android.SdkMemberTypeBase
868}
869
870func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
871 mctx.AddVariationDependencies(nil, dependencyTag, names...)
872}
873
874func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
875 _, ok := module.(*Test)
876 return ok
877}
878
Paul Duffin3a4eb502020-03-19 16:11:18 +0000879func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
880 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000881}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000882
Paul Duffin14eb4672020-03-02 11:33:02 +0000883func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
884 return &testSdkMemberProperties{}
885}
886
887type testSdkMemberProperties struct {
888 android.SdkMemberPropertiesBase
889
Paul Duffina551a1c2020-03-17 21:04:24 +0000890 JarToExport android.Path
891 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000892}
893
Paul Duffin3a4eb502020-03-19 16:11:18 +0000894func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000895 test := variant.(*Test)
896
897 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000898 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000899 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000900 }
901
Paul Duffina551a1c2020-03-17 21:04:24 +0000902 p.JarToExport = implementationJars[0]
903 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000904}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000905
Paul Duffin3a4eb502020-03-19 16:11:18 +0000906func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000907 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000908
Paul Duffina551a1c2020-03-17 21:04:24 +0000909 exportedJar := p.JarToExport
910 if exportedJar != nil {
911 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
912 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000913
914 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000915 }
916
917 testConfig := p.TestConfig
918 if testConfig != nil {
919 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
920 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000921 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
922 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000923}
924
Colin Cross1b16b0e2019-02-12 14:41:32 -0800925// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
926// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
927//
928// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
929// compiled against the device bootclasspath.
930//
931// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
932// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700933func TestFactory() android.Module {
934 module := &Test{}
935
Colin Crossce6734e2020-06-15 16:09:53 -0700936 module.addHostAndDeviceProperties()
937 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700938
Colin Cross9ae1b922018-06-26 17:59:05 -0700939 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800940 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700941 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700942
Paul Duffinb6b89a42021-05-06 16:33:43 +0100943 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -0700944 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700945 return module
946}
947
Paul Duffin42df1442019-03-20 12:45:53 +0000948// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
949func TestHelperLibraryFactory() android.Module {
950 module := &TestHelperLibrary{}
951
Colin Crossce6734e2020-06-15 16:09:53 -0700952 module.addHostAndDeviceProperties()
953 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000954
Colin Cross9a4abed2019-04-24 13:19:28 -0700955 module.Module.properties.Installable = proptools.BoolPtr(true)
956 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700957 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700958
Paul Duffin42df1442019-03-20 12:45:53 +0000959 InitJavaModule(module, android.HostAndDeviceSupported)
960 return module
961}
962
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000963// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
964// and makes sure that it is added to the appropriate test suite.
965//
966// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
967// compiled against an Android classpath.
968//
969// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
970// for host modules.
971func JavaTestImportFactory() android.Module {
972 module := &JavaTestImport{}
973
974 module.AddProperties(
975 &module.Import.properties,
976 &module.prebuiltTestProperties)
977
978 module.Import.properties.Installable = proptools.BoolPtr(true)
979
980 android.InitPrebuiltModule(module, &module.properties.Jars)
981 android.InitApexModule(module)
982 android.InitSdkAwareModule(module)
983 InitJavaModule(module, android.HostAndDeviceSupported)
984 return module
985}
986
Colin Cross1b16b0e2019-02-12 14:41:32 -0800987// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
988// allow running the test with `atest` or a `TEST_MAPPING` file.
989//
990// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
991// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700992func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -0700993 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -0700994
Colin Crossce6734e2020-06-15 16:09:53 -0700995 module.addHostProperties()
996 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -0700997 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700998
Yuexi Ma627263f2021-03-04 13:47:56 -0800999 InitTestHost(
1000 module,
1001 proptools.BoolPtr(true),
1002 nil,
1003 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001004
Liz Kammerdd849a82020-06-12 16:38:45 -07001005 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001006
Colin Cross05638fc2018-04-09 18:40:24 -07001007 return module
1008}
1009
Yuexi Ma627263f2021-03-04 13:47:56 -08001010func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1011 th.properties.Installable = installable
1012 th.testProperties.Auto_gen_config = autoGenConfig
1013 th.testProperties.Test_suites = testSuites
1014}
1015
Colin Cross05638fc2018-04-09 18:40:24 -07001016//
Colin Cross2fe66872015-03-30 17:20:39 -07001017// Java Binaries (.jar file plus wrapper script)
1018//
1019
Colin Crossf506d872017-07-19 15:53:04 -07001020type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001021 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001022 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001023
1024 // Name of the class containing main to be inserted into the manifest as Main-Class.
1025 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001026
1027 // Names of modules containing JNI libraries that should be installed alongside the host
1028 // variant of the binary.
1029 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -07001030}
1031
Colin Crossf506d872017-07-19 15:53:04 -07001032type Binary struct {
1033 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001034
Colin Crossf506d872017-07-19 15:53:04 -07001035 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001036
Colin Cross6b4a32d2017-12-05 13:42:45 -08001037 isWrapperVariant bool
1038
Colin Crossc3315992017-12-08 19:12:36 -08001039 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001040 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001041}
1042
Alex Light24237172017-10-26 09:46:21 -07001043func (j *Binary) HostToolPath() android.OptionalPath {
1044 return android.OptionalPathForPath(j.binaryFile)
1045}
1046
Colin Crossf506d872017-07-19 15:53:04 -07001047func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001048 if ctx.Arch().ArchType == android.Common {
1049 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001050 if j.binaryProperties.Main_class != nil {
1051 if j.properties.Manifest != nil {
1052 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1053 }
1054 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1055 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1056 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1057 }
1058
Colin Cross6b4a32d2017-12-05 13:42:45 -08001059 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001060 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001061 // Handle the binary wrapper
1062 j.isWrapperVariant = true
1063
Colin Cross366938f2017-12-11 16:29:02 -08001064 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001065 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001066 } else {
1067 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1068 }
1069
Colin Crossc179ea62020-10-09 10:54:15 -07001070 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001071 // of the wrapper variant, which will include the common variant's jar file and any JNI
1072 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001073 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07001074 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001075 }
Colin Cross2fe66872015-03-30 17:20:39 -07001076}
1077
Colin Crossf506d872017-07-19 15:53:04 -07001078func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001079 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001080 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001081 }
1082 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001083 // These dependencies ensure the host installation rules will install the jar file and
1084 // the jni libraries when the wrapper is installed.
1085 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1086 ctx.AddVariationDependencies(
1087 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1088 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001089 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001090}
1091
Colin Cross1b16b0e2019-02-12 14:41:32 -08001092// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1093// as well.
1094//
1095// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1096// compiled against the device bootclasspath.
1097//
1098// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1099// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001100func BinaryFactory() android.Module {
1101 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001102
Colin Crossce6734e2020-06-15 16:09:53 -07001103 module.addHostAndDeviceProperties()
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.HostAndDeviceSupported, 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
Colin Cross1b16b0e2019-02-12 14:41:32 -08001113// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1114//
1115// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1116// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001117func BinaryHostFactory() android.Module {
1118 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001119
Colin Crossce6734e2020-06-15 16:09:53 -07001120 module.addHostProperties()
1121 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001122
Colin Cross9ae1b922018-06-26 17:59:05 -07001123 module.Module.properties.Installable = proptools.BoolPtr(true)
1124
Colin Cross6b4a32d2017-12-05 13:42:45 -08001125 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1126 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001127 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001128}
1129
1130//
1131// Java prebuilts
1132//
1133
Colin Cross74d73e22017-08-02 11:05:49 -07001134type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001135 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001136
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001137 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1138 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001139 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001140
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001141 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1142 // specified.
1143 Min_sdk_version *string
1144
Colin Cross535e2cf2017-10-20 17:57:49 -07001145 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001146
Paul Duffin869de142021-07-15 14:14:41 +01001147 // If not empty, classes are restricted to the specified packages and their sub-packages.
1148 // This information is used to generate the updatable-bcp-packages.txt file.
1149 Permitted_packages []string
1150
Jiyong Park1be96912018-05-28 18:02:19 +09001151 // List of shared java libs that this module has dependencies to
1152 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001153
1154 // List of files to remove from the jar file(s)
1155 Exclude_files []string
1156
1157 // List of directories to remove from the jar file(s)
1158 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001159
1160 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001161 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001162
1163 // set the name of the output
1164 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001165
1166 Aidl struct {
1167 // directories that should be added as include directories for any aidl sources of modules
1168 // that depend on this module, as well as to aidl for this module.
1169 Export_include_dirs []string
1170 }
Colin Cross74d73e22017-08-02 11:05:49 -07001171}
1172
1173type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001174 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001175 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001176 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001177 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001178 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001179
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001180 // Functionality common to Module and Import.
1181 embeddableInModuleAndImport
1182
Liz Kammerd6c31d22020-08-05 15:40:41 -07001183 hiddenAPI
1184 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001185 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001186
Colin Cross74d73e22017-08-02 11:05:49 -07001187 properties ImportProperties
1188
Liz Kammerd6c31d22020-08-05 15:40:41 -07001189 // output file containing classes.dex and resources
Jeongik Chad5fe8782021-07-08 01:13:11 +09001190 dexJarFile android.Path
1191 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001192
Colin Cross0a6e0072017-08-30 14:24:55 -07001193 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001194 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001195 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001196
1197 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001198
1199 sdkVersion android.SdkSpec
1200 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001201}
1202
Paul Duffin630b11e2021-07-15 13:35:26 +01001203var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1204
1205func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1206 return j.properties.Permitted_packages
1207}
1208
Jiyong Park92315372021-04-02 08:45:46 +09001209func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1210 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001211}
1212
Jiyong Parkf1691d22021-03-29 20:11:58 +09001213func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001214 return "none"
1215}
1216
Jiyong Park92315372021-04-02 08:45:46 +09001217func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001218 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001219 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001220 }
Jiyong Park92315372021-04-02 08:45:46 +09001221 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001222}
1223
Jiyong Park92315372021-04-02 08:45:46 +09001224func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1225 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001226}
1227
Colin Cross74d73e22017-08-02 11:05:49 -07001228func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001229 return &j.prebuilt
1230}
1231
Colin Cross74d73e22017-08-02 11:05:49 -07001232func (j *Import) PrebuiltSrcs() []string {
1233 return j.properties.Jars
1234}
1235
1236func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001237 return j.prebuilt.Name(j.ModuleBase.Name())
1238}
1239
Jiyong Park0b238752019-10-29 11:23:10 +09001240func (j *Import) Stem() string {
1241 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1242}
1243
Jiyong Park618922e2020-01-08 13:35:43 +09001244func (a *Import) JacocoReportClassesFile() android.Path {
1245 return nil
1246}
1247
Bill Peckhama41a6962021-01-11 10:58:54 -08001248func (j *Import) LintDepSets() LintDepSets {
1249 return LintDepSets{}
1250}
1251
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001252func (j *Import) getStrictUpdatabilityLinting() bool {
1253 return false
1254}
1255
1256func (j *Import) setStrictUpdatabilityLinting(bool) {
1257}
1258
Colin Cross74d73e22017-08-02 11:05:49 -07001259func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001260 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001261
1262 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001263 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001264 }
Colin Cross1e676be2016-10-12 14:38:15 -07001265}
1266
Colin Cross74d73e22017-08-02 11:05:49 -07001267func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001268 j.sdkVersion = j.SdkVersion(ctx)
1269 j.minSdkVersion = j.MinSdkVersion(ctx)
1270
Colin Cross56a83212020-09-15 18:30:11 -07001271 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1272 j.hideApexVariantFromMake = true
1273 }
1274
Colin Cross8a497952019-03-05 22:25:09 -08001275 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001276
Jiyong Park0b238752019-10-29 11:23:10 +09001277 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001278 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001279 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1280 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001281 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001282 inputFile := outputFile
1283 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1284 TransformJetifier(ctx, outputFile, inputFile)
1285 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001286 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001287 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001288
Liz Kammerd6c31d22020-08-05 15:40:41 -07001289 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00001290 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07001291
Jiyong Park1be96912018-05-28 18:02:19 +09001292 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001293 tag := ctx.OtherModuleDependencyTag(module)
1294
Colin Crossdcf71b22021-02-01 13:59:03 -08001295 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1296 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001297 switch tag {
1298 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001299 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001300 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001301 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001302 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001303 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001304 switch tag {
1305 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001306 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001307 }
1308 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001309
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001310 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00001311
1312 // Save away the `deapexer` module on which this depends, if any.
1313 if tag == android.DeapexerTag {
1314 deapexerModule = module
1315 }
Jiyong Park1be96912018-05-28 18:02:19 +09001316 })
1317
Nan Zhang4973ecf2018-08-10 13:42:12 -07001318 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001319 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001320 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001321 }
Jiyong Park19604de2020-03-24 16:44:11 +09001322
1323 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001324
Paul Duffin064b70c2020-11-02 17:32:38 +00001325 if ctx.Device() {
1326 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1327 // obtained from the associated deapexer module.
1328 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1329 if ai.ForPrebuiltApex {
1330 if deapexerModule == nil {
1331 // This should never happen as a variant for a prebuilt_apex is only created if the
Paul Duffinb17d0442021-05-05 12:07:00 +01001332 // deapexer module has been configured to export the dex implementation jar for this module.
Paul Duffin064b70c2020-11-02 17:32:38 +00001333 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
1334 j.Name(), ai.ApexVariationName)
Paul Duffinb17d0442021-05-05 12:07:00 +01001335 return
Paul Duffin064b70c2020-11-02 17:32:38 +00001336 }
1337
1338 // Get the path of the dex implementation jar from the `deapexer` module.
1339 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001340 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Paul Duffin9d67ca62021-02-03 20:06:33 +00001341 j.dexJarFile = dexOutputPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001342 j.dexJarInstallFile = android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
Paul Duffin74d18d12021-05-14 14:18:47 +01001343
1344 // Initialize the hiddenapi structure.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001345 j.initHiddenAPI(ctx, dexOutputPath, outputFile, nil)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001346 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001347 // This should never happen as a variant for a prebuilt_apex is only created if the
1348 // prebuilt_apex has been configured to export the java library dex file.
1349 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
1350 }
1351 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001352 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001353 if sdkDep.invalidVersion {
1354 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1355 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1356 } else if sdkDep.useFiles {
1357 // sdkDep.jar is actually equivalent to turbine header.jar.
1358 flags.classpath = append(flags.classpath, sdkDep.jars...)
1359 }
1360
1361 // Dex compilation
1362
1363 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
1364 if j.dexProperties.Uncompress_dex == nil {
1365 // If the value was not force-set by the user, use reasonable default based on the module.
1366 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1367 }
1368 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1369
Paul Duffin612e6102021-02-02 13:38:13 +00001370 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001371 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001372 if ctx.Failed() {
1373 return
1374 }
1375
Paul Duffin74d18d12021-05-14 14:18:47 +01001376 // Initialize the hiddenapi structure.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001377 j.initHiddenAPI(ctx, dexOutputFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001378
1379 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001380 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001381
1382 j.dexJarFile = dexOutputFile
Jeongik Chad5fe8782021-07-08 01:13:11 +09001383 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001384 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001385 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001386
1387 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1388 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1389 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1390 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1391 AidlIncludeDirs: j.exportAidlIncludeDirs,
1392 })
Colin Cross2fe66872015-03-30 17:20:39 -07001393}
1394
Paul Duffinaa55f742020-10-06 17:20:13 +01001395func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1396 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001397 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001398 return android.Paths{j.combinedClasspathFile}, nil
1399 default:
1400 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1401 }
1402}
1403
1404var _ android.OutputFileProducer = (*Import)(nil)
1405
Nan Zhanged19fc32017-10-19 13:06:22 -07001406func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001407 if j.combinedClasspathFile == nil {
1408 return nil
1409 }
Colin Cross37f6d792018-07-12 12:28:41 -07001410 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001411}
1412
Colin Cross331a1212018-08-15 20:40:52 -07001413func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001414 if j.combinedClasspathFile == nil {
1415 return nil
1416 }
Colin Cross331a1212018-08-15 20:40:52 -07001417 return android.Paths{j.combinedClasspathFile}
1418}
1419
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001420func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001421 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001422}
1423
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001424func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001425 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001426}
1427
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001428func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1429 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001430}
1431
Jiyong Park45bf82e2020-12-15 22:29:02 +09001432var _ android.ApexModule = (*Import)(nil)
1433
1434// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001435func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001436 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001437}
1438
Jiyong Park45bf82e2020-12-15 22:29:02 +09001439// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001440func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1441 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001442 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001443 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001444 return fmt.Errorf("min_sdk_version is not specified")
1445 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001446 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001447 return nil
1448 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001449 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1450 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001451 }
Jooyung Han749dc692020-04-15 11:03:39 +09001452 return nil
1453}
1454
Paul Duffinfef55002021-06-17 14:56:05 +01001455// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1456// java_sdk_library_import with the specified base module name requires to be exported from a
1457// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001458func requiredFilesFromPrebuiltApexForImport(name string) []string {
1459 // Add the dex implementation jar to the set of exported files.
1460 return []string{
1461 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001462 }
1463}
1464
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001465// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1466// the java library with the specified name.
1467func apexRootRelativePathToJavaLib(name string) string {
1468 return filepath.Join("javalib", name+".jar")
1469}
1470
Paul Duffinfef55002021-06-17 14:56:05 +01001471var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1472
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001473func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001474 name := j.BaseModuleName()
1475 return requiredFilesFromPrebuiltApexForImport(name)
1476}
1477
albaltai36ff7dc2018-12-25 14:35:23 +08001478// Add compile time check for interface implementation
1479var _ android.IDEInfo = (*Import)(nil)
1480var _ android.IDECustomizedModuleName = (*Import)(nil)
1481
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001482// Collect information for opening IDE project files in java/jdeps.go.
1483const (
1484 removedPrefix = "prebuilt_"
1485)
1486
1487func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1488 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1489}
1490
1491func (j *Import) IDECustomizedModuleName() string {
1492 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1493 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1494 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001495 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001496}
1497
Colin Cross74d73e22017-08-02 11:05:49 -07001498var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001499
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001500func (j *Import) IsInstallable() bool {
1501 return Bool(j.properties.Installable)
1502}
1503
1504var _ dexpreopterInterface = (*Import)(nil)
1505
Colin Cross1b16b0e2019-02-12 14:41:32 -08001506// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1507//
1508// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1509// compiled against an Android classpath.
1510//
1511// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1512// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001513func ImportFactory() android.Module {
1514 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001515
Liz Kammerd6c31d22020-08-05 15:40:41 -07001516 module.AddProperties(
1517 &module.properties,
1518 &module.dexer.dexProperties,
1519 )
Colin Cross74d73e22017-08-02 11:05:49 -07001520
Paul Duffin71b33cc2021-06-23 11:39:47 +01001521 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001522
Liz Kammerd6c31d22020-08-05 15:40:41 -07001523 module.dexProperties.Optimize.EnabledByDefault = false
1524
Colin Cross74d73e22017-08-02 11:05:49 -07001525 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001526 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001527 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001528 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001529 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001530}
1531
Colin Cross1b16b0e2019-02-12 14:41:32 -08001532// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1533// module.
1534//
1535// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1536// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001537func ImportFactoryHost() android.Module {
1538 module := &Import{}
1539
1540 module.AddProperties(&module.properties)
1541
1542 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001543 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001544 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001545 return module
1546}
1547
Colin Cross42be7612019-02-21 18:12:14 -08001548// dex_import module
1549
1550type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001551 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001552
1553 // set the name of the output
1554 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001555}
1556
1557type DexImport struct {
1558 android.ModuleBase
1559 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001560 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001561 prebuilt android.Prebuilt
1562
1563 properties DexImportProperties
1564
Colin Crossb014f072021-02-26 14:54:36 -08001565 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08001566
1567 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001568
1569 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001570}
1571
1572func (j *DexImport) Prebuilt() *android.Prebuilt {
1573 return &j.prebuilt
1574}
1575
1576func (j *DexImport) PrebuiltSrcs() []string {
1577 return j.properties.Jars
1578}
1579
1580func (j *DexImport) Name() string {
1581 return j.prebuilt.Name(j.ModuleBase.Name())
1582}
1583
Jiyong Park0b238752019-10-29 11:23:10 +09001584func (j *DexImport) Stem() string {
1585 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1586}
1587
Jiyong Park77acec62020-06-01 21:39:15 +09001588func (a *DexImport) JacocoReportClassesFile() android.Path {
1589 return nil
1590}
1591
Colin Cross08dca382020-07-21 20:31:17 -07001592func (a *DexImport) LintDepSets() LintDepSets {
1593 return LintDepSets{}
1594}
1595
Martin Stjernholm6d415272020-01-31 17:10:36 +00001596func (j *DexImport) IsInstallable() bool {
1597 return true
1598}
1599
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001600func (j *DexImport) getStrictUpdatabilityLinting() bool {
1601 return false
1602}
1603
1604func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1605}
1606
Colin Cross42be7612019-02-21 18:12:14 -08001607func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1608 if len(j.properties.Jars) != 1 {
1609 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1610 }
1611
Colin Cross56a83212020-09-15 18:30:11 -07001612 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1613 if !apexInfo.IsForPlatform() {
1614 j.hideApexVariantFromMake = true
1615 }
1616
Jiyong Park0b238752019-10-29 11:23:10 +09001617 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08001618 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1619
1620 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1621 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1622
1623 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001624 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001625
1626 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1627 rule.Temporary(temporary)
1628
1629 // use zip2zip to uncompress classes*.dex files
1630 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001631 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001632 FlagWithInput("-i ", inputJar).
1633 FlagWithOutput("-o ", temporary).
1634 FlagWithArg("-0 ", "'classes*.dex'")
1635
1636 // use zipalign to align uncompressed classes*.dex files
1637 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001638 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001639 Flag("-f").
1640 Text("4").
1641 Input(temporary).
1642 Output(dexOutputFile)
1643
1644 rule.DeleteTemporaryFiles()
1645
Colin Crossf1a035e2020-11-16 17:32:30 -08001646 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001647 } else {
1648 ctx.Build(pctx, android.BuildParams{
1649 Rule: android.Cp,
1650 Input: inputJar,
1651 Output: dexOutputFile,
1652 })
1653 }
1654
1655 j.dexJarFile = dexOutputFile
1656
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001657 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001658
Colin Cross56a83212020-09-15 18:30:11 -07001659 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001660 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1661 j.Stem()+".jar", dexOutputFile)
1662 }
Colin Cross42be7612019-02-21 18:12:14 -08001663}
1664
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00001665func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08001666 return j.dexJarFile
1667}
1668
Jiyong Park45bf82e2020-12-15 22:29:02 +09001669var _ android.ApexModule = (*DexImport)(nil)
1670
1671// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001672func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1673 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001674 // we don't check prebuilt modules for sdk_version
1675 return nil
1676}
1677
Colin Cross42be7612019-02-21 18:12:14 -08001678// dex_import imports a `.jar` file containing classes.dex files.
1679//
1680// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1681// to the device.
1682func DexImportFactory() android.Module {
1683 module := &DexImport{}
1684
1685 module.AddProperties(&module.properties)
1686
1687 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001688 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001689 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001690 return module
1691}
1692
Colin Cross89536d42017-07-07 14:35:50 -07001693//
1694// Defaults
1695//
1696type Defaults struct {
1697 android.ModuleBase
1698 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001699 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001700}
1701
Colin Cross1b16b0e2019-02-12 14:41:32 -08001702// java_defaults provides a set of properties that can be inherited by other java or android modules.
1703//
1704// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1705// property in the defaults module that exists in the depending module will be prepended to the depending module's
1706// value for that property.
1707//
1708// Example:
1709//
1710// java_defaults {
1711// name: "example_defaults",
1712// srcs: ["common/**/*.java"],
1713// javacflags: ["-Xlint:all"],
1714// aaptflags: ["--auto-add-overlay"],
1715// }
1716//
1717// java_library {
1718// name: "example",
1719// defaults: ["example_defaults"],
1720// srcs: ["example/**/*.java"],
1721// }
1722//
1723// is functionally identical to:
1724//
1725// java_library {
1726// name: "example",
1727// srcs: [
1728// "common/**/*.java",
1729// "example/**/*.java",
1730// ],
1731// javacflags: ["-Xlint:all"],
1732// }
Paul Duffin47357662019-12-05 14:07:14 +00001733func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001734 module := &Defaults{}
1735
Colin Cross89536d42017-07-07 14:35:50 -07001736 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001737 &CommonProperties{},
1738 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001739 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001740 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001741 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001742 &aaptProperties{},
1743 &androidLibraryProperties{},
1744 &appProperties{},
1745 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001746 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001747 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001748 &ImportProperties{},
1749 &AARImportProperties{},
1750 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001751 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001752 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001753 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001754 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001755 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001756 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001757 )
1758
1759 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001760 return module
1761}
Nan Zhangea568a42017-11-08 21:20:04 -08001762
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001763func kytheExtractJavaFactory() android.Singleton {
1764 return &kytheExtractJavaSingleton{}
1765}
1766
1767type kytheExtractJavaSingleton struct {
1768}
1769
1770func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1771 var xrefTargets android.Paths
1772 ctx.VisitAllModules(func(module android.Module) {
1773 if javaModule, ok := module.(xref); ok {
1774 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1775 }
1776 })
1777 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1778 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001779 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001780 }
1781}
1782
Nan Zhangea568a42017-11-08 21:20:04 -08001783var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001784var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001785var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001786var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001787
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001788// Add class loader context (CLC) of a given dependency to the current CLC.
1789func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1790 clcMap dexpreopt.ClassLoaderContextMap) {
1791
1792 dep, ok := depModule.(UsesLibraryDependency)
1793 if !ok {
1794 return
1795 }
1796
1797 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
1798 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
1799 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
1800 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
1801 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
1802 // the current CLC.
1803 var implicitSdkLib *string
1804 comp, isComp := depModule.(SdkLibraryComponentDependency)
1805 if isComp {
1806 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
1807 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
1808 }
1809 if implicitSdkLib == nil {
1810 if ulib, ok := depModule.(ProvidesUsesLib); ok {
1811 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001812 }
1813 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001814
1815 depTag := ctx.OtherModuleDependencyTag(depModule)
1816 if depTag == libTag || depTag == usesLibTag {
1817 // Ok, propagate <uses-library> through non-static library dependencies.
1818 } else if depTag == staticLibTag {
1819 // Propagate <uses-library> through static library dependencies, unless it is a component
1820 // library (such as stubs). Component libraries have a dependency on their SDK library,
1821 // which should not be pulled just because of a static component library.
1822 if implicitSdkLib != nil {
1823 return
1824 }
1825 } else {
1826 // Don't propagate <uses-library> for other dependency tags.
1827 return
1828 }
1829
1830 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00001831 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001832 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
1833 } else {
1834 depName := ctx.OtherModuleName(depModule)
1835 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1836 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001837}