blob: 55ad8b019116b0d8c8e97e96edb8a6574efd0948 [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
Martin Stjernholm6d415272020-01-31 17:10:36 +000075 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
76 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
77 })
Martin Stjernholmd90676f2020-01-11 00:37:30 +000078
Paul Duffinf9b1da02019-12-18 19:51:55 +000079 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
80 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
81}
82
Jeongik Cha2cc570d2019-10-29 15:44:45 +090083func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
84 if j.SocSpecific() || j.DeviceSpecific() ||
85 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
86 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +090087 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090088 ctx.PropertyErrorf("sdk_version",
89 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
90 }
91 }
92 }
93}
94
Jeongik Cha538c0d02019-07-11 15:54:27 +090095func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
96 if sc, ok := ctx.Module().(sdkContext); ok {
97 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +090098 sdkVersionSpecified := sc.sdkVersion().specified()
99 if usePlatformAPI && sdkVersionSpecified {
100 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
101 } else if !usePlatformAPI && !sdkVersionSpecified {
102 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900103 }
104
105 }
106}
107
Colin Cross2fe66872015-03-30 17:20:39 -0700108// TODO:
109// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700110// Renderscript
111// Post-jar passes:
112// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700113// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700114// DroidDoc
115// Findbugs
116
Colin Cross89536d42017-07-07 14:35:50 -0700117type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700118 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
119 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800120 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700121
122 // list of source files that should not be used to build the Java module.
123 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800124 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700125
126 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700127 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700128
Colin Cross86a63ff2017-09-27 17:33:10 -0700129 // list of directories that should be excluded from java_resource_dirs
130 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700131
Colin Cross0f37af02017-09-27 17:42:05 -0700132 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800133 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700134
Colin Crosscedd4762018-09-13 11:26:19 -0700135 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800136 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700137
Colin Cross7d5136f2015-05-11 13:39:40 -0700138 // list of module-specific flags that will be used for javac compiles
139 Javacflags []string `android:"arch_variant"`
140
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200141 // list of module-specific flags that will be used for kotlinc compiles
142 Kotlincflags []string `android:"arch_variant"`
143
Colin Cross7d5136f2015-05-11 13:39:40 -0700144 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700145 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700146
147 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700148 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700149
150 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800151 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700152
Colin Cross540eff82017-06-22 17:01:52 -0700153 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800154 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700155
156 // If not blank, set the java version passed to javac as -source and -target
157 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700158
Colin Cross9ae1b922018-06-26 17:59:05 -0700159 // If set to true, allow this module to be dexed and installed on devices. Has no
160 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700161 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700162
Colin Cross0f37af02017-09-27 17:42:05 -0700163 // If set to true, include sources used to compile the module in to the final jar
164 Include_srcs *bool
165
Vladimir Marko0975ee02019-04-02 10:29:55 +0100166 // If not empty, classes are restricted to the specified packages and their sub-packages.
167 // This restriction is checked after applying jarjar rules and including static libs.
168 Permitted_packages []string
169
Colin Crossbe9cdb82019-01-21 21:37:16 -0800170 // List of modules to use as annotation processors
171 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700172
Artur Satayev9cf46692019-11-26 18:08:34 +0000173 // List of modules to export to libraries that directly depend on this library as annotation processors
174 Exported_plugins []string
175
Nan Zhang61eaedb2017-11-02 13:28:15 -0700176 // The number of Java source entries each Javac instance can process
177 Javac_shard_size *int64
178
Nan Zhang5f8cb422018-02-06 10:34:32 -0800179 // Add host jdk tools.jar to bootclasspath
180 Use_tools_jar *bool
181
Colin Cross1369cdb2017-09-29 17:58:17 -0700182 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700183 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800184 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700185
Colin Cross6cef4812019-10-17 14:23:50 -0700186 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700187 Javacflags []string
188 }
Colin Crosscb933592017-11-22 13:49:43 -0800189
Colin Cross81440082018-08-15 20:21:55 -0700190 // When compiling language level 9+ .java code in packages that are part of
191 // a system module, patch_module names the module that your sources and
192 // dependencies should be patched into. The Android runtime currently
193 // doesn't implement the JEP 261 module system so this option is only
194 // supported at compile time. It should only be needed to compile tests in
195 // packages that exist in libcore and which are inconvenient to move
196 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100197 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700198
Colin Crosscb933592017-11-22 13:49:43 -0800199 Jacoco struct {
200 // List of classes to include for instrumentation with jacoco to collect coverage
201 // information at runtime when building with coverage enabled. If unset defaults to all
202 // classes.
203 // Supports '*' as the last character of an entry in the list as a wildcard match.
204 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
205 // it matches classes in the package that have the class name as a prefix.
206 Include_filter []string
207
208 // List of classes to exclude from instrumentation with jacoco to collect coverage
209 // information at runtime when building with coverage enabled. Overrides classes selected
210 // by the include_filter property.
211 // Supports '*' as the last character of an entry in the list as a wildcard match.
212 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
213 // it matches classes in the package that have the class name as a prefix.
214 Exclude_filter []string
215 }
216
Andreas Gampef3e5b552018-01-22 21:27:21 -0800217 Errorprone struct {
218 // List of javac flags that should only be used when running errorprone.
219 Javacflags []string
220 }
221
Colin Cross0f2ee152017-12-14 15:22:43 -0800222 Proto struct {
223 // List of extra options that will be passed to the proto generator.
224 Output_params []string
225 }
226
Colin Crosscb933592017-11-22 13:49:43 -0800227 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800228
229 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800230 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700231}
232
Colin Cross89536d42017-07-07 14:35:50 -0700233type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700234 // list of module-specific flags that will be used for dex compiles
235 Dxflags []string `android:"arch_variant"`
236
Jeongik Cha538c0d02019-07-11 15:54:27 +0900237 // if not blank, set to the version of the sdk to compile against.
238 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800239 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700240
Colin Cross83bb3162018-06-25 15:48:06 -0700241 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
242 // Defaults to sdk_version if not set.
243 Min_sdk_version *string
244
Dan Willemsen419290a2018-10-31 15:28:47 -0700245 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
246 // Defaults to sdk_version if not set.
247 Target_sdk_version *string
248
Jeongik Cha356dac42019-08-19 14:09:52 +0900249 // Whether to compile against the platform APIs instead of an SDK.
250 // If true, then sdk_version must be empty. The value of this field
251 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700252 Platform_apis *bool
253
Colin Crossebe1a512017-11-14 13:12:14 -0800254 Aidl struct {
255 // Top level directories to pass to aidl tool
256 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700257
Colin Crossebe1a512017-11-14 13:12:14 -0800258 // Directories rooted at the Android.bp file to pass to aidl tool
259 Local_include_dirs []string
260
261 // directories that should be added as include directories for any aidl sources of modules
262 // that depend on this module, as well as to aidl for this module.
263 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100264
265 // whether to generate traces (for systrace) for this interface
266 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100267
268 // whether to generate Binder#GetTransaction name method.
269 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800270 }
Colin Cross92430102017-10-09 14:59:32 -0700271
272 // If true, export a copy of the module as a -hostdex module for host testing.
273 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700274
Colin Cross7f87f4f2019-04-24 13:41:45 -0700275 Target struct {
276 Hostdex struct {
277 // Additional required dependencies to add to -hostdex modules.
278 Required []string
279 }
280 }
281
David Brazdil17ef5632018-06-27 10:27:45 +0100282 // If set to true, compile dex regardless of installable. Defaults to false.
283 Compile_dex *bool
284
Colin Cross66dbc0b2017-12-28 12:23:20 -0800285 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700286 // If false, disable all optimization. Defaults to true for android_app and android_test
287 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800288 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700289 // True if the module containing this has it set by default.
290 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800291
292 // If true, optimize for size by removing unused code. Defaults to true for apps,
293 // false for libraries and tests.
294 Shrink *bool
295
296 // If true, optimize bytecode. Defaults to false.
297 Optimize *bool
298
299 // If true, obfuscate bytecode. Defaults to false.
300 Obfuscate *bool
301
302 // If true, do not use the flag files generated by aapt that automatically keep
303 // classes referenced by the app manifest. Defaults to false.
304 No_aapt_flags *bool
305
306 // Flags to pass to proguard.
307 Proguard_flags []string
308
309 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800310 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800311 }
312
Paul Duffine25c6442019-10-11 13:50:28 +0100313 // When targeting 1.9 and above, override the modules to use with --system,
314 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700315 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700316
Jiyong Park4c4c0242019-10-21 14:53:15 +0900317 // set the name of the output
318 Stem *string
319
Colin Cross5a0dcd52018-10-05 14:20:06 -0700320 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800321 IsSDKLibrary bool `blueprint:"mutated"`
Songchun Fan688de9a2020-03-24 20:32:24 -0700322
323 // If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file.
324 // Defaults to false.
325 V4_signature *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700326}
327
Sasha Smundak2057f822019-04-16 17:16:58 -0700328func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
329 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
330}
331
Colin Cross46c9b8b2017-06-22 16:51:17 -0700332// Module contains the properties and members used by all java module types
333type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700334 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700335 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900336 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900337 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700338
Colin Cross89536d42017-07-07 14:35:50 -0700339 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700340 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700341 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700342
Colin Cross331a1212018-08-15 20:40:52 -0700343 // jar file containing header classes including static library dependencies, suitable for
344 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700345 headerJarFile android.Path
346
Colin Cross331a1212018-08-15 20:40:52 -0700347 // jar file containing implementation classes including static library dependencies but no
348 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700349 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700350
Colin Cross331a1212018-08-15 20:40:52 -0700351 // jar file containing only resources including from static library dependencies
352 resourceJar android.Path
353
Colin Cross0c4ce212019-05-03 15:28:19 -0700354 // args and dependencies to package source files into a srcjar
355 srcJarArgs []string
356 srcJarDeps android.Paths
357
Colin Cross331a1212018-08-15 20:40:52 -0700358 // jar file containing implementation classes and resources including static library
359 // dependencies
360 implementationAndResourcesJar android.Path
361
362 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700363 dexJarFile android.Path
364
Colin Cross43f08db2018-11-12 10:13:39 -0800365 // output file that contains classes.dex if it should be in the output file
366 maybeStrippedDexJarFile android.Path
367
Colin Crosscb933592017-11-22 13:49:43 -0800368 // output file containing uninstrumented classes that will be instrumented by jacoco
369 jacocoReportClassesFile android.Path
370
Colin Cross66dbc0b2017-12-28 12:23:20 -0800371 // output file containing mapping of obfuscated names
372 proguardDictionary android.Path
373
Colin Cross331a1212018-08-15 20:40:52 -0700374 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700375 outputFile android.Path
376 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700377
Colin Cross635c3b02016-05-18 15:37:25 -0700378 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700379
Colin Cross635c3b02016-05-18 15:37:25 -0700380 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700381
Colin Cross2fe66872015-03-30 17:20:39 -0700382 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700383 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800384
385 // list of .java files and srcjars that was passed to javac
386 compiledJavaSrcs android.Paths
387 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800388
389 // list of extra progurad flag files
390 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900391
Colin Cross094054a2018-10-17 15:10:48 -0700392 // manifest file to use instead of properties.Manifest
393 overrideManifest android.OptionalPath
394
Artur Satayev9cf46692019-11-26 18:08:34 +0000395 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900396 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700397
Artur Satayev9cf46692019-11-26 18:08:34 +0000398 // list of plugins that this java module is exporting
399 exportedPluginJars android.Paths
400
401 // list of plugins that this java module is exporting
402 exportedPluginClasses []string
403
404 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800405 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700406 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800407
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800408 // expanded Jarjar_rules
409 expandJarjarRules android.Path
410
Vladimir Marko0975ee02019-04-02 10:29:55 +0100411 // list of additional targets for checkbuild
412 additionalCheckedModules android.Paths
413
Colin Cross988708c2019-05-06 14:04:11 -0700414 // Extra files generated by the module type to be added as java resources.
415 extraResources android.Paths
416
Colin Crossf24a22a2019-01-31 14:12:44 -0800417 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800418 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800419
420 // list of the xref extraction files
421 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700422}
423
Colin Cross41955e82019-05-29 14:40:35 -0700424func (j *Module) OutputFiles(tag string) (android.Paths, error) {
425 switch tag {
426 case "":
427 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700428 case ".jar":
429 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700430 case ".proguard_map":
431 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700432 default:
433 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
434 }
Colin Cross54250902017-12-05 09:28:08 -0800435}
436
Colin Cross41955e82019-05-29 14:40:35 -0700437var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800438
Colin Crossf506d872017-07-19 15:53:04 -0700439type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700440 HeaderJars() android.Paths
441 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700442 ResourceJars() android.Paths
443 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800444 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700445 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900446 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000447 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700448 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700449 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900450 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700451}
452
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900454 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
455 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456}
457
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800458type xref interface {
459 XrefJavaFiles() android.Paths
460}
461
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800462func (j *Module) XrefJavaFiles() android.Paths {
463 return j.kytheFiles
464}
465
Colin Cross89536d42017-07-07 14:35:50 -0700466func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
467 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
468 android.InitDefaultableModule(module)
469}
470
Colin Crossbe1da472017-07-07 15:59:46 -0700471type dependencyTag struct {
472 blueprint.BaseDependencyTag
473 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700474}
475
Colin Crossa4f08812018-10-02 22:03:40 -0700476type jniDependencyTag struct {
477 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700478}
479
Jiyong Park8be103b2019-11-08 15:53:48 +0900480func IsJniDepTag(depTag blueprint.DependencyTag) bool {
481 _, ok := depTag.(*jniDependencyTag)
482 return ok
483}
484
Colin Crossbe1da472017-07-07 15:59:46 -0700485var (
Colin Cross4b964c02018-10-15 16:18:06 -0700486 staticLibTag = dependencyTag{name: "staticlib"}
487 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700488 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800489 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000490 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700491 bootClasspathTag = dependencyTag{name: "bootclasspath"}
492 systemModulesTag = dependencyTag{name: "system modules"}
493 frameworkResTag = dependencyTag{name: "framework-res"}
494 frameworkApkTag = dependencyTag{name: "framework-apk"}
495 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800496 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700497 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
498 certificateTag = dependencyTag{name: "certificate"}
499 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700500 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700501)
Colin Cross2fe66872015-03-30 17:20:39 -0700502
Jiyong Park83dc74b2020-01-14 18:38:44 +0900503func IsLibDepTag(depTag blueprint.DependencyTag) bool {
504 return depTag == libTag
505}
506
507func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
508 return depTag == staticLibTag
509}
510
Colin Crossfc3674a2017-09-18 17:41:52 -0700511type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700512 useModule, useFiles, useDefaultLibs, invalidVersion bool
513
Colin Cross6cef4812019-10-17 14:23:50 -0700514 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
515 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100516
517 // The default system modules to use. Will be an empty string if no system
518 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700519 systemModules string
520
Colin Cross6cef4812019-10-17 14:23:50 -0700521 // The modules that will be added ot the classpath when targeting 1.9 or higher
522 java9Classpath []string
523
Colin Crossa97c5d32018-03-28 14:58:31 -0700524 frameworkResModule string
525
Colin Cross86a60ae2018-05-29 14:44:55 -0700526 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700527 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100528
529 noStandardLibs, noFrameworksLibs bool
530}
531
532func (s sdkDep) hasStandardLibs() bool {
533 return !s.noStandardLibs
534}
535
536func (s sdkDep) hasFrameworkLibs() bool {
537 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700538}
539
Colin Crossa4f08812018-10-02 22:03:40 -0700540type jniLib struct {
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700541 name string
542 path android.Path
543 target android.Target
544 coverageFile android.OptionalPath
Colin Crossa4f08812018-10-02 22:03:40 -0700545}
546
Colin Cross0ea8ba82019-06-06 14:33:29 -0700547func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800548 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
549}
550
Colin Cross0ea8ba82019-06-06 14:33:29 -0700551func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800552 return j.shouldInstrument(ctx) &&
553 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
554 ctx.Config().UnbundledBuild())
555}
556
Jiyong Park6a927c42020-01-21 02:03:43 +0900557func (j *Module) sdkVersion() sdkSpec {
558 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700559}
560
Paul Duffine25c6442019-10-11 13:50:28 +0100561func (j *Module) systemModules() string {
562 return proptools.String(j.deviceProperties.System_modules)
563}
564
Jiyong Park6a927c42020-01-21 02:03:43 +0900565func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700566 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900567 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700568 }
569 return j.sdkVersion()
570}
571
Jiyong Park6a927c42020-01-21 02:03:43 +0900572func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700573 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900574 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700575 }
576 return j.sdkVersion()
577}
578
Jiyong Parkb02bb402019-12-03 00:43:57 +0900579func (j *Module) AvailableFor(what string) bool {
580 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
581 // Exception: for hostdex: true libraries, the platform variant is created
582 // even if it's not marked as available to platform. In that case, the platform
583 // variant is used only for the hostdex and not installed to the device.
584 return true
585 }
586 return j.ApexModuleBase.AvailableFor(what)
587}
588
Colin Crossbe1da472017-07-07 15:59:46 -0700589func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700590 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100591 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700592 if sdkDep.useDefaultLibs {
593 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
594 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
595 if sdkDep.hasFrameworkLibs() {
596 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700597 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700598 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700599 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100600 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700601 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700602 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
603 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
604 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
605 }
Colin Cross2fe66872015-03-30 17:20:39 -0700606 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700607
Nan Zhangb2b33de2018-02-23 11:18:47 -0800608 if ctx.ModuleName() == "android_stubs_current" ||
609 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700610 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700611 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800612 }
Colin Cross2fe66872015-03-30 17:20:39 -0700613 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700614
Inseob Kimac1e9862019-12-09 18:15:47 +0900615 syspropPublicStubs := syspropPublicStubs(ctx.Config())
616
617 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
618 // and redirects dependency to public stub depending on the link type.
619 rewriteSyspropLibs := func(libs []string, prop string) []string {
620 // make a copy
621 ret := android.CopyOf(libs)
622
623 for idx, lib := range libs {
624 stub, ok := syspropPublicStubs[lib]
625
626 if !ok {
627 continue
628 }
629
630 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900631 // only platform modules can use internal props
632 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900633 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900634 }
635 }
636
637 return ret
638 }
639
640 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
641 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700642
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700643 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000644 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800645
Colin Crossfe17f6f2019-03-28 19:30:56 -0700646 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700647 if j.hasSrcExt(".proto") {
648 protoDeps(ctx, &j.protoProperties)
649 }
Colin Cross93e85952017-08-15 13:34:18 -0700650
651 if j.hasSrcExt(".kt") {
652 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
653 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700654 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
655 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800656 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800657 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
658 }
Colin Cross93e85952017-08-15 13:34:18 -0700659 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800660
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000661 // Framework libraries need special handling in static coverage builds: they should not have
662 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
663 // the same jacoco classes coming from different bootclasspath jars.
664 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
665 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
666 j.properties.Instrument = true
667 }
668 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700669 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800670 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700671}
672
673func hasSrcExt(srcs []string, ext string) bool {
674 for _, src := range srcs {
675 if filepath.Ext(src) == ext {
676 return true
677 }
678 }
679
680 return false
681}
682
683func (j *Module) hasSrcExt(ext string) bool {
684 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700685}
686
Colin Cross46c9b8b2017-06-22 16:51:17 -0700687func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700688 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700689
Colin Crossebe1a512017-11-14 13:12:14 -0800690 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
691 aidlIncludes = append(aidlIncludes,
692 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
693 aidlIncludes = append(aidlIncludes,
694 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700695
Colin Cross3047fa22019-04-18 10:56:44 -0700696 var flags []string
697 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700698
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700699 if aidlPreprocess.Valid() {
700 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700701 deps = append(deps, aidlPreprocess.Path())
702 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700703 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700704 }
705
Colin Cross3047fa22019-04-18 10:56:44 -0700706 if len(j.exportAidlIncludeDirs) > 0 {
707 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
708 }
709
710 if len(aidlIncludes) > 0 {
711 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
712 }
713
Colin Cross635c3b02016-05-18 15:37:25 -0700714 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800715 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700716 flags = append(flags, "-I"+src.String())
717 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700718
Martijn Coeneneab15642018-03-09 09:29:59 +0100719 if Bool(j.deviceProperties.Aidl.Generate_traces) {
720 flags = append(flags, "-t")
721 }
722
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100723 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
724 flags = append(flags, "--transaction_names")
725 }
726
Colin Cross3047fa22019-04-18 10:56:44 -0700727 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700728}
729
Colin Cross32f676a2017-09-06 13:41:06 -0700730type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800731 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700732 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800733 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700734 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800735 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700736 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700737 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700738 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700739 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800740 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700741 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700742 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700743 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700744 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800745 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800746
747 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700748}
Colin Cross2fe66872015-03-30 17:20:39 -0700749
Colin Cross54250902017-12-05 09:28:08 -0800750func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
751 for _, f := range dep.Srcs() {
752 if f.Ext() != ".jar" {
753 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
754 ctx.OtherModuleName(dep.(blueprint.Module)))
755 }
756 }
757}
758
Jiyong Park2d492942018-03-05 17:44:10 +0900759type linkType int
760
761const (
Jiyong Park50146e92020-01-30 18:00:15 +0900762 // TODO(jiyong) rename these for better readability. Make the allowed
763 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900764 javaCore linkType = iota
765 javaSdk
766 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900767 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900768 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +0900769 javaPlatform
770)
771
Jeongik Cha75b83b02019-11-01 15:28:00 +0900772type linkTypeContext interface {
773 android.Module
774 getLinkType(name string) (ret linkType, stubs bool)
775}
776
777func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700778 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700779 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900780 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
781 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100782 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900783 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900784 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900785 return javaCore, false
786 case name == "android_system_stubs_current":
787 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900788 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900789 return javaSystem, false
790 case name == "android_test_stubs_current":
791 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900792 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900793 return javaPlatform, false
794 case name == "android_stubs_current":
795 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900796 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900797 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900798 case name == "android_module_lib_stubs_current":
799 return javaModule, true
800 case ver.kind == sdkModule:
801 return javaModule, false
Anton Hanssonbbd78552020-03-19 15:23:38 +0000802 case name == "android_system_server_stubs_current":
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900803 return javaSystemServer, true
804 case ver.kind == sdkSystemServer:
805 return javaSystemServer, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900806 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900807 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900808 case !ver.valid():
809 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700810 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900811 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900812 }
813}
814
Jeongik Cha75b83b02019-11-01 15:28:00 +0900815func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700816 if ctx.Host() {
817 return
818 }
819
Jeongik Cha75b83b02019-11-01 15:28:00 +0900820 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900821 if stubs {
822 return
823 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900824 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900825 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."
826
827 switch myLinkType {
828 case javaCore:
829 if otherLinkType != javaCore {
830 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 +0900831 ctx.OtherModuleName(to))
832 }
Jiyong Park2d492942018-03-05 17:44:10 +0900833 break
834 case javaSdk:
835 if otherLinkType != javaCore && otherLinkType != javaSdk {
836 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
837 ctx.OtherModuleName(to))
838 }
839 break
840 case javaSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900841 if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer {
Jiyong Park2d492942018-03-05 17:44:10 +0900842 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
843 ctx.OtherModuleName(to))
844 }
845 break
Jiyong Park50146e92020-01-30 18:00:15 +0900846 case javaModule:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900847 if otherLinkType == javaPlatform || otherLinkType == javaSystemServer {
Jiyong Park50146e92020-01-30 18:00:15 +0900848 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
849 ctx.OtherModuleName(to))
850 }
851 break
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900852 case javaSystemServer:
853 if otherLinkType == javaPlatform {
854 ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage,
855 ctx.OtherModuleName(to))
856 }
857 break
Jiyong Park2d492942018-03-05 17:44:10 +0900858 case javaPlatform:
859 // no restriction on link-type
860 break
Jiyong Park750e5572018-01-31 00:20:13 +0900861 }
862}
863
Colin Cross32f676a2017-09-06 13:41:06 -0700864func (j *Module) collectDeps(ctx android.ModuleContext) deps {
865 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700866
Colin Cross300f0382018-03-06 13:11:51 -0800867 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700868 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800869 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700870 ctx.AddMissingDependencies(sdkDep.bootclasspath)
871 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800872 } else if sdkDep.useFiles {
873 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700874 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700875 deps.aidlPreprocess = sdkDep.aidl
876 } else {
877 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800878 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700879 }
880
Colin Crossd11fcda2017-10-23 17:59:01 -0700881 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700882 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700883 tag := ctx.OtherModuleDependencyTag(module)
884
Colin Crossa4f08812018-10-02 22:03:40 -0700885 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700886 // Handled by AndroidApp.collectAppDeps
887 return
888 }
889 if tag == certificateTag {
890 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700891 return
892 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900893 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900894 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900895 if to, ok := module.(linkTypeContext); ok {
896 switch tag {
897 case bootClasspathTag, libTag, staticLibTag:
898 checkLinkType(ctx, j, to, tag.(dependencyTag))
899 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700900 }
Jiyong Park750e5572018-01-31 00:20:13 +0900901 }
Colin Cross54250902017-12-05 09:28:08 -0800902 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800903 case SdkLibraryDependency:
904 switch tag {
905 case libTag:
906 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
907 // names of sdk libs that are directly depended are exported
908 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700909 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800910 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
911 }
Colin Cross54250902017-12-05 09:28:08 -0800912 case Dependency:
913 switch tag {
914 case bootClasspathTag:
915 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700916 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800917 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900918 // sdk lib names from dependencies are re-exported
919 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700920 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000921 pluginJars, pluginClasses := dep.ExportedPlugins()
922 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700923 case java9LibTag:
924 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800925 case staticLibTag:
926 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
927 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
928 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700929 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900930 // sdk lib names from dependencies are re-exported
931 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700932 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000933 pluginJars, pluginClasses := dep.ExportedPlugins()
934 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800935 case pluginTag:
936 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800937 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000938 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
939 } else {
940 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800941 }
942 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
943 } else {
944 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
945 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000946 case exportedPluginTag:
947 if plugin, ok := dep.(*Plugin); ok {
948 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
949 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
950 }
951 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
952 if plugin.pluginProperties.Processor_class != nil {
953 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
954 }
955 } else {
956 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
957 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800958 case frameworkApkTag:
959 if ctx.ModuleName() == "android_stubs_current" ||
960 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700961 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800962 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
963 // resource files out of there for aapt.
964 //
965 // Normally the package rule runs aapt, which includes the resource,
966 // but we're not running that in our package rule so just copy in the
967 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700968 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800969 }
Colin Cross54250902017-12-05 09:28:08 -0800970 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700971 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800972 case kotlinAnnotationsTag:
973 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800974 }
975
Colin Cross54250902017-12-05 09:28:08 -0800976 case android.SourceFileProducer:
977 switch tag {
978 case libTag:
979 checkProducesJars(ctx, dep)
980 deps.classpath = append(deps.classpath, dep.Srcs()...)
981 case staticLibTag:
982 checkProducesJars(ctx, dep)
983 deps.classpath = append(deps.classpath, dep.Srcs()...)
984 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
985 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800986 }
987 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700988 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100989 case bootClasspathTag:
990 // If a system modules dependency has been added to the bootclasspath
991 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000992 sm := module.(SystemModulesProvider)
993 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +0100994
Colin Cross1369cdb2017-09-29 17:58:17 -0700995 case systemModulesTag:
996 if deps.systemModules != nil {
997 panic("Found two system module dependencies")
998 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000999 sm := module.(SystemModulesProvider)
1000 outputDir, outputDeps := sm.OutputDirAndDeps()
1001 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001002 }
Colin Crossec7a0422017-07-07 14:47:12 -07001003 }
Colin Cross2fe66872015-03-30 17:20:39 -07001004 })
1005
Jiyong Park1be96912018-05-28 18:02:19 +09001006 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1007
Colin Cross32f676a2017-09-06 13:41:06 -07001008 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001009}
1010
Artur Satayev9cf46692019-11-26 18:08:34 +00001011func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1012 deps.processorPath = append(deps.processorPath, pluginJars...)
1013 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1014}
1015
Colin Cross1e743852019-10-28 11:37:20 -07001016func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001017 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001018 if err != nil {
1019 ctx.PropertyErrorf("sdk_version", "%s", err)
1020 }
Nan Zhang357466b2018-04-17 17:38:36 -07001021 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001022 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001023 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001024 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001025 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001026 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001027 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1028 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001029 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001030 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001031 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001032 }
Nan Zhang357466b2018-04-17 17:38:36 -07001033}
1034
Colin Cross1e743852019-10-28 11:37:20 -07001035type javaVersion int
1036
1037const (
1038 JAVA_VERSION_UNSUPPORTED = 0
1039 JAVA_VERSION_6 = 6
1040 JAVA_VERSION_7 = 7
1041 JAVA_VERSION_8 = 8
1042 JAVA_VERSION_9 = 9
1043)
1044
1045func (v javaVersion) String() string {
1046 switch v {
1047 case JAVA_VERSION_6:
1048 return "1.6"
1049 case JAVA_VERSION_7:
1050 return "1.7"
1051 case JAVA_VERSION_8:
1052 return "1.8"
1053 case JAVA_VERSION_9:
1054 return "1.9"
1055 default:
1056 return "unsupported"
1057 }
1058}
1059
1060// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1061func (v javaVersion) usesJavaModules() bool {
1062 return v >= 9
1063}
1064
1065func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001066 switch javaVersion {
1067 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001068 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001069 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001070 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001071 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001072 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001073 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001074 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001075 case "10", "11":
1076 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001077 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001078 default:
1079 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001080 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001081 }
1082}
1083
Nan Zhanged19fc32017-10-19 13:06:22 -07001084func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001085
Colin Crossf03c82b2015-04-13 13:53:40 -07001086 var flags javaBuilderFlags
1087
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001088 // javaVersion flag.
1089 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1090
Nan Zhanged19fc32017-10-19 13:06:22 -07001091 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001092 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001093 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001094 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001095 }
Colin Cross6510f912017-11-29 00:27:14 -08001096 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001097 // Override the -g flag passed globally to remove local variable debug info to reduce
1098 // disk and memory usage.
1099 javacFlags = append(javacFlags, "-g:source,lines")
1100 }
Colin Crossc228a702019-11-06 16:18:05 -08001101 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001102
Colin Cross66548102018-06-19 22:47:35 -07001103 if ctx.Config().RunErrorProne() {
1104 if config.ErrorProneClasspath == nil {
1105 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1106 }
1107
1108 errorProneFlags := []string{
1109 "-Xplugin:ErrorProne",
1110 "${config.ErrorProneChecks}",
1111 }
1112 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1113
1114 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1115 "'" + strings.Join(errorProneFlags, " ") + "'"
1116 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001117 }
1118
Nan Zhanged19fc32017-10-19 13:06:22 -07001119 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001120 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1121 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001122 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001123 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001124
Colin Crossbe9cdb82019-01-21 21:37:16 -08001125 flags.processor = strings.Join(deps.processorClasses, ",")
1126
Colin Cross1e743852019-10-28 11:37:20 -07001127 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1128 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001129 // Give host-side tools a version of OpenJDK's standard libraries
1130 // close to what they're targeting. As of Dec 2017, AOSP is only
1131 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1132 //
1133 // When building with OpenJDK 8, the following should have no
1134 // effect since those jars would be available by default.
1135 //
1136 // When building with OpenJDK 9 but targeting a version < 1.8,
1137 // putting them on the bootclasspath means that:
1138 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1139 // b) references to existing APIs are not reinterpreted in an
1140 // OpenJDK 9-specific way, eg. calls to subclasses of
1141 // java.nio.Buffer as in http://b/70862583
1142 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1143 flags.bootClasspath = append(flags.bootClasspath,
1144 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1145 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001146 if Bool(j.properties.Use_tools_jar) {
1147 flags.bootClasspath = append(flags.bootClasspath,
1148 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1149 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001150 }
1151
Colin Cross1e743852019-10-28 11:37:20 -07001152 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001153 // Manually specify build directory in case it is not under the repo root.
1154 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1155 // just adding a symlink under the root doesn't help.)
1156 patchPaths := ".:" + ctx.Config().BuildDir()
1157 classPath := flags.classpath.FormJavaClassPath("")
1158 if classPath != "" {
1159 patchPaths += ":" + classPath
1160 }
1161 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001162 }
1163
Nan Zhanged19fc32017-10-19 13:06:22 -07001164 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001165 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001166
Nan Zhanged19fc32017-10-19 13:06:22 -07001167 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001168 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001169
Colin Cross81440082018-08-15 20:21:55 -07001170 if len(javacFlags) > 0 {
1171 // optimization.
1172 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1173 flags.javacFlags = "$javacFlags"
1174 }
1175
Nan Zhanged19fc32017-10-19 13:06:22 -07001176 return flags
1177}
Colin Crossc0b06f12015-04-08 13:03:43 -07001178
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001179func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001180 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001181
1182 deps := j.collectDeps(ctx)
1183 flags := j.collectBuilderFlags(ctx, deps)
1184
Colin Cross1e743852019-10-28 11:37:20 -07001185 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001186 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1187 }
Colin Cross8a497952019-03-05 22:25:09 -08001188 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001189 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001190 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001191 }
1192
Colin Crossaf050172017-11-15 23:01:59 -08001193 srcFiles = j.genSources(ctx, srcFiles, flags)
1194
1195 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001196 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001197 if aaptSrcJar != nil {
1198 srcJars = append(srcJars, aaptSrcJar)
1199 }
Colin Crossb7a63242015-04-16 14:09:14 -07001200
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001201 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001202 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001203 }
1204
Colin Cross1ee23172017-10-18 14:44:18 -07001205 jarName := ctx.ModuleName() + ".jar"
1206
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001207 javaSrcFiles := srcFiles.FilterByExt(".java")
1208 var uniqueSrcFiles android.Paths
1209 set := make(map[string]bool)
1210 for _, v := range javaSrcFiles {
1211 if _, found := set[v.String()]; !found {
1212 set[v.String()] = true
1213 uniqueSrcFiles = append(uniqueSrcFiles, v)
1214 }
1215 }
1216
patricktu242faad2019-09-24 15:41:30 +08001217 // Collect .java files for AIDEGen
1218 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1219
Colin Cross55f63ea2018-08-27 12:37:09 -07001220 var kotlinJars android.Paths
1221
Colin Cross93e85952017-08-15 13:34:18 -07001222 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001223 // user defined kotlin flags.
1224 kotlincFlags := j.properties.Kotlincflags
1225 CheckKotlincFlags(ctx, kotlincFlags)
1226
Colin Cross93e85952017-08-15 13:34:18 -07001227 // If there are kotlin files, compile them first but pass all the kotlin and java files
1228 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1229 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001230 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001231 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001232 kotlincFlags = append(kotlincFlags, "-no-jdk")
1233 }
1234 if len(kotlincFlags) > 0 {
1235 // optimization.
1236 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1237 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001238 }
1239
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001240 var kotlinSrcFiles android.Paths
1241 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1242 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1243
patricktu242faad2019-09-24 15:41:30 +08001244 // Collect .kt files for AIDEGen
1245 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1246
Colin Crossafbb1732019-01-17 15:42:52 -08001247 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1248 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1249
1250 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1251 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1252
1253 if len(flags.processorPath) > 0 {
1254 // Use kapt for annotation processing
1255 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1256 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1257 srcJars = append(srcJars, kaptSrcJar)
1258 // Disable annotation processing in javac, it's already been handled by kapt
1259 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001260 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001261 }
Colin Cross93e85952017-08-15 13:34:18 -07001262
Colin Cross1ee23172017-10-18 14:44:18 -07001263 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001264 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001265 if ctx.Failed() {
1266 return
1267 }
1268
1269 // Make javac rule depend on the kotlinc rule
1270 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001271
Colin Cross93e85952017-08-15 13:34:18 -07001272 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001273 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001274 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001275 }
1276
Colin Cross55f63ea2018-08-27 12:37:09 -07001277 jars := append(android.Paths(nil), kotlinJars...)
1278
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001279 // Store the list of .java files that was passed to javac
1280 j.compiledJavaSrcs = uniqueSrcFiles
1281 j.compiledSrcJars = srcJars
1282
Nan Zhang61eaedb2017-11-02 13:28:15 -07001283 enable_sharding := false
Colin Crossf5a66282020-02-21 08:16:41 -08001284 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001285 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001286 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1287 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001288 // Formerly, there was a check here that prevented annotation processors
1289 // from being used when sharding was enabled, as some annotation processors
1290 // do not function correctly in sharded environments. It was removed to
1291 // allow for the use of annotation processors that do function correctly
1292 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001293 }
Colin Crossf5a66282020-02-21 08:16:41 -08001294 headerJarFileWithoutJarjar, j.headerJarFile =
1295 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001296 if ctx.Failed() {
1297 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001298 }
1299 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001300 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001301 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001302 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001303 // If error-prone is enabled, add an additional rule to compile the java files into
1304 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001305 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001306 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1307 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001308 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001309 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001310 extraJarDeps = append(extraJarDeps, errorprone)
1311 }
1312
Nan Zhang61eaedb2017-11-02 13:28:15 -07001313 if enable_sharding {
Colin Crossf5a66282020-02-21 08:16:41 -08001314 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001315 shardSize := int(*(j.properties.Javac_shard_size))
1316 var shardSrcs []android.Paths
1317 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001318 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001319 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001320 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1321 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001322 jars = append(jars, classes)
1323 }
1324 }
1325 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001326 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1327 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001328 jars = append(jars, classes)
1329 }
1330 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001331 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001332 jars = append(jars, classes)
1333 }
Colin Crossd6891432017-09-27 17:39:56 -07001334 if ctx.Failed() {
1335 return
1336 }
Colin Cross2fe66872015-03-30 17:20:39 -07001337 }
1338
Colin Cross0c4ce212019-05-03 15:28:19 -07001339 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1340
1341 var includeSrcJar android.WritablePath
1342 if Bool(j.properties.Include_srcs) {
1343 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1344 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1345 }
1346
Colin Crosscedd4762018-09-13 11:26:19 -07001347 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1348 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001349 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001350 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001351
1352 var resArgs []string
1353 var resDeps android.Paths
1354
1355 resArgs = append(resArgs, dirArgs...)
1356 resDeps = append(resDeps, dirDeps...)
1357
1358 resArgs = append(resArgs, fileArgs...)
1359 resDeps = append(resDeps, fileDeps...)
1360
Colin Cross988708c2019-05-06 14:04:11 -07001361 resArgs = append(resArgs, extraArgs...)
1362 resDeps = append(resDeps, extraDeps...)
1363
Colin Cross40a36712017-09-27 17:41:35 -07001364 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001365 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001366 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001367 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001368 if ctx.Failed() {
1369 return
1370 }
1371 }
1372
Colin Cross0c4ce212019-05-03 15:28:19 -07001373 var resourceJars android.Paths
1374 if j.resourceJar != nil {
1375 resourceJars = append(resourceJars, j.resourceJar)
1376 }
1377 if Bool(j.properties.Include_srcs) {
1378 resourceJars = append(resourceJars, includeSrcJar)
1379 }
1380 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001381
Colin Cross0c4ce212019-05-03 15:28:19 -07001382 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001383 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001384 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001385 false, nil, nil)
1386 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001387 } else if len(resourceJars) == 1 {
1388 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001389 }
1390
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001391 if len(deps.staticJars) > 0 {
1392 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001393 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001394
Colin Cross094054a2018-10-17 15:10:48 -07001395 manifest := j.overrideManifest
1396 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001397 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001398 }
Colin Cross635acc92017-09-12 22:50:46 -07001399
Colin Cross8a497952019-03-05 22:25:09 -08001400 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001401 if len(services) > 0 {
1402 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1403 var zipargs []string
1404 for _, file := range services {
1405 serviceFile := file.String()
1406 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1407 }
1408 ctx.Build(pctx, android.BuildParams{
1409 Rule: zip,
1410 Output: servicesJar,
1411 Implicits: services,
1412 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001413 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001414 },
1415 })
1416 jars = append(jars, servicesJar)
1417 }
1418
Colin Cross0a6e0072017-08-30 14:24:55 -07001419 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001420 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001421 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001422
1423 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001424 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1425 // Optimization: skip the combine step if there is nothing to do
1426 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1427 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1428 // any if len(jars) == 1.
1429 outputFile = moduleOutPath
1430 } else {
1431 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1432 ctx.Build(pctx, android.BuildParams{
1433 Rule: android.Cp,
1434 Input: jars[0],
1435 Output: combinedJar,
1436 })
1437 outputFile = combinedJar
1438 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001439 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001440 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001441 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001442 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001443 outputFile = combinedJar
1444 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001445
Colin Cross331a1212018-08-15 20:40:52 -07001446 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001447 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001448 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001449 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001450 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001451 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001452
1453 // jarjar resource jar if necessary
1454 if j.resourceJar != nil {
1455 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001456 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001457 j.resourceJar = resourceJarJarFile
1458 }
1459
Colin Cross0a6e0072017-08-30 14:24:55 -07001460 if ctx.Failed() {
1461 return
1462 }
1463 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001464
1465 // Check package restrictions if necessary.
1466 if len(j.properties.Permitted_packages) > 0 {
1467 // Check packages and copy to package-checked file.
1468 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1469 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1470 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1471
1472 if ctx.Failed() {
1473 return
1474 }
1475 }
1476
Nan Zhanged19fc32017-10-19 13:06:22 -07001477 j.implementationJarFile = outputFile
1478 if j.headerJarFile == nil {
1479 j.headerJarFile = j.implementationJarFile
1480 }
Colin Cross2fe66872015-03-30 17:20:39 -07001481
Jiyong Park28826602020-02-21 16:04:53 +09001482 // Force enable the instrumentation for java code that is built for APEXes ...
1483 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1484 // doesn't make sense)
1485 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1486 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park81aaa0c2020-02-18 12:50:44 +00001487 j.properties.Instrument = true
1488 }
1489
Colin Cross3144dfc2018-01-03 15:06:47 -08001490 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001491 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1492 }
1493
Colin Cross331a1212018-08-15 20:40:52 -07001494 // merge implementation jar with resources if necessary
1495 implementationAndResourcesJar := outputFile
1496 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001497 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001498 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001499 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001500 false, nil, nil)
1501 implementationAndResourcesJar = combinedJar
1502 }
1503
1504 j.implementationAndResourcesJar = implementationAndResourcesJar
1505
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001506 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1507 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1508 if j.deviceProperties.Compile_dex == nil {
1509 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1510 }
1511 if j.deviceProperties.Hostdex == nil {
1512 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1513 }
1514 }
1515
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001516 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001517 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001518 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001519 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001520 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001521 if ctx.Failed() {
1522 return
1523 }
Colin Cross331a1212018-08-15 20:40:52 -07001524
Jiyong Park09cb6292019-07-15 15:29:23 +09001525 // Hidden API CSV generation and dex encoding
1526 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1527 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001528
Colin Cross331a1212018-08-15 20:40:52 -07001529 // merge dex jar with resources if necessary
1530 if j.resourceJar != nil {
1531 jars := android.Paths{dexOutputFile, j.resourceJar}
1532 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1533 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1534 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001535 if j.deviceProperties.UncompressDex {
1536 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1537 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1538 dexOutputFile = combinedAlignedJar
1539 } else {
1540 dexOutputFile = combinedJar
1541 }
Colin Cross331a1212018-08-15 20:40:52 -07001542 }
1543
1544 j.dexJarFile = dexOutputFile
1545
Colin Cross8faf8fc2019-01-16 15:15:52 -08001546 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001547 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1548
1549 j.maybeStrippedDexJarFile = dexOutputFile
1550
Colin Cross3063b782018-08-15 11:19:12 -07001551 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001552
1553 if ctx.Failed() {
1554 return
1555 }
Colin Cross331a1212018-08-15 20:40:52 -07001556 } else {
1557 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001558 }
Colin Cross331a1212018-08-15 20:40:52 -07001559
Colin Crossb7a63242015-04-16 14:09:14 -07001560 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001561
1562 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1563 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001564}
1565
Colin Cross3b706fd2019-09-05 16:44:18 -07001566func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1567 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1568
1569 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1570 if idx >= 0 {
1571 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1572 jarName += strconv.Itoa(idx)
1573 }
1574
1575 classes := android.PathForModuleOut(ctx, "javac", jarName)
1576 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1577
1578 if ctx.Config().EmitXrefRules() {
1579 extractionFile := android.PathForModuleOut(ctx, kzipName)
1580 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1581 j.kytheFiles = append(j.kytheFiles, extractionFile)
1582 }
1583
1584 return classes
1585}
1586
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001587// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1588// since some of these flags may be used internally.
1589func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1590 for _, flag := range flags {
1591 flag = strings.TrimSpace(flag)
1592
1593 if !strings.HasPrefix(flag, "-") {
1594 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1595 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1596 ctx.PropertyErrorf("kotlincflags",
1597 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1598 } else if inList(flag, config.KotlincIllegalFlags) {
1599 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1600 } else if flag == "-include-runtime" {
1601 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1602 } else {
1603 args := strings.Split(flag, " ")
1604 if args[0] == "-kotlin-home" {
1605 ctx.PropertyErrorf("kotlincflags",
1606 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1607 }
1608 }
1609 }
1610}
1611
Colin Cross8eadbf02017-10-24 17:46:00 -07001612func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf5a66282020-02-21 08:16:41 -08001613 deps deps, flags javaBuilderFlags, jarName string,
1614 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001615
1616 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001617 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001618 // Compile java sources into turbine.jar.
1619 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1620 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1621 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001622 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001623 }
1624 jars = append(jars, turbineJar)
1625 }
1626
Colin Cross55f63ea2018-08-27 12:37:09 -07001627 jars = append(jars, extraJars...)
1628
Nan Zhanged19fc32017-10-19 13:06:22 -07001629 // Combine any static header libraries into classes-header.jar. If there is only
1630 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001631 jars = append(jars, deps.staticHeaderJars...)
1632
Colin Cross5c6ecc12017-10-23 18:12:27 -07001633 // we cannot skip the combine step for now if there is only one jar
1634 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1635 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001636 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001637 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001638 headerJar = combinedJar
Colin Crossf5a66282020-02-21 08:16:41 -08001639 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001640
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001641 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001642 // Transform classes.jar into classes-jarjar.jar
1643 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001644 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf5a66282020-02-21 08:16:41 -08001645 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001646 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001647 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001648 }
1649 }
1650
Colin Crossf5a66282020-02-21 08:16:41 -08001651 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001652}
1653
Colin Crosscb933592017-11-22 13:49:43 -08001654func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001655 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001656
Colin Cross7a3139e2017-12-19 13:57:50 -08001657 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001658
Colin Cross84c38822018-01-03 15:59:46 -08001659 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001660 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1661
1662 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1663
1664 j.jacocoReportClassesFile = jacocoReportClassesFile
1665
1666 return instrumentedJar
1667}
1668
albaltai36ff7dc2018-12-25 14:35:23 +08001669var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001670
Nan Zhanged19fc32017-10-19 13:06:22 -07001671func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001672 if j.headerJarFile == nil {
1673 return nil
1674 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001675 return android.Paths{j.headerJarFile}
1676}
1677
1678func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001679 if j.implementationJarFile == nil {
1680 return nil
1681 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001682 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001683}
1684
Colin Crossf24a22a2019-01-31 14:12:44 -08001685func (j *Module) DexJar() android.Path {
1686 return j.dexJarFile
1687}
1688
Colin Cross331a1212018-08-15 20:40:52 -07001689func (j *Module) ResourceJars() android.Paths {
1690 if j.resourceJar == nil {
1691 return nil
1692 }
1693 return android.Paths{j.resourceJar}
1694}
1695
1696func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001697 if j.implementationAndResourcesJar == nil {
1698 return nil
1699 }
Colin Cross331a1212018-08-15 20:40:52 -07001700 return android.Paths{j.implementationAndResourcesJar}
1701}
1702
Colin Cross46c9b8b2017-06-22 16:51:17 -07001703func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001704 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001705 return j.exportAidlIncludeDirs
1706}
1707
Jiyong Park1be96912018-05-28 18:02:19 +09001708func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001709 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001710 return j.exportedSdkLibs
1711}
1712
Artur Satayev9cf46692019-11-26 18:08:34 +00001713func (j *Module) ExportedPlugins() (android.Paths, []string) {
1714 return j.exportedPluginJars, j.exportedPluginClasses
1715}
1716
Colin Cross0c4ce212019-05-03 15:28:19 -07001717func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1718 return j.srcJarArgs, j.srcJarDeps
1719}
1720
Colin Cross46c9b8b2017-06-22 16:51:17 -07001721var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001722
Colin Cross46c9b8b2017-06-22 16:51:17 -07001723func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001724 return j.logtagsSrcs
1725}
1726
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001727// Collect information for opening IDE project files in java/jdeps.go.
1728func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1729 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1730 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001731 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001732 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001733 if j.expandJarjarRules != nil {
1734 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001735 }
1736}
1737
1738func (j *Module) CompilerDeps() []string {
1739 jdeps := []string{}
1740 jdeps = append(jdeps, j.properties.Libs...)
1741 jdeps = append(jdeps, j.properties.Static_libs...)
1742 return jdeps
1743}
1744
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001745func (j *Module) hasCode(ctx android.ModuleContext) bool {
1746 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1747 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1748}
1749
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001750func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001751 // Dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001752 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
1753 return true
1754 }
Jiyong Park0f80c182020-01-31 02:49:53 +09001755 // Also, a dependency to an sdk member is also considered as such. This is required because
1756 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001757 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
1758 return true
1759 }
1760 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001761}
1762
Jiyong Park0b238752019-10-29 11:23:10 +09001763func (j *Module) Stem() string {
1764 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1765}
1766
Jiyong Park618922e2020-01-08 13:35:43 +09001767func (j *Module) JacocoReportClassesFile() android.Path {
1768 return j.jacocoReportClassesFile
1769}
1770
Martin Stjernholm6d415272020-01-31 17:10:36 +00001771func (j *Module) IsInstallable() bool {
1772 return Bool(j.properties.Installable)
1773}
1774
Colin Cross2fe66872015-03-30 17:20:39 -07001775//
1776// Java libraries (.jar file)
1777//
1778
Colin Crossf506d872017-07-19 15:53:04 -07001779type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001780 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001781
1782 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001783}
1784
Colin Cross42be7612019-02-21 18:12:14 -08001785func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001786 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1787 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1788 return true
1789 }
1790
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001791 // Store uncompressed (and do not strip) dex files from boot class path jars.
1792 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1793 return true
1794 }
1795
1796 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001797 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001798 return true
1799 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001800 if ctx.Config().UncompressPrivAppDex() &&
1801 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1802 return true
1803 }
1804
Colin Cross2fc72f62018-12-21 12:59:54 -08001805 return false
1806}
1807
Colin Crossf506d872017-07-19 15:53:04 -07001808func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001809 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001810 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001811 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001812 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001813 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001814 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001815
Jiyong Park7f7766d2019-07-25 22:02:35 +09001816 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1817 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001818 var extraInstallDeps android.Paths
1819 if j.InstallMixin != nil {
1820 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1821 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001822 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001823 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001824 }
Colin Crossb7a63242015-04-16 14:09:14 -07001825}
1826
Colin Crossf506d872017-07-19 15:53:04 -07001827func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001828 j.deps(ctx)
1829}
1830
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001831const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001832 aidlIncludeDir = "aidl"
1833 javaDir = "java"
1834 jarFileSuffix = ".jar"
1835 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001836)
1837
Paul Duffina0dbf432019-12-05 11:25:53 +00001838// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001839func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1840 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1841}
1842
1843func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1844 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001845}
1846
Paul Duffin13879572019-11-28 14:31:38 +00001847type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001848 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001849}
1850
1851func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1852 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1853}
1854
1855func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1856 _, ok := module.(*Library)
1857 return ok
1858}
1859
Paul Duffina0dbf432019-12-05 11:25:53 +00001860func (mt *librarySdkMemberType) buildSnapshot(
1861 sdkModuleContext android.ModuleContext,
1862 builder android.SnapshotBuilder,
1863 member android.SdkMember,
1864 jarToExportGetter func(j *Library) android.Path) {
1865
Paul Duffin13879572019-11-28 14:31:38 +00001866 variants := member.Variants()
1867 if len(variants) != 1 {
1868 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1869 for _, variant := range variants {
1870 sdkModuleContext.ModuleErrorf(" %q", variant)
1871 }
1872 }
1873 variant := variants[0]
1874 j := variant.(*Library)
1875
Paul Duffina0dbf432019-12-05 11:25:53 +00001876 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001877 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001878 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001879
1880 for _, dir := range j.AidlIncludeDirs() {
1881 // TODO(jiyong): copy parcelable declarations only
1882 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1883 for _, file := range aidlFiles {
1884 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1885 }
1886 }
1887
Paul Duffin9d8d6092019-12-05 18:19:29 +00001888 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001889 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001890}
1891
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001892var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1893 librarySdkMemberType{
1894 android.SdkMemberTypeBase{
1895 PropertyName: "java_header_libs",
1896 SupportsSdk: true,
1897 },
1898 },
1899}
1900
Paul Duffina0dbf432019-12-05 11:25:53 +00001901type headerLibrarySdkMemberType struct {
1902 librarySdkMemberType
1903}
1904
1905func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1906 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1907 headerJars := j.HeaderJars()
1908 if len(headerJars) != 1 {
1909 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1910 }
1911
1912 return headerJars[0]
1913 })
1914}
1915
Paul Duffina0dbf432019-12-05 11:25:53 +00001916type implLibrarySdkMemberType struct {
1917 librarySdkMemberType
1918}
1919
1920func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1921 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1922 implementationJars := j.ImplementationJars()
1923 if len(implementationJars) != 1 {
1924 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1925 }
1926
1927 return implementationJars[0]
1928 })
1929}
1930
Colin Cross1b16b0e2019-02-12 14:41:32 -08001931// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1932//
1933// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1934// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1935// as a `static_libs` dependency of another module.
1936//
1937// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1938// a device.
1939//
1940// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1941// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001942func LibraryFactory() android.Module {
1943 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001944
Colin Cross9ae1b922018-06-26 17:59:05 -07001945 module.AddProperties(
1946 &module.Module.properties,
1947 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001948 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001949 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001950
Jiyong Park7f7766d2019-07-25 22:02:35 +09001951 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001952 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001953 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001954 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001955}
1956
Colin Cross1b16b0e2019-02-12 14:41:32 -08001957// java_library_static is an obsolete alias for java_library.
1958func LibraryStaticFactory() android.Module {
1959 return LibraryFactory()
1960}
1961
1962// java_library_host builds and links sources into a `.jar` file for the host.
1963//
1964// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1965// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001966func LibraryHostFactory() android.Module {
1967 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001968
Colin Cross6af17aa2017-09-20 12:59:05 -07001969 module.AddProperties(
1970 &module.Module.properties,
1971 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001972
Colin Cross9ae1b922018-06-26 17:59:05 -07001973 module.Module.properties.Installable = proptools.BoolPtr(true)
1974
Jiyong Park7f7766d2019-07-25 22:02:35 +09001975 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001976 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001977 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001978}
1979
1980//
Colin Crossb628ea52018-08-14 16:42:33 -07001981// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001982//
1983
1984type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001985 // list of compatibility suites (for example "cts", "vts") that the module should be
1986 // installed into.
1987 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001988
1989 // the name of the test configuration (for example "AndroidTest.xml") that should be
1990 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001991 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001992
Jack He33338892018-09-19 02:21:28 -07001993 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1994 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001995 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001996
Colin Crossd96ca352018-08-10 16:06:24 -07001997 // list of files or filegroup modules that provide data that should be installed alongside
1998 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001999 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002000
2001 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2002 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2003 // explicitly.
2004 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002005}
2006
Paul Duffin42df1442019-03-20 12:45:53 +00002007type testHelperLibraryProperties struct {
2008 // list of compatibility suites (for example "cts", "vts") that the module should be
2009 // installed into.
2010 Test_suites []string `android:"arch_variant"`
2011}
2012
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002013type prebuiltTestProperties struct {
2014 // list of compatibility suites (for example "cts", "vts") that the module should be
2015 // installed into.
2016 Test_suites []string `android:"arch_variant"`
2017
2018 // the name of the test configuration (for example "AndroidTest.xml") that should be
2019 // installed with the module.
2020 Test_config *string `android:"path,arch_variant"`
2021}
2022
Colin Cross05638fc2018-04-09 18:40:24 -07002023type Test struct {
2024 Library
2025
2026 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002027
2028 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002029 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002030}
2031
Paul Duffin42df1442019-03-20 12:45:53 +00002032type TestHelperLibrary struct {
2033 Library
2034
2035 testHelperLibraryProperties testHelperLibraryProperties
2036}
2037
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002038type JavaTestImport struct {
2039 Import
2040
2041 prebuiltTestProperties prebuiltTestProperties
2042
2043 testConfig android.Path
2044}
2045
Colin Cross303e21f2018-08-07 16:49:25 -07002046func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002047 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2048 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002049 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002050
2051 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002052}
2053
Paul Duffin42df1442019-03-20 12:45:53 +00002054func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2055 j.Library.GenerateAndroidBuildActions(ctx)
2056}
2057
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002058func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2059 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2060 j.prebuiltTestProperties.Test_suites, nil)
2061
2062 j.Import.GenerateAndroidBuildActions(ctx)
2063}
2064
2065type testSdkMemberType struct {
2066 android.SdkMemberTypeBase
2067}
2068
2069func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2070 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2071}
2072
2073func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2074 _, ok := module.(*Test)
2075 return ok
2076}
2077
2078func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2079 variants := member.Variants()
2080 if len(variants) != 1 {
2081 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2082 for _, variant := range variants {
2083 sdkModuleContext.ModuleErrorf(" %q", variant)
2084 }
2085 }
2086 variant := variants[0]
2087 j := variant.(*Test)
2088
2089 implementationJars := j.ImplementationJars()
2090 if len(implementationJars) != 1 {
2091 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2092 }
2093
2094 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2095 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2096
2097 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2098 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2099
2100 module := builder.AddPrebuiltModule(member, "java_test_import")
2101 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2102 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2103}
2104
Colin Cross1b16b0e2019-02-12 14:41:32 -08002105// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2106// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2107//
2108// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2109// compiled against the device bootclasspath.
2110//
2111// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2112// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002113func TestFactory() android.Module {
2114 module := &Test{}
2115
2116 module.AddProperties(
2117 &module.Module.properties,
2118 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002119 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002120 &module.Module.protoProperties,
2121 &module.testProperties)
2122
Colin Cross9ae1b922018-06-26 17:59:05 -07002123 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002124 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002125
Colin Cross05638fc2018-04-09 18:40:24 -07002126 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002127 return module
2128}
2129
Paul Duffin42df1442019-03-20 12:45:53 +00002130// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2131func TestHelperLibraryFactory() android.Module {
2132 module := &TestHelperLibrary{}
2133
2134 module.AddProperties(
2135 &module.Module.properties,
2136 &module.Module.deviceProperties,
2137 &module.Module.dexpreoptProperties,
2138 &module.Module.protoProperties,
2139 &module.testHelperLibraryProperties)
2140
Colin Cross9a4abed2019-04-24 13:19:28 -07002141 module.Module.properties.Installable = proptools.BoolPtr(true)
2142 module.Module.dexpreopter.isTest = true
2143
Paul Duffin42df1442019-03-20 12:45:53 +00002144 InitJavaModule(module, android.HostAndDeviceSupported)
2145 return module
2146}
2147
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002148// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2149// and makes sure that it is added to the appropriate test suite.
2150//
2151// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2152// compiled against an Android classpath.
2153//
2154// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2155// for host modules.
2156func JavaTestImportFactory() android.Module {
2157 module := &JavaTestImport{}
2158
2159 module.AddProperties(
2160 &module.Import.properties,
2161 &module.prebuiltTestProperties)
2162
2163 module.Import.properties.Installable = proptools.BoolPtr(true)
2164
2165 android.InitPrebuiltModule(module, &module.properties.Jars)
2166 android.InitApexModule(module)
2167 android.InitSdkAwareModule(module)
2168 InitJavaModule(module, android.HostAndDeviceSupported)
2169 return module
2170}
2171
Colin Cross1b16b0e2019-02-12 14:41:32 -08002172// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2173// allow running the test with `atest` or a `TEST_MAPPING` file.
2174//
2175// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2176// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002177func TestHostFactory() android.Module {
2178 module := &Test{}
2179
2180 module.AddProperties(
2181 &module.Module.properties,
2182 &module.Module.protoProperties,
2183 &module.testProperties)
2184
Colin Cross9ae1b922018-06-26 17:59:05 -07002185 module.Module.properties.Installable = proptools.BoolPtr(true)
2186
Colin Cross05638fc2018-04-09 18:40:24 -07002187 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002188 return module
2189}
2190
2191//
Colin Cross2fe66872015-03-30 17:20:39 -07002192// Java Binaries (.jar file plus wrapper script)
2193//
2194
Colin Crossf506d872017-07-19 15:53:04 -07002195type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002196 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002197 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002198
2199 // Name of the class containing main to be inserted into the manifest as Main-Class.
2200 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002201}
2202
Colin Crossf506d872017-07-19 15:53:04 -07002203type Binary struct {
2204 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002205
Colin Crossf506d872017-07-19 15:53:04 -07002206 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002207
Colin Cross6b4a32d2017-12-05 13:42:45 -08002208 isWrapperVariant bool
2209
Colin Crossc3315992017-12-08 19:12:36 -08002210 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002211 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002212}
2213
Alex Light24237172017-10-26 09:46:21 -07002214func (j *Binary) HostToolPath() android.OptionalPath {
2215 return android.OptionalPathForPath(j.binaryFile)
2216}
2217
Colin Crossf506d872017-07-19 15:53:04 -07002218func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002219 if ctx.Arch().ArchType == android.Common {
2220 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002221 if j.binaryProperties.Main_class != nil {
2222 if j.properties.Manifest != nil {
2223 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2224 }
2225 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2226 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2227 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2228 }
2229
Colin Cross6b4a32d2017-12-05 13:42:45 -08002230 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002231 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002232 // Handle the binary wrapper
2233 j.isWrapperVariant = true
2234
Colin Cross366938f2017-12-11 16:29:02 -08002235 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002236 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002237 } else {
2238 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2239 }
2240
2241 // Depend on the installed jar so that the wrapper doesn't get executed by
2242 // another build rule before the jar has been installed.
2243 jarFile := ctx.PrimaryModule().(*Binary).installFile
2244
2245 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2246 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002247 }
Colin Cross2fe66872015-03-30 17:20:39 -07002248}
2249
Colin Crossf506d872017-07-19 15:53:04 -07002250func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002251 if ctx.Arch().ArchType == android.Common {
2252 j.deps(ctx)
2253 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002254}
2255
Colin Cross1b16b0e2019-02-12 14:41:32 -08002256// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2257// as well.
2258//
2259// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2260// compiled against the device bootclasspath.
2261//
2262// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2263// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002264func BinaryFactory() android.Module {
2265 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002266
Colin Cross36242852017-06-23 15:06:31 -07002267 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002268 &module.Module.properties,
2269 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002270 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002271 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002272 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002273
Colin Cross9ae1b922018-06-26 17:59:05 -07002274 module.Module.properties.Installable = proptools.BoolPtr(true)
2275
Colin Cross6b4a32d2017-12-05 13:42:45 -08002276 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2277 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002278 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002279}
2280
Colin Cross1b16b0e2019-02-12 14:41:32 -08002281// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2282//
2283// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2284// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002285func BinaryHostFactory() android.Module {
2286 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002287
Colin Cross36242852017-06-23 15:06:31 -07002288 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002289 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002290 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002291 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002292
Colin Cross9ae1b922018-06-26 17:59:05 -07002293 module.Module.properties.Installable = proptools.BoolPtr(true)
2294
Colin Cross6b4a32d2017-12-05 13:42:45 -08002295 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2296 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002297 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002298}
2299
2300//
2301// Java prebuilts
2302//
2303
Colin Cross74d73e22017-08-02 11:05:49 -07002304type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002305 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002306
Nan Zhangea568a42017-11-08 21:20:04 -08002307 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002308
2309 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002310
2311 // List of shared java libs that this module has dependencies to
2312 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002313
2314 // List of files to remove from the jar file(s)
2315 Exclude_files []string
2316
2317 // List of directories to remove from the jar file(s)
2318 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002319
2320 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002321 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002322
2323 // set the name of the output
2324 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002325}
2326
2327type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002328 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002329 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002330 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002331 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002332 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002333
Colin Cross74d73e22017-08-02 11:05:49 -07002334 properties ImportProperties
2335
Colin Cross0a6e0072017-08-30 14:24:55 -07002336 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002337 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002338}
2339
Jiyong Park6a927c42020-01-21 02:03:43 +09002340func (j *Import) sdkVersion() sdkSpec {
2341 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002342}
2343
Jiyong Park6a927c42020-01-21 02:03:43 +09002344func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002345 return j.sdkVersion()
2346}
2347
Colin Cross74d73e22017-08-02 11:05:49 -07002348func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002349 return &j.prebuilt
2350}
2351
Colin Cross74d73e22017-08-02 11:05:49 -07002352func (j *Import) PrebuiltSrcs() []string {
2353 return j.properties.Jars
2354}
2355
2356func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002357 return j.prebuilt.Name(j.ModuleBase.Name())
2358}
2359
Jiyong Park0b238752019-10-29 11:23:10 +09002360func (j *Import) Stem() string {
2361 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2362}
2363
Jiyong Park618922e2020-01-08 13:35:43 +09002364func (a *Import) JacocoReportClassesFile() android.Path {
2365 return nil
2366}
2367
Colin Cross74d73e22017-08-02 11:05:49 -07002368func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002369 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002370}
2371
Colin Cross74d73e22017-08-02 11:05:49 -07002372func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002373 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002374
Jiyong Park0b238752019-10-29 11:23:10 +09002375 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002376 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002377 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2378 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002379 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002380 inputFile := outputFile
2381 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2382 TransformJetifier(ctx, outputFile, inputFile)
2383 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002384 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002385
2386 ctx.VisitDirectDeps(func(module android.Module) {
2387 otherName := ctx.OtherModuleName(module)
2388 tag := ctx.OtherModuleDependencyTag(module)
2389
2390 switch dep := module.(type) {
2391 case Dependency:
2392 switch tag {
2393 case libTag, staticLibTag:
2394 // sdk lib names from dependencies are re-exported
2395 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2396 }
2397 case SdkLibraryDependency:
2398 switch tag {
2399 case libTag:
2400 // names of sdk libs that are directly depended are exported
2401 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2402 }
2403 }
2404 })
2405
2406 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002407 if Bool(j.properties.Installable) {
2408 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002409 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002410 }
Colin Cross2fe66872015-03-30 17:20:39 -07002411}
2412
Colin Cross74d73e22017-08-02 11:05:49 -07002413var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002414
Nan Zhanged19fc32017-10-19 13:06:22 -07002415func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002416 if j.combinedClasspathFile == nil {
2417 return nil
2418 }
Colin Cross37f6d792018-07-12 12:28:41 -07002419 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002420}
2421
2422func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002423 if j.combinedClasspathFile == nil {
2424 return nil
2425 }
Colin Cross37f6d792018-07-12 12:28:41 -07002426 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002427}
2428
Colin Cross331a1212018-08-15 20:40:52 -07002429func (j *Import) ResourceJars() android.Paths {
2430 return nil
2431}
2432
2433func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002434 if j.combinedClasspathFile == nil {
2435 return nil
2436 }
Colin Cross331a1212018-08-15 20:40:52 -07002437 return android.Paths{j.combinedClasspathFile}
2438}
2439
Colin Crossf24a22a2019-01-31 14:12:44 -08002440func (j *Import) DexJar() android.Path {
2441 return nil
2442}
2443
Colin Cross74d73e22017-08-02 11:05:49 -07002444func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002445 return nil
2446}
2447
Jiyong Park1be96912018-05-28 18:02:19 +09002448func (j *Import) ExportedSdkLibs() []string {
2449 return j.exportedSdkLibs
2450}
2451
Artur Satayev9cf46692019-11-26 18:08:34 +00002452func (j *Import) ExportedPlugins() (android.Paths, []string) {
2453 return nil, nil
2454}
2455
Colin Cross0c4ce212019-05-03 15:28:19 -07002456func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2457 return nil, nil
2458}
2459
Jiyong Park0f80c182020-01-31 02:49:53 +09002460func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09002461 // dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002462 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
2463 return true
2464 }
Jiyong Park0f80c182020-01-31 02:49:53 +09002465 // Also, a dependency to an sdk member is also considered as such. This is required because
2466 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002467 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
2468 return true
2469 }
2470 return false
Jiyong Park0f80c182020-01-31 02:49:53 +09002471}
2472
albaltai36ff7dc2018-12-25 14:35:23 +08002473// Add compile time check for interface implementation
2474var _ android.IDEInfo = (*Import)(nil)
2475var _ android.IDECustomizedModuleName = (*Import)(nil)
2476
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002477// Collect information for opening IDE project files in java/jdeps.go.
2478const (
2479 removedPrefix = "prebuilt_"
2480)
2481
2482func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2483 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2484}
2485
2486func (j *Import) IDECustomizedModuleName() string {
2487 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2488 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2489 // solution to get the Import name.
2490 name := j.Name()
2491 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002492 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002493 }
2494 return name
2495}
2496
Colin Cross74d73e22017-08-02 11:05:49 -07002497var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002498
Colin Cross1b16b0e2019-02-12 14:41:32 -08002499// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2500//
2501// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2502// compiled against an Android classpath.
2503//
2504// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2505// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002506func ImportFactory() android.Module {
2507 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002508
Colin Cross74d73e22017-08-02 11:05:49 -07002509 module.AddProperties(&module.properties)
2510
2511 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002512 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002513 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002514 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002515 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002516}
2517
Colin Cross1b16b0e2019-02-12 14:41:32 -08002518// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2519// module.
2520//
2521// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2522// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002523func ImportFactoryHost() android.Module {
2524 module := &Import{}
2525
2526 module.AddProperties(&module.properties)
2527
2528 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002529 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002530 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002531 return module
2532}
2533
Colin Cross42be7612019-02-21 18:12:14 -08002534// dex_import module
2535
2536type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002537 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002538
2539 // set the name of the output
2540 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002541}
2542
2543type DexImport struct {
2544 android.ModuleBase
2545 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002546 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002547 prebuilt android.Prebuilt
2548
2549 properties DexImportProperties
2550
2551 dexJarFile android.Path
2552 maybeStrippedDexJarFile android.Path
2553
2554 dexpreopter
2555}
2556
2557func (j *DexImport) Prebuilt() *android.Prebuilt {
2558 return &j.prebuilt
2559}
2560
2561func (j *DexImport) PrebuiltSrcs() []string {
2562 return j.properties.Jars
2563}
2564
2565func (j *DexImport) Name() string {
2566 return j.prebuilt.Name(j.ModuleBase.Name())
2567}
2568
Jiyong Park0b238752019-10-29 11:23:10 +09002569func (j *DexImport) Stem() string {
2570 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2571}
2572
Martin Stjernholm6d415272020-01-31 17:10:36 +00002573func (j *DexImport) IsInstallable() bool {
2574 return true
2575}
2576
Colin Cross42be7612019-02-21 18:12:14 -08002577func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2578 if len(j.properties.Jars) != 1 {
2579 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2580 }
2581
Jiyong Park0b238752019-10-29 11:23:10 +09002582 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002583 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2584
2585 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2586 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2587
2588 if j.dexpreopter.uncompressedDex {
2589 rule := android.NewRuleBuilder()
2590
2591 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2592 rule.Temporary(temporary)
2593
2594 // use zip2zip to uncompress classes*.dex files
2595 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002596 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002597 FlagWithInput("-i ", inputJar).
2598 FlagWithOutput("-o ", temporary).
2599 FlagWithArg("-0 ", "'classes*.dex'")
2600
2601 // use zipalign to align uncompressed classes*.dex files
2602 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002603 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002604 Flag("-f").
2605 Text("4").
2606 Input(temporary).
2607 Output(dexOutputFile)
2608
2609 rule.DeleteTemporaryFiles()
2610
2611 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2612 } else {
2613 ctx.Build(pctx, android.BuildParams{
2614 Rule: android.Cp,
2615 Input: inputJar,
2616 Output: dexOutputFile,
2617 })
2618 }
2619
2620 j.dexJarFile = dexOutputFile
2621
2622 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2623
2624 j.maybeStrippedDexJarFile = dexOutputFile
2625
2626 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2627 ctx.ModuleName()+".jar", dexOutputFile)
2628}
2629
2630func (j *DexImport) DexJar() android.Path {
2631 return j.dexJarFile
2632}
2633
2634// dex_import imports a `.jar` file containing classes.dex files.
2635//
2636// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2637// to the device.
2638func DexImportFactory() android.Module {
2639 module := &DexImport{}
2640
2641 module.AddProperties(&module.properties)
2642
2643 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002644 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002645 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002646 return module
2647}
2648
Colin Cross89536d42017-07-07 14:35:50 -07002649//
2650// Defaults
2651//
2652type Defaults struct {
2653 android.ModuleBase
2654 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002655 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002656}
2657
Colin Cross1b16b0e2019-02-12 14:41:32 -08002658// java_defaults provides a set of properties that can be inherited by other java or android modules.
2659//
2660// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2661// property in the defaults module that exists in the depending module will be prepended to the depending module's
2662// value for that property.
2663//
2664// Example:
2665//
2666// java_defaults {
2667// name: "example_defaults",
2668// srcs: ["common/**/*.java"],
2669// javacflags: ["-Xlint:all"],
2670// aaptflags: ["--auto-add-overlay"],
2671// }
2672//
2673// java_library {
2674// name: "example",
2675// defaults: ["example_defaults"],
2676// srcs: ["example/**/*.java"],
2677// }
2678//
2679// is functionally identical to:
2680//
2681// java_library {
2682// name: "example",
2683// srcs: [
2684// "common/**/*.java",
2685// "example/**/*.java",
2686// ],
2687// javacflags: ["-Xlint:all"],
2688// }
Colin Cross89536d42017-07-07 14:35:50 -07002689func defaultsFactory() android.Module {
2690 return DefaultsFactory()
2691}
2692
Paul Duffin47357662019-12-05 14:07:14 +00002693func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002694 module := &Defaults{}
2695
Colin Cross89536d42017-07-07 14:35:50 -07002696 module.AddProperties(
2697 &CompilerProperties{},
2698 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002699 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002700 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002701 &aaptProperties{},
2702 &androidLibraryProperties{},
2703 &appProperties{},
2704 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002705 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002706 &ImportProperties{},
2707 &AARImportProperties{},
2708 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002709 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002710 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002711 )
2712
2713 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002714 return module
2715}
Nan Zhangea568a42017-11-08 21:20:04 -08002716
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002717func kytheExtractJavaFactory() android.Singleton {
2718 return &kytheExtractJavaSingleton{}
2719}
2720
2721type kytheExtractJavaSingleton struct {
2722}
2723
2724func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2725 var xrefTargets android.Paths
2726 ctx.VisitAllModules(func(module android.Module) {
2727 if javaModule, ok := module.(xref); ok {
2728 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2729 }
2730 })
2731 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2732 if len(xrefTargets) > 0 {
2733 ctx.Build(pctx, android.BuildParams{
2734 Rule: blueprint.Phony,
2735 Output: android.PathForPhony(ctx, "xref_java"),
2736 Inputs: xrefTargets,
2737 })
2738 }
2739}
2740
Nan Zhangea568a42017-11-08 21:20:04 -08002741var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002742var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002743var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002744var inList = android.InList