blob: 2e53654cc0dc8a255dc4d3f4ecd9b3033451b8fb [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"
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000026 "sync"
Colin Cross2fe66872015-03-30 17:20:39 -070027
28 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070029 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070030 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070031
Colin Cross635c3b02016-05-18 15:37:25 -070032 "android/soong/android"
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000033 "android/soong/dexpreopt"
Colin Cross3e3e72d2017-06-22 17:20:19 -070034 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070035 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070036)
37
Colin Cross463a90e2015-06-17 14:20:06 -070038func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000039 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000040
41 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000042 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000043
44 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
45 librarySdkMemberType{
46 android.SdkMemberTypeBase{
47 PropertyName: "java_libs",
48 },
49 },
50 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000051
52 android.RegisterSdkMemberType(&testSdkMemberType{
53 SdkMemberTypeBase: android.SdkMemberTypeBase{
54 PropertyName: "java_tests",
55 },
56 })
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000057
58 android.PostDepsMutators(RegisterPostDepsMutators)
Colin Cross463a90e2015-06-17 14:20:06 -070059}
60
Paul Duffinf9b1da02019-12-18 19:51:55 +000061func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
62 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
63
64 ctx.RegisterModuleType("java_library", LibraryFactory)
65 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
66 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
67 ctx.RegisterModuleType("java_binary", BinaryFactory)
68 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
69 ctx.RegisterModuleType("java_test", TestFactory)
70 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
71 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000072 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000073 ctx.RegisterModuleType("java_import", ImportFactory)
74 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
75 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
76 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
77 ctx.RegisterModuleType("dex_import", DexImportFactory)
78
Martin Stjernholmd90676f2020-01-11 00:37:30 +000079 ctx.FinalDepsMutators(dexpreopt.RegisterToolDepsMutator)
80
Paul Duffinf9b1da02019-12-18 19:51:55 +000081 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
82 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
83}
84
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000085func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
86 ctx.BottomUp("ordered_system_server_jars", systemServerJarsDepsMutator)
87}
88
89var (
90 dexpreoptedSystemServerJarsKey = android.NewOnceKey("dexpreoptedSystemServerJars")
91 dexpreoptedSystemServerJarsLock sync.Mutex
92)
93
94func DexpreoptedSystemServerJars(config android.Config) *[]string {
95 return config.Once(dexpreoptedSystemServerJarsKey, func() interface{} {
96 return &[]string{}
97 }).(*[]string)
98}
99
100// A PostDepsMutator pass that enforces total order on non-updatable system server jars. A total
101// order is neededed because such jars must be dexpreopted together (each jar on the list must have
102// all preceding jars in its class loader context). The total order must be compatible with the
103// partial order imposed by genuine dependencies between system server jars (which is not always
104// respected by the PRODUCT_SYSTEM_SERVER_JARS variable).
105//
106// An earlier mutator pass creates genuine dependencies, and this pass traverses the jars in that
107// order (which is partial and non-deterministic). This pass adds additional dependencies between
108// jars, making the order total and deterministic. It also constructs a global ordered list.
109func systemServerJarsDepsMutator(ctx android.BottomUpMutatorContext) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000110 jars := dexpreopt.NonUpdatableSystemServerJars(ctx, dexpreopt.GetGlobalConfig(ctx))
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000111 name := ctx.ModuleName()
112 if android.InList(name, jars) {
113 dexpreoptedSystemServerJarsLock.Lock()
114 defer dexpreoptedSystemServerJarsLock.Unlock()
115 jars := DexpreoptedSystemServerJars(ctx.Config())
116 for _, dep := range *jars {
117 ctx.AddDependency(ctx.Module(), dexpreopt.SystemServerDepTag, dep)
118 }
119 *jars = append(*jars, name)
120 }
121}
122
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900123func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
124 if j.SocSpecific() || j.DeviceSpecific() ||
125 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
126 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +0900127 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900128 ctx.PropertyErrorf("sdk_version",
129 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
130 }
131 }
132 }
133}
134
Jeongik Cha538c0d02019-07-11 15:54:27 +0900135func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
136 if sc, ok := ctx.Module().(sdkContext); ok {
137 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +0900138 sdkVersionSpecified := sc.sdkVersion().specified()
139 if usePlatformAPI && sdkVersionSpecified {
140 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
141 } else if !usePlatformAPI && !sdkVersionSpecified {
142 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143 }
144
145 }
146}
147
Colin Cross2fe66872015-03-30 17:20:39 -0700148// TODO:
149// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700150// Renderscript
151// Post-jar passes:
152// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700153// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700154// DroidDoc
155// Findbugs
156
Colin Cross89536d42017-07-07 14:35:50 -0700157type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700158 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
159 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800160 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700161
162 // list of source files that should not be used to build the Java module.
163 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800164 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700165
166 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700167 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700168
Colin Cross86a63ff2017-09-27 17:33:10 -0700169 // list of directories that should be excluded from java_resource_dirs
170 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700171
Colin Cross0f37af02017-09-27 17:42:05 -0700172 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800173 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700174
Colin Crosscedd4762018-09-13 11:26:19 -0700175 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800176 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700177
Colin Cross7d5136f2015-05-11 13:39:40 -0700178 // list of module-specific flags that will be used for javac compiles
179 Javacflags []string `android:"arch_variant"`
180
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200181 // list of module-specific flags that will be used for kotlinc compiles
182 Kotlincflags []string `android:"arch_variant"`
183
Colin Cross7d5136f2015-05-11 13:39:40 -0700184 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700185 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700186
187 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700188 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700189
190 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800191 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700192
Colin Cross540eff82017-06-22 17:01:52 -0700193 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800194 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700195
196 // If not blank, set the java version passed to javac as -source and -target
197 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700198
Colin Cross9ae1b922018-06-26 17:59:05 -0700199 // If set to true, allow this module to be dexed and installed on devices. Has no
200 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700201 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700202
Colin Cross0f37af02017-09-27 17:42:05 -0700203 // If set to true, include sources used to compile the module in to the final jar
204 Include_srcs *bool
205
Vladimir Marko0975ee02019-04-02 10:29:55 +0100206 // If not empty, classes are restricted to the specified packages and their sub-packages.
207 // This restriction is checked after applying jarjar rules and including static libs.
208 Permitted_packages []string
209
Colin Crossbe9cdb82019-01-21 21:37:16 -0800210 // List of modules to use as annotation processors
211 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700212
Artur Satayev9cf46692019-11-26 18:08:34 +0000213 // List of modules to export to libraries that directly depend on this library as annotation processors
214 Exported_plugins []string
215
Nan Zhang61eaedb2017-11-02 13:28:15 -0700216 // The number of Java source entries each Javac instance can process
217 Javac_shard_size *int64
218
Nan Zhang5f8cb422018-02-06 10:34:32 -0800219 // Add host jdk tools.jar to bootclasspath
220 Use_tools_jar *bool
221
Colin Cross1369cdb2017-09-29 17:58:17 -0700222 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700223 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800224 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700225
Colin Cross6cef4812019-10-17 14:23:50 -0700226 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700227 Javacflags []string
228 }
Colin Crosscb933592017-11-22 13:49:43 -0800229
Colin Cross81440082018-08-15 20:21:55 -0700230 // When compiling language level 9+ .java code in packages that are part of
231 // a system module, patch_module names the module that your sources and
232 // dependencies should be patched into. The Android runtime currently
233 // doesn't implement the JEP 261 module system so this option is only
234 // supported at compile time. It should only be needed to compile tests in
235 // packages that exist in libcore and which are inconvenient to move
236 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100237 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700238
Colin Crosscb933592017-11-22 13:49:43 -0800239 Jacoco struct {
240 // List of classes to include for instrumentation with jacoco to collect coverage
241 // information at runtime when building with coverage enabled. If unset defaults to all
242 // classes.
243 // Supports '*' as the last character of an entry in the list as a wildcard match.
244 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
245 // it matches classes in the package that have the class name as a prefix.
246 Include_filter []string
247
248 // List of classes to exclude from instrumentation with jacoco to collect coverage
249 // information at runtime when building with coverage enabled. Overrides classes selected
250 // by the include_filter property.
251 // Supports '*' as the last character of an entry in the list as a wildcard match.
252 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
253 // it matches classes in the package that have the class name as a prefix.
254 Exclude_filter []string
255 }
256
Andreas Gampef3e5b552018-01-22 21:27:21 -0800257 Errorprone struct {
258 // List of javac flags that should only be used when running errorprone.
259 Javacflags []string
260 }
261
Colin Cross0f2ee152017-12-14 15:22:43 -0800262 Proto struct {
263 // List of extra options that will be passed to the proto generator.
264 Output_params []string
265 }
266
Colin Crosscb933592017-11-22 13:49:43 -0800267 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800268
269 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800270 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700271}
272
Colin Cross89536d42017-07-07 14:35:50 -0700273type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700274 // list of module-specific flags that will be used for dex compiles
275 Dxflags []string `android:"arch_variant"`
276
Jeongik Cha538c0d02019-07-11 15:54:27 +0900277 // if not blank, set to the version of the sdk to compile against.
278 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800279 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700280
Colin Cross83bb3162018-06-25 15:48:06 -0700281 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
282 // Defaults to sdk_version if not set.
283 Min_sdk_version *string
284
Dan Willemsen419290a2018-10-31 15:28:47 -0700285 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
286 // Defaults to sdk_version if not set.
287 Target_sdk_version *string
288
Jeongik Cha356dac42019-08-19 14:09:52 +0900289 // Whether to compile against the platform APIs instead of an SDK.
290 // If true, then sdk_version must be empty. The value of this field
291 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700292 Platform_apis *bool
293
Colin Crossebe1a512017-11-14 13:12:14 -0800294 Aidl struct {
295 // Top level directories to pass to aidl tool
296 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700297
Colin Crossebe1a512017-11-14 13:12:14 -0800298 // Directories rooted at the Android.bp file to pass to aidl tool
299 Local_include_dirs []string
300
301 // directories that should be added as include directories for any aidl sources of modules
302 // that depend on this module, as well as to aidl for this module.
303 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100304
305 // whether to generate traces (for systrace) for this interface
306 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100307
308 // whether to generate Binder#GetTransaction name method.
309 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800310 }
Colin Cross92430102017-10-09 14:59:32 -0700311
312 // If true, export a copy of the module as a -hostdex module for host testing.
313 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700314
Colin Cross7f87f4f2019-04-24 13:41:45 -0700315 Target struct {
316 Hostdex struct {
317 // Additional required dependencies to add to -hostdex modules.
318 Required []string
319 }
320 }
321
David Brazdil17ef5632018-06-27 10:27:45 +0100322 // If set to true, compile dex regardless of installable. Defaults to false.
323 Compile_dex *bool
324
Colin Cross66dbc0b2017-12-28 12:23:20 -0800325 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700326 // If false, disable all optimization. Defaults to true for android_app and android_test
327 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800328 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700329 // True if the module containing this has it set by default.
330 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800331
332 // If true, optimize for size by removing unused code. Defaults to true for apps,
333 // false for libraries and tests.
334 Shrink *bool
335
336 // If true, optimize bytecode. Defaults to false.
337 Optimize *bool
338
339 // If true, obfuscate bytecode. Defaults to false.
340 Obfuscate *bool
341
342 // If true, do not use the flag files generated by aapt that automatically keep
343 // classes referenced by the app manifest. Defaults to false.
344 No_aapt_flags *bool
345
346 // Flags to pass to proguard.
347 Proguard_flags []string
348
349 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800350 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800351 }
352
Paul Duffine25c6442019-10-11 13:50:28 +0100353 // When targeting 1.9 and above, override the modules to use with --system,
354 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700355 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700356
Jiyong Park4c4c0242019-10-21 14:53:15 +0900357 // set the name of the output
358 Stem *string
359
Colin Cross5a0dcd52018-10-05 14:20:06 -0700360 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800361 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700362}
363
Sasha Smundak2057f822019-04-16 17:16:58 -0700364func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
365 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
366}
367
Colin Cross46c9b8b2017-06-22 16:51:17 -0700368// Module contains the properties and members used by all java module types
369type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700370 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700371 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900372 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900373 android.SdkBase
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000374 dexpreopt.DexPreoptModule
Colin Cross2fe66872015-03-30 17:20:39 -0700375
Colin Cross89536d42017-07-07 14:35:50 -0700376 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700377 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700378 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700379
Colin Cross331a1212018-08-15 20:40:52 -0700380 // jar file containing header classes including static library dependencies, suitable for
381 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700382 headerJarFile android.Path
383
Colin Cross331a1212018-08-15 20:40:52 -0700384 // jar file containing implementation classes including static library dependencies but no
385 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700386 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700387
Colin Cross331a1212018-08-15 20:40:52 -0700388 // jar file containing only resources including from static library dependencies
389 resourceJar android.Path
390
Colin Cross0c4ce212019-05-03 15:28:19 -0700391 // args and dependencies to package source files into a srcjar
392 srcJarArgs []string
393 srcJarDeps android.Paths
394
Colin Cross331a1212018-08-15 20:40:52 -0700395 // jar file containing implementation classes and resources including static library
396 // dependencies
397 implementationAndResourcesJar android.Path
398
399 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700400 dexJarFile android.Path
401
Colin Cross43f08db2018-11-12 10:13:39 -0800402 // output file that contains classes.dex if it should be in the output file
403 maybeStrippedDexJarFile android.Path
404
Colin Crosscb933592017-11-22 13:49:43 -0800405 // output file containing uninstrumented classes that will be instrumented by jacoco
406 jacocoReportClassesFile android.Path
407
Colin Cross66dbc0b2017-12-28 12:23:20 -0800408 // output file containing mapping of obfuscated names
409 proguardDictionary android.Path
410
Colin Cross331a1212018-08-15 20:40:52 -0700411 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700412 outputFile android.Path
413 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700414
Colin Cross635c3b02016-05-18 15:37:25 -0700415 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700416
Colin Cross635c3b02016-05-18 15:37:25 -0700417 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700418
Colin Cross2fe66872015-03-30 17:20:39 -0700419 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700420 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800421
422 // list of .java files and srcjars that was passed to javac
423 compiledJavaSrcs android.Paths
424 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800425
426 // list of extra progurad flag files
427 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900428
Colin Cross094054a2018-10-17 15:10:48 -0700429 // manifest file to use instead of properties.Manifest
430 overrideManifest android.OptionalPath
431
Artur Satayev9cf46692019-11-26 18:08:34 +0000432 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900433 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700434
Artur Satayev9cf46692019-11-26 18:08:34 +0000435 // list of plugins that this java module is exporting
436 exportedPluginJars android.Paths
437
438 // list of plugins that this java module is exporting
439 exportedPluginClasses []string
440
441 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800442 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700443 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800444
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800445 // expanded Jarjar_rules
446 expandJarjarRules android.Path
447
Vladimir Marko0975ee02019-04-02 10:29:55 +0100448 // list of additional targets for checkbuild
449 additionalCheckedModules android.Paths
450
Colin Cross988708c2019-05-06 14:04:11 -0700451 // Extra files generated by the module type to be added as java resources.
452 extraResources android.Paths
453
Colin Crossf24a22a2019-01-31 14:12:44 -0800454 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800455 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800456
457 // list of the xref extraction files
458 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700459}
460
Colin Cross41955e82019-05-29 14:40:35 -0700461func (j *Module) OutputFiles(tag string) (android.Paths, error) {
462 switch tag {
463 case "":
464 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700465 case ".jar":
466 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700467 case ".proguard_map":
468 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700469 default:
470 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
471 }
Colin Cross54250902017-12-05 09:28:08 -0800472}
473
Colin Cross41955e82019-05-29 14:40:35 -0700474var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800475
Colin Crossf506d872017-07-19 15:53:04 -0700476type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700477 HeaderJars() android.Paths
478 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700479 ResourceJars() android.Paths
480 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800481 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700482 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900483 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000484 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700485 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700486 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900487 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700488}
489
Jiyong Parkc678ad32018-04-10 13:07:10 +0900490type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900491 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
492 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900493}
494
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800495type xref interface {
496 XrefJavaFiles() android.Paths
497}
498
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800499func (j *Module) XrefJavaFiles() android.Paths {
500 return j.kytheFiles
501}
502
Colin Cross89536d42017-07-07 14:35:50 -0700503func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
504 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
505 android.InitDefaultableModule(module)
506}
507
Colin Crossbe1da472017-07-07 15:59:46 -0700508type dependencyTag struct {
509 blueprint.BaseDependencyTag
510 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700511}
512
Colin Crossa4f08812018-10-02 22:03:40 -0700513type jniDependencyTag struct {
514 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700515}
516
Jiyong Park8be103b2019-11-08 15:53:48 +0900517func IsJniDepTag(depTag blueprint.DependencyTag) bool {
518 _, ok := depTag.(*jniDependencyTag)
519 return ok
520}
521
Colin Crossbe1da472017-07-07 15:59:46 -0700522var (
Colin Cross4b964c02018-10-15 16:18:06 -0700523 staticLibTag = dependencyTag{name: "staticlib"}
524 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700525 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800526 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000527 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700528 bootClasspathTag = dependencyTag{name: "bootclasspath"}
529 systemModulesTag = dependencyTag{name: "system modules"}
530 frameworkResTag = dependencyTag{name: "framework-res"}
531 frameworkApkTag = dependencyTag{name: "framework-apk"}
532 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800533 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700534 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
535 certificateTag = dependencyTag{name: "certificate"}
536 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700537 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700538)
Colin Cross2fe66872015-03-30 17:20:39 -0700539
Jiyong Park83dc74b2020-01-14 18:38:44 +0900540func IsLibDepTag(depTag blueprint.DependencyTag) bool {
541 return depTag == libTag
542}
543
544func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
545 return depTag == staticLibTag
546}
547
Colin Crossfc3674a2017-09-18 17:41:52 -0700548type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700549 useModule, useFiles, useDefaultLibs, invalidVersion bool
550
Colin Cross6cef4812019-10-17 14:23:50 -0700551 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
552 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100553
554 // The default system modules to use. Will be an empty string if no system
555 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700556 systemModules string
557
Colin Cross6cef4812019-10-17 14:23:50 -0700558 // The modules that will be added ot the classpath when targeting 1.9 or higher
559 java9Classpath []string
560
Colin Crossa97c5d32018-03-28 14:58:31 -0700561 frameworkResModule string
562
Colin Cross86a60ae2018-05-29 14:44:55 -0700563 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700564 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100565
566 noStandardLibs, noFrameworksLibs bool
567}
568
569func (s sdkDep) hasStandardLibs() bool {
570 return !s.noStandardLibs
571}
572
573func (s sdkDep) hasFrameworkLibs() bool {
574 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700575}
576
Colin Crossa4f08812018-10-02 22:03:40 -0700577type jniLib struct {
578 name string
579 path android.Path
580 target android.Target
581}
582
Colin Cross0ea8ba82019-06-06 14:33:29 -0700583func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800584 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
585}
586
Colin Cross0ea8ba82019-06-06 14:33:29 -0700587func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800588 return j.shouldInstrument(ctx) &&
589 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
590 ctx.Config().UnbundledBuild())
591}
592
Jiyong Park6a927c42020-01-21 02:03:43 +0900593func (j *Module) sdkVersion() sdkSpec {
594 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700595}
596
Paul Duffine25c6442019-10-11 13:50:28 +0100597func (j *Module) systemModules() string {
598 return proptools.String(j.deviceProperties.System_modules)
599}
600
Jiyong Park6a927c42020-01-21 02:03:43 +0900601func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700602 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900603 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700604 }
605 return j.sdkVersion()
606}
607
Jiyong Park6a927c42020-01-21 02:03:43 +0900608func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700609 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900610 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700611 }
612 return j.sdkVersion()
613}
614
Jiyong Parkb02bb402019-12-03 00:43:57 +0900615func (j *Module) AvailableFor(what string) bool {
616 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
617 // Exception: for hostdex: true libraries, the platform variant is created
618 // even if it's not marked as available to platform. In that case, the platform
619 // variant is used only for the hostdex and not installed to the device.
620 return true
621 }
622 return j.ApexModuleBase.AvailableFor(what)
623}
624
Colin Crossbe1da472017-07-07 15:59:46 -0700625func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700626 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100627 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700628 if sdkDep.useDefaultLibs {
629 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
630 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
631 if sdkDep.hasFrameworkLibs() {
632 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700633 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700634 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700635 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100636 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700637 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700638 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
639 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
640 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
641 }
Colin Cross2fe66872015-03-30 17:20:39 -0700642 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700643
Nan Zhangb2b33de2018-02-23 11:18:47 -0800644 if ctx.ModuleName() == "android_stubs_current" ||
645 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700646 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700647 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800648 }
Colin Cross2fe66872015-03-30 17:20:39 -0700649 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700650
Inseob Kimac1e9862019-12-09 18:15:47 +0900651 syspropPublicStubs := syspropPublicStubs(ctx.Config())
652
653 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
654 // and redirects dependency to public stub depending on the link type.
655 rewriteSyspropLibs := func(libs []string, prop string) []string {
656 // make a copy
657 ret := android.CopyOf(libs)
658
659 for idx, lib := range libs {
660 stub, ok := syspropPublicStubs[lib]
661
662 if !ok {
663 continue
664 }
665
666 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900667 // only platform modules can use internal props
668 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900669 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900670 }
671 }
672
673 return ret
674 }
675
676 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
677 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700678
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700679 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000680 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800681
Colin Crossfe17f6f2019-03-28 19:30:56 -0700682 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700683 if j.hasSrcExt(".proto") {
684 protoDeps(ctx, &j.protoProperties)
685 }
Colin Cross93e85952017-08-15 13:34:18 -0700686
687 if j.hasSrcExt(".kt") {
688 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
689 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700690 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
691 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800692 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800693 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
694 }
Colin Cross93e85952017-08-15 13:34:18 -0700695 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800696
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000697 // Framework libraries need special handling in static coverage builds: they should not have
698 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
699 // the same jacoco classes coming from different bootclasspath jars.
700 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
701 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
702 j.properties.Instrument = true
703 }
704 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700705 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800706 }
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000707
708 // services depend on com.android.location.provider, but dependency in not registered in a Blueprint file
709 if ctx.ModuleName() == "services" {
710 ctx.AddDependency(ctx.Module(), dexpreopt.SystemServerForcedDepTag, "com.android.location.provider")
711 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700712}
713
714func hasSrcExt(srcs []string, ext string) bool {
715 for _, src := range srcs {
716 if filepath.Ext(src) == ext {
717 return true
718 }
719 }
720
721 return false
722}
723
724func (j *Module) hasSrcExt(ext string) bool {
725 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700726}
727
Colin Cross46c9b8b2017-06-22 16:51:17 -0700728func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700729 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700730
Colin Crossebe1a512017-11-14 13:12:14 -0800731 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
732 aidlIncludes = append(aidlIncludes,
733 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
734 aidlIncludes = append(aidlIncludes,
735 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700736
Colin Cross3047fa22019-04-18 10:56:44 -0700737 var flags []string
738 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700739
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700740 if aidlPreprocess.Valid() {
741 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700742 deps = append(deps, aidlPreprocess.Path())
743 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700744 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700745 }
746
Colin Cross3047fa22019-04-18 10:56:44 -0700747 if len(j.exportAidlIncludeDirs) > 0 {
748 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
749 }
750
751 if len(aidlIncludes) > 0 {
752 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
753 }
754
Colin Cross635c3b02016-05-18 15:37:25 -0700755 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800756 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700757 flags = append(flags, "-I"+src.String())
758 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700759
Martijn Coeneneab15642018-03-09 09:29:59 +0100760 if Bool(j.deviceProperties.Aidl.Generate_traces) {
761 flags = append(flags, "-t")
762 }
763
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100764 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
765 flags = append(flags, "--transaction_names")
766 }
767
Colin Cross3047fa22019-04-18 10:56:44 -0700768 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700769}
770
Colin Cross32f676a2017-09-06 13:41:06 -0700771type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800772 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700773 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800774 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700775 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800776 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700777 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700778 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700779 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700780 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800781 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700782 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700783 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700784 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700785 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800786 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800787
788 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700789}
Colin Cross2fe66872015-03-30 17:20:39 -0700790
Colin Cross54250902017-12-05 09:28:08 -0800791func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
792 for _, f := range dep.Srcs() {
793 if f.Ext() != ".jar" {
794 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
795 ctx.OtherModuleName(dep.(blueprint.Module)))
796 }
797 }
798}
799
Jiyong Park2d492942018-03-05 17:44:10 +0900800type linkType int
801
802const (
Jiyong Park50146e92020-01-30 18:00:15 +0900803 // TODO(jiyong) rename these for better readability. Make the allowed
804 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900805 javaCore linkType = iota
806 javaSdk
807 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900808 javaModule
Jiyong Park2d492942018-03-05 17:44:10 +0900809 javaPlatform
810)
811
Jeongik Cha75b83b02019-11-01 15:28:00 +0900812type linkTypeContext interface {
813 android.Module
814 getLinkType(name string) (ret linkType, stubs bool)
815}
816
817func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700818 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700819 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900820 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
821 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100822 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900823 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900824 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900825 return javaCore, false
826 case name == "android_system_stubs_current":
827 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900828 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900829 return javaSystem, false
830 case name == "android_test_stubs_current":
831 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900832 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900833 return javaPlatform, false
834 case name == "android_stubs_current":
835 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900836 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900837 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900838 case name == "android_module_lib_stubs_current":
839 return javaModule, true
840 case ver.kind == sdkModule:
841 return javaModule, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900842 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900843 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900844 case !ver.valid():
845 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700846 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900847 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900848 }
849}
850
Jeongik Cha75b83b02019-11-01 15:28:00 +0900851func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700852 if ctx.Host() {
853 return
854 }
855
Jeongik Cha75b83b02019-11-01 15:28:00 +0900856 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900857 if stubs {
858 return
859 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900860 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900861 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."
862
863 switch myLinkType {
864 case javaCore:
865 if otherLinkType != javaCore {
866 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 +0900867 ctx.OtherModuleName(to))
868 }
Jiyong Park2d492942018-03-05 17:44:10 +0900869 break
870 case javaSdk:
871 if otherLinkType != javaCore && otherLinkType != javaSdk {
872 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
873 ctx.OtherModuleName(to))
874 }
875 break
876 case javaSystem:
Jiyong Park50146e92020-01-30 18:00:15 +0900877 if otherLinkType == javaPlatform || otherLinkType == javaModule {
Jiyong Park2d492942018-03-05 17:44:10 +0900878 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
879 ctx.OtherModuleName(to))
880 }
881 break
Jiyong Park50146e92020-01-30 18:00:15 +0900882 case javaModule:
883 if otherLinkType == javaPlatform {
884 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
885 ctx.OtherModuleName(to))
886 }
887 break
Jiyong Park2d492942018-03-05 17:44:10 +0900888 case javaPlatform:
889 // no restriction on link-type
890 break
Jiyong Park750e5572018-01-31 00:20:13 +0900891 }
892}
893
Colin Cross32f676a2017-09-06 13:41:06 -0700894func (j *Module) collectDeps(ctx android.ModuleContext) deps {
895 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700896
Colin Cross300f0382018-03-06 13:11:51 -0800897 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700898 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800899 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700900 ctx.AddMissingDependencies(sdkDep.bootclasspath)
901 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800902 } else if sdkDep.useFiles {
903 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700904 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700905 deps.aidlPreprocess = sdkDep.aidl
906 } else {
907 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800908 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700909 }
910
Colin Crossd11fcda2017-10-23 17:59:01 -0700911 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700912 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700913 tag := ctx.OtherModuleDependencyTag(module)
914
Colin Crossa4f08812018-10-02 22:03:40 -0700915 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700916 // Handled by AndroidApp.collectAppDeps
917 return
918 }
919 if tag == certificateTag {
920 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700921 return
922 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900923 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900924 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900925 if to, ok := module.(linkTypeContext); ok {
926 switch tag {
927 case bootClasspathTag, libTag, staticLibTag:
928 checkLinkType(ctx, j, to, tag.(dependencyTag))
929 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700930 }
Jiyong Park750e5572018-01-31 00:20:13 +0900931 }
Colin Cross54250902017-12-05 09:28:08 -0800932 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800933 case SdkLibraryDependency:
934 switch tag {
935 case libTag:
936 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
937 // names of sdk libs that are directly depended are exported
938 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700939 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800940 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
941 }
Colin Cross54250902017-12-05 09:28:08 -0800942 case Dependency:
943 switch tag {
944 case bootClasspathTag:
945 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700946 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800947 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900948 // sdk lib names from dependencies are re-exported
949 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700950 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000951 pluginJars, pluginClasses := dep.ExportedPlugins()
952 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700953 case java9LibTag:
954 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800955 case staticLibTag:
956 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
957 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
958 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700959 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900960 // sdk lib names from dependencies are re-exported
961 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700962 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000963 pluginJars, pluginClasses := dep.ExportedPlugins()
964 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800965 case pluginTag:
966 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800967 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000968 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
969 } else {
970 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800971 }
972 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
973 } else {
974 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
975 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000976 case exportedPluginTag:
977 if plugin, ok := dep.(*Plugin); ok {
978 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
979 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
980 }
981 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
982 if plugin.pluginProperties.Processor_class != nil {
983 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
984 }
985 } else {
986 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
987 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800988 case frameworkApkTag:
989 if ctx.ModuleName() == "android_stubs_current" ||
990 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700991 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800992 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
993 // resource files out of there for aapt.
994 //
995 // Normally the package rule runs aapt, which includes the resource,
996 // but we're not running that in our package rule so just copy in the
997 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700998 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800999 }
Colin Cross54250902017-12-05 09:28:08 -08001000 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -07001001 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -08001002 case kotlinAnnotationsTag:
1003 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -08001004 }
1005
Colin Cross54250902017-12-05 09:28:08 -08001006 case android.SourceFileProducer:
1007 switch tag {
1008 case libTag:
1009 checkProducesJars(ctx, dep)
1010 deps.classpath = append(deps.classpath, dep.Srcs()...)
1011 case staticLibTag:
1012 checkProducesJars(ctx, dep)
1013 deps.classpath = append(deps.classpath, dep.Srcs()...)
1014 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
1015 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -08001016 }
1017 default:
Colin Crossec7a0422017-07-07 14:47:12 -07001018 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001019 case bootClasspathTag:
1020 // If a system modules dependency has been added to the bootclasspath
1021 // then add its libs to the bootclasspath.
1022 sm := module.(*SystemModules)
1023 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
1024
Colin Cross1369cdb2017-09-29 17:58:17 -07001025 case systemModulesTag:
1026 if deps.systemModules != nil {
1027 panic("Found two system module dependencies")
1028 }
1029 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +00001030 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -07001031 panic("Missing directory for system module dependency")
1032 }
Colin Crossb77043e2019-07-16 13:57:13 -07001033 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001034 }
Colin Crossec7a0422017-07-07 14:47:12 -07001035 }
Colin Cross2fe66872015-03-30 17:20:39 -07001036 })
1037
Jiyong Park1be96912018-05-28 18:02:19 +09001038 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1039
Colin Cross32f676a2017-09-06 13:41:06 -07001040 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001041}
1042
Artur Satayev9cf46692019-11-26 18:08:34 +00001043func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1044 deps.processorPath = append(deps.processorPath, pluginJars...)
1045 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1046}
1047
Colin Cross1e743852019-10-28 11:37:20 -07001048func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001049 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001050 if err != nil {
1051 ctx.PropertyErrorf("sdk_version", "%s", err)
1052 }
Nan Zhang357466b2018-04-17 17:38:36 -07001053 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001054 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001055 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001056 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001057 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001058 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001059 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1060 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001061 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001062 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001063 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001064 }
Nan Zhang357466b2018-04-17 17:38:36 -07001065}
1066
Colin Cross1e743852019-10-28 11:37:20 -07001067type javaVersion int
1068
1069const (
1070 JAVA_VERSION_UNSUPPORTED = 0
1071 JAVA_VERSION_6 = 6
1072 JAVA_VERSION_7 = 7
1073 JAVA_VERSION_8 = 8
1074 JAVA_VERSION_9 = 9
1075)
1076
1077func (v javaVersion) String() string {
1078 switch v {
1079 case JAVA_VERSION_6:
1080 return "1.6"
1081 case JAVA_VERSION_7:
1082 return "1.7"
1083 case JAVA_VERSION_8:
1084 return "1.8"
1085 case JAVA_VERSION_9:
1086 return "1.9"
1087 default:
1088 return "unsupported"
1089 }
1090}
1091
1092// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1093func (v javaVersion) usesJavaModules() bool {
1094 return v >= 9
1095}
1096
1097func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001098 switch javaVersion {
1099 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001100 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001101 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001102 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001103 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001104 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001105 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001106 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001107 case "10", "11":
1108 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001109 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001110 default:
1111 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001112 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001113 }
1114}
1115
Nan Zhanged19fc32017-10-19 13:06:22 -07001116func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001117
Colin Crossf03c82b2015-04-13 13:53:40 -07001118 var flags javaBuilderFlags
1119
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001120 // javaVersion flag.
1121 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1122
Nan Zhanged19fc32017-10-19 13:06:22 -07001123 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001124 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001125 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001126 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001127 }
Colin Cross6510f912017-11-29 00:27:14 -08001128 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001129 // Override the -g flag passed globally to remove local variable debug info to reduce
1130 // disk and memory usage.
1131 javacFlags = append(javacFlags, "-g:source,lines")
1132 }
Colin Crossc228a702019-11-06 16:18:05 -08001133 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001134
Colin Cross66548102018-06-19 22:47:35 -07001135 if ctx.Config().RunErrorProne() {
1136 if config.ErrorProneClasspath == nil {
1137 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1138 }
1139
1140 errorProneFlags := []string{
1141 "-Xplugin:ErrorProne",
1142 "${config.ErrorProneChecks}",
1143 }
1144 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1145
1146 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1147 "'" + strings.Join(errorProneFlags, " ") + "'"
1148 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001149 }
1150
Nan Zhanged19fc32017-10-19 13:06:22 -07001151 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001152 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1153 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001154 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001155 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001156
Colin Crossbe9cdb82019-01-21 21:37:16 -08001157 flags.processor = strings.Join(deps.processorClasses, ",")
1158
Colin Cross1e743852019-10-28 11:37:20 -07001159 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1160 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001161 // Give host-side tools a version of OpenJDK's standard libraries
1162 // close to what they're targeting. As of Dec 2017, AOSP is only
1163 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1164 //
1165 // When building with OpenJDK 8, the following should have no
1166 // effect since those jars would be available by default.
1167 //
1168 // When building with OpenJDK 9 but targeting a version < 1.8,
1169 // putting them on the bootclasspath means that:
1170 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1171 // b) references to existing APIs are not reinterpreted in an
1172 // OpenJDK 9-specific way, eg. calls to subclasses of
1173 // java.nio.Buffer as in http://b/70862583
1174 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1175 flags.bootClasspath = append(flags.bootClasspath,
1176 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1177 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001178 if Bool(j.properties.Use_tools_jar) {
1179 flags.bootClasspath = append(flags.bootClasspath,
1180 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1181 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001182 }
1183
Colin Cross1e743852019-10-28 11:37:20 -07001184 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001185 // Manually specify build directory in case it is not under the repo root.
1186 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1187 // just adding a symlink under the root doesn't help.)
1188 patchPaths := ".:" + ctx.Config().BuildDir()
1189 classPath := flags.classpath.FormJavaClassPath("")
1190 if classPath != "" {
1191 patchPaths += ":" + classPath
1192 }
1193 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001194 }
1195
Nan Zhanged19fc32017-10-19 13:06:22 -07001196 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001197 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001198
Nan Zhanged19fc32017-10-19 13:06:22 -07001199 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001200 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001201
Colin Cross81440082018-08-15 20:21:55 -07001202 if len(javacFlags) > 0 {
1203 // optimization.
1204 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1205 flags.javacFlags = "$javacFlags"
1206 }
1207
Nan Zhanged19fc32017-10-19 13:06:22 -07001208 return flags
1209}
Colin Crossc0b06f12015-04-08 13:03:43 -07001210
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001211func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001212 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001213
1214 deps := j.collectDeps(ctx)
1215 flags := j.collectBuilderFlags(ctx, deps)
1216
Colin Cross1e743852019-10-28 11:37:20 -07001217 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001218 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1219 }
Colin Cross8a497952019-03-05 22:25:09 -08001220 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001221 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001222 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001223 }
1224
Colin Crossaf050172017-11-15 23:01:59 -08001225 srcFiles = j.genSources(ctx, srcFiles, flags)
1226
1227 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001228 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001229 if aaptSrcJar != nil {
1230 srcJars = append(srcJars, aaptSrcJar)
1231 }
Colin Crossb7a63242015-04-16 14:09:14 -07001232
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001233 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001234 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001235 }
1236
Colin Cross1ee23172017-10-18 14:44:18 -07001237 jarName := ctx.ModuleName() + ".jar"
1238
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001239 javaSrcFiles := srcFiles.FilterByExt(".java")
1240 var uniqueSrcFiles android.Paths
1241 set := make(map[string]bool)
1242 for _, v := range javaSrcFiles {
1243 if _, found := set[v.String()]; !found {
1244 set[v.String()] = true
1245 uniqueSrcFiles = append(uniqueSrcFiles, v)
1246 }
1247 }
1248
patricktu242faad2019-09-24 15:41:30 +08001249 // Collect .java files for AIDEGen
1250 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1251
Colin Cross55f63ea2018-08-27 12:37:09 -07001252 var kotlinJars android.Paths
1253
Colin Cross93e85952017-08-15 13:34:18 -07001254 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001255 // user defined kotlin flags.
1256 kotlincFlags := j.properties.Kotlincflags
1257 CheckKotlincFlags(ctx, kotlincFlags)
1258
Colin Cross93e85952017-08-15 13:34:18 -07001259 // If there are kotlin files, compile them first but pass all the kotlin and java files
1260 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1261 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001262 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001263 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001264 kotlincFlags = append(kotlincFlags, "-no-jdk")
1265 }
1266 if len(kotlincFlags) > 0 {
1267 // optimization.
1268 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1269 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001270 }
1271
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001272 var kotlinSrcFiles android.Paths
1273 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1274 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1275
patricktu242faad2019-09-24 15:41:30 +08001276 // Collect .kt files for AIDEGen
1277 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1278
Colin Crossafbb1732019-01-17 15:42:52 -08001279 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1280 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1281
1282 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1283 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1284
1285 if len(flags.processorPath) > 0 {
1286 // Use kapt for annotation processing
1287 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1288 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1289 srcJars = append(srcJars, kaptSrcJar)
1290 // Disable annotation processing in javac, it's already been handled by kapt
1291 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001292 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001293 }
Colin Cross93e85952017-08-15 13:34:18 -07001294
Colin Cross1ee23172017-10-18 14:44:18 -07001295 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001296 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001297 if ctx.Failed() {
1298 return
1299 }
1300
1301 // Make javac rule depend on the kotlinc rule
1302 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001303
Colin Cross93e85952017-08-15 13:34:18 -07001304 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001305 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001306 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001307 }
1308
Colin Cross55f63ea2018-08-27 12:37:09 -07001309 jars := append(android.Paths(nil), kotlinJars...)
1310
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001311 // Store the list of .java files that was passed to javac
1312 j.compiledJavaSrcs = uniqueSrcFiles
1313 j.compiledSrcJars = srcJars
1314
Nan Zhang61eaedb2017-11-02 13:28:15 -07001315 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001316 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001317 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1318 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001319 // Formerly, there was a check here that prevented annotation processors
1320 // from being used when sharding was enabled, as some annotation processors
1321 // do not function correctly in sharded environments. It was removed to
1322 // allow for the use of annotation processors that do function correctly
1323 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001324 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001325 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001326 if ctx.Failed() {
1327 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001328 }
1329 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001330 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001331 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001332 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001333 // If error-prone is enabled, add an additional rule to compile the java files into
1334 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001335 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001336 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1337 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001338 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001339 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001340 extraJarDeps = append(extraJarDeps, errorprone)
1341 }
1342
Nan Zhang61eaedb2017-11-02 13:28:15 -07001343 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001344 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001345 shardSize := int(*(j.properties.Javac_shard_size))
1346 var shardSrcs []android.Paths
1347 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001348 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001349 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001350 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1351 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001352 jars = append(jars, classes)
1353 }
1354 }
1355 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001356 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1357 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001358 jars = append(jars, classes)
1359 }
1360 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001361 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001362 jars = append(jars, classes)
1363 }
Colin Crossd6891432017-09-27 17:39:56 -07001364 if ctx.Failed() {
1365 return
1366 }
Colin Cross2fe66872015-03-30 17:20:39 -07001367 }
1368
Colin Cross0c4ce212019-05-03 15:28:19 -07001369 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1370
1371 var includeSrcJar android.WritablePath
1372 if Bool(j.properties.Include_srcs) {
1373 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1374 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1375 }
1376
Colin Crosscedd4762018-09-13 11:26:19 -07001377 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1378 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001379 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001380 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001381
1382 var resArgs []string
1383 var resDeps android.Paths
1384
1385 resArgs = append(resArgs, dirArgs...)
1386 resDeps = append(resDeps, dirDeps...)
1387
1388 resArgs = append(resArgs, fileArgs...)
1389 resDeps = append(resDeps, fileDeps...)
1390
Colin Cross988708c2019-05-06 14:04:11 -07001391 resArgs = append(resArgs, extraArgs...)
1392 resDeps = append(resDeps, extraDeps...)
1393
Colin Cross40a36712017-09-27 17:41:35 -07001394 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001395 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001396 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001397 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001398 if ctx.Failed() {
1399 return
1400 }
1401 }
1402
Colin Cross0c4ce212019-05-03 15:28:19 -07001403 var resourceJars android.Paths
1404 if j.resourceJar != nil {
1405 resourceJars = append(resourceJars, j.resourceJar)
1406 }
1407 if Bool(j.properties.Include_srcs) {
1408 resourceJars = append(resourceJars, includeSrcJar)
1409 }
1410 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001411
Colin Cross0c4ce212019-05-03 15:28:19 -07001412 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001413 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001414 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001415 false, nil, nil)
1416 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001417 } else if len(resourceJars) == 1 {
1418 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001419 }
1420
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001421 if len(deps.staticJars) > 0 {
1422 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001423 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001424
Colin Cross094054a2018-10-17 15:10:48 -07001425 manifest := j.overrideManifest
1426 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001427 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001428 }
Colin Cross635acc92017-09-12 22:50:46 -07001429
Colin Cross8a497952019-03-05 22:25:09 -08001430 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001431 if len(services) > 0 {
1432 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1433 var zipargs []string
1434 for _, file := range services {
1435 serviceFile := file.String()
1436 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1437 }
1438 ctx.Build(pctx, android.BuildParams{
1439 Rule: zip,
1440 Output: servicesJar,
1441 Implicits: services,
1442 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001443 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001444 },
1445 })
1446 jars = append(jars, servicesJar)
1447 }
1448
Colin Cross0a6e0072017-08-30 14:24:55 -07001449 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001450 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001451 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001452
1453 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001454 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1455 // Optimization: skip the combine step if there is nothing to do
1456 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1457 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1458 // any if len(jars) == 1.
1459 outputFile = moduleOutPath
1460 } else {
1461 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1462 ctx.Build(pctx, android.BuildParams{
1463 Rule: android.Cp,
1464 Input: jars[0],
1465 Output: combinedJar,
1466 })
1467 outputFile = combinedJar
1468 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001469 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001470 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001471 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001472 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001473 outputFile = combinedJar
1474 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001475
Colin Cross331a1212018-08-15 20:40:52 -07001476 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001477 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001478 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001479 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001480 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001481 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001482
1483 // jarjar resource jar if necessary
1484 if j.resourceJar != nil {
1485 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001486 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001487 j.resourceJar = resourceJarJarFile
1488 }
1489
Colin Cross0a6e0072017-08-30 14:24:55 -07001490 if ctx.Failed() {
1491 return
1492 }
1493 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001494
1495 // Check package restrictions if necessary.
1496 if len(j.properties.Permitted_packages) > 0 {
1497 // Check packages and copy to package-checked file.
1498 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1499 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1500 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1501
1502 if ctx.Failed() {
1503 return
1504 }
1505 }
1506
Nan Zhanged19fc32017-10-19 13:06:22 -07001507 j.implementationJarFile = outputFile
1508 if j.headerJarFile == nil {
1509 j.headerJarFile = j.implementationJarFile
1510 }
Colin Cross2fe66872015-03-30 17:20:39 -07001511
Colin Cross3144dfc2018-01-03 15:06:47 -08001512 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001513 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1514 }
1515
Colin Cross331a1212018-08-15 20:40:52 -07001516 // merge implementation jar with resources if necessary
1517 implementationAndResourcesJar := outputFile
1518 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001519 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001520 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001521 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001522 false, nil, nil)
1523 implementationAndResourcesJar = combinedJar
1524 }
1525
1526 j.implementationAndResourcesJar = implementationAndResourcesJar
1527
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001528 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1529 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1530 if j.deviceProperties.Compile_dex == nil {
1531 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1532 }
1533 if j.deviceProperties.Hostdex == nil {
1534 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1535 }
1536 }
1537
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001538 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001539 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001540 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001541 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001542 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001543 if ctx.Failed() {
1544 return
1545 }
Colin Cross331a1212018-08-15 20:40:52 -07001546
Jiyong Park09cb6292019-07-15 15:29:23 +09001547 // Hidden API CSV generation and dex encoding
1548 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1549 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001550
Colin Cross331a1212018-08-15 20:40:52 -07001551 // merge dex jar with resources if necessary
1552 if j.resourceJar != nil {
1553 jars := android.Paths{dexOutputFile, j.resourceJar}
1554 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1555 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1556 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001557 if j.deviceProperties.UncompressDex {
1558 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1559 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1560 dexOutputFile = combinedAlignedJar
1561 } else {
1562 dexOutputFile = combinedJar
1563 }
Colin Cross331a1212018-08-15 20:40:52 -07001564 }
1565
1566 j.dexJarFile = dexOutputFile
1567
Colin Cross8faf8fc2019-01-16 15:15:52 -08001568 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001569 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1570
1571 j.maybeStrippedDexJarFile = dexOutputFile
1572
Colin Cross3063b782018-08-15 11:19:12 -07001573 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001574
1575 if ctx.Failed() {
1576 return
1577 }
Colin Cross331a1212018-08-15 20:40:52 -07001578 } else {
1579 outputFile = implementationAndResourcesJar
Martin Stjernholmd90676f2020-01-11 00:37:30 +00001580
1581 // dexpreopt.GetGlobalSoongConfig needs to be called at least once even if
1582 // no module actually is dexpreopted, to ensure there's a cached
1583 // GlobalSoongConfig for the dexpreopt singletons, which will run
1584 // regardless.
1585 // TODO(b/147613152): Remove when the singletons no longer rely on the
1586 // cached GlobalSoongConfig.
1587 if !dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
1588 _ = dexpreopt.GetGlobalSoongConfig(ctx)
1589 }
Colin Cross2fe66872015-03-30 17:20:39 -07001590 }
Colin Cross331a1212018-08-15 20:40:52 -07001591
Colin Crossb7a63242015-04-16 14:09:14 -07001592 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001593
1594 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1595 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001596}
1597
Colin Cross3b706fd2019-09-05 16:44:18 -07001598func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1599 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1600
1601 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1602 if idx >= 0 {
1603 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1604 jarName += strconv.Itoa(idx)
1605 }
1606
1607 classes := android.PathForModuleOut(ctx, "javac", jarName)
1608 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1609
1610 if ctx.Config().EmitXrefRules() {
1611 extractionFile := android.PathForModuleOut(ctx, kzipName)
1612 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1613 j.kytheFiles = append(j.kytheFiles, extractionFile)
1614 }
1615
1616 return classes
1617}
1618
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001619// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1620// since some of these flags may be used internally.
1621func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1622 for _, flag := range flags {
1623 flag = strings.TrimSpace(flag)
1624
1625 if !strings.HasPrefix(flag, "-") {
1626 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1627 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1628 ctx.PropertyErrorf("kotlincflags",
1629 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1630 } else if inList(flag, config.KotlincIllegalFlags) {
1631 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1632 } else if flag == "-include-runtime" {
1633 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1634 } else {
1635 args := strings.Split(flag, " ")
1636 if args[0] == "-kotlin-home" {
1637 ctx.PropertyErrorf("kotlincflags",
1638 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1639 }
1640 }
1641 }
1642}
1643
Colin Cross8eadbf02017-10-24 17:46:00 -07001644func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001645 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001646
1647 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001648 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001649 // Compile java sources into turbine.jar.
1650 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1651 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1652 if ctx.Failed() {
1653 return nil
1654 }
1655 jars = append(jars, turbineJar)
1656 }
1657
Colin Cross55f63ea2018-08-27 12:37:09 -07001658 jars = append(jars, extraJars...)
1659
Nan Zhanged19fc32017-10-19 13:06:22 -07001660 // Combine any static header libraries into classes-header.jar. If there is only
1661 // one input jar this step will be skipped.
1662 var headerJar android.Path
1663 jars = append(jars, deps.staticHeaderJars...)
1664
Colin Cross5c6ecc12017-10-23 18:12:27 -07001665 // we cannot skip the combine step for now if there is only one jar
1666 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1667 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001668 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001669 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001670 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001671
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001672 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001673 // Transform classes.jar into classes-jarjar.jar
1674 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001675 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001676 headerJar = jarjarFile
1677 if ctx.Failed() {
1678 return nil
1679 }
1680 }
1681
1682 return headerJar
1683}
1684
Colin Crosscb933592017-11-22 13:49:43 -08001685func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001686 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001687
Colin Cross7a3139e2017-12-19 13:57:50 -08001688 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001689
Colin Cross84c38822018-01-03 15:59:46 -08001690 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001691 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1692
1693 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1694
1695 j.jacocoReportClassesFile = jacocoReportClassesFile
1696
1697 return instrumentedJar
1698}
1699
albaltai36ff7dc2018-12-25 14:35:23 +08001700var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001701
Nan Zhanged19fc32017-10-19 13:06:22 -07001702func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001703 if j.headerJarFile == nil {
1704 return nil
1705 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001706 return android.Paths{j.headerJarFile}
1707}
1708
1709func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001710 if j.implementationJarFile == nil {
1711 return nil
1712 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001713 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001714}
1715
Colin Crossf24a22a2019-01-31 14:12:44 -08001716func (j *Module) DexJar() android.Path {
1717 return j.dexJarFile
1718}
1719
Colin Cross331a1212018-08-15 20:40:52 -07001720func (j *Module) ResourceJars() android.Paths {
1721 if j.resourceJar == nil {
1722 return nil
1723 }
1724 return android.Paths{j.resourceJar}
1725}
1726
1727func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001728 if j.implementationAndResourcesJar == nil {
1729 return nil
1730 }
Colin Cross331a1212018-08-15 20:40:52 -07001731 return android.Paths{j.implementationAndResourcesJar}
1732}
1733
Colin Cross46c9b8b2017-06-22 16:51:17 -07001734func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001735 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001736 return j.exportAidlIncludeDirs
1737}
1738
Jiyong Park1be96912018-05-28 18:02:19 +09001739func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001740 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001741 return j.exportedSdkLibs
1742}
1743
Artur Satayev9cf46692019-11-26 18:08:34 +00001744func (j *Module) ExportedPlugins() (android.Paths, []string) {
1745 return j.exportedPluginJars, j.exportedPluginClasses
1746}
1747
Colin Cross0c4ce212019-05-03 15:28:19 -07001748func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1749 return j.srcJarArgs, j.srcJarDeps
1750}
1751
Colin Cross46c9b8b2017-06-22 16:51:17 -07001752var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001753
Colin Cross46c9b8b2017-06-22 16:51:17 -07001754func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001755 return j.logtagsSrcs
1756}
1757
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001758// Collect information for opening IDE project files in java/jdeps.go.
1759func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1760 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1761 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001762 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001763 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001764 if j.expandJarjarRules != nil {
1765 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001766 }
1767}
1768
1769func (j *Module) CompilerDeps() []string {
1770 jdeps := []string{}
1771 jdeps = append(jdeps, j.properties.Libs...)
1772 jdeps = append(jdeps, j.properties.Static_libs...)
1773 return jdeps
1774}
1775
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001776func (j *Module) hasCode(ctx android.ModuleContext) bool {
1777 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1778 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1779}
1780
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001781func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1782 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001783 // Dependencies other than the static linkage are all considered crossing APEX boundary
1784 // Also, a dependency to an sdk member is also considered as such. This is required because
1785 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1786 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001787}
1788
Jiyong Park0b238752019-10-29 11:23:10 +09001789func (j *Module) Stem() string {
1790 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1791}
1792
Jiyong Park618922e2020-01-08 13:35:43 +09001793func (j *Module) JacocoReportClassesFile() android.Path {
1794 return j.jacocoReportClassesFile
1795}
1796
Colin Cross2fe66872015-03-30 17:20:39 -07001797//
1798// Java libraries (.jar file)
1799//
1800
Colin Crossf506d872017-07-19 15:53:04 -07001801type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001802 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001803
1804 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001805}
1806
Colin Cross42be7612019-02-21 18:12:14 -08001807func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001808 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1809 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1810 return true
1811 }
1812
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001813 // Store uncompressed (and do not strip) dex files from boot class path jars.
1814 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1815 return true
1816 }
1817
1818 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001819 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001820 return true
1821 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001822 if ctx.Config().UncompressPrivAppDex() &&
1823 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1824 return true
1825 }
1826
Colin Cross2fc72f62018-12-21 12:59:54 -08001827 return false
1828}
1829
Colin Crossf506d872017-07-19 15:53:04 -07001830func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001831 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001832 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001833 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001834 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001835 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001836 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001837 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001838
Jiyong Park7f7766d2019-07-25 22:02:35 +09001839 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1840 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001841 var extraInstallDeps android.Paths
1842 if j.InstallMixin != nil {
1843 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1844 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001845 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001846 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001847 }
Colin Crossb7a63242015-04-16 14:09:14 -07001848}
1849
Colin Crossf506d872017-07-19 15:53:04 -07001850func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001851 j.deps(ctx)
1852}
1853
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001854const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001855 aidlIncludeDir = "aidl"
1856 javaDir = "java"
1857 jarFileSuffix = ".jar"
1858 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001859)
1860
Paul Duffina0dbf432019-12-05 11:25:53 +00001861// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001862func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1863 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1864}
1865
1866func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1867 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001868}
1869
Paul Duffin13879572019-11-28 14:31:38 +00001870type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001871 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001872}
1873
1874func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1875 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1876}
1877
1878func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1879 _, ok := module.(*Library)
1880 return ok
1881}
1882
Paul Duffina0dbf432019-12-05 11:25:53 +00001883func (mt *librarySdkMemberType) buildSnapshot(
1884 sdkModuleContext android.ModuleContext,
1885 builder android.SnapshotBuilder,
1886 member android.SdkMember,
1887 jarToExportGetter func(j *Library) android.Path) {
1888
Paul Duffin13879572019-11-28 14:31:38 +00001889 variants := member.Variants()
1890 if len(variants) != 1 {
1891 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1892 for _, variant := range variants {
1893 sdkModuleContext.ModuleErrorf(" %q", variant)
1894 }
1895 }
1896 variant := variants[0]
1897 j := variant.(*Library)
1898
Paul Duffina0dbf432019-12-05 11:25:53 +00001899 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001900 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001901 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001902
1903 for _, dir := range j.AidlIncludeDirs() {
1904 // TODO(jiyong): copy parcelable declarations only
1905 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1906 for _, file := range aidlFiles {
1907 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1908 }
1909 }
1910
Paul Duffin9d8d6092019-12-05 18:19:29 +00001911 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001912 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001913}
1914
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001915var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1916 librarySdkMemberType{
1917 android.SdkMemberTypeBase{
1918 PropertyName: "java_header_libs",
1919 SupportsSdk: true,
1920 },
1921 },
1922}
1923
Paul Duffina0dbf432019-12-05 11:25:53 +00001924type headerLibrarySdkMemberType struct {
1925 librarySdkMemberType
1926}
1927
1928func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1929 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1930 headerJars := j.HeaderJars()
1931 if len(headerJars) != 1 {
1932 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1933 }
1934
1935 return headerJars[0]
1936 })
1937}
1938
Paul Duffina0dbf432019-12-05 11:25:53 +00001939type implLibrarySdkMemberType struct {
1940 librarySdkMemberType
1941}
1942
1943func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1944 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1945 implementationJars := j.ImplementationJars()
1946 if len(implementationJars) != 1 {
1947 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1948 }
1949
1950 return implementationJars[0]
1951 })
1952}
1953
Colin Cross1b16b0e2019-02-12 14:41:32 -08001954// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1955//
1956// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1957// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1958// as a `static_libs` dependency of another module.
1959//
1960// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1961// a device.
1962//
1963// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1964// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001965func LibraryFactory() android.Module {
1966 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001967
Colin Cross9ae1b922018-06-26 17:59:05 -07001968 module.AddProperties(
1969 &module.Module.properties,
1970 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001971 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001972 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001973
Jiyong Park7f7766d2019-07-25 22:02:35 +09001974 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001975 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001976 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001977 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001978}
1979
Colin Cross1b16b0e2019-02-12 14:41:32 -08001980// java_library_static is an obsolete alias for java_library.
1981func LibraryStaticFactory() android.Module {
1982 return LibraryFactory()
1983}
1984
1985// java_library_host builds and links sources into a `.jar` file for the host.
1986//
1987// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1988// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001989func LibraryHostFactory() android.Module {
1990 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001991
Colin Cross6af17aa2017-09-20 12:59:05 -07001992 module.AddProperties(
1993 &module.Module.properties,
1994 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001995
Colin Cross9ae1b922018-06-26 17:59:05 -07001996 module.Module.properties.Installable = proptools.BoolPtr(true)
1997
Jiyong Park7f7766d2019-07-25 22:02:35 +09001998 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001999 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002000 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002001}
2002
2003//
Colin Crossb628ea52018-08-14 16:42:33 -07002004// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002005//
2006
2007type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002008 // list of compatibility suites (for example "cts", "vts") that the module should be
2009 // installed into.
2010 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002011
2012 // the name of the test configuration (for example "AndroidTest.xml") that should be
2013 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002014 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002015
Jack He33338892018-09-19 02:21:28 -07002016 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2017 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002018 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002019
Colin Crossd96ca352018-08-10 16:06:24 -07002020 // list of files or filegroup modules that provide data that should be installed alongside
2021 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002022 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002023
2024 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2025 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2026 // explicitly.
2027 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002028}
2029
Paul Duffin42df1442019-03-20 12:45:53 +00002030type testHelperLibraryProperties struct {
2031 // list of compatibility suites (for example "cts", "vts") that the module should be
2032 // installed into.
2033 Test_suites []string `android:"arch_variant"`
2034}
2035
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002036type prebuiltTestProperties struct {
2037 // list of compatibility suites (for example "cts", "vts") that the module should be
2038 // installed into.
2039 Test_suites []string `android:"arch_variant"`
2040
2041 // the name of the test configuration (for example "AndroidTest.xml") that should be
2042 // installed with the module.
2043 Test_config *string `android:"path,arch_variant"`
2044}
2045
Colin Cross05638fc2018-04-09 18:40:24 -07002046type Test struct {
2047 Library
2048
2049 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002050
2051 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002052 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002053}
2054
Paul Duffin42df1442019-03-20 12:45:53 +00002055type TestHelperLibrary struct {
2056 Library
2057
2058 testHelperLibraryProperties testHelperLibraryProperties
2059}
2060
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002061type JavaTestImport struct {
2062 Import
2063
2064 prebuiltTestProperties prebuiltTestProperties
2065
2066 testConfig android.Path
2067}
2068
Colin Cross303e21f2018-08-07 16:49:25 -07002069func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002070 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2071 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002072 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002073
2074 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002075}
2076
Paul Duffin42df1442019-03-20 12:45:53 +00002077func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2078 j.Library.GenerateAndroidBuildActions(ctx)
2079}
2080
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002081func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2082 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2083 j.prebuiltTestProperties.Test_suites, nil)
2084
2085 j.Import.GenerateAndroidBuildActions(ctx)
2086}
2087
2088type testSdkMemberType struct {
2089 android.SdkMemberTypeBase
2090}
2091
2092func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2093 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2094}
2095
2096func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2097 _, ok := module.(*Test)
2098 return ok
2099}
2100
2101func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2102 variants := member.Variants()
2103 if len(variants) != 1 {
2104 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2105 for _, variant := range variants {
2106 sdkModuleContext.ModuleErrorf(" %q", variant)
2107 }
2108 }
2109 variant := variants[0]
2110 j := variant.(*Test)
2111
2112 implementationJars := j.ImplementationJars()
2113 if len(implementationJars) != 1 {
2114 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2115 }
2116
2117 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2118 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2119
2120 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2121 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2122
2123 module := builder.AddPrebuiltModule(member, "java_test_import")
2124 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2125 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2126}
2127
Colin Cross1b16b0e2019-02-12 14:41:32 -08002128// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2129// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2130//
2131// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2132// compiled against the device bootclasspath.
2133//
2134// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2135// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002136func TestFactory() android.Module {
2137 module := &Test{}
2138
2139 module.AddProperties(
2140 &module.Module.properties,
2141 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002142 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002143 &module.Module.protoProperties,
2144 &module.testProperties)
2145
Colin Cross9ae1b922018-06-26 17:59:05 -07002146 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002147 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002148
Colin Cross05638fc2018-04-09 18:40:24 -07002149 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002150 return module
2151}
2152
Paul Duffin42df1442019-03-20 12:45:53 +00002153// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2154func TestHelperLibraryFactory() android.Module {
2155 module := &TestHelperLibrary{}
2156
2157 module.AddProperties(
2158 &module.Module.properties,
2159 &module.Module.deviceProperties,
2160 &module.Module.dexpreoptProperties,
2161 &module.Module.protoProperties,
2162 &module.testHelperLibraryProperties)
2163
Colin Cross9a4abed2019-04-24 13:19:28 -07002164 module.Module.properties.Installable = proptools.BoolPtr(true)
2165 module.Module.dexpreopter.isTest = true
2166
Paul Duffin42df1442019-03-20 12:45:53 +00002167 InitJavaModule(module, android.HostAndDeviceSupported)
2168 return module
2169}
2170
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002171// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2172// and makes sure that it is added to the appropriate test suite.
2173//
2174// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2175// compiled against an Android classpath.
2176//
2177// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2178// for host modules.
2179func JavaTestImportFactory() android.Module {
2180 module := &JavaTestImport{}
2181
2182 module.AddProperties(
2183 &module.Import.properties,
2184 &module.prebuiltTestProperties)
2185
2186 module.Import.properties.Installable = proptools.BoolPtr(true)
2187
2188 android.InitPrebuiltModule(module, &module.properties.Jars)
2189 android.InitApexModule(module)
2190 android.InitSdkAwareModule(module)
2191 InitJavaModule(module, android.HostAndDeviceSupported)
2192 return module
2193}
2194
Colin Cross1b16b0e2019-02-12 14:41:32 -08002195// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2196// allow running the test with `atest` or a `TEST_MAPPING` file.
2197//
2198// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2199// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002200func TestHostFactory() android.Module {
2201 module := &Test{}
2202
2203 module.AddProperties(
2204 &module.Module.properties,
2205 &module.Module.protoProperties,
2206 &module.testProperties)
2207
Colin Cross9ae1b922018-06-26 17:59:05 -07002208 module.Module.properties.Installable = proptools.BoolPtr(true)
2209
Colin Cross05638fc2018-04-09 18:40:24 -07002210 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002211 return module
2212}
2213
2214//
Colin Cross2fe66872015-03-30 17:20:39 -07002215// Java Binaries (.jar file plus wrapper script)
2216//
2217
Colin Crossf506d872017-07-19 15:53:04 -07002218type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002219 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002220 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002221
2222 // Name of the class containing main to be inserted into the manifest as Main-Class.
2223 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002224}
2225
Colin Crossf506d872017-07-19 15:53:04 -07002226type Binary struct {
2227 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002228
Colin Crossf506d872017-07-19 15:53:04 -07002229 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002230
Colin Cross6b4a32d2017-12-05 13:42:45 -08002231 isWrapperVariant bool
2232
Colin Crossc3315992017-12-08 19:12:36 -08002233 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002234 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002235}
2236
Alex Light24237172017-10-26 09:46:21 -07002237func (j *Binary) HostToolPath() android.OptionalPath {
2238 return android.OptionalPathForPath(j.binaryFile)
2239}
2240
Colin Crossf506d872017-07-19 15:53:04 -07002241func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002242 if ctx.Arch().ArchType == android.Common {
2243 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002244 if j.binaryProperties.Main_class != nil {
2245 if j.properties.Manifest != nil {
2246 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2247 }
2248 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2249 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2250 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2251 }
2252
Colin Cross6b4a32d2017-12-05 13:42:45 -08002253 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002254 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002255 // Handle the binary wrapper
2256 j.isWrapperVariant = true
2257
Colin Cross366938f2017-12-11 16:29:02 -08002258 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002259 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002260 } else {
2261 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2262 }
2263
2264 // Depend on the installed jar so that the wrapper doesn't get executed by
2265 // another build rule before the jar has been installed.
2266 jarFile := ctx.PrimaryModule().(*Binary).installFile
2267
2268 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2269 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002270 }
Colin Cross2fe66872015-03-30 17:20:39 -07002271}
2272
Colin Crossf506d872017-07-19 15:53:04 -07002273func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002274 if ctx.Arch().ArchType == android.Common {
2275 j.deps(ctx)
2276 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002277}
2278
Colin Cross1b16b0e2019-02-12 14:41:32 -08002279// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2280// as well.
2281//
2282// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2283// compiled against the device bootclasspath.
2284//
2285// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2286// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002287func BinaryFactory() android.Module {
2288 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002289
Colin Cross36242852017-06-23 15:06:31 -07002290 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002291 &module.Module.properties,
2292 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002293 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002294 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002295 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002296
Colin Cross9ae1b922018-06-26 17:59:05 -07002297 module.Module.properties.Installable = proptools.BoolPtr(true)
2298
Colin Cross6b4a32d2017-12-05 13:42:45 -08002299 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2300 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002301 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002302}
2303
Colin Cross1b16b0e2019-02-12 14:41:32 -08002304// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2305//
2306// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2307// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002308func BinaryHostFactory() android.Module {
2309 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002310
Colin Cross36242852017-06-23 15:06:31 -07002311 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002312 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002313 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002314 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002315
Colin Cross9ae1b922018-06-26 17:59:05 -07002316 module.Module.properties.Installable = proptools.BoolPtr(true)
2317
Colin Cross6b4a32d2017-12-05 13:42:45 -08002318 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2319 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002320 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002321}
2322
2323//
2324// Java prebuilts
2325//
2326
Colin Cross74d73e22017-08-02 11:05:49 -07002327type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002328 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002329
Nan Zhangea568a42017-11-08 21:20:04 -08002330 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002331
2332 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002333
2334 // List of shared java libs that this module has dependencies to
2335 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002336
2337 // List of files to remove from the jar file(s)
2338 Exclude_files []string
2339
2340 // List of directories to remove from the jar file(s)
2341 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002342
2343 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002344 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002345
2346 // set the name of the output
2347 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002348}
2349
2350type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002351 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002352 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002353 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002354 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002355 android.SdkBase
Martin Stjernholmd90676f2020-01-11 00:37:30 +00002356 dexpreopt.DexPreoptModule
Colin Cross2fe66872015-03-30 17:20:39 -07002357
Colin Cross74d73e22017-08-02 11:05:49 -07002358 properties ImportProperties
2359
Colin Cross0a6e0072017-08-30 14:24:55 -07002360 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002361 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002362}
2363
Jiyong Park6a927c42020-01-21 02:03:43 +09002364func (j *Import) sdkVersion() sdkSpec {
2365 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002366}
2367
Jiyong Park6a927c42020-01-21 02:03:43 +09002368func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002369 return j.sdkVersion()
2370}
2371
Colin Cross74d73e22017-08-02 11:05:49 -07002372func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002373 return &j.prebuilt
2374}
2375
Colin Cross74d73e22017-08-02 11:05:49 -07002376func (j *Import) PrebuiltSrcs() []string {
2377 return j.properties.Jars
2378}
2379
2380func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002381 return j.prebuilt.Name(j.ModuleBase.Name())
2382}
2383
Jiyong Park0b238752019-10-29 11:23:10 +09002384func (j *Import) Stem() string {
2385 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2386}
2387
Jiyong Park618922e2020-01-08 13:35:43 +09002388func (a *Import) JacocoReportClassesFile() android.Path {
2389 return nil
2390}
2391
Colin Cross74d73e22017-08-02 11:05:49 -07002392func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002393 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002394}
2395
Colin Cross74d73e22017-08-02 11:05:49 -07002396func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002397 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002398
Jiyong Park0b238752019-10-29 11:23:10 +09002399 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002400 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002401 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2402 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002403 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002404 inputFile := outputFile
2405 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2406 TransformJetifier(ctx, outputFile, inputFile)
2407 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002408 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002409
2410 ctx.VisitDirectDeps(func(module android.Module) {
2411 otherName := ctx.OtherModuleName(module)
2412 tag := ctx.OtherModuleDependencyTag(module)
2413
2414 switch dep := module.(type) {
2415 case Dependency:
2416 switch tag {
2417 case libTag, staticLibTag:
2418 // sdk lib names from dependencies are re-exported
2419 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2420 }
2421 case SdkLibraryDependency:
2422 switch tag {
2423 case libTag:
2424 // names of sdk libs that are directly depended are exported
2425 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2426 }
2427 }
2428 })
2429
2430 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002431 if Bool(j.properties.Installable) {
2432 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002433 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002434 }
Colin Cross2fe66872015-03-30 17:20:39 -07002435}
2436
Colin Cross74d73e22017-08-02 11:05:49 -07002437var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002438
Nan Zhanged19fc32017-10-19 13:06:22 -07002439func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002440 if j.combinedClasspathFile == nil {
2441 return nil
2442 }
Colin Cross37f6d792018-07-12 12:28:41 -07002443 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002444}
2445
2446func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002447 if j.combinedClasspathFile == nil {
2448 return nil
2449 }
Colin Cross37f6d792018-07-12 12:28:41 -07002450 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002451}
2452
Colin Cross331a1212018-08-15 20:40:52 -07002453func (j *Import) ResourceJars() android.Paths {
2454 return nil
2455}
2456
2457func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002458 if j.combinedClasspathFile == nil {
2459 return nil
2460 }
Colin Cross331a1212018-08-15 20:40:52 -07002461 return android.Paths{j.combinedClasspathFile}
2462}
2463
Colin Crossf24a22a2019-01-31 14:12:44 -08002464func (j *Import) DexJar() android.Path {
2465 return nil
2466}
2467
Colin Cross74d73e22017-08-02 11:05:49 -07002468func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002469 return nil
2470}
2471
Jiyong Park1be96912018-05-28 18:02:19 +09002472func (j *Import) ExportedSdkLibs() []string {
2473 return j.exportedSdkLibs
2474}
2475
Artur Satayev9cf46692019-11-26 18:08:34 +00002476func (j *Import) ExportedPlugins() (android.Paths, []string) {
2477 return nil, nil
2478}
2479
Colin Cross0c4ce212019-05-03 15:28:19 -07002480func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2481 return nil, nil
2482}
2483
Jiyong Park0f80c182020-01-31 02:49:53 +09002484func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2485 depTag := ctx.OtherModuleDependencyTag(dep)
2486 // dependencies other than the static linkage are all considered crossing APEX boundary
2487 // Also, a dependency to an sdk member is also considered as such. This is required because
2488 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2489 return depTag == staticLibTag || j.IsInAnySdk()
2490}
2491
albaltai36ff7dc2018-12-25 14:35:23 +08002492// Add compile time check for interface implementation
2493var _ android.IDEInfo = (*Import)(nil)
2494var _ android.IDECustomizedModuleName = (*Import)(nil)
2495
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002496// Collect information for opening IDE project files in java/jdeps.go.
2497const (
2498 removedPrefix = "prebuilt_"
2499)
2500
2501func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2502 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2503}
2504
2505func (j *Import) IDECustomizedModuleName() string {
2506 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2507 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2508 // solution to get the Import name.
2509 name := j.Name()
2510 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002511 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002512 }
2513 return name
2514}
2515
Colin Cross74d73e22017-08-02 11:05:49 -07002516var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002517
Colin Cross1b16b0e2019-02-12 14:41:32 -08002518// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2519//
2520// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2521// compiled against an Android classpath.
2522//
2523// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2524// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002525func ImportFactory() android.Module {
2526 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002527
Colin Cross74d73e22017-08-02 11:05:49 -07002528 module.AddProperties(&module.properties)
2529
2530 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002531 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002532 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002533 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002534 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002535}
2536
Colin Cross1b16b0e2019-02-12 14:41:32 -08002537// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2538// module.
2539//
2540// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2541// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002542func ImportFactoryHost() android.Module {
2543 module := &Import{}
2544
2545 module.AddProperties(&module.properties)
2546
2547 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002548 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002549 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002550 return module
2551}
2552
Colin Cross42be7612019-02-21 18:12:14 -08002553// dex_import module
2554
2555type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002556 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002557
2558 // set the name of the output
2559 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002560}
2561
2562type DexImport struct {
2563 android.ModuleBase
2564 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002565 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002566 prebuilt android.Prebuilt
Martin Stjernholmd90676f2020-01-11 00:37:30 +00002567 dexpreopt.DexPreoptModule
Colin Cross42be7612019-02-21 18:12:14 -08002568
2569 properties DexImportProperties
2570
2571 dexJarFile android.Path
2572 maybeStrippedDexJarFile android.Path
2573
2574 dexpreopter
2575}
2576
2577func (j *DexImport) Prebuilt() *android.Prebuilt {
2578 return &j.prebuilt
2579}
2580
2581func (j *DexImport) PrebuiltSrcs() []string {
2582 return j.properties.Jars
2583}
2584
2585func (j *DexImport) Name() string {
2586 return j.prebuilt.Name(j.ModuleBase.Name())
2587}
2588
Jiyong Park0b238752019-10-29 11:23:10 +09002589func (j *DexImport) Stem() string {
2590 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2591}
2592
Colin Cross42be7612019-02-21 18:12:14 -08002593func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2594 if len(j.properties.Jars) != 1 {
2595 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2596 }
2597
Jiyong Park0b238752019-10-29 11:23:10 +09002598 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002599 j.dexpreopter.isInstallable = true
2600 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2601
2602 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2603 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2604
2605 if j.dexpreopter.uncompressedDex {
2606 rule := android.NewRuleBuilder()
2607
2608 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2609 rule.Temporary(temporary)
2610
2611 // use zip2zip to uncompress classes*.dex files
2612 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002613 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002614 FlagWithInput("-i ", inputJar).
2615 FlagWithOutput("-o ", temporary).
2616 FlagWithArg("-0 ", "'classes*.dex'")
2617
2618 // use zipalign to align uncompressed classes*.dex files
2619 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002620 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002621 Flag("-f").
2622 Text("4").
2623 Input(temporary).
2624 Output(dexOutputFile)
2625
2626 rule.DeleteTemporaryFiles()
2627
2628 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2629 } else {
2630 ctx.Build(pctx, android.BuildParams{
2631 Rule: android.Cp,
2632 Input: inputJar,
2633 Output: dexOutputFile,
2634 })
2635 }
2636
2637 j.dexJarFile = dexOutputFile
2638
2639 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2640
2641 j.maybeStrippedDexJarFile = dexOutputFile
2642
2643 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2644 ctx.ModuleName()+".jar", dexOutputFile)
2645}
2646
2647func (j *DexImport) DexJar() android.Path {
2648 return j.dexJarFile
2649}
2650
2651// dex_import imports a `.jar` file containing classes.dex files.
2652//
2653// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2654// to the device.
2655func DexImportFactory() android.Module {
2656 module := &DexImport{}
2657
2658 module.AddProperties(&module.properties)
2659
2660 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002661 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002662 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002663 return module
2664}
2665
Colin Cross89536d42017-07-07 14:35:50 -07002666//
2667// Defaults
2668//
2669type Defaults struct {
2670 android.ModuleBase
2671 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002672 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002673}
2674
Colin Cross1b16b0e2019-02-12 14:41:32 -08002675// java_defaults provides a set of properties that can be inherited by other java or android modules.
2676//
2677// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2678// property in the defaults module that exists in the depending module will be prepended to the depending module's
2679// value for that property.
2680//
2681// Example:
2682//
2683// java_defaults {
2684// name: "example_defaults",
2685// srcs: ["common/**/*.java"],
2686// javacflags: ["-Xlint:all"],
2687// aaptflags: ["--auto-add-overlay"],
2688// }
2689//
2690// java_library {
2691// name: "example",
2692// defaults: ["example_defaults"],
2693// srcs: ["example/**/*.java"],
2694// }
2695//
2696// is functionally identical to:
2697//
2698// java_library {
2699// name: "example",
2700// srcs: [
2701// "common/**/*.java",
2702// "example/**/*.java",
2703// ],
2704// javacflags: ["-Xlint:all"],
2705// }
Colin Cross89536d42017-07-07 14:35:50 -07002706func defaultsFactory() android.Module {
2707 return DefaultsFactory()
2708}
2709
Paul Duffin47357662019-12-05 14:07:14 +00002710func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002711 module := &Defaults{}
2712
Colin Cross89536d42017-07-07 14:35:50 -07002713 module.AddProperties(
2714 &CompilerProperties{},
2715 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002716 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002717 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002718 &aaptProperties{},
2719 &androidLibraryProperties{},
2720 &appProperties{},
2721 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002722 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002723 &ImportProperties{},
2724 &AARImportProperties{},
2725 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002726 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002727 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002728 )
2729
2730 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002731 return module
2732}
Nan Zhangea568a42017-11-08 21:20:04 -08002733
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002734func kytheExtractJavaFactory() android.Singleton {
2735 return &kytheExtractJavaSingleton{}
2736}
2737
2738type kytheExtractJavaSingleton struct {
2739}
2740
2741func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2742 var xrefTargets android.Paths
2743 ctx.VisitAllModules(func(module android.Module) {
2744 if javaModule, ok := module.(xref); ok {
2745 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2746 }
2747 })
2748 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2749 if len(xrefTargets) > 0 {
2750 ctx.Build(pctx, android.BuildParams{
2751 Rule: blueprint.Phony,
2752 Output: android.PathForPhony(ctx, "xref_java"),
2753 Inputs: xrefTargets,
2754 })
2755 }
2756}
2757
Nan Zhangea568a42017-11-08 21:20:04 -08002758var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002759var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002760var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002761var inList = android.InList