blob: 287bcfaebcd845509c15bcb399b53cd19a20bfb7 [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)
Jiakai Zhangea180332021-09-26 08:58:02 +000076 android.RegisterSdkMemberType(javaSystemserverLibsSdkMemberType)
Paul Duffin2da04242021-04-23 19:43:28 +010077 android.RegisterSdkMemberType(javaTestSdkMemberType)
78}
79
80var (
81 // Supports adding java header libraries to module_exports and sdk.
82 javaHeaderLibsSdkMemberType = &librarySdkMemberType{
83 android.SdkMemberTypeBase{
84 PropertyName: "java_header_libs",
85 SupportsSdk: true,
86 },
87 func(_ android.SdkMemberContext, j *Library) android.Path {
88 headerJars := j.HeaderJars()
89 if len(headerJars) != 1 {
90 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
91 }
92
93 return headerJars[0]
94 },
95 sdkSnapshotFilePathForJar,
96 copyEverythingToSnapshot,
97 }
Paul Duffin255f18e2019-12-13 11:22:16 +000098
Paul Duffin22ff0aa2021-02-04 11:15:34 +000099 // Export implementation classes jar as part of the sdk.
Paul Duffin2da04242021-04-23 19:43:28 +0100100 exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000101 implementationJars := j.ImplementationAndResourcesJars()
102 if len(implementationJars) != 1 {
103 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
104 }
105 return implementationJars[0]
106 }
107
Paul Duffin2da04242021-04-23 19:43:28 +0100108 // Supports adding java implementation libraries to module_exports but not sdk.
109 javaLibsSdkMemberType = &librarySdkMemberType{
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000110 android.SdkMemberTypeBase{
111 PropertyName: "java_libs",
112 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000113 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000114 sdkSnapshotFilePathForJar,
115 copyEverythingToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100116 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000117
Paul Duffin2da04242021-04-23 19:43:28 +0100118 // Supports adding java boot libraries to module_exports and sdk.
Paul Duffindb170e42020-12-08 17:48:25 +0000119 //
120 // The build has some implicit dependencies (via the boot jars configuration) on a number of
121 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
122 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
123 // used outside those mainline modules.
124 //
125 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
126 // either java_libs, or java_header_libs would end up exporting more information than was strictly
127 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
128 // sdk/module_exports without exposing any unnecessary information.
Paul Duffin2da04242021-04-23 19:43:28 +0100129 javaBootLibsSdkMemberType = &librarySdkMemberType{
Paul Duffindb170e42020-12-08 17:48:25 +0000130 android.SdkMemberTypeBase{
131 PropertyName: "java_boot_libs",
132 SupportsSdk: true,
133 },
Paul Duffin5c211452021-07-15 12:42:44 +0100134 func(ctx android.SdkMemberContext, j *Library) android.Path {
135 // Java boot libs are only provided in the SDK to provide access to their dex implementation
136 // jar for use by dexpreopting and boot jars package check. They do not need to provide an
137 // actual implementation jar but the java_import will need a file that exists so just copy an
138 // empty file. Any attempt to use that file as a jar will cause a build error.
139 return ctx.SnapshotBuilder().EmptyFile()
140 },
141 func(osPrefix, name string) string {
142 // Create a special name for the implementation jar to try and provide some useful information
143 // to a developer that attempts to compile against this.
144 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
145 return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
146 },
Paul Duffindb170e42020-12-08 17:48:25 +0000147 onlyCopyJarToSnapshot,
Paul Duffin2da04242021-04-23 19:43:28 +0100148 }
Paul Duffindb170e42020-12-08 17:48:25 +0000149
Jiakai Zhangea180332021-09-26 08:58:02 +0000150 // Supports adding java systemserver libraries to module_exports and sdk.
151 //
152 // The build has some implicit dependencies (via the systemserver jars configuration) on a number
153 // of modules that are part of the java systemserver classpath and which are provided by mainline
154 // modules but which are not otherwise used outside those mainline modules.
155 //
156 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
157 // either java_libs, or java_header_libs would end up exporting more information than was strictly
158 // necessary. The java_systemserver_libs property to allow those modules to be exported as part of
159 // the sdk/module_exports without exposing any unnecessary information.
160 javaSystemserverLibsSdkMemberType = &librarySdkMemberType{
161 android.SdkMemberTypeBase{
162 PropertyName: "java_systemserver_libs",
163 SupportsSdk: true,
164 },
165 func(ctx android.SdkMemberContext, j *Library) android.Path {
166 // Java systemserver libs are only provided in the SDK to provide access to their dex
167 // implementation jar for use by dexpreopting. They do not need to provide an actual
168 // implementation jar but the java_import will need a file that exists so just copy an empty
169 // file. Any attempt to use that file as a jar will cause a build error.
170 return ctx.SnapshotBuilder().EmptyFile()
171 },
172 func(osPrefix, name string) string {
173 // Create a special name for the implementation jar to try and provide some useful information
174 // to a developer that attempts to compile against this.
175 // TODO(b/175714559): Provide a proper error message in Soong not ninja.
176 return filepath.Join(osPrefix, "java_systemserver_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix)
177 },
178 onlyCopyJarToSnapshot,
179 }
180
Paul Duffin2da04242021-04-23 19:43:28 +0100181 // Supports adding java test libraries to module_exports but not sdk.
182 javaTestSdkMemberType = &testSdkMemberType{
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000183 SdkMemberTypeBase: android.SdkMemberTypeBase{
184 PropertyName: "java_tests",
185 },
Paul Duffin2da04242021-04-23 19:43:28 +0100186 }
187)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900188
Colin Crossdcf71b22021-02-01 13:59:03 -0800189// JavaInfo contains information about a java module for use by modules that depend on it.
190type JavaInfo struct {
191 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
192 // against this module. If empty, ImplementationJars should be used instead.
193 HeaderJars android.Paths
194
195 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
196 // in the module as well as any resources included in the module.
197 ImplementationAndResourcesJars android.Paths
198
199 // ImplementationJars is a list of jars that contain the implementations of classes in the
200 //module.
201 ImplementationJars android.Paths
202
203 // ResourceJars is a list of jars that contain the resources included in the module.
204 ResourceJars android.Paths
205
206 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
207 // depending on this module.
208 AidlIncludeDirs android.Paths
209
210 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
211 // module.
212 SrcJarArgs []string
213
214 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
215 SrcJarDeps android.Paths
216
217 // ExportedPlugins is a list of paths that should be used as annotation processors for any
218 // module that depends on this module.
219 ExportedPlugins android.Paths
220
221 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
222 // any module that depends on this module.
223 ExportedPluginClasses []string
224
225 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
226 // requiring disbling turbine for any modules that depend on it.
227 ExportedPluginDisableTurbine bool
228
229 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
230 // instrumented by jacoco.
231 JacocoReportClassesFile android.Path
232}
233
234var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
235
Colin Cross75ce9ec2021-02-26 16:20:32 -0800236// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
237// the sysprop implementation library.
238type SyspropPublicStubInfo struct {
239 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
240 // the sysprop implementation library.
241 JavaInfo JavaInfo
242}
243
244var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
245
Paul Duffin44b481b2020-06-17 16:59:43 +0100246// Methods that need to be implemented for a module that is added to apex java_libs property.
247type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700248 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100249 ImplementationAndResourcesJars() android.Paths
250}
251
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100252// Provides build path and install path to DEX jars.
253type UsesLibraryDependency interface {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100254 DexJarBuildPath() OptionalDexJarPath
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100255 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000256 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100257}
258
Jaewoong Jung26342642021-03-17 15:56:23 -0700259// TODO(jungjw): Move this to kythe.go once it's created.
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800260type xref interface {
261 XrefJavaFiles() android.Paths
262}
263
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800264func (j *Module) XrefJavaFiles() android.Paths {
265 return j.kytheFiles
266}
267
Colin Crossbe1da472017-07-07 15:59:46 -0700268type dependencyTag struct {
269 blueprint.BaseDependencyTag
270 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700271}
272
Colin Crosse9fe2942020-11-10 18:12:15 -0800273// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
274// dependency to be installed when the parent module is installed.
275type installDependencyTag struct {
276 blueprint.BaseDependencyTag
277 android.InstallAlwaysNeededDependencyTag
278 name string
279}
280
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100281type usesLibraryDependencyTag struct {
282 dependencyTag
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100283
284 // SDK version in which the library appared as a standalone library.
285 sdkVersion int
286
287 // If the dependency is optional or required.
288 optional bool
289
290 // Whether this is an implicit dependency inferred by Soong, or an explicit one added via
291 // `uses_libs`/`optional_uses_libs` properties.
292 implicit bool
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100293}
294
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100295func makeUsesLibraryDependencyTag(sdkVersion int, optional bool, implicit bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100296 return usesLibraryDependencyTag{
297 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
298 sdkVersion: sdkVersion,
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100299 optional: optional,
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100300 implicit: implicit,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100301 }
302}
303
Jiyong Park8be103b2019-11-08 15:53:48 +0900304func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700305 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900306}
307
Colin Crossbe1da472017-07-07 15:59:46 -0700308var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800309 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
310 staticLibTag = dependencyTag{name: "staticlib"}
311 libTag = dependencyTag{name: "javalib"}
312 java9LibTag = dependencyTag{name: "java9lib"}
313 pluginTag = dependencyTag{name: "plugin"}
314 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
315 exportedPluginTag = dependencyTag{name: "exported-plugin"}
316 bootClasspathTag = dependencyTag{name: "bootclasspath"}
317 systemModulesTag = dependencyTag{name: "system modules"}
318 frameworkResTag = dependencyTag{name: "framework-res"}
319 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
320 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700321 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800322 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
323 certificateTag = dependencyTag{name: "certificate"}
324 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
325 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
326 jniLibTag = dependencyTag{name: "jnilib"}
327 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
328 jniInstallTag = installDependencyTag{name: "jni install"}
329 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700330)
Colin Cross2fe66872015-03-30 17:20:39 -0700331
Jiyong Park83dc74b2020-01-14 18:38:44 +0900332func IsLibDepTag(depTag blueprint.DependencyTag) bool {
333 return depTag == libTag
334}
335
336func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
337 return depTag == staticLibTag
338}
339
Colin Crossfc3674a2017-09-18 17:41:52 -0700340type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100341 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700342
Colin Cross6cef4812019-10-17 14:23:50 -0700343 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
344 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100345
346 // The default system modules to use. Will be an empty string if no system
347 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700348 systemModules string
349
Pete Gilline3d44b22020-06-29 11:28:51 +0100350 // The modules that will be added to the classpath regardless of the Java language level targeted
351 classpath []string
352
Colin Cross6cef4812019-10-17 14:23:50 -0700353 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100354 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700355 java9Classpath []string
356
Colin Crossa97c5d32018-03-28 14:58:31 -0700357 frameworkResModule string
358
Colin Cross86a60ae2018-05-29 14:44:55 -0700359 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700360 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100361
362 noStandardLibs, noFrameworksLibs bool
363}
364
365func (s sdkDep) hasStandardLibs() bool {
366 return !s.noStandardLibs
367}
368
369func (s sdkDep) hasFrameworkLibs() bool {
370 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700371}
372
Colin Crossa4f08812018-10-02 22:03:40 -0700373type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700374 name string
375 path android.Path
376 target android.Target
377 coverageFile android.OptionalPath
378 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700379}
380
Jiyong Parkf1691d22021-03-29 20:11:58 +0900381func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700382 sdkDep := decodeSdkDep(ctx, sdkContext)
383 if sdkDep.useModule {
384 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
385 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
386 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
387 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
388 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
389 }
390 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
391 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
392 }
393 }
394 if sdkDep.systemModules != "" {
395 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
396 }
397}
398
Colin Cross32f676a2017-09-06 13:41:06 -0700399type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800400 classpath classpath
401 java9Classpath classpath
402 bootClasspath classpath
403 processorPath classpath
404 errorProneProcessorPath classpath
405 processorClasses []string
406 staticJars android.Paths
407 staticHeaderJars android.Paths
408 staticResourceJars android.Paths
409 aidlIncludeDirs android.Paths
410 srcs android.Paths
411 srcJars android.Paths
412 systemModules *systemModules
413 aidlPreprocess android.OptionalPath
414 kotlinStdlib android.Paths
415 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700416 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800417
418 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700419}
Colin Cross2fe66872015-03-30 17:20:39 -0700420
Colin Cross54250902017-12-05 09:28:08 -0800421func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
422 for _, f := range dep.Srcs() {
423 if f.Ext() != ".jar" {
424 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
425 ctx.OtherModuleName(dep.(blueprint.Module)))
426 }
427 }
428}
429
Jiyong Parkf1691d22021-03-29 20:11:58 +0900430func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700431 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700432 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700433 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900434 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700435 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700436 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700437 }
Nan Zhang357466b2018-04-17 17:38:36 -0700438}
439
Colin Cross1e743852019-10-28 11:37:20 -0700440type javaVersion int
441
442const (
443 JAVA_VERSION_UNSUPPORTED = 0
444 JAVA_VERSION_6 = 6
445 JAVA_VERSION_7 = 7
446 JAVA_VERSION_8 = 8
447 JAVA_VERSION_9 = 9
448)
449
450func (v javaVersion) String() string {
451 switch v {
452 case JAVA_VERSION_6:
453 return "1.6"
454 case JAVA_VERSION_7:
455 return "1.7"
456 case JAVA_VERSION_8:
457 return "1.8"
458 case JAVA_VERSION_9:
459 return "1.9"
460 default:
461 return "unsupported"
462 }
463}
464
465// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
466func (v javaVersion) usesJavaModules() bool {
467 return v >= 9
468}
469
470func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100471 switch javaVersion {
472 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700473 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100474 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700475 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100476 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700477 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100478 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700479 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100480 case "10", "11":
481 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700482 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100483 default:
484 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700485 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100486 }
487}
488
Colin Cross2fe66872015-03-30 17:20:39 -0700489//
490// Java libraries (.jar file)
491//
492
Colin Crossf506d872017-07-19 15:53:04 -0700493type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700494 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700495
496 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700497}
498
Jiyong Park45bf82e2020-12-15 22:29:02 +0900499var _ android.ApexModule = (*Library)(nil)
500
satayevd604b212021-07-21 14:23:52 +0100501// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100502type PermittedPackagesForUpdatableBootJars interface {
503 PermittedPackagesForUpdatableBootJars() []string
504}
505
506var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
507
508func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
509 return j.properties.Permitted_packages
510}
511
Colin Cross42be7612019-02-21 18:12:14 -0800512func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000513 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700514 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000515 return true
516 }
517
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000518 // Store uncompressed (and do not strip) dex files from boot class path jars.
519 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
520 return true
521 }
522
523 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000524 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000525 return true
526 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800527 if ctx.Config().UncompressPrivAppDex() &&
528 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
529 return true
530 }
531
Colin Cross2fc72f62018-12-21 12:59:54 -0800532 return false
533}
534
Colin Crossf506d872017-07-19 15:53:04 -0700535func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900536 j.sdkVersion = j.SdkVersion(ctx)
537 j.minSdkVersion = j.MinSdkVersion(ctx)
538
Colin Cross56a83212020-09-15 18:30:11 -0700539 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
540 if !apexInfo.IsForPlatform() {
541 j.hideApexVariantFromMake = true
542 }
543
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100544 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000545 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
546 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800547 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -0700548 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +0100549 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -0700550 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +0100551 }
Liz Kammera7a64f32020-07-09 15:16:41 -0700552 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100553 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700554 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700555
bralee1fbf4402020-05-21 10:11:59 +0800556 // Collect the module directory for IDE info in java/jdeps.go.
557 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
558
Colin Cross56a83212020-09-15 18:30:11 -0700559 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900560 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700561 var extraInstallDeps android.Paths
562 if j.InstallMixin != nil {
563 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
564 }
Colin Cross2c429dc2017-08-31 16:45:16 -0700565 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +0900566 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700567 }
Dan Willemsen8e6b3712021-09-20 23:11:24 -0700568
569 if ctx.Windows() {
570 j.HideFromMake()
571 }
Colin Crossb7a63242015-04-16 14:09:14 -0700572}
573
Colin Crossf506d872017-07-19 15:53:04 -0700574func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700575 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100576 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700577}
578
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000579const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000580 aidlIncludeDir = "aidl"
581 javaDir = "java"
582 jarFileSuffix = ".jar"
583 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000584)
585
Paul Duffina0dbf432019-12-05 11:25:53 +0000586// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000587func sdkSnapshotFilePathForJar(osPrefix, name string) string {
588 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000589}
590
Paul Duffina04c1072020-03-02 10:16:35 +0000591func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
592 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000593}
594
Paul Duffin13879572019-11-28 14:31:38 +0000595type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000596 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000597
598 // Function to retrieve the appropriate output jar (implementation or header) from
599 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000600 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
601
602 // Function to compute the snapshot relative path to which the named library's
603 // jar should be copied.
604 snapshotPathGetter func(osPrefix, name string) string
605
606 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
607 // files like aidl files should also be copied.
608 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000609}
610
Paul Duffindb170e42020-12-08 17:48:25 +0000611const (
612 onlyCopyJarToSnapshot = true
613 copyEverythingToSnapshot = false
614)
615
Paul Duffin296701e2021-07-14 10:29:36 +0100616func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
617 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000618}
619
620func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
621 _, ok := module.(*Library)
622 return ok
623}
624
Paul Duffin3a4eb502020-03-19 16:11:18 +0000625func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
626 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000627}
Paul Duffina0dbf432019-12-05 11:25:53 +0000628
Paul Duffin14eb4672020-03-02 11:33:02 +0000629func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000630 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000631}
632
633type librarySdkMemberProperties struct {
634 android.SdkMemberPropertiesBase
635
Paul Duffin864e1b42020-05-06 10:23:19 +0100636 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000637 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100638
639 // The list of permitted packages that need to be passed to the prebuilts as they are used to
640 // create the updatable-bcp-packages.txt file.
641 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000642}
643
Paul Duffin3a4eb502020-03-19 16:11:18 +0000644func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000645 j := variant.(*Library)
646
Paul Duffindb170e42020-12-08 17:48:25 +0000647 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
648
Paul Duffina551a1c2020-03-17 21:04:24 +0000649 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100650
651 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000652}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000653
Paul Duffin3a4eb502020-03-19 16:11:18 +0000654func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000655 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000656
Paul Duffindb170e42020-12-08 17:48:25 +0000657 memberType := ctx.MemberType().(*librarySdkMemberType)
658
Paul Duffina551a1c2020-03-17 21:04:24 +0000659 exportedJar := p.JarToExport
660 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000661 // Delegate the creation of the snapshot relative path to the member type.
662 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
663
664 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000665 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
666
Paul Duffina551a1c2020-03-17 21:04:24 +0000667 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
668 }
669
Paul Duffin869de142021-07-15 14:14:41 +0100670 if len(p.PermittedPackages) > 0 {
671 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
672 }
673
Paul Duffindb170e42020-12-08 17:48:25 +0000674 // Do not copy anything else to the snapshot.
675 if memberType.onlyCopyJarToSnapshot {
676 return
677 }
678
Paul Duffina551a1c2020-03-17 21:04:24 +0000679 aidlIncludeDirs := p.AidlIncludeDirs
680 if len(aidlIncludeDirs) != 0 {
681 sdkModuleContext := ctx.SdkModuleContext()
682 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000683 // TODO(jiyong): copy parcelable declarations only
684 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
685 for _, file := range aidlFiles {
686 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
687 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000688 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000689
Paul Duffina551a1c2020-03-17 21:04:24 +0000690 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000691 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000692}
693
Colin Cross1b16b0e2019-02-12 14:41:32 -0800694// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
695//
696// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
697// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
698// as a `static_libs` dependency of another module.
699//
700// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
701// a device.
702//
703// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
704// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700705func LibraryFactory() android.Module {
706 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700707
Colin Crossce6734e2020-06-15 16:09:53 -0700708 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700709
Paul Duffin71b33cc2021-06-23 11:39:47 +0100710 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100711
Jiyong Park7f7766d2019-07-25 22:02:35 +0900712 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900713 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900714 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700715 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700716}
717
Colin Cross1b16b0e2019-02-12 14:41:32 -0800718// java_library_static is an obsolete alias for java_library.
719func LibraryStaticFactory() android.Module {
720 return LibraryFactory()
721}
722
723// java_library_host builds and links sources into a `.jar` file for the host.
724//
725// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
726// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700727func LibraryHostFactory() android.Module {
728 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700729
Colin Crossce6734e2020-06-15 16:09:53 -0700730 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700731
Colin Cross9ae1b922018-06-26 17:59:05 -0700732 module.Module.properties.Installable = proptools.BoolPtr(true)
733
Jiyong Park7f7766d2019-07-25 22:02:35 +0900734 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100735 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900736 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700737 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700738}
739
740//
Colin Crossb628ea52018-08-14 16:42:33 -0700741// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700742//
743
Dan Shi95d19422020-08-15 12:24:26 -0700744// Test option struct.
745type TestOptions struct {
746 // a list of extra test configuration files that should be installed with the module.
747 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800748
749 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
750 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700751}
752
Colin Cross05638fc2018-04-09 18:40:24 -0700753type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700754 // list of compatibility suites (for example "cts", "vts") that the module should be
755 // installed into.
756 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700757
758 // the name of the test configuration (for example "AndroidTest.xml") that should be
759 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800760 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700761
Jack He33338892018-09-19 02:21:28 -0700762 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
763 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800764 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700765
Colin Crossd96ca352018-08-10 16:06:24 -0700766 // list of files or filegroup modules that provide data that should be installed alongside
767 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900768 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700769
770 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
771 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
772 // explicitly.
773 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800774
775 // Add parameterized mainline modules to auto generated test config. The options will be
776 // handled by TradeFed to do downloading and installing the specified modules on the device.
777 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700778
779 // Test options.
780 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800781
782 // Names of modules containing JNI libraries that should be installed alongside the test.
783 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700784
785 // Install the test into a folder named for the module in all test suites.
786 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700787}
788
Liz Kammerdd849a82020-06-12 16:38:45 -0700789type hostTestProperties struct {
790 // list of native binary modules that should be installed alongside the test
791 Data_native_bins []string `android:"arch_variant"`
792}
793
Paul Duffin42df1442019-03-20 12:45:53 +0000794type testHelperLibraryProperties struct {
795 // list of compatibility suites (for example "cts", "vts") that the module should be
796 // installed into.
797 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700798
799 // Install the test into a folder named for the module in all test suites.
800 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000801}
802
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000803type prebuiltTestProperties struct {
804 // list of compatibility suites (for example "cts", "vts") that the module should be
805 // installed into.
806 Test_suites []string `android:"arch_variant"`
807
808 // the name of the test configuration (for example "AndroidTest.xml") that should be
809 // installed with the module.
810 Test_config *string `android:"path,arch_variant"`
811}
812
Colin Cross05638fc2018-04-09 18:40:24 -0700813type Test struct {
814 Library
815
816 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700817
Dan Shi95d19422020-08-15 12:24:26 -0700818 testConfig android.Path
819 extraTestConfigs android.Paths
820 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700821}
822
Liz Kammerdd849a82020-06-12 16:38:45 -0700823type TestHost struct {
824 Test
825
826 testHostProperties hostTestProperties
827}
828
Paul Duffin42df1442019-03-20 12:45:53 +0000829type TestHelperLibrary struct {
830 Library
831
832 testHelperLibraryProperties testHelperLibraryProperties
833}
834
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000835type JavaTestImport struct {
836 Import
837
838 prebuiltTestProperties prebuiltTestProperties
839
840 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700841 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000842}
843
Liz Kammerdd849a82020-06-12 16:38:45 -0700844func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
845 if len(j.testHostProperties.Data_native_bins) > 0 {
846 for _, target := range ctx.MultiTargets() {
847 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
848 }
849 }
850
Colin Crossf8d9c492021-01-26 11:01:43 -0800851 if len(j.testProperties.Jni_libs) > 0 {
852 for _, target := range ctx.MultiTargets() {
853 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
854 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
855 }
856 }
857
Liz Kammerdd849a82020-06-12 16:38:45 -0700858 j.deps(ctx)
859}
860
Yuexi Ma627263f2021-03-04 13:47:56 -0800861func (j *TestHost) AddExtraResource(p android.Path) {
862 j.extraResources = append(j.extraResources, p)
863}
864
Colin Cross303e21f2018-08-07 16:49:25 -0700865func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000866 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
867 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700868 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000869 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
870 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700871 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800872 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700873
Colin Cross8a497952019-03-05 22:25:09 -0800874 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700875
Dan Shi95d19422020-08-15 12:24:26 -0700876 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
877
Liz Kammerdd849a82020-06-12 16:38:45 -0700878 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
879 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
880 })
881
Colin Crossf8d9c492021-01-26 11:01:43 -0800882 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
883 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
884 if sharedLibInfo.SharedLibrary != nil {
885 // Copy to an intermediate output directory to append "lib[64]" to the path,
886 // so that it's compatible with the default rpath values.
887 var relPath string
888 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
889 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
890 } else {
891 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
892 }
893 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
894 ctx.Build(pctx, android.BuildParams{
895 Rule: android.Cp,
896 Input: sharedLibInfo.SharedLibrary,
897 Output: relocatedLib,
898 })
899 j.data = append(j.data, relocatedLib)
900 } else {
901 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
902 }
903 })
904
Colin Cross303e21f2018-08-07 16:49:25 -0700905 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700906}
907
Paul Duffin42df1442019-03-20 12:45:53 +0000908func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
909 j.Library.GenerateAndroidBuildActions(ctx)
910}
911
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000912func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
913 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800914 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000915
916 j.Import.GenerateAndroidBuildActions(ctx)
917}
918
919type testSdkMemberType struct {
920 android.SdkMemberTypeBase
921}
922
Paul Duffin296701e2021-07-14 10:29:36 +0100923func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
924 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000925}
926
927func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
928 _, ok := module.(*Test)
929 return ok
930}
931
Paul Duffin3a4eb502020-03-19 16:11:18 +0000932func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
933 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000934}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000935
Paul Duffin14eb4672020-03-02 11:33:02 +0000936func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
937 return &testSdkMemberProperties{}
938}
939
940type testSdkMemberProperties struct {
941 android.SdkMemberPropertiesBase
942
Paul Duffina551a1c2020-03-17 21:04:24 +0000943 JarToExport android.Path
944 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000945}
946
Paul Duffin3a4eb502020-03-19 16:11:18 +0000947func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000948 test := variant.(*Test)
949
950 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000951 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000952 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000953 }
954
Paul Duffina551a1c2020-03-17 21:04:24 +0000955 p.JarToExport = implementationJars[0]
956 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000957}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000958
Paul Duffin3a4eb502020-03-19 16:11:18 +0000959func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000960 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000961
Paul Duffina551a1c2020-03-17 21:04:24 +0000962 exportedJar := p.JarToExport
963 if exportedJar != nil {
964 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
965 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000966
967 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +0000968 }
969
970 testConfig := p.TestConfig
971 if testConfig != nil {
972 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
973 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000974 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
975 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000976}
977
Colin Cross1b16b0e2019-02-12 14:41:32 -0800978// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
979// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
980//
981// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
982// compiled against the device bootclasspath.
983//
984// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
985// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -0700986func TestFactory() android.Module {
987 module := &Test{}
988
Colin Crossce6734e2020-06-15 16:09:53 -0700989 module.addHostAndDeviceProperties()
990 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -0700991
Colin Cross9ae1b922018-06-26 17:59:05 -0700992 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -0800993 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700994 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -0700995
Paul Duffinb6b89a42021-05-06 16:33:43 +0100996 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -0700997 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -0700998 return module
999}
1000
Paul Duffin42df1442019-03-20 12:45:53 +00001001// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1002func TestHelperLibraryFactory() android.Module {
1003 module := &TestHelperLibrary{}
1004
Colin Crossce6734e2020-06-15 16:09:53 -07001005 module.addHostAndDeviceProperties()
1006 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001007
Colin Cross9a4abed2019-04-24 13:19:28 -07001008 module.Module.properties.Installable = proptools.BoolPtr(true)
1009 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001010 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001011
Paul Duffin42df1442019-03-20 12:45:53 +00001012 InitJavaModule(module, android.HostAndDeviceSupported)
1013 return module
1014}
1015
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001016// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1017// and makes sure that it is added to the appropriate test suite.
1018//
1019// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1020// compiled against an Android classpath.
1021//
1022// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1023// for host modules.
1024func JavaTestImportFactory() android.Module {
1025 module := &JavaTestImport{}
1026
1027 module.AddProperties(
1028 &module.Import.properties,
1029 &module.prebuiltTestProperties)
1030
1031 module.Import.properties.Installable = proptools.BoolPtr(true)
1032
1033 android.InitPrebuiltModule(module, &module.properties.Jars)
1034 android.InitApexModule(module)
1035 android.InitSdkAwareModule(module)
1036 InitJavaModule(module, android.HostAndDeviceSupported)
1037 return module
1038}
1039
Colin Cross1b16b0e2019-02-12 14:41:32 -08001040// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1041// allow running the test with `atest` or a `TEST_MAPPING` file.
1042//
1043// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1044// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001045func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001046 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001047
Colin Crossce6734e2020-06-15 16:09:53 -07001048 module.addHostProperties()
1049 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001050 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001051
Yuexi Ma627263f2021-03-04 13:47:56 -08001052 InitTestHost(
1053 module,
1054 proptools.BoolPtr(true),
1055 nil,
1056 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001057
Liz Kammerdd849a82020-06-12 16:38:45 -07001058 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001059
Colin Cross05638fc2018-04-09 18:40:24 -07001060 return module
1061}
1062
Yuexi Ma627263f2021-03-04 13:47:56 -08001063func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1064 th.properties.Installable = installable
1065 th.testProperties.Auto_gen_config = autoGenConfig
1066 th.testProperties.Test_suites = testSuites
1067}
1068
Colin Cross05638fc2018-04-09 18:40:24 -07001069//
Colin Cross2fe66872015-03-30 17:20:39 -07001070// Java Binaries (.jar file plus wrapper script)
1071//
1072
Colin Crossf506d872017-07-19 15:53:04 -07001073type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001074 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001075 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001076
1077 // Name of the class containing main to be inserted into the manifest as Main-Class.
1078 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001079
1080 // Names of modules containing JNI libraries that should be installed alongside the host
1081 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001082 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001083}
1084
Colin Crossf506d872017-07-19 15:53:04 -07001085type Binary struct {
1086 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001087
Colin Crossf506d872017-07-19 15:53:04 -07001088 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001089
Colin Cross6b4a32d2017-12-05 13:42:45 -08001090 isWrapperVariant bool
1091
Colin Crossc3315992017-12-08 19:12:36 -08001092 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001093 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001094}
1095
Alex Light24237172017-10-26 09:46:21 -07001096func (j *Binary) HostToolPath() android.OptionalPath {
1097 return android.OptionalPathForPath(j.binaryFile)
1098}
1099
Colin Crossf506d872017-07-19 15:53:04 -07001100func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001101 if ctx.Arch().ArchType == android.Common {
1102 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001103 if j.binaryProperties.Main_class != nil {
1104 if j.properties.Manifest != nil {
1105 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1106 }
1107 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1108 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1109 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1110 }
1111
Colin Cross6b4a32d2017-12-05 13:42:45 -08001112 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001113 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001114 // Handle the binary wrapper
1115 j.isWrapperVariant = true
1116
Colin Cross366938f2017-12-11 16:29:02 -08001117 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001118 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001119 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001120 if ctx.Windows() {
1121 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1122 }
1123
Colin Cross6b4a32d2017-12-05 13:42:45 -08001124 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1125 }
1126
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001127 ext := ""
1128 if ctx.Windows() {
1129 ext = ".bat"
1130 }
1131
Colin Crossc179ea62020-10-09 10:54:15 -07001132 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001133 // of the wrapper variant, which will include the common variant's jar file and any JNI
1134 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001135 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001136 ctx.ModuleName()+ext, j.wrapperFile)
1137 }
1138
1139 if ctx.Windows() {
1140 j.HideFromMake()
Nan Zhang3c807db2017-11-03 14:53:31 -07001141 }
Colin Cross2fe66872015-03-30 17:20:39 -07001142}
1143
Colin Crossf506d872017-07-19 15:53:04 -07001144func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001145 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001146 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001147 }
1148 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001149 // These dependencies ensure the host installation rules will install the jar file and
1150 // the jni libraries when the wrapper is installed.
1151 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1152 ctx.AddVariationDependencies(
1153 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1154 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001155 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001156}
1157
Colin Cross1b16b0e2019-02-12 14:41:32 -08001158// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1159// as well.
1160//
1161// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1162// compiled against the device bootclasspath.
1163//
1164// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1165// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001166func BinaryFactory() android.Module {
1167 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001168
Colin Crossce6734e2020-06-15 16:09:53 -07001169 module.addHostAndDeviceProperties()
1170 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001171
Colin Cross9ae1b922018-06-26 17:59:05 -07001172 module.Module.properties.Installable = proptools.BoolPtr(true)
1173
Colin Cross6b4a32d2017-12-05 13:42:45 -08001174 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1175 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001176 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001177}
1178
Colin Cross1b16b0e2019-02-12 14:41:32 -08001179// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1180//
1181// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1182// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001183func BinaryHostFactory() android.Module {
1184 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001185
Colin Crossce6734e2020-06-15 16:09:53 -07001186 module.addHostProperties()
1187 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001188
Colin Cross9ae1b922018-06-26 17:59:05 -07001189 module.Module.properties.Installable = proptools.BoolPtr(true)
1190
Colin Cross6b4a32d2017-12-05 13:42:45 -08001191 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1192 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001193 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001194}
1195
1196//
1197// Java prebuilts
1198//
1199
Colin Cross74d73e22017-08-02 11:05:49 -07001200type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001201 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001202
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001203 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1204 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001205 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001206
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001207 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1208 // specified.
1209 Min_sdk_version *string
1210
Colin Cross535e2cf2017-10-20 17:57:49 -07001211 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001212
Paul Duffin869de142021-07-15 14:14:41 +01001213 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001214 Permitted_packages []string
1215
Jiyong Park1be96912018-05-28 18:02:19 +09001216 // List of shared java libs that this module has dependencies to
1217 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001218
1219 // List of files to remove from the jar file(s)
1220 Exclude_files []string
1221
1222 // List of directories to remove from the jar file(s)
1223 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001224
1225 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001226 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001227
1228 // set the name of the output
1229 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001230
1231 Aidl struct {
1232 // directories that should be added as include directories for any aidl sources of modules
1233 // that depend on this module, as well as to aidl for this module.
1234 Export_include_dirs []string
1235 }
Colin Cross74d73e22017-08-02 11:05:49 -07001236}
1237
1238type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001239 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001240 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001241 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001242 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001243 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001244
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001245 // Functionality common to Module and Import.
1246 embeddableInModuleAndImport
1247
Liz Kammerd6c31d22020-08-05 15:40:41 -07001248 hiddenAPI
1249 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001250 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001251
Colin Cross74d73e22017-08-02 11:05:49 -07001252 properties ImportProperties
1253
Liz Kammerd6c31d22020-08-05 15:40:41 -07001254 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001255 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001256 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001257
Colin Cross0a6e0072017-08-30 14:24:55 -07001258 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001259 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001260 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001261
1262 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001263
1264 sdkVersion android.SdkSpec
1265 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001266}
1267
Paul Duffin630b11e2021-07-15 13:35:26 +01001268var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1269
1270func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1271 return j.properties.Permitted_packages
1272}
1273
Jiyong Park92315372021-04-02 08:45:46 +09001274func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1275 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001276}
1277
Jiyong Parkf1691d22021-03-29 20:11:58 +09001278func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001279 return "none"
1280}
1281
Jiyong Park92315372021-04-02 08:45:46 +09001282func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001283 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001284 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001285 }
Jiyong Park92315372021-04-02 08:45:46 +09001286 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001287}
1288
Jiyong Park92315372021-04-02 08:45:46 +09001289func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1290 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001291}
1292
Colin Cross74d73e22017-08-02 11:05:49 -07001293func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001294 return &j.prebuilt
1295}
1296
Colin Cross74d73e22017-08-02 11:05:49 -07001297func (j *Import) PrebuiltSrcs() []string {
1298 return j.properties.Jars
1299}
1300
1301func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001302 return j.prebuilt.Name(j.ModuleBase.Name())
1303}
1304
Jiyong Park0b238752019-10-29 11:23:10 +09001305func (j *Import) Stem() string {
1306 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1307}
1308
Jiyong Park618922e2020-01-08 13:35:43 +09001309func (a *Import) JacocoReportClassesFile() android.Path {
1310 return nil
1311}
1312
Bill Peckhama41a6962021-01-11 10:58:54 -08001313func (j *Import) LintDepSets() LintDepSets {
1314 return LintDepSets{}
1315}
1316
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001317func (j *Import) getStrictUpdatabilityLinting() bool {
1318 return false
1319}
1320
1321func (j *Import) setStrictUpdatabilityLinting(bool) {
1322}
1323
Colin Cross74d73e22017-08-02 11:05:49 -07001324func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001325 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001326
1327 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001328 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001329 }
Colin Cross1e676be2016-10-12 14:38:15 -07001330}
1331
Colin Cross74d73e22017-08-02 11:05:49 -07001332func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001333 j.sdkVersion = j.SdkVersion(ctx)
1334 j.minSdkVersion = j.MinSdkVersion(ctx)
1335
Colin Cross56a83212020-09-15 18:30:11 -07001336 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1337 j.hideApexVariantFromMake = true
1338 }
1339
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001340 if ctx.Windows() {
1341 j.HideFromMake()
1342 }
1343
Colin Cross8a497952019-03-05 22:25:09 -08001344 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001345
Jiyong Park0b238752019-10-29 11:23:10 +09001346 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001347 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001348 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1349 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001350 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001351 inputFile := outputFile
1352 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1353 TransformJetifier(ctx, outputFile, inputFile)
1354 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001355 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001356 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001357
Liz Kammerd6c31d22020-08-05 15:40:41 -07001358 var flags javaBuilderFlags
1359
Jiyong Park1be96912018-05-28 18:02:19 +09001360 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001361 tag := ctx.OtherModuleDependencyTag(module)
1362
Colin Crossdcf71b22021-02-01 13:59:03 -08001363 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1364 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001365 switch tag {
1366 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001367 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001368 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001369 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001370 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001371 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001372 switch tag {
1373 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001374 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001375 }
1376 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001377
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001378 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001379 })
1380
Nan Zhang4973ecf2018-08-10 13:42:12 -07001381 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001382 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09001383 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001384 }
Jiyong Park19604de2020-03-24 16:44:11 +09001385
1386 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001387
Paul Duffin064b70c2020-11-02 17:32:38 +00001388 if ctx.Device() {
1389 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1390 // obtained from the associated deapexer module.
1391 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1392 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001393 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001394 di := android.FindDeapexerProviderForModule(ctx)
1395 if di == nil {
1396 return // An error has been reported by FindDeapexerProviderForModule.
1397 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001398 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001399 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1400 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001401 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1402 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001403
1404 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001405 j.initHiddenAPI(ctx, dexJarFile, outputFile, nil)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001406
1407 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
1408 if j.dexProperties.Uncompress_dex == nil {
1409 // If the value was not force-set by the user, use reasonable default based on the module.
1410 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1411 }
1412 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1413 j.dexpreopt(ctx, dexOutputPath)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001414 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001415 // This should never happen as a variant for a prebuilt_apex is only created if the
1416 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001417 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001418 }
1419 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001420 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001421 if sdkDep.invalidVersion {
1422 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1423 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1424 } else if sdkDep.useFiles {
1425 // sdkDep.jar is actually equivalent to turbine header.jar.
1426 flags.classpath = append(flags.classpath, sdkDep.jars...)
1427 }
1428
1429 // Dex compilation
1430
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001431 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1432 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Paul Duffin064b70c2020-11-02 17:32:38 +00001433 if j.dexProperties.Uncompress_dex == nil {
1434 // If the value was not force-set by the user, use reasonable default based on the module.
1435 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1436 }
1437 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1438
Paul Duffin612e6102021-02-02 13:38:13 +00001439 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001440 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001441 if ctx.Failed() {
1442 return
1443 }
1444
Paul Duffin74d18d12021-05-14 14:18:47 +01001445 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001446 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001447
1448 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001449 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001450
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001451 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001452 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001453 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001454 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001455
1456 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1457 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1458 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1459 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1460 AidlIncludeDirs: j.exportAidlIncludeDirs,
1461 })
Colin Cross2fe66872015-03-30 17:20:39 -07001462}
1463
Paul Duffinaa55f742020-10-06 17:20:13 +01001464func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1465 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001466 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001467 return android.Paths{j.combinedClasspathFile}, nil
1468 default:
1469 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1470 }
1471}
1472
1473var _ android.OutputFileProducer = (*Import)(nil)
1474
Nan Zhanged19fc32017-10-19 13:06:22 -07001475func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001476 if j.combinedClasspathFile == nil {
1477 return nil
1478 }
Colin Cross37f6d792018-07-12 12:28:41 -07001479 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001480}
1481
Colin Cross331a1212018-08-15 20:40:52 -07001482func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001483 if j.combinedClasspathFile == nil {
1484 return nil
1485 }
Colin Cross331a1212018-08-15 20:40:52 -07001486 return android.Paths{j.combinedClasspathFile}
1487}
1488
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001489func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001490 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001491}
1492
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001493func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001494 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001495}
1496
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001497func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1498 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001499}
1500
Jiyong Park45bf82e2020-12-15 22:29:02 +09001501var _ android.ApexModule = (*Import)(nil)
1502
1503// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001504func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001505 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001506}
1507
Jiyong Park45bf82e2020-12-15 22:29:02 +09001508// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001509func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1510 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001511 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001512 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001513 return fmt.Errorf("min_sdk_version is not specified")
1514 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001515 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001516 return nil
1517 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001518 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1519 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001520 }
Jooyung Han749dc692020-04-15 11:03:39 +09001521 return nil
1522}
1523
Paul Duffinfef55002021-06-17 14:56:05 +01001524// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1525// java_sdk_library_import with the specified base module name requires to be exported from a
1526// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001527func requiredFilesFromPrebuiltApexForImport(name string) []string {
1528 // Add the dex implementation jar to the set of exported files.
1529 return []string{
1530 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001531 }
1532}
1533
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001534// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1535// the java library with the specified name.
1536func apexRootRelativePathToJavaLib(name string) string {
1537 return filepath.Join("javalib", name+".jar")
1538}
1539
Paul Duffinfef55002021-06-17 14:56:05 +01001540var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1541
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001542func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001543 name := j.BaseModuleName()
1544 return requiredFilesFromPrebuiltApexForImport(name)
1545}
1546
albaltai36ff7dc2018-12-25 14:35:23 +08001547// Add compile time check for interface implementation
1548var _ android.IDEInfo = (*Import)(nil)
1549var _ android.IDECustomizedModuleName = (*Import)(nil)
1550
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001551// Collect information for opening IDE project files in java/jdeps.go.
1552const (
1553 removedPrefix = "prebuilt_"
1554)
1555
1556func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1557 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1558}
1559
1560func (j *Import) IDECustomizedModuleName() string {
1561 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1562 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1563 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001564 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001565}
1566
Colin Cross74d73e22017-08-02 11:05:49 -07001567var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001568
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001569func (j *Import) IsInstallable() bool {
1570 return Bool(j.properties.Installable)
1571}
1572
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001573var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001574
Colin Cross1b16b0e2019-02-12 14:41:32 -08001575// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1576//
1577// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1578// compiled against an Android classpath.
1579//
1580// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1581// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001582func ImportFactory() android.Module {
1583 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001584
Liz Kammerd6c31d22020-08-05 15:40:41 -07001585 module.AddProperties(
1586 &module.properties,
1587 &module.dexer.dexProperties,
1588 )
Colin Cross74d73e22017-08-02 11:05:49 -07001589
Paul Duffin71b33cc2021-06-23 11:39:47 +01001590 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001591
Liz Kammerd6c31d22020-08-05 15:40:41 -07001592 module.dexProperties.Optimize.EnabledByDefault = false
1593
Colin Cross74d73e22017-08-02 11:05:49 -07001594 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001595 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001596 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001597 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001598 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001599}
1600
Colin Cross1b16b0e2019-02-12 14:41:32 -08001601// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1602// module.
1603//
1604// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1605// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001606func ImportFactoryHost() android.Module {
1607 module := &Import{}
1608
1609 module.AddProperties(&module.properties)
1610
1611 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001612 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001613 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001614 return module
1615}
1616
Colin Cross42be7612019-02-21 18:12:14 -08001617// dex_import module
1618
1619type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001620 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001621
1622 // set the name of the output
1623 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001624}
1625
1626type DexImport struct {
1627 android.ModuleBase
1628 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001629 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001630 prebuilt android.Prebuilt
1631
1632 properties DexImportProperties
1633
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001634 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001635
1636 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001637
1638 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001639}
1640
1641func (j *DexImport) Prebuilt() *android.Prebuilt {
1642 return &j.prebuilt
1643}
1644
1645func (j *DexImport) PrebuiltSrcs() []string {
1646 return j.properties.Jars
1647}
1648
1649func (j *DexImport) Name() string {
1650 return j.prebuilt.Name(j.ModuleBase.Name())
1651}
1652
Jiyong Park0b238752019-10-29 11:23:10 +09001653func (j *DexImport) Stem() string {
1654 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1655}
1656
Jiyong Park77acec62020-06-01 21:39:15 +09001657func (a *DexImport) JacocoReportClassesFile() android.Path {
1658 return nil
1659}
1660
Colin Cross08dca382020-07-21 20:31:17 -07001661func (a *DexImport) LintDepSets() LintDepSets {
1662 return LintDepSets{}
1663}
1664
Martin Stjernholm6d415272020-01-31 17:10:36 +00001665func (j *DexImport) IsInstallable() bool {
1666 return true
1667}
1668
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001669func (j *DexImport) getStrictUpdatabilityLinting() bool {
1670 return false
1671}
1672
1673func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1674}
1675
Colin Cross42be7612019-02-21 18:12:14 -08001676func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1677 if len(j.properties.Jars) != 1 {
1678 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1679 }
1680
Colin Cross56a83212020-09-15 18:30:11 -07001681 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1682 if !apexInfo.IsForPlatform() {
1683 j.hideApexVariantFromMake = true
1684 }
1685
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001686 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1687 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001688 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1689
1690 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1691 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1692
1693 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001694 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001695
1696 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1697 rule.Temporary(temporary)
1698
1699 // use zip2zip to uncompress classes*.dex files
1700 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001701 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001702 FlagWithInput("-i ", inputJar).
1703 FlagWithOutput("-o ", temporary).
1704 FlagWithArg("-0 ", "'classes*.dex'")
1705
1706 // use zipalign to align uncompressed classes*.dex files
1707 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001708 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001709 Flag("-f").
1710 Text("4").
1711 Input(temporary).
1712 Output(dexOutputFile)
1713
1714 rule.DeleteTemporaryFiles()
1715
Colin Crossf1a035e2020-11-16 17:32:30 -08001716 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001717 } else {
1718 ctx.Build(pctx, android.BuildParams{
1719 Rule: android.Cp,
1720 Input: inputJar,
1721 Output: dexOutputFile,
1722 })
1723 }
1724
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001725 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001726
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001727 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001728
Colin Cross56a83212020-09-15 18:30:11 -07001729 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001730 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1731 j.Stem()+".jar", dexOutputFile)
1732 }
Colin Cross42be7612019-02-21 18:12:14 -08001733}
1734
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001735func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001736 return j.dexJarFile
1737}
1738
Jiyong Park45bf82e2020-12-15 22:29:02 +09001739var _ android.ApexModule = (*DexImport)(nil)
1740
1741// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001742func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1743 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001744 // we don't check prebuilt modules for sdk_version
1745 return nil
1746}
1747
Colin Cross42be7612019-02-21 18:12:14 -08001748// dex_import imports a `.jar` file containing classes.dex files.
1749//
1750// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1751// to the device.
1752func DexImportFactory() android.Module {
1753 module := &DexImport{}
1754
1755 module.AddProperties(&module.properties)
1756
1757 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001758 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001759 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001760 return module
1761}
1762
Colin Cross89536d42017-07-07 14:35:50 -07001763//
1764// Defaults
1765//
1766type Defaults struct {
1767 android.ModuleBase
1768 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001769 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001770}
1771
Colin Cross1b16b0e2019-02-12 14:41:32 -08001772// java_defaults provides a set of properties that can be inherited by other java or android modules.
1773//
1774// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1775// property in the defaults module that exists in the depending module will be prepended to the depending module's
1776// value for that property.
1777//
1778// Example:
1779//
1780// java_defaults {
1781// name: "example_defaults",
1782// srcs: ["common/**/*.java"],
1783// javacflags: ["-Xlint:all"],
1784// aaptflags: ["--auto-add-overlay"],
1785// }
1786//
1787// java_library {
1788// name: "example",
1789// defaults: ["example_defaults"],
1790// srcs: ["example/**/*.java"],
1791// }
1792//
1793// is functionally identical to:
1794//
1795// java_library {
1796// name: "example",
1797// srcs: [
1798// "common/**/*.java",
1799// "example/**/*.java",
1800// ],
1801// javacflags: ["-Xlint:all"],
1802// }
Paul Duffin47357662019-12-05 14:07:14 +00001803func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001804 module := &Defaults{}
1805
Colin Cross89536d42017-07-07 14:35:50 -07001806 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001807 &CommonProperties{},
1808 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001809 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001810 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001811 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001812 &aaptProperties{},
1813 &androidLibraryProperties{},
1814 &appProperties{},
1815 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001816 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001817 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001818 &ImportProperties{},
1819 &AARImportProperties{},
1820 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001821 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001822 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001823 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001824 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001825 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001826 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001827 )
1828
1829 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001830 return module
1831}
Nan Zhangea568a42017-11-08 21:20:04 -08001832
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001833func kytheExtractJavaFactory() android.Singleton {
1834 return &kytheExtractJavaSingleton{}
1835}
1836
1837type kytheExtractJavaSingleton struct {
1838}
1839
1840func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1841 var xrefTargets android.Paths
1842 ctx.VisitAllModules(func(module android.Module) {
1843 if javaModule, ok := module.(xref); ok {
1844 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1845 }
1846 })
1847 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1848 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001849 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001850 }
1851}
1852
Nan Zhangea568a42017-11-08 21:20:04 -08001853var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001854var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001855var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001856var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001857
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001858// Add class loader context (CLC) of a given dependency to the current CLC.
1859func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1860 clcMap dexpreopt.ClassLoaderContextMap) {
1861
1862 dep, ok := depModule.(UsesLibraryDependency)
1863 if !ok {
1864 return
1865 }
1866
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001867 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1868
1869 var sdkLib *string
1870 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1871 // A shared SDK library. This should be added as a top-level CLC element.
1872 sdkLib = &depName
1873 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1874 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1875 // property. This should be handled in the same way as a shared SDK library.
1876 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001877 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001878
1879 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001880 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001881 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001882 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1883 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1884 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1885 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001886 } else if depTag == staticLibTag {
1887 // Propagate <uses-library> through static library dependencies, unless it is a component
1888 // library (such as stubs). Component libraries have a dependency on their SDK library,
1889 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001890 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001891 return
1892 }
1893 } else {
1894 // Don't propagate <uses-library> for other dependency tags.
1895 return
1896 }
1897
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001898 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1899 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1900 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1901 // from its CLC should be added to the current CLC.
1902 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001903 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001904 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001905 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001906 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1907 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001908}