blob: 41fd8da9752a7e4ef87226ea0c567a9f23f454d4 [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 Park2d492942018-03-05 17:44:10 +0900810 javaPlatform
811)
812
Jeongik Cha75b83b02019-11-01 15:28:00 +0900813type linkTypeContext interface {
814 android.Module
815 getLinkType(name string) (ret linkType, stubs bool)
816}
817
818func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700819 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700820 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900821 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
822 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100823 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900824 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900825 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900826 return javaCore, false
827 case name == "android_system_stubs_current":
828 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900829 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900830 return javaSystem, false
831 case name == "android_test_stubs_current":
832 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900833 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900834 return javaPlatform, false
835 case name == "android_stubs_current":
836 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900837 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900838 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900839 case name == "android_module_lib_stubs_current":
840 return javaModule, true
841 case ver.kind == sdkModule:
842 return javaModule, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900843 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900844 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900845 case !ver.valid():
846 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700847 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900848 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900849 }
850}
851
Jeongik Cha75b83b02019-11-01 15:28:00 +0900852func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700853 if ctx.Host() {
854 return
855 }
856
Jeongik Cha75b83b02019-11-01 15:28:00 +0900857 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900858 if stubs {
859 return
860 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900861 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900862 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."
863
864 switch myLinkType {
865 case javaCore:
866 if otherLinkType != javaCore {
867 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 +0900868 ctx.OtherModuleName(to))
869 }
Jiyong Park2d492942018-03-05 17:44:10 +0900870 break
871 case javaSdk:
872 if otherLinkType != javaCore && otherLinkType != javaSdk {
873 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
874 ctx.OtherModuleName(to))
875 }
876 break
877 case javaSystem:
Jiyong Park50146e92020-01-30 18:00:15 +0900878 if otherLinkType == javaPlatform || otherLinkType == javaModule {
Jiyong Park2d492942018-03-05 17:44:10 +0900879 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
880 ctx.OtherModuleName(to))
881 }
882 break
Jiyong Park50146e92020-01-30 18:00:15 +0900883 case javaModule:
884 if otherLinkType == javaPlatform {
885 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
886 ctx.OtherModuleName(to))
887 }
888 break
Jiyong Park2d492942018-03-05 17:44:10 +0900889 case javaPlatform:
890 // no restriction on link-type
891 break
Jiyong Park750e5572018-01-31 00:20:13 +0900892 }
893}
894
Colin Cross32f676a2017-09-06 13:41:06 -0700895func (j *Module) collectDeps(ctx android.ModuleContext) deps {
896 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700897
Colin Cross300f0382018-03-06 13:11:51 -0800898 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700899 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800900 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700901 ctx.AddMissingDependencies(sdkDep.bootclasspath)
902 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800903 } else if sdkDep.useFiles {
904 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700905 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700906 deps.aidlPreprocess = sdkDep.aidl
907 } else {
908 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800909 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700910 }
911
Colin Crossd11fcda2017-10-23 17:59:01 -0700912 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700913 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700914 tag := ctx.OtherModuleDependencyTag(module)
915
Colin Crossa4f08812018-10-02 22:03:40 -0700916 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700917 // Handled by AndroidApp.collectAppDeps
918 return
919 }
920 if tag == certificateTag {
921 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700922 return
923 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900924 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900925 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900926 if to, ok := module.(linkTypeContext); ok {
927 switch tag {
928 case bootClasspathTag, libTag, staticLibTag:
929 checkLinkType(ctx, j, to, tag.(dependencyTag))
930 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700931 }
Jiyong Park750e5572018-01-31 00:20:13 +0900932 }
Colin Cross54250902017-12-05 09:28:08 -0800933 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800934 case SdkLibraryDependency:
935 switch tag {
936 case libTag:
937 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
938 // names of sdk libs that are directly depended are exported
939 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700940 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800941 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
942 }
Colin Cross54250902017-12-05 09:28:08 -0800943 case Dependency:
944 switch tag {
945 case bootClasspathTag:
946 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700947 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800948 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900949 // sdk lib names from dependencies are re-exported
950 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700951 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000952 pluginJars, pluginClasses := dep.ExportedPlugins()
953 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700954 case java9LibTag:
955 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800956 case staticLibTag:
957 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
958 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
959 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700960 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900961 // sdk lib names from dependencies are re-exported
962 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700963 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000964 pluginJars, pluginClasses := dep.ExportedPlugins()
965 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800966 case pluginTag:
967 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800968 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000969 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
970 } else {
971 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800972 }
973 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
974 } else {
975 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
976 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000977 case exportedPluginTag:
978 if plugin, ok := dep.(*Plugin); ok {
979 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
980 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
981 }
982 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
983 if plugin.pluginProperties.Processor_class != nil {
984 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
985 }
986 } else {
987 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
988 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800989 case frameworkApkTag:
990 if ctx.ModuleName() == "android_stubs_current" ||
991 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700992 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800993 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
994 // resource files out of there for aapt.
995 //
996 // Normally the package rule runs aapt, which includes the resource,
997 // but we're not running that in our package rule so just copy in the
998 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700999 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001000 }
Colin Cross54250902017-12-05 09:28:08 -08001001 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -07001002 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -08001003 case kotlinAnnotationsTag:
1004 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -08001005 }
1006
Colin Cross54250902017-12-05 09:28:08 -08001007 case android.SourceFileProducer:
1008 switch tag {
1009 case libTag:
1010 checkProducesJars(ctx, dep)
1011 deps.classpath = append(deps.classpath, dep.Srcs()...)
1012 case staticLibTag:
1013 checkProducesJars(ctx, dep)
1014 deps.classpath = append(deps.classpath, dep.Srcs()...)
1015 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
1016 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -08001017 }
1018 default:
Colin Crossec7a0422017-07-07 14:47:12 -07001019 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001020 case bootClasspathTag:
1021 // If a system modules dependency has been added to the bootclasspath
1022 // then add its libs to the bootclasspath.
1023 sm := module.(*SystemModules)
1024 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
1025
Colin Cross1369cdb2017-09-29 17:58:17 -07001026 case systemModulesTag:
1027 if deps.systemModules != nil {
1028 panic("Found two system module dependencies")
1029 }
1030 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +00001031 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -07001032 panic("Missing directory for system module dependency")
1033 }
Colin Crossb77043e2019-07-16 13:57:13 -07001034 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001035 }
Colin Crossec7a0422017-07-07 14:47:12 -07001036 }
Colin Cross2fe66872015-03-30 17:20:39 -07001037 })
1038
Jiyong Park1be96912018-05-28 18:02:19 +09001039 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1040
Colin Cross32f676a2017-09-06 13:41:06 -07001041 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001042}
1043
Artur Satayev9cf46692019-11-26 18:08:34 +00001044func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1045 deps.processorPath = append(deps.processorPath, pluginJars...)
1046 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1047}
1048
Colin Cross1e743852019-10-28 11:37:20 -07001049func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001050 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001051 if err != nil {
1052 ctx.PropertyErrorf("sdk_version", "%s", err)
1053 }
Nan Zhang357466b2018-04-17 17:38:36 -07001054 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001055 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001056 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001057 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001058 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001059 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001060 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1061 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001062 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001063 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001064 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001065 }
Nan Zhang357466b2018-04-17 17:38:36 -07001066}
1067
Colin Cross1e743852019-10-28 11:37:20 -07001068type javaVersion int
1069
1070const (
1071 JAVA_VERSION_UNSUPPORTED = 0
1072 JAVA_VERSION_6 = 6
1073 JAVA_VERSION_7 = 7
1074 JAVA_VERSION_8 = 8
1075 JAVA_VERSION_9 = 9
1076)
1077
1078func (v javaVersion) String() string {
1079 switch v {
1080 case JAVA_VERSION_6:
1081 return "1.6"
1082 case JAVA_VERSION_7:
1083 return "1.7"
1084 case JAVA_VERSION_8:
1085 return "1.8"
1086 case JAVA_VERSION_9:
1087 return "1.9"
1088 default:
1089 return "unsupported"
1090 }
1091}
1092
1093// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1094func (v javaVersion) usesJavaModules() bool {
1095 return v >= 9
1096}
1097
1098func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001099 switch javaVersion {
1100 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001101 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001102 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001103 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001104 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001105 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001106 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001107 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001108 case "10", "11":
1109 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001110 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001111 default:
1112 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001113 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001114 }
1115}
1116
Nan Zhanged19fc32017-10-19 13:06:22 -07001117func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001118
Colin Crossf03c82b2015-04-13 13:53:40 -07001119 var flags javaBuilderFlags
1120
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001121 // javaVersion flag.
1122 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1123
Nan Zhanged19fc32017-10-19 13:06:22 -07001124 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001125 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001126 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001127 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001128 }
Colin Cross6510f912017-11-29 00:27:14 -08001129 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001130 // Override the -g flag passed globally to remove local variable debug info to reduce
1131 // disk and memory usage.
1132 javacFlags = append(javacFlags, "-g:source,lines")
1133 }
Colin Crossc228a702019-11-06 16:18:05 -08001134 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001135
Colin Cross66548102018-06-19 22:47:35 -07001136 if ctx.Config().RunErrorProne() {
1137 if config.ErrorProneClasspath == nil {
1138 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1139 }
1140
1141 errorProneFlags := []string{
1142 "-Xplugin:ErrorProne",
1143 "${config.ErrorProneChecks}",
1144 }
1145 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1146
1147 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1148 "'" + strings.Join(errorProneFlags, " ") + "'"
1149 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001150 }
1151
Nan Zhanged19fc32017-10-19 13:06:22 -07001152 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001153 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1154 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001155 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001156 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001157
Colin Crossbe9cdb82019-01-21 21:37:16 -08001158 flags.processor = strings.Join(deps.processorClasses, ",")
1159
Colin Cross1e743852019-10-28 11:37:20 -07001160 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1161 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001162 // Give host-side tools a version of OpenJDK's standard libraries
1163 // close to what they're targeting. As of Dec 2017, AOSP is only
1164 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1165 //
1166 // When building with OpenJDK 8, the following should have no
1167 // effect since those jars would be available by default.
1168 //
1169 // When building with OpenJDK 9 but targeting a version < 1.8,
1170 // putting them on the bootclasspath means that:
1171 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1172 // b) references to existing APIs are not reinterpreted in an
1173 // OpenJDK 9-specific way, eg. calls to subclasses of
1174 // java.nio.Buffer as in http://b/70862583
1175 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1176 flags.bootClasspath = append(flags.bootClasspath,
1177 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1178 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001179 if Bool(j.properties.Use_tools_jar) {
1180 flags.bootClasspath = append(flags.bootClasspath,
1181 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1182 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001183 }
1184
Colin Cross1e743852019-10-28 11:37:20 -07001185 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001186 // Manually specify build directory in case it is not under the repo root.
1187 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1188 // just adding a symlink under the root doesn't help.)
1189 patchPaths := ".:" + ctx.Config().BuildDir()
1190 classPath := flags.classpath.FormJavaClassPath("")
1191 if classPath != "" {
1192 patchPaths += ":" + classPath
1193 }
1194 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001195 }
1196
Nan Zhanged19fc32017-10-19 13:06:22 -07001197 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001198 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001199
Nan Zhanged19fc32017-10-19 13:06:22 -07001200 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001201 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001202
Colin Cross81440082018-08-15 20:21:55 -07001203 if len(javacFlags) > 0 {
1204 // optimization.
1205 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1206 flags.javacFlags = "$javacFlags"
1207 }
1208
Nan Zhanged19fc32017-10-19 13:06:22 -07001209 return flags
1210}
Colin Crossc0b06f12015-04-08 13:03:43 -07001211
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001212func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001213 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001214
1215 deps := j.collectDeps(ctx)
1216 flags := j.collectBuilderFlags(ctx, deps)
1217
Colin Cross1e743852019-10-28 11:37:20 -07001218 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001219 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1220 }
Colin Cross8a497952019-03-05 22:25:09 -08001221 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001222 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001223 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001224 }
1225
Colin Crossaf050172017-11-15 23:01:59 -08001226 srcFiles = j.genSources(ctx, srcFiles, flags)
1227
1228 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001229 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001230 if aaptSrcJar != nil {
1231 srcJars = append(srcJars, aaptSrcJar)
1232 }
Colin Crossb7a63242015-04-16 14:09:14 -07001233
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001234 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001235 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001236 }
1237
Colin Cross1ee23172017-10-18 14:44:18 -07001238 jarName := ctx.ModuleName() + ".jar"
1239
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001240 javaSrcFiles := srcFiles.FilterByExt(".java")
1241 var uniqueSrcFiles android.Paths
1242 set := make(map[string]bool)
1243 for _, v := range javaSrcFiles {
1244 if _, found := set[v.String()]; !found {
1245 set[v.String()] = true
1246 uniqueSrcFiles = append(uniqueSrcFiles, v)
1247 }
1248 }
1249
patricktu242faad2019-09-24 15:41:30 +08001250 // Collect .java files for AIDEGen
1251 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1252
Colin Cross55f63ea2018-08-27 12:37:09 -07001253 var kotlinJars android.Paths
1254
Colin Cross93e85952017-08-15 13:34:18 -07001255 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001256 // user defined kotlin flags.
1257 kotlincFlags := j.properties.Kotlincflags
1258 CheckKotlincFlags(ctx, kotlincFlags)
1259
Colin Cross93e85952017-08-15 13:34:18 -07001260 // If there are kotlin files, compile them first but pass all the kotlin and java files
1261 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1262 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001263 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001264 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001265 kotlincFlags = append(kotlincFlags, "-no-jdk")
1266 }
1267 if len(kotlincFlags) > 0 {
1268 // optimization.
1269 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1270 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001271 }
1272
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001273 var kotlinSrcFiles android.Paths
1274 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1275 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1276
patricktu242faad2019-09-24 15:41:30 +08001277 // Collect .kt files for AIDEGen
1278 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1279
Colin Crossafbb1732019-01-17 15:42:52 -08001280 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1281 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1282
1283 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1284 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1285
1286 if len(flags.processorPath) > 0 {
1287 // Use kapt for annotation processing
1288 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1289 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1290 srcJars = append(srcJars, kaptSrcJar)
1291 // Disable annotation processing in javac, it's already been handled by kapt
1292 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001293 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001294 }
Colin Cross93e85952017-08-15 13:34:18 -07001295
Colin Cross1ee23172017-10-18 14:44:18 -07001296 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001297 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001298 if ctx.Failed() {
1299 return
1300 }
1301
1302 // Make javac rule depend on the kotlinc rule
1303 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001304
Colin Cross93e85952017-08-15 13:34:18 -07001305 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001306 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001307 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001308 }
1309
Colin Cross55f63ea2018-08-27 12:37:09 -07001310 jars := append(android.Paths(nil), kotlinJars...)
1311
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001312 // Store the list of .java files that was passed to javac
1313 j.compiledJavaSrcs = uniqueSrcFiles
1314 j.compiledSrcJars = srcJars
1315
Nan Zhang61eaedb2017-11-02 13:28:15 -07001316 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001317 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001318 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1319 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001320 // Formerly, there was a check here that prevented annotation processors
1321 // from being used when sharding was enabled, as some annotation processors
1322 // do not function correctly in sharded environments. It was removed to
1323 // allow for the use of annotation processors that do function correctly
1324 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001325 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001326 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001327 if ctx.Failed() {
1328 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001329 }
1330 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001331 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001332 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001333 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001334 // If error-prone is enabled, add an additional rule to compile the java files into
1335 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001336 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001337 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1338 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001339 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001340 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001341 extraJarDeps = append(extraJarDeps, errorprone)
1342 }
1343
Nan Zhang61eaedb2017-11-02 13:28:15 -07001344 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001345 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001346 shardSize := int(*(j.properties.Javac_shard_size))
1347 var shardSrcs []android.Paths
1348 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001349 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001350 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001351 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1352 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001353 jars = append(jars, classes)
1354 }
1355 }
1356 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001357 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1358 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001359 jars = append(jars, classes)
1360 }
1361 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001362 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001363 jars = append(jars, classes)
1364 }
Colin Crossd6891432017-09-27 17:39:56 -07001365 if ctx.Failed() {
1366 return
1367 }
Colin Cross2fe66872015-03-30 17:20:39 -07001368 }
1369
Colin Cross0c4ce212019-05-03 15:28:19 -07001370 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1371
1372 var includeSrcJar android.WritablePath
1373 if Bool(j.properties.Include_srcs) {
1374 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1375 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1376 }
1377
Colin Crosscedd4762018-09-13 11:26:19 -07001378 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1379 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001380 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001381 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001382
1383 var resArgs []string
1384 var resDeps android.Paths
1385
1386 resArgs = append(resArgs, dirArgs...)
1387 resDeps = append(resDeps, dirDeps...)
1388
1389 resArgs = append(resArgs, fileArgs...)
1390 resDeps = append(resDeps, fileDeps...)
1391
Colin Cross988708c2019-05-06 14:04:11 -07001392 resArgs = append(resArgs, extraArgs...)
1393 resDeps = append(resDeps, extraDeps...)
1394
Colin Cross40a36712017-09-27 17:41:35 -07001395 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001396 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001397 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001398 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001399 if ctx.Failed() {
1400 return
1401 }
1402 }
1403
Colin Cross0c4ce212019-05-03 15:28:19 -07001404 var resourceJars android.Paths
1405 if j.resourceJar != nil {
1406 resourceJars = append(resourceJars, j.resourceJar)
1407 }
1408 if Bool(j.properties.Include_srcs) {
1409 resourceJars = append(resourceJars, includeSrcJar)
1410 }
1411 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001412
Colin Cross0c4ce212019-05-03 15:28:19 -07001413 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001414 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001415 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001416 false, nil, nil)
1417 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001418 } else if len(resourceJars) == 1 {
1419 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001420 }
1421
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001422 if len(deps.staticJars) > 0 {
1423 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001424 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001425
Colin Cross094054a2018-10-17 15:10:48 -07001426 manifest := j.overrideManifest
1427 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001428 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001429 }
Colin Cross635acc92017-09-12 22:50:46 -07001430
Colin Cross8a497952019-03-05 22:25:09 -08001431 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001432 if len(services) > 0 {
1433 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1434 var zipargs []string
1435 for _, file := range services {
1436 serviceFile := file.String()
1437 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1438 }
1439 ctx.Build(pctx, android.BuildParams{
1440 Rule: zip,
1441 Output: servicesJar,
1442 Implicits: services,
1443 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001444 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001445 },
1446 })
1447 jars = append(jars, servicesJar)
1448 }
1449
Colin Cross0a6e0072017-08-30 14:24:55 -07001450 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001451 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001452 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001453
1454 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001455 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1456 // Optimization: skip the combine step if there is nothing to do
1457 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1458 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1459 // any if len(jars) == 1.
1460 outputFile = moduleOutPath
1461 } else {
1462 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1463 ctx.Build(pctx, android.BuildParams{
1464 Rule: android.Cp,
1465 Input: jars[0],
1466 Output: combinedJar,
1467 })
1468 outputFile = combinedJar
1469 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001470 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001471 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001472 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001473 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001474 outputFile = combinedJar
1475 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001476
Colin Cross331a1212018-08-15 20:40:52 -07001477 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001478 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001479 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001480 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001481 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001482 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001483
1484 // jarjar resource jar if necessary
1485 if j.resourceJar != nil {
1486 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001487 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001488 j.resourceJar = resourceJarJarFile
1489 }
1490
Colin Cross0a6e0072017-08-30 14:24:55 -07001491 if ctx.Failed() {
1492 return
1493 }
1494 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001495
1496 // Check package restrictions if necessary.
1497 if len(j.properties.Permitted_packages) > 0 {
1498 // Check packages and copy to package-checked file.
1499 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1500 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1501 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1502
1503 if ctx.Failed() {
1504 return
1505 }
1506 }
1507
Nan Zhanged19fc32017-10-19 13:06:22 -07001508 j.implementationJarFile = outputFile
1509 if j.headerJarFile == nil {
1510 j.headerJarFile = j.implementationJarFile
1511 }
Colin Cross2fe66872015-03-30 17:20:39 -07001512
Jiyong Park33b66542020-02-12 10:39:32 +09001513 // Force enable the instrumentation for java code that is built for APEXes
1514 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1515 j.properties.Instrument = true
1516 }
1517
Colin Cross3144dfc2018-01-03 15:06:47 -08001518 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001519 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1520 }
1521
Colin Cross331a1212018-08-15 20:40:52 -07001522 // merge implementation jar with resources if necessary
1523 implementationAndResourcesJar := outputFile
1524 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001525 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001526 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001527 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001528 false, nil, nil)
1529 implementationAndResourcesJar = combinedJar
1530 }
1531
1532 j.implementationAndResourcesJar = implementationAndResourcesJar
1533
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001534 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1535 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1536 if j.deviceProperties.Compile_dex == nil {
1537 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1538 }
1539 if j.deviceProperties.Hostdex == nil {
1540 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1541 }
1542 }
1543
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001544 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001545 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001546 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001547 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001548 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001549 if ctx.Failed() {
1550 return
1551 }
Colin Cross331a1212018-08-15 20:40:52 -07001552
Jiyong Park09cb6292019-07-15 15:29:23 +09001553 // Hidden API CSV generation and dex encoding
1554 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1555 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001556
Colin Cross331a1212018-08-15 20:40:52 -07001557 // merge dex jar with resources if necessary
1558 if j.resourceJar != nil {
1559 jars := android.Paths{dexOutputFile, j.resourceJar}
1560 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1561 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1562 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001563 if j.deviceProperties.UncompressDex {
1564 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1565 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1566 dexOutputFile = combinedAlignedJar
1567 } else {
1568 dexOutputFile = combinedJar
1569 }
Colin Cross331a1212018-08-15 20:40:52 -07001570 }
1571
1572 j.dexJarFile = dexOutputFile
1573
Colin Cross8faf8fc2019-01-16 15:15:52 -08001574 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001575 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1576
1577 j.maybeStrippedDexJarFile = dexOutputFile
1578
Colin Cross3063b782018-08-15 11:19:12 -07001579 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001580
1581 if ctx.Failed() {
1582 return
1583 }
Colin Cross331a1212018-08-15 20:40:52 -07001584 } else {
1585 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001586 }
Colin Cross331a1212018-08-15 20:40:52 -07001587
Colin Crossb7a63242015-04-16 14:09:14 -07001588 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001589
1590 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1591 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001592}
1593
Colin Cross3b706fd2019-09-05 16:44:18 -07001594func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1595 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1596
1597 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1598 if idx >= 0 {
1599 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1600 jarName += strconv.Itoa(idx)
1601 }
1602
1603 classes := android.PathForModuleOut(ctx, "javac", jarName)
1604 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1605
1606 if ctx.Config().EmitXrefRules() {
1607 extractionFile := android.PathForModuleOut(ctx, kzipName)
1608 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1609 j.kytheFiles = append(j.kytheFiles, extractionFile)
1610 }
1611
1612 return classes
1613}
1614
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001615// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1616// since some of these flags may be used internally.
1617func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1618 for _, flag := range flags {
1619 flag = strings.TrimSpace(flag)
1620
1621 if !strings.HasPrefix(flag, "-") {
1622 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1623 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1624 ctx.PropertyErrorf("kotlincflags",
1625 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1626 } else if inList(flag, config.KotlincIllegalFlags) {
1627 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1628 } else if flag == "-include-runtime" {
1629 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1630 } else {
1631 args := strings.Split(flag, " ")
1632 if args[0] == "-kotlin-home" {
1633 ctx.PropertyErrorf("kotlincflags",
1634 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1635 }
1636 }
1637 }
1638}
1639
Colin Cross8eadbf02017-10-24 17:46:00 -07001640func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001641 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001642
1643 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001644 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001645 // Compile java sources into turbine.jar.
1646 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1647 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1648 if ctx.Failed() {
1649 return nil
1650 }
1651 jars = append(jars, turbineJar)
1652 }
1653
Colin Cross55f63ea2018-08-27 12:37:09 -07001654 jars = append(jars, extraJars...)
1655
Nan Zhanged19fc32017-10-19 13:06:22 -07001656 // Combine any static header libraries into classes-header.jar. If there is only
1657 // one input jar this step will be skipped.
1658 var headerJar android.Path
1659 jars = append(jars, deps.staticHeaderJars...)
1660
Colin Cross5c6ecc12017-10-23 18:12:27 -07001661 // we cannot skip the combine step for now if there is only one jar
1662 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1663 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001664 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001665 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001666 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001667
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001668 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001669 // Transform classes.jar into classes-jarjar.jar
1670 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001671 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001672 headerJar = jarjarFile
1673 if ctx.Failed() {
1674 return nil
1675 }
1676 }
1677
1678 return headerJar
1679}
1680
Colin Crosscb933592017-11-22 13:49:43 -08001681func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001682 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001683
Colin Cross7a3139e2017-12-19 13:57:50 -08001684 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001685
Colin Cross84c38822018-01-03 15:59:46 -08001686 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001687 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1688
1689 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1690
1691 j.jacocoReportClassesFile = jacocoReportClassesFile
1692
1693 return instrumentedJar
1694}
1695
albaltai36ff7dc2018-12-25 14:35:23 +08001696var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001697
Nan Zhanged19fc32017-10-19 13:06:22 -07001698func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001699 if j.headerJarFile == nil {
1700 return nil
1701 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001702 return android.Paths{j.headerJarFile}
1703}
1704
1705func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001706 if j.implementationJarFile == nil {
1707 return nil
1708 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001709 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001710}
1711
Colin Crossf24a22a2019-01-31 14:12:44 -08001712func (j *Module) DexJar() android.Path {
1713 return j.dexJarFile
1714}
1715
Colin Cross331a1212018-08-15 20:40:52 -07001716func (j *Module) ResourceJars() android.Paths {
1717 if j.resourceJar == nil {
1718 return nil
1719 }
1720 return android.Paths{j.resourceJar}
1721}
1722
1723func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001724 if j.implementationAndResourcesJar == nil {
1725 return nil
1726 }
Colin Cross331a1212018-08-15 20:40:52 -07001727 return android.Paths{j.implementationAndResourcesJar}
1728}
1729
Colin Cross46c9b8b2017-06-22 16:51:17 -07001730func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001731 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001732 return j.exportAidlIncludeDirs
1733}
1734
Jiyong Park1be96912018-05-28 18:02:19 +09001735func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001736 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001737 return j.exportedSdkLibs
1738}
1739
Artur Satayev9cf46692019-11-26 18:08:34 +00001740func (j *Module) ExportedPlugins() (android.Paths, []string) {
1741 return j.exportedPluginJars, j.exportedPluginClasses
1742}
1743
Colin Cross0c4ce212019-05-03 15:28:19 -07001744func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1745 return j.srcJarArgs, j.srcJarDeps
1746}
1747
Colin Cross46c9b8b2017-06-22 16:51:17 -07001748var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001749
Colin Cross46c9b8b2017-06-22 16:51:17 -07001750func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001751 return j.logtagsSrcs
1752}
1753
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001754// Collect information for opening IDE project files in java/jdeps.go.
1755func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1756 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1757 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001758 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001759 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001760 if j.expandJarjarRules != nil {
1761 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001762 }
1763}
1764
1765func (j *Module) CompilerDeps() []string {
1766 jdeps := []string{}
1767 jdeps = append(jdeps, j.properties.Libs...)
1768 jdeps = append(jdeps, j.properties.Static_libs...)
1769 return jdeps
1770}
1771
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001772func (j *Module) hasCode(ctx android.ModuleContext) bool {
1773 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1774 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1775}
1776
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001777func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1778 depTag := ctx.OtherModuleDependencyTag(dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09001779 // Dependencies other than the static linkage are all considered crossing APEX boundary
1780 // Also, a dependency to an sdk member is also considered as such. This is required because
1781 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
1782 return depTag == staticLibTag || j.IsInAnySdk()
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001783}
1784
Jiyong Park0b238752019-10-29 11:23:10 +09001785func (j *Module) Stem() string {
1786 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1787}
1788
Jiyong Park618922e2020-01-08 13:35:43 +09001789func (j *Module) JacocoReportClassesFile() android.Path {
1790 return j.jacocoReportClassesFile
1791}
1792
Martin Stjernholm6d415272020-01-31 17:10:36 +00001793func (j *Module) IsInstallable() bool {
1794 return Bool(j.properties.Installable)
1795}
1796
Colin Cross2fe66872015-03-30 17:20:39 -07001797//
1798// Java libraries (.jar file)
1799//
1800
Colin Crossf506d872017-07-19 15:53:04 -07001801type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001802 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001803
1804 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001805}
1806
Colin Cross42be7612019-02-21 18:12:14 -08001807func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001808 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1809 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1810 return true
1811 }
1812
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001813 // Store uncompressed (and do not strip) dex files from boot class path jars.
1814 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1815 return true
1816 }
1817
1818 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001819 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001820 return true
1821 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001822 if ctx.Config().UncompressPrivAppDex() &&
1823 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1824 return true
1825 }
1826
Colin Cross2fc72f62018-12-21 12:59:54 -08001827 return false
1828}
1829
Colin Crossf506d872017-07-19 15:53:04 -07001830func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001831 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001832 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001833 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001834 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001835 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001836 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001837
Jiyong Park7f7766d2019-07-25 22:02:35 +09001838 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1839 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001840 var extraInstallDeps android.Paths
1841 if j.InstallMixin != nil {
1842 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1843 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001844 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001845 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001846 }
Colin Crossb7a63242015-04-16 14:09:14 -07001847}
1848
Colin Crossf506d872017-07-19 15:53:04 -07001849func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001850 j.deps(ctx)
1851}
1852
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001853const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001854 aidlIncludeDir = "aidl"
1855 javaDir = "java"
1856 jarFileSuffix = ".jar"
1857 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001858)
1859
Paul Duffina0dbf432019-12-05 11:25:53 +00001860// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001861func sdkSnapshotFilePathForJar(member android.SdkMember) string {
1862 return sdkSnapshotFilePathForMember(member, jarFileSuffix)
1863}
1864
1865func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
1866 return filepath.Join(javaDir, member.Name()+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001867}
1868
Paul Duffin13879572019-11-28 14:31:38 +00001869type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001870 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001871}
1872
1873func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1874 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1875}
1876
1877func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1878 _, ok := module.(*Library)
1879 return ok
1880}
1881
Paul Duffina0dbf432019-12-05 11:25:53 +00001882func (mt *librarySdkMemberType) buildSnapshot(
1883 sdkModuleContext android.ModuleContext,
1884 builder android.SnapshotBuilder,
1885 member android.SdkMember,
1886 jarToExportGetter func(j *Library) android.Path) {
1887
Paul Duffin13879572019-11-28 14:31:38 +00001888 variants := member.Variants()
1889 if len(variants) != 1 {
1890 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1891 for _, variant := range variants {
1892 sdkModuleContext.ModuleErrorf(" %q", variant)
1893 }
1894 }
1895 variant := variants[0]
1896 j := variant.(*Library)
1897
Paul Duffina0dbf432019-12-05 11:25:53 +00001898 exportedJar := jarToExportGetter(j)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001899 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
Paul Duffina0dbf432019-12-05 11:25:53 +00001900 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001901
1902 for _, dir := range j.AidlIncludeDirs() {
1903 // TODO(jiyong): copy parcelable declarations only
1904 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1905 for _, file := range aidlFiles {
1906 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1907 }
1908 }
1909
Paul Duffin9d8d6092019-12-05 18:19:29 +00001910 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001911 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001912}
1913
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001914var javaHeaderLibsSdkMemberType android.SdkMemberType = &headerLibrarySdkMemberType{
1915 librarySdkMemberType{
1916 android.SdkMemberTypeBase{
1917 PropertyName: "java_header_libs",
1918 SupportsSdk: true,
1919 },
1920 },
1921}
1922
Paul Duffina0dbf432019-12-05 11:25:53 +00001923type headerLibrarySdkMemberType struct {
1924 librarySdkMemberType
1925}
1926
1927func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1928 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1929 headerJars := j.HeaderJars()
1930 if len(headerJars) != 1 {
1931 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1932 }
1933
1934 return headerJars[0]
1935 })
1936}
1937
Paul Duffina0dbf432019-12-05 11:25:53 +00001938type implLibrarySdkMemberType struct {
1939 librarySdkMemberType
1940}
1941
1942func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1943 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1944 implementationJars := j.ImplementationJars()
1945 if len(implementationJars) != 1 {
1946 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1947 }
1948
1949 return implementationJars[0]
1950 })
1951}
1952
Colin Cross1b16b0e2019-02-12 14:41:32 -08001953// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1954//
1955// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1956// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1957// as a `static_libs` dependency of another module.
1958//
1959// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1960// a device.
1961//
1962// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1963// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001964func LibraryFactory() android.Module {
1965 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001966
Colin Cross9ae1b922018-06-26 17:59:05 -07001967 module.AddProperties(
1968 &module.Module.properties,
1969 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001970 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001971 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001972
Jiyong Park7f7766d2019-07-25 22:02:35 +09001973 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001974 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001975 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001976 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001977}
1978
Colin Cross1b16b0e2019-02-12 14:41:32 -08001979// java_library_static is an obsolete alias for java_library.
1980func LibraryStaticFactory() android.Module {
1981 return LibraryFactory()
1982}
1983
1984// java_library_host builds and links sources into a `.jar` file for the host.
1985//
1986// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1987// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001988func LibraryHostFactory() android.Module {
1989 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001990
Colin Cross6af17aa2017-09-20 12:59:05 -07001991 module.AddProperties(
1992 &module.Module.properties,
1993 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001994
Colin Cross9ae1b922018-06-26 17:59:05 -07001995 module.Module.properties.Installable = proptools.BoolPtr(true)
1996
Jiyong Park7f7766d2019-07-25 22:02:35 +09001997 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001998 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001999 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002000}
2001
2002//
Colin Crossb628ea52018-08-14 16:42:33 -07002003// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002004//
2005
2006type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002007 // list of compatibility suites (for example "cts", "vts") that the module should be
2008 // installed into.
2009 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002010
2011 // the name of the test configuration (for example "AndroidTest.xml") that should be
2012 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002013 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002014
Jack He33338892018-09-19 02:21:28 -07002015 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2016 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002017 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002018
Colin Crossd96ca352018-08-10 16:06:24 -07002019 // list of files or filegroup modules that provide data that should be installed alongside
2020 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002021 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002022
2023 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2024 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2025 // explicitly.
2026 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002027}
2028
Paul Duffin42df1442019-03-20 12:45:53 +00002029type testHelperLibraryProperties struct {
2030 // list of compatibility suites (for example "cts", "vts") that the module should be
2031 // installed into.
2032 Test_suites []string `android:"arch_variant"`
2033}
2034
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002035type prebuiltTestProperties struct {
2036 // list of compatibility suites (for example "cts", "vts") that the module should be
2037 // installed into.
2038 Test_suites []string `android:"arch_variant"`
2039
2040 // the name of the test configuration (for example "AndroidTest.xml") that should be
2041 // installed with the module.
2042 Test_config *string `android:"path,arch_variant"`
2043}
2044
Colin Cross05638fc2018-04-09 18:40:24 -07002045type Test struct {
2046 Library
2047
2048 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002049
2050 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002051 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002052}
2053
Paul Duffin42df1442019-03-20 12:45:53 +00002054type TestHelperLibrary struct {
2055 Library
2056
2057 testHelperLibraryProperties testHelperLibraryProperties
2058}
2059
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002060type JavaTestImport struct {
2061 Import
2062
2063 prebuiltTestProperties prebuiltTestProperties
2064
2065 testConfig android.Path
2066}
2067
Colin Cross303e21f2018-08-07 16:49:25 -07002068func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002069 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2070 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002071 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002072
2073 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002074}
2075
Paul Duffin42df1442019-03-20 12:45:53 +00002076func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2077 j.Library.GenerateAndroidBuildActions(ctx)
2078}
2079
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002080func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2081 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2082 j.prebuiltTestProperties.Test_suites, nil)
2083
2084 j.Import.GenerateAndroidBuildActions(ctx)
2085}
2086
2087type testSdkMemberType struct {
2088 android.SdkMemberTypeBase
2089}
2090
2091func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2092 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2093}
2094
2095func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2096 _, ok := module.(*Test)
2097 return ok
2098}
2099
2100func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
2101 variants := member.Variants()
2102 if len(variants) != 1 {
2103 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
2104 for _, variant := range variants {
2105 sdkModuleContext.ModuleErrorf(" %q", variant)
2106 }
2107 }
2108 variant := variants[0]
2109 j := variant.(*Test)
2110
2111 implementationJars := j.ImplementationJars()
2112 if len(implementationJars) != 1 {
2113 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
2114 }
2115
2116 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
2117 builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)
2118
2119 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
2120 builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)
2121
2122 module := builder.AddPrebuiltModule(member, "java_test_import")
2123 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2124 module.AddProperty("test_config", snapshotRelativeTestConfigPath)
2125}
2126
Colin Cross1b16b0e2019-02-12 14:41:32 -08002127// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2128// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2129//
2130// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2131// compiled against the device bootclasspath.
2132//
2133// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2134// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002135func TestFactory() android.Module {
2136 module := &Test{}
2137
2138 module.AddProperties(
2139 &module.Module.properties,
2140 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002141 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002142 &module.Module.protoProperties,
2143 &module.testProperties)
2144
Colin Cross9ae1b922018-06-26 17:59:05 -07002145 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002146 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002147
Colin Cross05638fc2018-04-09 18:40:24 -07002148 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002149 return module
2150}
2151
Paul Duffin42df1442019-03-20 12:45:53 +00002152// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2153func TestHelperLibraryFactory() android.Module {
2154 module := &TestHelperLibrary{}
2155
2156 module.AddProperties(
2157 &module.Module.properties,
2158 &module.Module.deviceProperties,
2159 &module.Module.dexpreoptProperties,
2160 &module.Module.protoProperties,
2161 &module.testHelperLibraryProperties)
2162
Colin Cross9a4abed2019-04-24 13:19:28 -07002163 module.Module.properties.Installable = proptools.BoolPtr(true)
2164 module.Module.dexpreopter.isTest = true
2165
Paul Duffin42df1442019-03-20 12:45:53 +00002166 InitJavaModule(module, android.HostAndDeviceSupported)
2167 return module
2168}
2169
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002170// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2171// and makes sure that it is added to the appropriate test suite.
2172//
2173// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2174// compiled against an Android classpath.
2175//
2176// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2177// for host modules.
2178func JavaTestImportFactory() android.Module {
2179 module := &JavaTestImport{}
2180
2181 module.AddProperties(
2182 &module.Import.properties,
2183 &module.prebuiltTestProperties)
2184
2185 module.Import.properties.Installable = proptools.BoolPtr(true)
2186
2187 android.InitPrebuiltModule(module, &module.properties.Jars)
2188 android.InitApexModule(module)
2189 android.InitSdkAwareModule(module)
2190 InitJavaModule(module, android.HostAndDeviceSupported)
2191 return module
2192}
2193
Colin Cross1b16b0e2019-02-12 14:41:32 -08002194// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2195// allow running the test with `atest` or a `TEST_MAPPING` file.
2196//
2197// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2198// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002199func TestHostFactory() android.Module {
2200 module := &Test{}
2201
2202 module.AddProperties(
2203 &module.Module.properties,
2204 &module.Module.protoProperties,
2205 &module.testProperties)
2206
Colin Cross9ae1b922018-06-26 17:59:05 -07002207 module.Module.properties.Installable = proptools.BoolPtr(true)
2208
Colin Cross05638fc2018-04-09 18:40:24 -07002209 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002210 return module
2211}
2212
2213//
Colin Cross2fe66872015-03-30 17:20:39 -07002214// Java Binaries (.jar file plus wrapper script)
2215//
2216
Colin Crossf506d872017-07-19 15:53:04 -07002217type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002218 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002219 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002220
2221 // Name of the class containing main to be inserted into the manifest as Main-Class.
2222 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002223}
2224
Colin Crossf506d872017-07-19 15:53:04 -07002225type Binary struct {
2226 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002227
Colin Crossf506d872017-07-19 15:53:04 -07002228 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002229
Colin Cross6b4a32d2017-12-05 13:42:45 -08002230 isWrapperVariant bool
2231
Colin Crossc3315992017-12-08 19:12:36 -08002232 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002233 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002234}
2235
Alex Light24237172017-10-26 09:46:21 -07002236func (j *Binary) HostToolPath() android.OptionalPath {
2237 return android.OptionalPathForPath(j.binaryFile)
2238}
2239
Colin Crossf506d872017-07-19 15:53:04 -07002240func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002241 if ctx.Arch().ArchType == android.Common {
2242 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002243 if j.binaryProperties.Main_class != nil {
2244 if j.properties.Manifest != nil {
2245 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2246 }
2247 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2248 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2249 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2250 }
2251
Colin Cross6b4a32d2017-12-05 13:42:45 -08002252 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002253 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002254 // Handle the binary wrapper
2255 j.isWrapperVariant = true
2256
Colin Cross366938f2017-12-11 16:29:02 -08002257 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002258 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002259 } else {
2260 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2261 }
2262
2263 // Depend on the installed jar so that the wrapper doesn't get executed by
2264 // another build rule before the jar has been installed.
2265 jarFile := ctx.PrimaryModule().(*Binary).installFile
2266
2267 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2268 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002269 }
Colin Cross2fe66872015-03-30 17:20:39 -07002270}
2271
Colin Crossf506d872017-07-19 15:53:04 -07002272func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002273 if ctx.Arch().ArchType == android.Common {
2274 j.deps(ctx)
2275 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002276}
2277
Colin Cross1b16b0e2019-02-12 14:41:32 -08002278// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2279// as well.
2280//
2281// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2282// compiled against the device bootclasspath.
2283//
2284// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2285// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002286func BinaryFactory() android.Module {
2287 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002288
Colin Cross36242852017-06-23 15:06:31 -07002289 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002290 &module.Module.properties,
2291 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002292 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002293 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002294 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002295
Colin Cross9ae1b922018-06-26 17:59:05 -07002296 module.Module.properties.Installable = proptools.BoolPtr(true)
2297
Colin Cross6b4a32d2017-12-05 13:42:45 -08002298 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2299 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002300 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002301}
2302
Colin Cross1b16b0e2019-02-12 14:41:32 -08002303// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2304//
2305// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2306// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002307func BinaryHostFactory() android.Module {
2308 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002309
Colin Cross36242852017-06-23 15:06:31 -07002310 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002311 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002312 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002313 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002314
Colin Cross9ae1b922018-06-26 17:59:05 -07002315 module.Module.properties.Installable = proptools.BoolPtr(true)
2316
Colin Cross6b4a32d2017-12-05 13:42:45 -08002317 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2318 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002319 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002320}
2321
2322//
2323// Java prebuilts
2324//
2325
Colin Cross74d73e22017-08-02 11:05:49 -07002326type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002327 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002328
Nan Zhangea568a42017-11-08 21:20:04 -08002329 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002330
2331 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002332
2333 // List of shared java libs that this module has dependencies to
2334 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002335
2336 // List of files to remove from the jar file(s)
2337 Exclude_files []string
2338
2339 // List of directories to remove from the jar file(s)
2340 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002341
2342 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002343 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002344
2345 // set the name of the output
2346 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002347}
2348
2349type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002350 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002351 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002352 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002353 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002354 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002355
Colin Cross74d73e22017-08-02 11:05:49 -07002356 properties ImportProperties
2357
Colin Cross0a6e0072017-08-30 14:24:55 -07002358 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002359 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002360}
2361
Jiyong Park6a927c42020-01-21 02:03:43 +09002362func (j *Import) sdkVersion() sdkSpec {
2363 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002364}
2365
Jiyong Park6a927c42020-01-21 02:03:43 +09002366func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002367 return j.sdkVersion()
2368}
2369
Colin Cross74d73e22017-08-02 11:05:49 -07002370func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002371 return &j.prebuilt
2372}
2373
Colin Cross74d73e22017-08-02 11:05:49 -07002374func (j *Import) PrebuiltSrcs() []string {
2375 return j.properties.Jars
2376}
2377
2378func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002379 return j.prebuilt.Name(j.ModuleBase.Name())
2380}
2381
Jiyong Park0b238752019-10-29 11:23:10 +09002382func (j *Import) Stem() string {
2383 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2384}
2385
Jiyong Park618922e2020-01-08 13:35:43 +09002386func (a *Import) JacocoReportClassesFile() android.Path {
2387 return nil
2388}
2389
Colin Cross74d73e22017-08-02 11:05:49 -07002390func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002391 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002392}
2393
Colin Cross74d73e22017-08-02 11:05:49 -07002394func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002395 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002396
Jiyong Park0b238752019-10-29 11:23:10 +09002397 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002398 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002399 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2400 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002401 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002402 inputFile := outputFile
2403 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2404 TransformJetifier(ctx, outputFile, inputFile)
2405 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002406 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002407
2408 ctx.VisitDirectDeps(func(module android.Module) {
2409 otherName := ctx.OtherModuleName(module)
2410 tag := ctx.OtherModuleDependencyTag(module)
2411
2412 switch dep := module.(type) {
2413 case Dependency:
2414 switch tag {
2415 case libTag, staticLibTag:
2416 // sdk lib names from dependencies are re-exported
2417 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2418 }
2419 case SdkLibraryDependency:
2420 switch tag {
2421 case libTag:
2422 // names of sdk libs that are directly depended are exported
2423 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2424 }
2425 }
2426 })
2427
2428 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002429 if Bool(j.properties.Installable) {
2430 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002431 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002432 }
Colin Cross2fe66872015-03-30 17:20:39 -07002433}
2434
Colin Cross74d73e22017-08-02 11:05:49 -07002435var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002436
Nan Zhanged19fc32017-10-19 13:06:22 -07002437func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002438 if j.combinedClasspathFile == nil {
2439 return nil
2440 }
Colin Cross37f6d792018-07-12 12:28:41 -07002441 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002442}
2443
2444func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002445 if j.combinedClasspathFile == nil {
2446 return nil
2447 }
Colin Cross37f6d792018-07-12 12:28:41 -07002448 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002449}
2450
Colin Cross331a1212018-08-15 20:40:52 -07002451func (j *Import) ResourceJars() android.Paths {
2452 return nil
2453}
2454
2455func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002456 if j.combinedClasspathFile == nil {
2457 return nil
2458 }
Colin Cross331a1212018-08-15 20:40:52 -07002459 return android.Paths{j.combinedClasspathFile}
2460}
2461
Colin Crossf24a22a2019-01-31 14:12:44 -08002462func (j *Import) DexJar() android.Path {
2463 return nil
2464}
2465
Colin Cross74d73e22017-08-02 11:05:49 -07002466func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002467 return nil
2468}
2469
Jiyong Park1be96912018-05-28 18:02:19 +09002470func (j *Import) ExportedSdkLibs() []string {
2471 return j.exportedSdkLibs
2472}
2473
Artur Satayev9cf46692019-11-26 18:08:34 +00002474func (j *Import) ExportedPlugins() (android.Paths, []string) {
2475 return nil, nil
2476}
2477
Colin Cross0c4ce212019-05-03 15:28:19 -07002478func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2479 return nil, nil
2480}
2481
Jiyong Park0f80c182020-01-31 02:49:53 +09002482func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
2483 depTag := ctx.OtherModuleDependencyTag(dep)
2484 // dependencies other than the static linkage are all considered crossing APEX boundary
2485 // Also, a dependency to an sdk member is also considered as such. This is required because
2486 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
2487 return depTag == staticLibTag || j.IsInAnySdk()
2488}
2489
albaltai36ff7dc2018-12-25 14:35:23 +08002490// Add compile time check for interface implementation
2491var _ android.IDEInfo = (*Import)(nil)
2492var _ android.IDECustomizedModuleName = (*Import)(nil)
2493
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002494// Collect information for opening IDE project files in java/jdeps.go.
2495const (
2496 removedPrefix = "prebuilt_"
2497)
2498
2499func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2500 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2501}
2502
2503func (j *Import) IDECustomizedModuleName() string {
2504 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2505 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2506 // solution to get the Import name.
2507 name := j.Name()
2508 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002509 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002510 }
2511 return name
2512}
2513
Colin Cross74d73e22017-08-02 11:05:49 -07002514var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002515
Colin Cross1b16b0e2019-02-12 14:41:32 -08002516// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2517//
2518// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2519// compiled against an Android classpath.
2520//
2521// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2522// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002523func ImportFactory() android.Module {
2524 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002525
Colin Cross74d73e22017-08-02 11:05:49 -07002526 module.AddProperties(&module.properties)
2527
2528 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002529 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002530 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002531 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002532 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002533}
2534
Colin Cross1b16b0e2019-02-12 14:41:32 -08002535// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2536// module.
2537//
2538// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2539// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002540func ImportFactoryHost() android.Module {
2541 module := &Import{}
2542
2543 module.AddProperties(&module.properties)
2544
2545 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002546 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002547 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002548 return module
2549}
2550
Colin Cross42be7612019-02-21 18:12:14 -08002551// dex_import module
2552
2553type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002554 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002555
2556 // set the name of the output
2557 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002558}
2559
2560type DexImport struct {
2561 android.ModuleBase
2562 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002563 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002564 prebuilt android.Prebuilt
2565
2566 properties DexImportProperties
2567
2568 dexJarFile android.Path
2569 maybeStrippedDexJarFile android.Path
2570
2571 dexpreopter
2572}
2573
2574func (j *DexImport) Prebuilt() *android.Prebuilt {
2575 return &j.prebuilt
2576}
2577
2578func (j *DexImport) PrebuiltSrcs() []string {
2579 return j.properties.Jars
2580}
2581
2582func (j *DexImport) Name() string {
2583 return j.prebuilt.Name(j.ModuleBase.Name())
2584}
2585
Jiyong Park0b238752019-10-29 11:23:10 +09002586func (j *DexImport) Stem() string {
2587 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2588}
2589
Martin Stjernholm6d415272020-01-31 17:10:36 +00002590func (j *DexImport) IsInstallable() bool {
2591 return true
2592}
2593
Colin Cross42be7612019-02-21 18:12:14 -08002594func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2595 if len(j.properties.Jars) != 1 {
2596 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2597 }
2598
Jiyong Park0b238752019-10-29 11:23:10 +09002599 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002600 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2601
2602 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2603 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2604
2605 if j.dexpreopter.uncompressedDex {
2606 rule := android.NewRuleBuilder()
2607
2608 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2609 rule.Temporary(temporary)
2610
2611 // use zip2zip to uncompress classes*.dex files
2612 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002613 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002614 FlagWithInput("-i ", inputJar).
2615 FlagWithOutput("-o ", temporary).
2616 FlagWithArg("-0 ", "'classes*.dex'")
2617
2618 // use zipalign to align uncompressed classes*.dex files
2619 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002620 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002621 Flag("-f").
2622 Text("4").
2623 Input(temporary).
2624 Output(dexOutputFile)
2625
2626 rule.DeleteTemporaryFiles()
2627
2628 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2629 } else {
2630 ctx.Build(pctx, android.BuildParams{
2631 Rule: android.Cp,
2632 Input: inputJar,
2633 Output: dexOutputFile,
2634 })
2635 }
2636
2637 j.dexJarFile = dexOutputFile
2638
2639 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2640
2641 j.maybeStrippedDexJarFile = dexOutputFile
2642
2643 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2644 ctx.ModuleName()+".jar", dexOutputFile)
2645}
2646
2647func (j *DexImport) DexJar() android.Path {
2648 return j.dexJarFile
2649}
2650
2651// dex_import imports a `.jar` file containing classes.dex files.
2652//
2653// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2654// to the device.
2655func DexImportFactory() android.Module {
2656 module := &DexImport{}
2657
2658 module.AddProperties(&module.properties)
2659
2660 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002661 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002662 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002663 return module
2664}
2665
Colin Cross89536d42017-07-07 14:35:50 -07002666//
2667// Defaults
2668//
2669type Defaults struct {
2670 android.ModuleBase
2671 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002672 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002673}
2674
Colin Cross1b16b0e2019-02-12 14:41:32 -08002675// java_defaults provides a set of properties that can be inherited by other java or android modules.
2676//
2677// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2678// property in the defaults module that exists in the depending module will be prepended to the depending module's
2679// value for that property.
2680//
2681// Example:
2682//
2683// java_defaults {
2684// name: "example_defaults",
2685// srcs: ["common/**/*.java"],
2686// javacflags: ["-Xlint:all"],
2687// aaptflags: ["--auto-add-overlay"],
2688// }
2689//
2690// java_library {
2691// name: "example",
2692// defaults: ["example_defaults"],
2693// srcs: ["example/**/*.java"],
2694// }
2695//
2696// is functionally identical to:
2697//
2698// java_library {
2699// name: "example",
2700// srcs: [
2701// "common/**/*.java",
2702// "example/**/*.java",
2703// ],
2704// javacflags: ["-Xlint:all"],
2705// }
Colin Cross89536d42017-07-07 14:35:50 -07002706func defaultsFactory() android.Module {
2707 return DefaultsFactory()
2708}
2709
Paul Duffin47357662019-12-05 14:07:14 +00002710func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002711 module := &Defaults{}
2712
Colin Cross89536d42017-07-07 14:35:50 -07002713 module.AddProperties(
2714 &CompilerProperties{},
2715 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002716 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002717 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002718 &aaptProperties{},
2719 &androidLibraryProperties{},
2720 &appProperties{},
2721 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002722 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002723 &ImportProperties{},
2724 &AARImportProperties{},
2725 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002726 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002727 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002728 )
2729
2730 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002731 return module
2732}
Nan Zhangea568a42017-11-08 21:20:04 -08002733
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002734func kytheExtractJavaFactory() android.Singleton {
2735 return &kytheExtractJavaSingleton{}
2736}
2737
2738type kytheExtractJavaSingleton struct {
2739}
2740
2741func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2742 var xrefTargets android.Paths
2743 ctx.VisitAllModules(func(module android.Module) {
2744 if javaModule, ok := module.(xref); ok {
2745 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2746 }
2747 })
2748 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2749 if len(xrefTargets) > 0 {
2750 ctx.Build(pctx, android.BuildParams{
2751 Rule: blueprint.Phony,
2752 Output: android.PathForPhony(ctx, "xref_java"),
2753 Inputs: xrefTargets,
2754 })
2755 }
2756}
2757
Nan Zhangea568a42017-11-08 21:20:04 -08002758var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002759var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002760var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002761var inList = android.InList