blob: c94ea829388958277cbf016cef443b82b08b07d9 [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 (
Jiyong Park50146e92020-01-30 18:00:15 +0900760 // TODO(jiyong) rename these for better readability. Make the allowed
761 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900762 javaCore linkType = iota
763 javaSdk
764 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900765 javaModule
Jiyong Park2d492942018-03-05 17:44:10 +0900766 javaPlatform
767)
768
Jeongik Cha75b83b02019-11-01 15:28:00 +0900769type linkTypeContext interface {
770 android.Module
771 getLinkType(name string) (ret linkType, stubs bool)
772}
773
774func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700775 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700776 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900777 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
778 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100779 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900780 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900781 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900782 return javaCore, false
783 case name == "android_system_stubs_current":
784 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900785 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900786 return javaSystem, false
787 case name == "android_test_stubs_current":
788 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900789 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900790 return javaPlatform, false
791 case name == "android_stubs_current":
792 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900793 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900794 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900795 case name == "android_module_lib_stubs_current":
796 return javaModule, true
797 case ver.kind == sdkModule:
798 return javaModule, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900799 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900800 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900801 case !ver.valid():
802 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700803 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900804 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900805 }
806}
807
Jeongik Cha75b83b02019-11-01 15:28:00 +0900808func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700809 if ctx.Host() {
810 return
811 }
812
Jeongik Cha75b83b02019-11-01 15:28:00 +0900813 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900814 if stubs {
815 return
816 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900817 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900818 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."
819
820 switch myLinkType {
821 case javaCore:
822 if otherLinkType != javaCore {
823 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 +0900824 ctx.OtherModuleName(to))
825 }
Jiyong Park2d492942018-03-05 17:44:10 +0900826 break
827 case javaSdk:
828 if otherLinkType != javaCore && otherLinkType != javaSdk {
829 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
830 ctx.OtherModuleName(to))
831 }
832 break
833 case javaSystem:
Jiyong Park50146e92020-01-30 18:00:15 +0900834 if otherLinkType == javaPlatform || otherLinkType == javaModule {
Jiyong Park2d492942018-03-05 17:44:10 +0900835 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
836 ctx.OtherModuleName(to))
837 }
838 break
Jiyong Park50146e92020-01-30 18:00:15 +0900839 case javaModule:
840 if otherLinkType == javaPlatform {
841 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
842 ctx.OtherModuleName(to))
843 }
844 break
Jiyong Park2d492942018-03-05 17:44:10 +0900845 case javaPlatform:
846 // no restriction on link-type
847 break
Jiyong Park750e5572018-01-31 00:20:13 +0900848 }
849}
850
Colin Cross32f676a2017-09-06 13:41:06 -0700851func (j *Module) collectDeps(ctx android.ModuleContext) deps {
852 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700853
Colin Cross300f0382018-03-06 13:11:51 -0800854 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700855 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800856 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700857 ctx.AddMissingDependencies(sdkDep.bootclasspath)
858 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800859 } else if sdkDep.useFiles {
860 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700861 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700862 deps.aidlPreprocess = sdkDep.aidl
863 } else {
864 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800865 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700866 }
867
Colin Crossd11fcda2017-10-23 17:59:01 -0700868 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700869 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700870 tag := ctx.OtherModuleDependencyTag(module)
871
Colin Crossa4f08812018-10-02 22:03:40 -0700872 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700873 // Handled by AndroidApp.collectAppDeps
874 return
875 }
876 if tag == certificateTag {
877 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700878 return
879 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900880 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900881 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900882 if to, ok := module.(linkTypeContext); ok {
883 switch tag {
884 case bootClasspathTag, libTag, staticLibTag:
885 checkLinkType(ctx, j, to, tag.(dependencyTag))
886 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700887 }
Jiyong Park750e5572018-01-31 00:20:13 +0900888 }
Colin Cross54250902017-12-05 09:28:08 -0800889 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800890 case SdkLibraryDependency:
891 switch tag {
892 case libTag:
893 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
894 // names of sdk libs that are directly depended are exported
895 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700896 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800897 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
898 }
Colin Cross54250902017-12-05 09:28:08 -0800899 case Dependency:
900 switch tag {
901 case bootClasspathTag:
902 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700903 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800904 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900905 // sdk lib names from dependencies are re-exported
906 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700907 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000908 pluginJars, pluginClasses := dep.ExportedPlugins()
909 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700910 case java9LibTag:
911 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800912 case staticLibTag:
913 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
914 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
915 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700916 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900917 // sdk lib names from dependencies are re-exported
918 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700919 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000920 pluginJars, pluginClasses := dep.ExportedPlugins()
921 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800922 case pluginTag:
923 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800924 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000925 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
926 } else {
927 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800928 }
929 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
930 } else {
931 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
932 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000933 case exportedPluginTag:
934 if plugin, ok := dep.(*Plugin); ok {
935 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
936 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
937 }
938 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
939 if plugin.pluginProperties.Processor_class != nil {
940 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
941 }
942 } else {
943 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
944 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800945 case frameworkApkTag:
946 if ctx.ModuleName() == "android_stubs_current" ||
947 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700948 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800949 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
950 // resource files out of there for aapt.
951 //
952 // Normally the package rule runs aapt, which includes the resource,
953 // but we're not running that in our package rule so just copy in the
954 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700955 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800956 }
Colin Cross54250902017-12-05 09:28:08 -0800957 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700958 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800959 case kotlinAnnotationsTag:
960 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800961 }
962
Colin Cross54250902017-12-05 09:28:08 -0800963 case android.SourceFileProducer:
964 switch tag {
965 case libTag:
966 checkProducesJars(ctx, dep)
967 deps.classpath = append(deps.classpath, dep.Srcs()...)
968 case staticLibTag:
969 checkProducesJars(ctx, dep)
970 deps.classpath = append(deps.classpath, dep.Srcs()...)
971 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
972 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800973 }
974 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700975 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100976 case bootClasspathTag:
977 // If a system modules dependency has been added to the bootclasspath
978 // then add its libs to the bootclasspath.
979 sm := module.(*SystemModules)
980 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
981
Colin Cross1369cdb2017-09-29 17:58:17 -0700982 case systemModulesTag:
983 if deps.systemModules != nil {
984 panic("Found two system module dependencies")
985 }
986 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000987 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700988 panic("Missing directory for system module dependency")
989 }
Colin Crossb77043e2019-07-16 13:57:13 -0700990 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700991 }
Colin Crossec7a0422017-07-07 14:47:12 -0700992 }
Colin Cross2fe66872015-03-30 17:20:39 -0700993 })
994
Jiyong Park1be96912018-05-28 18:02:19 +0900995 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
996
Colin Cross32f676a2017-09-06 13:41:06 -0700997 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700998}
999
Artur Satayev9cf46692019-11-26 18:08:34 +00001000func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1001 deps.processorPath = append(deps.processorPath, pluginJars...)
1002 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1003}
1004
Colin Cross1e743852019-10-28 11:37:20 -07001005func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001006 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001007 if err != nil {
1008 ctx.PropertyErrorf("sdk_version", "%s", err)
1009 }
Nan Zhang357466b2018-04-17 17:38:36 -07001010 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001011 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001012 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001013 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001014 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001015 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001016 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1017 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001018 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001019 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001020 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001021 }
Nan Zhang357466b2018-04-17 17:38:36 -07001022}
1023
Colin Cross1e743852019-10-28 11:37:20 -07001024type javaVersion int
1025
1026const (
1027 JAVA_VERSION_UNSUPPORTED = 0
1028 JAVA_VERSION_6 = 6
1029 JAVA_VERSION_7 = 7
1030 JAVA_VERSION_8 = 8
1031 JAVA_VERSION_9 = 9
1032)
1033
1034func (v javaVersion) String() string {
1035 switch v {
1036 case JAVA_VERSION_6:
1037 return "1.6"
1038 case JAVA_VERSION_7:
1039 return "1.7"
1040 case JAVA_VERSION_8:
1041 return "1.8"
1042 case JAVA_VERSION_9:
1043 return "1.9"
1044 default:
1045 return "unsupported"
1046 }
1047}
1048
1049// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1050func (v javaVersion) usesJavaModules() bool {
1051 return v >= 9
1052}
1053
1054func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001055 switch javaVersion {
1056 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001057 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001058 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001059 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001060 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001061 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001062 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001063 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001064 case "10", "11":
1065 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001066 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001067 default:
1068 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001069 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001070 }
1071}
1072
Nan Zhanged19fc32017-10-19 13:06:22 -07001073func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001074
Colin Crossf03c82b2015-04-13 13:53:40 -07001075 var flags javaBuilderFlags
1076
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001077 // javaVersion flag.
1078 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1079
Nan Zhanged19fc32017-10-19 13:06:22 -07001080 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001081 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001082 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001083 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001084 }
Colin Cross6510f912017-11-29 00:27:14 -08001085 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001086 // Override the -g flag passed globally to remove local variable debug info to reduce
1087 // disk and memory usage.
1088 javacFlags = append(javacFlags, "-g:source,lines")
1089 }
Colin Crossc228a702019-11-06 16:18:05 -08001090 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001091
Colin Cross66548102018-06-19 22:47:35 -07001092 if ctx.Config().RunErrorProne() {
1093 if config.ErrorProneClasspath == nil {
1094 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1095 }
1096
1097 errorProneFlags := []string{
1098 "-Xplugin:ErrorProne",
1099 "${config.ErrorProneChecks}",
1100 }
1101 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1102
1103 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1104 "'" + strings.Join(errorProneFlags, " ") + "'"
1105 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001106 }
1107
Nan Zhanged19fc32017-10-19 13:06:22 -07001108 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001109 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1110 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001111 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001112 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001113
Colin Crossbe9cdb82019-01-21 21:37:16 -08001114 flags.processor = strings.Join(deps.processorClasses, ",")
1115
Colin Cross1e743852019-10-28 11:37:20 -07001116 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1117 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001118 // Give host-side tools a version of OpenJDK's standard libraries
1119 // close to what they're targeting. As of Dec 2017, AOSP is only
1120 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1121 //
1122 // When building with OpenJDK 8, the following should have no
1123 // effect since those jars would be available by default.
1124 //
1125 // When building with OpenJDK 9 but targeting a version < 1.8,
1126 // putting them on the bootclasspath means that:
1127 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1128 // b) references to existing APIs are not reinterpreted in an
1129 // OpenJDK 9-specific way, eg. calls to subclasses of
1130 // java.nio.Buffer as in http://b/70862583
1131 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1132 flags.bootClasspath = append(flags.bootClasspath,
1133 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1134 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001135 if Bool(j.properties.Use_tools_jar) {
1136 flags.bootClasspath = append(flags.bootClasspath,
1137 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1138 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001139 }
1140
Colin Cross1e743852019-10-28 11:37:20 -07001141 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001142 // Manually specify build directory in case it is not under the repo root.
1143 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1144 // just adding a symlink under the root doesn't help.)
1145 patchPaths := ".:" + ctx.Config().BuildDir()
1146 classPath := flags.classpath.FormJavaClassPath("")
1147 if classPath != "" {
1148 patchPaths += ":" + classPath
1149 }
1150 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001151 }
1152
Nan Zhanged19fc32017-10-19 13:06:22 -07001153 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001154 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001155
Nan Zhanged19fc32017-10-19 13:06:22 -07001156 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001157 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001158
Colin Cross81440082018-08-15 20:21:55 -07001159 if len(javacFlags) > 0 {
1160 // optimization.
1161 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1162 flags.javacFlags = "$javacFlags"
1163 }
1164
Nan Zhanged19fc32017-10-19 13:06:22 -07001165 return flags
1166}
Colin Crossc0b06f12015-04-08 13:03:43 -07001167
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001168func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001169 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001170
1171 deps := j.collectDeps(ctx)
1172 flags := j.collectBuilderFlags(ctx, deps)
1173
Colin Cross1e743852019-10-28 11:37:20 -07001174 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001175 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1176 }
Colin Cross8a497952019-03-05 22:25:09 -08001177 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001178 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001179 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001180 }
1181
Colin Crossaf050172017-11-15 23:01:59 -08001182 srcFiles = j.genSources(ctx, srcFiles, flags)
1183
1184 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001185 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001186 if aaptSrcJar != nil {
1187 srcJars = append(srcJars, aaptSrcJar)
1188 }
Colin Crossb7a63242015-04-16 14:09:14 -07001189
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001190 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001191 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001192 }
1193
Colin Cross1ee23172017-10-18 14:44:18 -07001194 jarName := ctx.ModuleName() + ".jar"
1195
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001196 javaSrcFiles := srcFiles.FilterByExt(".java")
1197 var uniqueSrcFiles android.Paths
1198 set := make(map[string]bool)
1199 for _, v := range javaSrcFiles {
1200 if _, found := set[v.String()]; !found {
1201 set[v.String()] = true
1202 uniqueSrcFiles = append(uniqueSrcFiles, v)
1203 }
1204 }
1205
patricktu242faad2019-09-24 15:41:30 +08001206 // Collect .java files for AIDEGen
1207 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1208
Colin Cross55f63ea2018-08-27 12:37:09 -07001209 var kotlinJars android.Paths
1210
Colin Cross93e85952017-08-15 13:34:18 -07001211 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001212 // user defined kotlin flags.
1213 kotlincFlags := j.properties.Kotlincflags
1214 CheckKotlincFlags(ctx, kotlincFlags)
1215
Colin Cross93e85952017-08-15 13:34:18 -07001216 // If there are kotlin files, compile them first but pass all the kotlin and java files
1217 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1218 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001219 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001220 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001221 kotlincFlags = append(kotlincFlags, "-no-jdk")
1222 }
1223 if len(kotlincFlags) > 0 {
1224 // optimization.
1225 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1226 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001227 }
1228
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001229 var kotlinSrcFiles android.Paths
1230 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1231 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1232
patricktu242faad2019-09-24 15:41:30 +08001233 // Collect .kt files for AIDEGen
1234 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1235
Colin Crossafbb1732019-01-17 15:42:52 -08001236 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1237 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1238
1239 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1240 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1241
1242 if len(flags.processorPath) > 0 {
1243 // Use kapt for annotation processing
1244 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1245 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1246 srcJars = append(srcJars, kaptSrcJar)
1247 // Disable annotation processing in javac, it's already been handled by kapt
1248 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001249 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001250 }
Colin Cross93e85952017-08-15 13:34:18 -07001251
Colin Cross1ee23172017-10-18 14:44:18 -07001252 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001253 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001254 if ctx.Failed() {
1255 return
1256 }
1257
1258 // Make javac rule depend on the kotlinc rule
1259 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001260
Colin Cross93e85952017-08-15 13:34:18 -07001261 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001262 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001263 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001264 }
1265
Colin Cross55f63ea2018-08-27 12:37:09 -07001266 jars := append(android.Paths(nil), kotlinJars...)
1267
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001268 // Store the list of .java files that was passed to javac
1269 j.compiledJavaSrcs = uniqueSrcFiles
1270 j.compiledSrcJars = srcJars
1271
Nan Zhang61eaedb2017-11-02 13:28:15 -07001272 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001273 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001274 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1275 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001276 // Formerly, there was a check here that prevented annotation processors
1277 // from being used when sharding was enabled, as some annotation processors
1278 // do not function correctly in sharded environments. It was removed to
1279 // allow for the use of annotation processors that do function correctly
1280 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001281 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001282 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001283 if ctx.Failed() {
1284 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001285 }
1286 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001287 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001288 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001289 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001290 // If error-prone is enabled, add an additional rule to compile the java files into
1291 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001292 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001293 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1294 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001295 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001296 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001297 extraJarDeps = append(extraJarDeps, errorprone)
1298 }
1299
Nan Zhang61eaedb2017-11-02 13:28:15 -07001300 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001301 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001302 shardSize := int(*(j.properties.Javac_shard_size))
1303 var shardSrcs []android.Paths
1304 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001305 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001306 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001307 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1308 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001309 jars = append(jars, classes)
1310 }
1311 }
1312 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001313 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1314 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001315 jars = append(jars, classes)
1316 }
1317 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001318 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001319 jars = append(jars, classes)
1320 }
Colin Crossd6891432017-09-27 17:39:56 -07001321 if ctx.Failed() {
1322 return
1323 }
Colin Cross2fe66872015-03-30 17:20:39 -07001324 }
1325
Colin Cross0c4ce212019-05-03 15:28:19 -07001326 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1327
1328 var includeSrcJar android.WritablePath
1329 if Bool(j.properties.Include_srcs) {
1330 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1331 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1332 }
1333
Colin Crosscedd4762018-09-13 11:26:19 -07001334 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1335 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001336 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001337 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001338
1339 var resArgs []string
1340 var resDeps android.Paths
1341
1342 resArgs = append(resArgs, dirArgs...)
1343 resDeps = append(resDeps, dirDeps...)
1344
1345 resArgs = append(resArgs, fileArgs...)
1346 resDeps = append(resDeps, fileDeps...)
1347
Colin Cross988708c2019-05-06 14:04:11 -07001348 resArgs = append(resArgs, extraArgs...)
1349 resDeps = append(resDeps, extraDeps...)
1350
Colin Cross40a36712017-09-27 17:41:35 -07001351 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001352 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001353 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001354 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001355 if ctx.Failed() {
1356 return
1357 }
1358 }
1359
Colin Cross0c4ce212019-05-03 15:28:19 -07001360 var resourceJars android.Paths
1361 if j.resourceJar != nil {
1362 resourceJars = append(resourceJars, j.resourceJar)
1363 }
1364 if Bool(j.properties.Include_srcs) {
1365 resourceJars = append(resourceJars, includeSrcJar)
1366 }
1367 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001368
Colin Cross0c4ce212019-05-03 15:28:19 -07001369 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001370 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001371 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001372 false, nil, nil)
1373 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001374 } else if len(resourceJars) == 1 {
1375 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001376 }
1377
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001378 if len(deps.staticJars) > 0 {
1379 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001380 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001381
Colin Cross094054a2018-10-17 15:10:48 -07001382 manifest := j.overrideManifest
1383 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001384 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001385 }
Colin Cross635acc92017-09-12 22:50:46 -07001386
Colin Cross8a497952019-03-05 22:25:09 -08001387 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001388 if len(services) > 0 {
1389 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1390 var zipargs []string
1391 for _, file := range services {
1392 serviceFile := file.String()
1393 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1394 }
1395 ctx.Build(pctx, android.BuildParams{
1396 Rule: zip,
1397 Output: servicesJar,
1398 Implicits: services,
1399 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001400 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001401 },
1402 })
1403 jars = append(jars, servicesJar)
1404 }
1405
Colin Cross0a6e0072017-08-30 14:24:55 -07001406 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001407 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001408 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001409
1410 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001411 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1412 // Optimization: skip the combine step if there is nothing to do
1413 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1414 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1415 // any if len(jars) == 1.
1416 outputFile = moduleOutPath
1417 } else {
1418 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1419 ctx.Build(pctx, android.BuildParams{
1420 Rule: android.Cp,
1421 Input: jars[0],
1422 Output: combinedJar,
1423 })
1424 outputFile = combinedJar
1425 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001426 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001427 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001428 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001429 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001430 outputFile = combinedJar
1431 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001432
Colin Cross331a1212018-08-15 20:40:52 -07001433 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001434 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001435 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001436 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001437 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001438 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001439
1440 // jarjar resource jar if necessary
1441 if j.resourceJar != nil {
1442 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001443 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001444 j.resourceJar = resourceJarJarFile
1445 }
1446
Colin Cross0a6e0072017-08-30 14:24:55 -07001447 if ctx.Failed() {
1448 return
1449 }
1450 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001451
1452 // Check package restrictions if necessary.
1453 if len(j.properties.Permitted_packages) > 0 {
1454 // Check packages and copy to package-checked file.
1455 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1456 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1457 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1458
1459 if ctx.Failed() {
1460 return
1461 }
1462 }
1463
Nan Zhanged19fc32017-10-19 13:06:22 -07001464 j.implementationJarFile = outputFile
1465 if j.headerJarFile == nil {
1466 j.headerJarFile = j.implementationJarFile
1467 }
Colin Cross2fe66872015-03-30 17:20:39 -07001468
Colin Cross3144dfc2018-01-03 15:06:47 -08001469 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001470 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1471 }
1472
Colin Cross331a1212018-08-15 20:40:52 -07001473 // merge implementation jar with resources if necessary
1474 implementationAndResourcesJar := outputFile
1475 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001476 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001477 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001478 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001479 false, nil, nil)
1480 implementationAndResourcesJar = combinedJar
1481 }
1482
1483 j.implementationAndResourcesJar = implementationAndResourcesJar
1484
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001485 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001486 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001487 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001488 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001489 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001490 if ctx.Failed() {
1491 return
1492 }
Colin Cross331a1212018-08-15 20:40:52 -07001493
Jiyong Park09cb6292019-07-15 15:29:23 +09001494 // Hidden API CSV generation and dex encoding
1495 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1496 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001497
Colin Cross331a1212018-08-15 20:40:52 -07001498 // merge dex jar with resources if necessary
1499 if j.resourceJar != nil {
1500 jars := android.Paths{dexOutputFile, j.resourceJar}
1501 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1502 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1503 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001504 if j.deviceProperties.UncompressDex {
1505 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1506 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1507 dexOutputFile = combinedAlignedJar
1508 } else {
1509 dexOutputFile = combinedJar
1510 }
Colin Cross331a1212018-08-15 20:40:52 -07001511 }
1512
1513 j.dexJarFile = dexOutputFile
1514
Colin Cross8faf8fc2019-01-16 15:15:52 -08001515 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001516 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1517
1518 j.maybeStrippedDexJarFile = dexOutputFile
1519
Colin Cross3063b782018-08-15 11:19:12 -07001520 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001521
1522 if ctx.Failed() {
1523 return
1524 }
Colin Cross331a1212018-08-15 20:40:52 -07001525 } else {
1526 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001527 }
Colin Cross331a1212018-08-15 20:40:52 -07001528
Colin Crossb7a63242015-04-16 14:09:14 -07001529 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001530
1531 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1532 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001533}
1534
Colin Cross3b706fd2019-09-05 16:44:18 -07001535func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1536 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1537
1538 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1539 if idx >= 0 {
1540 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1541 jarName += strconv.Itoa(idx)
1542 }
1543
1544 classes := android.PathForModuleOut(ctx, "javac", jarName)
1545 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1546
1547 if ctx.Config().EmitXrefRules() {
1548 extractionFile := android.PathForModuleOut(ctx, kzipName)
1549 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1550 j.kytheFiles = append(j.kytheFiles, extractionFile)
1551 }
1552
1553 return classes
1554}
1555
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001556// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1557// since some of these flags may be used internally.
1558func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1559 for _, flag := range flags {
1560 flag = strings.TrimSpace(flag)
1561
1562 if !strings.HasPrefix(flag, "-") {
1563 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1564 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1565 ctx.PropertyErrorf("kotlincflags",
1566 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1567 } else if inList(flag, config.KotlincIllegalFlags) {
1568 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1569 } else if flag == "-include-runtime" {
1570 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1571 } else {
1572 args := strings.Split(flag, " ")
1573 if args[0] == "-kotlin-home" {
1574 ctx.PropertyErrorf("kotlincflags",
1575 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1576 }
1577 }
1578 }
1579}
1580
Colin Cross8eadbf02017-10-24 17:46:00 -07001581func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001582 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001583
1584 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001585 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001586 // Compile java sources into turbine.jar.
1587 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1588 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1589 if ctx.Failed() {
1590 return nil
1591 }
1592 jars = append(jars, turbineJar)
1593 }
1594
Colin Cross55f63ea2018-08-27 12:37:09 -07001595 jars = append(jars, extraJars...)
1596
Nan Zhanged19fc32017-10-19 13:06:22 -07001597 // Combine any static header libraries into classes-header.jar. If there is only
1598 // one input jar this step will be skipped.
1599 var headerJar android.Path
1600 jars = append(jars, deps.staticHeaderJars...)
1601
Colin Cross5c6ecc12017-10-23 18:12:27 -07001602 // we cannot skip the combine step for now if there is only one jar
1603 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1604 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001605 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001606 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001607 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001608
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001609 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001610 // Transform classes.jar into classes-jarjar.jar
1611 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001612 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001613 headerJar = jarjarFile
1614 if ctx.Failed() {
1615 return nil
1616 }
1617 }
1618
1619 return headerJar
1620}
1621
Colin Crosscb933592017-11-22 13:49:43 -08001622func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001623 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001624
Colin Cross7a3139e2017-12-19 13:57:50 -08001625 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001626
Colin Cross84c38822018-01-03 15:59:46 -08001627 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001628 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1629
1630 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1631
1632 j.jacocoReportClassesFile = jacocoReportClassesFile
1633
1634 return instrumentedJar
1635}
1636
albaltai36ff7dc2018-12-25 14:35:23 +08001637var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001638
Nan Zhanged19fc32017-10-19 13:06:22 -07001639func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001640 if j.headerJarFile == nil {
1641 return nil
1642 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001643 return android.Paths{j.headerJarFile}
1644}
1645
1646func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001647 if j.implementationJarFile == nil {
1648 return nil
1649 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001650 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001651}
1652
Colin Crossf24a22a2019-01-31 14:12:44 -08001653func (j *Module) DexJar() android.Path {
1654 return j.dexJarFile
1655}
1656
Colin Cross331a1212018-08-15 20:40:52 -07001657func (j *Module) ResourceJars() android.Paths {
1658 if j.resourceJar == nil {
1659 return nil
1660 }
1661 return android.Paths{j.resourceJar}
1662}
1663
1664func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001665 if j.implementationAndResourcesJar == nil {
1666 return nil
1667 }
Colin Cross331a1212018-08-15 20:40:52 -07001668 return android.Paths{j.implementationAndResourcesJar}
1669}
1670
Colin Cross46c9b8b2017-06-22 16:51:17 -07001671func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001672 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001673 return j.exportAidlIncludeDirs
1674}
1675
Jiyong Park1be96912018-05-28 18:02:19 +09001676func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001677 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001678 return j.exportedSdkLibs
1679}
1680
Artur Satayev9cf46692019-11-26 18:08:34 +00001681func (j *Module) ExportedPlugins() (android.Paths, []string) {
1682 return j.exportedPluginJars, j.exportedPluginClasses
1683}
1684
Colin Cross0c4ce212019-05-03 15:28:19 -07001685func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1686 return j.srcJarArgs, j.srcJarDeps
1687}
1688
Colin Cross46c9b8b2017-06-22 16:51:17 -07001689var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001690
Colin Cross46c9b8b2017-06-22 16:51:17 -07001691func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001692 return j.logtagsSrcs
1693}
1694
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001695// Collect information for opening IDE project files in java/jdeps.go.
1696func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1697 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1698 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001699 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001700 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001701 if j.expandJarjarRules != nil {
1702 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001703 }
1704}
1705
1706func (j *Module) CompilerDeps() []string {
1707 jdeps := []string{}
1708 jdeps = append(jdeps, j.properties.Libs...)
1709 jdeps = append(jdeps, j.properties.Static_libs...)
1710 return jdeps
1711}
1712
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001713func (j *Module) hasCode(ctx android.ModuleContext) bool {
1714 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1715 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1716}
1717
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001718func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1719 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001720 // Dependencies other than the static linkage are all considered crossing APEX boundary
1721 // Also, a dependency to an sdk member is also considered as such. This is required because
1722 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1723 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001724}
1725
Jiyong Park0b238752019-10-29 11:23:10 +09001726func (j *Module) Stem() string {
1727 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1728}
1729
Jiyong Park618922e2020-01-08 13:35:43 +09001730func (j *Module) JacocoReportClassesFile() android.Path {
1731 return j.jacocoReportClassesFile
1732}
1733
Colin Cross2fe66872015-03-30 17:20:39 -07001734//
1735// Java libraries (.jar file)
1736//
1737
Colin Crossf506d872017-07-19 15:53:04 -07001738type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001739 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001740
1741 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001742}
1743
Colin Cross42be7612019-02-21 18:12:14 -08001744func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001745 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1746 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1747 return true
1748 }
1749
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001750 // Store uncompressed (and do not strip) dex files from boot class path jars.
1751 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1752 return true
1753 }
1754
1755 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001756 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001757 return true
1758 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001759 if ctx.Config().UncompressPrivAppDex() &&
1760 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1761 return true
1762 }
1763
Colin Cross2fc72f62018-12-21 12:59:54 -08001764 return false
1765}
1766
Colin Crossf506d872017-07-19 15:53:04 -07001767func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001768 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001769 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001770 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001771 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001772 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001773 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001774 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001775
Jiyong Park7f7766d2019-07-25 22:02:35 +09001776 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1777 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001778 var extraInstallDeps android.Paths
1779 if j.InstallMixin != nil {
1780 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1781 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001782 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001783 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001784 }
Colin Crossb7a63242015-04-16 14:09:14 -07001785}
1786
Colin Crossf506d872017-07-19 15:53:04 -07001787func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001788 j.deps(ctx)
1789}
1790
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001791const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001792 aidlIncludeDir = "aidl"
1793 javaDir = "java"
1794 jarFileSuffix = ".jar"
1795 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001796)
1797
Paul Duffina0dbf432019-12-05 11:25:53 +00001798// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001799func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1800 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1801}
1802
1803func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1804 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001805}
1806
Paul Duffin13879572019-11-28 14:31:38 +00001807type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001808 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001809}
1810
1811func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1812 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1813}
1814
1815func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1816 _, ok := module.(*Library)
1817 return ok
1818}
1819
Paul Duffina0dbf432019-12-05 11:25:53 +00001820func (mt *librarySdkMemberType) buildSnapshot(
1821 sdkModuleContext android.ModuleContext,
1822 builder android.SnapshotBuilder,
1823 member android.SdkMember,
1824 jarToExportGetter func(j *Library) android.Path) {
1825
Paul Duffin13879572019-11-28 14:31:38 +00001826 variants := member.Variants()
1827 if len(variants) != 1 {
1828 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1829 for _, variant := range variants {
1830 sdkModuleContext.ModuleErrorf(" %q", variant)
1831 }
1832 }
1833 variant := variants[0]
1834 j := variant.(*Library)
1835
Paul Duffina0dbf432019-12-05 11:25:53 +00001836 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001837 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001838 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001839
1840 for _, dir := range j.AidlIncludeDirs() {
1841 // TODO(jiyong): copy parcelable declarations only
1842 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1843 for _, file := range aidlFiles {
1844 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1845 }
1846 }
1847
Paul Duffin9d8d6092019-12-05 18:19:29 +00001848 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001849 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001850}
1851
Paul Duffina0dbf432019-12-05 11:25:53 +00001852type headerLibrarySdkMemberType struct {
1853 librarySdkMemberType
1854}
1855
1856func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1857 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1858 headerJars := j.HeaderJars()
1859 if len(headerJars) != 1 {
1860 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1861 }
1862
1863 return headerJars[0]
1864 })
1865}
1866
Paul Duffina0dbf432019-12-05 11:25:53 +00001867type implLibrarySdkMemberType struct {
1868 librarySdkMemberType
1869}
1870
1871func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1872 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1873 implementationJars := j.ImplementationJars()
1874 if len(implementationJars) != 1 {
1875 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1876 }
1877
1878 return implementationJars[0]
1879 })
1880}
1881
Colin Cross1b16b0e2019-02-12 14:41:32 -08001882// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1883//
1884// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1885// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1886// as a `static_libs` dependency of another module.
1887//
1888// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1889// a device.
1890//
1891// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1892// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001893func LibraryFactory() android.Module {
1894 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001895
Colin Cross9ae1b922018-06-26 17:59:05 -07001896 module.AddProperties(
1897 &module.Module.properties,
1898 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001899 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001900 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001901
Jiyong Park7f7766d2019-07-25 22:02:35 +09001902 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001903 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001904 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001905 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001906}
1907
Colin Cross1b16b0e2019-02-12 14:41:32 -08001908// java_library_static is an obsolete alias for java_library.
1909func LibraryStaticFactory() android.Module {
1910 return LibraryFactory()
1911}
1912
1913// java_library_host builds and links sources into a `.jar` file for the host.
1914//
1915// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1916// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001917func LibraryHostFactory() android.Module {
1918 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001919
Colin Cross6af17aa2017-09-20 12:59:05 -07001920 module.AddProperties(
1921 &module.Module.properties,
1922 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001923
Colin Cross9ae1b922018-06-26 17:59:05 -07001924 module.Module.properties.Installable = proptools.BoolPtr(true)
1925
Jiyong Park7f7766d2019-07-25 22:02:35 +09001926 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001927 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001928 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001929}
1930
1931//
Colin Crossb628ea52018-08-14 16:42:33 -07001932// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001933//
1934
1935type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001936 // list of compatibility suites (for example "cts", "vts") that the module should be
1937 // installed into.
1938 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001939
1940 // the name of the test configuration (for example "AndroidTest.xml") that should be
1941 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001942 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001943
Jack He33338892018-09-19 02:21:28 -07001944 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1945 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001946 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001947
Colin Crossd96ca352018-08-10 16:06:24 -07001948 // list of files or filegroup modules that provide data that should be installed alongside
1949 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001950 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001951
1952 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1953 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1954 // explicitly.
1955 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001956}
1957
Paul Duffin42df1442019-03-20 12:45:53 +00001958type testHelperLibraryProperties struct {
1959 // list of compatibility suites (for example "cts", "vts") that the module should be
1960 // installed into.
1961 Test_suites []string `android:"arch_variant"`
1962}
1963
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001964type prebuiltTestProperties struct {
1965 // list of compatibility suites (for example "cts", "vts") that the module should be
1966 // installed into.
1967 Test_suites []string `android:"arch_variant"`
1968
1969 // the name of the test configuration (for example "AndroidTest.xml") that should be
1970 // installed with the module.
1971 Test_config *string `android:"path,arch_variant"`
1972}
1973
Colin Cross05638fc2018-04-09 18:40:24 -07001974type Test struct {
1975 Library
1976
1977 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001978
1979 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001980 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001981}
1982
Paul Duffin42df1442019-03-20 12:45:53 +00001983type TestHelperLibrary struct {
1984 Library
1985
1986 testHelperLibraryProperties testHelperLibraryProperties
1987}
1988
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001989type JavaTestImport struct {
1990 Import
1991
1992 prebuiltTestProperties prebuiltTestProperties
1993
1994 testConfig android.Path
1995}
1996
Colin Cross303e21f2018-08-07 16:49:25 -07001997func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001998 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1999 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002000 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002001
2002 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002003}
2004
Paul Duffin42df1442019-03-20 12:45:53 +00002005func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2006 j.Library.GenerateAndroidBuildActions(ctx)
2007}
2008
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002009func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2010 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2011 j.prebuiltTestProperties.Test_suites, nil)
2012
2013 j.Import.GenerateAndroidBuildActions(ctx)
2014}
2015
2016type testSdkMemberType struct {
2017 android.SdkMemberTypeBase
2018}
2019
2020func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2021 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2022}
2023
2024func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2025 _, ok := module.(*Test)
2026 return ok
2027}
2028
2029func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2030 variants := member.Variants()
2031 if len(variants) != 1 {
2032 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2033 for _, variant := range variants {
2034 sdkModuleContext.ModuleErrorf(" %q", variant)
2035 }
2036 }
2037 variant := variants[0]
2038 j := variant.(*Test)
2039
2040 implementationJars := j.ImplementationJars()
2041 if len(implementationJars) != 1 {
2042 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2043 }
2044
2045 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2046 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2047
2048 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2049 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2050
2051 module := builder.AddPrebuiltModule(member, "java_test_import")
2052 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2053 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2054}
2055
Colin Cross1b16b0e2019-02-12 14:41:32 -08002056// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2057// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2058//
2059// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2060// compiled against the device bootclasspath.
2061//
2062// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2063// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002064func TestFactory() android.Module {
2065 module := &Test{}
2066
2067 module.AddProperties(
2068 &module.Module.properties,
2069 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002070 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002071 &module.Module.protoProperties,
2072 &module.testProperties)
2073
Colin Cross9ae1b922018-06-26 17:59:05 -07002074 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002075 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002076
Colin Cross05638fc2018-04-09 18:40:24 -07002077 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002078 return module
2079}
2080
Paul Duffin42df1442019-03-20 12:45:53 +00002081// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2082func TestHelperLibraryFactory() android.Module {
2083 module := &TestHelperLibrary{}
2084
2085 module.AddProperties(
2086 &module.Module.properties,
2087 &module.Module.deviceProperties,
2088 &module.Module.dexpreoptProperties,
2089 &module.Module.protoProperties,
2090 &module.testHelperLibraryProperties)
2091
Colin Cross9a4abed2019-04-24 13:19:28 -07002092 module.Module.properties.Installable = proptools.BoolPtr(true)
2093 module.Module.dexpreopter.isTest = true
2094
Paul Duffin42df1442019-03-20 12:45:53 +00002095 InitJavaModule(module, android.HostAndDeviceSupported)
2096 return module
2097}
2098
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002099// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2100// and makes sure that it is added to the appropriate test suite.
2101//
2102// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2103// compiled against an Android classpath.
2104//
2105// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2106// for host modules.
2107func JavaTestImportFactory() android.Module {
2108 module := &JavaTestImport{}
2109
2110 module.AddProperties(
2111 &module.Import.properties,
2112 &module.prebuiltTestProperties)
2113
2114 module.Import.properties.Installable = proptools.BoolPtr(true)
2115
2116 android.InitPrebuiltModule(module, &module.properties.Jars)
2117 android.InitApexModule(module)
2118 android.InitSdkAwareModule(module)
2119 InitJavaModule(module, android.HostAndDeviceSupported)
2120 return module
2121}
2122
Colin Cross1b16b0e2019-02-12 14:41:32 -08002123// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2124// allow running the test with `atest` or a `TEST_MAPPING` file.
2125//
2126// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2127// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002128func TestHostFactory() android.Module {
2129 module := &Test{}
2130
2131 module.AddProperties(
2132 &module.Module.properties,
2133 &module.Module.protoProperties,
2134 &module.testProperties)
2135
Colin Cross9ae1b922018-06-26 17:59:05 -07002136 module.Module.properties.Installable = proptools.BoolPtr(true)
2137
Colin Cross05638fc2018-04-09 18:40:24 -07002138 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002139 return module
2140}
2141
2142//
Colin Cross2fe66872015-03-30 17:20:39 -07002143// Java Binaries (.jar file plus wrapper script)
2144//
2145
Colin Crossf506d872017-07-19 15:53:04 -07002146type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002147 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002148 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002149
2150 // Name of the class containing main to be inserted into the manifest as Main-Class.
2151 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002152}
2153
Colin Crossf506d872017-07-19 15:53:04 -07002154type Binary struct {
2155 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002156
Colin Crossf506d872017-07-19 15:53:04 -07002157 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002158
Colin Cross6b4a32d2017-12-05 13:42:45 -08002159 isWrapperVariant bool
2160
Colin Crossc3315992017-12-08 19:12:36 -08002161 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002162 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002163}
2164
Alex Light24237172017-10-26 09:46:21 -07002165func (j *Binary) HostToolPath() android.OptionalPath {
2166 return android.OptionalPathForPath(j.binaryFile)
2167}
2168
Colin Crossf506d872017-07-19 15:53:04 -07002169func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002170 if ctx.Arch().ArchType == android.Common {
2171 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002172 if j.binaryProperties.Main_class != nil {
2173 if j.properties.Manifest != nil {
2174 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2175 }
2176 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2177 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2178 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2179 }
2180
Colin Cross6b4a32d2017-12-05 13:42:45 -08002181 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002182 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002183 // Handle the binary wrapper
2184 j.isWrapperVariant = true
2185
Colin Cross366938f2017-12-11 16:29:02 -08002186 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002187 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002188 } else {
2189 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2190 }
2191
2192 // Depend on the installed jar so that the wrapper doesn't get executed by
2193 // another build rule before the jar has been installed.
2194 jarFile := ctx.PrimaryModule().(*Binary).installFile
2195
2196 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2197 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002198 }
Colin Cross2fe66872015-03-30 17:20:39 -07002199}
2200
Colin Crossf506d872017-07-19 15:53:04 -07002201func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002202 if ctx.Arch().ArchType == android.Common {
2203 j.deps(ctx)
2204 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002205}
2206
Colin Cross1b16b0e2019-02-12 14:41:32 -08002207// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2208// as well.
2209//
2210// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2211// compiled against the device bootclasspath.
2212//
2213// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2214// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002215func BinaryFactory() android.Module {
2216 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002217
Colin Cross36242852017-06-23 15:06:31 -07002218 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002219 &module.Module.properties,
2220 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002221 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002222 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002223 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002224
Colin Cross9ae1b922018-06-26 17:59:05 -07002225 module.Module.properties.Installable = proptools.BoolPtr(true)
2226
Colin Cross6b4a32d2017-12-05 13:42:45 -08002227 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2228 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002229 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002230}
2231
Colin Cross1b16b0e2019-02-12 14:41:32 -08002232// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2233//
2234// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2235// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002236func BinaryHostFactory() android.Module {
2237 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002238
Colin Cross36242852017-06-23 15:06:31 -07002239 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002240 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002241 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002242 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002243
Colin Cross9ae1b922018-06-26 17:59:05 -07002244 module.Module.properties.Installable = proptools.BoolPtr(true)
2245
Colin Cross6b4a32d2017-12-05 13:42:45 -08002246 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2247 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002248 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002249}
2250
2251//
2252// Java prebuilts
2253//
2254
Colin Cross74d73e22017-08-02 11:05:49 -07002255type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002256 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002257
Nan Zhangea568a42017-11-08 21:20:04 -08002258 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002259
2260 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002261
2262 // List of shared java libs that this module has dependencies to
2263 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002264
2265 // List of files to remove from the jar file(s)
2266 Exclude_files []string
2267
2268 // List of directories to remove from the jar file(s)
2269 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002270
2271 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002272 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002273
2274 // set the name of the output
2275 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002276}
2277
2278type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002279 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002280 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002281 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002282 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002283 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002284
Colin Cross74d73e22017-08-02 11:05:49 -07002285 properties ImportProperties
2286
Colin Cross0a6e0072017-08-30 14:24:55 -07002287 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002288 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002289}
2290
Jiyong Park6a927c42020-01-21 02:03:43 +09002291func (j *Import) sdkVersion() sdkSpec {
2292 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002293}
2294
Jiyong Park6a927c42020-01-21 02:03:43 +09002295func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002296 return j.sdkVersion()
2297}
2298
Colin Cross74d73e22017-08-02 11:05:49 -07002299func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002300 return &j.prebuilt
2301}
2302
Colin Cross74d73e22017-08-02 11:05:49 -07002303func (j *Import) PrebuiltSrcs() []string {
2304 return j.properties.Jars
2305}
2306
2307func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002308 return j.prebuilt.Name(j.ModuleBase.Name())
2309}
2310
Jiyong Park0b238752019-10-29 11:23:10 +09002311func (j *Import) Stem() string {
2312 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2313}
2314
Jiyong Park618922e2020-01-08 13:35:43 +09002315func (a *Import) JacocoReportClassesFile() android.Path {
2316 return nil
2317}
2318
Colin Cross74d73e22017-08-02 11:05:49 -07002319func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002320 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002321}
2322
Colin Cross74d73e22017-08-02 11:05:49 -07002323func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002324 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002325
Jiyong Park0b238752019-10-29 11:23:10 +09002326 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002327 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002328 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2329 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002330 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002331 inputFile := outputFile
2332 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2333 TransformJetifier(ctx, outputFile, inputFile)
2334 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002335 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002336
2337 ctx.VisitDirectDeps(func(module android.Module) {
2338 otherName := ctx.OtherModuleName(module)
2339 tag := ctx.OtherModuleDependencyTag(module)
2340
2341 switch dep := module.(type) {
2342 case Dependency:
2343 switch tag {
2344 case libTag, staticLibTag:
2345 // sdk lib names from dependencies are re-exported
2346 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2347 }
2348 case SdkLibraryDependency:
2349 switch tag {
2350 case libTag:
2351 // names of sdk libs that are directly depended are exported
2352 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2353 }
2354 }
2355 })
2356
2357 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002358 if Bool(j.properties.Installable) {
2359 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002360 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002361 }
Colin Cross2fe66872015-03-30 17:20:39 -07002362}
2363
Colin Cross74d73e22017-08-02 11:05:49 -07002364var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002365
Nan Zhanged19fc32017-10-19 13:06:22 -07002366func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002367 if j.combinedClasspathFile == nil {
2368 return nil
2369 }
Colin Cross37f6d792018-07-12 12:28:41 -07002370 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002371}
2372
2373func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002374 if j.combinedClasspathFile == nil {
2375 return nil
2376 }
Colin Cross37f6d792018-07-12 12:28:41 -07002377 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002378}
2379
Colin Cross331a1212018-08-15 20:40:52 -07002380func (j *Import) ResourceJars() android.Paths {
2381 return nil
2382}
2383
2384func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002385 if j.combinedClasspathFile == nil {
2386 return nil
2387 }
Colin Cross331a1212018-08-15 20:40:52 -07002388 return android.Paths{j.combinedClasspathFile}
2389}
2390
Colin Crossf24a22a2019-01-31 14:12:44 -08002391func (j *Import) DexJar() android.Path {
2392 return nil
2393}
2394
Colin Cross74d73e22017-08-02 11:05:49 -07002395func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002396 return nil
2397}
2398
Jiyong Park1be96912018-05-28 18:02:19 +09002399func (j *Import) ExportedSdkLibs() []string {
2400 return j.exportedSdkLibs
2401}
2402
Artur Satayev9cf46692019-11-26 18:08:34 +00002403func (j *Import) ExportedPlugins() (android.Paths, []string) {
2404 return nil, nil
2405}
2406
Colin Cross0c4ce212019-05-03 15:28:19 -07002407func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2408 return nil, nil
2409}
2410
Jiyong Park0f80c182020-01-31 02:49:53 +09002411func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2412 depTag := ctx.OtherModuleDependencyTag(dep)
2413 // dependencies other than the static linkage are all considered crossing APEX boundary
2414 // Also, a dependency to an sdk member is also considered as such. This is required because
2415 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2416 return depTag == staticLibTag || j.IsInAnySdk()
2417}
2418
albaltai36ff7dc2018-12-25 14:35:23 +08002419// Add compile time check for interface implementation
2420var _ android.IDEInfo = (*Import)(nil)
2421var _ android.IDECustomizedModuleName = (*Import)(nil)
2422
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002423// Collect information for opening IDE project files in java/jdeps.go.
2424const (
2425 removedPrefix = "prebuilt_"
2426)
2427
2428func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2429 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2430}
2431
2432func (j *Import) IDECustomizedModuleName() string {
2433 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2434 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2435 // solution to get the Import name.
2436 name := j.Name()
2437 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002438 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002439 }
2440 return name
2441}
2442
Colin Cross74d73e22017-08-02 11:05:49 -07002443var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002444
Colin Cross1b16b0e2019-02-12 14:41:32 -08002445// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2446//
2447// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2448// compiled against an Android classpath.
2449//
2450// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2451// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002452func ImportFactory() android.Module {
2453 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002454
Colin Cross74d73e22017-08-02 11:05:49 -07002455 module.AddProperties(&module.properties)
2456
2457 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002458 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002459 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002460 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002461 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002462}
2463
Colin Cross1b16b0e2019-02-12 14:41:32 -08002464// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2465// module.
2466//
2467// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2468// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002469func ImportFactoryHost() android.Module {
2470 module := &Import{}
2471
2472 module.AddProperties(&module.properties)
2473
2474 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002475 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002476 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002477 return module
2478}
2479
Colin Cross42be7612019-02-21 18:12:14 -08002480// dex_import module
2481
2482type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002483 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002484
2485 // set the name of the output
2486 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002487}
2488
2489type DexImport struct {
2490 android.ModuleBase
2491 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002492 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002493 prebuilt android.Prebuilt
2494
2495 properties DexImportProperties
2496
2497 dexJarFile android.Path
2498 maybeStrippedDexJarFile android.Path
2499
2500 dexpreopter
2501}
2502
2503func (j *DexImport) Prebuilt() *android.Prebuilt {
2504 return &j.prebuilt
2505}
2506
2507func (j *DexImport) PrebuiltSrcs() []string {
2508 return j.properties.Jars
2509}
2510
2511func (j *DexImport) Name() string {
2512 return j.prebuilt.Name(j.ModuleBase.Name())
2513}
2514
Jiyong Park0b238752019-10-29 11:23:10 +09002515func (j *DexImport) Stem() string {
2516 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2517}
2518
Colin Cross42be7612019-02-21 18:12:14 -08002519func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2520 if len(j.properties.Jars) != 1 {
2521 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2522 }
2523
Jiyong Park0b238752019-10-29 11:23:10 +09002524 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002525 j.dexpreopter.isInstallable = true
2526 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2527
2528 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2529 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2530
2531 if j.dexpreopter.uncompressedDex {
2532 rule := android.NewRuleBuilder()
2533
2534 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2535 rule.Temporary(temporary)
2536
2537 // use zip2zip to uncompress classes*.dex files
2538 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002539 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002540 FlagWithInput("-i ", inputJar).
2541 FlagWithOutput("-o ", temporary).
2542 FlagWithArg("-0 ", "'classes*.dex'")
2543
2544 // use zipalign to align uncompressed classes*.dex files
2545 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002546 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002547 Flag("-f").
2548 Text("4").
2549 Input(temporary).
2550 Output(dexOutputFile)
2551
2552 rule.DeleteTemporaryFiles()
2553
2554 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2555 } else {
2556 ctx.Build(pctx, android.BuildParams{
2557 Rule: android.Cp,
2558 Input: inputJar,
2559 Output: dexOutputFile,
2560 })
2561 }
2562
2563 j.dexJarFile = dexOutputFile
2564
2565 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2566
2567 j.maybeStrippedDexJarFile = dexOutputFile
2568
2569 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2570 ctx.ModuleName()+".jar", dexOutputFile)
2571}
2572
2573func (j *DexImport) DexJar() android.Path {
2574 return j.dexJarFile
2575}
2576
2577// dex_import imports a `.jar` file containing classes.dex files.
2578//
2579// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2580// to the device.
2581func DexImportFactory() android.Module {
2582 module := &DexImport{}
2583
2584 module.AddProperties(&module.properties)
2585
2586 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002587 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002588 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002589 return module
2590}
2591
Colin Cross89536d42017-07-07 14:35:50 -07002592//
2593// Defaults
2594//
2595type Defaults struct {
2596 android.ModuleBase
2597 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002598 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002599}
2600
Colin Cross1b16b0e2019-02-12 14:41:32 -08002601// java_defaults provides a set of properties that can be inherited by other java or android modules.
2602//
2603// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2604// property in the defaults module that exists in the depending module will be prepended to the depending module's
2605// value for that property.
2606//
2607// Example:
2608//
2609// java_defaults {
2610// name: "example_defaults",
2611// srcs: ["common/**/*.java"],
2612// javacflags: ["-Xlint:all"],
2613// aaptflags: ["--auto-add-overlay"],
2614// }
2615//
2616// java_library {
2617// name: "example",
2618// defaults: ["example_defaults"],
2619// srcs: ["example/**/*.java"],
2620// }
2621//
2622// is functionally identical to:
2623//
2624// java_library {
2625// name: "example",
2626// srcs: [
2627// "common/**/*.java",
2628// "example/**/*.java",
2629// ],
2630// javacflags: ["-Xlint:all"],
2631// }
Colin Cross89536d42017-07-07 14:35:50 -07002632func defaultsFactory() android.Module {
2633 return DefaultsFactory()
2634}
2635
Paul Duffin47357662019-12-05 14:07:14 +00002636func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002637 module := &Defaults{}
2638
Colin Cross89536d42017-07-07 14:35:50 -07002639 module.AddProperties(
2640 &CompilerProperties{},
2641 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002642 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002643 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002644 &aaptProperties{},
2645 &androidLibraryProperties{},
2646 &appProperties{},
2647 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002648 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002649 &ImportProperties{},
2650 &AARImportProperties{},
2651 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002652 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002653 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002654 )
2655
2656 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002657 return module
2658}
Nan Zhangea568a42017-11-08 21:20:04 -08002659
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002660func kytheExtractJavaFactory() android.Singleton {
2661 return &kytheExtractJavaSingleton{}
2662}
2663
2664type kytheExtractJavaSingleton struct {
2665}
2666
2667func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2668 var xrefTargets android.Paths
2669 ctx.VisitAllModules(func(module android.Module) {
2670 if javaModule, ok := module.(xref); ok {
2671 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2672 }
2673 })
2674 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2675 if len(xrefTargets) > 0 {
2676 ctx.Build(pctx, android.BuildParams{
2677 Rule: blueprint.Phony,
2678 Output: android.PathForPhony(ctx, "xref_java"),
2679 Inputs: xrefTargets,
2680 })
2681 }
2682}
2683
Nan Zhangea568a42017-11-08 21:20:04 -08002684var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002685var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002686var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002687var inList = android.InList