blob: b33cb2aa39ec38a927a4396c58f89b24a2b2b9f5 [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 Stjernholm6d415272020-01-31 17:10:36 +000079 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
80 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
81 })
Martin Stjernholmd90676f2020-01-11 00:37:30 +000082
Paul Duffinf9b1da02019-12-18 19:51:55 +000083 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
84 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
85}
86
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000087func RegisterPostDepsMutators(ctx android.RegisterMutatorsContext) {
88 ctx.BottomUp("ordered_system_server_jars", systemServerJarsDepsMutator)
89}
90
91var (
92 dexpreoptedSystemServerJarsKey = android.NewOnceKey("dexpreoptedSystemServerJars")
93 dexpreoptedSystemServerJarsLock sync.Mutex
94)
95
96func DexpreoptedSystemServerJars(config android.Config) *[]string {
97 return config.Once(dexpreoptedSystemServerJarsKey, func() interface{} {
98 return &[]string{}
99 }).(*[]string)
100}
101
102// A PostDepsMutator pass that enforces total order on non-updatable system server jars. A total
103// order is neededed because such jars must be dexpreopted together (each jar on the list must have
104// all preceding jars in its class loader context). The total order must be compatible with the
105// partial order imposed by genuine dependencies between system server jars (which is not always
106// respected by the PRODUCT_SYSTEM_SERVER_JARS variable).
107//
108// An earlier mutator pass creates genuine dependencies, and this pass traverses the jars in that
109// order (which is partial and non-deterministic). This pass adds additional dependencies between
110// jars, making the order total and deterministic. It also constructs a global ordered list.
111func systemServerJarsDepsMutator(ctx android.BottomUpMutatorContext) {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000112 jars := dexpreopt.NonUpdatableSystemServerJars(ctx, dexpreopt.GetGlobalConfig(ctx))
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000113 name := ctx.ModuleName()
114 if android.InList(name, jars) {
115 dexpreoptedSystemServerJarsLock.Lock()
116 defer dexpreoptedSystemServerJarsLock.Unlock()
117 jars := DexpreoptedSystemServerJars(ctx.Config())
118 for _, dep := range *jars {
119 ctx.AddDependency(ctx.Module(), dexpreopt.SystemServerDepTag, dep)
120 }
121 *jars = append(*jars, name)
122 }
123}
124
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900125func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
126 if j.SocSpecific() || j.DeviceSpecific() ||
127 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
128 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +0900129 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900130 ctx.PropertyErrorf("sdk_version",
131 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
132 }
133 }
134 }
135}
136
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
138 if sc, ok := ctx.Module().(sdkContext); ok {
139 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +0900140 sdkVersionSpecified := sc.sdkVersion().specified()
141 if usePlatformAPI && sdkVersionSpecified {
142 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
143 } else if !usePlatformAPI && !sdkVersionSpecified {
144 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900145 }
146
147 }
148}
149
Colin Cross2fe66872015-03-30 17:20:39 -0700150// TODO:
151// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700152// Renderscript
153// Post-jar passes:
154// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700155// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700156// DroidDoc
157// Findbugs
158
Colin Cross89536d42017-07-07 14:35:50 -0700159type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700160 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
161 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800162 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700163
164 // list of source files that should not be used to build the Java module.
165 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800166 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700167
168 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700169 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700170
Colin Cross86a63ff2017-09-27 17:33:10 -0700171 // list of directories that should be excluded from java_resource_dirs
172 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700173
Colin Cross0f37af02017-09-27 17:42:05 -0700174 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800175 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700176
Colin Crosscedd4762018-09-13 11:26:19 -0700177 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800178 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700179
Colin Cross7d5136f2015-05-11 13:39:40 -0700180 // list of module-specific flags that will be used for javac compiles
181 Javacflags []string `android:"arch_variant"`
182
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200183 // list of module-specific flags that will be used for kotlinc compiles
184 Kotlincflags []string `android:"arch_variant"`
185
Colin Cross7d5136f2015-05-11 13:39:40 -0700186 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700187 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700188
189 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700190 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700191
192 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800193 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700194
Colin Cross540eff82017-06-22 17:01:52 -0700195 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800196 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700197
198 // If not blank, set the java version passed to javac as -source and -target
199 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700200
Colin Cross9ae1b922018-06-26 17:59:05 -0700201 // If set to true, allow this module to be dexed and installed on devices. Has no
202 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700203 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700204
Colin Cross0f37af02017-09-27 17:42:05 -0700205 // If set to true, include sources used to compile the module in to the final jar
206 Include_srcs *bool
207
Vladimir Marko0975ee02019-04-02 10:29:55 +0100208 // If not empty, classes are restricted to the specified packages and their sub-packages.
209 // This restriction is checked after applying jarjar rules and including static libs.
210 Permitted_packages []string
211
Colin Crossbe9cdb82019-01-21 21:37:16 -0800212 // List of modules to use as annotation processors
213 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700214
Artur Satayev9cf46692019-11-26 18:08:34 +0000215 // List of modules to export to libraries that directly depend on this library as annotation processors
216 Exported_plugins []string
217
Nan Zhang61eaedb2017-11-02 13:28:15 -0700218 // The number of Java source entries each Javac instance can process
219 Javac_shard_size *int64
220
Nan Zhang5f8cb422018-02-06 10:34:32 -0800221 // Add host jdk tools.jar to bootclasspath
222 Use_tools_jar *bool
223
Colin Cross1369cdb2017-09-29 17:58:17 -0700224 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700225 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800226 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700227
Colin Cross6cef4812019-10-17 14:23:50 -0700228 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700229 Javacflags []string
230 }
Colin Crosscb933592017-11-22 13:49:43 -0800231
Colin Cross81440082018-08-15 20:21:55 -0700232 // When compiling language level 9+ .java code in packages that are part of
233 // a system module, patch_module names the module that your sources and
234 // dependencies should be patched into. The Android runtime currently
235 // doesn't implement the JEP 261 module system so this option is only
236 // supported at compile time. It should only be needed to compile tests in
237 // packages that exist in libcore and which are inconvenient to move
238 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100239 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700240
Colin Crosscb933592017-11-22 13:49:43 -0800241 Jacoco struct {
242 // List of classes to include for instrumentation with jacoco to collect coverage
243 // information at runtime when building with coverage enabled. If unset defaults to all
244 // classes.
245 // Supports '*' as the last character of an entry in the list as a wildcard match.
246 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
247 // it matches classes in the package that have the class name as a prefix.
248 Include_filter []string
249
250 // List of classes to exclude from instrumentation with jacoco to collect coverage
251 // information at runtime when building with coverage enabled. Overrides classes selected
252 // by the include_filter property.
253 // Supports '*' as the last character of an entry in the list as a wildcard match.
254 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
255 // it matches classes in the package that have the class name as a prefix.
256 Exclude_filter []string
257 }
258
Andreas Gampef3e5b552018-01-22 21:27:21 -0800259 Errorprone struct {
260 // List of javac flags that should only be used when running errorprone.
261 Javacflags []string
262 }
263
Colin Cross0f2ee152017-12-14 15:22:43 -0800264 Proto struct {
265 // List of extra options that will be passed to the proto generator.
266 Output_params []string
267 }
268
Colin Crosscb933592017-11-22 13:49:43 -0800269 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800270
271 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800272 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700273}
274
Colin Cross89536d42017-07-07 14:35:50 -0700275type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700276 // list of module-specific flags that will be used for dex compiles
277 Dxflags []string `android:"arch_variant"`
278
Jeongik Cha538c0d02019-07-11 15:54:27 +0900279 // if not blank, set to the version of the sdk to compile against.
280 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800281 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700282
Colin Cross83bb3162018-06-25 15:48:06 -0700283 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
284 // Defaults to sdk_version if not set.
285 Min_sdk_version *string
286
Dan Willemsen419290a2018-10-31 15:28:47 -0700287 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
288 // Defaults to sdk_version if not set.
289 Target_sdk_version *string
290
Jeongik Cha356dac42019-08-19 14:09:52 +0900291 // Whether to compile against the platform APIs instead of an SDK.
292 // If true, then sdk_version must be empty. The value of this field
293 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700294 Platform_apis *bool
295
Colin Crossebe1a512017-11-14 13:12:14 -0800296 Aidl struct {
297 // Top level directories to pass to aidl tool
298 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700299
Colin Crossebe1a512017-11-14 13:12:14 -0800300 // Directories rooted at the Android.bp file to pass to aidl tool
301 Local_include_dirs []string
302
303 // directories that should be added as include directories for any aidl sources of modules
304 // that depend on this module, as well as to aidl for this module.
305 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100306
307 // whether to generate traces (for systrace) for this interface
308 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100309
310 // whether to generate Binder#GetTransaction name method.
311 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800312 }
Colin Cross92430102017-10-09 14:59:32 -0700313
314 // If true, export a copy of the module as a -hostdex module for host testing.
315 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700316
Colin Cross7f87f4f2019-04-24 13:41:45 -0700317 Target struct {
318 Hostdex struct {
319 // Additional required dependencies to add to -hostdex modules.
320 Required []string
321 }
322 }
323
David Brazdil17ef5632018-06-27 10:27:45 +0100324 // If set to true, compile dex regardless of installable. Defaults to false.
325 Compile_dex *bool
326
Colin Cross66dbc0b2017-12-28 12:23:20 -0800327 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700328 // If false, disable all optimization. Defaults to true for android_app and android_test
329 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800330 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700331 // True if the module containing this has it set by default.
332 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800333
334 // If true, optimize for size by removing unused code. Defaults to true for apps,
335 // false for libraries and tests.
336 Shrink *bool
337
338 // If true, optimize bytecode. Defaults to false.
339 Optimize *bool
340
341 // If true, obfuscate bytecode. Defaults to false.
342 Obfuscate *bool
343
344 // If true, do not use the flag files generated by aapt that automatically keep
345 // classes referenced by the app manifest. Defaults to false.
346 No_aapt_flags *bool
347
348 // Flags to pass to proguard.
349 Proguard_flags []string
350
351 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800352 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800353 }
354
Paul Duffine25c6442019-10-11 13:50:28 +0100355 // When targeting 1.9 and above, override the modules to use with --system,
356 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700357 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700358
Jiyong Park4c4c0242019-10-21 14:53:15 +0900359 // set the name of the output
360 Stem *string
361
Colin Cross5a0dcd52018-10-05 14:20:06 -0700362 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800363 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700364}
365
Sasha Smundak2057f822019-04-16 17:16:58 -0700366func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
367 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
368}
369
Colin Cross46c9b8b2017-06-22 16:51:17 -0700370// Module contains the properties and members used by all java module types
371type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700372 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700373 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900374 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900375 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700376
Colin Cross89536d42017-07-07 14:35:50 -0700377 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700378 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700379 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700380
Colin Cross331a1212018-08-15 20:40:52 -0700381 // jar file containing header classes including static library dependencies, suitable for
382 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700383 headerJarFile android.Path
384
Colin Cross331a1212018-08-15 20:40:52 -0700385 // jar file containing implementation classes including static library dependencies but no
386 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700387 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700388
Colin Cross331a1212018-08-15 20:40:52 -0700389 // jar file containing only resources including from static library dependencies
390 resourceJar android.Path
391
Colin Cross0c4ce212019-05-03 15:28:19 -0700392 // args and dependencies to package source files into a srcjar
393 srcJarArgs []string
394 srcJarDeps android.Paths
395
Colin Cross331a1212018-08-15 20:40:52 -0700396 // jar file containing implementation classes and resources including static library
397 // dependencies
398 implementationAndResourcesJar android.Path
399
400 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700401 dexJarFile android.Path
402
Colin Cross43f08db2018-11-12 10:13:39 -0800403 // output file that contains classes.dex if it should be in the output file
404 maybeStrippedDexJarFile android.Path
405
Colin Crosscb933592017-11-22 13:49:43 -0800406 // output file containing uninstrumented classes that will be instrumented by jacoco
407 jacocoReportClassesFile android.Path
408
Colin Cross66dbc0b2017-12-28 12:23:20 -0800409 // output file containing mapping of obfuscated names
410 proguardDictionary android.Path
411
Colin Cross331a1212018-08-15 20:40:52 -0700412 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700413 outputFile android.Path
414 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700415
Colin Cross635c3b02016-05-18 15:37:25 -0700416 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700417
Colin Cross635c3b02016-05-18 15:37:25 -0700418 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700419
Colin Cross2fe66872015-03-30 17:20:39 -0700420 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700421 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800422
423 // list of .java files and srcjars that was passed to javac
424 compiledJavaSrcs android.Paths
425 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800426
427 // list of extra progurad flag files
428 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900429
Colin Cross094054a2018-10-17 15:10:48 -0700430 // manifest file to use instead of properties.Manifest
431 overrideManifest android.OptionalPath
432
Artur Satayev9cf46692019-11-26 18:08:34 +0000433 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900434 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700435
Artur Satayev9cf46692019-11-26 18:08:34 +0000436 // list of plugins that this java module is exporting
437 exportedPluginJars android.Paths
438
439 // list of plugins that this java module is exporting
440 exportedPluginClasses []string
441
442 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800443 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700444 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800445
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800446 // expanded Jarjar_rules
447 expandJarjarRules android.Path
448
Vladimir Marko0975ee02019-04-02 10:29:55 +0100449 // list of additional targets for checkbuild
450 additionalCheckedModules android.Paths
451
Colin Cross988708c2019-05-06 14:04:11 -0700452 // Extra files generated by the module type to be added as java resources.
453 extraResources android.Paths
454
Colin Crossf24a22a2019-01-31 14:12:44 -0800455 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800456 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800457
458 // list of the xref extraction files
459 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700460}
461
Colin Cross41955e82019-05-29 14:40:35 -0700462func (j *Module) OutputFiles(tag string) (android.Paths, error) {
463 switch tag {
464 case "":
465 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700466 case ".jar":
467 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700468 case ".proguard_map":
469 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700470 default:
471 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
472 }
Colin Cross54250902017-12-05 09:28:08 -0800473}
474
Colin Cross41955e82019-05-29 14:40:35 -0700475var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800476
Colin Crossf506d872017-07-19 15:53:04 -0700477type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700478 HeaderJars() android.Paths
479 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700480 ResourceJars() android.Paths
481 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800482 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700483 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900484 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000485 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700486 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700487 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900488 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700489}
490
Jiyong Parkc678ad32018-04-10 13:07:10 +0900491type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900492 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
493 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900494}
495
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800496type xref interface {
497 XrefJavaFiles() android.Paths
498}
499
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800500func (j *Module) XrefJavaFiles() android.Paths {
501 return j.kytheFiles
502}
503
Colin Cross89536d42017-07-07 14:35:50 -0700504func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
505 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
506 android.InitDefaultableModule(module)
507}
508
Colin Crossbe1da472017-07-07 15:59:46 -0700509type dependencyTag struct {
510 blueprint.BaseDependencyTag
511 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700512}
513
Colin Crossa4f08812018-10-02 22:03:40 -0700514type jniDependencyTag struct {
515 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700516}
517
Jiyong Park8be103b2019-11-08 15:53:48 +0900518func IsJniDepTag(depTag blueprint.DependencyTag) bool {
519 _, ok := depTag.(*jniDependencyTag)
520 return ok
521}
522
Colin Crossbe1da472017-07-07 15:59:46 -0700523var (
Colin Cross4b964c02018-10-15 16:18:06 -0700524 staticLibTag = dependencyTag{name: "staticlib"}
525 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700526 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800527 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000528 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700529 bootClasspathTag = dependencyTag{name: "bootclasspath"}
530 systemModulesTag = dependencyTag{name: "system modules"}
531 frameworkResTag = dependencyTag{name: "framework-res"}
532 frameworkApkTag = dependencyTag{name: "framework-apk"}
533 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800534 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700535 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
536 certificateTag = dependencyTag{name: "certificate"}
537 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700538 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700539)
Colin Cross2fe66872015-03-30 17:20:39 -0700540
Jiyong Park83dc74b2020-01-14 18:38:44 +0900541func IsLibDepTag(depTag blueprint.DependencyTag) bool {
542 return depTag == libTag
543}
544
545func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
546 return depTag == staticLibTag
547}
548
Colin Crossfc3674a2017-09-18 17:41:52 -0700549type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700550 useModule, useFiles, useDefaultLibs, invalidVersion bool
551
Colin Cross6cef4812019-10-17 14:23:50 -0700552 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
553 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100554
555 // The default system modules to use. Will be an empty string if no system
556 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700557 systemModules string
558
Colin Cross6cef4812019-10-17 14:23:50 -0700559 // The modules that will be added ot the classpath when targeting 1.9 or higher
560 java9Classpath []string
561
Colin Crossa97c5d32018-03-28 14:58:31 -0700562 frameworkResModule string
563
Colin Cross86a60ae2018-05-29 14:44:55 -0700564 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700565 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100566
567 noStandardLibs, noFrameworksLibs bool
568}
569
570func (s sdkDep) hasStandardLibs() bool {
571 return !s.noStandardLibs
572}
573
574func (s sdkDep) hasFrameworkLibs() bool {
575 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700576}
577
Colin Crossa4f08812018-10-02 22:03:40 -0700578type jniLib struct {
579 name string
580 path android.Path
581 target android.Target
582}
583
Colin Cross0ea8ba82019-06-06 14:33:29 -0700584func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800585 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
586}
587
Colin Cross0ea8ba82019-06-06 14:33:29 -0700588func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800589 return j.shouldInstrument(ctx) &&
590 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
591 ctx.Config().UnbundledBuild())
592}
593
Jiyong Park6a927c42020-01-21 02:03:43 +0900594func (j *Module) sdkVersion() sdkSpec {
595 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700596}
597
Paul Duffine25c6442019-10-11 13:50:28 +0100598func (j *Module) systemModules() string {
599 return proptools.String(j.deviceProperties.System_modules)
600}
601
Jiyong Park6a927c42020-01-21 02:03:43 +0900602func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700603 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900604 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700605 }
606 return j.sdkVersion()
607}
608
Jiyong Park6a927c42020-01-21 02:03:43 +0900609func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700610 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900611 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700612 }
613 return j.sdkVersion()
614}
615
Jiyong Parkb02bb402019-12-03 00:43:57 +0900616func (j *Module) AvailableFor(what string) bool {
617 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
618 // Exception: for hostdex: true libraries, the platform variant is created
619 // even if it's not marked as available to platform. In that case, the platform
620 // variant is used only for the hostdex and not installed to the device.
621 return true
622 }
623 return j.ApexModuleBase.AvailableFor(what)
624}
625
Colin Crossbe1da472017-07-07 15:59:46 -0700626func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700627 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100628 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700629 if sdkDep.useDefaultLibs {
630 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
631 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
632 if sdkDep.hasFrameworkLibs() {
633 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700634 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700635 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700636 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100637 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700638 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700639 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
640 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
641 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
642 }
Colin Cross2fe66872015-03-30 17:20:39 -0700643 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700644
Nan Zhangb2b33de2018-02-23 11:18:47 -0800645 if ctx.ModuleName() == "android_stubs_current" ||
646 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700647 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700648 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800649 }
Colin Cross2fe66872015-03-30 17:20:39 -0700650 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700651
Inseob Kimac1e9862019-12-09 18:15:47 +0900652 syspropPublicStubs := syspropPublicStubs(ctx.Config())
653
654 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
655 // and redirects dependency to public stub depending on the link type.
656 rewriteSyspropLibs := func(libs []string, prop string) []string {
657 // make a copy
658 ret := android.CopyOf(libs)
659
660 for idx, lib := range libs {
661 stub, ok := syspropPublicStubs[lib]
662
663 if !ok {
664 continue
665 }
666
667 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900668 // only platform modules can use internal props
669 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900670 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900671 }
672 }
673
674 return ret
675 }
676
677 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
678 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700679
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700680 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000681 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800682
Colin Crossfe17f6f2019-03-28 19:30:56 -0700683 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700684 if j.hasSrcExt(".proto") {
685 protoDeps(ctx, &j.protoProperties)
686 }
Colin Cross93e85952017-08-15 13:34:18 -0700687
688 if j.hasSrcExt(".kt") {
689 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
690 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700691 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
692 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800693 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800694 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
695 }
Colin Cross93e85952017-08-15 13:34:18 -0700696 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800697
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000698 // Framework libraries need special handling in static coverage builds: they should not have
699 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
700 // the same jacoco classes coming from different bootclasspath jars.
701 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
702 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
703 j.properties.Instrument = true
704 }
705 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700706 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800707 }
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000708
709 // services depend on com.android.location.provider, but dependency in not registered in a Blueprint file
710 if ctx.ModuleName() == "services" {
711 ctx.AddDependency(ctx.Module(), dexpreopt.SystemServerForcedDepTag, "com.android.location.provider")
712 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700713}
714
715func hasSrcExt(srcs []string, ext string) bool {
716 for _, src := range srcs {
717 if filepath.Ext(src) == ext {
718 return true
719 }
720 }
721
722 return false
723}
724
725func (j *Module) hasSrcExt(ext string) bool {
726 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700727}
728
Colin Cross46c9b8b2017-06-22 16:51:17 -0700729func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700730 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700731
Colin Crossebe1a512017-11-14 13:12:14 -0800732 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
733 aidlIncludes = append(aidlIncludes,
734 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
735 aidlIncludes = append(aidlIncludes,
736 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700737
Colin Cross3047fa22019-04-18 10:56:44 -0700738 var flags []string
739 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700740
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700741 if aidlPreprocess.Valid() {
742 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700743 deps = append(deps, aidlPreprocess.Path())
744 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700745 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700746 }
747
Colin Cross3047fa22019-04-18 10:56:44 -0700748 if len(j.exportAidlIncludeDirs) > 0 {
749 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
750 }
751
752 if len(aidlIncludes) > 0 {
753 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
754 }
755
Colin Cross635c3b02016-05-18 15:37:25 -0700756 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800757 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700758 flags = append(flags, "-I"+src.String())
759 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700760
Martijn Coeneneab15642018-03-09 09:29:59 +0100761 if Bool(j.deviceProperties.Aidl.Generate_traces) {
762 flags = append(flags, "-t")
763 }
764
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100765 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
766 flags = append(flags, "--transaction_names")
767 }
768
Colin Cross3047fa22019-04-18 10:56:44 -0700769 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700770}
771
Colin Cross32f676a2017-09-06 13:41:06 -0700772type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800773 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700774 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800775 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700776 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800777 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700778 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700779 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700780 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700781 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800782 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700783 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700784 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700785 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700786 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800787 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800788
789 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700790}
Colin Cross2fe66872015-03-30 17:20:39 -0700791
Colin Cross54250902017-12-05 09:28:08 -0800792func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
793 for _, f := range dep.Srcs() {
794 if f.Ext() != ".jar" {
795 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
796 ctx.OtherModuleName(dep.(blueprint.Module)))
797 }
798 }
799}
800
Jiyong Park2d492942018-03-05 17:44:10 +0900801type linkType int
802
803const (
Jiyong Park50146e92020-01-30 18:00:15 +0900804 // TODO(jiyong) rename these for better readability. Make the allowed
805 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900806 javaCore linkType = iota
807 javaSdk
808 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900809 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900810 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +0900811 javaPlatform
812)
813
Jeongik Cha75b83b02019-11-01 15:28:00 +0900814type linkTypeContext interface {
815 android.Module
816 getLinkType(name string) (ret linkType, stubs bool)
817}
818
819func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700820 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700821 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900822 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
823 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100824 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900825 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900826 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900827 return javaCore, false
828 case name == "android_system_stubs_current":
829 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900830 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900831 return javaSystem, false
832 case name == "android_test_stubs_current":
833 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900834 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900835 return javaPlatform, false
836 case name == "android_stubs_current":
837 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900838 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900839 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900840 case name == "android_module_lib_stubs_current":
841 return javaModule, true
842 case ver.kind == sdkModule:
843 return javaModule, false
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900844 case name == "services-stubs":
845 return javaSystemServer, true
846 case ver.kind == sdkSystemServer:
847 return javaSystemServer, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900848 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900849 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900850 case !ver.valid():
851 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700852 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900853 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900854 }
855}
856
Jeongik Cha75b83b02019-11-01 15:28:00 +0900857func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700858 if ctx.Host() {
859 return
860 }
861
Jeongik Cha75b83b02019-11-01 15:28:00 +0900862 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900863 if stubs {
864 return
865 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900866 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900867 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."
868
869 switch myLinkType {
870 case javaCore:
871 if otherLinkType != javaCore {
872 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 +0900873 ctx.OtherModuleName(to))
874 }
Jiyong Park2d492942018-03-05 17:44:10 +0900875 break
876 case javaSdk:
877 if otherLinkType != javaCore && otherLinkType != javaSdk {
878 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
879 ctx.OtherModuleName(to))
880 }
881 break
882 case javaSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900883 if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer {
Jiyong Park2d492942018-03-05 17:44:10 +0900884 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
885 ctx.OtherModuleName(to))
886 }
887 break
Jiyong Park50146e92020-01-30 18:00:15 +0900888 case javaModule:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900889 if otherLinkType == javaPlatform || otherLinkType == javaSystemServer {
Jiyong Park50146e92020-01-30 18:00:15 +0900890 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
891 ctx.OtherModuleName(to))
892 }
893 break
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900894 case javaSystemServer:
895 if otherLinkType == javaPlatform {
896 ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage,
897 ctx.OtherModuleName(to))
898 }
899 break
Jiyong Park2d492942018-03-05 17:44:10 +0900900 case javaPlatform:
901 // no restriction on link-type
902 break
Jiyong Park750e5572018-01-31 00:20:13 +0900903 }
904}
905
Colin Cross32f676a2017-09-06 13:41:06 -0700906func (j *Module) collectDeps(ctx android.ModuleContext) deps {
907 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700908
Colin Cross300f0382018-03-06 13:11:51 -0800909 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700910 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800911 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700912 ctx.AddMissingDependencies(sdkDep.bootclasspath)
913 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800914 } else if sdkDep.useFiles {
915 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700916 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700917 deps.aidlPreprocess = sdkDep.aidl
918 } else {
919 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800920 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700921 }
922
Colin Crossd11fcda2017-10-23 17:59:01 -0700923 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700924 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700925 tag := ctx.OtherModuleDependencyTag(module)
926
Colin Crossa4f08812018-10-02 22:03:40 -0700927 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700928 // Handled by AndroidApp.collectAppDeps
929 return
930 }
931 if tag == certificateTag {
932 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700933 return
934 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900935 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900936 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900937 if to, ok := module.(linkTypeContext); ok {
938 switch tag {
939 case bootClasspathTag, libTag, staticLibTag:
940 checkLinkType(ctx, j, to, tag.(dependencyTag))
941 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700942 }
Jiyong Park750e5572018-01-31 00:20:13 +0900943 }
Colin Cross54250902017-12-05 09:28:08 -0800944 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800945 case SdkLibraryDependency:
946 switch tag {
947 case libTag:
948 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
949 // names of sdk libs that are directly depended are exported
950 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700951 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800952 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
953 }
Colin Cross54250902017-12-05 09:28:08 -0800954 case Dependency:
955 switch tag {
956 case bootClasspathTag:
957 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700958 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800959 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
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 Cross6cef4812019-10-17 14:23:50 -0700965 case java9LibTag:
966 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800967 case staticLibTag:
968 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
969 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
970 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700971 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900972 // sdk lib names from dependencies are re-exported
973 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700974 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000975 pluginJars, pluginClasses := dep.ExportedPlugins()
976 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800977 case pluginTag:
978 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800979 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000980 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
981 } else {
982 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800983 }
984 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
985 } else {
986 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
987 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000988 case exportedPluginTag:
989 if plugin, ok := dep.(*Plugin); ok {
990 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
991 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
992 }
993 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
994 if plugin.pluginProperties.Processor_class != nil {
995 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
996 }
997 } else {
998 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
999 }
Nan Zhangb2b33de2018-02-23 11:18:47 -08001000 case frameworkApkTag:
1001 if ctx.ModuleName() == "android_stubs_current" ||
1002 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -07001003 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -08001004 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
1005 // resource files out of there for aapt.
1006 //
1007 // Normally the package rule runs aapt, which includes the resource,
1008 // but we're not running that in our package rule so just copy in the
1009 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -07001010 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001011 }
Colin Cross54250902017-12-05 09:28:08 -08001012 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -07001013 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -08001014 case kotlinAnnotationsTag:
1015 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -08001016 }
1017
Colin Cross54250902017-12-05 09:28:08 -08001018 case android.SourceFileProducer:
1019 switch tag {
1020 case libTag:
1021 checkProducesJars(ctx, dep)
1022 deps.classpath = append(deps.classpath, dep.Srcs()...)
1023 case staticLibTag:
1024 checkProducesJars(ctx, dep)
1025 deps.classpath = append(deps.classpath, dep.Srcs()...)
1026 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
1027 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -08001028 }
1029 default:
Colin Crossec7a0422017-07-07 14:47:12 -07001030 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001031 case bootClasspathTag:
1032 // If a system modules dependency has been added to the bootclasspath
1033 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +00001034 sm := module.(SystemModulesProvider)
1035 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +01001036
Colin Cross1369cdb2017-09-29 17:58:17 -07001037 case systemModulesTag:
1038 if deps.systemModules != nil {
1039 panic("Found two system module dependencies")
1040 }
Paul Duffin83a2d962019-11-19 19:44:10 +00001041 sm := module.(SystemModulesProvider)
1042 outputDir, outputDeps := sm.OutputDirAndDeps()
1043 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001044 }
Colin Crossec7a0422017-07-07 14:47:12 -07001045 }
Colin Cross2fe66872015-03-30 17:20:39 -07001046 })
1047
Jiyong Park1be96912018-05-28 18:02:19 +09001048 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1049
Colin Cross32f676a2017-09-06 13:41:06 -07001050 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001051}
1052
Artur Satayev9cf46692019-11-26 18:08:34 +00001053func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1054 deps.processorPath = append(deps.processorPath, pluginJars...)
1055 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1056}
1057
Colin Cross1e743852019-10-28 11:37:20 -07001058func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001059 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001060 if err != nil {
1061 ctx.PropertyErrorf("sdk_version", "%s", err)
1062 }
Nan Zhang357466b2018-04-17 17:38:36 -07001063 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001064 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001065 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001066 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001067 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001068 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001069 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1070 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001071 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001072 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001073 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001074 }
Nan Zhang357466b2018-04-17 17:38:36 -07001075}
1076
Colin Cross1e743852019-10-28 11:37:20 -07001077type javaVersion int
1078
1079const (
1080 JAVA_VERSION_UNSUPPORTED = 0
1081 JAVA_VERSION_6 = 6
1082 JAVA_VERSION_7 = 7
1083 JAVA_VERSION_8 = 8
1084 JAVA_VERSION_9 = 9
1085)
1086
1087func (v javaVersion) String() string {
1088 switch v {
1089 case JAVA_VERSION_6:
1090 return "1.6"
1091 case JAVA_VERSION_7:
1092 return "1.7"
1093 case JAVA_VERSION_8:
1094 return "1.8"
1095 case JAVA_VERSION_9:
1096 return "1.9"
1097 default:
1098 return "unsupported"
1099 }
1100}
1101
1102// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1103func (v javaVersion) usesJavaModules() bool {
1104 return v >= 9
1105}
1106
1107func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001108 switch javaVersion {
1109 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001110 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001111 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001112 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001113 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001114 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001115 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001116 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001117 case "10", "11":
1118 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001119 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001120 default:
1121 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001122 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001123 }
1124}
1125
Nan Zhanged19fc32017-10-19 13:06:22 -07001126func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001127
Colin Crossf03c82b2015-04-13 13:53:40 -07001128 var flags javaBuilderFlags
1129
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001130 // javaVersion flag.
1131 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1132
Nan Zhanged19fc32017-10-19 13:06:22 -07001133 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001134 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001135 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001136 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001137 }
Colin Cross6510f912017-11-29 00:27:14 -08001138 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001139 // Override the -g flag passed globally to remove local variable debug info to reduce
1140 // disk and memory usage.
1141 javacFlags = append(javacFlags, "-g:source,lines")
1142 }
Colin Crossc228a702019-11-06 16:18:05 -08001143 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001144
Colin Cross66548102018-06-19 22:47:35 -07001145 if ctx.Config().RunErrorProne() {
1146 if config.ErrorProneClasspath == nil {
1147 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1148 }
1149
1150 errorProneFlags := []string{
1151 "-Xplugin:ErrorProne",
1152 "${config.ErrorProneChecks}",
1153 }
1154 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1155
1156 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1157 "'" + strings.Join(errorProneFlags, " ") + "'"
1158 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001159 }
1160
Nan Zhanged19fc32017-10-19 13:06:22 -07001161 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001162 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1163 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001164 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001165 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001166
Colin Crossbe9cdb82019-01-21 21:37:16 -08001167 flags.processor = strings.Join(deps.processorClasses, ",")
1168
Colin Cross1e743852019-10-28 11:37:20 -07001169 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1170 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001171 // Give host-side tools a version of OpenJDK's standard libraries
1172 // close to what they're targeting. As of Dec 2017, AOSP is only
1173 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1174 //
1175 // When building with OpenJDK 8, the following should have no
1176 // effect since those jars would be available by default.
1177 //
1178 // When building with OpenJDK 9 but targeting a version < 1.8,
1179 // putting them on the bootclasspath means that:
1180 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1181 // b) references to existing APIs are not reinterpreted in an
1182 // OpenJDK 9-specific way, eg. calls to subclasses of
1183 // java.nio.Buffer as in http://b/70862583
1184 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1185 flags.bootClasspath = append(flags.bootClasspath,
1186 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1187 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001188 if Bool(j.properties.Use_tools_jar) {
1189 flags.bootClasspath = append(flags.bootClasspath,
1190 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1191 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001192 }
1193
Colin Cross1e743852019-10-28 11:37:20 -07001194 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001195 // Manually specify build directory in case it is not under the repo root.
1196 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1197 // just adding a symlink under the root doesn't help.)
1198 patchPaths := ".:" + ctx.Config().BuildDir()
1199 classPath := flags.classpath.FormJavaClassPath("")
1200 if classPath != "" {
1201 patchPaths += ":" + classPath
1202 }
1203 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001204 }
1205
Nan Zhanged19fc32017-10-19 13:06:22 -07001206 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001207 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001208
Nan Zhanged19fc32017-10-19 13:06:22 -07001209 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001210 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001211
Colin Cross81440082018-08-15 20:21:55 -07001212 if len(javacFlags) > 0 {
1213 // optimization.
1214 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1215 flags.javacFlags = "$javacFlags"
1216 }
1217
Nan Zhanged19fc32017-10-19 13:06:22 -07001218 return flags
1219}
Colin Crossc0b06f12015-04-08 13:03:43 -07001220
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001221func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001222 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001223
1224 deps := j.collectDeps(ctx)
1225 flags := j.collectBuilderFlags(ctx, deps)
1226
Colin Cross1e743852019-10-28 11:37:20 -07001227 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001228 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1229 }
Colin Cross8a497952019-03-05 22:25:09 -08001230 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001231 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001232 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001233 }
1234
Colin Crossaf050172017-11-15 23:01:59 -08001235 srcFiles = j.genSources(ctx, srcFiles, flags)
1236
1237 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001238 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001239 if aaptSrcJar != nil {
1240 srcJars = append(srcJars, aaptSrcJar)
1241 }
Colin Crossb7a63242015-04-16 14:09:14 -07001242
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001243 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001244 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001245 }
1246
Colin Cross1ee23172017-10-18 14:44:18 -07001247 jarName := ctx.ModuleName() + ".jar"
1248
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001249 javaSrcFiles := srcFiles.FilterByExt(".java")
1250 var uniqueSrcFiles android.Paths
1251 set := make(map[string]bool)
1252 for _, v := range javaSrcFiles {
1253 if _, found := set[v.String()]; !found {
1254 set[v.String()] = true
1255 uniqueSrcFiles = append(uniqueSrcFiles, v)
1256 }
1257 }
1258
patricktu242faad2019-09-24 15:41:30 +08001259 // Collect .java files for AIDEGen
1260 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1261
Colin Cross55f63ea2018-08-27 12:37:09 -07001262 var kotlinJars android.Paths
1263
Colin Cross93e85952017-08-15 13:34:18 -07001264 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001265 // user defined kotlin flags.
1266 kotlincFlags := j.properties.Kotlincflags
1267 CheckKotlincFlags(ctx, kotlincFlags)
1268
Colin Cross93e85952017-08-15 13:34:18 -07001269 // If there are kotlin files, compile them first but pass all the kotlin and java files
1270 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1271 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001272 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001273 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001274 kotlincFlags = append(kotlincFlags, "-no-jdk")
1275 }
1276 if len(kotlincFlags) > 0 {
1277 // optimization.
1278 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1279 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001280 }
1281
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001282 var kotlinSrcFiles android.Paths
1283 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1284 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1285
patricktu242faad2019-09-24 15:41:30 +08001286 // Collect .kt files for AIDEGen
1287 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1288
Colin Crossafbb1732019-01-17 15:42:52 -08001289 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1290 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1291
1292 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1293 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1294
1295 if len(flags.processorPath) > 0 {
1296 // Use kapt for annotation processing
1297 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1298 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1299 srcJars = append(srcJars, kaptSrcJar)
1300 // Disable annotation processing in javac, it's already been handled by kapt
1301 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001302 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001303 }
Colin Cross93e85952017-08-15 13:34:18 -07001304
Colin Cross1ee23172017-10-18 14:44:18 -07001305 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001306 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001307 if ctx.Failed() {
1308 return
1309 }
1310
1311 // Make javac rule depend on the kotlinc rule
1312 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001313
Colin Cross93e85952017-08-15 13:34:18 -07001314 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001315 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001316 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001317 }
1318
Colin Cross55f63ea2018-08-27 12:37:09 -07001319 jars := append(android.Paths(nil), kotlinJars...)
1320
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001321 // Store the list of .java files that was passed to javac
1322 j.compiledJavaSrcs = uniqueSrcFiles
1323 j.compiledSrcJars = srcJars
1324
Nan Zhang61eaedb2017-11-02 13:28:15 -07001325 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001326 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001327 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1328 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001329 // Formerly, there was a check here that prevented annotation processors
1330 // from being used when sharding was enabled, as some annotation processors
1331 // do not function correctly in sharded environments. It was removed to
1332 // allow for the use of annotation processors that do function correctly
1333 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001334 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001335 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001336 if ctx.Failed() {
1337 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001338 }
1339 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001340 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001341 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001342 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001343 // If error-prone is enabled, add an additional rule to compile the java files into
1344 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001345 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001346 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1347 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001348 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001349 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001350 extraJarDeps = append(extraJarDeps, errorprone)
1351 }
1352
Nan Zhang61eaedb2017-11-02 13:28:15 -07001353 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001354 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001355 shardSize := int(*(j.properties.Javac_shard_size))
1356 var shardSrcs []android.Paths
1357 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001358 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001359 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001360 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1361 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001362 jars = append(jars, classes)
1363 }
1364 }
1365 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001366 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1367 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001368 jars = append(jars, classes)
1369 }
1370 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001371 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001372 jars = append(jars, classes)
1373 }
Colin Crossd6891432017-09-27 17:39:56 -07001374 if ctx.Failed() {
1375 return
1376 }
Colin Cross2fe66872015-03-30 17:20:39 -07001377 }
1378
Colin Cross0c4ce212019-05-03 15:28:19 -07001379 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1380
1381 var includeSrcJar android.WritablePath
1382 if Bool(j.properties.Include_srcs) {
1383 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1384 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1385 }
1386
Colin Crosscedd4762018-09-13 11:26:19 -07001387 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1388 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001389 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001390 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001391
1392 var resArgs []string
1393 var resDeps android.Paths
1394
1395 resArgs = append(resArgs, dirArgs...)
1396 resDeps = append(resDeps, dirDeps...)
1397
1398 resArgs = append(resArgs, fileArgs...)
1399 resDeps = append(resDeps, fileDeps...)
1400
Colin Cross988708c2019-05-06 14:04:11 -07001401 resArgs = append(resArgs, extraArgs...)
1402 resDeps = append(resDeps, extraDeps...)
1403
Colin Cross40a36712017-09-27 17:41:35 -07001404 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001405 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001406 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001407 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001408 if ctx.Failed() {
1409 return
1410 }
1411 }
1412
Colin Cross0c4ce212019-05-03 15:28:19 -07001413 var resourceJars android.Paths
1414 if j.resourceJar != nil {
1415 resourceJars = append(resourceJars, j.resourceJar)
1416 }
1417 if Bool(j.properties.Include_srcs) {
1418 resourceJars = append(resourceJars, includeSrcJar)
1419 }
1420 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001421
Colin Cross0c4ce212019-05-03 15:28:19 -07001422 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001423 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001424 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001425 false, nil, nil)
1426 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001427 } else if len(resourceJars) == 1 {
1428 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001429 }
1430
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001431 if len(deps.staticJars) > 0 {
1432 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001433 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001434
Colin Cross094054a2018-10-17 15:10:48 -07001435 manifest := j.overrideManifest
1436 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001437 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001438 }
Colin Cross635acc92017-09-12 22:50:46 -07001439
Colin Cross8a497952019-03-05 22:25:09 -08001440 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001441 if len(services) > 0 {
1442 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1443 var zipargs []string
1444 for _, file := range services {
1445 serviceFile := file.String()
1446 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1447 }
1448 ctx.Build(pctx, android.BuildParams{
1449 Rule: zip,
1450 Output: servicesJar,
1451 Implicits: services,
1452 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001453 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001454 },
1455 })
1456 jars = append(jars, servicesJar)
1457 }
1458
Colin Cross0a6e0072017-08-30 14:24:55 -07001459 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001460 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001461 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001462
1463 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001464 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1465 // Optimization: skip the combine step if there is nothing to do
1466 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1467 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1468 // any if len(jars) == 1.
1469 outputFile = moduleOutPath
1470 } else {
1471 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1472 ctx.Build(pctx, android.BuildParams{
1473 Rule: android.Cp,
1474 Input: jars[0],
1475 Output: combinedJar,
1476 })
1477 outputFile = combinedJar
1478 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001479 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001480 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001481 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001482 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001483 outputFile = combinedJar
1484 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001485
Colin Cross331a1212018-08-15 20:40:52 -07001486 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001487 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001488 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001489 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001490 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001491 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001492
1493 // jarjar resource jar if necessary
1494 if j.resourceJar != nil {
1495 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001496 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001497 j.resourceJar = resourceJarJarFile
1498 }
1499
Colin Cross0a6e0072017-08-30 14:24:55 -07001500 if ctx.Failed() {
1501 return
1502 }
1503 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001504
1505 // Check package restrictions if necessary.
1506 if len(j.properties.Permitted_packages) > 0 {
1507 // Check packages and copy to package-checked file.
1508 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1509 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1510 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1511
1512 if ctx.Failed() {
1513 return
1514 }
1515 }
1516
Nan Zhanged19fc32017-10-19 13:06:22 -07001517 j.implementationJarFile = outputFile
1518 if j.headerJarFile == nil {
1519 j.headerJarFile = j.implementationJarFile
1520 }
Colin Cross2fe66872015-03-30 17:20:39 -07001521
Jiyong Park00cae1c2020-02-18 12:50:44 +00001522 // Force enable the instrumentation for java code that is built for APEXes
1523 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1524 j.properties.Instrument = true
1525 }
1526
Colin Cross3144dfc2018-01-03 15:06:47 -08001527 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001528 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1529 }
1530
Colin Cross331a1212018-08-15 20:40:52 -07001531 // merge implementation jar with resources if necessary
1532 implementationAndResourcesJar := outputFile
1533 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001534 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001535 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001536 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001537 false, nil, nil)
1538 implementationAndResourcesJar = combinedJar
1539 }
1540
1541 j.implementationAndResourcesJar = implementationAndResourcesJar
1542
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001543 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1544 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1545 if j.deviceProperties.Compile_dex == nil {
1546 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1547 }
1548 if j.deviceProperties.Hostdex == nil {
1549 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1550 }
1551 }
1552
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001553 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001554 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001555 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001556 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001557 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001558 if ctx.Failed() {
1559 return
1560 }
Colin Cross331a1212018-08-15 20:40:52 -07001561
Jiyong Park09cb6292019-07-15 15:29:23 +09001562 // Hidden API CSV generation and dex encoding
1563 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1564 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001565
Colin Cross331a1212018-08-15 20:40:52 -07001566 // merge dex jar with resources if necessary
1567 if j.resourceJar != nil {
1568 jars := android.Paths{dexOutputFile, j.resourceJar}
1569 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1570 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1571 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001572 if j.deviceProperties.UncompressDex {
1573 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1574 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1575 dexOutputFile = combinedAlignedJar
1576 } else {
1577 dexOutputFile = combinedJar
1578 }
Colin Cross331a1212018-08-15 20:40:52 -07001579 }
1580
1581 j.dexJarFile = dexOutputFile
1582
Colin Cross8faf8fc2019-01-16 15:15:52 -08001583 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001584 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1585
1586 j.maybeStrippedDexJarFile = dexOutputFile
1587
Colin Cross3063b782018-08-15 11:19:12 -07001588 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001589
1590 if ctx.Failed() {
1591 return
1592 }
Colin Cross331a1212018-08-15 20:40:52 -07001593 } else {
1594 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001595 }
Colin Cross331a1212018-08-15 20:40:52 -07001596
Colin Crossb7a63242015-04-16 14:09:14 -07001597 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001598
1599 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1600 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001601}
1602
Colin Cross3b706fd2019-09-05 16:44:18 -07001603func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1604 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1605
1606 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1607 if idx >= 0 {
1608 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1609 jarName += strconv.Itoa(idx)
1610 }
1611
1612 classes := android.PathForModuleOut(ctx, "javac", jarName)
1613 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1614
1615 if ctx.Config().EmitXrefRules() {
1616 extractionFile := android.PathForModuleOut(ctx, kzipName)
1617 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1618 j.kytheFiles = append(j.kytheFiles, extractionFile)
1619 }
1620
1621 return classes
1622}
1623
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001624// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1625// since some of these flags may be used internally.
1626func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1627 for _, flag := range flags {
1628 flag = strings.TrimSpace(flag)
1629
1630 if !strings.HasPrefix(flag, "-") {
1631 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1632 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1633 ctx.PropertyErrorf("kotlincflags",
1634 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1635 } else if inList(flag, config.KotlincIllegalFlags) {
1636 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1637 } else if flag == "-include-runtime" {
1638 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1639 } else {
1640 args := strings.Split(flag, " ")
1641 if args[0] == "-kotlin-home" {
1642 ctx.PropertyErrorf("kotlincflags",
1643 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1644 }
1645 }
1646 }
1647}
1648
Colin Cross8eadbf02017-10-24 17:46:00 -07001649func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001650 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001651
1652 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001653 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001654 // Compile java sources into turbine.jar.
1655 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1656 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1657 if ctx.Failed() {
1658 return nil
1659 }
1660 jars = append(jars, turbineJar)
1661 }
1662
Colin Cross55f63ea2018-08-27 12:37:09 -07001663 jars = append(jars, extraJars...)
1664
Nan Zhanged19fc32017-10-19 13:06:22 -07001665 // Combine any static header libraries into classes-header.jar. If there is only
1666 // one input jar this step will be skipped.
1667 var headerJar android.Path
1668 jars = append(jars, deps.staticHeaderJars...)
1669
Colin Cross5c6ecc12017-10-23 18:12:27 -07001670 // we cannot skip the combine step for now if there is only one jar
1671 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1672 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001673 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001674 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001675 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001676
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001677 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001678 // Transform classes.jar into classes-jarjar.jar
1679 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001680 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001681 headerJar = jarjarFile
1682 if ctx.Failed() {
1683 return nil
1684 }
1685 }
1686
1687 return headerJar
1688}
1689
Colin Crosscb933592017-11-22 13:49:43 -08001690func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001691 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001692
Colin Cross7a3139e2017-12-19 13:57:50 -08001693 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001694
Colin Cross84c38822018-01-03 15:59:46 -08001695 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001696 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1697
1698 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1699
1700 j.jacocoReportClassesFile = jacocoReportClassesFile
1701
1702 return instrumentedJar
1703}
1704
albaltai36ff7dc2018-12-25 14:35:23 +08001705var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001706
Nan Zhanged19fc32017-10-19 13:06:22 -07001707func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001708 if j.headerJarFile == nil {
1709 return nil
1710 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001711 return android.Paths{j.headerJarFile}
1712}
1713
1714func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001715 if j.implementationJarFile == nil {
1716 return nil
1717 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001718 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001719}
1720
Colin Crossf24a22a2019-01-31 14:12:44 -08001721func (j *Module) DexJar() android.Path {
1722 return j.dexJarFile
1723}
1724
Colin Cross331a1212018-08-15 20:40:52 -07001725func (j *Module) ResourceJars() android.Paths {
1726 if j.resourceJar == nil {
1727 return nil
1728 }
1729 return android.Paths{j.resourceJar}
1730}
1731
1732func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001733 if j.implementationAndResourcesJar == nil {
1734 return nil
1735 }
Colin Cross331a1212018-08-15 20:40:52 -07001736 return android.Paths{j.implementationAndResourcesJar}
1737}
1738
Colin Cross46c9b8b2017-06-22 16:51:17 -07001739func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001740 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001741 return j.exportAidlIncludeDirs
1742}
1743
Jiyong Park1be96912018-05-28 18:02:19 +09001744func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001745 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001746 return j.exportedSdkLibs
1747}
1748
Artur Satayev9cf46692019-11-26 18:08:34 +00001749func (j *Module) ExportedPlugins() (android.Paths, []string) {
1750 return j.exportedPluginJars, j.exportedPluginClasses
1751}
1752
Colin Cross0c4ce212019-05-03 15:28:19 -07001753func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1754 return j.srcJarArgs, j.srcJarDeps
1755}
1756
Colin Cross46c9b8b2017-06-22 16:51:17 -07001757var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001758
Colin Cross46c9b8b2017-06-22 16:51:17 -07001759func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001760 return j.logtagsSrcs
1761}
1762
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001763// Collect information for opening IDE project files in java/jdeps.go.
1764func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1765 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1766 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001767 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001768 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001769 if j.expandJarjarRules != nil {
1770 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001771 }
1772}
1773
1774func (j *Module) CompilerDeps() []string {
1775 jdeps := []string{}
1776 jdeps = append(jdeps, j.properties.Libs...)
1777 jdeps = append(jdeps, j.properties.Static_libs...)
1778 return jdeps
1779}
1780
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001781func (j *Module) hasCode(ctx android.ModuleContext) bool {
1782 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1783 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1784}
1785
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001786func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1787 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001788 // Dependencies other than the static linkage are all considered crossing APEX boundary
1789 // Also, a dependency to an sdk member is also considered as such. This is required because
1790 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1791 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001792}
1793
Jiyong Park0b238752019-10-29 11:23:10 +09001794func (j *Module) Stem() string {
1795 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1796}
1797
Jiyong Park618922e2020-01-08 13:35:43 +09001798func (j *Module) JacocoReportClassesFile() android.Path {
1799 return j.jacocoReportClassesFile
1800}
1801
Martin Stjernholm6d415272020-01-31 17:10:36 +00001802func (j *Module) IsInstallable() bool {
1803 return Bool(j.properties.Installable)
1804}
1805
Colin Cross2fe66872015-03-30 17:20:39 -07001806//
1807// Java libraries (.jar file)
1808//
1809
Colin Crossf506d872017-07-19 15:53:04 -07001810type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001811 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001812
1813 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001814}
1815
Colin Cross42be7612019-02-21 18:12:14 -08001816func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001817 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1818 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1819 return true
1820 }
1821
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001822 // Store uncompressed (and do not strip) dex files from boot class path jars.
1823 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1824 return true
1825 }
1826
1827 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001828 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001829 return true
1830 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001831 if ctx.Config().UncompressPrivAppDex() &&
1832 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1833 return true
1834 }
1835
Colin Cross2fc72f62018-12-21 12:59:54 -08001836 return false
1837}
1838
Colin Crossf506d872017-07-19 15:53:04 -07001839func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001840 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001841 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001842 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001843 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001844 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001845 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001846
Jiyong Park7f7766d2019-07-25 22:02:35 +09001847 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1848 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001849 var extraInstallDeps android.Paths
1850 if j.InstallMixin != nil {
1851 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1852 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001853 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001854 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001855 }
Colin Crossb7a63242015-04-16 14:09:14 -07001856}
1857
Colin Crossf506d872017-07-19 15:53:04 -07001858func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001859 j.deps(ctx)
1860}
1861
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001862const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001863 aidlIncludeDir = "aidl"
1864 javaDir = "java"
1865 jarFileSuffix = ".jar"
1866 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001867)
1868
Paul Duffina0dbf432019-12-05 11:25:53 +00001869// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001870func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1871 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1872}
1873
1874func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1875 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001876}
1877
Paul Duffin13879572019-11-28 14:31:38 +00001878type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001879 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001880}
1881
1882func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1883 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1884}
1885
1886func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1887 _, ok := module.(*Library)
1888 return ok
1889}
1890
Paul Duffina0dbf432019-12-05 11:25:53 +00001891func (mt *librarySdkMemberType) buildSnapshot(
1892 sdkModuleContext android.ModuleContext,
1893 builder android.SnapshotBuilder,
1894 member android.SdkMember,
1895 jarToExportGetter func(j *Library) android.Path) {
1896
Paul Duffin13879572019-11-28 14:31:38 +00001897 variants := member.Variants()
1898 if len(variants) != 1 {
1899 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1900 for _, variant := range variants {
1901 sdkModuleContext.ModuleErrorf(" %q", variant)
1902 }
1903 }
1904 variant := variants[0]
1905 j := variant.(*Library)
1906
Paul Duffina0dbf432019-12-05 11:25:53 +00001907 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001908 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001909 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001910
1911 for _, dir := range j.AidlIncludeDirs() {
1912 // TODO(jiyong): copy parcelable declarations only
1913 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1914 for _, file := range aidlFiles {
1915 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1916 }
1917 }
1918
Paul Duffin9d8d6092019-12-05 18:19:29 +00001919 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001920 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001921}
1922
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001923var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1924 librarySdkMemberType{
1925 android.SdkMemberTypeBase{
1926 PropertyName: "java_header_libs",
1927 SupportsSdk: true,
1928 },
1929 },
1930}
1931
Paul Duffina0dbf432019-12-05 11:25:53 +00001932type headerLibrarySdkMemberType struct {
1933 librarySdkMemberType
1934}
1935
1936func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1937 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1938 headerJars := j.HeaderJars()
1939 if len(headerJars) != 1 {
1940 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1941 }
1942
1943 return headerJars[0]
1944 })
1945}
1946
Paul Duffina0dbf432019-12-05 11:25:53 +00001947type implLibrarySdkMemberType struct {
1948 librarySdkMemberType
1949}
1950
1951func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1952 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1953 implementationJars := j.ImplementationJars()
1954 if len(implementationJars) != 1 {
1955 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1956 }
1957
1958 return implementationJars[0]
1959 })
1960}
1961
Colin Cross1b16b0e2019-02-12 14:41:32 -08001962// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1963//
1964// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1965// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1966// as a `static_libs` dependency of another module.
1967//
1968// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1969// a device.
1970//
1971// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1972// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001973func LibraryFactory() android.Module {
1974 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001975
Colin Cross9ae1b922018-06-26 17:59:05 -07001976 module.AddProperties(
1977 &module.Module.properties,
1978 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001979 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001980 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001981
Jiyong Park7f7766d2019-07-25 22:02:35 +09001982 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001983 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001984 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001985 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001986}
1987
Colin Cross1b16b0e2019-02-12 14:41:32 -08001988// java_library_static is an obsolete alias for java_library.
1989func LibraryStaticFactory() android.Module {
1990 return LibraryFactory()
1991}
1992
1993// java_library_host builds and links sources into a `.jar` file for the host.
1994//
1995// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1996// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001997func LibraryHostFactory() android.Module {
1998 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001999
Colin Cross6af17aa2017-09-20 12:59:05 -07002000 module.AddProperties(
2001 &module.Module.properties,
2002 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07002003
Colin Cross9ae1b922018-06-26 17:59:05 -07002004 module.Module.properties.Installable = proptools.BoolPtr(true)
2005
Jiyong Park7f7766d2019-07-25 22:02:35 +09002006 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002007 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002008 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002009}
2010
2011//
Colin Crossb628ea52018-08-14 16:42:33 -07002012// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002013//
2014
2015type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002016 // list of compatibility suites (for example "cts", "vts") that the module should be
2017 // installed into.
2018 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002019
2020 // the name of the test configuration (for example "AndroidTest.xml") that should be
2021 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002022 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002023
Jack He33338892018-09-19 02:21:28 -07002024 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2025 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002026 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002027
Colin Crossd96ca352018-08-10 16:06:24 -07002028 // list of files or filegroup modules that provide data that should be installed alongside
2029 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002030 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002031
2032 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2033 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2034 // explicitly.
2035 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002036}
2037
Paul Duffin42df1442019-03-20 12:45:53 +00002038type testHelperLibraryProperties struct {
2039 // list of compatibility suites (for example "cts", "vts") that the module should be
2040 // installed into.
2041 Test_suites []string `android:"arch_variant"`
2042}
2043
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002044type prebuiltTestProperties struct {
2045 // list of compatibility suites (for example "cts", "vts") that the module should be
2046 // installed into.
2047 Test_suites []string `android:"arch_variant"`
2048
2049 // the name of the test configuration (for example "AndroidTest.xml") that should be
2050 // installed with the module.
2051 Test_config *string `android:"path,arch_variant"`
2052}
2053
Colin Cross05638fc2018-04-09 18:40:24 -07002054type Test struct {
2055 Library
2056
2057 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002058
2059 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002060 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002061}
2062
Paul Duffin42df1442019-03-20 12:45:53 +00002063type TestHelperLibrary struct {
2064 Library
2065
2066 testHelperLibraryProperties testHelperLibraryProperties
2067}
2068
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002069type JavaTestImport struct {
2070 Import
2071
2072 prebuiltTestProperties prebuiltTestProperties
2073
2074 testConfig android.Path
2075}
2076
Colin Cross303e21f2018-08-07 16:49:25 -07002077func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002078 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2079 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002080 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002081
2082 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002083}
2084
Paul Duffin42df1442019-03-20 12:45:53 +00002085func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2086 j.Library.GenerateAndroidBuildActions(ctx)
2087}
2088
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002089func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2090 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2091 j.prebuiltTestProperties.Test_suites, nil)
2092
2093 j.Import.GenerateAndroidBuildActions(ctx)
2094}
2095
2096type testSdkMemberType struct {
2097 android.SdkMemberTypeBase
2098}
2099
2100func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2101 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2102}
2103
2104func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2105 _, ok := module.(*Test)
2106 return ok
2107}
2108
2109func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2110 variants := member.Variants()
2111 if len(variants) != 1 {
2112 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2113 for _, variant := range variants {
2114 sdkModuleContext.ModuleErrorf(" %q", variant)
2115 }
2116 }
2117 variant := variants[0]
2118 j := variant.(*Test)
2119
2120 implementationJars := j.ImplementationJars()
2121 if len(implementationJars) != 1 {
2122 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2123 }
2124
2125 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2126 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2127
2128 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2129 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2130
2131 module := builder.AddPrebuiltModule(member, "java_test_import")
2132 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2133 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2134}
2135
Colin Cross1b16b0e2019-02-12 14:41:32 -08002136// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2137// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2138//
2139// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2140// compiled against the device bootclasspath.
2141//
2142// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2143// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002144func TestFactory() android.Module {
2145 module := &Test{}
2146
2147 module.AddProperties(
2148 &module.Module.properties,
2149 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002150 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002151 &module.Module.protoProperties,
2152 &module.testProperties)
2153
Colin Cross9ae1b922018-06-26 17:59:05 -07002154 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002155 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002156
Colin Cross05638fc2018-04-09 18:40:24 -07002157 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002158 return module
2159}
2160
Paul Duffin42df1442019-03-20 12:45:53 +00002161// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2162func TestHelperLibraryFactory() android.Module {
2163 module := &TestHelperLibrary{}
2164
2165 module.AddProperties(
2166 &module.Module.properties,
2167 &module.Module.deviceProperties,
2168 &module.Module.dexpreoptProperties,
2169 &module.Module.protoProperties,
2170 &module.testHelperLibraryProperties)
2171
Colin Cross9a4abed2019-04-24 13:19:28 -07002172 module.Module.properties.Installable = proptools.BoolPtr(true)
2173 module.Module.dexpreopter.isTest = true
2174
Paul Duffin42df1442019-03-20 12:45:53 +00002175 InitJavaModule(module, android.HostAndDeviceSupported)
2176 return module
2177}
2178
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002179// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2180// and makes sure that it is added to the appropriate test suite.
2181//
2182// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2183// compiled against an Android classpath.
2184//
2185// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2186// for host modules.
2187func JavaTestImportFactory() android.Module {
2188 module := &JavaTestImport{}
2189
2190 module.AddProperties(
2191 &module.Import.properties,
2192 &module.prebuiltTestProperties)
2193
2194 module.Import.properties.Installable = proptools.BoolPtr(true)
2195
2196 android.InitPrebuiltModule(module, &module.properties.Jars)
2197 android.InitApexModule(module)
2198 android.InitSdkAwareModule(module)
2199 InitJavaModule(module, android.HostAndDeviceSupported)
2200 return module
2201}
2202
Colin Cross1b16b0e2019-02-12 14:41:32 -08002203// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2204// allow running the test with `atest` or a `TEST_MAPPING` file.
2205//
2206// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2207// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002208func TestHostFactory() android.Module {
2209 module := &Test{}
2210
2211 module.AddProperties(
2212 &module.Module.properties,
2213 &module.Module.protoProperties,
2214 &module.testProperties)
2215
Colin Cross9ae1b922018-06-26 17:59:05 -07002216 module.Module.properties.Installable = proptools.BoolPtr(true)
2217
Colin Cross05638fc2018-04-09 18:40:24 -07002218 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002219 return module
2220}
2221
2222//
Colin Cross2fe66872015-03-30 17:20:39 -07002223// Java Binaries (.jar file plus wrapper script)
2224//
2225
Colin Crossf506d872017-07-19 15:53:04 -07002226type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002227 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002228 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002229
2230 // Name of the class containing main to be inserted into the manifest as Main-Class.
2231 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002232}
2233
Colin Crossf506d872017-07-19 15:53:04 -07002234type Binary struct {
2235 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002236
Colin Crossf506d872017-07-19 15:53:04 -07002237 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002238
Colin Cross6b4a32d2017-12-05 13:42:45 -08002239 isWrapperVariant bool
2240
Colin Crossc3315992017-12-08 19:12:36 -08002241 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002242 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002243}
2244
Alex Light24237172017-10-26 09:46:21 -07002245func (j *Binary) HostToolPath() android.OptionalPath {
2246 return android.OptionalPathForPath(j.binaryFile)
2247}
2248
Colin Crossf506d872017-07-19 15:53:04 -07002249func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002250 if ctx.Arch().ArchType == android.Common {
2251 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002252 if j.binaryProperties.Main_class != nil {
2253 if j.properties.Manifest != nil {
2254 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2255 }
2256 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2257 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2258 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2259 }
2260
Colin Cross6b4a32d2017-12-05 13:42:45 -08002261 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002262 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002263 // Handle the binary wrapper
2264 j.isWrapperVariant = true
2265
Colin Cross366938f2017-12-11 16:29:02 -08002266 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002267 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002268 } else {
2269 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2270 }
2271
2272 // Depend on the installed jar so that the wrapper doesn't get executed by
2273 // another build rule before the jar has been installed.
2274 jarFile := ctx.PrimaryModule().(*Binary).installFile
2275
2276 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2277 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002278 }
Colin Cross2fe66872015-03-30 17:20:39 -07002279}
2280
Colin Crossf506d872017-07-19 15:53:04 -07002281func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002282 if ctx.Arch().ArchType == android.Common {
2283 j.deps(ctx)
2284 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002285}
2286
Colin Cross1b16b0e2019-02-12 14:41:32 -08002287// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2288// as well.
2289//
2290// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2291// compiled against the device bootclasspath.
2292//
2293// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2294// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002295func BinaryFactory() android.Module {
2296 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002297
Colin Cross36242852017-06-23 15:06:31 -07002298 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002299 &module.Module.properties,
2300 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002301 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002302 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002303 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002304
Colin Cross9ae1b922018-06-26 17:59:05 -07002305 module.Module.properties.Installable = proptools.BoolPtr(true)
2306
Colin Cross6b4a32d2017-12-05 13:42:45 -08002307 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2308 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002309 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002310}
2311
Colin Cross1b16b0e2019-02-12 14:41:32 -08002312// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2313//
2314// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2315// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002316func BinaryHostFactory() android.Module {
2317 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002318
Colin Cross36242852017-06-23 15:06:31 -07002319 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002320 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002321 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002322 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002323
Colin Cross9ae1b922018-06-26 17:59:05 -07002324 module.Module.properties.Installable = proptools.BoolPtr(true)
2325
Colin Cross6b4a32d2017-12-05 13:42:45 -08002326 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2327 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002328 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002329}
2330
2331//
2332// Java prebuilts
2333//
2334
Colin Cross74d73e22017-08-02 11:05:49 -07002335type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002336 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002337
Nan Zhangea568a42017-11-08 21:20:04 -08002338 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002339
2340 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002341
2342 // List of shared java libs that this module has dependencies to
2343 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002344
2345 // List of files to remove from the jar file(s)
2346 Exclude_files []string
2347
2348 // List of directories to remove from the jar file(s)
2349 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002350
2351 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002352 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002353
2354 // set the name of the output
2355 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002356}
2357
2358type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002359 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002360 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002361 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002362 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002363 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002364
Colin Cross74d73e22017-08-02 11:05:49 -07002365 properties ImportProperties
2366
Colin Cross0a6e0072017-08-30 14:24:55 -07002367 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002368 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002369}
2370
Jiyong Park6a927c42020-01-21 02:03:43 +09002371func (j *Import) sdkVersion() sdkSpec {
2372 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002373}
2374
Jiyong Park6a927c42020-01-21 02:03:43 +09002375func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002376 return j.sdkVersion()
2377}
2378
Colin Cross74d73e22017-08-02 11:05:49 -07002379func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002380 return &j.prebuilt
2381}
2382
Colin Cross74d73e22017-08-02 11:05:49 -07002383func (j *Import) PrebuiltSrcs() []string {
2384 return j.properties.Jars
2385}
2386
2387func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002388 return j.prebuilt.Name(j.ModuleBase.Name())
2389}
2390
Jiyong Park0b238752019-10-29 11:23:10 +09002391func (j *Import) Stem() string {
2392 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2393}
2394
Jiyong Park618922e2020-01-08 13:35:43 +09002395func (a *Import) JacocoReportClassesFile() android.Path {
2396 return nil
2397}
2398
Colin Cross74d73e22017-08-02 11:05:49 -07002399func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002400 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002401}
2402
Colin Cross74d73e22017-08-02 11:05:49 -07002403func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002404 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002405
Jiyong Park0b238752019-10-29 11:23:10 +09002406 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002407 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002408 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2409 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002410 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002411 inputFile := outputFile
2412 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2413 TransformJetifier(ctx, outputFile, inputFile)
2414 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002415 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002416
2417 ctx.VisitDirectDeps(func(module android.Module) {
2418 otherName := ctx.OtherModuleName(module)
2419 tag := ctx.OtherModuleDependencyTag(module)
2420
2421 switch dep := module.(type) {
2422 case Dependency:
2423 switch tag {
2424 case libTag, staticLibTag:
2425 // sdk lib names from dependencies are re-exported
2426 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2427 }
2428 case SdkLibraryDependency:
2429 switch tag {
2430 case libTag:
2431 // names of sdk libs that are directly depended are exported
2432 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2433 }
2434 }
2435 })
2436
2437 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002438 if Bool(j.properties.Installable) {
2439 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002440 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002441 }
Colin Cross2fe66872015-03-30 17:20:39 -07002442}
2443
Colin Cross74d73e22017-08-02 11:05:49 -07002444var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002445
Nan Zhanged19fc32017-10-19 13:06:22 -07002446func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002447 if j.combinedClasspathFile == nil {
2448 return nil
2449 }
Colin Cross37f6d792018-07-12 12:28:41 -07002450 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002451}
2452
2453func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002454 if j.combinedClasspathFile == nil {
2455 return nil
2456 }
Colin Cross37f6d792018-07-12 12:28:41 -07002457 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002458}
2459
Colin Cross331a1212018-08-15 20:40:52 -07002460func (j *Import) ResourceJars() android.Paths {
2461 return nil
2462}
2463
2464func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002465 if j.combinedClasspathFile == nil {
2466 return nil
2467 }
Colin Cross331a1212018-08-15 20:40:52 -07002468 return android.Paths{j.combinedClasspathFile}
2469}
2470
Colin Crossf24a22a2019-01-31 14:12:44 -08002471func (j *Import) DexJar() android.Path {
2472 return nil
2473}
2474
Colin Cross74d73e22017-08-02 11:05:49 -07002475func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002476 return nil
2477}
2478
Jiyong Park1be96912018-05-28 18:02:19 +09002479func (j *Import) ExportedSdkLibs() []string {
2480 return j.exportedSdkLibs
2481}
2482
Artur Satayev9cf46692019-11-26 18:08:34 +00002483func (j *Import) ExportedPlugins() (android.Paths, []string) {
2484 return nil, nil
2485}
2486
Colin Cross0c4ce212019-05-03 15:28:19 -07002487func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2488 return nil, nil
2489}
2490
Jiyong Park0f80c182020-01-31 02:49:53 +09002491func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2492 depTag := ctx.OtherModuleDependencyTag(dep)
2493 // dependencies other than the static linkage are all considered crossing APEX boundary
2494 // Also, a dependency to an sdk member is also considered as such. This is required because
2495 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2496 return depTag == staticLibTag || j.IsInAnySdk()
2497}
2498
albaltai36ff7dc2018-12-25 14:35:23 +08002499// Add compile time check for interface implementation
2500var _ android.IDEInfo = (*Import)(nil)
2501var _ android.IDECustomizedModuleName = (*Import)(nil)
2502
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002503// Collect information for opening IDE project files in java/jdeps.go.
2504const (
2505 removedPrefix = "prebuilt_"
2506)
2507
2508func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2509 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2510}
2511
2512func (j *Import) IDECustomizedModuleName() string {
2513 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2514 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2515 // solution to get the Import name.
2516 name := j.Name()
2517 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002518 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002519 }
2520 return name
2521}
2522
Colin Cross74d73e22017-08-02 11:05:49 -07002523var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002524
Colin Cross1b16b0e2019-02-12 14:41:32 -08002525// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2526//
2527// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2528// compiled against an Android classpath.
2529//
2530// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2531// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002532func ImportFactory() android.Module {
2533 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002534
Colin Cross74d73e22017-08-02 11:05:49 -07002535 module.AddProperties(&module.properties)
2536
2537 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002538 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002539 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002540 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002541 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002542}
2543
Colin Cross1b16b0e2019-02-12 14:41:32 -08002544// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2545// module.
2546//
2547// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2548// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002549func ImportFactoryHost() android.Module {
2550 module := &Import{}
2551
2552 module.AddProperties(&module.properties)
2553
2554 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002555 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002556 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002557 return module
2558}
2559
Colin Cross42be7612019-02-21 18:12:14 -08002560// dex_import module
2561
2562type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002563 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002564
2565 // set the name of the output
2566 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002567}
2568
2569type DexImport struct {
2570 android.ModuleBase
2571 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002572 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002573 prebuilt android.Prebuilt
2574
2575 properties DexImportProperties
2576
2577 dexJarFile android.Path
2578 maybeStrippedDexJarFile android.Path
2579
2580 dexpreopter
2581}
2582
2583func (j *DexImport) Prebuilt() *android.Prebuilt {
2584 return &j.prebuilt
2585}
2586
2587func (j *DexImport) PrebuiltSrcs() []string {
2588 return j.properties.Jars
2589}
2590
2591func (j *DexImport) Name() string {
2592 return j.prebuilt.Name(j.ModuleBase.Name())
2593}
2594
Jiyong Park0b238752019-10-29 11:23:10 +09002595func (j *DexImport) Stem() string {
2596 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2597}
2598
Martin Stjernholm6d415272020-01-31 17:10:36 +00002599func (j *DexImport) IsInstallable() bool {
2600 return true
2601}
2602
Colin Cross42be7612019-02-21 18:12:14 -08002603func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2604 if len(j.properties.Jars) != 1 {
2605 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2606 }
2607
Jiyong Park0b238752019-10-29 11:23:10 +09002608 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002609 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2610
2611 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2612 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2613
2614 if j.dexpreopter.uncompressedDex {
2615 rule := android.NewRuleBuilder()
2616
2617 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2618 rule.Temporary(temporary)
2619
2620 // use zip2zip to uncompress classes*.dex files
2621 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002622 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002623 FlagWithInput("-i ", inputJar).
2624 FlagWithOutput("-o ", temporary).
2625 FlagWithArg("-0 ", "'classes*.dex'")
2626
2627 // use zipalign to align uncompressed classes*.dex files
2628 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002629 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002630 Flag("-f").
2631 Text("4").
2632 Input(temporary).
2633 Output(dexOutputFile)
2634
2635 rule.DeleteTemporaryFiles()
2636
2637 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2638 } else {
2639 ctx.Build(pctx, android.BuildParams{
2640 Rule: android.Cp,
2641 Input: inputJar,
2642 Output: dexOutputFile,
2643 })
2644 }
2645
2646 j.dexJarFile = dexOutputFile
2647
2648 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2649
2650 j.maybeStrippedDexJarFile = dexOutputFile
2651
2652 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2653 ctx.ModuleName()+".jar", dexOutputFile)
2654}
2655
2656func (j *DexImport) DexJar() android.Path {
2657 return j.dexJarFile
2658}
2659
2660// dex_import imports a `.jar` file containing classes.dex files.
2661//
2662// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2663// to the device.
2664func DexImportFactory() android.Module {
2665 module := &DexImport{}
2666
2667 module.AddProperties(&module.properties)
2668
2669 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002670 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002671 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002672 return module
2673}
2674
Colin Cross89536d42017-07-07 14:35:50 -07002675//
2676// Defaults
2677//
2678type Defaults struct {
2679 android.ModuleBase
2680 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002681 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002682}
2683
Colin Cross1b16b0e2019-02-12 14:41:32 -08002684// java_defaults provides a set of properties that can be inherited by other java or android modules.
2685//
2686// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2687// property in the defaults module that exists in the depending module will be prepended to the depending module's
2688// value for that property.
2689//
2690// Example:
2691//
2692// java_defaults {
2693// name: "example_defaults",
2694// srcs: ["common/**/*.java"],
2695// javacflags: ["-Xlint:all"],
2696// aaptflags: ["--auto-add-overlay"],
2697// }
2698//
2699// java_library {
2700// name: "example",
2701// defaults: ["example_defaults"],
2702// srcs: ["example/**/*.java"],
2703// }
2704//
2705// is functionally identical to:
2706//
2707// java_library {
2708// name: "example",
2709// srcs: [
2710// "common/**/*.java",
2711// "example/**/*.java",
2712// ],
2713// javacflags: ["-Xlint:all"],
2714// }
Colin Cross89536d42017-07-07 14:35:50 -07002715func defaultsFactory() android.Module {
2716 return DefaultsFactory()
2717}
2718
Paul Duffin47357662019-12-05 14:07:14 +00002719func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002720 module := &Defaults{}
2721
Colin Cross89536d42017-07-07 14:35:50 -07002722 module.AddProperties(
2723 &CompilerProperties{},
2724 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002725 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002726 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002727 &aaptProperties{},
2728 &androidLibraryProperties{},
2729 &appProperties{},
2730 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002731 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002732 &ImportProperties{},
2733 &AARImportProperties{},
2734 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002735 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002736 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002737 )
2738
2739 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002740 return module
2741}
Nan Zhangea568a42017-11-08 21:20:04 -08002742
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002743func kytheExtractJavaFactory() android.Singleton {
2744 return &kytheExtractJavaSingleton{}
2745}
2746
2747type kytheExtractJavaSingleton struct {
2748}
2749
2750func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2751 var xrefTargets android.Paths
2752 ctx.VisitAllModules(func(module android.Module) {
2753 if javaModule, ok := module.(xref); ok {
2754 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2755 }
2756 })
2757 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2758 if len(xrefTargets) > 0 {
2759 ctx.Build(pctx, android.BuildParams{
2760 Rule: blueprint.Phony,
2761 Output: android.PathForPhony(ctx, "xref_java"),
2762 Inputs: xrefTargets,
2763 })
2764 }
2765}
2766
Nan Zhangea568a42017-11-08 21:20:04 -08002767var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002768var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002769var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002770var inList = android.InList