blob: 4c80ba77abbd2a2ebcfd5e5b7ef34d53e04730c6 [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",
44 },
45 },
46 })
47
48 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
49 librarySdkMemberType{
50 android.SdkMemberTypeBase{
51 PropertyName: "java_libs",
52 },
53 },
54 })
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Paul Duffinf9b1da02019-12-18 19:51:55 +000057func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
58 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
59
60 ctx.RegisterModuleType("java_library", LibraryFactory)
61 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
62 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
63 ctx.RegisterModuleType("java_binary", BinaryFactory)
64 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
65 ctx.RegisterModuleType("java_test", TestFactory)
66 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
67 ctx.RegisterModuleType("java_test_host", TestHostFactory)
68 ctx.RegisterModuleType("java_import", ImportFactory)
69 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
70 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
71 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
72 ctx.RegisterModuleType("dex_import", DexImportFactory)
73
74 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
75 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
76}
77
Jeongik Cha2cc570d2019-10-29 15:44:45 +090078func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
79 if j.SocSpecific() || j.DeviceSpecific() ||
80 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
81 if sc, ok := ctx.Module().(sdkContext); ok {
82 if sc.sdkVersion() == "" {
83 ctx.PropertyErrorf("sdk_version",
84 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
85 }
86 }
87 }
88}
89
Jeongik Cha538c0d02019-07-11 15:54:27 +090090func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
91 if sc, ok := ctx.Module().(sdkContext); ok {
92 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
93 if usePlatformAPI != (sc.sdkVersion() == "") {
94 if usePlatformAPI {
95 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
96 } else {
97 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
98 }
99 }
100
101 }
102}
103
Colin Cross2fe66872015-03-30 17:20:39 -0700104// TODO:
105// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700106// Renderscript
107// Post-jar passes:
108// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700109// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700110// DroidDoc
111// Findbugs
112
Colin Cross89536d42017-07-07 14:35:50 -0700113type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700114 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
115 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700117
118 // list of source files that should not be used to build the Java module.
119 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800120 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700121
122 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700123 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700124
Colin Cross86a63ff2017-09-27 17:33:10 -0700125 // list of directories that should be excluded from java_resource_dirs
126 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700127
Colin Cross0f37af02017-09-27 17:42:05 -0700128 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800129 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700130
Colin Crosscedd4762018-09-13 11:26:19 -0700131 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800132 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700133
Colin Cross7d5136f2015-05-11 13:39:40 -0700134 // list of module-specific flags that will be used for javac compiles
135 Javacflags []string `android:"arch_variant"`
136
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200137 // list of module-specific flags that will be used for kotlinc compiles
138 Kotlincflags []string `android:"arch_variant"`
139
Colin Cross7d5136f2015-05-11 13:39:40 -0700140 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700141 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700142
143 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700144 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700145
146 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800147 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700148
Colin Cross540eff82017-06-22 17:01:52 -0700149 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800150 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700151
152 // If not blank, set the java version passed to javac as -source and -target
153 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700154
Colin Cross9ae1b922018-06-26 17:59:05 -0700155 // If set to true, allow this module to be dexed and installed on devices. Has no
156 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700157 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700158
Colin Cross0f37af02017-09-27 17:42:05 -0700159 // If set to true, include sources used to compile the module in to the final jar
160 Include_srcs *bool
161
Vladimir Marko0975ee02019-04-02 10:29:55 +0100162 // If not empty, classes are restricted to the specified packages and their sub-packages.
163 // This restriction is checked after applying jarjar rules and including static libs.
164 Permitted_packages []string
165
Colin Crossbe9cdb82019-01-21 21:37:16 -0800166 // List of modules to use as annotation processors
167 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700168
Artur Satayev9cf46692019-11-26 18:08:34 +0000169 // List of modules to export to libraries that directly depend on this library as annotation processors
170 Exported_plugins []string
171
Nan Zhang61eaedb2017-11-02 13:28:15 -0700172 // The number of Java source entries each Javac instance can process
173 Javac_shard_size *int64
174
Nan Zhang5f8cb422018-02-06 10:34:32 -0800175 // Add host jdk tools.jar to bootclasspath
176 Use_tools_jar *bool
177
Colin Cross1369cdb2017-09-29 17:58:17 -0700178 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700179 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800180 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700181
Colin Cross6cef4812019-10-17 14:23:50 -0700182 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700183 Javacflags []string
184 }
Colin Crosscb933592017-11-22 13:49:43 -0800185
Colin Cross81440082018-08-15 20:21:55 -0700186 // When compiling language level 9+ .java code in packages that are part of
187 // a system module, patch_module names the module that your sources and
188 // dependencies should be patched into. The Android runtime currently
189 // doesn't implement the JEP 261 module system so this option is only
190 // supported at compile time. It should only be needed to compile tests in
191 // packages that exist in libcore and which are inconvenient to move
192 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100193 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700194
Colin Crosscb933592017-11-22 13:49:43 -0800195 Jacoco struct {
196 // List of classes to include for instrumentation with jacoco to collect coverage
197 // information at runtime when building with coverage enabled. If unset defaults to all
198 // classes.
199 // Supports '*' as the last character of an entry in the list as a wildcard match.
200 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
201 // it matches classes in the package that have the class name as a prefix.
202 Include_filter []string
203
204 // List of classes to exclude from instrumentation with jacoco to collect coverage
205 // information at runtime when building with coverage enabled. Overrides classes selected
206 // by the include_filter property.
207 // Supports '*' as the last character of an entry in the list as a wildcard match.
208 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
209 // it matches classes in the package that have the class name as a prefix.
210 Exclude_filter []string
211 }
212
Andreas Gampef3e5b552018-01-22 21:27:21 -0800213 Errorprone struct {
214 // List of javac flags that should only be used when running errorprone.
215 Javacflags []string
216 }
217
Colin Cross0f2ee152017-12-14 15:22:43 -0800218 Proto struct {
219 // List of extra options that will be passed to the proto generator.
220 Output_params []string
221 }
222
Colin Crosscb933592017-11-22 13:49:43 -0800223 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800224
225 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800226 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700227}
228
Colin Cross89536d42017-07-07 14:35:50 -0700229type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700230 // list of module-specific flags that will be used for dex compiles
231 Dxflags []string `android:"arch_variant"`
232
Jeongik Cha538c0d02019-07-11 15:54:27 +0900233 // if not blank, set to the version of the sdk to compile against.
234 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800235 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700236
Colin Cross83bb3162018-06-25 15:48:06 -0700237 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
238 // Defaults to sdk_version if not set.
239 Min_sdk_version *string
240
Dan Willemsen419290a2018-10-31 15:28:47 -0700241 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
242 // Defaults to sdk_version if not set.
243 Target_sdk_version *string
244
Jeongik Cha356dac42019-08-19 14:09:52 +0900245 // Whether to compile against the platform APIs instead of an SDK.
246 // If true, then sdk_version must be empty. The value of this field
247 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700248 Platform_apis *bool
249
Colin Crossebe1a512017-11-14 13:12:14 -0800250 Aidl struct {
251 // Top level directories to pass to aidl tool
252 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700253
Colin Crossebe1a512017-11-14 13:12:14 -0800254 // Directories rooted at the Android.bp file to pass to aidl tool
255 Local_include_dirs []string
256
257 // directories that should be added as include directories for any aidl sources of modules
258 // that depend on this module, as well as to aidl for this module.
259 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100260
261 // whether to generate traces (for systrace) for this interface
262 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100263
264 // whether to generate Binder#GetTransaction name method.
265 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800266 }
Colin Cross92430102017-10-09 14:59:32 -0700267
268 // If true, export a copy of the module as a -hostdex module for host testing.
269 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700270
Colin Cross7f87f4f2019-04-24 13:41:45 -0700271 Target struct {
272 Hostdex struct {
273 // Additional required dependencies to add to -hostdex modules.
274 Required []string
275 }
276 }
277
David Brazdil17ef5632018-06-27 10:27:45 +0100278 // If set to true, compile dex regardless of installable. Defaults to false.
279 Compile_dex *bool
280
Colin Cross66dbc0b2017-12-28 12:23:20 -0800281 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700282 // If false, disable all optimization. Defaults to true for android_app and android_test
283 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800284 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700285 // True if the module containing this has it set by default.
286 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800287
288 // If true, optimize for size by removing unused code. Defaults to true for apps,
289 // false for libraries and tests.
290 Shrink *bool
291
292 // If true, optimize bytecode. Defaults to false.
293 Optimize *bool
294
295 // If true, obfuscate bytecode. Defaults to false.
296 Obfuscate *bool
297
298 // If true, do not use the flag files generated by aapt that automatically keep
299 // classes referenced by the app manifest. Defaults to false.
300 No_aapt_flags *bool
301
302 // Flags to pass to proguard.
303 Proguard_flags []string
304
305 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800306 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800307 }
308
Paul Duffine25c6442019-10-11 13:50:28 +0100309 // When targeting 1.9 and above, override the modules to use with --system,
310 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700311 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700312
Jiyong Park4c4c0242019-10-21 14:53:15 +0900313 // set the name of the output
314 Stem *string
315
Colin Cross5a0dcd52018-10-05 14:20:06 -0700316 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800317 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700318}
319
Sasha Smundak2057f822019-04-16 17:16:58 -0700320func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
321 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
322}
323
Colin Cross46c9b8b2017-06-22 16:51:17 -0700324// Module contains the properties and members used by all java module types
325type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700326 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700327 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900328 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900329 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700330
Colin Cross89536d42017-07-07 14:35:50 -0700331 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700332 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700333 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700334
Colin Cross331a1212018-08-15 20:40:52 -0700335 // jar file containing header classes including static library dependencies, suitable for
336 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700337 headerJarFile android.Path
338
Colin Cross331a1212018-08-15 20:40:52 -0700339 // jar file containing implementation classes including static library dependencies but no
340 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700341 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700342
Colin Cross331a1212018-08-15 20:40:52 -0700343 // jar file containing only resources including from static library dependencies
344 resourceJar android.Path
345
Colin Cross0c4ce212019-05-03 15:28:19 -0700346 // args and dependencies to package source files into a srcjar
347 srcJarArgs []string
348 srcJarDeps android.Paths
349
Colin Cross331a1212018-08-15 20:40:52 -0700350 // jar file containing implementation classes and resources including static library
351 // dependencies
352 implementationAndResourcesJar android.Path
353
354 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700355 dexJarFile android.Path
356
Colin Cross43f08db2018-11-12 10:13:39 -0800357 // output file that contains classes.dex if it should be in the output file
358 maybeStrippedDexJarFile android.Path
359
Colin Crosscb933592017-11-22 13:49:43 -0800360 // output file containing uninstrumented classes that will be instrumented by jacoco
361 jacocoReportClassesFile android.Path
362
Colin Cross66dbc0b2017-12-28 12:23:20 -0800363 // output file containing mapping of obfuscated names
364 proguardDictionary android.Path
365
Colin Cross331a1212018-08-15 20:40:52 -0700366 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700367 outputFile android.Path
368 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700369
Colin Cross635c3b02016-05-18 15:37:25 -0700370 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700371
Colin Cross635c3b02016-05-18 15:37:25 -0700372 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700373
Colin Cross2fe66872015-03-30 17:20:39 -0700374 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700375 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800376
377 // list of .java files and srcjars that was passed to javac
378 compiledJavaSrcs android.Paths
379 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800380
381 // list of extra progurad flag files
382 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900383
Colin Cross094054a2018-10-17 15:10:48 -0700384 // manifest file to use instead of properties.Manifest
385 overrideManifest android.OptionalPath
386
Artur Satayev9cf46692019-11-26 18:08:34 +0000387 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900388 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700389
Artur Satayev9cf46692019-11-26 18:08:34 +0000390 // list of plugins that this java module is exporting
391 exportedPluginJars android.Paths
392
393 // list of plugins that this java module is exporting
394 exportedPluginClasses []string
395
396 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800397 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700398 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800399
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800400 // expanded Jarjar_rules
401 expandJarjarRules android.Path
402
Vladimir Marko0975ee02019-04-02 10:29:55 +0100403 // list of additional targets for checkbuild
404 additionalCheckedModules android.Paths
405
Colin Cross988708c2019-05-06 14:04:11 -0700406 // Extra files generated by the module type to be added as java resources.
407 extraResources android.Paths
408
Colin Crossf24a22a2019-01-31 14:12:44 -0800409 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800410 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800411
412 // list of the xref extraction files
413 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700414}
415
Colin Cross41955e82019-05-29 14:40:35 -0700416func (j *Module) OutputFiles(tag string) (android.Paths, error) {
417 switch tag {
418 case "":
419 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700420 case ".jar":
421 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700422 case ".proguard_map":
423 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700424 default:
425 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
426 }
Colin Cross54250902017-12-05 09:28:08 -0800427}
428
Jiyong Park8fd61922018-11-08 02:50:25 +0900429func (j *Module) DexJarFile() android.Path {
430 return j.dexJarFile
431}
432
Colin Cross41955e82019-05-29 14:40:35 -0700433var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800434
Colin Crossf506d872017-07-19 15:53:04 -0700435type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700436 HeaderJars() android.Paths
437 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700438 ResourceJars() android.Paths
439 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800440 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700441 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900442 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000443 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700444 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700445 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700446}
447
Jiyong Parkc678ad32018-04-10 13:07:10 +0900448type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700449 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
450 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900451}
452
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800453type xref interface {
454 XrefJavaFiles() android.Paths
455}
456
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800457func (j *Module) XrefJavaFiles() android.Paths {
458 return j.kytheFiles
459}
460
Colin Cross89536d42017-07-07 14:35:50 -0700461func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
462 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
463 android.InitDefaultableModule(module)
464}
465
Colin Crossbe1da472017-07-07 15:59:46 -0700466type dependencyTag struct {
467 blueprint.BaseDependencyTag
468 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700469}
470
Colin Crossa4f08812018-10-02 22:03:40 -0700471type jniDependencyTag struct {
472 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700473}
474
Jiyong Park8be103b2019-11-08 15:53:48 +0900475func IsJniDepTag(depTag blueprint.DependencyTag) bool {
476 _, ok := depTag.(*jniDependencyTag)
477 return ok
478}
479
Colin Crossbe1da472017-07-07 15:59:46 -0700480var (
Colin Cross4b964c02018-10-15 16:18:06 -0700481 staticLibTag = dependencyTag{name: "staticlib"}
482 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700483 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800484 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000485 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700486 bootClasspathTag = dependencyTag{name: "bootclasspath"}
487 systemModulesTag = dependencyTag{name: "system modules"}
488 frameworkResTag = dependencyTag{name: "framework-res"}
489 frameworkApkTag = dependencyTag{name: "framework-apk"}
490 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800491 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700492 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
493 certificateTag = dependencyTag{name: "certificate"}
494 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700495 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700496)
Colin Cross2fe66872015-03-30 17:20:39 -0700497
Colin Crossfc3674a2017-09-18 17:41:52 -0700498type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700499 useModule, useFiles, useDefaultLibs, invalidVersion bool
500
Colin Cross6cef4812019-10-17 14:23:50 -0700501 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
502 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100503
504 // The default system modules to use. Will be an empty string if no system
505 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700506 systemModules string
507
Colin Cross6cef4812019-10-17 14:23:50 -0700508 // The modules that will be added ot the classpath when targeting 1.9 or higher
509 java9Classpath []string
510
Colin Crossa97c5d32018-03-28 14:58:31 -0700511 frameworkResModule string
512
Colin Cross86a60ae2018-05-29 14:44:55 -0700513 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700514 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100515
516 noStandardLibs, noFrameworksLibs bool
517}
518
519func (s sdkDep) hasStandardLibs() bool {
520 return !s.noStandardLibs
521}
522
523func (s sdkDep) hasFrameworkLibs() bool {
524 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700525}
526
Colin Crossa4f08812018-10-02 22:03:40 -0700527type jniLib struct {
528 name string
529 path android.Path
530 target android.Target
531}
532
Colin Cross0ea8ba82019-06-06 14:33:29 -0700533func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800534 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
535}
536
Colin Cross0ea8ba82019-06-06 14:33:29 -0700537func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800538 return j.shouldInstrument(ctx) &&
539 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
540 ctx.Config().UnbundledBuild())
541}
542
Colin Cross83bb3162018-06-25 15:48:06 -0700543func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900544 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700545}
546
Paul Duffine25c6442019-10-11 13:50:28 +0100547func (j *Module) systemModules() string {
548 return proptools.String(j.deviceProperties.System_modules)
549}
550
Colin Cross83bb3162018-06-25 15:48:06 -0700551func (j *Module) minSdkVersion() string {
552 if j.deviceProperties.Min_sdk_version != nil {
553 return *j.deviceProperties.Min_sdk_version
554 }
555 return j.sdkVersion()
556}
557
Dan Willemsen419290a2018-10-31 15:28:47 -0700558func (j *Module) targetSdkVersion() string {
559 if j.deviceProperties.Target_sdk_version != nil {
560 return *j.deviceProperties.Target_sdk_version
561 }
562 return j.sdkVersion()
563}
564
Jiyong Parkb02bb402019-12-03 00:43:57 +0900565func (j *Module) AvailableFor(what string) bool {
566 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
567 // Exception: for hostdex: true libraries, the platform variant is created
568 // even if it's not marked as available to platform. In that case, the platform
569 // variant is used only for the hostdex and not installed to the device.
570 return true
571 }
572 return j.ApexModuleBase.AvailableFor(what)
573}
574
Colin Crossbe1da472017-07-07 15:59:46 -0700575func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700576 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100577 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700578 if sdkDep.useDefaultLibs {
579 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
580 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
581 if sdkDep.hasFrameworkLibs() {
582 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700583 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700584 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700585 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100586 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700587 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700588 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
589 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
590 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
591 }
Colin Cross2fe66872015-03-30 17:20:39 -0700592 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700593
Nan Zhangb2b33de2018-02-23 11:18:47 -0800594 if ctx.ModuleName() == "android_stubs_current" ||
595 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700596 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700597 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800598 }
Colin Cross2fe66872015-03-30 17:20:39 -0700599 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700600
Inseob Kimac1e9862019-12-09 18:15:47 +0900601 syspropPublicStubs := syspropPublicStubs(ctx.Config())
602
603 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
604 // and redirects dependency to public stub depending on the link type.
605 rewriteSyspropLibs := func(libs []string, prop string) []string {
606 // make a copy
607 ret := android.CopyOf(libs)
608
609 for idx, lib := range libs {
610 stub, ok := syspropPublicStubs[lib]
611
612 if !ok {
613 continue
614 }
615
616 linkType, _ := j.getLinkType(ctx.ModuleName())
617 if linkType == javaSystem {
618 ret[idx] = stub
619 } else if linkType != javaPlatform {
620 ctx.PropertyErrorf("sdk_version",
621 "can't link against sysprop_library %q from a module using public or core API",
622 lib)
623 }
624 }
625
626 return ret
627 }
628
629 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
630 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700631
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700632 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000633 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800634
Colin Crossfe17f6f2019-03-28 19:30:56 -0700635 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700636 if j.hasSrcExt(".proto") {
637 protoDeps(ctx, &j.protoProperties)
638 }
Colin Cross93e85952017-08-15 13:34:18 -0700639
640 if j.hasSrcExt(".kt") {
641 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
642 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700643 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
644 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800645 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800646 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
647 }
Colin Cross93e85952017-08-15 13:34:18 -0700648 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800649
650 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700651 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800652 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700653}
654
655func hasSrcExt(srcs []string, ext string) bool {
656 for _, src := range srcs {
657 if filepath.Ext(src) == ext {
658 return true
659 }
660 }
661
662 return false
663}
664
665func (j *Module) hasSrcExt(ext string) bool {
666 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700667}
668
Colin Cross46c9b8b2017-06-22 16:51:17 -0700669func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700670 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700671
Colin Crossebe1a512017-11-14 13:12:14 -0800672 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
673 aidlIncludes = append(aidlIncludes,
674 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
675 aidlIncludes = append(aidlIncludes,
676 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700677
Colin Cross3047fa22019-04-18 10:56:44 -0700678 var flags []string
679 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700680
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700681 if aidlPreprocess.Valid() {
682 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700683 deps = append(deps, aidlPreprocess.Path())
684 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700685 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700686 }
687
Colin Cross3047fa22019-04-18 10:56:44 -0700688 if len(j.exportAidlIncludeDirs) > 0 {
689 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
690 }
691
692 if len(aidlIncludes) > 0 {
693 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
694 }
695
Colin Cross635c3b02016-05-18 15:37:25 -0700696 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800697 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700698 flags = append(flags, "-I"+src.String())
699 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700700
Martijn Coeneneab15642018-03-09 09:29:59 +0100701 if Bool(j.deviceProperties.Aidl.Generate_traces) {
702 flags = append(flags, "-t")
703 }
704
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100705 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
706 flags = append(flags, "--transaction_names")
707 }
708
Colin Cross3047fa22019-04-18 10:56:44 -0700709 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700710}
711
Colin Cross32f676a2017-09-06 13:41:06 -0700712type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800713 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700714 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800715 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700716 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800717 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700718 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700719 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700720 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700721 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800722 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700723 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700724 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700725 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700726 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800727 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800728
729 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700730}
Colin Cross2fe66872015-03-30 17:20:39 -0700731
Colin Cross54250902017-12-05 09:28:08 -0800732func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
733 for _, f := range dep.Srcs() {
734 if f.Ext() != ".jar" {
735 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
736 ctx.OtherModuleName(dep.(blueprint.Module)))
737 }
738 }
739}
740
Jiyong Park2d492942018-03-05 17:44:10 +0900741type linkType int
742
743const (
744 javaCore linkType = iota
745 javaSdk
746 javaSystem
747 javaPlatform
748)
749
Jeongik Cha75b83b02019-11-01 15:28:00 +0900750type linkTypeContext interface {
751 android.Module
752 getLinkType(name string) (ret linkType, stubs bool)
753}
754
755func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700756 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700757 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900758 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
759 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100760 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900761 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100762 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900763 return javaCore, false
764 case name == "android_system_stubs_current":
765 return javaSystem, true
766 case strings.HasPrefix(ver, "system_"):
767 return javaSystem, false
768 case name == "android_test_stubs_current":
769 return javaSystem, true
770 case strings.HasPrefix(ver, "test_"):
771 return javaPlatform, false
772 case name == "android_stubs_current":
773 return javaSdk, true
774 case ver == "current":
775 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100776 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900777 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700778 default:
779 if _, err := strconv.Atoi(ver); err != nil {
780 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
781 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900782 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900783 }
784}
785
Jeongik Cha75b83b02019-11-01 15:28:00 +0900786func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700787 if ctx.Host() {
788 return
789 }
790
Jeongik Cha75b83b02019-11-01 15:28:00 +0900791 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900792 if stubs {
793 return
794 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900795 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900796 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."
797
798 switch myLinkType {
799 case javaCore:
800 if otherLinkType != javaCore {
801 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 +0900802 ctx.OtherModuleName(to))
803 }
Jiyong Park2d492942018-03-05 17:44:10 +0900804 break
805 case javaSdk:
806 if otherLinkType != javaCore && otherLinkType != javaSdk {
807 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
808 ctx.OtherModuleName(to))
809 }
810 break
811 case javaSystem:
812 if otherLinkType == javaPlatform {
813 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
814 ctx.OtherModuleName(to))
815 }
816 break
817 case javaPlatform:
818 // no restriction on link-type
819 break
Jiyong Park750e5572018-01-31 00:20:13 +0900820 }
821}
822
Colin Cross32f676a2017-09-06 13:41:06 -0700823func (j *Module) collectDeps(ctx android.ModuleContext) deps {
824 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700825
Colin Cross300f0382018-03-06 13:11:51 -0800826 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700827 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800828 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700829 ctx.AddMissingDependencies(sdkDep.bootclasspath)
830 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800831 } else if sdkDep.useFiles {
832 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700833 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700834 deps.aidlPreprocess = sdkDep.aidl
835 } else {
836 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800837 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700838 }
839
Colin Crossd11fcda2017-10-23 17:59:01 -0700840 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700841 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700842 tag := ctx.OtherModuleDependencyTag(module)
843
Colin Crossa4f08812018-10-02 22:03:40 -0700844 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700845 // Handled by AndroidApp.collectAppDeps
846 return
847 }
848 if tag == certificateTag {
849 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700850 return
851 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900852 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900853 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900854 if to, ok := module.(linkTypeContext); ok {
855 switch tag {
856 case bootClasspathTag, libTag, staticLibTag:
857 checkLinkType(ctx, j, to, tag.(dependencyTag))
858 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700859 }
Jiyong Park750e5572018-01-31 00:20:13 +0900860 }
Colin Cross54250902017-12-05 09:28:08 -0800861 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800862 case SdkLibraryDependency:
863 switch tag {
864 case libTag:
865 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
866 // names of sdk libs that are directly depended are exported
867 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700868 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800869 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
870 }
Colin Cross54250902017-12-05 09:28:08 -0800871 case Dependency:
872 switch tag {
873 case bootClasspathTag:
874 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700875 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800876 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900877 // sdk lib names from dependencies are re-exported
878 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700879 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000880 pluginJars, pluginClasses := dep.ExportedPlugins()
881 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700882 case java9LibTag:
883 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800884 case staticLibTag:
885 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
886 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
887 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700888 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900889 // sdk lib names from dependencies are re-exported
890 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700891 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000892 pluginJars, pluginClasses := dep.ExportedPlugins()
893 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800894 case pluginTag:
895 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800896 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000897 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
898 } else {
899 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800900 }
901 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
902 } else {
903 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
904 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000905 case exportedPluginTag:
906 if plugin, ok := dep.(*Plugin); ok {
907 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
908 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
909 }
910 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
911 if plugin.pluginProperties.Processor_class != nil {
912 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
913 }
914 } else {
915 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
916 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800917 case frameworkApkTag:
918 if ctx.ModuleName() == "android_stubs_current" ||
919 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700920 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800921 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
922 // resource files out of there for aapt.
923 //
924 // Normally the package rule runs aapt, which includes the resource,
925 // but we're not running that in our package rule so just copy in the
926 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700927 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800928 }
Colin Cross54250902017-12-05 09:28:08 -0800929 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700930 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800931 case kotlinAnnotationsTag:
932 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800933 }
934
Colin Cross54250902017-12-05 09:28:08 -0800935 case android.SourceFileProducer:
936 switch tag {
937 case libTag:
938 checkProducesJars(ctx, dep)
939 deps.classpath = append(deps.classpath, dep.Srcs()...)
940 case staticLibTag:
941 checkProducesJars(ctx, dep)
942 deps.classpath = append(deps.classpath, dep.Srcs()...)
943 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
944 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800945 }
946 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700947 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100948 case bootClasspathTag:
949 // If a system modules dependency has been added to the bootclasspath
950 // then add its libs to the bootclasspath.
951 sm := module.(*SystemModules)
952 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
953
Colin Cross1369cdb2017-09-29 17:58:17 -0700954 case systemModulesTag:
955 if deps.systemModules != nil {
956 panic("Found two system module dependencies")
957 }
958 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000959 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700960 panic("Missing directory for system module dependency")
961 }
Colin Crossb77043e2019-07-16 13:57:13 -0700962 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700963 }
Colin Crossec7a0422017-07-07 14:47:12 -0700964 }
Colin Cross2fe66872015-03-30 17:20:39 -0700965 })
966
Jiyong Park1be96912018-05-28 18:02:19 +0900967 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
968
Colin Cross32f676a2017-09-06 13:41:06 -0700969 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700970}
971
Artur Satayev9cf46692019-11-26 18:08:34 +0000972func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
973 deps.processorPath = append(deps.processorPath, pluginJars...)
974 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
975}
976
Colin Cross1e743852019-10-28 11:37:20 -0700977func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800978 v := sdkContext.sdkVersion()
979 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100980 if ctx.Config().IsPdkBuild() &&
981 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700982 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800983 latestSdkVersion := 0
984 if len(sdkVersions) > 0 {
985 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
986 }
987 v = strconv.Itoa(latestSdkVersion)
988 }
989
990 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700991 if err != nil {
992 ctx.PropertyErrorf("sdk_version", "%s", err)
993 }
Nan Zhang357466b2018-04-17 17:38:36 -0700994 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700995 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700996 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -0700997 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100998 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -0700999 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001000 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1001 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001002 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001003 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001004 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001005 }
Nan Zhang357466b2018-04-17 17:38:36 -07001006}
1007
Colin Cross1e743852019-10-28 11:37:20 -07001008type javaVersion int
1009
1010const (
1011 JAVA_VERSION_UNSUPPORTED = 0
1012 JAVA_VERSION_6 = 6
1013 JAVA_VERSION_7 = 7
1014 JAVA_VERSION_8 = 8
1015 JAVA_VERSION_9 = 9
1016)
1017
1018func (v javaVersion) String() string {
1019 switch v {
1020 case JAVA_VERSION_6:
1021 return "1.6"
1022 case JAVA_VERSION_7:
1023 return "1.7"
1024 case JAVA_VERSION_8:
1025 return "1.8"
1026 case JAVA_VERSION_9:
1027 return "1.9"
1028 default:
1029 return "unsupported"
1030 }
1031}
1032
1033// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1034func (v javaVersion) usesJavaModules() bool {
1035 return v >= 9
1036}
1037
1038func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001039 switch javaVersion {
1040 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001041 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001042 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001043 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001044 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001045 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001046 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001047 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001048 case "10", "11":
1049 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001050 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001051 default:
1052 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001053 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001054 }
1055}
1056
Nan Zhanged19fc32017-10-19 13:06:22 -07001057func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001058
Colin Crossf03c82b2015-04-13 13:53:40 -07001059 var flags javaBuilderFlags
1060
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001061 // javaVersion flag.
1062 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1063
Nan Zhanged19fc32017-10-19 13:06:22 -07001064 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001065 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001066 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001067 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001068 }
Colin Cross6510f912017-11-29 00:27:14 -08001069 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001070 // Override the -g flag passed globally to remove local variable debug info to reduce
1071 // disk and memory usage.
1072 javacFlags = append(javacFlags, "-g:source,lines")
1073 }
Colin Crossc228a702019-11-06 16:18:05 -08001074 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001075
Colin Cross66548102018-06-19 22:47:35 -07001076 if ctx.Config().RunErrorProne() {
1077 if config.ErrorProneClasspath == nil {
1078 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1079 }
1080
1081 errorProneFlags := []string{
1082 "-Xplugin:ErrorProne",
1083 "${config.ErrorProneChecks}",
1084 }
1085 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1086
1087 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1088 "'" + strings.Join(errorProneFlags, " ") + "'"
1089 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001090 }
1091
Nan Zhanged19fc32017-10-19 13:06:22 -07001092 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001093 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1094 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001095 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001096 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001097
Colin Crossbe9cdb82019-01-21 21:37:16 -08001098 flags.processor = strings.Join(deps.processorClasses, ",")
1099
Colin Cross1e743852019-10-28 11:37:20 -07001100 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1101 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001102 // Give host-side tools a version of OpenJDK's standard libraries
1103 // close to what they're targeting. As of Dec 2017, AOSP is only
1104 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1105 //
1106 // When building with OpenJDK 8, the following should have no
1107 // effect since those jars would be available by default.
1108 //
1109 // When building with OpenJDK 9 but targeting a version < 1.8,
1110 // putting them on the bootclasspath means that:
1111 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1112 // b) references to existing APIs are not reinterpreted in an
1113 // OpenJDK 9-specific way, eg. calls to subclasses of
1114 // java.nio.Buffer as in http://b/70862583
1115 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1116 flags.bootClasspath = append(flags.bootClasspath,
1117 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1118 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001119 if Bool(j.properties.Use_tools_jar) {
1120 flags.bootClasspath = append(flags.bootClasspath,
1121 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1122 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001123 }
1124
Colin Cross1e743852019-10-28 11:37:20 -07001125 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001126 // Manually specify build directory in case it is not under the repo root.
1127 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1128 // just adding a symlink under the root doesn't help.)
1129 patchPaths := ".:" + ctx.Config().BuildDir()
1130 classPath := flags.classpath.FormJavaClassPath("")
1131 if classPath != "" {
1132 patchPaths += ":" + classPath
1133 }
1134 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001135 }
1136
Nan Zhanged19fc32017-10-19 13:06:22 -07001137 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001138 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001139
Nan Zhanged19fc32017-10-19 13:06:22 -07001140 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001141 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001142
Colin Cross81440082018-08-15 20:21:55 -07001143 if len(javacFlags) > 0 {
1144 // optimization.
1145 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1146 flags.javacFlags = "$javacFlags"
1147 }
1148
Nan Zhanged19fc32017-10-19 13:06:22 -07001149 return flags
1150}
Colin Crossc0b06f12015-04-08 13:03:43 -07001151
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001152func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001153 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001154
1155 deps := j.collectDeps(ctx)
1156 flags := j.collectBuilderFlags(ctx, deps)
1157
Colin Cross1e743852019-10-28 11:37:20 -07001158 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001159 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1160 }
Colin Cross8a497952019-03-05 22:25:09 -08001161 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001162 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001163 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001164 }
1165
Colin Crossaf050172017-11-15 23:01:59 -08001166 srcFiles = j.genSources(ctx, srcFiles, flags)
1167
1168 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001169 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001170 if aaptSrcJar != nil {
1171 srcJars = append(srcJars, aaptSrcJar)
1172 }
Colin Crossb7a63242015-04-16 14:09:14 -07001173
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001174 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001175 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001176 }
1177
Colin Cross1ee23172017-10-18 14:44:18 -07001178 jarName := ctx.ModuleName() + ".jar"
1179
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001180 javaSrcFiles := srcFiles.FilterByExt(".java")
1181 var uniqueSrcFiles android.Paths
1182 set := make(map[string]bool)
1183 for _, v := range javaSrcFiles {
1184 if _, found := set[v.String()]; !found {
1185 set[v.String()] = true
1186 uniqueSrcFiles = append(uniqueSrcFiles, v)
1187 }
1188 }
1189
patricktu242faad2019-09-24 15:41:30 +08001190 // Collect .java files for AIDEGen
1191 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1192
Colin Cross55f63ea2018-08-27 12:37:09 -07001193 var kotlinJars android.Paths
1194
Colin Cross93e85952017-08-15 13:34:18 -07001195 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001196 // user defined kotlin flags.
1197 kotlincFlags := j.properties.Kotlincflags
1198 CheckKotlincFlags(ctx, kotlincFlags)
1199
Colin Cross93e85952017-08-15 13:34:18 -07001200 // If there are kotlin files, compile them first but pass all the kotlin and java files
1201 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1202 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001203 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001204 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001205 kotlincFlags = append(kotlincFlags, "-no-jdk")
1206 }
1207 if len(kotlincFlags) > 0 {
1208 // optimization.
1209 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1210 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001211 }
1212
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001213 var kotlinSrcFiles android.Paths
1214 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1215 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1216
patricktu242faad2019-09-24 15:41:30 +08001217 // Collect .kt files for AIDEGen
1218 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1219
Colin Crossafbb1732019-01-17 15:42:52 -08001220 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1221 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1222
1223 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1224 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1225
1226 if len(flags.processorPath) > 0 {
1227 // Use kapt for annotation processing
1228 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1229 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1230 srcJars = append(srcJars, kaptSrcJar)
1231 // Disable annotation processing in javac, it's already been handled by kapt
1232 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001233 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001234 }
Colin Cross93e85952017-08-15 13:34:18 -07001235
Colin Cross1ee23172017-10-18 14:44:18 -07001236 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001237 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001238 if ctx.Failed() {
1239 return
1240 }
1241
1242 // Make javac rule depend on the kotlinc rule
1243 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001244
Colin Cross93e85952017-08-15 13:34:18 -07001245 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001246 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001247 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001248 }
1249
Colin Cross55f63ea2018-08-27 12:37:09 -07001250 jars := append(android.Paths(nil), kotlinJars...)
1251
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001252 // Store the list of .java files that was passed to javac
1253 j.compiledJavaSrcs = uniqueSrcFiles
1254 j.compiledSrcJars = srcJars
1255
Nan Zhang61eaedb2017-11-02 13:28:15 -07001256 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001257 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001258 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1259 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001260 // Formerly, there was a check here that prevented annotation processors
1261 // from being used when sharding was enabled, as some annotation processors
1262 // do not function correctly in sharded environments. It was removed to
1263 // allow for the use of annotation processors that do function correctly
1264 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001265 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001266 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001267 if ctx.Failed() {
1268 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001269 }
1270 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001271 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001272 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001273 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001274 // If error-prone is enabled, add an additional rule to compile the java files into
1275 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001276 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001277 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1278 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001279 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001280 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001281 extraJarDeps = append(extraJarDeps, errorprone)
1282 }
1283
Nan Zhang61eaedb2017-11-02 13:28:15 -07001284 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001285 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001286 shardSize := int(*(j.properties.Javac_shard_size))
1287 var shardSrcs []android.Paths
1288 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001289 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001290 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001291 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1292 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001293 jars = append(jars, classes)
1294 }
1295 }
1296 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001297 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1298 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001299 jars = append(jars, classes)
1300 }
1301 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001302 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001303 jars = append(jars, classes)
1304 }
Colin Crossd6891432017-09-27 17:39:56 -07001305 if ctx.Failed() {
1306 return
1307 }
Colin Cross2fe66872015-03-30 17:20:39 -07001308 }
1309
Colin Cross0c4ce212019-05-03 15:28:19 -07001310 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1311
1312 var includeSrcJar android.WritablePath
1313 if Bool(j.properties.Include_srcs) {
1314 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1315 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1316 }
1317
Colin Crosscedd4762018-09-13 11:26:19 -07001318 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1319 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001320 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001321 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001322
1323 var resArgs []string
1324 var resDeps android.Paths
1325
1326 resArgs = append(resArgs, dirArgs...)
1327 resDeps = append(resDeps, dirDeps...)
1328
1329 resArgs = append(resArgs, fileArgs...)
1330 resDeps = append(resDeps, fileDeps...)
1331
Colin Cross988708c2019-05-06 14:04:11 -07001332 resArgs = append(resArgs, extraArgs...)
1333 resDeps = append(resDeps, extraDeps...)
1334
Colin Cross40a36712017-09-27 17:41:35 -07001335 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001336 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001337 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001338 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001339 if ctx.Failed() {
1340 return
1341 }
1342 }
1343
Colin Cross0c4ce212019-05-03 15:28:19 -07001344 var resourceJars android.Paths
1345 if j.resourceJar != nil {
1346 resourceJars = append(resourceJars, j.resourceJar)
1347 }
1348 if Bool(j.properties.Include_srcs) {
1349 resourceJars = append(resourceJars, includeSrcJar)
1350 }
1351 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001352
Colin Cross0c4ce212019-05-03 15:28:19 -07001353 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001354 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001355 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001356 false, nil, nil)
1357 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001358 } else if len(resourceJars) == 1 {
1359 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001360 }
1361
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001362 if len(deps.staticJars) > 0 {
1363 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001364 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001365
Colin Cross094054a2018-10-17 15:10:48 -07001366 manifest := j.overrideManifest
1367 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001368 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001369 }
Colin Cross635acc92017-09-12 22:50:46 -07001370
Colin Cross8a497952019-03-05 22:25:09 -08001371 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001372 if len(services) > 0 {
1373 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1374 var zipargs []string
1375 for _, file := range services {
1376 serviceFile := file.String()
1377 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1378 }
1379 ctx.Build(pctx, android.BuildParams{
1380 Rule: zip,
1381 Output: servicesJar,
1382 Implicits: services,
1383 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001384 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001385 },
1386 })
1387 jars = append(jars, servicesJar)
1388 }
1389
Colin Cross0a6e0072017-08-30 14:24:55 -07001390 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001391 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001392 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001393
1394 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001395 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1396 // Optimization: skip the combine step if there is nothing to do
1397 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1398 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1399 // any if len(jars) == 1.
1400 outputFile = moduleOutPath
1401 } else {
1402 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1403 ctx.Build(pctx, android.BuildParams{
1404 Rule: android.Cp,
1405 Input: jars[0],
1406 Output: combinedJar,
1407 })
1408 outputFile = combinedJar
1409 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001410 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001411 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001412 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001413 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001414 outputFile = combinedJar
1415 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001416
Colin Cross331a1212018-08-15 20:40:52 -07001417 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001418 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001419 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001420 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001421 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001422 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001423
1424 // jarjar resource jar if necessary
1425 if j.resourceJar != nil {
1426 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001427 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001428 j.resourceJar = resourceJarJarFile
1429 }
1430
Colin Cross0a6e0072017-08-30 14:24:55 -07001431 if ctx.Failed() {
1432 return
1433 }
1434 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001435
1436 // Check package restrictions if necessary.
1437 if len(j.properties.Permitted_packages) > 0 {
1438 // Check packages and copy to package-checked file.
1439 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1440 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1441 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1442
1443 if ctx.Failed() {
1444 return
1445 }
1446 }
1447
Nan Zhanged19fc32017-10-19 13:06:22 -07001448 j.implementationJarFile = outputFile
1449 if j.headerJarFile == nil {
1450 j.headerJarFile = j.implementationJarFile
1451 }
Colin Cross2fe66872015-03-30 17:20:39 -07001452
Colin Cross6510f912017-11-29 00:27:14 -08001453 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001454 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1455 j.properties.Instrument = true
1456 }
1457 }
1458
Colin Cross3144dfc2018-01-03 15:06:47 -08001459 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001460 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1461 }
1462
Colin Cross331a1212018-08-15 20:40:52 -07001463 // merge implementation jar with resources if necessary
1464 implementationAndResourcesJar := outputFile
1465 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001466 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001467 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001468 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001469 false, nil, nil)
1470 implementationAndResourcesJar = combinedJar
1471 }
1472
1473 j.implementationAndResourcesJar = implementationAndResourcesJar
1474
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001475 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001476 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001477 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001478 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001479 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001480 if ctx.Failed() {
1481 return
1482 }
Colin Cross331a1212018-08-15 20:40:52 -07001483
Jiyong Park09cb6292019-07-15 15:29:23 +09001484 // Hidden API CSV generation and dex encoding
1485 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1486 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001487
Colin Cross331a1212018-08-15 20:40:52 -07001488 // merge dex jar with resources if necessary
1489 if j.resourceJar != nil {
1490 jars := android.Paths{dexOutputFile, j.resourceJar}
1491 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1492 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1493 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001494 if j.deviceProperties.UncompressDex {
1495 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1496 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1497 dexOutputFile = combinedAlignedJar
1498 } else {
1499 dexOutputFile = combinedJar
1500 }
Colin Cross331a1212018-08-15 20:40:52 -07001501 }
1502
1503 j.dexJarFile = dexOutputFile
1504
Colin Cross8faf8fc2019-01-16 15:15:52 -08001505 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001506 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1507
1508 j.maybeStrippedDexJarFile = dexOutputFile
1509
Colin Cross3063b782018-08-15 11:19:12 -07001510 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001511
1512 if ctx.Failed() {
1513 return
1514 }
Colin Cross331a1212018-08-15 20:40:52 -07001515 } else {
1516 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001517 }
Colin Cross331a1212018-08-15 20:40:52 -07001518
Colin Crossb7a63242015-04-16 14:09:14 -07001519 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001520
1521 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1522 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001523}
1524
Colin Cross3b706fd2019-09-05 16:44:18 -07001525func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1526 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1527
1528 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1529 if idx >= 0 {
1530 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1531 jarName += strconv.Itoa(idx)
1532 }
1533
1534 classes := android.PathForModuleOut(ctx, "javac", jarName)
1535 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1536
1537 if ctx.Config().EmitXrefRules() {
1538 extractionFile := android.PathForModuleOut(ctx, kzipName)
1539 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1540 j.kytheFiles = append(j.kytheFiles, extractionFile)
1541 }
1542
1543 return classes
1544}
1545
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001546// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1547// since some of these flags may be used internally.
1548func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1549 for _, flag := range flags {
1550 flag = strings.TrimSpace(flag)
1551
1552 if !strings.HasPrefix(flag, "-") {
1553 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1554 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1555 ctx.PropertyErrorf("kotlincflags",
1556 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1557 } else if inList(flag, config.KotlincIllegalFlags) {
1558 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1559 } else if flag == "-include-runtime" {
1560 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1561 } else {
1562 args := strings.Split(flag, " ")
1563 if args[0] == "-kotlin-home" {
1564 ctx.PropertyErrorf("kotlincflags",
1565 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1566 }
1567 }
1568 }
1569}
1570
Colin Cross8eadbf02017-10-24 17:46:00 -07001571func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001572 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001573
1574 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001575 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001576 // Compile java sources into turbine.jar.
1577 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1578 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1579 if ctx.Failed() {
1580 return nil
1581 }
1582 jars = append(jars, turbineJar)
1583 }
1584
Colin Cross55f63ea2018-08-27 12:37:09 -07001585 jars = append(jars, extraJars...)
1586
Nan Zhanged19fc32017-10-19 13:06:22 -07001587 // Combine any static header libraries into classes-header.jar. If there is only
1588 // one input jar this step will be skipped.
1589 var headerJar android.Path
1590 jars = append(jars, deps.staticHeaderJars...)
1591
Colin Cross5c6ecc12017-10-23 18:12:27 -07001592 // we cannot skip the combine step for now if there is only one jar
1593 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1594 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001595 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001596 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001597 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001598
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001599 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001600 // Transform classes.jar into classes-jarjar.jar
1601 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001602 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001603 headerJar = jarjarFile
1604 if ctx.Failed() {
1605 return nil
1606 }
1607 }
1608
1609 return headerJar
1610}
1611
Colin Crosscb933592017-11-22 13:49:43 -08001612func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001613 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001614
Colin Cross7a3139e2017-12-19 13:57:50 -08001615 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001616
Colin Cross84c38822018-01-03 15:59:46 -08001617 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001618 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1619
1620 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1621
1622 j.jacocoReportClassesFile = jacocoReportClassesFile
1623
1624 return instrumentedJar
1625}
1626
albaltai36ff7dc2018-12-25 14:35:23 +08001627var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001628
Nan Zhanged19fc32017-10-19 13:06:22 -07001629func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001630 if j.headerJarFile == nil {
1631 return nil
1632 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001633 return android.Paths{j.headerJarFile}
1634}
1635
1636func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001637 if j.implementationJarFile == nil {
1638 return nil
1639 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001640 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001641}
1642
Colin Crossf24a22a2019-01-31 14:12:44 -08001643func (j *Module) DexJar() android.Path {
1644 return j.dexJarFile
1645}
1646
Colin Cross331a1212018-08-15 20:40:52 -07001647func (j *Module) ResourceJars() android.Paths {
1648 if j.resourceJar == nil {
1649 return nil
1650 }
1651 return android.Paths{j.resourceJar}
1652}
1653
1654func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001655 if j.implementationAndResourcesJar == nil {
1656 return nil
1657 }
Colin Cross331a1212018-08-15 20:40:52 -07001658 return android.Paths{j.implementationAndResourcesJar}
1659}
1660
Colin Cross46c9b8b2017-06-22 16:51:17 -07001661func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001662 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001663 return j.exportAidlIncludeDirs
1664}
1665
Jiyong Park1be96912018-05-28 18:02:19 +09001666func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001667 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001668 return j.exportedSdkLibs
1669}
1670
Artur Satayev9cf46692019-11-26 18:08:34 +00001671func (j *Module) ExportedPlugins() (android.Paths, []string) {
1672 return j.exportedPluginJars, j.exportedPluginClasses
1673}
1674
Colin Cross0c4ce212019-05-03 15:28:19 -07001675func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1676 return j.srcJarArgs, j.srcJarDeps
1677}
1678
Colin Cross46c9b8b2017-06-22 16:51:17 -07001679var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001680
Colin Cross46c9b8b2017-06-22 16:51:17 -07001681func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001682 return j.logtagsSrcs
1683}
1684
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001685// Collect information for opening IDE project files in java/jdeps.go.
1686func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1687 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1688 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001689 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001690 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001691 if j.expandJarjarRules != nil {
1692 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001693 }
1694}
1695
1696func (j *Module) CompilerDeps() []string {
1697 jdeps := []string{}
1698 jdeps = append(jdeps, j.properties.Libs...)
1699 jdeps = append(jdeps, j.properties.Static_libs...)
1700 return jdeps
1701}
1702
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001703func (j *Module) hasCode(ctx android.ModuleContext) bool {
1704 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1705 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1706}
1707
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001708func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1709 depTag := ctx.OtherModuleDependencyTag(dep)
1710 // dependencies other than the static linkage are all considered crossing APEX boundary
1711 return depTag == staticLibTag
1712}
1713
Jiyong Park0b238752019-10-29 11:23:10 +09001714func (j *Module) Stem() string {
1715 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1716}
1717
Colin Cross2fe66872015-03-30 17:20:39 -07001718//
1719// Java libraries (.jar file)
1720//
1721
Colin Crossf506d872017-07-19 15:53:04 -07001722type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001723 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001724
1725 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001726}
1727
Colin Cross42be7612019-02-21 18:12:14 -08001728func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001729 // Store uncompressed (and do not strip) dex files from boot class path jars.
1730 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1731 return true
1732 }
1733
1734 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001735 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001736 return true
1737 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001738 if ctx.Config().UncompressPrivAppDex() &&
1739 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1740 return true
1741 }
1742
Colin Cross2fc72f62018-12-21 12:59:54 -08001743 return false
1744}
1745
Colin Crossf506d872017-07-19 15:53:04 -07001746func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001747 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001748 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001749 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001750 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001751 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001752 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001753 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001754
Jiyong Park7f7766d2019-07-25 22:02:35 +09001755 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1756 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001757 var extraInstallDeps android.Paths
1758 if j.InstallMixin != nil {
1759 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1760 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001761 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001762 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001763 }
Colin Crossb7a63242015-04-16 14:09:14 -07001764}
1765
Colin Crossf506d872017-07-19 15:53:04 -07001766func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001767 j.deps(ctx)
1768}
1769
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001770const (
Paul Duffina0dbf432019-12-05 11:25:53 +00001771 aidlIncludeDir = "aidl"
1772 javaDir = "java"
1773 jarFileSuffix = ".jar"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001774)
1775
Paul Duffina0dbf432019-12-05 11:25:53 +00001776// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
1777func (j *Library) sdkSnapshotFilePathForJar() string {
1778 return filepath.Join(javaDir, j.Name()+jarFileSuffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001779}
1780
Paul Duffin13879572019-11-28 14:31:38 +00001781type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001782 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001783}
1784
1785func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1786 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1787}
1788
1789func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1790 _, ok := module.(*Library)
1791 return ok
1792}
1793
Paul Duffina0dbf432019-12-05 11:25:53 +00001794func (mt *librarySdkMemberType) buildSnapshot(
1795 sdkModuleContext android.ModuleContext,
1796 builder android.SnapshotBuilder,
1797 member android.SdkMember,
1798 jarToExportGetter func(j *Library) android.Path) {
1799
Paul Duffin13879572019-11-28 14:31:38 +00001800 variants := member.Variants()
1801 if len(variants) != 1 {
1802 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1803 for _, variant := range variants {
1804 sdkModuleContext.ModuleErrorf(" %q", variant)
1805 }
1806 }
1807 variant := variants[0]
1808 j := variant.(*Library)
1809
Paul Duffina0dbf432019-12-05 11:25:53 +00001810 exportedJar := jarToExportGetter(j)
1811 snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
1812 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001813
1814 for _, dir := range j.AidlIncludeDirs() {
1815 // TODO(jiyong): copy parcelable declarations only
1816 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1817 for _, file := range aidlFiles {
1818 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1819 }
1820 }
1821
Paul Duffin9d8d6092019-12-05 18:19:29 +00001822 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001823 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001824}
1825
Paul Duffina0dbf432019-12-05 11:25:53 +00001826type headerLibrarySdkMemberType struct {
1827 librarySdkMemberType
1828}
1829
1830func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1831 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1832 headerJars := j.HeaderJars()
1833 if len(headerJars) != 1 {
1834 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1835 }
1836
1837 return headerJars[0]
1838 })
1839}
1840
Paul Duffina0dbf432019-12-05 11:25:53 +00001841type implLibrarySdkMemberType struct {
1842 librarySdkMemberType
1843}
1844
1845func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1846 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1847 implementationJars := j.ImplementationJars()
1848 if len(implementationJars) != 1 {
1849 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1850 }
1851
1852 return implementationJars[0]
1853 })
1854}
1855
Colin Cross1b16b0e2019-02-12 14:41:32 -08001856// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1857//
1858// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1859// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1860// as a `static_libs` dependency of another module.
1861//
1862// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1863// a device.
1864//
1865// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1866// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001867func LibraryFactory() android.Module {
1868 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001869
Colin Cross9ae1b922018-06-26 17:59:05 -07001870 module.AddProperties(
1871 &module.Module.properties,
1872 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001873 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001874 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001875
Jiyong Park7f7766d2019-07-25 22:02:35 +09001876 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001877 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001878 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001879 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001880}
1881
Colin Cross1b16b0e2019-02-12 14:41:32 -08001882// java_library_static is an obsolete alias for java_library.
1883func LibraryStaticFactory() android.Module {
1884 return LibraryFactory()
1885}
1886
1887// java_library_host builds and links sources into a `.jar` file for the host.
1888//
1889// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1890// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001891func LibraryHostFactory() android.Module {
1892 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001893
Colin Cross6af17aa2017-09-20 12:59:05 -07001894 module.AddProperties(
1895 &module.Module.properties,
1896 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001897
Colin Cross9ae1b922018-06-26 17:59:05 -07001898 module.Module.properties.Installable = proptools.BoolPtr(true)
1899
Jiyong Park7f7766d2019-07-25 22:02:35 +09001900 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001901 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001902 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001903}
1904
1905//
Colin Crossb628ea52018-08-14 16:42:33 -07001906// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001907//
1908
1909type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001910 // list of compatibility suites (for example "cts", "vts") that the module should be
1911 // installed into.
1912 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001913
1914 // the name of the test configuration (for example "AndroidTest.xml") that should be
1915 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001916 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001917
Jack He33338892018-09-19 02:21:28 -07001918 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1919 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001920 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001921
Colin Crossd96ca352018-08-10 16:06:24 -07001922 // list of files or filegroup modules that provide data that should be installed alongside
1923 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001924 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001925
1926 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1927 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1928 // explicitly.
1929 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001930}
1931
Paul Duffin42df1442019-03-20 12:45:53 +00001932type testHelperLibraryProperties struct {
1933 // list of compatibility suites (for example "cts", "vts") that the module should be
1934 // installed into.
1935 Test_suites []string `android:"arch_variant"`
1936}
1937
Colin Cross05638fc2018-04-09 18:40:24 -07001938type Test struct {
1939 Library
1940
1941 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001942
1943 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001944 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001945}
1946
Paul Duffin42df1442019-03-20 12:45:53 +00001947type TestHelperLibrary struct {
1948 Library
1949
1950 testHelperLibraryProperties testHelperLibraryProperties
1951}
1952
Colin Cross303e21f2018-08-07 16:49:25 -07001953func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001954 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1955 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001956 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001957
1958 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001959}
1960
Paul Duffin42df1442019-03-20 12:45:53 +00001961func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1962 j.Library.GenerateAndroidBuildActions(ctx)
1963}
1964
Colin Cross1b16b0e2019-02-12 14:41:32 -08001965// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1966// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1967//
1968// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1969// compiled against the device bootclasspath.
1970//
1971// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1972// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001973func TestFactory() android.Module {
1974 module := &Test{}
1975
1976 module.AddProperties(
1977 &module.Module.properties,
1978 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001979 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001980 &module.Module.protoProperties,
1981 &module.testProperties)
1982
Colin Cross9ae1b922018-06-26 17:59:05 -07001983 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001984 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001985
Colin Cross05638fc2018-04-09 18:40:24 -07001986 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001987 return module
1988}
1989
Paul Duffin42df1442019-03-20 12:45:53 +00001990// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1991func TestHelperLibraryFactory() android.Module {
1992 module := &TestHelperLibrary{}
1993
1994 module.AddProperties(
1995 &module.Module.properties,
1996 &module.Module.deviceProperties,
1997 &module.Module.dexpreoptProperties,
1998 &module.Module.protoProperties,
1999 &module.testHelperLibraryProperties)
2000
Colin Cross9a4abed2019-04-24 13:19:28 -07002001 module.Module.properties.Installable = proptools.BoolPtr(true)
2002 module.Module.dexpreopter.isTest = true
2003
Paul Duffin42df1442019-03-20 12:45:53 +00002004 InitJavaModule(module, android.HostAndDeviceSupported)
2005 return module
2006}
2007
Colin Cross1b16b0e2019-02-12 14:41:32 -08002008// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2009// allow running the test with `atest` or a `TEST_MAPPING` file.
2010//
2011// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2012// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002013func TestHostFactory() android.Module {
2014 module := &Test{}
2015
2016 module.AddProperties(
2017 &module.Module.properties,
2018 &module.Module.protoProperties,
2019 &module.testProperties)
2020
Colin Cross9ae1b922018-06-26 17:59:05 -07002021 module.Module.properties.Installable = proptools.BoolPtr(true)
2022
Colin Cross05638fc2018-04-09 18:40:24 -07002023 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002024 return module
2025}
2026
2027//
Colin Cross2fe66872015-03-30 17:20:39 -07002028// Java Binaries (.jar file plus wrapper script)
2029//
2030
Colin Crossf506d872017-07-19 15:53:04 -07002031type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002032 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002033 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002034
2035 // Name of the class containing main to be inserted into the manifest as Main-Class.
2036 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002037}
2038
Colin Crossf506d872017-07-19 15:53:04 -07002039type Binary struct {
2040 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002041
Colin Crossf506d872017-07-19 15:53:04 -07002042 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002043
Colin Cross6b4a32d2017-12-05 13:42:45 -08002044 isWrapperVariant bool
2045
Colin Crossc3315992017-12-08 19:12:36 -08002046 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002047 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002048}
2049
Alex Light24237172017-10-26 09:46:21 -07002050func (j *Binary) HostToolPath() android.OptionalPath {
2051 return android.OptionalPathForPath(j.binaryFile)
2052}
2053
Colin Crossf506d872017-07-19 15:53:04 -07002054func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002055 if ctx.Arch().ArchType == android.Common {
2056 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002057 if j.binaryProperties.Main_class != nil {
2058 if j.properties.Manifest != nil {
2059 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2060 }
2061 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2062 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2063 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2064 }
2065
Colin Cross6b4a32d2017-12-05 13:42:45 -08002066 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002067 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002068 // Handle the binary wrapper
2069 j.isWrapperVariant = true
2070
Colin Cross366938f2017-12-11 16:29:02 -08002071 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002072 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002073 } else {
2074 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2075 }
2076
2077 // Depend on the installed jar so that the wrapper doesn't get executed by
2078 // another build rule before the jar has been installed.
2079 jarFile := ctx.PrimaryModule().(*Binary).installFile
2080
2081 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2082 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002083 }
Colin Cross2fe66872015-03-30 17:20:39 -07002084}
2085
Colin Crossf506d872017-07-19 15:53:04 -07002086func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002087 if ctx.Arch().ArchType == android.Common {
2088 j.deps(ctx)
2089 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002090}
2091
Colin Cross1b16b0e2019-02-12 14:41:32 -08002092// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2093// as well.
2094//
2095// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2096// compiled against the device bootclasspath.
2097//
2098// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2099// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002100func BinaryFactory() android.Module {
2101 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002102
Colin Cross36242852017-06-23 15:06:31 -07002103 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002104 &module.Module.properties,
2105 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002106 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002107 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002108 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002109
Colin Cross9ae1b922018-06-26 17:59:05 -07002110 module.Module.properties.Installable = proptools.BoolPtr(true)
2111
Colin Cross6b4a32d2017-12-05 13:42:45 -08002112 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2113 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002114 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002115}
2116
Colin Cross1b16b0e2019-02-12 14:41:32 -08002117// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2118//
2119// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2120// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002121func BinaryHostFactory() android.Module {
2122 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002123
Colin Cross36242852017-06-23 15:06:31 -07002124 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002125 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002126 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002127 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002128
Colin Cross9ae1b922018-06-26 17:59:05 -07002129 module.Module.properties.Installable = proptools.BoolPtr(true)
2130
Colin Cross6b4a32d2017-12-05 13:42:45 -08002131 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2132 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002133 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002134}
2135
2136//
2137// Java prebuilts
2138//
2139
Colin Cross74d73e22017-08-02 11:05:49 -07002140type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002141 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002142
Nan Zhangea568a42017-11-08 21:20:04 -08002143 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002144
2145 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002146
2147 // List of shared java libs that this module has dependencies to
2148 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002149
2150 // List of files to remove from the jar file(s)
2151 Exclude_files []string
2152
2153 // List of directories to remove from the jar file(s)
2154 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002155
2156 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002157 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002158
2159 // set the name of the output
2160 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002161}
2162
2163type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002164 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002165 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002166 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002167 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002168 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002169
Colin Cross74d73e22017-08-02 11:05:49 -07002170 properties ImportProperties
2171
Colin Cross0a6e0072017-08-30 14:24:55 -07002172 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002173 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002174}
2175
Colin Cross83bb3162018-06-25 15:48:06 -07002176func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002177 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002178}
2179
2180func (j *Import) minSdkVersion() string {
2181 return j.sdkVersion()
2182}
2183
Colin Cross74d73e22017-08-02 11:05:49 -07002184func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002185 return &j.prebuilt
2186}
2187
Colin Cross74d73e22017-08-02 11:05:49 -07002188func (j *Import) PrebuiltSrcs() []string {
2189 return j.properties.Jars
2190}
2191
2192func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002193 return j.prebuilt.Name(j.ModuleBase.Name())
2194}
2195
Jiyong Park0b238752019-10-29 11:23:10 +09002196func (j *Import) Stem() string {
2197 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2198}
2199
Colin Cross74d73e22017-08-02 11:05:49 -07002200func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002201 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002202}
2203
Colin Cross74d73e22017-08-02 11:05:49 -07002204func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002205 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002206
Jiyong Park0b238752019-10-29 11:23:10 +09002207 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002208 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002209 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2210 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002211 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002212 inputFile := outputFile
2213 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2214 TransformJetifier(ctx, outputFile, inputFile)
2215 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002216 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002217
2218 ctx.VisitDirectDeps(func(module android.Module) {
2219 otherName := ctx.OtherModuleName(module)
2220 tag := ctx.OtherModuleDependencyTag(module)
2221
2222 switch dep := module.(type) {
2223 case Dependency:
2224 switch tag {
2225 case libTag, staticLibTag:
2226 // sdk lib names from dependencies are re-exported
2227 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2228 }
2229 case SdkLibraryDependency:
2230 switch tag {
2231 case libTag:
2232 // names of sdk libs that are directly depended are exported
2233 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2234 }
2235 }
2236 })
2237
2238 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002239 if Bool(j.properties.Installable) {
2240 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002241 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002242 }
Colin Cross2fe66872015-03-30 17:20:39 -07002243}
2244
Colin Cross74d73e22017-08-02 11:05:49 -07002245var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002246
Nan Zhanged19fc32017-10-19 13:06:22 -07002247func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002248 if j.combinedClasspathFile == nil {
2249 return nil
2250 }
Colin Cross37f6d792018-07-12 12:28:41 -07002251 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002252}
2253
2254func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002255 if j.combinedClasspathFile == nil {
2256 return nil
2257 }
Colin Cross37f6d792018-07-12 12:28:41 -07002258 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002259}
2260
Colin Cross331a1212018-08-15 20:40:52 -07002261func (j *Import) ResourceJars() android.Paths {
2262 return nil
2263}
2264
2265func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002266 if j.combinedClasspathFile == nil {
2267 return nil
2268 }
Colin Cross331a1212018-08-15 20:40:52 -07002269 return android.Paths{j.combinedClasspathFile}
2270}
2271
Colin Crossf24a22a2019-01-31 14:12:44 -08002272func (j *Import) DexJar() android.Path {
2273 return nil
2274}
2275
Colin Cross74d73e22017-08-02 11:05:49 -07002276func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002277 return nil
2278}
2279
Jiyong Park1be96912018-05-28 18:02:19 +09002280func (j *Import) ExportedSdkLibs() []string {
2281 return j.exportedSdkLibs
2282}
2283
Artur Satayev9cf46692019-11-26 18:08:34 +00002284func (j *Import) ExportedPlugins() (android.Paths, []string) {
2285 return nil, nil
2286}
2287
Colin Cross0c4ce212019-05-03 15:28:19 -07002288func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2289 return nil, nil
2290}
2291
albaltai36ff7dc2018-12-25 14:35:23 +08002292// Add compile time check for interface implementation
2293var _ android.IDEInfo = (*Import)(nil)
2294var _ android.IDECustomizedModuleName = (*Import)(nil)
2295
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002296// Collect information for opening IDE project files in java/jdeps.go.
2297const (
2298 removedPrefix = "prebuilt_"
2299)
2300
2301func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2302 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2303}
2304
2305func (j *Import) IDECustomizedModuleName() string {
2306 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2307 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2308 // solution to get the Import name.
2309 name := j.Name()
2310 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002311 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002312 }
2313 return name
2314}
2315
Colin Cross74d73e22017-08-02 11:05:49 -07002316var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002317
Colin Cross1b16b0e2019-02-12 14:41:32 -08002318// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2319//
2320// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2321// compiled against an Android classpath.
2322//
2323// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2324// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002325func ImportFactory() android.Module {
2326 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002327
Colin Cross74d73e22017-08-02 11:05:49 -07002328 module.AddProperties(&module.properties)
2329
2330 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002331 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002332 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002333 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002334 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002335}
2336
Colin Cross1b16b0e2019-02-12 14:41:32 -08002337// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2338// module.
2339//
2340// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2341// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002342func ImportFactoryHost() android.Module {
2343 module := &Import{}
2344
2345 module.AddProperties(&module.properties)
2346
2347 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002348 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002349 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002350 return module
2351}
2352
Colin Cross42be7612019-02-21 18:12:14 -08002353// dex_import module
2354
2355type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002356 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002357
2358 // set the name of the output
2359 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002360}
2361
2362type DexImport struct {
2363 android.ModuleBase
2364 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002365 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002366 prebuilt android.Prebuilt
2367
2368 properties DexImportProperties
2369
2370 dexJarFile android.Path
2371 maybeStrippedDexJarFile android.Path
2372
2373 dexpreopter
2374}
2375
2376func (j *DexImport) Prebuilt() *android.Prebuilt {
2377 return &j.prebuilt
2378}
2379
2380func (j *DexImport) PrebuiltSrcs() []string {
2381 return j.properties.Jars
2382}
2383
2384func (j *DexImport) Name() string {
2385 return j.prebuilt.Name(j.ModuleBase.Name())
2386}
2387
Jiyong Park0b238752019-10-29 11:23:10 +09002388func (j *DexImport) Stem() string {
2389 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2390}
2391
Colin Cross42be7612019-02-21 18:12:14 -08002392func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2393 if len(j.properties.Jars) != 1 {
2394 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2395 }
2396
Jiyong Park0b238752019-10-29 11:23:10 +09002397 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002398 j.dexpreopter.isInstallable = true
2399 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2400
2401 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2402 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2403
2404 if j.dexpreopter.uncompressedDex {
2405 rule := android.NewRuleBuilder()
2406
2407 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2408 rule.Temporary(temporary)
2409
2410 // use zip2zip to uncompress classes*.dex files
2411 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002412 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002413 FlagWithInput("-i ", inputJar).
2414 FlagWithOutput("-o ", temporary).
2415 FlagWithArg("-0 ", "'classes*.dex'")
2416
2417 // use zipalign to align uncompressed classes*.dex files
2418 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002419 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002420 Flag("-f").
2421 Text("4").
2422 Input(temporary).
2423 Output(dexOutputFile)
2424
2425 rule.DeleteTemporaryFiles()
2426
2427 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2428 } else {
2429 ctx.Build(pctx, android.BuildParams{
2430 Rule: android.Cp,
2431 Input: inputJar,
2432 Output: dexOutputFile,
2433 })
2434 }
2435
2436 j.dexJarFile = dexOutputFile
2437
2438 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2439
2440 j.maybeStrippedDexJarFile = dexOutputFile
2441
2442 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2443 ctx.ModuleName()+".jar", dexOutputFile)
2444}
2445
2446func (j *DexImport) DexJar() android.Path {
2447 return j.dexJarFile
2448}
2449
2450// dex_import imports a `.jar` file containing classes.dex files.
2451//
2452// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2453// to the device.
2454func DexImportFactory() android.Module {
2455 module := &DexImport{}
2456
2457 module.AddProperties(&module.properties)
2458
2459 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002460 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002461 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002462 return module
2463}
2464
Colin Cross89536d42017-07-07 14:35:50 -07002465//
2466// Defaults
2467//
2468type Defaults struct {
2469 android.ModuleBase
2470 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002471 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002472}
2473
Colin Cross1b16b0e2019-02-12 14:41:32 -08002474// java_defaults provides a set of properties that can be inherited by other java or android modules.
2475//
2476// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2477// property in the defaults module that exists in the depending module will be prepended to the depending module's
2478// value for that property.
2479//
2480// Example:
2481//
2482// java_defaults {
2483// name: "example_defaults",
2484// srcs: ["common/**/*.java"],
2485// javacflags: ["-Xlint:all"],
2486// aaptflags: ["--auto-add-overlay"],
2487// }
2488//
2489// java_library {
2490// name: "example",
2491// defaults: ["example_defaults"],
2492// srcs: ["example/**/*.java"],
2493// }
2494//
2495// is functionally identical to:
2496//
2497// java_library {
2498// name: "example",
2499// srcs: [
2500// "common/**/*.java",
2501// "example/**/*.java",
2502// ],
2503// javacflags: ["-Xlint:all"],
2504// }
Colin Cross89536d42017-07-07 14:35:50 -07002505func defaultsFactory() android.Module {
2506 return DefaultsFactory()
2507}
2508
Paul Duffin47357662019-12-05 14:07:14 +00002509func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002510 module := &Defaults{}
2511
Colin Cross89536d42017-07-07 14:35:50 -07002512 module.AddProperties(
2513 &CompilerProperties{},
2514 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002515 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002516 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002517 &aaptProperties{},
2518 &androidLibraryProperties{},
2519 &appProperties{},
2520 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002521 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002522 &ImportProperties{},
2523 &AARImportProperties{},
2524 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002525 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002526 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002527 )
2528
2529 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002530 return module
2531}
Nan Zhangea568a42017-11-08 21:20:04 -08002532
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002533func kytheExtractJavaFactory() android.Singleton {
2534 return &kytheExtractJavaSingleton{}
2535}
2536
2537type kytheExtractJavaSingleton struct {
2538}
2539
2540func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2541 var xrefTargets android.Paths
2542 ctx.VisitAllModules(func(module android.Module) {
2543 if javaModule, ok := module.(xref); ok {
2544 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2545 }
2546 })
2547 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2548 if len(xrefTargets) > 0 {
2549 ctx.Build(pctx, android.BuildParams{
2550 Rule: blueprint.Phony,
2551 Output: android.PathForPhony(ctx, "xref_java"),
2552 Inputs: xrefTargets,
2553 })
2554 }
2555}
2556
Nan Zhangea568a42017-11-08 21:20:04 -08002557var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002558var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002559var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002560var inList = android.InList