blob: a9f3d1a0db2b6f1366289e921d027dc6623d2099 [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 Cross24cc4be62021-11-03 14:09:41 -0700268func (j *Module) InstallBypassMake() bool { return true }
269
Colin Crossbe1da472017-07-07 15:59:46 -0700270type dependencyTag struct {
271 blueprint.BaseDependencyTag
272 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700273}
274
Colin Crosse9fe2942020-11-10 18:12:15 -0800275// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
276// dependency to be installed when the parent module is installed.
277type installDependencyTag struct {
278 blueprint.BaseDependencyTag
279 android.InstallAlwaysNeededDependencyTag
280 name string
281}
282
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100283type usesLibraryDependencyTag struct {
284 dependencyTag
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100285
286 // SDK version in which the library appared as a standalone library.
287 sdkVersion int
288
289 // If the dependency is optional or required.
290 optional bool
291
292 // Whether this is an implicit dependency inferred by Soong, or an explicit one added via
293 // `uses_libs`/`optional_uses_libs` properties.
294 implicit bool
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100295}
296
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100297func makeUsesLibraryDependencyTag(sdkVersion int, optional bool, implicit bool) usesLibraryDependencyTag {
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100298 return usesLibraryDependencyTag{
Colin Crossabe2a4b2021-12-10 22:52:43 +0000299 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
300 sdkVersion: sdkVersion,
301 optional: optional,
302 implicit: implicit,
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100303 }
304}
305
Jiyong Park8be103b2019-11-08 15:53:48 +0900306func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700307 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900308}
309
Colin Crossbe1da472017-07-07 15:59:46 -0700310var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800311 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
312 staticLibTag = dependencyTag{name: "staticlib"}
Colin Crossabe2a4b2021-12-10 22:52:43 +0000313 libTag = dependencyTag{name: "javalib"}
314 java9LibTag = dependencyTag{name: "java9lib"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800315 pluginTag = dependencyTag{name: "plugin"}
316 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
317 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Crossabe2a4b2021-12-10 22:52:43 +0000318 bootClasspathTag = dependencyTag{name: "bootclasspath"}
319 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800320 frameworkResTag = dependencyTag{name: "framework-res"}
Colin Crossabe2a4b2021-12-10 22:52:43 +0000321 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
322 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Crossa1ff7c62021-09-17 14:11:52 -0700323 kotlinPluginTag = dependencyTag{name: "kotlin-plugin"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800324 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
325 certificateTag = dependencyTag{name: "certificate"}
326 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
327 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
Colin Crossabe2a4b2021-12-10 22:52:43 +0000328 jniLibTag = dependencyTag{name: "jnilib"}
Colin Cross75ce9ec2021-02-26 16:20:32 -0800329 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
330 jniInstallTag = installDependencyTag{name: "jni install"}
331 binaryInstallTag = installDependencyTag{name: "binary install"}
Colin Crossbe1da472017-07-07 15:59:46 -0700332)
Colin Cross2fe66872015-03-30 17:20:39 -0700333
Jiyong Park83dc74b2020-01-14 18:38:44 +0900334func IsLibDepTag(depTag blueprint.DependencyTag) bool {
335 return depTag == libTag
336}
337
338func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
339 return depTag == staticLibTag
340}
341
Colin Crossfc3674a2017-09-18 17:41:52 -0700342type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100343 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700344
Colin Cross6cef4812019-10-17 14:23:50 -0700345 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
346 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100347
348 // The default system modules to use. Will be an empty string if no system
349 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700350 systemModules string
351
Pete Gilline3d44b22020-06-29 11:28:51 +0100352 // The modules that will be added to the classpath regardless of the Java language level targeted
353 classpath []string
354
Colin Cross6cef4812019-10-17 14:23:50 -0700355 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100356 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700357 java9Classpath []string
358
Colin Crossa97c5d32018-03-28 14:58:31 -0700359 frameworkResModule string
360
Colin Cross86a60ae2018-05-29 14:44:55 -0700361 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700362 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100363
364 noStandardLibs, noFrameworksLibs bool
365}
366
367func (s sdkDep) hasStandardLibs() bool {
368 return !s.noStandardLibs
369}
370
371func (s sdkDep) hasFrameworkLibs() bool {
372 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700373}
374
Colin Crossa4f08812018-10-02 22:03:40 -0700375type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700376 name string
377 path android.Path
378 target android.Target
379 coverageFile android.OptionalPath
380 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700381}
382
Jiyong Parkf1691d22021-03-29 20:11:58 +0900383func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) {
Liz Kammerd6c31d22020-08-05 15:40:41 -0700384 sdkDep := decodeSdkDep(ctx, sdkContext)
385 if sdkDep.useModule {
386 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
387 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
388 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
389 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
390 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
391 }
392 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
393 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
394 }
395 }
396 if sdkDep.systemModules != "" {
397 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
398 }
399}
400
Colin Cross32f676a2017-09-06 13:41:06 -0700401type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800402 classpath classpath
403 java9Classpath classpath
404 bootClasspath classpath
405 processorPath classpath
406 errorProneProcessorPath classpath
407 processorClasses []string
408 staticJars android.Paths
409 staticHeaderJars android.Paths
410 staticResourceJars android.Paths
411 aidlIncludeDirs android.Paths
412 srcs android.Paths
413 srcJars android.Paths
414 systemModules *systemModules
415 aidlPreprocess android.OptionalPath
416 kotlinStdlib android.Paths
417 kotlinAnnotations android.Paths
Colin Crossa1ff7c62021-09-17 14:11:52 -0700418 kotlinPlugins android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800419
420 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700421}
Colin Cross2fe66872015-03-30 17:20:39 -0700422
Colin Cross54250902017-12-05 09:28:08 -0800423func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
424 for _, f := range dep.Srcs() {
425 if f.Ext() != ".jar" {
426 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
427 ctx.OtherModuleName(dep.(blueprint.Module)))
428 }
429 }
430}
431
Jiyong Parkf1691d22021-03-29 20:11:58 +0900432func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -0700433 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700434 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -0700435 } else if ctx.Device() {
Jiyong Park92315372021-04-02 08:45:46 +0900436 return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx))
Nan Zhang357466b2018-04-17 17:38:36 -0700437 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700438 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700439 }
Nan Zhang357466b2018-04-17 17:38:36 -0700440}
441
Colin Cross1e743852019-10-28 11:37:20 -0700442type javaVersion int
443
444const (
445 JAVA_VERSION_UNSUPPORTED = 0
446 JAVA_VERSION_6 = 6
447 JAVA_VERSION_7 = 7
448 JAVA_VERSION_8 = 8
449 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000450 JAVA_VERSION_11 = 11
Colin Cross1e743852019-10-28 11:37:20 -0700451)
452
453func (v javaVersion) String() string {
454 switch v {
455 case JAVA_VERSION_6:
456 return "1.6"
457 case JAVA_VERSION_7:
458 return "1.7"
459 case JAVA_VERSION_8:
460 return "1.8"
461 case JAVA_VERSION_9:
462 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000463 case JAVA_VERSION_11:
464 return "11"
Colin Cross1e743852019-10-28 11:37:20 -0700465 default:
466 return "unsupported"
467 }
468}
469
470// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
471func (v javaVersion) usesJavaModules() bool {
472 return v >= 9
473}
474
475func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100476 switch javaVersion {
477 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700478 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100479 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700480 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100481 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700482 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100483 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700484 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000485 case "11":
486 return JAVA_VERSION_11
487 case "10":
488 ctx.PropertyErrorf("java_version", "Java language levels 10 is not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700489 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100490 default:
491 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700492 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100493 }
494}
495
Colin Cross2fe66872015-03-30 17:20:39 -0700496//
497// Java libraries (.jar file)
498//
499
Colin Crossf506d872017-07-19 15:53:04 -0700500type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700501 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700502
503 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700504}
505
Jiyong Park45bf82e2020-12-15 22:29:02 +0900506var _ android.ApexModule = (*Library)(nil)
507
satayevd604b212021-07-21 14:23:52 +0100508// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100509type PermittedPackagesForUpdatableBootJars interface {
510 PermittedPackagesForUpdatableBootJars() []string
511}
512
513var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
514
515func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
516 return j.properties.Permitted_packages
517}
518
Colin Cross42be7612019-02-21 18:12:14 -0800519func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000520 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700521 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000522 return true
523 }
524
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000525 // Store uncompressed (and do not strip) dex files from boot class path jars.
526 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
527 return true
528 }
529
530 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000531 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000532 return true
533 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800534 if ctx.Config().UncompressPrivAppDex() &&
535 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
536 return true
537 }
538
Colin Cross2fc72f62018-12-21 12:59:54 -0800539 return false
540}
541
Jiakai Zhang22450f22021-10-11 03:05:20 +0000542// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
543func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
544 if dexer.dexProperties.Uncompress_dex == nil {
545 // If the value was not force-set by the user, use reasonable default based on the module.
546 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
547 }
548}
549
Colin Crossf506d872017-07-19 15:53:04 -0700550func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900551 j.sdkVersion = j.SdkVersion(ctx)
552 j.minSdkVersion = j.MinSdkVersion(ctx)
satayev0a420e72021-11-29 17:25:52 +0000553 j.maxSdkVersion = j.MaxSdkVersion(ctx)
Jiyong Park92315372021-04-02 08:45:46 +0900554
Colin Cross56a83212020-09-15 18:30:11 -0700555 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
556 if !apexInfo.IsForPlatform() {
557 j.hideApexVariantFromMake = true
558 }
559
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100560 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000561 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
562 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800563 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000564 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700565 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100566 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700567 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700568
bralee1fbf4402020-05-21 10:11:59 +0800569 // Collect the module directory for IDE info in java/jdeps.go.
570 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
571
Colin Cross56a83212020-09-15 18:30:11 -0700572 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700574 var extraInstallDeps android.Paths
575 if j.InstallMixin != nil {
576 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
577 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700578 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
579 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800580 j.hostdexInstallFile = ctx.InstallFile(
581 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700582 j.Stem()+"-hostdex.jar", j.outputFile)
583 }
584 var installDir android.InstallPath
585 if ctx.InstallInTestcases() {
586 var archDir string
587 if !ctx.Host() {
588 archDir = ctx.DeviceConfig().DeviceArch()
589 }
590 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
591 } else {
592 installDir = android.PathForModuleInstall(ctx, "framework")
593 }
594 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700595 }
Colin Crossb7a63242015-04-16 14:09:14 -0700596}
597
Colin Crossf506d872017-07-19 15:53:04 -0700598func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700599 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100600 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700601}
602
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000603const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000604 aidlIncludeDir = "aidl"
605 javaDir = "java"
606 jarFileSuffix = ".jar"
607 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000608)
609
Paul Duffina0dbf432019-12-05 11:25:53 +0000610// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000611func sdkSnapshotFilePathForJar(osPrefix, name string) string {
612 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000613}
614
Paul Duffina04c1072020-03-02 10:16:35 +0000615func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
616 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000617}
618
Paul Duffin13879572019-11-28 14:31:38 +0000619type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000620 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000621
622 // Function to retrieve the appropriate output jar (implementation or header) from
623 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000624 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
625
626 // Function to compute the snapshot relative path to which the named library's
627 // jar should be copied.
628 snapshotPathGetter func(osPrefix, name string) string
629
630 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
631 // files like aidl files should also be copied.
632 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000633}
634
Paul Duffindb170e42020-12-08 17:48:25 +0000635const (
636 onlyCopyJarToSnapshot = true
637 copyEverythingToSnapshot = false
638)
639
Paul Duffin296701e2021-07-14 10:29:36 +0100640func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
641 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000642}
643
644func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
645 _, ok := module.(*Library)
646 return ok
647}
648
Paul Duffin3a4eb502020-03-19 16:11:18 +0000649func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
650 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000651}
Paul Duffina0dbf432019-12-05 11:25:53 +0000652
Paul Duffin14eb4672020-03-02 11:33:02 +0000653func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000654 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000655}
656
657type librarySdkMemberProperties struct {
658 android.SdkMemberPropertiesBase
659
Paul Duffin864e1b42020-05-06 10:23:19 +0100660 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000661 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100662
663 // The list of permitted packages that need to be passed to the prebuilts as they are used to
664 // create the updatable-bcp-packages.txt file.
665 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000666}
667
Paul Duffin3a4eb502020-03-19 16:11:18 +0000668func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000669 j := variant.(*Library)
670
Paul Duffindb170e42020-12-08 17:48:25 +0000671 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
672
Paul Duffina551a1c2020-03-17 21:04:24 +0000673 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100674
675 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000676}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000677
Paul Duffin3a4eb502020-03-19 16:11:18 +0000678func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000679 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000680
Paul Duffindb170e42020-12-08 17:48:25 +0000681 memberType := ctx.MemberType().(*librarySdkMemberType)
682
Paul Duffina551a1c2020-03-17 21:04:24 +0000683 exportedJar := p.JarToExport
684 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000685 // Delegate the creation of the snapshot relative path to the member type.
686 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
687
688 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000689 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
690
Paul Duffina551a1c2020-03-17 21:04:24 +0000691 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
692 }
693
Paul Duffin869de142021-07-15 14:14:41 +0100694 if len(p.PermittedPackages) > 0 {
695 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
696 }
697
Paul Duffindb170e42020-12-08 17:48:25 +0000698 // Do not copy anything else to the snapshot.
699 if memberType.onlyCopyJarToSnapshot {
700 return
701 }
702
Paul Duffina551a1c2020-03-17 21:04:24 +0000703 aidlIncludeDirs := p.AidlIncludeDirs
704 if len(aidlIncludeDirs) != 0 {
705 sdkModuleContext := ctx.SdkModuleContext()
706 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000707 // TODO(jiyong): copy parcelable declarations only
708 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
709 for _, file := range aidlFiles {
710 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
711 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000712 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000713
Paul Duffina551a1c2020-03-17 21:04:24 +0000714 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000715 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000716}
717
Colin Cross1b16b0e2019-02-12 14:41:32 -0800718// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
719//
720// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
721// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
722// as a `static_libs` dependency of another module.
723//
724// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
725// a device.
726//
727// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
728// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700729func LibraryFactory() android.Module {
730 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700731
Colin Crossce6734e2020-06-15 16:09:53 -0700732 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700733
Paul Duffin71b33cc2021-06-23 11:39:47 +0100734 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100735
Jiyong Park7f7766d2019-07-25 22:02:35 +0900736 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900737 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900738 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700739 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700740}
741
Colin Cross1b16b0e2019-02-12 14:41:32 -0800742// java_library_static is an obsolete alias for java_library.
743func LibraryStaticFactory() android.Module {
744 return LibraryFactory()
745}
746
747// java_library_host builds and links sources into a `.jar` file for the host.
748//
749// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
750// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700751func LibraryHostFactory() android.Module {
752 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700753
Colin Crossce6734e2020-06-15 16:09:53 -0700754 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700755
Colin Cross9ae1b922018-06-26 17:59:05 -0700756 module.Module.properties.Installable = proptools.BoolPtr(true)
757
Jiyong Park7f7766d2019-07-25 22:02:35 +0900758 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100759 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900760 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700761 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700762}
763
764//
Colin Crossb628ea52018-08-14 16:42:33 -0700765// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700766//
767
Dan Shi95d19422020-08-15 12:24:26 -0700768// Test option struct.
769type TestOptions struct {
770 // a list of extra test configuration files that should be installed with the module.
771 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800772
773 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
774 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700775}
776
Colin Cross05638fc2018-04-09 18:40:24 -0700777type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700778 // list of compatibility suites (for example "cts", "vts") that the module should be
779 // installed into.
780 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700781
782 // the name of the test configuration (for example "AndroidTest.xml") that should be
783 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800784 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700785
Jack He33338892018-09-19 02:21:28 -0700786 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
787 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800788 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700789
Colin Crossd96ca352018-08-10 16:06:24 -0700790 // list of files or filegroup modules that provide data that should be installed alongside
791 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900792 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700793
794 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
795 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
796 // explicitly.
797 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800798
799 // Add parameterized mainline modules to auto generated test config. The options will be
800 // handled by TradeFed to do downloading and installing the specified modules on the device.
801 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700802
803 // Test options.
804 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800805
806 // Names of modules containing JNI libraries that should be installed alongside the test.
807 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700808
809 // Install the test into a folder named for the module in all test suites.
810 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700811}
812
Liz Kammerdd849a82020-06-12 16:38:45 -0700813type hostTestProperties struct {
814 // list of native binary modules that should be installed alongside the test
815 Data_native_bins []string `android:"arch_variant"`
816}
817
Paul Duffin42df1442019-03-20 12:45:53 +0000818type testHelperLibraryProperties struct {
819 // list of compatibility suites (for example "cts", "vts") that the module should be
820 // installed into.
821 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700822
823 // Install the test into a folder named for the module in all test suites.
824 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000825}
826
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000827type prebuiltTestProperties struct {
828 // list of compatibility suites (for example "cts", "vts") that the module should be
829 // installed into.
830 Test_suites []string `android:"arch_variant"`
831
832 // the name of the test configuration (for example "AndroidTest.xml") that should be
833 // installed with the module.
834 Test_config *string `android:"path,arch_variant"`
835}
836
Colin Cross05638fc2018-04-09 18:40:24 -0700837type Test struct {
838 Library
839
840 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700841
Dan Shi95d19422020-08-15 12:24:26 -0700842 testConfig android.Path
843 extraTestConfigs android.Paths
844 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700845}
846
Liz Kammerdd849a82020-06-12 16:38:45 -0700847type TestHost struct {
848 Test
849
850 testHostProperties hostTestProperties
851}
852
Paul Duffin42df1442019-03-20 12:45:53 +0000853type TestHelperLibrary struct {
854 Library
855
856 testHelperLibraryProperties testHelperLibraryProperties
857}
858
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000859type JavaTestImport struct {
860 Import
861
862 prebuiltTestProperties prebuiltTestProperties
863
864 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700865 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000866}
867
Colin Cross24cc4be62021-11-03 14:09:41 -0700868func (j *Test) InstallInTestcases() bool {
869 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
870 // testcases by base_rules.mk.
871 return !j.Host()
872}
873
874func (j *TestHelperLibrary) InstallInTestcases() bool {
875 return true
876}
877
878func (j *JavaTestImport) InstallInTestcases() bool {
879 return true
880}
881
Liz Kammerdd849a82020-06-12 16:38:45 -0700882func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
883 if len(j.testHostProperties.Data_native_bins) > 0 {
884 for _, target := range ctx.MultiTargets() {
885 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
886 }
887 }
888
Colin Crossf8d9c492021-01-26 11:01:43 -0800889 if len(j.testProperties.Jni_libs) > 0 {
890 for _, target := range ctx.MultiTargets() {
891 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
892 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
893 }
894 }
895
Liz Kammerdd849a82020-06-12 16:38:45 -0700896 j.deps(ctx)
897}
898
Yuexi Ma627263f2021-03-04 13:47:56 -0800899func (j *TestHost) AddExtraResource(p android.Path) {
900 j.extraResources = append(j.extraResources, p)
901}
902
Colin Cross303e21f2018-08-07 16:49:25 -0700903func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000904 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
905 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700906 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000907 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
908 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700909 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800910 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700911
Colin Cross8a497952019-03-05 22:25:09 -0800912 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700913
Dan Shi95d19422020-08-15 12:24:26 -0700914 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
915
Liz Kammerdd849a82020-06-12 16:38:45 -0700916 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
917 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
918 })
919
Colin Crossf8d9c492021-01-26 11:01:43 -0800920 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
921 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
922 if sharedLibInfo.SharedLibrary != nil {
923 // Copy to an intermediate output directory to append "lib[64]" to the path,
924 // so that it's compatible with the default rpath values.
925 var relPath string
926 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
927 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
928 } else {
929 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
930 }
931 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
932 ctx.Build(pctx, android.BuildParams{
933 Rule: android.Cp,
934 Input: sharedLibInfo.SharedLibrary,
935 Output: relocatedLib,
936 })
937 j.data = append(j.data, relocatedLib)
938 } else {
939 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
940 }
941 })
942
Colin Cross303e21f2018-08-07 16:49:25 -0700943 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700944}
945
Paul Duffin42df1442019-03-20 12:45:53 +0000946func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
947 j.Library.GenerateAndroidBuildActions(ctx)
948}
949
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000950func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
951 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800952 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000953
954 j.Import.GenerateAndroidBuildActions(ctx)
955}
956
957type testSdkMemberType struct {
958 android.SdkMemberTypeBase
959}
960
Paul Duffin296701e2021-07-14 10:29:36 +0100961func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
962 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000963}
964
965func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
966 _, ok := module.(*Test)
967 return ok
968}
969
Paul Duffin3a4eb502020-03-19 16:11:18 +0000970func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
971 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000972}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000973
Paul Duffin14eb4672020-03-02 11:33:02 +0000974func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
975 return &testSdkMemberProperties{}
976}
977
978type testSdkMemberProperties struct {
979 android.SdkMemberPropertiesBase
980
Paul Duffina551a1c2020-03-17 21:04:24 +0000981 JarToExport android.Path
982 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000983}
984
Paul Duffin3a4eb502020-03-19 16:11:18 +0000985func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000986 test := variant.(*Test)
987
988 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000989 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000990 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000991 }
992
Paul Duffina551a1c2020-03-17 21:04:24 +0000993 p.JarToExport = implementationJars[0]
994 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000995}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000996
Paul Duffin3a4eb502020-03-19 16:11:18 +0000997func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000998 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000999
Paul Duffina551a1c2020-03-17 21:04:24 +00001000 exportedJar := p.JarToExport
1001 if exportedJar != nil {
1002 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
1003 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001004
1005 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001006 }
1007
1008 testConfig := p.TestConfig
1009 if testConfig != nil {
1010 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1011 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001012 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1013 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001014}
1015
Colin Cross1b16b0e2019-02-12 14:41:32 -08001016// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1017// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1018//
1019// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1020// compiled against the device bootclasspath.
1021//
1022// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1023// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001024func TestFactory() android.Module {
1025 module := &Test{}
1026
Colin Crossce6734e2020-06-15 16:09:53 -07001027 module.addHostAndDeviceProperties()
1028 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001029
Colin Cross9ae1b922018-06-26 17:59:05 -07001030 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001031 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001032 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001033
Paul Duffinb6b89a42021-05-06 16:33:43 +01001034 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001035 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001036 return module
1037}
1038
Paul Duffin42df1442019-03-20 12:45:53 +00001039// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1040func TestHelperLibraryFactory() android.Module {
1041 module := &TestHelperLibrary{}
1042
Colin Crossce6734e2020-06-15 16:09:53 -07001043 module.addHostAndDeviceProperties()
1044 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001045
Colin Cross9a4abed2019-04-24 13:19:28 -07001046 module.Module.properties.Installable = proptools.BoolPtr(true)
1047 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001048 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001049
Paul Duffin42df1442019-03-20 12:45:53 +00001050 InitJavaModule(module, android.HostAndDeviceSupported)
1051 return module
1052}
1053
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001054// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1055// and makes sure that it is added to the appropriate test suite.
1056//
1057// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1058// compiled against an Android classpath.
1059//
1060// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1061// for host modules.
1062func JavaTestImportFactory() android.Module {
1063 module := &JavaTestImport{}
1064
1065 module.AddProperties(
1066 &module.Import.properties,
1067 &module.prebuiltTestProperties)
1068
1069 module.Import.properties.Installable = proptools.BoolPtr(true)
1070
1071 android.InitPrebuiltModule(module, &module.properties.Jars)
1072 android.InitApexModule(module)
1073 android.InitSdkAwareModule(module)
1074 InitJavaModule(module, android.HostAndDeviceSupported)
1075 return module
1076}
1077
Colin Cross1b16b0e2019-02-12 14:41:32 -08001078// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1079// allow running the test with `atest` or a `TEST_MAPPING` file.
1080//
1081// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1082// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001083func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001084 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001085
Colin Crossce6734e2020-06-15 16:09:53 -07001086 module.addHostProperties()
1087 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001088 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001089
Yuexi Ma627263f2021-03-04 13:47:56 -08001090 InitTestHost(
1091 module,
1092 proptools.BoolPtr(true),
1093 nil,
1094 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001095
Liz Kammerdd849a82020-06-12 16:38:45 -07001096 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001097
Colin Cross05638fc2018-04-09 18:40:24 -07001098 return module
1099}
1100
Yuexi Ma627263f2021-03-04 13:47:56 -08001101func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1102 th.properties.Installable = installable
1103 th.testProperties.Auto_gen_config = autoGenConfig
1104 th.testProperties.Test_suites = testSuites
1105}
1106
Colin Cross05638fc2018-04-09 18:40:24 -07001107//
Colin Cross2fe66872015-03-30 17:20:39 -07001108// Java Binaries (.jar file plus wrapper script)
1109//
1110
Colin Crossf506d872017-07-19 15:53:04 -07001111type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001112 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001113 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001114
1115 // Name of the class containing main to be inserted into the manifest as Main-Class.
1116 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001117
1118 // Names of modules containing JNI libraries that should be installed alongside the host
1119 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001120 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001121}
1122
Colin Crossf506d872017-07-19 15:53:04 -07001123type Binary struct {
1124 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001125
Colin Crossf506d872017-07-19 15:53:04 -07001126 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001127
Colin Cross6b4a32d2017-12-05 13:42:45 -08001128 isWrapperVariant bool
1129
Colin Crossc3315992017-12-08 19:12:36 -08001130 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001131 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001132}
1133
Alex Light24237172017-10-26 09:46:21 -07001134func (j *Binary) HostToolPath() android.OptionalPath {
1135 return android.OptionalPathForPath(j.binaryFile)
1136}
1137
Colin Crossf506d872017-07-19 15:53:04 -07001138func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001139 if ctx.Arch().ArchType == android.Common {
1140 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001141 if j.binaryProperties.Main_class != nil {
1142 if j.properties.Manifest != nil {
1143 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1144 }
1145 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1146 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1147 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1148 }
1149
Colin Cross6b4a32d2017-12-05 13:42:45 -08001150 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001151 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001152 // Handle the binary wrapper
1153 j.isWrapperVariant = true
1154
Colin Cross366938f2017-12-11 16:29:02 -08001155 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001156 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001157 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001158 if ctx.Windows() {
1159 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1160 }
1161
Colin Cross6b4a32d2017-12-05 13:42:45 -08001162 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1163 }
1164
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001165 ext := ""
1166 if ctx.Windows() {
1167 ext = ".bat"
1168 }
1169
Colin Crossc179ea62020-10-09 10:54:15 -07001170 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001171 // of the wrapper variant, which will include the common variant's jar file and any JNI
1172 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001173 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001174 ctx.ModuleName()+ext, j.wrapperFile)
1175 }
Colin Cross2fe66872015-03-30 17:20:39 -07001176}
1177
Colin Crossf506d872017-07-19 15:53:04 -07001178func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001179 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001180 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001181 }
1182 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001183 // These dependencies ensure the host installation rules will install the jar file and
1184 // the jni libraries when the wrapper is installed.
1185 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1186 ctx.AddVariationDependencies(
1187 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1188 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001189 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001190}
1191
Colin Cross1b16b0e2019-02-12 14:41:32 -08001192// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1193// as well.
1194//
1195// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1196// compiled against the device bootclasspath.
1197//
1198// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1199// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001200func BinaryFactory() android.Module {
1201 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001202
Colin Crossce6734e2020-06-15 16:09:53 -07001203 module.addHostAndDeviceProperties()
1204 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001205
Colin Cross9ae1b922018-06-26 17:59:05 -07001206 module.Module.properties.Installable = proptools.BoolPtr(true)
1207
Colin Cross6b4a32d2017-12-05 13:42:45 -08001208 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1209 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001210 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001211}
1212
Colin Cross1b16b0e2019-02-12 14:41:32 -08001213// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1214//
1215// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1216// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001217func BinaryHostFactory() android.Module {
1218 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001219
Colin Crossce6734e2020-06-15 16:09:53 -07001220 module.addHostProperties()
1221 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001222
Colin Cross9ae1b922018-06-26 17:59:05 -07001223 module.Module.properties.Installable = proptools.BoolPtr(true)
1224
Colin Cross6b4a32d2017-12-05 13:42:45 -08001225 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1226 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001227 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001228}
1229
1230//
1231// Java prebuilts
1232//
1233
Colin Cross74d73e22017-08-02 11:05:49 -07001234type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001235 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001236
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001237 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1238 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001239 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001240
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001241 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1242 // specified.
1243 Min_sdk_version *string
1244
Colin Cross535e2cf2017-10-20 17:57:49 -07001245 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001246
Paul Duffin869de142021-07-15 14:14:41 +01001247 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001248 Permitted_packages []string
1249
Jiyong Park1be96912018-05-28 18:02:19 +09001250 // List of shared java libs that this module has dependencies to
1251 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001252
1253 // List of files to remove from the jar file(s)
1254 Exclude_files []string
1255
1256 // List of directories to remove from the jar file(s)
1257 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001258
1259 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001260 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001261
1262 // set the name of the output
1263 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001264
1265 Aidl struct {
1266 // directories that should be added as include directories for any aidl sources of modules
1267 // that depend on this module, as well as to aidl for this module.
1268 Export_include_dirs []string
1269 }
Colin Cross74d73e22017-08-02 11:05:49 -07001270}
1271
1272type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001273 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001274 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001275 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001276 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001277 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001278
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001279 // Functionality common to Module and Import.
1280 embeddableInModuleAndImport
1281
Liz Kammerd6c31d22020-08-05 15:40:41 -07001282 hiddenAPI
1283 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001284 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001285
Colin Cross74d73e22017-08-02 11:05:49 -07001286 properties ImportProperties
1287
Liz Kammerd6c31d22020-08-05 15:40:41 -07001288 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001289 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001290 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001291
Colin Cross0a6e0072017-08-30 14:24:55 -07001292 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001293 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001294 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001295
1296 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001297
1298 sdkVersion android.SdkSpec
1299 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001300}
1301
Paul Duffin630b11e2021-07-15 13:35:26 +01001302var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1303
1304func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1305 return j.properties.Permitted_packages
1306}
1307
Jiyong Park92315372021-04-02 08:45:46 +09001308func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1309 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001310}
1311
Jiyong Parkf1691d22021-03-29 20:11:58 +09001312func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001313 return "none"
1314}
1315
Jiyong Park92315372021-04-02 08:45:46 +09001316func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001317 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001318 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001319 }
Jiyong Park92315372021-04-02 08:45:46 +09001320 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001321}
1322
Jiyong Park92315372021-04-02 08:45:46 +09001323func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1324 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001325}
1326
Colin Cross74d73e22017-08-02 11:05:49 -07001327func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001328 return &j.prebuilt
1329}
1330
Colin Cross74d73e22017-08-02 11:05:49 -07001331func (j *Import) PrebuiltSrcs() []string {
1332 return j.properties.Jars
1333}
1334
1335func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001336 return j.prebuilt.Name(j.ModuleBase.Name())
1337}
1338
Jiyong Park0b238752019-10-29 11:23:10 +09001339func (j *Import) Stem() string {
1340 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1341}
1342
Jiyong Park618922e2020-01-08 13:35:43 +09001343func (a *Import) JacocoReportClassesFile() android.Path {
1344 return nil
1345}
1346
Bill Peckhama41a6962021-01-11 10:58:54 -08001347func (j *Import) LintDepSets() LintDepSets {
1348 return LintDepSets{}
1349}
1350
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001351func (j *Import) getStrictUpdatabilityLinting() bool {
1352 return false
1353}
1354
1355func (j *Import) setStrictUpdatabilityLinting(bool) {
1356}
1357
Colin Cross74d73e22017-08-02 11:05:49 -07001358func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001359 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001360
1361 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001362 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001363 }
Colin Cross1e676be2016-10-12 14:38:15 -07001364}
1365
Colin Cross74d73e22017-08-02 11:05:49 -07001366func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001367 j.sdkVersion = j.SdkVersion(ctx)
1368 j.minSdkVersion = j.MinSdkVersion(ctx)
1369
Colin Cross56a83212020-09-15 18:30:11 -07001370 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1371 j.hideApexVariantFromMake = true
1372 }
1373
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001374 if ctx.Windows() {
1375 j.HideFromMake()
1376 }
1377
Colin Cross8a497952019-03-05 22:25:09 -08001378 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001379
Jiyong Park0b238752019-10-29 11:23:10 +09001380 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001381 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001382 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1383 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001384 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001385 inputFile := outputFile
1386 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1387 TransformJetifier(ctx, outputFile, inputFile)
1388 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001389 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001390 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001391
Liz Kammerd6c31d22020-08-05 15:40:41 -07001392 var flags javaBuilderFlags
1393
Jiyong Park1be96912018-05-28 18:02:19 +09001394 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001395 tag := ctx.OtherModuleDependencyTag(module)
1396
Colin Crossdcf71b22021-02-01 13:59:03 -08001397 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1398 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001399 switch tag {
1400 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001401 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001402 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001403 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001404 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001405 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001406 switch tag {
1407 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001408 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001409 }
1410 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001411
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001412 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001413 })
1414
Nan Zhang4973ecf2018-08-10 13:42:12 -07001415 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001416 var installDir android.InstallPath
1417 if ctx.InstallInTestcases() {
1418 var archDir string
1419 if !ctx.Host() {
1420 archDir = ctx.DeviceConfig().DeviceArch()
1421 }
1422 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1423 } else {
1424 installDir = android.PathForModuleInstall(ctx, "framework")
1425 }
1426 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001427 }
Jiyong Park19604de2020-03-24 16:44:11 +09001428
1429 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001430
Paul Duffin064b70c2020-11-02 17:32:38 +00001431 if ctx.Device() {
1432 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1433 // obtained from the associated deapexer module.
1434 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1435 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001436 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001437 di := android.FindDeapexerProviderForModule(ctx)
1438 if di == nil {
1439 return // An error has been reported by FindDeapexerProviderForModule.
1440 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001441 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001442 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1443 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001444 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1445 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001446
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001447 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001448 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001449 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1450 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001451
1452 // Initialize the hiddenapi structure.
1453 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001454 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001455 // This should never happen as a variant for a prebuilt_apex is only created if the
1456 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001457 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001458 }
1459 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001460 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001461 if sdkDep.invalidVersion {
1462 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1463 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1464 } else if sdkDep.useFiles {
1465 // sdkDep.jar is actually equivalent to turbine header.jar.
1466 flags.classpath = append(flags.classpath, sdkDep.jars...)
1467 }
1468
1469 // Dex compilation
1470
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001471 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1472 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001473 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001474 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1475
Paul Duffin612e6102021-02-02 13:38:13 +00001476 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001477 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001478 if ctx.Failed() {
1479 return
1480 }
1481
Paul Duffin74d18d12021-05-14 14:18:47 +01001482 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001483 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001484
1485 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001486 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001487
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001488 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001489 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001490 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001491 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001492
1493 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1494 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1495 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1496 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1497 AidlIncludeDirs: j.exportAidlIncludeDirs,
1498 })
Colin Cross2fe66872015-03-30 17:20:39 -07001499}
1500
Paul Duffinaa55f742020-10-06 17:20:13 +01001501func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1502 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001503 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001504 return android.Paths{j.combinedClasspathFile}, nil
1505 default:
1506 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1507 }
1508}
1509
1510var _ android.OutputFileProducer = (*Import)(nil)
1511
Nan Zhanged19fc32017-10-19 13:06:22 -07001512func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001513 if j.combinedClasspathFile == nil {
1514 return nil
1515 }
Colin Cross37f6d792018-07-12 12:28:41 -07001516 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001517}
1518
Colin Cross331a1212018-08-15 20:40:52 -07001519func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001520 if j.combinedClasspathFile == nil {
1521 return nil
1522 }
Colin Cross331a1212018-08-15 20:40:52 -07001523 return android.Paths{j.combinedClasspathFile}
1524}
1525
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001526func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001527 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001528}
1529
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001530func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001531 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001532}
1533
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001534func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1535 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001536}
1537
Jiyong Park45bf82e2020-12-15 22:29:02 +09001538var _ android.ApexModule = (*Import)(nil)
1539
1540// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001541func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001542 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001543}
1544
Jiyong Park45bf82e2020-12-15 22:29:02 +09001545// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001546func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1547 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001548 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001549 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001550 return fmt.Errorf("min_sdk_version is not specified")
1551 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001552 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001553 return nil
1554 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001555 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1556 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001557 }
Jooyung Han749dc692020-04-15 11:03:39 +09001558 return nil
1559}
1560
Paul Duffinfef55002021-06-17 14:56:05 +01001561// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1562// java_sdk_library_import with the specified base module name requires to be exported from a
1563// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001564func requiredFilesFromPrebuiltApexForImport(name string) []string {
1565 // Add the dex implementation jar to the set of exported files.
1566 return []string{
1567 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001568 }
1569}
1570
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001571// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1572// the java library with the specified name.
1573func apexRootRelativePathToJavaLib(name string) string {
1574 return filepath.Join("javalib", name+".jar")
1575}
1576
Paul Duffinfef55002021-06-17 14:56:05 +01001577var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1578
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001579func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001580 name := j.BaseModuleName()
1581 return requiredFilesFromPrebuiltApexForImport(name)
1582}
1583
albaltai36ff7dc2018-12-25 14:35:23 +08001584// Add compile time check for interface implementation
1585var _ android.IDEInfo = (*Import)(nil)
1586var _ android.IDECustomizedModuleName = (*Import)(nil)
1587
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001588// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001589
1590func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1591 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1592}
1593
1594func (j *Import) IDECustomizedModuleName() string {
1595 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1596 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1597 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001598 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001599}
1600
Colin Cross74d73e22017-08-02 11:05:49 -07001601var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001602
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001603func (j *Import) IsInstallable() bool {
1604 return Bool(j.properties.Installable)
1605}
1606
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001607var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001608
Colin Cross1b16b0e2019-02-12 14:41:32 -08001609// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1610//
1611// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1612// compiled against an Android classpath.
1613//
1614// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1615// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001616func ImportFactory() android.Module {
1617 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001618
Liz Kammerd6c31d22020-08-05 15:40:41 -07001619 module.AddProperties(
1620 &module.properties,
1621 &module.dexer.dexProperties,
1622 )
Colin Cross74d73e22017-08-02 11:05:49 -07001623
Paul Duffin71b33cc2021-06-23 11:39:47 +01001624 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001625
Liz Kammerd6c31d22020-08-05 15:40:41 -07001626 module.dexProperties.Optimize.EnabledByDefault = false
1627
Colin Cross74d73e22017-08-02 11:05:49 -07001628 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001629 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001630 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001631 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001632 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001633}
1634
Colin Cross1b16b0e2019-02-12 14:41:32 -08001635// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1636// module.
1637//
1638// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1639// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001640func ImportFactoryHost() android.Module {
1641 module := &Import{}
1642
1643 module.AddProperties(&module.properties)
1644
1645 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001646 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001647 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001648 return module
1649}
1650
Colin Cross42be7612019-02-21 18:12:14 -08001651// dex_import module
1652
1653type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001654 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001655
1656 // set the name of the output
1657 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001658}
1659
1660type DexImport struct {
1661 android.ModuleBase
1662 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001663 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001664 prebuilt android.Prebuilt
1665
1666 properties DexImportProperties
1667
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001668 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001669
1670 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001671
1672 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001673}
1674
1675func (j *DexImport) Prebuilt() *android.Prebuilt {
1676 return &j.prebuilt
1677}
1678
1679func (j *DexImport) PrebuiltSrcs() []string {
1680 return j.properties.Jars
1681}
1682
1683func (j *DexImport) Name() string {
1684 return j.prebuilt.Name(j.ModuleBase.Name())
1685}
1686
Jiyong Park0b238752019-10-29 11:23:10 +09001687func (j *DexImport) Stem() string {
1688 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1689}
1690
Jiyong Park77acec62020-06-01 21:39:15 +09001691func (a *DexImport) JacocoReportClassesFile() android.Path {
1692 return nil
1693}
1694
Colin Cross08dca382020-07-21 20:31:17 -07001695func (a *DexImport) LintDepSets() LintDepSets {
1696 return LintDepSets{}
1697}
1698
Martin Stjernholm6d415272020-01-31 17:10:36 +00001699func (j *DexImport) IsInstallable() bool {
1700 return true
1701}
1702
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001703func (j *DexImport) getStrictUpdatabilityLinting() bool {
1704 return false
1705}
1706
1707func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1708}
1709
Colin Cross42be7612019-02-21 18:12:14 -08001710func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1711 if len(j.properties.Jars) != 1 {
1712 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1713 }
1714
Colin Cross56a83212020-09-15 18:30:11 -07001715 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1716 if !apexInfo.IsForPlatform() {
1717 j.hideApexVariantFromMake = true
1718 }
1719
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001720 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1721 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001722 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1723
1724 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1725 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1726
1727 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001728 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001729
1730 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1731 rule.Temporary(temporary)
1732
1733 // use zip2zip to uncompress classes*.dex files
1734 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001735 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001736 FlagWithInput("-i ", inputJar).
1737 FlagWithOutput("-o ", temporary).
1738 FlagWithArg("-0 ", "'classes*.dex'")
1739
1740 // use zipalign to align uncompressed classes*.dex files
1741 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001742 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001743 Flag("-f").
1744 Text("4").
1745 Input(temporary).
1746 Output(dexOutputFile)
1747
1748 rule.DeleteTemporaryFiles()
1749
Colin Crossf1a035e2020-11-16 17:32:30 -08001750 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001751 } else {
1752 ctx.Build(pctx, android.BuildParams{
1753 Rule: android.Cp,
1754 Input: inputJar,
1755 Output: dexOutputFile,
1756 })
1757 }
1758
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001759 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001760
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001761 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001762
Colin Cross56a83212020-09-15 18:30:11 -07001763 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001764 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1765 j.Stem()+".jar", dexOutputFile)
1766 }
Colin Cross42be7612019-02-21 18:12:14 -08001767}
1768
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001769func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001770 return j.dexJarFile
1771}
1772
Jiyong Park45bf82e2020-12-15 22:29:02 +09001773var _ android.ApexModule = (*DexImport)(nil)
1774
1775// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001776func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1777 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001778 // we don't check prebuilt modules for sdk_version
1779 return nil
1780}
1781
Colin Cross42be7612019-02-21 18:12:14 -08001782// dex_import imports a `.jar` file containing classes.dex files.
1783//
1784// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1785// to the device.
1786func DexImportFactory() android.Module {
1787 module := &DexImport{}
1788
1789 module.AddProperties(&module.properties)
1790
1791 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001792 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001793 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001794 return module
1795}
1796
Colin Cross89536d42017-07-07 14:35:50 -07001797//
1798// Defaults
1799//
1800type Defaults struct {
1801 android.ModuleBase
1802 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001803 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001804}
1805
Colin Cross1b16b0e2019-02-12 14:41:32 -08001806// java_defaults provides a set of properties that can be inherited by other java or android modules.
1807//
1808// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1809// property in the defaults module that exists in the depending module will be prepended to the depending module's
1810// value for that property.
1811//
1812// Example:
1813//
1814// java_defaults {
1815// name: "example_defaults",
1816// srcs: ["common/**/*.java"],
1817// javacflags: ["-Xlint:all"],
1818// aaptflags: ["--auto-add-overlay"],
1819// }
1820//
1821// java_library {
1822// name: "example",
1823// defaults: ["example_defaults"],
1824// srcs: ["example/**/*.java"],
1825// }
1826//
1827// is functionally identical to:
1828//
1829// java_library {
1830// name: "example",
1831// srcs: [
1832// "common/**/*.java",
1833// "example/**/*.java",
1834// ],
1835// javacflags: ["-Xlint:all"],
1836// }
Paul Duffin47357662019-12-05 14:07:14 +00001837func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001838 module := &Defaults{}
1839
Colin Cross89536d42017-07-07 14:35:50 -07001840 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001841 &CommonProperties{},
1842 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001843 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001844 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001845 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001846 &aaptProperties{},
1847 &androidLibraryProperties{},
1848 &appProperties{},
1849 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001850 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001851 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001852 &ImportProperties{},
1853 &AARImportProperties{},
1854 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001855 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001856 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001857 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001858 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001859 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001860 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001861 )
1862
1863 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001864 return module
1865}
Nan Zhangea568a42017-11-08 21:20:04 -08001866
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001867func kytheExtractJavaFactory() android.Singleton {
1868 return &kytheExtractJavaSingleton{}
1869}
1870
1871type kytheExtractJavaSingleton struct {
1872}
1873
1874func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1875 var xrefTargets android.Paths
1876 ctx.VisitAllModules(func(module android.Module) {
1877 if javaModule, ok := module.(xref); ok {
1878 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1879 }
1880 })
1881 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1882 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001883 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001884 }
1885}
1886
Nan Zhangea568a42017-11-08 21:20:04 -08001887var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001888var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001889var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001890var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001891
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001892// Add class loader context (CLC) of a given dependency to the current CLC.
1893func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1894 clcMap dexpreopt.ClassLoaderContextMap) {
1895
1896 dep, ok := depModule.(UsesLibraryDependency)
1897 if !ok {
1898 return
1899 }
1900
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001901 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1902
1903 var sdkLib *string
1904 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1905 // A shared SDK library. This should be added as a top-level CLC element.
1906 sdkLib = &depName
1907 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1908 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1909 // property. This should be handled in the same way as a shared SDK library.
1910 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001911 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001912
1913 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001914 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001915 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001916 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1917 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1918 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1919 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001920 } else if depTag == staticLibTag {
1921 // Propagate <uses-library> through static library dependencies, unless it is a component
1922 // library (such as stubs). Component libraries have a dependency on their SDK library,
1923 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001924 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001925 return
1926 }
1927 } else {
1928 // Don't propagate <uses-library> for other dependency tags.
1929 return
1930 }
1931
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001932 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1933 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1934 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1935 // from its CLC should be added to the current CLC.
1936 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001937 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001938 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001939 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001940 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1941 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001942}