blob: 182dbc36084c68033e16b77b0d3fbba55e31e7f9 [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"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
39 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000040 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000041
42 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
43 librarySdkMemberType{
44 android.SdkMemberTypeBase{
45 PropertyName: "java_libs",
46 },
47 },
48 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000049
50 android.RegisterSdkMemberType(&testSdkMemberType{
51 SdkMemberTypeBase: android.SdkMemberTypeBase{
52 PropertyName: "java_tests",
53 },
54 })
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Paul Duffinf9b1da02019-12-18 19:51:55 +000057func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
58 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
59
60 ctx.RegisterModuleType("java_library", LibraryFactory)
61 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
62 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
63 ctx.RegisterModuleType("java_binary", BinaryFactory)
64 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
65 ctx.RegisterModuleType("java_test", TestFactory)
66 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
67 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000068 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000069 ctx.RegisterModuleType("java_import", ImportFactory)
70 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
71 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
72 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
73 ctx.RegisterModuleType("dex_import", DexImportFactory)
74
75 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
76 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
77}
78
Jeongik Cha2cc570d2019-10-29 15:44:45 +090079func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
80 if j.SocSpecific() || j.DeviceSpecific() ||
81 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
82 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +090083 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090084 ctx.PropertyErrorf("sdk_version",
85 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
86 }
87 }
88 }
89}
90
Jeongik Cha538c0d02019-07-11 15:54:27 +090091func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
92 if sc, ok := ctx.Module().(sdkContext); ok {
93 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +090094 sdkVersionSpecified := sc.sdkVersion().specified()
95 if usePlatformAPI && sdkVersionSpecified {
96 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
97 } else if !usePlatformAPI && !sdkVersionSpecified {
98 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +090099 }
100
101 }
102}
103
Colin Cross2fe66872015-03-30 17:20:39 -0700104// TODO:
105// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700106// Renderscript
107// Post-jar passes:
108// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700109// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700110// DroidDoc
111// Findbugs
112
Colin Cross89536d42017-07-07 14:35:50 -0700113type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700114 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
115 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700117
118 // list of source files that should not be used to build the Java module.
119 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800120 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700121
122 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700123 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700124
Colin Cross86a63ff2017-09-27 17:33:10 -0700125 // list of directories that should be excluded from java_resource_dirs
126 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700127
Colin Cross0f37af02017-09-27 17:42:05 -0700128 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800129 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700130
Colin Crosscedd4762018-09-13 11:26:19 -0700131 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800132 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700133
Colin Cross7d5136f2015-05-11 13:39:40 -0700134 // list of module-specific flags that will be used for javac compiles
135 Javacflags []string `android:"arch_variant"`
136
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200137 // list of module-specific flags that will be used for kotlinc compiles
138 Kotlincflags []string `android:"arch_variant"`
139
Colin Cross7d5136f2015-05-11 13:39:40 -0700140 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700141 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700142
143 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700144 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700145
146 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800147 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700148
Colin Cross540eff82017-06-22 17:01:52 -0700149 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800150 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700151
152 // If not blank, set the java version passed to javac as -source and -target
153 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700154
Colin Cross9ae1b922018-06-26 17:59:05 -0700155 // If set to true, allow this module to be dexed and installed on devices. Has no
156 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700157 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700158
Colin Cross0f37af02017-09-27 17:42:05 -0700159 // If set to true, include sources used to compile the module in to the final jar
160 Include_srcs *bool
161
Vladimir Marko0975ee02019-04-02 10:29:55 +0100162 // If not empty, classes are restricted to the specified packages and their sub-packages.
163 // This restriction is checked after applying jarjar rules and including static libs.
164 Permitted_packages []string
165
Colin Crossbe9cdb82019-01-21 21:37:16 -0800166 // List of modules to use as annotation processors
167 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700168
Artur Satayev9cf46692019-11-26 18:08:34 +0000169 // List of modules to export to libraries that directly depend on this library as annotation processors
170 Exported_plugins []string
171
Nan Zhang61eaedb2017-11-02 13:28:15 -0700172 // The number of Java source entries each Javac instance can process
173 Javac_shard_size *int64
174
Nan Zhang5f8cb422018-02-06 10:34:32 -0800175 // Add host jdk tools.jar to bootclasspath
176 Use_tools_jar *bool
177
Colin Cross1369cdb2017-09-29 17:58:17 -0700178 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700179 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800180 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700181
Colin Cross6cef4812019-10-17 14:23:50 -0700182 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700183 Javacflags []string
184 }
Colin Crosscb933592017-11-22 13:49:43 -0800185
Colin Cross81440082018-08-15 20:21:55 -0700186 // When compiling language level 9+ .java code in packages that are part of
187 // a system module, patch_module names the module that your sources and
188 // dependencies should be patched into. The Android runtime currently
189 // doesn't implement the JEP 261 module system so this option is only
190 // supported at compile time. It should only be needed to compile tests in
191 // packages that exist in libcore and which are inconvenient to move
192 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100193 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700194
Colin Crosscb933592017-11-22 13:49:43 -0800195 Jacoco struct {
196 // List of classes to include for instrumentation with jacoco to collect coverage
197 // information at runtime when building with coverage enabled. If unset defaults to all
198 // classes.
199 // Supports '*' as the last character of an entry in the list as a wildcard match.
200 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
201 // it matches classes in the package that have the class name as a prefix.
202 Include_filter []string
203
204 // List of classes to exclude from instrumentation with jacoco to collect coverage
205 // information at runtime when building with coverage enabled. Overrides classes selected
206 // by the include_filter property.
207 // Supports '*' as the last character of an entry in the list as a wildcard match.
208 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
209 // it matches classes in the package that have the class name as a prefix.
210 Exclude_filter []string
211 }
212
Andreas Gampef3e5b552018-01-22 21:27:21 -0800213 Errorprone struct {
214 // List of javac flags that should only be used when running errorprone.
215 Javacflags []string
216 }
217
Colin Cross0f2ee152017-12-14 15:22:43 -0800218 Proto struct {
219 // List of extra options that will be passed to the proto generator.
220 Output_params []string
221 }
222
Colin Crosscb933592017-11-22 13:49:43 -0800223 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800224
225 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800226 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700227}
228
Colin Cross89536d42017-07-07 14:35:50 -0700229type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700230 // list of module-specific flags that will be used for dex compiles
231 Dxflags []string `android:"arch_variant"`
232
Jeongik Cha538c0d02019-07-11 15:54:27 +0900233 // if not blank, set to the version of the sdk to compile against.
234 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800235 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700236
Colin Cross83bb3162018-06-25 15:48:06 -0700237 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
238 // Defaults to sdk_version if not set.
239 Min_sdk_version *string
240
Dan Willemsen419290a2018-10-31 15:28:47 -0700241 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
242 // Defaults to sdk_version if not set.
243 Target_sdk_version *string
244
Jeongik Cha356dac42019-08-19 14:09:52 +0900245 // Whether to compile against the platform APIs instead of an SDK.
246 // If true, then sdk_version must be empty. The value of this field
247 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700248 Platform_apis *bool
249
Colin Crossebe1a512017-11-14 13:12:14 -0800250 Aidl struct {
251 // Top level directories to pass to aidl tool
252 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700253
Colin Crossebe1a512017-11-14 13:12:14 -0800254 // Directories rooted at the Android.bp file to pass to aidl tool
255 Local_include_dirs []string
256
257 // directories that should be added as include directories for any aidl sources of modules
258 // that depend on this module, as well as to aidl for this module.
259 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100260
261 // whether to generate traces (for systrace) for this interface
262 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100263
264 // whether to generate Binder#GetTransaction name method.
265 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800266 }
Colin Cross92430102017-10-09 14:59:32 -0700267
268 // If true, export a copy of the module as a -hostdex module for host testing.
269 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700270
Colin Cross7f87f4f2019-04-24 13:41:45 -0700271 Target struct {
272 Hostdex struct {
273 // Additional required dependencies to add to -hostdex modules.
274 Required []string
275 }
276 }
277
David Brazdil17ef5632018-06-27 10:27:45 +0100278 // If set to true, compile dex regardless of installable. Defaults to false.
279 Compile_dex *bool
280
Colin Cross66dbc0b2017-12-28 12:23:20 -0800281 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700282 // If false, disable all optimization. Defaults to true for android_app and android_test
283 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800284 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700285 // True if the module containing this has it set by default.
286 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800287
288 // If true, optimize for size by removing unused code. Defaults to true for apps,
289 // false for libraries and tests.
290 Shrink *bool
291
292 // If true, optimize bytecode. Defaults to false.
293 Optimize *bool
294
295 // If true, obfuscate bytecode. Defaults to false.
296 Obfuscate *bool
297
298 // If true, do not use the flag files generated by aapt that automatically keep
299 // classes referenced by the app manifest. Defaults to false.
300 No_aapt_flags *bool
301
302 // Flags to pass to proguard.
303 Proguard_flags []string
304
305 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800306 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800307 }
308
Paul Duffine25c6442019-10-11 13:50:28 +0100309 // When targeting 1.9 and above, override the modules to use with --system,
310 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700311 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700312
Jiyong Park4c4c0242019-10-21 14:53:15 +0900313 // set the name of the output
314 Stem *string
315
Colin Cross5a0dcd52018-10-05 14:20:06 -0700316 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800317 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700318}
319
Sasha Smundak2057f822019-04-16 17:16:58 -0700320func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
321 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
322}
323
Colin Cross46c9b8b2017-06-22 16:51:17 -0700324// Module contains the properties and members used by all java module types
325type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700326 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700327 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900328 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900329 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700330
Colin Cross89536d42017-07-07 14:35:50 -0700331 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700332 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700333 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700334
Colin Cross331a1212018-08-15 20:40:52 -0700335 // jar file containing header classes including static library dependencies, suitable for
336 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700337 headerJarFile android.Path
338
Colin Cross331a1212018-08-15 20:40:52 -0700339 // jar file containing implementation classes including static library dependencies but no
340 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700341 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700342
Colin Cross331a1212018-08-15 20:40:52 -0700343 // jar file containing only resources including from static library dependencies
344 resourceJar android.Path
345
Colin Cross0c4ce212019-05-03 15:28:19 -0700346 // args and dependencies to package source files into a srcjar
347 srcJarArgs []string
348 srcJarDeps android.Paths
349
Colin Cross331a1212018-08-15 20:40:52 -0700350 // jar file containing implementation classes and resources including static library
351 // dependencies
352 implementationAndResourcesJar android.Path
353
354 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700355 dexJarFile android.Path
356
Colin Cross43f08db2018-11-12 10:13:39 -0800357 // output file that contains classes.dex if it should be in the output file
358 maybeStrippedDexJarFile android.Path
359
Colin Crosscb933592017-11-22 13:49:43 -0800360 // output file containing uninstrumented classes that will be instrumented by jacoco
361 jacocoReportClassesFile android.Path
362
Colin Cross66dbc0b2017-12-28 12:23:20 -0800363 // output file containing mapping of obfuscated names
364 proguardDictionary android.Path
365
Colin Cross331a1212018-08-15 20:40:52 -0700366 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700367 outputFile android.Path
368 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700369
Colin Cross635c3b02016-05-18 15:37:25 -0700370 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700371
Colin Cross635c3b02016-05-18 15:37:25 -0700372 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700373
Colin Cross2fe66872015-03-30 17:20:39 -0700374 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700375 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800376
377 // list of .java files and srcjars that was passed to javac
378 compiledJavaSrcs android.Paths
379 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800380
381 // list of extra progurad flag files
382 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900383
Colin Cross094054a2018-10-17 15:10:48 -0700384 // manifest file to use instead of properties.Manifest
385 overrideManifest android.OptionalPath
386
Artur Satayev9cf46692019-11-26 18:08:34 +0000387 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900388 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700389
Artur Satayev9cf46692019-11-26 18:08:34 +0000390 // list of plugins that this java module is exporting
391 exportedPluginJars android.Paths
392
393 // list of plugins that this java module is exporting
394 exportedPluginClasses []string
395
396 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800397 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700398 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800399
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800400 // expanded Jarjar_rules
401 expandJarjarRules android.Path
402
Vladimir Marko0975ee02019-04-02 10:29:55 +0100403 // list of additional targets for checkbuild
404 additionalCheckedModules android.Paths
405
Colin Cross988708c2019-05-06 14:04:11 -0700406 // Extra files generated by the module type to be added as java resources.
407 extraResources android.Paths
408
Colin Crossf24a22a2019-01-31 14:12:44 -0800409 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800410 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800411
412 // list of the xref extraction files
413 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700414}
415
Colin Cross41955e82019-05-29 14:40:35 -0700416func (j *Module) OutputFiles(tag string) (android.Paths, error) {
417 switch tag {
418 case "":
419 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700420 case ".jar":
421 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700422 case ".proguard_map":
423 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700424 default:
425 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
426 }
Colin Cross54250902017-12-05 09:28:08 -0800427}
428
Colin Cross41955e82019-05-29 14:40:35 -0700429var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800430
Colin Crossf506d872017-07-19 15:53:04 -0700431type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700432 HeaderJars() android.Paths
433 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700434 ResourceJars() android.Paths
435 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800436 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700437 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900438 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000439 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700440 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700441 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900442 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700443}
444
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900446 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
447 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900448}
449
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800450type xref interface {
451 XrefJavaFiles() android.Paths
452}
453
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800454func (j *Module) XrefJavaFiles() android.Paths {
455 return j.kytheFiles
456}
457
Colin Cross89536d42017-07-07 14:35:50 -0700458func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
459 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
460 android.InitDefaultableModule(module)
461}
462
Colin Crossbe1da472017-07-07 15:59:46 -0700463type dependencyTag struct {
464 blueprint.BaseDependencyTag
465 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700466}
467
Colin Crossa4f08812018-10-02 22:03:40 -0700468type jniDependencyTag struct {
469 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700470}
471
Jiyong Park8be103b2019-11-08 15:53:48 +0900472func IsJniDepTag(depTag blueprint.DependencyTag) bool {
473 _, ok := depTag.(*jniDependencyTag)
474 return ok
475}
476
Colin Crossbe1da472017-07-07 15:59:46 -0700477var (
Colin Cross4b964c02018-10-15 16:18:06 -0700478 staticLibTag = dependencyTag{name: "staticlib"}
479 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700480 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800481 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000482 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700483 bootClasspathTag = dependencyTag{name: "bootclasspath"}
484 systemModulesTag = dependencyTag{name: "system modules"}
485 frameworkResTag = dependencyTag{name: "framework-res"}
486 frameworkApkTag = dependencyTag{name: "framework-apk"}
487 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800488 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700489 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
490 certificateTag = dependencyTag{name: "certificate"}
491 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700492 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700493)
Colin Cross2fe66872015-03-30 17:20:39 -0700494
Jiyong Park83dc74b2020-01-14 18:38:44 +0900495func IsLibDepTag(depTag blueprint.DependencyTag) bool {
496 return depTag == libTag
497}
498
499func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
500 return depTag == staticLibTag
501}
502
Colin Crossfc3674a2017-09-18 17:41:52 -0700503type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700504 useModule, useFiles, useDefaultLibs, invalidVersion bool
505
Colin Cross6cef4812019-10-17 14:23:50 -0700506 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
507 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100508
509 // The default system modules to use. Will be an empty string if no system
510 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700511 systemModules string
512
Colin Cross6cef4812019-10-17 14:23:50 -0700513 // The modules that will be added ot the classpath when targeting 1.9 or higher
514 java9Classpath []string
515
Colin Crossa97c5d32018-03-28 14:58:31 -0700516 frameworkResModule string
517
Colin Cross86a60ae2018-05-29 14:44:55 -0700518 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700519 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100520
521 noStandardLibs, noFrameworksLibs bool
522}
523
524func (s sdkDep) hasStandardLibs() bool {
525 return !s.noStandardLibs
526}
527
528func (s sdkDep) hasFrameworkLibs() bool {
529 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700530}
531
Colin Crossa4f08812018-10-02 22:03:40 -0700532type jniLib struct {
533 name string
534 path android.Path
535 target android.Target
536}
537
Colin Cross0ea8ba82019-06-06 14:33:29 -0700538func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800539 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
540}
541
Colin Cross0ea8ba82019-06-06 14:33:29 -0700542func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800543 return j.shouldInstrument(ctx) &&
544 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
545 ctx.Config().UnbundledBuild())
546}
547
Jiyong Park6a927c42020-01-21 02:03:43 +0900548func (j *Module) sdkVersion() sdkSpec {
549 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700550}
551
Paul Duffine25c6442019-10-11 13:50:28 +0100552func (j *Module) systemModules() string {
553 return proptools.String(j.deviceProperties.System_modules)
554}
555
Jiyong Park6a927c42020-01-21 02:03:43 +0900556func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700557 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900558 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700559 }
560 return j.sdkVersion()
561}
562
Jiyong Park6a927c42020-01-21 02:03:43 +0900563func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700564 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900565 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700566 }
567 return j.sdkVersion()
568}
569
Jiyong Parkb02bb402019-12-03 00:43:57 +0900570func (j *Module) AvailableFor(what string) bool {
571 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
572 // Exception: for hostdex: true libraries, the platform variant is created
573 // even if it's not marked as available to platform. In that case, the platform
574 // variant is used only for the hostdex and not installed to the device.
575 return true
576 }
577 return j.ApexModuleBase.AvailableFor(what)
578}
579
Colin Crossbe1da472017-07-07 15:59:46 -0700580func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700581 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100582 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700583 if sdkDep.useDefaultLibs {
584 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
585 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
586 if sdkDep.hasFrameworkLibs() {
587 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700588 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700589 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700590 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100591 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700592 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700593 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
594 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
595 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
596 }
Colin Cross2fe66872015-03-30 17:20:39 -0700597 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700598
Nan Zhangb2b33de2018-02-23 11:18:47 -0800599 if ctx.ModuleName() == "android_stubs_current" ||
600 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700601 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700602 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800603 }
Colin Cross2fe66872015-03-30 17:20:39 -0700604 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700605
Inseob Kimac1e9862019-12-09 18:15:47 +0900606 syspropPublicStubs := syspropPublicStubs(ctx.Config())
607
608 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
609 // and redirects dependency to public stub depending on the link type.
610 rewriteSyspropLibs := func(libs []string, prop string) []string {
611 // make a copy
612 ret := android.CopyOf(libs)
613
614 for idx, lib := range libs {
615 stub, ok := syspropPublicStubs[lib]
616
617 if !ok {
618 continue
619 }
620
621 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900622 // only platform modules can use internal props
623 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900624 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900625 }
626 }
627
628 return ret
629 }
630
631 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
632 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700633
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700634 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000635 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800636
Colin Crossfe17f6f2019-03-28 19:30:56 -0700637 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700638 if j.hasSrcExt(".proto") {
639 protoDeps(ctx, &j.protoProperties)
640 }
Colin Cross93e85952017-08-15 13:34:18 -0700641
642 if j.hasSrcExt(".kt") {
643 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
644 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700645 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
646 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800647 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800648 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
649 }
Colin Cross93e85952017-08-15 13:34:18 -0700650 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800651
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000652 // Framework libraries need special handling in static coverage builds: they should not have
653 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
654 // the same jacoco classes coming from different bootclasspath jars.
655 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
656 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
657 j.properties.Instrument = true
658 }
659 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700660 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800661 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700662}
663
664func hasSrcExt(srcs []string, ext string) bool {
665 for _, src := range srcs {
666 if filepath.Ext(src) == ext {
667 return true
668 }
669 }
670
671 return false
672}
673
674func (j *Module) hasSrcExt(ext string) bool {
675 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700676}
677
Colin Cross46c9b8b2017-06-22 16:51:17 -0700678func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700679 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700680
Colin Crossebe1a512017-11-14 13:12:14 -0800681 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
682 aidlIncludes = append(aidlIncludes,
683 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
684 aidlIncludes = append(aidlIncludes,
685 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700686
Colin Cross3047fa22019-04-18 10:56:44 -0700687 var flags []string
688 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700689
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700690 if aidlPreprocess.Valid() {
691 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700692 deps = append(deps, aidlPreprocess.Path())
693 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700694 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700695 }
696
Colin Cross3047fa22019-04-18 10:56:44 -0700697 if len(j.exportAidlIncludeDirs) > 0 {
698 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
699 }
700
701 if len(aidlIncludes) > 0 {
702 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
703 }
704
Colin Cross635c3b02016-05-18 15:37:25 -0700705 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800706 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700707 flags = append(flags, "-I"+src.String())
708 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700709
Martijn Coeneneab15642018-03-09 09:29:59 +0100710 if Bool(j.deviceProperties.Aidl.Generate_traces) {
711 flags = append(flags, "-t")
712 }
713
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100714 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
715 flags = append(flags, "--transaction_names")
716 }
717
Colin Cross3047fa22019-04-18 10:56:44 -0700718 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700719}
720
Colin Cross32f676a2017-09-06 13:41:06 -0700721type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800722 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700723 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800724 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700725 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800726 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700727 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700728 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700729 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700730 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800731 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700732 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700733 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700734 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700735 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800736 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800737
738 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700739}
Colin Cross2fe66872015-03-30 17:20:39 -0700740
Colin Cross54250902017-12-05 09:28:08 -0800741func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
742 for _, f := range dep.Srcs() {
743 if f.Ext() != ".jar" {
744 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
745 ctx.OtherModuleName(dep.(blueprint.Module)))
746 }
747 }
748}
749
Jiyong Park2d492942018-03-05 17:44:10 +0900750type linkType int
751
752const (
Jiyong Park50146e92020-01-30 18:00:15 +0900753 // TODO(jiyong) rename these for better readability. Make the allowed
754 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900755 javaCore linkType = iota
756 javaSdk
757 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900758 javaModule
Jiyong Park2d492942018-03-05 17:44:10 +0900759 javaPlatform
760)
761
Jeongik Cha75b83b02019-11-01 15:28:00 +0900762type linkTypeContext interface {
763 android.Module
764 getLinkType(name string) (ret linkType, stubs bool)
765}
766
767func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700768 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700769 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900770 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
771 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100772 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900773 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900774 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900775 return javaCore, false
776 case name == "android_system_stubs_current":
777 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900778 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900779 return javaSystem, false
780 case name == "android_test_stubs_current":
781 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900782 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900783 return javaPlatform, false
784 case name == "android_stubs_current":
785 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900786 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900787 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900788 case name == "android_module_lib_stubs_current":
789 return javaModule, true
790 case ver.kind == sdkModule:
791 return javaModule, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900792 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900793 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900794 case !ver.valid():
795 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700796 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900797 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900798 }
799}
800
Jeongik Cha75b83b02019-11-01 15:28:00 +0900801func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700802 if ctx.Host() {
803 return
804 }
805
Jeongik Cha75b83b02019-11-01 15:28:00 +0900806 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900807 if stubs {
808 return
809 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900810 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900811 commonMessage := "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source."
812
813 switch myLinkType {
814 case javaCore:
815 if otherLinkType != javaCore {
816 ctx.ModuleErrorf("compiles against core Java API, but dependency %q is compiling against non-core Java APIs."+commonMessage,
Jiyong Park750e5572018-01-31 00:20:13 +0900817 ctx.OtherModuleName(to))
818 }
Jiyong Park2d492942018-03-05 17:44:10 +0900819 break
820 case javaSdk:
821 if otherLinkType != javaCore && otherLinkType != javaSdk {
822 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
823 ctx.OtherModuleName(to))
824 }
825 break
826 case javaSystem:
Jiyong Park50146e92020-01-30 18:00:15 +0900827 if otherLinkType == javaPlatform || otherLinkType == javaModule {
Jiyong Park2d492942018-03-05 17:44:10 +0900828 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
829 ctx.OtherModuleName(to))
830 }
831 break
Jiyong Park50146e92020-01-30 18:00:15 +0900832 case javaModule:
833 if otherLinkType == javaPlatform {
834 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
835 ctx.OtherModuleName(to))
836 }
837 break
Jiyong Park2d492942018-03-05 17:44:10 +0900838 case javaPlatform:
839 // no restriction on link-type
840 break
Jiyong Park750e5572018-01-31 00:20:13 +0900841 }
842}
843
Colin Cross32f676a2017-09-06 13:41:06 -0700844func (j *Module) collectDeps(ctx android.ModuleContext) deps {
845 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700846
Colin Cross300f0382018-03-06 13:11:51 -0800847 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700848 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800849 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700850 ctx.AddMissingDependencies(sdkDep.bootclasspath)
851 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800852 } else if sdkDep.useFiles {
853 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700854 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700855 deps.aidlPreprocess = sdkDep.aidl
856 } else {
857 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800858 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700859 }
860
Colin Crossd11fcda2017-10-23 17:59:01 -0700861 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700862 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700863 tag := ctx.OtherModuleDependencyTag(module)
864
Colin Crossa4f08812018-10-02 22:03:40 -0700865 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700866 // Handled by AndroidApp.collectAppDeps
867 return
868 }
869 if tag == certificateTag {
870 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700871 return
872 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900873 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900874 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900875 if to, ok := module.(linkTypeContext); ok {
876 switch tag {
877 case bootClasspathTag, libTag, staticLibTag:
878 checkLinkType(ctx, j, to, tag.(dependencyTag))
879 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700880 }
Jiyong Park750e5572018-01-31 00:20:13 +0900881 }
Colin Cross54250902017-12-05 09:28:08 -0800882 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800883 case SdkLibraryDependency:
884 switch tag {
885 case libTag:
886 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
887 // names of sdk libs that are directly depended are exported
888 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700889 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800890 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
891 }
Colin Cross54250902017-12-05 09:28:08 -0800892 case Dependency:
893 switch tag {
894 case bootClasspathTag:
895 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700896 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800897 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900898 // sdk lib names from dependencies are re-exported
899 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700900 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000901 pluginJars, pluginClasses := dep.ExportedPlugins()
902 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700903 case java9LibTag:
904 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800905 case staticLibTag:
906 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
907 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
908 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700909 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900910 // sdk lib names from dependencies are re-exported
911 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700912 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000913 pluginJars, pluginClasses := dep.ExportedPlugins()
914 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800915 case pluginTag:
916 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800917 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000918 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
919 } else {
920 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800921 }
922 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
923 } else {
924 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
925 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000926 case exportedPluginTag:
927 if plugin, ok := dep.(*Plugin); ok {
928 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
929 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
930 }
931 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
932 if plugin.pluginProperties.Processor_class != nil {
933 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
934 }
935 } else {
936 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
937 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800938 case frameworkApkTag:
939 if ctx.ModuleName() == "android_stubs_current" ||
940 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700941 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800942 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
943 // resource files out of there for aapt.
944 //
945 // Normally the package rule runs aapt, which includes the resource,
946 // but we're not running that in our package rule so just copy in the
947 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700948 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800949 }
Colin Cross54250902017-12-05 09:28:08 -0800950 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700951 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800952 case kotlinAnnotationsTag:
953 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800954 }
955
Colin Cross54250902017-12-05 09:28:08 -0800956 case android.SourceFileProducer:
957 switch tag {
958 case libTag:
959 checkProducesJars(ctx, dep)
960 deps.classpath = append(deps.classpath, dep.Srcs()...)
961 case staticLibTag:
962 checkProducesJars(ctx, dep)
963 deps.classpath = append(deps.classpath, dep.Srcs()...)
964 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
965 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800966 }
967 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700968 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100969 case bootClasspathTag:
970 // If a system modules dependency has been added to the bootclasspath
971 // then add its libs to the bootclasspath.
972 sm := module.(*SystemModules)
973 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
974
Colin Cross1369cdb2017-09-29 17:58:17 -0700975 case systemModulesTag:
976 if deps.systemModules != nil {
977 panic("Found two system module dependencies")
978 }
979 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000980 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700981 panic("Missing directory for system module dependency")
982 }
Colin Crossb77043e2019-07-16 13:57:13 -0700983 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700984 }
Colin Crossec7a0422017-07-07 14:47:12 -0700985 }
Colin Cross2fe66872015-03-30 17:20:39 -0700986 })
987
Jiyong Park1be96912018-05-28 18:02:19 +0900988 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
989
Colin Cross32f676a2017-09-06 13:41:06 -0700990 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700991}
992
Artur Satayev9cf46692019-11-26 18:08:34 +0000993func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
994 deps.processorPath = append(deps.processorPath, pluginJars...)
995 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
996}
997
Colin Cross1e743852019-10-28 11:37:20 -0700998func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +0900999 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001000 if err != nil {
1001 ctx.PropertyErrorf("sdk_version", "%s", err)
1002 }
Nan Zhang357466b2018-04-17 17:38:36 -07001003 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001004 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001005 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001006 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001007 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001008 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001009 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1010 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001011 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001012 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001013 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001014 }
Nan Zhang357466b2018-04-17 17:38:36 -07001015}
1016
Colin Cross1e743852019-10-28 11:37:20 -07001017type javaVersion int
1018
1019const (
1020 JAVA_VERSION_UNSUPPORTED = 0
1021 JAVA_VERSION_6 = 6
1022 JAVA_VERSION_7 = 7
1023 JAVA_VERSION_8 = 8
1024 JAVA_VERSION_9 = 9
1025)
1026
1027func (v javaVersion) String() string {
1028 switch v {
1029 case JAVA_VERSION_6:
1030 return "1.6"
1031 case JAVA_VERSION_7:
1032 return "1.7"
1033 case JAVA_VERSION_8:
1034 return "1.8"
1035 case JAVA_VERSION_9:
1036 return "1.9"
1037 default:
1038 return "unsupported"
1039 }
1040}
1041
1042// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1043func (v javaVersion) usesJavaModules() bool {
1044 return v >= 9
1045}
1046
1047func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001048 switch javaVersion {
1049 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001050 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001051 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001052 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001053 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001054 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001055 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001056 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001057 case "10", "11":
1058 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001059 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001060 default:
1061 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001062 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001063 }
1064}
1065
Nan Zhanged19fc32017-10-19 13:06:22 -07001066func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001067
Colin Crossf03c82b2015-04-13 13:53:40 -07001068 var flags javaBuilderFlags
1069
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001070 // javaVersion flag.
1071 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1072
Nan Zhanged19fc32017-10-19 13:06:22 -07001073 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001074 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001075 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001076 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001077 }
Colin Cross6510f912017-11-29 00:27:14 -08001078 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001079 // Override the -g flag passed globally to remove local variable debug info to reduce
1080 // disk and memory usage.
1081 javacFlags = append(javacFlags, "-g:source,lines")
1082 }
Colin Crossc228a702019-11-06 16:18:05 -08001083 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001084
Colin Cross66548102018-06-19 22:47:35 -07001085 if ctx.Config().RunErrorProne() {
1086 if config.ErrorProneClasspath == nil {
1087 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1088 }
1089
1090 errorProneFlags := []string{
1091 "-Xplugin:ErrorProne",
1092 "${config.ErrorProneChecks}",
1093 }
1094 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1095
1096 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1097 "'" + strings.Join(errorProneFlags, " ") + "'"
1098 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001099 }
1100
Nan Zhanged19fc32017-10-19 13:06:22 -07001101 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001102 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1103 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001104 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001105 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001106
Colin Crossbe9cdb82019-01-21 21:37:16 -08001107 flags.processor = strings.Join(deps.processorClasses, ",")
1108
Colin Cross1e743852019-10-28 11:37:20 -07001109 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1110 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001111 // Give host-side tools a version of OpenJDK's standard libraries
1112 // close to what they're targeting. As of Dec 2017, AOSP is only
1113 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1114 //
1115 // When building with OpenJDK 8, the following should have no
1116 // effect since those jars would be available by default.
1117 //
1118 // When building with OpenJDK 9 but targeting a version < 1.8,
1119 // putting them on the bootclasspath means that:
1120 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1121 // b) references to existing APIs are not reinterpreted in an
1122 // OpenJDK 9-specific way, eg. calls to subclasses of
1123 // java.nio.Buffer as in http://b/70862583
1124 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1125 flags.bootClasspath = append(flags.bootClasspath,
1126 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1127 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001128 if Bool(j.properties.Use_tools_jar) {
1129 flags.bootClasspath = append(flags.bootClasspath,
1130 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1131 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001132 }
1133
Colin Cross1e743852019-10-28 11:37:20 -07001134 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001135 // Manually specify build directory in case it is not under the repo root.
1136 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1137 // just adding a symlink under the root doesn't help.)
1138 patchPaths := ".:" + ctx.Config().BuildDir()
1139 classPath := flags.classpath.FormJavaClassPath("")
1140 if classPath != "" {
1141 patchPaths += ":" + classPath
1142 }
1143 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001144 }
1145
Nan Zhanged19fc32017-10-19 13:06:22 -07001146 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001147 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001148
Nan Zhanged19fc32017-10-19 13:06:22 -07001149 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001150 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001151
Colin Cross81440082018-08-15 20:21:55 -07001152 if len(javacFlags) > 0 {
1153 // optimization.
1154 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1155 flags.javacFlags = "$javacFlags"
1156 }
1157
Nan Zhanged19fc32017-10-19 13:06:22 -07001158 return flags
1159}
Colin Crossc0b06f12015-04-08 13:03:43 -07001160
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001161func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001162 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001163
1164 deps := j.collectDeps(ctx)
1165 flags := j.collectBuilderFlags(ctx, deps)
1166
Colin Cross1e743852019-10-28 11:37:20 -07001167 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001168 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1169 }
Colin Cross8a497952019-03-05 22:25:09 -08001170 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001171 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001172 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001173 }
1174
Colin Crossaf050172017-11-15 23:01:59 -08001175 srcFiles = j.genSources(ctx, srcFiles, flags)
1176
1177 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001178 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001179 if aaptSrcJar != nil {
1180 srcJars = append(srcJars, aaptSrcJar)
1181 }
Colin Crossb7a63242015-04-16 14:09:14 -07001182
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001183 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001184 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001185 }
1186
Colin Cross1ee23172017-10-18 14:44:18 -07001187 jarName := ctx.ModuleName() + ".jar"
1188
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001189 javaSrcFiles := srcFiles.FilterByExt(".java")
1190 var uniqueSrcFiles android.Paths
1191 set := make(map[string]bool)
1192 for _, v := range javaSrcFiles {
1193 if _, found := set[v.String()]; !found {
1194 set[v.String()] = true
1195 uniqueSrcFiles = append(uniqueSrcFiles, v)
1196 }
1197 }
1198
patricktu242faad2019-09-24 15:41:30 +08001199 // Collect .java files for AIDEGen
1200 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1201
Colin Cross55f63ea2018-08-27 12:37:09 -07001202 var kotlinJars android.Paths
1203
Colin Cross93e85952017-08-15 13:34:18 -07001204 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001205 // user defined kotlin flags.
1206 kotlincFlags := j.properties.Kotlincflags
1207 CheckKotlincFlags(ctx, kotlincFlags)
1208
Colin Cross93e85952017-08-15 13:34:18 -07001209 // If there are kotlin files, compile them first but pass all the kotlin and java files
1210 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1211 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001212 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001213 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001214 kotlincFlags = append(kotlincFlags, "-no-jdk")
1215 }
1216 if len(kotlincFlags) > 0 {
1217 // optimization.
1218 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1219 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001220 }
1221
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001222 var kotlinSrcFiles android.Paths
1223 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1224 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1225
patricktu242faad2019-09-24 15:41:30 +08001226 // Collect .kt files for AIDEGen
1227 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1228
Colin Crossafbb1732019-01-17 15:42:52 -08001229 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1230 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1231
1232 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1233 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1234
1235 if len(flags.processorPath) > 0 {
1236 // Use kapt for annotation processing
1237 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1238 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1239 srcJars = append(srcJars, kaptSrcJar)
1240 // Disable annotation processing in javac, it's already been handled by kapt
1241 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001242 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001243 }
Colin Cross93e85952017-08-15 13:34:18 -07001244
Colin Cross1ee23172017-10-18 14:44:18 -07001245 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001246 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001247 if ctx.Failed() {
1248 return
1249 }
1250
1251 // Make javac rule depend on the kotlinc rule
1252 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001253
Colin Cross93e85952017-08-15 13:34:18 -07001254 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001255 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001256 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001257 }
1258
Colin Cross55f63ea2018-08-27 12:37:09 -07001259 jars := append(android.Paths(nil), kotlinJars...)
1260
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001261 // Store the list of .java files that was passed to javac
1262 j.compiledJavaSrcs = uniqueSrcFiles
1263 j.compiledSrcJars = srcJars
1264
Nan Zhang61eaedb2017-11-02 13:28:15 -07001265 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001266 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001267 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1268 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001269 // Formerly, there was a check here that prevented annotation processors
1270 // from being used when sharding was enabled, as some annotation processors
1271 // do not function correctly in sharded environments. It was removed to
1272 // allow for the use of annotation processors that do function correctly
1273 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001274 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001275 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001276 if ctx.Failed() {
1277 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001278 }
1279 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001280 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001281 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001282 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001283 // If error-prone is enabled, add an additional rule to compile the java files into
1284 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001285 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001286 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1287 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001288 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001289 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001290 extraJarDeps = append(extraJarDeps, errorprone)
1291 }
1292
Nan Zhang61eaedb2017-11-02 13:28:15 -07001293 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001294 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001295 shardSize := int(*(j.properties.Javac_shard_size))
1296 var shardSrcs []android.Paths
1297 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001298 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001299 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001300 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1301 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001302 jars = append(jars, classes)
1303 }
1304 }
1305 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001306 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1307 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001308 jars = append(jars, classes)
1309 }
1310 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001311 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001312 jars = append(jars, classes)
1313 }
Colin Crossd6891432017-09-27 17:39:56 -07001314 if ctx.Failed() {
1315 return
1316 }
Colin Cross2fe66872015-03-30 17:20:39 -07001317 }
1318
Colin Cross0c4ce212019-05-03 15:28:19 -07001319 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1320
1321 var includeSrcJar android.WritablePath
1322 if Bool(j.properties.Include_srcs) {
1323 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1324 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1325 }
1326
Colin Crosscedd4762018-09-13 11:26:19 -07001327 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1328 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001329 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001330 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001331
1332 var resArgs []string
1333 var resDeps android.Paths
1334
1335 resArgs = append(resArgs, dirArgs...)
1336 resDeps = append(resDeps, dirDeps...)
1337
1338 resArgs = append(resArgs, fileArgs...)
1339 resDeps = append(resDeps, fileDeps...)
1340
Colin Cross988708c2019-05-06 14:04:11 -07001341 resArgs = append(resArgs, extraArgs...)
1342 resDeps = append(resDeps, extraDeps...)
1343
Colin Cross40a36712017-09-27 17:41:35 -07001344 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001345 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001346 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001347 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001348 if ctx.Failed() {
1349 return
1350 }
1351 }
1352
Colin Cross0c4ce212019-05-03 15:28:19 -07001353 var resourceJars android.Paths
1354 if j.resourceJar != nil {
1355 resourceJars = append(resourceJars, j.resourceJar)
1356 }
1357 if Bool(j.properties.Include_srcs) {
1358 resourceJars = append(resourceJars, includeSrcJar)
1359 }
1360 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001361
Colin Cross0c4ce212019-05-03 15:28:19 -07001362 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001363 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001364 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001365 false, nil, nil)
1366 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001367 } else if len(resourceJars) == 1 {
1368 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001369 }
1370
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001371 if len(deps.staticJars) > 0 {
1372 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001373 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001374
Colin Cross094054a2018-10-17 15:10:48 -07001375 manifest := j.overrideManifest
1376 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001377 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001378 }
Colin Cross635acc92017-09-12 22:50:46 -07001379
Colin Cross8a497952019-03-05 22:25:09 -08001380 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001381 if len(services) > 0 {
1382 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1383 var zipargs []string
1384 for _, file := range services {
1385 serviceFile := file.String()
1386 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1387 }
1388 ctx.Build(pctx, android.BuildParams{
1389 Rule: zip,
1390 Output: servicesJar,
1391 Implicits: services,
1392 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001393 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001394 },
1395 })
1396 jars = append(jars, servicesJar)
1397 }
1398
Colin Cross0a6e0072017-08-30 14:24:55 -07001399 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001400 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001401 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001402
1403 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001404 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1405 // Optimization: skip the combine step if there is nothing to do
1406 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1407 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1408 // any if len(jars) == 1.
1409 outputFile = moduleOutPath
1410 } else {
1411 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1412 ctx.Build(pctx, android.BuildParams{
1413 Rule: android.Cp,
1414 Input: jars[0],
1415 Output: combinedJar,
1416 })
1417 outputFile = combinedJar
1418 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001419 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001420 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001421 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001422 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001423 outputFile = combinedJar
1424 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001425
Colin Cross331a1212018-08-15 20:40:52 -07001426 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001427 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001428 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001429 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001430 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001431 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001432
1433 // jarjar resource jar if necessary
1434 if j.resourceJar != nil {
1435 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001436 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001437 j.resourceJar = resourceJarJarFile
1438 }
1439
Colin Cross0a6e0072017-08-30 14:24:55 -07001440 if ctx.Failed() {
1441 return
1442 }
1443 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001444
1445 // Check package restrictions if necessary.
1446 if len(j.properties.Permitted_packages) > 0 {
1447 // Check packages and copy to package-checked file.
1448 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1449 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1450 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1451
1452 if ctx.Failed() {
1453 return
1454 }
1455 }
1456
Nan Zhanged19fc32017-10-19 13:06:22 -07001457 j.implementationJarFile = outputFile
1458 if j.headerJarFile == nil {
1459 j.headerJarFile = j.implementationJarFile
1460 }
Colin Cross2fe66872015-03-30 17:20:39 -07001461
Jiyong Park33b66542020-02-12 10:39:32 +09001462 // Force enable the instrumentation for java code that is built for APEXes
1463 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1464 j.properties.Instrument = true
1465 }
1466
Colin Cross3144dfc2018-01-03 15:06:47 -08001467 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001468 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1469 }
1470
Colin Cross331a1212018-08-15 20:40:52 -07001471 // merge implementation jar with resources if necessary
1472 implementationAndResourcesJar := outputFile
1473 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001474 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001475 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001476 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001477 false, nil, nil)
1478 implementationAndResourcesJar = combinedJar
1479 }
1480
1481 j.implementationAndResourcesJar = implementationAndResourcesJar
1482
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001483 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001484 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001485 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001486 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001487 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001488 if ctx.Failed() {
1489 return
1490 }
Colin Cross331a1212018-08-15 20:40:52 -07001491
Jiyong Park09cb6292019-07-15 15:29:23 +09001492 // Hidden API CSV generation and dex encoding
1493 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1494 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001495
Colin Cross331a1212018-08-15 20:40:52 -07001496 // merge dex jar with resources if necessary
1497 if j.resourceJar != nil {
1498 jars := android.Paths{dexOutputFile, j.resourceJar}
1499 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1500 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1501 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001502 if j.deviceProperties.UncompressDex {
1503 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1504 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1505 dexOutputFile = combinedAlignedJar
1506 } else {
1507 dexOutputFile = combinedJar
1508 }
Colin Cross331a1212018-08-15 20:40:52 -07001509 }
1510
1511 j.dexJarFile = dexOutputFile
1512
Colin Cross8faf8fc2019-01-16 15:15:52 -08001513 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001514 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1515
1516 j.maybeStrippedDexJarFile = dexOutputFile
1517
Colin Cross3063b782018-08-15 11:19:12 -07001518 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001519
1520 if ctx.Failed() {
1521 return
1522 }
Colin Cross331a1212018-08-15 20:40:52 -07001523 } else {
1524 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001525 }
Colin Cross331a1212018-08-15 20:40:52 -07001526
Colin Crossb7a63242015-04-16 14:09:14 -07001527 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001528
1529 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1530 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001531}
1532
Colin Cross3b706fd2019-09-05 16:44:18 -07001533func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1534 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1535
1536 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1537 if idx >= 0 {
1538 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1539 jarName += strconv.Itoa(idx)
1540 }
1541
1542 classes := android.PathForModuleOut(ctx, "javac", jarName)
1543 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1544
1545 if ctx.Config().EmitXrefRules() {
1546 extractionFile := android.PathForModuleOut(ctx, kzipName)
1547 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1548 j.kytheFiles = append(j.kytheFiles, extractionFile)
1549 }
1550
1551 return classes
1552}
1553
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001554// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1555// since some of these flags may be used internally.
1556func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1557 for _, flag := range flags {
1558 flag = strings.TrimSpace(flag)
1559
1560 if !strings.HasPrefix(flag, "-") {
1561 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1562 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1563 ctx.PropertyErrorf("kotlincflags",
1564 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1565 } else if inList(flag, config.KotlincIllegalFlags) {
1566 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1567 } else if flag == "-include-runtime" {
1568 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1569 } else {
1570 args := strings.Split(flag, " ")
1571 if args[0] == "-kotlin-home" {
1572 ctx.PropertyErrorf("kotlincflags",
1573 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1574 }
1575 }
1576 }
1577}
1578
Colin Cross8eadbf02017-10-24 17:46:00 -07001579func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001580 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001581
1582 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001583 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001584 // Compile java sources into turbine.jar.
1585 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1586 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1587 if ctx.Failed() {
1588 return nil
1589 }
1590 jars = append(jars, turbineJar)
1591 }
1592
Colin Cross55f63ea2018-08-27 12:37:09 -07001593 jars = append(jars, extraJars...)
1594
Nan Zhanged19fc32017-10-19 13:06:22 -07001595 // Combine any static header libraries into classes-header.jar. If there is only
1596 // one input jar this step will be skipped.
1597 var headerJar android.Path
1598 jars = append(jars, deps.staticHeaderJars...)
1599
Colin Cross5c6ecc12017-10-23 18:12:27 -07001600 // we cannot skip the combine step for now if there is only one jar
1601 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1602 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001603 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001604 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001605 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001606
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001607 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001608 // Transform classes.jar into classes-jarjar.jar
1609 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001610 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001611 headerJar = jarjarFile
1612 if ctx.Failed() {
1613 return nil
1614 }
1615 }
1616
1617 return headerJar
1618}
1619
Colin Crosscb933592017-11-22 13:49:43 -08001620func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001621 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001622
Colin Cross7a3139e2017-12-19 13:57:50 -08001623 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001624
Colin Cross84c38822018-01-03 15:59:46 -08001625 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001626 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1627
1628 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1629
1630 j.jacocoReportClassesFile = jacocoReportClassesFile
1631
1632 return instrumentedJar
1633}
1634
albaltai36ff7dc2018-12-25 14:35:23 +08001635var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001636
Nan Zhanged19fc32017-10-19 13:06:22 -07001637func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001638 if j.headerJarFile == nil {
1639 return nil
1640 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001641 return android.Paths{j.headerJarFile}
1642}
1643
1644func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001645 if j.implementationJarFile == nil {
1646 return nil
1647 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001648 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001649}
1650
Colin Crossf24a22a2019-01-31 14:12:44 -08001651func (j *Module) DexJar() android.Path {
1652 return j.dexJarFile
1653}
1654
Colin Cross331a1212018-08-15 20:40:52 -07001655func (j *Module) ResourceJars() android.Paths {
1656 if j.resourceJar == nil {
1657 return nil
1658 }
1659 return android.Paths{j.resourceJar}
1660}
1661
1662func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001663 if j.implementationAndResourcesJar == nil {
1664 return nil
1665 }
Colin Cross331a1212018-08-15 20:40:52 -07001666 return android.Paths{j.implementationAndResourcesJar}
1667}
1668
Colin Cross46c9b8b2017-06-22 16:51:17 -07001669func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001670 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001671 return j.exportAidlIncludeDirs
1672}
1673
Jiyong Park1be96912018-05-28 18:02:19 +09001674func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001675 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001676 return j.exportedSdkLibs
1677}
1678
Artur Satayev9cf46692019-11-26 18:08:34 +00001679func (j *Module) ExportedPlugins() (android.Paths, []string) {
1680 return j.exportedPluginJars, j.exportedPluginClasses
1681}
1682
Colin Cross0c4ce212019-05-03 15:28:19 -07001683func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1684 return j.srcJarArgs, j.srcJarDeps
1685}
1686
Colin Cross46c9b8b2017-06-22 16:51:17 -07001687var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001688
Colin Cross46c9b8b2017-06-22 16:51:17 -07001689func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001690 return j.logtagsSrcs
1691}
1692
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001693// Collect information for opening IDE project files in java/jdeps.go.
1694func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1695 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1696 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001697 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001698 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001699 if j.expandJarjarRules != nil {
1700 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001701 }
1702}
1703
1704func (j *Module) CompilerDeps() []string {
1705 jdeps := []string{}
1706 jdeps = append(jdeps, j.properties.Libs...)
1707 jdeps = append(jdeps, j.properties.Static_libs...)
1708 return jdeps
1709}
1710
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001711func (j *Module) hasCode(ctx android.ModuleContext) bool {
1712 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1713 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1714}
1715
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001716func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1717 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001718 // Dependencies other than the static linkage are all considered crossing APEX boundary
1719 // Also, a dependency to an sdk member is also considered as such. This is required because
1720 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1721 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001722}
1723
Jiyong Park0b238752019-10-29 11:23:10 +09001724func (j *Module) Stem() string {
1725 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1726}
1727
Jiyong Park618922e2020-01-08 13:35:43 +09001728func (j *Module) JacocoReportClassesFile() android.Path {
1729 return j.jacocoReportClassesFile
1730}
1731
Colin Cross2fe66872015-03-30 17:20:39 -07001732//
1733// Java libraries (.jar file)
1734//
1735
Colin Crossf506d872017-07-19 15:53:04 -07001736type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001737 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001738
1739 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001740}
1741
Colin Cross42be7612019-02-21 18:12:14 -08001742func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001743 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1744 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1745 return true
1746 }
1747
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001748 // Store uncompressed (and do not strip) dex files from boot class path jars.
1749 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1750 return true
1751 }
1752
1753 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001754 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001755 return true
1756 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001757 if ctx.Config().UncompressPrivAppDex() &&
1758 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1759 return true
1760 }
1761
Colin Cross2fc72f62018-12-21 12:59:54 -08001762 return false
1763}
1764
Colin Crossf506d872017-07-19 15:53:04 -07001765func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001766 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001767 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001768 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001769 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001770 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001771 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001772 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001773
Jiyong Park7f7766d2019-07-25 22:02:35 +09001774 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1775 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001776 var extraInstallDeps android.Paths
1777 if j.InstallMixin != nil {
1778 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1779 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001780 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001781 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001782 }
Colin Crossb7a63242015-04-16 14:09:14 -07001783}
1784
Colin Crossf506d872017-07-19 15:53:04 -07001785func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001786 j.deps(ctx)
1787}
1788
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001789const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001790 aidlIncludeDir = "aidl"
1791 javaDir = "java"
1792 jarFileSuffix = ".jar"
1793 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001794)
1795
Paul Duffina0dbf432019-12-05 11:25:53 +00001796// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001797func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1798 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1799}
1800
1801func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1802 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001803}
1804
Paul Duffin13879572019-11-28 14:31:38 +00001805type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001806 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001807}
1808
1809func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1810 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1811}
1812
1813func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1814 _, ok := module.(*Library)
1815 return ok
1816}
1817
Paul Duffina0dbf432019-12-05 11:25:53 +00001818func (mt *librarySdkMemberType) buildSnapshot(
1819 sdkModuleContext android.ModuleContext,
1820 builder android.SnapshotBuilder,
1821 member android.SdkMember,
1822 jarToExportGetter func(j *Library) android.Path) {
1823
Paul Duffin13879572019-11-28 14:31:38 +00001824 variants := member.Variants()
1825 if len(variants) != 1 {
1826 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1827 for _, variant := range variants {
1828 sdkModuleContext.ModuleErrorf(" %q", variant)
1829 }
1830 }
1831 variant := variants[0]
1832 j := variant.(*Library)
1833
Paul Duffina0dbf432019-12-05 11:25:53 +00001834 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001835 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001836 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001837
1838 for _, dir := range j.AidlIncludeDirs() {
1839 // TODO(jiyong): copy parcelable declarations only
1840 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1841 for _, file := range aidlFiles {
1842 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1843 }
1844 }
1845
Paul Duffin9d8d6092019-12-05 18:19:29 +00001846 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001847 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001848}
1849
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001850var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1851 librarySdkMemberType{
1852 android.SdkMemberTypeBase{
1853 PropertyName: "java_header_libs",
1854 SupportsSdk: true,
1855 },
1856 },
1857}
1858
Paul Duffina0dbf432019-12-05 11:25:53 +00001859type headerLibrarySdkMemberType struct {
1860 librarySdkMemberType
1861}
1862
1863func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1864 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1865 headerJars := j.HeaderJars()
1866 if len(headerJars) != 1 {
1867 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1868 }
1869
1870 return headerJars[0]
1871 })
1872}
1873
Paul Duffina0dbf432019-12-05 11:25:53 +00001874type implLibrarySdkMemberType struct {
1875 librarySdkMemberType
1876}
1877
1878func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1879 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1880 implementationJars := j.ImplementationJars()
1881 if len(implementationJars) != 1 {
1882 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1883 }
1884
1885 return implementationJars[0]
1886 })
1887}
1888
Colin Cross1b16b0e2019-02-12 14:41:32 -08001889// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1890//
1891// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1892// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1893// as a `static_libs` dependency of another module.
1894//
1895// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1896// a device.
1897//
1898// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1899// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001900func LibraryFactory() android.Module {
1901 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001902
Colin Cross9ae1b922018-06-26 17:59:05 -07001903 module.AddProperties(
1904 &module.Module.properties,
1905 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001906 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001907 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001908
Jiyong Park7f7766d2019-07-25 22:02:35 +09001909 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001910 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001911 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001912 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001913}
1914
Colin Cross1b16b0e2019-02-12 14:41:32 -08001915// java_library_static is an obsolete alias for java_library.
1916func LibraryStaticFactory() android.Module {
1917 return LibraryFactory()
1918}
1919
1920// java_library_host builds and links sources into a `.jar` file for the host.
1921//
1922// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1923// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001924func LibraryHostFactory() android.Module {
1925 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001926
Colin Cross6af17aa2017-09-20 12:59:05 -07001927 module.AddProperties(
1928 &module.Module.properties,
1929 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001930
Colin Cross9ae1b922018-06-26 17:59:05 -07001931 module.Module.properties.Installable = proptools.BoolPtr(true)
1932
Jiyong Park7f7766d2019-07-25 22:02:35 +09001933 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001934 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001935 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001936}
1937
1938//
Colin Crossb628ea52018-08-14 16:42:33 -07001939// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001940//
1941
1942type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001943 // list of compatibility suites (for example "cts", "vts") that the module should be
1944 // installed into.
1945 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001946
1947 // the name of the test configuration (for example "AndroidTest.xml") that should be
1948 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001949 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001950
Jack He33338892018-09-19 02:21:28 -07001951 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1952 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001953 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001954
Colin Crossd96ca352018-08-10 16:06:24 -07001955 // list of files or filegroup modules that provide data that should be installed alongside
1956 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001957 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001958
1959 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1960 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1961 // explicitly.
1962 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001963}
1964
Paul Duffin42df1442019-03-20 12:45:53 +00001965type testHelperLibraryProperties struct {
1966 // list of compatibility suites (for example "cts", "vts") that the module should be
1967 // installed into.
1968 Test_suites []string `android:"arch_variant"`
1969}
1970
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001971type prebuiltTestProperties struct {
1972 // list of compatibility suites (for example "cts", "vts") that the module should be
1973 // installed into.
1974 Test_suites []string `android:"arch_variant"`
1975
1976 // the name of the test configuration (for example "AndroidTest.xml") that should be
1977 // installed with the module.
1978 Test_config *string `android:"path,arch_variant"`
1979}
1980
Colin Cross05638fc2018-04-09 18:40:24 -07001981type Test struct {
1982 Library
1983
1984 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001985
1986 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001987 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001988}
1989
Paul Duffin42df1442019-03-20 12:45:53 +00001990type TestHelperLibrary struct {
1991 Library
1992
1993 testHelperLibraryProperties testHelperLibraryProperties
1994}
1995
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001996type JavaTestImport struct {
1997 Import
1998
1999 prebuiltTestProperties prebuiltTestProperties
2000
2001 testConfig android.Path
2002}
2003
Colin Cross303e21f2018-08-07 16:49:25 -07002004func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002005 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2006 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002007 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002008
2009 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002010}
2011
Paul Duffin42df1442019-03-20 12:45:53 +00002012func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2013 j.Library.GenerateAndroidBuildActions(ctx)
2014}
2015
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002016func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2017 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2018 j.prebuiltTestProperties.Test_suites, nil)
2019
2020 j.Import.GenerateAndroidBuildActions(ctx)
2021}
2022
2023type testSdkMemberType struct {
2024 android.SdkMemberTypeBase
2025}
2026
2027func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2028 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2029}
2030
2031func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2032 _, ok := module.(*Test)
2033 return ok
2034}
2035
2036func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2037 variants := member.Variants()
2038 if len(variants) != 1 {
2039 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2040 for _, variant := range variants {
2041 sdkModuleContext.ModuleErrorf(" %q", variant)
2042 }
2043 }
2044 variant := variants[0]
2045 j := variant.(*Test)
2046
2047 implementationJars := j.ImplementationJars()
2048 if len(implementationJars) != 1 {
2049 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2050 }
2051
2052 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2053 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2054
2055 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2056 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2057
2058 module := builder.AddPrebuiltModule(member, "java_test_import")
2059 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2060 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2061}
2062
Colin Cross1b16b0e2019-02-12 14:41:32 -08002063// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2064// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2065//
2066// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2067// compiled against the device bootclasspath.
2068//
2069// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2070// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002071func TestFactory() android.Module {
2072 module := &Test{}
2073
2074 module.AddProperties(
2075 &module.Module.properties,
2076 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002077 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002078 &module.Module.protoProperties,
2079 &module.testProperties)
2080
Colin Cross9ae1b922018-06-26 17:59:05 -07002081 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002082 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002083
Colin Cross05638fc2018-04-09 18:40:24 -07002084 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002085 return module
2086}
2087
Paul Duffin42df1442019-03-20 12:45:53 +00002088// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2089func TestHelperLibraryFactory() android.Module {
2090 module := &TestHelperLibrary{}
2091
2092 module.AddProperties(
2093 &module.Module.properties,
2094 &module.Module.deviceProperties,
2095 &module.Module.dexpreoptProperties,
2096 &module.Module.protoProperties,
2097 &module.testHelperLibraryProperties)
2098
Colin Cross9a4abed2019-04-24 13:19:28 -07002099 module.Module.properties.Installable = proptools.BoolPtr(true)
2100 module.Module.dexpreopter.isTest = true
2101
Paul Duffin42df1442019-03-20 12:45:53 +00002102 InitJavaModule(module, android.HostAndDeviceSupported)
2103 return module
2104}
2105
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002106// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2107// and makes sure that it is added to the appropriate test suite.
2108//
2109// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2110// compiled against an Android classpath.
2111//
2112// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2113// for host modules.
2114func JavaTestImportFactory() android.Module {
2115 module := &JavaTestImport{}
2116
2117 module.AddProperties(
2118 &module.Import.properties,
2119 &module.prebuiltTestProperties)
2120
2121 module.Import.properties.Installable = proptools.BoolPtr(true)
2122
2123 android.InitPrebuiltModule(module, &module.properties.Jars)
2124 android.InitApexModule(module)
2125 android.InitSdkAwareModule(module)
2126 InitJavaModule(module, android.HostAndDeviceSupported)
2127 return module
2128}
2129
Colin Cross1b16b0e2019-02-12 14:41:32 -08002130// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2131// allow running the test with `atest` or a `TEST_MAPPING` file.
2132//
2133// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2134// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002135func TestHostFactory() android.Module {
2136 module := &Test{}
2137
2138 module.AddProperties(
2139 &module.Module.properties,
2140 &module.Module.protoProperties,
2141 &module.testProperties)
2142
Colin Cross9ae1b922018-06-26 17:59:05 -07002143 module.Module.properties.Installable = proptools.BoolPtr(true)
2144
Colin Cross05638fc2018-04-09 18:40:24 -07002145 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002146 return module
2147}
2148
2149//
Colin Cross2fe66872015-03-30 17:20:39 -07002150// Java Binaries (.jar file plus wrapper script)
2151//
2152
Colin Crossf506d872017-07-19 15:53:04 -07002153type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002154 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002155 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002156
2157 // Name of the class containing main to be inserted into the manifest as Main-Class.
2158 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002159}
2160
Colin Crossf506d872017-07-19 15:53:04 -07002161type Binary struct {
2162 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002163
Colin Crossf506d872017-07-19 15:53:04 -07002164 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002165
Colin Cross6b4a32d2017-12-05 13:42:45 -08002166 isWrapperVariant bool
2167
Colin Crossc3315992017-12-08 19:12:36 -08002168 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002169 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002170}
2171
Alex Light24237172017-10-26 09:46:21 -07002172func (j *Binary) HostToolPath() android.OptionalPath {
2173 return android.OptionalPathForPath(j.binaryFile)
2174}
2175
Colin Crossf506d872017-07-19 15:53:04 -07002176func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002177 if ctx.Arch().ArchType == android.Common {
2178 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002179 if j.binaryProperties.Main_class != nil {
2180 if j.properties.Manifest != nil {
2181 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2182 }
2183 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2184 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2185 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2186 }
2187
Colin Cross6b4a32d2017-12-05 13:42:45 -08002188 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002189 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002190 // Handle the binary wrapper
2191 j.isWrapperVariant = true
2192
Colin Cross366938f2017-12-11 16:29:02 -08002193 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002194 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002195 } else {
2196 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2197 }
2198
2199 // Depend on the installed jar so that the wrapper doesn't get executed by
2200 // another build rule before the jar has been installed.
2201 jarFile := ctx.PrimaryModule().(*Binary).installFile
2202
2203 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2204 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002205 }
Colin Cross2fe66872015-03-30 17:20:39 -07002206}
2207
Colin Crossf506d872017-07-19 15:53:04 -07002208func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002209 if ctx.Arch().ArchType == android.Common {
2210 j.deps(ctx)
2211 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002212}
2213
Colin Cross1b16b0e2019-02-12 14:41:32 -08002214// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2215// as well.
2216//
2217// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2218// compiled against the device bootclasspath.
2219//
2220// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2221// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002222func BinaryFactory() android.Module {
2223 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002224
Colin Cross36242852017-06-23 15:06:31 -07002225 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002226 &module.Module.properties,
2227 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002228 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002229 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002230 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002231
Colin Cross9ae1b922018-06-26 17:59:05 -07002232 module.Module.properties.Installable = proptools.BoolPtr(true)
2233
Colin Cross6b4a32d2017-12-05 13:42:45 -08002234 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2235 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002236 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002237}
2238
Colin Cross1b16b0e2019-02-12 14:41:32 -08002239// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2240//
2241// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2242// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002243func BinaryHostFactory() android.Module {
2244 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002245
Colin Cross36242852017-06-23 15:06:31 -07002246 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002247 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002248 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002249 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002250
Colin Cross9ae1b922018-06-26 17:59:05 -07002251 module.Module.properties.Installable = proptools.BoolPtr(true)
2252
Colin Cross6b4a32d2017-12-05 13:42:45 -08002253 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2254 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002255 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002256}
2257
2258//
2259// Java prebuilts
2260//
2261
Colin Cross74d73e22017-08-02 11:05:49 -07002262type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002263 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002264
Nan Zhangea568a42017-11-08 21:20:04 -08002265 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002266
2267 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002268
2269 // List of shared java libs that this module has dependencies to
2270 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002271
2272 // List of files to remove from the jar file(s)
2273 Exclude_files []string
2274
2275 // List of directories to remove from the jar file(s)
2276 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002277
2278 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002279 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002280
2281 // set the name of the output
2282 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002283}
2284
2285type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002286 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002287 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002288 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002289 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002290 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002291
Colin Cross74d73e22017-08-02 11:05:49 -07002292 properties ImportProperties
2293
Colin Cross0a6e0072017-08-30 14:24:55 -07002294 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002295 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002296}
2297
Jiyong Park6a927c42020-01-21 02:03:43 +09002298func (j *Import) sdkVersion() sdkSpec {
2299 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002300}
2301
Jiyong Park6a927c42020-01-21 02:03:43 +09002302func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002303 return j.sdkVersion()
2304}
2305
Colin Cross74d73e22017-08-02 11:05:49 -07002306func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002307 return &j.prebuilt
2308}
2309
Colin Cross74d73e22017-08-02 11:05:49 -07002310func (j *Import) PrebuiltSrcs() []string {
2311 return j.properties.Jars
2312}
2313
2314func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002315 return j.prebuilt.Name(j.ModuleBase.Name())
2316}
2317
Jiyong Park0b238752019-10-29 11:23:10 +09002318func (j *Import) Stem() string {
2319 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2320}
2321
Jiyong Park618922e2020-01-08 13:35:43 +09002322func (a *Import) JacocoReportClassesFile() android.Path {
2323 return nil
2324}
2325
Colin Cross74d73e22017-08-02 11:05:49 -07002326func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002327 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002328}
2329
Colin Cross74d73e22017-08-02 11:05:49 -07002330func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002331 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002332
Jiyong Park0b238752019-10-29 11:23:10 +09002333 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002334 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002335 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2336 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002337 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002338 inputFile := outputFile
2339 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2340 TransformJetifier(ctx, outputFile, inputFile)
2341 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002342 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002343
2344 ctx.VisitDirectDeps(func(module android.Module) {
2345 otherName := ctx.OtherModuleName(module)
2346 tag := ctx.OtherModuleDependencyTag(module)
2347
2348 switch dep := module.(type) {
2349 case Dependency:
2350 switch tag {
2351 case libTag, staticLibTag:
2352 // sdk lib names from dependencies are re-exported
2353 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2354 }
2355 case SdkLibraryDependency:
2356 switch tag {
2357 case libTag:
2358 // names of sdk libs that are directly depended are exported
2359 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2360 }
2361 }
2362 })
2363
2364 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002365 if Bool(j.properties.Installable) {
2366 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002367 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002368 }
Colin Cross2fe66872015-03-30 17:20:39 -07002369}
2370
Colin Cross74d73e22017-08-02 11:05:49 -07002371var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002372
Nan Zhanged19fc32017-10-19 13:06:22 -07002373func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002374 if j.combinedClasspathFile == nil {
2375 return nil
2376 }
Colin Cross37f6d792018-07-12 12:28:41 -07002377 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002378}
2379
2380func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002381 if j.combinedClasspathFile == nil {
2382 return nil
2383 }
Colin Cross37f6d792018-07-12 12:28:41 -07002384 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002385}
2386
Colin Cross331a1212018-08-15 20:40:52 -07002387func (j *Import) ResourceJars() android.Paths {
2388 return nil
2389}
2390
2391func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002392 if j.combinedClasspathFile == nil {
2393 return nil
2394 }
Colin Cross331a1212018-08-15 20:40:52 -07002395 return android.Paths{j.combinedClasspathFile}
2396}
2397
Colin Crossf24a22a2019-01-31 14:12:44 -08002398func (j *Import) DexJar() android.Path {
2399 return nil
2400}
2401
Colin Cross74d73e22017-08-02 11:05:49 -07002402func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002403 return nil
2404}
2405
Jiyong Park1be96912018-05-28 18:02:19 +09002406func (j *Import) ExportedSdkLibs() []string {
2407 return j.exportedSdkLibs
2408}
2409
Artur Satayev9cf46692019-11-26 18:08:34 +00002410func (j *Import) ExportedPlugins() (android.Paths, []string) {
2411 return nil, nil
2412}
2413
Colin Cross0c4ce212019-05-03 15:28:19 -07002414func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2415 return nil, nil
2416}
2417
Jiyong Park0f80c182020-01-31 02:49:53 +09002418func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2419 depTag := ctx.OtherModuleDependencyTag(dep)
2420 // dependencies other than the static linkage are all considered crossing APEX boundary
2421 // Also, a dependency to an sdk member is also considered as such. This is required because
2422 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2423 return depTag == staticLibTag || j.IsInAnySdk()
2424}
2425
albaltai36ff7dc2018-12-25 14:35:23 +08002426// Add compile time check for interface implementation
2427var _ android.IDEInfo = (*Import)(nil)
2428var _ android.IDECustomizedModuleName = (*Import)(nil)
2429
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002430// Collect information for opening IDE project files in java/jdeps.go.
2431const (
2432 removedPrefix = "prebuilt_"
2433)
2434
2435func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2436 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2437}
2438
2439func (j *Import) IDECustomizedModuleName() string {
2440 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2441 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2442 // solution to get the Import name.
2443 name := j.Name()
2444 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002445 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002446 }
2447 return name
2448}
2449
Colin Cross74d73e22017-08-02 11:05:49 -07002450var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002451
Colin Cross1b16b0e2019-02-12 14:41:32 -08002452// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2453//
2454// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2455// compiled against an Android classpath.
2456//
2457// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2458// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002459func ImportFactory() android.Module {
2460 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002461
Colin Cross74d73e22017-08-02 11:05:49 -07002462 module.AddProperties(&module.properties)
2463
2464 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002465 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002466 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002467 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002468 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002469}
2470
Colin Cross1b16b0e2019-02-12 14:41:32 -08002471// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2472// module.
2473//
2474// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2475// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002476func ImportFactoryHost() android.Module {
2477 module := &Import{}
2478
2479 module.AddProperties(&module.properties)
2480
2481 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002482 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002483 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002484 return module
2485}
2486
Colin Cross42be7612019-02-21 18:12:14 -08002487// dex_import module
2488
2489type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002490 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002491
2492 // set the name of the output
2493 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002494}
2495
2496type DexImport struct {
2497 android.ModuleBase
2498 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002499 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002500 prebuilt android.Prebuilt
2501
2502 properties DexImportProperties
2503
2504 dexJarFile android.Path
2505 maybeStrippedDexJarFile android.Path
2506
2507 dexpreopter
2508}
2509
2510func (j *DexImport) Prebuilt() *android.Prebuilt {
2511 return &j.prebuilt
2512}
2513
2514func (j *DexImport) PrebuiltSrcs() []string {
2515 return j.properties.Jars
2516}
2517
2518func (j *DexImport) Name() string {
2519 return j.prebuilt.Name(j.ModuleBase.Name())
2520}
2521
Jiyong Park0b238752019-10-29 11:23:10 +09002522func (j *DexImport) Stem() string {
2523 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2524}
2525
Colin Cross42be7612019-02-21 18:12:14 -08002526func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2527 if len(j.properties.Jars) != 1 {
2528 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2529 }
2530
Jiyong Park0b238752019-10-29 11:23:10 +09002531 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002532 j.dexpreopter.isInstallable = true
2533 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2534
2535 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2536 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2537
2538 if j.dexpreopter.uncompressedDex {
2539 rule := android.NewRuleBuilder()
2540
2541 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2542 rule.Temporary(temporary)
2543
2544 // use zip2zip to uncompress classes*.dex files
2545 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002546 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002547 FlagWithInput("-i ", inputJar).
2548 FlagWithOutput("-o ", temporary).
2549 FlagWithArg("-0 ", "'classes*.dex'")
2550
2551 // use zipalign to align uncompressed classes*.dex files
2552 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002553 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002554 Flag("-f").
2555 Text("4").
2556 Input(temporary).
2557 Output(dexOutputFile)
2558
2559 rule.DeleteTemporaryFiles()
2560
2561 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2562 } else {
2563 ctx.Build(pctx, android.BuildParams{
2564 Rule: android.Cp,
2565 Input: inputJar,
2566 Output: dexOutputFile,
2567 })
2568 }
2569
2570 j.dexJarFile = dexOutputFile
2571
2572 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2573
2574 j.maybeStrippedDexJarFile = dexOutputFile
2575
2576 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2577 ctx.ModuleName()+".jar", dexOutputFile)
2578}
2579
2580func (j *DexImport) DexJar() android.Path {
2581 return j.dexJarFile
2582}
2583
2584// dex_import imports a `.jar` file containing classes.dex files.
2585//
2586// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2587// to the device.
2588func DexImportFactory() android.Module {
2589 module := &DexImport{}
2590
2591 module.AddProperties(&module.properties)
2592
2593 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002594 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002595 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002596 return module
2597}
2598
Colin Cross89536d42017-07-07 14:35:50 -07002599//
2600// Defaults
2601//
2602type Defaults struct {
2603 android.ModuleBase
2604 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002605 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002606}
2607
Colin Cross1b16b0e2019-02-12 14:41:32 -08002608// java_defaults provides a set of properties that can be inherited by other java or android modules.
2609//
2610// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2611// property in the defaults module that exists in the depending module will be prepended to the depending module's
2612// value for that property.
2613//
2614// Example:
2615//
2616// java_defaults {
2617// name: "example_defaults",
2618// srcs: ["common/**/*.java"],
2619// javacflags: ["-Xlint:all"],
2620// aaptflags: ["--auto-add-overlay"],
2621// }
2622//
2623// java_library {
2624// name: "example",
2625// defaults: ["example_defaults"],
2626// srcs: ["example/**/*.java"],
2627// }
2628//
2629// is functionally identical to:
2630//
2631// java_library {
2632// name: "example",
2633// srcs: [
2634// "common/**/*.java",
2635// "example/**/*.java",
2636// ],
2637// javacflags: ["-Xlint:all"],
2638// }
Colin Cross89536d42017-07-07 14:35:50 -07002639func defaultsFactory() android.Module {
2640 return DefaultsFactory()
2641}
2642
Paul Duffin47357662019-12-05 14:07:14 +00002643func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002644 module := &Defaults{}
2645
Colin Cross89536d42017-07-07 14:35:50 -07002646 module.AddProperties(
2647 &CompilerProperties{},
2648 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002649 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002650 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002651 &aaptProperties{},
2652 &androidLibraryProperties{},
2653 &appProperties{},
2654 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002655 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002656 &ImportProperties{},
2657 &AARImportProperties{},
2658 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002659 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002660 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002661 )
2662
2663 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002664 return module
2665}
Nan Zhangea568a42017-11-08 21:20:04 -08002666
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002667func kytheExtractJavaFactory() android.Singleton {
2668 return &kytheExtractJavaSingleton{}
2669}
2670
2671type kytheExtractJavaSingleton struct {
2672}
2673
2674func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2675 var xrefTargets android.Paths
2676 ctx.VisitAllModules(func(module android.Module) {
2677 if javaModule, ok := module.(xref); ok {
2678 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2679 }
2680 })
2681 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2682 if len(xrefTargets) > 0 {
2683 ctx.Build(pctx, android.BuildParams{
2684 Rule: blueprint.Phony,
2685 Output: android.PathForPhony(ctx, "xref_java"),
2686 Inputs: xrefTargets,
2687 })
2688 }
2689}
2690
Nan Zhangea568a42017-11-08 21:20:04 -08002691var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002692var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002693var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002694var inList = android.InList