blob: 1d4d45972ba25660a0a6ffe0d794df4c7dcdd89d [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"
26
27 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
39 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000040 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000041
Paul Duffin2b9e3d32020-02-28 14:39:53 +000042 android.RegisterSdkMemberType(&librarySdkMemberType{
43 android.SdkMemberTypeBase{
44 PropertyName: "java_libs",
45 },
46 func(j *Library) android.Path {
47 implementationJars := j.ImplementationJars()
48 if len(implementationJars) != 1 {
49 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
50 }
51
52 return implementationJars[0]
Paul Duffin255f18e2019-12-13 11:22:16 +000053 },
54 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000055
56 android.RegisterSdkMemberType(&testSdkMemberType{
57 SdkMemberTypeBase: android.SdkMemberTypeBase{
58 PropertyName: "java_tests",
59 },
60 })
Colin Cross463a90e2015-06-17 14:20:06 -070061}
62
Paul Duffinf9b1da02019-12-18 19:51:55 +000063func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
64 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
65
66 ctx.RegisterModuleType("java_library", LibraryFactory)
67 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
68 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
69 ctx.RegisterModuleType("java_binary", BinaryFactory)
70 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
71 ctx.RegisterModuleType("java_test", TestFactory)
72 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
73 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000074 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000075 ctx.RegisterModuleType("java_import", ImportFactory)
76 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
77 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
78 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
79 ctx.RegisterModuleType("dex_import", DexImportFactory)
80
Martin Stjernholm6d415272020-01-31 17:10:36 +000081 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
82 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
83 })
Martin Stjernholmd90676f2020-01-11 00:37:30 +000084
Paul Duffinf9b1da02019-12-18 19:51:55 +000085 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
86 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
87}
88
Artur Satayev2eedf622020-04-15 17:29:42 +010089func (j *Module) CheckStableSdkVersion() error {
90 sdkVersion := j.sdkVersion()
91 if sdkVersion.stable() {
92 return nil
93 }
94 return fmt.Errorf("non stable SDK %v", sdkVersion)
95}
96
Artur Satayeve5ac15a2020-04-08 19:09:30 +010097func (j *Module) checkSdkVersions(ctx android.ModuleContext) {
Colin Crosseb032962020-05-13 11:05:02 -070098 if j.RequiresStableAPIs(ctx) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090099 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +0900100 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900101 ctx.PropertyErrorf("sdk_version",
102 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
103 }
104 }
105 }
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100106
107 ctx.VisitDirectDeps(func(module android.Module) {
108 tag := ctx.OtherModuleDependencyTag(module)
109 switch module.(type) {
110 // TODO(satayev): cover other types as well, e.g. imports
111 case *Library, *AndroidLibrary:
112 switch tag {
113 case bootClasspathTag, libTag, staticLibTag, java9LibTag:
114 checkLinkType(ctx, j, module.(linkTypeContext), tag.(dependencyTag))
115 }
116 }
117 })
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900118}
119
Jeongik Cha538c0d02019-07-11 15:54:27 +0900120func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
121 if sc, ok := ctx.Module().(sdkContext); ok {
122 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +0900123 sdkVersionSpecified := sc.sdkVersion().specified()
124 if usePlatformAPI && sdkVersionSpecified {
125 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
126 } else if !usePlatformAPI && !sdkVersionSpecified {
127 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900128 }
129
130 }
131}
132
Colin Cross2fe66872015-03-30 17:20:39 -0700133// TODO:
134// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700135// Renderscript
136// Post-jar passes:
137// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700138// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700139// DroidDoc
140// Findbugs
141
Colin Cross89536d42017-07-07 14:35:50 -0700142type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700143 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
144 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800145 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700146
147 // list of source files that should not be used to build the Java module.
148 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800149 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700150
151 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700152 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700153
Colin Cross86a63ff2017-09-27 17:33:10 -0700154 // list of directories that should be excluded from java_resource_dirs
155 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700156
Colin Cross0f37af02017-09-27 17:42:05 -0700157 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800158 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700159
Colin Crosscedd4762018-09-13 11:26:19 -0700160 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800161 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700162
Colin Cross7d5136f2015-05-11 13:39:40 -0700163 // list of module-specific flags that will be used for javac compiles
164 Javacflags []string `android:"arch_variant"`
165
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200166 // list of module-specific flags that will be used for kotlinc compiles
167 Kotlincflags []string `android:"arch_variant"`
168
Colin Cross7d5136f2015-05-11 13:39:40 -0700169 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700170 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700171
172 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700173 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700174
175 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800176 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700177
Colin Cross540eff82017-06-22 17:01:52 -0700178 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800179 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700180
181 // If not blank, set the java version passed to javac as -source and -target
182 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700183
Colin Cross9ae1b922018-06-26 17:59:05 -0700184 // If set to true, allow this module to be dexed and installed on devices. Has no
185 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700186 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700187
Colin Cross0f37af02017-09-27 17:42:05 -0700188 // If set to true, include sources used to compile the module in to the final jar
189 Include_srcs *bool
190
Vladimir Marko0975ee02019-04-02 10:29:55 +0100191 // If not empty, classes are restricted to the specified packages and their sub-packages.
192 // This restriction is checked after applying jarjar rules and including static libs.
193 Permitted_packages []string
194
Colin Crossbe9cdb82019-01-21 21:37:16 -0800195 // List of modules to use as annotation processors
196 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700197
Artur Satayev9cf46692019-11-26 18:08:34 +0000198 // List of modules to export to libraries that directly depend on this library as annotation processors
199 Exported_plugins []string
200
Nan Zhang61eaedb2017-11-02 13:28:15 -0700201 // The number of Java source entries each Javac instance can process
202 Javac_shard_size *int64
203
Nan Zhang5f8cb422018-02-06 10:34:32 -0800204 // Add host jdk tools.jar to bootclasspath
205 Use_tools_jar *bool
206
Colin Cross1369cdb2017-09-29 17:58:17 -0700207 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700208 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800209 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700210
Colin Cross6cef4812019-10-17 14:23:50 -0700211 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700212 Javacflags []string
213 }
Colin Crosscb933592017-11-22 13:49:43 -0800214
Colin Cross81440082018-08-15 20:21:55 -0700215 // When compiling language level 9+ .java code in packages that are part of
216 // a system module, patch_module names the module that your sources and
217 // dependencies should be patched into. The Android runtime currently
218 // doesn't implement the JEP 261 module system so this option is only
219 // supported at compile time. It should only be needed to compile tests in
220 // packages that exist in libcore and which are inconvenient to move
221 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100222 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700223
Colin Crosscb933592017-11-22 13:49:43 -0800224 Jacoco struct {
225 // List of classes to include for instrumentation with jacoco to collect coverage
226 // information at runtime when building with coverage enabled. If unset defaults to all
227 // classes.
228 // Supports '*' as the last character of an entry in the list as a wildcard match.
229 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
230 // it matches classes in the package that have the class name as a prefix.
231 Include_filter []string
232
233 // List of classes to exclude from instrumentation with jacoco to collect coverage
234 // information at runtime when building with coverage enabled. Overrides classes selected
235 // by the include_filter property.
236 // Supports '*' as the last character of an entry in the list as a wildcard match.
237 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
238 // it matches classes in the package that have the class name as a prefix.
239 Exclude_filter []string
240 }
241
Andreas Gampef3e5b552018-01-22 21:27:21 -0800242 Errorprone struct {
243 // List of javac flags that should only be used when running errorprone.
244 Javacflags []string
245 }
246
Colin Cross0f2ee152017-12-14 15:22:43 -0800247 Proto struct {
248 // List of extra options that will be passed to the proto generator.
249 Output_params []string
250 }
251
Colin Crosscb933592017-11-22 13:49:43 -0800252 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800253
254 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800255 Services []string `android:"path,arch_variant"`
Colin Crossaaf58062020-06-18 15:52:01 -0700256
257 // If true, package the kotlin stdlib into the jar. Defaults to true.
258 Static_kotlin_stdlib *bool `android:"arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700259}
260
Colin Cross89536d42017-07-07 14:35:50 -0700261type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700262 // list of module-specific flags that will be used for dex compiles
263 Dxflags []string `android:"arch_variant"`
264
Jeongik Cha538c0d02019-07-11 15:54:27 +0900265 // if not blank, set to the version of the sdk to compile against.
266 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800267 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700268
Colin Cross83bb3162018-06-25 15:48:06 -0700269 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
270 // Defaults to sdk_version if not set.
271 Min_sdk_version *string
272
Dan Willemsen419290a2018-10-31 15:28:47 -0700273 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
274 // Defaults to sdk_version if not set.
275 Target_sdk_version *string
276
Jeongik Cha356dac42019-08-19 14:09:52 +0900277 // Whether to compile against the platform APIs instead of an SDK.
278 // If true, then sdk_version must be empty. The value of this field
279 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700280 Platform_apis *bool
281
Colin Crossebe1a512017-11-14 13:12:14 -0800282 Aidl struct {
283 // Top level directories to pass to aidl tool
284 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700285
Colin Crossebe1a512017-11-14 13:12:14 -0800286 // Directories rooted at the Android.bp file to pass to aidl tool
287 Local_include_dirs []string
288
289 // directories that should be added as include directories for any aidl sources of modules
290 // that depend on this module, as well as to aidl for this module.
291 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100292
293 // whether to generate traces (for systrace) for this interface
294 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100295
296 // whether to generate Binder#GetTransaction name method.
297 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800298 }
Colin Cross92430102017-10-09 14:59:32 -0700299
300 // If true, export a copy of the module as a -hostdex module for host testing.
301 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700302
Colin Cross7f87f4f2019-04-24 13:41:45 -0700303 Target struct {
304 Hostdex struct {
305 // Additional required dependencies to add to -hostdex modules.
306 Required []string
307 }
308 }
309
David Brazdil17ef5632018-06-27 10:27:45 +0100310 // If set to true, compile dex regardless of installable. Defaults to false.
311 Compile_dex *bool
312
Colin Cross66dbc0b2017-12-28 12:23:20 -0800313 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700314 // If false, disable all optimization. Defaults to true for android_app and android_test
315 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800316 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700317 // True if the module containing this has it set by default.
318 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800319
320 // If true, optimize for size by removing unused code. Defaults to true for apps,
321 // false for libraries and tests.
322 Shrink *bool
323
324 // If true, optimize bytecode. Defaults to false.
325 Optimize *bool
326
327 // If true, obfuscate bytecode. Defaults to false.
328 Obfuscate *bool
329
330 // If true, do not use the flag files generated by aapt that automatically keep
331 // classes referenced by the app manifest. Defaults to false.
332 No_aapt_flags *bool
333
334 // Flags to pass to proguard.
335 Proguard_flags []string
336
337 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800338 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800339 }
340
Paul Duffine25c6442019-10-11 13:50:28 +0100341 // When targeting 1.9 and above, override the modules to use with --system,
342 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700343 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700344
Jiyong Park4c4c0242019-10-21 14:53:15 +0900345 // set the name of the output
346 Stem *string
347
David Srbecky98c71222020-05-20 22:20:28 +0100348 // Keep the data uncompressed. We always need uncompressed dex for execution,
349 // so this might actually save space by avoiding storing the same data twice.
350 // This defaults to reasonable value based on module and should not be set.
351 // It exists only to support ART tests.
352 Uncompress_dex *bool
353
354 IsSDKLibrary bool `blueprint:"mutated"`
Songchun Fan688de9a2020-03-24 20:32:24 -0700355
356 // If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file.
357 // Defaults to false.
358 V4_signature *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700359}
360
Sasha Smundak2057f822019-04-16 17:16:58 -0700361func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
362 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
363}
364
Paul Duffinc8f2f182020-05-17 08:34:50 +0100365// Functionality common to Module and Import
Paul Duffin64e61992020-05-15 10:20:31 +0100366//
367// It is embedded in Module so its functionality can be used by methods in Module
368// but it is currently only initialized by Import and Library.
Paul Duffinc8f2f182020-05-17 08:34:50 +0100369type embeddableInModuleAndImport struct {
Paul Duffin64e61992020-05-15 10:20:31 +0100370
371 // Functionality related to this being used as a component of a java_sdk_library.
372 EmbeddableSdkLibraryComponent
373}
374
375func (e *embeddableInModuleAndImport) initModuleAndImport(moduleBase *android.ModuleBase) {
376 e.initSdkLibraryComponent(moduleBase)
Paul Duffinc8f2f182020-05-17 08:34:50 +0100377}
378
379// Module/Import's DepIsInSameApex(...) delegates to this method.
380//
381// This cannot implement DepIsInSameApex(...) directly as that leads to ambiguity with
382// the one provided by ApexModuleBase.
383func (e *embeddableInModuleAndImport) depIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
384 // dependencies other than the static linkage are all considered crossing APEX boundary
385 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
386 return true
387 }
388 return false
389}
390
Colin Cross46c9b8b2017-06-22 16:51:17 -0700391// Module contains the properties and members used by all java module types
392type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700393 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700394 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900395 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900396 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700397
Paul Duffinc8f2f182020-05-17 08:34:50 +0100398 // Functionality common to Module and Import.
399 embeddableInModuleAndImport
400
Colin Cross89536d42017-07-07 14:35:50 -0700401 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700402 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700403 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700404
Colin Cross331a1212018-08-15 20:40:52 -0700405 // jar file containing header classes including static library dependencies, suitable for
406 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700407 headerJarFile android.Path
408
Colin Cross331a1212018-08-15 20:40:52 -0700409 // jar file containing implementation classes including static library dependencies but no
410 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700411 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700412
Colin Cross331a1212018-08-15 20:40:52 -0700413 // jar file containing only resources including from static library dependencies
414 resourceJar android.Path
415
Colin Cross0c4ce212019-05-03 15:28:19 -0700416 // args and dependencies to package source files into a srcjar
417 srcJarArgs []string
418 srcJarDeps android.Paths
419
Colin Cross331a1212018-08-15 20:40:52 -0700420 // jar file containing implementation classes and resources including static library
421 // dependencies
422 implementationAndResourcesJar android.Path
423
424 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700425 dexJarFile android.Path
426
Colin Cross43f08db2018-11-12 10:13:39 -0800427 // output file that contains classes.dex if it should be in the output file
428 maybeStrippedDexJarFile android.Path
429
Colin Crosscb933592017-11-22 13:49:43 -0800430 // output file containing uninstrumented classes that will be instrumented by jacoco
431 jacocoReportClassesFile android.Path
432
Colin Cross66dbc0b2017-12-28 12:23:20 -0800433 // output file containing mapping of obfuscated names
434 proguardDictionary android.Path
435
Colin Cross331a1212018-08-15 20:40:52 -0700436 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700437 outputFile android.Path
438 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700439
Colin Cross635c3b02016-05-18 15:37:25 -0700440 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700441
Colin Cross635c3b02016-05-18 15:37:25 -0700442 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700443
Colin Cross2fe66872015-03-30 17:20:39 -0700444 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700445 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800446
447 // list of .java files and srcjars that was passed to javac
448 compiledJavaSrcs android.Paths
449 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800450
451 // list of extra progurad flag files
452 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900453
Colin Cross094054a2018-10-17 15:10:48 -0700454 // manifest file to use instead of properties.Manifest
455 overrideManifest android.OptionalPath
456
Artur Satayev9cf46692019-11-26 18:08:34 +0000457 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900458 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700459
Artur Satayev9cf46692019-11-26 18:08:34 +0000460 // list of plugins that this java module is exporting
461 exportedPluginJars android.Paths
462
463 // list of plugins that this java module is exporting
464 exportedPluginClasses []string
465
466 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800467 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700468 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800469
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800470 // expanded Jarjar_rules
471 expandJarjarRules android.Path
472
Tej Singh83839722020-06-03 18:45:54 +0000473 // list of additional targets for checkbuild
474 additionalCheckedModules android.Paths
475
Colin Cross988708c2019-05-06 14:04:11 -0700476 // Extra files generated by the module type to be added as java resources.
477 extraResources android.Paths
478
Colin Crossf24a22a2019-01-31 14:12:44 -0800479 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800480 dexpreopter
Colin Cross1e28e3c2020-06-02 20:09:13 -0700481 linter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800482
483 // list of the xref extraction files
484 kytheFiles android.Paths
Anton Hansson1e65f942020-03-27 19:39:48 +0000485
486 distFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700487}
488
Colin Cross1c14b4e2020-06-15 16:09:53 -0700489func (j *Module) addHostProperties() {
490 j.AddProperties(
491 &j.properties,
492 &j.protoProperties,
493 )
494}
495
496func (j *Module) addHostAndDeviceProperties() {
497 j.addHostProperties()
498 j.AddProperties(
499 &j.deviceProperties,
500 &j.dexpreoptProperties,
Colin Cross1e28e3c2020-06-02 20:09:13 -0700501 &j.linter.properties,
Colin Cross1c14b4e2020-06-15 16:09:53 -0700502 )
503}
504
Colin Cross41955e82019-05-29 14:40:35 -0700505func (j *Module) OutputFiles(tag string) (android.Paths, error) {
506 switch tag {
507 case "":
508 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700509 case ".jar":
510 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700511 case ".proguard_map":
512 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700513 default:
514 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
515 }
Colin Cross54250902017-12-05 09:28:08 -0800516}
517
Colin Cross41955e82019-05-29 14:40:35 -0700518var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800519
Colin Crossf506d872017-07-19 15:53:04 -0700520type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700521 HeaderJars() android.Paths
522 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700523 ResourceJars() android.Paths
524 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800525 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700526 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900527 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000528 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700529 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700530 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900531 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700532}
533
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800534type xref interface {
535 XrefJavaFiles() android.Paths
536}
537
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800538func (j *Module) XrefJavaFiles() android.Paths {
539 return j.kytheFiles
540}
541
Colin Cross89536d42017-07-07 14:35:50 -0700542func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
543 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
544 android.InitDefaultableModule(module)
545}
546
Colin Crossbe1da472017-07-07 15:59:46 -0700547type dependencyTag struct {
548 blueprint.BaseDependencyTag
549 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700550}
551
Colin Crossa4f08812018-10-02 22:03:40 -0700552type jniDependencyTag struct {
553 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700554}
555
Jiyong Park8be103b2019-11-08 15:53:48 +0900556func IsJniDepTag(depTag blueprint.DependencyTag) bool {
557 _, ok := depTag.(*jniDependencyTag)
558 return ok
559}
560
Colin Crossbe1da472017-07-07 15:59:46 -0700561var (
Colin Cross4b964c02018-10-15 16:18:06 -0700562 staticLibTag = dependencyTag{name: "staticlib"}
563 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700564 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800565 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000566 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700567 bootClasspathTag = dependencyTag{name: "bootclasspath"}
568 systemModulesTag = dependencyTag{name: "system modules"}
569 frameworkResTag = dependencyTag{name: "framework-res"}
570 frameworkApkTag = dependencyTag{name: "framework-apk"}
571 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800572 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700573 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
574 certificateTag = dependencyTag{name: "certificate"}
575 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700576 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700577)
Colin Cross2fe66872015-03-30 17:20:39 -0700578
Jiyong Park83dc74b2020-01-14 18:38:44 +0900579func IsLibDepTag(depTag blueprint.DependencyTag) bool {
580 return depTag == libTag
581}
582
583func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
584 return depTag == staticLibTag
585}
586
Colin Crossfc3674a2017-09-18 17:41:52 -0700587type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700588 useModule, useFiles, useDefaultLibs, invalidVersion bool
589
Colin Cross6cef4812019-10-17 14:23:50 -0700590 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
591 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100592
593 // The default system modules to use. Will be an empty string if no system
594 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700595 systemModules string
596
Colin Cross6cef4812019-10-17 14:23:50 -0700597 // The modules that will be added ot the classpath when targeting 1.9 or higher
598 java9Classpath []string
599
Colin Crossa97c5d32018-03-28 14:58:31 -0700600 frameworkResModule string
601
Colin Cross86a60ae2018-05-29 14:44:55 -0700602 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700603 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100604
605 noStandardLibs, noFrameworksLibs bool
606}
607
608func (s sdkDep) hasStandardLibs() bool {
609 return !s.noStandardLibs
610}
611
612func (s sdkDep) hasFrameworkLibs() bool {
613 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700614}
615
Colin Crossa4f08812018-10-02 22:03:40 -0700616type jniLib struct {
Jaewoong Jung37ca4a12020-03-26 14:01:48 -0700617 name string
618 path android.Path
619 target android.Target
620 coverageFile android.OptionalPath
Colin Crossa4f08812018-10-02 22:03:40 -0700621}
622
Colin Cross0ea8ba82019-06-06 14:33:29 -0700623func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800624 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
625}
626
Colin Cross0ea8ba82019-06-06 14:33:29 -0700627func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800628 return j.shouldInstrument(ctx) &&
629 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
630 ctx.Config().UnbundledBuild())
631}
632
Jiyong Park6a927c42020-01-21 02:03:43 +0900633func (j *Module) sdkVersion() sdkSpec {
634 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700635}
636
Paul Duffine25c6442019-10-11 13:50:28 +0100637func (j *Module) systemModules() string {
638 return proptools.String(j.deviceProperties.System_modules)
639}
640
Jiyong Park6a927c42020-01-21 02:03:43 +0900641func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700642 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900643 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700644 }
645 return j.sdkVersion()
646}
647
Jiyong Park6a927c42020-01-21 02:03:43 +0900648func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700649 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900650 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700651 }
652 return j.sdkVersion()
653}
654
Artur Satayev388d39b2020-04-27 18:53:18 +0100655func (j *Module) MinSdkVersion() string {
656 return j.minSdkVersion().version.String()
657}
658
Jiyong Parkb02bb402019-12-03 00:43:57 +0900659func (j *Module) AvailableFor(what string) bool {
660 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
661 // Exception: for hostdex: true libraries, the platform variant is created
662 // even if it's not marked as available to platform. In that case, the platform
663 // variant is used only for the hostdex and not installed to the device.
664 return true
665 }
666 return j.ApexModuleBase.AvailableFor(what)
667}
668
Colin Crossbe1da472017-07-07 15:59:46 -0700669func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700670 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100671 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700672 if sdkDep.useDefaultLibs {
673 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
674 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
675 if sdkDep.hasFrameworkLibs() {
676 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700677 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700678 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700679 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Colin Cross6cef4812019-10-17 14:23:50 -0700680 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700681 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
682 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
683 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
684 }
Colin Cross2fe66872015-03-30 17:20:39 -0700685 }
Colin Crossdb224752020-05-14 18:05:32 -0700686 if sdkDep.systemModules != "" {
687 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
688 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700689
Nan Zhangb2b33de2018-02-23 11:18:47 -0800690 if ctx.ModuleName() == "android_stubs_current" ||
691 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700692 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700693 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800694 }
Colin Cross2fe66872015-03-30 17:20:39 -0700695 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700696
Inseob Kimac1e9862019-12-09 18:15:47 +0900697 syspropPublicStubs := syspropPublicStubs(ctx.Config())
698
699 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
700 // and redirects dependency to public stub depending on the link type.
701 rewriteSyspropLibs := func(libs []string, prop string) []string {
702 // make a copy
703 ret := android.CopyOf(libs)
704
705 for idx, lib := range libs {
706 stub, ok := syspropPublicStubs[lib]
707
708 if !ok {
709 continue
710 }
711
712 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900713 // only platform modules can use internal props
714 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900715 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900716 }
717 }
718
719 return ret
720 }
721
722 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
723 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700724
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700725 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000726 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800727
Colin Crossfe17f6f2019-03-28 19:30:56 -0700728 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700729 if j.hasSrcExt(".proto") {
730 protoDeps(ctx, &j.protoProperties)
731 }
Colin Cross93e85952017-08-15 13:34:18 -0700732
733 if j.hasSrcExt(".kt") {
734 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
735 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700736 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
737 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800738 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800739 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
740 }
Colin Cross93e85952017-08-15 13:34:18 -0700741 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800742
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000743 // Framework libraries need special handling in static coverage builds: they should not have
744 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
745 // the same jacoco classes coming from different bootclasspath jars.
746 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
747 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
748 j.properties.Instrument = true
749 }
750 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700751 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800752 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700753}
754
755func hasSrcExt(srcs []string, ext string) bool {
756 for _, src := range srcs {
757 if filepath.Ext(src) == ext {
758 return true
759 }
760 }
761
762 return false
763}
764
765func (j *Module) hasSrcExt(ext string) bool {
766 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700767}
768
Colin Cross46c9b8b2017-06-22 16:51:17 -0700769func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700770 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700771
Colin Crossebe1a512017-11-14 13:12:14 -0800772 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
773 aidlIncludes = append(aidlIncludes,
774 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
775 aidlIncludes = append(aidlIncludes,
776 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700777
Colin Cross3047fa22019-04-18 10:56:44 -0700778 var flags []string
779 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700780
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700781 if aidlPreprocess.Valid() {
782 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700783 deps = append(deps, aidlPreprocess.Path())
784 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700785 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700786 }
787
Colin Cross3047fa22019-04-18 10:56:44 -0700788 if len(j.exportAidlIncludeDirs) > 0 {
789 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
790 }
791
792 if len(aidlIncludes) > 0 {
793 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
794 }
795
Colin Cross635c3b02016-05-18 15:37:25 -0700796 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800797 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700798 flags = append(flags, "-I"+src.String())
799 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700800
Martijn Coeneneab15642018-03-09 09:29:59 +0100801 if Bool(j.deviceProperties.Aidl.Generate_traces) {
802 flags = append(flags, "-t")
803 }
804
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100805 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
806 flags = append(flags, "--transaction_names")
807 }
808
Colin Cross3047fa22019-04-18 10:56:44 -0700809 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700810}
811
Colin Cross32f676a2017-09-06 13:41:06 -0700812type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800813 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700814 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800815 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700816 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800817 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700818 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700819 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700820 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700821 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800822 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700823 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700824 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700825 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700826 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800827 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800828
829 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700830}
Colin Cross2fe66872015-03-30 17:20:39 -0700831
Colin Cross54250902017-12-05 09:28:08 -0800832func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
833 for _, f := range dep.Srcs() {
834 if f.Ext() != ".jar" {
835 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
836 ctx.OtherModuleName(dep.(blueprint.Module)))
837 }
838 }
839}
840
Jiyong Park2d492942018-03-05 17:44:10 +0900841type linkType int
842
843const (
Jiyong Park50146e92020-01-30 18:00:15 +0900844 // TODO(jiyong) rename these for better readability. Make the allowed
845 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900846 javaCore linkType = iota
847 javaSdk
848 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900849 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900850 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +0900851 javaPlatform
852)
853
Jeongik Cha75b83b02019-11-01 15:28:00 +0900854type linkTypeContext interface {
855 android.Module
856 getLinkType(name string) (ret linkType, stubs bool)
857}
858
859func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Anton Hanssonac103462020-05-19 12:06:48 +0100860 switch name {
861 case "core.current.stubs", "core.platform.api.stubs", "stub-annotations",
862 "private-stub-annotations-jar", "core-lambda-stubs", "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900863 return javaCore, true
Anton Hanssonac103462020-05-19 12:06:48 +0100864 case "android_stubs_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900865 return javaSdk, true
Anton Hanssonac103462020-05-19 12:06:48 +0100866 case "android_system_stubs_current":
867 return javaSystem, true
868 case "android_module_lib_stubs_current":
Jiyong Park50146e92020-01-30 18:00:15 +0900869 return javaModule, true
Anton Hanssonac103462020-05-19 12:06:48 +0100870 case "android_system_server_stubs_current":
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900871 return javaSystemServer, true
Anton Hanssonac103462020-05-19 12:06:48 +0100872 case "android_test_stubs_current":
873 return javaSystem, true
Jiyong Park2d492942018-03-05 17:44:10 +0900874 }
Anton Hanssonac103462020-05-19 12:06:48 +0100875
Anton Hansson0bd88d02020-05-25 12:20:51 +0100876 if stub, linkType := moduleStubLinkType(name); stub {
877 return linkType, true
878 }
879
Anton Hanssonac103462020-05-19 12:06:48 +0100880 ver := m.sdkVersion()
881 switch ver.kind {
882 case sdkCore:
883 return javaCore, false
884 case sdkSystem:
885 return javaSystem, false
886 case sdkPublic:
887 return javaSdk, false
888 case sdkModule:
889 return javaModule, false
890 case sdkSystemServer:
891 return javaSystemServer, false
892 case sdkPrivate, sdkNone, sdkCorePlatform, sdkTest:
893 return javaPlatform, false
894 }
895
896 if !ver.valid() {
897 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
898 }
899 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900900}
901
Jeongik Cha75b83b02019-11-01 15:28:00 +0900902func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700903 if ctx.Host() {
904 return
905 }
906
Jeongik Cha75b83b02019-11-01 15:28:00 +0900907 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900908 if stubs {
909 return
910 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900911 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900912 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."
913
914 switch myLinkType {
915 case javaCore:
916 if otherLinkType != javaCore {
917 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 +0900918 ctx.OtherModuleName(to))
919 }
Jiyong Park2d492942018-03-05 17:44:10 +0900920 break
921 case javaSdk:
922 if otherLinkType != javaCore && otherLinkType != javaSdk {
923 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
924 ctx.OtherModuleName(to))
925 }
926 break
927 case javaSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900928 if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer {
Jiyong Park2d492942018-03-05 17:44:10 +0900929 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
930 ctx.OtherModuleName(to))
931 }
932 break
Jiyong Park50146e92020-01-30 18:00:15 +0900933 case javaModule:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900934 if otherLinkType == javaPlatform || otherLinkType == javaSystemServer {
Jiyong Park50146e92020-01-30 18:00:15 +0900935 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
936 ctx.OtherModuleName(to))
937 }
938 break
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900939 case javaSystemServer:
940 if otherLinkType == javaPlatform {
941 ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage,
942 ctx.OtherModuleName(to))
943 }
944 break
Jiyong Park2d492942018-03-05 17:44:10 +0900945 case javaPlatform:
946 // no restriction on link-type
947 break
Jiyong Park750e5572018-01-31 00:20:13 +0900948 }
949}
950
Colin Cross32f676a2017-09-06 13:41:06 -0700951func (j *Module) collectDeps(ctx android.ModuleContext) deps {
952 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700953
Colin Cross300f0382018-03-06 13:11:51 -0800954 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700955 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800956 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700957 ctx.AddMissingDependencies(sdkDep.bootclasspath)
958 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800959 } else if sdkDep.useFiles {
960 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700961 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700962 deps.aidlPreprocess = sdkDep.aidl
963 } else {
964 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800965 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700966 }
967
Paul Duffin64e61992020-05-15 10:20:31 +0100968 // If this is a component library (stubs, etc.) for a java_sdk_library then
969 // add the name of that java_sdk_library to the exported sdk libs to make sure
970 // that, if necessary, a <uses-library> element for that java_sdk_library is
971 // added to the Android manifest.
972 j.exportedSdkLibs = append(j.exportedSdkLibs, j.OptionalImplicitSdkLibrary()...)
973
Colin Crossd11fcda2017-10-23 17:59:01 -0700974 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700975 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700976 tag := ctx.OtherModuleDependencyTag(module)
977
Colin Crossa4f08812018-10-02 22:03:40 -0700978 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700979 // Handled by AndroidApp.collectAppDeps
980 return
981 }
982 if tag == certificateTag {
983 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700984 return
985 }
Artur Satayeve5ac15a2020-04-08 19:09:30 +0100986
Colin Cross54250902017-12-05 09:28:08 -0800987 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800988 case SdkLibraryDependency:
989 switch tag {
990 case libTag:
991 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
992 // names of sdk libs that are directly depended are exported
Paul Duffin64e61992020-05-15 10:20:31 +0100993 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.OptionalImplicitSdkLibrary()...)
Colin Cross79c7c262019-04-17 11:11:46 -0700994 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800995 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
996 }
Colin Cross54250902017-12-05 09:28:08 -0800997 case Dependency:
998 switch tag {
999 case bootClasspathTag:
1000 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -07001001 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -08001002 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +09001003 // sdk lib names from dependencies are re-exported
1004 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -07001005 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +00001006 pluginJars, pluginClasses := dep.ExportedPlugins()
1007 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -07001008 case java9LibTag:
1009 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -08001010 case staticLibTag:
1011 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
1012 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
1013 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -07001014 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +09001015 // sdk lib names from dependencies are re-exported
1016 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -07001017 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +00001018 pluginJars, pluginClasses := dep.ExportedPlugins()
1019 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -08001020 case pluginTag:
1021 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -08001022 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +00001023 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
1024 } else {
1025 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -08001026 }
1027 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
1028 } else {
1029 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
1030 }
Artur Satayev9cf46692019-11-26 18:08:34 +00001031 case exportedPluginTag:
1032 if plugin, ok := dep.(*Plugin); ok {
1033 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
1034 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
1035 }
1036 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
1037 if plugin.pluginProperties.Processor_class != nil {
1038 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
1039 }
1040 } else {
1041 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
1042 }
Nan Zhangb2b33de2018-02-23 11:18:47 -08001043 case frameworkApkTag:
1044 if ctx.ModuleName() == "android_stubs_current" ||
1045 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -07001046 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -08001047 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
1048 // resource files out of there for aapt.
1049 //
1050 // Normally the package rule runs aapt, which includes the resource,
1051 // but we're not running that in our package rule so just copy in the
1052 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -07001053 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -08001054 }
Colin Cross54250902017-12-05 09:28:08 -08001055 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -07001056 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -08001057 case kotlinAnnotationsTag:
1058 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -08001059 }
1060
Colin Cross54250902017-12-05 09:28:08 -08001061 case android.SourceFileProducer:
1062 switch tag {
1063 case libTag:
1064 checkProducesJars(ctx, dep)
1065 deps.classpath = append(deps.classpath, dep.Srcs()...)
1066 case staticLibTag:
1067 checkProducesJars(ctx, dep)
1068 deps.classpath = append(deps.classpath, dep.Srcs()...)
1069 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
1070 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -08001071 }
1072 default:
Colin Crossec7a0422017-07-07 14:47:12 -07001073 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001074 case bootClasspathTag:
1075 // If a system modules dependency has been added to the bootclasspath
1076 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +00001077 sm := module.(SystemModulesProvider)
1078 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +01001079
Colin Cross1369cdb2017-09-29 17:58:17 -07001080 case systemModulesTag:
1081 if deps.systemModules != nil {
1082 panic("Found two system module dependencies")
1083 }
Paul Duffin83a2d962019-11-19 19:44:10 +00001084 sm := module.(SystemModulesProvider)
1085 outputDir, outputDeps := sm.OutputDirAndDeps()
1086 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001087 }
Colin Crossec7a0422017-07-07 14:47:12 -07001088 }
Colin Cross2fe66872015-03-30 17:20:39 -07001089 })
1090
Jiyong Park1be96912018-05-28 18:02:19 +09001091 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1092
Colin Cross32f676a2017-09-06 13:41:06 -07001093 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001094}
1095
Artur Satayev9cf46692019-11-26 18:08:34 +00001096func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1097 deps.processorPath = append(deps.processorPath, pluginJars...)
1098 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1099}
1100
Colin Cross1e743852019-10-28 11:37:20 -07001101func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Nan Zhang357466b2018-04-17 17:38:36 -07001102 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001103 return normalizeJavaVersion(ctx, javaVersion)
Colin Crossdb224752020-05-14 18:05:32 -07001104 } else if ctx.Device() {
1105 return sdkContext.sdkVersion().defaultJavaLanguageVersion(ctx)
Nan Zhang357466b2018-04-17 17:38:36 -07001106 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001107 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001108 }
Nan Zhang357466b2018-04-17 17:38:36 -07001109}
1110
Colin Cross1e743852019-10-28 11:37:20 -07001111type javaVersion int
1112
1113const (
1114 JAVA_VERSION_UNSUPPORTED = 0
1115 JAVA_VERSION_6 = 6
1116 JAVA_VERSION_7 = 7
1117 JAVA_VERSION_8 = 8
1118 JAVA_VERSION_9 = 9
1119)
1120
1121func (v javaVersion) String() string {
1122 switch v {
1123 case JAVA_VERSION_6:
1124 return "1.6"
1125 case JAVA_VERSION_7:
1126 return "1.7"
1127 case JAVA_VERSION_8:
1128 return "1.8"
1129 case JAVA_VERSION_9:
1130 return "1.9"
1131 default:
1132 return "unsupported"
1133 }
1134}
1135
1136// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1137func (v javaVersion) usesJavaModules() bool {
1138 return v >= 9
1139}
1140
1141func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001142 switch javaVersion {
1143 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001144 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001145 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001146 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001147 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001148 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001149 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001150 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001151 case "10", "11":
1152 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001153 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001154 default:
1155 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001156 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001157 }
1158}
1159
Nan Zhanged19fc32017-10-19 13:06:22 -07001160func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001161
Colin Crossf03c82b2015-04-13 13:53:40 -07001162 var flags javaBuilderFlags
1163
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001164 // javaVersion flag.
1165 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1166
Nan Zhanged19fc32017-10-19 13:06:22 -07001167 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001168 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001169 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001170 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001171 }
Colin Cross6510f912017-11-29 00:27:14 -08001172 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001173 // Override the -g flag passed globally to remove local variable debug info to reduce
1174 // disk and memory usage.
1175 javacFlags = append(javacFlags, "-g:source,lines")
1176 }
Colin Crossc228a702019-11-06 16:18:05 -08001177 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001178
Colin Cross66548102018-06-19 22:47:35 -07001179 if ctx.Config().RunErrorProne() {
1180 if config.ErrorProneClasspath == nil {
1181 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1182 }
1183
1184 errorProneFlags := []string{
1185 "-Xplugin:ErrorProne",
1186 "${config.ErrorProneChecks}",
1187 }
1188 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1189
1190 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1191 "'" + strings.Join(errorProneFlags, " ") + "'"
1192 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001193 }
1194
Nan Zhanged19fc32017-10-19 13:06:22 -07001195 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001196 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1197 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001198 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001199 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001200
Colin Crossd99540b2020-04-22 11:44:34 -07001201 flags.processors = append(flags.processors, deps.processorClasses...)
1202 flags.processors = android.FirstUniqueStrings(flags.processors)
Colin Crossbe9cdb82019-01-21 21:37:16 -08001203
Colin Cross1e743852019-10-28 11:37:20 -07001204 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1205 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001206 // Give host-side tools a version of OpenJDK's standard libraries
1207 // close to what they're targeting. As of Dec 2017, AOSP is only
1208 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1209 //
1210 // When building with OpenJDK 8, the following should have no
1211 // effect since those jars would be available by default.
1212 //
1213 // When building with OpenJDK 9 but targeting a version < 1.8,
1214 // putting them on the bootclasspath means that:
1215 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1216 // b) references to existing APIs are not reinterpreted in an
1217 // OpenJDK 9-specific way, eg. calls to subclasses of
1218 // java.nio.Buffer as in http://b/70862583
1219 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1220 flags.bootClasspath = append(flags.bootClasspath,
1221 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1222 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001223 if Bool(j.properties.Use_tools_jar) {
1224 flags.bootClasspath = append(flags.bootClasspath,
1225 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1226 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001227 }
1228
Colin Cross1e743852019-10-28 11:37:20 -07001229 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001230 // Manually specify build directory in case it is not under the repo root.
1231 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1232 // just adding a symlink under the root doesn't help.)
1233 patchPaths := ".:" + ctx.Config().BuildDir()
1234 classPath := flags.classpath.FormJavaClassPath("")
1235 if classPath != "" {
1236 patchPaths += ":" + classPath
1237 }
1238 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001239 }
1240
Nan Zhanged19fc32017-10-19 13:06:22 -07001241 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001242 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001243
Nan Zhanged19fc32017-10-19 13:06:22 -07001244 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001245 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001246
Colin Cross81440082018-08-15 20:21:55 -07001247 if len(javacFlags) > 0 {
1248 // optimization.
1249 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1250 flags.javacFlags = "$javacFlags"
1251 }
1252
Nan Zhanged19fc32017-10-19 13:06:22 -07001253 return flags
1254}
Colin Crossc0b06f12015-04-08 13:03:43 -07001255
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001256func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001257 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001258
1259 deps := j.collectDeps(ctx)
1260 flags := j.collectBuilderFlags(ctx, deps)
1261
Colin Cross1e743852019-10-28 11:37:20 -07001262 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001263 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1264 }
Colin Cross8a497952019-03-05 22:25:09 -08001265 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001266 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001267 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001268 }
1269
Colin Crossaf050172017-11-15 23:01:59 -08001270 srcFiles = j.genSources(ctx, srcFiles, flags)
1271
1272 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001273 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001274 if aaptSrcJar != nil {
1275 srcJars = append(srcJars, aaptSrcJar)
1276 }
Colin Crossb7a63242015-04-16 14:09:14 -07001277
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001278 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001279 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001280 }
1281
Colin Cross1ee23172017-10-18 14:44:18 -07001282 jarName := ctx.ModuleName() + ".jar"
1283
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001284 javaSrcFiles := srcFiles.FilterByExt(".java")
1285 var uniqueSrcFiles android.Paths
1286 set := make(map[string]bool)
1287 for _, v := range javaSrcFiles {
1288 if _, found := set[v.String()]; !found {
1289 set[v.String()] = true
1290 uniqueSrcFiles = append(uniqueSrcFiles, v)
1291 }
1292 }
1293
patricktu242faad2019-09-24 15:41:30 +08001294 // Collect .java files for AIDEGen
1295 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1296
Colin Cross55f63ea2018-08-27 12:37:09 -07001297 var kotlinJars android.Paths
1298
Colin Cross93e85952017-08-15 13:34:18 -07001299 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001300 // user defined kotlin flags.
1301 kotlincFlags := j.properties.Kotlincflags
1302 CheckKotlincFlags(ctx, kotlincFlags)
1303
Colin Cross93e85952017-08-15 13:34:18 -07001304 // If there are kotlin files, compile them first but pass all the kotlin and java files
1305 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1306 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001307 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001308 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001309 kotlincFlags = append(kotlincFlags, "-no-jdk")
1310 }
1311 if len(kotlincFlags) > 0 {
1312 // optimization.
1313 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1314 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001315 }
1316
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001317 var kotlinSrcFiles android.Paths
1318 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1319 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1320
patricktu242faad2019-09-24 15:41:30 +08001321 // Collect .kt files for AIDEGen
1322 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1323
Colin Crossafbb1732019-01-17 15:42:52 -08001324 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1325 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1326
1327 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1328 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1329
1330 if len(flags.processorPath) > 0 {
1331 // Use kapt for annotation processing
1332 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
Colin Cross3791b452020-06-18 15:46:32 -07001333 kaptResJar := android.PathForModuleOut(ctx, "kapt", "kapt-res.jar")
1334 kotlinKapt(ctx, kaptSrcJar, kaptResJar, kotlinSrcFiles, srcJars, flags)
Colin Crossafbb1732019-01-17 15:42:52 -08001335 srcJars = append(srcJars, kaptSrcJar)
Colin Cross3791b452020-06-18 15:46:32 -07001336 kotlinJars = append(kotlinJars, kaptResJar)
Colin Crossafbb1732019-01-17 15:42:52 -08001337 // Disable annotation processing in javac, it's already been handled by kapt
1338 flags.processorPath = nil
Colin Crossd99540b2020-04-22 11:44:34 -07001339 flags.processors = nil
Colin Crossafbb1732019-01-17 15:42:52 -08001340 }
Colin Cross93e85952017-08-15 13:34:18 -07001341
Colin Cross1ee23172017-10-18 14:44:18 -07001342 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001343 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001344 if ctx.Failed() {
1345 return
1346 }
1347
1348 // Make javac rule depend on the kotlinc rule
1349 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001350
Colin Cross55f63ea2018-08-27 12:37:09 -07001351 kotlinJars = append(kotlinJars, kotlinJar)
Colin Crossaaf58062020-06-18 15:52:01 -07001352 // Jar kotlin classes into the final jar after javac
1353 if BoolDefault(j.properties.Static_kotlin_stdlib, true) {
1354 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
1355 }
Colin Cross93e85952017-08-15 13:34:18 -07001356 }
1357
Colin Cross55f63ea2018-08-27 12:37:09 -07001358 jars := append(android.Paths(nil), kotlinJars...)
1359
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001360 // Store the list of .java files that was passed to javac
1361 j.compiledJavaSrcs = uniqueSrcFiles
1362 j.compiledSrcJars = srcJars
1363
Nan Zhang61eaedb2017-11-02 13:28:15 -07001364 enable_sharding := false
Colin Crossf5a66282020-02-21 08:16:41 -08001365 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001366 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001367 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1368 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001369 // Formerly, there was a check here that prevented annotation processors
1370 // from being used when sharding was enabled, as some annotation processors
1371 // do not function correctly in sharded environments. It was removed to
1372 // allow for the use of annotation processors that do function correctly
1373 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001374 }
Colin Crossf5a66282020-02-21 08:16:41 -08001375 headerJarFileWithoutJarjar, j.headerJarFile =
1376 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001377 if ctx.Failed() {
1378 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001379 }
1380 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001381 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001382 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001383 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001384 // If error-prone is enabled, add an additional rule to compile the java files into
1385 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001386 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001387 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1388 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001389 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001390 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001391 extraJarDeps = append(extraJarDeps, errorprone)
1392 }
1393
Nan Zhang61eaedb2017-11-02 13:28:15 -07001394 if enable_sharding {
Colin Crossf5a66282020-02-21 08:16:41 -08001395 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001396 shardSize := int(*(j.properties.Javac_shard_size))
1397 var shardSrcs []android.Paths
1398 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001399 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001400 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001401 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1402 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001403 jars = append(jars, classes)
1404 }
1405 }
1406 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001407 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1408 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001409 jars = append(jars, classes)
1410 }
1411 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001412 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001413 jars = append(jars, classes)
1414 }
Colin Crossd6891432017-09-27 17:39:56 -07001415 if ctx.Failed() {
1416 return
1417 }
Colin Cross2fe66872015-03-30 17:20:39 -07001418 }
1419
Colin Cross0c4ce212019-05-03 15:28:19 -07001420 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1421
1422 var includeSrcJar android.WritablePath
1423 if Bool(j.properties.Include_srcs) {
1424 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1425 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1426 }
1427
Colin Crosscedd4762018-09-13 11:26:19 -07001428 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1429 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001430 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001431 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001432
1433 var resArgs []string
1434 var resDeps android.Paths
1435
1436 resArgs = append(resArgs, dirArgs...)
1437 resDeps = append(resDeps, dirDeps...)
1438
1439 resArgs = append(resArgs, fileArgs...)
1440 resDeps = append(resDeps, fileDeps...)
1441
Colin Cross988708c2019-05-06 14:04:11 -07001442 resArgs = append(resArgs, extraArgs...)
1443 resDeps = append(resDeps, extraDeps...)
1444
Colin Cross40a36712017-09-27 17:41:35 -07001445 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001446 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001447 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001448 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001449 if ctx.Failed() {
1450 return
1451 }
1452 }
1453
Colin Cross0c4ce212019-05-03 15:28:19 -07001454 var resourceJars android.Paths
1455 if j.resourceJar != nil {
1456 resourceJars = append(resourceJars, j.resourceJar)
1457 }
1458 if Bool(j.properties.Include_srcs) {
1459 resourceJars = append(resourceJars, includeSrcJar)
1460 }
1461 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001462
Colin Cross0c4ce212019-05-03 15:28:19 -07001463 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001464 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001465 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001466 false, nil, nil)
1467 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001468 } else if len(resourceJars) == 1 {
1469 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001470 }
1471
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001472 if len(deps.staticJars) > 0 {
1473 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001474 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001475
Colin Cross094054a2018-10-17 15:10:48 -07001476 manifest := j.overrideManifest
1477 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001478 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001479 }
Colin Cross635acc92017-09-12 22:50:46 -07001480
Colin Cross8a497952019-03-05 22:25:09 -08001481 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001482 if len(services) > 0 {
1483 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1484 var zipargs []string
1485 for _, file := range services {
1486 serviceFile := file.String()
1487 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1488 }
Kousik Kumarc05da0a2020-05-20 11:27:16 -07001489 rule := zip
1490 args := map[string]string{
1491 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
1492 }
1493 if ctx.Config().IsEnvTrue("RBE_ZIP") {
1494 rule = zipRE
1495 args["implicits"] = strings.Join(services.Strings(), ",")
1496 }
Alex Light7f004a72019-02-21 13:27:37 -08001497 ctx.Build(pctx, android.BuildParams{
Kousik Kumarc05da0a2020-05-20 11:27:16 -07001498 Rule: rule,
Alex Light7f004a72019-02-21 13:27:37 -08001499 Output: servicesJar,
1500 Implicits: services,
Kousik Kumarc05da0a2020-05-20 11:27:16 -07001501 Args: args,
Alex Light7f004a72019-02-21 13:27:37 -08001502 })
1503 jars = append(jars, servicesJar)
1504 }
1505
Colin Cross0a6e0072017-08-30 14:24:55 -07001506 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001507 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001508 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001509
1510 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001511 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1512 // Optimization: skip the combine step if there is nothing to do
1513 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1514 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1515 // any if len(jars) == 1.
1516 outputFile = moduleOutPath
1517 } else {
1518 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1519 ctx.Build(pctx, android.BuildParams{
1520 Rule: android.Cp,
1521 Input: jars[0],
1522 Output: combinedJar,
1523 })
1524 outputFile = combinedJar
1525 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001526 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001527 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001528 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001529 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001530 outputFile = combinedJar
1531 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001532
Colin Cross331a1212018-08-15 20:40:52 -07001533 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001534 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001535 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001536 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001537 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001538 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001539
1540 // jarjar resource jar if necessary
1541 if j.resourceJar != nil {
1542 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001543 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001544 j.resourceJar = resourceJarJarFile
1545 }
1546
Colin Cross0a6e0072017-08-30 14:24:55 -07001547 if ctx.Failed() {
1548 return
1549 }
1550 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001551
1552 // Check package restrictions if necessary.
1553 if len(j.properties.Permitted_packages) > 0 {
Tej Singh83839722020-06-03 18:45:54 +00001554 // Check packages and copy to package-checked file.
1555 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1556 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1557 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
Vladimir Marko0975ee02019-04-02 10:29:55 +01001558
1559 if ctx.Failed() {
1560 return
1561 }
1562 }
1563
Nan Zhanged19fc32017-10-19 13:06:22 -07001564 j.implementationJarFile = outputFile
1565 if j.headerJarFile == nil {
1566 j.headerJarFile = j.implementationJarFile
1567 }
Colin Cross2fe66872015-03-30 17:20:39 -07001568
Jiyong Park28826602020-02-21 16:04:53 +09001569 // Force enable the instrumentation for java code that is built for APEXes ...
1570 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1571 // doesn't make sense)
1572 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1573 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park81aaa0c2020-02-18 12:50:44 +00001574 j.properties.Instrument = true
1575 }
1576
Colin Cross3144dfc2018-01-03 15:06:47 -08001577 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001578 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1579 }
1580
Colin Cross331a1212018-08-15 20:40:52 -07001581 // merge implementation jar with resources if necessary
1582 implementationAndResourcesJar := outputFile
1583 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001584 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001585 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001586 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001587 false, nil, nil)
1588 implementationAndResourcesJar = combinedJar
1589 }
1590
1591 j.implementationAndResourcesJar = implementationAndResourcesJar
1592
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001593 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1594 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1595 if j.deviceProperties.Compile_dex == nil {
1596 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1597 }
1598 if j.deviceProperties.Hostdex == nil {
1599 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1600 }
1601 }
1602
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001603 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001604 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001605 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001606 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001607 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001608 if ctx.Failed() {
1609 return
1610 }
Colin Cross331a1212018-08-15 20:40:52 -07001611
Jiyong Park09cb6292019-07-15 15:29:23 +09001612 // Hidden API CSV generation and dex encoding
1613 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
David Srbecky98c71222020-05-20 22:20:28 +01001614 proptools.Bool(j.deviceProperties.Uncompress_dex))
Colin Cross8faf8fc2019-01-16 15:15:52 -08001615
Colin Cross331a1212018-08-15 20:40:52 -07001616 // merge dex jar with resources if necessary
1617 if j.resourceJar != nil {
1618 jars := android.Paths{dexOutputFile, j.resourceJar}
1619 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1620 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1621 false, nil, nil)
David Srbecky98c71222020-05-20 22:20:28 +01001622 if *j.deviceProperties.Uncompress_dex {
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001623 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1624 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1625 dexOutputFile = combinedAlignedJar
1626 } else {
1627 dexOutputFile = combinedJar
1628 }
Colin Cross331a1212018-08-15 20:40:52 -07001629 }
1630
1631 j.dexJarFile = dexOutputFile
1632
Colin Cross8faf8fc2019-01-16 15:15:52 -08001633 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001634 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1635
1636 j.maybeStrippedDexJarFile = dexOutputFile
1637
Colin Cross3063b782018-08-15 11:19:12 -07001638 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001639
1640 if ctx.Failed() {
1641 return
1642 }
Colin Cross331a1212018-08-15 20:40:52 -07001643 } else {
1644 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001645 }
Colin Cross331a1212018-08-15 20:40:52 -07001646
Colin Cross1e28e3c2020-06-02 20:09:13 -07001647 if ctx.Device() {
1648 lintSDKVersionString := func(sdkSpec sdkSpec) string {
1649 if v := sdkSpec.version; v.isNumbered() {
1650 return v.String()
1651 } else {
1652 return ctx.Config().DefaultAppTargetSdk()
1653 }
1654 }
1655
1656 j.linter.name = ctx.ModuleName()
1657 j.linter.srcs = srcFiles
1658 j.linter.srcJars = srcJars
1659 j.linter.classpath = append(append(android.Paths(nil), flags.bootClasspath...), flags.classpath...)
1660 j.linter.classes = j.implementationJarFile
1661 j.linter.minSdkVersion = lintSDKVersionString(j.minSdkVersion())
1662 j.linter.targetSdkVersion = lintSDKVersionString(j.targetSdkVersion())
1663 j.linter.compileSdkVersion = lintSDKVersionString(j.sdkVersion())
1664 j.linter.javaLanguageLevel = flags.javaVersion.String()
1665 j.linter.kotlinLanguageLevel = "1.3"
1666 j.linter.lint(ctx)
1667 }
1668
Colin Crossb7a63242015-04-16 14:09:14 -07001669 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001670
1671 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1672 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001673}
1674
Colin Cross3b706fd2019-09-05 16:44:18 -07001675func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1676 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1677
1678 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1679 if idx >= 0 {
1680 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1681 jarName += strconv.Itoa(idx)
1682 }
1683
1684 classes := android.PathForModuleOut(ctx, "javac", jarName)
1685 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1686
1687 if ctx.Config().EmitXrefRules() {
1688 extractionFile := android.PathForModuleOut(ctx, kzipName)
1689 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1690 j.kytheFiles = append(j.kytheFiles, extractionFile)
1691 }
1692
1693 return classes
1694}
1695
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001696// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1697// since some of these flags may be used internally.
1698func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1699 for _, flag := range flags {
1700 flag = strings.TrimSpace(flag)
1701
1702 if !strings.HasPrefix(flag, "-") {
1703 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1704 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1705 ctx.PropertyErrorf("kotlincflags",
1706 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1707 } else if inList(flag, config.KotlincIllegalFlags) {
1708 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1709 } else if flag == "-include-runtime" {
1710 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1711 } else {
1712 args := strings.Split(flag, " ")
1713 if args[0] == "-kotlin-home" {
1714 ctx.PropertyErrorf("kotlincflags",
1715 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1716 }
1717 }
1718 }
1719}
1720
Colin Cross8eadbf02017-10-24 17:46:00 -07001721func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf5a66282020-02-21 08:16:41 -08001722 deps deps, flags javaBuilderFlags, jarName string,
1723 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001724
1725 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001726 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001727 // Compile java sources into turbine.jar.
1728 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1729 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1730 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001731 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001732 }
1733 jars = append(jars, turbineJar)
1734 }
1735
Colin Cross55f63ea2018-08-27 12:37:09 -07001736 jars = append(jars, extraJars...)
1737
Nan Zhanged19fc32017-10-19 13:06:22 -07001738 // Combine any static header libraries into classes-header.jar. If there is only
1739 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001740 jars = append(jars, deps.staticHeaderJars...)
1741
Colin Cross5c6ecc12017-10-23 18:12:27 -07001742 // we cannot skip the combine step for now if there is only one jar
1743 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1744 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001745 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001746 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001747 headerJar = combinedJar
Colin Crossf5a66282020-02-21 08:16:41 -08001748 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001749
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001750 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001751 // Transform classes.jar into classes-jarjar.jar
1752 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001753 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf5a66282020-02-21 08:16:41 -08001754 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001755 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001756 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001757 }
1758 }
1759
Colin Crossf5a66282020-02-21 08:16:41 -08001760 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001761}
1762
Colin Crosscb933592017-11-22 13:49:43 -08001763func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001764 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001765
Colin Cross7a3139e2017-12-19 13:57:50 -08001766 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001767
Colin Cross84c38822018-01-03 15:59:46 -08001768 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001769 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1770
1771 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1772
1773 j.jacocoReportClassesFile = jacocoReportClassesFile
1774
1775 return instrumentedJar
1776}
1777
albaltai36ff7dc2018-12-25 14:35:23 +08001778var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001779
Nan Zhanged19fc32017-10-19 13:06:22 -07001780func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001781 if j.headerJarFile == nil {
1782 return nil
1783 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001784 return android.Paths{j.headerJarFile}
1785}
1786
1787func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001788 if j.implementationJarFile == nil {
1789 return nil
1790 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001791 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001792}
1793
Colin Crossf24a22a2019-01-31 14:12:44 -08001794func (j *Module) DexJar() android.Path {
1795 return j.dexJarFile
1796}
1797
Colin Cross331a1212018-08-15 20:40:52 -07001798func (j *Module) ResourceJars() android.Paths {
1799 if j.resourceJar == nil {
1800 return nil
1801 }
1802 return android.Paths{j.resourceJar}
1803}
1804
1805func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001806 if j.implementationAndResourcesJar == nil {
1807 return nil
1808 }
Colin Cross331a1212018-08-15 20:40:52 -07001809 return android.Paths{j.implementationAndResourcesJar}
1810}
1811
Colin Cross46c9b8b2017-06-22 16:51:17 -07001812func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001813 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001814 return j.exportAidlIncludeDirs
1815}
1816
Jiyong Park1be96912018-05-28 18:02:19 +09001817func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001818 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001819 return j.exportedSdkLibs
1820}
1821
Artur Satayev9cf46692019-11-26 18:08:34 +00001822func (j *Module) ExportedPlugins() (android.Paths, []string) {
1823 return j.exportedPluginJars, j.exportedPluginClasses
1824}
1825
Colin Cross0c4ce212019-05-03 15:28:19 -07001826func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1827 return j.srcJarArgs, j.srcJarDeps
1828}
1829
Colin Cross46c9b8b2017-06-22 16:51:17 -07001830var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001831
Colin Cross46c9b8b2017-06-22 16:51:17 -07001832func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001833 return j.logtagsSrcs
1834}
1835
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001836// Collect information for opening IDE project files in java/jdeps.go.
1837func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1838 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1839 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001840 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001841 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001842 if j.expandJarjarRules != nil {
1843 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001844 }
1845}
1846
1847func (j *Module) CompilerDeps() []string {
1848 jdeps := []string{}
1849 jdeps = append(jdeps, j.properties.Libs...)
1850 jdeps = append(jdeps, j.properties.Static_libs...)
1851 return jdeps
1852}
1853
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001854func (j *Module) hasCode(ctx android.ModuleContext) bool {
1855 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1856 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1857}
1858
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001859func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffinc8f2f182020-05-17 08:34:50 +01001860 return j.depIsInSameApex(ctx, dep)
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001861}
1862
Jiyong Park0b238752019-10-29 11:23:10 +09001863func (j *Module) Stem() string {
1864 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1865}
1866
Jiyong Park618922e2020-01-08 13:35:43 +09001867func (j *Module) JacocoReportClassesFile() android.Path {
1868 return j.jacocoReportClassesFile
1869}
1870
Martin Stjernholm6d415272020-01-31 17:10:36 +00001871func (j *Module) IsInstallable() bool {
1872 return Bool(j.properties.Installable)
1873}
1874
Colin Cross2fe66872015-03-30 17:20:39 -07001875//
1876// Java libraries (.jar file)
1877//
1878
Anton Hansson1e65f942020-03-27 19:39:48 +00001879type LibraryProperties struct {
1880 Dist struct {
1881 // The tag of the output of this module that should be output.
1882 Tag *string `android:"arch_variant"`
1883 } `android:"arch_variant"`
1884}
1885
Colin Crossf506d872017-07-19 15:53:04 -07001886type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001887 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001888
Anton Hansson1e65f942020-03-27 19:39:48 +00001889 libraryProperties LibraryProperties
1890
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001891 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001892}
1893
Paul Duffina105cf92020-05-29 11:24:51 +01001894// Provides access to the list of permitted packages from updatable boot jars.
1895type PermittedPackagesForUpdatableBootJars interface {
1896 PermittedPackagesForUpdatableBootJars() []string
1897}
1898
1899var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil)
1900
1901func (j *Library) PermittedPackagesForUpdatableBootJars() []string {
1902 return j.properties.Permitted_packages
1903}
1904
Colin Cross42be7612019-02-21 18:12:14 -08001905func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001906 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1907 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1908 return true
1909 }
1910
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001911 // Store uncompressed (and do not strip) dex files from boot class path jars.
1912 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1913 return true
1914 }
1915
1916 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001917 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001918 return true
1919 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001920 if ctx.Config().UncompressPrivAppDex() &&
1921 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1922 return true
1923 }
1924
Colin Cross2fc72f62018-12-21 12:59:54 -08001925 return false
1926}
1927
Colin Crossf506d872017-07-19 15:53:04 -07001928func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayeve5ac15a2020-04-08 19:09:30 +01001929 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001930 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001931 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
David Srbecky98c71222020-05-20 22:20:28 +01001932 if j.deviceProperties.Uncompress_dex == nil {
1933 // If the value was not force-set by the user, use reasonable default based on the module.
1934 j.deviceProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, &j.dexpreopter))
1935 }
1936 j.dexpreopter.uncompressedDex = *j.deviceProperties.Uncompress_dex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001937 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001938
Jiyong Park7f7766d2019-07-25 22:02:35 +09001939 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1940 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001941 var extraInstallDeps android.Paths
1942 if j.InstallMixin != nil {
1943 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1944 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001945 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Parked50ca82020-05-28 23:46:55 +09001946 j.Stem()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001947 }
Anton Hansson1e65f942020-03-27 19:39:48 +00001948
1949 // Verify Dist.Tag is set to a supported output
1950 if j.libraryProperties.Dist.Tag != nil {
1951 distFiles, err := j.OutputFiles(*j.libraryProperties.Dist.Tag)
1952 if err != nil {
1953 ctx.PropertyErrorf("dist.tag", "%s", err.Error())
1954 }
1955 j.distFile = distFiles[0]
1956 }
Colin Crossb7a63242015-04-16 14:09:14 -07001957}
1958
Colin Crossf506d872017-07-19 15:53:04 -07001959func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001960 j.deps(ctx)
1961}
1962
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001963const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001964 aidlIncludeDir = "aidl"
1965 javaDir = "java"
1966 jarFileSuffix = ".jar"
1967 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001968)
1969
Paul Duffina0dbf432019-12-05 11:25:53 +00001970// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin9b358d72020-03-02 10:16:35 +00001971func sdkSnapshotFilePathForJar(osPrefix, name string) string {
1972 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001973}
1974
Paul Duffin9b358d72020-03-02 10:16:35 +00001975func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
1976 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001977}
1978
Paul Duffin13879572019-11-28 14:31:38 +00001979type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001980 android.SdkMemberTypeBase
Paul Duffin2b9e3d32020-02-28 14:39:53 +00001981
1982 // Function to retrieve the appropriate output jar (implementation or header) from
1983 // the library.
1984 jarToExportGetter func(j *Library) android.Path
Paul Duffin13879572019-11-28 14:31:38 +00001985}
1986
1987func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1988 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1989}
1990
1991func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1992 _, ok := module.(*Library)
1993 return ok
1994}
1995
Paul Duffin17ab8832020-03-19 16:11:18 +00001996func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1997 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffind76209b2020-03-02 11:33:02 +00001998}
Paul Duffina0dbf432019-12-05 11:25:53 +00001999
Paul Duffind76209b2020-03-02 11:33:02 +00002000func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffinc9103932020-03-17 21:04:24 +00002001 return &librarySdkMemberProperties{}
Paul Duffind76209b2020-03-02 11:33:02 +00002002}
2003
2004type librarySdkMemberProperties struct {
2005 android.SdkMemberPropertiesBase
2006
Paul Duffin08385bf2020-05-06 10:23:19 +01002007 JarToExport android.Path `android:"arch_variant"`
Paul Duffinc9103932020-03-17 21:04:24 +00002008 AidlIncludeDirs android.Paths
Paul Duffind76209b2020-03-02 11:33:02 +00002009}
2010
Paul Duffin17ab8832020-03-19 16:11:18 +00002011func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +00002012 j := variant.(*Library)
2013
Paul Duffinc9103932020-03-17 21:04:24 +00002014 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(j)
2015 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffind76209b2020-03-02 11:33:02 +00002016}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002017
Paul Duffin17ab8832020-03-19 16:11:18 +00002018func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinc9103932020-03-17 21:04:24 +00002019 builder := ctx.SnapshotBuilder()
Paul Duffin17ab8832020-03-19 16:11:18 +00002020
Paul Duffinc9103932020-03-17 21:04:24 +00002021 exportedJar := p.JarToExport
2022 if exportedJar != nil {
2023 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
Paul Duffind76209b2020-03-02 11:33:02 +00002024 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
2025
Paul Duffinc9103932020-03-17 21:04:24 +00002026 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2027 }
2028
2029 aidlIncludeDirs := p.AidlIncludeDirs
2030 if len(aidlIncludeDirs) != 0 {
2031 sdkModuleContext := ctx.SdkModuleContext()
2032 for _, dir := range aidlIncludeDirs {
Paul Duffind76209b2020-03-02 11:33:02 +00002033 // TODO(jiyong): copy parcelable declarations only
2034 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
2035 for _, file := range aidlFiles {
2036 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
2037 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002038 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002039
Paul Duffinc9103932020-03-17 21:04:24 +00002040 // TODO(b/151933053) - add aidl include dirs property
Paul Duffind76209b2020-03-02 11:33:02 +00002041 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00002042}
2043
Paul Duffin2b9e3d32020-02-28 14:39:53 +00002044var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
2045 android.SdkMemberTypeBase{
2046 PropertyName: "java_header_libs",
2047 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +00002048 },
Paul Duffin2b9e3d32020-02-28 14:39:53 +00002049 func(j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +00002050 headerJars := j.HeaderJars()
2051 if len(headerJars) != 1 {
2052 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
2053 }
2054
2055 return headerJars[0]
Paul Duffin2b9e3d32020-02-28 14:39:53 +00002056 },
Paul Duffina0dbf432019-12-05 11:25:53 +00002057}
2058
Colin Cross1b16b0e2019-02-12 14:41:32 -08002059// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
2060//
2061// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
2062// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
2063// as a `static_libs` dependency of another module.
2064//
2065// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
2066// a device.
2067//
2068// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2069// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07002070func LibraryFactory() android.Module {
2071 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07002072
Colin Cross1c14b4e2020-06-15 16:09:53 -07002073 module.addHostAndDeviceProperties()
2074 module.AddProperties(&module.libraryProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07002075
Paul Duffin64e61992020-05-15 10:20:31 +01002076 module.initModuleAndImport(&module.ModuleBase)
2077
Jiyong Park7f7766d2019-07-25 22:02:35 +09002078 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002079 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002080 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07002081 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002082}
2083
Colin Cross1b16b0e2019-02-12 14:41:32 -08002084// java_library_static is an obsolete alias for java_library.
2085func LibraryStaticFactory() android.Module {
2086 return LibraryFactory()
2087}
2088
2089// java_library_host builds and links sources into a `.jar` file for the host.
2090//
2091// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
2092// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002093func LibraryHostFactory() android.Module {
2094 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07002095
Colin Cross1c14b4e2020-06-15 16:09:53 -07002096 module.addHostProperties()
Colin Cross36242852017-06-23 15:06:31 -07002097
Colin Cross9ae1b922018-06-26 17:59:05 -07002098 module.Module.properties.Installable = proptools.BoolPtr(true)
2099
Jiyong Park7f7766d2019-07-25 22:02:35 +09002100 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002101 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002102 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002103}
2104
2105//
Colin Crossb628ea52018-08-14 16:42:33 -07002106// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002107//
2108
2109type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002110 // list of compatibility suites (for example "cts", "vts") that the module should be
2111 // installed into.
2112 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002113
2114 // the name of the test configuration (for example "AndroidTest.xml") that should be
2115 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002116 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002117
Jack He33338892018-09-19 02:21:28 -07002118 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2119 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002120 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002121
Colin Crossd96ca352018-08-10 16:06:24 -07002122 // list of files or filegroup modules that provide data that should be installed alongside
2123 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002124 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002125
2126 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2127 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2128 // explicitly.
2129 Auto_gen_config *bool
easoncyleeba606252020-04-30 14:57:06 +08002130
2131 // Add parameterized mainline modules to auto generated test config. The options will be
2132 // handled by TradeFed to do downloading and installing the specified modules on the device.
2133 Test_mainline_modules []string
Colin Cross05638fc2018-04-09 18:40:24 -07002134}
2135
Paul Duffin42df1442019-03-20 12:45:53 +00002136type testHelperLibraryProperties struct {
2137 // list of compatibility suites (for example "cts", "vts") that the module should be
2138 // installed into.
2139 Test_suites []string `android:"arch_variant"`
2140}
2141
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002142type prebuiltTestProperties struct {
2143 // list of compatibility suites (for example "cts", "vts") that the module should be
2144 // installed into.
2145 Test_suites []string `android:"arch_variant"`
2146
2147 // the name of the test configuration (for example "AndroidTest.xml") that should be
2148 // installed with the module.
2149 Test_config *string `android:"path,arch_variant"`
2150}
2151
Colin Cross05638fc2018-04-09 18:40:24 -07002152type Test struct {
2153 Library
2154
2155 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002156
2157 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002158 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002159}
2160
Paul Duffin42df1442019-03-20 12:45:53 +00002161type TestHelperLibrary struct {
2162 Library
2163
2164 testHelperLibraryProperties testHelperLibraryProperties
2165}
2166
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002167type JavaTestImport struct {
2168 Import
2169
2170 prebuiltTestProperties prebuiltTestProperties
2171
2172 testConfig android.Path
2173}
2174
Colin Cross303e21f2018-08-07 16:49:25 -07002175func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002176 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2177 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002178 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002179
2180 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002181}
2182
Paul Duffin42df1442019-03-20 12:45:53 +00002183func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2184 j.Library.GenerateAndroidBuildActions(ctx)
2185}
2186
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002187func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2188 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2189 j.prebuiltTestProperties.Test_suites, nil)
2190
2191 j.Import.GenerateAndroidBuildActions(ctx)
2192}
2193
2194type testSdkMemberType struct {
2195 android.SdkMemberTypeBase
2196}
2197
2198func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2199 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2200}
2201
2202func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2203 _, ok := module.(*Test)
2204 return ok
2205}
2206
Paul Duffin17ab8832020-03-19 16:11:18 +00002207func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2208 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffind76209b2020-03-02 11:33:02 +00002209}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002210
Paul Duffind76209b2020-03-02 11:33:02 +00002211func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2212 return &testSdkMemberProperties{}
2213}
2214
2215type testSdkMemberProperties struct {
2216 android.SdkMemberPropertiesBase
2217
Paul Duffinc9103932020-03-17 21:04:24 +00002218 JarToExport android.Path
2219 TestConfig android.Path
Paul Duffind76209b2020-03-02 11:33:02 +00002220}
2221
Paul Duffin17ab8832020-03-19 16:11:18 +00002222func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffind76209b2020-03-02 11:33:02 +00002223 test := variant.(*Test)
2224
2225 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002226 if len(implementationJars) != 1 {
Paul Duffind76209b2020-03-02 11:33:02 +00002227 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002228 }
2229
Paul Duffinc9103932020-03-17 21:04:24 +00002230 p.JarToExport = implementationJars[0]
2231 p.TestConfig = test.testConfig
Paul Duffind76209b2020-03-02 11:33:02 +00002232}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002233
Paul Duffin17ab8832020-03-19 16:11:18 +00002234func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffinc9103932020-03-17 21:04:24 +00002235 builder := ctx.SnapshotBuilder()
Paul Duffin17ab8832020-03-19 16:11:18 +00002236
Paul Duffinc9103932020-03-17 21:04:24 +00002237 exportedJar := p.JarToExport
2238 if exportedJar != nil {
2239 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
2240 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffind76209b2020-03-02 11:33:02 +00002241
2242 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffinc9103932020-03-17 21:04:24 +00002243 }
2244
2245 testConfig := p.TestConfig
2246 if testConfig != nil {
2247 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
2248 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffind76209b2020-03-02 11:33:02 +00002249 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
2250 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002251}
2252
Colin Cross1b16b0e2019-02-12 14:41:32 -08002253// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2254// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2255//
2256// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2257// compiled against the device bootclasspath.
2258//
2259// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2260// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002261func TestFactory() android.Module {
2262 module := &Test{}
2263
Colin Cross1c14b4e2020-06-15 16:09:53 -07002264 module.addHostAndDeviceProperties()
2265 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07002266
Colin Cross9ae1b922018-06-26 17:59:05 -07002267 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002268 module.Module.dexpreopter.isTest = true
Colin Cross1e28e3c2020-06-02 20:09:13 -07002269 module.Module.linter.test = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002270
Colin Cross05638fc2018-04-09 18:40:24 -07002271 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002272 return module
2273}
2274
Paul Duffin42df1442019-03-20 12:45:53 +00002275// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2276func TestHelperLibraryFactory() android.Module {
2277 module := &TestHelperLibrary{}
2278
Colin Cross1c14b4e2020-06-15 16:09:53 -07002279 module.addHostAndDeviceProperties()
2280 module.AddProperties(&module.testHelperLibraryProperties)
Paul Duffin42df1442019-03-20 12:45:53 +00002281
Colin Cross9a4abed2019-04-24 13:19:28 -07002282 module.Module.properties.Installable = proptools.BoolPtr(true)
2283 module.Module.dexpreopter.isTest = true
Colin Cross1e28e3c2020-06-02 20:09:13 -07002284 module.Module.linter.test = true
Colin Cross9a4abed2019-04-24 13:19:28 -07002285
Paul Duffin42df1442019-03-20 12:45:53 +00002286 InitJavaModule(module, android.HostAndDeviceSupported)
2287 return module
2288}
2289
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002290// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2291// and makes sure that it is added to the appropriate test suite.
2292//
2293// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2294// compiled against an Android classpath.
2295//
2296// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2297// for host modules.
2298func JavaTestImportFactory() android.Module {
2299 module := &JavaTestImport{}
2300
2301 module.AddProperties(
2302 &module.Import.properties,
2303 &module.prebuiltTestProperties)
2304
2305 module.Import.properties.Installable = proptools.BoolPtr(true)
2306
2307 android.InitPrebuiltModule(module, &module.properties.Jars)
2308 android.InitApexModule(module)
2309 android.InitSdkAwareModule(module)
2310 InitJavaModule(module, android.HostAndDeviceSupported)
2311 return module
2312}
2313
Colin Cross1b16b0e2019-02-12 14:41:32 -08002314// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2315// allow running the test with `atest` or a `TEST_MAPPING` file.
2316//
2317// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2318// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002319func TestHostFactory() android.Module {
2320 module := &Test{}
2321
Colin Cross1c14b4e2020-06-15 16:09:53 -07002322 module.addHostProperties()
2323 module.AddProperties(&module.testProperties)
Colin Cross05638fc2018-04-09 18:40:24 -07002324
Colin Cross9ae1b922018-06-26 17:59:05 -07002325 module.Module.properties.Installable = proptools.BoolPtr(true)
2326
Colin Cross05638fc2018-04-09 18:40:24 -07002327 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002328 return module
2329}
2330
2331//
Colin Cross2fe66872015-03-30 17:20:39 -07002332// Java Binaries (.jar file plus wrapper script)
2333//
2334
Colin Crossf506d872017-07-19 15:53:04 -07002335type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002336 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002337 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002338
2339 // Name of the class containing main to be inserted into the manifest as Main-Class.
2340 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002341}
2342
Colin Crossf506d872017-07-19 15:53:04 -07002343type Binary struct {
2344 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002345
Colin Crossf506d872017-07-19 15:53:04 -07002346 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002347
Colin Cross6b4a32d2017-12-05 13:42:45 -08002348 isWrapperVariant bool
2349
Colin Crossc3315992017-12-08 19:12:36 -08002350 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002351 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002352}
2353
Alex Light24237172017-10-26 09:46:21 -07002354func (j *Binary) HostToolPath() android.OptionalPath {
2355 return android.OptionalPathForPath(j.binaryFile)
2356}
2357
Colin Crossf506d872017-07-19 15:53:04 -07002358func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002359 if ctx.Arch().ArchType == android.Common {
2360 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002361 if j.binaryProperties.Main_class != nil {
2362 if j.properties.Manifest != nil {
2363 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2364 }
2365 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2366 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2367 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2368 }
2369
Colin Cross6b4a32d2017-12-05 13:42:45 -08002370 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002371 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002372 // Handle the binary wrapper
2373 j.isWrapperVariant = true
2374
Colin Cross366938f2017-12-11 16:29:02 -08002375 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002376 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002377 } else {
2378 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2379 }
2380
2381 // Depend on the installed jar so that the wrapper doesn't get executed by
2382 // another build rule before the jar has been installed.
2383 jarFile := ctx.PrimaryModule().(*Binary).installFile
2384
2385 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2386 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002387 }
Colin Cross2fe66872015-03-30 17:20:39 -07002388}
2389
Colin Crossf506d872017-07-19 15:53:04 -07002390func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002391 if ctx.Arch().ArchType == android.Common {
2392 j.deps(ctx)
2393 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002394}
2395
Colin Cross1b16b0e2019-02-12 14:41:32 -08002396// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2397// as well.
2398//
2399// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2400// compiled against the device bootclasspath.
2401//
2402// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2403// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002404func BinaryFactory() android.Module {
2405 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002406
Colin Cross1c14b4e2020-06-15 16:09:53 -07002407 module.addHostAndDeviceProperties()
2408 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002409
Colin Cross9ae1b922018-06-26 17:59:05 -07002410 module.Module.properties.Installable = proptools.BoolPtr(true)
2411
Colin Cross6b4a32d2017-12-05 13:42:45 -08002412 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2413 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002414 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002415}
2416
Colin Cross1b16b0e2019-02-12 14:41:32 -08002417// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2418//
2419// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2420// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002421func BinaryHostFactory() android.Module {
2422 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002423
Colin Cross1c14b4e2020-06-15 16:09:53 -07002424 module.addHostProperties()
2425 module.AddProperties(&module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002426
Colin Cross9ae1b922018-06-26 17:59:05 -07002427 module.Module.properties.Installable = proptools.BoolPtr(true)
2428
Colin Cross6b4a32d2017-12-05 13:42:45 -08002429 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2430 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002431 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002432}
2433
2434//
2435// Java prebuilts
2436//
2437
Colin Cross74d73e22017-08-02 11:05:49 -07002438type ImportProperties struct {
Paul Duffin9b358d72020-03-02 10:16:35 +00002439 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002440
Nan Zhangea568a42017-11-08 21:20:04 -08002441 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002442
2443 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002444
2445 // List of shared java libs that this module has dependencies to
2446 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002447
2448 // List of files to remove from the jar file(s)
2449 Exclude_files []string
2450
2451 // List of directories to remove from the jar file(s)
2452 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002453
2454 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002455 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002456
2457 // set the name of the output
2458 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002459}
2460
2461type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002462 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002463 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002464 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002465 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002466 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002467
Paul Duffinc8f2f182020-05-17 08:34:50 +01002468 // Functionality common to Module and Import.
2469 embeddableInModuleAndImport
2470
Colin Cross74d73e22017-08-02 11:05:49 -07002471 properties ImportProperties
2472
Colin Cross0a6e0072017-08-30 14:24:55 -07002473 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002474 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002475}
2476
Jiyong Park6a927c42020-01-21 02:03:43 +09002477func (j *Import) sdkVersion() sdkSpec {
2478 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002479}
2480
Jiyong Park6a927c42020-01-21 02:03:43 +09002481func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002482 return j.sdkVersion()
2483}
2484
Artur Satayev388d39b2020-04-27 18:53:18 +01002485func (j *Import) MinSdkVersion() string {
2486 return j.minSdkVersion().version.String()
2487}
2488
Colin Cross74d73e22017-08-02 11:05:49 -07002489func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002490 return &j.prebuilt
2491}
2492
Colin Cross74d73e22017-08-02 11:05:49 -07002493func (j *Import) PrebuiltSrcs() []string {
2494 return j.properties.Jars
2495}
2496
2497func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002498 return j.prebuilt.Name(j.ModuleBase.Name())
2499}
2500
Jiyong Park0b238752019-10-29 11:23:10 +09002501func (j *Import) Stem() string {
2502 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2503}
2504
Jiyong Park618922e2020-01-08 13:35:43 +09002505func (a *Import) JacocoReportClassesFile() android.Path {
2506 return nil
2507}
2508
Colin Cross74d73e22017-08-02 11:05:49 -07002509func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002510 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002511}
2512
Colin Cross74d73e22017-08-02 11:05:49 -07002513func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002514 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002515
Jiyong Park0b238752019-10-29 11:23:10 +09002516 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002517 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002518 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2519 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002520 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002521 inputFile := outputFile
2522 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2523 TransformJetifier(ctx, outputFile, inputFile)
2524 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002525 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002526
Paul Duffin64e61992020-05-15 10:20:31 +01002527 // If this is a component library (impl, stubs, etc.) for a java_sdk_library then
2528 // add the name of that java_sdk_library to the exported sdk libs to make sure
2529 // that, if necessary, a <uses-library> element for that java_sdk_library is
2530 // added to the Android manifest.
2531 j.exportedSdkLibs = append(j.exportedSdkLibs, j.OptionalImplicitSdkLibrary()...)
2532
Jiyong Park1be96912018-05-28 18:02:19 +09002533 ctx.VisitDirectDeps(func(module android.Module) {
2534 otherName := ctx.OtherModuleName(module)
2535 tag := ctx.OtherModuleDependencyTag(module)
2536
2537 switch dep := module.(type) {
2538 case Dependency:
2539 switch tag {
2540 case libTag, staticLibTag:
2541 // sdk lib names from dependencies are re-exported
2542 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2543 }
2544 case SdkLibraryDependency:
2545 switch tag {
2546 case libTag:
2547 // names of sdk libs that are directly depended are exported
2548 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2549 }
2550 }
2551 })
2552
2553 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002554 if Bool(j.properties.Installable) {
2555 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002556 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002557 }
Colin Cross2fe66872015-03-30 17:20:39 -07002558}
2559
Colin Cross74d73e22017-08-02 11:05:49 -07002560var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002561
Nan Zhanged19fc32017-10-19 13:06:22 -07002562func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002563 if j.combinedClasspathFile == nil {
2564 return nil
2565 }
Colin Cross37f6d792018-07-12 12:28:41 -07002566 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002567}
2568
2569func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002570 if j.combinedClasspathFile == nil {
2571 return nil
2572 }
Colin Cross37f6d792018-07-12 12:28:41 -07002573 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002574}
2575
Colin Cross331a1212018-08-15 20:40:52 -07002576func (j *Import) ResourceJars() android.Paths {
2577 return nil
2578}
2579
2580func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002581 if j.combinedClasspathFile == nil {
2582 return nil
2583 }
Colin Cross331a1212018-08-15 20:40:52 -07002584 return android.Paths{j.combinedClasspathFile}
2585}
2586
Colin Crossf24a22a2019-01-31 14:12:44 -08002587func (j *Import) DexJar() android.Path {
2588 return nil
2589}
2590
Colin Cross74d73e22017-08-02 11:05:49 -07002591func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002592 return nil
2593}
2594
Jiyong Park1be96912018-05-28 18:02:19 +09002595func (j *Import) ExportedSdkLibs() []string {
2596 return j.exportedSdkLibs
2597}
2598
Artur Satayev9cf46692019-11-26 18:08:34 +00002599func (j *Import) ExportedPlugins() (android.Paths, []string) {
2600 return nil, nil
2601}
2602
Colin Cross0c4ce212019-05-03 15:28:19 -07002603func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2604 return nil, nil
2605}
2606
Jiyong Park0f80c182020-01-31 02:49:53 +09002607func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Paul Duffinc8f2f182020-05-17 08:34:50 +01002608 return j.depIsInSameApex(ctx, dep)
Jiyong Park0f80c182020-01-31 02:49:53 +09002609}
2610
albaltai36ff7dc2018-12-25 14:35:23 +08002611// Add compile time check for interface implementation
2612var _ android.IDEInfo = (*Import)(nil)
2613var _ android.IDECustomizedModuleName = (*Import)(nil)
2614
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002615// Collect information for opening IDE project files in java/jdeps.go.
2616const (
2617 removedPrefix = "prebuilt_"
2618)
2619
2620func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2621 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2622}
2623
2624func (j *Import) IDECustomizedModuleName() string {
2625 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2626 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2627 // solution to get the Import name.
2628 name := j.Name()
2629 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002630 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002631 }
2632 return name
2633}
2634
Colin Cross74d73e22017-08-02 11:05:49 -07002635var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002636
Colin Cross1b16b0e2019-02-12 14:41:32 -08002637// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2638//
2639// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2640// compiled against an Android classpath.
2641//
2642// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2643// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002644func ImportFactory() android.Module {
2645 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002646
Colin Cross74d73e22017-08-02 11:05:49 -07002647 module.AddProperties(&module.properties)
2648
Paul Duffin64e61992020-05-15 10:20:31 +01002649 module.initModuleAndImport(&module.ModuleBase)
2650
Colin Cross74d73e22017-08-02 11:05:49 -07002651 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002652 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002653 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002654 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002655 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002656}
2657
Colin Cross1b16b0e2019-02-12 14:41:32 -08002658// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2659// module.
2660//
2661// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2662// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002663func ImportFactoryHost() android.Module {
2664 module := &Import{}
2665
2666 module.AddProperties(&module.properties)
2667
2668 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002669 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002670 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002671 return module
2672}
2673
Colin Cross42be7612019-02-21 18:12:14 -08002674// dex_import module
2675
2676type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002677 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002678
2679 // set the name of the output
2680 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002681}
2682
2683type DexImport struct {
2684 android.ModuleBase
2685 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002686 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002687 prebuilt android.Prebuilt
2688
2689 properties DexImportProperties
2690
2691 dexJarFile android.Path
2692 maybeStrippedDexJarFile android.Path
2693
2694 dexpreopter
2695}
2696
2697func (j *DexImport) Prebuilt() *android.Prebuilt {
2698 return &j.prebuilt
2699}
2700
2701func (j *DexImport) PrebuiltSrcs() []string {
2702 return j.properties.Jars
2703}
2704
2705func (j *DexImport) Name() string {
2706 return j.prebuilt.Name(j.ModuleBase.Name())
2707}
2708
Jiyong Park0b238752019-10-29 11:23:10 +09002709func (j *DexImport) Stem() string {
2710 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2711}
2712
Jiyong Park2cd081c2020-06-01 21:39:15 +09002713func (a *DexImport) JacocoReportClassesFile() android.Path {
2714 return nil
2715}
2716
Martin Stjernholm6d415272020-01-31 17:10:36 +00002717func (j *DexImport) IsInstallable() bool {
2718 return true
2719}
2720
Colin Cross42be7612019-02-21 18:12:14 -08002721func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2722 if len(j.properties.Jars) != 1 {
2723 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2724 }
2725
Jiyong Park0b238752019-10-29 11:23:10 +09002726 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002727 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2728
2729 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2730 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2731
2732 if j.dexpreopter.uncompressedDex {
2733 rule := android.NewRuleBuilder()
2734
2735 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2736 rule.Temporary(temporary)
2737
2738 // use zip2zip to uncompress classes*.dex files
2739 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002740 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002741 FlagWithInput("-i ", inputJar).
2742 FlagWithOutput("-o ", temporary).
2743 FlagWithArg("-0 ", "'classes*.dex'")
2744
2745 // use zipalign to align uncompressed classes*.dex files
2746 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002747 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002748 Flag("-f").
2749 Text("4").
2750 Input(temporary).
2751 Output(dexOutputFile)
2752
2753 rule.DeleteTemporaryFiles()
2754
2755 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2756 } else {
2757 ctx.Build(pctx, android.BuildParams{
2758 Rule: android.Cp,
2759 Input: inputJar,
2760 Output: dexOutputFile,
2761 })
2762 }
2763
2764 j.dexJarFile = dexOutputFile
2765
2766 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2767
2768 j.maybeStrippedDexJarFile = dexOutputFile
2769
Jiyong Parkafd3d112020-06-08 19:24:09 +09002770 if j.IsForPlatform() {
2771 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2772 j.Stem()+".jar", dexOutputFile)
2773 }
Colin Cross42be7612019-02-21 18:12:14 -08002774}
2775
2776func (j *DexImport) DexJar() android.Path {
2777 return j.dexJarFile
2778}
2779
2780// dex_import imports a `.jar` file containing classes.dex files.
2781//
2782// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2783// to the device.
2784func DexImportFactory() android.Module {
2785 module := &DexImport{}
2786
2787 module.AddProperties(&module.properties)
2788
2789 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002790 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002791 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002792 return module
2793}
2794
Colin Cross89536d42017-07-07 14:35:50 -07002795//
2796// Defaults
2797//
2798type Defaults struct {
2799 android.ModuleBase
2800 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002801 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002802}
2803
Colin Cross1b16b0e2019-02-12 14:41:32 -08002804// java_defaults provides a set of properties that can be inherited by other java or android modules.
2805//
2806// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2807// property in the defaults module that exists in the depending module will be prepended to the depending module's
2808// value for that property.
2809//
2810// Example:
2811//
2812// java_defaults {
2813// name: "example_defaults",
2814// srcs: ["common/**/*.java"],
2815// javacflags: ["-Xlint:all"],
2816// aaptflags: ["--auto-add-overlay"],
2817// }
2818//
2819// java_library {
2820// name: "example",
2821// defaults: ["example_defaults"],
2822// srcs: ["example/**/*.java"],
2823// }
2824//
2825// is functionally identical to:
2826//
2827// java_library {
2828// name: "example",
2829// srcs: [
2830// "common/**/*.java",
2831// "example/**/*.java",
2832// ],
2833// javacflags: ["-Xlint:all"],
2834// }
Colin Cross89536d42017-07-07 14:35:50 -07002835func defaultsFactory() android.Module {
2836 return DefaultsFactory()
2837}
2838
Paul Duffin47357662019-12-05 14:07:14 +00002839func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002840 module := &Defaults{}
2841
Colin Cross89536d42017-07-07 14:35:50 -07002842 module.AddProperties(
2843 &CompilerProperties{},
2844 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002845 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002846 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002847 &aaptProperties{},
2848 &androidLibraryProperties{},
2849 &appProperties{},
2850 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002851 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002852 &ImportProperties{},
2853 &AARImportProperties{},
2854 &sdkLibraryProperties{},
Paul Duffin1a724e62020-05-08 13:44:43 +01002855 &commonToSdkLibraryAndImportProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002856 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002857 &android.ApexProperties{},
Jaewoong Jung062ed7e2020-04-26 15:10:51 -07002858 &RuntimeResourceOverlayProperties{},
Colin Cross1e28e3c2020-06-02 20:09:13 -07002859 &LintProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002860 )
2861
2862 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002863 return module
2864}
Nan Zhangea568a42017-11-08 21:20:04 -08002865
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002866func kytheExtractJavaFactory() android.Singleton {
2867 return &kytheExtractJavaSingleton{}
2868}
2869
2870type kytheExtractJavaSingleton struct {
2871}
2872
2873func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2874 var xrefTargets android.Paths
2875 ctx.VisitAllModules(func(module android.Module) {
2876 if javaModule, ok := module.(xref); ok {
2877 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2878 }
2879 })
2880 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2881 if len(xrefTargets) > 0 {
Colin Cross98552072020-06-04 13:25:17 -07002882 ctx.Phony("xref_java", xrefTargets...)
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002883 }
2884}
2885
Nan Zhangea568a42017-11-08 21:20:04 -08002886var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002887var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002888var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002889var inList = android.InList