blob: 90a1b8999df340c59d8c95ca5934381fab77016a [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{
299 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
300 sdkVersion: sdkVersion,
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100301 optional: optional,
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +0100302 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"}
313 libTag = dependencyTag{name: "javalib"}
314 java9LibTag = dependencyTag{name: "java9lib"}
315 pluginTag = dependencyTag{name: "plugin"}
316 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
317 exportedPluginTag = dependencyTag{name: "exported-plugin"}
318 bootClasspathTag = dependencyTag{name: "bootclasspath"}
319 systemModulesTag = dependencyTag{name: "system modules"}
320 frameworkResTag = dependencyTag{name: "framework-res"}
321 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"}
328 jniLibTag = dependencyTag{name: "jnilib"}
329 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))
Sorin Basca8ef3e6f2021-11-26 17:27:24 +0000437 } else if ctx.Config().IsEnvTrue("EXPERIMENTAL_TARGET_JAVA_VERSION_11") {
438 // Temporary experimental flag to be able to try and build with
439 // java version 11 options. The flag, if used, just sets Java
440 // 11 as the default version, leaving any components that
441 // target an older version intact.
442 return JAVA_VERSION_11
Nan Zhang357466b2018-04-17 17:38:36 -0700443 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700444 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700445 }
Nan Zhang357466b2018-04-17 17:38:36 -0700446}
447
Colin Cross1e743852019-10-28 11:37:20 -0700448type javaVersion int
449
450const (
451 JAVA_VERSION_UNSUPPORTED = 0
452 JAVA_VERSION_6 = 6
453 JAVA_VERSION_7 = 7
454 JAVA_VERSION_8 = 8
455 JAVA_VERSION_9 = 9
Sorin Bascac0244da2021-11-26 17:26:33 +0000456 JAVA_VERSION_11 = 11
Colin Cross1e743852019-10-28 11:37:20 -0700457)
458
459func (v javaVersion) String() string {
460 switch v {
461 case JAVA_VERSION_6:
462 return "1.6"
463 case JAVA_VERSION_7:
464 return "1.7"
465 case JAVA_VERSION_8:
466 return "1.8"
467 case JAVA_VERSION_9:
468 return "1.9"
Sorin Bascac0244da2021-11-26 17:26:33 +0000469 case JAVA_VERSION_11:
470 return "11"
Colin Cross1e743852019-10-28 11:37:20 -0700471 default:
472 return "unsupported"
473 }
474}
475
476// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
477func (v javaVersion) usesJavaModules() bool {
478 return v >= 9
479}
480
481func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100482 switch javaVersion {
483 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700484 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100485 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700486 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100487 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700488 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100489 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700490 return JAVA_VERSION_9
Sorin Bascac0244da2021-11-26 17:26:33 +0000491 case "11":
492 return JAVA_VERSION_11
493 case "10":
494 ctx.PropertyErrorf("java_version", "Java language levels 10 is not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700495 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100496 default:
497 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700498 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100499 }
500}
501
Colin Cross2fe66872015-03-30 17:20:39 -0700502//
503// Java libraries (.jar file)
504//
505
Colin Crossf506d872017-07-19 15:53:04 -0700506type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700507 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700508
509 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700510}
511
Jiyong Park45bf82e2020-12-15 22:29:02 +0900512var _ android.ApexModule = (*Library)(nil)
513
satayevd604b212021-07-21 14:23:52 +0100514// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100515type PermittedPackagesForUpdatableBootJars interface {
516 PermittedPackagesForUpdatableBootJars() []string
517}
518
519var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
520
521func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
522 return j.properties.Permitted_packages
523}
524
Colin Cross42be7612019-02-21 18:12:14 -0800525func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000526 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700527 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000528 return true
529 }
530
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000531 // Store uncompressed (and do not strip) dex files from boot class path jars.
532 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
533 return true
534 }
535
536 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000537 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000538 return true
539 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800540 if ctx.Config().UncompressPrivAppDex() &&
541 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
542 return true
543 }
544
Colin Cross2fc72f62018-12-21 12:59:54 -0800545 return false
546}
547
Jiakai Zhang22450f22021-10-11 03:05:20 +0000548// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
549func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
550 if dexer.dexProperties.Uncompress_dex == nil {
551 // If the value was not force-set by the user, use reasonable default based on the module.
552 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
553 }
554}
555
Colin Crossf506d872017-07-19 15:53:04 -0700556func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900557 j.sdkVersion = j.SdkVersion(ctx)
558 j.minSdkVersion = j.MinSdkVersion(ctx)
559
Colin Cross56a83212020-09-15 18:30:11 -0700560 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
561 if !apexInfo.IsForPlatform() {
562 j.hideApexVariantFromMake = true
563 }
564
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100565 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000566 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
567 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800568 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000569 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700570 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100571 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700572 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700573
bralee1fbf4402020-05-21 10:11:59 +0800574 // Collect the module directory for IDE info in java/jdeps.go.
575 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
576
Colin Cross56a83212020-09-15 18:30:11 -0700577 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900578 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700579 var extraInstallDeps android.Paths
580 if j.InstallMixin != nil {
581 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
582 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700583 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
584 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800585 j.hostdexInstallFile = ctx.InstallFile(
586 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700587 j.Stem()+"-hostdex.jar", j.outputFile)
588 }
589 var installDir android.InstallPath
590 if ctx.InstallInTestcases() {
591 var archDir string
592 if !ctx.Host() {
593 archDir = ctx.DeviceConfig().DeviceArch()
594 }
595 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
596 } else {
597 installDir = android.PathForModuleInstall(ctx, "framework")
598 }
599 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700600 }
Colin Crossb7a63242015-04-16 14:09:14 -0700601}
602
Colin Crossf506d872017-07-19 15:53:04 -0700603func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700604 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100605 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700606}
607
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000608const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000609 aidlIncludeDir = "aidl"
610 javaDir = "java"
611 jarFileSuffix = ".jar"
612 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000613)
614
Paul Duffina0dbf432019-12-05 11:25:53 +0000615// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000616func sdkSnapshotFilePathForJar(osPrefix, name string) string {
617 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000618}
619
Paul Duffina04c1072020-03-02 10:16:35 +0000620func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
621 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000622}
623
Paul Duffin13879572019-11-28 14:31:38 +0000624type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000625 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000626
627 // Function to retrieve the appropriate output jar (implementation or header) from
628 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000629 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
630
631 // Function to compute the snapshot relative path to which the named library's
632 // jar should be copied.
633 snapshotPathGetter func(osPrefix, name string) string
634
635 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
636 // files like aidl files should also be copied.
637 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000638}
639
Paul Duffindb170e42020-12-08 17:48:25 +0000640const (
641 onlyCopyJarToSnapshot = true
642 copyEverythingToSnapshot = false
643)
644
Paul Duffin296701e2021-07-14 10:29:36 +0100645func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
646 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000647}
648
649func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
650 _, ok := module.(*Library)
651 return ok
652}
653
Paul Duffin3a4eb502020-03-19 16:11:18 +0000654func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
655 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000656}
Paul Duffina0dbf432019-12-05 11:25:53 +0000657
Paul Duffin14eb4672020-03-02 11:33:02 +0000658func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000659 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000660}
661
662type librarySdkMemberProperties struct {
663 android.SdkMemberPropertiesBase
664
Paul Duffin864e1b42020-05-06 10:23:19 +0100665 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000666 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100667
668 // The list of permitted packages that need to be passed to the prebuilts as they are used to
669 // create the updatable-bcp-packages.txt file.
670 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000671}
672
Paul Duffin3a4eb502020-03-19 16:11:18 +0000673func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000674 j := variant.(*Library)
675
Paul Duffindb170e42020-12-08 17:48:25 +0000676 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
677
Paul Duffina551a1c2020-03-17 21:04:24 +0000678 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100679
680 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000681}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000682
Paul Duffin3a4eb502020-03-19 16:11:18 +0000683func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000684 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000685
Paul Duffindb170e42020-12-08 17:48:25 +0000686 memberType := ctx.MemberType().(*librarySdkMemberType)
687
Paul Duffina551a1c2020-03-17 21:04:24 +0000688 exportedJar := p.JarToExport
689 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000690 // Delegate the creation of the snapshot relative path to the member type.
691 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
692
693 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000694 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
695
Paul Duffina551a1c2020-03-17 21:04:24 +0000696 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
697 }
698
Paul Duffin869de142021-07-15 14:14:41 +0100699 if len(p.PermittedPackages) > 0 {
700 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
701 }
702
Paul Duffindb170e42020-12-08 17:48:25 +0000703 // Do not copy anything else to the snapshot.
704 if memberType.onlyCopyJarToSnapshot {
705 return
706 }
707
Paul Duffina551a1c2020-03-17 21:04:24 +0000708 aidlIncludeDirs := p.AidlIncludeDirs
709 if len(aidlIncludeDirs) != 0 {
710 sdkModuleContext := ctx.SdkModuleContext()
711 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000712 // TODO(jiyong): copy parcelable declarations only
713 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
714 for _, file := range aidlFiles {
715 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
716 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000717 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000718
Paul Duffina551a1c2020-03-17 21:04:24 +0000719 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000720 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000721}
722
Colin Cross1b16b0e2019-02-12 14:41:32 -0800723// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
724//
725// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
726// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
727// as a `static_libs` dependency of another module.
728//
729// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
730// a device.
731//
732// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
733// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700734func LibraryFactory() android.Module {
735 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700736
Colin Crossce6734e2020-06-15 16:09:53 -0700737 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700738
Paul Duffin71b33cc2021-06-23 11:39:47 +0100739 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100740
Jiyong Park7f7766d2019-07-25 22:02:35 +0900741 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900742 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900743 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700744 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700745}
746
Colin Cross1b16b0e2019-02-12 14:41:32 -0800747// java_library_static is an obsolete alias for java_library.
748func LibraryStaticFactory() android.Module {
749 return LibraryFactory()
750}
751
752// java_library_host builds and links sources into a `.jar` file for the host.
753//
754// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
755// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700756func LibraryHostFactory() android.Module {
757 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700758
Colin Crossce6734e2020-06-15 16:09:53 -0700759 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700760
Colin Cross9ae1b922018-06-26 17:59:05 -0700761 module.Module.properties.Installable = proptools.BoolPtr(true)
762
Jiyong Park7f7766d2019-07-25 22:02:35 +0900763 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100764 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900765 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700766 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700767}
768
769//
Colin Crossb628ea52018-08-14 16:42:33 -0700770// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700771//
772
Dan Shi95d19422020-08-15 12:24:26 -0700773// Test option struct.
774type TestOptions struct {
775 // a list of extra test configuration files that should be installed with the module.
776 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800777
778 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
779 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700780}
781
Colin Cross05638fc2018-04-09 18:40:24 -0700782type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700783 // list of compatibility suites (for example "cts", "vts") that the module should be
784 // installed into.
785 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700786
787 // the name of the test configuration (for example "AndroidTest.xml") that should be
788 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800789 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700790
Jack He33338892018-09-19 02:21:28 -0700791 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
792 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800793 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700794
Colin Crossd96ca352018-08-10 16:06:24 -0700795 // list of files or filegroup modules that provide data that should be installed alongside
796 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900797 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700798
799 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
800 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
801 // explicitly.
802 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800803
804 // Add parameterized mainline modules to auto generated test config. The options will be
805 // handled by TradeFed to do downloading and installing the specified modules on the device.
806 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700807
808 // Test options.
809 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800810
811 // Names of modules containing JNI libraries that should be installed alongside the test.
812 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700813
814 // Install the test into a folder named for the module in all test suites.
815 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700816}
817
Liz Kammerdd849a82020-06-12 16:38:45 -0700818type hostTestProperties struct {
819 // list of native binary modules that should be installed alongside the test
820 Data_native_bins []string `android:"arch_variant"`
821}
822
Paul Duffin42df1442019-03-20 12:45:53 +0000823type testHelperLibraryProperties struct {
824 // list of compatibility suites (for example "cts", "vts") that the module should be
825 // installed into.
826 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700827
828 // Install the test into a folder named for the module in all test suites.
829 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000830}
831
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000832type prebuiltTestProperties struct {
833 // list of compatibility suites (for example "cts", "vts") that the module should be
834 // installed into.
835 Test_suites []string `android:"arch_variant"`
836
837 // the name of the test configuration (for example "AndroidTest.xml") that should be
838 // installed with the module.
839 Test_config *string `android:"path,arch_variant"`
840}
841
Colin Cross05638fc2018-04-09 18:40:24 -0700842type Test struct {
843 Library
844
845 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700846
Dan Shi95d19422020-08-15 12:24:26 -0700847 testConfig android.Path
848 extraTestConfigs android.Paths
849 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700850}
851
Liz Kammerdd849a82020-06-12 16:38:45 -0700852type TestHost struct {
853 Test
854
855 testHostProperties hostTestProperties
856}
857
Paul Duffin42df1442019-03-20 12:45:53 +0000858type TestHelperLibrary struct {
859 Library
860
861 testHelperLibraryProperties testHelperLibraryProperties
862}
863
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000864type JavaTestImport struct {
865 Import
866
867 prebuiltTestProperties prebuiltTestProperties
868
869 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700870 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000871}
872
Colin Cross24cc4be62021-11-03 14:09:41 -0700873func (j *Test) InstallInTestcases() bool {
874 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
875 // testcases by base_rules.mk.
876 return !j.Host()
877}
878
879func (j *TestHelperLibrary) InstallInTestcases() bool {
880 return true
881}
882
883func (j *JavaTestImport) InstallInTestcases() bool {
884 return true
885}
886
Liz Kammerdd849a82020-06-12 16:38:45 -0700887func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
888 if len(j.testHostProperties.Data_native_bins) > 0 {
889 for _, target := range ctx.MultiTargets() {
890 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
891 }
892 }
893
Colin Crossf8d9c492021-01-26 11:01:43 -0800894 if len(j.testProperties.Jni_libs) > 0 {
895 for _, target := range ctx.MultiTargets() {
896 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
897 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
898 }
899 }
900
Liz Kammerdd849a82020-06-12 16:38:45 -0700901 j.deps(ctx)
902}
903
Yuexi Ma627263f2021-03-04 13:47:56 -0800904func (j *TestHost) AddExtraResource(p android.Path) {
905 j.extraResources = append(j.extraResources, p)
906}
907
Colin Cross303e21f2018-08-07 16:49:25 -0700908func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000909 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
910 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700911 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000912 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
913 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700914 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800915 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700916
Colin Cross8a497952019-03-05 22:25:09 -0800917 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700918
Dan Shi95d19422020-08-15 12:24:26 -0700919 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
920
Liz Kammerdd849a82020-06-12 16:38:45 -0700921 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
922 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
923 })
924
Colin Crossf8d9c492021-01-26 11:01:43 -0800925 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
926 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
927 if sharedLibInfo.SharedLibrary != nil {
928 // Copy to an intermediate output directory to append "lib[64]" to the path,
929 // so that it's compatible with the default rpath values.
930 var relPath string
931 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
932 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
933 } else {
934 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
935 }
936 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
937 ctx.Build(pctx, android.BuildParams{
938 Rule: android.Cp,
939 Input: sharedLibInfo.SharedLibrary,
940 Output: relocatedLib,
941 })
942 j.data = append(j.data, relocatedLib)
943 } else {
944 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
945 }
946 })
947
Colin Cross303e21f2018-08-07 16:49:25 -0700948 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700949}
950
Paul Duffin42df1442019-03-20 12:45:53 +0000951func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
952 j.Library.GenerateAndroidBuildActions(ctx)
953}
954
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000955func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
956 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800957 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000958
959 j.Import.GenerateAndroidBuildActions(ctx)
960}
961
962type testSdkMemberType struct {
963 android.SdkMemberTypeBase
964}
965
Paul Duffin296701e2021-07-14 10:29:36 +0100966func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
967 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000968}
969
970func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
971 _, ok := module.(*Test)
972 return ok
973}
974
Paul Duffin3a4eb502020-03-19 16:11:18 +0000975func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
976 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000977}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000978
Paul Duffin14eb4672020-03-02 11:33:02 +0000979func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
980 return &testSdkMemberProperties{}
981}
982
983type testSdkMemberProperties struct {
984 android.SdkMemberPropertiesBase
985
Paul Duffina551a1c2020-03-17 21:04:24 +0000986 JarToExport android.Path
987 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000988}
989
Paul Duffin3a4eb502020-03-19 16:11:18 +0000990func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000991 test := variant.(*Test)
992
993 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000994 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000995 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000996 }
997
Paul Duffina551a1c2020-03-17 21:04:24 +0000998 p.JarToExport = implementationJars[0]
999 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00001000}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001001
Paul Duffin3a4eb502020-03-19 16:11:18 +00001002func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001003 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001004
Paul Duffina551a1c2020-03-17 21:04:24 +00001005 exportedJar := p.JarToExport
1006 if exportedJar != nil {
1007 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
1008 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001009
1010 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001011 }
1012
1013 testConfig := p.TestConfig
1014 if testConfig != nil {
1015 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1016 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001017 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1018 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001019}
1020
Colin Cross1b16b0e2019-02-12 14:41:32 -08001021// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1022// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1023//
1024// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1025// compiled against the device bootclasspath.
1026//
1027// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1028// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001029func TestFactory() android.Module {
1030 module := &Test{}
1031
Colin Crossce6734e2020-06-15 16:09:53 -07001032 module.addHostAndDeviceProperties()
1033 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001034
Colin Cross9ae1b922018-06-26 17:59:05 -07001035 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001036 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001037 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001038
Paul Duffinb6b89a42021-05-06 16:33:43 +01001039 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001040 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001041 return module
1042}
1043
Paul Duffin42df1442019-03-20 12:45:53 +00001044// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1045func TestHelperLibraryFactory() android.Module {
1046 module := &TestHelperLibrary{}
1047
Colin Crossce6734e2020-06-15 16:09:53 -07001048 module.addHostAndDeviceProperties()
1049 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001050
Colin Cross9a4abed2019-04-24 13:19:28 -07001051 module.Module.properties.Installable = proptools.BoolPtr(true)
1052 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001053 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001054
Paul Duffin42df1442019-03-20 12:45:53 +00001055 InitJavaModule(module, android.HostAndDeviceSupported)
1056 return module
1057}
1058
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001059// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1060// and makes sure that it is added to the appropriate test suite.
1061//
1062// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1063// compiled against an Android classpath.
1064//
1065// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1066// for host modules.
1067func JavaTestImportFactory() android.Module {
1068 module := &JavaTestImport{}
1069
1070 module.AddProperties(
1071 &module.Import.properties,
1072 &module.prebuiltTestProperties)
1073
1074 module.Import.properties.Installable = proptools.BoolPtr(true)
1075
1076 android.InitPrebuiltModule(module, &module.properties.Jars)
1077 android.InitApexModule(module)
1078 android.InitSdkAwareModule(module)
1079 InitJavaModule(module, android.HostAndDeviceSupported)
1080 return module
1081}
1082
Colin Cross1b16b0e2019-02-12 14:41:32 -08001083// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1084// allow running the test with `atest` or a `TEST_MAPPING` file.
1085//
1086// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1087// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001088func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001089 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001090
Colin Crossce6734e2020-06-15 16:09:53 -07001091 module.addHostProperties()
1092 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001093 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001094
Yuexi Ma627263f2021-03-04 13:47:56 -08001095 InitTestHost(
1096 module,
1097 proptools.BoolPtr(true),
1098 nil,
1099 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001100
Liz Kammerdd849a82020-06-12 16:38:45 -07001101 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001102
Colin Cross05638fc2018-04-09 18:40:24 -07001103 return module
1104}
1105
Yuexi Ma627263f2021-03-04 13:47:56 -08001106func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1107 th.properties.Installable = installable
1108 th.testProperties.Auto_gen_config = autoGenConfig
1109 th.testProperties.Test_suites = testSuites
1110}
1111
Colin Cross05638fc2018-04-09 18:40:24 -07001112//
Colin Cross2fe66872015-03-30 17:20:39 -07001113// Java Binaries (.jar file plus wrapper script)
1114//
1115
Colin Crossf506d872017-07-19 15:53:04 -07001116type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001117 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001118 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001119
1120 // Name of the class containing main to be inserted into the manifest as Main-Class.
1121 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001122
1123 // Names of modules containing JNI libraries that should be installed alongside the host
1124 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001125 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001126}
1127
Colin Crossf506d872017-07-19 15:53:04 -07001128type Binary struct {
1129 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001130
Colin Crossf506d872017-07-19 15:53:04 -07001131 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001132
Colin Cross6b4a32d2017-12-05 13:42:45 -08001133 isWrapperVariant bool
1134
Colin Crossc3315992017-12-08 19:12:36 -08001135 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001136 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001137}
1138
Alex Light24237172017-10-26 09:46:21 -07001139func (j *Binary) HostToolPath() android.OptionalPath {
1140 return android.OptionalPathForPath(j.binaryFile)
1141}
1142
Colin Crossf506d872017-07-19 15:53:04 -07001143func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001144 if ctx.Arch().ArchType == android.Common {
1145 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001146 if j.binaryProperties.Main_class != nil {
1147 if j.properties.Manifest != nil {
1148 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1149 }
1150 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1151 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1152 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1153 }
1154
Colin Cross6b4a32d2017-12-05 13:42:45 -08001155 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001156 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001157 // Handle the binary wrapper
1158 j.isWrapperVariant = true
1159
Colin Cross366938f2017-12-11 16:29:02 -08001160 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001161 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001162 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001163 if ctx.Windows() {
1164 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1165 }
1166
Colin Cross6b4a32d2017-12-05 13:42:45 -08001167 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1168 }
1169
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001170 ext := ""
1171 if ctx.Windows() {
1172 ext = ".bat"
1173 }
1174
Colin Crossc179ea62020-10-09 10:54:15 -07001175 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001176 // of the wrapper variant, which will include the common variant's jar file and any JNI
1177 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001178 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001179 ctx.ModuleName()+ext, j.wrapperFile)
1180 }
Colin Cross2fe66872015-03-30 17:20:39 -07001181}
1182
Colin Crossf506d872017-07-19 15:53:04 -07001183func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001184 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001185 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001186 }
1187 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001188 // These dependencies ensure the host installation rules will install the jar file and
1189 // the jni libraries when the wrapper is installed.
1190 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1191 ctx.AddVariationDependencies(
1192 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1193 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001194 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001195}
1196
Colin Cross1b16b0e2019-02-12 14:41:32 -08001197// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1198// as well.
1199//
1200// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1201// compiled against the device bootclasspath.
1202//
1203// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1204// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001205func BinaryFactory() android.Module {
1206 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001207
Colin Crossce6734e2020-06-15 16:09:53 -07001208 module.addHostAndDeviceProperties()
1209 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001210
Colin Cross9ae1b922018-06-26 17:59:05 -07001211 module.Module.properties.Installable = proptools.BoolPtr(true)
1212
Colin Cross6b4a32d2017-12-05 13:42:45 -08001213 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1214 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001215 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001216}
1217
Colin Cross1b16b0e2019-02-12 14:41:32 -08001218// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1219//
1220// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1221// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001222func BinaryHostFactory() android.Module {
1223 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001224
Colin Crossce6734e2020-06-15 16:09:53 -07001225 module.addHostProperties()
1226 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001227
Colin Cross9ae1b922018-06-26 17:59:05 -07001228 module.Module.properties.Installable = proptools.BoolPtr(true)
1229
Colin Cross6b4a32d2017-12-05 13:42:45 -08001230 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1231 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001232 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001233}
1234
1235//
1236// Java prebuilts
1237//
1238
Colin Cross74d73e22017-08-02 11:05:49 -07001239type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001240 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001241
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001242 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1243 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001244 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001245
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001246 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1247 // specified.
1248 Min_sdk_version *string
1249
Colin Cross535e2cf2017-10-20 17:57:49 -07001250 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001251
Paul Duffin869de142021-07-15 14:14:41 +01001252 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001253 Permitted_packages []string
1254
Jiyong Park1be96912018-05-28 18:02:19 +09001255 // List of shared java libs that this module has dependencies to
1256 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001257
1258 // List of files to remove from the jar file(s)
1259 Exclude_files []string
1260
1261 // List of directories to remove from the jar file(s)
1262 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001263
1264 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001265 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001266
1267 // set the name of the output
1268 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001269
1270 Aidl struct {
1271 // directories that should be added as include directories for any aidl sources of modules
1272 // that depend on this module, as well as to aidl for this module.
1273 Export_include_dirs []string
1274 }
Colin Cross74d73e22017-08-02 11:05:49 -07001275}
1276
1277type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001278 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001279 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001280 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001281 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001282 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001283
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001284 // Functionality common to Module and Import.
1285 embeddableInModuleAndImport
1286
Liz Kammerd6c31d22020-08-05 15:40:41 -07001287 hiddenAPI
1288 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001289 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001290
Colin Cross74d73e22017-08-02 11:05:49 -07001291 properties ImportProperties
1292
Liz Kammerd6c31d22020-08-05 15:40:41 -07001293 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001294 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001295 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001296
Colin Cross0a6e0072017-08-30 14:24:55 -07001297 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001298 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001299 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001300
1301 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001302
1303 sdkVersion android.SdkSpec
1304 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001305}
1306
Paul Duffin630b11e2021-07-15 13:35:26 +01001307var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1308
1309func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1310 return j.properties.Permitted_packages
1311}
1312
Jiyong Park92315372021-04-02 08:45:46 +09001313func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1314 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001315}
1316
Jiyong Parkf1691d22021-03-29 20:11:58 +09001317func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001318 return "none"
1319}
1320
Jiyong Park92315372021-04-02 08:45:46 +09001321func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001322 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001323 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001324 }
Jiyong Park92315372021-04-02 08:45:46 +09001325 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001326}
1327
Jiyong Park92315372021-04-02 08:45:46 +09001328func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1329 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001330}
1331
Colin Cross74d73e22017-08-02 11:05:49 -07001332func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001333 return &j.prebuilt
1334}
1335
Colin Cross74d73e22017-08-02 11:05:49 -07001336func (j *Import) PrebuiltSrcs() []string {
1337 return j.properties.Jars
1338}
1339
1340func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001341 return j.prebuilt.Name(j.ModuleBase.Name())
1342}
1343
Jiyong Park0b238752019-10-29 11:23:10 +09001344func (j *Import) Stem() string {
1345 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1346}
1347
Jiyong Park618922e2020-01-08 13:35:43 +09001348func (a *Import) JacocoReportClassesFile() android.Path {
1349 return nil
1350}
1351
Bill Peckhama41a6962021-01-11 10:58:54 -08001352func (j *Import) LintDepSets() LintDepSets {
1353 return LintDepSets{}
1354}
1355
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001356func (j *Import) getStrictUpdatabilityLinting() bool {
1357 return false
1358}
1359
1360func (j *Import) setStrictUpdatabilityLinting(bool) {
1361}
1362
Colin Cross74d73e22017-08-02 11:05:49 -07001363func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001364 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001365
1366 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001367 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001368 }
Colin Cross1e676be2016-10-12 14:38:15 -07001369}
1370
Colin Cross74d73e22017-08-02 11:05:49 -07001371func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001372 j.sdkVersion = j.SdkVersion(ctx)
1373 j.minSdkVersion = j.MinSdkVersion(ctx)
1374
Colin Cross56a83212020-09-15 18:30:11 -07001375 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1376 j.hideApexVariantFromMake = true
1377 }
1378
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001379 if ctx.Windows() {
1380 j.HideFromMake()
1381 }
1382
Colin Cross8a497952019-03-05 22:25:09 -08001383 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001384
Jiyong Park0b238752019-10-29 11:23:10 +09001385 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001386 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001387 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1388 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001389 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001390 inputFile := outputFile
1391 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1392 TransformJetifier(ctx, outputFile, inputFile)
1393 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001394 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001395 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001396
Liz Kammerd6c31d22020-08-05 15:40:41 -07001397 var flags javaBuilderFlags
1398
Jiyong Park1be96912018-05-28 18:02:19 +09001399 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001400 tag := ctx.OtherModuleDependencyTag(module)
1401
Colin Crossdcf71b22021-02-01 13:59:03 -08001402 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1403 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001404 switch tag {
1405 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001406 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001407 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001408 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001409 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001410 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001411 switch tag {
1412 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001413 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001414 }
1415 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001416
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001417 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001418 })
1419
Nan Zhang4973ecf2018-08-10 13:42:12 -07001420 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001421 var installDir android.InstallPath
1422 if ctx.InstallInTestcases() {
1423 var archDir string
1424 if !ctx.Host() {
1425 archDir = ctx.DeviceConfig().DeviceArch()
1426 }
1427 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1428 } else {
1429 installDir = android.PathForModuleInstall(ctx, "framework")
1430 }
1431 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001432 }
Jiyong Park19604de2020-03-24 16:44:11 +09001433
1434 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001435
Paul Duffin064b70c2020-11-02 17:32:38 +00001436 if ctx.Device() {
1437 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1438 // obtained from the associated deapexer module.
1439 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1440 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001441 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001442 di := android.FindDeapexerProviderForModule(ctx)
1443 if di == nil {
1444 return // An error has been reported by FindDeapexerProviderForModule.
1445 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001446 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001447 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1448 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001449 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1450 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001451
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001452 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001453 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001454 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1455 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001456
1457 // Initialize the hiddenapi structure.
1458 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001459 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001460 // This should never happen as a variant for a prebuilt_apex is only created if the
1461 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001462 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001463 }
1464 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001465 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001466 if sdkDep.invalidVersion {
1467 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1468 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1469 } else if sdkDep.useFiles {
1470 // sdkDep.jar is actually equivalent to turbine header.jar.
1471 flags.classpath = append(flags.classpath, sdkDep.jars...)
1472 }
1473
1474 // Dex compilation
1475
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001476 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1477 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001478 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001479 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1480
Paul Duffin612e6102021-02-02 13:38:13 +00001481 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001482 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001483 if ctx.Failed() {
1484 return
1485 }
1486
Paul Duffin74d18d12021-05-14 14:18:47 +01001487 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001488 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001489
1490 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001491 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001492
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001493 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001494 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001495 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001496 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001497
1498 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1499 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1500 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1501 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1502 AidlIncludeDirs: j.exportAidlIncludeDirs,
1503 })
Colin Cross2fe66872015-03-30 17:20:39 -07001504}
1505
Paul Duffinaa55f742020-10-06 17:20:13 +01001506func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1507 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001508 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001509 return android.Paths{j.combinedClasspathFile}, nil
1510 default:
1511 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1512 }
1513}
1514
1515var _ android.OutputFileProducer = (*Import)(nil)
1516
Nan Zhanged19fc32017-10-19 13:06:22 -07001517func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001518 if j.combinedClasspathFile == nil {
1519 return nil
1520 }
Colin Cross37f6d792018-07-12 12:28:41 -07001521 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001522}
1523
Colin Cross331a1212018-08-15 20:40:52 -07001524func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001525 if j.combinedClasspathFile == nil {
1526 return nil
1527 }
Colin Cross331a1212018-08-15 20:40:52 -07001528 return android.Paths{j.combinedClasspathFile}
1529}
1530
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001531func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001532 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001533}
1534
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001535func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001536 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001537}
1538
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001539func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1540 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001541}
1542
Jiyong Park45bf82e2020-12-15 22:29:02 +09001543var _ android.ApexModule = (*Import)(nil)
1544
1545// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001546func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001547 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001548}
1549
Jiyong Park45bf82e2020-12-15 22:29:02 +09001550// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001551func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1552 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001553 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001554 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001555 return fmt.Errorf("min_sdk_version is not specified")
1556 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001557 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001558 return nil
1559 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001560 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1561 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001562 }
Jooyung Han749dc692020-04-15 11:03:39 +09001563 return nil
1564}
1565
Paul Duffinfef55002021-06-17 14:56:05 +01001566// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1567// java_sdk_library_import with the specified base module name requires to be exported from a
1568// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001569func requiredFilesFromPrebuiltApexForImport(name string) []string {
1570 // Add the dex implementation jar to the set of exported files.
1571 return []string{
1572 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001573 }
1574}
1575
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001576// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1577// the java library with the specified name.
1578func apexRootRelativePathToJavaLib(name string) string {
1579 return filepath.Join("javalib", name+".jar")
1580}
1581
Paul Duffinfef55002021-06-17 14:56:05 +01001582var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1583
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001584func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001585 name := j.BaseModuleName()
1586 return requiredFilesFromPrebuiltApexForImport(name)
1587}
1588
albaltai36ff7dc2018-12-25 14:35:23 +08001589// Add compile time check for interface implementation
1590var _ android.IDEInfo = (*Import)(nil)
1591var _ android.IDECustomizedModuleName = (*Import)(nil)
1592
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001593// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001594
1595func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1596 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1597}
1598
1599func (j *Import) IDECustomizedModuleName() string {
1600 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1601 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1602 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001603 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001604}
1605
Colin Cross74d73e22017-08-02 11:05:49 -07001606var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001607
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001608func (j *Import) IsInstallable() bool {
1609 return Bool(j.properties.Installable)
1610}
1611
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001612var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001613
Colin Cross1b16b0e2019-02-12 14:41:32 -08001614// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1615//
1616// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1617// compiled against an Android classpath.
1618//
1619// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1620// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001621func ImportFactory() android.Module {
1622 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001623
Liz Kammerd6c31d22020-08-05 15:40:41 -07001624 module.AddProperties(
1625 &module.properties,
1626 &module.dexer.dexProperties,
1627 )
Colin Cross74d73e22017-08-02 11:05:49 -07001628
Paul Duffin71b33cc2021-06-23 11:39:47 +01001629 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001630
Liz Kammerd6c31d22020-08-05 15:40:41 -07001631 module.dexProperties.Optimize.EnabledByDefault = false
1632
Colin Cross74d73e22017-08-02 11:05:49 -07001633 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001634 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001635 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001636 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001637 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001638}
1639
Colin Cross1b16b0e2019-02-12 14:41:32 -08001640// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1641// module.
1642//
1643// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1644// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001645func ImportFactoryHost() android.Module {
1646 module := &Import{}
1647
1648 module.AddProperties(&module.properties)
1649
1650 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001651 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001652 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001653 return module
1654}
1655
Colin Cross42be7612019-02-21 18:12:14 -08001656// dex_import module
1657
1658type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001659 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001660
1661 // set the name of the output
1662 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001663}
1664
1665type DexImport struct {
1666 android.ModuleBase
1667 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001668 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001669 prebuilt android.Prebuilt
1670
1671 properties DexImportProperties
1672
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001673 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001674
1675 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001676
1677 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001678}
1679
1680func (j *DexImport) Prebuilt() *android.Prebuilt {
1681 return &j.prebuilt
1682}
1683
1684func (j *DexImport) PrebuiltSrcs() []string {
1685 return j.properties.Jars
1686}
1687
1688func (j *DexImport) Name() string {
1689 return j.prebuilt.Name(j.ModuleBase.Name())
1690}
1691
Jiyong Park0b238752019-10-29 11:23:10 +09001692func (j *DexImport) Stem() string {
1693 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1694}
1695
Jiyong Park77acec62020-06-01 21:39:15 +09001696func (a *DexImport) JacocoReportClassesFile() android.Path {
1697 return nil
1698}
1699
Colin Cross08dca382020-07-21 20:31:17 -07001700func (a *DexImport) LintDepSets() LintDepSets {
1701 return LintDepSets{}
1702}
1703
Martin Stjernholm6d415272020-01-31 17:10:36 +00001704func (j *DexImport) IsInstallable() bool {
1705 return true
1706}
1707
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001708func (j *DexImport) getStrictUpdatabilityLinting() bool {
1709 return false
1710}
1711
1712func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1713}
1714
Colin Cross42be7612019-02-21 18:12:14 -08001715func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1716 if len(j.properties.Jars) != 1 {
1717 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1718 }
1719
Colin Cross56a83212020-09-15 18:30:11 -07001720 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1721 if !apexInfo.IsForPlatform() {
1722 j.hideApexVariantFromMake = true
1723 }
1724
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001725 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1726 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001727 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1728
1729 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1730 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1731
1732 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001733 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001734
1735 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1736 rule.Temporary(temporary)
1737
1738 // use zip2zip to uncompress classes*.dex files
1739 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001740 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001741 FlagWithInput("-i ", inputJar).
1742 FlagWithOutput("-o ", temporary).
1743 FlagWithArg("-0 ", "'classes*.dex'")
1744
1745 // use zipalign to align uncompressed classes*.dex files
1746 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001747 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001748 Flag("-f").
1749 Text("4").
1750 Input(temporary).
1751 Output(dexOutputFile)
1752
1753 rule.DeleteTemporaryFiles()
1754
Colin Crossf1a035e2020-11-16 17:32:30 -08001755 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001756 } else {
1757 ctx.Build(pctx, android.BuildParams{
1758 Rule: android.Cp,
1759 Input: inputJar,
1760 Output: dexOutputFile,
1761 })
1762 }
1763
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001764 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001765
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001766 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001767
Colin Cross56a83212020-09-15 18:30:11 -07001768 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001769 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1770 j.Stem()+".jar", dexOutputFile)
1771 }
Colin Cross42be7612019-02-21 18:12:14 -08001772}
1773
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001774func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001775 return j.dexJarFile
1776}
1777
Jiyong Park45bf82e2020-12-15 22:29:02 +09001778var _ android.ApexModule = (*DexImport)(nil)
1779
1780// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001781func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1782 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001783 // we don't check prebuilt modules for sdk_version
1784 return nil
1785}
1786
Colin Cross42be7612019-02-21 18:12:14 -08001787// dex_import imports a `.jar` file containing classes.dex files.
1788//
1789// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1790// to the device.
1791func DexImportFactory() android.Module {
1792 module := &DexImport{}
1793
1794 module.AddProperties(&module.properties)
1795
1796 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001797 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001798 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001799 return module
1800}
1801
Colin Cross89536d42017-07-07 14:35:50 -07001802//
1803// Defaults
1804//
1805type Defaults struct {
1806 android.ModuleBase
1807 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001808 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001809}
1810
Colin Cross1b16b0e2019-02-12 14:41:32 -08001811// java_defaults provides a set of properties that can be inherited by other java or android modules.
1812//
1813// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1814// property in the defaults module that exists in the depending module will be prepended to the depending module's
1815// value for that property.
1816//
1817// Example:
1818//
1819// java_defaults {
1820// name: "example_defaults",
1821// srcs: ["common/**/*.java"],
1822// javacflags: ["-Xlint:all"],
1823// aaptflags: ["--auto-add-overlay"],
1824// }
1825//
1826// java_library {
1827// name: "example",
1828// defaults: ["example_defaults"],
1829// srcs: ["example/**/*.java"],
1830// }
1831//
1832// is functionally identical to:
1833//
1834// java_library {
1835// name: "example",
1836// srcs: [
1837// "common/**/*.java",
1838// "example/**/*.java",
1839// ],
1840// javacflags: ["-Xlint:all"],
1841// }
Paul Duffin47357662019-12-05 14:07:14 +00001842func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001843 module := &Defaults{}
1844
Colin Cross89536d42017-07-07 14:35:50 -07001845 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001846 &CommonProperties{},
1847 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001848 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001849 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001850 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001851 &aaptProperties{},
1852 &androidLibraryProperties{},
1853 &appProperties{},
1854 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001855 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001856 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001857 &ImportProperties{},
1858 &AARImportProperties{},
1859 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001860 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001861 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001862 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001863 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001864 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001865 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001866 )
1867
1868 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001869 return module
1870}
Nan Zhangea568a42017-11-08 21:20:04 -08001871
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001872func kytheExtractJavaFactory() android.Singleton {
1873 return &kytheExtractJavaSingleton{}
1874}
1875
1876type kytheExtractJavaSingleton struct {
1877}
1878
1879func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1880 var xrefTargets android.Paths
1881 ctx.VisitAllModules(func(module android.Module) {
1882 if javaModule, ok := module.(xref); ok {
1883 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1884 }
1885 })
1886 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1887 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001888 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001889 }
1890}
1891
Nan Zhangea568a42017-11-08 21:20:04 -08001892var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001893var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001894var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001895var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001896
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001897// Add class loader context (CLC) of a given dependency to the current CLC.
1898func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1899 clcMap dexpreopt.ClassLoaderContextMap) {
1900
1901 dep, ok := depModule.(UsesLibraryDependency)
1902 if !ok {
1903 return
1904 }
1905
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001906 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1907
1908 var sdkLib *string
1909 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1910 // A shared SDK library. This should be added as a top-level CLC element.
1911 sdkLib = &depName
1912 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1913 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1914 // property. This should be handled in the same way as a shared SDK library.
1915 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001916 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001917
1918 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001919 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001920 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001921 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1922 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1923 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1924 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001925 } else if depTag == staticLibTag {
1926 // Propagate <uses-library> through static library dependencies, unless it is a component
1927 // library (such as stubs). Component libraries have a dependency on their SDK library,
1928 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001929 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001930 return
1931 }
1932 } else {
1933 // Don't propagate <uses-library> for other dependency tags.
1934 return
1935 }
1936
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001937 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1938 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1939 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1940 // from its CLC should be added to the current CLC.
1941 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001942 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001943 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001944 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001945 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1946 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001947}