blob: 27f69b848fd6dd657afff397139dd3a9e7edfdea [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
39 // Register sdk member types.
40 android.RegisterSdkMemberType(&headerLibrarySdkMemberType{
41 librarySdkMemberType{
42 android.SdkMemberTypeBase{
43 PropertyName: "java_header_libs",
Paul Duffine6029182019-12-16 17:43:48 +000044 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000045 },
46 },
47 })
48
49 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
50 librarySdkMemberType{
51 android.SdkMemberTypeBase{
52 PropertyName: "java_libs",
53 },
54 },
55 })
Colin Cross463a90e2015-06-17 14:20:06 -070056}
57
Paul Duffinf9b1da02019-12-18 19:51:55 +000058func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
59 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
60
61 ctx.RegisterModuleType("java_library", LibraryFactory)
62 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
63 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
64 ctx.RegisterModuleType("java_binary", BinaryFactory)
65 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
66 ctx.RegisterModuleType("java_test", TestFactory)
67 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
68 ctx.RegisterModuleType("java_test_host", TestHostFactory)
69 ctx.RegisterModuleType("java_import", ImportFactory)
70 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
71 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
72 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
73 ctx.RegisterModuleType("dex_import", DexImportFactory)
74
75 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
76 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
77}
78
Jeongik Cha2cc570d2019-10-29 15:44:45 +090079func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
80 if j.SocSpecific() || j.DeviceSpecific() ||
81 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
82 if sc, ok := ctx.Module().(sdkContext); ok {
83 if sc.sdkVersion() == "" {
84 ctx.PropertyErrorf("sdk_version",
85 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
86 }
87 }
88 }
89}
90
Jeongik Cha538c0d02019-07-11 15:54:27 +090091func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
92 if sc, ok := ctx.Module().(sdkContext); ok {
93 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
94 if usePlatformAPI != (sc.sdkVersion() == "") {
95 if usePlatformAPI {
96 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
97 } else {
98 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
99 }
100 }
101
102 }
103}
104
Colin Cross2fe66872015-03-30 17:20:39 -0700105// TODO:
106// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700107// Renderscript
108// Post-jar passes:
109// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700110// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700111// DroidDoc
112// Findbugs
113
Colin Cross89536d42017-07-07 14:35:50 -0700114type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700115 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
116 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800117 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700118
119 // list of source files that should not be used to build the Java module.
120 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800121 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700122
123 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700124 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700125
Colin Cross86a63ff2017-09-27 17:33:10 -0700126 // list of directories that should be excluded from java_resource_dirs
127 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700128
Colin Cross0f37af02017-09-27 17:42:05 -0700129 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800130 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700131
Colin Crosscedd4762018-09-13 11:26:19 -0700132 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800133 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700134
Colin Cross7d5136f2015-05-11 13:39:40 -0700135 // list of module-specific flags that will be used for javac compiles
136 Javacflags []string `android:"arch_variant"`
137
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200138 // list of module-specific flags that will be used for kotlinc compiles
139 Kotlincflags []string `android:"arch_variant"`
140
Colin Cross7d5136f2015-05-11 13:39:40 -0700141 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700142 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700143
144 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700145 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700146
147 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800148 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700149
Colin Cross540eff82017-06-22 17:01:52 -0700150 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800151 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700152
153 // If not blank, set the java version passed to javac as -source and -target
154 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700155
Colin Cross9ae1b922018-06-26 17:59:05 -0700156 // If set to true, allow this module to be dexed and installed on devices. Has no
157 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700158 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700159
Colin Cross0f37af02017-09-27 17:42:05 -0700160 // If set to true, include sources used to compile the module in to the final jar
161 Include_srcs *bool
162
Vladimir Marko0975ee02019-04-02 10:29:55 +0100163 // If not empty, classes are restricted to the specified packages and their sub-packages.
164 // This restriction is checked after applying jarjar rules and including static libs.
165 Permitted_packages []string
166
Colin Crossbe9cdb82019-01-21 21:37:16 -0800167 // List of modules to use as annotation processors
168 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700169
Artur Satayev9cf46692019-11-26 18:08:34 +0000170 // List of modules to export to libraries that directly depend on this library as annotation processors
171 Exported_plugins []string
172
Nan Zhang61eaedb2017-11-02 13:28:15 -0700173 // The number of Java source entries each Javac instance can process
174 Javac_shard_size *int64
175
Nan Zhang5f8cb422018-02-06 10:34:32 -0800176 // Add host jdk tools.jar to bootclasspath
177 Use_tools_jar *bool
178
Colin Cross1369cdb2017-09-29 17:58:17 -0700179 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700180 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800181 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700182
Colin Cross6cef4812019-10-17 14:23:50 -0700183 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700184 Javacflags []string
185 }
Colin Crosscb933592017-11-22 13:49:43 -0800186
Colin Cross81440082018-08-15 20:21:55 -0700187 // When compiling language level 9+ .java code in packages that are part of
188 // a system module, patch_module names the module that your sources and
189 // dependencies should be patched into. The Android runtime currently
190 // doesn't implement the JEP 261 module system so this option is only
191 // supported at compile time. It should only be needed to compile tests in
192 // packages that exist in libcore and which are inconvenient to move
193 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100194 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700195
Colin Crosscb933592017-11-22 13:49:43 -0800196 Jacoco struct {
197 // List of classes to include for instrumentation with jacoco to collect coverage
198 // information at runtime when building with coverage enabled. If unset defaults to all
199 // classes.
200 // Supports '*' as the last character of an entry in the list as a wildcard match.
201 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
202 // it matches classes in the package that have the class name as a prefix.
203 Include_filter []string
204
205 // List of classes to exclude from instrumentation with jacoco to collect coverage
206 // information at runtime when building with coverage enabled. Overrides classes selected
207 // by the include_filter property.
208 // Supports '*' as the last character of an entry in the list as a wildcard match.
209 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
210 // it matches classes in the package that have the class name as a prefix.
211 Exclude_filter []string
212 }
213
Andreas Gampef3e5b552018-01-22 21:27:21 -0800214 Errorprone struct {
215 // List of javac flags that should only be used when running errorprone.
216 Javacflags []string
217 }
218
Colin Cross0f2ee152017-12-14 15:22:43 -0800219 Proto struct {
220 // List of extra options that will be passed to the proto generator.
221 Output_params []string
222 }
223
Colin Crosscb933592017-11-22 13:49:43 -0800224 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800225
226 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800227 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700228}
229
Colin Cross89536d42017-07-07 14:35:50 -0700230type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700231 // list of module-specific flags that will be used for dex compiles
232 Dxflags []string `android:"arch_variant"`
233
Jeongik Cha538c0d02019-07-11 15:54:27 +0900234 // if not blank, set to the version of the sdk to compile against.
235 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800236 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700237
Colin Cross83bb3162018-06-25 15:48:06 -0700238 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
239 // Defaults to sdk_version if not set.
240 Min_sdk_version *string
241
Dan Willemsen419290a2018-10-31 15:28:47 -0700242 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
243 // Defaults to sdk_version if not set.
244 Target_sdk_version *string
245
Jeongik Cha356dac42019-08-19 14:09:52 +0900246 // Whether to compile against the platform APIs instead of an SDK.
247 // If true, then sdk_version must be empty. The value of this field
248 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700249 Platform_apis *bool
250
Colin Crossebe1a512017-11-14 13:12:14 -0800251 Aidl struct {
252 // Top level directories to pass to aidl tool
253 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700254
Colin Crossebe1a512017-11-14 13:12:14 -0800255 // Directories rooted at the Android.bp file to pass to aidl tool
256 Local_include_dirs []string
257
258 // directories that should be added as include directories for any aidl sources of modules
259 // that depend on this module, as well as to aidl for this module.
260 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100261
262 // whether to generate traces (for systrace) for this interface
263 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100264
265 // whether to generate Binder#GetTransaction name method.
266 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800267 }
Colin Cross92430102017-10-09 14:59:32 -0700268
269 // If true, export a copy of the module as a -hostdex module for host testing.
270 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700271
Colin Cross7f87f4f2019-04-24 13:41:45 -0700272 Target struct {
273 Hostdex struct {
274 // Additional required dependencies to add to -hostdex modules.
275 Required []string
276 }
277 }
278
David Brazdil17ef5632018-06-27 10:27:45 +0100279 // If set to true, compile dex regardless of installable. Defaults to false.
280 Compile_dex *bool
281
Colin Cross66dbc0b2017-12-28 12:23:20 -0800282 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700283 // If false, disable all optimization. Defaults to true for android_app and android_test
284 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800285 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700286 // True if the module containing this has it set by default.
287 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800288
289 // If true, optimize for size by removing unused code. Defaults to true for apps,
290 // false for libraries and tests.
291 Shrink *bool
292
293 // If true, optimize bytecode. Defaults to false.
294 Optimize *bool
295
296 // If true, obfuscate bytecode. Defaults to false.
297 Obfuscate *bool
298
299 // If true, do not use the flag files generated by aapt that automatically keep
300 // classes referenced by the app manifest. Defaults to false.
301 No_aapt_flags *bool
302
303 // Flags to pass to proguard.
304 Proguard_flags []string
305
306 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800307 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800308 }
309
Paul Duffine25c6442019-10-11 13:50:28 +0100310 // When targeting 1.9 and above, override the modules to use with --system,
311 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700312 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700313
Jiyong Park4c4c0242019-10-21 14:53:15 +0900314 // set the name of the output
315 Stem *string
316
Colin Cross5a0dcd52018-10-05 14:20:06 -0700317 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800318 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700319}
320
Sasha Smundak2057f822019-04-16 17:16:58 -0700321func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
322 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
323}
324
Colin Cross46c9b8b2017-06-22 16:51:17 -0700325// Module contains the properties and members used by all java module types
326type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700327 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700328 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900329 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900330 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700331
Colin Cross89536d42017-07-07 14:35:50 -0700332 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700333 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700334 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700335
Colin Cross331a1212018-08-15 20:40:52 -0700336 // jar file containing header classes including static library dependencies, suitable for
337 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700338 headerJarFile android.Path
339
Colin Cross331a1212018-08-15 20:40:52 -0700340 // jar file containing implementation classes including static library dependencies but no
341 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700342 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700343
Colin Cross331a1212018-08-15 20:40:52 -0700344 // jar file containing only resources including from static library dependencies
345 resourceJar android.Path
346
Colin Cross0c4ce212019-05-03 15:28:19 -0700347 // args and dependencies to package source files into a srcjar
348 srcJarArgs []string
349 srcJarDeps android.Paths
350
Colin Cross331a1212018-08-15 20:40:52 -0700351 // jar file containing implementation classes and resources including static library
352 // dependencies
353 implementationAndResourcesJar android.Path
354
355 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700356 dexJarFile android.Path
357
Colin Cross43f08db2018-11-12 10:13:39 -0800358 // output file that contains classes.dex if it should be in the output file
359 maybeStrippedDexJarFile android.Path
360
Colin Crosscb933592017-11-22 13:49:43 -0800361 // output file containing uninstrumented classes that will be instrumented by jacoco
362 jacocoReportClassesFile android.Path
363
Colin Cross66dbc0b2017-12-28 12:23:20 -0800364 // output file containing mapping of obfuscated names
365 proguardDictionary android.Path
366
Colin Cross331a1212018-08-15 20:40:52 -0700367 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700368 outputFile android.Path
369 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700370
Colin Cross635c3b02016-05-18 15:37:25 -0700371 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700372
Colin Cross635c3b02016-05-18 15:37:25 -0700373 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700374
Colin Cross2fe66872015-03-30 17:20:39 -0700375 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700376 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800377
378 // list of .java files and srcjars that was passed to javac
379 compiledJavaSrcs android.Paths
380 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800381
382 // list of extra progurad flag files
383 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900384
Colin Cross094054a2018-10-17 15:10:48 -0700385 // manifest file to use instead of properties.Manifest
386 overrideManifest android.OptionalPath
387
Artur Satayev9cf46692019-11-26 18:08:34 +0000388 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900389 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700390
Artur Satayev9cf46692019-11-26 18:08:34 +0000391 // list of plugins that this java module is exporting
392 exportedPluginJars android.Paths
393
394 // list of plugins that this java module is exporting
395 exportedPluginClasses []string
396
397 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800398 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700399 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800400
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800401 // expanded Jarjar_rules
402 expandJarjarRules android.Path
403
Vladimir Marko0975ee02019-04-02 10:29:55 +0100404 // list of additional targets for checkbuild
405 additionalCheckedModules android.Paths
406
Colin Cross988708c2019-05-06 14:04:11 -0700407 // Extra files generated by the module type to be added as java resources.
408 extraResources android.Paths
409
Colin Crossf24a22a2019-01-31 14:12:44 -0800410 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800411 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800412
413 // list of the xref extraction files
414 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700415}
416
Colin Cross41955e82019-05-29 14:40:35 -0700417func (j *Module) OutputFiles(tag string) (android.Paths, error) {
418 switch tag {
419 case "":
420 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700421 case ".jar":
422 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700423 case ".proguard_map":
424 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700425 default:
426 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
427 }
Colin Cross54250902017-12-05 09:28:08 -0800428}
429
Colin Cross41955e82019-05-29 14:40:35 -0700430var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800431
Colin Crossf506d872017-07-19 15:53:04 -0700432type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700433 HeaderJars() android.Paths
434 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700435 ResourceJars() android.Paths
436 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800437 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700438 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900439 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000440 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700441 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700442 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700443}
444
Jiyong Parkc678ad32018-04-10 13:07:10 +0900445type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700446 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
447 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900448}
449
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800450type xref interface {
451 XrefJavaFiles() android.Paths
452}
453
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800454func (j *Module) XrefJavaFiles() android.Paths {
455 return j.kytheFiles
456}
457
Colin Cross89536d42017-07-07 14:35:50 -0700458func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
459 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
460 android.InitDefaultableModule(module)
461}
462
Colin Crossbe1da472017-07-07 15:59:46 -0700463type dependencyTag struct {
464 blueprint.BaseDependencyTag
465 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700466}
467
Colin Crossa4f08812018-10-02 22:03:40 -0700468type jniDependencyTag struct {
469 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700470}
471
Jiyong Park8be103b2019-11-08 15:53:48 +0900472func IsJniDepTag(depTag blueprint.DependencyTag) bool {
473 _, ok := depTag.(*jniDependencyTag)
474 return ok
475}
476
Colin Crossbe1da472017-07-07 15:59:46 -0700477var (
Colin Cross4b964c02018-10-15 16:18:06 -0700478 staticLibTag = dependencyTag{name: "staticlib"}
479 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700480 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800481 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000482 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700483 bootClasspathTag = dependencyTag{name: "bootclasspath"}
484 systemModulesTag = dependencyTag{name: "system modules"}
485 frameworkResTag = dependencyTag{name: "framework-res"}
486 frameworkApkTag = dependencyTag{name: "framework-apk"}
487 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800488 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700489 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
490 certificateTag = dependencyTag{name: "certificate"}
491 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700492 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700493)
Colin Cross2fe66872015-03-30 17:20:39 -0700494
Colin Crossfc3674a2017-09-18 17:41:52 -0700495type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700496 useModule, useFiles, useDefaultLibs, invalidVersion bool
497
Colin Cross6cef4812019-10-17 14:23:50 -0700498 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
499 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100500
501 // The default system modules to use. Will be an empty string if no system
502 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700503 systemModules string
504
Colin Cross6cef4812019-10-17 14:23:50 -0700505 // The modules that will be added ot the classpath when targeting 1.9 or higher
506 java9Classpath []string
507
Colin Crossa97c5d32018-03-28 14:58:31 -0700508 frameworkResModule string
509
Colin Cross86a60ae2018-05-29 14:44:55 -0700510 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700511 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100512
513 noStandardLibs, noFrameworksLibs bool
514}
515
516func (s sdkDep) hasStandardLibs() bool {
517 return !s.noStandardLibs
518}
519
520func (s sdkDep) hasFrameworkLibs() bool {
521 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700522}
523
Colin Crossa4f08812018-10-02 22:03:40 -0700524type jniLib struct {
525 name string
526 path android.Path
527 target android.Target
528}
529
Colin Cross0ea8ba82019-06-06 14:33:29 -0700530func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800531 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
532}
533
Colin Cross0ea8ba82019-06-06 14:33:29 -0700534func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800535 return j.shouldInstrument(ctx) &&
536 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
537 ctx.Config().UnbundledBuild())
538}
539
Colin Cross83bb3162018-06-25 15:48:06 -0700540func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900541 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700542}
543
Paul Duffine25c6442019-10-11 13:50:28 +0100544func (j *Module) systemModules() string {
545 return proptools.String(j.deviceProperties.System_modules)
546}
547
Colin Cross83bb3162018-06-25 15:48:06 -0700548func (j *Module) minSdkVersion() string {
549 if j.deviceProperties.Min_sdk_version != nil {
550 return *j.deviceProperties.Min_sdk_version
551 }
552 return j.sdkVersion()
553}
554
Dan Willemsen419290a2018-10-31 15:28:47 -0700555func (j *Module) targetSdkVersion() string {
556 if j.deviceProperties.Target_sdk_version != nil {
557 return *j.deviceProperties.Target_sdk_version
558 }
559 return j.sdkVersion()
560}
561
Jiyong Parkb02bb402019-12-03 00:43:57 +0900562func (j *Module) AvailableFor(what string) bool {
563 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
564 // Exception: for hostdex: true libraries, the platform variant is created
565 // even if it's not marked as available to platform. In that case, the platform
566 // variant is used only for the hostdex and not installed to the device.
567 return true
568 }
569 return j.ApexModuleBase.AvailableFor(what)
570}
571
Colin Crossbe1da472017-07-07 15:59:46 -0700572func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700573 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100574 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700575 if sdkDep.useDefaultLibs {
576 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
577 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
578 if sdkDep.hasFrameworkLibs() {
579 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700580 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700581 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700582 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100583 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700584 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700585 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
586 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
587 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
588 }
Colin Cross2fe66872015-03-30 17:20:39 -0700589 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700590
Nan Zhangb2b33de2018-02-23 11:18:47 -0800591 if ctx.ModuleName() == "android_stubs_current" ||
592 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700593 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700594 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800595 }
Colin Cross2fe66872015-03-30 17:20:39 -0700596 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700597
Inseob Kimac1e9862019-12-09 18:15:47 +0900598 syspropPublicStubs := syspropPublicStubs(ctx.Config())
599
600 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
601 // and redirects dependency to public stub depending on the link type.
602 rewriteSyspropLibs := func(libs []string, prop string) []string {
603 // make a copy
604 ret := android.CopyOf(libs)
605
606 for idx, lib := range libs {
607 stub, ok := syspropPublicStubs[lib]
608
609 if !ok {
610 continue
611 }
612
613 linkType, _ := j.getLinkType(ctx.ModuleName())
614 if linkType == javaSystem {
615 ret[idx] = stub
616 } else if linkType != javaPlatform {
617 ctx.PropertyErrorf("sdk_version",
618 "can't link against sysprop_library %q from a module using public or core API",
619 lib)
620 }
621 }
622
623 return ret
624 }
625
626 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
627 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700628
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700629 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000630 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800631
Colin Crossfe17f6f2019-03-28 19:30:56 -0700632 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700633 if j.hasSrcExt(".proto") {
634 protoDeps(ctx, &j.protoProperties)
635 }
Colin Cross93e85952017-08-15 13:34:18 -0700636
637 if j.hasSrcExt(".kt") {
638 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
639 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700640 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
641 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800642 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800643 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
644 }
Colin Cross93e85952017-08-15 13:34:18 -0700645 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800646
647 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700648 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800649 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700650}
651
652func hasSrcExt(srcs []string, ext string) bool {
653 for _, src := range srcs {
654 if filepath.Ext(src) == ext {
655 return true
656 }
657 }
658
659 return false
660}
661
662func (j *Module) hasSrcExt(ext string) bool {
663 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700664}
665
Colin Cross46c9b8b2017-06-22 16:51:17 -0700666func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700667 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700668
Colin Crossebe1a512017-11-14 13:12:14 -0800669 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
670 aidlIncludes = append(aidlIncludes,
671 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
672 aidlIncludes = append(aidlIncludes,
673 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700674
Colin Cross3047fa22019-04-18 10:56:44 -0700675 var flags []string
676 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700677
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700678 if aidlPreprocess.Valid() {
679 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700680 deps = append(deps, aidlPreprocess.Path())
681 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700682 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700683 }
684
Colin Cross3047fa22019-04-18 10:56:44 -0700685 if len(j.exportAidlIncludeDirs) > 0 {
686 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
687 }
688
689 if len(aidlIncludes) > 0 {
690 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
691 }
692
Colin Cross635c3b02016-05-18 15:37:25 -0700693 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800694 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700695 flags = append(flags, "-I"+src.String())
696 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700697
Martijn Coeneneab15642018-03-09 09:29:59 +0100698 if Bool(j.deviceProperties.Aidl.Generate_traces) {
699 flags = append(flags, "-t")
700 }
701
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100702 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
703 flags = append(flags, "--transaction_names")
704 }
705
Colin Cross3047fa22019-04-18 10:56:44 -0700706 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700707}
708
Colin Cross32f676a2017-09-06 13:41:06 -0700709type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800710 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700711 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800712 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700713 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800714 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700715 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700716 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700717 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700718 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800719 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700720 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700721 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700722 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700723 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800724 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800725
726 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700727}
Colin Cross2fe66872015-03-30 17:20:39 -0700728
Colin Cross54250902017-12-05 09:28:08 -0800729func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
730 for _, f := range dep.Srcs() {
731 if f.Ext() != ".jar" {
732 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
733 ctx.OtherModuleName(dep.(blueprint.Module)))
734 }
735 }
736}
737
Jiyong Park2d492942018-03-05 17:44:10 +0900738type linkType int
739
740const (
741 javaCore linkType = iota
742 javaSdk
743 javaSystem
744 javaPlatform
745)
746
Jeongik Cha75b83b02019-11-01 15:28:00 +0900747type linkTypeContext interface {
748 android.Module
749 getLinkType(name string) (ret linkType, stubs bool)
750}
751
752func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700753 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700754 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900755 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
756 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100757 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900758 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100759 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900760 return javaCore, false
761 case name == "android_system_stubs_current":
762 return javaSystem, true
763 case strings.HasPrefix(ver, "system_"):
764 return javaSystem, false
765 case name == "android_test_stubs_current":
766 return javaSystem, true
767 case strings.HasPrefix(ver, "test_"):
768 return javaPlatform, false
769 case name == "android_stubs_current":
770 return javaSdk, true
771 case ver == "current":
772 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100773 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900774 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700775 default:
776 if _, err := strconv.Atoi(ver); err != nil {
777 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
778 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900779 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900780 }
781}
782
Jeongik Cha75b83b02019-11-01 15:28:00 +0900783func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700784 if ctx.Host() {
785 return
786 }
787
Jeongik Cha75b83b02019-11-01 15:28:00 +0900788 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900789 if stubs {
790 return
791 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900792 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900793 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."
794
795 switch myLinkType {
796 case javaCore:
797 if otherLinkType != javaCore {
798 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 +0900799 ctx.OtherModuleName(to))
800 }
Jiyong Park2d492942018-03-05 17:44:10 +0900801 break
802 case javaSdk:
803 if otherLinkType != javaCore && otherLinkType != javaSdk {
804 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
805 ctx.OtherModuleName(to))
806 }
807 break
808 case javaSystem:
809 if otherLinkType == javaPlatform {
810 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
811 ctx.OtherModuleName(to))
812 }
813 break
814 case javaPlatform:
815 // no restriction on link-type
816 break
Jiyong Park750e5572018-01-31 00:20:13 +0900817 }
818}
819
Colin Cross32f676a2017-09-06 13:41:06 -0700820func (j *Module) collectDeps(ctx android.ModuleContext) deps {
821 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700822
Colin Cross300f0382018-03-06 13:11:51 -0800823 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700824 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800825 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700826 ctx.AddMissingDependencies(sdkDep.bootclasspath)
827 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800828 } else if sdkDep.useFiles {
829 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700830 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700831 deps.aidlPreprocess = sdkDep.aidl
832 } else {
833 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800834 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700835 }
836
Colin Crossd11fcda2017-10-23 17:59:01 -0700837 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700838 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700839 tag := ctx.OtherModuleDependencyTag(module)
840
Colin Crossa4f08812018-10-02 22:03:40 -0700841 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700842 // Handled by AndroidApp.collectAppDeps
843 return
844 }
845 if tag == certificateTag {
846 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700847 return
848 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900849 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900850 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900851 if to, ok := module.(linkTypeContext); ok {
852 switch tag {
853 case bootClasspathTag, libTag, staticLibTag:
854 checkLinkType(ctx, j, to, tag.(dependencyTag))
855 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700856 }
Jiyong Park750e5572018-01-31 00:20:13 +0900857 }
Colin Cross54250902017-12-05 09:28:08 -0800858 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800859 case SdkLibraryDependency:
860 switch tag {
861 case libTag:
862 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
863 // names of sdk libs that are directly depended are exported
864 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700865 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800866 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
867 }
Colin Cross54250902017-12-05 09:28:08 -0800868 case Dependency:
869 switch tag {
870 case bootClasspathTag:
871 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700872 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800873 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900874 // sdk lib names from dependencies are re-exported
875 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700876 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000877 pluginJars, pluginClasses := dep.ExportedPlugins()
878 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700879 case java9LibTag:
880 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800881 case staticLibTag:
882 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
883 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
884 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700885 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900886 // sdk lib names from dependencies are re-exported
887 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700888 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000889 pluginJars, pluginClasses := dep.ExportedPlugins()
890 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800891 case pluginTag:
892 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800893 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000894 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
895 } else {
896 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800897 }
898 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
899 } else {
900 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
901 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000902 case exportedPluginTag:
903 if plugin, ok := dep.(*Plugin); ok {
904 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
905 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
906 }
907 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
908 if plugin.pluginProperties.Processor_class != nil {
909 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
910 }
911 } else {
912 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
913 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800914 case frameworkApkTag:
915 if ctx.ModuleName() == "android_stubs_current" ||
916 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700917 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800918 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
919 // resource files out of there for aapt.
920 //
921 // Normally the package rule runs aapt, which includes the resource,
922 // but we're not running that in our package rule so just copy in the
923 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700924 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800925 }
Colin Cross54250902017-12-05 09:28:08 -0800926 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700927 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800928 case kotlinAnnotationsTag:
929 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800930 }
931
Colin Cross54250902017-12-05 09:28:08 -0800932 case android.SourceFileProducer:
933 switch tag {
934 case libTag:
935 checkProducesJars(ctx, dep)
936 deps.classpath = append(deps.classpath, dep.Srcs()...)
937 case staticLibTag:
938 checkProducesJars(ctx, dep)
939 deps.classpath = append(deps.classpath, dep.Srcs()...)
940 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
941 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800942 }
943 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700944 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100945 case bootClasspathTag:
946 // If a system modules dependency has been added to the bootclasspath
947 // then add its libs to the bootclasspath.
948 sm := module.(*SystemModules)
949 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
950
Colin Cross1369cdb2017-09-29 17:58:17 -0700951 case systemModulesTag:
952 if deps.systemModules != nil {
953 panic("Found two system module dependencies")
954 }
955 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000956 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700957 panic("Missing directory for system module dependency")
958 }
Colin Crossb77043e2019-07-16 13:57:13 -0700959 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700960 }
Colin Crossec7a0422017-07-07 14:47:12 -0700961 }
Colin Cross2fe66872015-03-30 17:20:39 -0700962 })
963
Jiyong Park1be96912018-05-28 18:02:19 +0900964 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
965
Colin Cross32f676a2017-09-06 13:41:06 -0700966 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700967}
968
Artur Satayev9cf46692019-11-26 18:08:34 +0000969func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
970 deps.processorPath = append(deps.processorPath, pluginJars...)
971 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
972}
973
Colin Cross1e743852019-10-28 11:37:20 -0700974func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800975 v := sdkContext.sdkVersion()
976 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100977 if ctx.Config().IsPdkBuild() &&
978 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700979 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800980 latestSdkVersion := 0
981 if len(sdkVersions) > 0 {
982 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
983 }
984 v = strconv.Itoa(latestSdkVersion)
985 }
986
987 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700988 if err != nil {
989 ctx.PropertyErrorf("sdk_version", "%s", err)
990 }
Nan Zhang357466b2018-04-17 17:38:36 -0700991 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700992 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700993 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -0700994 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100995 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -0700996 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -0700997 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
998 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -0700999 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001000 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001001 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001002 }
Nan Zhang357466b2018-04-17 17:38:36 -07001003}
1004
Colin Cross1e743852019-10-28 11:37:20 -07001005type javaVersion int
1006
1007const (
1008 JAVA_VERSION_UNSUPPORTED = 0
1009 JAVA_VERSION_6 = 6
1010 JAVA_VERSION_7 = 7
1011 JAVA_VERSION_8 = 8
1012 JAVA_VERSION_9 = 9
1013)
1014
1015func (v javaVersion) String() string {
1016 switch v {
1017 case JAVA_VERSION_6:
1018 return "1.6"
1019 case JAVA_VERSION_7:
1020 return "1.7"
1021 case JAVA_VERSION_8:
1022 return "1.8"
1023 case JAVA_VERSION_9:
1024 return "1.9"
1025 default:
1026 return "unsupported"
1027 }
1028}
1029
1030// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1031func (v javaVersion) usesJavaModules() bool {
1032 return v >= 9
1033}
1034
1035func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001036 switch javaVersion {
1037 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001038 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001039 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001040 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001041 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001042 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001043 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001044 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001045 case "10", "11":
1046 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001047 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001048 default:
1049 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001050 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001051 }
1052}
1053
Nan Zhanged19fc32017-10-19 13:06:22 -07001054func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001055
Colin Crossf03c82b2015-04-13 13:53:40 -07001056 var flags javaBuilderFlags
1057
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001058 // javaVersion flag.
1059 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1060
Nan Zhanged19fc32017-10-19 13:06:22 -07001061 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001062 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001063 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001064 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001065 }
Colin Cross6510f912017-11-29 00:27:14 -08001066 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001067 // Override the -g flag passed globally to remove local variable debug info to reduce
1068 // disk and memory usage.
1069 javacFlags = append(javacFlags, "-g:source,lines")
1070 }
Colin Crossc228a702019-11-06 16:18:05 -08001071 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001072
Colin Cross66548102018-06-19 22:47:35 -07001073 if ctx.Config().RunErrorProne() {
1074 if config.ErrorProneClasspath == nil {
1075 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1076 }
1077
1078 errorProneFlags := []string{
1079 "-Xplugin:ErrorProne",
1080 "${config.ErrorProneChecks}",
1081 }
1082 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1083
1084 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1085 "'" + strings.Join(errorProneFlags, " ") + "'"
1086 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001087 }
1088
Nan Zhanged19fc32017-10-19 13:06:22 -07001089 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001090 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1091 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001092 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001093 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001094
Colin Crossbe9cdb82019-01-21 21:37:16 -08001095 flags.processor = strings.Join(deps.processorClasses, ",")
1096
Colin Cross1e743852019-10-28 11:37:20 -07001097 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1098 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001099 // Give host-side tools a version of OpenJDK's standard libraries
1100 // close to what they're targeting. As of Dec 2017, AOSP is only
1101 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1102 //
1103 // When building with OpenJDK 8, the following should have no
1104 // effect since those jars would be available by default.
1105 //
1106 // When building with OpenJDK 9 but targeting a version < 1.8,
1107 // putting them on the bootclasspath means that:
1108 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1109 // b) references to existing APIs are not reinterpreted in an
1110 // OpenJDK 9-specific way, eg. calls to subclasses of
1111 // java.nio.Buffer as in http://b/70862583
1112 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1113 flags.bootClasspath = append(flags.bootClasspath,
1114 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1115 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001116 if Bool(j.properties.Use_tools_jar) {
1117 flags.bootClasspath = append(flags.bootClasspath,
1118 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1119 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001120 }
1121
Colin Cross1e743852019-10-28 11:37:20 -07001122 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001123 // Manually specify build directory in case it is not under the repo root.
1124 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1125 // just adding a symlink under the root doesn't help.)
1126 patchPaths := ".:" + ctx.Config().BuildDir()
1127 classPath := flags.classpath.FormJavaClassPath("")
1128 if classPath != "" {
1129 patchPaths += ":" + classPath
1130 }
1131 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001132 }
1133
Nan Zhanged19fc32017-10-19 13:06:22 -07001134 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001135 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001136
Nan Zhanged19fc32017-10-19 13:06:22 -07001137 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001138 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001139
Colin Cross81440082018-08-15 20:21:55 -07001140 if len(javacFlags) > 0 {
1141 // optimization.
1142 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1143 flags.javacFlags = "$javacFlags"
1144 }
1145
Nan Zhanged19fc32017-10-19 13:06:22 -07001146 return flags
1147}
Colin Crossc0b06f12015-04-08 13:03:43 -07001148
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001149func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001150 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001151
1152 deps := j.collectDeps(ctx)
1153 flags := j.collectBuilderFlags(ctx, deps)
1154
Colin Cross1e743852019-10-28 11:37:20 -07001155 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001156 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1157 }
Colin Cross8a497952019-03-05 22:25:09 -08001158 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001159 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001160 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001161 }
1162
Colin Crossaf050172017-11-15 23:01:59 -08001163 srcFiles = j.genSources(ctx, srcFiles, flags)
1164
1165 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001166 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001167 if aaptSrcJar != nil {
1168 srcJars = append(srcJars, aaptSrcJar)
1169 }
Colin Crossb7a63242015-04-16 14:09:14 -07001170
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001171 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001172 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001173 }
1174
Colin Cross1ee23172017-10-18 14:44:18 -07001175 jarName := ctx.ModuleName() + ".jar"
1176
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001177 javaSrcFiles := srcFiles.FilterByExt(".java")
1178 var uniqueSrcFiles android.Paths
1179 set := make(map[string]bool)
1180 for _, v := range javaSrcFiles {
1181 if _, found := set[v.String()]; !found {
1182 set[v.String()] = true
1183 uniqueSrcFiles = append(uniqueSrcFiles, v)
1184 }
1185 }
1186
patricktu242faad2019-09-24 15:41:30 +08001187 // Collect .java files for AIDEGen
1188 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1189
Colin Cross55f63ea2018-08-27 12:37:09 -07001190 var kotlinJars android.Paths
1191
Colin Cross93e85952017-08-15 13:34:18 -07001192 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001193 // user defined kotlin flags.
1194 kotlincFlags := j.properties.Kotlincflags
1195 CheckKotlincFlags(ctx, kotlincFlags)
1196
Colin Cross93e85952017-08-15 13:34:18 -07001197 // If there are kotlin files, compile them first but pass all the kotlin and java files
1198 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1199 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001200 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001201 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001202 kotlincFlags = append(kotlincFlags, "-no-jdk")
1203 }
1204 if len(kotlincFlags) > 0 {
1205 // optimization.
1206 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1207 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001208 }
1209
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001210 var kotlinSrcFiles android.Paths
1211 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1212 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1213
patricktu242faad2019-09-24 15:41:30 +08001214 // Collect .kt files for AIDEGen
1215 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1216
Colin Crossafbb1732019-01-17 15:42:52 -08001217 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1218 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1219
1220 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1221 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1222
1223 if len(flags.processorPath) > 0 {
1224 // Use kapt for annotation processing
1225 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1226 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1227 srcJars = append(srcJars, kaptSrcJar)
1228 // Disable annotation processing in javac, it's already been handled by kapt
1229 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001230 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001231 }
Colin Cross93e85952017-08-15 13:34:18 -07001232
Colin Cross1ee23172017-10-18 14:44:18 -07001233 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001234 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001235 if ctx.Failed() {
1236 return
1237 }
1238
1239 // Make javac rule depend on the kotlinc rule
1240 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001241
Colin Cross93e85952017-08-15 13:34:18 -07001242 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001243 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001244 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001245 }
1246
Colin Cross55f63ea2018-08-27 12:37:09 -07001247 jars := append(android.Paths(nil), kotlinJars...)
1248
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001249 // Store the list of .java files that was passed to javac
1250 j.compiledJavaSrcs = uniqueSrcFiles
1251 j.compiledSrcJars = srcJars
1252
Nan Zhang61eaedb2017-11-02 13:28:15 -07001253 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001254 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001255 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1256 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001257 // Formerly, there was a check here that prevented annotation processors
1258 // from being used when sharding was enabled, as some annotation processors
1259 // do not function correctly in sharded environments. It was removed to
1260 // allow for the use of annotation processors that do function correctly
1261 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001262 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001263 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001264 if ctx.Failed() {
1265 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001266 }
1267 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001268 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001269 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001270 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001271 // If error-prone is enabled, add an additional rule to compile the java files into
1272 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001273 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001274 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1275 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001276 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001277 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001278 extraJarDeps = append(extraJarDeps, errorprone)
1279 }
1280
Nan Zhang61eaedb2017-11-02 13:28:15 -07001281 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001282 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001283 shardSize := int(*(j.properties.Javac_shard_size))
1284 var shardSrcs []android.Paths
1285 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001286 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001287 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001288 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1289 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001290 jars = append(jars, classes)
1291 }
1292 }
1293 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001294 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1295 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001296 jars = append(jars, classes)
1297 }
1298 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001299 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001300 jars = append(jars, classes)
1301 }
Colin Crossd6891432017-09-27 17:39:56 -07001302 if ctx.Failed() {
1303 return
1304 }
Colin Cross2fe66872015-03-30 17:20:39 -07001305 }
1306
Colin Cross0c4ce212019-05-03 15:28:19 -07001307 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1308
1309 var includeSrcJar android.WritablePath
1310 if Bool(j.properties.Include_srcs) {
1311 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1312 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1313 }
1314
Colin Crosscedd4762018-09-13 11:26:19 -07001315 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1316 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001317 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001318 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001319
1320 var resArgs []string
1321 var resDeps android.Paths
1322
1323 resArgs = append(resArgs, dirArgs...)
1324 resDeps = append(resDeps, dirDeps...)
1325
1326 resArgs = append(resArgs, fileArgs...)
1327 resDeps = append(resDeps, fileDeps...)
1328
Colin Cross988708c2019-05-06 14:04:11 -07001329 resArgs = append(resArgs, extraArgs...)
1330 resDeps = append(resDeps, extraDeps...)
1331
Colin Cross40a36712017-09-27 17:41:35 -07001332 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001333 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001334 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001335 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001336 if ctx.Failed() {
1337 return
1338 }
1339 }
1340
Colin Cross0c4ce212019-05-03 15:28:19 -07001341 var resourceJars android.Paths
1342 if j.resourceJar != nil {
1343 resourceJars = append(resourceJars, j.resourceJar)
1344 }
1345 if Bool(j.properties.Include_srcs) {
1346 resourceJars = append(resourceJars, includeSrcJar)
1347 }
1348 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001349
Colin Cross0c4ce212019-05-03 15:28:19 -07001350 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001351 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001352 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001353 false, nil, nil)
1354 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001355 } else if len(resourceJars) == 1 {
1356 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001357 }
1358
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001359 if len(deps.staticJars) > 0 {
1360 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001361 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001362
Colin Cross094054a2018-10-17 15:10:48 -07001363 manifest := j.overrideManifest
1364 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001365 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001366 }
Colin Cross635acc92017-09-12 22:50:46 -07001367
Colin Cross8a497952019-03-05 22:25:09 -08001368 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001369 if len(services) > 0 {
1370 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1371 var zipargs []string
1372 for _, file := range services {
1373 serviceFile := file.String()
1374 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1375 }
1376 ctx.Build(pctx, android.BuildParams{
1377 Rule: zip,
1378 Output: servicesJar,
1379 Implicits: services,
1380 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001381 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001382 },
1383 })
1384 jars = append(jars, servicesJar)
1385 }
1386
Colin Cross0a6e0072017-08-30 14:24:55 -07001387 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001388 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001389 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001390
1391 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001392 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1393 // Optimization: skip the combine step if there is nothing to do
1394 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1395 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1396 // any if len(jars) == 1.
1397 outputFile = moduleOutPath
1398 } else {
1399 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1400 ctx.Build(pctx, android.BuildParams{
1401 Rule: android.Cp,
1402 Input: jars[0],
1403 Output: combinedJar,
1404 })
1405 outputFile = combinedJar
1406 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001407 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001408 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001409 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001410 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001411 outputFile = combinedJar
1412 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001413
Colin Cross331a1212018-08-15 20:40:52 -07001414 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001415 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001416 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001417 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001418 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001419 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001420
1421 // jarjar resource jar if necessary
1422 if j.resourceJar != nil {
1423 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001424 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001425 j.resourceJar = resourceJarJarFile
1426 }
1427
Colin Cross0a6e0072017-08-30 14:24:55 -07001428 if ctx.Failed() {
1429 return
1430 }
1431 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001432
1433 // Check package restrictions if necessary.
1434 if len(j.properties.Permitted_packages) > 0 {
1435 // Check packages and copy to package-checked file.
1436 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1437 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1438 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1439
1440 if ctx.Failed() {
1441 return
1442 }
1443 }
1444
Nan Zhanged19fc32017-10-19 13:06:22 -07001445 j.implementationJarFile = outputFile
1446 if j.headerJarFile == nil {
1447 j.headerJarFile = j.implementationJarFile
1448 }
Colin Cross2fe66872015-03-30 17:20:39 -07001449
Colin Cross6510f912017-11-29 00:27:14 -08001450 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001451 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1452 j.properties.Instrument = true
1453 }
1454 }
1455
Colin Cross3144dfc2018-01-03 15:06:47 -08001456 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001457 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1458 }
1459
Colin Cross331a1212018-08-15 20:40:52 -07001460 // merge implementation jar with resources if necessary
1461 implementationAndResourcesJar := outputFile
1462 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001463 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001464 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001465 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001466 false, nil, nil)
1467 implementationAndResourcesJar = combinedJar
1468 }
1469
1470 j.implementationAndResourcesJar = implementationAndResourcesJar
1471
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001472 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001473 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001474 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001475 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001476 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001477 if ctx.Failed() {
1478 return
1479 }
Colin Cross331a1212018-08-15 20:40:52 -07001480
Jiyong Park09cb6292019-07-15 15:29:23 +09001481 // Hidden API CSV generation and dex encoding
1482 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1483 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001484
Colin Cross331a1212018-08-15 20:40:52 -07001485 // merge dex jar with resources if necessary
1486 if j.resourceJar != nil {
1487 jars := android.Paths{dexOutputFile, j.resourceJar}
1488 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1489 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1490 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001491 if j.deviceProperties.UncompressDex {
1492 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1493 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1494 dexOutputFile = combinedAlignedJar
1495 } else {
1496 dexOutputFile = combinedJar
1497 }
Colin Cross331a1212018-08-15 20:40:52 -07001498 }
1499
1500 j.dexJarFile = dexOutputFile
1501
Colin Cross8faf8fc2019-01-16 15:15:52 -08001502 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001503 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1504
1505 j.maybeStrippedDexJarFile = dexOutputFile
1506
Colin Cross3063b782018-08-15 11:19:12 -07001507 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001508
1509 if ctx.Failed() {
1510 return
1511 }
Colin Cross331a1212018-08-15 20:40:52 -07001512 } else {
1513 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001514 }
Colin Cross331a1212018-08-15 20:40:52 -07001515
Colin Crossb7a63242015-04-16 14:09:14 -07001516 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001517
1518 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1519 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001520}
1521
Colin Cross3b706fd2019-09-05 16:44:18 -07001522func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1523 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1524
1525 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1526 if idx >= 0 {
1527 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1528 jarName += strconv.Itoa(idx)
1529 }
1530
1531 classes := android.PathForModuleOut(ctx, "javac", jarName)
1532 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1533
1534 if ctx.Config().EmitXrefRules() {
1535 extractionFile := android.PathForModuleOut(ctx, kzipName)
1536 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1537 j.kytheFiles = append(j.kytheFiles, extractionFile)
1538 }
1539
1540 return classes
1541}
1542
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001543// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1544// since some of these flags may be used internally.
1545func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1546 for _, flag := range flags {
1547 flag = strings.TrimSpace(flag)
1548
1549 if !strings.HasPrefix(flag, "-") {
1550 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1551 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1552 ctx.PropertyErrorf("kotlincflags",
1553 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1554 } else if inList(flag, config.KotlincIllegalFlags) {
1555 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1556 } else if flag == "-include-runtime" {
1557 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1558 } else {
1559 args := strings.Split(flag, " ")
1560 if args[0] == "-kotlin-home" {
1561 ctx.PropertyErrorf("kotlincflags",
1562 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1563 }
1564 }
1565 }
1566}
1567
Colin Cross8eadbf02017-10-24 17:46:00 -07001568func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001569 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001570
1571 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001572 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001573 // Compile java sources into turbine.jar.
1574 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1575 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1576 if ctx.Failed() {
1577 return nil
1578 }
1579 jars = append(jars, turbineJar)
1580 }
1581
Colin Cross55f63ea2018-08-27 12:37:09 -07001582 jars = append(jars, extraJars...)
1583
Nan Zhanged19fc32017-10-19 13:06:22 -07001584 // Combine any static header libraries into classes-header.jar. If there is only
1585 // one input jar this step will be skipped.
1586 var headerJar android.Path
1587 jars = append(jars, deps.staticHeaderJars...)
1588
Colin Cross5c6ecc12017-10-23 18:12:27 -07001589 // we cannot skip the combine step for now if there is only one jar
1590 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1591 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001592 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001593 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001594 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001595
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001596 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001597 // Transform classes.jar into classes-jarjar.jar
1598 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001599 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001600 headerJar = jarjarFile
1601 if ctx.Failed() {
1602 return nil
1603 }
1604 }
1605
1606 return headerJar
1607}
1608
Colin Crosscb933592017-11-22 13:49:43 -08001609func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001610 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001611
Colin Cross7a3139e2017-12-19 13:57:50 -08001612 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001613
Colin Cross84c38822018-01-03 15:59:46 -08001614 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001615 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1616
1617 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1618
1619 j.jacocoReportClassesFile = jacocoReportClassesFile
1620
1621 return instrumentedJar
1622}
1623
albaltai36ff7dc2018-12-25 14:35:23 +08001624var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001625
Nan Zhanged19fc32017-10-19 13:06:22 -07001626func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001627 if j.headerJarFile == nil {
1628 return nil
1629 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001630 return android.Paths{j.headerJarFile}
1631}
1632
1633func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001634 if j.implementationJarFile == nil {
1635 return nil
1636 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001637 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001638}
1639
Colin Crossf24a22a2019-01-31 14:12:44 -08001640func (j *Module) DexJar() android.Path {
1641 return j.dexJarFile
1642}
1643
Colin Cross331a1212018-08-15 20:40:52 -07001644func (j *Module) ResourceJars() android.Paths {
1645 if j.resourceJar == nil {
1646 return nil
1647 }
1648 return android.Paths{j.resourceJar}
1649}
1650
1651func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001652 if j.implementationAndResourcesJar == nil {
1653 return nil
1654 }
Colin Cross331a1212018-08-15 20:40:52 -07001655 return android.Paths{j.implementationAndResourcesJar}
1656}
1657
Colin Cross46c9b8b2017-06-22 16:51:17 -07001658func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001659 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001660 return j.exportAidlIncludeDirs
1661}
1662
Jiyong Park1be96912018-05-28 18:02:19 +09001663func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001664 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001665 return j.exportedSdkLibs
1666}
1667
Artur Satayev9cf46692019-11-26 18:08:34 +00001668func (j *Module) ExportedPlugins() (android.Paths, []string) {
1669 return j.exportedPluginJars, j.exportedPluginClasses
1670}
1671
Colin Cross0c4ce212019-05-03 15:28:19 -07001672func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1673 return j.srcJarArgs, j.srcJarDeps
1674}
1675
Colin Cross46c9b8b2017-06-22 16:51:17 -07001676var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001677
Colin Cross46c9b8b2017-06-22 16:51:17 -07001678func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001679 return j.logtagsSrcs
1680}
1681
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001682// Collect information for opening IDE project files in java/jdeps.go.
1683func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1684 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1685 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001686 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001687 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001688 if j.expandJarjarRules != nil {
1689 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001690 }
1691}
1692
1693func (j *Module) CompilerDeps() []string {
1694 jdeps := []string{}
1695 jdeps = append(jdeps, j.properties.Libs...)
1696 jdeps = append(jdeps, j.properties.Static_libs...)
1697 return jdeps
1698}
1699
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001700func (j *Module) hasCode(ctx android.ModuleContext) bool {
1701 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1702 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1703}
1704
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001705func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1706 depTag := ctx.OtherModuleDependencyTag(dep)
1707 // dependencies other than the static linkage are all considered crossing APEX boundary
1708 return depTag == staticLibTag
1709}
1710
Jiyong Park0b238752019-10-29 11:23:10 +09001711func (j *Module) Stem() string {
1712 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1713}
1714
Colin Cross2fe66872015-03-30 17:20:39 -07001715//
1716// Java libraries (.jar file)
1717//
1718
Colin Crossf506d872017-07-19 15:53:04 -07001719type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001720 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001721
1722 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001723}
1724
Colin Cross42be7612019-02-21 18:12:14 -08001725func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001726 // Store uncompressed (and do not strip) dex files from boot class path jars.
1727 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1728 return true
1729 }
1730
1731 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001732 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001733 return true
1734 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001735 if ctx.Config().UncompressPrivAppDex() &&
1736 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1737 return true
1738 }
1739
Colin Cross2fc72f62018-12-21 12:59:54 -08001740 return false
1741}
1742
Colin Crossf506d872017-07-19 15:53:04 -07001743func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001744 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001745 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001746 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001747 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001748 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001749 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001750 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001751
Jiyong Park7f7766d2019-07-25 22:02:35 +09001752 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1753 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001754 var extraInstallDeps android.Paths
1755 if j.InstallMixin != nil {
1756 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1757 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001758 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001759 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001760 }
Colin Crossb7a63242015-04-16 14:09:14 -07001761}
1762
Colin Crossf506d872017-07-19 15:53:04 -07001763func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001764 j.deps(ctx)
1765}
1766
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001767const (
Paul Duffina0dbf432019-12-05 11:25:53 +00001768 aidlIncludeDir = "aidl"
1769 javaDir = "java"
1770 jarFileSuffix = ".jar"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001771)
1772
Paul Duffina0dbf432019-12-05 11:25:53 +00001773// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
1774func (j *Library) sdkSnapshotFilePathForJar() string {
1775 return filepath.Join(javaDir, j.Name()+jarFileSuffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001776}
1777
Paul Duffin13879572019-11-28 14:31:38 +00001778type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001779 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001780}
1781
1782func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1783 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1784}
1785
1786func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1787 _, ok := module.(*Library)
1788 return ok
1789}
1790
Paul Duffina0dbf432019-12-05 11:25:53 +00001791func (mt *librarySdkMemberType) buildSnapshot(
1792 sdkModuleContext android.ModuleContext,
1793 builder android.SnapshotBuilder,
1794 member android.SdkMember,
1795 jarToExportGetter func(j *Library) android.Path) {
1796
Paul Duffin13879572019-11-28 14:31:38 +00001797 variants := member.Variants()
1798 if len(variants) != 1 {
1799 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1800 for _, variant := range variants {
1801 sdkModuleContext.ModuleErrorf(" %q", variant)
1802 }
1803 }
1804 variant := variants[0]
1805 j := variant.(*Library)
1806
Paul Duffina0dbf432019-12-05 11:25:53 +00001807 exportedJar := jarToExportGetter(j)
1808 snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
1809 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001810
1811 for _, dir := range j.AidlIncludeDirs() {
1812 // TODO(jiyong): copy parcelable declarations only
1813 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1814 for _, file := range aidlFiles {
1815 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1816 }
1817 }
1818
Paul Duffin9d8d6092019-12-05 18:19:29 +00001819 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001820 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001821}
1822
Paul Duffina0dbf432019-12-05 11:25:53 +00001823type headerLibrarySdkMemberType struct {
1824 librarySdkMemberType
1825}
1826
1827func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1828 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1829 headerJars := j.HeaderJars()
1830 if len(headerJars) != 1 {
1831 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1832 }
1833
1834 return headerJars[0]
1835 })
1836}
1837
Paul Duffina0dbf432019-12-05 11:25:53 +00001838type implLibrarySdkMemberType struct {
1839 librarySdkMemberType
1840}
1841
1842func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1843 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1844 implementationJars := j.ImplementationJars()
1845 if len(implementationJars) != 1 {
1846 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1847 }
1848
1849 return implementationJars[0]
1850 })
1851}
1852
Colin Cross1b16b0e2019-02-12 14:41:32 -08001853// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1854//
1855// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1856// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1857// as a `static_libs` dependency of another module.
1858//
1859// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1860// a device.
1861//
1862// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1863// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001864func LibraryFactory() android.Module {
1865 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001866
Colin Cross9ae1b922018-06-26 17:59:05 -07001867 module.AddProperties(
1868 &module.Module.properties,
1869 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001870 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001871 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001872
Jiyong Park7f7766d2019-07-25 22:02:35 +09001873 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001874 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001875 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001876 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001877}
1878
Colin Cross1b16b0e2019-02-12 14:41:32 -08001879// java_library_static is an obsolete alias for java_library.
1880func LibraryStaticFactory() android.Module {
1881 return LibraryFactory()
1882}
1883
1884// java_library_host builds and links sources into a `.jar` file for the host.
1885//
1886// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1887// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001888func LibraryHostFactory() android.Module {
1889 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001890
Colin Cross6af17aa2017-09-20 12:59:05 -07001891 module.AddProperties(
1892 &module.Module.properties,
1893 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001894
Colin Cross9ae1b922018-06-26 17:59:05 -07001895 module.Module.properties.Installable = proptools.BoolPtr(true)
1896
Jiyong Park7f7766d2019-07-25 22:02:35 +09001897 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001898 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001899 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001900}
1901
1902//
Colin Crossb628ea52018-08-14 16:42:33 -07001903// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001904//
1905
1906type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001907 // list of compatibility suites (for example "cts", "vts") that the module should be
1908 // installed into.
1909 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001910
1911 // the name of the test configuration (for example "AndroidTest.xml") that should be
1912 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001913 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001914
Jack He33338892018-09-19 02:21:28 -07001915 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1916 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001917 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001918
Colin Crossd96ca352018-08-10 16:06:24 -07001919 // list of files or filegroup modules that provide data that should be installed alongside
1920 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001921 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001922
1923 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1924 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1925 // explicitly.
1926 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001927}
1928
Paul Duffin42df1442019-03-20 12:45:53 +00001929type testHelperLibraryProperties struct {
1930 // list of compatibility suites (for example "cts", "vts") that the module should be
1931 // installed into.
1932 Test_suites []string `android:"arch_variant"`
1933}
1934
Colin Cross05638fc2018-04-09 18:40:24 -07001935type Test struct {
1936 Library
1937
1938 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001939
1940 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001941 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001942}
1943
Paul Duffin42df1442019-03-20 12:45:53 +00001944type TestHelperLibrary struct {
1945 Library
1946
1947 testHelperLibraryProperties testHelperLibraryProperties
1948}
1949
Colin Cross303e21f2018-08-07 16:49:25 -07001950func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001951 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1952 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001953 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001954
1955 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001956}
1957
Paul Duffin42df1442019-03-20 12:45:53 +00001958func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1959 j.Library.GenerateAndroidBuildActions(ctx)
1960}
1961
Colin Cross1b16b0e2019-02-12 14:41:32 -08001962// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1963// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1964//
1965// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1966// compiled against the device bootclasspath.
1967//
1968// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1969// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001970func TestFactory() android.Module {
1971 module := &Test{}
1972
1973 module.AddProperties(
1974 &module.Module.properties,
1975 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001976 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001977 &module.Module.protoProperties,
1978 &module.testProperties)
1979
Colin Cross9ae1b922018-06-26 17:59:05 -07001980 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001981 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001982
Colin Cross05638fc2018-04-09 18:40:24 -07001983 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001984 return module
1985}
1986
Paul Duffin42df1442019-03-20 12:45:53 +00001987// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1988func TestHelperLibraryFactory() android.Module {
1989 module := &TestHelperLibrary{}
1990
1991 module.AddProperties(
1992 &module.Module.properties,
1993 &module.Module.deviceProperties,
1994 &module.Module.dexpreoptProperties,
1995 &module.Module.protoProperties,
1996 &module.testHelperLibraryProperties)
1997
Colin Cross9a4abed2019-04-24 13:19:28 -07001998 module.Module.properties.Installable = proptools.BoolPtr(true)
1999 module.Module.dexpreopter.isTest = true
2000
Paul Duffin42df1442019-03-20 12:45:53 +00002001 InitJavaModule(module, android.HostAndDeviceSupported)
2002 return module
2003}
2004
Colin Cross1b16b0e2019-02-12 14:41:32 -08002005// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2006// allow running the test with `atest` or a `TEST_MAPPING` file.
2007//
2008// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2009// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002010func TestHostFactory() android.Module {
2011 module := &Test{}
2012
2013 module.AddProperties(
2014 &module.Module.properties,
2015 &module.Module.protoProperties,
2016 &module.testProperties)
2017
Colin Cross9ae1b922018-06-26 17:59:05 -07002018 module.Module.properties.Installable = proptools.BoolPtr(true)
2019
Colin Cross05638fc2018-04-09 18:40:24 -07002020 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002021 return module
2022}
2023
2024//
Colin Cross2fe66872015-03-30 17:20:39 -07002025// Java Binaries (.jar file plus wrapper script)
2026//
2027
Colin Crossf506d872017-07-19 15:53:04 -07002028type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002029 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002030 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002031
2032 // Name of the class containing main to be inserted into the manifest as Main-Class.
2033 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002034}
2035
Colin Crossf506d872017-07-19 15:53:04 -07002036type Binary struct {
2037 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002038
Colin Crossf506d872017-07-19 15:53:04 -07002039 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002040
Colin Cross6b4a32d2017-12-05 13:42:45 -08002041 isWrapperVariant bool
2042
Colin Crossc3315992017-12-08 19:12:36 -08002043 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002044 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002045}
2046
Alex Light24237172017-10-26 09:46:21 -07002047func (j *Binary) HostToolPath() android.OptionalPath {
2048 return android.OptionalPathForPath(j.binaryFile)
2049}
2050
Colin Crossf506d872017-07-19 15:53:04 -07002051func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002052 if ctx.Arch().ArchType == android.Common {
2053 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002054 if j.binaryProperties.Main_class != nil {
2055 if j.properties.Manifest != nil {
2056 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2057 }
2058 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2059 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2060 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2061 }
2062
Colin Cross6b4a32d2017-12-05 13:42:45 -08002063 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002064 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002065 // Handle the binary wrapper
2066 j.isWrapperVariant = true
2067
Colin Cross366938f2017-12-11 16:29:02 -08002068 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002069 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002070 } else {
2071 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2072 }
2073
2074 // Depend on the installed jar so that the wrapper doesn't get executed by
2075 // another build rule before the jar has been installed.
2076 jarFile := ctx.PrimaryModule().(*Binary).installFile
2077
2078 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2079 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002080 }
Colin Cross2fe66872015-03-30 17:20:39 -07002081}
2082
Colin Crossf506d872017-07-19 15:53:04 -07002083func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002084 if ctx.Arch().ArchType == android.Common {
2085 j.deps(ctx)
2086 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002087}
2088
Colin Cross1b16b0e2019-02-12 14:41:32 -08002089// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2090// as well.
2091//
2092// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2093// compiled against the device bootclasspath.
2094//
2095// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2096// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002097func BinaryFactory() android.Module {
2098 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002099
Colin Cross36242852017-06-23 15:06:31 -07002100 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002101 &module.Module.properties,
2102 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002103 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002104 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002105 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002106
Colin Cross9ae1b922018-06-26 17:59:05 -07002107 module.Module.properties.Installable = proptools.BoolPtr(true)
2108
Colin Cross6b4a32d2017-12-05 13:42:45 -08002109 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2110 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002111 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002112}
2113
Colin Cross1b16b0e2019-02-12 14:41:32 -08002114// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2115//
2116// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2117// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002118func BinaryHostFactory() android.Module {
2119 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002120
Colin Cross36242852017-06-23 15:06:31 -07002121 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002122 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002123 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002124 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002125
Colin Cross9ae1b922018-06-26 17:59:05 -07002126 module.Module.properties.Installable = proptools.BoolPtr(true)
2127
Colin Cross6b4a32d2017-12-05 13:42:45 -08002128 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2129 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002130 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002131}
2132
2133//
2134// Java prebuilts
2135//
2136
Colin Cross74d73e22017-08-02 11:05:49 -07002137type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002138 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002139
Nan Zhangea568a42017-11-08 21:20:04 -08002140 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002141
2142 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002143
2144 // List of shared java libs that this module has dependencies to
2145 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002146
2147 // List of files to remove from the jar file(s)
2148 Exclude_files []string
2149
2150 // List of directories to remove from the jar file(s)
2151 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002152
2153 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002154 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002155
2156 // set the name of the output
2157 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002158}
2159
2160type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002161 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002162 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002163 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002164 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002165 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002166
Colin Cross74d73e22017-08-02 11:05:49 -07002167 properties ImportProperties
2168
Colin Cross0a6e0072017-08-30 14:24:55 -07002169 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002170 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002171}
2172
Colin Cross83bb3162018-06-25 15:48:06 -07002173func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002174 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002175}
2176
2177func (j *Import) minSdkVersion() string {
2178 return j.sdkVersion()
2179}
2180
Colin Cross74d73e22017-08-02 11:05:49 -07002181func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002182 return &j.prebuilt
2183}
2184
Colin Cross74d73e22017-08-02 11:05:49 -07002185func (j *Import) PrebuiltSrcs() []string {
2186 return j.properties.Jars
2187}
2188
2189func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002190 return j.prebuilt.Name(j.ModuleBase.Name())
2191}
2192
Jiyong Park0b238752019-10-29 11:23:10 +09002193func (j *Import) Stem() string {
2194 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2195}
2196
Colin Cross74d73e22017-08-02 11:05:49 -07002197func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002198 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002199}
2200
Colin Cross74d73e22017-08-02 11:05:49 -07002201func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002202 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002203
Jiyong Park0b238752019-10-29 11:23:10 +09002204 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002205 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002206 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2207 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002208 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002209 inputFile := outputFile
2210 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2211 TransformJetifier(ctx, outputFile, inputFile)
2212 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002213 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002214
2215 ctx.VisitDirectDeps(func(module android.Module) {
2216 otherName := ctx.OtherModuleName(module)
2217 tag := ctx.OtherModuleDependencyTag(module)
2218
2219 switch dep := module.(type) {
2220 case Dependency:
2221 switch tag {
2222 case libTag, staticLibTag:
2223 // sdk lib names from dependencies are re-exported
2224 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2225 }
2226 case SdkLibraryDependency:
2227 switch tag {
2228 case libTag:
2229 // names of sdk libs that are directly depended are exported
2230 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2231 }
2232 }
2233 })
2234
2235 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002236 if Bool(j.properties.Installable) {
2237 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002238 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002239 }
Colin Cross2fe66872015-03-30 17:20:39 -07002240}
2241
Colin Cross74d73e22017-08-02 11:05:49 -07002242var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002243
Nan Zhanged19fc32017-10-19 13:06:22 -07002244func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002245 if j.combinedClasspathFile == nil {
2246 return nil
2247 }
Colin Cross37f6d792018-07-12 12:28:41 -07002248 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002249}
2250
2251func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002252 if j.combinedClasspathFile == nil {
2253 return nil
2254 }
Colin Cross37f6d792018-07-12 12:28:41 -07002255 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002256}
2257
Colin Cross331a1212018-08-15 20:40:52 -07002258func (j *Import) ResourceJars() android.Paths {
2259 return nil
2260}
2261
2262func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002263 if j.combinedClasspathFile == nil {
2264 return nil
2265 }
Colin Cross331a1212018-08-15 20:40:52 -07002266 return android.Paths{j.combinedClasspathFile}
2267}
2268
Colin Crossf24a22a2019-01-31 14:12:44 -08002269func (j *Import) DexJar() android.Path {
2270 return nil
2271}
2272
Colin Cross74d73e22017-08-02 11:05:49 -07002273func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002274 return nil
2275}
2276
Jiyong Park1be96912018-05-28 18:02:19 +09002277func (j *Import) ExportedSdkLibs() []string {
2278 return j.exportedSdkLibs
2279}
2280
Artur Satayev9cf46692019-11-26 18:08:34 +00002281func (j *Import) ExportedPlugins() (android.Paths, []string) {
2282 return nil, nil
2283}
2284
Colin Cross0c4ce212019-05-03 15:28:19 -07002285func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2286 return nil, nil
2287}
2288
albaltai36ff7dc2018-12-25 14:35:23 +08002289// Add compile time check for interface implementation
2290var _ android.IDEInfo = (*Import)(nil)
2291var _ android.IDECustomizedModuleName = (*Import)(nil)
2292
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002293// Collect information for opening IDE project files in java/jdeps.go.
2294const (
2295 removedPrefix = "prebuilt_"
2296)
2297
2298func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2299 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2300}
2301
2302func (j *Import) IDECustomizedModuleName() string {
2303 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2304 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2305 // solution to get the Import name.
2306 name := j.Name()
2307 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002308 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002309 }
2310 return name
2311}
2312
Colin Cross74d73e22017-08-02 11:05:49 -07002313var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002314
Colin Cross1b16b0e2019-02-12 14:41:32 -08002315// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2316//
2317// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2318// compiled against an Android classpath.
2319//
2320// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2321// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002322func ImportFactory() android.Module {
2323 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002324
Colin Cross74d73e22017-08-02 11:05:49 -07002325 module.AddProperties(&module.properties)
2326
2327 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002328 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002329 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002330 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002331 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002332}
2333
Colin Cross1b16b0e2019-02-12 14:41:32 -08002334// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2335// module.
2336//
2337// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2338// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002339func ImportFactoryHost() android.Module {
2340 module := &Import{}
2341
2342 module.AddProperties(&module.properties)
2343
2344 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002345 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002346 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002347 return module
2348}
2349
Colin Cross42be7612019-02-21 18:12:14 -08002350// dex_import module
2351
2352type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002353 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002354
2355 // set the name of the output
2356 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002357}
2358
2359type DexImport struct {
2360 android.ModuleBase
2361 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002362 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002363 prebuilt android.Prebuilt
2364
2365 properties DexImportProperties
2366
2367 dexJarFile android.Path
2368 maybeStrippedDexJarFile android.Path
2369
2370 dexpreopter
2371}
2372
2373func (j *DexImport) Prebuilt() *android.Prebuilt {
2374 return &j.prebuilt
2375}
2376
2377func (j *DexImport) PrebuiltSrcs() []string {
2378 return j.properties.Jars
2379}
2380
2381func (j *DexImport) Name() string {
2382 return j.prebuilt.Name(j.ModuleBase.Name())
2383}
2384
Jiyong Park0b238752019-10-29 11:23:10 +09002385func (j *DexImport) Stem() string {
2386 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2387}
2388
Colin Cross42be7612019-02-21 18:12:14 -08002389func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2390 if len(j.properties.Jars) != 1 {
2391 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2392 }
2393
Jiyong Park0b238752019-10-29 11:23:10 +09002394 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002395 j.dexpreopter.isInstallable = true
2396 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2397
2398 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2399 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2400
2401 if j.dexpreopter.uncompressedDex {
2402 rule := android.NewRuleBuilder()
2403
2404 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2405 rule.Temporary(temporary)
2406
2407 // use zip2zip to uncompress classes*.dex files
2408 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002409 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002410 FlagWithInput("-i ", inputJar).
2411 FlagWithOutput("-o ", temporary).
2412 FlagWithArg("-0 ", "'classes*.dex'")
2413
2414 // use zipalign to align uncompressed classes*.dex files
2415 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002416 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002417 Flag("-f").
2418 Text("4").
2419 Input(temporary).
2420 Output(dexOutputFile)
2421
2422 rule.DeleteTemporaryFiles()
2423
2424 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2425 } else {
2426 ctx.Build(pctx, android.BuildParams{
2427 Rule: android.Cp,
2428 Input: inputJar,
2429 Output: dexOutputFile,
2430 })
2431 }
2432
2433 j.dexJarFile = dexOutputFile
2434
2435 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2436
2437 j.maybeStrippedDexJarFile = dexOutputFile
2438
2439 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2440 ctx.ModuleName()+".jar", dexOutputFile)
2441}
2442
2443func (j *DexImport) DexJar() android.Path {
2444 return j.dexJarFile
2445}
2446
2447// dex_import imports a `.jar` file containing classes.dex files.
2448//
2449// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2450// to the device.
2451func DexImportFactory() android.Module {
2452 module := &DexImport{}
2453
2454 module.AddProperties(&module.properties)
2455
2456 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002457 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002458 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002459 return module
2460}
2461
Colin Cross89536d42017-07-07 14:35:50 -07002462//
2463// Defaults
2464//
2465type Defaults struct {
2466 android.ModuleBase
2467 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002468 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002469}
2470
Colin Cross1b16b0e2019-02-12 14:41:32 -08002471// java_defaults provides a set of properties that can be inherited by other java or android modules.
2472//
2473// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2474// property in the defaults module that exists in the depending module will be prepended to the depending module's
2475// value for that property.
2476//
2477// Example:
2478//
2479// java_defaults {
2480// name: "example_defaults",
2481// srcs: ["common/**/*.java"],
2482// javacflags: ["-Xlint:all"],
2483// aaptflags: ["--auto-add-overlay"],
2484// }
2485//
2486// java_library {
2487// name: "example",
2488// defaults: ["example_defaults"],
2489// srcs: ["example/**/*.java"],
2490// }
2491//
2492// is functionally identical to:
2493//
2494// java_library {
2495// name: "example",
2496// srcs: [
2497// "common/**/*.java",
2498// "example/**/*.java",
2499// ],
2500// javacflags: ["-Xlint:all"],
2501// }
Colin Cross89536d42017-07-07 14:35:50 -07002502func defaultsFactory() android.Module {
2503 return DefaultsFactory()
2504}
2505
Paul Duffin47357662019-12-05 14:07:14 +00002506func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002507 module := &Defaults{}
2508
Colin Cross89536d42017-07-07 14:35:50 -07002509 module.AddProperties(
2510 &CompilerProperties{},
2511 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002512 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002513 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002514 &aaptProperties{},
2515 &androidLibraryProperties{},
2516 &appProperties{},
2517 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002518 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002519 &ImportProperties{},
2520 &AARImportProperties{},
2521 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002522 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002523 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002524 )
2525
2526 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002527 return module
2528}
Nan Zhangea568a42017-11-08 21:20:04 -08002529
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002530func kytheExtractJavaFactory() android.Singleton {
2531 return &kytheExtractJavaSingleton{}
2532}
2533
2534type kytheExtractJavaSingleton struct {
2535}
2536
2537func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2538 var xrefTargets android.Paths
2539 ctx.VisitAllModules(func(module android.Module) {
2540 if javaModule, ok := module.(xref); ok {
2541 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2542 }
2543 })
2544 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2545 if len(xrefTargets) > 0 {
2546 ctx.Build(pctx, android.BuildParams{
2547 Rule: blueprint.Phony,
2548 Output: android.PathForPhony(ctx, "xref_java"),
2549 Inputs: xrefTargets,
2550 })
2551 }
2552}
2553
Nan Zhangea568a42017-11-08 21:20:04 -08002554var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002555var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002556var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002557var inList = android.InList