blob: 94c12bdbb596212aa030bda11337ec714166ab7d [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 {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100222 DexJarBuildPath() OptionalDexJarPath
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
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100251
252 // SDK version in which the library appared as a standalone library.
253 sdkVersion int
254
255 // If the dependency is optional or required.
256 optional bool
257
258 // Whether this is an implicit dependency inferred by Soong, or an explicit one added via
259 // `uses_libs`/`optional_uses_libs` properties.
260 implicit bool
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100261}
262
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100263func makeUsesLibraryDependencyTag(sdkVersion int, optional bool, implicit bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100264 return usesLibraryDependencyTag{
265 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
266 sdkVersion: sdkVersion,
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100267 optional: optional,
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100268 implicit: implicit,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100269 }
270}
271
Jiyong Park8be103b2019-11-08 15:53:48 +0900272func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700273 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900274}
275
Colin Crossbe1da472017-07-07 15:59:46 -0700276var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800277 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
278 staticLibTag = dependencyTag{name: "staticlib"}
279 libTag = dependencyTag{name: "javalib"}
280 java9LibTag = dependencyTag{name: "java9lib"}
281 pluginTag = dependencyTag{name: "plugin"}
282 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
283 exportedPluginTag = dependencyTag{name: "exported-plugin"}
284 bootClasspathTag = dependencyTag{name: "bootclasspath"}
285 systemModulesTag = dependencyTag{name: "system modules"}
286 frameworkResTag = dependencyTag{name: "framework-res"}
287 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
288 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700289 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800290 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
291 certificateTag = dependencyTag{name: "certificate"}
292 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
293 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
294 jniLibTag = dependencyTag{name: "jnilib"}
295 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
296 jniInstallTag = installDependencyTag{name: "jni install"}
297 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700298)
Colin Cross2fe66872015-03-30 17:20:39 -0700299
Jiyong Park83dc74b2020-01-14 18:38:44 +0900300func IsLibDepTag(depTag blueprint.DependencyTag) bool {
301 return depTag == libTag
302}
303
304func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
305 return depTag == staticLibTag
306}
307
Colin Crossfc3674a2017-09-18 17:41:52 -0700308type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100309 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700310
Colin Cross6cef4812019-10-17 14:23:50 -0700311 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
312 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100313
314 // The default system modules to use. Will be an empty string if no system
315 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700316 systemModules string
317
Pete Gilline3d44b22020-06-29 11:28:51 +0100318 // The modules that will be added to the classpath regardless of the Java language level targeted
319 classpath []string
320
Colin Cross6cef4812019-10-17 14:23:50 -0700321 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100322 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700323 java9Classpath []string
324
Colin Crossa97c5d32018-03-28 14:58:31 -0700325 frameworkResModule string
326
Colin Cross86a60ae2018-05-29 14:44:55 -0700327 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700328 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100329
330 noStandardLibs, noFrameworksLibs bool
331}
332
333func (s sdkDep) hasStandardLibs() bool {
334 return !s.noStandardLibs
335}
336
337func (s sdkDep) hasFrameworkLibs() bool {
338 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700339}
340
Colin Crossa4f08812018-10-02 22:03:40 -0700341type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700342 name string
343 path android.Path
344 target android.Target
345 coverageFile android.OptionalPath
346 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700347}
348
Jiyong Parkf1691d22021-03-29 20:11:58 +0900349func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700350 sdkDep := decodeSdkDep(ctx, sdkContext)
351 if sdkDep.useModule {
352 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
353 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
354 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
355 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
356 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
357 }
358 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
359 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
360 }
361 }
362 if sdkDep.systemModules != "" {
363 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
364 }
365}
366
Colin Cross32f676a2017-09-06 13:41:06 -0700367type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800368 classpath classpath
369 java9Classpath classpath
370 bootClasspath classpath
371 processorPath classpath
372 errorProneProcessorPath classpath
373 processorClasses []string
374 staticJars android.Paths
375 staticHeaderJars android.Paths
376 staticResourceJars android.Paths
377 aidlIncludeDirs android.Paths
378 srcs android.Paths
379 srcJars android.Paths
380 systemModules *systemModules
381 aidlPreprocess android.OptionalPath
382 kotlinStdlib android.Paths
383 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700384 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800385
386 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700387}
Colin Cross2fe66872015-03-30 17:20:39 -0700388
Colin Cross54250902017-12-05 09:28:08 -0800389func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
390 for _, f := range dep.Srcs() {
391 if f.Ext() != ".jar" {
392 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
393 ctx.OtherModuleName(dep.(blueprint.Module)))
394 }
395 }
396}
397
Jiyong Parkf1691d22021-03-29 20:11:58 +0900398func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700399 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700400 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700401 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900402 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700403 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700404 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700405 }
Nan Zhang357466b2018-04-17 17:38:36 -0700406}
407
Colin Cross1e743852019-10-28 11:37:20 -0700408type javaVersion int
409
410const (
411 JAVA_VERSION_UNSUPPORTED = 0
412 JAVA_VERSION_6 = 6
413 JAVA_VERSION_7 = 7
414 JAVA_VERSION_8 = 8
415 JAVA_VERSION_9 = 9
416)
417
418func (v javaVersion) String() string {
419 switch v {
420 case JAVA_VERSION_6:
421 return "1.6"
422 case JAVA_VERSION_7:
423 return "1.7"
424 case JAVA_VERSION_8:
425 return "1.8"
426 case JAVA_VERSION_9:
427 return "1.9"
428 default:
429 return "unsupported"
430 }
431}
432
433// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
434func (v javaVersion) usesJavaModules() bool {
435 return v >= 9
436}
437
438func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100439 switch javaVersion {
440 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700441 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100442 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700443 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100444 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700445 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100446 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700447 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100448 case "10", "11":
449 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700450 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100451 default:
452 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700453 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100454 }
455}
456
Colin Cross2fe66872015-03-30 17:20:39 -0700457//
458// Java libraries (.jar file)
459//
460
Colin Crossf506d872017-07-19 15:53:04 -0700461type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700462 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700463
464 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700465}
466
Jiyong Park45bf82e2020-12-15 22:29:02 +0900467var _ android.ApexModule = (*Library)(nil)
468
satayevd604b212021-07-21 14:23:52 +0100469// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100470type PermittedPackagesForUpdatableBootJars interface {
471 PermittedPackagesForUpdatableBootJars() []string
472}
473
474var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
475
476func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
477 return j.properties.Permitted_packages
478}
479
Colin Cross42be7612019-02-21 18:12:14 -0800480func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000481 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700482 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000483 return true
484 }
485
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000486 // Store uncompressed (and do not strip) dex files from boot class path jars.
487 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
488 return true
489 }
490
491 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000492 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000493 return true
494 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800495 if ctx.Config().UncompressPrivAppDex() &&
496 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
497 return true
498 }
499
Colin Cross2fc72f62018-12-21 12:59:54 -0800500 return false
501}
502
Colin Crossf506d872017-07-19 15:53:04 -0700503func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900504 j.sdkVersion = j.SdkVersion(ctx)
505 j.minSdkVersion = j.MinSdkVersion(ctx)
506
Colin Cross56a83212020-09-15 18:30:11 -0700507 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
508 if !apexInfo.IsForPlatform() {
509 j.hideApexVariantFromMake = true
510 }
511
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100512 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000513 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
514 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800515 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700516 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100517 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700518 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100519 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700520 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100521 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700522 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700523
bralee1fbf4402020-05-21 10:11:59 +0800524 // Collect the module directory for IDE info in java/jdeps.go.
525 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
526
Colin Cross56a83212020-09-15 18:30:11 -0700527 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900528 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700529 var extraInstallDeps android.Paths
530 if j.InstallMixin != nil {
531 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
532 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700533 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900534 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700535 }
Dan Willemsen8e6b3712021-09-20 23:11:24 -0700536
537 if ctx.Windows() {
538 j.HideFromMake()
539 }
Colin Crossb7a63242015-04-16 14:09:14 -0700540}
541
Colin Crossf506d872017-07-19 15:53:04 -0700542func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700543 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100544 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700545}
546
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000547const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000548 aidlIncludeDir = "aidl"
549 javaDir = "java"
550 jarFileSuffix = ".jar"
551 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000552)
553
Paul Duffina0dbf432019-12-05 11:25:53 +0000554// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000555func sdkSnapshotFilePathForJar(osPrefix, name string) string {
556 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000557}
558
Paul Duffina04c1072020-03-02 10:16:35 +0000559func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
560 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000561}
562
Paul Duffin13879572019-11-28 14:31:38 +0000563type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000564 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000565
566 // Function to retrieve the appropriate output jar (implementation or header) from
567 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000568 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
569
570 // Function to compute the snapshot relative path to which the named library's
571 // jar should be copied.
572 snapshotPathGetter func(osPrefix, name string) string
573
574 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
575 // files like aidl files should also be copied.
576 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000577}
578
Paul Duffindb170e42020-12-08 17:48:25 +0000579const (
580 onlyCopyJarToSnapshot = true
581 copyEverythingToSnapshot = false
582)
583
Paul Duffin296701e2021-07-14 10:29:36 +0100584func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
585 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000586}
587
588func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
589 _, ok := module.(*Library)
590 return ok
591}
592
Paul Duffin3a4eb502020-03-19 16:11:18 +0000593func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
594 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000595}
Paul Duffina0dbf432019-12-05 11:25:53 +0000596
Paul Duffin14eb4672020-03-02 11:33:02 +0000597func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000598 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000599}
600
601type librarySdkMemberProperties struct {
602 android.SdkMemberPropertiesBase
603
Paul Duffin864e1b42020-05-06 10:23:19 +0100604 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000605 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100606
607 // The list of permitted packages that need to be passed to the prebuilts as they are used to
608 // create the updatable-bcp-packages.txt file.
609 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000610}
611
Paul Duffin3a4eb502020-03-19 16:11:18 +0000612func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000613 j := variant.(*Library)
614
Paul Duffindb170e42020-12-08 17:48:25 +0000615 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
616
Paul Duffina551a1c2020-03-17 21:04:24 +0000617 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100618
619 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000620}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000621
Paul Duffin3a4eb502020-03-19 16:11:18 +0000622func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000623 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000624
Paul Duffindb170e42020-12-08 17:48:25 +0000625 memberType := ctx.MemberType().(*librarySdkMemberType)
626
Paul Duffina551a1c2020-03-17 21:04:24 +0000627 exportedJar := p.JarToExport
628 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000629 // Delegate the creation of the snapshot relative path to the member type.
630 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
631
632 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000633 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
634
Paul Duffina551a1c2020-03-17 21:04:24 +0000635 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
636 }
637
Paul Duffin869de142021-07-15 14:14:41 +0100638 if len(p.PermittedPackages) > 0 {
639 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
640 }
641
Paul Duffindb170e42020-12-08 17:48:25 +0000642 // Do not copy anything else to the snapshot.
643 if memberType.onlyCopyJarToSnapshot {
644 return
645 }
646
Paul Duffina551a1c2020-03-17 21:04:24 +0000647 aidlIncludeDirs := p.AidlIncludeDirs
648 if len(aidlIncludeDirs) != 0 {
649 sdkModuleContext := ctx.SdkModuleContext()
650 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000651 // TODO(jiyong): copy parcelable declarations only
652 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
653 for _, file := range aidlFiles {
654 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
655 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000656 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000657
Paul Duffina551a1c2020-03-17 21:04:24 +0000658 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000659 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000660}
661
Colin Cross1b16b0e2019-02-12 14:41:32 -0800662// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
663//
664// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
665// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
666// as a `static_libs` dependency of another module.
667//
668// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
669// a device.
670//
671// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
672// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700673func LibraryFactory() android.Module {
674 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700675
Colin Crossce6734e2020-06-15 16:09:53 -0700676 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700677
Paul Duffin71b33cc2021-06-23 11:39:47 +0100678 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100679
Jiyong Park7f7766d2019-07-25 22:02:35 +0900680 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900681 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900682 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700683 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700684}
685
Colin Cross1b16b0e2019-02-12 14:41:32 -0800686// java_library_static is an obsolete alias for java_library.
687func LibraryStaticFactory() android.Module {
688 return LibraryFactory()
689}
690
691// java_library_host builds and links sources into a `.jar` file for the host.
692//
693// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
694// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700695func LibraryHostFactory() android.Module {
696 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700697
Colin Crossce6734e2020-06-15 16:09:53 -0700698 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700699
Colin Cross9ae1b922018-06-26 17:59:05 -0700700 module.Module.properties.Installable = proptools.BoolPtr(true)
701
Jiyong Park7f7766d2019-07-25 22:02:35 +0900702 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100703 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900704 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700705 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700706}
707
708//
Colin Crossb628ea52018-08-14 16:42:33 -0700709// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700710//
711
Dan Shi95d19422020-08-15 12:24:26 -0700712// Test option struct.
713type TestOptions struct {
714 // a list of extra test configuration files that should be installed with the module.
715 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800716
717 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
718 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700719}
720
Colin Cross05638fc2018-04-09 18:40:24 -0700721type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700722 // list of compatibility suites (for example "cts", "vts") that the module should be
723 // installed into.
724 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700725
726 // the name of the test configuration (for example "AndroidTest.xml") that should be
727 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800728 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700729
Jack He33338892018-09-19 02:21:28 -0700730 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
731 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800732 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700733
Colin Crossd96ca352018-08-10 16:06:24 -0700734 // list of files or filegroup modules that provide data that should be installed alongside
735 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900736 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700737
738 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
739 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
740 // explicitly.
741 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800742
743 // Add parameterized mainline modules to auto generated test config. The options will be
744 // handled by TradeFed to do downloading and installing the specified modules on the device.
745 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700746
747 // Test options.
748 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800749
750 // Names of modules containing JNI libraries that should be installed alongside the test.
751 Jni_libs []string
Colin Cross05638fc2018-04-09 18:40:24 -0700752}
753
Liz Kammerdd849a82020-06-12 16:38:45 -0700754type hostTestProperties struct {
755 // list of native binary modules that should be installed alongside the test
756 Data_native_bins []string `android:"arch_variant"`
757}
758
Paul Duffin42df1442019-03-20 12:45:53 +0000759type testHelperLibraryProperties struct {
760 // list of compatibility suites (for example "cts", "vts") that the module should be
761 // installed into.
762 Test_suites []string `android:"arch_variant"`
763}
764
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000765type prebuiltTestProperties struct {
766 // list of compatibility suites (for example "cts", "vts") that the module should be
767 // installed into.
768 Test_suites []string `android:"arch_variant"`
769
770 // the name of the test configuration (for example "AndroidTest.xml") that should be
771 // installed with the module.
772 Test_config *string `android:"path,arch_variant"`
773}
774
Colin Cross05638fc2018-04-09 18:40:24 -0700775type Test struct {
776 Library
777
778 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700779
Dan Shi95d19422020-08-15 12:24:26 -0700780 testConfig android.Path
781 extraTestConfigs android.Paths
782 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700783}
784
Liz Kammerdd849a82020-06-12 16:38:45 -0700785type TestHost struct {
786 Test
787
788 testHostProperties hostTestProperties
789}
790
Paul Duffin42df1442019-03-20 12:45:53 +0000791type TestHelperLibrary struct {
792 Library
793
794 testHelperLibraryProperties testHelperLibraryProperties
795}
796
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000797type JavaTestImport struct {
798 Import
799
800 prebuiltTestProperties prebuiltTestProperties
801
802 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700803 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000804}
805
Liz Kammerdd849a82020-06-12 16:38:45 -0700806func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
807 if len(j.testHostProperties.Data_native_bins) > 0 {
808 for _, target := range ctx.MultiTargets() {
809 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
810 }
811 }
812
Colin Crossf8d9c492021-01-26 11:01:43 -0800813 if len(j.testProperties.Jni_libs) > 0 {
814 for _, target := range ctx.MultiTargets() {
815 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
816 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
817 }
818 }
819
Liz Kammerdd849a82020-06-12 16:38:45 -0700820 j.deps(ctx)
821}
822
Yuexi Ma627263f2021-03-04 13:47:56 -0800823func (j *TestHost) AddExtraResource(p android.Path) {
824 j.extraResources = append(j.extraResources, p)
825}
826
Colin Cross303e21f2018-08-07 16:49:25 -0700827func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000828 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
829 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700830 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000831 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
832 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700833 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800834 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700835
Colin Cross8a497952019-03-05 22:25:09 -0800836 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700837
Dan Shi95d19422020-08-15 12:24:26 -0700838 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
839
Liz Kammerdd849a82020-06-12 16:38:45 -0700840 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
841 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
842 })
843
Colin Crossf8d9c492021-01-26 11:01:43 -0800844 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
845 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
846 if sharedLibInfo.SharedLibrary != nil {
847 // Copy to an intermediate output directory to append "lib[64]" to the path,
848 // so that it's compatible with the default rpath values.
849 var relPath string
850 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
851 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
852 } else {
853 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
854 }
855 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
856 ctx.Build(pctx, android.BuildParams{
857 Rule: android.Cp,
858 Input: sharedLibInfo.SharedLibrary,
859 Output: relocatedLib,
860 })
861 j.data = append(j.data, relocatedLib)
862 } else {
863 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
864 }
865 })
866
Colin Cross303e21f2018-08-07 16:49:25 -0700867 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700868}
869
Paul Duffin42df1442019-03-20 12:45:53 +0000870func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
871 j.Library.GenerateAndroidBuildActions(ctx)
872}
873
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000874func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
875 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800876 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000877
878 j.Import.GenerateAndroidBuildActions(ctx)
879}
880
881type testSdkMemberType struct {
882 android.SdkMemberTypeBase
883}
884
Paul Duffin296701e2021-07-14 10:29:36 +0100885func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
886 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000887}
888
889func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
890 _, ok := module.(*Test)
891 return ok
892}
893
Paul Duffin3a4eb502020-03-19 16:11:18 +0000894func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
895 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000896}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000897
Paul Duffin14eb4672020-03-02 11:33:02 +0000898func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
899 return &testSdkMemberProperties{}
900}
901
902type testSdkMemberProperties struct {
903 android.SdkMemberPropertiesBase
904
Paul Duffina551a1c2020-03-17 21:04:24 +0000905 JarToExport android.Path
906 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000907}
908
Paul Duffin3a4eb502020-03-19 16:11:18 +0000909func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000910 test := variant.(*Test)
911
912 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000913 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000914 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000915 }
916
Paul Duffina551a1c2020-03-17 21:04:24 +0000917 p.JarToExport = implementationJars[0]
918 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000919}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000920
Paul Duffin3a4eb502020-03-19 16:11:18 +0000921func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000922 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000923
Paul Duffina551a1c2020-03-17 21:04:24 +0000924 exportedJar := p.JarToExport
925 if exportedJar != nil {
926 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
927 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000928
929 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000930 }
931
932 testConfig := p.TestConfig
933 if testConfig != nil {
934 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
935 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000936 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
937 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000938}
939
Colin Cross1b16b0e2019-02-12 14:41:32 -0800940// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
941// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
942//
943// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
944// compiled against the device bootclasspath.
945//
946// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
947// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700948func TestFactory() android.Module {
949 module := &Test{}
950
Colin Crossce6734e2020-06-15 16:09:53 -0700951 module.addHostAndDeviceProperties()
952 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700953
Colin Cross9ae1b922018-06-26 17:59:05 -0700954 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800955 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700956 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700957
Paul Duffinb6b89a42021-05-06 16:33:43 +0100958 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -0700959 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700960 return module
961}
962
Paul Duffin42df1442019-03-20 12:45:53 +0000963// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
964func TestHelperLibraryFactory() android.Module {
965 module := &TestHelperLibrary{}
966
Colin Crossce6734e2020-06-15 16:09:53 -0700967 module.addHostAndDeviceProperties()
968 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +0000969
Colin Cross9a4abed2019-04-24 13:19:28 -0700970 module.Module.properties.Installable = proptools.BoolPtr(true)
971 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700972 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -0700973
Paul Duffin42df1442019-03-20 12:45:53 +0000974 InitJavaModule(module, android.HostAndDeviceSupported)
975 return module
976}
977
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000978// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
979// and makes sure that it is added to the appropriate test suite.
980//
981// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
982// compiled against an Android classpath.
983//
984// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
985// for host modules.
986func JavaTestImportFactory() android.Module {
987 module := &JavaTestImport{}
988
989 module.AddProperties(
990 &module.Import.properties,
991 &module.prebuiltTestProperties)
992
993 module.Import.properties.Installable = proptools.BoolPtr(true)
994
995 android.InitPrebuiltModule(module, &module.properties.Jars)
996 android.InitApexModule(module)
997 android.InitSdkAwareModule(module)
998 InitJavaModule(module, android.HostAndDeviceSupported)
999 return module
1000}
1001
Colin Cross1b16b0e2019-02-12 14:41:32 -08001002// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1003// allow running the test with `atest` or a `TEST_MAPPING` file.
1004//
1005// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1006// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001007func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001008 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001009
Colin Crossce6734e2020-06-15 16:09:53 -07001010 module.addHostProperties()
1011 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001012 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001013
Yuexi Ma627263f2021-03-04 13:47:56 -08001014 InitTestHost(
1015 module,
1016 proptools.BoolPtr(true),
1017 nil,
1018 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001019
Liz Kammerdd849a82020-06-12 16:38:45 -07001020 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001021
Colin Cross05638fc2018-04-09 18:40:24 -07001022 return module
1023}
1024
Yuexi Ma627263f2021-03-04 13:47:56 -08001025func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1026 th.properties.Installable = installable
1027 th.testProperties.Auto_gen_config = autoGenConfig
1028 th.testProperties.Test_suites = testSuites
1029}
1030
Colin Cross05638fc2018-04-09 18:40:24 -07001031//
Colin Cross2fe66872015-03-30 17:20:39 -07001032// Java Binaries (.jar file plus wrapper script)
1033//
1034
Colin Crossf506d872017-07-19 15:53:04 -07001035type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001036 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001037 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001038
1039 // Name of the class containing main to be inserted into the manifest as Main-Class.
1040 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001041
1042 // Names of modules containing JNI libraries that should be installed alongside the host
1043 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001044 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001045}
1046
Colin Crossf506d872017-07-19 15:53:04 -07001047type Binary struct {
1048 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001049
Colin Crossf506d872017-07-19 15:53:04 -07001050 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001051
Colin Cross6b4a32d2017-12-05 13:42:45 -08001052 isWrapperVariant bool
1053
Colin Crossc3315992017-12-08 19:12:36 -08001054 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001055 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001056}
1057
Alex Light24237172017-10-26 09:46:21 -07001058func (j *Binary) HostToolPath() android.OptionalPath {
1059 return android.OptionalPathForPath(j.binaryFile)
1060}
1061
Colin Crossf506d872017-07-19 15:53:04 -07001062func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001063 if ctx.Arch().ArchType == android.Common {
1064 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001065 if j.binaryProperties.Main_class != nil {
1066 if j.properties.Manifest != nil {
1067 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1068 }
1069 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1070 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1071 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1072 }
1073
Colin Cross6b4a32d2017-12-05 13:42:45 -08001074 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001075 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001076 // Handle the binary wrapper
1077 j.isWrapperVariant = true
1078
Colin Cross366938f2017-12-11 16:29:02 -08001079 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001080 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001081 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001082 if ctx.Windows() {
1083 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1084 }
1085
Colin Cross6b4a32d2017-12-05 13:42:45 -08001086 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1087 }
1088
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001089 ext := ""
1090 if ctx.Windows() {
1091 ext = ".bat"
1092 }
1093
Colin Crossc179ea62020-10-09 10:54:15 -07001094 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001095 // of the wrapper variant, which will include the common variant's jar file and any JNI
1096 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001097 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001098 ctx.ModuleName()+ext, j.wrapperFile)
1099 }
1100
1101 if ctx.Windows() {
1102 j.HideFromMake()
Nan Zhang3c807db2017-11-03 14:53:31 -07001103 }
Colin Cross2fe66872015-03-30 17:20:39 -07001104}
1105
Colin Crossf506d872017-07-19 15:53:04 -07001106func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001107 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001108 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001109 }
1110 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001111 // These dependencies ensure the host installation rules will install the jar file and
1112 // the jni libraries when the wrapper is installed.
1113 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1114 ctx.AddVariationDependencies(
1115 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1116 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001117 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001118}
1119
Colin Cross1b16b0e2019-02-12 14:41:32 -08001120// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1121// as well.
1122//
1123// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1124// compiled against the device bootclasspath.
1125//
1126// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1127// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001128func BinaryFactory() android.Module {
1129 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001130
Colin Crossce6734e2020-06-15 16:09:53 -07001131 module.addHostAndDeviceProperties()
1132 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001133
Colin Cross9ae1b922018-06-26 17:59:05 -07001134 module.Module.properties.Installable = proptools.BoolPtr(true)
1135
Colin Cross6b4a32d2017-12-05 13:42:45 -08001136 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1137 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001138 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001139}
1140
Colin Cross1b16b0e2019-02-12 14:41:32 -08001141// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1142//
1143// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1144// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001145func BinaryHostFactory() android.Module {
1146 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001147
Colin Crossce6734e2020-06-15 16:09:53 -07001148 module.addHostProperties()
1149 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001150
Colin Cross9ae1b922018-06-26 17:59:05 -07001151 module.Module.properties.Installable = proptools.BoolPtr(true)
1152
Colin Cross6b4a32d2017-12-05 13:42:45 -08001153 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1154 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001155 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001156}
1157
1158//
1159// Java prebuilts
1160//
1161
Colin Cross74d73e22017-08-02 11:05:49 -07001162type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001163 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001164
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001165 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1166 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001167 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001168
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001169 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1170 // specified.
1171 Min_sdk_version *string
1172
Colin Cross535e2cf2017-10-20 17:57:49 -07001173 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001174
Paul Duffin869de142021-07-15 14:14:41 +01001175 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001176 Permitted_packages []string
1177
Jiyong Park1be96912018-05-28 18:02:19 +09001178 // List of shared java libs that this module has dependencies to
1179 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001180
1181 // List of files to remove from the jar file(s)
1182 Exclude_files []string
1183
1184 // List of directories to remove from the jar file(s)
1185 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001186
1187 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001188 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001189
1190 // set the name of the output
1191 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001192
1193 Aidl struct {
1194 // directories that should be added as include directories for any aidl sources of modules
1195 // that depend on this module, as well as to aidl for this module.
1196 Export_include_dirs []string
1197 }
Colin Cross74d73e22017-08-02 11:05:49 -07001198}
1199
1200type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001201 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001202 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001203 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001204 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001205 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001206
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001207 // Functionality common to Module and Import.
1208 embeddableInModuleAndImport
1209
Liz Kammerd6c31d22020-08-05 15:40:41 -07001210 hiddenAPI
1211 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001212 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001213
Colin Cross74d73e22017-08-02 11:05:49 -07001214 properties ImportProperties
1215
Liz Kammerd6c31d22020-08-05 15:40:41 -07001216 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001217 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001218 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001219
Colin Cross0a6e0072017-08-30 14:24:55 -07001220 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001221 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001222 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001223
1224 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001225
1226 sdkVersion android.SdkSpec
1227 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001228}
1229
Paul Duffin630b11e2021-07-15 13:35:26 +01001230var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1231
1232func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1233 return j.properties.Permitted_packages
1234}
1235
Jiyong Park92315372021-04-02 08:45:46 +09001236func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1237 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001238}
1239
Jiyong Parkf1691d22021-03-29 20:11:58 +09001240func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001241 return "none"
1242}
1243
Jiyong Park92315372021-04-02 08:45:46 +09001244func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001245 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001246 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001247 }
Jiyong Park92315372021-04-02 08:45:46 +09001248 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001249}
1250
Jiyong Park92315372021-04-02 08:45:46 +09001251func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1252 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001253}
1254
Colin Cross74d73e22017-08-02 11:05:49 -07001255func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001256 return &j.prebuilt
1257}
1258
Colin Cross74d73e22017-08-02 11:05:49 -07001259func (j *Import) PrebuiltSrcs() []string {
1260 return j.properties.Jars
1261}
1262
1263func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001264 return j.prebuilt.Name(j.ModuleBase.Name())
1265}
1266
Jiyong Park0b238752019-10-29 11:23:10 +09001267func (j *Import) Stem() string {
1268 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1269}
1270
Jiyong Park618922e2020-01-08 13:35:43 +09001271func (a *Import) JacocoReportClassesFile() android.Path {
1272 return nil
1273}
1274
Bill Peckhama41a6962021-01-11 10:58:54 -08001275func (j *Import) LintDepSets() LintDepSets {
1276 return LintDepSets{}
1277}
1278
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001279func (j *Import) getStrictUpdatabilityLinting() bool {
1280 return false
1281}
1282
1283func (j *Import) setStrictUpdatabilityLinting(bool) {
1284}
1285
Colin Cross74d73e22017-08-02 11:05:49 -07001286func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001287 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001288
1289 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001290 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001291 }
Colin Cross1e676be2016-10-12 14:38:15 -07001292}
1293
Colin Cross74d73e22017-08-02 11:05:49 -07001294func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001295 j.sdkVersion = j.SdkVersion(ctx)
1296 j.minSdkVersion = j.MinSdkVersion(ctx)
1297
Colin Cross56a83212020-09-15 18:30:11 -07001298 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1299 j.hideApexVariantFromMake = true
1300 }
1301
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001302 if ctx.Windows() {
1303 j.HideFromMake()
1304 }
1305
Colin Cross8a497952019-03-05 22:25:09 -08001306 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001307
Jiyong Park0b238752019-10-29 11:23:10 +09001308 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001309 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001310 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1311 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001312 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001313 inputFile := outputFile
1314 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1315 TransformJetifier(ctx, outputFile, inputFile)
1316 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001317 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001318 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001319
Liz Kammerd6c31d22020-08-05 15:40:41 -07001320 var flags javaBuilderFlags
1321
Jiyong Park1be96912018-05-28 18:02:19 +09001322 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001323 tag := ctx.OtherModuleDependencyTag(module)
1324
Colin Crossdcf71b22021-02-01 13:59:03 -08001325 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1326 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001327 switch tag {
1328 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001329 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001330 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001331 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001332 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001333 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001334 switch tag {
1335 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001336 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001337 }
1338 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001339
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001340 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001341 })
1342
Nan Zhang4973ecf2018-08-10 13:42:12 -07001343 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001344 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001345 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001346 }
Jiyong Park19604de2020-03-24 16:44:11 +09001347
1348 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001349
Paul Duffin064b70c2020-11-02 17:32:38 +00001350 if ctx.Device() {
1351 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1352 // obtained from the associated deapexer module.
1353 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1354 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001355 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001356 di := android.FindDeapexerProviderForModule(ctx)
1357 if di == nil {
1358 return // An error has been reported by FindDeapexerProviderForModule.
1359 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001360 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001361 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1362 j.dexJarFile = dexJarFile
Jeongik Chad5fe8782021-07-08 01:13:11 +09001363 j.dexJarInstallFile = android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
Paul Duffin74d18d12021-05-14 14:18:47 +01001364
1365 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001366 j.initHiddenAPI(ctx, dexJarFile, outputFile, nil)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001367 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001368 // This should never happen as a variant for a prebuilt_apex is only created if the
1369 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001370 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001371 }
1372 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001373 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001374 if sdkDep.invalidVersion {
1375 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1376 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1377 } else if sdkDep.useFiles {
1378 // sdkDep.jar is actually equivalent to turbine header.jar.
1379 flags.classpath = append(flags.classpath, sdkDep.jars...)
1380 }
1381
1382 // Dex compilation
1383
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001384 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1385 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Paul Duffin064b70c2020-11-02 17:32:38 +00001386 if j.dexProperties.Uncompress_dex == nil {
1387 // If the value was not force-set by the user, use reasonable default based on the module.
1388 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1389 }
1390 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1391
Paul Duffin612e6102021-02-02 13:38:13 +00001392 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001393 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001394 if ctx.Failed() {
1395 return
1396 }
1397
Paul Duffin74d18d12021-05-14 14:18:47 +01001398 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001399 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001400
1401 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001402 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001403
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001404 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001405 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001406 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001407 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001408
1409 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1410 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1411 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1412 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1413 AidlIncludeDirs: j.exportAidlIncludeDirs,
1414 })
Colin Cross2fe66872015-03-30 17:20:39 -07001415}
1416
Paul Duffinaa55f742020-10-06 17:20:13 +01001417func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1418 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001419 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001420 return android.Paths{j.combinedClasspathFile}, nil
1421 default:
1422 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1423 }
1424}
1425
1426var _ android.OutputFileProducer = (*Import)(nil)
1427
Nan Zhanged19fc32017-10-19 13:06:22 -07001428func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001429 if j.combinedClasspathFile == nil {
1430 return nil
1431 }
Colin Cross37f6d792018-07-12 12:28:41 -07001432 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001433}
1434
Colin Cross331a1212018-08-15 20:40:52 -07001435func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001436 if j.combinedClasspathFile == nil {
1437 return nil
1438 }
Colin Cross331a1212018-08-15 20:40:52 -07001439 return android.Paths{j.combinedClasspathFile}
1440}
1441
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001442func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001443 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001444}
1445
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001446func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001447 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001448}
1449
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001450func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1451 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001452}
1453
Jiyong Park45bf82e2020-12-15 22:29:02 +09001454var _ android.ApexModule = (*Import)(nil)
1455
1456// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001457func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001458 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001459}
1460
Jiyong Park45bf82e2020-12-15 22:29:02 +09001461// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001462func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1463 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001464 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001465 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001466 return fmt.Errorf("min_sdk_version is not specified")
1467 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001468 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001469 return nil
1470 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001471 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1472 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001473 }
Jooyung Han749dc692020-04-15 11:03:39 +09001474 return nil
1475}
1476
Paul Duffinfef55002021-06-17 14:56:05 +01001477// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1478// java_sdk_library_import with the specified base module name requires to be exported from a
1479// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001480func requiredFilesFromPrebuiltApexForImport(name string) []string {
1481 // Add the dex implementation jar to the set of exported files.
1482 return []string{
1483 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001484 }
1485}
1486
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001487// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1488// the java library with the specified name.
1489func apexRootRelativePathToJavaLib(name string) string {
1490 return filepath.Join("javalib", name+".jar")
1491}
1492
Paul Duffinfef55002021-06-17 14:56:05 +01001493var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1494
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001495func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001496 name := j.BaseModuleName()
1497 return requiredFilesFromPrebuiltApexForImport(name)
1498}
1499
albaltai36ff7dc2018-12-25 14:35:23 +08001500// Add compile time check for interface implementation
1501var _ android.IDEInfo = (*Import)(nil)
1502var _ android.IDECustomizedModuleName = (*Import)(nil)
1503
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001504// Collect information for opening IDE project files in java/jdeps.go.
1505const (
1506 removedPrefix = "prebuilt_"
1507)
1508
1509func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1510 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1511}
1512
1513func (j *Import) IDECustomizedModuleName() string {
1514 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1515 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1516 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001517 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001518}
1519
Colin Cross74d73e22017-08-02 11:05:49 -07001520var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001521
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001522func (j *Import) IsInstallable() bool {
1523 return Bool(j.properties.Installable)
1524}
1525
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001526var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001527
Colin Cross1b16b0e2019-02-12 14:41:32 -08001528// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1529//
1530// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1531// compiled against an Android classpath.
1532//
1533// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1534// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001535func ImportFactory() android.Module {
1536 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001537
Liz Kammerd6c31d22020-08-05 15:40:41 -07001538 module.AddProperties(
1539 &module.properties,
1540 &module.dexer.dexProperties,
1541 )
Colin Cross74d73e22017-08-02 11:05:49 -07001542
Paul Duffin71b33cc2021-06-23 11:39:47 +01001543 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001544
Liz Kammerd6c31d22020-08-05 15:40:41 -07001545 module.dexProperties.Optimize.EnabledByDefault = false
1546
Colin Cross74d73e22017-08-02 11:05:49 -07001547 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001548 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001549 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001550 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001551 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001552}
1553
Colin Cross1b16b0e2019-02-12 14:41:32 -08001554// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1555// module.
1556//
1557// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1558// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001559func ImportFactoryHost() android.Module {
1560 module := &Import{}
1561
1562 module.AddProperties(&module.properties)
1563
1564 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001565 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001566 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001567 return module
1568}
1569
Colin Cross42be7612019-02-21 18:12:14 -08001570// dex_import module
1571
1572type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001573 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001574
1575 // set the name of the output
1576 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001577}
1578
1579type DexImport struct {
1580 android.ModuleBase
1581 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001582 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001583 prebuilt android.Prebuilt
1584
1585 properties DexImportProperties
1586
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001587 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001588
1589 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001590
1591 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001592}
1593
1594func (j *DexImport) Prebuilt() *android.Prebuilt {
1595 return &j.prebuilt
1596}
1597
1598func (j *DexImport) PrebuiltSrcs() []string {
1599 return j.properties.Jars
1600}
1601
1602func (j *DexImport) Name() string {
1603 return j.prebuilt.Name(j.ModuleBase.Name())
1604}
1605
Jiyong Park0b238752019-10-29 11:23:10 +09001606func (j *DexImport) Stem() string {
1607 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1608}
1609
Jiyong Park77acec62020-06-01 21:39:15 +09001610func (a *DexImport) JacocoReportClassesFile() android.Path {
1611 return nil
1612}
1613
Colin Cross08dca382020-07-21 20:31:17 -07001614func (a *DexImport) LintDepSets() LintDepSets {
1615 return LintDepSets{}
1616}
1617
Martin Stjernholm6d415272020-01-31 17:10:36 +00001618func (j *DexImport) IsInstallable() bool {
1619 return true
1620}
1621
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001622func (j *DexImport) getStrictUpdatabilityLinting() bool {
1623 return false
1624}
1625
1626func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1627}
1628
Colin Cross42be7612019-02-21 18:12:14 -08001629func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1630 if len(j.properties.Jars) != 1 {
1631 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1632 }
1633
Colin Cross56a83212020-09-15 18:30:11 -07001634 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1635 if !apexInfo.IsForPlatform() {
1636 j.hideApexVariantFromMake = true
1637 }
1638
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001639 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1640 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001641 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1642
1643 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1644 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1645
1646 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001647 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001648
1649 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1650 rule.Temporary(temporary)
1651
1652 // use zip2zip to uncompress classes*.dex files
1653 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001654 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001655 FlagWithInput("-i ", inputJar).
1656 FlagWithOutput("-o ", temporary).
1657 FlagWithArg("-0 ", "'classes*.dex'")
1658
1659 // use zipalign to align uncompressed classes*.dex files
1660 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001661 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001662 Flag("-f").
1663 Text("4").
1664 Input(temporary).
1665 Output(dexOutputFile)
1666
1667 rule.DeleteTemporaryFiles()
1668
Colin Crossf1a035e2020-11-16 17:32:30 -08001669 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001670 } else {
1671 ctx.Build(pctx, android.BuildParams{
1672 Rule: android.Cp,
1673 Input: inputJar,
1674 Output: dexOutputFile,
1675 })
1676 }
1677
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001678 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001679
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001680 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001681
Colin Cross56a83212020-09-15 18:30:11 -07001682 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001683 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1684 j.Stem()+".jar", dexOutputFile)
1685 }
Colin Cross42be7612019-02-21 18:12:14 -08001686}
1687
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001688func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001689 return j.dexJarFile
1690}
1691
Jiyong Park45bf82e2020-12-15 22:29:02 +09001692var _ android.ApexModule = (*DexImport)(nil)
1693
1694// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001695func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1696 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001697 // we don't check prebuilt modules for sdk_version
1698 return nil
1699}
1700
Colin Cross42be7612019-02-21 18:12:14 -08001701// dex_import imports a `.jar` file containing classes.dex files.
1702//
1703// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1704// to the device.
1705func DexImportFactory() android.Module {
1706 module := &DexImport{}
1707
1708 module.AddProperties(&module.properties)
1709
1710 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001711 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001712 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001713 return module
1714}
1715
Colin Cross89536d42017-07-07 14:35:50 -07001716//
1717// Defaults
1718//
1719type Defaults struct {
1720 android.ModuleBase
1721 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001722 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001723}
1724
Colin Cross1b16b0e2019-02-12 14:41:32 -08001725// java_defaults provides a set of properties that can be inherited by other java or android modules.
1726//
1727// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1728// property in the defaults module that exists in the depending module will be prepended to the depending module's
1729// value for that property.
1730//
1731// Example:
1732//
1733// java_defaults {
1734// name: "example_defaults",
1735// srcs: ["common/**/*.java"],
1736// javacflags: ["-Xlint:all"],
1737// aaptflags: ["--auto-add-overlay"],
1738// }
1739//
1740// java_library {
1741// name: "example",
1742// defaults: ["example_defaults"],
1743// srcs: ["example/**/*.java"],
1744// }
1745//
1746// is functionally identical to:
1747//
1748// java_library {
1749// name: "example",
1750// srcs: [
1751// "common/**/*.java",
1752// "example/**/*.java",
1753// ],
1754// javacflags: ["-Xlint:all"],
1755// }
Paul Duffin47357662019-12-05 14:07:14 +00001756func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001757 module := &Defaults{}
1758
Colin Cross89536d42017-07-07 14:35:50 -07001759 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001760 &CommonProperties{},
1761 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001762 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001763 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001764 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001765 &aaptProperties{},
1766 &androidLibraryProperties{},
1767 &appProperties{},
1768 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001769 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001770 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001771 &ImportProperties{},
1772 &AARImportProperties{},
1773 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001774 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001775 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001776 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001777 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001778 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001779 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001780 )
1781
1782 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001783 return module
1784}
Nan Zhangea568a42017-11-08 21:20:04 -08001785
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001786func kytheExtractJavaFactory() android.Singleton {
1787 return &kytheExtractJavaSingleton{}
1788}
1789
1790type kytheExtractJavaSingleton struct {
1791}
1792
1793func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1794 var xrefTargets android.Paths
1795 ctx.VisitAllModules(func(module android.Module) {
1796 if javaModule, ok := module.(xref); ok {
1797 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1798 }
1799 })
1800 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1801 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001802 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001803 }
1804}
1805
Nan Zhangea568a42017-11-08 21:20:04 -08001806var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001807var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001808var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001809var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001810
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001811// Add class loader context (CLC) of a given dependency to the current CLC.
1812func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1813 clcMap dexpreopt.ClassLoaderContextMap) {
1814
1815 dep, ok := depModule.(UsesLibraryDependency)
1816 if !ok {
1817 return
1818 }
1819
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001820 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1821
1822 var sdkLib *string
1823 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1824 // A shared SDK library. This should be added as a top-level CLC element.
1825 sdkLib = &depName
1826 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1827 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1828 // property. This should be handled in the same way as a shared SDK library.
1829 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001830 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001831
1832 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001833 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001834 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001835 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1836 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1837 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1838 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001839 } else if depTag == staticLibTag {
1840 // Propagate <uses-library> through static library dependencies, unless it is a component
1841 // library (such as stubs). Component libraries have a dependency on their SDK library,
1842 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001843 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001844 return
1845 }
1846 } else {
1847 // Don't propagate <uses-library> for other dependency tags.
1848 return
1849 }
1850
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001851 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1852 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1853 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1854 // from its CLC should be added to the current CLC.
1855 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001856 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001857 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001858 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001859 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1860 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001861}