blob: a546cd5249ff957a4427484b62244a9658067164 [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
Jiyong Park618922e2020-01-08 13:35:43 +0900450 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700451}
452
Jiyong Parkc678ad32018-04-10 13:07:10 +0900453type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700454 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
455 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900456}
457
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800458type xref interface {
459 XrefJavaFiles() android.Paths
460}
461
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800462func (j *Module) XrefJavaFiles() android.Paths {
463 return j.kytheFiles
464}
465
Colin Cross89536d42017-07-07 14:35:50 -0700466func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
467 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
468 android.InitDefaultableModule(module)
469}
470
Colin Crossbe1da472017-07-07 15:59:46 -0700471type dependencyTag struct {
472 blueprint.BaseDependencyTag
473 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700474}
475
Colin Crossa4f08812018-10-02 22:03:40 -0700476type jniDependencyTag struct {
477 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700478}
479
Jiyong Park8be103b2019-11-08 15:53:48 +0900480func IsJniDepTag(depTag blueprint.DependencyTag) bool {
481 _, ok := depTag.(*jniDependencyTag)
482 return ok
483}
484
Colin Crossbe1da472017-07-07 15:59:46 -0700485var (
Colin Cross4b964c02018-10-15 16:18:06 -0700486 staticLibTag = dependencyTag{name: "staticlib"}
487 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700488 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800489 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000490 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700491 bootClasspathTag = dependencyTag{name: "bootclasspath"}
492 systemModulesTag = dependencyTag{name: "system modules"}
493 frameworkResTag = dependencyTag{name: "framework-res"}
494 frameworkApkTag = dependencyTag{name: "framework-apk"}
495 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800496 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700497 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
498 certificateTag = dependencyTag{name: "certificate"}
499 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700500 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700501)
Colin Cross2fe66872015-03-30 17:20:39 -0700502
Colin Crossfc3674a2017-09-18 17:41:52 -0700503type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700504 useModule, useFiles, useDefaultLibs, invalidVersion bool
505
Colin Cross6cef4812019-10-17 14:23:50 -0700506 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
507 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100508
509 // The default system modules to use. Will be an empty string if no system
510 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700511 systemModules string
512
Colin Cross6cef4812019-10-17 14:23:50 -0700513 // The modules that will be added ot the classpath when targeting 1.9 or higher
514 java9Classpath []string
515
Colin Crossa97c5d32018-03-28 14:58:31 -0700516 frameworkResModule string
517
Colin Cross86a60ae2018-05-29 14:44:55 -0700518 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700519 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100520
521 noStandardLibs, noFrameworksLibs bool
522}
523
524func (s sdkDep) hasStandardLibs() bool {
525 return !s.noStandardLibs
526}
527
528func (s sdkDep) hasFrameworkLibs() bool {
529 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700530}
531
Colin Crossa4f08812018-10-02 22:03:40 -0700532type jniLib struct {
533 name string
534 path android.Path
535 target android.Target
536}
537
Colin Cross0ea8ba82019-06-06 14:33:29 -0700538func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800539 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
540}
541
Colin Cross0ea8ba82019-06-06 14:33:29 -0700542func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800543 return j.shouldInstrument(ctx) &&
544 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
545 ctx.Config().UnbundledBuild())
546}
547
Colin Cross83bb3162018-06-25 15:48:06 -0700548func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900549 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700550}
551
Paul Duffine25c6442019-10-11 13:50:28 +0100552func (j *Module) systemModules() string {
553 return proptools.String(j.deviceProperties.System_modules)
554}
555
Colin Cross83bb3162018-06-25 15:48:06 -0700556func (j *Module) minSdkVersion() string {
557 if j.deviceProperties.Min_sdk_version != nil {
558 return *j.deviceProperties.Min_sdk_version
559 }
560 return j.sdkVersion()
561}
562
Dan Willemsen419290a2018-10-31 15:28:47 -0700563func (j *Module) targetSdkVersion() string {
564 if j.deviceProperties.Target_sdk_version != nil {
565 return *j.deviceProperties.Target_sdk_version
566 }
567 return j.sdkVersion()
568}
569
Jiyong Parkb02bb402019-12-03 00:43:57 +0900570func (j *Module) AvailableFor(what string) bool {
571 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
572 // Exception: for hostdex: true libraries, the platform variant is created
573 // even if it's not marked as available to platform. In that case, the platform
574 // variant is used only for the hostdex and not installed to the device.
575 return true
576 }
577 return j.ApexModuleBase.AvailableFor(what)
578}
579
Colin Crossbe1da472017-07-07 15:59:46 -0700580func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700581 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100582 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700583 if sdkDep.useDefaultLibs {
584 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
585 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
586 if sdkDep.hasFrameworkLibs() {
587 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700588 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700589 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700590 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100591 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700592 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700593 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
594 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
595 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
596 }
Colin Cross2fe66872015-03-30 17:20:39 -0700597 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700598
Nan Zhangb2b33de2018-02-23 11:18:47 -0800599 if ctx.ModuleName() == "android_stubs_current" ||
600 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700601 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700602 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800603 }
Colin Cross2fe66872015-03-30 17:20:39 -0700604 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700605
Inseob Kimac1e9862019-12-09 18:15:47 +0900606 syspropPublicStubs := syspropPublicStubs(ctx.Config())
607
608 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
609 // and redirects dependency to public stub depending on the link type.
610 rewriteSyspropLibs := func(libs []string, prop string) []string {
611 // make a copy
612 ret := android.CopyOf(libs)
613
614 for idx, lib := range libs {
615 stub, ok := syspropPublicStubs[lib]
616
617 if !ok {
618 continue
619 }
620
621 linkType, _ := j.getLinkType(ctx.ModuleName())
622 if linkType == javaSystem {
623 ret[idx] = stub
624 } else if linkType != javaPlatform {
625 ctx.PropertyErrorf("sdk_version",
626 "can't link against sysprop_library %q from a module using public or core API",
627 lib)
628 }
629 }
630
631 return ret
632 }
633
634 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
635 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700636
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700637 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000638 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800639
Colin Crossfe17f6f2019-03-28 19:30:56 -0700640 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700641 if j.hasSrcExt(".proto") {
642 protoDeps(ctx, &j.protoProperties)
643 }
Colin Cross93e85952017-08-15 13:34:18 -0700644
645 if j.hasSrcExt(".kt") {
646 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
647 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700648 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
649 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800650 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800651 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
652 }
Colin Cross93e85952017-08-15 13:34:18 -0700653 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800654
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000655 // Framework libraries need special handling in static coverage builds: they should not have
656 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
657 // the same jacoco classes coming from different bootclasspath jars.
658 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
659 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
660 j.properties.Instrument = true
661 }
662 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700663 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800664 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700665}
666
667func hasSrcExt(srcs []string, ext string) bool {
668 for _, src := range srcs {
669 if filepath.Ext(src) == ext {
670 return true
671 }
672 }
673
674 return false
675}
676
677func (j *Module) hasSrcExt(ext string) bool {
678 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700679}
680
Colin Cross46c9b8b2017-06-22 16:51:17 -0700681func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700682 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700683
Colin Crossebe1a512017-11-14 13:12:14 -0800684 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
685 aidlIncludes = append(aidlIncludes,
686 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
687 aidlIncludes = append(aidlIncludes,
688 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700689
Colin Cross3047fa22019-04-18 10:56:44 -0700690 var flags []string
691 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700692
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700693 if aidlPreprocess.Valid() {
694 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700695 deps = append(deps, aidlPreprocess.Path())
696 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700697 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700698 }
699
Colin Cross3047fa22019-04-18 10:56:44 -0700700 if len(j.exportAidlIncludeDirs) > 0 {
701 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
702 }
703
704 if len(aidlIncludes) > 0 {
705 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
706 }
707
Colin Cross635c3b02016-05-18 15:37:25 -0700708 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800709 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700710 flags = append(flags, "-I"+src.String())
711 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700712
Martijn Coeneneab15642018-03-09 09:29:59 +0100713 if Bool(j.deviceProperties.Aidl.Generate_traces) {
714 flags = append(flags, "-t")
715 }
716
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100717 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
718 flags = append(flags, "--transaction_names")
719 }
720
Colin Cross3047fa22019-04-18 10:56:44 -0700721 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700722}
723
Colin Cross32f676a2017-09-06 13:41:06 -0700724type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800725 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700726 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800727 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700728 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800729 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700730 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700731 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700732 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700733 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800734 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700735 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700736 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700737 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700738 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800739 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800740
741 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700742}
Colin Cross2fe66872015-03-30 17:20:39 -0700743
Colin Cross54250902017-12-05 09:28:08 -0800744func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
745 for _, f := range dep.Srcs() {
746 if f.Ext() != ".jar" {
747 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
748 ctx.OtherModuleName(dep.(blueprint.Module)))
749 }
750 }
751}
752
Jiyong Park2d492942018-03-05 17:44:10 +0900753type linkType int
754
755const (
756 javaCore linkType = iota
757 javaSdk
758 javaSystem
759 javaPlatform
760)
761
Jeongik Cha75b83b02019-11-01 15:28:00 +0900762type linkTypeContext interface {
763 android.Module
764 getLinkType(name string) (ret linkType, stubs bool)
765}
766
767func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700768 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700769 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900770 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
771 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100772 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900773 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100774 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900775 return javaCore, false
776 case name == "android_system_stubs_current":
777 return javaSystem, true
778 case strings.HasPrefix(ver, "system_"):
779 return javaSystem, false
780 case name == "android_test_stubs_current":
781 return javaSystem, true
782 case strings.HasPrefix(ver, "test_"):
783 return javaPlatform, false
784 case name == "android_stubs_current":
785 return javaSdk, true
786 case ver == "current":
787 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100788 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900789 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700790 default:
791 if _, err := strconv.Atoi(ver); err != nil {
792 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
793 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900794 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900795 }
796}
797
Jeongik Cha75b83b02019-11-01 15:28:00 +0900798func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700799 if ctx.Host() {
800 return
801 }
802
Jeongik Cha75b83b02019-11-01 15:28:00 +0900803 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900804 if stubs {
805 return
806 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900807 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900808 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."
809
810 switch myLinkType {
811 case javaCore:
812 if otherLinkType != javaCore {
813 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 +0900814 ctx.OtherModuleName(to))
815 }
Jiyong Park2d492942018-03-05 17:44:10 +0900816 break
817 case javaSdk:
818 if otherLinkType != javaCore && otherLinkType != javaSdk {
819 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
820 ctx.OtherModuleName(to))
821 }
822 break
823 case javaSystem:
824 if otherLinkType == javaPlatform {
825 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
826 ctx.OtherModuleName(to))
827 }
828 break
829 case javaPlatform:
830 // no restriction on link-type
831 break
Jiyong Park750e5572018-01-31 00:20:13 +0900832 }
833}
834
Colin Cross32f676a2017-09-06 13:41:06 -0700835func (j *Module) collectDeps(ctx android.ModuleContext) deps {
836 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700837
Colin Cross300f0382018-03-06 13:11:51 -0800838 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700839 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800840 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700841 ctx.AddMissingDependencies(sdkDep.bootclasspath)
842 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800843 } else if sdkDep.useFiles {
844 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700845 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700846 deps.aidlPreprocess = sdkDep.aidl
847 } else {
848 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800849 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700850 }
851
Colin Crossd11fcda2017-10-23 17:59:01 -0700852 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700853 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700854 tag := ctx.OtherModuleDependencyTag(module)
855
Colin Crossa4f08812018-10-02 22:03:40 -0700856 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700857 // Handled by AndroidApp.collectAppDeps
858 return
859 }
860 if tag == certificateTag {
861 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700862 return
863 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900864 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900865 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900866 if to, ok := module.(linkTypeContext); ok {
867 switch tag {
868 case bootClasspathTag, libTag, staticLibTag:
869 checkLinkType(ctx, j, to, tag.(dependencyTag))
870 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700871 }
Jiyong Park750e5572018-01-31 00:20:13 +0900872 }
Colin Cross54250902017-12-05 09:28:08 -0800873 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800874 case SdkLibraryDependency:
875 switch tag {
876 case libTag:
877 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
878 // names of sdk libs that are directly depended are exported
879 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700880 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800881 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
882 }
Colin Cross54250902017-12-05 09:28:08 -0800883 case Dependency:
884 switch tag {
885 case bootClasspathTag:
886 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700887 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800888 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900889 // sdk lib names from dependencies are re-exported
890 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700891 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000892 pluginJars, pluginClasses := dep.ExportedPlugins()
893 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700894 case java9LibTag:
895 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800896 case staticLibTag:
897 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
898 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
899 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700900 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900901 // sdk lib names from dependencies are re-exported
902 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700903 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000904 pluginJars, pluginClasses := dep.ExportedPlugins()
905 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800906 case pluginTag:
907 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800908 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000909 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
910 } else {
911 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800912 }
913 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
914 } else {
915 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
916 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000917 case exportedPluginTag:
918 if plugin, ok := dep.(*Plugin); ok {
919 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
920 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
921 }
922 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
923 if plugin.pluginProperties.Processor_class != nil {
924 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
925 }
926 } else {
927 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
928 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800929 case frameworkApkTag:
930 if ctx.ModuleName() == "android_stubs_current" ||
931 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700932 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800933 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
934 // resource files out of there for aapt.
935 //
936 // Normally the package rule runs aapt, which includes the resource,
937 // but we're not running that in our package rule so just copy in the
938 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700939 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800940 }
Colin Cross54250902017-12-05 09:28:08 -0800941 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700942 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800943 case kotlinAnnotationsTag:
944 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800945 }
946
Colin Cross54250902017-12-05 09:28:08 -0800947 case android.SourceFileProducer:
948 switch tag {
949 case libTag:
950 checkProducesJars(ctx, dep)
951 deps.classpath = append(deps.classpath, dep.Srcs()...)
952 case staticLibTag:
953 checkProducesJars(ctx, dep)
954 deps.classpath = append(deps.classpath, dep.Srcs()...)
955 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
956 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800957 }
958 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700959 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100960 case bootClasspathTag:
961 // If a system modules dependency has been added to the bootclasspath
962 // then add its libs to the bootclasspath.
963 sm := module.(*SystemModules)
964 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
965
Colin Cross1369cdb2017-09-29 17:58:17 -0700966 case systemModulesTag:
967 if deps.systemModules != nil {
968 panic("Found two system module dependencies")
969 }
970 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000971 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700972 panic("Missing directory for system module dependency")
973 }
Colin Crossb77043e2019-07-16 13:57:13 -0700974 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700975 }
Colin Crossec7a0422017-07-07 14:47:12 -0700976 }
Colin Cross2fe66872015-03-30 17:20:39 -0700977 })
978
Jiyong Park1be96912018-05-28 18:02:19 +0900979 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
980
Colin Cross32f676a2017-09-06 13:41:06 -0700981 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700982}
983
Artur Satayev9cf46692019-11-26 18:08:34 +0000984func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
985 deps.processorPath = append(deps.processorPath, pluginJars...)
986 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
987}
988
Colin Cross1e743852019-10-28 11:37:20 -0700989func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800990 v := sdkContext.sdkVersion()
991 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100992 if ctx.Config().IsPdkBuild() &&
993 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700994 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800995 latestSdkVersion := 0
996 if len(sdkVersions) > 0 {
997 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
998 }
999 v = strconv.Itoa(latestSdkVersion)
1000 }
1001
1002 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -07001003 if err != nil {
1004 ctx.PropertyErrorf("sdk_version", "%s", err)
1005 }
Nan Zhang357466b2018-04-17 17:38:36 -07001006 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001007 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001008 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001009 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001010 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001011 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001012 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1013 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001014 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001015 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001016 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001017 }
Nan Zhang357466b2018-04-17 17:38:36 -07001018}
1019
Colin Cross1e743852019-10-28 11:37:20 -07001020type javaVersion int
1021
1022const (
1023 JAVA_VERSION_UNSUPPORTED = 0
1024 JAVA_VERSION_6 = 6
1025 JAVA_VERSION_7 = 7
1026 JAVA_VERSION_8 = 8
1027 JAVA_VERSION_9 = 9
1028)
1029
1030func (v javaVersion) String() string {
1031 switch v {
1032 case JAVA_VERSION_6:
1033 return "1.6"
1034 case JAVA_VERSION_7:
1035 return "1.7"
1036 case JAVA_VERSION_8:
1037 return "1.8"
1038 case JAVA_VERSION_9:
1039 return "1.9"
1040 default:
1041 return "unsupported"
1042 }
1043}
1044
1045// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1046func (v javaVersion) usesJavaModules() bool {
1047 return v >= 9
1048}
1049
1050func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001051 switch javaVersion {
1052 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001053 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001054 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001055 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001056 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001057 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001058 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001059 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001060 case "10", "11":
1061 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001062 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001063 default:
1064 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001065 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001066 }
1067}
1068
Nan Zhanged19fc32017-10-19 13:06:22 -07001069func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001070
Colin Crossf03c82b2015-04-13 13:53:40 -07001071 var flags javaBuilderFlags
1072
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001073 // javaVersion flag.
1074 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1075
Nan Zhanged19fc32017-10-19 13:06:22 -07001076 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001077 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001078 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001079 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001080 }
Colin Cross6510f912017-11-29 00:27:14 -08001081 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001082 // Override the -g flag passed globally to remove local variable debug info to reduce
1083 // disk and memory usage.
1084 javacFlags = append(javacFlags, "-g:source,lines")
1085 }
Colin Crossc228a702019-11-06 16:18:05 -08001086 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001087
Colin Cross66548102018-06-19 22:47:35 -07001088 if ctx.Config().RunErrorProne() {
1089 if config.ErrorProneClasspath == nil {
1090 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1091 }
1092
1093 errorProneFlags := []string{
1094 "-Xplugin:ErrorProne",
1095 "${config.ErrorProneChecks}",
1096 }
1097 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1098
1099 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1100 "'" + strings.Join(errorProneFlags, " ") + "'"
1101 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001102 }
1103
Nan Zhanged19fc32017-10-19 13:06:22 -07001104 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001105 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1106 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001107 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001108 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001109
Colin Crossbe9cdb82019-01-21 21:37:16 -08001110 flags.processor = strings.Join(deps.processorClasses, ",")
1111
Colin Cross1e743852019-10-28 11:37:20 -07001112 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1113 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001114 // Give host-side tools a version of OpenJDK's standard libraries
1115 // close to what they're targeting. As of Dec 2017, AOSP is only
1116 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1117 //
1118 // When building with OpenJDK 8, the following should have no
1119 // effect since those jars would be available by default.
1120 //
1121 // When building with OpenJDK 9 but targeting a version < 1.8,
1122 // putting them on the bootclasspath means that:
1123 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1124 // b) references to existing APIs are not reinterpreted in an
1125 // OpenJDK 9-specific way, eg. calls to subclasses of
1126 // java.nio.Buffer as in http://b/70862583
1127 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1128 flags.bootClasspath = append(flags.bootClasspath,
1129 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1130 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001131 if Bool(j.properties.Use_tools_jar) {
1132 flags.bootClasspath = append(flags.bootClasspath,
1133 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1134 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001135 }
1136
Colin Cross1e743852019-10-28 11:37:20 -07001137 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001138 // Manually specify build directory in case it is not under the repo root.
1139 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1140 // just adding a symlink under the root doesn't help.)
1141 patchPaths := ".:" + ctx.Config().BuildDir()
1142 classPath := flags.classpath.FormJavaClassPath("")
1143 if classPath != "" {
1144 patchPaths += ":" + classPath
1145 }
1146 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001147 }
1148
Nan Zhanged19fc32017-10-19 13:06:22 -07001149 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001150 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001151
Nan Zhanged19fc32017-10-19 13:06:22 -07001152 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001153 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001154
Colin Cross81440082018-08-15 20:21:55 -07001155 if len(javacFlags) > 0 {
1156 // optimization.
1157 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1158 flags.javacFlags = "$javacFlags"
1159 }
1160
Nan Zhanged19fc32017-10-19 13:06:22 -07001161 return flags
1162}
Colin Crossc0b06f12015-04-08 13:03:43 -07001163
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001164func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001165 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001166
1167 deps := j.collectDeps(ctx)
1168 flags := j.collectBuilderFlags(ctx, deps)
1169
Colin Cross1e743852019-10-28 11:37:20 -07001170 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001171 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1172 }
Colin Cross8a497952019-03-05 22:25:09 -08001173 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001174 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001175 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001176 }
1177
Colin Crossaf050172017-11-15 23:01:59 -08001178 srcFiles = j.genSources(ctx, srcFiles, flags)
1179
1180 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001181 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001182 if aaptSrcJar != nil {
1183 srcJars = append(srcJars, aaptSrcJar)
1184 }
Colin Crossb7a63242015-04-16 14:09:14 -07001185
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001186 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001187 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001188 }
1189
Colin Cross1ee23172017-10-18 14:44:18 -07001190 jarName := ctx.ModuleName() + ".jar"
1191
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001192 javaSrcFiles := srcFiles.FilterByExt(".java")
1193 var uniqueSrcFiles android.Paths
1194 set := make(map[string]bool)
1195 for _, v := range javaSrcFiles {
1196 if _, found := set[v.String()]; !found {
1197 set[v.String()] = true
1198 uniqueSrcFiles = append(uniqueSrcFiles, v)
1199 }
1200 }
1201
patricktu242faad2019-09-24 15:41:30 +08001202 // Collect .java files for AIDEGen
1203 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1204
Colin Cross55f63ea2018-08-27 12:37:09 -07001205 var kotlinJars android.Paths
1206
Colin Cross93e85952017-08-15 13:34:18 -07001207 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001208 // user defined kotlin flags.
1209 kotlincFlags := j.properties.Kotlincflags
1210 CheckKotlincFlags(ctx, kotlincFlags)
1211
Colin Cross93e85952017-08-15 13:34:18 -07001212 // If there are kotlin files, compile them first but pass all the kotlin and java files
1213 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1214 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001215 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001216 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001217 kotlincFlags = append(kotlincFlags, "-no-jdk")
1218 }
1219 if len(kotlincFlags) > 0 {
1220 // optimization.
1221 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1222 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001223 }
1224
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001225 var kotlinSrcFiles android.Paths
1226 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1227 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1228
patricktu242faad2019-09-24 15:41:30 +08001229 // Collect .kt files for AIDEGen
1230 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1231
Colin Crossafbb1732019-01-17 15:42:52 -08001232 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1233 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1234
1235 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1236 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1237
1238 if len(flags.processorPath) > 0 {
1239 // Use kapt for annotation processing
1240 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1241 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1242 srcJars = append(srcJars, kaptSrcJar)
1243 // Disable annotation processing in javac, it's already been handled by kapt
1244 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001245 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001246 }
Colin Cross93e85952017-08-15 13:34:18 -07001247
Colin Cross1ee23172017-10-18 14:44:18 -07001248 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001249 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001250 if ctx.Failed() {
1251 return
1252 }
1253
1254 // Make javac rule depend on the kotlinc rule
1255 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001256
Colin Cross93e85952017-08-15 13:34:18 -07001257 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001258 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001259 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001260 }
1261
Colin Cross55f63ea2018-08-27 12:37:09 -07001262 jars := append(android.Paths(nil), kotlinJars...)
1263
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001264 // Store the list of .java files that was passed to javac
1265 j.compiledJavaSrcs = uniqueSrcFiles
1266 j.compiledSrcJars = srcJars
1267
Nan Zhang61eaedb2017-11-02 13:28:15 -07001268 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001269 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001270 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1271 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001272 // Formerly, there was a check here that prevented annotation processors
1273 // from being used when sharding was enabled, as some annotation processors
1274 // do not function correctly in sharded environments. It was removed to
1275 // allow for the use of annotation processors that do function correctly
1276 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001277 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001278 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001279 if ctx.Failed() {
1280 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001281 }
1282 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001283 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001284 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001285 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001286 // If error-prone is enabled, add an additional rule to compile the java files into
1287 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001288 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001289 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1290 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001291 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001292 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001293 extraJarDeps = append(extraJarDeps, errorprone)
1294 }
1295
Nan Zhang61eaedb2017-11-02 13:28:15 -07001296 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001297 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001298 shardSize := int(*(j.properties.Javac_shard_size))
1299 var shardSrcs []android.Paths
1300 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001301 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001302 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001303 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1304 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001305 jars = append(jars, classes)
1306 }
1307 }
1308 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001309 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1310 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001311 jars = append(jars, classes)
1312 }
1313 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001314 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001315 jars = append(jars, classes)
1316 }
Colin Crossd6891432017-09-27 17:39:56 -07001317 if ctx.Failed() {
1318 return
1319 }
Colin Cross2fe66872015-03-30 17:20:39 -07001320 }
1321
Colin Cross0c4ce212019-05-03 15:28:19 -07001322 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1323
1324 var includeSrcJar android.WritablePath
1325 if Bool(j.properties.Include_srcs) {
1326 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1327 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1328 }
1329
Colin Crosscedd4762018-09-13 11:26:19 -07001330 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1331 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001332 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001333 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001334
1335 var resArgs []string
1336 var resDeps android.Paths
1337
1338 resArgs = append(resArgs, dirArgs...)
1339 resDeps = append(resDeps, dirDeps...)
1340
1341 resArgs = append(resArgs, fileArgs...)
1342 resDeps = append(resDeps, fileDeps...)
1343
Colin Cross988708c2019-05-06 14:04:11 -07001344 resArgs = append(resArgs, extraArgs...)
1345 resDeps = append(resDeps, extraDeps...)
1346
Colin Cross40a36712017-09-27 17:41:35 -07001347 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001348 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001349 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001350 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001351 if ctx.Failed() {
1352 return
1353 }
1354 }
1355
Colin Cross0c4ce212019-05-03 15:28:19 -07001356 var resourceJars android.Paths
1357 if j.resourceJar != nil {
1358 resourceJars = append(resourceJars, j.resourceJar)
1359 }
1360 if Bool(j.properties.Include_srcs) {
1361 resourceJars = append(resourceJars, includeSrcJar)
1362 }
1363 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001364
Colin Cross0c4ce212019-05-03 15:28:19 -07001365 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001366 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001367 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001368 false, nil, nil)
1369 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001370 } else if len(resourceJars) == 1 {
1371 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001372 }
1373
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001374 if len(deps.staticJars) > 0 {
1375 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001376 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001377
Colin Cross094054a2018-10-17 15:10:48 -07001378 manifest := j.overrideManifest
1379 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001380 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001381 }
Colin Cross635acc92017-09-12 22:50:46 -07001382
Colin Cross8a497952019-03-05 22:25:09 -08001383 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001384 if len(services) > 0 {
1385 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1386 var zipargs []string
1387 for _, file := range services {
1388 serviceFile := file.String()
1389 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1390 }
1391 ctx.Build(pctx, android.BuildParams{
1392 Rule: zip,
1393 Output: servicesJar,
1394 Implicits: services,
1395 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001396 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001397 },
1398 })
1399 jars = append(jars, servicesJar)
1400 }
1401
Colin Cross0a6e0072017-08-30 14:24:55 -07001402 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001403 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001404 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001405
1406 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001407 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1408 // Optimization: skip the combine step if there is nothing to do
1409 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1410 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1411 // any if len(jars) == 1.
1412 outputFile = moduleOutPath
1413 } else {
1414 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1415 ctx.Build(pctx, android.BuildParams{
1416 Rule: android.Cp,
1417 Input: jars[0],
1418 Output: combinedJar,
1419 })
1420 outputFile = combinedJar
1421 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001422 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001423 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001424 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001425 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001426 outputFile = combinedJar
1427 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001428
Colin Cross331a1212018-08-15 20:40:52 -07001429 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001430 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001431 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001432 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001433 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001434 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001435
1436 // jarjar resource jar if necessary
1437 if j.resourceJar != nil {
1438 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001439 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001440 j.resourceJar = resourceJarJarFile
1441 }
1442
Colin Cross0a6e0072017-08-30 14:24:55 -07001443 if ctx.Failed() {
1444 return
1445 }
1446 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001447
1448 // Check package restrictions if necessary.
1449 if len(j.properties.Permitted_packages) > 0 {
1450 // Check packages and copy to package-checked file.
1451 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1452 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1453 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1454
1455 if ctx.Failed() {
1456 return
1457 }
1458 }
1459
Nan Zhanged19fc32017-10-19 13:06:22 -07001460 j.implementationJarFile = outputFile
1461 if j.headerJarFile == nil {
1462 j.headerJarFile = j.implementationJarFile
1463 }
Colin Cross2fe66872015-03-30 17:20:39 -07001464
Colin Cross3144dfc2018-01-03 15:06:47 -08001465 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001466 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1467 }
1468
Colin Cross331a1212018-08-15 20:40:52 -07001469 // merge implementation jar with resources if necessary
1470 implementationAndResourcesJar := outputFile
1471 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001472 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001473 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001474 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001475 false, nil, nil)
1476 implementationAndResourcesJar = combinedJar
1477 }
1478
1479 j.implementationAndResourcesJar = implementationAndResourcesJar
1480
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001481 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001482 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001483 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001484 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001485 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001486 if ctx.Failed() {
1487 return
1488 }
Colin Cross331a1212018-08-15 20:40:52 -07001489
Jiyong Park09cb6292019-07-15 15:29:23 +09001490 // Hidden API CSV generation and dex encoding
1491 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1492 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001493
Colin Cross331a1212018-08-15 20:40:52 -07001494 // merge dex jar with resources if necessary
1495 if j.resourceJar != nil {
1496 jars := android.Paths{dexOutputFile, j.resourceJar}
1497 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1498 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1499 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001500 if j.deviceProperties.UncompressDex {
1501 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1502 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1503 dexOutputFile = combinedAlignedJar
1504 } else {
1505 dexOutputFile = combinedJar
1506 }
Colin Cross331a1212018-08-15 20:40:52 -07001507 }
1508
1509 j.dexJarFile = dexOutputFile
1510
Colin Cross8faf8fc2019-01-16 15:15:52 -08001511 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001512 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1513
1514 j.maybeStrippedDexJarFile = dexOutputFile
1515
Colin Cross3063b782018-08-15 11:19:12 -07001516 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001517
1518 if ctx.Failed() {
1519 return
1520 }
Colin Cross331a1212018-08-15 20:40:52 -07001521 } else {
1522 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001523 }
Colin Cross331a1212018-08-15 20:40:52 -07001524
Colin Crossb7a63242015-04-16 14:09:14 -07001525 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001526
1527 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1528 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001529}
1530
Colin Cross3b706fd2019-09-05 16:44:18 -07001531func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1532 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1533
1534 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1535 if idx >= 0 {
1536 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1537 jarName += strconv.Itoa(idx)
1538 }
1539
1540 classes := android.PathForModuleOut(ctx, "javac", jarName)
1541 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1542
1543 if ctx.Config().EmitXrefRules() {
1544 extractionFile := android.PathForModuleOut(ctx, kzipName)
1545 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1546 j.kytheFiles = append(j.kytheFiles, extractionFile)
1547 }
1548
1549 return classes
1550}
1551
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001552// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1553// since some of these flags may be used internally.
1554func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1555 for _, flag := range flags {
1556 flag = strings.TrimSpace(flag)
1557
1558 if !strings.HasPrefix(flag, "-") {
1559 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1560 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1561 ctx.PropertyErrorf("kotlincflags",
1562 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1563 } else if inList(flag, config.KotlincIllegalFlags) {
1564 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1565 } else if flag == "-include-runtime" {
1566 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1567 } else {
1568 args := strings.Split(flag, " ")
1569 if args[0] == "-kotlin-home" {
1570 ctx.PropertyErrorf("kotlincflags",
1571 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1572 }
1573 }
1574 }
1575}
1576
Colin Cross8eadbf02017-10-24 17:46:00 -07001577func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001578 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001579
1580 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001581 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001582 // Compile java sources into turbine.jar.
1583 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1584 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1585 if ctx.Failed() {
1586 return nil
1587 }
1588 jars = append(jars, turbineJar)
1589 }
1590
Colin Cross55f63ea2018-08-27 12:37:09 -07001591 jars = append(jars, extraJars...)
1592
Nan Zhanged19fc32017-10-19 13:06:22 -07001593 // Combine any static header libraries into classes-header.jar. If there is only
1594 // one input jar this step will be skipped.
1595 var headerJar android.Path
1596 jars = append(jars, deps.staticHeaderJars...)
1597
Colin Cross5c6ecc12017-10-23 18:12:27 -07001598 // we cannot skip the combine step for now if there is only one jar
1599 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1600 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001601 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001602 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001603 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001604
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001605 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001606 // Transform classes.jar into classes-jarjar.jar
1607 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001608 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001609 headerJar = jarjarFile
1610 if ctx.Failed() {
1611 return nil
1612 }
1613 }
1614
1615 return headerJar
1616}
1617
Colin Crosscb933592017-11-22 13:49:43 -08001618func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001619 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001620
Colin Cross7a3139e2017-12-19 13:57:50 -08001621 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001622
Colin Cross84c38822018-01-03 15:59:46 -08001623 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001624 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1625
1626 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1627
1628 j.jacocoReportClassesFile = jacocoReportClassesFile
1629
1630 return instrumentedJar
1631}
1632
albaltai36ff7dc2018-12-25 14:35:23 +08001633var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001634
Nan Zhanged19fc32017-10-19 13:06:22 -07001635func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001636 if j.headerJarFile == nil {
1637 return nil
1638 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001639 return android.Paths{j.headerJarFile}
1640}
1641
1642func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001643 if j.implementationJarFile == nil {
1644 return nil
1645 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001646 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001647}
1648
Colin Crossf24a22a2019-01-31 14:12:44 -08001649func (j *Module) DexJar() android.Path {
1650 return j.dexJarFile
1651}
1652
Colin Cross331a1212018-08-15 20:40:52 -07001653func (j *Module) ResourceJars() android.Paths {
1654 if j.resourceJar == nil {
1655 return nil
1656 }
1657 return android.Paths{j.resourceJar}
1658}
1659
1660func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001661 if j.implementationAndResourcesJar == nil {
1662 return nil
1663 }
Colin Cross331a1212018-08-15 20:40:52 -07001664 return android.Paths{j.implementationAndResourcesJar}
1665}
1666
Colin Cross46c9b8b2017-06-22 16:51:17 -07001667func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001668 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001669 return j.exportAidlIncludeDirs
1670}
1671
Jiyong Park1be96912018-05-28 18:02:19 +09001672func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001673 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001674 return j.exportedSdkLibs
1675}
1676
Artur Satayev9cf46692019-11-26 18:08:34 +00001677func (j *Module) ExportedPlugins() (android.Paths, []string) {
1678 return j.exportedPluginJars, j.exportedPluginClasses
1679}
1680
Colin Cross0c4ce212019-05-03 15:28:19 -07001681func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1682 return j.srcJarArgs, j.srcJarDeps
1683}
1684
Colin Cross46c9b8b2017-06-22 16:51:17 -07001685var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001686
Colin Cross46c9b8b2017-06-22 16:51:17 -07001687func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001688 return j.logtagsSrcs
1689}
1690
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001691// Collect information for opening IDE project files in java/jdeps.go.
1692func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1693 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1694 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001695 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001696 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001697 if j.expandJarjarRules != nil {
1698 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001699 }
1700}
1701
1702func (j *Module) CompilerDeps() []string {
1703 jdeps := []string{}
1704 jdeps = append(jdeps, j.properties.Libs...)
1705 jdeps = append(jdeps, j.properties.Static_libs...)
1706 return jdeps
1707}
1708
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001709func (j *Module) hasCode(ctx android.ModuleContext) bool {
1710 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1711 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1712}
1713
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001714func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1715 depTag := ctx.OtherModuleDependencyTag(dep)
1716 // dependencies other than the static linkage are all considered crossing APEX boundary
1717 return depTag == staticLibTag
1718}
1719
Jiyong Park0b238752019-10-29 11:23:10 +09001720func (j *Module) Stem() string {
1721 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1722}
1723
Jiyong Park618922e2020-01-08 13:35:43 +09001724func (j *Module) JacocoReportClassesFile() android.Path {
1725 return j.jacocoReportClassesFile
1726}
1727
Colin Cross2fe66872015-03-30 17:20:39 -07001728//
1729// Java libraries (.jar file)
1730//
1731
Colin Crossf506d872017-07-19 15:53:04 -07001732type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001733 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001734
1735 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001736}
1737
Colin Cross42be7612019-02-21 18:12:14 -08001738func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001739 // Store uncompressed (and do not strip) dex files from boot class path jars.
1740 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1741 return true
1742 }
1743
1744 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001745 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001746 return true
1747 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001748 if ctx.Config().UncompressPrivAppDex() &&
1749 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1750 return true
1751 }
1752
Colin Cross2fc72f62018-12-21 12:59:54 -08001753 return false
1754}
1755
Colin Crossf506d872017-07-19 15:53:04 -07001756func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001757 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001758 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001759 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001760 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001761 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001762 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001763 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001764
Jiyong Park7f7766d2019-07-25 22:02:35 +09001765 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1766 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001767 var extraInstallDeps android.Paths
1768 if j.InstallMixin != nil {
1769 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1770 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001771 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001772 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001773 }
Colin Crossb7a63242015-04-16 14:09:14 -07001774}
1775
Colin Crossf506d872017-07-19 15:53:04 -07001776func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001777 j.deps(ctx)
1778}
1779
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001780const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001781 aidlIncludeDir = "aidl"
1782 javaDir = "java"
1783 jarFileSuffix = ".jar"
1784 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001785)
1786
Paul Duffina0dbf432019-12-05 11:25:53 +00001787// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001788func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1789 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1790}
1791
1792func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1793 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001794}
1795
Paul Duffin13879572019-11-28 14:31:38 +00001796type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001797 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001798}
1799
1800func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1801 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1802}
1803
1804func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1805 _, ok := module.(*Library)
1806 return ok
1807}
1808
Paul Duffina0dbf432019-12-05 11:25:53 +00001809func (mt *librarySdkMemberType) buildSnapshot(
1810 sdkModuleContext android.ModuleContext,
1811 builder android.SnapshotBuilder,
1812 member android.SdkMember,
1813 jarToExportGetter func(j *Library) android.Path) {
1814
Paul Duffin13879572019-11-28 14:31:38 +00001815 variants := member.Variants()
1816 if len(variants) != 1 {
1817 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1818 for _, variant := range variants {
1819 sdkModuleContext.ModuleErrorf(" %q", variant)
1820 }
1821 }
1822 variant := variants[0]
1823 j := variant.(*Library)
1824
Paul Duffina0dbf432019-12-05 11:25:53 +00001825 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001826 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001827 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001828
1829 for _, dir := range j.AidlIncludeDirs() {
1830 // TODO(jiyong): copy parcelable declarations only
1831 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1832 for _, file := range aidlFiles {
1833 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1834 }
1835 }
1836
Paul Duffin9d8d6092019-12-05 18:19:29 +00001837 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001838 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001839}
1840
Paul Duffina0dbf432019-12-05 11:25:53 +00001841type headerLibrarySdkMemberType struct {
1842 librarySdkMemberType
1843}
1844
1845func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1846 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1847 headerJars := j.HeaderJars()
1848 if len(headerJars) != 1 {
1849 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1850 }
1851
1852 return headerJars[0]
1853 })
1854}
1855
Paul Duffina0dbf432019-12-05 11:25:53 +00001856type implLibrarySdkMemberType struct {
1857 librarySdkMemberType
1858}
1859
1860func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1861 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1862 implementationJars := j.ImplementationJars()
1863 if len(implementationJars) != 1 {
1864 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1865 }
1866
1867 return implementationJars[0]
1868 })
1869}
1870
Colin Cross1b16b0e2019-02-12 14:41:32 -08001871// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1872//
1873// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1874// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1875// as a `static_libs` dependency of another module.
1876//
1877// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1878// a device.
1879//
1880// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1881// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001882func LibraryFactory() android.Module {
1883 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001884
Colin Cross9ae1b922018-06-26 17:59:05 -07001885 module.AddProperties(
1886 &module.Module.properties,
1887 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001888 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001889 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001890
Jiyong Park7f7766d2019-07-25 22:02:35 +09001891 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001892 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001893 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001894 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001895}
1896
Colin Cross1b16b0e2019-02-12 14:41:32 -08001897// java_library_static is an obsolete alias for java_library.
1898func LibraryStaticFactory() android.Module {
1899 return LibraryFactory()
1900}
1901
1902// java_library_host builds and links sources into a `.jar` file for the host.
1903//
1904// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1905// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001906func LibraryHostFactory() android.Module {
1907 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001908
Colin Cross6af17aa2017-09-20 12:59:05 -07001909 module.AddProperties(
1910 &module.Module.properties,
1911 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001912
Colin Cross9ae1b922018-06-26 17:59:05 -07001913 module.Module.properties.Installable = proptools.BoolPtr(true)
1914
Jiyong Park7f7766d2019-07-25 22:02:35 +09001915 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001916 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001917 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001918}
1919
1920//
Colin Crossb628ea52018-08-14 16:42:33 -07001921// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001922//
1923
1924type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001925 // list of compatibility suites (for example "cts", "vts") that the module should be
1926 // installed into.
1927 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001928
1929 // the name of the test configuration (for example "AndroidTest.xml") that should be
1930 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001931 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001932
Jack He33338892018-09-19 02:21:28 -07001933 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1934 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001935 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001936
Colin Crossd96ca352018-08-10 16:06:24 -07001937 // list of files or filegroup modules that provide data that should be installed alongside
1938 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001939 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001940
1941 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1942 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1943 // explicitly.
1944 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001945}
1946
Paul Duffin42df1442019-03-20 12:45:53 +00001947type testHelperLibraryProperties 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
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001953type prebuiltTestProperties struct {
1954 // list of compatibility suites (for example "cts", "vts") that the module should be
1955 // installed into.
1956 Test_suites []string `android:"arch_variant"`
1957
1958 // the name of the test configuration (for example "AndroidTest.xml") that should be
1959 // installed with the module.
1960 Test_config *string `android:"path,arch_variant"`
1961}
1962
Colin Cross05638fc2018-04-09 18:40:24 -07001963type Test struct {
1964 Library
1965
1966 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001967
1968 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001969 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001970}
1971
Paul Duffin42df1442019-03-20 12:45:53 +00001972type TestHelperLibrary struct {
1973 Library
1974
1975 testHelperLibraryProperties testHelperLibraryProperties
1976}
1977
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001978type JavaTestImport struct {
1979 Import
1980
1981 prebuiltTestProperties prebuiltTestProperties
1982
1983 testConfig android.Path
1984}
1985
Colin Cross303e21f2018-08-07 16:49:25 -07001986func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001987 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1988 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001989 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001990
1991 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001992}
1993
Paul Duffin42df1442019-03-20 12:45:53 +00001994func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1995 j.Library.GenerateAndroidBuildActions(ctx)
1996}
1997
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001998func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1999 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2000 j.prebuiltTestProperties.Test_suites, nil)
2001
2002 j.Import.GenerateAndroidBuildActions(ctx)
2003}
2004
2005type testSdkMemberType struct {
2006 android.SdkMemberTypeBase
2007}
2008
2009func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2010 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2011}
2012
2013func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2014 _, ok := module.(*Test)
2015 return ok
2016}
2017
2018func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2019 variants := member.Variants()
2020 if len(variants) != 1 {
2021 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2022 for _, variant := range variants {
2023 sdkModuleContext.ModuleErrorf(" %q", variant)
2024 }
2025 }
2026 variant := variants[0]
2027 j := variant.(*Test)
2028
2029 implementationJars := j.ImplementationJars()
2030 if len(implementationJars) != 1 {
2031 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2032 }
2033
2034 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2035 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2036
2037 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2038 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2039
2040 module := builder.AddPrebuiltModule(member, "java_test_import")
2041 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2042 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2043}
2044
Colin Cross1b16b0e2019-02-12 14:41:32 -08002045// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2046// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2047//
2048// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2049// compiled against the device bootclasspath.
2050//
2051// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2052// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002053func TestFactory() android.Module {
2054 module := &Test{}
2055
2056 module.AddProperties(
2057 &module.Module.properties,
2058 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002059 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002060 &module.Module.protoProperties,
2061 &module.testProperties)
2062
Colin Cross9ae1b922018-06-26 17:59:05 -07002063 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002064 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002065
Colin Cross05638fc2018-04-09 18:40:24 -07002066 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002067 return module
2068}
2069
Paul Duffin42df1442019-03-20 12:45:53 +00002070// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2071func TestHelperLibraryFactory() android.Module {
2072 module := &TestHelperLibrary{}
2073
2074 module.AddProperties(
2075 &module.Module.properties,
2076 &module.Module.deviceProperties,
2077 &module.Module.dexpreoptProperties,
2078 &module.Module.protoProperties,
2079 &module.testHelperLibraryProperties)
2080
Colin Cross9a4abed2019-04-24 13:19:28 -07002081 module.Module.properties.Installable = proptools.BoolPtr(true)
2082 module.Module.dexpreopter.isTest = true
2083
Paul Duffin42df1442019-03-20 12:45:53 +00002084 InitJavaModule(module, android.HostAndDeviceSupported)
2085 return module
2086}
2087
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002088// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2089// and makes sure that it is added to the appropriate test suite.
2090//
2091// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2092// compiled against an Android classpath.
2093//
2094// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2095// for host modules.
2096func JavaTestImportFactory() android.Module {
2097 module := &JavaTestImport{}
2098
2099 module.AddProperties(
2100 &module.Import.properties,
2101 &module.prebuiltTestProperties)
2102
2103 module.Import.properties.Installable = proptools.BoolPtr(true)
2104
2105 android.InitPrebuiltModule(module, &module.properties.Jars)
2106 android.InitApexModule(module)
2107 android.InitSdkAwareModule(module)
2108 InitJavaModule(module, android.HostAndDeviceSupported)
2109 return module
2110}
2111
Colin Cross1b16b0e2019-02-12 14:41:32 -08002112// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2113// allow running the test with `atest` or a `TEST_MAPPING` file.
2114//
2115// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2116// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002117func TestHostFactory() android.Module {
2118 module := &Test{}
2119
2120 module.AddProperties(
2121 &module.Module.properties,
2122 &module.Module.protoProperties,
2123 &module.testProperties)
2124
Colin Cross9ae1b922018-06-26 17:59:05 -07002125 module.Module.properties.Installable = proptools.BoolPtr(true)
2126
Colin Cross05638fc2018-04-09 18:40:24 -07002127 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002128 return module
2129}
2130
2131//
Colin Cross2fe66872015-03-30 17:20:39 -07002132// Java Binaries (.jar file plus wrapper script)
2133//
2134
Colin Crossf506d872017-07-19 15:53:04 -07002135type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002136 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002137 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002138
2139 // Name of the class containing main to be inserted into the manifest as Main-Class.
2140 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002141}
2142
Colin Crossf506d872017-07-19 15:53:04 -07002143type Binary struct {
2144 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002145
Colin Crossf506d872017-07-19 15:53:04 -07002146 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002147
Colin Cross6b4a32d2017-12-05 13:42:45 -08002148 isWrapperVariant bool
2149
Colin Crossc3315992017-12-08 19:12:36 -08002150 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002151 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002152}
2153
Alex Light24237172017-10-26 09:46:21 -07002154func (j *Binary) HostToolPath() android.OptionalPath {
2155 return android.OptionalPathForPath(j.binaryFile)
2156}
2157
Colin Crossf506d872017-07-19 15:53:04 -07002158func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002159 if ctx.Arch().ArchType == android.Common {
2160 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002161 if j.binaryProperties.Main_class != nil {
2162 if j.properties.Manifest != nil {
2163 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2164 }
2165 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2166 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2167 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2168 }
2169
Colin Cross6b4a32d2017-12-05 13:42:45 -08002170 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002171 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002172 // Handle the binary wrapper
2173 j.isWrapperVariant = true
2174
Colin Cross366938f2017-12-11 16:29:02 -08002175 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002176 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002177 } else {
2178 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2179 }
2180
2181 // Depend on the installed jar so that the wrapper doesn't get executed by
2182 // another build rule before the jar has been installed.
2183 jarFile := ctx.PrimaryModule().(*Binary).installFile
2184
2185 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2186 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002187 }
Colin Cross2fe66872015-03-30 17:20:39 -07002188}
2189
Colin Crossf506d872017-07-19 15:53:04 -07002190func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002191 if ctx.Arch().ArchType == android.Common {
2192 j.deps(ctx)
2193 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002194}
2195
Colin Cross1b16b0e2019-02-12 14:41:32 -08002196// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2197// as well.
2198//
2199// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2200// compiled against the device bootclasspath.
2201//
2202// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2203// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002204func BinaryFactory() android.Module {
2205 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002206
Colin Cross36242852017-06-23 15:06:31 -07002207 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002208 &module.Module.properties,
2209 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002210 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002211 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002212 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002213
Colin Cross9ae1b922018-06-26 17:59:05 -07002214 module.Module.properties.Installable = proptools.BoolPtr(true)
2215
Colin Cross6b4a32d2017-12-05 13:42:45 -08002216 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2217 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002218 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002219}
2220
Colin Cross1b16b0e2019-02-12 14:41:32 -08002221// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2222//
2223// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2224// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002225func BinaryHostFactory() android.Module {
2226 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002227
Colin Cross36242852017-06-23 15:06:31 -07002228 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002229 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002230 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002231 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002232
Colin Cross9ae1b922018-06-26 17:59:05 -07002233 module.Module.properties.Installable = proptools.BoolPtr(true)
2234
Colin Cross6b4a32d2017-12-05 13:42:45 -08002235 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2236 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002237 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002238}
2239
2240//
2241// Java prebuilts
2242//
2243
Colin Cross74d73e22017-08-02 11:05:49 -07002244type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002245 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002246
Nan Zhangea568a42017-11-08 21:20:04 -08002247 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002248
2249 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002250
2251 // List of shared java libs that this module has dependencies to
2252 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002253
2254 // List of files to remove from the jar file(s)
2255 Exclude_files []string
2256
2257 // List of directories to remove from the jar file(s)
2258 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002259
2260 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002261 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002262
2263 // set the name of the output
2264 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002265}
2266
2267type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002268 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002269 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002270 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002271 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002272 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002273
Colin Cross74d73e22017-08-02 11:05:49 -07002274 properties ImportProperties
2275
Colin Cross0a6e0072017-08-30 14:24:55 -07002276 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002277 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002278}
2279
Colin Cross83bb3162018-06-25 15:48:06 -07002280func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002281 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002282}
2283
2284func (j *Import) minSdkVersion() string {
2285 return j.sdkVersion()
2286}
2287
Colin Cross74d73e22017-08-02 11:05:49 -07002288func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002289 return &j.prebuilt
2290}
2291
Colin Cross74d73e22017-08-02 11:05:49 -07002292func (j *Import) PrebuiltSrcs() []string {
2293 return j.properties.Jars
2294}
2295
2296func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002297 return j.prebuilt.Name(j.ModuleBase.Name())
2298}
2299
Jiyong Park0b238752019-10-29 11:23:10 +09002300func (j *Import) Stem() string {
2301 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2302}
2303
Jiyong Park618922e2020-01-08 13:35:43 +09002304func (a *Import) JacocoReportClassesFile() android.Path {
2305 return nil
2306}
2307
Colin Cross74d73e22017-08-02 11:05:49 -07002308func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002309 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002310}
2311
Colin Cross74d73e22017-08-02 11:05:49 -07002312func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002313 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002314
Jiyong Park0b238752019-10-29 11:23:10 +09002315 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002316 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002317 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2318 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002319 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002320 inputFile := outputFile
2321 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2322 TransformJetifier(ctx, outputFile, inputFile)
2323 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002324 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002325
2326 ctx.VisitDirectDeps(func(module android.Module) {
2327 otherName := ctx.OtherModuleName(module)
2328 tag := ctx.OtherModuleDependencyTag(module)
2329
2330 switch dep := module.(type) {
2331 case Dependency:
2332 switch tag {
2333 case libTag, staticLibTag:
2334 // sdk lib names from dependencies are re-exported
2335 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2336 }
2337 case SdkLibraryDependency:
2338 switch tag {
2339 case libTag:
2340 // names of sdk libs that are directly depended are exported
2341 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2342 }
2343 }
2344 })
2345
2346 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002347 if Bool(j.properties.Installable) {
2348 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002349 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002350 }
Colin Cross2fe66872015-03-30 17:20:39 -07002351}
2352
Colin Cross74d73e22017-08-02 11:05:49 -07002353var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002354
Nan Zhanged19fc32017-10-19 13:06:22 -07002355func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002356 if j.combinedClasspathFile == nil {
2357 return nil
2358 }
Colin Cross37f6d792018-07-12 12:28:41 -07002359 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002360}
2361
2362func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002363 if j.combinedClasspathFile == nil {
2364 return nil
2365 }
Colin Cross37f6d792018-07-12 12:28:41 -07002366 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002367}
2368
Colin Cross331a1212018-08-15 20:40:52 -07002369func (j *Import) ResourceJars() android.Paths {
2370 return nil
2371}
2372
2373func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002374 if j.combinedClasspathFile == nil {
2375 return nil
2376 }
Colin Cross331a1212018-08-15 20:40:52 -07002377 return android.Paths{j.combinedClasspathFile}
2378}
2379
Colin Crossf24a22a2019-01-31 14:12:44 -08002380func (j *Import) DexJar() android.Path {
2381 return nil
2382}
2383
Colin Cross74d73e22017-08-02 11:05:49 -07002384func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002385 return nil
2386}
2387
Jiyong Park1be96912018-05-28 18:02:19 +09002388func (j *Import) ExportedSdkLibs() []string {
2389 return j.exportedSdkLibs
2390}
2391
Artur Satayev9cf46692019-11-26 18:08:34 +00002392func (j *Import) ExportedPlugins() (android.Paths, []string) {
2393 return nil, nil
2394}
2395
Colin Cross0c4ce212019-05-03 15:28:19 -07002396func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2397 return nil, nil
2398}
2399
albaltai36ff7dc2018-12-25 14:35:23 +08002400// Add compile time check for interface implementation
2401var _ android.IDEInfo = (*Import)(nil)
2402var _ android.IDECustomizedModuleName = (*Import)(nil)
2403
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002404// Collect information for opening IDE project files in java/jdeps.go.
2405const (
2406 removedPrefix = "prebuilt_"
2407)
2408
2409func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2410 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2411}
2412
2413func (j *Import) IDECustomizedModuleName() string {
2414 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2415 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2416 // solution to get the Import name.
2417 name := j.Name()
2418 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002419 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002420 }
2421 return name
2422}
2423
Colin Cross74d73e22017-08-02 11:05:49 -07002424var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002425
Colin Cross1b16b0e2019-02-12 14:41:32 -08002426// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2427//
2428// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2429// compiled against an Android classpath.
2430//
2431// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2432// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002433func ImportFactory() android.Module {
2434 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002435
Colin Cross74d73e22017-08-02 11:05:49 -07002436 module.AddProperties(&module.properties)
2437
2438 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002439 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002440 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002441 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002442 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002443}
2444
Colin Cross1b16b0e2019-02-12 14:41:32 -08002445// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2446// module.
2447//
2448// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2449// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002450func ImportFactoryHost() android.Module {
2451 module := &Import{}
2452
2453 module.AddProperties(&module.properties)
2454
2455 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002456 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002457 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002458 return module
2459}
2460
Colin Cross42be7612019-02-21 18:12:14 -08002461// dex_import module
2462
2463type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002464 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002465
2466 // set the name of the output
2467 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002468}
2469
2470type DexImport struct {
2471 android.ModuleBase
2472 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002473 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002474 prebuilt android.Prebuilt
2475
2476 properties DexImportProperties
2477
2478 dexJarFile android.Path
2479 maybeStrippedDexJarFile android.Path
2480
2481 dexpreopter
2482}
2483
2484func (j *DexImport) Prebuilt() *android.Prebuilt {
2485 return &j.prebuilt
2486}
2487
2488func (j *DexImport) PrebuiltSrcs() []string {
2489 return j.properties.Jars
2490}
2491
2492func (j *DexImport) Name() string {
2493 return j.prebuilt.Name(j.ModuleBase.Name())
2494}
2495
Jiyong Park0b238752019-10-29 11:23:10 +09002496func (j *DexImport) Stem() string {
2497 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2498}
2499
Colin Cross42be7612019-02-21 18:12:14 -08002500func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2501 if len(j.properties.Jars) != 1 {
2502 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2503 }
2504
Jiyong Park0b238752019-10-29 11:23:10 +09002505 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002506 j.dexpreopter.isInstallable = true
2507 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2508
2509 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2510 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2511
2512 if j.dexpreopter.uncompressedDex {
2513 rule := android.NewRuleBuilder()
2514
2515 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2516 rule.Temporary(temporary)
2517
2518 // use zip2zip to uncompress classes*.dex files
2519 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002520 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002521 FlagWithInput("-i ", inputJar).
2522 FlagWithOutput("-o ", temporary).
2523 FlagWithArg("-0 ", "'classes*.dex'")
2524
2525 // use zipalign to align uncompressed classes*.dex files
2526 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002527 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002528 Flag("-f").
2529 Text("4").
2530 Input(temporary).
2531 Output(dexOutputFile)
2532
2533 rule.DeleteTemporaryFiles()
2534
2535 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2536 } else {
2537 ctx.Build(pctx, android.BuildParams{
2538 Rule: android.Cp,
2539 Input: inputJar,
2540 Output: dexOutputFile,
2541 })
2542 }
2543
2544 j.dexJarFile = dexOutputFile
2545
2546 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2547
2548 j.maybeStrippedDexJarFile = dexOutputFile
2549
2550 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2551 ctx.ModuleName()+".jar", dexOutputFile)
2552}
2553
2554func (j *DexImport) DexJar() android.Path {
2555 return j.dexJarFile
2556}
2557
2558// dex_import imports a `.jar` file containing classes.dex files.
2559//
2560// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2561// to the device.
2562func DexImportFactory() android.Module {
2563 module := &DexImport{}
2564
2565 module.AddProperties(&module.properties)
2566
2567 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002568 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002569 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002570 return module
2571}
2572
Colin Cross89536d42017-07-07 14:35:50 -07002573//
2574// Defaults
2575//
2576type Defaults struct {
2577 android.ModuleBase
2578 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002579 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002580}
2581
Colin Cross1b16b0e2019-02-12 14:41:32 -08002582// java_defaults provides a set of properties that can be inherited by other java or android modules.
2583//
2584// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2585// property in the defaults module that exists in the depending module will be prepended to the depending module's
2586// value for that property.
2587//
2588// Example:
2589//
2590// java_defaults {
2591// name: "example_defaults",
2592// srcs: ["common/**/*.java"],
2593// javacflags: ["-Xlint:all"],
2594// aaptflags: ["--auto-add-overlay"],
2595// }
2596//
2597// java_library {
2598// name: "example",
2599// defaults: ["example_defaults"],
2600// srcs: ["example/**/*.java"],
2601// }
2602//
2603// is functionally identical to:
2604//
2605// java_library {
2606// name: "example",
2607// srcs: [
2608// "common/**/*.java",
2609// "example/**/*.java",
2610// ],
2611// javacflags: ["-Xlint:all"],
2612// }
Colin Cross89536d42017-07-07 14:35:50 -07002613func defaultsFactory() android.Module {
2614 return DefaultsFactory()
2615}
2616
Paul Duffin47357662019-12-05 14:07:14 +00002617func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002618 module := &Defaults{}
2619
Colin Cross89536d42017-07-07 14:35:50 -07002620 module.AddProperties(
2621 &CompilerProperties{},
2622 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002623 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002624 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002625 &aaptProperties{},
2626 &androidLibraryProperties{},
2627 &appProperties{},
2628 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002629 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002630 &ImportProperties{},
2631 &AARImportProperties{},
2632 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002633 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002634 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002635 )
2636
2637 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002638 return module
2639}
Nan Zhangea568a42017-11-08 21:20:04 -08002640
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002641func kytheExtractJavaFactory() android.Singleton {
2642 return &kytheExtractJavaSingleton{}
2643}
2644
2645type kytheExtractJavaSingleton struct {
2646}
2647
2648func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2649 var xrefTargets android.Paths
2650 ctx.VisitAllModules(func(module android.Module) {
2651 if javaModule, ok := module.(xref); ok {
2652 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2653 }
2654 })
2655 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2656 if len(xrefTargets) > 0 {
2657 ctx.Build(pctx, android.BuildParams{
2658 Rule: blueprint.Phony,
2659 Output: android.PathForPhony(ctx, "xref_java"),
2660 Inputs: xrefTargets,
2661 })
2662 }
2663}
2664
Nan Zhangea568a42017-11-08 21:20:04 -08002665var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002666var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002667var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002668var inList = android.InList