blob: b3aca4989d4ba13fbaa24e934e6f38f1d9f32b96 [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 Park93e57a02020-02-21 16:04:53 +09001522 // Force enable the instrumentation for java code that is built for APEXes ...
1523 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1524 // doesn't make sense)
1525 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1526 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park00cae1c2020-02-18 12:50:44 +00001527 j.properties.Instrument = true
1528 }
1529
Colin Cross3144dfc2018-01-03 15:06:47 -08001530 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001531 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1532 }
1533
Colin Cross331a1212018-08-15 20:40:52 -07001534 // merge implementation jar with resources if necessary
1535 implementationAndResourcesJar := outputFile
1536 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001537 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001538 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001539 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001540 false, nil, nil)
1541 implementationAndResourcesJar = combinedJar
1542 }
1543
1544 j.implementationAndResourcesJar = implementationAndResourcesJar
1545
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001546 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1547 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1548 if j.deviceProperties.Compile_dex == nil {
1549 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1550 }
1551 if j.deviceProperties.Hostdex == nil {
1552 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1553 }
1554 }
1555
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001556 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001557 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001558 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001559 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001560 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001561 if ctx.Failed() {
1562 return
1563 }
Colin Cross331a1212018-08-15 20:40:52 -07001564
Jiyong Park09cb6292019-07-15 15:29:23 +09001565 // Hidden API CSV generation and dex encoding
1566 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1567 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001568
Colin Cross331a1212018-08-15 20:40:52 -07001569 // merge dex jar with resources if necessary
1570 if j.resourceJar != nil {
1571 jars := android.Paths{dexOutputFile, j.resourceJar}
1572 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1573 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1574 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001575 if j.deviceProperties.UncompressDex {
1576 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1577 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1578 dexOutputFile = combinedAlignedJar
1579 } else {
1580 dexOutputFile = combinedJar
1581 }
Colin Cross331a1212018-08-15 20:40:52 -07001582 }
1583
1584 j.dexJarFile = dexOutputFile
1585
Colin Cross8faf8fc2019-01-16 15:15:52 -08001586 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001587 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1588
1589 j.maybeStrippedDexJarFile = dexOutputFile
1590
Colin Cross3063b782018-08-15 11:19:12 -07001591 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001592
1593 if ctx.Failed() {
1594 return
1595 }
Colin Cross331a1212018-08-15 20:40:52 -07001596 } else {
1597 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001598 }
Colin Cross331a1212018-08-15 20:40:52 -07001599
Colin Crossb7a63242015-04-16 14:09:14 -07001600 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001601
1602 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1603 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001604}
1605
Colin Cross3b706fd2019-09-05 16:44:18 -07001606func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1607 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1608
1609 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1610 if idx >= 0 {
1611 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1612 jarName += strconv.Itoa(idx)
1613 }
1614
1615 classes := android.PathForModuleOut(ctx, "javac", jarName)
1616 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1617
1618 if ctx.Config().EmitXrefRules() {
1619 extractionFile := android.PathForModuleOut(ctx, kzipName)
1620 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1621 j.kytheFiles = append(j.kytheFiles, extractionFile)
1622 }
1623
1624 return classes
1625}
1626
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001627// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1628// since some of these flags may be used internally.
1629func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1630 for _, flag := range flags {
1631 flag = strings.TrimSpace(flag)
1632
1633 if !strings.HasPrefix(flag, "-") {
1634 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1635 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1636 ctx.PropertyErrorf("kotlincflags",
1637 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1638 } else if inList(flag, config.KotlincIllegalFlags) {
1639 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1640 } else if flag == "-include-runtime" {
1641 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1642 } else {
1643 args := strings.Split(flag, " ")
1644 if args[0] == "-kotlin-home" {
1645 ctx.PropertyErrorf("kotlincflags",
1646 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1647 }
1648 }
1649 }
1650}
1651
Colin Cross8eadbf02017-10-24 17:46:00 -07001652func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001653 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001654
1655 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001656 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001657 // Compile java sources into turbine.jar.
1658 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1659 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1660 if ctx.Failed() {
1661 return nil
1662 }
1663 jars = append(jars, turbineJar)
1664 }
1665
Colin Cross55f63ea2018-08-27 12:37:09 -07001666 jars = append(jars, extraJars...)
1667
Nan Zhanged19fc32017-10-19 13:06:22 -07001668 // Combine any static header libraries into classes-header.jar. If there is only
1669 // one input jar this step will be skipped.
1670 var headerJar android.Path
1671 jars = append(jars, deps.staticHeaderJars...)
1672
Colin Cross5c6ecc12017-10-23 18:12:27 -07001673 // we cannot skip the combine step for now if there is only one jar
1674 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1675 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001676 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001677 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001678 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001679
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001680 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001681 // Transform classes.jar into classes-jarjar.jar
1682 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001683 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001684 headerJar = jarjarFile
1685 if ctx.Failed() {
1686 return nil
1687 }
1688 }
1689
1690 return headerJar
1691}
1692
Colin Crosscb933592017-11-22 13:49:43 -08001693func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001694 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001695
Colin Cross7a3139e2017-12-19 13:57:50 -08001696 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001697
Colin Cross84c38822018-01-03 15:59:46 -08001698 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001699 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1700
1701 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1702
1703 j.jacocoReportClassesFile = jacocoReportClassesFile
1704
1705 return instrumentedJar
1706}
1707
albaltai36ff7dc2018-12-25 14:35:23 +08001708var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001709
Nan Zhanged19fc32017-10-19 13:06:22 -07001710func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001711 if j.headerJarFile == nil {
1712 return nil
1713 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001714 return android.Paths{j.headerJarFile}
1715}
1716
1717func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001718 if j.implementationJarFile == nil {
1719 return nil
1720 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001721 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001722}
1723
Colin Crossf24a22a2019-01-31 14:12:44 -08001724func (j *Module) DexJar() android.Path {
1725 return j.dexJarFile
1726}
1727
Colin Cross331a1212018-08-15 20:40:52 -07001728func (j *Module) ResourceJars() android.Paths {
1729 if j.resourceJar == nil {
1730 return nil
1731 }
1732 return android.Paths{j.resourceJar}
1733}
1734
1735func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001736 if j.implementationAndResourcesJar == nil {
1737 return nil
1738 }
Colin Cross331a1212018-08-15 20:40:52 -07001739 return android.Paths{j.implementationAndResourcesJar}
1740}
1741
Colin Cross46c9b8b2017-06-22 16:51:17 -07001742func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001743 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001744 return j.exportAidlIncludeDirs
1745}
1746
Jiyong Park1be96912018-05-28 18:02:19 +09001747func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001748 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001749 return j.exportedSdkLibs
1750}
1751
Artur Satayev9cf46692019-11-26 18:08:34 +00001752func (j *Module) ExportedPlugins() (android.Paths, []string) {
1753 return j.exportedPluginJars, j.exportedPluginClasses
1754}
1755
Colin Cross0c4ce212019-05-03 15:28:19 -07001756func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1757 return j.srcJarArgs, j.srcJarDeps
1758}
1759
Colin Cross46c9b8b2017-06-22 16:51:17 -07001760var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001761
Colin Cross46c9b8b2017-06-22 16:51:17 -07001762func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001763 return j.logtagsSrcs
1764}
1765
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001766// Collect information for opening IDE project files in java/jdeps.go.
1767func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1768 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1769 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001770 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001771 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001772 if j.expandJarjarRules != nil {
1773 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001774 }
1775}
1776
1777func (j *Module) CompilerDeps() []string {
1778 jdeps := []string{}
1779 jdeps = append(jdeps, j.properties.Libs...)
1780 jdeps = append(jdeps, j.properties.Static_libs...)
1781 return jdeps
1782}
1783
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001784func (j *Module) hasCode(ctx android.ModuleContext) bool {
1785 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1786 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1787}
1788
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001789func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1790 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001791 // Dependencies other than the static linkage are all considered crossing APEX boundary
1792 // Also, a dependency to an sdk member is also considered as such. This is required because
1793 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1794 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001795}
1796
Jiyong Park0b238752019-10-29 11:23:10 +09001797func (j *Module) Stem() string {
1798 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1799}
1800
Jiyong Park618922e2020-01-08 13:35:43 +09001801func (j *Module) JacocoReportClassesFile() android.Path {
1802 return j.jacocoReportClassesFile
1803}
1804
Martin Stjernholm6d415272020-01-31 17:10:36 +00001805func (j *Module) IsInstallable() bool {
1806 return Bool(j.properties.Installable)
1807}
1808
Colin Cross2fe66872015-03-30 17:20:39 -07001809//
1810// Java libraries (.jar file)
1811//
1812
Colin Crossf506d872017-07-19 15:53:04 -07001813type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001814 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001815
1816 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001817}
1818
Colin Cross42be7612019-02-21 18:12:14 -08001819func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001820 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1821 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1822 return true
1823 }
1824
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001825 // Store uncompressed (and do not strip) dex files from boot class path jars.
1826 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1827 return true
1828 }
1829
1830 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001831 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001832 return true
1833 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001834 if ctx.Config().UncompressPrivAppDex() &&
1835 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1836 return true
1837 }
1838
Colin Cross2fc72f62018-12-21 12:59:54 -08001839 return false
1840}
1841
Colin Crossf506d872017-07-19 15:53:04 -07001842func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001843 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001844 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001845 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001846 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001847 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001848 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001849
Jiyong Park7f7766d2019-07-25 22:02:35 +09001850 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1851 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001852 var extraInstallDeps android.Paths
1853 if j.InstallMixin != nil {
1854 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1855 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001856 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001857 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001858 }
Colin Crossb7a63242015-04-16 14:09:14 -07001859}
1860
Colin Crossf506d872017-07-19 15:53:04 -07001861func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001862 j.deps(ctx)
1863}
1864
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001865const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001866 aidlIncludeDir = "aidl"
1867 javaDir = "java"
1868 jarFileSuffix = ".jar"
1869 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001870)
1871
Paul Duffina0dbf432019-12-05 11:25:53 +00001872// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001873func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1874 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1875}
1876
1877func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1878 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001879}
1880
Paul Duffin13879572019-11-28 14:31:38 +00001881type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001882 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001883}
1884
1885func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1886 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1887}
1888
1889func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1890 _, ok := module.(*Library)
1891 return ok
1892}
1893
Paul Duffina0dbf432019-12-05 11:25:53 +00001894func (mt *librarySdkMemberType) buildSnapshot(
1895 sdkModuleContext android.ModuleContext,
1896 builder android.SnapshotBuilder,
1897 member android.SdkMember,
1898 jarToExportGetter func(j *Library) android.Path) {
1899
Paul Duffin13879572019-11-28 14:31:38 +00001900 variants := member.Variants()
1901 if len(variants) != 1 {
1902 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1903 for _, variant := range variants {
1904 sdkModuleContext.ModuleErrorf(" %q", variant)
1905 }
1906 }
1907 variant := variants[0]
1908 j := variant.(*Library)
1909
Paul Duffina0dbf432019-12-05 11:25:53 +00001910 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001911 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001912 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001913
1914 for _, dir := range j.AidlIncludeDirs() {
1915 // TODO(jiyong): copy parcelable declarations only
1916 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1917 for _, file := range aidlFiles {
1918 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1919 }
1920 }
1921
Paul Duffin9d8d6092019-12-05 18:19:29 +00001922 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001923 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001924}
1925
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001926var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1927 librarySdkMemberType{
1928 android.SdkMemberTypeBase{
1929 PropertyName: "java_header_libs",
1930 SupportsSdk: true,
1931 },
1932 },
1933}
1934
Paul Duffina0dbf432019-12-05 11:25:53 +00001935type headerLibrarySdkMemberType struct {
1936 librarySdkMemberType
1937}
1938
1939func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1940 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1941 headerJars := j.HeaderJars()
1942 if len(headerJars) != 1 {
1943 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1944 }
1945
1946 return headerJars[0]
1947 })
1948}
1949
Paul Duffina0dbf432019-12-05 11:25:53 +00001950type implLibrarySdkMemberType struct {
1951 librarySdkMemberType
1952}
1953
1954func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1955 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1956 implementationJars := j.ImplementationJars()
1957 if len(implementationJars) != 1 {
1958 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1959 }
1960
1961 return implementationJars[0]
1962 })
1963}
1964
Colin Cross1b16b0e2019-02-12 14:41:32 -08001965// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1966//
1967// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1968// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1969// as a `static_libs` dependency of another module.
1970//
1971// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1972// a device.
1973//
1974// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1975// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001976func LibraryFactory() android.Module {
1977 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001978
Colin Cross9ae1b922018-06-26 17:59:05 -07001979 module.AddProperties(
1980 &module.Module.properties,
1981 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001982 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001983 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001984
Jiyong Park7f7766d2019-07-25 22:02:35 +09001985 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001986 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001987 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001988 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001989}
1990
Colin Cross1b16b0e2019-02-12 14:41:32 -08001991// java_library_static is an obsolete alias for java_library.
1992func LibraryStaticFactory() android.Module {
1993 return LibraryFactory()
1994}
1995
1996// java_library_host builds and links sources into a `.jar` file for the host.
1997//
1998// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1999// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002000func LibraryHostFactory() android.Module {
2001 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07002002
Colin Cross6af17aa2017-09-20 12:59:05 -07002003 module.AddProperties(
2004 &module.Module.properties,
2005 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07002006
Colin Cross9ae1b922018-06-26 17:59:05 -07002007 module.Module.properties.Installable = proptools.BoolPtr(true)
2008
Jiyong Park7f7766d2019-07-25 22:02:35 +09002009 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002010 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002011 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002012}
2013
2014//
Colin Crossb628ea52018-08-14 16:42:33 -07002015// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002016//
2017
2018type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002019 // list of compatibility suites (for example "cts", "vts") that the module should be
2020 // installed into.
2021 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002022
2023 // the name of the test configuration (for example "AndroidTest.xml") that should be
2024 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002025 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002026
Jack He33338892018-09-19 02:21:28 -07002027 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2028 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002029 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002030
Colin Crossd96ca352018-08-10 16:06:24 -07002031 // list of files or filegroup modules that provide data that should be installed alongside
2032 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002033 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002034
2035 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2036 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2037 // explicitly.
2038 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002039}
2040
Paul Duffin42df1442019-03-20 12:45:53 +00002041type testHelperLibraryProperties struct {
2042 // list of compatibility suites (for example "cts", "vts") that the module should be
2043 // installed into.
2044 Test_suites []string `android:"arch_variant"`
2045}
2046
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002047type prebuiltTestProperties struct {
2048 // list of compatibility suites (for example "cts", "vts") that the module should be
2049 // installed into.
2050 Test_suites []string `android:"arch_variant"`
2051
2052 // the name of the test configuration (for example "AndroidTest.xml") that should be
2053 // installed with the module.
2054 Test_config *string `android:"path,arch_variant"`
2055}
2056
Colin Cross05638fc2018-04-09 18:40:24 -07002057type Test struct {
2058 Library
2059
2060 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002061
2062 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002063 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002064}
2065
Paul Duffin42df1442019-03-20 12:45:53 +00002066type TestHelperLibrary struct {
2067 Library
2068
2069 testHelperLibraryProperties testHelperLibraryProperties
2070}
2071
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002072type JavaTestImport struct {
2073 Import
2074
2075 prebuiltTestProperties prebuiltTestProperties
2076
2077 testConfig android.Path
2078}
2079
Colin Cross303e21f2018-08-07 16:49:25 -07002080func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002081 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2082 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002083 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002084
2085 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002086}
2087
Paul Duffin42df1442019-03-20 12:45:53 +00002088func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2089 j.Library.GenerateAndroidBuildActions(ctx)
2090}
2091
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002092func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2093 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2094 j.prebuiltTestProperties.Test_suites, nil)
2095
2096 j.Import.GenerateAndroidBuildActions(ctx)
2097}
2098
2099type testSdkMemberType struct {
2100 android.SdkMemberTypeBase
2101}
2102
2103func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2104 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2105}
2106
2107func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2108 _, ok := module.(*Test)
2109 return ok
2110}
2111
2112func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2113 variants := member.Variants()
2114 if len(variants) != 1 {
2115 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2116 for _, variant := range variants {
2117 sdkModuleContext.ModuleErrorf(" %q", variant)
2118 }
2119 }
2120 variant := variants[0]
2121 j := variant.(*Test)
2122
2123 implementationJars := j.ImplementationJars()
2124 if len(implementationJars) != 1 {
2125 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2126 }
2127
2128 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2129 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2130
2131 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2132 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2133
2134 module := builder.AddPrebuiltModule(member, "java_test_import")
2135 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2136 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2137}
2138
Colin Cross1b16b0e2019-02-12 14:41:32 -08002139// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2140// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2141//
2142// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2143// compiled against the device bootclasspath.
2144//
2145// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2146// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002147func TestFactory() android.Module {
2148 module := &Test{}
2149
2150 module.AddProperties(
2151 &module.Module.properties,
2152 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002153 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002154 &module.Module.protoProperties,
2155 &module.testProperties)
2156
Colin Cross9ae1b922018-06-26 17:59:05 -07002157 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002158 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002159
Colin Cross05638fc2018-04-09 18:40:24 -07002160 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002161 return module
2162}
2163
Paul Duffin42df1442019-03-20 12:45:53 +00002164// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2165func TestHelperLibraryFactory() android.Module {
2166 module := &TestHelperLibrary{}
2167
2168 module.AddProperties(
2169 &module.Module.properties,
2170 &module.Module.deviceProperties,
2171 &module.Module.dexpreoptProperties,
2172 &module.Module.protoProperties,
2173 &module.testHelperLibraryProperties)
2174
Colin Cross9a4abed2019-04-24 13:19:28 -07002175 module.Module.properties.Installable = proptools.BoolPtr(true)
2176 module.Module.dexpreopter.isTest = true
2177
Paul Duffin42df1442019-03-20 12:45:53 +00002178 InitJavaModule(module, android.HostAndDeviceSupported)
2179 return module
2180}
2181
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002182// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2183// and makes sure that it is added to the appropriate test suite.
2184//
2185// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2186// compiled against an Android classpath.
2187//
2188// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2189// for host modules.
2190func JavaTestImportFactory() android.Module {
2191 module := &JavaTestImport{}
2192
2193 module.AddProperties(
2194 &module.Import.properties,
2195 &module.prebuiltTestProperties)
2196
2197 module.Import.properties.Installable = proptools.BoolPtr(true)
2198
2199 android.InitPrebuiltModule(module, &module.properties.Jars)
2200 android.InitApexModule(module)
2201 android.InitSdkAwareModule(module)
2202 InitJavaModule(module, android.HostAndDeviceSupported)
2203 return module
2204}
2205
Colin Cross1b16b0e2019-02-12 14:41:32 -08002206// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2207// allow running the test with `atest` or a `TEST_MAPPING` file.
2208//
2209// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2210// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002211func TestHostFactory() android.Module {
2212 module := &Test{}
2213
2214 module.AddProperties(
2215 &module.Module.properties,
2216 &module.Module.protoProperties,
2217 &module.testProperties)
2218
Colin Cross9ae1b922018-06-26 17:59:05 -07002219 module.Module.properties.Installable = proptools.BoolPtr(true)
2220
Colin Cross05638fc2018-04-09 18:40:24 -07002221 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002222 return module
2223}
2224
2225//
Colin Cross2fe66872015-03-30 17:20:39 -07002226// Java Binaries (.jar file plus wrapper script)
2227//
2228
Colin Crossf506d872017-07-19 15:53:04 -07002229type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002230 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002231 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002232
2233 // Name of the class containing main to be inserted into the manifest as Main-Class.
2234 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002235}
2236
Colin Crossf506d872017-07-19 15:53:04 -07002237type Binary struct {
2238 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002239
Colin Crossf506d872017-07-19 15:53:04 -07002240 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002241
Colin Cross6b4a32d2017-12-05 13:42:45 -08002242 isWrapperVariant bool
2243
Colin Crossc3315992017-12-08 19:12:36 -08002244 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002245 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002246}
2247
Alex Light24237172017-10-26 09:46:21 -07002248func (j *Binary) HostToolPath() android.OptionalPath {
2249 return android.OptionalPathForPath(j.binaryFile)
2250}
2251
Colin Crossf506d872017-07-19 15:53:04 -07002252func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002253 if ctx.Arch().ArchType == android.Common {
2254 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002255 if j.binaryProperties.Main_class != nil {
2256 if j.properties.Manifest != nil {
2257 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2258 }
2259 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2260 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2261 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2262 }
2263
Colin Cross6b4a32d2017-12-05 13:42:45 -08002264 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002265 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002266 // Handle the binary wrapper
2267 j.isWrapperVariant = true
2268
Colin Cross366938f2017-12-11 16:29:02 -08002269 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002270 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002271 } else {
2272 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2273 }
2274
2275 // Depend on the installed jar so that the wrapper doesn't get executed by
2276 // another build rule before the jar has been installed.
2277 jarFile := ctx.PrimaryModule().(*Binary).installFile
2278
2279 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2280 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002281 }
Colin Cross2fe66872015-03-30 17:20:39 -07002282}
2283
Colin Crossf506d872017-07-19 15:53:04 -07002284func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002285 if ctx.Arch().ArchType == android.Common {
2286 j.deps(ctx)
2287 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002288}
2289
Colin Cross1b16b0e2019-02-12 14:41:32 -08002290// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2291// as well.
2292//
2293// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2294// compiled against the device bootclasspath.
2295//
2296// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2297// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002298func BinaryFactory() android.Module {
2299 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002300
Colin Cross36242852017-06-23 15:06:31 -07002301 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002302 &module.Module.properties,
2303 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002304 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002305 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002306 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002307
Colin Cross9ae1b922018-06-26 17:59:05 -07002308 module.Module.properties.Installable = proptools.BoolPtr(true)
2309
Colin Cross6b4a32d2017-12-05 13:42:45 -08002310 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2311 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002312 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002313}
2314
Colin Cross1b16b0e2019-02-12 14:41:32 -08002315// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2316//
2317// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2318// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002319func BinaryHostFactory() android.Module {
2320 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002321
Colin Cross36242852017-06-23 15:06:31 -07002322 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002323 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002324 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002325 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002326
Colin Cross9ae1b922018-06-26 17:59:05 -07002327 module.Module.properties.Installable = proptools.BoolPtr(true)
2328
Colin Cross6b4a32d2017-12-05 13:42:45 -08002329 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2330 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002331 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002332}
2333
2334//
2335// Java prebuilts
2336//
2337
Colin Cross74d73e22017-08-02 11:05:49 -07002338type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002339 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002340
Nan Zhangea568a42017-11-08 21:20:04 -08002341 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002342
2343 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002344
2345 // List of shared java libs that this module has dependencies to
2346 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002347
2348 // List of files to remove from the jar file(s)
2349 Exclude_files []string
2350
2351 // List of directories to remove from the jar file(s)
2352 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002353
2354 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002355 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002356
2357 // set the name of the output
2358 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002359}
2360
2361type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002362 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002363 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002364 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002365 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002366 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002367
Colin Cross74d73e22017-08-02 11:05:49 -07002368 properties ImportProperties
2369
Colin Cross0a6e0072017-08-30 14:24:55 -07002370 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002371 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002372}
2373
Jiyong Park6a927c42020-01-21 02:03:43 +09002374func (j *Import) sdkVersion() sdkSpec {
2375 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002376}
2377
Jiyong Park6a927c42020-01-21 02:03:43 +09002378func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002379 return j.sdkVersion()
2380}
2381
Colin Cross74d73e22017-08-02 11:05:49 -07002382func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002383 return &j.prebuilt
2384}
2385
Colin Cross74d73e22017-08-02 11:05:49 -07002386func (j *Import) PrebuiltSrcs() []string {
2387 return j.properties.Jars
2388}
2389
2390func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002391 return j.prebuilt.Name(j.ModuleBase.Name())
2392}
2393
Jiyong Park0b238752019-10-29 11:23:10 +09002394func (j *Import) Stem() string {
2395 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2396}
2397
Jiyong Park618922e2020-01-08 13:35:43 +09002398func (a *Import) JacocoReportClassesFile() android.Path {
2399 return nil
2400}
2401
Colin Cross74d73e22017-08-02 11:05:49 -07002402func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002403 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002404}
2405
Colin Cross74d73e22017-08-02 11:05:49 -07002406func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002407 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002408
Jiyong Park0b238752019-10-29 11:23:10 +09002409 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002410 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002411 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2412 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002413 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002414 inputFile := outputFile
2415 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2416 TransformJetifier(ctx, outputFile, inputFile)
2417 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002418 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002419
2420 ctx.VisitDirectDeps(func(module android.Module) {
2421 otherName := ctx.OtherModuleName(module)
2422 tag := ctx.OtherModuleDependencyTag(module)
2423
2424 switch dep := module.(type) {
2425 case Dependency:
2426 switch tag {
2427 case libTag, staticLibTag:
2428 // sdk lib names from dependencies are re-exported
2429 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2430 }
2431 case SdkLibraryDependency:
2432 switch tag {
2433 case libTag:
2434 // names of sdk libs that are directly depended are exported
2435 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2436 }
2437 }
2438 })
2439
2440 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002441 if Bool(j.properties.Installable) {
2442 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002443 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002444 }
Colin Cross2fe66872015-03-30 17:20:39 -07002445}
2446
Colin Cross74d73e22017-08-02 11:05:49 -07002447var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002448
Nan Zhanged19fc32017-10-19 13:06:22 -07002449func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002450 if j.combinedClasspathFile == nil {
2451 return nil
2452 }
Colin Cross37f6d792018-07-12 12:28:41 -07002453 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002454}
2455
2456func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002457 if j.combinedClasspathFile == nil {
2458 return nil
2459 }
Colin Cross37f6d792018-07-12 12:28:41 -07002460 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002461}
2462
Colin Cross331a1212018-08-15 20:40:52 -07002463func (j *Import) ResourceJars() android.Paths {
2464 return nil
2465}
2466
2467func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002468 if j.combinedClasspathFile == nil {
2469 return nil
2470 }
Colin Cross331a1212018-08-15 20:40:52 -07002471 return android.Paths{j.combinedClasspathFile}
2472}
2473
Colin Crossf24a22a2019-01-31 14:12:44 -08002474func (j *Import) DexJar() android.Path {
2475 return nil
2476}
2477
Colin Cross74d73e22017-08-02 11:05:49 -07002478func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002479 return nil
2480}
2481
Jiyong Park1be96912018-05-28 18:02:19 +09002482func (j *Import) ExportedSdkLibs() []string {
2483 return j.exportedSdkLibs
2484}
2485
Artur Satayev9cf46692019-11-26 18:08:34 +00002486func (j *Import) ExportedPlugins() (android.Paths, []string) {
2487 return nil, nil
2488}
2489
Colin Cross0c4ce212019-05-03 15:28:19 -07002490func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2491 return nil, nil
2492}
2493
Jiyong Park0f80c182020-01-31 02:49:53 +09002494func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2495 depTag := ctx.OtherModuleDependencyTag(dep)
2496 // dependencies other than the static linkage are all considered crossing APEX boundary
2497 // Also, a dependency to an sdk member is also considered as such. This is required because
2498 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2499 return depTag == staticLibTag || j.IsInAnySdk()
2500}
2501
albaltai36ff7dc2018-12-25 14:35:23 +08002502// Add compile time check for interface implementation
2503var _ android.IDEInfo = (*Import)(nil)
2504var _ android.IDECustomizedModuleName = (*Import)(nil)
2505
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002506// Collect information for opening IDE project files in java/jdeps.go.
2507const (
2508 removedPrefix = "prebuilt_"
2509)
2510
2511func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2512 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2513}
2514
2515func (j *Import) IDECustomizedModuleName() string {
2516 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2517 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2518 // solution to get the Import name.
2519 name := j.Name()
2520 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002521 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002522 }
2523 return name
2524}
2525
Colin Cross74d73e22017-08-02 11:05:49 -07002526var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002527
Colin Cross1b16b0e2019-02-12 14:41:32 -08002528// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2529//
2530// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2531// compiled against an Android classpath.
2532//
2533// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2534// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002535func ImportFactory() android.Module {
2536 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002537
Colin Cross74d73e22017-08-02 11:05:49 -07002538 module.AddProperties(&module.properties)
2539
2540 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002541 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002542 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002543 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002544 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002545}
2546
Colin Cross1b16b0e2019-02-12 14:41:32 -08002547// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2548// module.
2549//
2550// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2551// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002552func ImportFactoryHost() android.Module {
2553 module := &Import{}
2554
2555 module.AddProperties(&module.properties)
2556
2557 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002558 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002559 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002560 return module
2561}
2562
Colin Cross42be7612019-02-21 18:12:14 -08002563// dex_import module
2564
2565type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002566 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002567
2568 // set the name of the output
2569 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002570}
2571
2572type DexImport struct {
2573 android.ModuleBase
2574 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002575 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002576 prebuilt android.Prebuilt
2577
2578 properties DexImportProperties
2579
2580 dexJarFile android.Path
2581 maybeStrippedDexJarFile android.Path
2582
2583 dexpreopter
2584}
2585
2586func (j *DexImport) Prebuilt() *android.Prebuilt {
2587 return &j.prebuilt
2588}
2589
2590func (j *DexImport) PrebuiltSrcs() []string {
2591 return j.properties.Jars
2592}
2593
2594func (j *DexImport) Name() string {
2595 return j.prebuilt.Name(j.ModuleBase.Name())
2596}
2597
Jiyong Park0b238752019-10-29 11:23:10 +09002598func (j *DexImport) Stem() string {
2599 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2600}
2601
Martin Stjernholm6d415272020-01-31 17:10:36 +00002602func (j *DexImport) IsInstallable() bool {
2603 return true
2604}
2605
Colin Cross42be7612019-02-21 18:12:14 -08002606func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2607 if len(j.properties.Jars) != 1 {
2608 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2609 }
2610
Jiyong Park0b238752019-10-29 11:23:10 +09002611 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002612 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2613
2614 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2615 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2616
2617 if j.dexpreopter.uncompressedDex {
2618 rule := android.NewRuleBuilder()
2619
2620 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2621 rule.Temporary(temporary)
2622
2623 // use zip2zip to uncompress classes*.dex files
2624 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002625 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002626 FlagWithInput("-i ", inputJar).
2627 FlagWithOutput("-o ", temporary).
2628 FlagWithArg("-0 ", "'classes*.dex'")
2629
2630 // use zipalign to align uncompressed classes*.dex files
2631 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002632 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002633 Flag("-f").
2634 Text("4").
2635 Input(temporary).
2636 Output(dexOutputFile)
2637
2638 rule.DeleteTemporaryFiles()
2639
2640 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2641 } else {
2642 ctx.Build(pctx, android.BuildParams{
2643 Rule: android.Cp,
2644 Input: inputJar,
2645 Output: dexOutputFile,
2646 })
2647 }
2648
2649 j.dexJarFile = dexOutputFile
2650
2651 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2652
2653 j.maybeStrippedDexJarFile = dexOutputFile
2654
2655 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2656 ctx.ModuleName()+".jar", dexOutputFile)
2657}
2658
2659func (j *DexImport) DexJar() android.Path {
2660 return j.dexJarFile
2661}
2662
2663// dex_import imports a `.jar` file containing classes.dex files.
2664//
2665// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2666// to the device.
2667func DexImportFactory() android.Module {
2668 module := &DexImport{}
2669
2670 module.AddProperties(&module.properties)
2671
2672 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002673 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002674 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002675 return module
2676}
2677
Colin Cross89536d42017-07-07 14:35:50 -07002678//
2679// Defaults
2680//
2681type Defaults struct {
2682 android.ModuleBase
2683 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002684 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002685}
2686
Colin Cross1b16b0e2019-02-12 14:41:32 -08002687// java_defaults provides a set of properties that can be inherited by other java or android modules.
2688//
2689// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2690// property in the defaults module that exists in the depending module will be prepended to the depending module's
2691// value for that property.
2692//
2693// Example:
2694//
2695// java_defaults {
2696// name: "example_defaults",
2697// srcs: ["common/**/*.java"],
2698// javacflags: ["-Xlint:all"],
2699// aaptflags: ["--auto-add-overlay"],
2700// }
2701//
2702// java_library {
2703// name: "example",
2704// defaults: ["example_defaults"],
2705// srcs: ["example/**/*.java"],
2706// }
2707//
2708// is functionally identical to:
2709//
2710// java_library {
2711// name: "example",
2712// srcs: [
2713// "common/**/*.java",
2714// "example/**/*.java",
2715// ],
2716// javacflags: ["-Xlint:all"],
2717// }
Colin Cross89536d42017-07-07 14:35:50 -07002718func defaultsFactory() android.Module {
2719 return DefaultsFactory()
2720}
2721
Paul Duffin47357662019-12-05 14:07:14 +00002722func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002723 module := &Defaults{}
2724
Colin Cross89536d42017-07-07 14:35:50 -07002725 module.AddProperties(
2726 &CompilerProperties{},
2727 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002728 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002729 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002730 &aaptProperties{},
2731 &androidLibraryProperties{},
2732 &appProperties{},
2733 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002734 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002735 &ImportProperties{},
2736 &AARImportProperties{},
2737 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002738 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002739 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002740 )
2741
2742 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002743 return module
2744}
Nan Zhangea568a42017-11-08 21:20:04 -08002745
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002746func kytheExtractJavaFactory() android.Singleton {
2747 return &kytheExtractJavaSingleton{}
2748}
2749
2750type kytheExtractJavaSingleton struct {
2751}
2752
2753func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2754 var xrefTargets android.Paths
2755 ctx.VisitAllModules(func(module android.Module) {
2756 if javaModule, ok := module.(xref); ok {
2757 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2758 }
2759 })
2760 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2761 if len(xrefTargets) > 0 {
2762 ctx.Build(pctx, android.BuildParams{
2763 Rule: blueprint.Phony,
2764 Output: android.PathForPhony(ctx, "xref_java"),
2765 Inputs: xrefTargets,
2766 })
2767 }
2768}
2769
Nan Zhangea568a42017-11-08 21:20:04 -08002770var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002771var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002772var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002773var inList = android.InList