blob: 0188cf1a26c00ded8285b0c04d2e567c01358f2c [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
39 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000040 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000041
Paul Duffinf5c0a9c2020-02-28 14:39:53 +000042 android.RegisterSdkMemberType(&librarySdkMemberType{
43 android.SdkMemberTypeBase{
44 PropertyName: "java_libs",
45 },
46 func(j *Library) android.Path {
47 implementationJars := j.ImplementationJars()
48 if len(implementationJars) != 1 {
49 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
50 }
51
52 return implementationJars[0]
Paul Duffin255f18e2019-12-13 11:22:16 +000053 },
54 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000055
56 android.RegisterSdkMemberType(&testSdkMemberType{
57 SdkMemberTypeBase: android.SdkMemberTypeBase{
58 PropertyName: "java_tests",
59 },
60 })
Colin Cross463a90e2015-06-17 14:20:06 -070061}
62
Paul Duffinf9b1da02019-12-18 19:51:55 +000063func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
64 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
65
66 ctx.RegisterModuleType("java_library", LibraryFactory)
67 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
68 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
69 ctx.RegisterModuleType("java_binary", BinaryFactory)
70 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
71 ctx.RegisterModuleType("java_test", TestFactory)
72 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
73 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000074 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000075 ctx.RegisterModuleType("java_import", ImportFactory)
76 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
77 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
78 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
79 ctx.RegisterModuleType("dex_import", DexImportFactory)
80
Martin Stjernholm6d415272020-01-31 17:10:36 +000081 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
82 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
83 })
Martin Stjernholmd90676f2020-01-11 00:37:30 +000084
Paul Duffinf9b1da02019-12-18 19:51:55 +000085 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
86 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
87}
88
Jeongik Cha2cc570d2019-10-29 15:44:45 +090089func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
90 if j.SocSpecific() || j.DeviceSpecific() ||
91 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
92 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +090093 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090094 ctx.PropertyErrorf("sdk_version",
95 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
96 }
97 }
98 }
99}
100
Jeongik Cha538c0d02019-07-11 15:54:27 +0900101func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
102 if sc, ok := ctx.Module().(sdkContext); ok {
103 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +0900104 sdkVersionSpecified := sc.sdkVersion().specified()
105 if usePlatformAPI && sdkVersionSpecified {
106 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
107 } else if !usePlatformAPI && !sdkVersionSpecified {
108 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900109 }
110
111 }
112}
113
Colin Cross2fe66872015-03-30 17:20:39 -0700114// TODO:
115// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700116// Renderscript
117// Post-jar passes:
118// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700119// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700120// DroidDoc
121// Findbugs
122
Colin Cross89536d42017-07-07 14:35:50 -0700123type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700124 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
125 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800126 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700127
128 // list of source files that should not be used to build the Java module.
129 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800130 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700131
132 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700133 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700134
Colin Cross86a63ff2017-09-27 17:33:10 -0700135 // list of directories that should be excluded from java_resource_dirs
136 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700137
Colin Cross0f37af02017-09-27 17:42:05 -0700138 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800139 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700140
Colin Crosscedd4762018-09-13 11:26:19 -0700141 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800142 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700143
Colin Cross7d5136f2015-05-11 13:39:40 -0700144 // list of module-specific flags that will be used for javac compiles
145 Javacflags []string `android:"arch_variant"`
146
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200147 // list of module-specific flags that will be used for kotlinc compiles
148 Kotlincflags []string `android:"arch_variant"`
149
Colin Cross7d5136f2015-05-11 13:39:40 -0700150 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700151 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700152
153 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700154 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700155
156 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800157 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700158
Colin Cross540eff82017-06-22 17:01:52 -0700159 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800160 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700161
162 // If not blank, set the java version passed to javac as -source and -target
163 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700164
Colin Cross9ae1b922018-06-26 17:59:05 -0700165 // If set to true, allow this module to be dexed and installed on devices. Has no
166 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700167 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700168
Colin Cross0f37af02017-09-27 17:42:05 -0700169 // If set to true, include sources used to compile the module in to the final jar
170 Include_srcs *bool
171
Vladimir Marko0975ee02019-04-02 10:29:55 +0100172 // If not empty, classes are restricted to the specified packages and their sub-packages.
173 // This restriction is checked after applying jarjar rules and including static libs.
174 Permitted_packages []string
175
Colin Crossbe9cdb82019-01-21 21:37:16 -0800176 // List of modules to use as annotation processors
177 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700178
Artur Satayev9cf46692019-11-26 18:08:34 +0000179 // List of modules to export to libraries that directly depend on this library as annotation processors
180 Exported_plugins []string
181
Nan Zhang61eaedb2017-11-02 13:28:15 -0700182 // The number of Java source entries each Javac instance can process
183 Javac_shard_size *int64
184
Nan Zhang5f8cb422018-02-06 10:34:32 -0800185 // Add host jdk tools.jar to bootclasspath
186 Use_tools_jar *bool
187
Colin Cross1369cdb2017-09-29 17:58:17 -0700188 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700189 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800190 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700191
Colin Cross6cef4812019-10-17 14:23:50 -0700192 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700193 Javacflags []string
194 }
Colin Crosscb933592017-11-22 13:49:43 -0800195
Colin Cross81440082018-08-15 20:21:55 -0700196 // When compiling language level 9+ .java code in packages that are part of
197 // a system module, patch_module names the module that your sources and
198 // dependencies should be patched into. The Android runtime currently
199 // doesn't implement the JEP 261 module system so this option is only
200 // supported at compile time. It should only be needed to compile tests in
201 // packages that exist in libcore and which are inconvenient to move
202 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100203 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700204
Colin Crosscb933592017-11-22 13:49:43 -0800205 Jacoco struct {
206 // List of classes to include for instrumentation with jacoco to collect coverage
207 // information at runtime when building with coverage enabled. If unset defaults to all
208 // classes.
209 // Supports '*' as the last character of an entry in the list as a wildcard match.
210 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
211 // it matches classes in the package that have the class name as a prefix.
212 Include_filter []string
213
214 // List of classes to exclude from instrumentation with jacoco to collect coverage
215 // information at runtime when building with coverage enabled. Overrides classes selected
216 // by the include_filter property.
217 // Supports '*' as the last character of an entry in the list as a wildcard match.
218 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
219 // it matches classes in the package that have the class name as a prefix.
220 Exclude_filter []string
221 }
222
Andreas Gampef3e5b552018-01-22 21:27:21 -0800223 Errorprone struct {
224 // List of javac flags that should only be used when running errorprone.
225 Javacflags []string
226 }
227
Colin Cross0f2ee152017-12-14 15:22:43 -0800228 Proto struct {
229 // List of extra options that will be passed to the proto generator.
230 Output_params []string
231 }
232
Colin Crosscb933592017-11-22 13:49:43 -0800233 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800234
235 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800236 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700237}
238
Colin Cross89536d42017-07-07 14:35:50 -0700239type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700240 // list of module-specific flags that will be used for dex compiles
241 Dxflags []string `android:"arch_variant"`
242
Jeongik Cha538c0d02019-07-11 15:54:27 +0900243 // if not blank, set to the version of the sdk to compile against.
244 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800245 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700246
Colin Cross83bb3162018-06-25 15:48:06 -0700247 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
248 // Defaults to sdk_version if not set.
249 Min_sdk_version *string
250
Dan Willemsen419290a2018-10-31 15:28:47 -0700251 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
252 // Defaults to sdk_version if not set.
253 Target_sdk_version *string
254
Jeongik Cha356dac42019-08-19 14:09:52 +0900255 // Whether to compile against the platform APIs instead of an SDK.
256 // If true, then sdk_version must be empty. The value of this field
257 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700258 Platform_apis *bool
259
Colin Crossebe1a512017-11-14 13:12:14 -0800260 Aidl struct {
261 // Top level directories to pass to aidl tool
262 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700263
Colin Crossebe1a512017-11-14 13:12:14 -0800264 // Directories rooted at the Android.bp file to pass to aidl tool
265 Local_include_dirs []string
266
267 // directories that should be added as include directories for any aidl sources of modules
268 // that depend on this module, as well as to aidl for this module.
269 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100270
271 // whether to generate traces (for systrace) for this interface
272 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100273
274 // whether to generate Binder#GetTransaction name method.
275 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800276 }
Colin Cross92430102017-10-09 14:59:32 -0700277
278 // If true, export a copy of the module as a -hostdex module for host testing.
279 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700280
Colin Cross7f87f4f2019-04-24 13:41:45 -0700281 Target struct {
282 Hostdex struct {
283 // Additional required dependencies to add to -hostdex modules.
284 Required []string
285 }
286 }
287
David Brazdil17ef5632018-06-27 10:27:45 +0100288 // If set to true, compile dex regardless of installable. Defaults to false.
289 Compile_dex *bool
290
Colin Cross66dbc0b2017-12-28 12:23:20 -0800291 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700292 // If false, disable all optimization. Defaults to true for android_app and android_test
293 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800294 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700295 // True if the module containing this has it set by default.
296 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800297
298 // If true, optimize for size by removing unused code. Defaults to true for apps,
299 // false for libraries and tests.
300 Shrink *bool
301
302 // If true, optimize bytecode. Defaults to false.
303 Optimize *bool
304
305 // If true, obfuscate bytecode. Defaults to false.
306 Obfuscate *bool
307
308 // If true, do not use the flag files generated by aapt that automatically keep
309 // classes referenced by the app manifest. Defaults to false.
310 No_aapt_flags *bool
311
312 // Flags to pass to proguard.
313 Proguard_flags []string
314
315 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800316 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800317 }
318
Paul Duffine25c6442019-10-11 13:50:28 +0100319 // When targeting 1.9 and above, override the modules to use with --system,
320 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700321 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700322
Jiyong Park4c4c0242019-10-21 14:53:15 +0900323 // set the name of the output
324 Stem *string
325
Colin Cross5a0dcd52018-10-05 14:20:06 -0700326 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800327 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700328}
329
Sasha Smundak2057f822019-04-16 17:16:58 -0700330func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
331 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
332}
333
Colin Cross46c9b8b2017-06-22 16:51:17 -0700334// Module contains the properties and members used by all java module types
335type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700336 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700337 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900338 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900339 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700340
Colin Cross89536d42017-07-07 14:35:50 -0700341 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700342 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700343 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700344
Colin Cross331a1212018-08-15 20:40:52 -0700345 // jar file containing header classes including static library dependencies, suitable for
346 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700347 headerJarFile android.Path
348
Colin Cross331a1212018-08-15 20:40:52 -0700349 // jar file containing implementation classes including static library dependencies but no
350 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700351 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700352
Colin Cross331a1212018-08-15 20:40:52 -0700353 // jar file containing only resources including from static library dependencies
354 resourceJar android.Path
355
Colin Cross0c4ce212019-05-03 15:28:19 -0700356 // args and dependencies to package source files into a srcjar
357 srcJarArgs []string
358 srcJarDeps android.Paths
359
Colin Cross331a1212018-08-15 20:40:52 -0700360 // jar file containing implementation classes and resources including static library
361 // dependencies
362 implementationAndResourcesJar android.Path
363
364 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700365 dexJarFile android.Path
366
Colin Cross43f08db2018-11-12 10:13:39 -0800367 // output file that contains classes.dex if it should be in the output file
368 maybeStrippedDexJarFile android.Path
369
Colin Crosscb933592017-11-22 13:49:43 -0800370 // output file containing uninstrumented classes that will be instrumented by jacoco
371 jacocoReportClassesFile android.Path
372
Colin Cross66dbc0b2017-12-28 12:23:20 -0800373 // output file containing mapping of obfuscated names
374 proguardDictionary android.Path
375
Colin Cross331a1212018-08-15 20:40:52 -0700376 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700377 outputFile android.Path
378 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700379
Colin Cross635c3b02016-05-18 15:37:25 -0700380 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700381
Colin Cross635c3b02016-05-18 15:37:25 -0700382 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700383
Colin Cross2fe66872015-03-30 17:20:39 -0700384 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700385 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800386
387 // list of .java files and srcjars that was passed to javac
388 compiledJavaSrcs android.Paths
389 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800390
391 // list of extra progurad flag files
392 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900393
Colin Cross094054a2018-10-17 15:10:48 -0700394 // manifest file to use instead of properties.Manifest
395 overrideManifest android.OptionalPath
396
Artur Satayev9cf46692019-11-26 18:08:34 +0000397 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900398 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700399
Artur Satayev9cf46692019-11-26 18:08:34 +0000400 // list of plugins that this java module is exporting
401 exportedPluginJars android.Paths
402
403 // list of plugins that this java module is exporting
404 exportedPluginClasses []string
405
406 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800407 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700408 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800409
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800410 // expanded Jarjar_rules
411 expandJarjarRules android.Path
412
Vladimir Marko0975ee02019-04-02 10:29:55 +0100413 // list of additional targets for checkbuild
414 additionalCheckedModules android.Paths
415
Colin Cross988708c2019-05-06 14:04:11 -0700416 // Extra files generated by the module type to be added as java resources.
417 extraResources android.Paths
418
Colin Crossf24a22a2019-01-31 14:12:44 -0800419 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800420 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800421
422 // list of the xref extraction files
423 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700424}
425
Colin Cross41955e82019-05-29 14:40:35 -0700426func (j *Module) OutputFiles(tag string) (android.Paths, error) {
427 switch tag {
428 case "":
429 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700430 case ".jar":
431 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700432 case ".proguard_map":
433 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700434 default:
435 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
436 }
Colin Cross54250902017-12-05 09:28:08 -0800437}
438
Colin Cross41955e82019-05-29 14:40:35 -0700439var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800440
Colin Crossf506d872017-07-19 15:53:04 -0700441type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700442 HeaderJars() android.Paths
443 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700444 ResourceJars() android.Paths
445 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800446 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700447 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900448 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000449 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700450 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700451 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900452 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700453}
454
Jiyong Parkc678ad32018-04-10 13:07:10 +0900455type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900456 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
457 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900458}
459
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800460type xref interface {
461 XrefJavaFiles() android.Paths
462}
463
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800464func (j *Module) XrefJavaFiles() android.Paths {
465 return j.kytheFiles
466}
467
Colin Cross89536d42017-07-07 14:35:50 -0700468func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
469 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
470 android.InitDefaultableModule(module)
471}
472
Colin Crossbe1da472017-07-07 15:59:46 -0700473type dependencyTag struct {
474 blueprint.BaseDependencyTag
475 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700476}
477
Colin Crossa4f08812018-10-02 22:03:40 -0700478type jniDependencyTag struct {
479 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700480}
481
Jiyong Park8be103b2019-11-08 15:53:48 +0900482func IsJniDepTag(depTag blueprint.DependencyTag) bool {
483 _, ok := depTag.(*jniDependencyTag)
484 return ok
485}
486
Colin Crossbe1da472017-07-07 15:59:46 -0700487var (
Colin Cross4b964c02018-10-15 16:18:06 -0700488 staticLibTag = dependencyTag{name: "staticlib"}
489 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700490 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800491 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000492 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700493 bootClasspathTag = dependencyTag{name: "bootclasspath"}
494 systemModulesTag = dependencyTag{name: "system modules"}
495 frameworkResTag = dependencyTag{name: "framework-res"}
496 frameworkApkTag = dependencyTag{name: "framework-apk"}
497 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800498 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700499 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
500 certificateTag = dependencyTag{name: "certificate"}
501 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700502 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700503)
Colin Cross2fe66872015-03-30 17:20:39 -0700504
Jiyong Park83dc74b2020-01-14 18:38:44 +0900505func IsLibDepTag(depTag blueprint.DependencyTag) bool {
506 return depTag == libTag
507}
508
509func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
510 return depTag == staticLibTag
511}
512
Colin Crossfc3674a2017-09-18 17:41:52 -0700513type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700514 useModule, useFiles, useDefaultLibs, invalidVersion bool
515
Colin Cross6cef4812019-10-17 14:23:50 -0700516 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
517 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100518
519 // The default system modules to use. Will be an empty string if no system
520 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700521 systemModules string
522
Colin Cross6cef4812019-10-17 14:23:50 -0700523 // The modules that will be added ot the classpath when targeting 1.9 or higher
524 java9Classpath []string
525
Colin Crossa97c5d32018-03-28 14:58:31 -0700526 frameworkResModule string
527
Colin Cross86a60ae2018-05-29 14:44:55 -0700528 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700529 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100530
531 noStandardLibs, noFrameworksLibs bool
532}
533
534func (s sdkDep) hasStandardLibs() bool {
535 return !s.noStandardLibs
536}
537
538func (s sdkDep) hasFrameworkLibs() bool {
539 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700540}
541
Colin Crossa4f08812018-10-02 22:03:40 -0700542type jniLib struct {
543 name string
544 path android.Path
545 target android.Target
546}
547
Colin Cross0ea8ba82019-06-06 14:33:29 -0700548func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800549 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
550}
551
Colin Cross0ea8ba82019-06-06 14:33:29 -0700552func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800553 return j.shouldInstrument(ctx) &&
554 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
555 ctx.Config().UnbundledBuild())
556}
557
Jiyong Park6a927c42020-01-21 02:03:43 +0900558func (j *Module) sdkVersion() sdkSpec {
559 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700560}
561
Paul Duffine25c6442019-10-11 13:50:28 +0100562func (j *Module) systemModules() string {
563 return proptools.String(j.deviceProperties.System_modules)
564}
565
Jiyong Park6a927c42020-01-21 02:03:43 +0900566func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700567 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900568 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700569 }
570 return j.sdkVersion()
571}
572
Jiyong Park6a927c42020-01-21 02:03:43 +0900573func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700574 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900575 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700576 }
577 return j.sdkVersion()
578}
579
Jiyong Parkb02bb402019-12-03 00:43:57 +0900580func (j *Module) AvailableFor(what string) bool {
581 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
582 // Exception: for hostdex: true libraries, the platform variant is created
583 // even if it's not marked as available to platform. In that case, the platform
584 // variant is used only for the hostdex and not installed to the device.
585 return true
586 }
587 return j.ApexModuleBase.AvailableFor(what)
588}
589
Colin Crossbe1da472017-07-07 15:59:46 -0700590func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700591 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100592 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700593 if sdkDep.useDefaultLibs {
594 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
595 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
596 if sdkDep.hasFrameworkLibs() {
597 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700598 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700599 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700600 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100601 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700602 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700603 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
604 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
605 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
606 }
Colin Cross2fe66872015-03-30 17:20:39 -0700607 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700608
Nan Zhangb2b33de2018-02-23 11:18:47 -0800609 if ctx.ModuleName() == "android_stubs_current" ||
610 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700611 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700612 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800613 }
Colin Cross2fe66872015-03-30 17:20:39 -0700614 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700615
Inseob Kimac1e9862019-12-09 18:15:47 +0900616 syspropPublicStubs := syspropPublicStubs(ctx.Config())
617
618 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
619 // and redirects dependency to public stub depending on the link type.
620 rewriteSyspropLibs := func(libs []string, prop string) []string {
621 // make a copy
622 ret := android.CopyOf(libs)
623
624 for idx, lib := range libs {
625 stub, ok := syspropPublicStubs[lib]
626
627 if !ok {
628 continue
629 }
630
631 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900632 // only platform modules can use internal props
633 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900634 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900635 }
636 }
637
638 return ret
639 }
640
641 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
642 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700643
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700644 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000645 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800646
Colin Crossfe17f6f2019-03-28 19:30:56 -0700647 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700648 if j.hasSrcExt(".proto") {
649 protoDeps(ctx, &j.protoProperties)
650 }
Colin Cross93e85952017-08-15 13:34:18 -0700651
652 if j.hasSrcExt(".kt") {
653 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
654 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700655 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
656 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800657 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800658 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
659 }
Colin Cross93e85952017-08-15 13:34:18 -0700660 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800661
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000662 // Framework libraries need special handling in static coverage builds: they should not have
663 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
664 // the same jacoco classes coming from different bootclasspath jars.
665 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
666 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
667 j.properties.Instrument = true
668 }
669 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700670 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800671 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700672}
673
674func hasSrcExt(srcs []string, ext string) bool {
675 for _, src := range srcs {
676 if filepath.Ext(src) == ext {
677 return true
678 }
679 }
680
681 return false
682}
683
684func (j *Module) hasSrcExt(ext string) bool {
685 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700686}
687
Colin Cross46c9b8b2017-06-22 16:51:17 -0700688func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700689 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700690
Colin Crossebe1a512017-11-14 13:12:14 -0800691 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
692 aidlIncludes = append(aidlIncludes,
693 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
694 aidlIncludes = append(aidlIncludes,
695 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700696
Colin Cross3047fa22019-04-18 10:56:44 -0700697 var flags []string
698 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700699
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700700 if aidlPreprocess.Valid() {
701 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700702 deps = append(deps, aidlPreprocess.Path())
703 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700704 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700705 }
706
Colin Cross3047fa22019-04-18 10:56:44 -0700707 if len(j.exportAidlIncludeDirs) > 0 {
708 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
709 }
710
711 if len(aidlIncludes) > 0 {
712 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
713 }
714
Colin Cross635c3b02016-05-18 15:37:25 -0700715 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800716 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700717 flags = append(flags, "-I"+src.String())
718 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700719
Martijn Coeneneab15642018-03-09 09:29:59 +0100720 if Bool(j.deviceProperties.Aidl.Generate_traces) {
721 flags = append(flags, "-t")
722 }
723
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100724 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
725 flags = append(flags, "--transaction_names")
726 }
727
Colin Cross3047fa22019-04-18 10:56:44 -0700728 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700729}
730
Colin Cross32f676a2017-09-06 13:41:06 -0700731type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800732 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700733 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800734 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700735 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800736 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700737 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700738 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700739 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700740 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800741 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700742 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700743 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700744 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700745 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800746 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800747
748 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700749}
Colin Cross2fe66872015-03-30 17:20:39 -0700750
Colin Cross54250902017-12-05 09:28:08 -0800751func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
752 for _, f := range dep.Srcs() {
753 if f.Ext() != ".jar" {
754 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
755 ctx.OtherModuleName(dep.(blueprint.Module)))
756 }
757 }
758}
759
Jiyong Park2d492942018-03-05 17:44:10 +0900760type linkType int
761
762const (
Jiyong Park50146e92020-01-30 18:00:15 +0900763 // TODO(jiyong) rename these for better readability. Make the allowed
764 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900765 javaCore linkType = iota
766 javaSdk
767 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900768 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900769 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +0900770 javaPlatform
771)
772
Jeongik Cha75b83b02019-11-01 15:28:00 +0900773type linkTypeContext interface {
774 android.Module
775 getLinkType(name string) (ret linkType, stubs bool)
776}
777
778func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700779 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700780 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900781 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
782 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100783 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900784 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900785 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900786 return javaCore, false
787 case name == "android_system_stubs_current":
788 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900789 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900790 return javaSystem, false
791 case name == "android_test_stubs_current":
792 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900793 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900794 return javaPlatform, false
795 case name == "android_stubs_current":
796 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900797 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900798 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900799 case name == "android_module_lib_stubs_current":
800 return javaModule, true
801 case ver.kind == sdkModule:
802 return javaModule, false
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000803 case name == "android_system_server_stubs_current":
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900804 return javaSystemServer, true
805 case ver.kind == sdkSystemServer:
806 return javaSystemServer, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900807 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900808 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900809 case !ver.valid():
810 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700811 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900812 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900813 }
814}
815
Jeongik Cha75b83b02019-11-01 15:28:00 +0900816func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700817 if ctx.Host() {
818 return
819 }
820
Jeongik Cha75b83b02019-11-01 15:28:00 +0900821 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900822 if stubs {
823 return
824 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900825 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900826 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."
827
828 switch myLinkType {
829 case javaCore:
830 if otherLinkType != javaCore {
831 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 +0900832 ctx.OtherModuleName(to))
833 }
Jiyong Park2d492942018-03-05 17:44:10 +0900834 break
835 case javaSdk:
836 if otherLinkType != javaCore && otherLinkType != javaSdk {
837 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
838 ctx.OtherModuleName(to))
839 }
840 break
841 case javaSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900842 if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer {
Jiyong Park2d492942018-03-05 17:44:10 +0900843 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
844 ctx.OtherModuleName(to))
845 }
846 break
Jiyong Park50146e92020-01-30 18:00:15 +0900847 case javaModule:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900848 if otherLinkType == javaPlatform || otherLinkType == javaSystemServer {
Jiyong Park50146e92020-01-30 18:00:15 +0900849 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
850 ctx.OtherModuleName(to))
851 }
852 break
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900853 case javaSystemServer:
854 if otherLinkType == javaPlatform {
855 ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage,
856 ctx.OtherModuleName(to))
857 }
858 break
Jiyong Park2d492942018-03-05 17:44:10 +0900859 case javaPlatform:
860 // no restriction on link-type
861 break
Jiyong Park750e5572018-01-31 00:20:13 +0900862 }
863}
864
Colin Cross32f676a2017-09-06 13:41:06 -0700865func (j *Module) collectDeps(ctx android.ModuleContext) deps {
866 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700867
Colin Cross300f0382018-03-06 13:11:51 -0800868 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700869 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800870 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700871 ctx.AddMissingDependencies(sdkDep.bootclasspath)
872 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800873 } else if sdkDep.useFiles {
874 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700875 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700876 deps.aidlPreprocess = sdkDep.aidl
877 } else {
878 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800879 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700880 }
881
Colin Crossd11fcda2017-10-23 17:59:01 -0700882 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700883 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700884 tag := ctx.OtherModuleDependencyTag(module)
885
Colin Crossa4f08812018-10-02 22:03:40 -0700886 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700887 // Handled by AndroidApp.collectAppDeps
888 return
889 }
890 if tag == certificateTag {
891 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700892 return
893 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900894 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900895 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900896 if to, ok := module.(linkTypeContext); ok {
897 switch tag {
898 case bootClasspathTag, libTag, staticLibTag:
899 checkLinkType(ctx, j, to, tag.(dependencyTag))
900 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700901 }
Jiyong Park750e5572018-01-31 00:20:13 +0900902 }
Colin Cross54250902017-12-05 09:28:08 -0800903 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800904 case SdkLibraryDependency:
905 switch tag {
906 case libTag:
907 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
908 // names of sdk libs that are directly depended are exported
909 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700910 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800911 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
912 }
Colin Cross54250902017-12-05 09:28:08 -0800913 case Dependency:
914 switch tag {
915 case bootClasspathTag:
916 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700917 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800918 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900919 // sdk lib names from dependencies are re-exported
920 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700921 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000922 pluginJars, pluginClasses := dep.ExportedPlugins()
923 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700924 case java9LibTag:
925 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800926 case staticLibTag:
927 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
928 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
929 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700930 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900931 // sdk lib names from dependencies are re-exported
932 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700933 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000934 pluginJars, pluginClasses := dep.ExportedPlugins()
935 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800936 case pluginTag:
937 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800938 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000939 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
940 } else {
941 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800942 }
943 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
944 } else {
945 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
946 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000947 case exportedPluginTag:
948 if plugin, ok := dep.(*Plugin); ok {
949 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
950 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
951 }
952 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
953 if plugin.pluginProperties.Processor_class != nil {
954 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
955 }
956 } else {
957 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
958 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800959 case frameworkApkTag:
960 if ctx.ModuleName() == "android_stubs_current" ||
961 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700962 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800963 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
964 // resource files out of there for aapt.
965 //
966 // Normally the package rule runs aapt, which includes the resource,
967 // but we're not running that in our package rule so just copy in the
968 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700969 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800970 }
Colin Cross54250902017-12-05 09:28:08 -0800971 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700972 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800973 case kotlinAnnotationsTag:
974 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800975 }
976
Colin Cross54250902017-12-05 09:28:08 -0800977 case android.SourceFileProducer:
978 switch tag {
979 case libTag:
980 checkProducesJars(ctx, dep)
981 deps.classpath = append(deps.classpath, dep.Srcs()...)
982 case staticLibTag:
983 checkProducesJars(ctx, dep)
984 deps.classpath = append(deps.classpath, dep.Srcs()...)
985 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
986 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800987 }
988 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700989 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100990 case bootClasspathTag:
991 // If a system modules dependency has been added to the bootclasspath
992 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +0000993 sm := module.(SystemModulesProvider)
994 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +0100995
Colin Cross1369cdb2017-09-29 17:58:17 -0700996 case systemModulesTag:
997 if deps.systemModules != nil {
998 panic("Found two system module dependencies")
999 }
Paul Duffin83a2d962019-11-19 19:44:10 +00001000 sm := module.(SystemModulesProvider)
1001 outputDir, outputDeps := sm.OutputDirAndDeps()
1002 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001003 }
Colin Crossec7a0422017-07-07 14:47:12 -07001004 }
Colin Cross2fe66872015-03-30 17:20:39 -07001005 })
1006
Jiyong Park1be96912018-05-28 18:02:19 +09001007 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1008
Colin Cross32f676a2017-09-06 13:41:06 -07001009 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001010}
1011
Artur Satayev9cf46692019-11-26 18:08:34 +00001012func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1013 deps.processorPath = append(deps.processorPath, pluginJars...)
1014 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1015}
1016
Colin Cross1e743852019-10-28 11:37:20 -07001017func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001018 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001019 if err != nil {
1020 ctx.PropertyErrorf("sdk_version", "%s", err)
1021 }
Nan Zhang357466b2018-04-17 17:38:36 -07001022 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001023 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001024 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001025 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001026 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001027 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001028 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1029 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001030 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001031 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001032 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001033 }
Nan Zhang357466b2018-04-17 17:38:36 -07001034}
1035
Colin Cross1e743852019-10-28 11:37:20 -07001036type javaVersion int
1037
1038const (
1039 JAVA_VERSION_UNSUPPORTED = 0
1040 JAVA_VERSION_6 = 6
1041 JAVA_VERSION_7 = 7
1042 JAVA_VERSION_8 = 8
1043 JAVA_VERSION_9 = 9
1044)
1045
1046func (v javaVersion) String() string {
1047 switch v {
1048 case JAVA_VERSION_6:
1049 return "1.6"
1050 case JAVA_VERSION_7:
1051 return "1.7"
1052 case JAVA_VERSION_8:
1053 return "1.8"
1054 case JAVA_VERSION_9:
1055 return "1.9"
1056 default:
1057 return "unsupported"
1058 }
1059}
1060
1061// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1062func (v javaVersion) usesJavaModules() bool {
1063 return v >= 9
1064}
1065
1066func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001067 switch javaVersion {
1068 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001069 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001070 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001071 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001072 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001073 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001074 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001075 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001076 case "10", "11":
1077 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001078 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001079 default:
1080 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001081 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001082 }
1083}
1084
Nan Zhanged19fc32017-10-19 13:06:22 -07001085func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001086
Colin Crossf03c82b2015-04-13 13:53:40 -07001087 var flags javaBuilderFlags
1088
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001089 // javaVersion flag.
1090 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1091
Nan Zhanged19fc32017-10-19 13:06:22 -07001092 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001093 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001094 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001095 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001096 }
Colin Cross6510f912017-11-29 00:27:14 -08001097 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001098 // Override the -g flag passed globally to remove local variable debug info to reduce
1099 // disk and memory usage.
1100 javacFlags = append(javacFlags, "-g:source,lines")
1101 }
Colin Crossc228a702019-11-06 16:18:05 -08001102 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001103
Colin Cross66548102018-06-19 22:47:35 -07001104 if ctx.Config().RunErrorProne() {
1105 if config.ErrorProneClasspath == nil {
1106 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1107 }
1108
1109 errorProneFlags := []string{
1110 "-Xplugin:ErrorProne",
1111 "${config.ErrorProneChecks}",
1112 }
1113 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1114
1115 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1116 "'" + strings.Join(errorProneFlags, " ") + "'"
1117 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001118 }
1119
Nan Zhanged19fc32017-10-19 13:06:22 -07001120 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001121 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1122 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001123 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001124 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001125
Colin Crossbe9cdb82019-01-21 21:37:16 -08001126 flags.processor = strings.Join(deps.processorClasses, ",")
1127
Colin Cross1e743852019-10-28 11:37:20 -07001128 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1129 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001130 // Give host-side tools a version of OpenJDK's standard libraries
1131 // close to what they're targeting. As of Dec 2017, AOSP is only
1132 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1133 //
1134 // When building with OpenJDK 8, the following should have no
1135 // effect since those jars would be available by default.
1136 //
1137 // When building with OpenJDK 9 but targeting a version < 1.8,
1138 // putting them on the bootclasspath means that:
1139 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1140 // b) references to existing APIs are not reinterpreted in an
1141 // OpenJDK 9-specific way, eg. calls to subclasses of
1142 // java.nio.Buffer as in http://b/70862583
1143 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1144 flags.bootClasspath = append(flags.bootClasspath,
1145 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1146 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001147 if Bool(j.properties.Use_tools_jar) {
1148 flags.bootClasspath = append(flags.bootClasspath,
1149 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1150 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001151 }
1152
Colin Cross1e743852019-10-28 11:37:20 -07001153 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001154 // Manually specify build directory in case it is not under the repo root.
1155 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1156 // just adding a symlink under the root doesn't help.)
1157 patchPaths := ".:" + ctx.Config().BuildDir()
1158 classPath := flags.classpath.FormJavaClassPath("")
1159 if classPath != "" {
1160 patchPaths += ":" + classPath
1161 }
1162 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001163 }
1164
Nan Zhanged19fc32017-10-19 13:06:22 -07001165 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001166 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001167
Nan Zhanged19fc32017-10-19 13:06:22 -07001168 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001169 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001170
Colin Cross81440082018-08-15 20:21:55 -07001171 if len(javacFlags) > 0 {
1172 // optimization.
1173 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1174 flags.javacFlags = "$javacFlags"
1175 }
1176
Nan Zhanged19fc32017-10-19 13:06:22 -07001177 return flags
1178}
Colin Crossc0b06f12015-04-08 13:03:43 -07001179
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001180func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001181 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001182
1183 deps := j.collectDeps(ctx)
1184 flags := j.collectBuilderFlags(ctx, deps)
1185
Colin Cross1e743852019-10-28 11:37:20 -07001186 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001187 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1188 }
Colin Cross8a497952019-03-05 22:25:09 -08001189 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001190 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001191 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001192 }
1193
Colin Crossaf050172017-11-15 23:01:59 -08001194 srcFiles = j.genSources(ctx, srcFiles, flags)
1195
1196 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001197 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001198 if aaptSrcJar != nil {
1199 srcJars = append(srcJars, aaptSrcJar)
1200 }
Colin Crossb7a63242015-04-16 14:09:14 -07001201
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001202 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001203 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001204 }
1205
Colin Cross1ee23172017-10-18 14:44:18 -07001206 jarName := ctx.ModuleName() + ".jar"
1207
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001208 javaSrcFiles := srcFiles.FilterByExt(".java")
1209 var uniqueSrcFiles android.Paths
1210 set := make(map[string]bool)
1211 for _, v := range javaSrcFiles {
1212 if _, found := set[v.String()]; !found {
1213 set[v.String()] = true
1214 uniqueSrcFiles = append(uniqueSrcFiles, v)
1215 }
1216 }
1217
patricktu242faad2019-09-24 15:41:30 +08001218 // Collect .java files for AIDEGen
1219 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1220
Colin Cross55f63ea2018-08-27 12:37:09 -07001221 var kotlinJars android.Paths
1222
Colin Cross93e85952017-08-15 13:34:18 -07001223 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001224 // user defined kotlin flags.
1225 kotlincFlags := j.properties.Kotlincflags
1226 CheckKotlincFlags(ctx, kotlincFlags)
1227
Colin Cross93e85952017-08-15 13:34:18 -07001228 // If there are kotlin files, compile them first but pass all the kotlin and java files
1229 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1230 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001231 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001232 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001233 kotlincFlags = append(kotlincFlags, "-no-jdk")
1234 }
1235 if len(kotlincFlags) > 0 {
1236 // optimization.
1237 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1238 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001239 }
1240
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001241 var kotlinSrcFiles android.Paths
1242 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1243 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1244
patricktu242faad2019-09-24 15:41:30 +08001245 // Collect .kt files for AIDEGen
1246 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1247
Colin Crossafbb1732019-01-17 15:42:52 -08001248 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1249 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1250
1251 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1252 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1253
1254 if len(flags.processorPath) > 0 {
1255 // Use kapt for annotation processing
1256 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1257 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1258 srcJars = append(srcJars, kaptSrcJar)
1259 // Disable annotation processing in javac, it's already been handled by kapt
1260 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001261 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001262 }
Colin Cross93e85952017-08-15 13:34:18 -07001263
Colin Cross1ee23172017-10-18 14:44:18 -07001264 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001265 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001266 if ctx.Failed() {
1267 return
1268 }
1269
1270 // Make javac rule depend on the kotlinc rule
1271 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001272
Colin Cross93e85952017-08-15 13:34:18 -07001273 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001274 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001275 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001276 }
1277
Colin Cross55f63ea2018-08-27 12:37:09 -07001278 jars := append(android.Paths(nil), kotlinJars...)
1279
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001280 // Store the list of .java files that was passed to javac
1281 j.compiledJavaSrcs = uniqueSrcFiles
1282 j.compiledSrcJars = srcJars
1283
Nan Zhang61eaedb2017-11-02 13:28:15 -07001284 enable_sharding := false
Colin Crossf7d84012020-02-21 08:16:41 -08001285 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001286 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001287 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1288 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001289 // Formerly, there was a check here that prevented annotation processors
1290 // from being used when sharding was enabled, as some annotation processors
1291 // do not function correctly in sharded environments. It was removed to
1292 // allow for the use of annotation processors that do function correctly
1293 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001294 }
Colin Crossf7d84012020-02-21 08:16:41 -08001295 headerJarFileWithoutJarjar, j.headerJarFile =
1296 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001297 if ctx.Failed() {
1298 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001299 }
1300 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001301 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001302 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001303 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001304 // If error-prone is enabled, add an additional rule to compile the java files into
1305 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001306 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001307 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1308 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001309 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001310 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001311 extraJarDeps = append(extraJarDeps, errorprone)
1312 }
1313
Nan Zhang61eaedb2017-11-02 13:28:15 -07001314 if enable_sharding {
Colin Crossf7d84012020-02-21 08:16:41 -08001315 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001316 shardSize := int(*(j.properties.Javac_shard_size))
1317 var shardSrcs []android.Paths
1318 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001319 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001320 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001321 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1322 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001323 jars = append(jars, classes)
1324 }
1325 }
1326 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001327 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1328 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001329 jars = append(jars, classes)
1330 }
1331 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001332 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001333 jars = append(jars, classes)
1334 }
Colin Crossd6891432017-09-27 17:39:56 -07001335 if ctx.Failed() {
1336 return
1337 }
Colin Cross2fe66872015-03-30 17:20:39 -07001338 }
1339
Colin Cross0c4ce212019-05-03 15:28:19 -07001340 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1341
1342 var includeSrcJar android.WritablePath
1343 if Bool(j.properties.Include_srcs) {
1344 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1345 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1346 }
1347
Colin Crosscedd4762018-09-13 11:26:19 -07001348 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1349 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001350 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001351 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001352
1353 var resArgs []string
1354 var resDeps android.Paths
1355
1356 resArgs = append(resArgs, dirArgs...)
1357 resDeps = append(resDeps, dirDeps...)
1358
1359 resArgs = append(resArgs, fileArgs...)
1360 resDeps = append(resDeps, fileDeps...)
1361
Colin Cross988708c2019-05-06 14:04:11 -07001362 resArgs = append(resArgs, extraArgs...)
1363 resDeps = append(resDeps, extraDeps...)
1364
Colin Cross40a36712017-09-27 17:41:35 -07001365 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001366 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001367 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001368 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001369 if ctx.Failed() {
1370 return
1371 }
1372 }
1373
Colin Cross0c4ce212019-05-03 15:28:19 -07001374 var resourceJars android.Paths
1375 if j.resourceJar != nil {
1376 resourceJars = append(resourceJars, j.resourceJar)
1377 }
1378 if Bool(j.properties.Include_srcs) {
1379 resourceJars = append(resourceJars, includeSrcJar)
1380 }
1381 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001382
Colin Cross0c4ce212019-05-03 15:28:19 -07001383 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001384 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001385 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001386 false, nil, nil)
1387 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001388 } else if len(resourceJars) == 1 {
1389 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001390 }
1391
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001392 if len(deps.staticJars) > 0 {
1393 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001394 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001395
Colin Cross094054a2018-10-17 15:10:48 -07001396 manifest := j.overrideManifest
1397 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001398 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001399 }
Colin Cross635acc92017-09-12 22:50:46 -07001400
Colin Cross8a497952019-03-05 22:25:09 -08001401 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001402 if len(services) > 0 {
1403 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1404 var zipargs []string
1405 for _, file := range services {
1406 serviceFile := file.String()
1407 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1408 }
1409 ctx.Build(pctx, android.BuildParams{
1410 Rule: zip,
1411 Output: servicesJar,
1412 Implicits: services,
1413 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001414 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001415 },
1416 })
1417 jars = append(jars, servicesJar)
1418 }
1419
Colin Cross0a6e0072017-08-30 14:24:55 -07001420 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001421 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001422 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001423
1424 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001425 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1426 // Optimization: skip the combine step if there is nothing to do
1427 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1428 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1429 // any if len(jars) == 1.
1430 outputFile = moduleOutPath
1431 } else {
1432 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1433 ctx.Build(pctx, android.BuildParams{
1434 Rule: android.Cp,
1435 Input: jars[0],
1436 Output: combinedJar,
1437 })
1438 outputFile = combinedJar
1439 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001440 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001441 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001442 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001443 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001444 outputFile = combinedJar
1445 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001446
Colin Cross331a1212018-08-15 20:40:52 -07001447 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001448 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001449 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001450 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001451 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001452 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001453
1454 // jarjar resource jar if necessary
1455 if j.resourceJar != nil {
1456 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001457 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001458 j.resourceJar = resourceJarJarFile
1459 }
1460
Colin Cross0a6e0072017-08-30 14:24:55 -07001461 if ctx.Failed() {
1462 return
1463 }
1464 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001465
1466 // Check package restrictions if necessary.
1467 if len(j.properties.Permitted_packages) > 0 {
1468 // Check packages and copy to package-checked file.
1469 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1470 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1471 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1472
1473 if ctx.Failed() {
1474 return
1475 }
1476 }
1477
Nan Zhanged19fc32017-10-19 13:06:22 -07001478 j.implementationJarFile = outputFile
1479 if j.headerJarFile == nil {
1480 j.headerJarFile = j.implementationJarFile
1481 }
Colin Cross2fe66872015-03-30 17:20:39 -07001482
Jiyong Park93e57a02020-02-21 16:04:53 +09001483 // Force enable the instrumentation for java code that is built for APEXes ...
1484 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1485 // doesn't make sense)
1486 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1487 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park00cae1c2020-02-18 12:50:44 +00001488 j.properties.Instrument = true
1489 }
1490
Colin Cross3144dfc2018-01-03 15:06:47 -08001491 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001492 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1493 }
1494
Colin Cross331a1212018-08-15 20:40:52 -07001495 // merge implementation jar with resources if necessary
1496 implementationAndResourcesJar := outputFile
1497 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001498 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001499 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001500 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001501 false, nil, nil)
1502 implementationAndResourcesJar = combinedJar
1503 }
1504
1505 j.implementationAndResourcesJar = implementationAndResourcesJar
1506
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001507 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1508 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1509 if j.deviceProperties.Compile_dex == nil {
1510 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1511 }
1512 if j.deviceProperties.Hostdex == nil {
1513 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1514 }
1515 }
1516
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001517 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001518 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001519 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001520 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001521 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001522 if ctx.Failed() {
1523 return
1524 }
Colin Cross331a1212018-08-15 20:40:52 -07001525
Jiyong Park09cb6292019-07-15 15:29:23 +09001526 // Hidden API CSV generation and dex encoding
1527 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1528 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001529
Colin Cross331a1212018-08-15 20:40:52 -07001530 // merge dex jar with resources if necessary
1531 if j.resourceJar != nil {
1532 jars := android.Paths{dexOutputFile, j.resourceJar}
1533 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1534 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1535 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001536 if j.deviceProperties.UncompressDex {
1537 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1538 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1539 dexOutputFile = combinedAlignedJar
1540 } else {
1541 dexOutputFile = combinedJar
1542 }
Colin Cross331a1212018-08-15 20:40:52 -07001543 }
1544
1545 j.dexJarFile = dexOutputFile
1546
Colin Cross8faf8fc2019-01-16 15:15:52 -08001547 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001548 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1549
1550 j.maybeStrippedDexJarFile = dexOutputFile
1551
Colin Cross3063b782018-08-15 11:19:12 -07001552 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001553
1554 if ctx.Failed() {
1555 return
1556 }
Colin Cross331a1212018-08-15 20:40:52 -07001557 } else {
1558 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001559 }
Colin Cross331a1212018-08-15 20:40:52 -07001560
Colin Crossb7a63242015-04-16 14:09:14 -07001561 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001562
1563 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1564 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001565}
1566
Colin Cross3b706fd2019-09-05 16:44:18 -07001567func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1568 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1569
1570 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1571 if idx >= 0 {
1572 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1573 jarName += strconv.Itoa(idx)
1574 }
1575
1576 classes := android.PathForModuleOut(ctx, "javac", jarName)
1577 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1578
1579 if ctx.Config().EmitXrefRules() {
1580 extractionFile := android.PathForModuleOut(ctx, kzipName)
1581 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1582 j.kytheFiles = append(j.kytheFiles, extractionFile)
1583 }
1584
1585 return classes
1586}
1587
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001588// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1589// since some of these flags may be used internally.
1590func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1591 for _, flag := range flags {
1592 flag = strings.TrimSpace(flag)
1593
1594 if !strings.HasPrefix(flag, "-") {
1595 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1596 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1597 ctx.PropertyErrorf("kotlincflags",
1598 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1599 } else if inList(flag, config.KotlincIllegalFlags) {
1600 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1601 } else if flag == "-include-runtime" {
1602 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1603 } else {
1604 args := strings.Split(flag, " ")
1605 if args[0] == "-kotlin-home" {
1606 ctx.PropertyErrorf("kotlincflags",
1607 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1608 }
1609 }
1610 }
1611}
1612
Colin Cross8eadbf02017-10-24 17:46:00 -07001613func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf7d84012020-02-21 08:16:41 -08001614 deps deps, flags javaBuilderFlags, jarName string,
1615 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001616
1617 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001618 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001619 // Compile java sources into turbine.jar.
1620 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1621 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1622 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08001623 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001624 }
1625 jars = append(jars, turbineJar)
1626 }
1627
Colin Cross55f63ea2018-08-27 12:37:09 -07001628 jars = append(jars, extraJars...)
1629
Nan Zhanged19fc32017-10-19 13:06:22 -07001630 // Combine any static header libraries into classes-header.jar. If there is only
1631 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001632 jars = append(jars, deps.staticHeaderJars...)
1633
Colin Cross5c6ecc12017-10-23 18:12:27 -07001634 // we cannot skip the combine step for now if there is only one jar
1635 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1636 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001637 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001638 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001639 headerJar = combinedJar
Colin Crossf7d84012020-02-21 08:16:41 -08001640 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001641
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001642 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001643 // Transform classes.jar into classes-jarjar.jar
1644 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001645 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf7d84012020-02-21 08:16:41 -08001646 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001647 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08001648 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001649 }
1650 }
1651
Colin Crossf7d84012020-02-21 08:16:41 -08001652 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001653}
1654
Colin Crosscb933592017-11-22 13:49:43 -08001655func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001656 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001657
Colin Cross7a3139e2017-12-19 13:57:50 -08001658 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001659
Colin Cross84c38822018-01-03 15:59:46 -08001660 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001661 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1662
1663 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1664
1665 j.jacocoReportClassesFile = jacocoReportClassesFile
1666
1667 return instrumentedJar
1668}
1669
albaltai36ff7dc2018-12-25 14:35:23 +08001670var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001671
Nan Zhanged19fc32017-10-19 13:06:22 -07001672func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001673 if j.headerJarFile == nil {
1674 return nil
1675 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001676 return android.Paths{j.headerJarFile}
1677}
1678
1679func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001680 if j.implementationJarFile == nil {
1681 return nil
1682 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001683 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001684}
1685
Colin Crossf24a22a2019-01-31 14:12:44 -08001686func (j *Module) DexJar() android.Path {
1687 return j.dexJarFile
1688}
1689
Colin Cross331a1212018-08-15 20:40:52 -07001690func (j *Module) ResourceJars() android.Paths {
1691 if j.resourceJar == nil {
1692 return nil
1693 }
1694 return android.Paths{j.resourceJar}
1695}
1696
1697func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001698 if j.implementationAndResourcesJar == nil {
1699 return nil
1700 }
Colin Cross331a1212018-08-15 20:40:52 -07001701 return android.Paths{j.implementationAndResourcesJar}
1702}
1703
Colin Cross46c9b8b2017-06-22 16:51:17 -07001704func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001705 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001706 return j.exportAidlIncludeDirs
1707}
1708
Jiyong Park1be96912018-05-28 18:02:19 +09001709func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001710 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001711 return j.exportedSdkLibs
1712}
1713
Artur Satayev9cf46692019-11-26 18:08:34 +00001714func (j *Module) ExportedPlugins() (android.Paths, []string) {
1715 return j.exportedPluginJars, j.exportedPluginClasses
1716}
1717
Colin Cross0c4ce212019-05-03 15:28:19 -07001718func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1719 return j.srcJarArgs, j.srcJarDeps
1720}
1721
Colin Cross46c9b8b2017-06-22 16:51:17 -07001722var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001723
Colin Cross46c9b8b2017-06-22 16:51:17 -07001724func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001725 return j.logtagsSrcs
1726}
1727
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001728// Collect information for opening IDE project files in java/jdeps.go.
1729func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1730 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1731 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001732 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001733 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001734 if j.expandJarjarRules != nil {
1735 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001736 }
1737}
1738
1739func (j *Module) CompilerDeps() []string {
1740 jdeps := []string{}
1741 jdeps = append(jdeps, j.properties.Libs...)
1742 jdeps = append(jdeps, j.properties.Static_libs...)
1743 return jdeps
1744}
1745
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001746func (j *Module) hasCode(ctx android.ModuleContext) bool {
1747 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1748 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1749}
1750
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001751func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001752 // Dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Han5e9013b2020-03-10 06:23:13 +09001753 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
1754 return true
1755 }
Jiyong Park0f80c182020-01-31 02:49:53 +09001756 // Also, a dependency to an sdk member is also considered as such. This is required because
1757 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Han5e9013b2020-03-10 06:23:13 +09001758 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
1759 return true
1760 }
1761 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001762}
1763
Jiyong Park0b238752019-10-29 11:23:10 +09001764func (j *Module) Stem() string {
1765 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1766}
1767
Jiyong Park618922e2020-01-08 13:35:43 +09001768func (j *Module) JacocoReportClassesFile() android.Path {
1769 return j.jacocoReportClassesFile
1770}
1771
Martin Stjernholm6d415272020-01-31 17:10:36 +00001772func (j *Module) IsInstallable() bool {
1773 return Bool(j.properties.Installable)
1774}
1775
Colin Cross2fe66872015-03-30 17:20:39 -07001776//
1777// Java libraries (.jar file)
1778//
1779
Colin Crossf506d872017-07-19 15:53:04 -07001780type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001781 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001782
1783 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001784}
1785
Colin Cross42be7612019-02-21 18:12:14 -08001786func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001787 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1788 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1789 return true
1790 }
1791
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001792 // Store uncompressed (and do not strip) dex files from boot class path jars.
1793 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1794 return true
1795 }
1796
1797 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001798 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001799 return true
1800 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001801 if ctx.Config().UncompressPrivAppDex() &&
1802 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1803 return true
1804 }
1805
Colin Cross2fc72f62018-12-21 12:59:54 -08001806 return false
1807}
1808
Colin Crossf506d872017-07-19 15:53:04 -07001809func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001810 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001811 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001812 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001813 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001814 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001815 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001816
Jiyong Park7f7766d2019-07-25 22:02:35 +09001817 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1818 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001819 var extraInstallDeps android.Paths
1820 if j.InstallMixin != nil {
1821 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1822 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001823 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001824 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001825 }
Colin Crossb7a63242015-04-16 14:09:14 -07001826}
1827
Colin Crossf506d872017-07-19 15:53:04 -07001828func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001829 j.deps(ctx)
1830}
1831
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001832const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001833 aidlIncludeDir = "aidl"
1834 javaDir = "java"
1835 jarFileSuffix = ".jar"
1836 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001837)
1838
Paul Duffina0dbf432019-12-05 11:25:53 +00001839// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +00001840func sdkSnapshotFilePathForJar(osPrefix, name string) string {
1841 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001842}
1843
Paul Duffina04c1072020-03-02 10:16:35 +00001844func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
1845 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001846}
1847
Paul Duffin13879572019-11-28 14:31:38 +00001848type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001849 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001850
1851 // Function to retrieve the appropriate output jar (implementation or header) from
1852 // the library.
1853 jarToExportGetter func(j *Library) android.Path
Paul Duffin13879572019-11-28 14:31:38 +00001854}
1855
1856func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1857 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1858}
1859
1860func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1861 _, ok := module.(*Library)
1862 return ok
1863}
1864
Paul Duffin3a4eb502020-03-19 16:11:18 +00001865func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1866 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00001867}
Paul Duffina0dbf432019-12-05 11:25:53 +00001868
Paul Duffin14eb4672020-03-02 11:33:02 +00001869func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +00001870 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +00001871}
1872
1873type librarySdkMemberProperties struct {
1874 android.SdkMemberPropertiesBase
1875
Paul Duffina551a1c2020-03-17 21:04:24 +00001876 JarToExport android.Path
1877 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +00001878}
1879
Paul Duffin3a4eb502020-03-19 16:11:18 +00001880func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +00001881 j := variant.(*Library)
1882
Paul Duffina551a1c2020-03-17 21:04:24 +00001883 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(j)
1884 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +00001885}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001886
Paul Duffin3a4eb502020-03-19 16:11:18 +00001887func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001888 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001889
Paul Duffina551a1c2020-03-17 21:04:24 +00001890 exportedJar := p.JarToExport
1891 if exportedJar != nil {
1892 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
Paul Duffin14eb4672020-03-02 11:33:02 +00001893 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
1894
Paul Duffina551a1c2020-03-17 21:04:24 +00001895 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
1896 }
1897
1898 aidlIncludeDirs := p.AidlIncludeDirs
1899 if len(aidlIncludeDirs) != 0 {
1900 sdkModuleContext := ctx.SdkModuleContext()
1901 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +00001902 // TODO(jiyong): copy parcelable declarations only
1903 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1904 for _, file := range aidlFiles {
1905 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1906 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001907 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001908
Paul Duffina551a1c2020-03-17 21:04:24 +00001909 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +00001910 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001911}
1912
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001913var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
1914 android.SdkMemberTypeBase{
1915 PropertyName: "java_header_libs",
1916 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001917 },
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001918 func(j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +00001919 headerJars := j.HeaderJars()
1920 if len(headerJars) != 1 {
1921 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1922 }
1923
1924 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001925 },
Paul Duffina0dbf432019-12-05 11:25:53 +00001926}
1927
Colin Cross1b16b0e2019-02-12 14:41:32 -08001928// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1929//
1930// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1931// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1932// as a `static_libs` dependency of another module.
1933//
1934// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1935// a device.
1936//
1937// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1938// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001939func LibraryFactory() android.Module {
1940 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001941
Colin Cross9ae1b922018-06-26 17:59:05 -07001942 module.AddProperties(
1943 &module.Module.properties,
1944 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001945 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001946 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001947
Jiyong Park7f7766d2019-07-25 22:02:35 +09001948 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001949 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001950 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001951 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001952}
1953
Colin Cross1b16b0e2019-02-12 14:41:32 -08001954// java_library_static is an obsolete alias for java_library.
1955func LibraryStaticFactory() android.Module {
1956 return LibraryFactory()
1957}
1958
1959// java_library_host builds and links sources into a `.jar` file for the host.
1960//
1961// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1962// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001963func LibraryHostFactory() android.Module {
1964 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001965
Colin Cross6af17aa2017-09-20 12:59:05 -07001966 module.AddProperties(
1967 &module.Module.properties,
1968 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001969
Colin Cross9ae1b922018-06-26 17:59:05 -07001970 module.Module.properties.Installable = proptools.BoolPtr(true)
1971
Jiyong Park7f7766d2019-07-25 22:02:35 +09001972 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001973 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001974 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001975}
1976
1977//
Colin Crossb628ea52018-08-14 16:42:33 -07001978// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001979//
1980
1981type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001982 // list of compatibility suites (for example "cts", "vts") that the module should be
1983 // installed into.
1984 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001985
1986 // the name of the test configuration (for example "AndroidTest.xml") that should be
1987 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001988 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001989
Jack He33338892018-09-19 02:21:28 -07001990 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1991 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001992 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001993
Colin Crossd96ca352018-08-10 16:06:24 -07001994 // list of files or filegroup modules that provide data that should be installed alongside
1995 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001996 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001997
1998 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1999 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2000 // explicitly.
2001 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002002}
2003
Paul Duffin42df1442019-03-20 12:45:53 +00002004type testHelperLibraryProperties struct {
2005 // list of compatibility suites (for example "cts", "vts") that the module should be
2006 // installed into.
2007 Test_suites []string `android:"arch_variant"`
2008}
2009
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002010type prebuiltTestProperties struct {
2011 // list of compatibility suites (for example "cts", "vts") that the module should be
2012 // installed into.
2013 Test_suites []string `android:"arch_variant"`
2014
2015 // the name of the test configuration (for example "AndroidTest.xml") that should be
2016 // installed with the module.
2017 Test_config *string `android:"path,arch_variant"`
2018}
2019
Colin Cross05638fc2018-04-09 18:40:24 -07002020type Test struct {
2021 Library
2022
2023 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002024
2025 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002026 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002027}
2028
Paul Duffin42df1442019-03-20 12:45:53 +00002029type TestHelperLibrary struct {
2030 Library
2031
2032 testHelperLibraryProperties testHelperLibraryProperties
2033}
2034
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002035type JavaTestImport struct {
2036 Import
2037
2038 prebuiltTestProperties prebuiltTestProperties
2039
2040 testConfig android.Path
2041}
2042
Colin Cross303e21f2018-08-07 16:49:25 -07002043func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002044 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2045 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002046 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002047
2048 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002049}
2050
Paul Duffin42df1442019-03-20 12:45:53 +00002051func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2052 j.Library.GenerateAndroidBuildActions(ctx)
2053}
2054
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002055func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2056 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2057 j.prebuiltTestProperties.Test_suites, nil)
2058
2059 j.Import.GenerateAndroidBuildActions(ctx)
2060}
2061
2062type testSdkMemberType struct {
2063 android.SdkMemberTypeBase
2064}
2065
2066func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2067 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2068}
2069
2070func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2071 _, ok := module.(*Test)
2072 return ok
2073}
2074
Paul Duffin3a4eb502020-03-19 16:11:18 +00002075func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2076 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00002077}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002078
Paul Duffin14eb4672020-03-02 11:33:02 +00002079func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2080 return &testSdkMemberProperties{}
2081}
2082
2083type testSdkMemberProperties struct {
2084 android.SdkMemberPropertiesBase
2085
Paul Duffina551a1c2020-03-17 21:04:24 +00002086 JarToExport android.Path
2087 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00002088}
2089
Paul Duffin3a4eb502020-03-19 16:11:18 +00002090func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00002091 test := variant.(*Test)
2092
2093 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002094 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00002095 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002096 }
2097
Paul Duffina551a1c2020-03-17 21:04:24 +00002098 p.JarToExport = implementationJars[0]
2099 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00002100}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002101
Paul Duffin3a4eb502020-03-19 16:11:18 +00002102func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00002103 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00002104
Paul Duffina551a1c2020-03-17 21:04:24 +00002105 exportedJar := p.JarToExport
2106 if exportedJar != nil {
2107 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
2108 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002109
2110 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00002111 }
2112
2113 testConfig := p.TestConfig
2114 if testConfig != nil {
2115 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
2116 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002117 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
2118 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002119}
2120
Colin Cross1b16b0e2019-02-12 14:41:32 -08002121// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2122// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2123//
2124// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2125// compiled against the device bootclasspath.
2126//
2127// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2128// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002129func TestFactory() android.Module {
2130 module := &Test{}
2131
2132 module.AddProperties(
2133 &module.Module.properties,
2134 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002135 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002136 &module.Module.protoProperties,
2137 &module.testProperties)
2138
Colin Cross9ae1b922018-06-26 17:59:05 -07002139 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002140 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002141
Colin Cross05638fc2018-04-09 18:40:24 -07002142 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002143 return module
2144}
2145
Paul Duffin42df1442019-03-20 12:45:53 +00002146// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2147func TestHelperLibraryFactory() android.Module {
2148 module := &TestHelperLibrary{}
2149
2150 module.AddProperties(
2151 &module.Module.properties,
2152 &module.Module.deviceProperties,
2153 &module.Module.dexpreoptProperties,
2154 &module.Module.protoProperties,
2155 &module.testHelperLibraryProperties)
2156
Colin Cross9a4abed2019-04-24 13:19:28 -07002157 module.Module.properties.Installable = proptools.BoolPtr(true)
2158 module.Module.dexpreopter.isTest = true
2159
Paul Duffin42df1442019-03-20 12:45:53 +00002160 InitJavaModule(module, android.HostAndDeviceSupported)
2161 return module
2162}
2163
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002164// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2165// and makes sure that it is added to the appropriate test suite.
2166//
2167// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2168// compiled against an Android classpath.
2169//
2170// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2171// for host modules.
2172func JavaTestImportFactory() android.Module {
2173 module := &JavaTestImport{}
2174
2175 module.AddProperties(
2176 &module.Import.properties,
2177 &module.prebuiltTestProperties)
2178
2179 module.Import.properties.Installable = proptools.BoolPtr(true)
2180
2181 android.InitPrebuiltModule(module, &module.properties.Jars)
2182 android.InitApexModule(module)
2183 android.InitSdkAwareModule(module)
2184 InitJavaModule(module, android.HostAndDeviceSupported)
2185 return module
2186}
2187
Colin Cross1b16b0e2019-02-12 14:41:32 -08002188// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2189// allow running the test with `atest` or a `TEST_MAPPING` file.
2190//
2191// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2192// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002193func TestHostFactory() android.Module {
2194 module := &Test{}
2195
2196 module.AddProperties(
2197 &module.Module.properties,
2198 &module.Module.protoProperties,
2199 &module.testProperties)
2200
Colin Cross9ae1b922018-06-26 17:59:05 -07002201 module.Module.properties.Installable = proptools.BoolPtr(true)
2202
Colin Cross05638fc2018-04-09 18:40:24 -07002203 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002204 return module
2205}
2206
2207//
Colin Cross2fe66872015-03-30 17:20:39 -07002208// Java Binaries (.jar file plus wrapper script)
2209//
2210
Colin Crossf506d872017-07-19 15:53:04 -07002211type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002212 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002213 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002214
2215 // Name of the class containing main to be inserted into the manifest as Main-Class.
2216 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002217}
2218
Colin Crossf506d872017-07-19 15:53:04 -07002219type Binary struct {
2220 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002221
Colin Crossf506d872017-07-19 15:53:04 -07002222 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002223
Colin Cross6b4a32d2017-12-05 13:42:45 -08002224 isWrapperVariant bool
2225
Colin Crossc3315992017-12-08 19:12:36 -08002226 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002227 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002228}
2229
Alex Light24237172017-10-26 09:46:21 -07002230func (j *Binary) HostToolPath() android.OptionalPath {
2231 return android.OptionalPathForPath(j.binaryFile)
2232}
2233
Colin Crossf506d872017-07-19 15:53:04 -07002234func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002235 if ctx.Arch().ArchType == android.Common {
2236 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002237 if j.binaryProperties.Main_class != nil {
2238 if j.properties.Manifest != nil {
2239 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2240 }
2241 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2242 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2243 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2244 }
2245
Colin Cross6b4a32d2017-12-05 13:42:45 -08002246 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002247 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002248 // Handle the binary wrapper
2249 j.isWrapperVariant = true
2250
Colin Cross366938f2017-12-11 16:29:02 -08002251 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002252 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002253 } else {
2254 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2255 }
2256
2257 // Depend on the installed jar so that the wrapper doesn't get executed by
2258 // another build rule before the jar has been installed.
2259 jarFile := ctx.PrimaryModule().(*Binary).installFile
2260
2261 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2262 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002263 }
Colin Cross2fe66872015-03-30 17:20:39 -07002264}
2265
Colin Crossf506d872017-07-19 15:53:04 -07002266func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002267 if ctx.Arch().ArchType == android.Common {
2268 j.deps(ctx)
2269 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002270}
2271
Colin Cross1b16b0e2019-02-12 14:41:32 -08002272// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2273// as well.
2274//
2275// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2276// compiled against the device bootclasspath.
2277//
2278// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2279// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002280func BinaryFactory() android.Module {
2281 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002282
Colin Cross36242852017-06-23 15:06:31 -07002283 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002284 &module.Module.properties,
2285 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002286 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002287 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002288 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002289
Colin Cross9ae1b922018-06-26 17:59:05 -07002290 module.Module.properties.Installable = proptools.BoolPtr(true)
2291
Colin Cross6b4a32d2017-12-05 13:42:45 -08002292 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2293 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002294 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002295}
2296
Colin Cross1b16b0e2019-02-12 14:41:32 -08002297// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2298//
2299// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2300// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002301func BinaryHostFactory() android.Module {
2302 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002303
Colin Cross36242852017-06-23 15:06:31 -07002304 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002305 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002306 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002307 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002308
Colin Cross9ae1b922018-06-26 17:59:05 -07002309 module.Module.properties.Installable = proptools.BoolPtr(true)
2310
Colin Cross6b4a32d2017-12-05 13:42:45 -08002311 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2312 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002313 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002314}
2315
2316//
2317// Java prebuilts
2318//
2319
Colin Cross74d73e22017-08-02 11:05:49 -07002320type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00002321 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002322
Nan Zhangea568a42017-11-08 21:20:04 -08002323 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002324
2325 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002326
2327 // List of shared java libs that this module has dependencies to
2328 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002329
2330 // List of files to remove from the jar file(s)
2331 Exclude_files []string
2332
2333 // List of directories to remove from the jar file(s)
2334 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002335
2336 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002337 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002338
2339 // set the name of the output
2340 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09002341
2342 Aidl struct {
2343 // directories that should be added as include directories for any aidl sources of modules
2344 // that depend on this module, as well as to aidl for this module.
2345 Export_include_dirs []string
2346 }
Colin Cross74d73e22017-08-02 11:05:49 -07002347}
2348
2349type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002350 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002351 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002352 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002353 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002354 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002355
Colin Cross74d73e22017-08-02 11:05:49 -07002356 properties ImportProperties
2357
Colin Cross0a6e0072017-08-30 14:24:55 -07002358 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002359 exportedSdkLibs []string
Jiyong Park19604de2020-03-24 16:44:11 +09002360 exportAidlIncludeDirs android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -07002361}
2362
Jiyong Park6a927c42020-01-21 02:03:43 +09002363func (j *Import) sdkVersion() sdkSpec {
2364 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002365}
2366
Jiyong Park6a927c42020-01-21 02:03:43 +09002367func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002368 return j.sdkVersion()
2369}
2370
Colin Cross74d73e22017-08-02 11:05:49 -07002371func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002372 return &j.prebuilt
2373}
2374
Colin Cross74d73e22017-08-02 11:05:49 -07002375func (j *Import) PrebuiltSrcs() []string {
2376 return j.properties.Jars
2377}
2378
2379func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002380 return j.prebuilt.Name(j.ModuleBase.Name())
2381}
2382
Jiyong Park0b238752019-10-29 11:23:10 +09002383func (j *Import) Stem() string {
2384 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2385}
2386
Jiyong Park618922e2020-01-08 13:35:43 +09002387func (a *Import) JacocoReportClassesFile() android.Path {
2388 return nil
2389}
2390
Colin Cross74d73e22017-08-02 11:05:49 -07002391func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002392 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002393}
2394
Colin Cross74d73e22017-08-02 11:05:49 -07002395func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002396 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002397
Jiyong Park0b238752019-10-29 11:23:10 +09002398 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002399 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002400 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2401 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002402 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002403 inputFile := outputFile
2404 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2405 TransformJetifier(ctx, outputFile, inputFile)
2406 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002407 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002408
2409 ctx.VisitDirectDeps(func(module android.Module) {
2410 otherName := ctx.OtherModuleName(module)
2411 tag := ctx.OtherModuleDependencyTag(module)
2412
2413 switch dep := module.(type) {
2414 case Dependency:
2415 switch tag {
2416 case libTag, staticLibTag:
2417 // sdk lib names from dependencies are re-exported
2418 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2419 }
2420 case SdkLibraryDependency:
2421 switch tag {
2422 case libTag:
2423 // names of sdk libs that are directly depended are exported
2424 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2425 }
2426 }
2427 })
2428
2429 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002430 if Bool(j.properties.Installable) {
2431 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002432 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002433 }
Jiyong Park19604de2020-03-24 16:44:11 +09002434
2435 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Colin Cross2fe66872015-03-30 17:20:39 -07002436}
2437
Colin Cross74d73e22017-08-02 11:05:49 -07002438var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002439
Nan Zhanged19fc32017-10-19 13:06:22 -07002440func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002441 if j.combinedClasspathFile == nil {
2442 return nil
2443 }
Colin Cross37f6d792018-07-12 12:28:41 -07002444 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002445}
2446
2447func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002448 if j.combinedClasspathFile == nil {
2449 return nil
2450 }
Colin Cross37f6d792018-07-12 12:28:41 -07002451 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002452}
2453
Colin Cross331a1212018-08-15 20:40:52 -07002454func (j *Import) ResourceJars() android.Paths {
2455 return nil
2456}
2457
2458func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002459 if j.combinedClasspathFile == nil {
2460 return nil
2461 }
Colin Cross331a1212018-08-15 20:40:52 -07002462 return android.Paths{j.combinedClasspathFile}
2463}
2464
Colin Crossf24a22a2019-01-31 14:12:44 -08002465func (j *Import) DexJar() android.Path {
2466 return nil
2467}
2468
Colin Cross74d73e22017-08-02 11:05:49 -07002469func (j *Import) AidlIncludeDirs() android.Paths {
Jiyong Park19604de2020-03-24 16:44:11 +09002470 return j.exportAidlIncludeDirs
Colin Crossc0b06f12015-04-08 13:03:43 -07002471}
2472
Jiyong Park1be96912018-05-28 18:02:19 +09002473func (j *Import) ExportedSdkLibs() []string {
2474 return j.exportedSdkLibs
2475}
2476
Artur Satayev9cf46692019-11-26 18:08:34 +00002477func (j *Import) ExportedPlugins() (android.Paths, []string) {
2478 return nil, nil
2479}
2480
Colin Cross0c4ce212019-05-03 15:28:19 -07002481func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2482 return nil, nil
2483}
2484
Jiyong Park0f80c182020-01-31 02:49:53 +09002485func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09002486 // dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Han5e9013b2020-03-10 06:23:13 +09002487 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
2488 return true
2489 }
Jiyong Park0f80c182020-01-31 02:49:53 +09002490 // Also, a dependency to an sdk member is also considered as such. This is required because
2491 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Han5e9013b2020-03-10 06:23:13 +09002492 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
2493 return true
2494 }
2495 return false
Jiyong Park0f80c182020-01-31 02:49:53 +09002496}
2497
albaltai36ff7dc2018-12-25 14:35:23 +08002498// Add compile time check for interface implementation
2499var _ android.IDEInfo = (*Import)(nil)
2500var _ android.IDECustomizedModuleName = (*Import)(nil)
2501
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002502// Collect information for opening IDE project files in java/jdeps.go.
2503const (
2504 removedPrefix = "prebuilt_"
2505)
2506
2507func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2508 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2509}
2510
2511func (j *Import) IDECustomizedModuleName() string {
2512 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2513 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2514 // solution to get the Import name.
2515 name := j.Name()
2516 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002517 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002518 }
2519 return name
2520}
2521
Colin Cross74d73e22017-08-02 11:05:49 -07002522var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002523
Colin Cross1b16b0e2019-02-12 14:41:32 -08002524// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2525//
2526// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2527// compiled against an Android classpath.
2528//
2529// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2530// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002531func ImportFactory() android.Module {
2532 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002533
Colin Cross74d73e22017-08-02 11:05:49 -07002534 module.AddProperties(&module.properties)
2535
2536 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002537 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002538 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002539 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002540 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002541}
2542
Colin Cross1b16b0e2019-02-12 14:41:32 -08002543// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2544// module.
2545//
2546// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2547// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002548func ImportFactoryHost() android.Module {
2549 module := &Import{}
2550
2551 module.AddProperties(&module.properties)
2552
2553 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002554 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002555 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002556 return module
2557}
2558
Colin Cross42be7612019-02-21 18:12:14 -08002559// dex_import module
2560
2561type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002562 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002563
2564 // set the name of the output
2565 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002566}
2567
2568type DexImport struct {
2569 android.ModuleBase
2570 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002571 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002572 prebuilt android.Prebuilt
2573
2574 properties DexImportProperties
2575
2576 dexJarFile android.Path
2577 maybeStrippedDexJarFile android.Path
2578
2579 dexpreopter
2580}
2581
2582func (j *DexImport) Prebuilt() *android.Prebuilt {
2583 return &j.prebuilt
2584}
2585
2586func (j *DexImport) PrebuiltSrcs() []string {
2587 return j.properties.Jars
2588}
2589
2590func (j *DexImport) Name() string {
2591 return j.prebuilt.Name(j.ModuleBase.Name())
2592}
2593
Jiyong Park0b238752019-10-29 11:23:10 +09002594func (j *DexImport) Stem() string {
2595 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2596}
2597
Martin Stjernholm6d415272020-01-31 17:10:36 +00002598func (j *DexImport) IsInstallable() bool {
2599 return true
2600}
2601
Colin Cross42be7612019-02-21 18:12:14 -08002602func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2603 if len(j.properties.Jars) != 1 {
2604 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2605 }
2606
Jiyong Park0b238752019-10-29 11:23:10 +09002607 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002608 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2609
2610 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2611 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2612
2613 if j.dexpreopter.uncompressedDex {
2614 rule := android.NewRuleBuilder()
2615
2616 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2617 rule.Temporary(temporary)
2618
2619 // use zip2zip to uncompress classes*.dex files
2620 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002621 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002622 FlagWithInput("-i ", inputJar).
2623 FlagWithOutput("-o ", temporary).
2624 FlagWithArg("-0 ", "'classes*.dex'")
2625
2626 // use zipalign to align uncompressed classes*.dex files
2627 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002628 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002629 Flag("-f").
2630 Text("4").
2631 Input(temporary).
2632 Output(dexOutputFile)
2633
2634 rule.DeleteTemporaryFiles()
2635
2636 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2637 } else {
2638 ctx.Build(pctx, android.BuildParams{
2639 Rule: android.Cp,
2640 Input: inputJar,
2641 Output: dexOutputFile,
2642 })
2643 }
2644
2645 j.dexJarFile = dexOutputFile
2646
2647 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2648
2649 j.maybeStrippedDexJarFile = dexOutputFile
2650
2651 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2652 ctx.ModuleName()+".jar", dexOutputFile)
2653}
2654
2655func (j *DexImport) DexJar() android.Path {
2656 return j.dexJarFile
2657}
2658
2659// dex_import imports a `.jar` file containing classes.dex files.
2660//
2661// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2662// to the device.
2663func DexImportFactory() android.Module {
2664 module := &DexImport{}
2665
2666 module.AddProperties(&module.properties)
2667
2668 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002669 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002670 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002671 return module
2672}
2673
Colin Cross89536d42017-07-07 14:35:50 -07002674//
2675// Defaults
2676//
2677type Defaults struct {
2678 android.ModuleBase
2679 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002680 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002681}
2682
Colin Cross1b16b0e2019-02-12 14:41:32 -08002683// java_defaults provides a set of properties that can be inherited by other java or android modules.
2684//
2685// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2686// property in the defaults module that exists in the depending module will be prepended to the depending module's
2687// value for that property.
2688//
2689// Example:
2690//
2691// java_defaults {
2692// name: "example_defaults",
2693// srcs: ["common/**/*.java"],
2694// javacflags: ["-Xlint:all"],
2695// aaptflags: ["--auto-add-overlay"],
2696// }
2697//
2698// java_library {
2699// name: "example",
2700// defaults: ["example_defaults"],
2701// srcs: ["example/**/*.java"],
2702// }
2703//
2704// is functionally identical to:
2705//
2706// java_library {
2707// name: "example",
2708// srcs: [
2709// "common/**/*.java",
2710// "example/**/*.java",
2711// ],
2712// javacflags: ["-Xlint:all"],
2713// }
Colin Cross89536d42017-07-07 14:35:50 -07002714func defaultsFactory() android.Module {
2715 return DefaultsFactory()
2716}
2717
Paul Duffin47357662019-12-05 14:07:14 +00002718func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002719 module := &Defaults{}
2720
Colin Cross89536d42017-07-07 14:35:50 -07002721 module.AddProperties(
2722 &CompilerProperties{},
2723 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002724 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002725 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002726 &aaptProperties{},
2727 &androidLibraryProperties{},
2728 &appProperties{},
2729 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002730 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002731 &ImportProperties{},
2732 &AARImportProperties{},
2733 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002734 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002735 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002736 )
2737
2738 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002739 return module
2740}
Nan Zhangea568a42017-11-08 21:20:04 -08002741
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002742func kytheExtractJavaFactory() android.Singleton {
2743 return &kytheExtractJavaSingleton{}
2744}
2745
2746type kytheExtractJavaSingleton struct {
2747}
2748
2749func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2750 var xrefTargets android.Paths
2751 ctx.VisitAllModules(func(module android.Module) {
2752 if javaModule, ok := module.(xref); ok {
2753 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2754 }
2755 })
2756 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2757 if len(xrefTargets) > 0 {
2758 ctx.Build(pctx, android.BuildParams{
2759 Rule: blueprint.Phony,
2760 Output: android.PathForPhony(ctx, "xref_java"),
2761 Inputs: xrefTargets,
2762 })
2763 }
2764}
2765
Nan Zhangea568a42017-11-08 21:20:04 -08002766var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002767var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002768var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002769var inList = android.InList