blob: ed3dca9e0f33d6ba42716ff51ecbf8e341840426 [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.
40 android.RegisterSdkMemberType(&headerLibrarySdkMemberType{
41 librarySdkMemberType{
42 android.SdkMemberTypeBase{
43 PropertyName: "java_header_libs",
Paul Duffine6029182019-12-16 17:43:48 +000044 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000045 },
46 },
47 })
48
49 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
50 librarySdkMemberType{
51 android.SdkMemberTypeBase{
52 PropertyName: "java_libs",
53 },
54 },
55 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000056
57 android.RegisterSdkMemberType(&testSdkMemberType{
58 SdkMemberTypeBase: android.SdkMemberTypeBase{
59 PropertyName: "java_tests",
60 },
61 })
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Paul Duffinf9b1da02019-12-18 19:51:55 +000064func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
65 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
66
67 ctx.RegisterModuleType("java_library", LibraryFactory)
68 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
69 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
70 ctx.RegisterModuleType("java_binary", BinaryFactory)
71 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
72 ctx.RegisterModuleType("java_test", TestFactory)
73 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
74 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000075 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 ctx.RegisterModuleType("java_import", ImportFactory)
77 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
78 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
79 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
80 ctx.RegisterModuleType("dex_import", DexImportFactory)
81
82 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
83 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
84}
85
Jeongik Cha2cc570d2019-10-29 15:44:45 +090086func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
87 if j.SocSpecific() || j.DeviceSpecific() ||
88 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
89 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +090090 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090091 ctx.PropertyErrorf("sdk_version",
92 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
93 }
94 }
95 }
96}
97
Jeongik Cha538c0d02019-07-11 15:54:27 +090098func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
99 if sc, ok := ctx.Module().(sdkContext); ok {
100 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +0900101 sdkVersionSpecified := sc.sdkVersion().specified()
102 if usePlatformAPI && sdkVersionSpecified {
103 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
104 } else if !usePlatformAPI && !sdkVersionSpecified {
105 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900106 }
107
108 }
109}
110
Colin Cross2fe66872015-03-30 17:20:39 -0700111// TODO:
112// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700113// Renderscript
114// Post-jar passes:
115// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700116// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700117// DroidDoc
118// Findbugs
119
Colin Cross89536d42017-07-07 14:35:50 -0700120type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700121 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
122 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800123 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700124
125 // list of source files that should not be used to build the Java module.
126 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800127 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700128
129 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700130 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700131
Colin Cross86a63ff2017-09-27 17:33:10 -0700132 // list of directories that should be excluded from java_resource_dirs
133 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700134
Colin Cross0f37af02017-09-27 17:42:05 -0700135 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800136 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700137
Colin Crosscedd4762018-09-13 11:26:19 -0700138 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800139 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700140
Colin Cross7d5136f2015-05-11 13:39:40 -0700141 // list of module-specific flags that will be used for javac compiles
142 Javacflags []string `android:"arch_variant"`
143
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200144 // list of module-specific flags that will be used for kotlinc compiles
145 Kotlincflags []string `android:"arch_variant"`
146
Colin Cross7d5136f2015-05-11 13:39:40 -0700147 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700148 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700149
150 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700151 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700152
153 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800154 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700155
Colin Cross540eff82017-06-22 17:01:52 -0700156 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800157 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700158
159 // If not blank, set the java version passed to javac as -source and -target
160 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700161
Colin Cross9ae1b922018-06-26 17:59:05 -0700162 // If set to true, allow this module to be dexed and installed on devices. Has no
163 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700164 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700165
Colin Cross0f37af02017-09-27 17:42:05 -0700166 // If set to true, include sources used to compile the module in to the final jar
167 Include_srcs *bool
168
Vladimir Marko0975ee02019-04-02 10:29:55 +0100169 // If not empty, classes are restricted to the specified packages and their sub-packages.
170 // This restriction is checked after applying jarjar rules and including static libs.
171 Permitted_packages []string
172
Colin Crossbe9cdb82019-01-21 21:37:16 -0800173 // List of modules to use as annotation processors
174 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700175
Artur Satayev9cf46692019-11-26 18:08:34 +0000176 // List of modules to export to libraries that directly depend on this library as annotation processors
177 Exported_plugins []string
178
Nan Zhang61eaedb2017-11-02 13:28:15 -0700179 // The number of Java source entries each Javac instance can process
180 Javac_shard_size *int64
181
Nan Zhang5f8cb422018-02-06 10:34:32 -0800182 // Add host jdk tools.jar to bootclasspath
183 Use_tools_jar *bool
184
Colin Cross1369cdb2017-09-29 17:58:17 -0700185 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700186 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800187 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700188
Colin Cross6cef4812019-10-17 14:23:50 -0700189 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700190 Javacflags []string
191 }
Colin Crosscb933592017-11-22 13:49:43 -0800192
Colin Cross81440082018-08-15 20:21:55 -0700193 // When compiling language level 9+ .java code in packages that are part of
194 // a system module, patch_module names the module that your sources and
195 // dependencies should be patched into. The Android runtime currently
196 // doesn't implement the JEP 261 module system so this option is only
197 // supported at compile time. It should only be needed to compile tests in
198 // packages that exist in libcore and which are inconvenient to move
199 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100200 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700201
Colin Crosscb933592017-11-22 13:49:43 -0800202 Jacoco struct {
203 // List of classes to include for instrumentation with jacoco to collect coverage
204 // information at runtime when building with coverage enabled. If unset defaults to all
205 // classes.
206 // Supports '*' as the last character of an entry in the list as a wildcard match.
207 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
208 // it matches classes in the package that have the class name as a prefix.
209 Include_filter []string
210
211 // List of classes to exclude from instrumentation with jacoco to collect coverage
212 // information at runtime when building with coverage enabled. Overrides classes selected
213 // by the include_filter property.
214 // Supports '*' as the last character of an entry in the list as a wildcard match.
215 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
216 // it matches classes in the package that have the class name as a prefix.
217 Exclude_filter []string
218 }
219
Andreas Gampef3e5b552018-01-22 21:27:21 -0800220 Errorprone struct {
221 // List of javac flags that should only be used when running errorprone.
222 Javacflags []string
223 }
224
Colin Cross0f2ee152017-12-14 15:22:43 -0800225 Proto struct {
226 // List of extra options that will be passed to the proto generator.
227 Output_params []string
228 }
229
Colin Crosscb933592017-11-22 13:49:43 -0800230 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800231
232 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800233 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700234}
235
Colin Cross89536d42017-07-07 14:35:50 -0700236type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700237 // list of module-specific flags that will be used for dex compiles
238 Dxflags []string `android:"arch_variant"`
239
Jeongik Cha538c0d02019-07-11 15:54:27 +0900240 // if not blank, set to the version of the sdk to compile against.
241 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800242 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700243
Colin Cross83bb3162018-06-25 15:48:06 -0700244 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
245 // Defaults to sdk_version if not set.
246 Min_sdk_version *string
247
Dan Willemsen419290a2018-10-31 15:28:47 -0700248 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
249 // Defaults to sdk_version if not set.
250 Target_sdk_version *string
251
Jeongik Cha356dac42019-08-19 14:09:52 +0900252 // Whether to compile against the platform APIs instead of an SDK.
253 // If true, then sdk_version must be empty. The value of this field
254 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700255 Platform_apis *bool
256
Colin Crossebe1a512017-11-14 13:12:14 -0800257 Aidl struct {
258 // Top level directories to pass to aidl tool
259 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700260
Colin Crossebe1a512017-11-14 13:12:14 -0800261 // Directories rooted at the Android.bp file to pass to aidl tool
262 Local_include_dirs []string
263
264 // directories that should be added as include directories for any aidl sources of modules
265 // that depend on this module, as well as to aidl for this module.
266 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100267
268 // whether to generate traces (for systrace) for this interface
269 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100270
271 // whether to generate Binder#GetTransaction name method.
272 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800273 }
Colin Cross92430102017-10-09 14:59:32 -0700274
275 // If true, export a copy of the module as a -hostdex module for host testing.
276 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700277
Colin Cross7f87f4f2019-04-24 13:41:45 -0700278 Target struct {
279 Hostdex struct {
280 // Additional required dependencies to add to -hostdex modules.
281 Required []string
282 }
283 }
284
David Brazdil17ef5632018-06-27 10:27:45 +0100285 // If set to true, compile dex regardless of installable. Defaults to false.
286 Compile_dex *bool
287
Colin Cross66dbc0b2017-12-28 12:23:20 -0800288 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700289 // If false, disable all optimization. Defaults to true for android_app and android_test
290 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800291 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700292 // True if the module containing this has it set by default.
293 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800294
295 // If true, optimize for size by removing unused code. Defaults to true for apps,
296 // false for libraries and tests.
297 Shrink *bool
298
299 // If true, optimize bytecode. Defaults to false.
300 Optimize *bool
301
302 // If true, obfuscate bytecode. Defaults to false.
303 Obfuscate *bool
304
305 // If true, do not use the flag files generated by aapt that automatically keep
306 // classes referenced by the app manifest. Defaults to false.
307 No_aapt_flags *bool
308
309 // Flags to pass to proguard.
310 Proguard_flags []string
311
312 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800313 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800314 }
315
Paul Duffine25c6442019-10-11 13:50:28 +0100316 // When targeting 1.9 and above, override the modules to use with --system,
317 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700318 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700319
Jiyong Park4c4c0242019-10-21 14:53:15 +0900320 // set the name of the output
321 Stem *string
322
Colin Cross5a0dcd52018-10-05 14:20:06 -0700323 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800324 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700325}
326
Sasha Smundak2057f822019-04-16 17:16:58 -0700327func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
328 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
329}
330
Colin Cross46c9b8b2017-06-22 16:51:17 -0700331// Module contains the properties and members used by all java module types
332type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700333 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700334 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900335 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900336 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700337
Colin Cross89536d42017-07-07 14:35:50 -0700338 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700339 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700340 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700341
Colin Cross331a1212018-08-15 20:40:52 -0700342 // jar file containing header classes including static library dependencies, suitable for
343 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700344 headerJarFile android.Path
345
Colin Cross331a1212018-08-15 20:40:52 -0700346 // jar file containing implementation classes including static library dependencies but no
347 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700348 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700349
Colin Cross331a1212018-08-15 20:40:52 -0700350 // jar file containing only resources including from static library dependencies
351 resourceJar android.Path
352
Colin Cross0c4ce212019-05-03 15:28:19 -0700353 // args and dependencies to package source files into a srcjar
354 srcJarArgs []string
355 srcJarDeps android.Paths
356
Colin Cross331a1212018-08-15 20:40:52 -0700357 // jar file containing implementation classes and resources including static library
358 // dependencies
359 implementationAndResourcesJar android.Path
360
361 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700362 dexJarFile android.Path
363
Colin Cross43f08db2018-11-12 10:13:39 -0800364 // output file that contains classes.dex if it should be in the output file
365 maybeStrippedDexJarFile android.Path
366
Colin Crosscb933592017-11-22 13:49:43 -0800367 // output file containing uninstrumented classes that will be instrumented by jacoco
368 jacocoReportClassesFile android.Path
369
Colin Cross66dbc0b2017-12-28 12:23:20 -0800370 // output file containing mapping of obfuscated names
371 proguardDictionary android.Path
372
Colin Cross331a1212018-08-15 20:40:52 -0700373 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700374 outputFile android.Path
375 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700376
Colin Cross635c3b02016-05-18 15:37:25 -0700377 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700378
Colin Cross635c3b02016-05-18 15:37:25 -0700379 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700380
Colin Cross2fe66872015-03-30 17:20:39 -0700381 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700382 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800383
384 // list of .java files and srcjars that was passed to javac
385 compiledJavaSrcs android.Paths
386 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800387
388 // list of extra progurad flag files
389 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900390
Colin Cross094054a2018-10-17 15:10:48 -0700391 // manifest file to use instead of properties.Manifest
392 overrideManifest android.OptionalPath
393
Artur Satayev9cf46692019-11-26 18:08:34 +0000394 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900395 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700396
Artur Satayev9cf46692019-11-26 18:08:34 +0000397 // list of plugins that this java module is exporting
398 exportedPluginJars android.Paths
399
400 // list of plugins that this java module is exporting
401 exportedPluginClasses []string
402
403 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800404 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700405 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800406
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800407 // expanded Jarjar_rules
408 expandJarjarRules android.Path
409
Vladimir Marko0975ee02019-04-02 10:29:55 +0100410 // list of additional targets for checkbuild
411 additionalCheckedModules android.Paths
412
Colin Cross988708c2019-05-06 14:04:11 -0700413 // Extra files generated by the module type to be added as java resources.
414 extraResources android.Paths
415
Colin Crossf24a22a2019-01-31 14:12:44 -0800416 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800417 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800418
419 // list of the xref extraction files
420 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700421}
422
Colin Cross41955e82019-05-29 14:40:35 -0700423func (j *Module) OutputFiles(tag string) (android.Paths, error) {
424 switch tag {
425 case "":
426 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700427 case ".jar":
428 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700429 case ".proguard_map":
430 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700431 default:
432 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
433 }
Colin Cross54250902017-12-05 09:28:08 -0800434}
435
Colin Cross41955e82019-05-29 14:40:35 -0700436var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800437
Colin Crossf506d872017-07-19 15:53:04 -0700438type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700439 HeaderJars() android.Paths
440 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700441 ResourceJars() android.Paths
442 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800443 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700444 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900445 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000446 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700447 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700448 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900449 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700450}
451
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900453 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
454 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455}
456
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800457type xref interface {
458 XrefJavaFiles() android.Paths
459}
460
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800461func (j *Module) XrefJavaFiles() android.Paths {
462 return j.kytheFiles
463}
464
Colin Cross89536d42017-07-07 14:35:50 -0700465func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
466 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
467 android.InitDefaultableModule(module)
468}
469
Colin Crossbe1da472017-07-07 15:59:46 -0700470type dependencyTag struct {
471 blueprint.BaseDependencyTag
472 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700473}
474
Colin Crossa4f08812018-10-02 22:03:40 -0700475type jniDependencyTag struct {
476 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700477}
478
Jiyong Park8be103b2019-11-08 15:53:48 +0900479func IsJniDepTag(depTag blueprint.DependencyTag) bool {
480 _, ok := depTag.(*jniDependencyTag)
481 return ok
482}
483
Colin Crossbe1da472017-07-07 15:59:46 -0700484var (
Colin Cross4b964c02018-10-15 16:18:06 -0700485 staticLibTag = dependencyTag{name: "staticlib"}
486 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700487 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800488 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000489 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700490 bootClasspathTag = dependencyTag{name: "bootclasspath"}
491 systemModulesTag = dependencyTag{name: "system modules"}
492 frameworkResTag = dependencyTag{name: "framework-res"}
493 frameworkApkTag = dependencyTag{name: "framework-apk"}
494 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800495 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700496 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
497 certificateTag = dependencyTag{name: "certificate"}
498 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700499 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700500)
Colin Cross2fe66872015-03-30 17:20:39 -0700501
Jiyong Park83dc74b2020-01-14 18:38:44 +0900502func IsLibDepTag(depTag blueprint.DependencyTag) bool {
503 return depTag == libTag
504}
505
506func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
507 return depTag == staticLibTag
508}
509
Colin Crossfc3674a2017-09-18 17:41:52 -0700510type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700511 useModule, useFiles, useDefaultLibs, invalidVersion bool
512
Colin Cross6cef4812019-10-17 14:23:50 -0700513 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
514 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100515
516 // The default system modules to use. Will be an empty string if no system
517 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700518 systemModules string
519
Colin Cross6cef4812019-10-17 14:23:50 -0700520 // The modules that will be added ot the classpath when targeting 1.9 or higher
521 java9Classpath []string
522
Colin Crossa97c5d32018-03-28 14:58:31 -0700523 frameworkResModule string
524
Colin Cross86a60ae2018-05-29 14:44:55 -0700525 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700526 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100527
528 noStandardLibs, noFrameworksLibs bool
529}
530
531func (s sdkDep) hasStandardLibs() bool {
532 return !s.noStandardLibs
533}
534
535func (s sdkDep) hasFrameworkLibs() bool {
536 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700537}
538
Colin Crossa4f08812018-10-02 22:03:40 -0700539type jniLib struct {
540 name string
541 path android.Path
542 target android.Target
543}
544
Colin Cross0ea8ba82019-06-06 14:33:29 -0700545func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800546 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
547}
548
Colin Cross0ea8ba82019-06-06 14:33:29 -0700549func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800550 return j.shouldInstrument(ctx) &&
551 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
552 ctx.Config().UnbundledBuild())
553}
554
Jiyong Park6a927c42020-01-21 02:03:43 +0900555func (j *Module) sdkVersion() sdkSpec {
556 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700557}
558
Paul Duffine25c6442019-10-11 13:50:28 +0100559func (j *Module) systemModules() string {
560 return proptools.String(j.deviceProperties.System_modules)
561}
562
Jiyong Park6a927c42020-01-21 02:03:43 +0900563func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700564 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900565 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700566 }
567 return j.sdkVersion()
568}
569
Jiyong Park6a927c42020-01-21 02:03:43 +0900570func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700571 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900572 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700573 }
574 return j.sdkVersion()
575}
576
Jiyong Parkb02bb402019-12-03 00:43:57 +0900577func (j *Module) AvailableFor(what string) bool {
578 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
579 // Exception: for hostdex: true libraries, the platform variant is created
580 // even if it's not marked as available to platform. In that case, the platform
581 // variant is used only for the hostdex and not installed to the device.
582 return true
583 }
584 return j.ApexModuleBase.AvailableFor(what)
585}
586
Colin Crossbe1da472017-07-07 15:59:46 -0700587func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700588 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100589 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700590 if sdkDep.useDefaultLibs {
591 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
592 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
593 if sdkDep.hasFrameworkLibs() {
594 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700595 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700596 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700597 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100598 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700599 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700600 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
601 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
602 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
603 }
Colin Cross2fe66872015-03-30 17:20:39 -0700604 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700605
Nan Zhangb2b33de2018-02-23 11:18:47 -0800606 if ctx.ModuleName() == "android_stubs_current" ||
607 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700608 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700609 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800610 }
Colin Cross2fe66872015-03-30 17:20:39 -0700611 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700612
Inseob Kimac1e9862019-12-09 18:15:47 +0900613 syspropPublicStubs := syspropPublicStubs(ctx.Config())
614
615 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
616 // and redirects dependency to public stub depending on the link type.
617 rewriteSyspropLibs := func(libs []string, prop string) []string {
618 // make a copy
619 ret := android.CopyOf(libs)
620
621 for idx, lib := range libs {
622 stub, ok := syspropPublicStubs[lib]
623
624 if !ok {
625 continue
626 }
627
628 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900629 // only platform modules can use internal props
630 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900631 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900632 }
633 }
634
635 return ret
636 }
637
638 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
639 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700640
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700641 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000642 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800643
Colin Crossfe17f6f2019-03-28 19:30:56 -0700644 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700645 if j.hasSrcExt(".proto") {
646 protoDeps(ctx, &j.protoProperties)
647 }
Colin Cross93e85952017-08-15 13:34:18 -0700648
649 if j.hasSrcExt(".kt") {
650 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
651 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700652 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
653 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800654 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800655 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
656 }
Colin Cross93e85952017-08-15 13:34:18 -0700657 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800658
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000659 // Framework libraries need special handling in static coverage builds: they should not have
660 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
661 // the same jacoco classes coming from different bootclasspath jars.
662 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
663 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
664 j.properties.Instrument = true
665 }
666 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700667 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800668 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700669}
670
671func hasSrcExt(srcs []string, ext string) bool {
672 for _, src := range srcs {
673 if filepath.Ext(src) == ext {
674 return true
675 }
676 }
677
678 return false
679}
680
681func (j *Module) hasSrcExt(ext string) bool {
682 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700683}
684
Colin Cross46c9b8b2017-06-22 16:51:17 -0700685func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700686 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700687
Colin Crossebe1a512017-11-14 13:12:14 -0800688 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
689 aidlIncludes = append(aidlIncludes,
690 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
691 aidlIncludes = append(aidlIncludes,
692 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700693
Colin Cross3047fa22019-04-18 10:56:44 -0700694 var flags []string
695 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700696
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697 if aidlPreprocess.Valid() {
698 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700699 deps = append(deps, aidlPreprocess.Path())
700 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700701 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700702 }
703
Colin Cross3047fa22019-04-18 10:56:44 -0700704 if len(j.exportAidlIncludeDirs) > 0 {
705 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
706 }
707
708 if len(aidlIncludes) > 0 {
709 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
710 }
711
Colin Cross635c3b02016-05-18 15:37:25 -0700712 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800713 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700714 flags = append(flags, "-I"+src.String())
715 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700716
Martijn Coeneneab15642018-03-09 09:29:59 +0100717 if Bool(j.deviceProperties.Aidl.Generate_traces) {
718 flags = append(flags, "-t")
719 }
720
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100721 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
722 flags = append(flags, "--transaction_names")
723 }
724
Colin Cross3047fa22019-04-18 10:56:44 -0700725 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700726}
727
Colin Cross32f676a2017-09-06 13:41:06 -0700728type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800729 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700730 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800731 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700732 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800733 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700734 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700735 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700736 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700737 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800738 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700739 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700740 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700741 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700742 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800743 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800744
745 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700746}
Colin Cross2fe66872015-03-30 17:20:39 -0700747
Colin Cross54250902017-12-05 09:28:08 -0800748func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
749 for _, f := range dep.Srcs() {
750 if f.Ext() != ".jar" {
751 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
752 ctx.OtherModuleName(dep.(blueprint.Module)))
753 }
754 }
755}
756
Jiyong Park2d492942018-03-05 17:44:10 +0900757type linkType int
758
759const (
760 javaCore linkType = iota
761 javaSdk
762 javaSystem
763 javaPlatform
764)
765
Jeongik Cha75b83b02019-11-01 15:28:00 +0900766type linkTypeContext interface {
767 android.Module
768 getLinkType(name string) (ret linkType, stubs bool)
769}
770
771func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700772 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700773 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900774 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
775 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100776 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900777 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900778 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900779 return javaCore, false
780 case name == "android_system_stubs_current":
781 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900782 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900783 return javaSystem, false
784 case name == "android_test_stubs_current":
785 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900786 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900787 return javaPlatform, false
788 case name == "android_stubs_current":
789 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900790 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900791 return javaSdk, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900792 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900793 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900794 case !ver.valid():
795 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700796 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900797 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900798 }
799}
800
Jeongik Cha75b83b02019-11-01 15:28:00 +0900801func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700802 if ctx.Host() {
803 return
804 }
805
Jeongik Cha75b83b02019-11-01 15:28:00 +0900806 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900807 if stubs {
808 return
809 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900810 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900811 commonMessage := "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source."
812
813 switch myLinkType {
814 case javaCore:
815 if otherLinkType != javaCore {
816 ctx.ModuleErrorf("compiles against core Java API, but dependency %q is compiling against non-core Java APIs."+commonMessage,
Jiyong Park750e5572018-01-31 00:20:13 +0900817 ctx.OtherModuleName(to))
818 }
Jiyong Park2d492942018-03-05 17:44:10 +0900819 break
820 case javaSdk:
821 if otherLinkType != javaCore && otherLinkType != javaSdk {
822 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
823 ctx.OtherModuleName(to))
824 }
825 break
826 case javaSystem:
827 if otherLinkType == javaPlatform {
828 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
829 ctx.OtherModuleName(to))
830 }
831 break
832 case javaPlatform:
833 // no restriction on link-type
834 break
Jiyong Park750e5572018-01-31 00:20:13 +0900835 }
836}
837
Colin Cross32f676a2017-09-06 13:41:06 -0700838func (j *Module) collectDeps(ctx android.ModuleContext) deps {
839 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700840
Colin Cross300f0382018-03-06 13:11:51 -0800841 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700842 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800843 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700844 ctx.AddMissingDependencies(sdkDep.bootclasspath)
845 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800846 } else if sdkDep.useFiles {
847 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700848 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700849 deps.aidlPreprocess = sdkDep.aidl
850 } else {
851 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800852 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700853 }
854
Colin Crossd11fcda2017-10-23 17:59:01 -0700855 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700856 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700857 tag := ctx.OtherModuleDependencyTag(module)
858
Colin Crossa4f08812018-10-02 22:03:40 -0700859 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700860 // Handled by AndroidApp.collectAppDeps
861 return
862 }
863 if tag == certificateTag {
864 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700865 return
866 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900867 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900868 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900869 if to, ok := module.(linkTypeContext); ok {
870 switch tag {
871 case bootClasspathTag, libTag, staticLibTag:
872 checkLinkType(ctx, j, to, tag.(dependencyTag))
873 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700874 }
Jiyong Park750e5572018-01-31 00:20:13 +0900875 }
Colin Cross54250902017-12-05 09:28:08 -0800876 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800877 case SdkLibraryDependency:
878 switch tag {
879 case libTag:
880 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
881 // names of sdk libs that are directly depended are exported
882 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700883 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800884 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
885 }
Colin Cross54250902017-12-05 09:28:08 -0800886 case Dependency:
887 switch tag {
888 case bootClasspathTag:
889 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700890 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800891 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900892 // sdk lib names from dependencies are re-exported
893 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700894 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000895 pluginJars, pluginClasses := dep.ExportedPlugins()
896 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700897 case java9LibTag:
898 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800899 case staticLibTag:
900 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
901 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
902 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700903 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900904 // sdk lib names from dependencies are re-exported
905 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700906 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000907 pluginJars, pluginClasses := dep.ExportedPlugins()
908 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800909 case pluginTag:
910 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800911 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000912 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
913 } else {
914 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800915 }
916 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
917 } else {
918 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
919 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000920 case exportedPluginTag:
921 if plugin, ok := dep.(*Plugin); ok {
922 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
923 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
924 }
925 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
926 if plugin.pluginProperties.Processor_class != nil {
927 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
928 }
929 } else {
930 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
931 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800932 case frameworkApkTag:
933 if ctx.ModuleName() == "android_stubs_current" ||
934 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700935 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800936 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
937 // resource files out of there for aapt.
938 //
939 // Normally the package rule runs aapt, which includes the resource,
940 // but we're not running that in our package rule so just copy in the
941 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700942 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800943 }
Colin Cross54250902017-12-05 09:28:08 -0800944 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700945 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800946 case kotlinAnnotationsTag:
947 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800948 }
949
Colin Cross54250902017-12-05 09:28:08 -0800950 case android.SourceFileProducer:
951 switch tag {
952 case libTag:
953 checkProducesJars(ctx, dep)
954 deps.classpath = append(deps.classpath, dep.Srcs()...)
955 case staticLibTag:
956 checkProducesJars(ctx, dep)
957 deps.classpath = append(deps.classpath, dep.Srcs()...)
958 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
959 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800960 }
961 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700962 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100963 case bootClasspathTag:
964 // If a system modules dependency has been added to the bootclasspath
965 // then add its libs to the bootclasspath.
966 sm := module.(*SystemModules)
967 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
968
Colin Cross1369cdb2017-09-29 17:58:17 -0700969 case systemModulesTag:
970 if deps.systemModules != nil {
971 panic("Found two system module dependencies")
972 }
973 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000974 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700975 panic("Missing directory for system module dependency")
976 }
Colin Crossb77043e2019-07-16 13:57:13 -0700977 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700978 }
Colin Crossec7a0422017-07-07 14:47:12 -0700979 }
Colin Cross2fe66872015-03-30 17:20:39 -0700980 })
981
Jiyong Park1be96912018-05-28 18:02:19 +0900982 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
983
Colin Cross32f676a2017-09-06 13:41:06 -0700984 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700985}
986
Artur Satayev9cf46692019-11-26 18:08:34 +0000987func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
988 deps.processorPath = append(deps.processorPath, pluginJars...)
989 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
990}
991
Colin Cross1e743852019-10-28 11:37:20 -0700992func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +0900993 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -0700994 if err != nil {
995 ctx.PropertyErrorf("sdk_version", "%s", err)
996 }
Nan Zhang357466b2018-04-17 17:38:36 -0700997 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700998 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700999 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001000 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001001 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001002 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001003 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1004 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001005 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001006 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001007 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001008 }
Nan Zhang357466b2018-04-17 17:38:36 -07001009}
1010
Colin Cross1e743852019-10-28 11:37:20 -07001011type javaVersion int
1012
1013const (
1014 JAVA_VERSION_UNSUPPORTED = 0
1015 JAVA_VERSION_6 = 6
1016 JAVA_VERSION_7 = 7
1017 JAVA_VERSION_8 = 8
1018 JAVA_VERSION_9 = 9
1019)
1020
1021func (v javaVersion) String() string {
1022 switch v {
1023 case JAVA_VERSION_6:
1024 return "1.6"
1025 case JAVA_VERSION_7:
1026 return "1.7"
1027 case JAVA_VERSION_8:
1028 return "1.8"
1029 case JAVA_VERSION_9:
1030 return "1.9"
1031 default:
1032 return "unsupported"
1033 }
1034}
1035
1036// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1037func (v javaVersion) usesJavaModules() bool {
1038 return v >= 9
1039}
1040
1041func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001042 switch javaVersion {
1043 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001044 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001045 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001046 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001047 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001048 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001049 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001050 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001051 case "10", "11":
1052 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001053 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001054 default:
1055 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001056 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001057 }
1058}
1059
Nan Zhanged19fc32017-10-19 13:06:22 -07001060func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001061
Colin Crossf03c82b2015-04-13 13:53:40 -07001062 var flags javaBuilderFlags
1063
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001064 // javaVersion flag.
1065 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1066
Nan Zhanged19fc32017-10-19 13:06:22 -07001067 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001068 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001069 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001070 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001071 }
Colin Cross6510f912017-11-29 00:27:14 -08001072 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001073 // Override the -g flag passed globally to remove local variable debug info to reduce
1074 // disk and memory usage.
1075 javacFlags = append(javacFlags, "-g:source,lines")
1076 }
Colin Crossc228a702019-11-06 16:18:05 -08001077 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001078
Colin Cross66548102018-06-19 22:47:35 -07001079 if ctx.Config().RunErrorProne() {
1080 if config.ErrorProneClasspath == nil {
1081 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1082 }
1083
1084 errorProneFlags := []string{
1085 "-Xplugin:ErrorProne",
1086 "${config.ErrorProneChecks}",
1087 }
1088 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1089
1090 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1091 "'" + strings.Join(errorProneFlags, " ") + "'"
1092 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001093 }
1094
Nan Zhanged19fc32017-10-19 13:06:22 -07001095 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001096 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1097 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001098 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001099 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001100
Colin Crossbe9cdb82019-01-21 21:37:16 -08001101 flags.processor = strings.Join(deps.processorClasses, ",")
1102
Colin Cross1e743852019-10-28 11:37:20 -07001103 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1104 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001105 // Give host-side tools a version of OpenJDK's standard libraries
1106 // close to what they're targeting. As of Dec 2017, AOSP is only
1107 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1108 //
1109 // When building with OpenJDK 8, the following should have no
1110 // effect since those jars would be available by default.
1111 //
1112 // When building with OpenJDK 9 but targeting a version < 1.8,
1113 // putting them on the bootclasspath means that:
1114 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1115 // b) references to existing APIs are not reinterpreted in an
1116 // OpenJDK 9-specific way, eg. calls to subclasses of
1117 // java.nio.Buffer as in http://b/70862583
1118 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1119 flags.bootClasspath = append(flags.bootClasspath,
1120 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1121 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001122 if Bool(j.properties.Use_tools_jar) {
1123 flags.bootClasspath = append(flags.bootClasspath,
1124 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1125 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001126 }
1127
Colin Cross1e743852019-10-28 11:37:20 -07001128 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001129 // Manually specify build directory in case it is not under the repo root.
1130 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1131 // just adding a symlink under the root doesn't help.)
1132 patchPaths := ".:" + ctx.Config().BuildDir()
1133 classPath := flags.classpath.FormJavaClassPath("")
1134 if classPath != "" {
1135 patchPaths += ":" + classPath
1136 }
1137 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001138 }
1139
Nan Zhanged19fc32017-10-19 13:06:22 -07001140 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001141 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001142
Nan Zhanged19fc32017-10-19 13:06:22 -07001143 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001144 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001145
Colin Cross81440082018-08-15 20:21:55 -07001146 if len(javacFlags) > 0 {
1147 // optimization.
1148 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1149 flags.javacFlags = "$javacFlags"
1150 }
1151
Nan Zhanged19fc32017-10-19 13:06:22 -07001152 return flags
1153}
Colin Crossc0b06f12015-04-08 13:03:43 -07001154
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001155func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001156 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001157
1158 deps := j.collectDeps(ctx)
1159 flags := j.collectBuilderFlags(ctx, deps)
1160
Colin Cross1e743852019-10-28 11:37:20 -07001161 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001162 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1163 }
Colin Cross8a497952019-03-05 22:25:09 -08001164 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001165 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001166 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001167 }
1168
Colin Crossaf050172017-11-15 23:01:59 -08001169 srcFiles = j.genSources(ctx, srcFiles, flags)
1170
1171 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001172 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001173 if aaptSrcJar != nil {
1174 srcJars = append(srcJars, aaptSrcJar)
1175 }
Colin Crossb7a63242015-04-16 14:09:14 -07001176
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001177 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001178 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001179 }
1180
Colin Cross1ee23172017-10-18 14:44:18 -07001181 jarName := ctx.ModuleName() + ".jar"
1182
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001183 javaSrcFiles := srcFiles.FilterByExt(".java")
1184 var uniqueSrcFiles android.Paths
1185 set := make(map[string]bool)
1186 for _, v := range javaSrcFiles {
1187 if _, found := set[v.String()]; !found {
1188 set[v.String()] = true
1189 uniqueSrcFiles = append(uniqueSrcFiles, v)
1190 }
1191 }
1192
patricktu242faad2019-09-24 15:41:30 +08001193 // Collect .java files for AIDEGen
1194 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1195
Colin Cross55f63ea2018-08-27 12:37:09 -07001196 var kotlinJars android.Paths
1197
Colin Cross93e85952017-08-15 13:34:18 -07001198 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001199 // user defined kotlin flags.
1200 kotlincFlags := j.properties.Kotlincflags
1201 CheckKotlincFlags(ctx, kotlincFlags)
1202
Colin Cross93e85952017-08-15 13:34:18 -07001203 // If there are kotlin files, compile them first but pass all the kotlin and java files
1204 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1205 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001206 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001207 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001208 kotlincFlags = append(kotlincFlags, "-no-jdk")
1209 }
1210 if len(kotlincFlags) > 0 {
1211 // optimization.
1212 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1213 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001214 }
1215
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001216 var kotlinSrcFiles android.Paths
1217 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1218 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1219
patricktu242faad2019-09-24 15:41:30 +08001220 // Collect .kt files for AIDEGen
1221 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1222
Colin Crossafbb1732019-01-17 15:42:52 -08001223 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1224 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1225
1226 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1227 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1228
1229 if len(flags.processorPath) > 0 {
1230 // Use kapt for annotation processing
1231 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1232 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1233 srcJars = append(srcJars, kaptSrcJar)
1234 // Disable annotation processing in javac, it's already been handled by kapt
1235 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001236 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001237 }
Colin Cross93e85952017-08-15 13:34:18 -07001238
Colin Cross1ee23172017-10-18 14:44:18 -07001239 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001240 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001241 if ctx.Failed() {
1242 return
1243 }
1244
1245 // Make javac rule depend on the kotlinc rule
1246 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001247
Colin Cross93e85952017-08-15 13:34:18 -07001248 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001249 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001250 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001251 }
1252
Colin Cross55f63ea2018-08-27 12:37:09 -07001253 jars := append(android.Paths(nil), kotlinJars...)
1254
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001255 // Store the list of .java files that was passed to javac
1256 j.compiledJavaSrcs = uniqueSrcFiles
1257 j.compiledSrcJars = srcJars
1258
Nan Zhang61eaedb2017-11-02 13:28:15 -07001259 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001260 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001261 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1262 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001263 // Formerly, there was a check here that prevented annotation processors
1264 // from being used when sharding was enabled, as some annotation processors
1265 // do not function correctly in sharded environments. It was removed to
1266 // allow for the use of annotation processors that do function correctly
1267 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001268 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001269 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001270 if ctx.Failed() {
1271 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001272 }
1273 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001274 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001275 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001276 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001277 // If error-prone is enabled, add an additional rule to compile the java files into
1278 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001279 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001280 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1281 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001282 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001283 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001284 extraJarDeps = append(extraJarDeps, errorprone)
1285 }
1286
Nan Zhang61eaedb2017-11-02 13:28:15 -07001287 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001288 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001289 shardSize := int(*(j.properties.Javac_shard_size))
1290 var shardSrcs []android.Paths
1291 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001292 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001293 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001294 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1295 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001296 jars = append(jars, classes)
1297 }
1298 }
1299 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001300 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1301 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001302 jars = append(jars, classes)
1303 }
1304 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001305 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001306 jars = append(jars, classes)
1307 }
Colin Crossd6891432017-09-27 17:39:56 -07001308 if ctx.Failed() {
1309 return
1310 }
Colin Cross2fe66872015-03-30 17:20:39 -07001311 }
1312
Colin Cross0c4ce212019-05-03 15:28:19 -07001313 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1314
1315 var includeSrcJar android.WritablePath
1316 if Bool(j.properties.Include_srcs) {
1317 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1318 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1319 }
1320
Colin Crosscedd4762018-09-13 11:26:19 -07001321 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1322 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001323 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001324 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001325
1326 var resArgs []string
1327 var resDeps android.Paths
1328
1329 resArgs = append(resArgs, dirArgs...)
1330 resDeps = append(resDeps, dirDeps...)
1331
1332 resArgs = append(resArgs, fileArgs...)
1333 resDeps = append(resDeps, fileDeps...)
1334
Colin Cross988708c2019-05-06 14:04:11 -07001335 resArgs = append(resArgs, extraArgs...)
1336 resDeps = append(resDeps, extraDeps...)
1337
Colin Cross40a36712017-09-27 17:41:35 -07001338 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001339 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001340 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001341 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001342 if ctx.Failed() {
1343 return
1344 }
1345 }
1346
Colin Cross0c4ce212019-05-03 15:28:19 -07001347 var resourceJars android.Paths
1348 if j.resourceJar != nil {
1349 resourceJars = append(resourceJars, j.resourceJar)
1350 }
1351 if Bool(j.properties.Include_srcs) {
1352 resourceJars = append(resourceJars, includeSrcJar)
1353 }
1354 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001355
Colin Cross0c4ce212019-05-03 15:28:19 -07001356 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001357 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001358 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001359 false, nil, nil)
1360 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001361 } else if len(resourceJars) == 1 {
1362 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001363 }
1364
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001365 if len(deps.staticJars) > 0 {
1366 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001367 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001368
Colin Cross094054a2018-10-17 15:10:48 -07001369 manifest := j.overrideManifest
1370 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001371 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001372 }
Colin Cross635acc92017-09-12 22:50:46 -07001373
Colin Cross8a497952019-03-05 22:25:09 -08001374 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001375 if len(services) > 0 {
1376 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1377 var zipargs []string
1378 for _, file := range services {
1379 serviceFile := file.String()
1380 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1381 }
1382 ctx.Build(pctx, android.BuildParams{
1383 Rule: zip,
1384 Output: servicesJar,
1385 Implicits: services,
1386 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001387 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001388 },
1389 })
1390 jars = append(jars, servicesJar)
1391 }
1392
Colin Cross0a6e0072017-08-30 14:24:55 -07001393 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001394 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001395 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001396
1397 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001398 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1399 // Optimization: skip the combine step if there is nothing to do
1400 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1401 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1402 // any if len(jars) == 1.
1403 outputFile = moduleOutPath
1404 } else {
1405 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1406 ctx.Build(pctx, android.BuildParams{
1407 Rule: android.Cp,
1408 Input: jars[0],
1409 Output: combinedJar,
1410 })
1411 outputFile = combinedJar
1412 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001413 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001414 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001415 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001416 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001417 outputFile = combinedJar
1418 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001419
Colin Cross331a1212018-08-15 20:40:52 -07001420 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001421 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001422 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001423 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001424 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001425 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001426
1427 // jarjar resource jar if necessary
1428 if j.resourceJar != nil {
1429 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001430 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001431 j.resourceJar = resourceJarJarFile
1432 }
1433
Colin Cross0a6e0072017-08-30 14:24:55 -07001434 if ctx.Failed() {
1435 return
1436 }
1437 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001438
1439 // Check package restrictions if necessary.
1440 if len(j.properties.Permitted_packages) > 0 {
1441 // Check packages and copy to package-checked file.
1442 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1443 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1444 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1445
1446 if ctx.Failed() {
1447 return
1448 }
1449 }
1450
Nan Zhanged19fc32017-10-19 13:06:22 -07001451 j.implementationJarFile = outputFile
1452 if j.headerJarFile == nil {
1453 j.headerJarFile = j.implementationJarFile
1454 }
Colin Cross2fe66872015-03-30 17:20:39 -07001455
Colin Cross3144dfc2018-01-03 15:06:47 -08001456 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001457 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1458 }
1459
Colin Cross331a1212018-08-15 20:40:52 -07001460 // merge implementation jar with resources if necessary
1461 implementationAndResourcesJar := outputFile
1462 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001463 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001464 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001465 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001466 false, nil, nil)
1467 implementationAndResourcesJar = combinedJar
1468 }
1469
1470 j.implementationAndResourcesJar = implementationAndResourcesJar
1471
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001472 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001473 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001474 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001475 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001476 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001477 if ctx.Failed() {
1478 return
1479 }
Colin Cross331a1212018-08-15 20:40:52 -07001480
Jiyong Park09cb6292019-07-15 15:29:23 +09001481 // Hidden API CSV generation and dex encoding
1482 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1483 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001484
Colin Cross331a1212018-08-15 20:40:52 -07001485 // merge dex jar with resources if necessary
1486 if j.resourceJar != nil {
1487 jars := android.Paths{dexOutputFile, j.resourceJar}
1488 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1489 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1490 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001491 if j.deviceProperties.UncompressDex {
1492 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1493 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1494 dexOutputFile = combinedAlignedJar
1495 } else {
1496 dexOutputFile = combinedJar
1497 }
Colin Cross331a1212018-08-15 20:40:52 -07001498 }
1499
1500 j.dexJarFile = dexOutputFile
1501
Colin Cross8faf8fc2019-01-16 15:15:52 -08001502 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001503 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1504
1505 j.maybeStrippedDexJarFile = dexOutputFile
1506
Colin Cross3063b782018-08-15 11:19:12 -07001507 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001508
1509 if ctx.Failed() {
1510 return
1511 }
Colin Cross331a1212018-08-15 20:40:52 -07001512 } else {
1513 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001514 }
Colin Cross331a1212018-08-15 20:40:52 -07001515
Colin Crossb7a63242015-04-16 14:09:14 -07001516 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001517
1518 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1519 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001520}
1521
Colin Cross3b706fd2019-09-05 16:44:18 -07001522func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1523 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1524
1525 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1526 if idx >= 0 {
1527 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1528 jarName += strconv.Itoa(idx)
1529 }
1530
1531 classes := android.PathForModuleOut(ctx, "javac", jarName)
1532 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1533
1534 if ctx.Config().EmitXrefRules() {
1535 extractionFile := android.PathForModuleOut(ctx, kzipName)
1536 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1537 j.kytheFiles = append(j.kytheFiles, extractionFile)
1538 }
1539
1540 return classes
1541}
1542
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001543// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1544// since some of these flags may be used internally.
1545func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1546 for _, flag := range flags {
1547 flag = strings.TrimSpace(flag)
1548
1549 if !strings.HasPrefix(flag, "-") {
1550 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1551 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1552 ctx.PropertyErrorf("kotlincflags",
1553 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1554 } else if inList(flag, config.KotlincIllegalFlags) {
1555 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1556 } else if flag == "-include-runtime" {
1557 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1558 } else {
1559 args := strings.Split(flag, " ")
1560 if args[0] == "-kotlin-home" {
1561 ctx.PropertyErrorf("kotlincflags",
1562 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1563 }
1564 }
1565 }
1566}
1567
Colin Cross8eadbf02017-10-24 17:46:00 -07001568func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001569 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001570
1571 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001572 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001573 // Compile java sources into turbine.jar.
1574 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1575 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1576 if ctx.Failed() {
1577 return nil
1578 }
1579 jars = append(jars, turbineJar)
1580 }
1581
Colin Cross55f63ea2018-08-27 12:37:09 -07001582 jars = append(jars, extraJars...)
1583
Nan Zhanged19fc32017-10-19 13:06:22 -07001584 // Combine any static header libraries into classes-header.jar. If there is only
1585 // one input jar this step will be skipped.
1586 var headerJar android.Path
1587 jars = append(jars, deps.staticHeaderJars...)
1588
Colin Cross5c6ecc12017-10-23 18:12:27 -07001589 // we cannot skip the combine step for now if there is only one jar
1590 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1591 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001592 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001593 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001594 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001595
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001596 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001597 // Transform classes.jar into classes-jarjar.jar
1598 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001599 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001600 headerJar = jarjarFile
1601 if ctx.Failed() {
1602 return nil
1603 }
1604 }
1605
1606 return headerJar
1607}
1608
Colin Crosscb933592017-11-22 13:49:43 -08001609func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001610 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001611
Colin Cross7a3139e2017-12-19 13:57:50 -08001612 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001613
Colin Cross84c38822018-01-03 15:59:46 -08001614 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001615 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1616
1617 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1618
1619 j.jacocoReportClassesFile = jacocoReportClassesFile
1620
1621 return instrumentedJar
1622}
1623
albaltai36ff7dc2018-12-25 14:35:23 +08001624var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001625
Nan Zhanged19fc32017-10-19 13:06:22 -07001626func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001627 if j.headerJarFile == nil {
1628 return nil
1629 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001630 return android.Paths{j.headerJarFile}
1631}
1632
1633func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001634 if j.implementationJarFile == nil {
1635 return nil
1636 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001637 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001638}
1639
Colin Crossf24a22a2019-01-31 14:12:44 -08001640func (j *Module) DexJar() android.Path {
1641 return j.dexJarFile
1642}
1643
Colin Cross331a1212018-08-15 20:40:52 -07001644func (j *Module) ResourceJars() android.Paths {
1645 if j.resourceJar == nil {
1646 return nil
1647 }
1648 return android.Paths{j.resourceJar}
1649}
1650
1651func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001652 if j.implementationAndResourcesJar == nil {
1653 return nil
1654 }
Colin Cross331a1212018-08-15 20:40:52 -07001655 return android.Paths{j.implementationAndResourcesJar}
1656}
1657
Colin Cross46c9b8b2017-06-22 16:51:17 -07001658func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001659 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001660 return j.exportAidlIncludeDirs
1661}
1662
Jiyong Park1be96912018-05-28 18:02:19 +09001663func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001664 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001665 return j.exportedSdkLibs
1666}
1667
Artur Satayev9cf46692019-11-26 18:08:34 +00001668func (j *Module) ExportedPlugins() (android.Paths, []string) {
1669 return j.exportedPluginJars, j.exportedPluginClasses
1670}
1671
Colin Cross0c4ce212019-05-03 15:28:19 -07001672func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1673 return j.srcJarArgs, j.srcJarDeps
1674}
1675
Colin Cross46c9b8b2017-06-22 16:51:17 -07001676var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001677
Colin Cross46c9b8b2017-06-22 16:51:17 -07001678func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001679 return j.logtagsSrcs
1680}
1681
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001682// Collect information for opening IDE project files in java/jdeps.go.
1683func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1684 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1685 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001686 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001687 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001688 if j.expandJarjarRules != nil {
1689 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001690 }
1691}
1692
1693func (j *Module) CompilerDeps() []string {
1694 jdeps := []string{}
1695 jdeps = append(jdeps, j.properties.Libs...)
1696 jdeps = append(jdeps, j.properties.Static_libs...)
1697 return jdeps
1698}
1699
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001700func (j *Module) hasCode(ctx android.ModuleContext) bool {
1701 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1702 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1703}
1704
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001705func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1706 depTag := ctx.OtherModuleDependencyTag(dep)
1707 // dependencies other than the static linkage are all considered crossing APEX boundary
1708 return depTag == staticLibTag
1709}
1710
Jiyong Park0b238752019-10-29 11:23:10 +09001711func (j *Module) Stem() string {
1712 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1713}
1714
Jiyong Park618922e2020-01-08 13:35:43 +09001715func (j *Module) JacocoReportClassesFile() android.Path {
1716 return j.jacocoReportClassesFile
1717}
1718
Colin Cross2fe66872015-03-30 17:20:39 -07001719//
1720// Java libraries (.jar file)
1721//
1722
Colin Crossf506d872017-07-19 15:53:04 -07001723type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001724 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001725
1726 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001727}
1728
Colin Cross42be7612019-02-21 18:12:14 -08001729func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001730 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1731 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1732 return true
1733 }
1734
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001735 // Store uncompressed (and do not strip) dex files from boot class path jars.
1736 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1737 return true
1738 }
1739
1740 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001741 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001742 return true
1743 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001744 if ctx.Config().UncompressPrivAppDex() &&
1745 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1746 return true
1747 }
1748
Colin Cross2fc72f62018-12-21 12:59:54 -08001749 return false
1750}
1751
Colin Crossf506d872017-07-19 15:53:04 -07001752func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001753 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001754 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001755 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001756 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001757 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001758 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001759 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001760
Jiyong Park7f7766d2019-07-25 22:02:35 +09001761 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1762 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001763 var extraInstallDeps android.Paths
1764 if j.InstallMixin != nil {
1765 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1766 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001767 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001768 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001769 }
Colin Crossb7a63242015-04-16 14:09:14 -07001770}
1771
Colin Crossf506d872017-07-19 15:53:04 -07001772func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001773 j.deps(ctx)
1774}
1775
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001776const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001777 aidlIncludeDir = "aidl"
1778 javaDir = "java"
1779 jarFileSuffix = ".jar"
1780 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001781)
1782
Paul Duffina0dbf432019-12-05 11:25:53 +00001783// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001784func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1785 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1786}
1787
1788func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1789 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001790}
1791
Paul Duffin13879572019-11-28 14:31:38 +00001792type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001793 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001794}
1795
1796func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1797 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1798}
1799
1800func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1801 _, ok := module.(*Library)
1802 return ok
1803}
1804
Paul Duffina0dbf432019-12-05 11:25:53 +00001805func (mt *librarySdkMemberType) buildSnapshot(
1806 sdkModuleContext android.ModuleContext,
1807 builder android.SnapshotBuilder,
1808 member android.SdkMember,
1809 jarToExportGetter func(j *Library) android.Path) {
1810
Paul Duffin13879572019-11-28 14:31:38 +00001811 variants := member.Variants()
1812 if len(variants) != 1 {
1813 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1814 for _, variant := range variants {
1815 sdkModuleContext.ModuleErrorf(" %q", variant)
1816 }
1817 }
1818 variant := variants[0]
1819 j := variant.(*Library)
1820
Paul Duffina0dbf432019-12-05 11:25:53 +00001821 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001822 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001823 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001824
1825 for _, dir := range j.AidlIncludeDirs() {
1826 // TODO(jiyong): copy parcelable declarations only
1827 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1828 for _, file := range aidlFiles {
1829 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1830 }
1831 }
1832
Paul Duffin9d8d6092019-12-05 18:19:29 +00001833 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001834 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001835}
1836
Paul Duffina0dbf432019-12-05 11:25:53 +00001837type headerLibrarySdkMemberType struct {
1838 librarySdkMemberType
1839}
1840
1841func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1842 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1843 headerJars := j.HeaderJars()
1844 if len(headerJars) != 1 {
1845 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1846 }
1847
1848 return headerJars[0]
1849 })
1850}
1851
Paul Duffina0dbf432019-12-05 11:25:53 +00001852type implLibrarySdkMemberType struct {
1853 librarySdkMemberType
1854}
1855
1856func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1857 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1858 implementationJars := j.ImplementationJars()
1859 if len(implementationJars) != 1 {
1860 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1861 }
1862
1863 return implementationJars[0]
1864 })
1865}
1866
Colin Cross1b16b0e2019-02-12 14:41:32 -08001867// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1868//
1869// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1870// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1871// as a `static_libs` dependency of another module.
1872//
1873// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1874// a device.
1875//
1876// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1877// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001878func LibraryFactory() android.Module {
1879 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001880
Colin Cross9ae1b922018-06-26 17:59:05 -07001881 module.AddProperties(
1882 &module.Module.properties,
1883 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001884 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001885 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001886
Jiyong Park7f7766d2019-07-25 22:02:35 +09001887 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001888 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001889 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001890 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001891}
1892
Colin Cross1b16b0e2019-02-12 14:41:32 -08001893// java_library_static is an obsolete alias for java_library.
1894func LibraryStaticFactory() android.Module {
1895 return LibraryFactory()
1896}
1897
1898// java_library_host builds and links sources into a `.jar` file for the host.
1899//
1900// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1901// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001902func LibraryHostFactory() android.Module {
1903 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001904
Colin Cross6af17aa2017-09-20 12:59:05 -07001905 module.AddProperties(
1906 &module.Module.properties,
1907 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001908
Colin Cross9ae1b922018-06-26 17:59:05 -07001909 module.Module.properties.Installable = proptools.BoolPtr(true)
1910
Jiyong Park7f7766d2019-07-25 22:02:35 +09001911 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001912 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001913 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001914}
1915
1916//
Colin Crossb628ea52018-08-14 16:42:33 -07001917// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001918//
1919
1920type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001921 // list of compatibility suites (for example "cts", "vts") that the module should be
1922 // installed into.
1923 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001924
1925 // the name of the test configuration (for example "AndroidTest.xml") that should be
1926 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001927 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001928
Jack He33338892018-09-19 02:21:28 -07001929 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1930 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001931 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001932
Colin Crossd96ca352018-08-10 16:06:24 -07001933 // list of files or filegroup modules that provide data that should be installed alongside
1934 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001935 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001936
1937 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1938 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1939 // explicitly.
1940 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001941}
1942
Paul Duffin42df1442019-03-20 12:45:53 +00001943type testHelperLibraryProperties struct {
1944 // list of compatibility suites (for example "cts", "vts") that the module should be
1945 // installed into.
1946 Test_suites []string `android:"arch_variant"`
1947}
1948
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001949type prebuiltTestProperties struct {
1950 // list of compatibility suites (for example "cts", "vts") that the module should be
1951 // installed into.
1952 Test_suites []string `android:"arch_variant"`
1953
1954 // the name of the test configuration (for example "AndroidTest.xml") that should be
1955 // installed with the module.
1956 Test_config *string `android:"path,arch_variant"`
1957}
1958
Colin Cross05638fc2018-04-09 18:40:24 -07001959type Test struct {
1960 Library
1961
1962 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001963
1964 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001965 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001966}
1967
Paul Duffin42df1442019-03-20 12:45:53 +00001968type TestHelperLibrary struct {
1969 Library
1970
1971 testHelperLibraryProperties testHelperLibraryProperties
1972}
1973
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001974type JavaTestImport struct {
1975 Import
1976
1977 prebuiltTestProperties prebuiltTestProperties
1978
1979 testConfig android.Path
1980}
1981
Colin Cross303e21f2018-08-07 16:49:25 -07001982func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001983 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1984 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001985 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001986
1987 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001988}
1989
Paul Duffin42df1442019-03-20 12:45:53 +00001990func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1991 j.Library.GenerateAndroidBuildActions(ctx)
1992}
1993
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001994func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1995 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
1996 j.prebuiltTestProperties.Test_suites, nil)
1997
1998 j.Import.GenerateAndroidBuildActions(ctx)
1999}
2000
2001type testSdkMemberType struct {
2002 android.SdkMemberTypeBase
2003}
2004
2005func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2006 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2007}
2008
2009func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2010 _, ok := module.(*Test)
2011 return ok
2012}
2013
2014func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2015 variants := member.Variants()
2016 if len(variants) != 1 {
2017 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2018 for _, variant := range variants {
2019 sdkModuleContext.ModuleErrorf(" %q", variant)
2020 }
2021 }
2022 variant := variants[0]
2023 j := variant.(*Test)
2024
2025 implementationJars := j.ImplementationJars()
2026 if len(implementationJars) != 1 {
2027 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2028 }
2029
2030 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2031 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2032
2033 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2034 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2035
2036 module := builder.AddPrebuiltModule(member, "java_test_import")
2037 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2038 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2039}
2040
Colin Cross1b16b0e2019-02-12 14:41:32 -08002041// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2042// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2043//
2044// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2045// compiled against the device bootclasspath.
2046//
2047// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2048// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002049func TestFactory() android.Module {
2050 module := &Test{}
2051
2052 module.AddProperties(
2053 &module.Module.properties,
2054 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002055 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002056 &module.Module.protoProperties,
2057 &module.testProperties)
2058
Colin Cross9ae1b922018-06-26 17:59:05 -07002059 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002060 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002061
Colin Cross05638fc2018-04-09 18:40:24 -07002062 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002063 return module
2064}
2065
Paul Duffin42df1442019-03-20 12:45:53 +00002066// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2067func TestHelperLibraryFactory() android.Module {
2068 module := &TestHelperLibrary{}
2069
2070 module.AddProperties(
2071 &module.Module.properties,
2072 &module.Module.deviceProperties,
2073 &module.Module.dexpreoptProperties,
2074 &module.Module.protoProperties,
2075 &module.testHelperLibraryProperties)
2076
Colin Cross9a4abed2019-04-24 13:19:28 -07002077 module.Module.properties.Installable = proptools.BoolPtr(true)
2078 module.Module.dexpreopter.isTest = true
2079
Paul Duffin42df1442019-03-20 12:45:53 +00002080 InitJavaModule(module, android.HostAndDeviceSupported)
2081 return module
2082}
2083
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002084// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2085// and makes sure that it is added to the appropriate test suite.
2086//
2087// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2088// compiled against an Android classpath.
2089//
2090// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2091// for host modules.
2092func JavaTestImportFactory() android.Module {
2093 module := &JavaTestImport{}
2094
2095 module.AddProperties(
2096 &module.Import.properties,
2097 &module.prebuiltTestProperties)
2098
2099 module.Import.properties.Installable = proptools.BoolPtr(true)
2100
2101 android.InitPrebuiltModule(module, &module.properties.Jars)
2102 android.InitApexModule(module)
2103 android.InitSdkAwareModule(module)
2104 InitJavaModule(module, android.HostAndDeviceSupported)
2105 return module
2106}
2107
Colin Cross1b16b0e2019-02-12 14:41:32 -08002108// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2109// allow running the test with `atest` or a `TEST_MAPPING` file.
2110//
2111// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2112// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002113func TestHostFactory() android.Module {
2114 module := &Test{}
2115
2116 module.AddProperties(
2117 &module.Module.properties,
2118 &module.Module.protoProperties,
2119 &module.testProperties)
2120
Colin Cross9ae1b922018-06-26 17:59:05 -07002121 module.Module.properties.Installable = proptools.BoolPtr(true)
2122
Colin Cross05638fc2018-04-09 18:40:24 -07002123 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002124 return module
2125}
2126
2127//
Colin Cross2fe66872015-03-30 17:20:39 -07002128// Java Binaries (.jar file plus wrapper script)
2129//
2130
Colin Crossf506d872017-07-19 15:53:04 -07002131type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002132 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002133 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002134
2135 // Name of the class containing main to be inserted into the manifest as Main-Class.
2136 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002137}
2138
Colin Crossf506d872017-07-19 15:53:04 -07002139type Binary struct {
2140 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002141
Colin Crossf506d872017-07-19 15:53:04 -07002142 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002143
Colin Cross6b4a32d2017-12-05 13:42:45 -08002144 isWrapperVariant bool
2145
Colin Crossc3315992017-12-08 19:12:36 -08002146 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002147 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002148}
2149
Alex Light24237172017-10-26 09:46:21 -07002150func (j *Binary) HostToolPath() android.OptionalPath {
2151 return android.OptionalPathForPath(j.binaryFile)
2152}
2153
Colin Crossf506d872017-07-19 15:53:04 -07002154func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002155 if ctx.Arch().ArchType == android.Common {
2156 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002157 if j.binaryProperties.Main_class != nil {
2158 if j.properties.Manifest != nil {
2159 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2160 }
2161 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2162 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2163 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2164 }
2165
Colin Cross6b4a32d2017-12-05 13:42:45 -08002166 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002167 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002168 // Handle the binary wrapper
2169 j.isWrapperVariant = true
2170
Colin Cross366938f2017-12-11 16:29:02 -08002171 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002172 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002173 } else {
2174 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2175 }
2176
2177 // Depend on the installed jar so that the wrapper doesn't get executed by
2178 // another build rule before the jar has been installed.
2179 jarFile := ctx.PrimaryModule().(*Binary).installFile
2180
2181 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2182 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002183 }
Colin Cross2fe66872015-03-30 17:20:39 -07002184}
2185
Colin Crossf506d872017-07-19 15:53:04 -07002186func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002187 if ctx.Arch().ArchType == android.Common {
2188 j.deps(ctx)
2189 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002190}
2191
Colin Cross1b16b0e2019-02-12 14:41:32 -08002192// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2193// as well.
2194//
2195// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2196// compiled against the device bootclasspath.
2197//
2198// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2199// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002200func BinaryFactory() android.Module {
2201 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002202
Colin Cross36242852017-06-23 15:06:31 -07002203 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002204 &module.Module.properties,
2205 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002206 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002207 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002208 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002209
Colin Cross9ae1b922018-06-26 17:59:05 -07002210 module.Module.properties.Installable = proptools.BoolPtr(true)
2211
Colin Cross6b4a32d2017-12-05 13:42:45 -08002212 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2213 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002214 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002215}
2216
Colin Cross1b16b0e2019-02-12 14:41:32 -08002217// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2218//
2219// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2220// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002221func BinaryHostFactory() android.Module {
2222 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002223
Colin Cross36242852017-06-23 15:06:31 -07002224 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002225 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002226 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002227 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002228
Colin Cross9ae1b922018-06-26 17:59:05 -07002229 module.Module.properties.Installable = proptools.BoolPtr(true)
2230
Colin Cross6b4a32d2017-12-05 13:42:45 -08002231 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2232 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002233 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002234}
2235
2236//
2237// Java prebuilts
2238//
2239
Colin Cross74d73e22017-08-02 11:05:49 -07002240type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002241 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002242
Nan Zhangea568a42017-11-08 21:20:04 -08002243 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002244
2245 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002246
2247 // List of shared java libs that this module has dependencies to
2248 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002249
2250 // List of files to remove from the jar file(s)
2251 Exclude_files []string
2252
2253 // List of directories to remove from the jar file(s)
2254 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002255
2256 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002257 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002258
2259 // set the name of the output
2260 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002261}
2262
2263type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002264 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002265 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002266 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002267 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002268 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002269
Colin Cross74d73e22017-08-02 11:05:49 -07002270 properties ImportProperties
2271
Colin Cross0a6e0072017-08-30 14:24:55 -07002272 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002273 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002274}
2275
Jiyong Park6a927c42020-01-21 02:03:43 +09002276func (j *Import) sdkVersion() sdkSpec {
2277 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002278}
2279
Jiyong Park6a927c42020-01-21 02:03:43 +09002280func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002281 return j.sdkVersion()
2282}
2283
Colin Cross74d73e22017-08-02 11:05:49 -07002284func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002285 return &j.prebuilt
2286}
2287
Colin Cross74d73e22017-08-02 11:05:49 -07002288func (j *Import) PrebuiltSrcs() []string {
2289 return j.properties.Jars
2290}
2291
2292func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002293 return j.prebuilt.Name(j.ModuleBase.Name())
2294}
2295
Jiyong Park0b238752019-10-29 11:23:10 +09002296func (j *Import) Stem() string {
2297 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2298}
2299
Jiyong Park618922e2020-01-08 13:35:43 +09002300func (a *Import) JacocoReportClassesFile() android.Path {
2301 return nil
2302}
2303
Colin Cross74d73e22017-08-02 11:05:49 -07002304func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002305 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002306}
2307
Colin Cross74d73e22017-08-02 11:05:49 -07002308func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002309 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002310
Jiyong Park0b238752019-10-29 11:23:10 +09002311 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002312 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002313 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2314 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002315 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002316 inputFile := outputFile
2317 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2318 TransformJetifier(ctx, outputFile, inputFile)
2319 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002320 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002321
2322 ctx.VisitDirectDeps(func(module android.Module) {
2323 otherName := ctx.OtherModuleName(module)
2324 tag := ctx.OtherModuleDependencyTag(module)
2325
2326 switch dep := module.(type) {
2327 case Dependency:
2328 switch tag {
2329 case libTag, staticLibTag:
2330 // sdk lib names from dependencies are re-exported
2331 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2332 }
2333 case SdkLibraryDependency:
2334 switch tag {
2335 case libTag:
2336 // names of sdk libs that are directly depended are exported
2337 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2338 }
2339 }
2340 })
2341
2342 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002343 if Bool(j.properties.Installable) {
2344 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002345 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002346 }
Colin Cross2fe66872015-03-30 17:20:39 -07002347}
2348
Colin Cross74d73e22017-08-02 11:05:49 -07002349var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002350
Nan Zhanged19fc32017-10-19 13:06:22 -07002351func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002352 if j.combinedClasspathFile == nil {
2353 return nil
2354 }
Colin Cross37f6d792018-07-12 12:28:41 -07002355 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002356}
2357
2358func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002359 if j.combinedClasspathFile == nil {
2360 return nil
2361 }
Colin Cross37f6d792018-07-12 12:28:41 -07002362 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002363}
2364
Colin Cross331a1212018-08-15 20:40:52 -07002365func (j *Import) ResourceJars() android.Paths {
2366 return nil
2367}
2368
2369func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002370 if j.combinedClasspathFile == nil {
2371 return nil
2372 }
Colin Cross331a1212018-08-15 20:40:52 -07002373 return android.Paths{j.combinedClasspathFile}
2374}
2375
Colin Crossf24a22a2019-01-31 14:12:44 -08002376func (j *Import) DexJar() android.Path {
2377 return nil
2378}
2379
Colin Cross74d73e22017-08-02 11:05:49 -07002380func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002381 return nil
2382}
2383
Jiyong Park1be96912018-05-28 18:02:19 +09002384func (j *Import) ExportedSdkLibs() []string {
2385 return j.exportedSdkLibs
2386}
2387
Artur Satayev9cf46692019-11-26 18:08:34 +00002388func (j *Import) ExportedPlugins() (android.Paths, []string) {
2389 return nil, nil
2390}
2391
Colin Cross0c4ce212019-05-03 15:28:19 -07002392func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2393 return nil, nil
2394}
2395
albaltai36ff7dc2018-12-25 14:35:23 +08002396// Add compile time check for interface implementation
2397var _ android.IDEInfo = (*Import)(nil)
2398var _ android.IDECustomizedModuleName = (*Import)(nil)
2399
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002400// Collect information for opening IDE project files in java/jdeps.go.
2401const (
2402 removedPrefix = "prebuilt_"
2403)
2404
2405func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2406 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2407}
2408
2409func (j *Import) IDECustomizedModuleName() string {
2410 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2411 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2412 // solution to get the Import name.
2413 name := j.Name()
2414 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002415 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002416 }
2417 return name
2418}
2419
Colin Cross74d73e22017-08-02 11:05:49 -07002420var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002421
Colin Cross1b16b0e2019-02-12 14:41:32 -08002422// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2423//
2424// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2425// compiled against an Android classpath.
2426//
2427// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2428// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002429func ImportFactory() android.Module {
2430 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002431
Colin Cross74d73e22017-08-02 11:05:49 -07002432 module.AddProperties(&module.properties)
2433
2434 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002435 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002436 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002437 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002438 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002439}
2440
Colin Cross1b16b0e2019-02-12 14:41:32 -08002441// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2442// module.
2443//
2444// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2445// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002446func ImportFactoryHost() android.Module {
2447 module := &Import{}
2448
2449 module.AddProperties(&module.properties)
2450
2451 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002452 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002453 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002454 return module
2455}
2456
Colin Cross42be7612019-02-21 18:12:14 -08002457// dex_import module
2458
2459type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002460 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002461
2462 // set the name of the output
2463 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002464}
2465
2466type DexImport struct {
2467 android.ModuleBase
2468 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002469 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002470 prebuilt android.Prebuilt
2471
2472 properties DexImportProperties
2473
2474 dexJarFile android.Path
2475 maybeStrippedDexJarFile android.Path
2476
2477 dexpreopter
2478}
2479
2480func (j *DexImport) Prebuilt() *android.Prebuilt {
2481 return &j.prebuilt
2482}
2483
2484func (j *DexImport) PrebuiltSrcs() []string {
2485 return j.properties.Jars
2486}
2487
2488func (j *DexImport) Name() string {
2489 return j.prebuilt.Name(j.ModuleBase.Name())
2490}
2491
Jiyong Park0b238752019-10-29 11:23:10 +09002492func (j *DexImport) Stem() string {
2493 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2494}
2495
Colin Cross42be7612019-02-21 18:12:14 -08002496func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2497 if len(j.properties.Jars) != 1 {
2498 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2499 }
2500
Jiyong Park0b238752019-10-29 11:23:10 +09002501 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002502 j.dexpreopter.isInstallable = true
2503 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2504
2505 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2506 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2507
2508 if j.dexpreopter.uncompressedDex {
2509 rule := android.NewRuleBuilder()
2510
2511 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2512 rule.Temporary(temporary)
2513
2514 // use zip2zip to uncompress classes*.dex files
2515 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002516 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002517 FlagWithInput("-i ", inputJar).
2518 FlagWithOutput("-o ", temporary).
2519 FlagWithArg("-0 ", "'classes*.dex'")
2520
2521 // use zipalign to align uncompressed classes*.dex files
2522 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002523 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002524 Flag("-f").
2525 Text("4").
2526 Input(temporary).
2527 Output(dexOutputFile)
2528
2529 rule.DeleteTemporaryFiles()
2530
2531 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2532 } else {
2533 ctx.Build(pctx, android.BuildParams{
2534 Rule: android.Cp,
2535 Input: inputJar,
2536 Output: dexOutputFile,
2537 })
2538 }
2539
2540 j.dexJarFile = dexOutputFile
2541
2542 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2543
2544 j.maybeStrippedDexJarFile = dexOutputFile
2545
2546 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2547 ctx.ModuleName()+".jar", dexOutputFile)
2548}
2549
2550func (j *DexImport) DexJar() android.Path {
2551 return j.dexJarFile
2552}
2553
2554// dex_import imports a `.jar` file containing classes.dex files.
2555//
2556// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2557// to the device.
2558func DexImportFactory() android.Module {
2559 module := &DexImport{}
2560
2561 module.AddProperties(&module.properties)
2562
2563 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002564 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002565 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002566 return module
2567}
2568
Colin Cross89536d42017-07-07 14:35:50 -07002569//
2570// Defaults
2571//
2572type Defaults struct {
2573 android.ModuleBase
2574 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002575 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002576}
2577
Colin Cross1b16b0e2019-02-12 14:41:32 -08002578// java_defaults provides a set of properties that can be inherited by other java or android modules.
2579//
2580// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2581// property in the defaults module that exists in the depending module will be prepended to the depending module's
2582// value for that property.
2583//
2584// Example:
2585//
2586// java_defaults {
2587// name: "example_defaults",
2588// srcs: ["common/**/*.java"],
2589// javacflags: ["-Xlint:all"],
2590// aaptflags: ["--auto-add-overlay"],
2591// }
2592//
2593// java_library {
2594// name: "example",
2595// defaults: ["example_defaults"],
2596// srcs: ["example/**/*.java"],
2597// }
2598//
2599// is functionally identical to:
2600//
2601// java_library {
2602// name: "example",
2603// srcs: [
2604// "common/**/*.java",
2605// "example/**/*.java",
2606// ],
2607// javacflags: ["-Xlint:all"],
2608// }
Colin Cross89536d42017-07-07 14:35:50 -07002609func defaultsFactory() android.Module {
2610 return DefaultsFactory()
2611}
2612
Paul Duffin47357662019-12-05 14:07:14 +00002613func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002614 module := &Defaults{}
2615
Colin Cross89536d42017-07-07 14:35:50 -07002616 module.AddProperties(
2617 &CompilerProperties{},
2618 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002619 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002620 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002621 &aaptProperties{},
2622 &androidLibraryProperties{},
2623 &appProperties{},
2624 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002625 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002626 &ImportProperties{},
2627 &AARImportProperties{},
2628 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002629 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002630 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002631 )
2632
2633 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002634 return module
2635}
Nan Zhangea568a42017-11-08 21:20:04 -08002636
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002637func kytheExtractJavaFactory() android.Singleton {
2638 return &kytheExtractJavaSingleton{}
2639}
2640
2641type kytheExtractJavaSingleton struct {
2642}
2643
2644func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2645 var xrefTargets android.Paths
2646 ctx.VisitAllModules(func(module android.Module) {
2647 if javaModule, ok := module.(xref); ok {
2648 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2649 }
2650 })
2651 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2652 if len(xrefTargets) > 0 {
2653 ctx.Build(pctx, android.BuildParams{
2654 Rule: blueprint.Phony,
2655 Output: android.PathForPhony(ctx, "xref_java"),
2656 Inputs: xrefTargets,
2657 })
2658 }
2659}
2660
Nan Zhangea568a42017-11-08 21:20:04 -08002661var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002662var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002663var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002664var inList = android.InList