blob: a48b5a3004dd101f39cd28cd30a694c16ed756f9 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
39 // Register sdk member types.
40 android.RegisterSdkMemberType(&headerLibrarySdkMemberType{
41 librarySdkMemberType{
42 android.SdkMemberTypeBase{
43 PropertyName: "java_header_libs",
Paul Duffine6029182019-12-16 17:43:48 +000044 SupportsSdk: true,
Paul Duffin255f18e2019-12-13 11:22:16 +000045 },
46 },
47 })
48
49 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
50 librarySdkMemberType{
51 android.SdkMemberTypeBase{
52 PropertyName: "java_libs",
53 },
54 },
55 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000056
57 android.RegisterSdkMemberType(&testSdkMemberType{
58 SdkMemberTypeBase: android.SdkMemberTypeBase{
59 PropertyName: "java_tests",
60 },
61 })
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Paul Duffinf9b1da02019-12-18 19:51:55 +000064func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
65 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
66
67 ctx.RegisterModuleType("java_library", LibraryFactory)
68 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
69 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
70 ctx.RegisterModuleType("java_binary", BinaryFactory)
71 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
72 ctx.RegisterModuleType("java_test", TestFactory)
73 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
74 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000075 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 ctx.RegisterModuleType("java_import", ImportFactory)
77 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
78 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
79 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
80 ctx.RegisterModuleType("dex_import", DexImportFactory)
81
82 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
83 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
84}
85
Jeongik Cha2cc570d2019-10-29 15:44:45 +090086func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
87 if j.SocSpecific() || j.DeviceSpecific() ||
88 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
89 if sc, ok := ctx.Module().(sdkContext); ok {
90 if sc.sdkVersion() == "" {
91 ctx.PropertyErrorf("sdk_version",
92 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
93 }
94 }
95 }
96}
97
Jeongik Cha538c0d02019-07-11 15:54:27 +090098func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
99 if sc, ok := ctx.Module().(sdkContext); ok {
100 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
101 if usePlatformAPI != (sc.sdkVersion() == "") {
102 if usePlatformAPI {
103 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
104 } else {
105 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
106 }
107 }
108
109 }
110}
111
Colin Cross2fe66872015-03-30 17:20:39 -0700112// TODO:
113// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700114// Renderscript
115// Post-jar passes:
116// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700117// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700118// DroidDoc
119// Findbugs
120
Colin Cross89536d42017-07-07 14:35:50 -0700121type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700122 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
123 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800124 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700125
126 // list of source files that should not be used to build the Java module.
127 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800128 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700129
130 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700131 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700132
Colin Cross86a63ff2017-09-27 17:33:10 -0700133 // list of directories that should be excluded from java_resource_dirs
134 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700135
Colin Cross0f37af02017-09-27 17:42:05 -0700136 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800137 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700138
Colin Crosscedd4762018-09-13 11:26:19 -0700139 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800140 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700141
Colin Cross7d5136f2015-05-11 13:39:40 -0700142 // list of module-specific flags that will be used for javac compiles
143 Javacflags []string `android:"arch_variant"`
144
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200145 // list of module-specific flags that will be used for kotlinc compiles
146 Kotlincflags []string `android:"arch_variant"`
147
Colin Cross7d5136f2015-05-11 13:39:40 -0700148 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700149 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700150
151 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700152 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700153
154 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800155 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700156
Colin Cross540eff82017-06-22 17:01:52 -0700157 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800158 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700159
160 // If not blank, set the java version passed to javac as -source and -target
161 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700162
Colin Cross9ae1b922018-06-26 17:59:05 -0700163 // If set to true, allow this module to be dexed and installed on devices. Has no
164 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700165 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700166
Colin Cross0f37af02017-09-27 17:42:05 -0700167 // If set to true, include sources used to compile the module in to the final jar
168 Include_srcs *bool
169
Vladimir Marko0975ee02019-04-02 10:29:55 +0100170 // If not empty, classes are restricted to the specified packages and their sub-packages.
171 // This restriction is checked after applying jarjar rules and including static libs.
172 Permitted_packages []string
173
Colin Crossbe9cdb82019-01-21 21:37:16 -0800174 // List of modules to use as annotation processors
175 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700176
Artur Satayev9cf46692019-11-26 18:08:34 +0000177 // List of modules to export to libraries that directly depend on this library as annotation processors
178 Exported_plugins []string
179
Nan Zhang61eaedb2017-11-02 13:28:15 -0700180 // The number of Java source entries each Javac instance can process
181 Javac_shard_size *int64
182
Nan Zhang5f8cb422018-02-06 10:34:32 -0800183 // Add host jdk tools.jar to bootclasspath
184 Use_tools_jar *bool
185
Colin Cross1369cdb2017-09-29 17:58:17 -0700186 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700187 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800188 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700189
Colin Cross6cef4812019-10-17 14:23:50 -0700190 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700191 Javacflags []string
192 }
Colin Crosscb933592017-11-22 13:49:43 -0800193
Colin Cross81440082018-08-15 20:21:55 -0700194 // When compiling language level 9+ .java code in packages that are part of
195 // a system module, patch_module names the module that your sources and
196 // dependencies should be patched into. The Android runtime currently
197 // doesn't implement the JEP 261 module system so this option is only
198 // supported at compile time. It should only be needed to compile tests in
199 // packages that exist in libcore and which are inconvenient to move
200 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100201 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700202
Colin Crosscb933592017-11-22 13:49:43 -0800203 Jacoco struct {
204 // List of classes to include for instrumentation with jacoco to collect coverage
205 // information at runtime when building with coverage enabled. If unset defaults to all
206 // classes.
207 // Supports '*' as the last character of an entry in the list as a wildcard match.
208 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
209 // it matches classes in the package that have the class name as a prefix.
210 Include_filter []string
211
212 // List of classes to exclude from instrumentation with jacoco to collect coverage
213 // information at runtime when building with coverage enabled. Overrides classes selected
214 // by the include_filter property.
215 // Supports '*' as the last character of an entry in the list as a wildcard match.
216 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
217 // it matches classes in the package that have the class name as a prefix.
218 Exclude_filter []string
219 }
220
Andreas Gampef3e5b552018-01-22 21:27:21 -0800221 Errorprone struct {
222 // List of javac flags that should only be used when running errorprone.
223 Javacflags []string
224 }
225
Colin Cross0f2ee152017-12-14 15:22:43 -0800226 Proto struct {
227 // List of extra options that will be passed to the proto generator.
228 Output_params []string
229 }
230
Colin Crosscb933592017-11-22 13:49:43 -0800231 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800232
233 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800234 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700235}
236
Colin Cross89536d42017-07-07 14:35:50 -0700237type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700238 // list of module-specific flags that will be used for dex compiles
239 Dxflags []string `android:"arch_variant"`
240
Jeongik Cha538c0d02019-07-11 15:54:27 +0900241 // if not blank, set to the version of the sdk to compile against.
242 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800243 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700244
Colin Cross83bb3162018-06-25 15:48:06 -0700245 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
246 // Defaults to sdk_version if not set.
247 Min_sdk_version *string
248
Dan Willemsen419290a2018-10-31 15:28:47 -0700249 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
250 // Defaults to sdk_version if not set.
251 Target_sdk_version *string
252
Jeongik Cha356dac42019-08-19 14:09:52 +0900253 // Whether to compile against the platform APIs instead of an SDK.
254 // If true, then sdk_version must be empty. The value of this field
255 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700256 Platform_apis *bool
257
Colin Crossebe1a512017-11-14 13:12:14 -0800258 Aidl struct {
259 // Top level directories to pass to aidl tool
260 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700261
Colin Crossebe1a512017-11-14 13:12:14 -0800262 // Directories rooted at the Android.bp file to pass to aidl tool
263 Local_include_dirs []string
264
265 // directories that should be added as include directories for any aidl sources of modules
266 // that depend on this module, as well as to aidl for this module.
267 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100268
269 // whether to generate traces (for systrace) for this interface
270 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100271
272 // whether to generate Binder#GetTransaction name method.
273 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800274 }
Colin Cross92430102017-10-09 14:59:32 -0700275
276 // If true, export a copy of the module as a -hostdex module for host testing.
277 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700278
Colin Cross7f87f4f2019-04-24 13:41:45 -0700279 Target struct {
280 Hostdex struct {
281 // Additional required dependencies to add to -hostdex modules.
282 Required []string
283 }
284 }
285
David Brazdil17ef5632018-06-27 10:27:45 +0100286 // If set to true, compile dex regardless of installable. Defaults to false.
287 Compile_dex *bool
288
Colin Cross66dbc0b2017-12-28 12:23:20 -0800289 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700290 // If false, disable all optimization. Defaults to true for android_app and android_test
291 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800292 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700293 // True if the module containing this has it set by default.
294 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800295
296 // If true, optimize for size by removing unused code. Defaults to true for apps,
297 // false for libraries and tests.
298 Shrink *bool
299
300 // If true, optimize bytecode. Defaults to false.
301 Optimize *bool
302
303 // If true, obfuscate bytecode. Defaults to false.
304 Obfuscate *bool
305
306 // If true, do not use the flag files generated by aapt that automatically keep
307 // classes referenced by the app manifest. Defaults to false.
308 No_aapt_flags *bool
309
310 // Flags to pass to proguard.
311 Proguard_flags []string
312
313 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800314 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800315 }
316
Paul Duffine25c6442019-10-11 13:50:28 +0100317 // When targeting 1.9 and above, override the modules to use with --system,
318 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700319 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700320
Jiyong Park4c4c0242019-10-21 14:53:15 +0900321 // set the name of the output
322 Stem *string
323
Colin Cross5a0dcd52018-10-05 14:20:06 -0700324 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800325 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700326}
327
Sasha Smundak2057f822019-04-16 17:16:58 -0700328func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
329 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
330}
331
Colin Cross46c9b8b2017-06-22 16:51:17 -0700332// Module contains the properties and members used by all java module types
333type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700334 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700335 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900336 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900337 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700338
Colin Cross89536d42017-07-07 14:35:50 -0700339 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700340 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700341 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700342
Colin Cross331a1212018-08-15 20:40:52 -0700343 // jar file containing header classes including static library dependencies, suitable for
344 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700345 headerJarFile android.Path
346
Colin Cross331a1212018-08-15 20:40:52 -0700347 // jar file containing implementation classes including static library dependencies but no
348 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700349 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700350
Colin Cross331a1212018-08-15 20:40:52 -0700351 // jar file containing only resources including from static library dependencies
352 resourceJar android.Path
353
Colin Cross0c4ce212019-05-03 15:28:19 -0700354 // args and dependencies to package source files into a srcjar
355 srcJarArgs []string
356 srcJarDeps android.Paths
357
Colin Cross331a1212018-08-15 20:40:52 -0700358 // jar file containing implementation classes and resources including static library
359 // dependencies
360 implementationAndResourcesJar android.Path
361
362 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700363 dexJarFile android.Path
364
Colin Cross43f08db2018-11-12 10:13:39 -0800365 // output file that contains classes.dex if it should be in the output file
366 maybeStrippedDexJarFile android.Path
367
Colin Crosscb933592017-11-22 13:49:43 -0800368 // output file containing uninstrumented classes that will be instrumented by jacoco
369 jacocoReportClassesFile android.Path
370
Colin Cross66dbc0b2017-12-28 12:23:20 -0800371 // output file containing mapping of obfuscated names
372 proguardDictionary android.Path
373
Colin Cross331a1212018-08-15 20:40:52 -0700374 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700375 outputFile android.Path
376 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700377
Colin Cross635c3b02016-05-18 15:37:25 -0700378 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700379
Colin Cross635c3b02016-05-18 15:37:25 -0700380 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700381
Colin Cross2fe66872015-03-30 17:20:39 -0700382 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700383 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800384
385 // list of .java files and srcjars that was passed to javac
386 compiledJavaSrcs android.Paths
387 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800388
389 // list of extra progurad flag files
390 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900391
Colin Cross094054a2018-10-17 15:10:48 -0700392 // manifest file to use instead of properties.Manifest
393 overrideManifest android.OptionalPath
394
Artur Satayev9cf46692019-11-26 18:08:34 +0000395 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900396 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700397
Artur Satayev9cf46692019-11-26 18:08:34 +0000398 // list of plugins that this java module is exporting
399 exportedPluginJars android.Paths
400
401 // list of plugins that this java module is exporting
402 exportedPluginClasses []string
403
404 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800405 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700406 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800407
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800408 // expanded Jarjar_rules
409 expandJarjarRules android.Path
410
Vladimir Marko0975ee02019-04-02 10:29:55 +0100411 // list of additional targets for checkbuild
412 additionalCheckedModules android.Paths
413
Colin Cross988708c2019-05-06 14:04:11 -0700414 // Extra files generated by the module type to be added as java resources.
415 extraResources android.Paths
416
Colin Crossf24a22a2019-01-31 14:12:44 -0800417 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800418 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800419
420 // list of the xref extraction files
421 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700422}
423
Colin Cross41955e82019-05-29 14:40:35 -0700424func (j *Module) OutputFiles(tag string) (android.Paths, error) {
425 switch tag {
426 case "":
427 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700428 case ".jar":
429 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700430 case ".proguard_map":
431 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700432 default:
433 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
434 }
Colin Cross54250902017-12-05 09:28:08 -0800435}
436
Colin Cross41955e82019-05-29 14:40:35 -0700437var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800438
Colin Crossf506d872017-07-19 15:53:04 -0700439type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700440 HeaderJars() android.Paths
441 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700442 ResourceJars() android.Paths
443 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800444 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700445 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900446 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000447 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700448 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700449 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700450}
451
Jiyong Parkc678ad32018-04-10 13:07:10 +0900452type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700453 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
454 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455}
456
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800457type xref interface {
458 XrefJavaFiles() android.Paths
459}
460
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800461func (j *Module) XrefJavaFiles() android.Paths {
462 return j.kytheFiles
463}
464
Colin Cross89536d42017-07-07 14:35:50 -0700465func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
466 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
467 android.InitDefaultableModule(module)
468}
469
Colin Crossbe1da472017-07-07 15:59:46 -0700470type dependencyTag struct {
471 blueprint.BaseDependencyTag
472 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700473}
474
Colin Crossa4f08812018-10-02 22:03:40 -0700475type jniDependencyTag struct {
476 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700477}
478
Jiyong Park8be103b2019-11-08 15:53:48 +0900479func IsJniDepTag(depTag blueprint.DependencyTag) bool {
480 _, ok := depTag.(*jniDependencyTag)
481 return ok
482}
483
Colin Crossbe1da472017-07-07 15:59:46 -0700484var (
Colin Cross4b964c02018-10-15 16:18:06 -0700485 staticLibTag = dependencyTag{name: "staticlib"}
486 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700487 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800488 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000489 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700490 bootClasspathTag = dependencyTag{name: "bootclasspath"}
491 systemModulesTag = dependencyTag{name: "system modules"}
492 frameworkResTag = dependencyTag{name: "framework-res"}
493 frameworkApkTag = dependencyTag{name: "framework-apk"}
494 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800495 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700496 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
497 certificateTag = dependencyTag{name: "certificate"}
498 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700499 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700500)
Colin Cross2fe66872015-03-30 17:20:39 -0700501
Colin Crossfc3674a2017-09-18 17:41:52 -0700502type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700503 useModule, useFiles, useDefaultLibs, invalidVersion bool
504
Colin Cross6cef4812019-10-17 14:23:50 -0700505 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
506 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100507
508 // The default system modules to use. Will be an empty string if no system
509 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700510 systemModules string
511
Colin Cross6cef4812019-10-17 14:23:50 -0700512 // The modules that will be added ot the classpath when targeting 1.9 or higher
513 java9Classpath []string
514
Colin Crossa97c5d32018-03-28 14:58:31 -0700515 frameworkResModule string
516
Colin Cross86a60ae2018-05-29 14:44:55 -0700517 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700518 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100519
520 noStandardLibs, noFrameworksLibs bool
521}
522
523func (s sdkDep) hasStandardLibs() bool {
524 return !s.noStandardLibs
525}
526
527func (s sdkDep) hasFrameworkLibs() bool {
528 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700529}
530
Colin Crossa4f08812018-10-02 22:03:40 -0700531type jniLib struct {
532 name string
533 path android.Path
534 target android.Target
535}
536
Colin Cross0ea8ba82019-06-06 14:33:29 -0700537func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800538 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
539}
540
Colin Cross0ea8ba82019-06-06 14:33:29 -0700541func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800542 return j.shouldInstrument(ctx) &&
543 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
544 ctx.Config().UnbundledBuild())
545}
546
Colin Cross83bb3162018-06-25 15:48:06 -0700547func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900548 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700549}
550
Paul Duffine25c6442019-10-11 13:50:28 +0100551func (j *Module) systemModules() string {
552 return proptools.String(j.deviceProperties.System_modules)
553}
554
Colin Cross83bb3162018-06-25 15:48:06 -0700555func (j *Module) minSdkVersion() string {
556 if j.deviceProperties.Min_sdk_version != nil {
557 return *j.deviceProperties.Min_sdk_version
558 }
559 return j.sdkVersion()
560}
561
Dan Willemsen419290a2018-10-31 15:28:47 -0700562func (j *Module) targetSdkVersion() string {
563 if j.deviceProperties.Target_sdk_version != nil {
564 return *j.deviceProperties.Target_sdk_version
565 }
566 return j.sdkVersion()
567}
568
Jiyong Parkb02bb402019-12-03 00:43:57 +0900569func (j *Module) AvailableFor(what string) bool {
570 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
571 // Exception: for hostdex: true libraries, the platform variant is created
572 // even if it's not marked as available to platform. In that case, the platform
573 // variant is used only for the hostdex and not installed to the device.
574 return true
575 }
576 return j.ApexModuleBase.AvailableFor(what)
577}
578
Colin Crossbe1da472017-07-07 15:59:46 -0700579func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700580 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100581 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700582 if sdkDep.useDefaultLibs {
583 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
584 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
585 if sdkDep.hasFrameworkLibs() {
586 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700587 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700588 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700589 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100590 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700591 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700592 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
593 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
594 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
595 }
Colin Cross2fe66872015-03-30 17:20:39 -0700596 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700597
Nan Zhangb2b33de2018-02-23 11:18:47 -0800598 if ctx.ModuleName() == "android_stubs_current" ||
599 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700600 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700601 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800602 }
Colin Cross2fe66872015-03-30 17:20:39 -0700603 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700604
Inseob Kimac1e9862019-12-09 18:15:47 +0900605 syspropPublicStubs := syspropPublicStubs(ctx.Config())
606
607 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
608 // and redirects dependency to public stub depending on the link type.
609 rewriteSyspropLibs := func(libs []string, prop string) []string {
610 // make a copy
611 ret := android.CopyOf(libs)
612
613 for idx, lib := range libs {
614 stub, ok := syspropPublicStubs[lib]
615
616 if !ok {
617 continue
618 }
619
620 linkType, _ := j.getLinkType(ctx.ModuleName())
621 if linkType == javaSystem {
622 ret[idx] = stub
623 } else if linkType != javaPlatform {
624 ctx.PropertyErrorf("sdk_version",
625 "can't link against sysprop_library %q from a module using public or core API",
626 lib)
627 }
628 }
629
630 return ret
631 }
632
633 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
634 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700635
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700636 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000637 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800638
Colin Crossfe17f6f2019-03-28 19:30:56 -0700639 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700640 if j.hasSrcExt(".proto") {
641 protoDeps(ctx, &j.protoProperties)
642 }
Colin Cross93e85952017-08-15 13:34:18 -0700643
644 if j.hasSrcExt(".kt") {
645 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
646 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700647 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
648 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800649 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800650 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
651 }
Colin Cross93e85952017-08-15 13:34:18 -0700652 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800653
654 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700655 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800656 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700657}
658
659func hasSrcExt(srcs []string, ext string) bool {
660 for _, src := range srcs {
661 if filepath.Ext(src) == ext {
662 return true
663 }
664 }
665
666 return false
667}
668
669func (j *Module) hasSrcExt(ext string) bool {
670 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700671}
672
Colin Cross46c9b8b2017-06-22 16:51:17 -0700673func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700674 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700675
Colin Crossebe1a512017-11-14 13:12:14 -0800676 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
677 aidlIncludes = append(aidlIncludes,
678 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
679 aidlIncludes = append(aidlIncludes,
680 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700681
Colin Cross3047fa22019-04-18 10:56:44 -0700682 var flags []string
683 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700684
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700685 if aidlPreprocess.Valid() {
686 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700687 deps = append(deps, aidlPreprocess.Path())
688 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700689 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700690 }
691
Colin Cross3047fa22019-04-18 10:56:44 -0700692 if len(j.exportAidlIncludeDirs) > 0 {
693 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
694 }
695
696 if len(aidlIncludes) > 0 {
697 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
698 }
699
Colin Cross635c3b02016-05-18 15:37:25 -0700700 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800701 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700702 flags = append(flags, "-I"+src.String())
703 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700704
Martijn Coeneneab15642018-03-09 09:29:59 +0100705 if Bool(j.deviceProperties.Aidl.Generate_traces) {
706 flags = append(flags, "-t")
707 }
708
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100709 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
710 flags = append(flags, "--transaction_names")
711 }
712
Colin Cross3047fa22019-04-18 10:56:44 -0700713 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700714}
715
Colin Cross32f676a2017-09-06 13:41:06 -0700716type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800717 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700718 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800719 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700720 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800721 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700722 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700723 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700724 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700725 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800726 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700727 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700728 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700729 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700730 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800731 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800732
733 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700734}
Colin Cross2fe66872015-03-30 17:20:39 -0700735
Colin Cross54250902017-12-05 09:28:08 -0800736func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
737 for _, f := range dep.Srcs() {
738 if f.Ext() != ".jar" {
739 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
740 ctx.OtherModuleName(dep.(blueprint.Module)))
741 }
742 }
743}
744
Jiyong Park2d492942018-03-05 17:44:10 +0900745type linkType int
746
747const (
748 javaCore linkType = iota
749 javaSdk
750 javaSystem
751 javaPlatform
752)
753
Jeongik Cha75b83b02019-11-01 15:28:00 +0900754type linkTypeContext interface {
755 android.Module
756 getLinkType(name string) (ret linkType, stubs bool)
757}
758
759func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700760 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700761 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900762 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
763 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100764 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900765 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100766 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900767 return javaCore, false
768 case name == "android_system_stubs_current":
769 return javaSystem, true
770 case strings.HasPrefix(ver, "system_"):
771 return javaSystem, false
772 case name == "android_test_stubs_current":
773 return javaSystem, true
774 case strings.HasPrefix(ver, "test_"):
775 return javaPlatform, false
776 case name == "android_stubs_current":
777 return javaSdk, true
778 case ver == "current":
779 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100780 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900781 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700782 default:
783 if _, err := strconv.Atoi(ver); err != nil {
784 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
785 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900786 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900787 }
788}
789
Jeongik Cha75b83b02019-11-01 15:28:00 +0900790func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700791 if ctx.Host() {
792 return
793 }
794
Jeongik Cha75b83b02019-11-01 15:28:00 +0900795 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900796 if stubs {
797 return
798 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900799 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900800 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."
801
802 switch myLinkType {
803 case javaCore:
804 if otherLinkType != javaCore {
805 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 +0900806 ctx.OtherModuleName(to))
807 }
Jiyong Park2d492942018-03-05 17:44:10 +0900808 break
809 case javaSdk:
810 if otherLinkType != javaCore && otherLinkType != javaSdk {
811 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
812 ctx.OtherModuleName(to))
813 }
814 break
815 case javaSystem:
816 if otherLinkType == javaPlatform {
817 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
818 ctx.OtherModuleName(to))
819 }
820 break
821 case javaPlatform:
822 // no restriction on link-type
823 break
Jiyong Park750e5572018-01-31 00:20:13 +0900824 }
825}
826
Colin Cross32f676a2017-09-06 13:41:06 -0700827func (j *Module) collectDeps(ctx android.ModuleContext) deps {
828 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700829
Colin Cross300f0382018-03-06 13:11:51 -0800830 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700831 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800832 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700833 ctx.AddMissingDependencies(sdkDep.bootclasspath)
834 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800835 } else if sdkDep.useFiles {
836 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700837 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700838 deps.aidlPreprocess = sdkDep.aidl
839 } else {
840 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800841 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700842 }
843
Colin Crossd11fcda2017-10-23 17:59:01 -0700844 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700845 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700846 tag := ctx.OtherModuleDependencyTag(module)
847
Colin Crossa4f08812018-10-02 22:03:40 -0700848 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700849 // Handled by AndroidApp.collectAppDeps
850 return
851 }
852 if tag == certificateTag {
853 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700854 return
855 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900856 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900857 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900858 if to, ok := module.(linkTypeContext); ok {
859 switch tag {
860 case bootClasspathTag, libTag, staticLibTag:
861 checkLinkType(ctx, j, to, tag.(dependencyTag))
862 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700863 }
Jiyong Park750e5572018-01-31 00:20:13 +0900864 }
Colin Cross54250902017-12-05 09:28:08 -0800865 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800866 case SdkLibraryDependency:
867 switch tag {
868 case libTag:
869 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
870 // names of sdk libs that are directly depended are exported
871 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700872 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800873 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
874 }
Colin Cross54250902017-12-05 09:28:08 -0800875 case Dependency:
876 switch tag {
877 case bootClasspathTag:
878 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700879 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800880 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900881 // sdk lib names from dependencies are re-exported
882 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700883 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000884 pluginJars, pluginClasses := dep.ExportedPlugins()
885 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700886 case java9LibTag:
887 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800888 case staticLibTag:
889 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
890 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
891 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700892 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900893 // sdk lib names from dependencies are re-exported
894 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700895 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000896 pluginJars, pluginClasses := dep.ExportedPlugins()
897 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800898 case pluginTag:
899 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800900 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000901 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
902 } else {
903 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800904 }
905 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
906 } else {
907 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
908 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000909 case exportedPluginTag:
910 if plugin, ok := dep.(*Plugin); ok {
911 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
912 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
913 }
914 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
915 if plugin.pluginProperties.Processor_class != nil {
916 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
917 }
918 } else {
919 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
920 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800921 case frameworkApkTag:
922 if ctx.ModuleName() == "android_stubs_current" ||
923 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700924 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800925 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
926 // resource files out of there for aapt.
927 //
928 // Normally the package rule runs aapt, which includes the resource,
929 // but we're not running that in our package rule so just copy in the
930 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700931 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800932 }
Colin Cross54250902017-12-05 09:28:08 -0800933 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700934 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800935 case kotlinAnnotationsTag:
936 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800937 }
938
Colin Cross54250902017-12-05 09:28:08 -0800939 case android.SourceFileProducer:
940 switch tag {
941 case libTag:
942 checkProducesJars(ctx, dep)
943 deps.classpath = append(deps.classpath, dep.Srcs()...)
944 case staticLibTag:
945 checkProducesJars(ctx, dep)
946 deps.classpath = append(deps.classpath, dep.Srcs()...)
947 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
948 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800949 }
950 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700951 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100952 case bootClasspathTag:
953 // If a system modules dependency has been added to the bootclasspath
954 // then add its libs to the bootclasspath.
955 sm := module.(*SystemModules)
956 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
957
Colin Cross1369cdb2017-09-29 17:58:17 -0700958 case systemModulesTag:
959 if deps.systemModules != nil {
960 panic("Found two system module dependencies")
961 }
962 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000963 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700964 panic("Missing directory for system module dependency")
965 }
Colin Crossb77043e2019-07-16 13:57:13 -0700966 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700967 }
Colin Crossec7a0422017-07-07 14:47:12 -0700968 }
Colin Cross2fe66872015-03-30 17:20:39 -0700969 })
970
Jiyong Park1be96912018-05-28 18:02:19 +0900971 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
972
Colin Cross32f676a2017-09-06 13:41:06 -0700973 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700974}
975
Artur Satayev9cf46692019-11-26 18:08:34 +0000976func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
977 deps.processorPath = append(deps.processorPath, pluginJars...)
978 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
979}
980
Colin Cross1e743852019-10-28 11:37:20 -0700981func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800982 v := sdkContext.sdkVersion()
983 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100984 if ctx.Config().IsPdkBuild() &&
985 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700986 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800987 latestSdkVersion := 0
988 if len(sdkVersions) > 0 {
989 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
990 }
991 v = strconv.Itoa(latestSdkVersion)
992 }
993
994 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700995 if err != nil {
996 ctx.PropertyErrorf("sdk_version", "%s", err)
997 }
Nan Zhang357466b2018-04-17 17:38:36 -0700998 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700999 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001000 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001001 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001002 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001003 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001004 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1005 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001006 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001007 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001008 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001009 }
Nan Zhang357466b2018-04-17 17:38:36 -07001010}
1011
Colin Cross1e743852019-10-28 11:37:20 -07001012type javaVersion int
1013
1014const (
1015 JAVA_VERSION_UNSUPPORTED = 0
1016 JAVA_VERSION_6 = 6
1017 JAVA_VERSION_7 = 7
1018 JAVA_VERSION_8 = 8
1019 JAVA_VERSION_9 = 9
1020)
1021
1022func (v javaVersion) String() string {
1023 switch v {
1024 case JAVA_VERSION_6:
1025 return "1.6"
1026 case JAVA_VERSION_7:
1027 return "1.7"
1028 case JAVA_VERSION_8:
1029 return "1.8"
1030 case JAVA_VERSION_9:
1031 return "1.9"
1032 default:
1033 return "unsupported"
1034 }
1035}
1036
1037// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1038func (v javaVersion) usesJavaModules() bool {
1039 return v >= 9
1040}
1041
1042func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001043 switch javaVersion {
1044 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001045 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001046 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001047 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001048 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001049 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001050 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001051 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001052 case "10", "11":
1053 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001054 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001055 default:
1056 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001057 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001058 }
1059}
1060
Nan Zhanged19fc32017-10-19 13:06:22 -07001061func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001062
Colin Crossf03c82b2015-04-13 13:53:40 -07001063 var flags javaBuilderFlags
1064
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001065 // javaVersion flag.
1066 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1067
Nan Zhanged19fc32017-10-19 13:06:22 -07001068 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001069 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001070 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001071 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001072 }
Colin Cross6510f912017-11-29 00:27:14 -08001073 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001074 // Override the -g flag passed globally to remove local variable debug info to reduce
1075 // disk and memory usage.
1076 javacFlags = append(javacFlags, "-g:source,lines")
1077 }
Colin Crossc228a702019-11-06 16:18:05 -08001078 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001079
Colin Cross66548102018-06-19 22:47:35 -07001080 if ctx.Config().RunErrorProne() {
1081 if config.ErrorProneClasspath == nil {
1082 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1083 }
1084
1085 errorProneFlags := []string{
1086 "-Xplugin:ErrorProne",
1087 "${config.ErrorProneChecks}",
1088 }
1089 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1090
1091 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1092 "'" + strings.Join(errorProneFlags, " ") + "'"
1093 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001094 }
1095
Nan Zhanged19fc32017-10-19 13:06:22 -07001096 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001097 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1098 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001099 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001100 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001101
Colin Crossbe9cdb82019-01-21 21:37:16 -08001102 flags.processor = strings.Join(deps.processorClasses, ",")
1103
Colin Cross1e743852019-10-28 11:37:20 -07001104 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1105 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001106 // Give host-side tools a version of OpenJDK's standard libraries
1107 // close to what they're targeting. As of Dec 2017, AOSP is only
1108 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1109 //
1110 // When building with OpenJDK 8, the following should have no
1111 // effect since those jars would be available by default.
1112 //
1113 // When building with OpenJDK 9 but targeting a version < 1.8,
1114 // putting them on the bootclasspath means that:
1115 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1116 // b) references to existing APIs are not reinterpreted in an
1117 // OpenJDK 9-specific way, eg. calls to subclasses of
1118 // java.nio.Buffer as in http://b/70862583
1119 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1120 flags.bootClasspath = append(flags.bootClasspath,
1121 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1122 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001123 if Bool(j.properties.Use_tools_jar) {
1124 flags.bootClasspath = append(flags.bootClasspath,
1125 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1126 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001127 }
1128
Colin Cross1e743852019-10-28 11:37:20 -07001129 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001130 // Manually specify build directory in case it is not under the repo root.
1131 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1132 // just adding a symlink under the root doesn't help.)
1133 patchPaths := ".:" + ctx.Config().BuildDir()
1134 classPath := flags.classpath.FormJavaClassPath("")
1135 if classPath != "" {
1136 patchPaths += ":" + classPath
1137 }
1138 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001139 }
1140
Nan Zhanged19fc32017-10-19 13:06:22 -07001141 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001142 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001143
Nan Zhanged19fc32017-10-19 13:06:22 -07001144 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001145 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001146
Colin Cross81440082018-08-15 20:21:55 -07001147 if len(javacFlags) > 0 {
1148 // optimization.
1149 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1150 flags.javacFlags = "$javacFlags"
1151 }
1152
Nan Zhanged19fc32017-10-19 13:06:22 -07001153 return flags
1154}
Colin Crossc0b06f12015-04-08 13:03:43 -07001155
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001156func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001157 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001158
1159 deps := j.collectDeps(ctx)
1160 flags := j.collectBuilderFlags(ctx, deps)
1161
Colin Cross1e743852019-10-28 11:37:20 -07001162 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001163 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1164 }
Colin Cross8a497952019-03-05 22:25:09 -08001165 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001166 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001167 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001168 }
1169
Colin Crossaf050172017-11-15 23:01:59 -08001170 srcFiles = j.genSources(ctx, srcFiles, flags)
1171
1172 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001173 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001174 if aaptSrcJar != nil {
1175 srcJars = append(srcJars, aaptSrcJar)
1176 }
Colin Crossb7a63242015-04-16 14:09:14 -07001177
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001178 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001179 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001180 }
1181
Colin Cross1ee23172017-10-18 14:44:18 -07001182 jarName := ctx.ModuleName() + ".jar"
1183
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001184 javaSrcFiles := srcFiles.FilterByExt(".java")
1185 var uniqueSrcFiles android.Paths
1186 set := make(map[string]bool)
1187 for _, v := range javaSrcFiles {
1188 if _, found := set[v.String()]; !found {
1189 set[v.String()] = true
1190 uniqueSrcFiles = append(uniqueSrcFiles, v)
1191 }
1192 }
1193
patricktu242faad2019-09-24 15:41:30 +08001194 // Collect .java files for AIDEGen
1195 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1196
Colin Cross55f63ea2018-08-27 12:37:09 -07001197 var kotlinJars android.Paths
1198
Colin Cross93e85952017-08-15 13:34:18 -07001199 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001200 // user defined kotlin flags.
1201 kotlincFlags := j.properties.Kotlincflags
1202 CheckKotlincFlags(ctx, kotlincFlags)
1203
Colin Cross93e85952017-08-15 13:34:18 -07001204 // If there are kotlin files, compile them first but pass all the kotlin and java files
1205 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1206 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001207 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001208 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001209 kotlincFlags = append(kotlincFlags, "-no-jdk")
1210 }
1211 if len(kotlincFlags) > 0 {
1212 // optimization.
1213 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1214 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001215 }
1216
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001217 var kotlinSrcFiles android.Paths
1218 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1219 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1220
patricktu242faad2019-09-24 15:41:30 +08001221 // Collect .kt files for AIDEGen
1222 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1223
Colin Crossafbb1732019-01-17 15:42:52 -08001224 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1225 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1226
1227 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1228 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1229
1230 if len(flags.processorPath) > 0 {
1231 // Use kapt for annotation processing
1232 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1233 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1234 srcJars = append(srcJars, kaptSrcJar)
1235 // Disable annotation processing in javac, it's already been handled by kapt
1236 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001237 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001238 }
Colin Cross93e85952017-08-15 13:34:18 -07001239
Colin Cross1ee23172017-10-18 14:44:18 -07001240 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001241 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001242 if ctx.Failed() {
1243 return
1244 }
1245
1246 // Make javac rule depend on the kotlinc rule
1247 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001248
Colin Cross93e85952017-08-15 13:34:18 -07001249 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001250 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001251 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001252 }
1253
Colin Cross55f63ea2018-08-27 12:37:09 -07001254 jars := append(android.Paths(nil), kotlinJars...)
1255
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001256 // Store the list of .java files that was passed to javac
1257 j.compiledJavaSrcs = uniqueSrcFiles
1258 j.compiledSrcJars = srcJars
1259
Nan Zhang61eaedb2017-11-02 13:28:15 -07001260 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001261 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001262 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1263 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001264 // Formerly, there was a check here that prevented annotation processors
1265 // from being used when sharding was enabled, as some annotation processors
1266 // do not function correctly in sharded environments. It was removed to
1267 // allow for the use of annotation processors that do function correctly
1268 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001269 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001270 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001271 if ctx.Failed() {
1272 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001273 }
1274 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001275 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001276 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001277 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001278 // If error-prone is enabled, add an additional rule to compile the java files into
1279 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001280 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001281 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1282 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001283 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001284 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001285 extraJarDeps = append(extraJarDeps, errorprone)
1286 }
1287
Nan Zhang61eaedb2017-11-02 13:28:15 -07001288 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001289 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001290 shardSize := int(*(j.properties.Javac_shard_size))
1291 var shardSrcs []android.Paths
1292 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001293 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001294 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001295 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1296 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001297 jars = append(jars, classes)
1298 }
1299 }
1300 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001301 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1302 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001303 jars = append(jars, classes)
1304 }
1305 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001306 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001307 jars = append(jars, classes)
1308 }
Colin Crossd6891432017-09-27 17:39:56 -07001309 if ctx.Failed() {
1310 return
1311 }
Colin Cross2fe66872015-03-30 17:20:39 -07001312 }
1313
Colin Cross0c4ce212019-05-03 15:28:19 -07001314 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1315
1316 var includeSrcJar android.WritablePath
1317 if Bool(j.properties.Include_srcs) {
1318 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1319 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1320 }
1321
Colin Crosscedd4762018-09-13 11:26:19 -07001322 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1323 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001324 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001325 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001326
1327 var resArgs []string
1328 var resDeps android.Paths
1329
1330 resArgs = append(resArgs, dirArgs...)
1331 resDeps = append(resDeps, dirDeps...)
1332
1333 resArgs = append(resArgs, fileArgs...)
1334 resDeps = append(resDeps, fileDeps...)
1335
Colin Cross988708c2019-05-06 14:04:11 -07001336 resArgs = append(resArgs, extraArgs...)
1337 resDeps = append(resDeps, extraDeps...)
1338
Colin Cross40a36712017-09-27 17:41:35 -07001339 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001340 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001341 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001342 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001343 if ctx.Failed() {
1344 return
1345 }
1346 }
1347
Colin Cross0c4ce212019-05-03 15:28:19 -07001348 var resourceJars android.Paths
1349 if j.resourceJar != nil {
1350 resourceJars = append(resourceJars, j.resourceJar)
1351 }
1352 if Bool(j.properties.Include_srcs) {
1353 resourceJars = append(resourceJars, includeSrcJar)
1354 }
1355 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001356
Colin Cross0c4ce212019-05-03 15:28:19 -07001357 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001358 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001359 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001360 false, nil, nil)
1361 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001362 } else if len(resourceJars) == 1 {
1363 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001364 }
1365
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001366 if len(deps.staticJars) > 0 {
1367 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001368 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001369
Colin Cross094054a2018-10-17 15:10:48 -07001370 manifest := j.overrideManifest
1371 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001372 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001373 }
Colin Cross635acc92017-09-12 22:50:46 -07001374
Colin Cross8a497952019-03-05 22:25:09 -08001375 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001376 if len(services) > 0 {
1377 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1378 var zipargs []string
1379 for _, file := range services {
1380 serviceFile := file.String()
1381 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1382 }
1383 ctx.Build(pctx, android.BuildParams{
1384 Rule: zip,
1385 Output: servicesJar,
1386 Implicits: services,
1387 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001388 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001389 },
1390 })
1391 jars = append(jars, servicesJar)
1392 }
1393
Colin Cross0a6e0072017-08-30 14:24:55 -07001394 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001395 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001396 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001397
1398 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001399 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1400 // Optimization: skip the combine step if there is nothing to do
1401 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1402 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1403 // any if len(jars) == 1.
1404 outputFile = moduleOutPath
1405 } else {
1406 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1407 ctx.Build(pctx, android.BuildParams{
1408 Rule: android.Cp,
1409 Input: jars[0],
1410 Output: combinedJar,
1411 })
1412 outputFile = combinedJar
1413 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001414 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001415 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001416 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001417 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001418 outputFile = combinedJar
1419 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001420
Colin Cross331a1212018-08-15 20:40:52 -07001421 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001422 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001423 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001424 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001425 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001426 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001427
1428 // jarjar resource jar if necessary
1429 if j.resourceJar != nil {
1430 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001431 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001432 j.resourceJar = resourceJarJarFile
1433 }
1434
Colin Cross0a6e0072017-08-30 14:24:55 -07001435 if ctx.Failed() {
1436 return
1437 }
1438 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001439
1440 // Check package restrictions if necessary.
1441 if len(j.properties.Permitted_packages) > 0 {
1442 // Check packages and copy to package-checked file.
1443 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1444 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1445 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1446
1447 if ctx.Failed() {
1448 return
1449 }
1450 }
1451
Nan Zhanged19fc32017-10-19 13:06:22 -07001452 j.implementationJarFile = outputFile
1453 if j.headerJarFile == nil {
1454 j.headerJarFile = j.implementationJarFile
1455 }
Colin Cross2fe66872015-03-30 17:20:39 -07001456
Colin Cross6510f912017-11-29 00:27:14 -08001457 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001458 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1459 j.properties.Instrument = true
1460 }
1461 }
1462
Colin Cross3144dfc2018-01-03 15:06:47 -08001463 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001464 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1465 }
1466
Colin Cross331a1212018-08-15 20:40:52 -07001467 // merge implementation jar with resources if necessary
1468 implementationAndResourcesJar := outputFile
1469 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001470 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001471 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001472 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001473 false, nil, nil)
1474 implementationAndResourcesJar = combinedJar
1475 }
1476
1477 j.implementationAndResourcesJar = implementationAndResourcesJar
1478
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001479 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001480 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001481 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001482 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001483 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001484 if ctx.Failed() {
1485 return
1486 }
Colin Cross331a1212018-08-15 20:40:52 -07001487
Jiyong Park09cb6292019-07-15 15:29:23 +09001488 // Hidden API CSV generation and dex encoding
1489 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1490 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001491
Colin Cross331a1212018-08-15 20:40:52 -07001492 // merge dex jar with resources if necessary
1493 if j.resourceJar != nil {
1494 jars := android.Paths{dexOutputFile, j.resourceJar}
1495 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1496 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1497 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001498 if j.deviceProperties.UncompressDex {
1499 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1500 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1501 dexOutputFile = combinedAlignedJar
1502 } else {
1503 dexOutputFile = combinedJar
1504 }
Colin Cross331a1212018-08-15 20:40:52 -07001505 }
1506
1507 j.dexJarFile = dexOutputFile
1508
Colin Cross8faf8fc2019-01-16 15:15:52 -08001509 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001510 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1511
1512 j.maybeStrippedDexJarFile = dexOutputFile
1513
Colin Cross3063b782018-08-15 11:19:12 -07001514 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001515
1516 if ctx.Failed() {
1517 return
1518 }
Colin Cross331a1212018-08-15 20:40:52 -07001519 } else {
1520 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001521 }
Colin Cross331a1212018-08-15 20:40:52 -07001522
Colin Crossb7a63242015-04-16 14:09:14 -07001523 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001524
1525 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1526 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001527}
1528
Colin Cross3b706fd2019-09-05 16:44:18 -07001529func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1530 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1531
1532 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1533 if idx >= 0 {
1534 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1535 jarName += strconv.Itoa(idx)
1536 }
1537
1538 classes := android.PathForModuleOut(ctx, "javac", jarName)
1539 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1540
1541 if ctx.Config().EmitXrefRules() {
1542 extractionFile := android.PathForModuleOut(ctx, kzipName)
1543 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1544 j.kytheFiles = append(j.kytheFiles, extractionFile)
1545 }
1546
1547 return classes
1548}
1549
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001550// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1551// since some of these flags may be used internally.
1552func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1553 for _, flag := range flags {
1554 flag = strings.TrimSpace(flag)
1555
1556 if !strings.HasPrefix(flag, "-") {
1557 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1558 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1559 ctx.PropertyErrorf("kotlincflags",
1560 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1561 } else if inList(flag, config.KotlincIllegalFlags) {
1562 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1563 } else if flag == "-include-runtime" {
1564 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1565 } else {
1566 args := strings.Split(flag, " ")
1567 if args[0] == "-kotlin-home" {
1568 ctx.PropertyErrorf("kotlincflags",
1569 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1570 }
1571 }
1572 }
1573}
1574
Colin Cross8eadbf02017-10-24 17:46:00 -07001575func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001576 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001577
1578 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001579 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001580 // Compile java sources into turbine.jar.
1581 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1582 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1583 if ctx.Failed() {
1584 return nil
1585 }
1586 jars = append(jars, turbineJar)
1587 }
1588
Colin Cross55f63ea2018-08-27 12:37:09 -07001589 jars = append(jars, extraJars...)
1590
Nan Zhanged19fc32017-10-19 13:06:22 -07001591 // Combine any static header libraries into classes-header.jar. If there is only
1592 // one input jar this step will be skipped.
1593 var headerJar android.Path
1594 jars = append(jars, deps.staticHeaderJars...)
1595
Colin Cross5c6ecc12017-10-23 18:12:27 -07001596 // we cannot skip the combine step for now if there is only one jar
1597 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1598 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001599 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001600 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001601 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001602
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001603 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001604 // Transform classes.jar into classes-jarjar.jar
1605 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001606 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001607 headerJar = jarjarFile
1608 if ctx.Failed() {
1609 return nil
1610 }
1611 }
1612
1613 return headerJar
1614}
1615
Colin Crosscb933592017-11-22 13:49:43 -08001616func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001617 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001618
Colin Cross7a3139e2017-12-19 13:57:50 -08001619 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001620
Colin Cross84c38822018-01-03 15:59:46 -08001621 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001622 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1623
1624 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1625
1626 j.jacocoReportClassesFile = jacocoReportClassesFile
1627
1628 return instrumentedJar
1629}
1630
albaltai36ff7dc2018-12-25 14:35:23 +08001631var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001632
Nan Zhanged19fc32017-10-19 13:06:22 -07001633func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001634 if j.headerJarFile == nil {
1635 return nil
1636 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001637 return android.Paths{j.headerJarFile}
1638}
1639
1640func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001641 if j.implementationJarFile == nil {
1642 return nil
1643 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001644 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001645}
1646
Colin Crossf24a22a2019-01-31 14:12:44 -08001647func (j *Module) DexJar() android.Path {
1648 return j.dexJarFile
1649}
1650
Colin Cross331a1212018-08-15 20:40:52 -07001651func (j *Module) ResourceJars() android.Paths {
1652 if j.resourceJar == nil {
1653 return nil
1654 }
1655 return android.Paths{j.resourceJar}
1656}
1657
1658func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001659 if j.implementationAndResourcesJar == nil {
1660 return nil
1661 }
Colin Cross331a1212018-08-15 20:40:52 -07001662 return android.Paths{j.implementationAndResourcesJar}
1663}
1664
Colin Cross46c9b8b2017-06-22 16:51:17 -07001665func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001666 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001667 return j.exportAidlIncludeDirs
1668}
1669
Jiyong Park1be96912018-05-28 18:02:19 +09001670func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001671 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001672 return j.exportedSdkLibs
1673}
1674
Artur Satayev9cf46692019-11-26 18:08:34 +00001675func (j *Module) ExportedPlugins() (android.Paths, []string) {
1676 return j.exportedPluginJars, j.exportedPluginClasses
1677}
1678
Colin Cross0c4ce212019-05-03 15:28:19 -07001679func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1680 return j.srcJarArgs, j.srcJarDeps
1681}
1682
Colin Cross46c9b8b2017-06-22 16:51:17 -07001683var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001684
Colin Cross46c9b8b2017-06-22 16:51:17 -07001685func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001686 return j.logtagsSrcs
1687}
1688
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001689// Collect information for opening IDE project files in java/jdeps.go.
1690func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1691 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1692 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001693 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001694 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001695 if j.expandJarjarRules != nil {
1696 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001697 }
1698}
1699
1700func (j *Module) CompilerDeps() []string {
1701 jdeps := []string{}
1702 jdeps = append(jdeps, j.properties.Libs...)
1703 jdeps = append(jdeps, j.properties.Static_libs...)
1704 return jdeps
1705}
1706
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001707func (j *Module) hasCode(ctx android.ModuleContext) bool {
1708 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1709 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1710}
1711
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001712func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1713 depTag := ctx.OtherModuleDependencyTag(dep)
1714 // dependencies other than the static linkage are all considered crossing APEX boundary
1715 return depTag == staticLibTag
1716}
1717
Jiyong Park0b238752019-10-29 11:23:10 +09001718func (j *Module) Stem() string {
1719 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1720}
1721
Colin Cross2fe66872015-03-30 17:20:39 -07001722//
1723// Java libraries (.jar file)
1724//
1725
Colin Crossf506d872017-07-19 15:53:04 -07001726type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001727 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001728
1729 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001730}
1731
Colin Cross42be7612019-02-21 18:12:14 -08001732func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001733 // Store uncompressed (and do not strip) dex files from boot class path jars.
1734 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1735 return true
1736 }
1737
1738 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001739 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001740 return true
1741 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001742 if ctx.Config().UncompressPrivAppDex() &&
1743 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1744 return true
1745 }
1746
Colin Cross2fc72f62018-12-21 12:59:54 -08001747 return false
1748}
1749
Colin Crossf506d872017-07-19 15:53:04 -07001750func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001751 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001752 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001753 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001754 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001755 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001756 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001757 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001758
Jiyong Park7f7766d2019-07-25 22:02:35 +09001759 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1760 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001761 var extraInstallDeps android.Paths
1762 if j.InstallMixin != nil {
1763 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1764 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001765 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001766 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001767 }
Colin Crossb7a63242015-04-16 14:09:14 -07001768}
1769
Colin Crossf506d872017-07-19 15:53:04 -07001770func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001771 j.deps(ctx)
1772}
1773
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001774const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001775 aidlIncludeDir = "aidl"
1776 javaDir = "java"
1777 jarFileSuffix = ".jar"
1778 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001779)
1780
Paul Duffina0dbf432019-12-05 11:25:53 +00001781// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001782func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1783 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1784}
1785
1786func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1787 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001788}
1789
Paul Duffin13879572019-11-28 14:31:38 +00001790type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001791 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001792}
1793
1794func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1795 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1796}
1797
1798func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1799 _, ok := module.(*Library)
1800 return ok
1801}
1802
Paul Duffina0dbf432019-12-05 11:25:53 +00001803func (mt *librarySdkMemberType) buildSnapshot(
1804 sdkModuleContext android.ModuleContext,
1805 builder android.SnapshotBuilder,
1806 member android.SdkMember,
1807 jarToExportGetter func(j *Library) android.Path) {
1808
Paul Duffin13879572019-11-28 14:31:38 +00001809 variants := member.Variants()
1810 if len(variants) != 1 {
1811 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1812 for _, variant := range variants {
1813 sdkModuleContext.ModuleErrorf(" %q", variant)
1814 }
1815 }
1816 variant := variants[0]
1817 j := variant.(*Library)
1818
Paul Duffina0dbf432019-12-05 11:25:53 +00001819 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001820 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001821 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001822
1823 for _, dir := range j.AidlIncludeDirs() {
1824 // TODO(jiyong): copy parcelable declarations only
1825 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1826 for _, file := range aidlFiles {
1827 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1828 }
1829 }
1830
Paul Duffin9d8d6092019-12-05 18:19:29 +00001831 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001832 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001833}
1834
Paul Duffina0dbf432019-12-05 11:25:53 +00001835type headerLibrarySdkMemberType struct {
1836 librarySdkMemberType
1837}
1838
1839func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1840 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1841 headerJars := j.HeaderJars()
1842 if len(headerJars) != 1 {
1843 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1844 }
1845
1846 return headerJars[0]
1847 })
1848}
1849
Paul Duffina0dbf432019-12-05 11:25:53 +00001850type implLibrarySdkMemberType struct {
1851 librarySdkMemberType
1852}
1853
1854func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1855 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1856 implementationJars := j.ImplementationJars()
1857 if len(implementationJars) != 1 {
1858 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1859 }
1860
1861 return implementationJars[0]
1862 })
1863}
1864
Colin Cross1b16b0e2019-02-12 14:41:32 -08001865// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1866//
1867// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1868// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1869// as a `static_libs` dependency of another module.
1870//
1871// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1872// a device.
1873//
1874// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1875// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001876func LibraryFactory() android.Module {
1877 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001878
Colin Cross9ae1b922018-06-26 17:59:05 -07001879 module.AddProperties(
1880 &module.Module.properties,
1881 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001882 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001883 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001884
Jiyong Park7f7766d2019-07-25 22:02:35 +09001885 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001886 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001887 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001888 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001889}
1890
Colin Cross1b16b0e2019-02-12 14:41:32 -08001891// java_library_static is an obsolete alias for java_library.
1892func LibraryStaticFactory() android.Module {
1893 return LibraryFactory()
1894}
1895
1896// java_library_host builds and links sources into a `.jar` file for the host.
1897//
1898// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1899// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001900func LibraryHostFactory() android.Module {
1901 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001902
Colin Cross6af17aa2017-09-20 12:59:05 -07001903 module.AddProperties(
1904 &module.Module.properties,
1905 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001906
Colin Cross9ae1b922018-06-26 17:59:05 -07001907 module.Module.properties.Installable = proptools.BoolPtr(true)
1908
Jiyong Park7f7766d2019-07-25 22:02:35 +09001909 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001910 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001911 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001912}
1913
1914//
Colin Crossb628ea52018-08-14 16:42:33 -07001915// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001916//
1917
1918type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001919 // list of compatibility suites (for example "cts", "vts") that the module should be
1920 // installed into.
1921 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001922
1923 // the name of the test configuration (for example "AndroidTest.xml") that should be
1924 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001925 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001926
Jack He33338892018-09-19 02:21:28 -07001927 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1928 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001929 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001930
Colin Crossd96ca352018-08-10 16:06:24 -07001931 // list of files or filegroup modules that provide data that should be installed alongside
1932 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001933 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001934
1935 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1936 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1937 // explicitly.
1938 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001939}
1940
Paul Duffin42df1442019-03-20 12:45:53 +00001941type testHelperLibraryProperties struct {
1942 // list of compatibility suites (for example "cts", "vts") that the module should be
1943 // installed into.
1944 Test_suites []string `android:"arch_variant"`
1945}
1946
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001947type prebuiltTestProperties struct {
1948 // list of compatibility suites (for example "cts", "vts") that the module should be
1949 // installed into.
1950 Test_suites []string `android:"arch_variant"`
1951
1952 // the name of the test configuration (for example "AndroidTest.xml") that should be
1953 // installed with the module.
1954 Test_config *string `android:"path,arch_variant"`
1955}
1956
Colin Cross05638fc2018-04-09 18:40:24 -07001957type Test struct {
1958 Library
1959
1960 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001961
1962 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001963 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001964}
1965
Paul Duffin42df1442019-03-20 12:45:53 +00001966type TestHelperLibrary struct {
1967 Library
1968
1969 testHelperLibraryProperties testHelperLibraryProperties
1970}
1971
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001972type JavaTestImport struct {
1973 Import
1974
1975 prebuiltTestProperties prebuiltTestProperties
1976
1977 testConfig android.Path
1978}
1979
Colin Cross303e21f2018-08-07 16:49:25 -07001980func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001981 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1982 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001983 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001984
1985 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001986}
1987
Paul Duffin42df1442019-03-20 12:45:53 +00001988func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1989 j.Library.GenerateAndroidBuildActions(ctx)
1990}
1991
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001992func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1993 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
1994 j.prebuiltTestProperties.Test_suites, nil)
1995
1996 j.Import.GenerateAndroidBuildActions(ctx)
1997}
1998
1999type testSdkMemberType struct {
2000 android.SdkMemberTypeBase
2001}
2002
2003func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2004 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2005}
2006
2007func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2008 _, ok := module.(*Test)
2009 return ok
2010}
2011
2012func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2013 variants := member.Variants()
2014 if len(variants) != 1 {
2015 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2016 for _, variant := range variants {
2017 sdkModuleContext.ModuleErrorf(" %q", variant)
2018 }
2019 }
2020 variant := variants[0]
2021 j := variant.(*Test)
2022
2023 implementationJars := j.ImplementationJars()
2024 if len(implementationJars) != 1 {
2025 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2026 }
2027
2028 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2029 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2030
2031 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2032 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2033
2034 module := builder.AddPrebuiltModule(member, "java_test_import")
2035 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2036 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2037}
2038
Colin Cross1b16b0e2019-02-12 14:41:32 -08002039// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2040// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2041//
2042// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2043// compiled against the device bootclasspath.
2044//
2045// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2046// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002047func TestFactory() android.Module {
2048 module := &Test{}
2049
2050 module.AddProperties(
2051 &module.Module.properties,
2052 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002053 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002054 &module.Module.protoProperties,
2055 &module.testProperties)
2056
Colin Cross9ae1b922018-06-26 17:59:05 -07002057 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002058 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002059
Colin Cross05638fc2018-04-09 18:40:24 -07002060 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002061 return module
2062}
2063
Paul Duffin42df1442019-03-20 12:45:53 +00002064// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2065func TestHelperLibraryFactory() android.Module {
2066 module := &TestHelperLibrary{}
2067
2068 module.AddProperties(
2069 &module.Module.properties,
2070 &module.Module.deviceProperties,
2071 &module.Module.dexpreoptProperties,
2072 &module.Module.protoProperties,
2073 &module.testHelperLibraryProperties)
2074
Colin Cross9a4abed2019-04-24 13:19:28 -07002075 module.Module.properties.Installable = proptools.BoolPtr(true)
2076 module.Module.dexpreopter.isTest = true
2077
Paul Duffin42df1442019-03-20 12:45:53 +00002078 InitJavaModule(module, android.HostAndDeviceSupported)
2079 return module
2080}
2081
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002082// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2083// and makes sure that it is added to the appropriate test suite.
2084//
2085// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2086// compiled against an Android classpath.
2087//
2088// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2089// for host modules.
2090func JavaTestImportFactory() android.Module {
2091 module := &JavaTestImport{}
2092
2093 module.AddProperties(
2094 &module.Import.properties,
2095 &module.prebuiltTestProperties)
2096
2097 module.Import.properties.Installable = proptools.BoolPtr(true)
2098
2099 android.InitPrebuiltModule(module, &module.properties.Jars)
2100 android.InitApexModule(module)
2101 android.InitSdkAwareModule(module)
2102 InitJavaModule(module, android.HostAndDeviceSupported)
2103 return module
2104}
2105
Colin Cross1b16b0e2019-02-12 14:41:32 -08002106// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2107// allow running the test with `atest` or a `TEST_MAPPING` file.
2108//
2109// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2110// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002111func TestHostFactory() android.Module {
2112 module := &Test{}
2113
2114 module.AddProperties(
2115 &module.Module.properties,
2116 &module.Module.protoProperties,
2117 &module.testProperties)
2118
Colin Cross9ae1b922018-06-26 17:59:05 -07002119 module.Module.properties.Installable = proptools.BoolPtr(true)
2120
Colin Cross05638fc2018-04-09 18:40:24 -07002121 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002122 return module
2123}
2124
2125//
Colin Cross2fe66872015-03-30 17:20:39 -07002126// Java Binaries (.jar file plus wrapper script)
2127//
2128
Colin Crossf506d872017-07-19 15:53:04 -07002129type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002130 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002131 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002132
2133 // Name of the class containing main to be inserted into the manifest as Main-Class.
2134 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002135}
2136
Colin Crossf506d872017-07-19 15:53:04 -07002137type Binary struct {
2138 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002139
Colin Crossf506d872017-07-19 15:53:04 -07002140 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002141
Colin Cross6b4a32d2017-12-05 13:42:45 -08002142 isWrapperVariant bool
2143
Colin Crossc3315992017-12-08 19:12:36 -08002144 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002145 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002146}
2147
Alex Light24237172017-10-26 09:46:21 -07002148func (j *Binary) HostToolPath() android.OptionalPath {
2149 return android.OptionalPathForPath(j.binaryFile)
2150}
2151
Colin Crossf506d872017-07-19 15:53:04 -07002152func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002153 if ctx.Arch().ArchType == android.Common {
2154 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002155 if j.binaryProperties.Main_class != nil {
2156 if j.properties.Manifest != nil {
2157 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2158 }
2159 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2160 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2161 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2162 }
2163
Colin Cross6b4a32d2017-12-05 13:42:45 -08002164 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002165 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002166 // Handle the binary wrapper
2167 j.isWrapperVariant = true
2168
Colin Cross366938f2017-12-11 16:29:02 -08002169 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002170 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002171 } else {
2172 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2173 }
2174
2175 // Depend on the installed jar so that the wrapper doesn't get executed by
2176 // another build rule before the jar has been installed.
2177 jarFile := ctx.PrimaryModule().(*Binary).installFile
2178
2179 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2180 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002181 }
Colin Cross2fe66872015-03-30 17:20:39 -07002182}
2183
Colin Crossf506d872017-07-19 15:53:04 -07002184func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002185 if ctx.Arch().ArchType == android.Common {
2186 j.deps(ctx)
2187 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002188}
2189
Colin Cross1b16b0e2019-02-12 14:41:32 -08002190// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2191// as well.
2192//
2193// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2194// compiled against the device bootclasspath.
2195//
2196// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2197// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002198func BinaryFactory() android.Module {
2199 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002200
Colin Cross36242852017-06-23 15:06:31 -07002201 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002202 &module.Module.properties,
2203 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002204 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002205 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002206 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002207
Colin Cross9ae1b922018-06-26 17:59:05 -07002208 module.Module.properties.Installable = proptools.BoolPtr(true)
2209
Colin Cross6b4a32d2017-12-05 13:42:45 -08002210 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2211 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002212 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002213}
2214
Colin Cross1b16b0e2019-02-12 14:41:32 -08002215// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2216//
2217// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2218// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002219func BinaryHostFactory() android.Module {
2220 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002221
Colin Cross36242852017-06-23 15:06:31 -07002222 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002223 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002224 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002225 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002226
Colin Cross9ae1b922018-06-26 17:59:05 -07002227 module.Module.properties.Installable = proptools.BoolPtr(true)
2228
Colin Cross6b4a32d2017-12-05 13:42:45 -08002229 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2230 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002231 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002232}
2233
2234//
2235// Java prebuilts
2236//
2237
Colin Cross74d73e22017-08-02 11:05:49 -07002238type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002239 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002240
Nan Zhangea568a42017-11-08 21:20:04 -08002241 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002242
2243 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002244
2245 // List of shared java libs that this module has dependencies to
2246 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002247
2248 // List of files to remove from the jar file(s)
2249 Exclude_files []string
2250
2251 // List of directories to remove from the jar file(s)
2252 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002253
2254 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002255 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002256
2257 // set the name of the output
2258 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002259}
2260
2261type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002262 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002263 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002264 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002265 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002266 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002267
Colin Cross74d73e22017-08-02 11:05:49 -07002268 properties ImportProperties
2269
Colin Cross0a6e0072017-08-30 14:24:55 -07002270 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002271 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002272}
2273
Colin Cross83bb3162018-06-25 15:48:06 -07002274func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002275 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002276}
2277
2278func (j *Import) minSdkVersion() string {
2279 return j.sdkVersion()
2280}
2281
Colin Cross74d73e22017-08-02 11:05:49 -07002282func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002283 return &j.prebuilt
2284}
2285
Colin Cross74d73e22017-08-02 11:05:49 -07002286func (j *Import) PrebuiltSrcs() []string {
2287 return j.properties.Jars
2288}
2289
2290func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002291 return j.prebuilt.Name(j.ModuleBase.Name())
2292}
2293
Jiyong Park0b238752019-10-29 11:23:10 +09002294func (j *Import) Stem() string {
2295 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2296}
2297
Colin Cross74d73e22017-08-02 11:05:49 -07002298func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002299 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002300}
2301
Colin Cross74d73e22017-08-02 11:05:49 -07002302func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002303 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002304
Jiyong Park0b238752019-10-29 11:23:10 +09002305 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002306 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002307 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2308 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002309 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002310 inputFile := outputFile
2311 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2312 TransformJetifier(ctx, outputFile, inputFile)
2313 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002314 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002315
2316 ctx.VisitDirectDeps(func(module android.Module) {
2317 otherName := ctx.OtherModuleName(module)
2318 tag := ctx.OtherModuleDependencyTag(module)
2319
2320 switch dep := module.(type) {
2321 case Dependency:
2322 switch tag {
2323 case libTag, staticLibTag:
2324 // sdk lib names from dependencies are re-exported
2325 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2326 }
2327 case SdkLibraryDependency:
2328 switch tag {
2329 case libTag:
2330 // names of sdk libs that are directly depended are exported
2331 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2332 }
2333 }
2334 })
2335
2336 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002337 if Bool(j.properties.Installable) {
2338 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002339 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002340 }
Colin Cross2fe66872015-03-30 17:20:39 -07002341}
2342
Colin Cross74d73e22017-08-02 11:05:49 -07002343var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002344
Nan Zhanged19fc32017-10-19 13:06:22 -07002345func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002346 if j.combinedClasspathFile == nil {
2347 return nil
2348 }
Colin Cross37f6d792018-07-12 12:28:41 -07002349 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002350}
2351
2352func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002353 if j.combinedClasspathFile == nil {
2354 return nil
2355 }
Colin Cross37f6d792018-07-12 12:28:41 -07002356 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002357}
2358
Colin Cross331a1212018-08-15 20:40:52 -07002359func (j *Import) ResourceJars() android.Paths {
2360 return nil
2361}
2362
2363func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002364 if j.combinedClasspathFile == nil {
2365 return nil
2366 }
Colin Cross331a1212018-08-15 20:40:52 -07002367 return android.Paths{j.combinedClasspathFile}
2368}
2369
Colin Crossf24a22a2019-01-31 14:12:44 -08002370func (j *Import) DexJar() android.Path {
2371 return nil
2372}
2373
Colin Cross74d73e22017-08-02 11:05:49 -07002374func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002375 return nil
2376}
2377
Jiyong Park1be96912018-05-28 18:02:19 +09002378func (j *Import) ExportedSdkLibs() []string {
2379 return j.exportedSdkLibs
2380}
2381
Artur Satayev9cf46692019-11-26 18:08:34 +00002382func (j *Import) ExportedPlugins() (android.Paths, []string) {
2383 return nil, nil
2384}
2385
Colin Cross0c4ce212019-05-03 15:28:19 -07002386func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2387 return nil, nil
2388}
2389
albaltai36ff7dc2018-12-25 14:35:23 +08002390// Add compile time check for interface implementation
2391var _ android.IDEInfo = (*Import)(nil)
2392var _ android.IDECustomizedModuleName = (*Import)(nil)
2393
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002394// Collect information for opening IDE project files in java/jdeps.go.
2395const (
2396 removedPrefix = "prebuilt_"
2397)
2398
2399func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2400 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2401}
2402
2403func (j *Import) IDECustomizedModuleName() string {
2404 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2405 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2406 // solution to get the Import name.
2407 name := j.Name()
2408 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002409 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002410 }
2411 return name
2412}
2413
Colin Cross74d73e22017-08-02 11:05:49 -07002414var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002415
Colin Cross1b16b0e2019-02-12 14:41:32 -08002416// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2417//
2418// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2419// compiled against an Android classpath.
2420//
2421// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2422// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002423func ImportFactory() android.Module {
2424 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002425
Colin Cross74d73e22017-08-02 11:05:49 -07002426 module.AddProperties(&module.properties)
2427
2428 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002429 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002430 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002431 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002432 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002433}
2434
Colin Cross1b16b0e2019-02-12 14:41:32 -08002435// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2436// module.
2437//
2438// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2439// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002440func ImportFactoryHost() android.Module {
2441 module := &Import{}
2442
2443 module.AddProperties(&module.properties)
2444
2445 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002446 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002447 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002448 return module
2449}
2450
Colin Cross42be7612019-02-21 18:12:14 -08002451// dex_import module
2452
2453type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002454 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002455
2456 // set the name of the output
2457 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002458}
2459
2460type DexImport struct {
2461 android.ModuleBase
2462 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002463 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002464 prebuilt android.Prebuilt
2465
2466 properties DexImportProperties
2467
2468 dexJarFile android.Path
2469 maybeStrippedDexJarFile android.Path
2470
2471 dexpreopter
2472}
2473
2474func (j *DexImport) Prebuilt() *android.Prebuilt {
2475 return &j.prebuilt
2476}
2477
2478func (j *DexImport) PrebuiltSrcs() []string {
2479 return j.properties.Jars
2480}
2481
2482func (j *DexImport) Name() string {
2483 return j.prebuilt.Name(j.ModuleBase.Name())
2484}
2485
Jiyong Park0b238752019-10-29 11:23:10 +09002486func (j *DexImport) Stem() string {
2487 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2488}
2489
Colin Cross42be7612019-02-21 18:12:14 -08002490func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2491 if len(j.properties.Jars) != 1 {
2492 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2493 }
2494
Jiyong Park0b238752019-10-29 11:23:10 +09002495 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002496 j.dexpreopter.isInstallable = true
2497 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2498
2499 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2500 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2501
2502 if j.dexpreopter.uncompressedDex {
2503 rule := android.NewRuleBuilder()
2504
2505 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2506 rule.Temporary(temporary)
2507
2508 // use zip2zip to uncompress classes*.dex files
2509 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002510 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002511 FlagWithInput("-i ", inputJar).
2512 FlagWithOutput("-o ", temporary).
2513 FlagWithArg("-0 ", "'classes*.dex'")
2514
2515 // use zipalign to align uncompressed classes*.dex files
2516 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002517 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002518 Flag("-f").
2519 Text("4").
2520 Input(temporary).
2521 Output(dexOutputFile)
2522
2523 rule.DeleteTemporaryFiles()
2524
2525 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2526 } else {
2527 ctx.Build(pctx, android.BuildParams{
2528 Rule: android.Cp,
2529 Input: inputJar,
2530 Output: dexOutputFile,
2531 })
2532 }
2533
2534 j.dexJarFile = dexOutputFile
2535
2536 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2537
2538 j.maybeStrippedDexJarFile = dexOutputFile
2539
2540 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2541 ctx.ModuleName()+".jar", dexOutputFile)
2542}
2543
2544func (j *DexImport) DexJar() android.Path {
2545 return j.dexJarFile
2546}
2547
2548// dex_import imports a `.jar` file containing classes.dex files.
2549//
2550// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2551// to the device.
2552func DexImportFactory() android.Module {
2553 module := &DexImport{}
2554
2555 module.AddProperties(&module.properties)
2556
2557 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002558 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002559 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002560 return module
2561}
2562
Colin Cross89536d42017-07-07 14:35:50 -07002563//
2564// Defaults
2565//
2566type Defaults struct {
2567 android.ModuleBase
2568 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002569 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002570}
2571
Colin Cross1b16b0e2019-02-12 14:41:32 -08002572// java_defaults provides a set of properties that can be inherited by other java or android modules.
2573//
2574// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2575// property in the defaults module that exists in the depending module will be prepended to the depending module's
2576// value for that property.
2577//
2578// Example:
2579//
2580// java_defaults {
2581// name: "example_defaults",
2582// srcs: ["common/**/*.java"],
2583// javacflags: ["-Xlint:all"],
2584// aaptflags: ["--auto-add-overlay"],
2585// }
2586//
2587// java_library {
2588// name: "example",
2589// defaults: ["example_defaults"],
2590// srcs: ["example/**/*.java"],
2591// }
2592//
2593// is functionally identical to:
2594//
2595// java_library {
2596// name: "example",
2597// srcs: [
2598// "common/**/*.java",
2599// "example/**/*.java",
2600// ],
2601// javacflags: ["-Xlint:all"],
2602// }
Colin Cross89536d42017-07-07 14:35:50 -07002603func defaultsFactory() android.Module {
2604 return DefaultsFactory()
2605}
2606
Paul Duffin47357662019-12-05 14:07:14 +00002607func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002608 module := &Defaults{}
2609
Colin Cross89536d42017-07-07 14:35:50 -07002610 module.AddProperties(
2611 &CompilerProperties{},
2612 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002613 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002614 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002615 &aaptProperties{},
2616 &androidLibraryProperties{},
2617 &appProperties{},
2618 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002619 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002620 &ImportProperties{},
2621 &AARImportProperties{},
2622 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002623 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002624 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002625 )
2626
2627 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002628 return module
2629}
Nan Zhangea568a42017-11-08 21:20:04 -08002630
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002631func kytheExtractJavaFactory() android.Singleton {
2632 return &kytheExtractJavaSingleton{}
2633}
2634
2635type kytheExtractJavaSingleton struct {
2636}
2637
2638func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2639 var xrefTargets android.Paths
2640 ctx.VisitAllModules(func(module android.Module) {
2641 if javaModule, ok := module.(xref); ok {
2642 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2643 }
2644 })
2645 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2646 if len(xrefTargets) > 0 {
2647 ctx.Build(pctx, android.BuildParams{
2648 Rule: blueprint.Phony,
2649 Output: android.PathForPhony(ctx, "xref_java"),
2650 Inputs: xrefTargets,
2651 })
2652 }
2653}
2654
Nan Zhangea568a42017-11-08 21:20:04 -08002655var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002656var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002657var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002658var inList = android.InList