blob: 8c714eeb2ff91a597ea8b9c31fa2417f8a468c06 [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 Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Ulya Trafimovich31e444e2020-08-14 17:32:16 +010032 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070033 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070034 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070035)
36
Colin Cross463a90e2015-06-17 14:20:06 -070037func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000038 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000039
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -080040 RegisterJavaSdkMemberTypes()
41}
42
43func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
44 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
45
46 ctx.RegisterModuleType("java_library", LibraryFactory)
47 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
48 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
49 ctx.RegisterModuleType("java_binary", BinaryFactory)
50 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
51 ctx.RegisterModuleType("java_test", TestFactory)
52 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
53 ctx.RegisterModuleType("java_test_host", TestHostFactory)
54 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
55 ctx.RegisterModuleType("java_import", ImportFactory)
56 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
57 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
58 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
59 ctx.RegisterModuleType("dex_import", DexImportFactory)
60
61 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
62 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
63 })
64
65 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
66 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
67}
68
69func RegisterJavaSdkMemberTypes() {
Paul Duffin255f18e2019-12-13 11:22:16 +000070 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000071 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000072
Paul Duffin22ff0aa2021-02-04 11:15:34 +000073 // Export implementation classes jar as part of the sdk.
74 exportImplementationClassesJar := func(_ android.SdkMemberContext, j *Library) android.Path {
75 implementationJars := j.ImplementationAndResourcesJars()
76 if len(implementationJars) != 1 {
77 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
78 }
79 return implementationJars[0]
80 }
81
Paul Duffindb170e42020-12-08 17:48:25 +000082 // Register java implementation libraries for use only in module_exports (not sdk).
Paul Duffinf5c0a9c2020-02-28 14:39:53 +000083 android.RegisterSdkMemberType(&librarySdkMemberType{
84 android.SdkMemberTypeBase{
85 PropertyName: "java_libs",
86 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +000087 exportImplementationClassesJar,
Paul Duffindb170e42020-12-08 17:48:25 +000088 sdkSnapshotFilePathForJar,
89 copyEverythingToSnapshot,
Paul Duffin255f18e2019-12-13 11:22:16 +000090 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000091
Paul Duffindb170e42020-12-08 17:48:25 +000092 // Register java boot libraries for use in sdk.
93 //
94 // The build has some implicit dependencies (via the boot jars configuration) on a number of
95 // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are
96 // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise
97 // used outside those mainline modules.
98 //
99 // As they are not needed outside the mainline modules adding them to the sdk/module-exports as
100 // either java_libs, or java_header_libs would end up exporting more information than was strictly
101 // necessary. The java_boot_libs property to allow those modules to be exported as part of the
102 // sdk/module_exports without exposing any unnecessary information.
103 android.RegisterSdkMemberType(&librarySdkMemberType{
104 android.SdkMemberTypeBase{
105 PropertyName: "java_boot_libs",
106 SupportsSdk: true,
107 },
Paul Duffin22ff0aa2021-02-04 11:15:34 +0000108 // Temporarily export implementation classes jar for java_boot_libs as it is required for the
109 // hiddenapi processing.
110 // TODO(b/179354495): Revert once hiddenapi processing has been modularized.
111 exportImplementationClassesJar,
112 sdkSnapshotFilePathForJar,
Paul Duffindb170e42020-12-08 17:48:25 +0000113 onlyCopyJarToSnapshot,
114 })
115
116 // Register java test libraries for use only in module_exports (not sdk).
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000117 android.RegisterSdkMemberType(&testSdkMemberType{
118 SdkMemberTypeBase: android.SdkMemberTypeBase{
119 PropertyName: "java_tests",
120 },
121 })
Colin Cross463a90e2015-06-17 14:20:06 -0700122
Jeongik Cha538c0d02019-07-11 15:54:27 +0900123}
124
Colin Cross2fe66872015-03-30 17:20:39 -0700125// TODO:
126// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700127// Renderscript
128// Post-jar passes:
129// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700130// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700131// DroidDoc
132// Findbugs
133
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800134// Properties that are common to most Java modules, i.e. whether it's a host or device module.
135type CommonProperties struct {
Colin Crossa4c8cc62020-06-25 17:13:36 -0700136 // list of source files used to compile the Java module. May be .java, .kt, .logtags, .proto,
Colin Cross7d5136f2015-05-11 13:39:40 -0700137 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800138 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700139
Colin Crossa4c8cc62020-06-25 17:13:36 -0700140 // list Kotlin of source files containing Kotlin code that should be treated as common code in
141 // a codebase that supports Kotlin multiplatform. See
142 // https://kotlinlang.org/docs/reference/multiplatform.html. May be only be .kt files.
143 Common_srcs []string `android:"path,arch_variant"`
144
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700145 // list of source files that should not be used to build the Java module.
146 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800147 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700148
149 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700150 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700151
Colin Cross86a63ff2017-09-27 17:33:10 -0700152 // list of directories that should be excluded from java_resource_dirs
153 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700154
Colin Cross0f37af02017-09-27 17:42:05 -0700155 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800156 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700157
Colin Crosscedd4762018-09-13 11:26:19 -0700158 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800159 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700160
Colin Cross7d5136f2015-05-11 13:39:40 -0700161 // list of module-specific flags that will be used for javac compiles
162 Javacflags []string `android:"arch_variant"`
163
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200164 // list of module-specific flags that will be used for kotlinc compiles
165 Kotlincflags []string `android:"arch_variant"`
166
Colin Cross7d5136f2015-05-11 13:39:40 -0700167 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700168 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700169
170 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700171 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700172
173 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800174 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700175
Colin Cross540eff82017-06-22 17:01:52 -0700176 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800177 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700178
179 // If not blank, set the java version passed to javac as -source and -target
180 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700181
Colin Cross9ae1b922018-06-26 17:59:05 -0700182 // If set to true, allow this module to be dexed and installed on devices. Has no
183 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700184 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700185
Colin Cross0f37af02017-09-27 17:42:05 -0700186 // If set to true, include sources used to compile the module in to the final jar
187 Include_srcs *bool
188
Vladimir Marko0975ee02019-04-02 10:29:55 +0100189 // If not empty, classes are restricted to the specified packages and their sub-packages.
190 // This restriction is checked after applying jarjar rules and including static libs.
191 Permitted_packages []string
192
Colin Crossbe9cdb82019-01-21 21:37:16 -0800193 // List of modules to use as annotation processors
194 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700195
Colin Crossc9fe10f2020-11-19 18:06:03 -0800196 // List of modules to export to libraries that directly depend on this library as annotation
197 // processors. Note that if the plugins set generates_api: true this will disable the turbine
198 // optimization on modules that depend on this module, which will reduce parallelism and cause
199 // more recompilation.
Artur Satayev9cf46692019-11-26 18:08:34 +0000200 Exported_plugins []string
201
Nan Zhang61eaedb2017-11-02 13:28:15 -0700202 // The number of Java source entries each Javac instance can process
203 Javac_shard_size *int64
204
Nan Zhang5f8cb422018-02-06 10:34:32 -0800205 // Add host jdk tools.jar to bootclasspath
206 Use_tools_jar *bool
207
Colin Cross1369cdb2017-09-29 17:58:17 -0700208 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700209 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800210 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700211
Colin Cross6cef4812019-10-17 14:23:50 -0700212 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700213 Javacflags []string
214 }
Colin Crosscb933592017-11-22 13:49:43 -0800215
Colin Cross81440082018-08-15 20:21:55 -0700216 // When compiling language level 9+ .java code in packages that are part of
217 // a system module, patch_module names the module that your sources and
218 // dependencies should be patched into. The Android runtime currently
219 // doesn't implement the JEP 261 module system so this option is only
220 // supported at compile time. It should only be needed to compile tests in
221 // packages that exist in libcore and which are inconvenient to move
222 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100223 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700224
Colin Crosscb933592017-11-22 13:49:43 -0800225 Jacoco struct {
226 // List of classes to include for instrumentation with jacoco to collect coverage
227 // information at runtime when building with coverage enabled. If unset defaults to all
228 // classes.
229 // Supports '*' as the last character of an entry in the list as a wildcard match.
230 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
231 // it matches classes in the package that have the class name as a prefix.
232 Include_filter []string
233
234 // List of classes to exclude from instrumentation with jacoco to collect coverage
235 // information at runtime when building with coverage enabled. Overrides classes selected
236 // by the include_filter property.
237 // Supports '*' as the last character of an entry in the list as a wildcard match.
238 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
239 // it matches classes in the package that have the class name as a prefix.
240 Exclude_filter []string
241 }
242
Andreas Gampef3e5b552018-01-22 21:27:21 -0800243 Errorprone struct {
244 // List of javac flags that should only be used when running errorprone.
245 Javacflags []string
Colin Cross748b2d82020-11-19 13:52:06 -0800246
247 // List of java_plugin modules that provide extra errorprone checks.
248 Extra_check_modules []string
Andreas Gampef3e5b552018-01-22 21:27:21 -0800249 }
250
Colin Cross0f2ee152017-12-14 15:22:43 -0800251 Proto struct {
252 // List of extra options that will be passed to the proto generator.
253 Output_params []string
254 }
255
Colin Crosscb933592017-11-22 13:49:43 -0800256 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800257
258 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800259 Services []string `android:"path,arch_variant"`
Colin Cross0b67a8b2020-06-18 15:52:01 -0700260
261 // If true, package the kotlin stdlib into the jar. Defaults to true.
262 Static_kotlin_stdlib *bool `android:"arch_variant"`
Paul Duffin031d8692021-02-12 11:46:42 +0000263
264 // A list of java_library instances that provide additional hiddenapi annotations for the library.
265 Hiddenapi_additional_annotations []string
Colin Cross540eff82017-06-22 17:01:52 -0700266}
267
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800268// Properties that are specific to device modules. Host module factories should not add these when
269// constructing a new module.
270type DeviceProperties struct {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900271 // if not blank, set to the version of the sdk to compile against.
272 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800273 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700274
Colin Cross83bb3162018-06-25 15:48:06 -0700275 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
276 // Defaults to sdk_version if not set.
277 Min_sdk_version *string
278
Dan Willemsen419290a2018-10-31 15:28:47 -0700279 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
280 // Defaults to sdk_version if not set.
281 Target_sdk_version *string
282
Jeongik Cha356dac42019-08-19 14:09:52 +0900283 // Whether to compile against the platform APIs instead of an SDK.
284 // If true, then sdk_version must be empty. The value of this field
285 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700286 Platform_apis *bool
287
Colin Crossebe1a512017-11-14 13:12:14 -0800288 Aidl struct {
289 // Top level directories to pass to aidl tool
290 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700291
Colin Crossebe1a512017-11-14 13:12:14 -0800292 // Directories rooted at the Android.bp file to pass to aidl tool
293 Local_include_dirs []string
294
295 // directories that should be added as include directories for any aidl sources of modules
296 // that depend on this module, as well as to aidl for this module.
297 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100298
299 // whether to generate traces (for systrace) for this interface
300 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100301
302 // whether to generate Binder#GetTransaction name method.
303 Generate_get_transaction_name *bool
Jooyung Hane197d8b2021-01-05 10:33:16 +0900304
305 // list of flags that will be passed to the AIDL compiler
306 Flags []string
Colin Crossebe1a512017-11-14 13:12:14 -0800307 }
Colin Cross92430102017-10-09 14:59:32 -0700308
309 // If true, export a copy of the module as a -hostdex module for host testing.
310 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700311
Colin Cross7f87f4f2019-04-24 13:41:45 -0700312 Target struct {
313 Hostdex struct {
314 // Additional required dependencies to add to -hostdex modules.
315 Required []string
316 }
317 }
318
Paul Duffine25c6442019-10-11 13:50:28 +0100319 // When targeting 1.9 and above, override the modules to use with --system,
320 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700321 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700322
Paul Duffina2058f82020-06-24 16:22:38 +0100323 // The name of the module as used in build configuration.
324 //
325 // Allows a library to separate its actual name from the name used in
326 // build configuration, e.g.ctx.Config().BootJars().
327 ConfigurationName *string `blueprint:"mutated"`
328
Jiyong Park4c4c0242019-10-21 14:53:15 +0900329 // set the name of the output
330 Stem *string
331
David Srbeckye033cba2020-05-20 22:20:28 +0100332 IsSDKLibrary bool `blueprint:"mutated"`
Songchun Fan17d69e32020-03-24 20:32:24 -0700333
334 // If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file.
335 // Defaults to false.
336 V4_signature *bool
Colin Cross75ce9ec2021-02-26 16:20:32 -0800337
338 // Only for libraries created by a sysprop_library module, SyspropPublicStub is the name of the
339 // public stubs library.
340 SyspropPublicStub string `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700341}
342
Paul Duffin0d3c2e12020-05-17 08:34:50 +0100343// Functionality common to Module and Import
Paul Duffin859fe962020-05-15 10:20:31 +0100344//
345// It is embedded in Module so its functionality can be used by methods in Module
346// but it is currently only initialized by Import and Library.
Paul Duffin0d3c2e12020-05-17 08:34:50 +0100347type embeddableInModuleAndImport struct {
Paul Duffin859fe962020-05-15 10:20:31 +0100348
349 // Functionality related to this being used as a component of a java_sdk_library.
350 EmbeddableSdkLibraryComponent
351}
352
353func (e *embeddableInModuleAndImport) initModuleAndImport(moduleBase *android.ModuleBase) {
354 e.initSdkLibraryComponent(moduleBase)
Paul Duffin0d3c2e12020-05-17 08:34:50 +0100355}
356
357// Module/Import's DepIsInSameApex(...) delegates to this method.
358//
359// This cannot implement DepIsInSameApex(...) directly as that leads to ambiguity with
360// the one provided by ApexModuleBase.
361func (e *embeddableInModuleAndImport) depIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
362 // dependencies other than the static linkage are all considered crossing APEX boundary
363 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
364 return true
365 }
366 return false
367}
368
Colin Cross46c9b8b2017-06-22 16:51:17 -0700369// Module contains the properties and members used by all java module types
370type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700371 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700372 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900373 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900374 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700375
Paul Duffin0d3c2e12020-05-17 08:34:50 +0100376 // Functionality common to Module and Import.
377 embeddableInModuleAndImport
378
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800379 properties CommonProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700380 protoProperties android.ProtoProperties
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800381 deviceProperties DeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700382
Colin Cross331a1212018-08-15 20:40:52 -0700383 // jar file containing header classes including static library dependencies, suitable for
384 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700385 headerJarFile android.Path
386
Colin Cross331a1212018-08-15 20:40:52 -0700387 // jar file containing implementation classes including static library dependencies but no
388 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700389 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700390
Colin Cross331a1212018-08-15 20:40:52 -0700391 // jar file containing only resources including from static library dependencies
392 resourceJar android.Path
393
Colin Cross0c4ce212019-05-03 15:28:19 -0700394 // args and dependencies to package source files into a srcjar
395 srcJarArgs []string
396 srcJarDeps android.Paths
397
Colin Cross331a1212018-08-15 20:40:52 -0700398 // jar file containing implementation classes and resources including static library
399 // dependencies
400 implementationAndResourcesJar android.Path
401
402 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700403 dexJarFile android.Path
404
Colin Crosscb933592017-11-22 13:49:43 -0800405 // output file containing uninstrumented classes that will be instrumented by jacoco
406 jacocoReportClassesFile android.Path
407
Colin Cross331a1212018-08-15 20:40:52 -0700408 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700409 outputFile android.Path
410 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700411
Colin Cross635c3b02016-05-18 15:37:25 -0700412 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700413
Colin Cross635c3b02016-05-18 15:37:25 -0700414 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700415
Colin Cross2fe66872015-03-30 17:20:39 -0700416 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700417 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800418
419 // list of .java files and srcjars that was passed to javac
420 compiledJavaSrcs android.Paths
421 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800422
Colin Cross094054a2018-10-17 15:10:48 -0700423 // manifest file to use instead of properties.Manifest
424 overrideManifest android.OptionalPath
425
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000426 // map of SDK version to class loader context
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +0100427 classLoaderContexts dexpreopt.ClassLoaderContextMap
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700428
Artur Satayev9cf46692019-11-26 18:08:34 +0000429 // list of plugins that this java module is exporting
430 exportedPluginJars android.Paths
431
432 // list of plugins that this java module is exporting
433 exportedPluginClasses []string
434
Colin Crossc9fe10f2020-11-19 18:06:03 -0800435 // if true, the exported plugins generate API and require disabling turbine.
436 exportedDisableTurbine bool
437
Artur Satayev9cf46692019-11-26 18:08:34 +0000438 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800439 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700440 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800441
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800442 // expanded Jarjar_rules
443 expandJarjarRules android.Path
444
Vladimir Marko0975ee02019-04-02 10:29:55 +0100445 // list of additional targets for checkbuild
446 additionalCheckedModules android.Paths
447
Colin Cross988708c2019-05-06 14:04:11 -0700448 // Extra files generated by the module type to be added as java resources.
449 extraResources android.Paths
450
Colin Crossf24a22a2019-01-31 14:12:44 -0800451 hiddenAPI
Liz Kammera7a64f32020-07-09 15:16:41 -0700452 dexer
Colin Cross43f08db2018-11-12 10:13:39 -0800453 dexpreopter
Ulya Trafimovich21a73752020-09-01 17:33:48 +0100454 usesLibrary
Colin Cross014489c2020-06-02 20:09:13 -0700455 linter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800456
457 // list of the xref extraction files
458 kytheFiles android.Paths
Anton Hansson78156ef2020-03-27 19:39:48 +0000459
bralee1fbf4402020-05-21 10:11:59 +0800460 // Collect the module directory for IDE info in java/jdeps.go.
461 modulePaths []string
Colin Cross56a83212020-09-15 18:30:11 -0700462
463 hideApexVariantFromMake bool
Colin Cross2fe66872015-03-30 17:20:39 -0700464}
465
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -0800466func (j *Module) CheckStableSdkVersion() error {
467 sdkVersion := j.sdkVersion()
468 if sdkVersion.stable() {
469 return nil
470 }
471 if sdkVersion.kind == sdkCorePlatform {
472 if useLegacyCorePlatformApiByName(j.BaseModuleName()) {
473 return fmt.Errorf("non stable SDK %v - uses legacy core platform", sdkVersion)
474 } else {
475 // Treat stable core platform as stable.
476 return nil
477 }
478 } else {
479 return fmt.Errorf("non stable SDK %v", sdkVersion)
480 }
481}
482
483// checkSdkVersions enforces restrictions around SDK dependencies.
484func (j *Module) checkSdkVersions(ctx android.ModuleContext) {
485 if j.RequiresStableAPIs(ctx) {
486 if sc, ok := ctx.Module().(sdkContext); ok {
487 if !sc.sdkVersion().specified() {
488 ctx.PropertyErrorf("sdk_version",
489 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
490 }
491 }
492 }
493
494 // Make sure this module doesn't statically link to modules with lower-ranked SDK link type.
495 // See rank() for details.
496 ctx.VisitDirectDeps(func(module android.Module) {
497 tag := ctx.OtherModuleDependencyTag(module)
498 switch module.(type) {
499 // TODO(satayev): cover other types as well, e.g. imports
500 case *Library, *AndroidLibrary:
501 switch tag {
502 case bootClasspathTag, libTag, staticLibTag, java9LibTag:
503 j.checkSdkLinkType(ctx, module.(moduleWithSdkDep), tag.(dependencyTag))
504 }
505 }
506 })
507}
508
509func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
510 if sc, ok := ctx.Module().(sdkContext); ok {
511 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
512 sdkVersionSpecified := sc.sdkVersion().specified()
513 if usePlatformAPI && sdkVersionSpecified {
514 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
515 } else if !usePlatformAPI && !sdkVersionSpecified {
516 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
517 }
518
519 }
520}
521
Colin Crossce6734e2020-06-15 16:09:53 -0700522func (j *Module) addHostProperties() {
523 j.AddProperties(
524 &j.properties,
525 &j.protoProperties,
Ulya Trafimovich21a73752020-09-01 17:33:48 +0100526 &j.usesLibraryProperties,
Colin Crossce6734e2020-06-15 16:09:53 -0700527 )
528}
529
530func (j *Module) addHostAndDeviceProperties() {
531 j.addHostProperties()
532 j.AddProperties(
533 &j.deviceProperties,
Liz Kammera7a64f32020-07-09 15:16:41 -0700534 &j.dexer.dexProperties,
Colin Crossce6734e2020-06-15 16:09:53 -0700535 &j.dexpreoptProperties,
Colin Cross014489c2020-06-02 20:09:13 -0700536 &j.linter.properties,
Colin Crossce6734e2020-06-15 16:09:53 -0700537 )
538}
539
Colin Cross41955e82019-05-29 14:40:35 -0700540func (j *Module) OutputFiles(tag string) (android.Paths, error) {
541 switch tag {
542 case "":
543 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Paul Duffin74f05592020-11-25 16:37:46 +0000544 case android.DefaultDistTag:
545 return android.Paths{j.outputFile}, nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700546 case ".jar":
547 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700548 case ".proguard_map":
Liz Kammera7a64f32020-07-09 15:16:41 -0700549 if j.dexer.proguardDictionary.Valid() {
550 return android.Paths{j.dexer.proguardDictionary.Path()}, nil
551 }
552 return nil, fmt.Errorf("%q was requested, but no output file was found.", tag)
Colin Cross41955e82019-05-29 14:40:35 -0700553 default:
554 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
555 }
Colin Cross54250902017-12-05 09:28:08 -0800556}
557
Colin Cross41955e82019-05-29 14:40:35 -0700558var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800559
Colin Crossdcf71b22021-02-01 13:59:03 -0800560// JavaInfo contains information about a java module for use by modules that depend on it.
561type JavaInfo struct {
562 // HeaderJars is a list of jars that can be passed as the javac classpath in order to link
563 // against this module. If empty, ImplementationJars should be used instead.
564 HeaderJars android.Paths
565
566 // ImplementationAndResourceJars is a list of jars that contain the implementations of classes
567 // in the module as well as any resources included in the module.
568 ImplementationAndResourcesJars android.Paths
569
570 // ImplementationJars is a list of jars that contain the implementations of classes in the
571 //module.
572 ImplementationJars android.Paths
573
574 // ResourceJars is a list of jars that contain the resources included in the module.
575 ResourceJars android.Paths
576
577 // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when
578 // depending on this module.
579 AidlIncludeDirs android.Paths
580
581 // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this
582 // module.
583 SrcJarArgs []string
584
585 // SrcJarDeps is a list of paths to depend on when packaging the sources of this module.
586 SrcJarDeps android.Paths
587
588 // ExportedPlugins is a list of paths that should be used as annotation processors for any
589 // module that depends on this module.
590 ExportedPlugins android.Paths
591
592 // ExportedPluginClasses is a list of classes that should be run as annotation processors for
593 // any module that depends on this module.
594 ExportedPluginClasses []string
595
596 // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs,
597 // requiring disbling turbine for any modules that depend on it.
598 ExportedPluginDisableTurbine bool
599
600 // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be
601 // instrumented by jacoco.
602 JacocoReportClassesFile android.Path
603}
604
605var JavaInfoProvider = blueprint.NewProvider(JavaInfo{})
606
Colin Cross75ce9ec2021-02-26 16:20:32 -0800607// SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to
608// the sysprop implementation library.
609type SyspropPublicStubInfo struct {
610 // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to
611 // the sysprop implementation library.
612 JavaInfo JavaInfo
613}
614
615var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{})
616
Paul Duffin44b481b2020-06-17 16:59:43 +0100617// Methods that need to be implemented for a module that is added to apex java_libs property.
618type ApexDependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700619 HeaderJars() android.Paths
Paul Duffin44b481b2020-06-17 16:59:43 +0100620 ImplementationAndResourcesJars() android.Paths
621}
622
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100623// Provides build path and install path to DEX jars.
624type UsesLibraryDependency interface {
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +0000625 DexJarBuildPath() android.Path
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +0100626 DexJarInstallPath() android.Path
Ulya Trafimovichdbf31662020-12-17 12:07:54 +0000627 ClassLoaderContexts() dexpreopt.ClassLoaderContextMap
Ulya Trafimovich31e444e2020-08-14 17:32:16 +0100628}
629
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800630type xref interface {
631 XrefJavaFiles() android.Paths
632}
633
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800634func (j *Module) XrefJavaFiles() android.Paths {
635 return j.kytheFiles
636}
637
Colin Cross89536d42017-07-07 14:35:50 -0700638func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
Liz Kammerdd849a82020-06-12 16:38:45 -0700639 initJavaModule(module, hod, false)
640}
641
642func InitJavaModuleMultiTargets(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
643 initJavaModule(module, hod, true)
644}
645
646func initJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported, multiTargets bool) {
647 multilib := android.MultilibCommon
648 if multiTargets {
649 android.InitAndroidMultiTargetsArchModule(module, hod, multilib)
650 } else {
651 android.InitAndroidArchModule(module, hod, multilib)
652 }
Colin Cross89536d42017-07-07 14:35:50 -0700653 android.InitDefaultableModule(module)
654}
655
Colin Crossbe1da472017-07-07 15:59:46 -0700656type dependencyTag struct {
657 blueprint.BaseDependencyTag
658 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700659}
660
Colin Crosse9fe2942020-11-10 18:12:15 -0800661// installDependencyTag is a dependency tag that is annotated to cause the installed files of the
662// dependency to be installed when the parent module is installed.
663type installDependencyTag struct {
664 blueprint.BaseDependencyTag
665 android.InstallAlwaysNeededDependencyTag
666 name string
667}
668
Ulya Trafimovichb5218112020-10-07 15:11:32 +0100669type usesLibraryDependencyTag struct {
670 dependencyTag
671 sdkVersion int // SDK version in which the library appared as a standalone library.
672}
673
674func makeUsesLibraryDependencyTag(sdkVersion int) usesLibraryDependencyTag {
675 return usesLibraryDependencyTag{
676 dependencyTag: dependencyTag{name: fmt.Sprintf("uses-library-%d", sdkVersion)},
677 sdkVersion: sdkVersion,
678 }
679}
680
Jiyong Park8be103b2019-11-08 15:53:48 +0900681func IsJniDepTag(depTag blueprint.DependencyTag) bool {
Colin Crossde78d132020-10-09 18:59:49 -0700682 return depTag == jniLibTag
Jiyong Park8be103b2019-11-08 15:53:48 +0900683}
684
Colin Crossbe1da472017-07-07 15:59:46 -0700685var (
Colin Cross75ce9ec2021-02-26 16:20:32 -0800686 dataNativeBinsTag = dependencyTag{name: "dataNativeBins"}
687 staticLibTag = dependencyTag{name: "staticlib"}
688 libTag = dependencyTag{name: "javalib"}
689 java9LibTag = dependencyTag{name: "java9lib"}
690 pluginTag = dependencyTag{name: "plugin"}
691 errorpronePluginTag = dependencyTag{name: "errorprone-plugin"}
692 exportedPluginTag = dependencyTag{name: "exported-plugin"}
693 bootClasspathTag = dependencyTag{name: "bootclasspath"}
694 systemModulesTag = dependencyTag{name: "system modules"}
695 frameworkResTag = dependencyTag{name: "framework-res"}
696 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
697 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
698 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
699 certificateTag = dependencyTag{name: "certificate"}
700 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
701 extraLintCheckTag = dependencyTag{name: "extra-lint-check"}
702 jniLibTag = dependencyTag{name: "jnilib"}
703 syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"}
704 jniInstallTag = installDependencyTag{name: "jni install"}
705 binaryInstallTag = installDependencyTag{name: "binary install"}
706 usesLibTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion)
707 usesLibCompat28Tag = makeUsesLibraryDependencyTag(28)
708 usesLibCompat29Tag = makeUsesLibraryDependencyTag(29)
709 usesLibCompat30Tag = makeUsesLibraryDependencyTag(30)
Colin Crossbe1da472017-07-07 15:59:46 -0700710)
Colin Cross2fe66872015-03-30 17:20:39 -0700711
Jiyong Park83dc74b2020-01-14 18:38:44 +0900712func IsLibDepTag(depTag blueprint.DependencyTag) bool {
713 return depTag == libTag
714}
715
716func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
717 return depTag == staticLibTag
718}
719
Colin Crossfc3674a2017-09-18 17:41:52 -0700720type sdkDep struct {
Pete Gilline3d44b22020-06-29 11:28:51 +0100721 useModule, useFiles, invalidVersion bool
Colin Cross47ff2522017-10-02 14:22:08 -0700722
Colin Cross6cef4812019-10-17 14:23:50 -0700723 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
724 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100725
726 // The default system modules to use. Will be an empty string if no system
727 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700728 systemModules string
729
Pete Gilline3d44b22020-06-29 11:28:51 +0100730 // The modules that will be added to the classpath regardless of the Java language level targeted
731 classpath []string
732
Colin Cross6cef4812019-10-17 14:23:50 -0700733 // The modules that will be added ot the classpath when targeting 1.9 or higher
Pete Gilline3d44b22020-06-29 11:28:51 +0100734 // (normally these will be on the bootclasspath when targeting 1.8 or lower)
Colin Cross6cef4812019-10-17 14:23:50 -0700735 java9Classpath []string
736
Colin Crossa97c5d32018-03-28 14:58:31 -0700737 frameworkResModule string
738
Colin Cross86a60ae2018-05-29 14:44:55 -0700739 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700740 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100741
742 noStandardLibs, noFrameworksLibs bool
743}
744
745func (s sdkDep) hasStandardLibs() bool {
746 return !s.noStandardLibs
747}
748
749func (s sdkDep) hasFrameworkLibs() bool {
750 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700751}
752
Colin Crossa4f08812018-10-02 22:03:40 -0700753type jniLib struct {
Colin Cross403cc152020-07-06 14:15:24 -0700754 name string
755 path android.Path
756 target android.Target
757 coverageFile android.OptionalPath
758 unstrippedFile android.Path
Colin Crossa4f08812018-10-02 22:03:40 -0700759}
760
Colin Cross0ea8ba82019-06-06 14:33:29 -0700761func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Roland Levillainada12702020-06-09 13:07:36 +0100762 return j.properties.Instrument &&
763 ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") &&
764 ctx.DeviceConfig().JavaCoverageEnabledForPath(ctx.ModuleDir())
Colin Cross3144dfc2018-01-03 15:06:47 -0800765}
766
Colin Cross0ea8ba82019-06-06 14:33:29 -0700767func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800768 return j.shouldInstrument(ctx) &&
769 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
770 ctx.Config().UnbundledBuild())
771}
772
Chris Gross190fdc02020-05-29 16:01:19 +0000773func (j *Module) shouldInstrumentInApex(ctx android.BaseModuleContext) bool {
774 // Force enable the instrumentation for java code that is built for APEXes ...
775 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
776 // doesn't make sense) or framework libraries (e.g. libraries found in the InstrumentFrameworkModules list) unless EMMA_INSTRUMENT_FRAMEWORK is true.
Colin Cross56a83212020-09-15 18:30:11 -0700777 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Chris Gross190fdc02020-05-29 16:01:19 +0000778 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
Colin Cross56a83212020-09-15 18:30:11 -0700779 if j.DirectlyInAnyApex() && !isJacocoAgent && !apexInfo.IsForPlatform() {
Chris Gross190fdc02020-05-29 16:01:19 +0000780 if !inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
781 return true
782 } else if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
783 return true
784 }
785 }
786 return false
787}
788
Jiyong Park6a927c42020-01-21 02:03:43 +0900789func (j *Module) sdkVersion() sdkSpec {
790 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700791}
792
Paul Duffine25c6442019-10-11 13:50:28 +0100793func (j *Module) systemModules() string {
794 return proptools.String(j.deviceProperties.System_modules)
795}
796
Jiyong Park6a927c42020-01-21 02:03:43 +0900797func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700798 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900799 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700800 }
801 return j.sdkVersion()
802}
803
Jiyong Park6a927c42020-01-21 02:03:43 +0900804func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700805 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900806 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700807 }
808 return j.sdkVersion()
809}
810
Artur Satayev480e25b2020-04-27 18:53:18 +0100811func (j *Module) MinSdkVersion() string {
812 return j.minSdkVersion().version.String()
813}
814
Jiyong Parkb02bb402019-12-03 00:43:57 +0900815func (j *Module) AvailableFor(what string) bool {
816 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
817 // Exception: for hostdex: true libraries, the platform variant is created
818 // even if it's not marked as available to platform. In that case, the platform
819 // variant is used only for the hostdex and not installed to the device.
820 return true
821 }
822 return j.ApexModuleBase.AvailableFor(what)
823}
824
Liz Kammerd6c31d22020-08-05 15:40:41 -0700825func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext sdkContext, d dexer) {
826 sdkDep := decodeSdkDep(ctx, sdkContext)
827 if sdkDep.useModule {
828 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
829 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
830 ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...)
831 if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
832 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...)
833 }
834 if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() {
835 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...)
836 }
837 }
838 if sdkDep.systemModules != "" {
839 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
840 }
841}
842
Colin Crossbe1da472017-07-07 15:59:46 -0700843func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700844 if ctx.Device() {
Colin Cross92e4b462020-06-18 15:56:48 -0700845 j.linter.deps(ctx)
846
Liz Kammerd6c31d22020-08-05 15:40:41 -0700847 sdkDeps(ctx, sdkContext(j), j.dexer)
Colin Cross1369cdb2017-09-29 17:58:17 -0700848
Colin Cross75ce9ec2021-02-26 16:20:32 -0800849 if j.deviceProperties.SyspropPublicStub != "" {
850 // This is a sysprop implementation library that has a corresponding sysprop public
851 // stubs library, and a dependency on it so that dependencies on the implementation can
852 // be forwarded to the public stubs library when necessary.
853 ctx.AddVariationDependencies(nil, syspropPublicStubDepTag, j.deviceProperties.SyspropPublicStub)
Inseob Kimac1e9862019-12-09 18:15:47 +0900854 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900855 }
856
Colin Cross75ce9ec2021-02-26 16:20:32 -0800857 libDeps := ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
858 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700859
Paul Duffin031d8692021-02-12 11:46:42 +0000860 // Add dependency on libraries that provide additional hidden api annotations.
861 ctx.AddVariationDependencies(nil, hiddenApiAnnotationsTag, j.properties.Hiddenapi_additional_annotations...)
862
JaeMan Parkff715562020-10-19 17:25:58 +0900863 if ctx.DeviceConfig().VndkVersion() != "" && ctx.Config().EnforceInterPartitionJavaSdkLibrary() {
864 // Require java_sdk_library at inter-partition java dependency to ensure stable
865 // interface between partitions. If inter-partition java_library dependency is detected,
866 // raise build error because java_library doesn't have a stable interface.
867 //
868 // Inputs:
869 // PRODUCT_ENFORCE_INTER_PARTITION_JAVA_SDK_LIBRARY
870 // if true, enable enforcement
871 // PRODUCT_INTER_PARTITION_JAVA_LIBRARY_ALLOWLIST
872 // exception list of java_library names to allow inter-partition dependency
Colin Cross75ce9ec2021-02-26 16:20:32 -0800873 for idx := range j.properties.Libs {
JaeMan Parkff715562020-10-19 17:25:58 +0900874 if libDeps[idx] == nil {
875 continue
876 }
877
JaeMan Parkff715562020-10-19 17:25:58 +0900878 if javaDep, ok := libDeps[idx].(javaSdkLibraryEnforceContext); ok {
879 // java_sdk_library is always allowed at inter-partition dependency.
880 // So, skip check.
881 if _, ok := javaDep.(*SdkLibrary); ok {
882 continue
883 }
884
885 j.checkPartitionsForJavaDependency(ctx, "libs", javaDep)
886 }
887 }
888 }
889
Ulya Trafimovich39b437b2020-09-23 16:42:35 +0100890 // For library dependencies that are component libraries (like stubs), add the implementation
891 // as a dependency (dexpreopt needs to be against the implementation library, not stubs).
892 for _, dep := range libDeps {
893 if dep != nil {
894 if component, ok := dep.(SdkLibraryComponentDependency); ok {
895 if lib := component.OptionalSdkLibraryImplementation(); lib != nil {
896 ctx.AddVariationDependencies(nil, usesLibTag, *lib)
897 }
898 }
899 }
900 }
901
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700902 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Colin Cross748b2d82020-11-19 13:52:06 -0800903 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), errorpronePluginTag, j.properties.Errorprone.Extra_check_modules...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000904 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800905
Colin Crossfe17f6f2019-03-28 19:30:56 -0700906 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700907 if j.hasSrcExt(".proto") {
908 protoDeps(ctx, &j.protoProperties)
909 }
Colin Cross93e85952017-08-15 13:34:18 -0700910
911 if j.hasSrcExt(".kt") {
912 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
913 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700914 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
915 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800916 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800917 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
918 }
Colin Cross93e85952017-08-15 13:34:18 -0700919 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800920
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000921 // Framework libraries need special handling in static coverage builds: they should not have
922 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
923 // the same jacoco classes coming from different bootclasspath jars.
924 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
925 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
926 j.properties.Instrument = true
927 }
928 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700929 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800930 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700931}
932
933func hasSrcExt(srcs []string, ext string) bool {
934 for _, src := range srcs {
935 if filepath.Ext(src) == ext {
936 return true
937 }
938 }
939
940 return false
941}
942
943func (j *Module) hasSrcExt(ext string) bool {
944 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700945}
946
Colin Cross46c9b8b2017-06-22 16:51:17 -0700947func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700948 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700949
Colin Crossebe1a512017-11-14 13:12:14 -0800950 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
951 aidlIncludes = append(aidlIncludes,
952 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
953 aidlIncludes = append(aidlIncludes,
954 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700955
Colin Cross3047fa22019-04-18 10:56:44 -0700956 var flags []string
957 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700958
Jooyung Hane197d8b2021-01-05 10:33:16 +0900959 flags = append(flags, j.deviceProperties.Aidl.Flags...)
960
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700961 if aidlPreprocess.Valid() {
962 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700963 deps = append(deps, aidlPreprocess.Path())
964 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700965 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700966 }
967
Colin Cross3047fa22019-04-18 10:56:44 -0700968 if len(j.exportAidlIncludeDirs) > 0 {
969 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
970 }
971
972 if len(aidlIncludes) > 0 {
973 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
974 }
975
Colin Cross635c3b02016-05-18 15:37:25 -0700976 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800977 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700978 flags = append(flags, "-I"+src.String())
979 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700980
Martijn Coeneneab15642018-03-09 09:29:59 +0100981 if Bool(j.deviceProperties.Aidl.Generate_traces) {
982 flags = append(flags, "-t")
983 }
984
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100985 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
986 flags = append(flags, "--transaction_names")
987 }
988
Colin Cross3047fa22019-04-18 10:56:44 -0700989 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700990}
991
Colin Cross32f676a2017-09-06 13:41:06 -0700992type deps struct {
Colin Cross748b2d82020-11-19 13:52:06 -0800993 classpath classpath
994 java9Classpath classpath
995 bootClasspath classpath
996 processorPath classpath
997 errorProneProcessorPath classpath
998 processorClasses []string
999 staticJars android.Paths
1000 staticHeaderJars android.Paths
1001 staticResourceJars android.Paths
1002 aidlIncludeDirs android.Paths
1003 srcs android.Paths
1004 srcJars android.Paths
1005 systemModules *systemModules
1006 aidlPreprocess android.OptionalPath
1007 kotlinStdlib android.Paths
1008 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -08001009
1010 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -07001011}
Colin Cross2fe66872015-03-30 17:20:39 -07001012
Colin Cross54250902017-12-05 09:28:08 -08001013func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
1014 for _, f := range dep.Srcs() {
1015 if f.Ext() != ".jar" {
1016 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
1017 ctx.OtherModuleName(dep.(blueprint.Module)))
1018 }
1019 }
1020}
1021
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001022type sdkLinkType int
Jiyong Park2d492942018-03-05 17:44:10 +09001023
1024const (
Jiyong Park50146e92020-01-30 18:00:15 +09001025 // TODO(jiyong) rename these for better readability. Make the allowed
1026 // and disallowed link types explicit
Jiyong Park7f87e1a2021-02-18 20:29:05 +09001027 // order is important here. See rank()
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001028 javaCore sdkLinkType = iota
Jiyong Park2d492942018-03-05 17:44:10 +09001029 javaSdk
1030 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +09001031 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +09001032 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +09001033 javaPlatform
1034)
1035
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001036func (lt sdkLinkType) String() string {
Jiyong Park670e0f62021-02-18 13:10:18 +09001037 switch lt {
1038 case javaCore:
1039 return "core Java API"
1040 case javaSdk:
1041 return "Android API"
1042 case javaSystem:
1043 return "system API"
1044 case javaModule:
1045 return "module API"
1046 case javaSystemServer:
1047 return "system server API"
1048 case javaPlatform:
1049 return "private API"
1050 default:
Jiyong Parkd4cbf342021-02-23 11:14:31 +09001051 panic(fmt.Errorf("unrecognized linktype: %d", lt))
Jiyong Park670e0f62021-02-18 13:10:18 +09001052 }
1053}
1054
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001055// rank determines the total order among sdkLinkType. An SDK link type of rank A can link to
1056// another SDK link type of rank B only when B <= A. For example, a module linking to Android SDK
1057// can't statically depend on modules that use Platform API.
1058func (lt sdkLinkType) rank() int {
Jiyong Park7f87e1a2021-02-18 20:29:05 +09001059 return int(lt)
1060}
1061
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001062type moduleWithSdkDep interface {
Jeongik Cha75b83b02019-11-01 15:28:00 +09001063 android.Module
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001064 getSdkLinkType(name string) (ret sdkLinkType, stubs bool)
Jeongik Cha75b83b02019-11-01 15:28:00 +09001065}
1066
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001067func (m *Module) getSdkLinkType(name string) (ret sdkLinkType, stubs bool) {
Anton Hanssoncc51a682020-05-19 12:06:48 +01001068 switch name {
Pete Gillin1f41dbf2020-06-02 15:59:45 +01001069 case "core.current.stubs", "legacy.core.platform.api.stubs", "stable.core.platform.api.stubs",
1070 "stub-annotations", "private-stub-annotations-jar",
1071 "core-lambda-stubs", "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +09001072 return javaCore, true
Anton Hanssoncc51a682020-05-19 12:06:48 +01001073 case "android_stubs_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +09001074 return javaSdk, true
Anton Hanssoncc51a682020-05-19 12:06:48 +01001075 case "android_system_stubs_current":
1076 return javaSystem, true
1077 case "android_module_lib_stubs_current":
Jiyong Park50146e92020-01-30 18:00:15 +09001078 return javaModule, true
Anton Hanssoncc51a682020-05-19 12:06:48 +01001079 case "android_system_server_stubs_current":
Jiyong Parkaae9bd12020-02-12 04:36:43 +09001080 return javaSystemServer, true
Anton Hanssoncc51a682020-05-19 12:06:48 +01001081 case "android_test_stubs_current":
1082 return javaSystem, true
Jiyong Park2d492942018-03-05 17:44:10 +09001083 }
Anton Hanssoncc51a682020-05-19 12:06:48 +01001084
Anton Hansson2d0c1942020-05-25 12:20:51 +01001085 if stub, linkType := moduleStubLinkType(name); stub {
1086 return linkType, true
1087 }
1088
Anton Hanssoncc51a682020-05-19 12:06:48 +01001089 ver := m.sdkVersion()
1090 switch ver.kind {
1091 case sdkCore:
1092 return javaCore, false
1093 case sdkSystem:
1094 return javaSystem, false
1095 case sdkPublic:
1096 return javaSdk, false
1097 case sdkModule:
1098 return javaModule, false
1099 case sdkSystemServer:
1100 return javaSystemServer, false
1101 case sdkPrivate, sdkNone, sdkCorePlatform, sdkTest:
1102 return javaPlatform, false
1103 }
1104
1105 if !ver.valid() {
1106 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
1107 }
1108 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +09001109}
1110
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001111// checkSdkLinkType make sures the given dependency doesn't have a lower SDK link type rank than
1112// this module's. See the comment on rank() for details and an example.
1113func (j *Module) checkSdkLinkType(
1114 ctx android.ModuleContext, dep moduleWithSdkDep, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -07001115 if ctx.Host() {
1116 return
1117 }
1118
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001119 myLinkType, stubs := j.getSdkLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +09001120 if stubs {
1121 return
1122 }
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001123 depLinkType, _ := dep.getSdkLinkType(ctx.OtherModuleName(dep))
Jiyong Park2d492942018-03-05 17:44:10 +09001124
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001125 if myLinkType.rank() < depLinkType.rank() {
Jiyong Park670e0f62021-02-18 13:10:18 +09001126 ctx.ModuleErrorf("compiles against %v, but dependency %q is compiling against %v. "+
1127 "In order to fix this, consider adjusting sdk_version: OR platform_apis: "+
1128 "property of the source or target module so that target module is built "+
1129 "with the same or smaller API set when compared to the source.",
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001130 myLinkType, ctx.OtherModuleName(dep), depLinkType)
Jiyong Park670e0f62021-02-18 13:10:18 +09001131 }
Jiyong Park750e5572018-01-31 00:20:13 +09001132}
1133
Colin Cross32f676a2017-09-06 13:41:06 -07001134func (j *Module) collectDeps(ctx android.ModuleContext) deps {
1135 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -07001136
Colin Cross300f0382018-03-06 13:11:51 -08001137 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -07001138 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -08001139 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -07001140 ctx.AddMissingDependencies(sdkDep.bootclasspath)
1141 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -08001142 } else if sdkDep.useFiles {
1143 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -07001144 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -07001145 deps.aidlPreprocess = sdkDep.aidl
1146 } else {
1147 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -08001148 }
Colin Crossfc3674a2017-09-18 17:41:52 -07001149 }
1150
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001151 sdkLinkType, _ := j.getSdkLinkType(ctx.ModuleName())
Colin Cross75ce9ec2021-02-26 16:20:32 -08001152
Colin Crossd11fcda2017-10-23 17:59:01 -07001153 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -07001154 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -07001155 tag := ctx.OtherModuleDependencyTag(module)
1156
Colin Crossde78d132020-10-09 18:59:49 -07001157 if IsJniDepTag(tag) {
Colin Crossbd01e2a2018-10-04 15:21:03 -07001158 // Handled by AndroidApp.collectAppDeps
1159 return
1160 }
1161 if tag == certificateTag {
1162 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -07001163 return
1164 }
Artur Satayev2db1c3f2020-04-08 19:09:30 +01001165
Colin Crossdcf71b22021-02-01 13:59:03 -08001166 if dep, ok := module.(SdkLibraryDependency); ok {
Colin Cross897d2ed2019-02-11 14:03:51 -08001167 switch tag {
1168 case libTag:
1169 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Colin Cross79c7c262019-04-17 11:11:46 -07001170 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -08001171 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
1172 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001173 } else if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
1174 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08001175 if sdkLinkType != javaPlatform &&
Colin Cross75ce9ec2021-02-26 16:20:32 -08001176 ctx.OtherModuleHasProvider(module, SyspropPublicStubInfoProvider) {
1177 // dep is a sysprop implementation library, but this module is not linking against
1178 // the platform, so it gets the sysprop public stubs library instead. Replace
1179 // dep with the JavaInfo from the SyspropPublicStubInfoProvider.
1180 syspropDep := ctx.OtherModuleProvider(module, SyspropPublicStubInfoProvider).(SyspropPublicStubInfo)
1181 dep = syspropDep.JavaInfo
1182 }
Colin Cross54250902017-12-05 09:28:08 -08001183 switch tag {
1184 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001185 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars...)
Colin Cross4b964c02018-10-15 16:18:06 -07001186 case libTag, instrumentationForTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001187 deps.classpath = append(deps.classpath, dep.HeaderJars...)
1188 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
1189 addPlugins(&deps, dep.ExportedPlugins, dep.ExportedPluginClasses...)
1190 deps.disableTurbine = deps.disableTurbine || dep.ExportedPluginDisableTurbine
Colin Cross6cef4812019-10-17 14:23:50 -07001191 case java9LibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001192 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars...)
Colin Cross54250902017-12-05 09:28:08 -08001193 case staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001194 deps.classpath = append(deps.classpath, dep.HeaderJars...)
1195 deps.staticJars = append(deps.staticJars, dep.ImplementationJars...)
1196 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars...)
1197 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars...)
1198 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs...)
1199 addPlugins(&deps, dep.ExportedPlugins, dep.ExportedPluginClasses...)
Colin Crossc9fe10f2020-11-19 18:06:03 -08001200 // Turbine doesn't run annotation processors, so any module that uses an
1201 // annotation processor that generates API is incompatible with the turbine
1202 // optimization.
Colin Crossdcf71b22021-02-01 13:59:03 -08001203 deps.disableTurbine = deps.disableTurbine || dep.ExportedPluginDisableTurbine
Colin Crossbe9cdb82019-01-21 21:37:16 -08001204 case pluginTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001205 if plugin, ok := module.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -08001206 if plugin.pluginProperties.Processor_class != nil {
Colin Crossdcf71b22021-02-01 13:59:03 -08001207 addPlugins(&deps, dep.ImplementationAndResourcesJars, *plugin.pluginProperties.Processor_class)
Artur Satayev9cf46692019-11-26 18:08:34 +00001208 } else {
Colin Crossdcf71b22021-02-01 13:59:03 -08001209 addPlugins(&deps, dep.ImplementationAndResourcesJars)
Colin Crossbe9cdb82019-01-21 21:37:16 -08001210 }
Colin Crossc9fe10f2020-11-19 18:06:03 -08001211 // Turbine doesn't run annotation processors, so any module that uses an
1212 // annotation processor that generates API is incompatible with the turbine
1213 // optimization.
Colin Crossbe9cdb82019-01-21 21:37:16 -08001214 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
1215 } else {
1216 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
1217 }
Colin Cross748b2d82020-11-19 13:52:06 -08001218 case errorpronePluginTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001219 if _, ok := module.(*Plugin); ok {
1220 deps.errorProneProcessorPath = append(deps.errorProneProcessorPath, dep.ImplementationAndResourcesJars...)
Colin Cross748b2d82020-11-19 13:52:06 -08001221 } else {
1222 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
1223 }
Artur Satayev9cf46692019-11-26 18:08:34 +00001224 case exportedPluginTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001225 if plugin, ok := module.(*Plugin); ok {
1226 j.exportedPluginJars = append(j.exportedPluginJars, dep.ImplementationAndResourcesJars...)
Artur Satayev9cf46692019-11-26 18:08:34 +00001227 if plugin.pluginProperties.Processor_class != nil {
1228 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
1229 }
Colin Crossc9fe10f2020-11-19 18:06:03 -08001230 // Turbine doesn't run annotation processors, so any module that uses an
1231 // annotation processor that generates API is incompatible with the turbine
1232 // optimization.
1233 j.exportedDisableTurbine = Bool(plugin.pluginProperties.Generates_api)
Artur Satayev9cf46692019-11-26 18:08:34 +00001234 } else {
1235 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
1236 }
Colin Cross54250902017-12-05 09:28:08 -08001237 case kotlinStdlibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001238 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars...)
Colin Crossafbb1732019-01-17 15:42:52 -08001239 case kotlinAnnotationsTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08001240 deps.kotlinAnnotations = dep.HeaderJars
Colin Cross75ce9ec2021-02-26 16:20:32 -08001241 case syspropPublicStubDepTag:
1242 // This is a sysprop implementation library, forward the JavaInfoProvider from
1243 // the corresponding sysprop public stub library as SyspropPublicStubInfoProvider.
1244 ctx.SetProvider(SyspropPublicStubInfoProvider, SyspropPublicStubInfo{
1245 JavaInfo: dep,
1246 })
Colin Cross54250902017-12-05 09:28:08 -08001247 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001248 } else if dep, ok := module.(android.SourceFileProducer); ok {
Colin Cross54250902017-12-05 09:28:08 -08001249 switch tag {
1250 case libTag:
1251 checkProducesJars(ctx, dep)
1252 deps.classpath = append(deps.classpath, dep.Srcs()...)
1253 case staticLibTag:
1254 checkProducesJars(ctx, dep)
1255 deps.classpath = append(deps.classpath, dep.Srcs()...)
1256 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
1257 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -08001258 }
Colin Crossdcf71b22021-02-01 13:59:03 -08001259 } else {
Colin Crossec7a0422017-07-07 14:47:12 -07001260 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001261 case bootClasspathTag:
1262 // If a system modules dependency has been added to the bootclasspath
1263 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +00001264 sm := module.(SystemModulesProvider)
1265 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +01001266
Colin Cross1369cdb2017-09-29 17:58:17 -07001267 case systemModulesTag:
1268 if deps.systemModules != nil {
1269 panic("Found two system module dependencies")
1270 }
Paul Duffin83a2d962019-11-19 19:44:10 +00001271 sm := module.(SystemModulesProvider)
1272 outputDir, outputDeps := sm.OutputDirAndDeps()
1273 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001274 }
Colin Crossec7a0422017-07-07 14:47:12 -07001275 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00001276
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00001277 addCLCFromDep(ctx, module, j.classLoaderContexts)
Colin Cross2fe66872015-03-30 17:20:39 -07001278 })
1279
Colin Cross32f676a2017-09-06 13:41:06 -07001280 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001281}
1282
Artur Satayev9cf46692019-11-26 18:08:34 +00001283func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1284 deps.processorPath = append(deps.processorPath, pluginJars...)
1285 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1286}
1287
Colin Cross1e743852019-10-28 11:37:20 -07001288func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -07001289 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001290 return normalizeJavaVersion(ctx, javaVersion)
Colin Cross17dec172020-05-14 18:05:32 -07001291 } else if ctx.Device() {
1292 return sdkContext.sdkVersion().defaultJavaLanguageVersion(ctx)
Nan Zhang357466b2018-04-17 17:38:36 -07001293 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001294 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001295 }
Nan Zhang357466b2018-04-17 17:38:36 -07001296}
1297
Colin Cross1e743852019-10-28 11:37:20 -07001298type javaVersion int
1299
1300const (
1301 JAVA_VERSION_UNSUPPORTED = 0
1302 JAVA_VERSION_6 = 6
1303 JAVA_VERSION_7 = 7
1304 JAVA_VERSION_8 = 8
1305 JAVA_VERSION_9 = 9
1306)
1307
1308func (v javaVersion) String() string {
1309 switch v {
1310 case JAVA_VERSION_6:
1311 return "1.6"
1312 case JAVA_VERSION_7:
1313 return "1.7"
1314 case JAVA_VERSION_8:
1315 return "1.8"
1316 case JAVA_VERSION_9:
1317 return "1.9"
1318 default:
1319 return "unsupported"
1320 }
1321}
1322
1323// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1324func (v javaVersion) usesJavaModules() bool {
1325 return v >= 9
1326}
1327
1328func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001329 switch javaVersion {
1330 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001331 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001332 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001333 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001334 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001335 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001336 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001337 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001338 case "10", "11":
1339 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001340 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001341 default:
1342 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001343 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001344 }
1345}
1346
Nan Zhanged19fc32017-10-19 13:06:22 -07001347func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001348
Colin Crossf03c82b2015-04-13 13:53:40 -07001349 var flags javaBuilderFlags
1350
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001351 // javaVersion flag.
1352 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1353
Colin Cross66548102018-06-19 22:47:35 -07001354 if ctx.Config().RunErrorProne() {
Colin Cross748b2d82020-11-19 13:52:06 -08001355 if config.ErrorProneClasspath == nil && ctx.Config().TestProductVariables == nil {
Colin Cross66548102018-06-19 22:47:35 -07001356 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1357 }
1358
1359 errorProneFlags := []string{
1360 "-Xplugin:ErrorProne",
1361 "${config.ErrorProneChecks}",
1362 }
1363 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1364
1365 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1366 "'" + strings.Join(errorProneFlags, " ") + "'"
1367 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001368 }
1369
Nan Zhanged19fc32017-10-19 13:06:22 -07001370 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001371 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1372 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001373 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001374 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross748b2d82020-11-19 13:52:06 -08001375 flags.errorProneProcessorPath = append(flags.errorProneProcessorPath, deps.errorProneProcessorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001376
Colin Cross5a116862020-04-22 11:44:34 -07001377 flags.processors = append(flags.processors, deps.processorClasses...)
1378 flags.processors = android.FirstUniqueStrings(flags.processors)
Colin Crossbe9cdb82019-01-21 21:37:16 -08001379
Colin Cross1e743852019-10-28 11:37:20 -07001380 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1381 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001382 // Give host-side tools a version of OpenJDK's standard libraries
1383 // close to what they're targeting. As of Dec 2017, AOSP is only
1384 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1385 //
1386 // When building with OpenJDK 8, the following should have no
1387 // effect since those jars would be available by default.
1388 //
1389 // When building with OpenJDK 9 but targeting a version < 1.8,
1390 // putting them on the bootclasspath means that:
1391 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1392 // b) references to existing APIs are not reinterpreted in an
1393 // OpenJDK 9-specific way, eg. calls to subclasses of
1394 // java.nio.Buffer as in http://b/70862583
1395 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1396 flags.bootClasspath = append(flags.bootClasspath,
1397 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1398 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001399 if Bool(j.properties.Use_tools_jar) {
1400 flags.bootClasspath = append(flags.bootClasspath,
1401 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1402 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001403 }
1404
Nan Zhanged19fc32017-10-19 13:06:22 -07001405 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001406 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001407
Nan Zhanged19fc32017-10-19 13:06:22 -07001408 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001409 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001410
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001411 return flags
1412}
1413
Jingwen Chen5136a6e2020-10-30 01:01:35 -04001414func (j *Module) collectJavacFlags(
1415 ctx android.ModuleContext, flags javaBuilderFlags, srcFiles android.Paths) javaBuilderFlags {
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001416 // javac flags.
1417 javacFlags := j.properties.Javacflags
1418
1419 if ctx.Config().MinimizeJavaDebugInfo() && !ctx.Host() {
1420 // For non-host binaries, override the -g flag passed globally to remove
1421 // local variable debug info to reduce disk and memory usage.
1422 javacFlags = append(javacFlags, "-g:source,lines")
1423 }
1424 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
1425
1426 if flags.javaVersion.usesJavaModules() {
1427 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
1428
1429 if j.properties.Patch_module != nil {
1430 // Manually specify build directory in case it is not under the repo root.
Jingwen Chen5136a6e2020-10-30 01:01:35 -04001431 // (javac doesn't seem to expand into symbolic links when searching for patch-module targets, so
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001432 // just adding a symlink under the root doesn't help.)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04001433 patchPaths := []string{".", ctx.Config().BuildDir()}
1434
1435 // b/150878007
1436 //
1437 // Workaround to support *Bazel-executed* JDK9 javac in Bazel's
1438 // execution root for --patch-module. If this javac command line is
1439 // invoked within Bazel's execution root working directory, the top
1440 // level directories (e.g. libcore/, tools/, frameworks/) are all
1441 // symlinks. JDK9 javac does not traverse into symlinks, which causes
1442 // --patch-module to fail source file lookups when invoked in the
1443 // execution root.
1444 //
1445 // Short of patching javac or enumerating *all* directories as possible
1446 // input dirs, manually add the top level dir of the source files to be
1447 // compiled.
1448 topLevelDirs := map[string]bool{}
1449 for _, srcFilePath := range srcFiles {
1450 srcFileParts := strings.Split(srcFilePath.String(), "/")
1451 // Ignore source files that are already in the top level directory
1452 // as well as generated files in the out directory. The out
1453 // directory may be an absolute path, which means srcFileParts[0] is the
1454 // empty string, so check that as well. Note that "out" in Bazel's execution
1455 // root is *not* a symlink, which doesn't cause problems for --patch-modules
1456 // anyway, so it's fine to not apply this workaround for generated
1457 // source files.
1458 if len(srcFileParts) > 1 &&
1459 srcFileParts[0] != "" &&
1460 srcFileParts[0] != "out" {
1461 topLevelDirs[srcFileParts[0]] = true
1462 }
1463 }
1464 patchPaths = append(patchPaths, android.SortedStringKeys(topLevelDirs)...)
1465
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001466 classPath := flags.classpath.FormJavaClassPath("")
1467 if classPath != "" {
Jingwen Chen5136a6e2020-10-30 01:01:35 -04001468 patchPaths = append(patchPaths, classPath)
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001469 }
Jingwen Chen5136a6e2020-10-30 01:01:35 -04001470 javacFlags = append(
1471 javacFlags,
1472 "--patch-module="+String(j.properties.Patch_module)+"="+strings.Join(patchPaths, ":"))
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001473 }
1474 }
1475
Colin Cross81440082018-08-15 20:21:55 -07001476 if len(javacFlags) > 0 {
1477 // optimization.
1478 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1479 flags.javacFlags = "$javacFlags"
1480 }
1481
Nan Zhanged19fc32017-10-19 13:06:22 -07001482 return flags
1483}
Colin Crossc0b06f12015-04-08 13:03:43 -07001484
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001485func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001486 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001487
1488 deps := j.collectDeps(ctx)
1489 flags := j.collectBuilderFlags(ctx, deps)
1490
Colin Cross1e743852019-10-28 11:37:20 -07001491 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001492 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1493 }
Colin Cross8a497952019-03-05 22:25:09 -08001494 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001495 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001496 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001497 }
1498
Colin Crossa4c8cc62020-06-25 17:13:36 -07001499 kotlinCommonSrcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Common_srcs, nil)
1500 if len(kotlinCommonSrcFiles.FilterOutByExt(".kt")) > 0 {
1501 ctx.PropertyErrorf("common_srcs", "common_srcs must be .kt files")
1502 }
1503
Colin Crossaf050172017-11-15 23:01:59 -08001504 srcFiles = j.genSources(ctx, srcFiles, flags)
1505
Jingwen Chen5136a6e2020-10-30 01:01:35 -04001506 // Collect javac flags only after computing the full set of srcFiles to
1507 // ensure that the --patch-module lookup paths are complete.
1508 flags = j.collectJavacFlags(ctx, flags, srcFiles)
Jingwen Chen9cb8d1b2020-10-21 10:06:35 -04001509
Colin Crossaf050172017-11-15 23:01:59 -08001510 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001511 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001512 if aaptSrcJar != nil {
1513 srcJars = append(srcJars, aaptSrcJar)
1514 }
Colin Crossb7a63242015-04-16 14:09:14 -07001515
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001516 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001517 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001518 }
1519
Colin Cross1ee23172017-10-18 14:44:18 -07001520 jarName := ctx.ModuleName() + ".jar"
1521
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001522 javaSrcFiles := srcFiles.FilterByExt(".java")
1523 var uniqueSrcFiles android.Paths
1524 set := make(map[string]bool)
1525 for _, v := range javaSrcFiles {
1526 if _, found := set[v.String()]; !found {
1527 set[v.String()] = true
1528 uniqueSrcFiles = append(uniqueSrcFiles, v)
1529 }
1530 }
1531
patricktu242faad2019-09-24 15:41:30 +08001532 // Collect .java files for AIDEGen
1533 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1534
Colin Cross55f63ea2018-08-27 12:37:09 -07001535 var kotlinJars android.Paths
1536
Colin Cross93e85952017-08-15 13:34:18 -07001537 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001538 // user defined kotlin flags.
1539 kotlincFlags := j.properties.Kotlincflags
1540 CheckKotlincFlags(ctx, kotlincFlags)
1541
Mads Agerad2bfda2020-12-03 12:21:14 +01001542 // Dogfood the JVM_IR backend.
1543 kotlincFlags = append(kotlincFlags, "-Xuse-ir")
1544
Colin Cross93e85952017-08-15 13:34:18 -07001545 // If there are kotlin files, compile them first but pass all the kotlin and java files
1546 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1547 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001548 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001549 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001550 kotlincFlags = append(kotlincFlags, "-no-jdk")
1551 }
1552 if len(kotlincFlags) > 0 {
1553 // optimization.
1554 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1555 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001556 }
1557
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001558 var kotlinSrcFiles android.Paths
1559 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1560 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1561
patricktu242faad2019-09-24 15:41:30 +08001562 // Collect .kt files for AIDEGen
1563 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
Colin Crossa4c8cc62020-06-25 17:13:36 -07001564 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, kotlinCommonSrcFiles.Strings()...)
patricktu242faad2019-09-24 15:41:30 +08001565
Colin Crossafbb1732019-01-17 15:42:52 -08001566 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1567 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1568
1569 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1570 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1571
1572 if len(flags.processorPath) > 0 {
1573 // Use kapt for annotation processing
1574 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
Colin Cross9ca38d22020-06-18 15:46:32 -07001575 kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
Colin Crossa4c8cc62020-06-25 17:13:36 -07001576 kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
Colin Crossafbb1732019-01-17 15:42:52 -08001577 srcJars = append(srcJars, kaptSrcJar)
Colin Cross9ca38d22020-06-18 15:46:32 -07001578 kotlinJars = append(kotlinJars, kaptResJar)
Colin Crossafbb1732019-01-17 15:42:52 -08001579 // Disable annotation processing in javac, it's already been handled by kapt
1580 flags.processorPath = nil
Colin Cross5a116862020-04-22 11:44:34 -07001581 flags.processors = nil
Colin Crossafbb1732019-01-17 15:42:52 -08001582 }
Colin Cross93e85952017-08-15 13:34:18 -07001583
Colin Cross1ee23172017-10-18 14:44:18 -07001584 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Crossa4c8cc62020-06-25 17:13:36 -07001585 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, kotlinCommonSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001586 if ctx.Failed() {
1587 return
1588 }
1589
1590 // Make javac rule depend on the kotlinc rule
1591 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001592
Colin Cross55f63ea2018-08-27 12:37:09 -07001593 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross0b67a8b2020-06-18 15:52:01 -07001594 // Jar kotlin classes into the final jar after javac
1595 if BoolDefault(j.properties.Static_kotlin_stdlib, true) {
1596 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
1597 }
Colin Cross93e85952017-08-15 13:34:18 -07001598 }
1599
Colin Cross55f63ea2018-08-27 12:37:09 -07001600 jars := append(android.Paths(nil), kotlinJars...)
1601
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001602 // Store the list of .java files that was passed to javac
1603 j.compiledJavaSrcs = uniqueSrcFiles
1604 j.compiledSrcJars = srcJars
1605
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001606 enableSharding := false
Colin Crossf7d84012020-02-21 08:16:41 -08001607 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001608 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001609 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001610 enableSharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001611 // Formerly, there was a check here that prevented annotation processors
1612 // from being used when sharding was enabled, as some annotation processors
1613 // do not function correctly in sharded environments. It was removed to
1614 // allow for the use of annotation processors that do function correctly
1615 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001616 }
Colin Crossf7d84012020-02-21 08:16:41 -08001617 headerJarFileWithoutJarjar, j.headerJarFile =
1618 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001619 if ctx.Failed() {
1620 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001621 }
1622 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001623 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001624 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001625 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001626 // If error-prone is enabled, add an additional rule to compile the java files into
1627 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001628 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001629 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1630 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001631 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001632 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001633 extraJarDeps = append(extraJarDeps, errorprone)
1634 }
1635
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001636 if enableSharding {
Colin Crossf7d84012020-02-21 08:16:41 -08001637 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001638 shardSize := int(*(j.properties.Javac_shard_size))
1639 var shardSrcs []android.Paths
1640 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001641 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001642 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001643 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1644 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001645 jars = append(jars, classes)
1646 }
1647 }
1648 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001649 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1650 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001651 jars = append(jars, classes)
1652 }
1653 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001654 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001655 jars = append(jars, classes)
1656 }
Colin Crossd6891432017-09-27 17:39:56 -07001657 if ctx.Failed() {
1658 return
1659 }
Colin Cross2fe66872015-03-30 17:20:39 -07001660 }
1661
Colin Cross0c4ce212019-05-03 15:28:19 -07001662 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1663
1664 var includeSrcJar android.WritablePath
1665 if Bool(j.properties.Include_srcs) {
1666 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1667 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1668 }
1669
Colin Crosscedd4762018-09-13 11:26:19 -07001670 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1671 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001672 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001673 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001674
1675 var resArgs []string
1676 var resDeps android.Paths
1677
1678 resArgs = append(resArgs, dirArgs...)
1679 resDeps = append(resDeps, dirDeps...)
1680
1681 resArgs = append(resArgs, fileArgs...)
1682 resDeps = append(resDeps, fileDeps...)
1683
Colin Cross988708c2019-05-06 14:04:11 -07001684 resArgs = append(resArgs, extraArgs...)
1685 resDeps = append(resDeps, extraDeps...)
1686
Colin Cross40a36712017-09-27 17:41:35 -07001687 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001688 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001689 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001690 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001691 if ctx.Failed() {
1692 return
1693 }
1694 }
1695
Colin Cross0c4ce212019-05-03 15:28:19 -07001696 var resourceJars android.Paths
1697 if j.resourceJar != nil {
1698 resourceJars = append(resourceJars, j.resourceJar)
1699 }
1700 if Bool(j.properties.Include_srcs) {
1701 resourceJars = append(resourceJars, includeSrcJar)
1702 }
1703 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001704
Colin Cross0c4ce212019-05-03 15:28:19 -07001705 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001706 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001707 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001708 false, nil, nil)
1709 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001710 } else if len(resourceJars) == 1 {
1711 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001712 }
1713
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001714 if len(deps.staticJars) > 0 {
1715 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001716 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001717
Colin Cross094054a2018-10-17 15:10:48 -07001718 manifest := j.overrideManifest
1719 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001720 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001721 }
Colin Cross635acc92017-09-12 22:50:46 -07001722
Colin Cross8a497952019-03-05 22:25:09 -08001723 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001724 if len(services) > 0 {
1725 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1726 var zipargs []string
1727 for _, file := range services {
1728 serviceFile := file.String()
1729 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1730 }
Kousik Kumar366afc52020-05-20 11:27:16 -07001731 rule := zip
1732 args := map[string]string{
1733 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
1734 }
Ramy Medhat16f23a42020-09-03 01:29:49 -04001735 if ctx.Config().UseRBE() && ctx.Config().IsEnvTrue("RBE_ZIP") {
Kousik Kumar366afc52020-05-20 11:27:16 -07001736 rule = zipRE
1737 args["implicits"] = strings.Join(services.Strings(), ",")
1738 }
Alex Light7f004a72019-02-21 13:27:37 -08001739 ctx.Build(pctx, android.BuildParams{
Kousik Kumar366afc52020-05-20 11:27:16 -07001740 Rule: rule,
Alex Light7f004a72019-02-21 13:27:37 -08001741 Output: servicesJar,
1742 Implicits: services,
Kousik Kumar366afc52020-05-20 11:27:16 -07001743 Args: args,
Alex Light7f004a72019-02-21 13:27:37 -08001744 })
1745 jars = append(jars, servicesJar)
1746 }
1747
Colin Cross0a6e0072017-08-30 14:24:55 -07001748 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001749 // classes.jar. If there is only one input jar this step will be skipped.
Paul Duffin612e6102021-02-02 13:38:13 +00001750 var outputFile android.OutputPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001751
1752 if len(jars) == 1 && !manifest.Valid() {
Paul Duffin612e6102021-02-02 13:38:13 +00001753 // Optimization: skip the combine step as there is nothing to do
1754 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1755 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1756 // any if len(jars) == 1.
1757
1758 // Transform the single path to the jar into an OutputPath as that is required by the following
1759 // code.
Colin Cross3063b782018-08-15 11:19:12 -07001760 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
Paul Duffin612e6102021-02-02 13:38:13 +00001761 // The path contains an embedded OutputPath so reuse that.
1762 outputFile = moduleOutPath.OutputPath
1763 } else if outputPath, ok := jars[0].(android.OutputPath); ok {
1764 // The path is an OutputPath so reuse it directly.
1765 outputFile = outputPath
Colin Cross3063b782018-08-15 11:19:12 -07001766 } else {
Paul Duffin612e6102021-02-02 13:38:13 +00001767 // The file is not in the out directory so create an OutputPath into which it can be copied
1768 // and which the following code can use to refer to it.
Colin Cross3063b782018-08-15 11:19:12 -07001769 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1770 ctx.Build(pctx, android.BuildParams{
1771 Rule: android.Cp,
1772 Input: jars[0],
1773 Output: combinedJar,
1774 })
Paul Duffin612e6102021-02-02 13:38:13 +00001775 outputFile = combinedJar.OutputPath
Colin Cross3063b782018-08-15 11:19:12 -07001776 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001777 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001778 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001779 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001780 false, nil, nil)
Paul Duffin612e6102021-02-02 13:38:13 +00001781 outputFile = combinedJar.OutputPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001782 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001783
Colin Cross331a1212018-08-15 20:40:52 -07001784 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001785 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001786 // Transform classes.jar into classes-jarjar.jar
Paul Duffin612e6102021-02-02 13:38:13 +00001787 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName).OutputPath
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001788 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001789 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001790
1791 // jarjar resource jar if necessary
1792 if j.resourceJar != nil {
1793 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001794 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001795 j.resourceJar = resourceJarJarFile
1796 }
1797
Colin Cross0a6e0072017-08-30 14:24:55 -07001798 if ctx.Failed() {
1799 return
1800 }
1801 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001802
1803 // Check package restrictions if necessary.
1804 if len(j.properties.Permitted_packages) > 0 {
1805 // Check packages and copy to package-checked file.
1806 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1807 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1808 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1809
1810 if ctx.Failed() {
1811 return
1812 }
1813 }
1814
Nan Zhanged19fc32017-10-19 13:06:22 -07001815 j.implementationJarFile = outputFile
1816 if j.headerJarFile == nil {
1817 j.headerJarFile = j.implementationJarFile
1818 }
Colin Cross2fe66872015-03-30 17:20:39 -07001819
Chris Gross190fdc02020-05-29 16:01:19 +00001820 if j.shouldInstrumentInApex(ctx) {
Jiyong Park00cae1c2020-02-18 12:50:44 +00001821 j.properties.Instrument = true
1822 }
1823
Colin Cross3144dfc2018-01-03 15:06:47 -08001824 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001825 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1826 }
1827
Colin Cross331a1212018-08-15 20:40:52 -07001828 // merge implementation jar with resources if necessary
1829 implementationAndResourcesJar := outputFile
1830 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001831 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Paul Duffin612e6102021-02-02 13:38:13 +00001832 combinedJar := android.PathForModuleOut(ctx, "withres", jarName).OutputPath
Colin Cross08a409d2019-04-29 10:22:44 -07001833 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001834 false, nil, nil)
1835 implementationAndResourcesJar = combinedJar
1836 }
1837
1838 j.implementationAndResourcesJar = implementationAndResourcesJar
1839
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001840 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
Colin Cross56a83212020-09-15 18:30:11 -07001841 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1842 if j.DirectlyInAnyApex() && !apexInfo.IsForPlatform() {
Liz Kammera7a64f32020-07-09 15:16:41 -07001843 if j.dexProperties.Compile_dex == nil {
1844 j.dexProperties.Compile_dex = proptools.BoolPtr(true)
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001845 }
1846 if j.deviceProperties.Hostdex == nil {
1847 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1848 }
1849 }
1850
Colin Crossb014f072021-02-26 14:54:36 -08001851 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.dexProperties.Compile_dex)) {
1852 if j.hasCode(ctx) {
1853 if j.shouldInstrumentStatic(ctx) {
1854 j.dexer.extraProguardFlagFiles = append(j.dexer.extraProguardFlagFiles,
1855 android.PathForSource(ctx, "build/make/core/proguard.jacoco.flags"))
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001856 }
Colin Crossb014f072021-02-26 14:54:36 -08001857 // Dex compilation
1858 var dexOutputFile android.OutputPath
1859 dexOutputFile = j.dexer.compileDex(ctx, flags, j.minSdkVersion(), outputFile, jarName)
1860 if ctx.Failed() {
1861 return
1862 }
1863
1864 // Hidden API CSV generation and dex encoding
1865 dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, j.implementationJarFile,
1866 proptools.Bool(j.dexProperties.Uncompress_dex))
1867
1868 // merge dex jar with resources if necessary
1869 if j.resourceJar != nil {
1870 jars := android.Paths{dexOutputFile, j.resourceJar}
1871 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName).OutputPath
1872 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1873 false, nil, nil)
1874 if *j.dexProperties.Uncompress_dex {
1875 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName).OutputPath
1876 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1877 dexOutputFile = combinedAlignedJar
1878 } else {
1879 dexOutputFile = combinedJar
1880 }
1881 }
1882
1883 j.dexJarFile = dexOutputFile
1884
1885 // Dexpreopting
1886 j.dexpreopt(ctx, dexOutputFile)
1887
1888 outputFile = dexOutputFile
1889 } else {
1890 // There is no code to compile into a dex jar, make sure the resources are propagated
1891 // to the APK if this is an app.
1892 outputFile = implementationAndResourcesJar
1893 j.dexJarFile = j.resourceJar
Colin Cross331a1212018-08-15 20:40:52 -07001894 }
1895
Colin Cross43f08db2018-11-12 10:13:39 -08001896 if ctx.Failed() {
1897 return
1898 }
Colin Cross331a1212018-08-15 20:40:52 -07001899 } else {
1900 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001901 }
Colin Cross331a1212018-08-15 20:40:52 -07001902
Colin Cross014489c2020-06-02 20:09:13 -07001903 if ctx.Device() {
1904 lintSDKVersionString := func(sdkSpec sdkSpec) string {
1905 if v := sdkSpec.version; v.isNumbered() {
1906 return v.String()
1907 } else {
Dan Albert4f378d72020-07-23 17:32:15 -07001908 return ctx.Config().DefaultAppTargetSdk(ctx).String()
Colin Cross014489c2020-06-02 20:09:13 -07001909 }
1910 }
1911
1912 j.linter.name = ctx.ModuleName()
1913 j.linter.srcs = srcFiles
1914 j.linter.srcJars = srcJars
1915 j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...)
1916 j.linter.classes = j.implementationJarFile
1917 j.linter.minSdkVersion = lintSDKVersionString(j.minSdkVersion())
1918 j.linter.targetSdkVersion = lintSDKVersionString(j.targetSdkVersion())
1919 j.linter.compileSdkVersion = lintSDKVersionString(j.sdkVersion())
1920 j.linter.javaLanguageLevel = flags.javaVersion.String()
1921 j.linter.kotlinLanguageLevel = "1.3"
Colin Cross56a83212020-09-15 18:30:11 -07001922 if !apexInfo.IsForPlatform() && ctx.Config().UnbundledBuildApps() {
Colin Cross08dca382020-07-21 20:31:17 -07001923 j.linter.buildModuleReportZip = true
1924 }
Colin Cross014489c2020-06-02 20:09:13 -07001925 j.linter.lint(ctx)
1926 }
1927
Colin Crossb7a63242015-04-16 14:09:14 -07001928 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001929
Colin Crossdcf71b22021-02-01 13:59:03 -08001930 ctx.SetProvider(JavaInfoProvider, JavaInfo{
1931 HeaderJars: android.PathsIfNonNil(j.headerJarFile),
1932 ImplementationAndResourcesJars: android.PathsIfNonNil(j.implementationAndResourcesJar),
1933 ImplementationJars: android.PathsIfNonNil(j.implementationJarFile),
1934 ResourceJars: android.PathsIfNonNil(j.resourceJar),
1935 AidlIncludeDirs: j.exportAidlIncludeDirs,
1936 SrcJarArgs: j.srcJarArgs,
1937 SrcJarDeps: j.srcJarDeps,
1938 ExportedPlugins: j.exportedPluginJars,
1939 ExportedPluginClasses: j.exportedPluginClasses,
1940 ExportedPluginDisableTurbine: j.exportedDisableTurbine,
1941 JacocoReportClassesFile: j.jacocoReportClassesFile,
1942 })
1943
Colin Cross3063b782018-08-15 11:19:12 -07001944 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1945 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001946}
1947
Colin Cross3b706fd2019-09-05 16:44:18 -07001948func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1949 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1950
1951 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1952 if idx >= 0 {
1953 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1954 jarName += strconv.Itoa(idx)
1955 }
1956
Paul Duffin612e6102021-02-02 13:38:13 +00001957 classes := android.PathForModuleOut(ctx, "javac", jarName).OutputPath
Colin Cross3b706fd2019-09-05 16:44:18 -07001958 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1959
1960 if ctx.Config().EmitXrefRules() {
1961 extractionFile := android.PathForModuleOut(ctx, kzipName)
1962 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1963 j.kytheFiles = append(j.kytheFiles, extractionFile)
1964 }
1965
1966 return classes
1967}
1968
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001969// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1970// since some of these flags may be used internally.
1971func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1972 for _, flag := range flags {
1973 flag = strings.TrimSpace(flag)
1974
1975 if !strings.HasPrefix(flag, "-") {
1976 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1977 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1978 ctx.PropertyErrorf("kotlincflags",
1979 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1980 } else if inList(flag, config.KotlincIllegalFlags) {
1981 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1982 } else if flag == "-include-runtime" {
1983 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1984 } else {
1985 args := strings.Split(flag, " ")
1986 if args[0] == "-kotlin-home" {
1987 ctx.PropertyErrorf("kotlincflags",
1988 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1989 }
1990 }
1991 }
1992}
1993
Colin Cross8eadbf02017-10-24 17:46:00 -07001994func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf7d84012020-02-21 08:16:41 -08001995 deps deps, flags javaBuilderFlags, jarName string,
1996 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001997
1998 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001999 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07002000 // Compile java sources into turbine.jar.
2001 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
2002 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
2003 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08002004 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07002005 }
2006 jars = append(jars, turbineJar)
2007 }
2008
Colin Cross55f63ea2018-08-27 12:37:09 -07002009 jars = append(jars, extraJars...)
2010
Nan Zhanged19fc32017-10-19 13:06:22 -07002011 // Combine any static header libraries into classes-header.jar. If there is only
2012 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07002013 jars = append(jars, deps.staticHeaderJars...)
2014
Colin Cross5c6ecc12017-10-23 18:12:27 -07002015 // we cannot skip the combine step for now if there is only one jar
2016 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
2017 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002018 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07002019 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07002020 headerJar = combinedJar
Colin Crossf7d84012020-02-21 08:16:41 -08002021 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07002022
Steven Morelandc4efd9c2019-01-18 11:51:25 -08002023 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07002024 // Transform classes.jar into classes-jarjar.jar
2025 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08002026 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf7d84012020-02-21 08:16:41 -08002027 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07002028 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08002029 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07002030 }
2031 }
2032
Colin Crossf7d84012020-02-21 08:16:41 -08002033 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07002034}
2035
Colin Crosscb933592017-11-22 13:49:43 -08002036func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Paul Duffin612e6102021-02-02 13:38:13 +00002037 classesJar android.Path, jarName string) android.OutputPath {
Colin Crosscb933592017-11-22 13:49:43 -08002038
Colin Cross7a3139e2017-12-19 13:57:50 -08002039 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08002040
Colin Cross84c38822018-01-03 15:59:46 -08002041 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Paul Duffin612e6102021-02-02 13:38:13 +00002042 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName).OutputPath
Colin Crosscb933592017-11-22 13:49:43 -08002043
2044 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
2045
2046 j.jacocoReportClassesFile = jacocoReportClassesFile
2047
2048 return instrumentedJar
2049}
2050
Nan Zhanged19fc32017-10-19 13:06:22 -07002051func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002052 if j.headerJarFile == nil {
2053 return nil
2054 }
Nan Zhanged19fc32017-10-19 13:06:22 -07002055 return android.Paths{j.headerJarFile}
2056}
2057
2058func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002059 if j.implementationJarFile == nil {
2060 return nil
2061 }
Nan Zhanged19fc32017-10-19 13:06:22 -07002062 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002063}
2064
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00002065func (j *Module) DexJarBuildPath() android.Path {
Colin Crossf24a22a2019-01-31 14:12:44 -08002066 return j.dexJarFile
2067}
2068
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01002069func (j *Module) DexJarInstallPath() android.Path {
2070 return j.installFile
2071}
2072
Colin Cross331a1212018-08-15 20:40:52 -07002073func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002074 if j.implementationAndResourcesJar == nil {
2075 return nil
2076 }
Colin Cross331a1212018-08-15 20:40:52 -07002077 return android.Paths{j.implementationAndResourcesJar}
2078}
2079
Colin Cross46c9b8b2017-06-22 16:51:17 -07002080func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002081 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07002082 return j.exportAidlIncludeDirs
2083}
2084
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002085func (j *Module) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
2086 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09002087}
2088
Colin Cross46c9b8b2017-06-22 16:51:17 -07002089var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07002090
Colin Cross46c9b8b2017-06-22 16:51:17 -07002091func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07002092 return j.logtagsSrcs
2093}
2094
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002095// Collect information for opening IDE project files in java/jdeps.go.
2096func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
2097 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
2098 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08002099 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002100 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08002101 if j.expandJarjarRules != nil {
2102 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002103 }
bralee1fbf4402020-05-21 10:11:59 +08002104 dpInfo.Paths = append(dpInfo.Paths, j.modulePaths...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002105}
2106
2107func (j *Module) CompilerDeps() []string {
2108 jdeps := []string{}
2109 jdeps = append(jdeps, j.properties.Libs...)
2110 jdeps = append(jdeps, j.properties.Static_libs...)
2111 return jdeps
2112}
2113
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002114func (j *Module) hasCode(ctx android.ModuleContext) bool {
2115 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
2116 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
2117}
2118
Jiyong Park45bf82e2020-12-15 22:29:02 +09002119// Implements android.ApexModule
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09002120func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01002121 return j.depIsInSameApex(ctx, dep)
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09002122}
2123
Jiyong Park45bf82e2020-12-15 22:29:02 +09002124// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07002125func (j *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
2126 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09002127 sdkSpec := j.minSdkVersion()
2128 if !sdkSpec.specified() {
2129 return fmt.Errorf("min_sdk_version is not specified")
2130 }
2131 if sdkSpec.kind == sdkCore {
2132 return nil
2133 }
2134 ver, err := sdkSpec.effectiveVersion(ctx)
2135 if err != nil {
2136 return err
2137 }
Dan Albertc8060532020-07-22 22:32:17 -07002138 if ver.ApiLevel(ctx).GreaterThan(sdkVersion) {
Jooyung Han749dc692020-04-15 11:03:39 +09002139 return fmt.Errorf("newer SDK(%v)", ver)
2140 }
2141 return nil
2142}
2143
Jiyong Park0b238752019-10-29 11:23:10 +09002144func (j *Module) Stem() string {
2145 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
2146}
2147
Paul Duffin4103e922021-02-01 19:01:34 +00002148// ConfigurationName returns the name of the module as used in build configuration.
2149//
2150// This is usually the same as BaseModuleName() except for the <x>.impl libraries created by
2151// java_sdk_library in which case this is the BaseModuleName() without the ".impl" suffix,
2152// i.e. just <x>.
Paul Duffina2058f82020-06-24 16:22:38 +01002153func (j *Module) ConfigurationName() string {
2154 return proptools.StringDefault(j.deviceProperties.ConfigurationName, j.BaseModuleName())
2155}
2156
Jiyong Park618922e2020-01-08 13:35:43 +09002157func (j *Module) JacocoReportClassesFile() android.Path {
2158 return j.jacocoReportClassesFile
2159}
2160
Martin Stjernholm6d415272020-01-31 17:10:36 +00002161func (j *Module) IsInstallable() bool {
2162 return Bool(j.properties.Installable)
2163}
2164
Colin Cross2fe66872015-03-30 17:20:39 -07002165//
2166// Java libraries (.jar file)
2167//
2168
Colin Crossf506d872017-07-19 15:53:04 -07002169type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07002170 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07002171
2172 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07002173}
2174
Jiyong Park45bf82e2020-12-15 22:29:02 +09002175var _ android.ApexModule = (*Library)(nil)
2176
Paul Duffine739f1e2020-05-29 11:24:51 +01002177// Provides access to the list of permitted packages from updatable boot jars.
2178type PermittedPackagesForUpdatableBootJars interface {
2179 PermittedPackagesForUpdatableBootJars() []string
2180}
2181
2182var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
2183
2184func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
2185 return j.properties.Permitted_packages
2186}
2187
Colin Cross42be7612019-02-21 18:12:14 -08002188func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00002189 // Store uncompressed (and aligned) any dex files from jars in APEXes.
Colin Cross56a83212020-09-15 18:30:11 -07002190 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00002191 return true
2192 }
2193
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00002194 // Store uncompressed (and do not strip) dex files from boot class path jars.
2195 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
2196 return true
2197 }
2198
2199 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08002200 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00002201 return true
2202 }
Colin Cross083a2aa2019-02-06 16:37:12 -08002203 if ctx.Config().UncompressPrivAppDex() &&
2204 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
2205 return true
2206 }
2207
Colin Cross2fc72f62018-12-21 12:59:54 -08002208 return false
2209}
2210
Colin Crossf506d872017-07-19 15:53:04 -07002211func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin4103e922021-02-01 19:01:34 +00002212 // Initialize the hiddenapi structure. Pass in the configuration name rather than the module name
2213 // so the hidden api will encode the <x>.impl java_ library created by java_sdk_library just as it
2214 // would the <x> library if <x> was configured as a boot jar.
2215 j.initHiddenAPI(ctx, j.ConfigurationName())
2216
Colin Cross56a83212020-09-15 18:30:11 -07002217 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2218 if !apexInfo.IsForPlatform() {
2219 j.hideApexVariantFromMake = true
2220 }
2221
Artur Satayev2db1c3f2020-04-08 19:09:30 +01002222 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09002223 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08002224 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Liz Kammera7a64f32020-07-09 15:16:41 -07002225 if j.dexProperties.Uncompress_dex == nil {
David Srbeckye033cba2020-05-20 22:20:28 +01002226 // If the value was not force-set by the user, use reasonable default based on the module.
Liz Kammera7a64f32020-07-09 15:16:41 -07002227 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
David Srbeckye033cba2020-05-20 22:20:28 +01002228 }
Liz Kammera7a64f32020-07-09 15:16:41 -07002229 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002230 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07002231 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07002232
bralee1fbf4402020-05-21 10:11:59 +08002233 // Collect the module directory for IDE info in java/jdeps.go.
2234 j.modulePaths = append(j.modulePaths, ctx.ModuleDir())
2235
Colin Cross56a83212020-09-15 18:30:11 -07002236 exclusivelyForApex := !apexInfo.IsForPlatform()
Jiyong Park7f7766d2019-07-25 22:02:35 +09002237 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07002238 var extraInstallDeps android.Paths
2239 if j.InstallMixin != nil {
2240 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
2241 }
Colin Cross2c429dc2017-08-31 16:45:16 -07002242 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parka62aa232020-05-28 23:46:55 +09002243 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07002244 }
Colin Crossb7a63242015-04-16 14:09:14 -07002245}
2246
Colin Crossf506d872017-07-19 15:53:04 -07002247func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07002248 j.deps(ctx)
2249}
2250
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002251const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002252 aidlIncludeDir = "aidl"
2253 javaDir = "java"
2254 jarFileSuffix = ".jar"
2255 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002256)
2257
Paul Duffina0dbf432019-12-05 11:25:53 +00002258// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +00002259func sdkSnapshotFilePathForJar(osPrefix, name string) string {
2260 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002261}
2262
Paul Duffina04c1072020-03-02 10:16:35 +00002263func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
2264 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002265}
2266
Paul Duffin13879572019-11-28 14:31:38 +00002267type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00002268 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00002269
2270 // Function to retrieve the appropriate output jar (implementation or header) from
2271 // the library.
Paul Duffindb170e42020-12-08 17:48:25 +00002272 jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path
2273
2274 // Function to compute the snapshot relative path to which the named library's
2275 // jar should be copied.
2276 snapshotPathGetter func(osPrefix, name string) string
2277
2278 // True if only the jar should be copied to the snapshot, false if the jar plus any additional
2279 // files like aidl files should also be copied.
2280 onlyCopyJarToSnapshot bool
Paul Duffin13879572019-11-28 14:31:38 +00002281}
2282
Paul Duffindb170e42020-12-08 17:48:25 +00002283const (
2284 onlyCopyJarToSnapshot = true
2285 copyEverythingToSnapshot = false
2286)
2287
Paul Duffin13879572019-11-28 14:31:38 +00002288func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2289 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2290}
2291
2292func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
2293 _, ok := module.(*Library)
2294 return ok
2295}
2296
Paul Duffin3a4eb502020-03-19 16:11:18 +00002297func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2298 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00002299}
Paul Duffina0dbf432019-12-05 11:25:53 +00002300
Paul Duffin14eb4672020-03-02 11:33:02 +00002301func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +00002302 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +00002303}
2304
2305type librarySdkMemberProperties struct {
2306 android.SdkMemberPropertiesBase
2307
Paul Duffin864e1b42020-05-06 10:23:19 +01002308 JarToExport android.Path `android:"arch_variant"`
Paul Duffina551a1c2020-03-17 21:04:24 +00002309 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +00002310}
2311
Paul Duffin3a4eb502020-03-19 16:11:18 +00002312func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +00002313 j := variant.(*Library)
2314
Paul Duffindb170e42020-12-08 17:48:25 +00002315 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j)
2316
Paul Duffina551a1c2020-03-17 21:04:24 +00002317 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +00002318}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002319
Paul Duffin3a4eb502020-03-19 16:11:18 +00002320func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00002321 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00002322
Paul Duffindb170e42020-12-08 17:48:25 +00002323 memberType := ctx.MemberType().(*librarySdkMemberType)
2324
Paul Duffina551a1c2020-03-17 21:04:24 +00002325 exportedJar := p.JarToExport
2326 if exportedJar != nil {
Paul Duffindb170e42020-12-08 17:48:25 +00002327 // Delegate the creation of the snapshot relative path to the member type.
2328 snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(p.OsPrefix(), ctx.Name())
2329
2330 // Copy the exported jar to the snapshot.
Paul Duffin14eb4672020-03-02 11:33:02 +00002331 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
2332
Paul Duffina551a1c2020-03-17 21:04:24 +00002333 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2334 }
2335
Paul Duffindb170e42020-12-08 17:48:25 +00002336 // Do not copy anything else to the snapshot.
2337 if memberType.onlyCopyJarToSnapshot {
2338 return
2339 }
2340
Paul Duffina551a1c2020-03-17 21:04:24 +00002341 aidlIncludeDirs := p.AidlIncludeDirs
2342 if len(aidlIncludeDirs) != 0 {
2343 sdkModuleContext := ctx.SdkModuleContext()
2344 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +00002345 // TODO(jiyong): copy parcelable declarations only
2346 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
2347 for _, file := range aidlFiles {
2348 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
2349 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002350 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002351
Paul Duffina551a1c2020-03-17 21:04:24 +00002352 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +00002353 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002354}
2355
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00002356var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
2357 android.SdkMemberTypeBase{
2358 PropertyName: "java_header_libs",
2359 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +00002360 },
Paul Duffindb170e42020-12-08 17:48:25 +00002361 func(_ android.SdkMemberContext, j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +00002362 headerJars := j.HeaderJars()
2363 if len(headerJars) != 1 {
2364 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
2365 }
2366
2367 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00002368 },
Paul Duffindb170e42020-12-08 17:48:25 +00002369 sdkSnapshotFilePathForJar,
2370 copyEverythingToSnapshot,
Paul Duffina0dbf432019-12-05 11:25:53 +00002371}
2372
Colin Cross1b16b0e2019-02-12 14:41:32 -08002373// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
2374//
2375// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
2376// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
2377// as a `static_libs` dependency of another module.
2378//
2379// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
2380// a device.
2381//
2382// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2383// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07002384func LibraryFactory() android.Module {
2385 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07002386
Colin Crossce6734e2020-06-15 16:09:53 -07002387 module.addHostAndDeviceProperties()
Colin Cross2fe66872015-03-30 17:20:39 -07002388
Paul Duffin859fe962020-05-15 10:20:31 +01002389 module.initModuleAndImport(&module.ModuleBase)
2390
Jiyong Park7f7766d2019-07-25 22:02:35 +09002391 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002392 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002393 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07002394 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002395}
2396
Colin Cross1b16b0e2019-02-12 14:41:32 -08002397// java_library_static is an obsolete alias for java_library.
2398func LibraryStaticFactory() android.Module {
2399 return LibraryFactory()
2400}
2401
2402// java_library_host builds and links sources into a `.jar` file for the host.
2403//
2404// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
2405// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002406func LibraryHostFactory() android.Module {
2407 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07002408
Colin Crossce6734e2020-06-15 16:09:53 -07002409 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -07002410
Colin Cross9ae1b922018-06-26 17:59:05 -07002411 module.Module.properties.Installable = proptools.BoolPtr(true)
2412
Jiyong Park7f7766d2019-07-25 22:02:35 +09002413 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002414 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002415 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002416}
2417
2418//
Colin Crossb628ea52018-08-14 16:42:33 -07002419// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002420//
2421
Dan Shi95d19422020-08-15 12:24:26 -07002422// Test option struct.
2423type TestOptions struct {
2424 // a list of extra test configuration files that should be installed with the module.
2425 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -08002426
2427 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
2428 Unit_test *bool
Dan Shi95d19422020-08-15 12:24:26 -07002429}
2430
Colin Cross05638fc2018-04-09 18:40:24 -07002431type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002432 // list of compatibility suites (for example "cts", "vts") that the module should be
2433 // installed into.
2434 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002435
2436 // the name of the test configuration (for example "AndroidTest.xml") that should be
2437 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002438 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002439
Jack He33338892018-09-19 02:21:28 -07002440 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2441 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002442 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002443
Colin Crossd96ca352018-08-10 16:06:24 -07002444 // list of files or filegroup modules that provide data that should be installed alongside
2445 // the test
Jiyong Park2b0e4902021-02-16 06:52:39 +09002446 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002447
2448 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2449 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2450 // explicitly.
2451 Auto_gen_config *bool
easoncylee5bcff5d2020-04-30 14:57:06 +08002452
2453 // Add parameterized mainline modules to auto generated test config. The options will be
2454 // handled by TradeFed to do downloading and installing the specified modules on the device.
2455 Test_mainline_modules []string
Dan Shi95d19422020-08-15 12:24:26 -07002456
2457 // Test options.
2458 Test_options TestOptions
Colin Cross05638fc2018-04-09 18:40:24 -07002459}
2460
Liz Kammerdd849a82020-06-12 16:38:45 -07002461type hostTestProperties struct {
2462 // list of native binary modules that should be installed alongside the test
2463 Data_native_bins []string `android:"arch_variant"`
2464}
2465
Paul Duffin42df1442019-03-20 12:45:53 +00002466type testHelperLibraryProperties struct {
2467 // list of compatibility suites (for example "cts", "vts") that the module should be
2468 // installed into.
2469 Test_suites []string `android:"arch_variant"`
2470}
2471
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002472type prebuiltTestProperties struct {
2473 // list of compatibility suites (for example "cts", "vts") that the module should be
2474 // installed into.
2475 Test_suites []string `android:"arch_variant"`
2476
2477 // the name of the test configuration (for example "AndroidTest.xml") that should be
2478 // installed with the module.
2479 Test_config *string `android:"path,arch_variant"`
2480}
2481
Colin Cross05638fc2018-04-09 18:40:24 -07002482type Test struct {
2483 Library
2484
2485 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002486
Dan Shi95d19422020-08-15 12:24:26 -07002487 testConfig android.Path
2488 extraTestConfigs android.Paths
2489 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002490}
2491
Liz Kammerdd849a82020-06-12 16:38:45 -07002492type TestHost struct {
2493 Test
2494
2495 testHostProperties hostTestProperties
2496}
2497
Paul Duffin42df1442019-03-20 12:45:53 +00002498type TestHelperLibrary struct {
2499 Library
2500
2501 testHelperLibraryProperties testHelperLibraryProperties
2502}
2503
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002504type JavaTestImport struct {
2505 Import
2506
2507 prebuiltTestProperties prebuiltTestProperties
2508
2509 testConfig android.Path
Liz Kammerd6c31d22020-08-05 15:40:41 -07002510 dexJarFile android.Path
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002511}
2512
Liz Kammerdd849a82020-06-12 16:38:45 -07002513func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) {
2514 if len(j.testHostProperties.Data_native_bins) > 0 {
2515 for _, target := range ctx.MultiTargets() {
2516 ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...)
2517 }
2518 }
2519
2520 j.deps(ctx)
2521}
2522
Yuexi Ma627263f2021-03-04 13:47:56 -08002523func (j *TestHost) AddExtraResource(p android.Path) {
2524 j.extraResources = append(j.extraResources, p)
2525}
2526
Colin Cross303e21f2018-08-07 16:49:25 -07002527func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Julien Desprezb2166612021-03-05 18:08:36 +00002528 if j.testProperties.Test_options.Unit_test == nil && ctx.Host() {
2529 // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding.
Julien Desprez3f4e7a12021-03-09 13:02:29 -08002530 defaultUnitTest := !inList("tradefed", j.properties.Static_libs) && !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites)
Julien Desprezb2166612021-03-05 18:08:36 +00002531 j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest)
2532 }
Dan Shi6ffaaa82019-09-26 11:41:36 -07002533 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
Julien Desprez70898c42020-11-19 09:43:45 -08002534 j.testProperties.Test_suites, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test)
Liz Kammerdd849a82020-06-12 16:38:45 -07002535
Colin Cross8a497952019-03-05 22:25:09 -08002536 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002537
Dan Shi95d19422020-08-15 12:24:26 -07002538 j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs)
2539
Liz Kammerdd849a82020-06-12 16:38:45 -07002540 ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) {
2541 j.data = append(j.data, android.OutputFileForModule(ctx, dep, ""))
2542 })
2543
Colin Cross303e21f2018-08-07 16:49:25 -07002544 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002545}
2546
Paul Duffin42df1442019-03-20 12:45:53 +00002547func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2548 j.Library.GenerateAndroidBuildActions(ctx)
2549}
2550
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002551func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2552 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
Julien Desprez70898c42020-11-19 09:43:45 -08002553 j.prebuiltTestProperties.Test_suites, nil, nil)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002554
2555 j.Import.GenerateAndroidBuildActions(ctx)
2556}
2557
2558type testSdkMemberType struct {
2559 android.SdkMemberTypeBase
2560}
2561
2562func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2563 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2564}
2565
2566func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2567 _, ok := module.(*Test)
2568 return ok
2569}
2570
Paul Duffin3a4eb502020-03-19 16:11:18 +00002571func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2572 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00002573}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002574
Paul Duffin14eb4672020-03-02 11:33:02 +00002575func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2576 return &testSdkMemberProperties{}
2577}
2578
2579type testSdkMemberProperties struct {
2580 android.SdkMemberPropertiesBase
2581
Paul Duffina551a1c2020-03-17 21:04:24 +00002582 JarToExport android.Path
2583 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00002584}
2585
Paul Duffin3a4eb502020-03-19 16:11:18 +00002586func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00002587 test := variant.(*Test)
2588
2589 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002590 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00002591 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002592 }
2593
Paul Duffina551a1c2020-03-17 21:04:24 +00002594 p.JarToExport = implementationJars[0]
2595 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00002596}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002597
Paul Duffin3a4eb502020-03-19 16:11:18 +00002598func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00002599 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00002600
Paul Duffina551a1c2020-03-17 21:04:24 +00002601 exportedJar := p.JarToExport
2602 if exportedJar != nil {
2603 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
2604 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002605
2606 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00002607 }
2608
2609 testConfig := p.TestConfig
2610 if testConfig != nil {
2611 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
2612 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002613 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
2614 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002615}
2616
Colin Cross1b16b0e2019-02-12 14:41:32 -08002617// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2618// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2619//
2620// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2621// compiled against the device bootclasspath.
2622//
2623// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2624// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002625func TestFactory() android.Module {
2626 module := &Test{}
2627
Colin Crossce6734e2020-06-15 16:09:53 -07002628 module.addHostAndDeviceProperties()
2629 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07002630
Colin Cross9ae1b922018-06-26 17:59:05 -07002631 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002632 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07002633 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002634
Colin Cross05638fc2018-04-09 18:40:24 -07002635 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002636 return module
2637}
2638
Paul Duffin42df1442019-03-20 12:45:53 +00002639// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2640func TestHelperLibraryFactory() android.Module {
2641 module := &TestHelperLibrary{}
2642
Colin Crossce6734e2020-06-15 16:09:53 -07002643 module.addHostAndDeviceProperties()
2644 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00002645
Colin Cross9a4abed2019-04-24 13:19:28 -07002646 module.Module.properties.Installable = proptools.BoolPtr(true)
2647 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -07002648 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07002649
Paul Duffin42df1442019-03-20 12:45:53 +00002650 InitJavaModule(module, android.HostAndDeviceSupported)
2651 return module
2652}
2653
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002654// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2655// and makes sure that it is added to the appropriate test suite.
2656//
2657// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2658// compiled against an Android classpath.
2659//
2660// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2661// for host modules.
2662func JavaTestImportFactory() android.Module {
2663 module := &JavaTestImport{}
2664
2665 module.AddProperties(
2666 &module.Import.properties,
2667 &module.prebuiltTestProperties)
2668
2669 module.Import.properties.Installable = proptools.BoolPtr(true)
2670
2671 android.InitPrebuiltModule(module, &module.properties.Jars)
2672 android.InitApexModule(module)
2673 android.InitSdkAwareModule(module)
2674 InitJavaModule(module, android.HostAndDeviceSupported)
2675 return module
2676}
2677
Colin Cross1b16b0e2019-02-12 14:41:32 -08002678// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2679// allow running the test with `atest` or a `TEST_MAPPING` file.
2680//
2681// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2682// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002683func TestHostFactory() android.Module {
Liz Kammerdd849a82020-06-12 16:38:45 -07002684 module := &TestHost{}
Colin Cross05638fc2018-04-09 18:40:24 -07002685
Colin Crossce6734e2020-06-15 16:09:53 -07002686 module.addHostProperties()
2687 module.AddProperties(&module.testProperties)
Liz Kammerdd849a82020-06-12 16:38:45 -07002688 module.AddProperties(&module.testHostProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07002689
Yuexi Ma627263f2021-03-04 13:47:56 -08002690 InitTestHost(
2691 module,
2692 proptools.BoolPtr(true),
2693 nil,
2694 nil)
Colin Cross9ae1b922018-06-26 17:59:05 -07002695
Liz Kammerdd849a82020-06-12 16:38:45 -07002696 InitJavaModuleMultiTargets(module, android.HostSupported)
Julien Desprezb2166612021-03-05 18:08:36 +00002697
Colin Cross05638fc2018-04-09 18:40:24 -07002698 return module
2699}
2700
Yuexi Ma627263f2021-03-04 13:47:56 -08002701func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) {
2702 th.properties.Installable = installable
2703 th.testProperties.Auto_gen_config = autoGenConfig
2704 th.testProperties.Test_suites = testSuites
2705}
2706
Colin Cross05638fc2018-04-09 18:40:24 -07002707//
Colin Cross2fe66872015-03-30 17:20:39 -07002708// Java Binaries (.jar file plus wrapper script)
2709//
2710
Colin Crossf506d872017-07-19 15:53:04 -07002711type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002712 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002713 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002714
2715 // Name of the class containing main to be inserted into the manifest as Main-Class.
2716 Main_class *string
Colin Cross89226d92020-10-09 19:00:54 -07002717
2718 // Names of modules containing JNI libraries that should be installed alongside the host
2719 // variant of the binary.
2720 Jni_libs []string
Colin Cross7d5136f2015-05-11 13:39:40 -07002721}
2722
Colin Crossf506d872017-07-19 15:53:04 -07002723type Binary struct {
2724 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002725
Colin Crossf506d872017-07-19 15:53:04 -07002726 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002727
Colin Cross6b4a32d2017-12-05 13:42:45 -08002728 isWrapperVariant bool
2729
Colin Crossc3315992017-12-08 19:12:36 -08002730 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002731 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002732}
2733
Alex Light24237172017-10-26 09:46:21 -07002734func (j *Binary) HostToolPath() android.OptionalPath {
2735 return android.OptionalPathForPath(j.binaryFile)
2736}
2737
Colin Crossf506d872017-07-19 15:53:04 -07002738func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002739 if ctx.Arch().ArchType == android.Common {
2740 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002741 if j.binaryProperties.Main_class != nil {
2742 if j.properties.Manifest != nil {
2743 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2744 }
2745 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2746 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2747 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2748 }
2749
Colin Cross6b4a32d2017-12-05 13:42:45 -08002750 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002751 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002752 // Handle the binary wrapper
2753 j.isWrapperVariant = true
2754
Colin Cross366938f2017-12-11 16:29:02 -08002755 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002756 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002757 } else {
2758 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2759 }
2760
Colin Crossc179ea62020-10-09 10:54:15 -07002761 // The host installation rules make the installed wrapper depend on all the dependencies
Colin Cross89226d92020-10-09 19:00:54 -07002762 // of the wrapper variant, which will include the common variant's jar file and any JNI
2763 // libraries. This is verified by TestBinary.
Colin Cross6b4a32d2017-12-05 13:42:45 -08002764 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
Colin Crossc179ea62020-10-09 10:54:15 -07002765 ctx.ModuleName(), j.wrapperFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002766 }
Colin Cross2fe66872015-03-30 17:20:39 -07002767}
2768
Colin Crossf506d872017-07-19 15:53:04 -07002769func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -05002770 if ctx.Arch().ArchType == android.Common || ctx.BazelConversionMode() {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002771 j.deps(ctx)
Liz Kammer356f7d42021-01-26 09:18:53 -05002772 }
2773 if ctx.Arch().ArchType != android.Common || ctx.BazelConversionMode() {
Colin Crosse9fe2942020-11-10 18:12:15 -08002774 // These dependencies ensure the host installation rules will install the jar file and
2775 // the jni libraries when the wrapper is installed.
2776 ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
2777 ctx.AddVariationDependencies(
2778 []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
2779 binaryInstallTag, ctx.ModuleName())
Colin Cross6b4a32d2017-12-05 13:42:45 -08002780 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002781}
2782
Colin Cross1b16b0e2019-02-12 14:41:32 -08002783// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2784// as well.
2785//
2786// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2787// compiled against the device bootclasspath.
2788//
2789// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2790// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002791func BinaryFactory() android.Module {
2792 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002793
Colin Crossce6734e2020-06-15 16:09:53 -07002794 module.addHostAndDeviceProperties()
2795 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002796
Colin Cross9ae1b922018-06-26 17:59:05 -07002797 module.Module.properties.Installable = proptools.BoolPtr(true)
2798
Colin Cross6b4a32d2017-12-05 13:42:45 -08002799 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2800 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002801 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002802}
2803
Colin Cross1b16b0e2019-02-12 14:41:32 -08002804// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2805//
2806// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2807// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002808func BinaryHostFactory() android.Module {
2809 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002810
Colin Crossce6734e2020-06-15 16:09:53 -07002811 module.addHostProperties()
2812 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002813
Colin Cross9ae1b922018-06-26 17:59:05 -07002814 module.Module.properties.Installable = proptools.BoolPtr(true)
2815
Colin Cross6b4a32d2017-12-05 13:42:45 -08002816 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2817 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002818 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002819}
2820
2821//
2822// Java prebuilts
2823//
2824
Colin Cross74d73e22017-08-02 11:05:49 -07002825type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00002826 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002827
Nan Zhangea568a42017-11-08 21:20:04 -08002828 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002829
2830 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002831
2832 // List of shared java libs that this module has dependencies to
2833 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002834
2835 // List of files to remove from the jar file(s)
2836 Exclude_files []string
2837
2838 // List of directories to remove from the jar file(s)
2839 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002840
2841 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002842 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002843
2844 // set the name of the output
2845 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09002846
2847 Aidl struct {
2848 // directories that should be added as include directories for any aidl sources of modules
2849 // that depend on this module, as well as to aidl for this module.
2850 Export_include_dirs []string
2851 }
Colin Cross74d73e22017-08-02 11:05:49 -07002852}
2853
2854type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002855 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002856 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002857 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002858 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002859 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002860
Paul Duffin0d3c2e12020-05-17 08:34:50 +01002861 // Functionality common to Module and Import.
2862 embeddableInModuleAndImport
2863
Liz Kammerd6c31d22020-08-05 15:40:41 -07002864 hiddenAPI
2865 dexer
Bill Peckhamff89ffa2020-12-23 16:13:04 -08002866 dexpreopter
Liz Kammerd6c31d22020-08-05 15:40:41 -07002867
Colin Cross74d73e22017-08-02 11:05:49 -07002868 properties ImportProperties
2869
Liz Kammerd6c31d22020-08-05 15:40:41 -07002870 // output file containing classes.dex and resources
2871 dexJarFile android.Path
2872
Colin Cross0a6e0072017-08-30 14:24:55 -07002873 combinedClasspathFile android.Path
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002874 classLoaderContexts dexpreopt.ClassLoaderContextMap
Jiyong Park19604de2020-03-24 16:44:11 +09002875 exportAidlIncludeDirs android.Paths
Colin Cross56a83212020-09-15 18:30:11 -07002876
2877 hideApexVariantFromMake bool
Colin Cross2fe66872015-03-30 17:20:39 -07002878}
2879
Jiyong Park6a927c42020-01-21 02:03:43 +09002880func (j *Import) sdkVersion() sdkSpec {
2881 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002882}
2883
Liz Kammer2d2fd852020-08-12 14:42:30 -07002884func (j *Import) makeSdkVersion() string {
2885 return j.sdkVersion().raw
2886}
2887
Liz Kammerd6c31d22020-08-05 15:40:41 -07002888func (j *Import) systemModules() string {
2889 return "none"
2890}
2891
Jiyong Park6a927c42020-01-21 02:03:43 +09002892func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002893 return j.sdkVersion()
2894}
2895
Liz Kammerd6c31d22020-08-05 15:40:41 -07002896func (j *Import) targetSdkVersion() sdkSpec {
2897 return j.sdkVersion()
2898}
2899
Artur Satayev480e25b2020-04-27 18:53:18 +01002900func (j *Import) MinSdkVersion() string {
2901 return j.minSdkVersion().version.String()
2902}
2903
Colin Cross74d73e22017-08-02 11:05:49 -07002904func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002905 return &j.prebuilt
2906}
2907
Colin Cross74d73e22017-08-02 11:05:49 -07002908func (j *Import) PrebuiltSrcs() []string {
2909 return j.properties.Jars
2910}
2911
2912func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002913 return j.prebuilt.Name(j.ModuleBase.Name())
2914}
2915
Jiyong Park0b238752019-10-29 11:23:10 +09002916func (j *Import) Stem() string {
2917 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2918}
2919
Jiyong Park618922e2020-01-08 13:35:43 +09002920func (a *Import) JacocoReportClassesFile() android.Path {
2921 return nil
2922}
2923
Bill Peckhama41a6962021-01-11 10:58:54 -08002924func (j *Import) LintDepSets() LintDepSets {
2925 return LintDepSets{}
2926}
2927
Colin Cross74d73e22017-08-02 11:05:49 -07002928func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002929 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002930
2931 if ctx.Device() && Bool(j.dexProperties.Compile_dex) {
2932 sdkDeps(ctx, sdkContext(j), j.dexer)
2933 }
Colin Cross1e676be2016-10-12 14:38:15 -07002934}
2935
Colin Cross74d73e22017-08-02 11:05:49 -07002936func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin4103e922021-02-01 19:01:34 +00002937 // Initialize the hiddenapi structure.
2938 j.initHiddenAPI(ctx, j.BaseModuleName())
2939
Colin Cross56a83212020-09-15 18:30:11 -07002940 if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() {
2941 j.hideApexVariantFromMake = true
2942 }
2943
Colin Cross8a497952019-03-05 22:25:09 -08002944 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002945
Jiyong Park0b238752019-10-29 11:23:10 +09002946 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002947 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002948 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2949 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002950 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002951 inputFile := outputFile
2952 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2953 TransformJetifier(ctx, outputFile, inputFile)
2954 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002955 j.combinedClasspathFile = outputFile
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01002956 j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap)
Paul Duffin859fe962020-05-15 10:20:31 +01002957
Liz Kammerd6c31d22020-08-05 15:40:41 -07002958 var flags javaBuilderFlags
Paul Duffin064b70c2020-11-02 17:32:38 +00002959 var deapexerModule android.Module
Liz Kammerd6c31d22020-08-05 15:40:41 -07002960
Jiyong Park1be96912018-05-28 18:02:19 +09002961 ctx.VisitDirectDeps(func(module android.Module) {
Jiyong Park1be96912018-05-28 18:02:19 +09002962 tag := ctx.OtherModuleDependencyTag(module)
2963
Colin Crossdcf71b22021-02-01 13:59:03 -08002964 if ctx.OtherModuleHasProvider(module, JavaInfoProvider) {
2965 dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo)
Jiyong Park1be96912018-05-28 18:02:19 +09002966 switch tag {
2967 case libTag, staticLibTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08002968 flags.classpath = append(flags.classpath, dep.HeaderJars...)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002969 case bootClasspathTag:
Colin Crossdcf71b22021-02-01 13:59:03 -08002970 flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...)
Jiyong Park1be96912018-05-28 18:02:19 +09002971 }
Colin Crossdcf71b22021-02-01 13:59:03 -08002972 } else if dep, ok := module.(SdkLibraryDependency); ok {
Jiyong Park1be96912018-05-28 18:02:19 +09002973 switch tag {
2974 case libTag:
Liz Kammerd6c31d22020-08-05 15:40:41 -07002975 flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
Jiyong Park1be96912018-05-28 18:02:19 +09002976 }
2977 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00002978
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00002979 addCLCFromDep(ctx, module, j.classLoaderContexts)
Paul Duffin064b70c2020-11-02 17:32:38 +00002980
2981 // Save away the `deapexer` module on which this depends, if any.
2982 if tag == android.DeapexerTag {
2983 deapexerModule = module
2984 }
Jiyong Park1be96912018-05-28 18:02:19 +09002985 })
2986
Nan Zhang4973ecf2018-08-10 13:42:12 -07002987 if Bool(j.properties.Installable) {
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00002988 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002989 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002990 }
Jiyong Park19604de2020-03-24 16:44:11 +09002991
2992 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Liz Kammerd6c31d22020-08-05 15:40:41 -07002993
Paul Duffin064b70c2020-11-02 17:32:38 +00002994 if ctx.Device() {
2995 // If this is a variant created for a prebuilt_apex then use the dex implementation jar
2996 // obtained from the associated deapexer module.
2997 ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
2998 if ai.ForPrebuiltApex {
2999 if deapexerModule == nil {
3000 // This should never happen as a variant for a prebuilt_apex is only created if the
3001 // deapxer module has been configured to export the dex implementation jar for this module.
3002 ctx.ModuleErrorf("internal error: module %q does not depend on a `deapexer` module for prebuilt_apex %q",
3003 j.Name(), ai.ApexVariationName)
3004 }
3005
3006 // Get the path of the dex implementation jar from the `deapexer` module.
3007 di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
Paul Duffin9d67ca62021-02-03 20:06:33 +00003008 if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
3009 j.dexJarFile = dexOutputPath
Paul Duffinf75e5272021-02-09 14:34:25 +00003010 j.hiddenAPIExtractInformation(ctx, dexOutputPath, outputFile)
Paul Duffin9d67ca62021-02-03 20:06:33 +00003011 } else {
Paul Duffin064b70c2020-11-02 17:32:38 +00003012 // This should never happen as a variant for a prebuilt_apex is only created if the
3013 // prebuilt_apex has been configured to export the java library dex file.
3014 ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt_apex %q", deapexerModule.Name())
3015 }
3016 } else if Bool(j.dexProperties.Compile_dex) {
3017 sdkDep := decodeSdkDep(ctx, sdkContext(j))
3018 if sdkDep.invalidVersion {
3019 ctx.AddMissingDependencies(sdkDep.bootclasspath)
3020 ctx.AddMissingDependencies(sdkDep.java9Classpath)
3021 } else if sdkDep.useFiles {
3022 // sdkDep.jar is actually equivalent to turbine header.jar.
3023 flags.classpath = append(flags.classpath, sdkDep.jars...)
3024 }
3025
3026 // Dex compilation
3027
3028 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", jarName)
3029 if j.dexProperties.Uncompress_dex == nil {
3030 // If the value was not force-set by the user, use reasonable default based on the module.
3031 j.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
3032 }
3033 j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex
3034
Paul Duffin612e6102021-02-02 13:38:13 +00003035 var dexOutputFile android.OutputPath
Paul Duffin064b70c2020-11-02 17:32:38 +00003036 dexOutputFile = j.dexer.compileDex(ctx, flags, j.minSdkVersion(), outputFile, jarName)
3037 if ctx.Failed() {
3038 return
3039 }
3040
Paul Duffin064b70c2020-11-02 17:32:38 +00003041 // Hidden API CSV generation and dex encoding
Paul Duffinf75e5272021-02-09 14:34:25 +00003042 dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, outputFile,
Paul Duffin064b70c2020-11-02 17:32:38 +00003043 proptools.Bool(j.dexProperties.Uncompress_dex))
3044
3045 j.dexJarFile = dexOutputFile
Liz Kammerd6c31d22020-08-05 15:40:41 -07003046 }
Liz Kammerd6c31d22020-08-05 15:40:41 -07003047 }
Colin Crossdcf71b22021-02-01 13:59:03 -08003048
3049 ctx.SetProvider(JavaInfoProvider, JavaInfo{
3050 HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile),
3051 ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile),
3052 ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile),
3053 AidlIncludeDirs: j.exportAidlIncludeDirs,
3054 })
Colin Cross2fe66872015-03-30 17:20:39 -07003055}
3056
Paul Duffinaa55f742020-10-06 17:20:13 +01003057func (j *Import) OutputFiles(tag string) (android.Paths, error) {
3058 switch tag {
Saeid Farivar Asanjan128fe5c2020-10-15 17:54:40 +00003059 case "", ".jar":
Paul Duffinaa55f742020-10-06 17:20:13 +01003060 return android.Paths{j.combinedClasspathFile}, nil
3061 default:
3062 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
3063 }
3064}
3065
3066var _ android.OutputFileProducer = (*Import)(nil)
3067
Nan Zhanged19fc32017-10-19 13:06:22 -07003068func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08003069 if j.combinedClasspathFile == nil {
3070 return nil
3071 }
Colin Cross37f6d792018-07-12 12:28:41 -07003072 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07003073}
3074
Colin Cross331a1212018-08-15 20:40:52 -07003075func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08003076 if j.combinedClasspathFile == nil {
3077 return nil
3078 }
Colin Cross331a1212018-08-15 20:40:52 -07003079 return android.Paths{j.combinedClasspathFile}
3080}
3081
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00003082func (j *Import) DexJarBuildPath() android.Path {
Liz Kammerd6c31d22020-08-05 15:40:41 -07003083 return j.dexJarFile
Colin Crossf24a22a2019-01-31 14:12:44 -08003084}
3085
Ulya Trafimovich9f3052c2020-06-09 14:31:19 +01003086func (j *Import) DexJarInstallPath() android.Path {
3087 return nil
3088}
3089
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01003090func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
3091 return j.classLoaderContexts
Jiyong Park1be96912018-05-28 18:02:19 +09003092}
3093
Jiyong Park45bf82e2020-12-15 22:29:02 +09003094var _ android.ApexModule = (*Import)(nil)
3095
3096// Implements android.ApexModule
Jiyong Park0f80c182020-01-31 02:49:53 +09003097func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffin0d3c2e12020-05-17 08:34:50 +01003098 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09003099}
3100
Jiyong Park45bf82e2020-12-15 22:29:02 +09003101// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003102func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3103 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003104 // Do not check for prebuilts against the min_sdk_version of enclosing APEX
3105 return nil
3106}
3107
albaltai36ff7dc2018-12-25 14:35:23 +08003108// Add compile time check for interface implementation
3109var _ android.IDEInfo = (*Import)(nil)
3110var _ android.IDECustomizedModuleName = (*Import)(nil)
3111
Brandon Lee5d45c6f2018-08-15 15:35:38 -07003112// Collect information for opening IDE project files in java/jdeps.go.
3113const (
3114 removedPrefix = "prebuilt_"
3115)
3116
3117func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
3118 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
3119}
3120
3121func (j *Import) IDECustomizedModuleName() string {
3122 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
3123 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
3124 // solution to get the Import name.
3125 name := j.Name()
3126 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08003127 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07003128 }
3129 return name
3130}
3131
Colin Cross74d73e22017-08-02 11:05:49 -07003132var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07003133
Bill Peckhamff89ffa2020-12-23 16:13:04 -08003134func (j *Import) IsInstallable() bool {
3135 return Bool(j.properties.Installable)
3136}
3137
3138var _ dexpreopterInterface = (*Import)(nil)
3139
Colin Cross1b16b0e2019-02-12 14:41:32 -08003140// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
3141//
3142// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
3143// compiled against an Android classpath.
3144//
3145// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
3146// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07003147func ImportFactory() android.Module {
3148 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07003149
Liz Kammerd6c31d22020-08-05 15:40:41 -07003150 module.AddProperties(
3151 &module.properties,
3152 &module.dexer.dexProperties,
3153 )
Colin Cross74d73e22017-08-02 11:05:49 -07003154
Paul Duffin859fe962020-05-15 10:20:31 +01003155 module.initModuleAndImport(&module.ModuleBase)
3156
Liz Kammerd6c31d22020-08-05 15:40:41 -07003157 module.dexProperties.Optimize.EnabledByDefault = false
3158
Colin Cross74d73e22017-08-02 11:05:49 -07003159 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09003160 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09003161 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09003162 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07003163 return module
Colin Cross2fe66872015-03-30 17:20:39 -07003164}
3165
Colin Cross1b16b0e2019-02-12 14:41:32 -08003166// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
3167// module.
3168//
3169// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
3170// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07003171func ImportFactoryHost() android.Module {
3172 module := &Import{}
3173
3174 module.AddProperties(&module.properties)
3175
3176 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09003177 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09003178 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07003179 return module
3180}
3181
Colin Cross42be7612019-02-21 18:12:14 -08003182// dex_import module
3183
3184type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07003185 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09003186
3187 // set the name of the output
3188 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08003189}
3190
3191type DexImport struct {
3192 android.ModuleBase
3193 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09003194 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08003195 prebuilt android.Prebuilt
3196
3197 properties DexImportProperties
3198
Colin Crossb014f072021-02-26 14:54:36 -08003199 dexJarFile android.Path
Colin Cross42be7612019-02-21 18:12:14 -08003200
3201 dexpreopter
Colin Cross56a83212020-09-15 18:30:11 -07003202
3203 hideApexVariantFromMake bool
Colin Cross42be7612019-02-21 18:12:14 -08003204}
3205
3206func (j *DexImport) Prebuilt() *android.Prebuilt {
3207 return &j.prebuilt
3208}
3209
3210func (j *DexImport) PrebuiltSrcs() []string {
3211 return j.properties.Jars
3212}
3213
3214func (j *DexImport) Name() string {
3215 return j.prebuilt.Name(j.ModuleBase.Name())
3216}
3217
Jiyong Park0b238752019-10-29 11:23:10 +09003218func (j *DexImport) Stem() string {
3219 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
3220}
3221
Jiyong Park77acec62020-06-01 21:39:15 +09003222func (a *DexImport) JacocoReportClassesFile() android.Path {
3223 return nil
3224}
3225
Colin Cross08dca382020-07-21 20:31:17 -07003226func (a *DexImport) LintDepSets() LintDepSets {
3227 return LintDepSets{}
3228}
3229
Martin Stjernholm6d415272020-01-31 17:10:36 +00003230func (j *DexImport) IsInstallable() bool {
3231 return true
3232}
3233
Colin Cross42be7612019-02-21 18:12:14 -08003234func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
3235 if len(j.properties.Jars) != 1 {
3236 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
3237 }
3238
Colin Cross56a83212020-09-15 18:30:11 -07003239 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
3240 if !apexInfo.IsForPlatform() {
3241 j.hideApexVariantFromMake = true
3242 }
3243
Jiyong Park0b238752019-10-29 11:23:10 +09003244 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08003245 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
3246
3247 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
3248 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
3249
3250 if j.dexpreopter.uncompressedDex {
Colin Crossf1a035e2020-11-16 17:32:30 -08003251 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross42be7612019-02-21 18:12:14 -08003252
3253 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
3254 rule.Temporary(temporary)
3255
3256 // use zip2zip to uncompress classes*.dex files
3257 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08003258 BuiltTool("zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08003259 FlagWithInput("-i ", inputJar).
3260 FlagWithOutput("-o ", temporary).
3261 FlagWithArg("-0 ", "'classes*.dex'")
3262
3263 // use zipalign to align uncompressed classes*.dex files
3264 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -08003265 BuiltTool("zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08003266 Flag("-f").
3267 Text("4").
3268 Input(temporary).
3269 Output(dexOutputFile)
3270
3271 rule.DeleteTemporaryFiles()
3272
Colin Crossf1a035e2020-11-16 17:32:30 -08003273 rule.Build("uncompress_dex", "uncompress dex")
Colin Cross42be7612019-02-21 18:12:14 -08003274 } else {
3275 ctx.Build(pctx, android.BuildParams{
3276 Rule: android.Cp,
3277 Input: inputJar,
3278 Output: dexOutputFile,
3279 })
3280 }
3281
3282 j.dexJarFile = dexOutputFile
3283
Jaewoong Jung4b97a562020-12-17 09:43:28 -08003284 j.dexpreopt(ctx, dexOutputFile)
Colin Cross42be7612019-02-21 18:12:14 -08003285
Colin Cross56a83212020-09-15 18:30:11 -07003286 if apexInfo.IsForPlatform() {
Jiyong Park01bca752020-06-08 19:24:09 +09003287 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
3288 j.Stem()+".jar", dexOutputFile)
3289 }
Colin Cross42be7612019-02-21 18:12:14 -08003290}
3291
Ulyana Trafimovich5539e7b2020-06-04 14:08:17 +00003292func (j *DexImport) DexJarBuildPath() android.Path {
Colin Cross42be7612019-02-21 18:12:14 -08003293 return j.dexJarFile
3294}
3295
Jiyong Park45bf82e2020-12-15 22:29:02 +09003296var _ android.ApexModule = (*DexImport)(nil)
3297
3298// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -07003299func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
3300 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +09003301 // we don't check prebuilt modules for sdk_version
3302 return nil
3303}
3304
Colin Cross42be7612019-02-21 18:12:14 -08003305// dex_import imports a `.jar` file containing classes.dex files.
3306//
3307// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
3308// to the device.
3309func DexImportFactory() android.Module {
3310 module := &DexImport{}
3311
3312 module.AddProperties(&module.properties)
3313
3314 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09003315 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09003316 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08003317 return module
3318}
3319
Colin Cross89536d42017-07-07 14:35:50 -07003320//
3321// Defaults
3322//
3323type Defaults struct {
3324 android.ModuleBase
3325 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09003326 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07003327}
3328
Colin Cross1b16b0e2019-02-12 14:41:32 -08003329// java_defaults provides a set of properties that can be inherited by other java or android modules.
3330//
3331// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
3332// property in the defaults module that exists in the depending module will be prepended to the depending module's
3333// value for that property.
3334//
3335// Example:
3336//
3337// java_defaults {
3338// name: "example_defaults",
3339// srcs: ["common/**/*.java"],
3340// javacflags: ["-Xlint:all"],
3341// aaptflags: ["--auto-add-overlay"],
3342// }
3343//
3344// java_library {
3345// name: "example",
3346// defaults: ["example_defaults"],
3347// srcs: ["example/**/*.java"],
3348// }
3349//
3350// is functionally identical to:
3351//
3352// java_library {
3353// name: "example",
3354// srcs: [
3355// "common/**/*.java",
3356// "example/**/*.java",
3357// ],
3358// javacflags: ["-Xlint:all"],
3359// }
Paul Duffin47357662019-12-05 14:07:14 +00003360func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07003361 module := &Defaults{}
3362
Colin Cross89536d42017-07-07 14:35:50 -07003363 module.AddProperties(
Jaewoong Jungbc15e3a2021-03-10 17:02:43 -08003364 &CommonProperties{},
3365 &DeviceProperties{},
Liz Kammera7a64f32020-07-09 15:16:41 -07003366 &DexProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08003367 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08003368 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07003369 &aaptProperties{},
3370 &androidLibraryProperties{},
3371 &appProperties{},
3372 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08003373 &overridableAppProperties{},
Roland Levillainb5b0ff32020-02-04 15:45:49 +00003374 &testProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07003375 &ImportProperties{},
3376 &AARImportProperties{},
3377 &sdkLibraryProperties{},
Paul Duffin1b1e8062020-05-08 13:44:43 +01003378 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08003379 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09003380 &android.ApexProperties{},
Jaewoong Jungbf135462020-04-26 15:10:51 -07003381 &RuntimeResourceOverlayProperties{},
Colin Cross014489c2020-06-02 20:09:13 -07003382 &LintProperties{},
Colin Crosscbce0b02021-02-09 10:38:30 -08003383 &appTestHelperAppProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07003384 )
3385
3386 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07003387 return module
3388}
Nan Zhangea568a42017-11-08 21:20:04 -08003389
Sasha Smundak2a4549e2018-11-05 16:49:08 -08003390func kytheExtractJavaFactory() android.Singleton {
3391 return &kytheExtractJavaSingleton{}
3392}
3393
3394type kytheExtractJavaSingleton struct {
3395}
3396
3397func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
3398 var xrefTargets android.Paths
3399 ctx.VisitAllModules(func(module android.Module) {
3400 if javaModule, ok := module.(xref); ok {
3401 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
3402 }
3403 })
3404 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
3405 if len(xrefTargets) > 0 {
Colin Crossc3d87d32020-06-04 13:25:17 -07003406 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08003407 }
3408}
3409
Nan Zhangea568a42017-11-08 21:20:04 -08003410var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07003411var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08003412var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08003413var inList = android.InList
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003414
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003415// TODO(b/132357300) Generalize SdkLibrarComponentDependency to non-SDK libraries and merge with
3416// this interface.
3417type ProvidesUsesLib interface {
3418 ProvidesUsesLib() *string
3419}
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003420
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003421func (j *Module) ProvidesUsesLib() *string {
3422 return j.usesLibraryProperties.Provides_uses_lib
3423}
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003424
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003425// Add class loader context (CLC) of a given dependency to the current CLC.
3426func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
3427 clcMap dexpreopt.ClassLoaderContextMap) {
3428
3429 dep, ok := depModule.(UsesLibraryDependency)
3430 if !ok {
3431 return
3432 }
3433
3434 // Find out if the dependency is either an SDK library or an ordinary library that is disguised
3435 // as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
3436 // a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
3437 // as subtree of that node. Otherwise the library is not a <uses_library> and should not be
3438 // added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
3439 // the current CLC.
3440 var implicitSdkLib *string
3441 comp, isComp := depModule.(SdkLibraryComponentDependency)
3442 if isComp {
3443 implicitSdkLib = comp.OptionalImplicitSdkLibrary()
3444 // OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
3445 }
3446 if implicitSdkLib == nil {
3447 if ulib, ok := depModule.(ProvidesUsesLib); ok {
3448 implicitSdkLib = ulib.ProvidesUsesLib()
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003449 }
3450 }
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003451
3452 depTag := ctx.OtherModuleDependencyTag(depModule)
3453 if depTag == libTag || depTag == usesLibTag {
3454 // Ok, propagate <uses-library> through non-static library dependencies.
3455 } else if depTag == staticLibTag {
3456 // Propagate <uses-library> through static library dependencies, unless it is a component
3457 // library (such as stubs). Component libraries have a dependency on their SDK library,
3458 // which should not be pulled just because of a static component library.
3459 if implicitSdkLib != nil {
3460 return
3461 }
3462 } else {
3463 // Don't propagate <uses-library> for other dependency tags.
3464 return
3465 }
3466
3467 if implicitSdkLib != nil {
Ulya Trafimovich7bc1cf52021-01-05 15:41:55 +00003468 clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +00003469 dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
3470 } else {
3471 depName := ctx.OtherModuleName(depModule)
3472 clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
3473 }
Ulya Trafimovich65b03192020-12-03 16:50:22 +00003474}