blob: 462dba809f32e78529a561118aa23a1b05de1f30 [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
Colin Cross3144dfc2018-01-03 15:06:47 -08001522 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001523 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1524 }
1525
Colin Cross331a1212018-08-15 20:40:52 -07001526 // merge implementation jar with resources if necessary
1527 implementationAndResourcesJar := outputFile
1528 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001529 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001530 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001531 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001532 false, nil, nil)
1533 implementationAndResourcesJar = combinedJar
1534 }
1535
1536 j.implementationAndResourcesJar = implementationAndResourcesJar
1537
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001538 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1539 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1540 if j.deviceProperties.Compile_dex == nil {
1541 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1542 }
1543 if j.deviceProperties.Hostdex == nil {
1544 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1545 }
1546 }
1547
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001548 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001549 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001550 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001551 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001552 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001553 if ctx.Failed() {
1554 return
1555 }
Colin Cross331a1212018-08-15 20:40:52 -07001556
Jiyong Park09cb6292019-07-15 15:29:23 +09001557 // Hidden API CSV generation and dex encoding
1558 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1559 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001560
Colin Cross331a1212018-08-15 20:40:52 -07001561 // merge dex jar with resources if necessary
1562 if j.resourceJar != nil {
1563 jars := android.Paths{dexOutputFile, j.resourceJar}
1564 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1565 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1566 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001567 if j.deviceProperties.UncompressDex {
1568 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1569 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1570 dexOutputFile = combinedAlignedJar
1571 } else {
1572 dexOutputFile = combinedJar
1573 }
Colin Cross331a1212018-08-15 20:40:52 -07001574 }
1575
1576 j.dexJarFile = dexOutputFile
1577
Colin Cross8faf8fc2019-01-16 15:15:52 -08001578 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001579 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1580
1581 j.maybeStrippedDexJarFile = dexOutputFile
1582
Colin Cross3063b782018-08-15 11:19:12 -07001583 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001584
1585 if ctx.Failed() {
1586 return
1587 }
Colin Cross331a1212018-08-15 20:40:52 -07001588 } else {
1589 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001590 }
Colin Cross331a1212018-08-15 20:40:52 -07001591
Colin Crossb7a63242015-04-16 14:09:14 -07001592 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001593
1594 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1595 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001596}
1597
Colin Cross3b706fd2019-09-05 16:44:18 -07001598func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1599 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1600
1601 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1602 if idx >= 0 {
1603 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1604 jarName += strconv.Itoa(idx)
1605 }
1606
1607 classes := android.PathForModuleOut(ctx, "javac", jarName)
1608 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1609
1610 if ctx.Config().EmitXrefRules() {
1611 extractionFile := android.PathForModuleOut(ctx, kzipName)
1612 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1613 j.kytheFiles = append(j.kytheFiles, extractionFile)
1614 }
1615
1616 return classes
1617}
1618
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001619// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1620// since some of these flags may be used internally.
1621func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1622 for _, flag := range flags {
1623 flag = strings.TrimSpace(flag)
1624
1625 if !strings.HasPrefix(flag, "-") {
1626 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1627 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1628 ctx.PropertyErrorf("kotlincflags",
1629 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1630 } else if inList(flag, config.KotlincIllegalFlags) {
1631 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1632 } else if flag == "-include-runtime" {
1633 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1634 } else {
1635 args := strings.Split(flag, " ")
1636 if args[0] == "-kotlin-home" {
1637 ctx.PropertyErrorf("kotlincflags",
1638 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1639 }
1640 }
1641 }
1642}
1643
Colin Cross8eadbf02017-10-24 17:46:00 -07001644func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001645 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001646
1647 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001648 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001649 // Compile java sources into turbine.jar.
1650 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1651 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1652 if ctx.Failed() {
1653 return nil
1654 }
1655 jars = append(jars, turbineJar)
1656 }
1657
Colin Cross55f63ea2018-08-27 12:37:09 -07001658 jars = append(jars, extraJars...)
1659
Nan Zhanged19fc32017-10-19 13:06:22 -07001660 // Combine any static header libraries into classes-header.jar. If there is only
1661 // one input jar this step will be skipped.
1662 var headerJar android.Path
1663 jars = append(jars, deps.staticHeaderJars...)
1664
Colin Cross5c6ecc12017-10-23 18:12:27 -07001665 // we cannot skip the combine step for now if there is only one jar
1666 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1667 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001668 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001669 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001670 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001671
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001672 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001673 // Transform classes.jar into classes-jarjar.jar
1674 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001675 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001676 headerJar = jarjarFile
1677 if ctx.Failed() {
1678 return nil
1679 }
1680 }
1681
1682 return headerJar
1683}
1684
Colin Crosscb933592017-11-22 13:49:43 -08001685func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001686 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001687
Colin Cross7a3139e2017-12-19 13:57:50 -08001688 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001689
Colin Cross84c38822018-01-03 15:59:46 -08001690 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001691 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1692
1693 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1694
1695 j.jacocoReportClassesFile = jacocoReportClassesFile
1696
1697 return instrumentedJar
1698}
1699
albaltai36ff7dc2018-12-25 14:35:23 +08001700var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001701
Nan Zhanged19fc32017-10-19 13:06:22 -07001702func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001703 if j.headerJarFile == nil {
1704 return nil
1705 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001706 return android.Paths{j.headerJarFile}
1707}
1708
1709func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001710 if j.implementationJarFile == nil {
1711 return nil
1712 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001713 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001714}
1715
Colin Crossf24a22a2019-01-31 14:12:44 -08001716func (j *Module) DexJar() android.Path {
1717 return j.dexJarFile
1718}
1719
Colin Cross331a1212018-08-15 20:40:52 -07001720func (j *Module) ResourceJars() android.Paths {
1721 if j.resourceJar == nil {
1722 return nil
1723 }
1724 return android.Paths{j.resourceJar}
1725}
1726
1727func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001728 if j.implementationAndResourcesJar == nil {
1729 return nil
1730 }
Colin Cross331a1212018-08-15 20:40:52 -07001731 return android.Paths{j.implementationAndResourcesJar}
1732}
1733
Colin Cross46c9b8b2017-06-22 16:51:17 -07001734func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001735 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001736 return j.exportAidlIncludeDirs
1737}
1738
Jiyong Park1be96912018-05-28 18:02:19 +09001739func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001740 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001741 return j.exportedSdkLibs
1742}
1743
Artur Satayev9cf46692019-11-26 18:08:34 +00001744func (j *Module) ExportedPlugins() (android.Paths, []string) {
1745 return j.exportedPluginJars, j.exportedPluginClasses
1746}
1747
Colin Cross0c4ce212019-05-03 15:28:19 -07001748func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1749 return j.srcJarArgs, j.srcJarDeps
1750}
1751
Colin Cross46c9b8b2017-06-22 16:51:17 -07001752var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001753
Colin Cross46c9b8b2017-06-22 16:51:17 -07001754func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001755 return j.logtagsSrcs
1756}
1757
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001758// Collect information for opening IDE project files in java/jdeps.go.
1759func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1760 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1761 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001762 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001763 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001764 if j.expandJarjarRules != nil {
1765 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001766 }
1767}
1768
1769func (j *Module) CompilerDeps() []string {
1770 jdeps := []string{}
1771 jdeps = append(jdeps, j.properties.Libs...)
1772 jdeps = append(jdeps, j.properties.Static_libs...)
1773 return jdeps
1774}
1775
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001776func (j *Module) hasCode(ctx android.ModuleContext) bool {
1777 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1778 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1779}
1780
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001781func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1782 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001783 // Dependencies other than the static linkage are all considered crossing APEX boundary
1784 // Also, a dependency to an sdk member is also considered as such. This is required because
1785 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1786 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001787}
1788
Jiyong Park0b238752019-10-29 11:23:10 +09001789func (j *Module) Stem() string {
1790 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1791}
1792
Jiyong Park618922e2020-01-08 13:35:43 +09001793func (j *Module) JacocoReportClassesFile() android.Path {
1794 return j.jacocoReportClassesFile
1795}
1796
Martin Stjernholm6d415272020-01-31 17:10:36 +00001797func (j *Module) IsInstallable() bool {
1798 return Bool(j.properties.Installable)
1799}
1800
Colin Cross2fe66872015-03-30 17:20:39 -07001801//
1802// Java libraries (.jar file)
1803//
1804
Colin Crossf506d872017-07-19 15:53:04 -07001805type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001806 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001807
1808 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001809}
1810
Colin Cross42be7612019-02-21 18:12:14 -08001811func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001812 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1813 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1814 return true
1815 }
1816
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001817 // Store uncompressed (and do not strip) dex files from boot class path jars.
1818 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1819 return true
1820 }
1821
1822 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001823 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001824 return true
1825 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001826 if ctx.Config().UncompressPrivAppDex() &&
1827 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1828 return true
1829 }
1830
Colin Cross2fc72f62018-12-21 12:59:54 -08001831 return false
1832}
1833
Colin Crossf506d872017-07-19 15:53:04 -07001834func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001835 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001836 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001837 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001838 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001839 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001840 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001841
Jiyong Park7f7766d2019-07-25 22:02:35 +09001842 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1843 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001844 var extraInstallDeps android.Paths
1845 if j.InstallMixin != nil {
1846 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1847 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001848 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001849 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001850 }
Colin Crossb7a63242015-04-16 14:09:14 -07001851}
1852
Colin Crossf506d872017-07-19 15:53:04 -07001853func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001854 j.deps(ctx)
1855}
1856
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001857const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001858 aidlIncludeDir = "aidl"
1859 javaDir = "java"
1860 jarFileSuffix = ".jar"
1861 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001862)
1863
Paul Duffina0dbf432019-12-05 11:25:53 +00001864// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001865func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1866 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1867}
1868
1869func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1870 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001871}
1872
Paul Duffin13879572019-11-28 14:31:38 +00001873type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001874 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001875}
1876
1877func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1878 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1879}
1880
1881func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1882 _, ok := module.(*Library)
1883 return ok
1884}
1885
Paul Duffina0dbf432019-12-05 11:25:53 +00001886func (mt *librarySdkMemberType) buildSnapshot(
1887 sdkModuleContext android.ModuleContext,
1888 builder android.SnapshotBuilder,
1889 member android.SdkMember,
1890 jarToExportGetter func(j *Library) android.Path) {
1891
Paul Duffin13879572019-11-28 14:31:38 +00001892 variants := member.Variants()
1893 if len(variants) != 1 {
1894 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1895 for _, variant := range variants {
1896 sdkModuleContext.ModuleErrorf(" %q", variant)
1897 }
1898 }
1899 variant := variants[0]
1900 j := variant.(*Library)
1901
Paul Duffina0dbf432019-12-05 11:25:53 +00001902 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001903 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001904 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001905
1906 for _, dir := range j.AidlIncludeDirs() {
1907 // TODO(jiyong): copy parcelable declarations only
1908 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1909 for _, file := range aidlFiles {
1910 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1911 }
1912 }
1913
Paul Duffin9d8d6092019-12-05 18:19:29 +00001914 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001915 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001916}
1917
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001918var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1919 librarySdkMemberType{
1920 android.SdkMemberTypeBase{
1921 PropertyName: "java_header_libs",
1922 SupportsSdk: true,
1923 },
1924 },
1925}
1926
Paul Duffina0dbf432019-12-05 11:25:53 +00001927type headerLibrarySdkMemberType struct {
1928 librarySdkMemberType
1929}
1930
1931func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1932 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1933 headerJars := j.HeaderJars()
1934 if len(headerJars) != 1 {
1935 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1936 }
1937
1938 return headerJars[0]
1939 })
1940}
1941
Paul Duffina0dbf432019-12-05 11:25:53 +00001942type implLibrarySdkMemberType struct {
1943 librarySdkMemberType
1944}
1945
1946func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1947 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1948 implementationJars := j.ImplementationJars()
1949 if len(implementationJars) != 1 {
1950 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1951 }
1952
1953 return implementationJars[0]
1954 })
1955}
1956
Colin Cross1b16b0e2019-02-12 14:41:32 -08001957// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1958//
1959// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1960// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1961// as a `static_libs` dependency of another module.
1962//
1963// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1964// a device.
1965//
1966// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1967// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001968func LibraryFactory() android.Module {
1969 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001970
Colin Cross9ae1b922018-06-26 17:59:05 -07001971 module.AddProperties(
1972 &module.Module.properties,
1973 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001974 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001975 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001976
Jiyong Park7f7766d2019-07-25 22:02:35 +09001977 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001978 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001979 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001980 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001981}
1982
Colin Cross1b16b0e2019-02-12 14:41:32 -08001983// java_library_static is an obsolete alias for java_library.
1984func LibraryStaticFactory() android.Module {
1985 return LibraryFactory()
1986}
1987
1988// java_library_host builds and links sources into a `.jar` file for the host.
1989//
1990// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1991// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001992func LibraryHostFactory() android.Module {
1993 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001994
Colin Cross6af17aa2017-09-20 12:59:05 -07001995 module.AddProperties(
1996 &module.Module.properties,
1997 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001998
Colin Cross9ae1b922018-06-26 17:59:05 -07001999 module.Module.properties.Installable = proptools.BoolPtr(true)
2000
Jiyong Park7f7766d2019-07-25 22:02:35 +09002001 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002002 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002003 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002004}
2005
2006//
Colin Crossb628ea52018-08-14 16:42:33 -07002007// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002008//
2009
2010type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002011 // list of compatibility suites (for example "cts", "vts") that the module should be
2012 // installed into.
2013 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002014
2015 // the name of the test configuration (for example "AndroidTest.xml") that should be
2016 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002017 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002018
Jack He33338892018-09-19 02:21:28 -07002019 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2020 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002021 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002022
Colin Crossd96ca352018-08-10 16:06:24 -07002023 // list of files or filegroup modules that provide data that should be installed alongside
2024 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002025 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002026
2027 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2028 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2029 // explicitly.
2030 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002031}
2032
Paul Duffin42df1442019-03-20 12:45:53 +00002033type testHelperLibraryProperties struct {
2034 // list of compatibility suites (for example "cts", "vts") that the module should be
2035 // installed into.
2036 Test_suites []string `android:"arch_variant"`
2037}
2038
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002039type prebuiltTestProperties struct {
2040 // list of compatibility suites (for example "cts", "vts") that the module should be
2041 // installed into.
2042 Test_suites []string `android:"arch_variant"`
2043
2044 // the name of the test configuration (for example "AndroidTest.xml") that should be
2045 // installed with the module.
2046 Test_config *string `android:"path,arch_variant"`
2047}
2048
Colin Cross05638fc2018-04-09 18:40:24 -07002049type Test struct {
2050 Library
2051
2052 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002053
2054 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002055 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002056}
2057
Paul Duffin42df1442019-03-20 12:45:53 +00002058type TestHelperLibrary struct {
2059 Library
2060
2061 testHelperLibraryProperties testHelperLibraryProperties
2062}
2063
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002064type JavaTestImport struct {
2065 Import
2066
2067 prebuiltTestProperties prebuiltTestProperties
2068
2069 testConfig android.Path
2070}
2071
Colin Cross303e21f2018-08-07 16:49:25 -07002072func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002073 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2074 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002075 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002076
2077 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002078}
2079
Paul Duffin42df1442019-03-20 12:45:53 +00002080func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2081 j.Library.GenerateAndroidBuildActions(ctx)
2082}
2083
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002084func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2085 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2086 j.prebuiltTestProperties.Test_suites, nil)
2087
2088 j.Import.GenerateAndroidBuildActions(ctx)
2089}
2090
2091type testSdkMemberType struct {
2092 android.SdkMemberTypeBase
2093}
2094
2095func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2096 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2097}
2098
2099func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2100 _, ok := module.(*Test)
2101 return ok
2102}
2103
2104func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2105 variants := member.Variants()
2106 if len(variants) != 1 {
2107 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2108 for _, variant := range variants {
2109 sdkModuleContext.ModuleErrorf(" %q", variant)
2110 }
2111 }
2112 variant := variants[0]
2113 j := variant.(*Test)
2114
2115 implementationJars := j.ImplementationJars()
2116 if len(implementationJars) != 1 {
2117 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2118 }
2119
2120 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2121 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2122
2123 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2124 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2125
2126 module := builder.AddPrebuiltModule(member, "java_test_import")
2127 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2128 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2129}
2130
Colin Cross1b16b0e2019-02-12 14:41:32 -08002131// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2132// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2133//
2134// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2135// compiled against the device bootclasspath.
2136//
2137// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2138// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002139func TestFactory() android.Module {
2140 module := &Test{}
2141
2142 module.AddProperties(
2143 &module.Module.properties,
2144 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002145 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002146 &module.Module.protoProperties,
2147 &module.testProperties)
2148
Colin Cross9ae1b922018-06-26 17:59:05 -07002149 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002150 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002151
Colin Cross05638fc2018-04-09 18:40:24 -07002152 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002153 return module
2154}
2155
Paul Duffin42df1442019-03-20 12:45:53 +00002156// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2157func TestHelperLibraryFactory() android.Module {
2158 module := &TestHelperLibrary{}
2159
2160 module.AddProperties(
2161 &module.Module.properties,
2162 &module.Module.deviceProperties,
2163 &module.Module.dexpreoptProperties,
2164 &module.Module.protoProperties,
2165 &module.testHelperLibraryProperties)
2166
Colin Cross9a4abed2019-04-24 13:19:28 -07002167 module.Module.properties.Installable = proptools.BoolPtr(true)
2168 module.Module.dexpreopter.isTest = true
2169
Paul Duffin42df1442019-03-20 12:45:53 +00002170 InitJavaModule(module, android.HostAndDeviceSupported)
2171 return module
2172}
2173
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002174// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2175// and makes sure that it is added to the appropriate test suite.
2176//
2177// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2178// compiled against an Android classpath.
2179//
2180// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2181// for host modules.
2182func JavaTestImportFactory() android.Module {
2183 module := &JavaTestImport{}
2184
2185 module.AddProperties(
2186 &module.Import.properties,
2187 &module.prebuiltTestProperties)
2188
2189 module.Import.properties.Installable = proptools.BoolPtr(true)
2190
2191 android.InitPrebuiltModule(module, &module.properties.Jars)
2192 android.InitApexModule(module)
2193 android.InitSdkAwareModule(module)
2194 InitJavaModule(module, android.HostAndDeviceSupported)
2195 return module
2196}
2197
Colin Cross1b16b0e2019-02-12 14:41:32 -08002198// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2199// allow running the test with `atest` or a `TEST_MAPPING` file.
2200//
2201// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2202// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002203func TestHostFactory() android.Module {
2204 module := &Test{}
2205
2206 module.AddProperties(
2207 &module.Module.properties,
2208 &module.Module.protoProperties,
2209 &module.testProperties)
2210
Colin Cross9ae1b922018-06-26 17:59:05 -07002211 module.Module.properties.Installable = proptools.BoolPtr(true)
2212
Colin Cross05638fc2018-04-09 18:40:24 -07002213 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002214 return module
2215}
2216
2217//
Colin Cross2fe66872015-03-30 17:20:39 -07002218// Java Binaries (.jar file plus wrapper script)
2219//
2220
Colin Crossf506d872017-07-19 15:53:04 -07002221type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002222 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002223 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002224
2225 // Name of the class containing main to be inserted into the manifest as Main-Class.
2226 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002227}
2228
Colin Crossf506d872017-07-19 15:53:04 -07002229type Binary struct {
2230 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002231
Colin Crossf506d872017-07-19 15:53:04 -07002232 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002233
Colin Cross6b4a32d2017-12-05 13:42:45 -08002234 isWrapperVariant bool
2235
Colin Crossc3315992017-12-08 19:12:36 -08002236 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002237 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002238}
2239
Alex Light24237172017-10-26 09:46:21 -07002240func (j *Binary) HostToolPath() android.OptionalPath {
2241 return android.OptionalPathForPath(j.binaryFile)
2242}
2243
Colin Crossf506d872017-07-19 15:53:04 -07002244func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002245 if ctx.Arch().ArchType == android.Common {
2246 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002247 if j.binaryProperties.Main_class != nil {
2248 if j.properties.Manifest != nil {
2249 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2250 }
2251 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2252 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2253 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2254 }
2255
Colin Cross6b4a32d2017-12-05 13:42:45 -08002256 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002257 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002258 // Handle the binary wrapper
2259 j.isWrapperVariant = true
2260
Colin Cross366938f2017-12-11 16:29:02 -08002261 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002262 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002263 } else {
2264 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2265 }
2266
2267 // Depend on the installed jar so that the wrapper doesn't get executed by
2268 // another build rule before the jar has been installed.
2269 jarFile := ctx.PrimaryModule().(*Binary).installFile
2270
2271 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2272 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002273 }
Colin Cross2fe66872015-03-30 17:20:39 -07002274}
2275
Colin Crossf506d872017-07-19 15:53:04 -07002276func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002277 if ctx.Arch().ArchType == android.Common {
2278 j.deps(ctx)
2279 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002280}
2281
Colin Cross1b16b0e2019-02-12 14:41:32 -08002282// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2283// as well.
2284//
2285// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2286// compiled against the device bootclasspath.
2287//
2288// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2289// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002290func BinaryFactory() android.Module {
2291 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002292
Colin Cross36242852017-06-23 15:06:31 -07002293 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002294 &module.Module.properties,
2295 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002296 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002297 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002298 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002299
Colin Cross9ae1b922018-06-26 17:59:05 -07002300 module.Module.properties.Installable = proptools.BoolPtr(true)
2301
Colin Cross6b4a32d2017-12-05 13:42:45 -08002302 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2303 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002304 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002305}
2306
Colin Cross1b16b0e2019-02-12 14:41:32 -08002307// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2308//
2309// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2310// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002311func BinaryHostFactory() android.Module {
2312 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002313
Colin Cross36242852017-06-23 15:06:31 -07002314 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002315 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002316 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002317 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002318
Colin Cross9ae1b922018-06-26 17:59:05 -07002319 module.Module.properties.Installable = proptools.BoolPtr(true)
2320
Colin Cross6b4a32d2017-12-05 13:42:45 -08002321 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2322 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002323 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002324}
2325
2326//
2327// Java prebuilts
2328//
2329
Colin Cross74d73e22017-08-02 11:05:49 -07002330type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002331 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002332
Nan Zhangea568a42017-11-08 21:20:04 -08002333 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002334
2335 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002336
2337 // List of shared java libs that this module has dependencies to
2338 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002339
2340 // List of files to remove from the jar file(s)
2341 Exclude_files []string
2342
2343 // List of directories to remove from the jar file(s)
2344 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002345
2346 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002347 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002348
2349 // set the name of the output
2350 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002351}
2352
2353type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002354 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002355 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002356 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002357 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002358 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002359
Colin Cross74d73e22017-08-02 11:05:49 -07002360 properties ImportProperties
2361
Colin Cross0a6e0072017-08-30 14:24:55 -07002362 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002363 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002364}
2365
Jiyong Park6a927c42020-01-21 02:03:43 +09002366func (j *Import) sdkVersion() sdkSpec {
2367 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002368}
2369
Jiyong Park6a927c42020-01-21 02:03:43 +09002370func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002371 return j.sdkVersion()
2372}
2373
Colin Cross74d73e22017-08-02 11:05:49 -07002374func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002375 return &j.prebuilt
2376}
2377
Colin Cross74d73e22017-08-02 11:05:49 -07002378func (j *Import) PrebuiltSrcs() []string {
2379 return j.properties.Jars
2380}
2381
2382func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002383 return j.prebuilt.Name(j.ModuleBase.Name())
2384}
2385
Jiyong Park0b238752019-10-29 11:23:10 +09002386func (j *Import) Stem() string {
2387 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2388}
2389
Jiyong Park618922e2020-01-08 13:35:43 +09002390func (a *Import) JacocoReportClassesFile() android.Path {
2391 return nil
2392}
2393
Colin Cross74d73e22017-08-02 11:05:49 -07002394func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002395 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002396}
2397
Colin Cross74d73e22017-08-02 11:05:49 -07002398func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002399 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002400
Jiyong Park0b238752019-10-29 11:23:10 +09002401 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002402 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002403 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2404 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002405 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002406 inputFile := outputFile
2407 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2408 TransformJetifier(ctx, outputFile, inputFile)
2409 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002410 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002411
2412 ctx.VisitDirectDeps(func(module android.Module) {
2413 otherName := ctx.OtherModuleName(module)
2414 tag := ctx.OtherModuleDependencyTag(module)
2415
2416 switch dep := module.(type) {
2417 case Dependency:
2418 switch tag {
2419 case libTag, staticLibTag:
2420 // sdk lib names from dependencies are re-exported
2421 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2422 }
2423 case SdkLibraryDependency:
2424 switch tag {
2425 case libTag:
2426 // names of sdk libs that are directly depended are exported
2427 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2428 }
2429 }
2430 })
2431
2432 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002433 if Bool(j.properties.Installable) {
2434 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002435 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002436 }
Colin Cross2fe66872015-03-30 17:20:39 -07002437}
2438
Colin Cross74d73e22017-08-02 11:05:49 -07002439var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002440
Nan Zhanged19fc32017-10-19 13:06:22 -07002441func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002442 if j.combinedClasspathFile == nil {
2443 return nil
2444 }
Colin Cross37f6d792018-07-12 12:28:41 -07002445 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002446}
2447
2448func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002449 if j.combinedClasspathFile == nil {
2450 return nil
2451 }
Colin Cross37f6d792018-07-12 12:28:41 -07002452 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002453}
2454
Colin Cross331a1212018-08-15 20:40:52 -07002455func (j *Import) ResourceJars() android.Paths {
2456 return nil
2457}
2458
2459func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002460 if j.combinedClasspathFile == nil {
2461 return nil
2462 }
Colin Cross331a1212018-08-15 20:40:52 -07002463 return android.Paths{j.combinedClasspathFile}
2464}
2465
Colin Crossf24a22a2019-01-31 14:12:44 -08002466func (j *Import) DexJar() android.Path {
2467 return nil
2468}
2469
Colin Cross74d73e22017-08-02 11:05:49 -07002470func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002471 return nil
2472}
2473
Jiyong Park1be96912018-05-28 18:02:19 +09002474func (j *Import) ExportedSdkLibs() []string {
2475 return j.exportedSdkLibs
2476}
2477
Artur Satayev9cf46692019-11-26 18:08:34 +00002478func (j *Import) ExportedPlugins() (android.Paths, []string) {
2479 return nil, nil
2480}
2481
Colin Cross0c4ce212019-05-03 15:28:19 -07002482func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2483 return nil, nil
2484}
2485
Jiyong Park0f80c182020-01-31 02:49:53 +09002486func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2487 depTag := ctx.OtherModuleDependencyTag(dep)
2488 // dependencies other than the static linkage are all considered crossing APEX boundary
2489 // Also, a dependency to an sdk member is also considered as such. This is required because
2490 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2491 return depTag == staticLibTag || j.IsInAnySdk()
2492}
2493
albaltai36ff7dc2018-12-25 14:35:23 +08002494// Add compile time check for interface implementation
2495var _ android.IDEInfo = (*Import)(nil)
2496var _ android.IDECustomizedModuleName = (*Import)(nil)
2497
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002498// Collect information for opening IDE project files in java/jdeps.go.
2499const (
2500 removedPrefix = "prebuilt_"
2501)
2502
2503func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2504 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2505}
2506
2507func (j *Import) IDECustomizedModuleName() string {
2508 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2509 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2510 // solution to get the Import name.
2511 name := j.Name()
2512 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002513 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002514 }
2515 return name
2516}
2517
Colin Cross74d73e22017-08-02 11:05:49 -07002518var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002519
Colin Cross1b16b0e2019-02-12 14:41:32 -08002520// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2521//
2522// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2523// compiled against an Android classpath.
2524//
2525// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2526// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002527func ImportFactory() android.Module {
2528 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002529
Colin Cross74d73e22017-08-02 11:05:49 -07002530 module.AddProperties(&module.properties)
2531
2532 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002533 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002534 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002535 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002536 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002537}
2538
Colin Cross1b16b0e2019-02-12 14:41:32 -08002539// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2540// module.
2541//
2542// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2543// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002544func ImportFactoryHost() android.Module {
2545 module := &Import{}
2546
2547 module.AddProperties(&module.properties)
2548
2549 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002550 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002551 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002552 return module
2553}
2554
Colin Cross42be7612019-02-21 18:12:14 -08002555// dex_import module
2556
2557type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002558 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002559
2560 // set the name of the output
2561 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002562}
2563
2564type DexImport struct {
2565 android.ModuleBase
2566 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002567 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002568 prebuilt android.Prebuilt
2569
2570 properties DexImportProperties
2571
2572 dexJarFile android.Path
2573 maybeStrippedDexJarFile android.Path
2574
2575 dexpreopter
2576}
2577
2578func (j *DexImport) Prebuilt() *android.Prebuilt {
2579 return &j.prebuilt
2580}
2581
2582func (j *DexImport) PrebuiltSrcs() []string {
2583 return j.properties.Jars
2584}
2585
2586func (j *DexImport) Name() string {
2587 return j.prebuilt.Name(j.ModuleBase.Name())
2588}
2589
Jiyong Park0b238752019-10-29 11:23:10 +09002590func (j *DexImport) Stem() string {
2591 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2592}
2593
Martin Stjernholm6d415272020-01-31 17:10:36 +00002594func (j *DexImport) IsInstallable() bool {
2595 return true
2596}
2597
Colin Cross42be7612019-02-21 18:12:14 -08002598func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2599 if len(j.properties.Jars) != 1 {
2600 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2601 }
2602
Jiyong Park0b238752019-10-29 11:23:10 +09002603 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002604 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2605
2606 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2607 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2608
2609 if j.dexpreopter.uncompressedDex {
2610 rule := android.NewRuleBuilder()
2611
2612 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2613 rule.Temporary(temporary)
2614
2615 // use zip2zip to uncompress classes*.dex files
2616 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002617 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002618 FlagWithInput("-i ", inputJar).
2619 FlagWithOutput("-o ", temporary).
2620 FlagWithArg("-0 ", "'classes*.dex'")
2621
2622 // use zipalign to align uncompressed classes*.dex files
2623 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002624 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002625 Flag("-f").
2626 Text("4").
2627 Input(temporary).
2628 Output(dexOutputFile)
2629
2630 rule.DeleteTemporaryFiles()
2631
2632 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2633 } else {
2634 ctx.Build(pctx, android.BuildParams{
2635 Rule: android.Cp,
2636 Input: inputJar,
2637 Output: dexOutputFile,
2638 })
2639 }
2640
2641 j.dexJarFile = dexOutputFile
2642
2643 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2644
2645 j.maybeStrippedDexJarFile = dexOutputFile
2646
2647 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2648 ctx.ModuleName()+".jar", dexOutputFile)
2649}
2650
2651func (j *DexImport) DexJar() android.Path {
2652 return j.dexJarFile
2653}
2654
2655// dex_import imports a `.jar` file containing classes.dex files.
2656//
2657// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2658// to the device.
2659func DexImportFactory() android.Module {
2660 module := &DexImport{}
2661
2662 module.AddProperties(&module.properties)
2663
2664 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002665 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002666 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002667 return module
2668}
2669
Colin Cross89536d42017-07-07 14:35:50 -07002670//
2671// Defaults
2672//
2673type Defaults struct {
2674 android.ModuleBase
2675 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002676 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002677}
2678
Colin Cross1b16b0e2019-02-12 14:41:32 -08002679// java_defaults provides a set of properties that can be inherited by other java or android modules.
2680//
2681// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2682// property in the defaults module that exists in the depending module will be prepended to the depending module's
2683// value for that property.
2684//
2685// Example:
2686//
2687// java_defaults {
2688// name: "example_defaults",
2689// srcs: ["common/**/*.java"],
2690// javacflags: ["-Xlint:all"],
2691// aaptflags: ["--auto-add-overlay"],
2692// }
2693//
2694// java_library {
2695// name: "example",
2696// defaults: ["example_defaults"],
2697// srcs: ["example/**/*.java"],
2698// }
2699//
2700// is functionally identical to:
2701//
2702// java_library {
2703// name: "example",
2704// srcs: [
2705// "common/**/*.java",
2706// "example/**/*.java",
2707// ],
2708// javacflags: ["-Xlint:all"],
2709// }
Colin Cross89536d42017-07-07 14:35:50 -07002710func defaultsFactory() android.Module {
2711 return DefaultsFactory()
2712}
2713
Paul Duffin47357662019-12-05 14:07:14 +00002714func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002715 module := &Defaults{}
2716
Colin Cross89536d42017-07-07 14:35:50 -07002717 module.AddProperties(
2718 &CompilerProperties{},
2719 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002720 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002721 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002722 &aaptProperties{},
2723 &androidLibraryProperties{},
2724 &appProperties{},
2725 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002726 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002727 &ImportProperties{},
2728 &AARImportProperties{},
2729 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002730 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002731 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002732 )
2733
2734 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002735 return module
2736}
Nan Zhangea568a42017-11-08 21:20:04 -08002737
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002738func kytheExtractJavaFactory() android.Singleton {
2739 return &kytheExtractJavaSingleton{}
2740}
2741
2742type kytheExtractJavaSingleton struct {
2743}
2744
2745func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2746 var xrefTargets android.Paths
2747 ctx.VisitAllModules(func(module android.Module) {
2748 if javaModule, ok := module.(xref); ok {
2749 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2750 }
2751 })
2752 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2753 if len(xrefTargets) > 0 {
2754 ctx.Build(pctx, android.BuildParams{
2755 Rule: blueprint.Phony,
2756 Output: android.PathForPhony(ctx, "xref_java"),
2757 Inputs: xrefTargets,
2758 })
2759 }
2760}
2761
Nan Zhangea568a42017-11-08 21:20:04 -08002762var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002763var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002764var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002765var inList = android.InList