blob: f58e5ba24d4f90065d1c6f3a9d38dc5ff0ca2501 [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 Duffin47357662019-12-05 14:07:14 +000037 android.RegisterModuleType("java_defaults", DefaultsFactory)
Colin Cross89536d42017-07-07 14:35:50 -070038
Colin Cross9ae1b922018-06-26 17:59:05 -070039 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080040 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070041 android.RegisterModuleType("java_library_host", LibraryHostFactory)
42 android.RegisterModuleType("java_binary", BinaryFactory)
43 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070044 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000045 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070046 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070047 android.RegisterModuleType("java_import", ImportFactory)
48 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080049 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
50 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080051 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070052
Colin Cross798bfce2016-10-12 14:28:16 -070053 android.RegisterSingletonType("logtags", LogtagsSingleton)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080054 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Jeongik Cha2cc570d2019-10-29 15:44:45 +090057func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
58 if j.SocSpecific() || j.DeviceSpecific() ||
59 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
60 if sc, ok := ctx.Module().(sdkContext); ok {
61 if sc.sdkVersion() == "" {
62 ctx.PropertyErrorf("sdk_version",
63 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
64 }
65 }
66 }
67}
68
Jeongik Cha538c0d02019-07-11 15:54:27 +090069func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
70 if sc, ok := ctx.Module().(sdkContext); ok {
71 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
72 if usePlatformAPI != (sc.sdkVersion() == "") {
73 if usePlatformAPI {
74 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
75 } else {
76 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
77 }
78 }
79
80 }
81}
82
Colin Cross2fe66872015-03-30 17:20:39 -070083// TODO:
84// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070085// Renderscript
86// Post-jar passes:
87// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070088// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070089// DroidDoc
90// Findbugs
91
Colin Cross89536d42017-07-07 14:35:50 -070092type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070093 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
94 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080095 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070096
97 // list of source files that should not be used to build the Java module.
98 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080099 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700102 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross86a63ff2017-09-27 17:33:10 -0700104 // list of directories that should be excluded from java_resource_dirs
105 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700106
Colin Cross0f37af02017-09-27 17:42:05 -0700107 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700109
Colin Crosscedd4762018-09-13 11:26:19 -0700110 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800111 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700112
Colin Cross7d5136f2015-05-11 13:39:40 -0700113 // list of module-specific flags that will be used for javac compiles
114 Javacflags []string `android:"arch_variant"`
115
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200116 // list of module-specific flags that will be used for kotlinc compiles
117 Kotlincflags []string `android:"arch_variant"`
118
Colin Cross7d5136f2015-05-11 13:39:40 -0700119 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700120 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700121
122 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700123 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700124
125 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800126 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700127
Colin Cross540eff82017-06-22 17:01:52 -0700128 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800129 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700130
131 // If not blank, set the java version passed to javac as -source and -target
132 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700133
Colin Cross9ae1b922018-06-26 17:59:05 -0700134 // If set to true, allow this module to be dexed and installed on devices. Has no
135 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700136 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700137
Colin Cross0f37af02017-09-27 17:42:05 -0700138 // If set to true, include sources used to compile the module in to the final jar
139 Include_srcs *bool
140
Vladimir Marko0975ee02019-04-02 10:29:55 +0100141 // If not empty, classes are restricted to the specified packages and their sub-packages.
142 // This restriction is checked after applying jarjar rules and including static libs.
143 Permitted_packages []string
144
Colin Crossbe9cdb82019-01-21 21:37:16 -0800145 // List of modules to use as annotation processors
146 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700147
Artur Satayev9cf46692019-11-26 18:08:34 +0000148 // List of modules to export to libraries that directly depend on this library as annotation processors
149 Exported_plugins []string
150
Nan Zhang61eaedb2017-11-02 13:28:15 -0700151 // The number of Java source entries each Javac instance can process
152 Javac_shard_size *int64
153
Nan Zhang5f8cb422018-02-06 10:34:32 -0800154 // Add host jdk tools.jar to bootclasspath
155 Use_tools_jar *bool
156
Colin Cross1369cdb2017-09-29 17:58:17 -0700157 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700158 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800159 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700160
Colin Cross6cef4812019-10-17 14:23:50 -0700161 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700162 Javacflags []string
163 }
Colin Crosscb933592017-11-22 13:49:43 -0800164
Colin Cross81440082018-08-15 20:21:55 -0700165 // When compiling language level 9+ .java code in packages that are part of
166 // a system module, patch_module names the module that your sources and
167 // dependencies should be patched into. The Android runtime currently
168 // doesn't implement the JEP 261 module system so this option is only
169 // supported at compile time. It should only be needed to compile tests in
170 // packages that exist in libcore and which are inconvenient to move
171 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100172 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700173
Colin Crosscb933592017-11-22 13:49:43 -0800174 Jacoco struct {
175 // List of classes to include for instrumentation with jacoco to collect coverage
176 // information at runtime when building with coverage enabled. If unset defaults to all
177 // classes.
178 // Supports '*' as the last character of an entry in the list as a wildcard match.
179 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
180 // it matches classes in the package that have the class name as a prefix.
181 Include_filter []string
182
183 // List of classes to exclude from instrumentation with jacoco to collect coverage
184 // information at runtime when building with coverage enabled. Overrides classes selected
185 // by the include_filter property.
186 // Supports '*' as the last character of an entry in the list as a wildcard match.
187 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
188 // it matches classes in the package that have the class name as a prefix.
189 Exclude_filter []string
190 }
191
Andreas Gampef3e5b552018-01-22 21:27:21 -0800192 Errorprone struct {
193 // List of javac flags that should only be used when running errorprone.
194 Javacflags []string
195 }
196
Colin Cross0f2ee152017-12-14 15:22:43 -0800197 Proto struct {
198 // List of extra options that will be passed to the proto generator.
199 Output_params []string
200 }
201
Colin Crosscb933592017-11-22 13:49:43 -0800202 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800203
204 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800205 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700206}
207
Colin Cross89536d42017-07-07 14:35:50 -0700208type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700209 // list of module-specific flags that will be used for dex compiles
210 Dxflags []string `android:"arch_variant"`
211
Jeongik Cha538c0d02019-07-11 15:54:27 +0900212 // if not blank, set to the version of the sdk to compile against.
213 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800214 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700215
Colin Cross83bb3162018-06-25 15:48:06 -0700216 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
217 // Defaults to sdk_version if not set.
218 Min_sdk_version *string
219
Dan Willemsen419290a2018-10-31 15:28:47 -0700220 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
221 // Defaults to sdk_version if not set.
222 Target_sdk_version *string
223
Jeongik Cha356dac42019-08-19 14:09:52 +0900224 // Whether to compile against the platform APIs instead of an SDK.
225 // If true, then sdk_version must be empty. The value of this field
226 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700227 Platform_apis *bool
228
Colin Crossebe1a512017-11-14 13:12:14 -0800229 Aidl struct {
230 // Top level directories to pass to aidl tool
231 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700232
Colin Crossebe1a512017-11-14 13:12:14 -0800233 // Directories rooted at the Android.bp file to pass to aidl tool
234 Local_include_dirs []string
235
236 // directories that should be added as include directories for any aidl sources of modules
237 // that depend on this module, as well as to aidl for this module.
238 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100239
240 // whether to generate traces (for systrace) for this interface
241 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100242
243 // whether to generate Binder#GetTransaction name method.
244 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800245 }
Colin Cross92430102017-10-09 14:59:32 -0700246
247 // If true, export a copy of the module as a -hostdex module for host testing.
248 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700249
Colin Cross7f87f4f2019-04-24 13:41:45 -0700250 Target struct {
251 Hostdex struct {
252 // Additional required dependencies to add to -hostdex modules.
253 Required []string
254 }
255 }
256
David Brazdil17ef5632018-06-27 10:27:45 +0100257 // If set to true, compile dex regardless of installable. Defaults to false.
258 Compile_dex *bool
259
Colin Cross66dbc0b2017-12-28 12:23:20 -0800260 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700261 // If false, disable all optimization. Defaults to true for android_app and android_test
262 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800263 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700264 // True if the module containing this has it set by default.
265 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800266
267 // If true, optimize for size by removing unused code. Defaults to true for apps,
268 // false for libraries and tests.
269 Shrink *bool
270
271 // If true, optimize bytecode. Defaults to false.
272 Optimize *bool
273
274 // If true, obfuscate bytecode. Defaults to false.
275 Obfuscate *bool
276
277 // If true, do not use the flag files generated by aapt that automatically keep
278 // classes referenced by the app manifest. Defaults to false.
279 No_aapt_flags *bool
280
281 // Flags to pass to proguard.
282 Proguard_flags []string
283
284 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800285 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800286 }
287
Paul Duffine25c6442019-10-11 13:50:28 +0100288 // When targeting 1.9 and above, override the modules to use with --system,
289 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700290 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700291
Jiyong Park4c4c0242019-10-21 14:53:15 +0900292 // set the name of the output
293 Stem *string
294
Colin Cross5a0dcd52018-10-05 14:20:06 -0700295 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800296 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700297}
298
Sasha Smundak2057f822019-04-16 17:16:58 -0700299func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
300 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
301}
302
Colin Cross46c9b8b2017-06-22 16:51:17 -0700303// Module contains the properties and members used by all java module types
304type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700305 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700306 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900307 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900308 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700309
Colin Cross89536d42017-07-07 14:35:50 -0700310 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700311 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700312 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700313
Colin Cross331a1212018-08-15 20:40:52 -0700314 // jar file containing header classes including static library dependencies, suitable for
315 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700316 headerJarFile android.Path
317
Colin Cross331a1212018-08-15 20:40:52 -0700318 // jar file containing implementation classes including static library dependencies but no
319 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700320 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700321
Colin Cross331a1212018-08-15 20:40:52 -0700322 // jar file containing only resources including from static library dependencies
323 resourceJar android.Path
324
Colin Cross0c4ce212019-05-03 15:28:19 -0700325 // args and dependencies to package source files into a srcjar
326 srcJarArgs []string
327 srcJarDeps android.Paths
328
Colin Cross331a1212018-08-15 20:40:52 -0700329 // jar file containing implementation classes and resources including static library
330 // dependencies
331 implementationAndResourcesJar android.Path
332
333 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700334 dexJarFile android.Path
335
Colin Cross43f08db2018-11-12 10:13:39 -0800336 // output file that contains classes.dex if it should be in the output file
337 maybeStrippedDexJarFile android.Path
338
Colin Crosscb933592017-11-22 13:49:43 -0800339 // output file containing uninstrumented classes that will be instrumented by jacoco
340 jacocoReportClassesFile android.Path
341
Colin Cross66dbc0b2017-12-28 12:23:20 -0800342 // output file containing mapping of obfuscated names
343 proguardDictionary android.Path
344
Colin Cross331a1212018-08-15 20:40:52 -0700345 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700346 outputFile android.Path
347 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700348
Colin Cross635c3b02016-05-18 15:37:25 -0700349 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700350
Colin Cross635c3b02016-05-18 15:37:25 -0700351 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700352
Colin Cross2fe66872015-03-30 17:20:39 -0700353 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700354 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800355
356 // list of .java files and srcjars that was passed to javac
357 compiledJavaSrcs android.Paths
358 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800359
360 // list of extra progurad flag files
361 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900362
Colin Cross094054a2018-10-17 15:10:48 -0700363 // manifest file to use instead of properties.Manifest
364 overrideManifest android.OptionalPath
365
Artur Satayev9cf46692019-11-26 18:08:34 +0000366 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900367 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700368
Artur Satayev9cf46692019-11-26 18:08:34 +0000369 // list of plugins that this java module is exporting
370 exportedPluginJars android.Paths
371
372 // list of plugins that this java module is exporting
373 exportedPluginClasses []string
374
375 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800376 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700377 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800378
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800379 // expanded Jarjar_rules
380 expandJarjarRules android.Path
381
Vladimir Marko0975ee02019-04-02 10:29:55 +0100382 // list of additional targets for checkbuild
383 additionalCheckedModules android.Paths
384
Colin Cross988708c2019-05-06 14:04:11 -0700385 // Extra files generated by the module type to be added as java resources.
386 extraResources android.Paths
387
Colin Crossf24a22a2019-01-31 14:12:44 -0800388 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800389 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800390
391 // list of the xref extraction files
392 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700393}
394
Colin Cross41955e82019-05-29 14:40:35 -0700395func (j *Module) OutputFiles(tag string) (android.Paths, error) {
396 switch tag {
397 case "":
398 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700399 case ".jar":
400 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700401 case ".proguard_map":
402 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700403 default:
404 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
405 }
Colin Cross54250902017-12-05 09:28:08 -0800406}
407
Jiyong Park8fd61922018-11-08 02:50:25 +0900408func (j *Module) DexJarFile() android.Path {
409 return j.dexJarFile
410}
411
Colin Cross41955e82019-05-29 14:40:35 -0700412var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800413
Colin Crossf506d872017-07-19 15:53:04 -0700414type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700415 HeaderJars() android.Paths
416 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700417 ResourceJars() android.Paths
418 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800419 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700420 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900421 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000422 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700423 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700424 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700425}
426
Jiyong Parkc678ad32018-04-10 13:07:10 +0900427type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700428 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
429 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900430}
431
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800432type xref interface {
433 XrefJavaFiles() android.Paths
434}
435
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800436func (j *Module) XrefJavaFiles() android.Paths {
437 return j.kytheFiles
438}
439
Colin Cross89536d42017-07-07 14:35:50 -0700440func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
441 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
442 android.InitDefaultableModule(module)
443}
444
Colin Crossbe1da472017-07-07 15:59:46 -0700445type dependencyTag struct {
446 blueprint.BaseDependencyTag
447 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700448}
449
Colin Crossa4f08812018-10-02 22:03:40 -0700450type jniDependencyTag struct {
451 blueprint.BaseDependencyTag
452 target android.Target
453}
454
Jiyong Park8be103b2019-11-08 15:53:48 +0900455func IsJniDepTag(depTag blueprint.DependencyTag) bool {
456 _, ok := depTag.(*jniDependencyTag)
457 return ok
458}
459
Colin Crossbe1da472017-07-07 15:59:46 -0700460var (
Colin Cross4b964c02018-10-15 16:18:06 -0700461 staticLibTag = dependencyTag{name: "staticlib"}
462 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700463 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800464 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000465 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700466 bootClasspathTag = dependencyTag{name: "bootclasspath"}
467 systemModulesTag = dependencyTag{name: "system modules"}
468 frameworkResTag = dependencyTag{name: "framework-res"}
469 frameworkApkTag = dependencyTag{name: "framework-apk"}
470 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800471 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700472 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
473 certificateTag = dependencyTag{name: "certificate"}
474 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700475 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700476)
Colin Cross2fe66872015-03-30 17:20:39 -0700477
Colin Crossfc3674a2017-09-18 17:41:52 -0700478type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700479 useModule, useFiles, useDefaultLibs, invalidVersion bool
480
Colin Cross6cef4812019-10-17 14:23:50 -0700481 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
482 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100483
484 // The default system modules to use. Will be an empty string if no system
485 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700486 systemModules string
487
Colin Cross6cef4812019-10-17 14:23:50 -0700488 // The modules that will be added ot the classpath when targeting 1.9 or higher
489 java9Classpath []string
490
Colin Crossa97c5d32018-03-28 14:58:31 -0700491 frameworkResModule string
492
Colin Cross86a60ae2018-05-29 14:44:55 -0700493 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700494 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100495
496 noStandardLibs, noFrameworksLibs bool
497}
498
499func (s sdkDep) hasStandardLibs() bool {
500 return !s.noStandardLibs
501}
502
503func (s sdkDep) hasFrameworkLibs() bool {
504 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700505}
506
Colin Crossa4f08812018-10-02 22:03:40 -0700507type jniLib struct {
508 name string
509 path android.Path
510 target android.Target
511}
512
Colin Cross0ea8ba82019-06-06 14:33:29 -0700513func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800514 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
515}
516
Colin Cross0ea8ba82019-06-06 14:33:29 -0700517func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800518 return j.shouldInstrument(ctx) &&
519 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
520 ctx.Config().UnbundledBuild())
521}
522
Colin Cross83bb3162018-06-25 15:48:06 -0700523func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900524 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700525}
526
Paul Duffine25c6442019-10-11 13:50:28 +0100527func (j *Module) systemModules() string {
528 return proptools.String(j.deviceProperties.System_modules)
529}
530
Colin Cross83bb3162018-06-25 15:48:06 -0700531func (j *Module) minSdkVersion() string {
532 if j.deviceProperties.Min_sdk_version != nil {
533 return *j.deviceProperties.Min_sdk_version
534 }
535 return j.sdkVersion()
536}
537
Dan Willemsen419290a2018-10-31 15:28:47 -0700538func (j *Module) targetSdkVersion() string {
539 if j.deviceProperties.Target_sdk_version != nil {
540 return *j.deviceProperties.Target_sdk_version
541 }
542 return j.sdkVersion()
543}
544
Colin Crossbe1da472017-07-07 15:59:46 -0700545func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700546 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100547 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700548 if sdkDep.useDefaultLibs {
549 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
550 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
551 if sdkDep.hasFrameworkLibs() {
552 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700553 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700554 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700555 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100556 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700557 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700558 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
559 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
560 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
561 }
Colin Cross2fe66872015-03-30 17:20:39 -0700562 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700563
Nan Zhangb2b33de2018-02-23 11:18:47 -0800564 if ctx.ModuleName() == "android_stubs_current" ||
565 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700566 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700567 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800568 }
Colin Cross2fe66872015-03-30 17:20:39 -0700569 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700570
Colin Cross42d48b72018-08-29 14:10:52 -0700571 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
572 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700573
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700574 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000575 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800576
Colin Crossfe17f6f2019-03-28 19:30:56 -0700577 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700578 if j.hasSrcExt(".proto") {
579 protoDeps(ctx, &j.protoProperties)
580 }
Colin Cross93e85952017-08-15 13:34:18 -0700581
582 if j.hasSrcExt(".kt") {
583 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
584 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700585 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
586 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800587 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800588 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
589 }
Colin Cross93e85952017-08-15 13:34:18 -0700590 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800591
592 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700593 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800594 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700595}
596
597func hasSrcExt(srcs []string, ext string) bool {
598 for _, src := range srcs {
599 if filepath.Ext(src) == ext {
600 return true
601 }
602 }
603
604 return false
605}
606
607func (j *Module) hasSrcExt(ext string) bool {
608 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700609}
610
Colin Cross46c9b8b2017-06-22 16:51:17 -0700611func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700612 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700613
Colin Crossebe1a512017-11-14 13:12:14 -0800614 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
615 aidlIncludes = append(aidlIncludes,
616 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
617 aidlIncludes = append(aidlIncludes,
618 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700619
Colin Cross3047fa22019-04-18 10:56:44 -0700620 var flags []string
621 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700622
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700623 if aidlPreprocess.Valid() {
624 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700625 deps = append(deps, aidlPreprocess.Path())
626 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700627 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700628 }
629
Colin Cross3047fa22019-04-18 10:56:44 -0700630 if len(j.exportAidlIncludeDirs) > 0 {
631 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
632 }
633
634 if len(aidlIncludes) > 0 {
635 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
636 }
637
Colin Cross635c3b02016-05-18 15:37:25 -0700638 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800639 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700640 flags = append(flags, "-I"+src.String())
641 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700642
Martijn Coeneneab15642018-03-09 09:29:59 +0100643 if Bool(j.deviceProperties.Aidl.Generate_traces) {
644 flags = append(flags, "-t")
645 }
646
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100647 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
648 flags = append(flags, "--transaction_names")
649 }
650
Colin Cross3047fa22019-04-18 10:56:44 -0700651 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700652}
653
Colin Cross32f676a2017-09-06 13:41:06 -0700654type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800655 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700656 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800657 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700658 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800659 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700660 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700661 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700662 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700663 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800664 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700665 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700666 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700667 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700668 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800669 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800670
671 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700672}
Colin Cross2fe66872015-03-30 17:20:39 -0700673
Colin Cross54250902017-12-05 09:28:08 -0800674func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
675 for _, f := range dep.Srcs() {
676 if f.Ext() != ".jar" {
677 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
678 ctx.OtherModuleName(dep.(blueprint.Module)))
679 }
680 }
681}
682
Jiyong Park2d492942018-03-05 17:44:10 +0900683type linkType int
684
685const (
686 javaCore linkType = iota
687 javaSdk
688 javaSystem
689 javaPlatform
690)
691
Jeongik Cha75b83b02019-11-01 15:28:00 +0900692type linkTypeContext interface {
693 android.Module
694 getLinkType(name string) (ret linkType, stubs bool)
695}
696
697func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700698 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700699 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900700 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
701 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100702 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900703 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100704 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900705 return javaCore, false
706 case name == "android_system_stubs_current":
707 return javaSystem, true
708 case strings.HasPrefix(ver, "system_"):
709 return javaSystem, false
710 case name == "android_test_stubs_current":
711 return javaSystem, true
712 case strings.HasPrefix(ver, "test_"):
713 return javaPlatform, false
714 case name == "android_stubs_current":
715 return javaSdk, true
716 case ver == "current":
717 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100718 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900719 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700720 default:
721 if _, err := strconv.Atoi(ver); err != nil {
722 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
723 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900724 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900725 }
726}
727
Jeongik Cha75b83b02019-11-01 15:28:00 +0900728func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700729 if ctx.Host() {
730 return
731 }
732
Jeongik Cha75b83b02019-11-01 15:28:00 +0900733 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900734 if stubs {
735 return
736 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900737 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900738 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."
739
740 switch myLinkType {
741 case javaCore:
742 if otherLinkType != javaCore {
743 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 +0900744 ctx.OtherModuleName(to))
745 }
Jiyong Park2d492942018-03-05 17:44:10 +0900746 break
747 case javaSdk:
748 if otherLinkType != javaCore && otherLinkType != javaSdk {
749 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
750 ctx.OtherModuleName(to))
751 }
752 break
753 case javaSystem:
754 if otherLinkType == javaPlatform {
755 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
756 ctx.OtherModuleName(to))
757 }
758 break
759 case javaPlatform:
760 // no restriction on link-type
761 break
Jiyong Park750e5572018-01-31 00:20:13 +0900762 }
763}
764
Colin Cross32f676a2017-09-06 13:41:06 -0700765func (j *Module) collectDeps(ctx android.ModuleContext) deps {
766 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700767
Colin Cross300f0382018-03-06 13:11:51 -0800768 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700769 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800770 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700771 ctx.AddMissingDependencies(sdkDep.bootclasspath)
772 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800773 } else if sdkDep.useFiles {
774 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700775 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700776 deps.aidlPreprocess = sdkDep.aidl
777 } else {
778 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800779 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700780 }
781
Colin Crossd11fcda2017-10-23 17:59:01 -0700782 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700783 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700784 tag := ctx.OtherModuleDependencyTag(module)
785
Colin Crossa4f08812018-10-02 22:03:40 -0700786 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700787 // Handled by AndroidApp.collectAppDeps
788 return
789 }
790 if tag == certificateTag {
791 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700792 return
793 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900794 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900795 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900796 if to, ok := module.(linkTypeContext); ok {
797 switch tag {
798 case bootClasspathTag, libTag, staticLibTag:
799 checkLinkType(ctx, j, to, tag.(dependencyTag))
800 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700801 }
Jiyong Park750e5572018-01-31 00:20:13 +0900802 }
Colin Cross54250902017-12-05 09:28:08 -0800803 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800804 case SdkLibraryDependency:
805 switch tag {
806 case libTag:
807 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
808 // names of sdk libs that are directly depended are exported
809 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700810 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800811 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
812 }
Colin Cross54250902017-12-05 09:28:08 -0800813 case Dependency:
814 switch tag {
815 case bootClasspathTag:
816 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700817 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800818 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900819 // sdk lib names from dependencies are re-exported
820 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700821 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000822 pluginJars, pluginClasses := dep.ExportedPlugins()
823 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700824 case java9LibTag:
825 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800826 case staticLibTag:
827 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
828 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
829 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700830 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900831 // sdk lib names from dependencies are re-exported
832 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700833 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000834 pluginJars, pluginClasses := dep.ExportedPlugins()
835 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800836 case pluginTag:
837 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800838 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000839 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
840 } else {
841 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800842 }
843 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
844 } else {
845 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
846 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000847 case exportedPluginTag:
848 if plugin, ok := dep.(*Plugin); ok {
849 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
850 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
851 }
852 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
853 if plugin.pluginProperties.Processor_class != nil {
854 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
855 }
856 } else {
857 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
858 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800859 case frameworkApkTag:
860 if ctx.ModuleName() == "android_stubs_current" ||
861 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700862 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800863 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
864 // resource files out of there for aapt.
865 //
866 // Normally the package rule runs aapt, which includes the resource,
867 // but we're not running that in our package rule so just copy in the
868 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700869 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800870 }
Colin Cross54250902017-12-05 09:28:08 -0800871 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700872 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800873 case kotlinAnnotationsTag:
874 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800875 }
876
Colin Cross54250902017-12-05 09:28:08 -0800877 case android.SourceFileProducer:
878 switch tag {
879 case libTag:
880 checkProducesJars(ctx, dep)
881 deps.classpath = append(deps.classpath, dep.Srcs()...)
882 case staticLibTag:
883 checkProducesJars(ctx, dep)
884 deps.classpath = append(deps.classpath, dep.Srcs()...)
885 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
886 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800887 }
888 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700889 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100890 case bootClasspathTag:
891 // If a system modules dependency has been added to the bootclasspath
892 // then add its libs to the bootclasspath.
893 sm := module.(*SystemModules)
894 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
895
Colin Cross1369cdb2017-09-29 17:58:17 -0700896 case systemModulesTag:
897 if deps.systemModules != nil {
898 panic("Found two system module dependencies")
899 }
900 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000901 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700902 panic("Missing directory for system module dependency")
903 }
Colin Crossb77043e2019-07-16 13:57:13 -0700904 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700905 }
Colin Crossec7a0422017-07-07 14:47:12 -0700906 }
Colin Cross2fe66872015-03-30 17:20:39 -0700907 })
908
Jiyong Park1be96912018-05-28 18:02:19 +0900909 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
910
Colin Cross32f676a2017-09-06 13:41:06 -0700911 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700912}
913
Artur Satayev9cf46692019-11-26 18:08:34 +0000914func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
915 deps.processorPath = append(deps.processorPath, pluginJars...)
916 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
917}
918
Colin Cross1e743852019-10-28 11:37:20 -0700919func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800920 v := sdkContext.sdkVersion()
921 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100922 if ctx.Config().IsPdkBuild() &&
923 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700924 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800925 latestSdkVersion := 0
926 if len(sdkVersions) > 0 {
927 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
928 }
929 v = strconv.Itoa(latestSdkVersion)
930 }
931
932 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700933 if err != nil {
934 ctx.PropertyErrorf("sdk_version", "%s", err)
935 }
Nan Zhang357466b2018-04-17 17:38:36 -0700936 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700937 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700938 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -0700939 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100940 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -0700941 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -0700942 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
943 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -0700944 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -0700945 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700946 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700947 }
Nan Zhang357466b2018-04-17 17:38:36 -0700948}
949
Colin Cross1e743852019-10-28 11:37:20 -0700950type javaVersion int
951
952const (
953 JAVA_VERSION_UNSUPPORTED = 0
954 JAVA_VERSION_6 = 6
955 JAVA_VERSION_7 = 7
956 JAVA_VERSION_8 = 8
957 JAVA_VERSION_9 = 9
958)
959
960func (v javaVersion) String() string {
961 switch v {
962 case JAVA_VERSION_6:
963 return "1.6"
964 case JAVA_VERSION_7:
965 return "1.7"
966 case JAVA_VERSION_8:
967 return "1.8"
968 case JAVA_VERSION_9:
969 return "1.9"
970 default:
971 return "unsupported"
972 }
973}
974
975// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
976func (v javaVersion) usesJavaModules() bool {
977 return v >= 9
978}
979
980func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100981 switch javaVersion {
982 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -0700983 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100984 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -0700985 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100986 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -0700987 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100988 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -0700989 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100990 case "10", "11":
991 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -0700992 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100993 default:
994 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -0700995 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100996 }
997}
998
Nan Zhanged19fc32017-10-19 13:06:22 -0700999func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001000
Colin Crossf03c82b2015-04-13 13:53:40 -07001001 var flags javaBuilderFlags
1002
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001003 // javaVersion flag.
1004 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1005
Nan Zhanged19fc32017-10-19 13:06:22 -07001006 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001007 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001008 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001009 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001010 }
Colin Cross6510f912017-11-29 00:27:14 -08001011 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001012 // Override the -g flag passed globally to remove local variable debug info to reduce
1013 // disk and memory usage.
1014 javacFlags = append(javacFlags, "-g:source,lines")
1015 }
Colin Crossc228a702019-11-06 16:18:05 -08001016 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001017
Colin Cross66548102018-06-19 22:47:35 -07001018 if ctx.Config().RunErrorProne() {
1019 if config.ErrorProneClasspath == nil {
1020 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1021 }
1022
1023 errorProneFlags := []string{
1024 "-Xplugin:ErrorProne",
1025 "${config.ErrorProneChecks}",
1026 }
1027 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1028
1029 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1030 "'" + strings.Join(errorProneFlags, " ") + "'"
1031 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001032 }
1033
Nan Zhanged19fc32017-10-19 13:06:22 -07001034 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001035 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1036 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001037 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001038 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001039
Colin Crossbe9cdb82019-01-21 21:37:16 -08001040 flags.processor = strings.Join(deps.processorClasses, ",")
1041
Colin Cross1e743852019-10-28 11:37:20 -07001042 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1043 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001044 // Give host-side tools a version of OpenJDK's standard libraries
1045 // close to what they're targeting. As of Dec 2017, AOSP is only
1046 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1047 //
1048 // When building with OpenJDK 8, the following should have no
1049 // effect since those jars would be available by default.
1050 //
1051 // When building with OpenJDK 9 but targeting a version < 1.8,
1052 // putting them on the bootclasspath means that:
1053 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1054 // b) references to existing APIs are not reinterpreted in an
1055 // OpenJDK 9-specific way, eg. calls to subclasses of
1056 // java.nio.Buffer as in http://b/70862583
1057 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1058 flags.bootClasspath = append(flags.bootClasspath,
1059 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1060 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001061 if Bool(j.properties.Use_tools_jar) {
1062 flags.bootClasspath = append(flags.bootClasspath,
1063 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1064 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001065 }
1066
Colin Cross1e743852019-10-28 11:37:20 -07001067 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001068 // Manually specify build directory in case it is not under the repo root.
1069 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1070 // just adding a symlink under the root doesn't help.)
1071 patchPaths := ".:" + ctx.Config().BuildDir()
1072 classPath := flags.classpath.FormJavaClassPath("")
1073 if classPath != "" {
1074 patchPaths += ":" + classPath
1075 }
1076 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001077 }
1078
Nan Zhanged19fc32017-10-19 13:06:22 -07001079 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001080 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001081
Nan Zhanged19fc32017-10-19 13:06:22 -07001082 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001083 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001084
Colin Cross81440082018-08-15 20:21:55 -07001085 if len(javacFlags) > 0 {
1086 // optimization.
1087 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1088 flags.javacFlags = "$javacFlags"
1089 }
1090
Nan Zhanged19fc32017-10-19 13:06:22 -07001091 return flags
1092}
Colin Crossc0b06f12015-04-08 13:03:43 -07001093
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001094func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001095 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001096
1097 deps := j.collectDeps(ctx)
1098 flags := j.collectBuilderFlags(ctx, deps)
1099
Colin Cross1e743852019-10-28 11:37:20 -07001100 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001101 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1102 }
Colin Cross8a497952019-03-05 22:25:09 -08001103 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001104 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001105 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001106 }
1107
Colin Crossaf050172017-11-15 23:01:59 -08001108 srcFiles = j.genSources(ctx, srcFiles, flags)
1109
1110 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001111 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001112 if aaptSrcJar != nil {
1113 srcJars = append(srcJars, aaptSrcJar)
1114 }
Colin Crossb7a63242015-04-16 14:09:14 -07001115
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001116 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001117 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001118 }
1119
Colin Cross1ee23172017-10-18 14:44:18 -07001120 jarName := ctx.ModuleName() + ".jar"
1121
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001122 javaSrcFiles := srcFiles.FilterByExt(".java")
1123 var uniqueSrcFiles android.Paths
1124 set := make(map[string]bool)
1125 for _, v := range javaSrcFiles {
1126 if _, found := set[v.String()]; !found {
1127 set[v.String()] = true
1128 uniqueSrcFiles = append(uniqueSrcFiles, v)
1129 }
1130 }
1131
patricktu242faad2019-09-24 15:41:30 +08001132 // Collect .java files for AIDEGen
1133 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1134
Colin Cross55f63ea2018-08-27 12:37:09 -07001135 var kotlinJars android.Paths
1136
Colin Cross93e85952017-08-15 13:34:18 -07001137 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001138 // user defined kotlin flags.
1139 kotlincFlags := j.properties.Kotlincflags
1140 CheckKotlincFlags(ctx, kotlincFlags)
1141
Colin Cross93e85952017-08-15 13:34:18 -07001142 // If there are kotlin files, compile them first but pass all the kotlin and java files
1143 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1144 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001145 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001146 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001147 kotlincFlags = append(kotlincFlags, "-no-jdk")
1148 }
1149 if len(kotlincFlags) > 0 {
1150 // optimization.
1151 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1152 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001153 }
1154
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001155 var kotlinSrcFiles android.Paths
1156 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1157 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1158
patricktu242faad2019-09-24 15:41:30 +08001159 // Collect .kt files for AIDEGen
1160 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1161
Colin Crossafbb1732019-01-17 15:42:52 -08001162 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1163 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1164
1165 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1166 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1167
1168 if len(flags.processorPath) > 0 {
1169 // Use kapt for annotation processing
1170 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1171 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1172 srcJars = append(srcJars, kaptSrcJar)
1173 // Disable annotation processing in javac, it's already been handled by kapt
1174 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001175 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001176 }
Colin Cross93e85952017-08-15 13:34:18 -07001177
Colin Cross1ee23172017-10-18 14:44:18 -07001178 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001179 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001180 if ctx.Failed() {
1181 return
1182 }
1183
1184 // Make javac rule depend on the kotlinc rule
1185 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001186
Colin Cross93e85952017-08-15 13:34:18 -07001187 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001188 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001189 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001190 }
1191
Colin Cross55f63ea2018-08-27 12:37:09 -07001192 jars := append(android.Paths(nil), kotlinJars...)
1193
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001194 // Store the list of .java files that was passed to javac
1195 j.compiledJavaSrcs = uniqueSrcFiles
1196 j.compiledSrcJars = srcJars
1197
Nan Zhang61eaedb2017-11-02 13:28:15 -07001198 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001199 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001200 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1201 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001202 // Formerly, there was a check here that prevented annotation processors
1203 // from being used when sharding was enabled, as some annotation processors
1204 // do not function correctly in sharded environments. It was removed to
1205 // allow for the use of annotation processors that do function correctly
1206 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001207 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001208 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001209 if ctx.Failed() {
1210 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001211 }
1212 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001213 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001214 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001215 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001216 // If error-prone is enabled, add an additional rule to compile the java files into
1217 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001218 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001219 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1220 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001221 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001222 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001223 extraJarDeps = append(extraJarDeps, errorprone)
1224 }
1225
Nan Zhang61eaedb2017-11-02 13:28:15 -07001226 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001227 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001228 shardSize := int(*(j.properties.Javac_shard_size))
1229 var shardSrcs []android.Paths
1230 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001231 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001232 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001233 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1234 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001235 jars = append(jars, classes)
1236 }
1237 }
1238 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001239 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1240 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001241 jars = append(jars, classes)
1242 }
1243 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001244 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001245 jars = append(jars, classes)
1246 }
Colin Crossd6891432017-09-27 17:39:56 -07001247 if ctx.Failed() {
1248 return
1249 }
Colin Cross2fe66872015-03-30 17:20:39 -07001250 }
1251
Colin Cross0c4ce212019-05-03 15:28:19 -07001252 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1253
1254 var includeSrcJar android.WritablePath
1255 if Bool(j.properties.Include_srcs) {
1256 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1257 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1258 }
1259
Colin Crosscedd4762018-09-13 11:26:19 -07001260 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1261 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001262 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001263 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001264
1265 var resArgs []string
1266 var resDeps android.Paths
1267
1268 resArgs = append(resArgs, dirArgs...)
1269 resDeps = append(resDeps, dirDeps...)
1270
1271 resArgs = append(resArgs, fileArgs...)
1272 resDeps = append(resDeps, fileDeps...)
1273
Colin Cross988708c2019-05-06 14:04:11 -07001274 resArgs = append(resArgs, extraArgs...)
1275 resDeps = append(resDeps, extraDeps...)
1276
Colin Cross40a36712017-09-27 17:41:35 -07001277 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001278 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001279 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001280 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001281 if ctx.Failed() {
1282 return
1283 }
1284 }
1285
Colin Cross0c4ce212019-05-03 15:28:19 -07001286 var resourceJars android.Paths
1287 if j.resourceJar != nil {
1288 resourceJars = append(resourceJars, j.resourceJar)
1289 }
1290 if Bool(j.properties.Include_srcs) {
1291 resourceJars = append(resourceJars, includeSrcJar)
1292 }
1293 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001294
Colin Cross0c4ce212019-05-03 15:28:19 -07001295 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001296 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001297 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001298 false, nil, nil)
1299 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001300 } else if len(resourceJars) == 1 {
1301 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001302 }
1303
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001304 if len(deps.staticJars) > 0 {
1305 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001306 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001307
Colin Cross094054a2018-10-17 15:10:48 -07001308 manifest := j.overrideManifest
1309 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001310 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001311 }
Colin Cross635acc92017-09-12 22:50:46 -07001312
Colin Cross8a497952019-03-05 22:25:09 -08001313 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001314 if len(services) > 0 {
1315 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1316 var zipargs []string
1317 for _, file := range services {
1318 serviceFile := file.String()
1319 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1320 }
1321 ctx.Build(pctx, android.BuildParams{
1322 Rule: zip,
1323 Output: servicesJar,
1324 Implicits: services,
1325 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001326 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001327 },
1328 })
1329 jars = append(jars, servicesJar)
1330 }
1331
Colin Cross0a6e0072017-08-30 14:24:55 -07001332 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001333 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001334 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001335
1336 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001337 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1338 // Optimization: skip the combine step if there is nothing to do
1339 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1340 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1341 // any if len(jars) == 1.
1342 outputFile = moduleOutPath
1343 } else {
1344 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1345 ctx.Build(pctx, android.BuildParams{
1346 Rule: android.Cp,
1347 Input: jars[0],
1348 Output: combinedJar,
1349 })
1350 outputFile = combinedJar
1351 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001352 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001353 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001354 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001355 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001356 outputFile = combinedJar
1357 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001358
Colin Cross331a1212018-08-15 20:40:52 -07001359 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001360 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001361 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001362 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001363 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001364 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001365
1366 // jarjar resource jar if necessary
1367 if j.resourceJar != nil {
1368 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001369 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001370 j.resourceJar = resourceJarJarFile
1371 }
1372
Colin Cross0a6e0072017-08-30 14:24:55 -07001373 if ctx.Failed() {
1374 return
1375 }
1376 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001377
1378 // Check package restrictions if necessary.
1379 if len(j.properties.Permitted_packages) > 0 {
1380 // Check packages and copy to package-checked file.
1381 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1382 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1383 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1384
1385 if ctx.Failed() {
1386 return
1387 }
1388 }
1389
Nan Zhanged19fc32017-10-19 13:06:22 -07001390 j.implementationJarFile = outputFile
1391 if j.headerJarFile == nil {
1392 j.headerJarFile = j.implementationJarFile
1393 }
Colin Cross2fe66872015-03-30 17:20:39 -07001394
Colin Cross6510f912017-11-29 00:27:14 -08001395 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001396 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1397 j.properties.Instrument = true
1398 }
1399 }
1400
Colin Cross3144dfc2018-01-03 15:06:47 -08001401 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001402 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1403 }
1404
Colin Cross331a1212018-08-15 20:40:52 -07001405 // merge implementation jar with resources if necessary
1406 implementationAndResourcesJar := outputFile
1407 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001408 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001409 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001410 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001411 false, nil, nil)
1412 implementationAndResourcesJar = combinedJar
1413 }
1414
1415 j.implementationAndResourcesJar = implementationAndResourcesJar
1416
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001417 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001418 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001419 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001420 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001421 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001422 if ctx.Failed() {
1423 return
1424 }
Colin Cross331a1212018-08-15 20:40:52 -07001425
Jiyong Park09cb6292019-07-15 15:29:23 +09001426 // Hidden API CSV generation and dex encoding
1427 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1428 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001429
Colin Cross331a1212018-08-15 20:40:52 -07001430 // merge dex jar with resources if necessary
1431 if j.resourceJar != nil {
1432 jars := android.Paths{dexOutputFile, j.resourceJar}
1433 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1434 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1435 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001436 if j.deviceProperties.UncompressDex {
1437 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1438 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1439 dexOutputFile = combinedAlignedJar
1440 } else {
1441 dexOutputFile = combinedJar
1442 }
Colin Cross331a1212018-08-15 20:40:52 -07001443 }
1444
1445 j.dexJarFile = dexOutputFile
1446
Colin Cross8faf8fc2019-01-16 15:15:52 -08001447 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001448 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1449
1450 j.maybeStrippedDexJarFile = dexOutputFile
1451
Colin Cross3063b782018-08-15 11:19:12 -07001452 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001453
1454 if ctx.Failed() {
1455 return
1456 }
Colin Cross331a1212018-08-15 20:40:52 -07001457 } else {
1458 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001459 }
Colin Cross331a1212018-08-15 20:40:52 -07001460
Colin Crossb7a63242015-04-16 14:09:14 -07001461 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001462
1463 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1464 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001465}
1466
Colin Cross3b706fd2019-09-05 16:44:18 -07001467func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1468 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1469
1470 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1471 if idx >= 0 {
1472 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1473 jarName += strconv.Itoa(idx)
1474 }
1475
1476 classes := android.PathForModuleOut(ctx, "javac", jarName)
1477 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1478
1479 if ctx.Config().EmitXrefRules() {
1480 extractionFile := android.PathForModuleOut(ctx, kzipName)
1481 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1482 j.kytheFiles = append(j.kytheFiles, extractionFile)
1483 }
1484
1485 return classes
1486}
1487
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001488// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1489// since some of these flags may be used internally.
1490func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1491 for _, flag := range flags {
1492 flag = strings.TrimSpace(flag)
1493
1494 if !strings.HasPrefix(flag, "-") {
1495 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1496 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1497 ctx.PropertyErrorf("kotlincflags",
1498 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1499 } else if inList(flag, config.KotlincIllegalFlags) {
1500 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1501 } else if flag == "-include-runtime" {
1502 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1503 } else {
1504 args := strings.Split(flag, " ")
1505 if args[0] == "-kotlin-home" {
1506 ctx.PropertyErrorf("kotlincflags",
1507 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1508 }
1509 }
1510 }
1511}
1512
Colin Cross8eadbf02017-10-24 17:46:00 -07001513func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001514 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001515
1516 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001517 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001518 // Compile java sources into turbine.jar.
1519 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1520 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1521 if ctx.Failed() {
1522 return nil
1523 }
1524 jars = append(jars, turbineJar)
1525 }
1526
Colin Cross55f63ea2018-08-27 12:37:09 -07001527 jars = append(jars, extraJars...)
1528
Nan Zhanged19fc32017-10-19 13:06:22 -07001529 // Combine any static header libraries into classes-header.jar. If there is only
1530 // one input jar this step will be skipped.
1531 var headerJar android.Path
1532 jars = append(jars, deps.staticHeaderJars...)
1533
Colin Cross5c6ecc12017-10-23 18:12:27 -07001534 // we cannot skip the combine step for now if there is only one jar
1535 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1536 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001537 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001538 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001539 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001540
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001541 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001542 // Transform classes.jar into classes-jarjar.jar
1543 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001544 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001545 headerJar = jarjarFile
1546 if ctx.Failed() {
1547 return nil
1548 }
1549 }
1550
1551 return headerJar
1552}
1553
Colin Crosscb933592017-11-22 13:49:43 -08001554func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001555 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001556
Colin Cross7a3139e2017-12-19 13:57:50 -08001557 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001558
Colin Cross84c38822018-01-03 15:59:46 -08001559 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001560 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1561
1562 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1563
1564 j.jacocoReportClassesFile = jacocoReportClassesFile
1565
1566 return instrumentedJar
1567}
1568
albaltai36ff7dc2018-12-25 14:35:23 +08001569var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001570
Nan Zhanged19fc32017-10-19 13:06:22 -07001571func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001572 if j.headerJarFile == nil {
1573 return nil
1574 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001575 return android.Paths{j.headerJarFile}
1576}
1577
1578func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001579 if j.implementationJarFile == nil {
1580 return nil
1581 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001582 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001583}
1584
Colin Crossf24a22a2019-01-31 14:12:44 -08001585func (j *Module) DexJar() android.Path {
1586 return j.dexJarFile
1587}
1588
Colin Cross331a1212018-08-15 20:40:52 -07001589func (j *Module) ResourceJars() android.Paths {
1590 if j.resourceJar == nil {
1591 return nil
1592 }
1593 return android.Paths{j.resourceJar}
1594}
1595
1596func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001597 if j.implementationAndResourcesJar == nil {
1598 return nil
1599 }
Colin Cross331a1212018-08-15 20:40:52 -07001600 return android.Paths{j.implementationAndResourcesJar}
1601}
1602
Colin Cross46c9b8b2017-06-22 16:51:17 -07001603func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001604 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001605 return j.exportAidlIncludeDirs
1606}
1607
Jiyong Park1be96912018-05-28 18:02:19 +09001608func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001609 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001610 return j.exportedSdkLibs
1611}
1612
Artur Satayev9cf46692019-11-26 18:08:34 +00001613func (j *Module) ExportedPlugins() (android.Paths, []string) {
1614 return j.exportedPluginJars, j.exportedPluginClasses
1615}
1616
Colin Cross0c4ce212019-05-03 15:28:19 -07001617func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1618 return j.srcJarArgs, j.srcJarDeps
1619}
1620
Colin Cross46c9b8b2017-06-22 16:51:17 -07001621var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001622
Colin Cross46c9b8b2017-06-22 16:51:17 -07001623func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001624 return j.logtagsSrcs
1625}
1626
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001627// Collect information for opening IDE project files in java/jdeps.go.
1628func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1629 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1630 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001631 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001632 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001633 if j.expandJarjarRules != nil {
1634 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001635 }
1636}
1637
1638func (j *Module) CompilerDeps() []string {
1639 jdeps := []string{}
1640 jdeps = append(jdeps, j.properties.Libs...)
1641 jdeps = append(jdeps, j.properties.Static_libs...)
1642 return jdeps
1643}
1644
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001645func (j *Module) hasCode(ctx android.ModuleContext) bool {
1646 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1647 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1648}
1649
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001650func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1651 depTag := ctx.OtherModuleDependencyTag(dep)
1652 // dependencies other than the static linkage are all considered crossing APEX boundary
1653 return depTag == staticLibTag
1654}
1655
Jiyong Park0b238752019-10-29 11:23:10 +09001656func (j *Module) Stem() string {
1657 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1658}
1659
Colin Cross2fe66872015-03-30 17:20:39 -07001660//
1661// Java libraries (.jar file)
1662//
1663
Colin Crossf506d872017-07-19 15:53:04 -07001664type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001665 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001666
1667 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001668}
1669
Colin Cross42be7612019-02-21 18:12:14 -08001670func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001671 // Store uncompressed (and do not strip) dex files from boot class path jars.
1672 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1673 return true
1674 }
1675
1676 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001677 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001678 return true
1679 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001680 if ctx.Config().UncompressPrivAppDex() &&
1681 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1682 return true
1683 }
1684
Colin Cross2fc72f62018-12-21 12:59:54 -08001685 return false
1686}
1687
Colin Crossf506d872017-07-19 15:53:04 -07001688func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001689 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001690 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001691 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001692 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001693 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001694 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001695 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001696
Jiyong Park7f7766d2019-07-25 22:02:35 +09001697 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1698 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001699 var extraInstallDeps android.Paths
1700 if j.InstallMixin != nil {
1701 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1702 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001703 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001704 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001705 }
Colin Crossb7a63242015-04-16 14:09:14 -07001706}
1707
Colin Crossf506d872017-07-19 15:53:04 -07001708func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001709 j.deps(ctx)
1710}
1711
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001712const (
Paul Duffina0dbf432019-12-05 11:25:53 +00001713 aidlIncludeDir = "aidl"
1714 javaDir = "java"
1715 jarFileSuffix = ".jar"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001716)
1717
Paul Duffina0dbf432019-12-05 11:25:53 +00001718// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
1719func (j *Library) sdkSnapshotFilePathForJar() string {
1720 return filepath.Join(javaDir, j.Name()+jarFileSuffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001721}
1722
Paul Duffin13879572019-11-28 14:31:38 +00001723type librarySdkMemberType struct {
1724}
1725
1726func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1727 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1728}
1729
1730func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1731 _, ok := module.(*Library)
1732 return ok
1733}
1734
Paul Duffina0dbf432019-12-05 11:25:53 +00001735func (mt *librarySdkMemberType) buildSnapshot(
1736 sdkModuleContext android.ModuleContext,
1737 builder android.SnapshotBuilder,
1738 member android.SdkMember,
1739 jarToExportGetter func(j *Library) android.Path) {
1740
Paul Duffin13879572019-11-28 14:31:38 +00001741 variants := member.Variants()
1742 if len(variants) != 1 {
1743 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1744 for _, variant := range variants {
1745 sdkModuleContext.ModuleErrorf(" %q", variant)
1746 }
1747 }
1748 variant := variants[0]
1749 j := variant.(*Library)
1750
Paul Duffina0dbf432019-12-05 11:25:53 +00001751 exportedJar := jarToExportGetter(j)
1752 snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
1753 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001754
1755 for _, dir := range j.AidlIncludeDirs() {
1756 // TODO(jiyong): copy parcelable declarations only
1757 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1758 for _, file := range aidlFiles {
1759 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1760 }
1761 }
1762
Paul Duffin9d8d6092019-12-05 18:19:29 +00001763 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001764 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001765}
1766
Paul Duffina0dbf432019-12-05 11:25:53 +00001767var HeaderLibrarySdkMemberType = &headerLibrarySdkMemberType{}
1768
1769type headerLibrarySdkMemberType struct {
1770 librarySdkMemberType
1771}
1772
1773func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1774 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1775 headerJars := j.HeaderJars()
1776 if len(headerJars) != 1 {
1777 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1778 }
1779
1780 return headerJars[0]
1781 })
1782}
1783
1784var ImplLibrarySdkMemberType = &implLibrarySdkMemberType{}
1785
1786type implLibrarySdkMemberType struct {
1787 librarySdkMemberType
1788}
1789
1790func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1791 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1792 implementationJars := j.ImplementationJars()
1793 if len(implementationJars) != 1 {
1794 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1795 }
1796
1797 return implementationJars[0]
1798 })
1799}
1800
Colin Cross1b16b0e2019-02-12 14:41:32 -08001801// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1802//
1803// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1804// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1805// as a `static_libs` dependency of another module.
1806//
1807// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1808// a device.
1809//
1810// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1811// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001812func LibraryFactory() android.Module {
1813 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001814
Colin Cross9ae1b922018-06-26 17:59:05 -07001815 module.AddProperties(
1816 &module.Module.properties,
1817 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001818 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001819 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001820
Jiyong Park7f7766d2019-07-25 22:02:35 +09001821 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001822 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001823 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001824 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001825}
1826
Colin Cross1b16b0e2019-02-12 14:41:32 -08001827// java_library_static is an obsolete alias for java_library.
1828func LibraryStaticFactory() android.Module {
1829 return LibraryFactory()
1830}
1831
1832// java_library_host builds and links sources into a `.jar` file for the host.
1833//
1834// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1835// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001836func LibraryHostFactory() android.Module {
1837 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001838
Colin Cross6af17aa2017-09-20 12:59:05 -07001839 module.AddProperties(
1840 &module.Module.properties,
1841 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001842
Colin Cross9ae1b922018-06-26 17:59:05 -07001843 module.Module.properties.Installable = proptools.BoolPtr(true)
1844
Jiyong Park7f7766d2019-07-25 22:02:35 +09001845 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001846 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001847 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001848}
1849
1850//
Colin Crossb628ea52018-08-14 16:42:33 -07001851// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001852//
1853
1854type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001855 // list of compatibility suites (for example "cts", "vts") that the module should be
1856 // installed into.
1857 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001858
1859 // the name of the test configuration (for example "AndroidTest.xml") that should be
1860 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001861 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001862
Jack He33338892018-09-19 02:21:28 -07001863 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1864 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001865 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001866
Colin Crossd96ca352018-08-10 16:06:24 -07001867 // list of files or filegroup modules that provide data that should be installed alongside
1868 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001869 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001870
1871 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1872 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1873 // explicitly.
1874 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001875}
1876
Paul Duffin42df1442019-03-20 12:45:53 +00001877type testHelperLibraryProperties struct {
1878 // list of compatibility suites (for example "cts", "vts") that the module should be
1879 // installed into.
1880 Test_suites []string `android:"arch_variant"`
1881}
1882
Colin Cross05638fc2018-04-09 18:40:24 -07001883type Test struct {
1884 Library
1885
1886 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001887
1888 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001889 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001890}
1891
Paul Duffin42df1442019-03-20 12:45:53 +00001892type TestHelperLibrary struct {
1893 Library
1894
1895 testHelperLibraryProperties testHelperLibraryProperties
1896}
1897
Colin Cross303e21f2018-08-07 16:49:25 -07001898func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001899 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1900 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001901 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001902
1903 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001904}
1905
Paul Duffin42df1442019-03-20 12:45:53 +00001906func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1907 j.Library.GenerateAndroidBuildActions(ctx)
1908}
1909
Colin Cross1b16b0e2019-02-12 14:41:32 -08001910// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1911// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1912//
1913// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1914// compiled against the device bootclasspath.
1915//
1916// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1917// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001918func TestFactory() android.Module {
1919 module := &Test{}
1920
1921 module.AddProperties(
1922 &module.Module.properties,
1923 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001924 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001925 &module.Module.protoProperties,
1926 &module.testProperties)
1927
Colin Cross9ae1b922018-06-26 17:59:05 -07001928 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001929 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001930
Colin Cross05638fc2018-04-09 18:40:24 -07001931 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001932 return module
1933}
1934
Paul Duffin42df1442019-03-20 12:45:53 +00001935// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1936func TestHelperLibraryFactory() android.Module {
1937 module := &TestHelperLibrary{}
1938
1939 module.AddProperties(
1940 &module.Module.properties,
1941 &module.Module.deviceProperties,
1942 &module.Module.dexpreoptProperties,
1943 &module.Module.protoProperties,
1944 &module.testHelperLibraryProperties)
1945
Colin Cross9a4abed2019-04-24 13:19:28 -07001946 module.Module.properties.Installable = proptools.BoolPtr(true)
1947 module.Module.dexpreopter.isTest = true
1948
Paul Duffin42df1442019-03-20 12:45:53 +00001949 InitJavaModule(module, android.HostAndDeviceSupported)
1950 return module
1951}
1952
Colin Cross1b16b0e2019-02-12 14:41:32 -08001953// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1954// allow running the test with `atest` or a `TEST_MAPPING` file.
1955//
1956// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1957// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001958func TestHostFactory() android.Module {
1959 module := &Test{}
1960
1961 module.AddProperties(
1962 &module.Module.properties,
1963 &module.Module.protoProperties,
1964 &module.testProperties)
1965
Colin Cross9ae1b922018-06-26 17:59:05 -07001966 module.Module.properties.Installable = proptools.BoolPtr(true)
1967
Colin Cross05638fc2018-04-09 18:40:24 -07001968 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001969 return module
1970}
1971
1972//
Colin Cross2fe66872015-03-30 17:20:39 -07001973// Java Binaries (.jar file plus wrapper script)
1974//
1975
Colin Crossf506d872017-07-19 15:53:04 -07001976type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001977 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001978 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001979
1980 // Name of the class containing main to be inserted into the manifest as Main-Class.
1981 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001982}
1983
Colin Crossf506d872017-07-19 15:53:04 -07001984type Binary struct {
1985 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001986
Colin Crossf506d872017-07-19 15:53:04 -07001987 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001988
Colin Cross6b4a32d2017-12-05 13:42:45 -08001989 isWrapperVariant bool
1990
Colin Crossc3315992017-12-08 19:12:36 -08001991 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07001992 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07001993}
1994
Alex Light24237172017-10-26 09:46:21 -07001995func (j *Binary) HostToolPath() android.OptionalPath {
1996 return android.OptionalPathForPath(j.binaryFile)
1997}
1998
Colin Crossf506d872017-07-19 15:53:04 -07001999func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002000 if ctx.Arch().ArchType == android.Common {
2001 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002002 if j.binaryProperties.Main_class != nil {
2003 if j.properties.Manifest != nil {
2004 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2005 }
2006 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2007 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2008 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2009 }
2010
Colin Cross6b4a32d2017-12-05 13:42:45 -08002011 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002012 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002013 // Handle the binary wrapper
2014 j.isWrapperVariant = true
2015
Colin Cross366938f2017-12-11 16:29:02 -08002016 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002017 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002018 } else {
2019 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2020 }
2021
2022 // Depend on the installed jar so that the wrapper doesn't get executed by
2023 // another build rule before the jar has been installed.
2024 jarFile := ctx.PrimaryModule().(*Binary).installFile
2025
2026 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2027 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002028 }
Colin Cross2fe66872015-03-30 17:20:39 -07002029}
2030
Colin Crossf506d872017-07-19 15:53:04 -07002031func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002032 if ctx.Arch().ArchType == android.Common {
2033 j.deps(ctx)
2034 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002035}
2036
Colin Cross1b16b0e2019-02-12 14:41:32 -08002037// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2038// as well.
2039//
2040// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2041// compiled against the device bootclasspath.
2042//
2043// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2044// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002045func BinaryFactory() android.Module {
2046 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002047
Colin Cross36242852017-06-23 15:06:31 -07002048 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002049 &module.Module.properties,
2050 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002051 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002052 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002053 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002054
Colin Cross9ae1b922018-06-26 17:59:05 -07002055 module.Module.properties.Installable = proptools.BoolPtr(true)
2056
Colin Cross6b4a32d2017-12-05 13:42:45 -08002057 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2058 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002059 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002060}
2061
Colin Cross1b16b0e2019-02-12 14:41:32 -08002062// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2063//
2064// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2065// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002066func BinaryHostFactory() android.Module {
2067 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002068
Colin Cross36242852017-06-23 15:06:31 -07002069 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002070 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002071 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002072 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002073
Colin Cross9ae1b922018-06-26 17:59:05 -07002074 module.Module.properties.Installable = proptools.BoolPtr(true)
2075
Colin Cross6b4a32d2017-12-05 13:42:45 -08002076 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2077 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002078 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002079}
2080
2081//
2082// Java prebuilts
2083//
2084
Colin Cross74d73e22017-08-02 11:05:49 -07002085type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002086 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002087
Nan Zhangea568a42017-11-08 21:20:04 -08002088 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002089
2090 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002091
2092 // List of shared java libs that this module has dependencies to
2093 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002094
2095 // List of files to remove from the jar file(s)
2096 Exclude_files []string
2097
2098 // List of directories to remove from the jar file(s)
2099 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002100
2101 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002102 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002103
2104 // set the name of the output
2105 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002106}
2107
2108type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002109 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002110 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002111 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002112 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002113 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002114
Colin Cross74d73e22017-08-02 11:05:49 -07002115 properties ImportProperties
2116
Colin Cross0a6e0072017-08-30 14:24:55 -07002117 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002118 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002119}
2120
Colin Cross83bb3162018-06-25 15:48:06 -07002121func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002122 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002123}
2124
2125func (j *Import) minSdkVersion() string {
2126 return j.sdkVersion()
2127}
2128
Colin Cross74d73e22017-08-02 11:05:49 -07002129func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002130 return &j.prebuilt
2131}
2132
Colin Cross74d73e22017-08-02 11:05:49 -07002133func (j *Import) PrebuiltSrcs() []string {
2134 return j.properties.Jars
2135}
2136
2137func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002138 return j.prebuilt.Name(j.ModuleBase.Name())
2139}
2140
Jiyong Park0b238752019-10-29 11:23:10 +09002141func (j *Import) Stem() string {
2142 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2143}
2144
Colin Cross74d73e22017-08-02 11:05:49 -07002145func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002146 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002147}
2148
Colin Cross74d73e22017-08-02 11:05:49 -07002149func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002150 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002151
Jiyong Park0b238752019-10-29 11:23:10 +09002152 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002153 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002154 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2155 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002156 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002157 inputFile := outputFile
2158 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2159 TransformJetifier(ctx, outputFile, inputFile)
2160 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002161 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002162
2163 ctx.VisitDirectDeps(func(module android.Module) {
2164 otherName := ctx.OtherModuleName(module)
2165 tag := ctx.OtherModuleDependencyTag(module)
2166
2167 switch dep := module.(type) {
2168 case Dependency:
2169 switch tag {
2170 case libTag, staticLibTag:
2171 // sdk lib names from dependencies are re-exported
2172 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2173 }
2174 case SdkLibraryDependency:
2175 switch tag {
2176 case libTag:
2177 // names of sdk libs that are directly depended are exported
2178 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2179 }
2180 }
2181 })
2182
2183 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002184 if Bool(j.properties.Installable) {
2185 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002186 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002187 }
Colin Cross2fe66872015-03-30 17:20:39 -07002188}
2189
Colin Cross74d73e22017-08-02 11:05:49 -07002190var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002191
Nan Zhanged19fc32017-10-19 13:06:22 -07002192func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002193 if j.combinedClasspathFile == nil {
2194 return nil
2195 }
Colin Cross37f6d792018-07-12 12:28:41 -07002196 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002197}
2198
2199func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002200 if j.combinedClasspathFile == nil {
2201 return nil
2202 }
Colin Cross37f6d792018-07-12 12:28:41 -07002203 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002204}
2205
Colin Cross331a1212018-08-15 20:40:52 -07002206func (j *Import) ResourceJars() android.Paths {
2207 return nil
2208}
2209
2210func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002211 if j.combinedClasspathFile == nil {
2212 return nil
2213 }
Colin Cross331a1212018-08-15 20:40:52 -07002214 return android.Paths{j.combinedClasspathFile}
2215}
2216
Colin Crossf24a22a2019-01-31 14:12:44 -08002217func (j *Import) DexJar() android.Path {
2218 return nil
2219}
2220
Colin Cross74d73e22017-08-02 11:05:49 -07002221func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002222 return nil
2223}
2224
Jiyong Park1be96912018-05-28 18:02:19 +09002225func (j *Import) ExportedSdkLibs() []string {
2226 return j.exportedSdkLibs
2227}
2228
Artur Satayev9cf46692019-11-26 18:08:34 +00002229func (j *Import) ExportedPlugins() (android.Paths, []string) {
2230 return nil, nil
2231}
2232
Colin Cross0c4ce212019-05-03 15:28:19 -07002233func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2234 return nil, nil
2235}
2236
albaltai36ff7dc2018-12-25 14:35:23 +08002237// Add compile time check for interface implementation
2238var _ android.IDEInfo = (*Import)(nil)
2239var _ android.IDECustomizedModuleName = (*Import)(nil)
2240
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002241// Collect information for opening IDE project files in java/jdeps.go.
2242const (
2243 removedPrefix = "prebuilt_"
2244)
2245
2246func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2247 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2248}
2249
2250func (j *Import) IDECustomizedModuleName() string {
2251 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2252 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2253 // solution to get the Import name.
2254 name := j.Name()
2255 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002256 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002257 }
2258 return name
2259}
2260
Colin Cross74d73e22017-08-02 11:05:49 -07002261var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002262
Colin Cross1b16b0e2019-02-12 14:41:32 -08002263// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2264//
2265// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2266// compiled against an Android classpath.
2267//
2268// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2269// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002270func ImportFactory() android.Module {
2271 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002272
Colin Cross74d73e22017-08-02 11:05:49 -07002273 module.AddProperties(&module.properties)
2274
2275 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002276 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002277 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002278 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002279 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002280}
2281
Colin Cross1b16b0e2019-02-12 14:41:32 -08002282// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2283// module.
2284//
2285// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2286// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002287func ImportFactoryHost() android.Module {
2288 module := &Import{}
2289
2290 module.AddProperties(&module.properties)
2291
2292 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002293 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002294 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002295 return module
2296}
2297
Colin Cross42be7612019-02-21 18:12:14 -08002298// dex_import module
2299
2300type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002301 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002302
2303 // set the name of the output
2304 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002305}
2306
2307type DexImport struct {
2308 android.ModuleBase
2309 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002310 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002311 prebuilt android.Prebuilt
2312
2313 properties DexImportProperties
2314
2315 dexJarFile android.Path
2316 maybeStrippedDexJarFile android.Path
2317
2318 dexpreopter
2319}
2320
2321func (j *DexImport) Prebuilt() *android.Prebuilt {
2322 return &j.prebuilt
2323}
2324
2325func (j *DexImport) PrebuiltSrcs() []string {
2326 return j.properties.Jars
2327}
2328
2329func (j *DexImport) Name() string {
2330 return j.prebuilt.Name(j.ModuleBase.Name())
2331}
2332
Jiyong Park0b238752019-10-29 11:23:10 +09002333func (j *DexImport) Stem() string {
2334 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2335}
2336
Colin Cross42be7612019-02-21 18:12:14 -08002337func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2338 if len(j.properties.Jars) != 1 {
2339 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2340 }
2341
Jiyong Park0b238752019-10-29 11:23:10 +09002342 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002343 j.dexpreopter.isInstallable = true
2344 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2345
2346 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2347 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2348
2349 if j.dexpreopter.uncompressedDex {
2350 rule := android.NewRuleBuilder()
2351
2352 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2353 rule.Temporary(temporary)
2354
2355 // use zip2zip to uncompress classes*.dex files
2356 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002357 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002358 FlagWithInput("-i ", inputJar).
2359 FlagWithOutput("-o ", temporary).
2360 FlagWithArg("-0 ", "'classes*.dex'")
2361
2362 // use zipalign to align uncompressed classes*.dex files
2363 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002364 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002365 Flag("-f").
2366 Text("4").
2367 Input(temporary).
2368 Output(dexOutputFile)
2369
2370 rule.DeleteTemporaryFiles()
2371
2372 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2373 } else {
2374 ctx.Build(pctx, android.BuildParams{
2375 Rule: android.Cp,
2376 Input: inputJar,
2377 Output: dexOutputFile,
2378 })
2379 }
2380
2381 j.dexJarFile = dexOutputFile
2382
2383 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2384
2385 j.maybeStrippedDexJarFile = dexOutputFile
2386
2387 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2388 ctx.ModuleName()+".jar", dexOutputFile)
2389}
2390
2391func (j *DexImport) DexJar() android.Path {
2392 return j.dexJarFile
2393}
2394
2395// dex_import imports a `.jar` file containing classes.dex files.
2396//
2397// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2398// to the device.
2399func DexImportFactory() android.Module {
2400 module := &DexImport{}
2401
2402 module.AddProperties(&module.properties)
2403
2404 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002405 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002406 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002407 return module
2408}
2409
Colin Cross89536d42017-07-07 14:35:50 -07002410//
2411// Defaults
2412//
2413type Defaults struct {
2414 android.ModuleBase
2415 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002416 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002417}
2418
Colin Cross1b16b0e2019-02-12 14:41:32 -08002419// java_defaults provides a set of properties that can be inherited by other java or android modules.
2420//
2421// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2422// property in the defaults module that exists in the depending module will be prepended to the depending module's
2423// value for that property.
2424//
2425// Example:
2426//
2427// java_defaults {
2428// name: "example_defaults",
2429// srcs: ["common/**/*.java"],
2430// javacflags: ["-Xlint:all"],
2431// aaptflags: ["--auto-add-overlay"],
2432// }
2433//
2434// java_library {
2435// name: "example",
2436// defaults: ["example_defaults"],
2437// srcs: ["example/**/*.java"],
2438// }
2439//
2440// is functionally identical to:
2441//
2442// java_library {
2443// name: "example",
2444// srcs: [
2445// "common/**/*.java",
2446// "example/**/*.java",
2447// ],
2448// javacflags: ["-Xlint:all"],
2449// }
Colin Cross89536d42017-07-07 14:35:50 -07002450func defaultsFactory() android.Module {
2451 return DefaultsFactory()
2452}
2453
Paul Duffin47357662019-12-05 14:07:14 +00002454func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002455 module := &Defaults{}
2456
Colin Cross89536d42017-07-07 14:35:50 -07002457 module.AddProperties(
2458 &CompilerProperties{},
2459 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002460 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002461 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002462 &aaptProperties{},
2463 &androidLibraryProperties{},
2464 &appProperties{},
2465 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002466 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002467 &ImportProperties{},
2468 &AARImportProperties{},
2469 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002470 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002471 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002472 )
2473
2474 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002475 return module
2476}
Nan Zhangea568a42017-11-08 21:20:04 -08002477
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002478func kytheExtractJavaFactory() android.Singleton {
2479 return &kytheExtractJavaSingleton{}
2480}
2481
2482type kytheExtractJavaSingleton struct {
2483}
2484
2485func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2486 var xrefTargets android.Paths
2487 ctx.VisitAllModules(func(module android.Module) {
2488 if javaModule, ok := module.(xref); ok {
2489 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2490 }
2491 })
2492 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2493 if len(xrefTargets) > 0 {
2494 ctx.Build(pctx, android.BuildParams{
2495 Rule: blueprint.Phony,
2496 Output: android.PathForPhony(ctx, "xref_java"),
2497 Inputs: xrefTargets,
2498 })
2499 }
2500}
2501
Nan Zhangea568a42017-11-08 21:20:04 -08002502var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002503var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002504var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002505var inList = android.InList