blob: 2f9e03a8097e1ccbc54104083ef8740eb7bdbbfc [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))
Nan Zhang357466b2018-04-17 17:38:36 -0700437 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700438 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700439 }
Nan Zhang357466b2018-04-17 17:38:36 -0700440}
441
Colin Cross1e743852019-10-28 11:37:20 -0700442type javaVersion int
443
444const (
445 JAVA_VERSION_UNSUPPORTED = 0
446 JAVA_VERSION_6 = 6
447 JAVA_VERSION_7 = 7
448 JAVA_VERSION_8 = 8
449 JAVA_VERSION_9 = 9
450)
451
452func (v javaVersion) String() string {
453 switch v {
454 case JAVA_VERSION_6:
455 return "1.6"
456 case JAVA_VERSION_7:
457 return "1.7"
458 case JAVA_VERSION_8:
459 return "1.8"
460 case JAVA_VERSION_9:
461 return "1.9"
462 default:
463 return "unsupported"
464 }
465}
466
467// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
468func (v javaVersion) usesJavaModules() bool {
469 return v >= 9
470}
471
472func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100473 switch javaVersion {
474 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700475 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100476 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700477 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100478 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700479 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100480 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700481 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100482 case "10", "11":
483 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700484 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100485 default:
486 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700487 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100488 }
489}
490
Colin Cross2fe66872015-03-30 17:20:39 -0700491//
492// Java libraries (.jar file)
493//
494
Colin Crossf506d872017-07-19 15:53:04 -0700495type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700496 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700497
498 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700499}
500
Jiyong Park45bf82e2020-12-15 22:29:02 +0900501var _ android.ApexModule = (*Library)(nil)
502
satayevd604b212021-07-21 14:23:52 +0100503// Provides access to the list of permitted packages from apex boot jars.
Paul Duffine739f1e2020-05-29 11:24:51 +0100504type PermittedPackagesForUpdatableBootJars interface {
505 PermittedPackagesForUpdatableBootJars() []string
506}
507
508var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
509
510func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
511 return j.properties.Permitted_packages
512}
513
Colin Cross42be7612019-02-21 18:12:14 -0800514func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000515 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -0700516 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +0000517 return true
518 }
519
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000520 // Store uncompressed (and do not strip) dex files from boot class path jars.
521 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
522 return true
523 }
524
525 // Store uncompressed dex files that are preopted on /system.
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000526 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +0000527 return true
528 }
Colin Cross083a2aa2019-02-06 16:37:12 -0800529 if ctx.Config().UncompressPrivAppDex() &&
530 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
531 return true
532 }
533
Colin Cross2fc72f62018-12-21 12:59:54 -0800534 return false
535}
536
Jiakai Zhang22450f22021-10-11 03:05:20 +0000537// Sets `dexer.dexProperties.Uncompress_dex` to the proper value.
538func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) {
539 if dexer.dexProperties.Uncompress_dex == nil {
540 // If the value was not force-set by the user, use reasonable default based on the module.
541 dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter))
542 }
543}
544
Colin Crossf506d872017-07-19 15:53:04 -0700545func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +0900546 j.sdkVersion = j.SdkVersion(ctx)
547 j.minSdkVersion = j.MinSdkVersion(ctx)
548
Colin Cross56a83212020-09-15 18:30:11 -0700549 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
550 if !apexInfo.IsForPlatform() {
551 j.hideApexVariantFromMake = true
552 }
553
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100554 j.checkSdkVersions(ctx)
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000555 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
556 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800557 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Jiakai Zhang22450f22021-10-11 03:05:20 +0000558 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Liz Kammera7a64f32020-07-09 15:16:41 -0700559 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100560 j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700561 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -0700562
bralee1fbf4402020-05-21 10:11:59 +0800563 // Collect the module directory for IDE info in java/jdeps.go.
564 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
565
Colin Cross56a83212020-09-15 18:30:11 -0700566 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +0900567 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -0700568 var extraInstallDeps android.Paths
569 if j.InstallMixin != nil {
570 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
571 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700572 hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host()
573 if hostDexNeeded {
Colin Cross3108ce12021-11-10 14:38:50 -0800574 j.hostdexInstallFile = ctx.InstallFile(
575 android.PathForHostDexInstall(ctx, "framework"),
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700576 j.Stem()+"-hostdex.jar", j.outputFile)
577 }
578 var installDir android.InstallPath
579 if ctx.InstallInTestcases() {
580 var archDir string
581 if !ctx.Host() {
582 archDir = ctx.DeviceConfig().DeviceArch()
583 }
584 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
585 } else {
586 installDir = android.PathForModuleInstall(ctx, "framework")
587 }
588 j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -0700589 }
Colin Crossb7a63242015-04-16 14:09:14 -0700590}
591
Colin Crossf506d872017-07-19 15:53:04 -0700592func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -0700593 j.deps(ctx)
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100594 j.usesLibrary.deps(ctx, false)
Colin Cross46c9b8b2017-06-22 16:51:17 -0700595}
596
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000597const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000598 aidlIncludeDir = "aidl"
599 javaDir = "java"
600 jarFileSuffix = ".jar"
601 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000602)
603
Paul Duffina0dbf432019-12-05 11:25:53 +0000604// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +0000605func sdkSnapshotFilePathForJar(osPrefix, name string) string {
606 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000607}
608
Paul Duffina04c1072020-03-02 10:16:35 +0000609func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
610 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000611}
612
Paul Duffin13879572019-11-28 14:31:38 +0000613type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +0000614 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +0000615
616 // Function to retrieve the appropriate output jar (implementation or header) from
617 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +0000618 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
619
620 // Function to compute the snapshot relative path to which the named library's
621 // jar should be copied.
622 snapshotPathGetter func(osPrefix, name string) string
623
624 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
625 // files like aidl files should also be copied.
626 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +0000627}
628
Paul Duffindb170e42020-12-08 17:48:25 +0000629const (
630 onlyCopyJarToSnapshot = true
631 copyEverythingToSnapshot = false
632)
633
Paul Duffin296701e2021-07-14 10:29:36 +0100634func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
635 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin13879572019-11-28 14:31:38 +0000636}
637
638func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
639 _, ok := module.(*Library)
640 return ok
641}
642
Paul Duffin3a4eb502020-03-19 16:11:18 +0000643func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
644 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000645}
Paul Duffina0dbf432019-12-05 11:25:53 +0000646
Paul Duffin14eb4672020-03-02 11:33:02 +0000647func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +0000648 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +0000649}
650
651type librarySdkMemberProperties struct {
652 android.SdkMemberPropertiesBase
653
Paul Duffin864e1b42020-05-06 10:23:19 +0100654 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +0000655 AidlIncludeDirs android.Paths
Paul Duffin869de142021-07-15 14:14:41 +0100656
657 // The list of permitted packages that need to be passed to the prebuilts as they are used to
658 // create the updatable-bcp-packages.txt file.
659 PermittedPackages []string
Paul Duffin14eb4672020-03-02 11:33:02 +0000660}
661
Paul Duffin3a4eb502020-03-19 16:11:18 +0000662func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +0000663 j := variant.(*Library)
664
Paul Duffindb170e42020-12-08 17:48:25 +0000665 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
666
Paul Duffina551a1c2020-03-17 21:04:24 +0000667 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin869de142021-07-15 14:14:41 +0100668
669 p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()
Paul Duffin14eb4672020-03-02 11:33:02 +0000670}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000671
Paul Duffin3a4eb502020-03-19 16:11:18 +0000672func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000673 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000674
Paul Duffindb170e42020-12-08 17:48:25 +0000675 memberType := ctx.MemberType().(*librarySdkMemberType)
676
Paul Duffina551a1c2020-03-17 21:04:24 +0000677 exportedJar := p.JarToExport
678 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +0000679 // Delegate the creation of the snapshot relative path to the member type.
680 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
681
682 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +0000683 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
684
Paul Duffina551a1c2020-03-17 21:04:24 +0000685 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
686 }
687
Paul Duffin869de142021-07-15 14:14:41 +0100688 if len(p.PermittedPackages) > 0 {
689 propertySet.AddProperty("permitted_packages", p.PermittedPackages)
690 }
691
Paul Duffindb170e42020-12-08 17:48:25 +0000692 // Do not copy anything else to the snapshot.
693 if memberType.onlyCopyJarToSnapshot {
694 return
695 }
696
Paul Duffina551a1c2020-03-17 21:04:24 +0000697 aidlIncludeDirs := p.AidlIncludeDirs
698 if len(aidlIncludeDirs) != 0 {
699 sdkModuleContext := ctx.SdkModuleContext()
700 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +0000701 // TODO(jiyong): copy parcelable declarations only
702 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
703 for _, file := range aidlFiles {
704 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
705 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000706 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000707
Paul Duffina551a1c2020-03-17 21:04:24 +0000708 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +0000709 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +0000710}
711
Colin Cross1b16b0e2019-02-12 14:41:32 -0800712// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
713//
714// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
715// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
716// as a `static_libs` dependency of another module.
717//
718// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
719// a device.
720//
721// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
722// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -0700723func LibraryFactory() android.Module {
724 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700725
Colin Crossce6734e2020-06-15 16:09:53 -0700726 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -0700727
Paul Duffin71b33cc2021-06-23 11:39:47 +0100728 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +0100729
Jiyong Park7f7766d2019-07-25 22:02:35 +0900730 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +0900731 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900732 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -0700733 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700734}
735
Colin Cross1b16b0e2019-02-12 14:41:32 -0800736// java_library_static is an obsolete alias for java_library.
737func LibraryStaticFactory() android.Module {
738 return LibraryFactory()
739}
740
741// java_library_host builds and links sources into a `.jar` file for the host.
742//
743// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
744// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -0700745func LibraryHostFactory() android.Module {
746 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -0700747
Colin Crossce6734e2020-06-15 16:09:53 -0700748 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -0700749
Colin Cross9ae1b922018-06-26 17:59:05 -0700750 module.Module.properties.Installable = proptools.BoolPtr(true)
751
Jiyong Park7f7766d2019-07-25 22:02:35 +0900752 android.InitApexModule(module)
Paul Duffinb6b89a42021-05-06 16:33:43 +0100753 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +0900754 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -0700755 return module
Colin Cross2fe66872015-03-30 17:20:39 -0700756}
757
758//
Colin Crossb628ea52018-08-14 16:42:33 -0700759// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -0700760//
761
Dan Shi95d19422020-08-15 12:24:26 -0700762// Test option struct.
763type TestOptions struct {
764 // a list of extra test configuration files that should be installed with the module.
765 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -0800766
767 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
768 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -0700769}
770
Colin Cross05638fc2018-04-09 18:40:24 -0700771type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -0700772 // list of compatibility suites (for example "cts", "vts") that the module should be
773 // installed into.
774 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -0700775
776 // the name of the test configuration (for example "AndroidTest.xml") that should be
777 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800778 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -0700779
Jack He33338892018-09-19 02:21:28 -0700780 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
781 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800782 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700783
Colin Crossd96ca352018-08-10 16:06:24 -0700784 // list of files or filegroup modules that provide data that should be installed alongside
785 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +0900786 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700787
788 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
789 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
790 // explicitly.
791 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +0800792
793 // Add parameterized mainline modules to auto generated test config. The options will be
794 // handled by TradeFed to do downloading and installing the specified modules on the device.
795 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -0700796
797 // Test options.
798 Test_options TestOptions
Colin Crossf8d9c492021-01-26 11:01:43 -0800799
800 // Names of modules containing JNI libraries that should be installed alongside the test.
801 Jni_libs []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700802
803 // Install the test into a folder named for the module in all test suites.
804 Per_testcase_directory *bool
Colin Cross05638fc2018-04-09 18:40:24 -0700805}
806
Liz Kammerdd849a82020-06-12 16:38:45 -0700807type hostTestProperties struct {
808 // list of native binary modules that should be installed alongside the test
809 Data_native_bins []string `android:"arch_variant"`
810}
811
Paul Duffin42df1442019-03-20 12:45:53 +0000812type testHelperLibraryProperties struct {
813 // list of compatibility suites (for example "cts", "vts") that the module should be
814 // installed into.
815 Test_suites []string `android:"arch_variant"`
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700816
817 // Install the test into a folder named for the module in all test suites.
818 Per_testcase_directory *bool
Paul Duffin42df1442019-03-20 12:45:53 +0000819}
820
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000821type prebuiltTestProperties struct {
822 // list of compatibility suites (for example "cts", "vts") that the module should be
823 // installed into.
824 Test_suites []string `android:"arch_variant"`
825
826 // the name of the test configuration (for example "AndroidTest.xml") that should be
827 // installed with the module.
828 Test_config *string `android:"path,arch_variant"`
829}
830
Colin Cross05638fc2018-04-09 18:40:24 -0700831type Test struct {
832 Library
833
834 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -0700835
Dan Shi95d19422020-08-15 12:24:26 -0700836 testConfig android.Path
837 extraTestConfigs android.Paths
838 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700839}
840
Liz Kammerdd849a82020-06-12 16:38:45 -0700841type TestHost struct {
842 Test
843
844 testHostProperties hostTestProperties
845}
846
Paul Duffin42df1442019-03-20 12:45:53 +0000847type TestHelperLibrary struct {
848 Library
849
850 testHelperLibraryProperties testHelperLibraryProperties
851}
852
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000853type JavaTestImport struct {
854 Import
855
856 prebuiltTestProperties prebuiltTestProperties
857
858 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -0700859 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000860}
861
Colin Cross24cc4be62021-11-03 14:09:41 -0700862func (j *Test) InstallInTestcases() bool {
863 // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into
864 // testcases by base_rules.mk.
865 return !j.Host()
866}
867
868func (j *TestHelperLibrary) InstallInTestcases() bool {
869 return true
870}
871
872func (j *JavaTestImport) InstallInTestcases() bool {
873 return true
874}
875
Liz Kammerdd849a82020-06-12 16:38:45 -0700876func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
877 if len(j.testHostProperties.Data_native_bins) > 0 {
878 for _, target := range ctx.MultiTargets() {
879 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
880 }
881 }
882
Colin Crossf8d9c492021-01-26 11:01:43 -0800883 if len(j.testProperties.Jni_libs) > 0 {
884 for _, target := range ctx.MultiTargets() {
885 sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"})
886 ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...)
887 }
888 }
889
Liz Kammerdd849a82020-06-12 16:38:45 -0700890 j.deps(ctx)
891}
892
Yuexi Ma627263f2021-03-04 13:47:56 -0800893func (j *TestHost) AddExtraResource(p android.Path) {
894 j.extraResources = append(j.extraResources, p)
895}
896
Colin Cross303e21f2018-08-07 16:49:25 -0700897func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +0000898 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
899 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprezf666b152021-03-15 13:07:53 -0700900 defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +0000901 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
902 }
Dan Shi6ffaaa82019-09-26 11:41:36 -0700903 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -0800904 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -0700905
Colin Cross8a497952019-03-05 22:25:09 -0800906 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700907
Dan Shi95d19422020-08-15 12:24:26 -0700908 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
909
Liz Kammerdd849a82020-06-12 16:38:45 -0700910 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
911 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
912 })
913
Colin Crossf8d9c492021-01-26 11:01:43 -0800914 ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) {
915 sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo)
916 if sharedLibInfo.SharedLibrary != nil {
917 // Copy to an intermediate output directory to append "lib[64]" to the path,
918 // so that it's compatible with the default rpath values.
919 var relPath string
920 if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" {
921 relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base())
922 } else {
923 relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base())
924 }
925 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
926 ctx.Build(pctx, android.BuildParams{
927 Rule: android.Cp,
928 Input: sharedLibInfo.SharedLibrary,
929 Output: relocatedLib,
930 })
931 j.data = append(j.data, relocatedLib)
932 } else {
933 ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
934 }
935 })
936
Colin Cross303e21f2018-08-07 16:49:25 -0700937 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -0700938}
939
Paul Duffin42df1442019-03-20 12:45:53 +0000940func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
941 j.Library.GenerateAndroidBuildActions(ctx)
942}
943
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000944func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
945 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -0800946 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000947
948 j.Import.GenerateAndroidBuildActions(ctx)
949}
950
951type testSdkMemberType struct {
952 android.SdkMemberTypeBase
953}
954
Paul Duffin296701e2021-07-14 10:29:36 +0100955func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
956 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000957}
958
959func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
960 _, ok := module.(*Test)
961 return ok
962}
963
Paul Duffin3a4eb502020-03-19 16:11:18 +0000964func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
965 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +0000966}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000967
Paul Duffin14eb4672020-03-02 11:33:02 +0000968func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
969 return &testSdkMemberProperties{}
970}
971
972type testSdkMemberProperties struct {
973 android.SdkMemberPropertiesBase
974
Paul Duffina551a1c2020-03-17 21:04:24 +0000975 JarToExport android.Path
976 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +0000977}
978
Paul Duffin3a4eb502020-03-19 16:11:18 +0000979func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +0000980 test := variant.(*Test)
981
982 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000983 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +0000984 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000985 }
986
Paul Duffina551a1c2020-03-17 21:04:24 +0000987 p.JarToExport = implementationJars[0]
988 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +0000989}
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000990
Paul Duffin3a4eb502020-03-19 16:11:18 +0000991func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +0000992 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +0000993
Paul Duffina551a1c2020-03-17 21:04:24 +0000994 exportedJar := p.JarToExport
995 if exportedJar != nil {
996 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
997 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +0000998
999 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00001000 }
1001
1002 testConfig := p.TestConfig
1003 if testConfig != nil {
1004 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
1005 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00001006 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
1007 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001008}
1009
Colin Cross1b16b0e2019-02-12 14:41:32 -08001010// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1011// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1012//
1013// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1014// compiled against the device bootclasspath.
1015//
1016// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1017// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001018func TestFactory() android.Module {
1019 module := &Test{}
1020
Colin Crossce6734e2020-06-15 16:09:53 -07001021 module.addHostAndDeviceProperties()
1022 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001023
Colin Cross9ae1b922018-06-26 17:59:05 -07001024 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001025 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001026 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001027
Paul Duffinb6b89a42021-05-06 16:33:43 +01001028 android.InitSdkAwareModule(module)
Colin Cross05638fc2018-04-09 18:40:24 -07001029 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001030 return module
1031}
1032
Paul Duffin42df1442019-03-20 12:45:53 +00001033// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1034func TestHelperLibraryFactory() android.Module {
1035 module := &TestHelperLibrary{}
1036
Colin Crossce6734e2020-06-15 16:09:53 -07001037 module.addHostAndDeviceProperties()
1038 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00001039
Colin Cross9a4abed2019-04-24 13:19:28 -07001040 module.Module.properties.Installable = proptools.BoolPtr(true)
1041 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07001042 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07001043
Paul Duffin42df1442019-03-20 12:45:53 +00001044 InitJavaModule(module, android.HostAndDeviceSupported)
1045 return module
1046}
1047
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001048// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
1049// and makes sure that it is added to the appropriate test suite.
1050//
1051// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
1052// compiled against an Android classpath.
1053//
1054// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1055// for host modules.
1056func JavaTestImportFactory() android.Module {
1057 module := &JavaTestImport{}
1058
1059 module.AddProperties(
1060 &module.Import.properties,
1061 &module.prebuiltTestProperties)
1062
1063 module.Import.properties.Installable = proptools.BoolPtr(true)
1064
1065 android.InitPrebuiltModule(module, &module.properties.Jars)
1066 android.InitApexModule(module)
1067 android.InitSdkAwareModule(module)
1068 InitJavaModule(module, android.HostAndDeviceSupported)
1069 return module
1070}
1071
Colin Cross1b16b0e2019-02-12 14:41:32 -08001072// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1073// allow running the test with `atest` or a `TEST_MAPPING` file.
1074//
1075// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1076// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001077func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07001078 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07001079
Colin Crossce6734e2020-06-15 16:09:53 -07001080 module.addHostProperties()
1081 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07001082 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07001083
Yuexi Ma627263f2021-03-04 13:47:56 -08001084 InitTestHost(
1085 module,
1086 proptools.BoolPtr(true),
1087 nil,
1088 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07001089
Liz Kammerdd849a82020-06-12 16:38:45 -07001090 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00001091
Colin Cross05638fc2018-04-09 18:40:24 -07001092 return module
1093}
1094
Yuexi Ma627263f2021-03-04 13:47:56 -08001095func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
1096 th.properties.Installable = installable
1097 th.testProperties.Auto_gen_config = autoGenConfig
1098 th.testProperties.Test_suites = testSuites
1099}
1100
Colin Cross05638fc2018-04-09 18:40:24 -07001101//
Colin Cross2fe66872015-03-30 17:20:39 -07001102// Java Binaries (.jar file plus wrapper script)
1103//
1104
Colin Crossf506d872017-07-19 15:53:04 -07001105type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001106 // installable script to execute the resulting jar
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001107 Wrapper *string `android:"path,arch_variant"`
Colin Cross094054a2018-10-17 15:10:48 -07001108
1109 // Name of the class containing main to be inserted into the manifest as Main-Class.
1110 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07001111
1112 // Names of modules containing JNI libraries that should be installed alongside the host
1113 // variant of the binary.
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001114 Jni_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001115}
1116
Colin Crossf506d872017-07-19 15:53:04 -07001117type Binary struct {
1118 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001119
Colin Crossf506d872017-07-19 15:53:04 -07001120 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001121
Colin Cross6b4a32d2017-12-05 13:42:45 -08001122 isWrapperVariant bool
1123
Colin Crossc3315992017-12-08 19:12:36 -08001124 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001125 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001126}
1127
Alex Light24237172017-10-26 09:46:21 -07001128func (j *Binary) HostToolPath() android.OptionalPath {
1129 return android.OptionalPathForPath(j.binaryFile)
1130}
1131
Colin Crossf506d872017-07-19 15:53:04 -07001132func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001133 if ctx.Arch().ArchType == android.Common {
1134 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001135 if j.binaryProperties.Main_class != nil {
1136 if j.properties.Manifest != nil {
1137 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1138 }
1139 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1140 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1141 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1142 }
1143
Colin Cross6b4a32d2017-12-05 13:42:45 -08001144 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001145 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001146 // Handle the binary wrapper
1147 j.isWrapperVariant = true
1148
Colin Cross366938f2017-12-11 16:29:02 -08001149 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001150 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001151 } else {
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001152 if ctx.Windows() {
1153 ctx.PropertyErrorf("wrapper", "wrapper is required for Windows")
1154 }
1155
Colin Cross6b4a32d2017-12-05 13:42:45 -08001156 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1157 }
1158
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001159 ext := ""
1160 if ctx.Windows() {
1161 ext = ".bat"
1162 }
1163
Colin Crossc179ea62020-10-09 10:54:15 -07001164 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07001165 // of the wrapper variant, which will include the common variant's jar file and any JNI
1166 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08001167 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001168 ctx.ModuleName()+ext, j.wrapperFile)
1169 }
Colin Cross2fe66872015-03-30 17:20:39 -07001170}
1171
Colin Crossf506d872017-07-19 15:53:04 -07001172func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05001173 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001174 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05001175 }
1176 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08001177 // These dependencies ensure the host installation rules will install the jar file and
1178 // the jni libraries when the wrapper is installed.
1179 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
1180 ctx.AddVariationDependencies(
1181 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
1182 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08001183 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001184}
1185
Colin Cross1b16b0e2019-02-12 14:41:32 -08001186// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1187// as well.
1188//
1189// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1190// compiled against the device bootclasspath.
1191//
1192// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1193// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001194func BinaryFactory() android.Module {
1195 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001196
Colin Crossce6734e2020-06-15 16:09:53 -07001197 module.addHostAndDeviceProperties()
1198 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001199
Colin Cross9ae1b922018-06-26 17:59:05 -07001200 module.Module.properties.Installable = proptools.BoolPtr(true)
1201
Colin Cross6b4a32d2017-12-05 13:42:45 -08001202 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1203 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001204 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001205}
1206
Colin Cross1b16b0e2019-02-12 14:41:32 -08001207// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1208//
1209// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1210// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001211func BinaryHostFactory() android.Module {
1212 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001213
Colin Crossce6734e2020-06-15 16:09:53 -07001214 module.addHostProperties()
1215 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001216
Colin Cross9ae1b922018-06-26 17:59:05 -07001217 module.Module.properties.Installable = proptools.BoolPtr(true)
1218
Colin Cross6b4a32d2017-12-05 13:42:45 -08001219 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1220 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001221 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001222}
1223
1224//
1225// Java prebuilts
1226//
1227
Colin Cross74d73e22017-08-02 11:05:49 -07001228type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00001229 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001230
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001231 // The version of the SDK that the source prebuilt file was built against. Defaults to the
1232 // current version if not specified.
Nan Zhangea568a42017-11-08 21:20:04 -08001233 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001234
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001235 // The minimum version of the SDK that this module supports. Defaults to sdk_version if not
1236 // specified.
1237 Min_sdk_version *string
1238
Colin Cross535e2cf2017-10-20 17:57:49 -07001239 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001240
Paul Duffin869de142021-07-15 14:14:41 +01001241 // If not empty, classes are restricted to the specified packages and their sub-packages.
Paul Duffin869de142021-07-15 14:14:41 +01001242 Permitted_packages []string
1243
Jiyong Park1be96912018-05-28 18:02:19 +09001244 // List of shared java libs that this module has dependencies to
1245 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001246
1247 // List of files to remove from the jar file(s)
1248 Exclude_files []string
1249
1250 // List of directories to remove from the jar file(s)
1251 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001252
1253 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001254 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09001255
1256 // set the name of the output
1257 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09001258
1259 Aidl struct {
1260 // directories that should be added as include directories for any aidl sources of modules
1261 // that depend on this module, as well as to aidl for this module.
1262 Export_include_dirs []string
1263 }
Colin Cross74d73e22017-08-02 11:05:49 -07001264}
1265
1266type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001267 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001268 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001269 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001270 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09001271 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07001272
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001273 // Functionality common to Module and Import.
1274 embeddableInModuleAndImport
1275
Liz Kammerd6c31d22020-08-05 15:40:41 -07001276 hiddenAPI
1277 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001278 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07001279
Colin Cross74d73e22017-08-02 11:05:49 -07001280 properties ImportProperties
1281
Liz Kammerd6c31d22020-08-05 15:40:41 -07001282 // output file containing classes.dex and resources
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001283 dexJarFile OptionalDexJarPath
Jeongik Chad5fe8782021-07-08 01:13:11 +09001284 dexJarInstallFile android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07001285
Colin Cross0a6e0072017-08-30 14:24:55 -07001286 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001287 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09001288 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07001289
1290 hideApexVariantFromMake bool
Jiyong Park92315372021-04-02 08:45:46 +09001291
1292 sdkVersion android.SdkSpec
1293 minSdkVersion android.SdkSpec
Colin Cross2fe66872015-03-30 17:20:39 -07001294}
1295
Paul Duffin630b11e2021-07-15 13:35:26 +01001296var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil)
1297
1298func (j *Import) PermittedPackagesForUpdatableBootJars() []string {
1299 return j.properties.Permitted_packages
1300}
1301
Jiyong Park92315372021-04-02 08:45:46 +09001302func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1303 return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version))
Liz Kammer2d2fd852020-08-12 14:42:30 -07001304}
1305
Jiyong Parkf1691d22021-03-29 20:11:58 +09001306func (j *Import) SystemModules() string {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001307 return "none"
1308}
1309
Jiyong Park92315372021-04-02 08:45:46 +09001310func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001311 if j.properties.Min_sdk_version != nil {
Jiyong Park92315372021-04-02 08:45:46 +09001312 return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001313 }
Jiyong Park92315372021-04-02 08:45:46 +09001314 return j.SdkVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001315}
1316
Jiyong Park92315372021-04-02 08:45:46 +09001317func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec {
1318 return j.SdkVersion(ctx)
Artur Satayev480e25b2020-04-27 18:53:18 +01001319}
1320
Colin Cross74d73e22017-08-02 11:05:49 -07001321func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001322 return &j.prebuilt
1323}
1324
Colin Cross74d73e22017-08-02 11:05:49 -07001325func (j *Import) PrebuiltSrcs() []string {
1326 return j.properties.Jars
1327}
1328
1329func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001330 return j.prebuilt.Name(j.ModuleBase.Name())
1331}
1332
Jiyong Park0b238752019-10-29 11:23:10 +09001333func (j *Import) Stem() string {
1334 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1335}
1336
Jiyong Park618922e2020-01-08 13:35:43 +09001337func (a *Import) JacocoReportClassesFile() android.Path {
1338 return nil
1339}
1340
Bill Peckhama41a6962021-01-11 10:58:54 -08001341func (j *Import) LintDepSets() LintDepSets {
1342 return LintDepSets{}
1343}
1344
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001345func (j *Import) getStrictUpdatabilityLinting() bool {
1346 return false
1347}
1348
1349func (j *Import) setStrictUpdatabilityLinting(bool) {
1350}
1351
Colin Cross74d73e22017-08-02 11:05:49 -07001352func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001353 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001354
1355 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001356 sdkDeps(ctx, android.SdkContext(j), j.dexer)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001357 }
Colin Cross1e676be2016-10-12 14:38:15 -07001358}
1359
Colin Cross74d73e22017-08-02 11:05:49 -07001360func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park92315372021-04-02 08:45:46 +09001361 j.sdkVersion = j.SdkVersion(ctx)
1362 j.minSdkVersion = j.MinSdkVersion(ctx)
1363
Colin Cross56a83212020-09-15 18:30:11 -07001364 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
1365 j.hideApexVariantFromMake = true
1366 }
1367
Dan Willemsen8e6b3712021-09-20 23:11:24 -07001368 if ctx.Windows() {
1369 j.HideFromMake()
1370 }
1371
Colin Cross8a497952019-03-05 22:25:09 -08001372 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001373
Jiyong Park0b238752019-10-29 11:23:10 +09001374 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07001375 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001376 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1377 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001378 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001379 inputFile := outputFile
1380 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1381 TransformJetifier(ctx, outputFile, inputFile)
1382 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001383 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001384 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01001385
Liz Kammerd6c31d22020-08-05 15:40:41 -07001386 var flags javaBuilderFlags
1387
Jiyong Park1be96912018-05-28 18:02:19 +09001388 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09001389 tag := ctx.OtherModuleDependencyTag(module)
1390
Colin Crossdcf71b22021-02-01 13:59:03 -08001391 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1392 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09001393 switch tag {
1394 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001395 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001396 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001397 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09001398 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001399 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09001400 switch tag {
1401 case libTag:
Jiyong Park92315372021-04-02 08:45:46 +09001402 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...)
Jiyong Park1be96912018-05-28 18:02:19 +09001403 }
1404 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001405
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001406 addCLCFromDep(ctx, module, j.classLoaderContexts)
Jiyong Park1be96912018-05-28 18:02:19 +09001407 })
1408
Nan Zhang4973ecf2018-08-10 13:42:12 -07001409 if Bool(j.properties.Installable) {
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001410 var installDir android.InstallPath
1411 if ctx.InstallInTestcases() {
1412 var archDir string
1413 if !ctx.Host() {
1414 archDir = ctx.DeviceConfig().DeviceArch()
1415 }
1416 installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir)
1417 } else {
1418 installDir = android.PathForModuleInstall(ctx, "framework")
1419 }
1420 ctx.InstallFile(installDir, jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001421 }
Jiyong Park19604de2020-03-24 16:44:11 +09001422
1423 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001424
Paul Duffin064b70c2020-11-02 17:32:38 +00001425 if ctx.Device() {
1426 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
1427 // obtained from the associated deapexer module.
1428 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1429 if ai.ForPrebuiltApex {
Paul Duffin064b70c2020-11-02 17:32:38 +00001430 // Get the path of the dex implementation jar from the `deapexer` module.
Martin Stjernholm44825602021-09-17 01:44:12 +01001431 di := android.FindDeapexerProviderForModule(ctx)
1432 if di == nil {
1433 return // An error has been reported by FindDeapexerProviderForModule.
1434 }
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001435 if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001436 dexJarFile := makeDexJarPathFromPath(dexOutputPath)
1437 j.dexJarFile = dexJarFile
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001438 installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName()))
1439 j.dexJarInstallFile = installPath
Paul Duffin74d18d12021-05-14 14:18:47 +01001440
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001441 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001442 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Jiakai Zhang5b24f722021-09-30 09:32:57 +00001443 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1444 j.dexpreopt(ctx, dexOutputPath)
Jiakai Zhang22450f22021-10-11 03:05:20 +00001445
1446 // Initialize the hiddenapi structure.
1447 j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex)
Paul Duffin9d67ca62021-02-03 20:06:33 +00001448 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00001449 // This should never happen as a variant for a prebuilt_apex is only created if the
1450 // prebuilt_apex has been configured to export the java library dex file.
Martin Stjernholm44825602021-09-17 01:44:12 +01001451 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName())
Paul Duffin064b70c2020-11-02 17:32:38 +00001452 }
1453 } else if Bool(j.dexProperties.Compile_dex) {
Jiyong Parkf1691d22021-03-29 20:11:58 +09001454 sdkDep := decodeSdkDep(ctx, android.SdkContext(j))
Paul Duffin064b70c2020-11-02 17:32:38 +00001455 if sdkDep.invalidVersion {
1456 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1457 ctx.AddMissingDependencies(sdkDep.java9Classpath)
1458 } else if sdkDep.useFiles {
1459 // sdkDep.jar is actually equivalent to turbine header.jar.
1460 flags.classpath = append(flags.classpath, sdkDep.jars...)
1461 }
1462
1463 // Dex compilation
1464
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001465 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1466 ctx, android.PathForModuleInstall(ctx, "framework", jarName))
Jiakai Zhang22450f22021-10-11 03:05:20 +00001467 setUncompressDex(ctx, &j.dexpreopter, &j.dexer)
Paul Duffin064b70c2020-11-02 17:32:38 +00001468 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
1469
Paul Duffin612e6102021-02-02 13:38:13 +00001470 var dexOutputFile android.OutputPath
Jiyong Park92315372021-04-02 08:45:46 +09001471 dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName)
Paul Duffin064b70c2020-11-02 17:32:38 +00001472 if ctx.Failed() {
1473 return
1474 }
1475
Paul Duffin74d18d12021-05-14 14:18:47 +01001476 // Initialize the hiddenapi structure.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001477 j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex)
Paul Duffinafaa47c2021-05-14 13:04:04 +01001478
1479 // Encode hidden API flags in dex file.
Paul Duffin1bbd0622021-05-14 15:52:25 +01001480 dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile)
Paul Duffin064b70c2020-11-02 17:32:38 +00001481
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001482 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Jeongik Chad5fe8782021-07-08 01:13:11 +09001483 j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName)
Liz Kammerd6c31d22020-08-05 15:40:41 -07001484 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07001485 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001486
1487 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1488 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
1489 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
1490 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
1491 AidlIncludeDirs: j.exportAidlIncludeDirs,
1492 })
Colin Cross2fe66872015-03-30 17:20:39 -07001493}
1494
Paul Duffinaa55f742020-10-06 17:20:13 +01001495func (j *Import) OutputFiles(tag string) (android.Paths, error) {
1496 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00001497 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01001498 return android.Paths{j.combinedClasspathFile}, nil
1499 default:
1500 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1501 }
1502}
1503
1504var _ android.OutputFileProducer = (*Import)(nil)
1505
Nan Zhanged19fc32017-10-19 13:06:22 -07001506func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001507 if j.combinedClasspathFile == nil {
1508 return nil
1509 }
Colin Cross37f6d792018-07-12 12:28:41 -07001510 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001511}
1512
Colin Cross331a1212018-08-15 20:40:52 -07001513func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001514 if j.combinedClasspathFile == nil {
1515 return nil
1516 }
Colin Cross331a1212018-08-15 20:40:52 -07001517 return android.Paths{j.combinedClasspathFile}
1518}
1519
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001520func (j *Import) DexJarBuildPath() OptionalDexJarPath {
Liz Kammerd6c31d22020-08-05 15:40:41 -07001521 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08001522}
1523
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001524func (j *Import) DexJarInstallPath() android.Path {
Jeongik Chad5fe8782021-07-08 01:13:11 +09001525 return j.dexJarInstallFile
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01001526}
1527
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001528func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
1529 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09001530}
1531
Jiyong Park45bf82e2020-12-15 22:29:02 +09001532var _ android.ApexModule = (*Import)(nil)
1533
1534// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09001535func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01001536 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001537}
1538
Jiyong Park45bf82e2020-12-15 22:29:02 +09001539// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001540func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1541 sdkVersion android.ApiLevel) error {
Jiyong Park92315372021-04-02 08:45:46 +09001542 sdkSpec := j.MinSdkVersion(ctx)
Jiyong Parkf1691d22021-03-29 20:11:58 +09001543 if !sdkSpec.Specified() {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001544 return fmt.Errorf("min_sdk_version is not specified")
1545 }
Jiyong Parkf1691d22021-03-29 20:11:58 +09001546 if sdkSpec.Kind == android.SdkCore {
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001547 return nil
1548 }
Jooyung Han4c4da062021-06-23 10:23:16 +09001549 if sdkSpec.ApiLevel.GreaterThan(sdkVersion) {
1550 return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00001551 }
Jooyung Han749dc692020-04-15 11:03:39 +09001552 return nil
1553}
1554
Paul Duffinfef55002021-06-17 14:56:05 +01001555// requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or
1556// java_sdk_library_import with the specified base module name requires to be exported from a
1557// prebuilt_apex/apex_set.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001558func requiredFilesFromPrebuiltApexForImport(name string) []string {
1559 // Add the dex implementation jar to the set of exported files.
1560 return []string{
1561 apexRootRelativePathToJavaLib(name),
Paul Duffinfef55002021-06-17 14:56:05 +01001562 }
1563}
1564
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001565// apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for
1566// the java library with the specified name.
1567func apexRootRelativePathToJavaLib(name string) string {
1568 return filepath.Join("javalib", name+".jar")
1569}
1570
Paul Duffinfef55002021-06-17 14:56:05 +01001571var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil)
1572
Paul Duffinb4bbf2c2021-06-17 15:59:07 +01001573func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string {
Paul Duffinfef55002021-06-17 14:56:05 +01001574 name := j.BaseModuleName()
1575 return requiredFilesFromPrebuiltApexForImport(name)
1576}
1577
albaltai36ff7dc2018-12-25 14:35:23 +08001578// Add compile time check for interface implementation
1579var _ android.IDEInfo = (*Import)(nil)
1580var _ android.IDECustomizedModuleName = (*Import)(nil)
1581
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001582// Collect information for opening IDE project files in java/jdeps.go.
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001583
1584func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1585 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1586}
1587
1588func (j *Import) IDECustomizedModuleName() string {
1589 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1590 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1591 // solution to get the Import name.
Ulya Trafimovich497a0932021-07-14 16:35:33 +01001592 return android.RemoveOptionalPrebuiltPrefix(j.Name())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001593}
1594
Colin Cross74d73e22017-08-02 11:05:49 -07001595var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001596
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001597func (j *Import) IsInstallable() bool {
1598 return Bool(j.properties.Installable)
1599}
1600
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001601var _ DexpreopterInterface = (*Import)(nil)
Bill Peckhamff89ffa2020-12-23 16:13:04 -08001602
Colin Cross1b16b0e2019-02-12 14:41:32 -08001603// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1604//
1605// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1606// compiled against an Android classpath.
1607//
1608// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1609// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001610func ImportFactory() android.Module {
1611 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001612
Liz Kammerd6c31d22020-08-05 15:40:41 -07001613 module.AddProperties(
1614 &module.properties,
1615 &module.dexer.dexProperties,
1616 )
Colin Cross74d73e22017-08-02 11:05:49 -07001617
Paul Duffin71b33cc2021-06-23 11:39:47 +01001618 module.initModuleAndImport(module)
Paul Duffin859fe962020-05-15 10:20:31 +01001619
Liz Kammerd6c31d22020-08-05 15:40:41 -07001620 module.dexProperties.Optimize.EnabledByDefault = false
1621
Colin Cross74d73e22017-08-02 11:05:49 -07001622 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001623 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001624 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001625 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001626 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001627}
1628
Colin Cross1b16b0e2019-02-12 14:41:32 -08001629// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1630// module.
1631//
1632// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1633// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001634func ImportFactoryHost() android.Module {
1635 module := &Import{}
1636
1637 module.AddProperties(&module.properties)
1638
1639 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001640 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001641 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001642 return module
1643}
1644
Colin Cross42be7612019-02-21 18:12:14 -08001645// dex_import module
1646
1647type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07001648 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09001649
1650 // set the name of the output
1651 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08001652}
1653
1654type DexImport struct {
1655 android.ModuleBase
1656 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001657 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08001658 prebuilt android.Prebuilt
1659
1660 properties DexImportProperties
1661
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001662 dexJarFile OptionalDexJarPath
Colin Cross42be7612019-02-21 18:12:14 -08001663
1664 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07001665
1666 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08001667}
1668
1669func (j *DexImport) Prebuilt() *android.Prebuilt {
1670 return &j.prebuilt
1671}
1672
1673func (j *DexImport) PrebuiltSrcs() []string {
1674 return j.properties.Jars
1675}
1676
1677func (j *DexImport) Name() string {
1678 return j.prebuilt.Name(j.ModuleBase.Name())
1679}
1680
Jiyong Park0b238752019-10-29 11:23:10 +09001681func (j *DexImport) Stem() string {
1682 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
1683}
1684
Jiyong Park77acec62020-06-01 21:39:15 +09001685func (a *DexImport) JacocoReportClassesFile() android.Path {
1686 return nil
1687}
1688
Colin Cross08dca382020-07-21 20:31:17 -07001689func (a *DexImport) LintDepSets() LintDepSets {
1690 return LintDepSets{}
1691}
1692
Martin Stjernholm6d415272020-01-31 17:10:36 +00001693func (j *DexImport) IsInstallable() bool {
1694 return true
1695}
1696
Jaewoong Jung476b9d62021-05-10 15:30:00 -07001697func (j *DexImport) getStrictUpdatabilityLinting() bool {
1698 return false
1699}
1700
1701func (j *DexImport) setStrictUpdatabilityLinting(bool) {
1702}
1703
Colin Cross42be7612019-02-21 18:12:14 -08001704func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1705 if len(j.properties.Jars) != 1 {
1706 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
1707 }
1708
Colin Cross56a83212020-09-15 18:30:11 -07001709 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1710 if !apexInfo.IsForPlatform() {
1711 j.hideApexVariantFromMake = true
1712 }
1713
Jiakai Zhang519c5c82021-09-16 06:15:39 +00001714 j.dexpreopter.installPath = j.dexpreopter.getInstallPath(
1715 ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar"))
Colin Cross42be7612019-02-21 18:12:14 -08001716 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
1717
1718 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
1719 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
1720
1721 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08001722 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08001723
1724 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
1725 rule.Temporary(temporary)
1726
1727 // use zip2zip to uncompress classes*.dex files
1728 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001729 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08001730 FlagWithInput("-i ", inputJar).
1731 FlagWithOutput("-o ", temporary).
1732 FlagWithArg("-0 ", "'classes*.dex'")
1733
1734 // use zipalign to align uncompressed classes*.dex files
1735 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08001736 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08001737 Flag("-f").
1738 Text("4").
1739 Input(temporary).
1740 Output(dexOutputFile)
1741
1742 rule.DeleteTemporaryFiles()
1743
Colin Crossf1a035e2020-11-16 17:32:30 -08001744 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08001745 } else {
1746 ctx.Build(pctx, android.BuildParams{
1747 Rule: android.Cp,
1748 Input: inputJar,
1749 Output: dexOutputFile,
1750 })
1751 }
1752
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001753 j.dexJarFile = makeDexJarPathFromPath(dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001754
Jaewoong Jung4b97a562020-12-17 09:43:28 -08001755 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08001756
Colin Cross56a83212020-09-15 18:30:11 -07001757 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09001758 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1759 j.Stem()+".jar", dexOutputFile)
1760 }
Colin Cross42be7612019-02-21 18:12:14 -08001761}
1762
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001763func (j *DexImport) DexJarBuildPath() OptionalDexJarPath {
Colin Cross42be7612019-02-21 18:12:14 -08001764 return j.dexJarFile
1765}
1766
Jiyong Park45bf82e2020-12-15 22:29:02 +09001767var _ android.ApexModule = (*DexImport)(nil)
1768
1769// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07001770func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
1771 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09001772 // we don't check prebuilt modules for sdk_version
1773 return nil
1774}
1775
Colin Cross42be7612019-02-21 18:12:14 -08001776// dex_import imports a `.jar` file containing classes.dex files.
1777//
1778// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
1779// to the device.
1780func DexImportFactory() android.Module {
1781 module := &DexImport{}
1782
1783 module.AddProperties(&module.properties)
1784
1785 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001786 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001787 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08001788 return module
1789}
1790
Colin Cross89536d42017-07-07 14:35:50 -07001791//
1792// Defaults
1793//
1794type Defaults struct {
1795 android.ModuleBase
1796 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001797 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07001798}
1799
Colin Cross1b16b0e2019-02-12 14:41:32 -08001800// java_defaults provides a set of properties that can be inherited by other java or android modules.
1801//
1802// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
1803// property in the defaults module that exists in the depending module will be prepended to the depending module's
1804// value for that property.
1805//
1806// Example:
1807//
1808// java_defaults {
1809// name: "example_defaults",
1810// srcs: ["common/**/*.java"],
1811// javacflags: ["-Xlint:all"],
1812// aaptflags: ["--auto-add-overlay"],
1813// }
1814//
1815// java_library {
1816// name: "example",
1817// defaults: ["example_defaults"],
1818// srcs: ["example/**/*.java"],
1819// }
1820//
1821// is functionally identical to:
1822//
1823// java_library {
1824// name: "example",
1825// srcs: [
1826// "common/**/*.java",
1827// "example/**/*.java",
1828// ],
1829// javacflags: ["-Xlint:all"],
1830// }
Paul Duffin47357662019-12-05 14:07:14 +00001831func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07001832 module := &Defaults{}
1833
Colin Cross89536d42017-07-07 14:35:50 -07001834 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001835 &CommonProperties{},
1836 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07001837 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001838 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001839 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001840 &aaptProperties{},
1841 &androidLibraryProperties{},
1842 &appProperties{},
1843 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08001844 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00001845 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001846 &ImportProperties{},
1847 &AARImportProperties{},
1848 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01001849 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08001850 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09001851 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07001852 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07001853 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08001854 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001855 )
1856
1857 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07001858 return module
1859}
Nan Zhangea568a42017-11-08 21:20:04 -08001860
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001861func kytheExtractJavaFactory() android.Singleton {
1862 return &kytheExtractJavaSingleton{}
1863}
1864
1865type kytheExtractJavaSingleton struct {
1866}
1867
1868func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1869 var xrefTargets android.Paths
1870 ctx.VisitAllModules(func(module android.Module) {
1871 if javaModule, ok := module.(xref); ok {
1872 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
1873 }
1874 })
1875 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
1876 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07001877 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001878 }
1879}
1880
Nan Zhangea568a42017-11-08 21:20:04 -08001881var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001882var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001883var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001884var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001885
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001886// Add class loader context (CLC) of a given dependency to the current CLC.
1887func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
1888 clcMap dexpreopt.ClassLoaderContextMap) {
1889
1890 dep, ok := depModule.(UsesLibraryDependency)
1891 if !ok {
1892 return
1893 }
1894
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001895 depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
1896
1897 var sdkLib *string
1898 if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
1899 // A shared SDK library. This should be added as a top-level CLC element.
1900 sdkLib = &depName
1901 } else if ulib, ok := depModule.(ProvidesUsesLib); ok {
1902 // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
1903 // property. This should be handled in the same way as a shared SDK library.
1904 sdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001905 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001906
1907 depTag := ctx.OtherModuleDependencyTag(depModule)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +01001908 if depTag == libTag {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001909 // Ok, propagate <uses-library> through non-static library dependencies.
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001910 } else if tag, ok := depTag.(usesLibraryDependencyTag); ok &&
1911 tag.sdkVersion == dexpreopt.AnySdkVersion && tag.implicit {
1912 // Ok, propagate <uses-library> through non-compatibility implicit <uses-library>
1913 // dependencies.
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001914 } else if depTag == staticLibTag {
1915 // Propagate <uses-library> through static library dependencies, unless it is a component
1916 // library (such as stubs). Component libraries have a dependency on their SDK library,
1917 // which should not be pulled just because of a static component library.
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001918 if sdkLib != nil {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001919 return
1920 }
1921 } else {
1922 // Don't propagate <uses-library> for other dependency tags.
1923 return
1924 }
1925
Ulya Trafimovich840efb62021-07-15 14:34:40 +01001926 // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
1927 // and its CLC should be added as subtree of that node. Otherwise the library is not a
1928 // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
1929 // from its CLC should be added to the current CLC.
1930 if sdkLib != nil {
Ulya Trafimovich0b1c70e2021-08-20 15:39:12 +01001931 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, true,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01001932 dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001933 } else {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001934 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
1935 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001936}