blob: 320cb7b5588b30dfe0fef84b1f474b582430a0ee [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"
Martin Stjernholm335d5962020-01-11 00:37:30 +000032 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070033 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070034 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070035)
36
Colin Cross463a90e2015-06-17 14:20:06 -070037func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000038 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000039
40 // Register sdk member types.
41 android.RegisterSdkMemberType(&headerLibrarySdkMemberType{
42 librarySdkMemberType{
43 android.SdkMemberTypeBase{
44 PropertyName: "java_header_libs",
Paul Duffine6029182019-12-16 17:43:48 +000045 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000046 },
47 },
48 })
49
50 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
51 librarySdkMemberType{
52 android.SdkMemberTypeBase{
53 PropertyName: "java_libs",
54 },
55 },
56 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000057
58 android.RegisterSdkMemberType(&testSdkMemberType{
59 SdkMemberTypeBase: android.SdkMemberTypeBase{
60 PropertyName: "java_tests",
61 },
62 })
Colin Cross463a90e2015-06-17 14:20:06 -070063}
64
Paul Duffinf9b1da02019-12-18 19:51:55 +000065func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
66 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
67
68 ctx.RegisterModuleType("java_library", LibraryFactory)
69 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
70 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
71 ctx.RegisterModuleType("java_binary", BinaryFactory)
72 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
73 ctx.RegisterModuleType("java_test", TestFactory)
74 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
75 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000076 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000077 ctx.RegisterModuleType("java_import", ImportFactory)
78 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
79 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
80 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
81 ctx.RegisterModuleType("dex_import", DexImportFactory)
82
Martin Stjernholm335d5962020-01-11 00:37:30 +000083 ctx.FinalDepsMutators(dexpreopt.RegisterToolDepsMutator)
84
Paul Duffinf9b1da02019-12-18 19:51:55 +000085 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
86 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
87}
88
Jeongik Cha2cc570d2019-10-29 15:44:45 +090089func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
90 if j.SocSpecific() || j.DeviceSpecific() ||
91 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
92 if sc, ok := ctx.Module().(sdkContext); ok {
93 if sc.sdkVersion() == "" {
94 ctx.PropertyErrorf("sdk_version",
95 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
96 }
97 }
98 }
99}
100
Jeongik Cha538c0d02019-07-11 15:54:27 +0900101func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
102 if sc, ok := ctx.Module().(sdkContext); ok {
103 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
104 if usePlatformAPI != (sc.sdkVersion() == "") {
105 if usePlatformAPI {
106 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
107 } else {
108 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
109 }
110 }
111
112 }
113}
114
Colin Cross2fe66872015-03-30 17:20:39 -0700115// TODO:
116// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700117// Renderscript
118// Post-jar passes:
119// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700120// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700121// DroidDoc
122// Findbugs
123
Colin Cross89536d42017-07-07 14:35:50 -0700124type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700125 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
126 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800127 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700128
129 // list of source files that should not be used to build the Java module.
130 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800131 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700132
133 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700134 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700135
Colin Cross86a63ff2017-09-27 17:33:10 -0700136 // list of directories that should be excluded from java_resource_dirs
137 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700138
Colin Cross0f37af02017-09-27 17:42:05 -0700139 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800140 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700141
Colin Crosscedd4762018-09-13 11:26:19 -0700142 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800143 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700144
Colin Cross7d5136f2015-05-11 13:39:40 -0700145 // list of module-specific flags that will be used for javac compiles
146 Javacflags []string `android:"arch_variant"`
147
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200148 // list of module-specific flags that will be used for kotlinc compiles
149 Kotlincflags []string `android:"arch_variant"`
150
Colin Cross7d5136f2015-05-11 13:39:40 -0700151 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700152 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700153
154 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700155 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700156
157 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800158 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700159
Colin Cross540eff82017-06-22 17:01:52 -0700160 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800161 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700162
163 // If not blank, set the java version passed to javac as -source and -target
164 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700165
Colin Cross9ae1b922018-06-26 17:59:05 -0700166 // If set to true, allow this module to be dexed and installed on devices. Has no
167 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700168 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700169
Colin Cross0f37af02017-09-27 17:42:05 -0700170 // If set to true, include sources used to compile the module in to the final jar
171 Include_srcs *bool
172
Vladimir Marko0975ee02019-04-02 10:29:55 +0100173 // If not empty, classes are restricted to the specified packages and their sub-packages.
174 // This restriction is checked after applying jarjar rules and including static libs.
175 Permitted_packages []string
176
Colin Crossbe9cdb82019-01-21 21:37:16 -0800177 // List of modules to use as annotation processors
178 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700179
Artur Satayev9cf46692019-11-26 18:08:34 +0000180 // List of modules to export to libraries that directly depend on this library as annotation processors
181 Exported_plugins []string
182
Nan Zhang61eaedb2017-11-02 13:28:15 -0700183 // The number of Java source entries each Javac instance can process
184 Javac_shard_size *int64
185
Nan Zhang5f8cb422018-02-06 10:34:32 -0800186 // Add host jdk tools.jar to bootclasspath
187 Use_tools_jar *bool
188
Colin Cross1369cdb2017-09-29 17:58:17 -0700189 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700190 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800191 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700192
Colin Cross6cef4812019-10-17 14:23:50 -0700193 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700194 Javacflags []string
195 }
Colin Crosscb933592017-11-22 13:49:43 -0800196
Colin Cross81440082018-08-15 20:21:55 -0700197 // When compiling language level 9+ .java code in packages that are part of
198 // a system module, patch_module names the module that your sources and
199 // dependencies should be patched into. The Android runtime currently
200 // doesn't implement the JEP 261 module system so this option is only
201 // supported at compile time. It should only be needed to compile tests in
202 // packages that exist in libcore and which are inconvenient to move
203 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100204 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700205
Colin Crosscb933592017-11-22 13:49:43 -0800206 Jacoco struct {
207 // List of classes to include for instrumentation with jacoco to collect coverage
208 // information at runtime when building with coverage enabled. If unset defaults to all
209 // classes.
210 // Supports '*' as the last character of an entry in the list as a wildcard match.
211 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
212 // it matches classes in the package that have the class name as a prefix.
213 Include_filter []string
214
215 // List of classes to exclude from instrumentation with jacoco to collect coverage
216 // information at runtime when building with coverage enabled. Overrides classes selected
217 // by the include_filter property.
218 // Supports '*' as the last character of an entry in the list as a wildcard match.
219 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
220 // it matches classes in the package that have the class name as a prefix.
221 Exclude_filter []string
222 }
223
Andreas Gampef3e5b552018-01-22 21:27:21 -0800224 Errorprone struct {
225 // List of javac flags that should only be used when running errorprone.
226 Javacflags []string
227 }
228
Colin Cross0f2ee152017-12-14 15:22:43 -0800229 Proto struct {
230 // List of extra options that will be passed to the proto generator.
231 Output_params []string
232 }
233
Colin Crosscb933592017-11-22 13:49:43 -0800234 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800235
236 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800237 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700238}
239
Colin Cross89536d42017-07-07 14:35:50 -0700240type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700241 // list of module-specific flags that will be used for dex compiles
242 Dxflags []string `android:"arch_variant"`
243
Jeongik Cha538c0d02019-07-11 15:54:27 +0900244 // if not blank, set to the version of the sdk to compile against.
245 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800246 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700247
Colin Cross83bb3162018-06-25 15:48:06 -0700248 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
249 // Defaults to sdk_version if not set.
250 Min_sdk_version *string
251
Dan Willemsen419290a2018-10-31 15:28:47 -0700252 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
253 // Defaults to sdk_version if not set.
254 Target_sdk_version *string
255
Jeongik Cha356dac42019-08-19 14:09:52 +0900256 // Whether to compile against the platform APIs instead of an SDK.
257 // If true, then sdk_version must be empty. The value of this field
258 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700259 Platform_apis *bool
260
Colin Crossebe1a512017-11-14 13:12:14 -0800261 Aidl struct {
262 // Top level directories to pass to aidl tool
263 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700264
Colin Crossebe1a512017-11-14 13:12:14 -0800265 // Directories rooted at the Android.bp file to pass to aidl tool
266 Local_include_dirs []string
267
268 // directories that should be added as include directories for any aidl sources of modules
269 // that depend on this module, as well as to aidl for this module.
270 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100271
272 // whether to generate traces (for systrace) for this interface
273 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100274
275 // whether to generate Binder#GetTransaction name method.
276 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800277 }
Colin Cross92430102017-10-09 14:59:32 -0700278
279 // If true, export a copy of the module as a -hostdex module for host testing.
280 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700281
Colin Cross7f87f4f2019-04-24 13:41:45 -0700282 Target struct {
283 Hostdex struct {
284 // Additional required dependencies to add to -hostdex modules.
285 Required []string
286 }
287 }
288
David Brazdil17ef5632018-06-27 10:27:45 +0100289 // If set to true, compile dex regardless of installable. Defaults to false.
290 Compile_dex *bool
291
Colin Cross66dbc0b2017-12-28 12:23:20 -0800292 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700293 // If false, disable all optimization. Defaults to true for android_app and android_test
294 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800295 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700296 // True if the module containing this has it set by default.
297 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800298
299 // If true, optimize for size by removing unused code. Defaults to true for apps,
300 // false for libraries and tests.
301 Shrink *bool
302
303 // If true, optimize bytecode. Defaults to false.
304 Optimize *bool
305
306 // If true, obfuscate bytecode. Defaults to false.
307 Obfuscate *bool
308
309 // If true, do not use the flag files generated by aapt that automatically keep
310 // classes referenced by the app manifest. Defaults to false.
311 No_aapt_flags *bool
312
313 // Flags to pass to proguard.
314 Proguard_flags []string
315
316 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800317 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800318 }
319
Paul Duffine25c6442019-10-11 13:50:28 +0100320 // When targeting 1.9 and above, override the modules to use with --system,
321 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700322 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700323
Jiyong Park4c4c0242019-10-21 14:53:15 +0900324 // set the name of the output
325 Stem *string
326
Colin Cross5a0dcd52018-10-05 14:20:06 -0700327 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800328 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700329}
330
Sasha Smundak2057f822019-04-16 17:16:58 -0700331func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
332 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
333}
334
Colin Cross46c9b8b2017-06-22 16:51:17 -0700335// Module contains the properties and members used by all java module types
336type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700337 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700338 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900339 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900340 android.SdkBase
Martin Stjernholm335d5962020-01-11 00:37:30 +0000341 dexpreopt.DexPreoptModule
Colin Cross2fe66872015-03-30 17:20:39 -0700342
Colin Cross89536d42017-07-07 14:35:50 -0700343 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700344 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700345 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700346
Colin Cross331a1212018-08-15 20:40:52 -0700347 // jar file containing header classes including static library dependencies, suitable for
348 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700349 headerJarFile android.Path
350
Colin Cross331a1212018-08-15 20:40:52 -0700351 // jar file containing implementation classes including static library dependencies but no
352 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700353 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700354
Colin Cross331a1212018-08-15 20:40:52 -0700355 // jar file containing only resources including from static library dependencies
356 resourceJar android.Path
357
Colin Cross0c4ce212019-05-03 15:28:19 -0700358 // args and dependencies to package source files into a srcjar
359 srcJarArgs []string
360 srcJarDeps android.Paths
361
Colin Cross331a1212018-08-15 20:40:52 -0700362 // jar file containing implementation classes and resources including static library
363 // dependencies
364 implementationAndResourcesJar android.Path
365
366 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700367 dexJarFile android.Path
368
Colin Cross43f08db2018-11-12 10:13:39 -0800369 // output file that contains classes.dex if it should be in the output file
370 maybeStrippedDexJarFile android.Path
371
Colin Crosscb933592017-11-22 13:49:43 -0800372 // output file containing uninstrumented classes that will be instrumented by jacoco
373 jacocoReportClassesFile android.Path
374
Colin Cross66dbc0b2017-12-28 12:23:20 -0800375 // output file containing mapping of obfuscated names
376 proguardDictionary android.Path
377
Colin Cross331a1212018-08-15 20:40:52 -0700378 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700379 outputFile android.Path
380 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700381
Colin Cross635c3b02016-05-18 15:37:25 -0700382 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700383
Colin Cross635c3b02016-05-18 15:37:25 -0700384 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700385
Colin Cross2fe66872015-03-30 17:20:39 -0700386 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700387 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800388
389 // list of .java files and srcjars that was passed to javac
390 compiledJavaSrcs android.Paths
391 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800392
393 // list of extra progurad flag files
394 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900395
Colin Cross094054a2018-10-17 15:10:48 -0700396 // manifest file to use instead of properties.Manifest
397 overrideManifest android.OptionalPath
398
Artur Satayev9cf46692019-11-26 18:08:34 +0000399 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900400 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700401
Artur Satayev9cf46692019-11-26 18:08:34 +0000402 // list of plugins that this java module is exporting
403 exportedPluginJars android.Paths
404
405 // list of plugins that this java module is exporting
406 exportedPluginClasses []string
407
408 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800409 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700410 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800411
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800412 // expanded Jarjar_rules
413 expandJarjarRules android.Path
414
Vladimir Marko0975ee02019-04-02 10:29:55 +0100415 // list of additional targets for checkbuild
416 additionalCheckedModules android.Paths
417
Colin Cross988708c2019-05-06 14:04:11 -0700418 // Extra files generated by the module type to be added as java resources.
419 extraResources android.Paths
420
Colin Crossf24a22a2019-01-31 14:12:44 -0800421 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800422 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800423
424 // list of the xref extraction files
425 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700426}
427
Colin Cross41955e82019-05-29 14:40:35 -0700428func (j *Module) OutputFiles(tag string) (android.Paths, error) {
429 switch tag {
430 case "":
431 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700432 case ".jar":
433 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700434 case ".proguard_map":
435 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700436 default:
437 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
438 }
Colin Cross54250902017-12-05 09:28:08 -0800439}
440
Colin Cross41955e82019-05-29 14:40:35 -0700441var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800442
Colin Crossf506d872017-07-19 15:53:04 -0700443type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700444 HeaderJars() android.Paths
445 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700446 ResourceJars() android.Paths
447 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800448 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700449 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900450 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000451 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700452 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700453 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900454 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700455}
456
Jiyong Parkc678ad32018-04-10 13:07:10 +0900457type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700458 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
459 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900460}
461
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800462type xref interface {
463 XrefJavaFiles() android.Paths
464}
465
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800466func (j *Module) XrefJavaFiles() android.Paths {
467 return j.kytheFiles
468}
469
Colin Cross89536d42017-07-07 14:35:50 -0700470func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
471 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
472 android.InitDefaultableModule(module)
473}
474
Colin Crossbe1da472017-07-07 15:59:46 -0700475type dependencyTag struct {
476 blueprint.BaseDependencyTag
477 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700478}
479
Colin Crossa4f08812018-10-02 22:03:40 -0700480type jniDependencyTag struct {
481 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700482}
483
Jiyong Park8be103b2019-11-08 15:53:48 +0900484func IsJniDepTag(depTag blueprint.DependencyTag) bool {
485 _, ok := depTag.(*jniDependencyTag)
486 return ok
487}
488
Colin Crossbe1da472017-07-07 15:59:46 -0700489var (
Colin Cross4b964c02018-10-15 16:18:06 -0700490 staticLibTag = dependencyTag{name: "staticlib"}
491 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700492 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800493 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000494 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700495 bootClasspathTag = dependencyTag{name: "bootclasspath"}
496 systemModulesTag = dependencyTag{name: "system modules"}
497 frameworkResTag = dependencyTag{name: "framework-res"}
498 frameworkApkTag = dependencyTag{name: "framework-apk"}
499 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800500 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700501 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
502 certificateTag = dependencyTag{name: "certificate"}
503 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700504 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700505)
Colin Cross2fe66872015-03-30 17:20:39 -0700506
Jiyong Park83dc74b2020-01-14 18:38:44 +0900507func IsLibDepTag(depTag blueprint.DependencyTag) bool {
508 return depTag == libTag
509}
510
511func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
512 return depTag == staticLibTag
513}
514
Colin Crossfc3674a2017-09-18 17:41:52 -0700515type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700516 useModule, useFiles, useDefaultLibs, invalidVersion bool
517
Colin Cross6cef4812019-10-17 14:23:50 -0700518 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
519 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100520
521 // The default system modules to use. Will be an empty string if no system
522 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700523 systemModules string
524
Colin Cross6cef4812019-10-17 14:23:50 -0700525 // The modules that will be added ot the classpath when targeting 1.9 or higher
526 java9Classpath []string
527
Colin Crossa97c5d32018-03-28 14:58:31 -0700528 frameworkResModule string
529
Colin Cross86a60ae2018-05-29 14:44:55 -0700530 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700531 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100532
533 noStandardLibs, noFrameworksLibs bool
534}
535
536func (s sdkDep) hasStandardLibs() bool {
537 return !s.noStandardLibs
538}
539
540func (s sdkDep) hasFrameworkLibs() bool {
541 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700542}
543
Colin Crossa4f08812018-10-02 22:03:40 -0700544type jniLib struct {
545 name string
546 path android.Path
547 target android.Target
548}
549
Colin Cross0ea8ba82019-06-06 14:33:29 -0700550func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800551 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
552}
553
Colin Cross0ea8ba82019-06-06 14:33:29 -0700554func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800555 return j.shouldInstrument(ctx) &&
556 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
557 ctx.Config().UnbundledBuild())
558}
559
Colin Cross83bb3162018-06-25 15:48:06 -0700560func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900561 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700562}
563
Paul Duffine25c6442019-10-11 13:50:28 +0100564func (j *Module) systemModules() string {
565 return proptools.String(j.deviceProperties.System_modules)
566}
567
Colin Cross83bb3162018-06-25 15:48:06 -0700568func (j *Module) minSdkVersion() string {
569 if j.deviceProperties.Min_sdk_version != nil {
570 return *j.deviceProperties.Min_sdk_version
571 }
572 return j.sdkVersion()
573}
574
Dan Willemsen419290a2018-10-31 15:28:47 -0700575func (j *Module) targetSdkVersion() string {
576 if j.deviceProperties.Target_sdk_version != nil {
577 return *j.deviceProperties.Target_sdk_version
578 }
579 return j.sdkVersion()
580}
581
Jiyong Parkb02bb402019-12-03 00:43:57 +0900582func (j *Module) AvailableFor(what string) bool {
583 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
584 // Exception: for hostdex: true libraries, the platform variant is created
585 // even if it's not marked as available to platform. In that case, the platform
586 // variant is used only for the hostdex and not installed to the device.
587 return true
588 }
589 return j.ApexModuleBase.AvailableFor(what)
590}
591
Colin Crossbe1da472017-07-07 15:59:46 -0700592func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700593 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100594 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700595 if sdkDep.useDefaultLibs {
596 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
597 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
598 if sdkDep.hasFrameworkLibs() {
599 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700600 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700601 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700602 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100603 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700604 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700605 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
606 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
607 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
608 }
Colin Cross2fe66872015-03-30 17:20:39 -0700609 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700610
Nan Zhangb2b33de2018-02-23 11:18:47 -0800611 if ctx.ModuleName() == "android_stubs_current" ||
612 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700613 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700614 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800615 }
Colin Cross2fe66872015-03-30 17:20:39 -0700616 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700617
Inseob Kimac1e9862019-12-09 18:15:47 +0900618 syspropPublicStubs := syspropPublicStubs(ctx.Config())
619
620 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
621 // and redirects dependency to public stub depending on the link type.
622 rewriteSyspropLibs := func(libs []string, prop string) []string {
623 // make a copy
624 ret := android.CopyOf(libs)
625
626 for idx, lib := range libs {
627 stub, ok := syspropPublicStubs[lib]
628
629 if !ok {
630 continue
631 }
632
633 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900634 // only platform modules can use internal props
635 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900636 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900637 }
638 }
639
640 return ret
641 }
642
643 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
644 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700645
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700646 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000647 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800648
Colin Crossfe17f6f2019-03-28 19:30:56 -0700649 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700650 if j.hasSrcExt(".proto") {
651 protoDeps(ctx, &j.protoProperties)
652 }
Colin Cross93e85952017-08-15 13:34:18 -0700653
654 if j.hasSrcExt(".kt") {
655 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
656 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700657 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
658 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800659 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800660 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
661 }
Colin Cross93e85952017-08-15 13:34:18 -0700662 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800663
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000664 // Framework libraries need special handling in static coverage builds: they should not have
665 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
666 // the same jacoco classes coming from different bootclasspath jars.
667 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
668 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
669 j.properties.Instrument = true
670 }
671 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700672 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800673 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700674}
675
676func hasSrcExt(srcs []string, ext string) bool {
677 for _, src := range srcs {
678 if filepath.Ext(src) == ext {
679 return true
680 }
681 }
682
683 return false
684}
685
686func (j *Module) hasSrcExt(ext string) bool {
687 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700688}
689
Colin Cross46c9b8b2017-06-22 16:51:17 -0700690func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700691 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700692
Colin Crossebe1a512017-11-14 13:12:14 -0800693 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
694 aidlIncludes = append(aidlIncludes,
695 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
696 aidlIncludes = append(aidlIncludes,
697 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700698
Colin Cross3047fa22019-04-18 10:56:44 -0700699 var flags []string
700 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700701
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700702 if aidlPreprocess.Valid() {
703 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700704 deps = append(deps, aidlPreprocess.Path())
705 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700706 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700707 }
708
Colin Cross3047fa22019-04-18 10:56:44 -0700709 if len(j.exportAidlIncludeDirs) > 0 {
710 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
711 }
712
713 if len(aidlIncludes) > 0 {
714 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
715 }
716
Colin Cross635c3b02016-05-18 15:37:25 -0700717 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800718 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700719 flags = append(flags, "-I"+src.String())
720 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700721
Martijn Coeneneab15642018-03-09 09:29:59 +0100722 if Bool(j.deviceProperties.Aidl.Generate_traces) {
723 flags = append(flags, "-t")
724 }
725
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100726 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
727 flags = append(flags, "--transaction_names")
728 }
729
Colin Cross3047fa22019-04-18 10:56:44 -0700730 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700731}
732
Colin Cross32f676a2017-09-06 13:41:06 -0700733type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800734 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700735 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800736 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700737 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800738 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700739 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700740 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700741 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700742 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800743 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700744 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700745 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700746 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700747 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800748 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800749
750 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700751}
Colin Cross2fe66872015-03-30 17:20:39 -0700752
Colin Cross54250902017-12-05 09:28:08 -0800753func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
754 for _, f := range dep.Srcs() {
755 if f.Ext() != ".jar" {
756 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
757 ctx.OtherModuleName(dep.(blueprint.Module)))
758 }
759 }
760}
761
Jiyong Park2d492942018-03-05 17:44:10 +0900762type linkType int
763
764const (
765 javaCore linkType = iota
766 javaSdk
767 javaSystem
768 javaPlatform
769)
770
Jeongik Cha75b83b02019-11-01 15:28:00 +0900771type linkTypeContext interface {
772 android.Module
773 getLinkType(name string) (ret linkType, stubs bool)
774}
775
776func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700777 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700778 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900779 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
780 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100781 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900782 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100783 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900784 return javaCore, false
785 case name == "android_system_stubs_current":
786 return javaSystem, true
787 case strings.HasPrefix(ver, "system_"):
788 return javaSystem, false
789 case name == "android_test_stubs_current":
790 return javaSystem, true
791 case strings.HasPrefix(ver, "test_"):
792 return javaPlatform, false
793 case name == "android_stubs_current":
794 return javaSdk, true
795 case ver == "current":
796 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100797 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900798 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700799 default:
800 if _, err := strconv.Atoi(ver); err != nil {
801 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
802 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900803 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900804 }
805}
806
Jeongik Cha75b83b02019-11-01 15:28:00 +0900807func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700808 if ctx.Host() {
809 return
810 }
811
Jeongik Cha75b83b02019-11-01 15:28:00 +0900812 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900813 if stubs {
814 return
815 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900816 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900817 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."
818
819 switch myLinkType {
820 case javaCore:
821 if otherLinkType != javaCore {
822 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 +0900823 ctx.OtherModuleName(to))
824 }
Jiyong Park2d492942018-03-05 17:44:10 +0900825 break
826 case javaSdk:
827 if otherLinkType != javaCore && otherLinkType != javaSdk {
828 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
829 ctx.OtherModuleName(to))
830 }
831 break
832 case javaSystem:
833 if otherLinkType == javaPlatform {
834 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
835 ctx.OtherModuleName(to))
836 }
837 break
838 case javaPlatform:
839 // no restriction on link-type
840 break
Jiyong Park750e5572018-01-31 00:20:13 +0900841 }
842}
843
Colin Cross32f676a2017-09-06 13:41:06 -0700844func (j *Module) collectDeps(ctx android.ModuleContext) deps {
845 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700846
Colin Cross300f0382018-03-06 13:11:51 -0800847 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700848 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800849 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700850 ctx.AddMissingDependencies(sdkDep.bootclasspath)
851 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800852 } else if sdkDep.useFiles {
853 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700854 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700855 deps.aidlPreprocess = sdkDep.aidl
856 } else {
857 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800858 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700859 }
860
Colin Crossd11fcda2017-10-23 17:59:01 -0700861 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700862 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700863 tag := ctx.OtherModuleDependencyTag(module)
864
Colin Crossa4f08812018-10-02 22:03:40 -0700865 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700866 // Handled by AndroidApp.collectAppDeps
867 return
868 }
869 if tag == certificateTag {
870 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700871 return
872 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900873 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900874 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900875 if to, ok := module.(linkTypeContext); ok {
876 switch tag {
877 case bootClasspathTag, libTag, staticLibTag:
878 checkLinkType(ctx, j, to, tag.(dependencyTag))
879 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700880 }
Jiyong Park750e5572018-01-31 00:20:13 +0900881 }
Colin Cross54250902017-12-05 09:28:08 -0800882 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800883 case SdkLibraryDependency:
884 switch tag {
885 case libTag:
886 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
887 // names of sdk libs that are directly depended are exported
888 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700889 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800890 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
891 }
Colin Cross54250902017-12-05 09:28:08 -0800892 case Dependency:
893 switch tag {
894 case bootClasspathTag:
895 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700896 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800897 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900898 // sdk lib names from dependencies are re-exported
899 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700900 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000901 pluginJars, pluginClasses := dep.ExportedPlugins()
902 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700903 case java9LibTag:
904 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800905 case staticLibTag:
906 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
907 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
908 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700909 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900910 // sdk lib names from dependencies are re-exported
911 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700912 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000913 pluginJars, pluginClasses := dep.ExportedPlugins()
914 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800915 case pluginTag:
916 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800917 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000918 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
919 } else {
920 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800921 }
922 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
923 } else {
924 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
925 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000926 case exportedPluginTag:
927 if plugin, ok := dep.(*Plugin); ok {
928 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
929 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
930 }
931 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
932 if plugin.pluginProperties.Processor_class != nil {
933 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
934 }
935 } else {
936 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
937 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800938 case frameworkApkTag:
939 if ctx.ModuleName() == "android_stubs_current" ||
940 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700941 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800942 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
943 // resource files out of there for aapt.
944 //
945 // Normally the package rule runs aapt, which includes the resource,
946 // but we're not running that in our package rule so just copy in the
947 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700948 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800949 }
Colin Cross54250902017-12-05 09:28:08 -0800950 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700951 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800952 case kotlinAnnotationsTag:
953 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800954 }
955
Colin Cross54250902017-12-05 09:28:08 -0800956 case android.SourceFileProducer:
957 switch tag {
958 case libTag:
959 checkProducesJars(ctx, dep)
960 deps.classpath = append(deps.classpath, dep.Srcs()...)
961 case staticLibTag:
962 checkProducesJars(ctx, dep)
963 deps.classpath = append(deps.classpath, dep.Srcs()...)
964 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
965 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800966 }
967 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700968 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100969 case bootClasspathTag:
970 // If a system modules dependency has been added to the bootclasspath
971 // then add its libs to the bootclasspath.
972 sm := module.(*SystemModules)
973 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
974
Colin Cross1369cdb2017-09-29 17:58:17 -0700975 case systemModulesTag:
976 if deps.systemModules != nil {
977 panic("Found two system module dependencies")
978 }
979 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000980 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700981 panic("Missing directory for system module dependency")
982 }
Colin Crossb77043e2019-07-16 13:57:13 -0700983 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700984 }
Colin Crossec7a0422017-07-07 14:47:12 -0700985 }
Colin Cross2fe66872015-03-30 17:20:39 -0700986 })
987
Jiyong Park1be96912018-05-28 18:02:19 +0900988 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
989
Colin Cross32f676a2017-09-06 13:41:06 -0700990 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700991}
992
Artur Satayev9cf46692019-11-26 18:08:34 +0000993func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
994 deps.processorPath = append(deps.processorPath, pluginJars...)
995 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
996}
997
Colin Cross1e743852019-10-28 11:37:20 -0700998func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800999 v := sdkContext.sdkVersion()
1000 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +01001001 if ctx.Config().IsPdkBuild() &&
1002 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -07001003 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -08001004 latestSdkVersion := 0
1005 if len(sdkVersions) > 0 {
1006 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
1007 }
1008 v = strconv.Itoa(latestSdkVersion)
1009 }
1010
1011 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -07001012 if err != nil {
1013 ctx.PropertyErrorf("sdk_version", "%s", err)
1014 }
Nan Zhang357466b2018-04-17 17:38:36 -07001015 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001016 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001017 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001018 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001019 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001020 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001021 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1022 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001023 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001024 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001025 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001026 }
Nan Zhang357466b2018-04-17 17:38:36 -07001027}
1028
Colin Cross1e743852019-10-28 11:37:20 -07001029type javaVersion int
1030
1031const (
1032 JAVA_VERSION_UNSUPPORTED = 0
1033 JAVA_VERSION_6 = 6
1034 JAVA_VERSION_7 = 7
1035 JAVA_VERSION_8 = 8
1036 JAVA_VERSION_9 = 9
1037)
1038
1039func (v javaVersion) String() string {
1040 switch v {
1041 case JAVA_VERSION_6:
1042 return "1.6"
1043 case JAVA_VERSION_7:
1044 return "1.7"
1045 case JAVA_VERSION_8:
1046 return "1.8"
1047 case JAVA_VERSION_9:
1048 return "1.9"
1049 default:
1050 return "unsupported"
1051 }
1052}
1053
1054// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1055func (v javaVersion) usesJavaModules() bool {
1056 return v >= 9
1057}
1058
1059func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001060 switch javaVersion {
1061 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001062 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001063 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001064 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001065 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001066 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001067 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001068 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001069 case "10", "11":
1070 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001071 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001072 default:
1073 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001074 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001075 }
1076}
1077
Nan Zhanged19fc32017-10-19 13:06:22 -07001078func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001079
Colin Crossf03c82b2015-04-13 13:53:40 -07001080 var flags javaBuilderFlags
1081
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001082 // javaVersion flag.
1083 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1084
Nan Zhanged19fc32017-10-19 13:06:22 -07001085 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001086 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001087 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001088 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001089 }
Colin Cross6510f912017-11-29 00:27:14 -08001090 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001091 // Override the -g flag passed globally to remove local variable debug info to reduce
1092 // disk and memory usage.
1093 javacFlags = append(javacFlags, "-g:source,lines")
1094 }
Colin Crossc228a702019-11-06 16:18:05 -08001095 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001096
Colin Cross66548102018-06-19 22:47:35 -07001097 if ctx.Config().RunErrorProne() {
1098 if config.ErrorProneClasspath == nil {
1099 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1100 }
1101
1102 errorProneFlags := []string{
1103 "-Xplugin:ErrorProne",
1104 "${config.ErrorProneChecks}",
1105 }
1106 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1107
1108 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1109 "'" + strings.Join(errorProneFlags, " ") + "'"
1110 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001111 }
1112
Nan Zhanged19fc32017-10-19 13:06:22 -07001113 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001114 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1115 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001116 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001117 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001118
Colin Crossbe9cdb82019-01-21 21:37:16 -08001119 flags.processor = strings.Join(deps.processorClasses, ",")
1120
Colin Cross1e743852019-10-28 11:37:20 -07001121 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1122 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001123 // Give host-side tools a version of OpenJDK's standard libraries
1124 // close to what they're targeting. As of Dec 2017, AOSP is only
1125 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1126 //
1127 // When building with OpenJDK 8, the following should have no
1128 // effect since those jars would be available by default.
1129 //
1130 // When building with OpenJDK 9 but targeting a version < 1.8,
1131 // putting them on the bootclasspath means that:
1132 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1133 // b) references to existing APIs are not reinterpreted in an
1134 // OpenJDK 9-specific way, eg. calls to subclasses of
1135 // java.nio.Buffer as in http://b/70862583
1136 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1137 flags.bootClasspath = append(flags.bootClasspath,
1138 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1139 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001140 if Bool(j.properties.Use_tools_jar) {
1141 flags.bootClasspath = append(flags.bootClasspath,
1142 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1143 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001144 }
1145
Colin Cross1e743852019-10-28 11:37:20 -07001146 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001147 // Manually specify build directory in case it is not under the repo root.
1148 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1149 // just adding a symlink under the root doesn't help.)
1150 patchPaths := ".:" + ctx.Config().BuildDir()
1151 classPath := flags.classpath.FormJavaClassPath("")
1152 if classPath != "" {
1153 patchPaths += ":" + classPath
1154 }
1155 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001156 }
1157
Nan Zhanged19fc32017-10-19 13:06:22 -07001158 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001159 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001160
Nan Zhanged19fc32017-10-19 13:06:22 -07001161 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001162 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001163
Colin Cross81440082018-08-15 20:21:55 -07001164 if len(javacFlags) > 0 {
1165 // optimization.
1166 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1167 flags.javacFlags = "$javacFlags"
1168 }
1169
Nan Zhanged19fc32017-10-19 13:06:22 -07001170 return flags
1171}
Colin Crossc0b06f12015-04-08 13:03:43 -07001172
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001173func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001174 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001175
1176 deps := j.collectDeps(ctx)
1177 flags := j.collectBuilderFlags(ctx, deps)
1178
Colin Cross1e743852019-10-28 11:37:20 -07001179 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001180 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1181 }
Colin Cross8a497952019-03-05 22:25:09 -08001182 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001183 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001184 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001185 }
1186
Colin Crossaf050172017-11-15 23:01:59 -08001187 srcFiles = j.genSources(ctx, srcFiles, flags)
1188
1189 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001190 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001191 if aaptSrcJar != nil {
1192 srcJars = append(srcJars, aaptSrcJar)
1193 }
Colin Crossb7a63242015-04-16 14:09:14 -07001194
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001195 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001196 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001197 }
1198
Colin Cross1ee23172017-10-18 14:44:18 -07001199 jarName := ctx.ModuleName() + ".jar"
1200
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001201 javaSrcFiles := srcFiles.FilterByExt(".java")
1202 var uniqueSrcFiles android.Paths
1203 set := make(map[string]bool)
1204 for _, v := range javaSrcFiles {
1205 if _, found := set[v.String()]; !found {
1206 set[v.String()] = true
1207 uniqueSrcFiles = append(uniqueSrcFiles, v)
1208 }
1209 }
1210
patricktu242faad2019-09-24 15:41:30 +08001211 // Collect .java files for AIDEGen
1212 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1213
Colin Cross55f63ea2018-08-27 12:37:09 -07001214 var kotlinJars android.Paths
1215
Colin Cross93e85952017-08-15 13:34:18 -07001216 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001217 // user defined kotlin flags.
1218 kotlincFlags := j.properties.Kotlincflags
1219 CheckKotlincFlags(ctx, kotlincFlags)
1220
Colin Cross93e85952017-08-15 13:34:18 -07001221 // If there are kotlin files, compile them first but pass all the kotlin and java files
1222 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1223 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001224 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001225 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001226 kotlincFlags = append(kotlincFlags, "-no-jdk")
1227 }
1228 if len(kotlincFlags) > 0 {
1229 // optimization.
1230 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1231 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001232 }
1233
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001234 var kotlinSrcFiles android.Paths
1235 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1236 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1237
patricktu242faad2019-09-24 15:41:30 +08001238 // Collect .kt files for AIDEGen
1239 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1240
Colin Crossafbb1732019-01-17 15:42:52 -08001241 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1242 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1243
1244 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1245 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1246
1247 if len(flags.processorPath) > 0 {
1248 // Use kapt for annotation processing
1249 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1250 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1251 srcJars = append(srcJars, kaptSrcJar)
1252 // Disable annotation processing in javac, it's already been handled by kapt
1253 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001254 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001255 }
Colin Cross93e85952017-08-15 13:34:18 -07001256
Colin Cross1ee23172017-10-18 14:44:18 -07001257 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001258 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001259 if ctx.Failed() {
1260 return
1261 }
1262
1263 // Make javac rule depend on the kotlinc rule
1264 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001265
Colin Cross93e85952017-08-15 13:34:18 -07001266 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001267 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001268 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001269 }
1270
Colin Cross55f63ea2018-08-27 12:37:09 -07001271 jars := append(android.Paths(nil), kotlinJars...)
1272
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001273 // Store the list of .java files that was passed to javac
1274 j.compiledJavaSrcs = uniqueSrcFiles
1275 j.compiledSrcJars = srcJars
1276
Nan Zhang61eaedb2017-11-02 13:28:15 -07001277 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001278 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001279 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1280 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001281 // Formerly, there was a check here that prevented annotation processors
1282 // from being used when sharding was enabled, as some annotation processors
1283 // do not function correctly in sharded environments. It was removed to
1284 // allow for the use of annotation processors that do function correctly
1285 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001286 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001287 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001288 if ctx.Failed() {
1289 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001290 }
1291 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001292 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001293 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001294 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001295 // If error-prone is enabled, add an additional rule to compile the java files into
1296 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001297 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001298 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1299 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001300 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001301 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001302 extraJarDeps = append(extraJarDeps, errorprone)
1303 }
1304
Nan Zhang61eaedb2017-11-02 13:28:15 -07001305 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001306 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001307 shardSize := int(*(j.properties.Javac_shard_size))
1308 var shardSrcs []android.Paths
1309 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001310 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001311 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001312 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1313 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001314 jars = append(jars, classes)
1315 }
1316 }
1317 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001318 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1319 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001320 jars = append(jars, classes)
1321 }
1322 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001323 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001324 jars = append(jars, classes)
1325 }
Colin Crossd6891432017-09-27 17:39:56 -07001326 if ctx.Failed() {
1327 return
1328 }
Colin Cross2fe66872015-03-30 17:20:39 -07001329 }
1330
Colin Cross0c4ce212019-05-03 15:28:19 -07001331 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1332
1333 var includeSrcJar android.WritablePath
1334 if Bool(j.properties.Include_srcs) {
1335 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1336 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1337 }
1338
Colin Crosscedd4762018-09-13 11:26:19 -07001339 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1340 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001341 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001342 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001343
1344 var resArgs []string
1345 var resDeps android.Paths
1346
1347 resArgs = append(resArgs, dirArgs...)
1348 resDeps = append(resDeps, dirDeps...)
1349
1350 resArgs = append(resArgs, fileArgs...)
1351 resDeps = append(resDeps, fileDeps...)
1352
Colin Cross988708c2019-05-06 14:04:11 -07001353 resArgs = append(resArgs, extraArgs...)
1354 resDeps = append(resDeps, extraDeps...)
1355
Colin Cross40a36712017-09-27 17:41:35 -07001356 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001357 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001358 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001359 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001360 if ctx.Failed() {
1361 return
1362 }
1363 }
1364
Colin Cross0c4ce212019-05-03 15:28:19 -07001365 var resourceJars android.Paths
1366 if j.resourceJar != nil {
1367 resourceJars = append(resourceJars, j.resourceJar)
1368 }
1369 if Bool(j.properties.Include_srcs) {
1370 resourceJars = append(resourceJars, includeSrcJar)
1371 }
1372 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001373
Colin Cross0c4ce212019-05-03 15:28:19 -07001374 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001375 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001376 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001377 false, nil, nil)
1378 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001379 } else if len(resourceJars) == 1 {
1380 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001381 }
1382
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001383 if len(deps.staticJars) > 0 {
1384 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001385 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001386
Colin Cross094054a2018-10-17 15:10:48 -07001387 manifest := j.overrideManifest
1388 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001389 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001390 }
Colin Cross635acc92017-09-12 22:50:46 -07001391
Colin Cross8a497952019-03-05 22:25:09 -08001392 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001393 if len(services) > 0 {
1394 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1395 var zipargs []string
1396 for _, file := range services {
1397 serviceFile := file.String()
1398 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1399 }
1400 ctx.Build(pctx, android.BuildParams{
1401 Rule: zip,
1402 Output: servicesJar,
1403 Implicits: services,
1404 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001405 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001406 },
1407 })
1408 jars = append(jars, servicesJar)
1409 }
1410
Colin Cross0a6e0072017-08-30 14:24:55 -07001411 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001412 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001413 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001414
1415 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001416 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1417 // Optimization: skip the combine step if there is nothing to do
1418 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1419 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1420 // any if len(jars) == 1.
1421 outputFile = moduleOutPath
1422 } else {
1423 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1424 ctx.Build(pctx, android.BuildParams{
1425 Rule: android.Cp,
1426 Input: jars[0],
1427 Output: combinedJar,
1428 })
1429 outputFile = combinedJar
1430 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001431 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001432 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001433 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001434 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001435 outputFile = combinedJar
1436 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001437
Colin Cross331a1212018-08-15 20:40:52 -07001438 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001439 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001440 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001441 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001442 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001443 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001444
1445 // jarjar resource jar if necessary
1446 if j.resourceJar != nil {
1447 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001448 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001449 j.resourceJar = resourceJarJarFile
1450 }
1451
Colin Cross0a6e0072017-08-30 14:24:55 -07001452 if ctx.Failed() {
1453 return
1454 }
1455 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001456
1457 // Check package restrictions if necessary.
1458 if len(j.properties.Permitted_packages) > 0 {
1459 // Check packages and copy to package-checked file.
1460 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1461 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1462 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1463
1464 if ctx.Failed() {
1465 return
1466 }
1467 }
1468
Nan Zhanged19fc32017-10-19 13:06:22 -07001469 j.implementationJarFile = outputFile
1470 if j.headerJarFile == nil {
1471 j.headerJarFile = j.implementationJarFile
1472 }
Colin Cross2fe66872015-03-30 17:20:39 -07001473
Colin Cross3144dfc2018-01-03 15:06:47 -08001474 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001475 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1476 }
1477
Colin Cross331a1212018-08-15 20:40:52 -07001478 // merge implementation jar with resources if necessary
1479 implementationAndResourcesJar := outputFile
1480 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001481 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001482 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001483 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001484 false, nil, nil)
1485 implementationAndResourcesJar = combinedJar
1486 }
1487
1488 j.implementationAndResourcesJar = implementationAndResourcesJar
1489
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001490 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001491 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001492 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001493 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001494 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001495 if ctx.Failed() {
1496 return
1497 }
Colin Cross331a1212018-08-15 20:40:52 -07001498
Jiyong Park09cb6292019-07-15 15:29:23 +09001499 // Hidden API CSV generation and dex encoding
1500 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1501 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001502
Colin Cross331a1212018-08-15 20:40:52 -07001503 // merge dex jar with resources if necessary
1504 if j.resourceJar != nil {
1505 jars := android.Paths{dexOutputFile, j.resourceJar}
1506 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1507 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1508 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001509 if j.deviceProperties.UncompressDex {
1510 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1511 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1512 dexOutputFile = combinedAlignedJar
1513 } else {
1514 dexOutputFile = combinedJar
1515 }
Colin Cross331a1212018-08-15 20:40:52 -07001516 }
1517
1518 j.dexJarFile = dexOutputFile
1519
Colin Cross8faf8fc2019-01-16 15:15:52 -08001520 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001521 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1522
1523 j.maybeStrippedDexJarFile = dexOutputFile
1524
Colin Cross3063b782018-08-15 11:19:12 -07001525 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001526
1527 if ctx.Failed() {
1528 return
1529 }
Colin Cross331a1212018-08-15 20:40:52 -07001530 } else {
1531 outputFile = implementationAndResourcesJar
Martin Stjernholm335d5962020-01-11 00:37:30 +00001532
1533 // dexpreopt.GetGlobalSoongConfig needs to be called at least once even if
1534 // no module actually is dexpreopted, to ensure there's a cached
1535 // GlobalSoongConfig for the dexpreopt singletons, which will run
1536 // regardless.
1537 // TODO(b/147613152): Remove when the singletons no longer rely on the
1538 // cached GlobalSoongConfig.
1539 if !dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
1540 _ = dexpreopt.GetGlobalSoongConfig(ctx)
1541 }
Colin Cross2fe66872015-03-30 17:20:39 -07001542 }
Colin Cross331a1212018-08-15 20:40:52 -07001543
Colin Crossb7a63242015-04-16 14:09:14 -07001544 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001545
1546 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1547 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001548}
1549
Colin Cross3b706fd2019-09-05 16:44:18 -07001550func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1551 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1552
1553 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1554 if idx >= 0 {
1555 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1556 jarName += strconv.Itoa(idx)
1557 }
1558
1559 classes := android.PathForModuleOut(ctx, "javac", jarName)
1560 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1561
1562 if ctx.Config().EmitXrefRules() {
1563 extractionFile := android.PathForModuleOut(ctx, kzipName)
1564 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1565 j.kytheFiles = append(j.kytheFiles, extractionFile)
1566 }
1567
1568 return classes
1569}
1570
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001571// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1572// since some of these flags may be used internally.
1573func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1574 for _, flag := range flags {
1575 flag = strings.TrimSpace(flag)
1576
1577 if !strings.HasPrefix(flag, "-") {
1578 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1579 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1580 ctx.PropertyErrorf("kotlincflags",
1581 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1582 } else if inList(flag, config.KotlincIllegalFlags) {
1583 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1584 } else if flag == "-include-runtime" {
1585 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1586 } else {
1587 args := strings.Split(flag, " ")
1588 if args[0] == "-kotlin-home" {
1589 ctx.PropertyErrorf("kotlincflags",
1590 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1591 }
1592 }
1593 }
1594}
1595
Colin Cross8eadbf02017-10-24 17:46:00 -07001596func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001597 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001598
1599 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001600 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001601 // Compile java sources into turbine.jar.
1602 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1603 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1604 if ctx.Failed() {
1605 return nil
1606 }
1607 jars = append(jars, turbineJar)
1608 }
1609
Colin Cross55f63ea2018-08-27 12:37:09 -07001610 jars = append(jars, extraJars...)
1611
Nan Zhanged19fc32017-10-19 13:06:22 -07001612 // Combine any static header libraries into classes-header.jar. If there is only
1613 // one input jar this step will be skipped.
1614 var headerJar android.Path
1615 jars = append(jars, deps.staticHeaderJars...)
1616
Colin Cross5c6ecc12017-10-23 18:12:27 -07001617 // we cannot skip the combine step for now if there is only one jar
1618 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1619 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001620 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001621 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001622 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001623
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001624 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001625 // Transform classes.jar into classes-jarjar.jar
1626 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001627 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001628 headerJar = jarjarFile
1629 if ctx.Failed() {
1630 return nil
1631 }
1632 }
1633
1634 return headerJar
1635}
1636
Colin Crosscb933592017-11-22 13:49:43 -08001637func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001638 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001639
Colin Cross7a3139e2017-12-19 13:57:50 -08001640 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001641
Colin Cross84c38822018-01-03 15:59:46 -08001642 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001643 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1644
1645 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1646
1647 j.jacocoReportClassesFile = jacocoReportClassesFile
1648
1649 return instrumentedJar
1650}
1651
albaltai36ff7dc2018-12-25 14:35:23 +08001652var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001653
Nan Zhanged19fc32017-10-19 13:06:22 -07001654func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001655 if j.headerJarFile == nil {
1656 return nil
1657 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001658 return android.Paths{j.headerJarFile}
1659}
1660
1661func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001662 if j.implementationJarFile == nil {
1663 return nil
1664 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001665 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001666}
1667
Colin Crossf24a22a2019-01-31 14:12:44 -08001668func (j *Module) DexJar() android.Path {
1669 return j.dexJarFile
1670}
1671
Colin Cross331a1212018-08-15 20:40:52 -07001672func (j *Module) ResourceJars() android.Paths {
1673 if j.resourceJar == nil {
1674 return nil
1675 }
1676 return android.Paths{j.resourceJar}
1677}
1678
1679func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001680 if j.implementationAndResourcesJar == nil {
1681 return nil
1682 }
Colin Cross331a1212018-08-15 20:40:52 -07001683 return android.Paths{j.implementationAndResourcesJar}
1684}
1685
Colin Cross46c9b8b2017-06-22 16:51:17 -07001686func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001687 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001688 return j.exportAidlIncludeDirs
1689}
1690
Jiyong Park1be96912018-05-28 18:02:19 +09001691func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001692 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001693 return j.exportedSdkLibs
1694}
1695
Artur Satayev9cf46692019-11-26 18:08:34 +00001696func (j *Module) ExportedPlugins() (android.Paths, []string) {
1697 return j.exportedPluginJars, j.exportedPluginClasses
1698}
1699
Colin Cross0c4ce212019-05-03 15:28:19 -07001700func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1701 return j.srcJarArgs, j.srcJarDeps
1702}
1703
Colin Cross46c9b8b2017-06-22 16:51:17 -07001704var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001705
Colin Cross46c9b8b2017-06-22 16:51:17 -07001706func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001707 return j.logtagsSrcs
1708}
1709
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001710// Collect information for opening IDE project files in java/jdeps.go.
1711func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1712 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1713 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001714 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001715 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001716 if j.expandJarjarRules != nil {
1717 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001718 }
1719}
1720
1721func (j *Module) CompilerDeps() []string {
1722 jdeps := []string{}
1723 jdeps = append(jdeps, j.properties.Libs...)
1724 jdeps = append(jdeps, j.properties.Static_libs...)
1725 return jdeps
1726}
1727
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001728func (j *Module) hasCode(ctx android.ModuleContext) bool {
1729 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1730 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1731}
1732
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001733func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1734 depTag := ctx.OtherModuleDependencyTag(dep)
1735 // dependencies other than the static linkage are all considered crossing APEX boundary
1736 return depTag == staticLibTag
1737}
1738
Jiyong Park0b238752019-10-29 11:23:10 +09001739func (j *Module) Stem() string {
1740 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1741}
1742
Jiyong Park618922e2020-01-08 13:35:43 +09001743func (j *Module) JacocoReportClassesFile() android.Path {
1744 return j.jacocoReportClassesFile
1745}
1746
Colin Cross2fe66872015-03-30 17:20:39 -07001747//
1748// Java libraries (.jar file)
1749//
1750
Colin Crossf506d872017-07-19 15:53:04 -07001751type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001752 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001753
1754 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001755}
1756
Colin Cross42be7612019-02-21 18:12:14 -08001757func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001758 // Store uncompressed (and do not strip) dex files from boot class path jars.
1759 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1760 return true
1761 }
1762
1763 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001764 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001765 return true
1766 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001767 if ctx.Config().UncompressPrivAppDex() &&
1768 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1769 return true
1770 }
1771
Colin Cross2fc72f62018-12-21 12:59:54 -08001772 return false
1773}
1774
Colin Crossf506d872017-07-19 15:53:04 -07001775func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001776 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001777 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001778 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001779 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001780 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001781 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001782 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001783
Jiyong Park7f7766d2019-07-25 22:02:35 +09001784 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1785 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001786 var extraInstallDeps android.Paths
1787 if j.InstallMixin != nil {
1788 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1789 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001790 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001791 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001792 }
Colin Crossb7a63242015-04-16 14:09:14 -07001793}
1794
Colin Crossf506d872017-07-19 15:53:04 -07001795func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001796 j.deps(ctx)
1797}
1798
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001799const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001800 aidlIncludeDir = "aidl"
1801 javaDir = "java"
1802 jarFileSuffix = ".jar"
1803 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001804)
1805
Paul Duffina0dbf432019-12-05 11:25:53 +00001806// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001807func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1808 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1809}
1810
1811func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1812 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001813}
1814
Paul Duffin13879572019-11-28 14:31:38 +00001815type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001816 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001817}
1818
1819func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1820 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1821}
1822
1823func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1824 _, ok := module.(*Library)
1825 return ok
1826}
1827
Paul Duffina0dbf432019-12-05 11:25:53 +00001828func (mt *librarySdkMemberType) buildSnapshot(
1829 sdkModuleContext android.ModuleContext,
1830 builder android.SnapshotBuilder,
1831 member android.SdkMember,
1832 jarToExportGetter func(j *Library) android.Path) {
1833
Paul Duffin13879572019-11-28 14:31:38 +00001834 variants := member.Variants()
1835 if len(variants) != 1 {
1836 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1837 for _, variant := range variants {
1838 sdkModuleContext.ModuleErrorf(" %q", variant)
1839 }
1840 }
1841 variant := variants[0]
1842 j := variant.(*Library)
1843
Paul Duffina0dbf432019-12-05 11:25:53 +00001844 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001845 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001846 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001847
1848 for _, dir := range j.AidlIncludeDirs() {
1849 // TODO(jiyong): copy parcelable declarations only
1850 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1851 for _, file := range aidlFiles {
1852 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1853 }
1854 }
1855
Paul Duffin9d8d6092019-12-05 18:19:29 +00001856 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001857 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001858}
1859
Paul Duffina0dbf432019-12-05 11:25:53 +00001860type headerLibrarySdkMemberType struct {
1861 librarySdkMemberType
1862}
1863
1864func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1865 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1866 headerJars := j.HeaderJars()
1867 if len(headerJars) != 1 {
1868 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1869 }
1870
1871 return headerJars[0]
1872 })
1873}
1874
Paul Duffina0dbf432019-12-05 11:25:53 +00001875type implLibrarySdkMemberType struct {
1876 librarySdkMemberType
1877}
1878
1879func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1880 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1881 implementationJars := j.ImplementationJars()
1882 if len(implementationJars) != 1 {
1883 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1884 }
1885
1886 return implementationJars[0]
1887 })
1888}
1889
Colin Cross1b16b0e2019-02-12 14:41:32 -08001890// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1891//
1892// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1893// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1894// as a `static_libs` dependency of another module.
1895//
1896// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1897// a device.
1898//
1899// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1900// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001901func LibraryFactory() android.Module {
1902 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001903
Colin Cross9ae1b922018-06-26 17:59:05 -07001904 module.AddProperties(
1905 &module.Module.properties,
1906 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001907 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001908 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001909
Jiyong Park7f7766d2019-07-25 22:02:35 +09001910 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001911 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001912 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001913 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001914}
1915
Colin Cross1b16b0e2019-02-12 14:41:32 -08001916// java_library_static is an obsolete alias for java_library.
1917func LibraryStaticFactory() android.Module {
1918 return LibraryFactory()
1919}
1920
1921// java_library_host builds and links sources into a `.jar` file for the host.
1922//
1923// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1924// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001925func LibraryHostFactory() android.Module {
1926 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001927
Colin Cross6af17aa2017-09-20 12:59:05 -07001928 module.AddProperties(
1929 &module.Module.properties,
1930 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001931
Colin Cross9ae1b922018-06-26 17:59:05 -07001932 module.Module.properties.Installable = proptools.BoolPtr(true)
1933
Jiyong Park7f7766d2019-07-25 22:02:35 +09001934 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001935 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001936 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001937}
1938
1939//
Colin Crossb628ea52018-08-14 16:42:33 -07001940// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001941//
1942
1943type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001944 // list of compatibility suites (for example "cts", "vts") that the module should be
1945 // installed into.
1946 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001947
1948 // the name of the test configuration (for example "AndroidTest.xml") that should be
1949 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001950 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001951
Jack He33338892018-09-19 02:21:28 -07001952 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1953 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001954 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001955
Colin Crossd96ca352018-08-10 16:06:24 -07001956 // list of files or filegroup modules that provide data that should be installed alongside
1957 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001958 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001959
1960 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1961 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1962 // explicitly.
1963 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001964}
1965
Paul Duffin42df1442019-03-20 12:45:53 +00001966type testHelperLibraryProperties struct {
1967 // list of compatibility suites (for example "cts", "vts") that the module should be
1968 // installed into.
1969 Test_suites []string `android:"arch_variant"`
1970}
1971
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001972type prebuiltTestProperties struct {
1973 // list of compatibility suites (for example "cts", "vts") that the module should be
1974 // installed into.
1975 Test_suites []string `android:"arch_variant"`
1976
1977 // the name of the test configuration (for example "AndroidTest.xml") that should be
1978 // installed with the module.
1979 Test_config *string `android:"path,arch_variant"`
1980}
1981
Colin Cross05638fc2018-04-09 18:40:24 -07001982type Test struct {
1983 Library
1984
1985 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001986
1987 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001988 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001989}
1990
Paul Duffin42df1442019-03-20 12:45:53 +00001991type TestHelperLibrary struct {
1992 Library
1993
1994 testHelperLibraryProperties testHelperLibraryProperties
1995}
1996
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001997type JavaTestImport struct {
1998 Import
1999
2000 prebuiltTestProperties prebuiltTestProperties
2001
2002 testConfig android.Path
2003}
2004
Colin Cross303e21f2018-08-07 16:49:25 -07002005func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002006 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2007 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002008 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002009
2010 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002011}
2012
Paul Duffin42df1442019-03-20 12:45:53 +00002013func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2014 j.Library.GenerateAndroidBuildActions(ctx)
2015}
2016
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002017func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2018 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2019 j.prebuiltTestProperties.Test_suites, nil)
2020
2021 j.Import.GenerateAndroidBuildActions(ctx)
2022}
2023
2024type testSdkMemberType struct {
2025 android.SdkMemberTypeBase
2026}
2027
2028func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2029 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2030}
2031
2032func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2033 _, ok := module.(*Test)
2034 return ok
2035}
2036
2037func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2038 variants := member.Variants()
2039 if len(variants) != 1 {
2040 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2041 for _, variant := range variants {
2042 sdkModuleContext.ModuleErrorf(" %q", variant)
2043 }
2044 }
2045 variant := variants[0]
2046 j := variant.(*Test)
2047
2048 implementationJars := j.ImplementationJars()
2049 if len(implementationJars) != 1 {
2050 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2051 }
2052
2053 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2054 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2055
2056 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2057 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2058
2059 module := builder.AddPrebuiltModule(member, "java_test_import")
2060 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2061 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2062}
2063
Colin Cross1b16b0e2019-02-12 14:41:32 -08002064// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2065// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2066//
2067// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2068// compiled against the device bootclasspath.
2069//
2070// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2071// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002072func TestFactory() android.Module {
2073 module := &Test{}
2074
2075 module.AddProperties(
2076 &module.Module.properties,
2077 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002078 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002079 &module.Module.protoProperties,
2080 &module.testProperties)
2081
Colin Cross9ae1b922018-06-26 17:59:05 -07002082 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002083 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002084
Colin Cross05638fc2018-04-09 18:40:24 -07002085 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002086 return module
2087}
2088
Paul Duffin42df1442019-03-20 12:45:53 +00002089// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2090func TestHelperLibraryFactory() android.Module {
2091 module := &TestHelperLibrary{}
2092
2093 module.AddProperties(
2094 &module.Module.properties,
2095 &module.Module.deviceProperties,
2096 &module.Module.dexpreoptProperties,
2097 &module.Module.protoProperties,
2098 &module.testHelperLibraryProperties)
2099
Colin Cross9a4abed2019-04-24 13:19:28 -07002100 module.Module.properties.Installable = proptools.BoolPtr(true)
2101 module.Module.dexpreopter.isTest = true
2102
Paul Duffin42df1442019-03-20 12:45:53 +00002103 InitJavaModule(module, android.HostAndDeviceSupported)
2104 return module
2105}
2106
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002107// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2108// and makes sure that it is added to the appropriate test suite.
2109//
2110// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2111// compiled against an Android classpath.
2112//
2113// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2114// for host modules.
2115func JavaTestImportFactory() android.Module {
2116 module := &JavaTestImport{}
2117
2118 module.AddProperties(
2119 &module.Import.properties,
2120 &module.prebuiltTestProperties)
2121
2122 module.Import.properties.Installable = proptools.BoolPtr(true)
2123
2124 android.InitPrebuiltModule(module, &module.properties.Jars)
2125 android.InitApexModule(module)
2126 android.InitSdkAwareModule(module)
2127 InitJavaModule(module, android.HostAndDeviceSupported)
2128 return module
2129}
2130
Colin Cross1b16b0e2019-02-12 14:41:32 -08002131// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2132// allow running the test with `atest` or a `TEST_MAPPING` file.
2133//
2134// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2135// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002136func TestHostFactory() android.Module {
2137 module := &Test{}
2138
2139 module.AddProperties(
2140 &module.Module.properties,
2141 &module.Module.protoProperties,
2142 &module.testProperties)
2143
Colin Cross9ae1b922018-06-26 17:59:05 -07002144 module.Module.properties.Installable = proptools.BoolPtr(true)
2145
Colin Cross05638fc2018-04-09 18:40:24 -07002146 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002147 return module
2148}
2149
2150//
Colin Cross2fe66872015-03-30 17:20:39 -07002151// Java Binaries (.jar file plus wrapper script)
2152//
2153
Colin Crossf506d872017-07-19 15:53:04 -07002154type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002155 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002156 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002157
2158 // Name of the class containing main to be inserted into the manifest as Main-Class.
2159 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002160}
2161
Colin Crossf506d872017-07-19 15:53:04 -07002162type Binary struct {
2163 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002164
Colin Crossf506d872017-07-19 15:53:04 -07002165 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002166
Colin Cross6b4a32d2017-12-05 13:42:45 -08002167 isWrapperVariant bool
2168
Colin Crossc3315992017-12-08 19:12:36 -08002169 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002170 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002171}
2172
Alex Light24237172017-10-26 09:46:21 -07002173func (j *Binary) HostToolPath() android.OptionalPath {
2174 return android.OptionalPathForPath(j.binaryFile)
2175}
2176
Colin Crossf506d872017-07-19 15:53:04 -07002177func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002178 if ctx.Arch().ArchType == android.Common {
2179 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002180 if j.binaryProperties.Main_class != nil {
2181 if j.properties.Manifest != nil {
2182 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2183 }
2184 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2185 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2186 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2187 }
2188
Colin Cross6b4a32d2017-12-05 13:42:45 -08002189 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002190 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002191 // Handle the binary wrapper
2192 j.isWrapperVariant = true
2193
Colin Cross366938f2017-12-11 16:29:02 -08002194 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002195 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002196 } else {
2197 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2198 }
2199
2200 // Depend on the installed jar so that the wrapper doesn't get executed by
2201 // another build rule before the jar has been installed.
2202 jarFile := ctx.PrimaryModule().(*Binary).installFile
2203
2204 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2205 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002206 }
Colin Cross2fe66872015-03-30 17:20:39 -07002207}
2208
Colin Crossf506d872017-07-19 15:53:04 -07002209func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002210 if ctx.Arch().ArchType == android.Common {
2211 j.deps(ctx)
2212 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002213}
2214
Colin Cross1b16b0e2019-02-12 14:41:32 -08002215// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2216// as well.
2217//
2218// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2219// compiled against the device bootclasspath.
2220//
2221// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2222// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002223func BinaryFactory() android.Module {
2224 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002225
Colin Cross36242852017-06-23 15:06:31 -07002226 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002227 &module.Module.properties,
2228 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002229 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002230 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002231 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002232
Colin Cross9ae1b922018-06-26 17:59:05 -07002233 module.Module.properties.Installable = proptools.BoolPtr(true)
2234
Colin Cross6b4a32d2017-12-05 13:42:45 -08002235 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2236 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002237 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002238}
2239
Colin Cross1b16b0e2019-02-12 14:41:32 -08002240// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2241//
2242// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2243// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002244func BinaryHostFactory() android.Module {
2245 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002246
Colin Cross36242852017-06-23 15:06:31 -07002247 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002248 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002249 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002250 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002251
Colin Cross9ae1b922018-06-26 17:59:05 -07002252 module.Module.properties.Installable = proptools.BoolPtr(true)
2253
Colin Cross6b4a32d2017-12-05 13:42:45 -08002254 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2255 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002256 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002257}
2258
2259//
2260// Java prebuilts
2261//
2262
Colin Cross74d73e22017-08-02 11:05:49 -07002263type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002264 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002265
Nan Zhangea568a42017-11-08 21:20:04 -08002266 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002267
2268 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002269
2270 // List of shared java libs that this module has dependencies to
2271 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002272
2273 // List of files to remove from the jar file(s)
2274 Exclude_files []string
2275
2276 // List of directories to remove from the jar file(s)
2277 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002278
2279 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002280 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002281
2282 // set the name of the output
2283 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002284}
2285
2286type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002287 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002288 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002289 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002290 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002291 android.SdkBase
Martin Stjernholm335d5962020-01-11 00:37:30 +00002292 dexpreopt.DexPreoptModule
Colin Cross2fe66872015-03-30 17:20:39 -07002293
Colin Cross74d73e22017-08-02 11:05:49 -07002294 properties ImportProperties
2295
Colin Cross0a6e0072017-08-30 14:24:55 -07002296 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002297 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002298}
2299
Colin Cross83bb3162018-06-25 15:48:06 -07002300func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002301 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002302}
2303
2304func (j *Import) minSdkVersion() string {
2305 return j.sdkVersion()
2306}
2307
Colin Cross74d73e22017-08-02 11:05:49 -07002308func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002309 return &j.prebuilt
2310}
2311
Colin Cross74d73e22017-08-02 11:05:49 -07002312func (j *Import) PrebuiltSrcs() []string {
2313 return j.properties.Jars
2314}
2315
2316func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002317 return j.prebuilt.Name(j.ModuleBase.Name())
2318}
2319
Jiyong Park0b238752019-10-29 11:23:10 +09002320func (j *Import) Stem() string {
2321 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2322}
2323
Jiyong Park618922e2020-01-08 13:35:43 +09002324func (a *Import) JacocoReportClassesFile() android.Path {
2325 return nil
2326}
2327
Colin Cross74d73e22017-08-02 11:05:49 -07002328func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002329 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002330}
2331
Colin Cross74d73e22017-08-02 11:05:49 -07002332func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002333 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002334
Jiyong Park0b238752019-10-29 11:23:10 +09002335 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002336 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002337 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2338 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002339 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002340 inputFile := outputFile
2341 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2342 TransformJetifier(ctx, outputFile, inputFile)
2343 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002344 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002345
2346 ctx.VisitDirectDeps(func(module android.Module) {
2347 otherName := ctx.OtherModuleName(module)
2348 tag := ctx.OtherModuleDependencyTag(module)
2349
2350 switch dep := module.(type) {
2351 case Dependency:
2352 switch tag {
2353 case libTag, staticLibTag:
2354 // sdk lib names from dependencies are re-exported
2355 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2356 }
2357 case SdkLibraryDependency:
2358 switch tag {
2359 case libTag:
2360 // names of sdk libs that are directly depended are exported
2361 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2362 }
2363 }
2364 })
2365
2366 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002367 if Bool(j.properties.Installable) {
2368 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002369 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002370 }
Colin Cross2fe66872015-03-30 17:20:39 -07002371}
2372
Colin Cross74d73e22017-08-02 11:05:49 -07002373var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002374
Nan Zhanged19fc32017-10-19 13:06:22 -07002375func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002376 if j.combinedClasspathFile == nil {
2377 return nil
2378 }
Colin Cross37f6d792018-07-12 12:28:41 -07002379 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002380}
2381
2382func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002383 if j.combinedClasspathFile == nil {
2384 return nil
2385 }
Colin Cross37f6d792018-07-12 12:28:41 -07002386 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002387}
2388
Colin Cross331a1212018-08-15 20:40:52 -07002389func (j *Import) ResourceJars() android.Paths {
2390 return nil
2391}
2392
2393func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002394 if j.combinedClasspathFile == nil {
2395 return nil
2396 }
Colin Cross331a1212018-08-15 20:40:52 -07002397 return android.Paths{j.combinedClasspathFile}
2398}
2399
Colin Crossf24a22a2019-01-31 14:12:44 -08002400func (j *Import) DexJar() android.Path {
2401 return nil
2402}
2403
Colin Cross74d73e22017-08-02 11:05:49 -07002404func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002405 return nil
2406}
2407
Jiyong Park1be96912018-05-28 18:02:19 +09002408func (j *Import) ExportedSdkLibs() []string {
2409 return j.exportedSdkLibs
2410}
2411
Artur Satayev9cf46692019-11-26 18:08:34 +00002412func (j *Import) ExportedPlugins() (android.Paths, []string) {
2413 return nil, nil
2414}
2415
Colin Cross0c4ce212019-05-03 15:28:19 -07002416func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2417 return nil, nil
2418}
2419
albaltai36ff7dc2018-12-25 14:35:23 +08002420// Add compile time check for interface implementation
2421var _ android.IDEInfo = (*Import)(nil)
2422var _ android.IDECustomizedModuleName = (*Import)(nil)
2423
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002424// Collect information for opening IDE project files in java/jdeps.go.
2425const (
2426 removedPrefix = "prebuilt_"
2427)
2428
2429func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2430 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2431}
2432
2433func (j *Import) IDECustomizedModuleName() string {
2434 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2435 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2436 // solution to get the Import name.
2437 name := j.Name()
2438 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002439 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002440 }
2441 return name
2442}
2443
Colin Cross74d73e22017-08-02 11:05:49 -07002444var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002445
Colin Cross1b16b0e2019-02-12 14:41:32 -08002446// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2447//
2448// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2449// compiled against an Android classpath.
2450//
2451// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2452// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002453func ImportFactory() android.Module {
2454 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002455
Colin Cross74d73e22017-08-02 11:05:49 -07002456 module.AddProperties(&module.properties)
2457
2458 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002459 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002460 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002461 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002462 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002463}
2464
Colin Cross1b16b0e2019-02-12 14:41:32 -08002465// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2466// module.
2467//
2468// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2469// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002470func ImportFactoryHost() android.Module {
2471 module := &Import{}
2472
2473 module.AddProperties(&module.properties)
2474
2475 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002476 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002477 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002478 return module
2479}
2480
Colin Cross42be7612019-02-21 18:12:14 -08002481// dex_import module
2482
2483type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002484 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002485
2486 // set the name of the output
2487 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002488}
2489
2490type DexImport struct {
2491 android.ModuleBase
2492 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002493 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002494 prebuilt android.Prebuilt
Martin Stjernholm335d5962020-01-11 00:37:30 +00002495 dexpreopt.DexPreoptModule
Colin Cross42be7612019-02-21 18:12:14 -08002496
2497 properties DexImportProperties
2498
2499 dexJarFile android.Path
2500 maybeStrippedDexJarFile android.Path
2501
2502 dexpreopter
2503}
2504
2505func (j *DexImport) Prebuilt() *android.Prebuilt {
2506 return &j.prebuilt
2507}
2508
2509func (j *DexImport) PrebuiltSrcs() []string {
2510 return j.properties.Jars
2511}
2512
2513func (j *DexImport) Name() string {
2514 return j.prebuilt.Name(j.ModuleBase.Name())
2515}
2516
Jiyong Park0b238752019-10-29 11:23:10 +09002517func (j *DexImport) Stem() string {
2518 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2519}
2520
Colin Cross42be7612019-02-21 18:12:14 -08002521func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2522 if len(j.properties.Jars) != 1 {
2523 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2524 }
2525
Jiyong Park0b238752019-10-29 11:23:10 +09002526 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002527 j.dexpreopter.isInstallable = true
2528 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2529
2530 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2531 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2532
2533 if j.dexpreopter.uncompressedDex {
2534 rule := android.NewRuleBuilder()
2535
2536 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2537 rule.Temporary(temporary)
2538
2539 // use zip2zip to uncompress classes*.dex files
2540 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002541 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002542 FlagWithInput("-i ", inputJar).
2543 FlagWithOutput("-o ", temporary).
2544 FlagWithArg("-0 ", "'classes*.dex'")
2545
2546 // use zipalign to align uncompressed classes*.dex files
2547 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002548 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002549 Flag("-f").
2550 Text("4").
2551 Input(temporary).
2552 Output(dexOutputFile)
2553
2554 rule.DeleteTemporaryFiles()
2555
2556 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2557 } else {
2558 ctx.Build(pctx, android.BuildParams{
2559 Rule: android.Cp,
2560 Input: inputJar,
2561 Output: dexOutputFile,
2562 })
2563 }
2564
2565 j.dexJarFile = dexOutputFile
2566
2567 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2568
2569 j.maybeStrippedDexJarFile = dexOutputFile
2570
2571 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2572 ctx.ModuleName()+".jar", dexOutputFile)
2573}
2574
2575func (j *DexImport) DexJar() android.Path {
2576 return j.dexJarFile
2577}
2578
2579// dex_import imports a `.jar` file containing classes.dex files.
2580//
2581// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2582// to the device.
2583func DexImportFactory() android.Module {
2584 module := &DexImport{}
2585
2586 module.AddProperties(&module.properties)
2587
2588 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002589 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002590 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002591 return module
2592}
2593
Colin Cross89536d42017-07-07 14:35:50 -07002594//
2595// Defaults
2596//
2597type Defaults struct {
2598 android.ModuleBase
2599 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002600 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002601}
2602
Colin Cross1b16b0e2019-02-12 14:41:32 -08002603// java_defaults provides a set of properties that can be inherited by other java or android modules.
2604//
2605// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2606// property in the defaults module that exists in the depending module will be prepended to the depending module's
2607// value for that property.
2608//
2609// Example:
2610//
2611// java_defaults {
2612// name: "example_defaults",
2613// srcs: ["common/**/*.java"],
2614// javacflags: ["-Xlint:all"],
2615// aaptflags: ["--auto-add-overlay"],
2616// }
2617//
2618// java_library {
2619// name: "example",
2620// defaults: ["example_defaults"],
2621// srcs: ["example/**/*.java"],
2622// }
2623//
2624// is functionally identical to:
2625//
2626// java_library {
2627// name: "example",
2628// srcs: [
2629// "common/**/*.java",
2630// "example/**/*.java",
2631// ],
2632// javacflags: ["-Xlint:all"],
2633// }
Colin Cross89536d42017-07-07 14:35:50 -07002634func defaultsFactory() android.Module {
2635 return DefaultsFactory()
2636}
2637
Paul Duffin47357662019-12-05 14:07:14 +00002638func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002639 module := &Defaults{}
2640
Colin Cross89536d42017-07-07 14:35:50 -07002641 module.AddProperties(
2642 &CompilerProperties{},
2643 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002644 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002645 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002646 &aaptProperties{},
2647 &androidLibraryProperties{},
2648 &appProperties{},
2649 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002650 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002651 &ImportProperties{},
2652 &AARImportProperties{},
2653 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002654 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002655 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002656 )
2657
2658 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002659 return module
2660}
Nan Zhangea568a42017-11-08 21:20:04 -08002661
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002662func kytheExtractJavaFactory() android.Singleton {
2663 return &kytheExtractJavaSingleton{}
2664}
2665
2666type kytheExtractJavaSingleton struct {
2667}
2668
2669func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2670 var xrefTargets android.Paths
2671 ctx.VisitAllModules(func(module android.Module) {
2672 if javaModule, ok := module.(xref); ok {
2673 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2674 }
2675 })
2676 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2677 if len(xrefTargets) > 0 {
2678 ctx.Build(pctx, android.BuildParams{
2679 Rule: blueprint.Phony,
2680 Output: android.PathForPhony(ctx, "xref_java"),
2681 Inputs: xrefTargets,
2682 })
2683 }
2684}
2685
Nan Zhangea568a42017-11-08 21:20:04 -08002686var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002687var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002688var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002689var inList = android.InList