blob: 46adedcd13fe8d096740305447faf95acce68d57 [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"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700322}
323
Sasha Smundak2057f822019-04-16 17:16:58 -0700324func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
325 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
326}
327
Colin Cross46c9b8b2017-06-22 16:51:17 -0700328// Module contains the properties and members used by all java module types
329type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700330 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700331 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900332 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900333 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700334
Colin Cross89536d42017-07-07 14:35:50 -0700335 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700336 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700337 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700338
Colin Cross331a1212018-08-15 20:40:52 -0700339 // jar file containing header classes including static library dependencies, suitable for
340 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700341 headerJarFile android.Path
342
Colin Cross331a1212018-08-15 20:40:52 -0700343 // jar file containing implementation classes including static library dependencies but no
344 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700345 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700346
Colin Cross331a1212018-08-15 20:40:52 -0700347 // jar file containing only resources including from static library dependencies
348 resourceJar android.Path
349
Colin Cross0c4ce212019-05-03 15:28:19 -0700350 // args and dependencies to package source files into a srcjar
351 srcJarArgs []string
352 srcJarDeps android.Paths
353
Colin Cross331a1212018-08-15 20:40:52 -0700354 // jar file containing implementation classes and resources including static library
355 // dependencies
356 implementationAndResourcesJar android.Path
357
358 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700359 dexJarFile android.Path
360
Colin Cross43f08db2018-11-12 10:13:39 -0800361 // output file that contains classes.dex if it should be in the output file
362 maybeStrippedDexJarFile android.Path
363
Colin Crosscb933592017-11-22 13:49:43 -0800364 // output file containing uninstrumented classes that will be instrumented by jacoco
365 jacocoReportClassesFile android.Path
366
Colin Cross66dbc0b2017-12-28 12:23:20 -0800367 // output file containing mapping of obfuscated names
368 proguardDictionary android.Path
369
Colin Cross331a1212018-08-15 20:40:52 -0700370 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700371 outputFile android.Path
372 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700373
Colin Cross635c3b02016-05-18 15:37:25 -0700374 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700375
Colin Cross635c3b02016-05-18 15:37:25 -0700376 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700377
Colin Cross2fe66872015-03-30 17:20:39 -0700378 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700379 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800380
381 // list of .java files and srcjars that was passed to javac
382 compiledJavaSrcs android.Paths
383 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800384
385 // list of extra progurad flag files
386 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900387
Colin Cross094054a2018-10-17 15:10:48 -0700388 // manifest file to use instead of properties.Manifest
389 overrideManifest android.OptionalPath
390
Artur Satayev9cf46692019-11-26 18:08:34 +0000391 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900392 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700393
Artur Satayev9cf46692019-11-26 18:08:34 +0000394 // list of plugins that this java module is exporting
395 exportedPluginJars android.Paths
396
397 // list of plugins that this java module is exporting
398 exportedPluginClasses []string
399
400 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800401 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700402 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800403
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800404 // expanded Jarjar_rules
405 expandJarjarRules android.Path
406
Vladimir Marko0975ee02019-04-02 10:29:55 +0100407 // list of additional targets for checkbuild
408 additionalCheckedModules android.Paths
409
Colin Cross988708c2019-05-06 14:04:11 -0700410 // Extra files generated by the module type to be added as java resources.
411 extraResources android.Paths
412
Colin Crossf24a22a2019-01-31 14:12:44 -0800413 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800414 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800415
416 // list of the xref extraction files
417 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700418}
419
Colin Cross41955e82019-05-29 14:40:35 -0700420func (j *Module) OutputFiles(tag string) (android.Paths, error) {
421 switch tag {
422 case "":
423 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700424 case ".jar":
425 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700426 case ".proguard_map":
427 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700428 default:
429 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
430 }
Colin Cross54250902017-12-05 09:28:08 -0800431}
432
Colin Cross41955e82019-05-29 14:40:35 -0700433var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800434
Colin Crossf506d872017-07-19 15:53:04 -0700435type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700436 HeaderJars() android.Paths
437 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700438 ResourceJars() android.Paths
439 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800440 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700441 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900442 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000443 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700444 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700445 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900446 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700447}
448
Jiyong Parkc678ad32018-04-10 13:07:10 +0900449type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900450 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
451 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452}
453
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800454type xref interface {
455 XrefJavaFiles() android.Paths
456}
457
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800458func (j *Module) XrefJavaFiles() android.Paths {
459 return j.kytheFiles
460}
461
Colin Cross89536d42017-07-07 14:35:50 -0700462func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
463 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
464 android.InitDefaultableModule(module)
465}
466
Colin Crossbe1da472017-07-07 15:59:46 -0700467type dependencyTag struct {
468 blueprint.BaseDependencyTag
469 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700470}
471
Colin Crossa4f08812018-10-02 22:03:40 -0700472type jniDependencyTag struct {
473 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700474}
475
Jiyong Park8be103b2019-11-08 15:53:48 +0900476func IsJniDepTag(depTag blueprint.DependencyTag) bool {
477 _, ok := depTag.(*jniDependencyTag)
478 return ok
479}
480
Colin Crossbe1da472017-07-07 15:59:46 -0700481var (
Colin Cross4b964c02018-10-15 16:18:06 -0700482 staticLibTag = dependencyTag{name: "staticlib"}
483 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700484 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800485 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000486 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700487 bootClasspathTag = dependencyTag{name: "bootclasspath"}
488 systemModulesTag = dependencyTag{name: "system modules"}
489 frameworkResTag = dependencyTag{name: "framework-res"}
490 frameworkApkTag = dependencyTag{name: "framework-apk"}
491 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800492 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700493 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
494 certificateTag = dependencyTag{name: "certificate"}
495 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700496 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700497)
Colin Cross2fe66872015-03-30 17:20:39 -0700498
Jiyong Park83dc74b2020-01-14 18:38:44 +0900499func IsLibDepTag(depTag blueprint.DependencyTag) bool {
500 return depTag == libTag
501}
502
503func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
504 return depTag == staticLibTag
505}
506
Colin Crossfc3674a2017-09-18 17:41:52 -0700507type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700508 useModule, useFiles, useDefaultLibs, invalidVersion bool
509
Colin Cross6cef4812019-10-17 14:23:50 -0700510 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
511 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100512
513 // The default system modules to use. Will be an empty string if no system
514 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700515 systemModules string
516
Colin Cross6cef4812019-10-17 14:23:50 -0700517 // The modules that will be added ot the classpath when targeting 1.9 or higher
518 java9Classpath []string
519
Colin Crossa97c5d32018-03-28 14:58:31 -0700520 frameworkResModule string
521
Colin Cross86a60ae2018-05-29 14:44:55 -0700522 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700523 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100524
525 noStandardLibs, noFrameworksLibs bool
526}
527
528func (s sdkDep) hasStandardLibs() bool {
529 return !s.noStandardLibs
530}
531
532func (s sdkDep) hasFrameworkLibs() bool {
533 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700534}
535
Colin Crossa4f08812018-10-02 22:03:40 -0700536type jniLib struct {
537 name string
538 path android.Path
539 target android.Target
540}
541
Colin Cross0ea8ba82019-06-06 14:33:29 -0700542func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800543 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
544}
545
Colin Cross0ea8ba82019-06-06 14:33:29 -0700546func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800547 return j.shouldInstrument(ctx) &&
548 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
549 ctx.Config().UnbundledBuild())
550}
551
Jiyong Park6a927c42020-01-21 02:03:43 +0900552func (j *Module) sdkVersion() sdkSpec {
553 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700554}
555
Paul Duffine25c6442019-10-11 13:50:28 +0100556func (j *Module) systemModules() string {
557 return proptools.String(j.deviceProperties.System_modules)
558}
559
Jiyong Park6a927c42020-01-21 02:03:43 +0900560func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700561 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900562 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700563 }
564 return j.sdkVersion()
565}
566
Jiyong Park6a927c42020-01-21 02:03:43 +0900567func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700568 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900569 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700570 }
571 return j.sdkVersion()
572}
573
Jiyong Parkb02bb402019-12-03 00:43:57 +0900574func (j *Module) AvailableFor(what string) bool {
575 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
576 // Exception: for hostdex: true libraries, the platform variant is created
577 // even if it's not marked as available to platform. In that case, the platform
578 // variant is used only for the hostdex and not installed to the device.
579 return true
580 }
581 return j.ApexModuleBase.AvailableFor(what)
582}
583
Colin Crossbe1da472017-07-07 15:59:46 -0700584func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700585 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100586 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700587 if sdkDep.useDefaultLibs {
588 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
589 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
590 if sdkDep.hasFrameworkLibs() {
591 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700592 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700593 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700594 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100595 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700596 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700597 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
598 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
599 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
600 }
Colin Cross2fe66872015-03-30 17:20:39 -0700601 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700602
Nan Zhangb2b33de2018-02-23 11:18:47 -0800603 if ctx.ModuleName() == "android_stubs_current" ||
604 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700605 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700606 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800607 }
Colin Cross2fe66872015-03-30 17:20:39 -0700608 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700609
Inseob Kimac1e9862019-12-09 18:15:47 +0900610 syspropPublicStubs := syspropPublicStubs(ctx.Config())
611
612 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
613 // and redirects dependency to public stub depending on the link type.
614 rewriteSyspropLibs := func(libs []string, prop string) []string {
615 // make a copy
616 ret := android.CopyOf(libs)
617
618 for idx, lib := range libs {
619 stub, ok := syspropPublicStubs[lib]
620
621 if !ok {
622 continue
623 }
624
625 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900626 // only platform modules can use internal props
627 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900628 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900629 }
630 }
631
632 return ret
633 }
634
635 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
636 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700637
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700638 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000639 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800640
Colin Crossfe17f6f2019-03-28 19:30:56 -0700641 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700642 if j.hasSrcExt(".proto") {
643 protoDeps(ctx, &j.protoProperties)
644 }
Colin Cross93e85952017-08-15 13:34:18 -0700645
646 if j.hasSrcExt(".kt") {
647 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
648 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700649 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
650 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800651 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800652 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
653 }
Colin Cross93e85952017-08-15 13:34:18 -0700654 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800655
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000656 // Framework libraries need special handling in static coverage builds: they should not have
657 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
658 // the same jacoco classes coming from different bootclasspath jars.
659 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
660 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
661 j.properties.Instrument = true
662 }
663 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700664 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800665 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700666}
667
668func hasSrcExt(srcs []string, ext string) bool {
669 for _, src := range srcs {
670 if filepath.Ext(src) == ext {
671 return true
672 }
673 }
674
675 return false
676}
677
678func (j *Module) hasSrcExt(ext string) bool {
679 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700680}
681
Colin Cross46c9b8b2017-06-22 16:51:17 -0700682func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700683 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700684
Colin Crossebe1a512017-11-14 13:12:14 -0800685 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
686 aidlIncludes = append(aidlIncludes,
687 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
688 aidlIncludes = append(aidlIncludes,
689 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700690
Colin Cross3047fa22019-04-18 10:56:44 -0700691 var flags []string
692 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700693
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700694 if aidlPreprocess.Valid() {
695 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700696 deps = append(deps, aidlPreprocess.Path())
697 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700698 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700699 }
700
Colin Cross3047fa22019-04-18 10:56:44 -0700701 if len(j.exportAidlIncludeDirs) > 0 {
702 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
703 }
704
705 if len(aidlIncludes) > 0 {
706 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
707 }
708
Colin Cross635c3b02016-05-18 15:37:25 -0700709 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800710 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700711 flags = append(flags, "-I"+src.String())
712 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700713
Martijn Coeneneab15642018-03-09 09:29:59 +0100714 if Bool(j.deviceProperties.Aidl.Generate_traces) {
715 flags = append(flags, "-t")
716 }
717
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100718 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
719 flags = append(flags, "--transaction_names")
720 }
721
Colin Cross3047fa22019-04-18 10:56:44 -0700722 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700723}
724
Colin Cross32f676a2017-09-06 13:41:06 -0700725type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800726 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700727 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800728 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700729 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800730 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700731 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700732 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700733 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700734 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800735 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700736 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700737 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700738 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700739 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800740 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800741
742 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700743}
Colin Cross2fe66872015-03-30 17:20:39 -0700744
Colin Cross54250902017-12-05 09:28:08 -0800745func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
746 for _, f := range dep.Srcs() {
747 if f.Ext() != ".jar" {
748 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
749 ctx.OtherModuleName(dep.(blueprint.Module)))
750 }
751 }
752}
753
Jiyong Park2d492942018-03-05 17:44:10 +0900754type linkType int
755
756const (
Jiyong Park50146e92020-01-30 18:00:15 +0900757 // TODO(jiyong) rename these for better readability. Make the allowed
758 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900759 javaCore linkType = iota
760 javaSdk
761 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900762 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900763 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +0900764 javaPlatform
765)
766
Jeongik Cha75b83b02019-11-01 15:28:00 +0900767type linkTypeContext interface {
768 android.Module
769 getLinkType(name string) (ret linkType, stubs bool)
770}
771
772func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700773 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700774 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900775 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
776 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100777 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900778 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900779 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900780 return javaCore, false
781 case name == "android_system_stubs_current":
782 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900783 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900784 return javaSystem, false
785 case name == "android_test_stubs_current":
786 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900787 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900788 return javaPlatform, false
789 case name == "android_stubs_current":
790 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900791 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900792 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900793 case name == "android_module_lib_stubs_current":
794 return javaModule, true
795 case ver.kind == sdkModule:
796 return javaModule, false
Anton Hanssonbbd78552020-03-19 15:23:38 +0000797 case name == "android_system_server_stubs_current":
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900798 return javaSystemServer, true
799 case ver.kind == sdkSystemServer:
800 return javaSystemServer, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900801 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900802 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900803 case !ver.valid():
804 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700805 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900806 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900807 }
808}
809
Jeongik Cha75b83b02019-11-01 15:28:00 +0900810func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700811 if ctx.Host() {
812 return
813 }
814
Jeongik Cha75b83b02019-11-01 15:28:00 +0900815 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900816 if stubs {
817 return
818 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900819 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900820 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."
821
822 switch myLinkType {
823 case javaCore:
824 if otherLinkType != javaCore {
825 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 +0900826 ctx.OtherModuleName(to))
827 }
Jiyong Park2d492942018-03-05 17:44:10 +0900828 break
829 case javaSdk:
830 if otherLinkType != javaCore && otherLinkType != javaSdk {
831 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
832 ctx.OtherModuleName(to))
833 }
834 break
835 case javaSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900836 if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer {
Jiyong Park2d492942018-03-05 17:44:10 +0900837 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
838 ctx.OtherModuleName(to))
839 }
840 break
Jiyong Park50146e92020-01-30 18:00:15 +0900841 case javaModule:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900842 if otherLinkType == javaPlatform || otherLinkType == javaSystemServer {
Jiyong Park50146e92020-01-30 18:00:15 +0900843 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
844 ctx.OtherModuleName(to))
845 }
846 break
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900847 case javaSystemServer:
848 if otherLinkType == javaPlatform {
849 ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage,
850 ctx.OtherModuleName(to))
851 }
852 break
Jiyong Park2d492942018-03-05 17:44:10 +0900853 case javaPlatform:
854 // no restriction on link-type
855 break
Jiyong Park750e5572018-01-31 00:20:13 +0900856 }
857}
858
Colin Cross32f676a2017-09-06 13:41:06 -0700859func (j *Module) collectDeps(ctx android.ModuleContext) deps {
860 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700861
Colin Cross300f0382018-03-06 13:11:51 -0800862 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700863 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800864 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700865 ctx.AddMissingDependencies(sdkDep.bootclasspath)
866 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800867 } else if sdkDep.useFiles {
868 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700869 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700870 deps.aidlPreprocess = sdkDep.aidl
871 } else {
872 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800873 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700874 }
875
Colin Crossd11fcda2017-10-23 17:59:01 -0700876 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700877 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700878 tag := ctx.OtherModuleDependencyTag(module)
879
Colin Crossa4f08812018-10-02 22:03:40 -0700880 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700881 // Handled by AndroidApp.collectAppDeps
882 return
883 }
884 if tag == certificateTag {
885 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700886 return
887 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900888 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900889 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900890 if to, ok := module.(linkTypeContext); ok {
891 switch tag {
892 case bootClasspathTag, libTag, staticLibTag:
893 checkLinkType(ctx, j, to, tag.(dependencyTag))
894 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700895 }
Jiyong Park750e5572018-01-31 00:20:13 +0900896 }
Colin Cross54250902017-12-05 09:28:08 -0800897 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800898 case SdkLibraryDependency:
899 switch tag {
900 case libTag:
901 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
902 // names of sdk libs that are directly depended are exported
903 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700904 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800905 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
906 }
Colin Cross54250902017-12-05 09:28:08 -0800907 case Dependency:
908 switch tag {
909 case bootClasspathTag:
910 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700911 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800912 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900913 // sdk lib names from dependencies are re-exported
914 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700915 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000916 pluginJars, pluginClasses := dep.ExportedPlugins()
917 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700918 case java9LibTag:
919 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800920 case staticLibTag:
921 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
922 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
923 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700924 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900925 // sdk lib names from dependencies are re-exported
926 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700927 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000928 pluginJars, pluginClasses := dep.ExportedPlugins()
929 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800930 case pluginTag:
931 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800932 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000933 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
934 } else {
935 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800936 }
937 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
938 } else {
939 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
940 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000941 case exportedPluginTag:
942 if plugin, ok := dep.(*Plugin); ok {
943 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
944 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
945 }
946 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
947 if plugin.pluginProperties.Processor_class != nil {
948 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
949 }
950 } else {
951 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
952 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800953 case frameworkApkTag:
954 if ctx.ModuleName() == "android_stubs_current" ||
955 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700956 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800957 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
958 // resource files out of there for aapt.
959 //
960 // Normally the package rule runs aapt, which includes the resource,
961 // but we're not running that in our package rule so just copy in the
962 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700963 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800964 }
Colin Cross54250902017-12-05 09:28:08 -0800965 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700966 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800967 case kotlinAnnotationsTag:
968 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800969 }
970
Colin Cross54250902017-12-05 09:28:08 -0800971 case android.SourceFileProducer:
972 switch tag {
973 case libTag:
974 checkProducesJars(ctx, dep)
975 deps.classpath = append(deps.classpath, dep.Srcs()...)
976 case staticLibTag:
977 checkProducesJars(ctx, dep)
978 deps.classpath = append(deps.classpath, dep.Srcs()...)
979 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
980 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800981 }
982 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700983 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100984 case bootClasspathTag:
985 // If a system modules dependency has been added to the bootclasspath
986 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000987 sm := module.(SystemModulesProvider)
988 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +0100989
Colin Cross1369cdb2017-09-29 17:58:17 -0700990 case systemModulesTag:
991 if deps.systemModules != nil {
992 panic("Found two system module dependencies")
993 }
Paul Duffin83a2d962019-11-19 19:44:10 +0000994 sm := module.(SystemModulesProvider)
995 outputDir, outputDeps := sm.OutputDirAndDeps()
996 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700997 }
Colin Crossec7a0422017-07-07 14:47:12 -0700998 }
Colin Cross2fe66872015-03-30 17:20:39 -0700999 })
1000
Jiyong Park1be96912018-05-28 18:02:19 +09001001 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1002
Colin Cross32f676a2017-09-06 13:41:06 -07001003 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001004}
1005
Artur Satayev9cf46692019-11-26 18:08:34 +00001006func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1007 deps.processorPath = append(deps.processorPath, pluginJars...)
1008 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1009}
1010
Colin Cross1e743852019-10-28 11:37:20 -07001011func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001012 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001013 if err != nil {
1014 ctx.PropertyErrorf("sdk_version", "%s", err)
1015 }
Nan Zhang357466b2018-04-17 17:38:36 -07001016 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001017 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001018 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001019 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001020 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001021 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001022 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1023 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001024 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001025 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001026 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001027 }
Nan Zhang357466b2018-04-17 17:38:36 -07001028}
1029
Colin Cross1e743852019-10-28 11:37:20 -07001030type javaVersion int
1031
1032const (
1033 JAVA_VERSION_UNSUPPORTED = 0
1034 JAVA_VERSION_6 = 6
1035 JAVA_VERSION_7 = 7
1036 JAVA_VERSION_8 = 8
1037 JAVA_VERSION_9 = 9
1038)
1039
1040func (v javaVersion) String() string {
1041 switch v {
1042 case JAVA_VERSION_6:
1043 return "1.6"
1044 case JAVA_VERSION_7:
1045 return "1.7"
1046 case JAVA_VERSION_8:
1047 return "1.8"
1048 case JAVA_VERSION_9:
1049 return "1.9"
1050 default:
1051 return "unsupported"
1052 }
1053}
1054
1055// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1056func (v javaVersion) usesJavaModules() bool {
1057 return v >= 9
1058}
1059
1060func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001061 switch javaVersion {
1062 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001063 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001064 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001065 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001066 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001067 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001068 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001069 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001070 case "10", "11":
1071 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001072 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001073 default:
1074 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001075 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001076 }
1077}
1078
Nan Zhanged19fc32017-10-19 13:06:22 -07001079func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001080
Colin Crossf03c82b2015-04-13 13:53:40 -07001081 var flags javaBuilderFlags
1082
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001083 // javaVersion flag.
1084 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1085
Nan Zhanged19fc32017-10-19 13:06:22 -07001086 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001087 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001088 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001089 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001090 }
Colin Cross6510f912017-11-29 00:27:14 -08001091 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001092 // Override the -g flag passed globally to remove local variable debug info to reduce
1093 // disk and memory usage.
1094 javacFlags = append(javacFlags, "-g:source,lines")
1095 }
Colin Crossc228a702019-11-06 16:18:05 -08001096 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001097
Colin Cross66548102018-06-19 22:47:35 -07001098 if ctx.Config().RunErrorProne() {
1099 if config.ErrorProneClasspath == nil {
1100 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1101 }
1102
1103 errorProneFlags := []string{
1104 "-Xplugin:ErrorProne",
1105 "${config.ErrorProneChecks}",
1106 }
1107 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1108
1109 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1110 "'" + strings.Join(errorProneFlags, " ") + "'"
1111 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001112 }
1113
Nan Zhanged19fc32017-10-19 13:06:22 -07001114 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001115 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1116 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001117 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001118 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001119
Colin Crossbe9cdb82019-01-21 21:37:16 -08001120 flags.processor = strings.Join(deps.processorClasses, ",")
1121
Colin Cross1e743852019-10-28 11:37:20 -07001122 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1123 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001124 // Give host-side tools a version of OpenJDK's standard libraries
1125 // close to what they're targeting. As of Dec 2017, AOSP is only
1126 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1127 //
1128 // When building with OpenJDK 8, the following should have no
1129 // effect since those jars would be available by default.
1130 //
1131 // When building with OpenJDK 9 but targeting a version < 1.8,
1132 // putting them on the bootclasspath means that:
1133 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1134 // b) references to existing APIs are not reinterpreted in an
1135 // OpenJDK 9-specific way, eg. calls to subclasses of
1136 // java.nio.Buffer as in http://b/70862583
1137 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1138 flags.bootClasspath = append(flags.bootClasspath,
1139 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1140 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001141 if Bool(j.properties.Use_tools_jar) {
1142 flags.bootClasspath = append(flags.bootClasspath,
1143 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1144 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001145 }
1146
Colin Cross1e743852019-10-28 11:37:20 -07001147 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001148 // Manually specify build directory in case it is not under the repo root.
1149 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1150 // just adding a symlink under the root doesn't help.)
1151 patchPaths := ".:" + ctx.Config().BuildDir()
1152 classPath := flags.classpath.FormJavaClassPath("")
1153 if classPath != "" {
1154 patchPaths += ":" + classPath
1155 }
1156 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001157 }
1158
Nan Zhanged19fc32017-10-19 13:06:22 -07001159 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001160 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001161
Nan Zhanged19fc32017-10-19 13:06:22 -07001162 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001163 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001164
Colin Cross81440082018-08-15 20:21:55 -07001165 if len(javacFlags) > 0 {
1166 // optimization.
1167 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1168 flags.javacFlags = "$javacFlags"
1169 }
1170
Nan Zhanged19fc32017-10-19 13:06:22 -07001171 return flags
1172}
Colin Crossc0b06f12015-04-08 13:03:43 -07001173
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001174func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001175 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001176
1177 deps := j.collectDeps(ctx)
1178 flags := j.collectBuilderFlags(ctx, deps)
1179
Colin Cross1e743852019-10-28 11:37:20 -07001180 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001181 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1182 }
Colin Cross8a497952019-03-05 22:25:09 -08001183 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001184 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001185 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001186 }
1187
Colin Crossaf050172017-11-15 23:01:59 -08001188 srcFiles = j.genSources(ctx, srcFiles, flags)
1189
1190 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001191 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001192 if aaptSrcJar != nil {
1193 srcJars = append(srcJars, aaptSrcJar)
1194 }
Colin Crossb7a63242015-04-16 14:09:14 -07001195
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001196 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001197 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001198 }
1199
Colin Cross1ee23172017-10-18 14:44:18 -07001200 jarName := ctx.ModuleName() + ".jar"
1201
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001202 javaSrcFiles := srcFiles.FilterByExt(".java")
1203 var uniqueSrcFiles android.Paths
1204 set := make(map[string]bool)
1205 for _, v := range javaSrcFiles {
1206 if _, found := set[v.String()]; !found {
1207 set[v.String()] = true
1208 uniqueSrcFiles = append(uniqueSrcFiles, v)
1209 }
1210 }
1211
patricktu242faad2019-09-24 15:41:30 +08001212 // Collect .java files for AIDEGen
1213 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1214
Colin Cross55f63ea2018-08-27 12:37:09 -07001215 var kotlinJars android.Paths
1216
Colin Cross93e85952017-08-15 13:34:18 -07001217 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001218 // user defined kotlin flags.
1219 kotlincFlags := j.properties.Kotlincflags
1220 CheckKotlincFlags(ctx, kotlincFlags)
1221
Colin Cross93e85952017-08-15 13:34:18 -07001222 // If there are kotlin files, compile them first but pass all the kotlin and java files
1223 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1224 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001225 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001226 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001227 kotlincFlags = append(kotlincFlags, "-no-jdk")
1228 }
1229 if len(kotlincFlags) > 0 {
1230 // optimization.
1231 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1232 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001233 }
1234
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001235 var kotlinSrcFiles android.Paths
1236 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1237 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1238
patricktu242faad2019-09-24 15:41:30 +08001239 // Collect .kt files for AIDEGen
1240 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1241
Colin Crossafbb1732019-01-17 15:42:52 -08001242 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1243 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1244
1245 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1246 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1247
1248 if len(flags.processorPath) > 0 {
1249 // Use kapt for annotation processing
1250 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1251 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1252 srcJars = append(srcJars, kaptSrcJar)
1253 // Disable annotation processing in javac, it's already been handled by kapt
1254 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001255 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001256 }
Colin Cross93e85952017-08-15 13:34:18 -07001257
Colin Cross1ee23172017-10-18 14:44:18 -07001258 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001259 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001260 if ctx.Failed() {
1261 return
1262 }
1263
1264 // Make javac rule depend on the kotlinc rule
1265 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001266
Colin Cross93e85952017-08-15 13:34:18 -07001267 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001268 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001269 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001270 }
1271
Colin Cross55f63ea2018-08-27 12:37:09 -07001272 jars := append(android.Paths(nil), kotlinJars...)
1273
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001274 // Store the list of .java files that was passed to javac
1275 j.compiledJavaSrcs = uniqueSrcFiles
1276 j.compiledSrcJars = srcJars
1277
Nan Zhang61eaedb2017-11-02 13:28:15 -07001278 enable_sharding := false
Colin Crossf5a66282020-02-21 08:16:41 -08001279 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001280 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001281 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1282 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001283 // Formerly, there was a check here that prevented annotation processors
1284 // from being used when sharding was enabled, as some annotation processors
1285 // do not function correctly in sharded environments. It was removed to
1286 // allow for the use of annotation processors that do function correctly
1287 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001288 }
Colin Crossf5a66282020-02-21 08:16:41 -08001289 headerJarFileWithoutJarjar, j.headerJarFile =
1290 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001291 if ctx.Failed() {
1292 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001293 }
1294 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001295 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001296 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001297 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001298 // If error-prone is enabled, add an additional rule to compile the java files into
1299 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001300 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001301 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1302 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001303 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001304 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001305 extraJarDeps = append(extraJarDeps, errorprone)
1306 }
1307
Nan Zhang61eaedb2017-11-02 13:28:15 -07001308 if enable_sharding {
Colin Crossf5a66282020-02-21 08:16:41 -08001309 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001310 shardSize := int(*(j.properties.Javac_shard_size))
1311 var shardSrcs []android.Paths
1312 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001313 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001314 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001315 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1316 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001317 jars = append(jars, classes)
1318 }
1319 }
1320 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001321 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1322 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001323 jars = append(jars, classes)
1324 }
1325 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001326 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001327 jars = append(jars, classes)
1328 }
Colin Crossd6891432017-09-27 17:39:56 -07001329 if ctx.Failed() {
1330 return
1331 }
Colin Cross2fe66872015-03-30 17:20:39 -07001332 }
1333
Colin Cross0c4ce212019-05-03 15:28:19 -07001334 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1335
1336 var includeSrcJar android.WritablePath
1337 if Bool(j.properties.Include_srcs) {
1338 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1339 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1340 }
1341
Colin Crosscedd4762018-09-13 11:26:19 -07001342 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1343 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001344 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001345 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001346
1347 var resArgs []string
1348 var resDeps android.Paths
1349
1350 resArgs = append(resArgs, dirArgs...)
1351 resDeps = append(resDeps, dirDeps...)
1352
1353 resArgs = append(resArgs, fileArgs...)
1354 resDeps = append(resDeps, fileDeps...)
1355
Colin Cross988708c2019-05-06 14:04:11 -07001356 resArgs = append(resArgs, extraArgs...)
1357 resDeps = append(resDeps, extraDeps...)
1358
Colin Cross40a36712017-09-27 17:41:35 -07001359 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001360 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001361 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001362 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001363 if ctx.Failed() {
1364 return
1365 }
1366 }
1367
Colin Cross0c4ce212019-05-03 15:28:19 -07001368 var resourceJars android.Paths
1369 if j.resourceJar != nil {
1370 resourceJars = append(resourceJars, j.resourceJar)
1371 }
1372 if Bool(j.properties.Include_srcs) {
1373 resourceJars = append(resourceJars, includeSrcJar)
1374 }
1375 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001376
Colin Cross0c4ce212019-05-03 15:28:19 -07001377 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001378 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001379 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001380 false, nil, nil)
1381 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001382 } else if len(resourceJars) == 1 {
1383 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001384 }
1385
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001386 if len(deps.staticJars) > 0 {
1387 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001388 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001389
Colin Cross094054a2018-10-17 15:10:48 -07001390 manifest := j.overrideManifest
1391 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001392 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001393 }
Colin Cross635acc92017-09-12 22:50:46 -07001394
Colin Cross8a497952019-03-05 22:25:09 -08001395 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001396 if len(services) > 0 {
1397 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1398 var zipargs []string
1399 for _, file := range services {
1400 serviceFile := file.String()
1401 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1402 }
1403 ctx.Build(pctx, android.BuildParams{
1404 Rule: zip,
1405 Output: servicesJar,
1406 Implicits: services,
1407 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001408 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001409 },
1410 })
1411 jars = append(jars, servicesJar)
1412 }
1413
Colin Cross0a6e0072017-08-30 14:24:55 -07001414 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001415 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001416 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001417
1418 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001419 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1420 // Optimization: skip the combine step if there is nothing to do
1421 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1422 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1423 // any if len(jars) == 1.
1424 outputFile = moduleOutPath
1425 } else {
1426 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1427 ctx.Build(pctx, android.BuildParams{
1428 Rule: android.Cp,
1429 Input: jars[0],
1430 Output: combinedJar,
1431 })
1432 outputFile = combinedJar
1433 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001434 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001435 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001436 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001437 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001438 outputFile = combinedJar
1439 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001440
Colin Cross331a1212018-08-15 20:40:52 -07001441 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001442 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001443 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001444 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001445 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001446 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001447
1448 // jarjar resource jar if necessary
1449 if j.resourceJar != nil {
1450 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001451 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001452 j.resourceJar = resourceJarJarFile
1453 }
1454
Colin Cross0a6e0072017-08-30 14:24:55 -07001455 if ctx.Failed() {
1456 return
1457 }
1458 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001459
1460 // Check package restrictions if necessary.
1461 if len(j.properties.Permitted_packages) > 0 {
1462 // Check packages and copy to package-checked file.
1463 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1464 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1465 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1466
1467 if ctx.Failed() {
1468 return
1469 }
1470 }
1471
Nan Zhanged19fc32017-10-19 13:06:22 -07001472 j.implementationJarFile = outputFile
1473 if j.headerJarFile == nil {
1474 j.headerJarFile = j.implementationJarFile
1475 }
Colin Cross2fe66872015-03-30 17:20:39 -07001476
Jiyong Park28826602020-02-21 16:04:53 +09001477 // Force enable the instrumentation for java code that is built for APEXes ...
1478 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1479 // doesn't make sense)
1480 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1481 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park81aaa0c2020-02-18 12:50:44 +00001482 j.properties.Instrument = true
1483 }
1484
Colin Cross3144dfc2018-01-03 15:06:47 -08001485 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001486 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1487 }
1488
Colin Cross331a1212018-08-15 20:40:52 -07001489 // merge implementation jar with resources if necessary
1490 implementationAndResourcesJar := outputFile
1491 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001492 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001493 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001494 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001495 false, nil, nil)
1496 implementationAndResourcesJar = combinedJar
1497 }
1498
1499 j.implementationAndResourcesJar = implementationAndResourcesJar
1500
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001501 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1502 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1503 if j.deviceProperties.Compile_dex == nil {
1504 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1505 }
1506 if j.deviceProperties.Hostdex == nil {
1507 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1508 }
1509 }
1510
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001511 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001512 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001513 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001514 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001515 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001516 if ctx.Failed() {
1517 return
1518 }
Colin Cross331a1212018-08-15 20:40:52 -07001519
Jiyong Park09cb6292019-07-15 15:29:23 +09001520 // Hidden API CSV generation and dex encoding
1521 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1522 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001523
Colin Cross331a1212018-08-15 20:40:52 -07001524 // merge dex jar with resources if necessary
1525 if j.resourceJar != nil {
1526 jars := android.Paths{dexOutputFile, j.resourceJar}
1527 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1528 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1529 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001530 if j.deviceProperties.UncompressDex {
1531 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1532 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1533 dexOutputFile = combinedAlignedJar
1534 } else {
1535 dexOutputFile = combinedJar
1536 }
Colin Cross331a1212018-08-15 20:40:52 -07001537 }
1538
1539 j.dexJarFile = dexOutputFile
1540
Colin Cross8faf8fc2019-01-16 15:15:52 -08001541 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001542 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1543
1544 j.maybeStrippedDexJarFile = dexOutputFile
1545
Colin Cross3063b782018-08-15 11:19:12 -07001546 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001547
1548 if ctx.Failed() {
1549 return
1550 }
Colin Cross331a1212018-08-15 20:40:52 -07001551 } else {
1552 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001553 }
Colin Cross331a1212018-08-15 20:40:52 -07001554
Colin Crossb7a63242015-04-16 14:09:14 -07001555 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001556
1557 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1558 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001559}
1560
Colin Cross3b706fd2019-09-05 16:44:18 -07001561func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1562 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1563
1564 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1565 if idx >= 0 {
1566 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1567 jarName += strconv.Itoa(idx)
1568 }
1569
1570 classes := android.PathForModuleOut(ctx, "javac", jarName)
1571 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1572
1573 if ctx.Config().EmitXrefRules() {
1574 extractionFile := android.PathForModuleOut(ctx, kzipName)
1575 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1576 j.kytheFiles = append(j.kytheFiles, extractionFile)
1577 }
1578
1579 return classes
1580}
1581
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001582// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1583// since some of these flags may be used internally.
1584func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1585 for _, flag := range flags {
1586 flag = strings.TrimSpace(flag)
1587
1588 if !strings.HasPrefix(flag, "-") {
1589 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1590 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1591 ctx.PropertyErrorf("kotlincflags",
1592 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1593 } else if inList(flag, config.KotlincIllegalFlags) {
1594 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1595 } else if flag == "-include-runtime" {
1596 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1597 } else {
1598 args := strings.Split(flag, " ")
1599 if args[0] == "-kotlin-home" {
1600 ctx.PropertyErrorf("kotlincflags",
1601 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1602 }
1603 }
1604 }
1605}
1606
Colin Cross8eadbf02017-10-24 17:46:00 -07001607func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf5a66282020-02-21 08:16:41 -08001608 deps deps, flags javaBuilderFlags, jarName string,
1609 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001610
1611 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001612 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001613 // Compile java sources into turbine.jar.
1614 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1615 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1616 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001617 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001618 }
1619 jars = append(jars, turbineJar)
1620 }
1621
Colin Cross55f63ea2018-08-27 12:37:09 -07001622 jars = append(jars, extraJars...)
1623
Nan Zhanged19fc32017-10-19 13:06:22 -07001624 // Combine any static header libraries into classes-header.jar. If there is only
1625 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001626 jars = append(jars, deps.staticHeaderJars...)
1627
Colin Cross5c6ecc12017-10-23 18:12:27 -07001628 // we cannot skip the combine step for now if there is only one jar
1629 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1630 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001631 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001632 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001633 headerJar = combinedJar
Colin Crossf5a66282020-02-21 08:16:41 -08001634 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001635
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001636 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001637 // Transform classes.jar into classes-jarjar.jar
1638 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001639 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf5a66282020-02-21 08:16:41 -08001640 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001641 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001642 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001643 }
1644 }
1645
Colin Crossf5a66282020-02-21 08:16:41 -08001646 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001647}
1648
Colin Crosscb933592017-11-22 13:49:43 -08001649func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001650 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001651
Colin Cross7a3139e2017-12-19 13:57:50 -08001652 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001653
Colin Cross84c38822018-01-03 15:59:46 -08001654 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001655 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1656
1657 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1658
1659 j.jacocoReportClassesFile = jacocoReportClassesFile
1660
1661 return instrumentedJar
1662}
1663
albaltai36ff7dc2018-12-25 14:35:23 +08001664var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001665
Nan Zhanged19fc32017-10-19 13:06:22 -07001666func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001667 if j.headerJarFile == nil {
1668 return nil
1669 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001670 return android.Paths{j.headerJarFile}
1671}
1672
1673func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001674 if j.implementationJarFile == nil {
1675 return nil
1676 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001677 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001678}
1679
Colin Crossf24a22a2019-01-31 14:12:44 -08001680func (j *Module) DexJar() android.Path {
1681 return j.dexJarFile
1682}
1683
Colin Cross331a1212018-08-15 20:40:52 -07001684func (j *Module) ResourceJars() android.Paths {
1685 if j.resourceJar == nil {
1686 return nil
1687 }
1688 return android.Paths{j.resourceJar}
1689}
1690
1691func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001692 if j.implementationAndResourcesJar == nil {
1693 return nil
1694 }
Colin Cross331a1212018-08-15 20:40:52 -07001695 return android.Paths{j.implementationAndResourcesJar}
1696}
1697
Colin Cross46c9b8b2017-06-22 16:51:17 -07001698func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001699 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001700 return j.exportAidlIncludeDirs
1701}
1702
Jiyong Park1be96912018-05-28 18:02:19 +09001703func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001704 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001705 return j.exportedSdkLibs
1706}
1707
Artur Satayev9cf46692019-11-26 18:08:34 +00001708func (j *Module) ExportedPlugins() (android.Paths, []string) {
1709 return j.exportedPluginJars, j.exportedPluginClasses
1710}
1711
Colin Cross0c4ce212019-05-03 15:28:19 -07001712func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1713 return j.srcJarArgs, j.srcJarDeps
1714}
1715
Colin Cross46c9b8b2017-06-22 16:51:17 -07001716var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001717
Colin Cross46c9b8b2017-06-22 16:51:17 -07001718func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001719 return j.logtagsSrcs
1720}
1721
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001722// Collect information for opening IDE project files in java/jdeps.go.
1723func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1724 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1725 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001726 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001727 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001728 if j.expandJarjarRules != nil {
1729 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001730 }
1731}
1732
1733func (j *Module) CompilerDeps() []string {
1734 jdeps := []string{}
1735 jdeps = append(jdeps, j.properties.Libs...)
1736 jdeps = append(jdeps, j.properties.Static_libs...)
1737 return jdeps
1738}
1739
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001740func (j *Module) hasCode(ctx android.ModuleContext) bool {
1741 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1742 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1743}
1744
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001745func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001746 // Dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001747 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
1748 return true
1749 }
Jiyong Park0f80c182020-01-31 02:49:53 +09001750 // Also, a dependency to an sdk member is also considered as such. This is required because
1751 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001752 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
1753 return true
1754 }
1755 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001756}
1757
Jiyong Park0b238752019-10-29 11:23:10 +09001758func (j *Module) Stem() string {
1759 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1760}
1761
Jiyong Park618922e2020-01-08 13:35:43 +09001762func (j *Module) JacocoReportClassesFile() android.Path {
1763 return j.jacocoReportClassesFile
1764}
1765
Martin Stjernholm6d415272020-01-31 17:10:36 +00001766func (j *Module) IsInstallable() bool {
1767 return Bool(j.properties.Installable)
1768}
1769
Colin Cross2fe66872015-03-30 17:20:39 -07001770//
1771// Java libraries (.jar file)
1772//
1773
Colin Crossf506d872017-07-19 15:53:04 -07001774type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001775 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001776
1777 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001778}
1779
Colin Cross42be7612019-02-21 18:12:14 -08001780func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001781 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1782 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1783 return true
1784 }
1785
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001786 // Store uncompressed (and do not strip) dex files from boot class path jars.
1787 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1788 return true
1789 }
1790
1791 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001792 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001793 return true
1794 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001795 if ctx.Config().UncompressPrivAppDex() &&
1796 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1797 return true
1798 }
1799
Colin Cross2fc72f62018-12-21 12:59:54 -08001800 return false
1801}
1802
Colin Crossf506d872017-07-19 15:53:04 -07001803func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001804 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001805 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001806 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001807 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001808 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001809 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001810
Jiyong Park7f7766d2019-07-25 22:02:35 +09001811 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1812 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001813 var extraInstallDeps android.Paths
1814 if j.InstallMixin != nil {
1815 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1816 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001817 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001818 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001819 }
Colin Crossb7a63242015-04-16 14:09:14 -07001820}
1821
Colin Crossf506d872017-07-19 15:53:04 -07001822func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001823 j.deps(ctx)
1824}
1825
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001826const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001827 aidlIncludeDir = "aidl"
1828 javaDir = "java"
1829 jarFileSuffix = ".jar"
1830 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001831)
1832
Paul Duffina0dbf432019-12-05 11:25:53 +00001833// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001834func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1835 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1836}
1837
1838func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1839 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001840}
1841
Paul Duffin13879572019-11-28 14:31:38 +00001842type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001843 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001844}
1845
1846func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1847 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1848}
1849
1850func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1851 _, ok := module.(*Library)
1852 return ok
1853}
1854
Paul Duffina0dbf432019-12-05 11:25:53 +00001855func (mt *librarySdkMemberType) buildSnapshot(
1856 sdkModuleContext android.ModuleContext,
1857 builder android.SnapshotBuilder,
1858 member android.SdkMember,
1859 jarToExportGetter func(j *Library) android.Path) {
1860
Paul Duffin13879572019-11-28 14:31:38 +00001861 variants := member.Variants()
1862 if len(variants) != 1 {
1863 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1864 for _, variant := range variants {
1865 sdkModuleContext.ModuleErrorf(" %q", variant)
1866 }
1867 }
1868 variant := variants[0]
1869 j := variant.(*Library)
1870
Paul Duffina0dbf432019-12-05 11:25:53 +00001871 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001872 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001873 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001874
1875 for _, dir := range j.AidlIncludeDirs() {
1876 // TODO(jiyong): copy parcelable declarations only
1877 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1878 for _, file := range aidlFiles {
1879 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1880 }
1881 }
1882
Paul Duffin9d8d6092019-12-05 18:19:29 +00001883 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001884 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001885}
1886
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001887var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1888 librarySdkMemberType{
1889 android.SdkMemberTypeBase{
1890 PropertyName: "java_header_libs",
1891 SupportsSdk: true,
1892 },
1893 },
1894}
1895
Paul Duffina0dbf432019-12-05 11:25:53 +00001896type headerLibrarySdkMemberType struct {
1897 librarySdkMemberType
1898}
1899
1900func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1901 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1902 headerJars := j.HeaderJars()
1903 if len(headerJars) != 1 {
1904 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1905 }
1906
1907 return headerJars[0]
1908 })
1909}
1910
Paul Duffina0dbf432019-12-05 11:25:53 +00001911type implLibrarySdkMemberType struct {
1912 librarySdkMemberType
1913}
1914
1915func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1916 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1917 implementationJars := j.ImplementationJars()
1918 if len(implementationJars) != 1 {
1919 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1920 }
1921
1922 return implementationJars[0]
1923 })
1924}
1925
Colin Cross1b16b0e2019-02-12 14:41:32 -08001926// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1927//
1928// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1929// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1930// as a `static_libs` dependency of another module.
1931//
1932// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1933// a device.
1934//
1935// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1936// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001937func LibraryFactory() android.Module {
1938 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001939
Colin Cross9ae1b922018-06-26 17:59:05 -07001940 module.AddProperties(
1941 &module.Module.properties,
1942 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001943 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001944 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001945
Jiyong Park7f7766d2019-07-25 22:02:35 +09001946 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001947 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001948 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001949 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001950}
1951
Colin Cross1b16b0e2019-02-12 14:41:32 -08001952// java_library_static is an obsolete alias for java_library.
1953func LibraryStaticFactory() android.Module {
1954 return LibraryFactory()
1955}
1956
1957// java_library_host builds and links sources into a `.jar` file for the host.
1958//
1959// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1960// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001961func LibraryHostFactory() android.Module {
1962 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001963
Colin Cross6af17aa2017-09-20 12:59:05 -07001964 module.AddProperties(
1965 &module.Module.properties,
1966 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001967
Colin Cross9ae1b922018-06-26 17:59:05 -07001968 module.Module.properties.Installable = proptools.BoolPtr(true)
1969
Jiyong Park7f7766d2019-07-25 22:02:35 +09001970 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001971 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001972 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001973}
1974
1975//
Colin Crossb628ea52018-08-14 16:42:33 -07001976// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001977//
1978
1979type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001980 // list of compatibility suites (for example "cts", "vts") that the module should be
1981 // installed into.
1982 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001983
1984 // the name of the test configuration (for example "AndroidTest.xml") that should be
1985 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001986 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001987
Jack He33338892018-09-19 02:21:28 -07001988 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1989 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001990 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001991
Colin Crossd96ca352018-08-10 16:06:24 -07001992 // list of files or filegroup modules that provide data that should be installed alongside
1993 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001994 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001995
1996 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1997 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1998 // explicitly.
1999 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002000}
2001
Paul Duffin42df1442019-03-20 12:45:53 +00002002type testHelperLibraryProperties struct {
2003 // list of compatibility suites (for example "cts", "vts") that the module should be
2004 // installed into.
2005 Test_suites []string `android:"arch_variant"`
2006}
2007
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002008type prebuiltTestProperties struct {
2009 // list of compatibility suites (for example "cts", "vts") that the module should be
2010 // installed into.
2011 Test_suites []string `android:"arch_variant"`
2012
2013 // the name of the test configuration (for example "AndroidTest.xml") that should be
2014 // installed with the module.
2015 Test_config *string `android:"path,arch_variant"`
2016}
2017
Colin Cross05638fc2018-04-09 18:40:24 -07002018type Test struct {
2019 Library
2020
2021 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002022
2023 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002024 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002025}
2026
Paul Duffin42df1442019-03-20 12:45:53 +00002027type TestHelperLibrary struct {
2028 Library
2029
2030 testHelperLibraryProperties testHelperLibraryProperties
2031}
2032
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002033type JavaTestImport struct {
2034 Import
2035
2036 prebuiltTestProperties prebuiltTestProperties
2037
2038 testConfig android.Path
2039}
2040
Colin Cross303e21f2018-08-07 16:49:25 -07002041func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002042 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2043 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002044 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002045
2046 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002047}
2048
Paul Duffin42df1442019-03-20 12:45:53 +00002049func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2050 j.Library.GenerateAndroidBuildActions(ctx)
2051}
2052
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002053func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2054 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2055 j.prebuiltTestProperties.Test_suites, nil)
2056
2057 j.Import.GenerateAndroidBuildActions(ctx)
2058}
2059
2060type testSdkMemberType struct {
2061 android.SdkMemberTypeBase
2062}
2063
2064func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2065 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2066}
2067
2068func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2069 _, ok := module.(*Test)
2070 return ok
2071}
2072
2073func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2074 variants := member.Variants()
2075 if len(variants) != 1 {
2076 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2077 for _, variant := range variants {
2078 sdkModuleContext.ModuleErrorf(" %q", variant)
2079 }
2080 }
2081 variant := variants[0]
2082 j := variant.(*Test)
2083
2084 implementationJars := j.ImplementationJars()
2085 if len(implementationJars) != 1 {
2086 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2087 }
2088
2089 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2090 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2091
2092 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2093 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2094
2095 module := builder.AddPrebuiltModule(member, "java_test_import")
2096 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2097 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2098}
2099
Colin Cross1b16b0e2019-02-12 14:41:32 -08002100// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2101// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2102//
2103// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2104// compiled against the device bootclasspath.
2105//
2106// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2107// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002108func TestFactory() android.Module {
2109 module := &Test{}
2110
2111 module.AddProperties(
2112 &module.Module.properties,
2113 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002114 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002115 &module.Module.protoProperties,
2116 &module.testProperties)
2117
Colin Cross9ae1b922018-06-26 17:59:05 -07002118 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002119 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002120
Colin Cross05638fc2018-04-09 18:40:24 -07002121 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002122 return module
2123}
2124
Paul Duffin42df1442019-03-20 12:45:53 +00002125// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2126func TestHelperLibraryFactory() android.Module {
2127 module := &TestHelperLibrary{}
2128
2129 module.AddProperties(
2130 &module.Module.properties,
2131 &module.Module.deviceProperties,
2132 &module.Module.dexpreoptProperties,
2133 &module.Module.protoProperties,
2134 &module.testHelperLibraryProperties)
2135
Colin Cross9a4abed2019-04-24 13:19:28 -07002136 module.Module.properties.Installable = proptools.BoolPtr(true)
2137 module.Module.dexpreopter.isTest = true
2138
Paul Duffin42df1442019-03-20 12:45:53 +00002139 InitJavaModule(module, android.HostAndDeviceSupported)
2140 return module
2141}
2142
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002143// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2144// and makes sure that it is added to the appropriate test suite.
2145//
2146// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2147// compiled against an Android classpath.
2148//
2149// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2150// for host modules.
2151func JavaTestImportFactory() android.Module {
2152 module := &JavaTestImport{}
2153
2154 module.AddProperties(
2155 &module.Import.properties,
2156 &module.prebuiltTestProperties)
2157
2158 module.Import.properties.Installable = proptools.BoolPtr(true)
2159
2160 android.InitPrebuiltModule(module, &module.properties.Jars)
2161 android.InitApexModule(module)
2162 android.InitSdkAwareModule(module)
2163 InitJavaModule(module, android.HostAndDeviceSupported)
2164 return module
2165}
2166
Colin Cross1b16b0e2019-02-12 14:41:32 -08002167// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2168// allow running the test with `atest` or a `TEST_MAPPING` file.
2169//
2170// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2171// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002172func TestHostFactory() android.Module {
2173 module := &Test{}
2174
2175 module.AddProperties(
2176 &module.Module.properties,
2177 &module.Module.protoProperties,
2178 &module.testProperties)
2179
Colin Cross9ae1b922018-06-26 17:59:05 -07002180 module.Module.properties.Installable = proptools.BoolPtr(true)
2181
Colin Cross05638fc2018-04-09 18:40:24 -07002182 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002183 return module
2184}
2185
2186//
Colin Cross2fe66872015-03-30 17:20:39 -07002187// Java Binaries (.jar file plus wrapper script)
2188//
2189
Colin Crossf506d872017-07-19 15:53:04 -07002190type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002191 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002192 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002193
2194 // Name of the class containing main to be inserted into the manifest as Main-Class.
2195 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002196}
2197
Colin Crossf506d872017-07-19 15:53:04 -07002198type Binary struct {
2199 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002200
Colin Crossf506d872017-07-19 15:53:04 -07002201 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002202
Colin Cross6b4a32d2017-12-05 13:42:45 -08002203 isWrapperVariant bool
2204
Colin Crossc3315992017-12-08 19:12:36 -08002205 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002206 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002207}
2208
Alex Light24237172017-10-26 09:46:21 -07002209func (j *Binary) HostToolPath() android.OptionalPath {
2210 return android.OptionalPathForPath(j.binaryFile)
2211}
2212
Colin Crossf506d872017-07-19 15:53:04 -07002213func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002214 if ctx.Arch().ArchType == android.Common {
2215 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002216 if j.binaryProperties.Main_class != nil {
2217 if j.properties.Manifest != nil {
2218 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2219 }
2220 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2221 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2222 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2223 }
2224
Colin Cross6b4a32d2017-12-05 13:42:45 -08002225 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002226 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002227 // Handle the binary wrapper
2228 j.isWrapperVariant = true
2229
Colin Cross366938f2017-12-11 16:29:02 -08002230 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002231 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002232 } else {
2233 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2234 }
2235
2236 // Depend on the installed jar so that the wrapper doesn't get executed by
2237 // another build rule before the jar has been installed.
2238 jarFile := ctx.PrimaryModule().(*Binary).installFile
2239
2240 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2241 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002242 }
Colin Cross2fe66872015-03-30 17:20:39 -07002243}
2244
Colin Crossf506d872017-07-19 15:53:04 -07002245func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002246 if ctx.Arch().ArchType == android.Common {
2247 j.deps(ctx)
2248 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002249}
2250
Colin Cross1b16b0e2019-02-12 14:41:32 -08002251// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2252// as well.
2253//
2254// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2255// compiled against the device bootclasspath.
2256//
2257// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2258// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002259func BinaryFactory() android.Module {
2260 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002261
Colin Cross36242852017-06-23 15:06:31 -07002262 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002263 &module.Module.properties,
2264 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002265 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002266 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002267 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002268
Colin Cross9ae1b922018-06-26 17:59:05 -07002269 module.Module.properties.Installable = proptools.BoolPtr(true)
2270
Colin Cross6b4a32d2017-12-05 13:42:45 -08002271 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2272 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002273 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002274}
2275
Colin Cross1b16b0e2019-02-12 14:41:32 -08002276// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2277//
2278// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2279// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002280func BinaryHostFactory() android.Module {
2281 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002282
Colin Cross36242852017-06-23 15:06:31 -07002283 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002284 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002285 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002286 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002287
Colin Cross9ae1b922018-06-26 17:59:05 -07002288 module.Module.properties.Installable = proptools.BoolPtr(true)
2289
Colin Cross6b4a32d2017-12-05 13:42:45 -08002290 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2291 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002292 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002293}
2294
2295//
2296// Java prebuilts
2297//
2298
Colin Cross74d73e22017-08-02 11:05:49 -07002299type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002300 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002301
Nan Zhangea568a42017-11-08 21:20:04 -08002302 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002303
2304 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002305
2306 // List of shared java libs that this module has dependencies to
2307 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002308
2309 // List of files to remove from the jar file(s)
2310 Exclude_files []string
2311
2312 // List of directories to remove from the jar file(s)
2313 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002314
2315 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002316 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002317
2318 // set the name of the output
2319 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002320}
2321
2322type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002323 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002324 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002325 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002326 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002327 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002328
Colin Cross74d73e22017-08-02 11:05:49 -07002329 properties ImportProperties
2330
Colin Cross0a6e0072017-08-30 14:24:55 -07002331 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002332 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002333}
2334
Jiyong Park6a927c42020-01-21 02:03:43 +09002335func (j *Import) sdkVersion() sdkSpec {
2336 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002337}
2338
Jiyong Park6a927c42020-01-21 02:03:43 +09002339func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002340 return j.sdkVersion()
2341}
2342
Colin Cross74d73e22017-08-02 11:05:49 -07002343func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002344 return &j.prebuilt
2345}
2346
Colin Cross74d73e22017-08-02 11:05:49 -07002347func (j *Import) PrebuiltSrcs() []string {
2348 return j.properties.Jars
2349}
2350
2351func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002352 return j.prebuilt.Name(j.ModuleBase.Name())
2353}
2354
Jiyong Park0b238752019-10-29 11:23:10 +09002355func (j *Import) Stem() string {
2356 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2357}
2358
Jiyong Park618922e2020-01-08 13:35:43 +09002359func (a *Import) JacocoReportClassesFile() android.Path {
2360 return nil
2361}
2362
Colin Cross74d73e22017-08-02 11:05:49 -07002363func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002364 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002365}
2366
Colin Cross74d73e22017-08-02 11:05:49 -07002367func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002368 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002369
Jiyong Park0b238752019-10-29 11:23:10 +09002370 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002371 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002372 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2373 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002374 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002375 inputFile := outputFile
2376 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2377 TransformJetifier(ctx, outputFile, inputFile)
2378 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002379 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002380
2381 ctx.VisitDirectDeps(func(module android.Module) {
2382 otherName := ctx.OtherModuleName(module)
2383 tag := ctx.OtherModuleDependencyTag(module)
2384
2385 switch dep := module.(type) {
2386 case Dependency:
2387 switch tag {
2388 case libTag, staticLibTag:
2389 // sdk lib names from dependencies are re-exported
2390 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2391 }
2392 case SdkLibraryDependency:
2393 switch tag {
2394 case libTag:
2395 // names of sdk libs that are directly depended are exported
2396 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2397 }
2398 }
2399 })
2400
2401 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002402 if Bool(j.properties.Installable) {
2403 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002404 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002405 }
Colin Cross2fe66872015-03-30 17:20:39 -07002406}
2407
Colin Cross74d73e22017-08-02 11:05:49 -07002408var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002409
Nan Zhanged19fc32017-10-19 13:06:22 -07002410func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002411 if j.combinedClasspathFile == nil {
2412 return nil
2413 }
Colin Cross37f6d792018-07-12 12:28:41 -07002414 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002415}
2416
2417func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002418 if j.combinedClasspathFile == nil {
2419 return nil
2420 }
Colin Cross37f6d792018-07-12 12:28:41 -07002421 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002422}
2423
Colin Cross331a1212018-08-15 20:40:52 -07002424func (j *Import) ResourceJars() android.Paths {
2425 return nil
2426}
2427
2428func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002429 if j.combinedClasspathFile == nil {
2430 return nil
2431 }
Colin Cross331a1212018-08-15 20:40:52 -07002432 return android.Paths{j.combinedClasspathFile}
2433}
2434
Colin Crossf24a22a2019-01-31 14:12:44 -08002435func (j *Import) DexJar() android.Path {
2436 return nil
2437}
2438
Colin Cross74d73e22017-08-02 11:05:49 -07002439func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002440 return nil
2441}
2442
Jiyong Park1be96912018-05-28 18:02:19 +09002443func (j *Import) ExportedSdkLibs() []string {
2444 return j.exportedSdkLibs
2445}
2446
Artur Satayev9cf46692019-11-26 18:08:34 +00002447func (j *Import) ExportedPlugins() (android.Paths, []string) {
2448 return nil, nil
2449}
2450
Colin Cross0c4ce212019-05-03 15:28:19 -07002451func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2452 return nil, nil
2453}
2454
Jiyong Park0f80c182020-01-31 02:49:53 +09002455func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09002456 // dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002457 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
2458 return true
2459 }
Jiyong Park0f80c182020-01-31 02:49:53 +09002460 // Also, a dependency to an sdk member is also considered as such. This is required because
2461 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002462 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
2463 return true
2464 }
2465 return false
Jiyong Park0f80c182020-01-31 02:49:53 +09002466}
2467
albaltai36ff7dc2018-12-25 14:35:23 +08002468// Add compile time check for interface implementation
2469var _ android.IDEInfo = (*Import)(nil)
2470var _ android.IDECustomizedModuleName = (*Import)(nil)
2471
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002472// Collect information for opening IDE project files in java/jdeps.go.
2473const (
2474 removedPrefix = "prebuilt_"
2475)
2476
2477func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2478 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2479}
2480
2481func (j *Import) IDECustomizedModuleName() string {
2482 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2483 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2484 // solution to get the Import name.
2485 name := j.Name()
2486 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002487 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002488 }
2489 return name
2490}
2491
Colin Cross74d73e22017-08-02 11:05:49 -07002492var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002493
Colin Cross1b16b0e2019-02-12 14:41:32 -08002494// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2495//
2496// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2497// compiled against an Android classpath.
2498//
2499// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2500// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002501func ImportFactory() android.Module {
2502 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002503
Colin Cross74d73e22017-08-02 11:05:49 -07002504 module.AddProperties(&module.properties)
2505
2506 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002507 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002508 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002509 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002510 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002511}
2512
Colin Cross1b16b0e2019-02-12 14:41:32 -08002513// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2514// module.
2515//
2516// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2517// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002518func ImportFactoryHost() android.Module {
2519 module := &Import{}
2520
2521 module.AddProperties(&module.properties)
2522
2523 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002524 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002525 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002526 return module
2527}
2528
Colin Cross42be7612019-02-21 18:12:14 -08002529// dex_import module
2530
2531type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002532 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002533
2534 // set the name of the output
2535 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002536}
2537
2538type DexImport struct {
2539 android.ModuleBase
2540 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002541 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002542 prebuilt android.Prebuilt
2543
2544 properties DexImportProperties
2545
2546 dexJarFile android.Path
2547 maybeStrippedDexJarFile android.Path
2548
2549 dexpreopter
2550}
2551
2552func (j *DexImport) Prebuilt() *android.Prebuilt {
2553 return &j.prebuilt
2554}
2555
2556func (j *DexImport) PrebuiltSrcs() []string {
2557 return j.properties.Jars
2558}
2559
2560func (j *DexImport) Name() string {
2561 return j.prebuilt.Name(j.ModuleBase.Name())
2562}
2563
Jiyong Park0b238752019-10-29 11:23:10 +09002564func (j *DexImport) Stem() string {
2565 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2566}
2567
Martin Stjernholm6d415272020-01-31 17:10:36 +00002568func (j *DexImport) IsInstallable() bool {
2569 return true
2570}
2571
Colin Cross42be7612019-02-21 18:12:14 -08002572func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2573 if len(j.properties.Jars) != 1 {
2574 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2575 }
2576
Jiyong Park0b238752019-10-29 11:23:10 +09002577 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002578 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2579
2580 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2581 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2582
2583 if j.dexpreopter.uncompressedDex {
2584 rule := android.NewRuleBuilder()
2585
2586 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2587 rule.Temporary(temporary)
2588
2589 // use zip2zip to uncompress classes*.dex files
2590 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002591 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002592 FlagWithInput("-i ", inputJar).
2593 FlagWithOutput("-o ", temporary).
2594 FlagWithArg("-0 ", "'classes*.dex'")
2595
2596 // use zipalign to align uncompressed classes*.dex files
2597 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002598 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002599 Flag("-f").
2600 Text("4").
2601 Input(temporary).
2602 Output(dexOutputFile)
2603
2604 rule.DeleteTemporaryFiles()
2605
2606 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2607 } else {
2608 ctx.Build(pctx, android.BuildParams{
2609 Rule: android.Cp,
2610 Input: inputJar,
2611 Output: dexOutputFile,
2612 })
2613 }
2614
2615 j.dexJarFile = dexOutputFile
2616
2617 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2618
2619 j.maybeStrippedDexJarFile = dexOutputFile
2620
2621 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2622 ctx.ModuleName()+".jar", dexOutputFile)
2623}
2624
2625func (j *DexImport) DexJar() android.Path {
2626 return j.dexJarFile
2627}
2628
2629// dex_import imports a `.jar` file containing classes.dex files.
2630//
2631// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2632// to the device.
2633func DexImportFactory() android.Module {
2634 module := &DexImport{}
2635
2636 module.AddProperties(&module.properties)
2637
2638 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002639 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002640 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002641 return module
2642}
2643
Colin Cross89536d42017-07-07 14:35:50 -07002644//
2645// Defaults
2646//
2647type Defaults struct {
2648 android.ModuleBase
2649 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002650 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002651}
2652
Colin Cross1b16b0e2019-02-12 14:41:32 -08002653// java_defaults provides a set of properties that can be inherited by other java or android modules.
2654//
2655// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2656// property in the defaults module that exists in the depending module will be prepended to the depending module's
2657// value for that property.
2658//
2659// Example:
2660//
2661// java_defaults {
2662// name: "example_defaults",
2663// srcs: ["common/**/*.java"],
2664// javacflags: ["-Xlint:all"],
2665// aaptflags: ["--auto-add-overlay"],
2666// }
2667//
2668// java_library {
2669// name: "example",
2670// defaults: ["example_defaults"],
2671// srcs: ["example/**/*.java"],
2672// }
2673//
2674// is functionally identical to:
2675//
2676// java_library {
2677// name: "example",
2678// srcs: [
2679// "common/**/*.java",
2680// "example/**/*.java",
2681// ],
2682// javacflags: ["-Xlint:all"],
2683// }
Colin Cross89536d42017-07-07 14:35:50 -07002684func defaultsFactory() android.Module {
2685 return DefaultsFactory()
2686}
2687
Paul Duffin47357662019-12-05 14:07:14 +00002688func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002689 module := &Defaults{}
2690
Colin Cross89536d42017-07-07 14:35:50 -07002691 module.AddProperties(
2692 &CompilerProperties{},
2693 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002694 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002695 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002696 &aaptProperties{},
2697 &androidLibraryProperties{},
2698 &appProperties{},
2699 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002700 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002701 &ImportProperties{},
2702 &AARImportProperties{},
2703 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002704 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002705 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002706 )
2707
2708 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002709 return module
2710}
Nan Zhangea568a42017-11-08 21:20:04 -08002711
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002712func kytheExtractJavaFactory() android.Singleton {
2713 return &kytheExtractJavaSingleton{}
2714}
2715
2716type kytheExtractJavaSingleton struct {
2717}
2718
2719func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2720 var xrefTargets android.Paths
2721 ctx.VisitAllModules(func(module android.Module) {
2722 if javaModule, ok := module.(xref); ok {
2723 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2724 }
2725 })
2726 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2727 if len(xrefTargets) > 0 {
2728 ctx.Build(pctx, android.BuildParams{
2729 Rule: blueprint.Phony,
2730 Output: android.PathForPhony(ctx, "xref_java"),
2731 Inputs: xrefTargets,
2732 })
2733 }
2734}
2735
Nan Zhangea568a42017-11-08 21:20:04 -08002736var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002737var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002738var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002739var inList = android.InList