blob: 61974f5aff48cdc28d13aa02965945e07f7dd047 [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 Duffinf5c0a9c2020-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 Satayev2db1c3f2020-04-08 19:09:30 +010089func (j *Module) checkSdkVersions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090090 if j.SocSpecific() || j.DeviceSpecific() ||
91 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
92 if sc, ok := ctx.Module().(sdkContext); ok {
Jiyong Park6a927c42020-01-21 02:03:43 +090093 if !sc.sdkVersion().specified() {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090094 ctx.PropertyErrorf("sdk_version",
95 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
96 }
97 }
98 }
Artur Satayev2db1c3f2020-04-08 19:09:30 +010099
100 ctx.VisitDirectDeps(func(module android.Module) {
101 tag := ctx.OtherModuleDependencyTag(module)
102 switch module.(type) {
103 // TODO(satayev): cover other types as well, e.g. imports
104 case *Library, *AndroidLibrary:
105 switch tag {
106 case bootClasspathTag, libTag, staticLibTag, java9LibTag:
107 checkLinkType(ctx, j, module.(linkTypeContext), tag.(dependencyTag))
108 }
109 }
110 })
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900111}
112
Jeongik Cha538c0d02019-07-11 15:54:27 +0900113func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
114 if sc, ok := ctx.Module().(sdkContext); ok {
115 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
Jiyong Park6a927c42020-01-21 02:03:43 +0900116 sdkVersionSpecified := sc.sdkVersion().specified()
117 if usePlatformAPI && sdkVersionSpecified {
118 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
119 } else if !usePlatformAPI && !sdkVersionSpecified {
120 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
Jeongik Cha538c0d02019-07-11 15:54:27 +0900121 }
122
123 }
124}
125
Colin Cross2fe66872015-03-30 17:20:39 -0700126// TODO:
127// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700128// Renderscript
129// Post-jar passes:
130// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700131// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700132// DroidDoc
133// Findbugs
134
Colin Cross89536d42017-07-07 14:35:50 -0700135type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700136 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
137 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800138 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700139
140 // list of source files that should not be used to build the Java module.
141 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800142 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700143
144 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700145 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700146
Colin Cross86a63ff2017-09-27 17:33:10 -0700147 // list of directories that should be excluded from java_resource_dirs
148 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700149
Colin Cross0f37af02017-09-27 17:42:05 -0700150 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800151 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700152
Colin Crosscedd4762018-09-13 11:26:19 -0700153 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800154 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700155
Colin Cross7d5136f2015-05-11 13:39:40 -0700156 // list of module-specific flags that will be used for javac compiles
157 Javacflags []string `android:"arch_variant"`
158
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200159 // list of module-specific flags that will be used for kotlinc compiles
160 Kotlincflags []string `android:"arch_variant"`
161
Colin Cross7d5136f2015-05-11 13:39:40 -0700162 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700163 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700164
165 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700166 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700167
168 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800169 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700170
Colin Cross540eff82017-06-22 17:01:52 -0700171 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800172 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700173
174 // If not blank, set the java version passed to javac as -source and -target
175 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700176
Colin Cross9ae1b922018-06-26 17:59:05 -0700177 // If set to true, allow this module to be dexed and installed on devices. Has no
178 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700179 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700180
Colin Cross0f37af02017-09-27 17:42:05 -0700181 // If set to true, include sources used to compile the module in to the final jar
182 Include_srcs *bool
183
Vladimir Marko0975ee02019-04-02 10:29:55 +0100184 // If not empty, classes are restricted to the specified packages and their sub-packages.
185 // This restriction is checked after applying jarjar rules and including static libs.
186 Permitted_packages []string
187
Colin Crossbe9cdb82019-01-21 21:37:16 -0800188 // List of modules to use as annotation processors
189 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700190
Artur Satayev9cf46692019-11-26 18:08:34 +0000191 // List of modules to export to libraries that directly depend on this library as annotation processors
192 Exported_plugins []string
193
Nan Zhang61eaedb2017-11-02 13:28:15 -0700194 // The number of Java source entries each Javac instance can process
195 Javac_shard_size *int64
196
Nan Zhang5f8cb422018-02-06 10:34:32 -0800197 // Add host jdk tools.jar to bootclasspath
198 Use_tools_jar *bool
199
Colin Cross1369cdb2017-09-29 17:58:17 -0700200 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700201 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800202 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700203
Colin Cross6cef4812019-10-17 14:23:50 -0700204 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700205 Javacflags []string
206 }
Colin Crosscb933592017-11-22 13:49:43 -0800207
Colin Cross81440082018-08-15 20:21:55 -0700208 // When compiling language level 9+ .java code in packages that are part of
209 // a system module, patch_module names the module that your sources and
210 // dependencies should be patched into. The Android runtime currently
211 // doesn't implement the JEP 261 module system so this option is only
212 // supported at compile time. It should only be needed to compile tests in
213 // packages that exist in libcore and which are inconvenient to move
214 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100215 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700216
Colin Crosscb933592017-11-22 13:49:43 -0800217 Jacoco struct {
218 // List of classes to include for instrumentation with jacoco to collect coverage
219 // information at runtime when building with coverage enabled. If unset defaults to all
220 // classes.
221 // Supports '*' as the last character of an entry in the list as a wildcard match.
222 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
223 // it matches classes in the package that have the class name as a prefix.
224 Include_filter []string
225
226 // List of classes to exclude from instrumentation with jacoco to collect coverage
227 // information at runtime when building with coverage enabled. Overrides classes selected
228 // by the include_filter property.
229 // Supports '*' as the last character of an entry in the list as a wildcard match.
230 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
231 // it matches classes in the package that have the class name as a prefix.
232 Exclude_filter []string
233 }
234
Andreas Gampef3e5b552018-01-22 21:27:21 -0800235 Errorprone struct {
236 // List of javac flags that should only be used when running errorprone.
237 Javacflags []string
238 }
239
Colin Cross0f2ee152017-12-14 15:22:43 -0800240 Proto struct {
241 // List of extra options that will be passed to the proto generator.
242 Output_params []string
243 }
244
Colin Crosscb933592017-11-22 13:49:43 -0800245 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800246
247 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800248 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700249}
250
Colin Cross89536d42017-07-07 14:35:50 -0700251type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700252 // list of module-specific flags that will be used for dex compiles
253 Dxflags []string `android:"arch_variant"`
254
Jeongik Cha538c0d02019-07-11 15:54:27 +0900255 // if not blank, set to the version of the sdk to compile against.
256 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800257 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700258
Colin Cross83bb3162018-06-25 15:48:06 -0700259 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
260 // Defaults to sdk_version if not set.
261 Min_sdk_version *string
262
Dan Willemsen419290a2018-10-31 15:28:47 -0700263 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
264 // Defaults to sdk_version if not set.
265 Target_sdk_version *string
266
Jeongik Cha356dac42019-08-19 14:09:52 +0900267 // Whether to compile against the platform APIs instead of an SDK.
268 // If true, then sdk_version must be empty. The value of this field
269 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700270 Platform_apis *bool
271
Colin Crossebe1a512017-11-14 13:12:14 -0800272 Aidl struct {
273 // Top level directories to pass to aidl tool
274 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700275
Colin Crossebe1a512017-11-14 13:12:14 -0800276 // Directories rooted at the Android.bp file to pass to aidl tool
277 Local_include_dirs []string
278
279 // directories that should be added as include directories for any aidl sources of modules
280 // that depend on this module, as well as to aidl for this module.
281 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100282
283 // whether to generate traces (for systrace) for this interface
284 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100285
286 // whether to generate Binder#GetTransaction name method.
287 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800288 }
Colin Cross92430102017-10-09 14:59:32 -0700289
290 // If true, export a copy of the module as a -hostdex module for host testing.
291 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700292
Colin Cross7f87f4f2019-04-24 13:41:45 -0700293 Target struct {
294 Hostdex struct {
295 // Additional required dependencies to add to -hostdex modules.
296 Required []string
297 }
298 }
299
David Brazdil17ef5632018-06-27 10:27:45 +0100300 // If set to true, compile dex regardless of installable. Defaults to false.
301 Compile_dex *bool
302
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700304 // If false, disable all optimization. Defaults to true for android_app and android_test
305 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800306 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700307 // True if the module containing this has it set by default.
308 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800309
310 // If true, optimize for size by removing unused code. Defaults to true for apps,
311 // false for libraries and tests.
312 Shrink *bool
313
314 // If true, optimize bytecode. Defaults to false.
315 Optimize *bool
316
317 // If true, obfuscate bytecode. Defaults to false.
318 Obfuscate *bool
319
320 // If true, do not use the flag files generated by aapt that automatically keep
321 // classes referenced by the app manifest. Defaults to false.
322 No_aapt_flags *bool
323
324 // Flags to pass to proguard.
325 Proguard_flags []string
326
327 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800328 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800329 }
330
Paul Duffine25c6442019-10-11 13:50:28 +0100331 // When targeting 1.9 and above, override the modules to use with --system,
332 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700333 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700334
Jiyong Park4c4c0242019-10-21 14:53:15 +0900335 // set the name of the output
336 Stem *string
337
Colin Cross5a0dcd52018-10-05 14:20:06 -0700338 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800339 IsSDKLibrary bool `blueprint:"mutated"`
Songchun Fan17d69e32020-03-24 20:32:24 -0700340
341 // If true, generate the signature file of APK Signing Scheme V4, along side the signed APK file.
342 // Defaults to false.
343 V4_signature *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700344}
345
Sasha Smundak2057f822019-04-16 17:16:58 -0700346func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
347 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
348}
349
Colin Cross46c9b8b2017-06-22 16:51:17 -0700350// Module contains the properties and members used by all java module types
351type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700352 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700353 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900354 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900355 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700356
Colin Cross89536d42017-07-07 14:35:50 -0700357 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700358 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700359 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700360
Colin Cross331a1212018-08-15 20:40:52 -0700361 // jar file containing header classes including static library dependencies, suitable for
362 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700363 headerJarFile android.Path
364
Colin Cross331a1212018-08-15 20:40:52 -0700365 // jar file containing implementation classes including static library dependencies but no
366 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700367 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700368
Colin Cross331a1212018-08-15 20:40:52 -0700369 // jar file containing only resources including from static library dependencies
370 resourceJar android.Path
371
Colin Cross0c4ce212019-05-03 15:28:19 -0700372 // args and dependencies to package source files into a srcjar
373 srcJarArgs []string
374 srcJarDeps android.Paths
375
Colin Cross331a1212018-08-15 20:40:52 -0700376 // jar file containing implementation classes and resources including static library
377 // dependencies
378 implementationAndResourcesJar android.Path
379
380 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700381 dexJarFile android.Path
382
Colin Cross43f08db2018-11-12 10:13:39 -0800383 // output file that contains classes.dex if it should be in the output file
384 maybeStrippedDexJarFile android.Path
385
Colin Crosscb933592017-11-22 13:49:43 -0800386 // output file containing uninstrumented classes that will be instrumented by jacoco
387 jacocoReportClassesFile android.Path
388
Colin Cross66dbc0b2017-12-28 12:23:20 -0800389 // output file containing mapping of obfuscated names
390 proguardDictionary android.Path
391
Colin Cross331a1212018-08-15 20:40:52 -0700392 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700393 outputFile android.Path
394 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700395
Colin Cross635c3b02016-05-18 15:37:25 -0700396 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700397
Colin Cross635c3b02016-05-18 15:37:25 -0700398 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700399
Colin Cross2fe66872015-03-30 17:20:39 -0700400 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700401 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800402
403 // list of .java files and srcjars that was passed to javac
404 compiledJavaSrcs android.Paths
405 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800406
407 // list of extra progurad flag files
408 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900409
Colin Cross094054a2018-10-17 15:10:48 -0700410 // manifest file to use instead of properties.Manifest
411 overrideManifest android.OptionalPath
412
Artur Satayev9cf46692019-11-26 18:08:34 +0000413 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900414 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700415
Artur Satayev9cf46692019-11-26 18:08:34 +0000416 // list of plugins that this java module is exporting
417 exportedPluginJars android.Paths
418
419 // list of plugins that this java module is exporting
420 exportedPluginClasses []string
421
422 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800423 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700424 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800425
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800426 // expanded Jarjar_rules
427 expandJarjarRules android.Path
428
Vladimir Marko0975ee02019-04-02 10:29:55 +0100429 // list of additional targets for checkbuild
430 additionalCheckedModules android.Paths
431
Colin Cross988708c2019-05-06 14:04:11 -0700432 // Extra files generated by the module type to be added as java resources.
433 extraResources android.Paths
434
Colin Crossf24a22a2019-01-31 14:12:44 -0800435 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800436 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800437
438 // list of the xref extraction files
439 kytheFiles android.Paths
Anton Hansson78156ef2020-03-27 19:39:48 +0000440
441 distFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700442}
443
Colin Cross41955e82019-05-29 14:40:35 -0700444func (j *Module) OutputFiles(tag string) (android.Paths, error) {
445 switch tag {
446 case "":
447 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700448 case ".jar":
449 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700450 case ".proguard_map":
451 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700452 default:
453 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
454 }
Colin Cross54250902017-12-05 09:28:08 -0800455}
456
Colin Cross41955e82019-05-29 14:40:35 -0700457var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800458
Colin Crossf506d872017-07-19 15:53:04 -0700459type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700460 HeaderJars() android.Paths
461 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700462 ResourceJars() android.Paths
463 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800464 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700465 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900466 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000467 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700468 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700469 BaseModuleName() string
Jiyong Park618922e2020-01-08 13:35:43 +0900470 JacocoReportClassesFile() android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700471}
472
Jiyong Parkc678ad32018-04-10 13:07:10 +0900473type SdkLibraryDependency interface {
Jiyong Park6a927c42020-01-21 02:03:43 +0900474 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
475 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion sdkSpec) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900476}
477
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800478type xref interface {
479 XrefJavaFiles() android.Paths
480}
481
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800482func (j *Module) XrefJavaFiles() android.Paths {
483 return j.kytheFiles
484}
485
Colin Cross89536d42017-07-07 14:35:50 -0700486func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
487 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
488 android.InitDefaultableModule(module)
489}
490
Colin Crossbe1da472017-07-07 15:59:46 -0700491type dependencyTag struct {
492 blueprint.BaseDependencyTag
493 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700494}
495
Colin Crossa4f08812018-10-02 22:03:40 -0700496type jniDependencyTag struct {
497 blueprint.BaseDependencyTag
Colin Crossa4f08812018-10-02 22:03:40 -0700498}
499
Jiyong Park8be103b2019-11-08 15:53:48 +0900500func IsJniDepTag(depTag blueprint.DependencyTag) bool {
501 _, ok := depTag.(*jniDependencyTag)
502 return ok
503}
504
Colin Crossbe1da472017-07-07 15:59:46 -0700505var (
Colin Cross4b964c02018-10-15 16:18:06 -0700506 staticLibTag = dependencyTag{name: "staticlib"}
507 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700508 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800509 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000510 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700511 bootClasspathTag = dependencyTag{name: "bootclasspath"}
512 systemModulesTag = dependencyTag{name: "system modules"}
513 frameworkResTag = dependencyTag{name: "framework-res"}
514 frameworkApkTag = dependencyTag{name: "framework-apk"}
515 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800516 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700517 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
518 certificateTag = dependencyTag{name: "certificate"}
519 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700520 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700521)
Colin Cross2fe66872015-03-30 17:20:39 -0700522
Jiyong Park83dc74b2020-01-14 18:38:44 +0900523func IsLibDepTag(depTag blueprint.DependencyTag) bool {
524 return depTag == libTag
525}
526
527func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool {
528 return depTag == staticLibTag
529}
530
Colin Crossfc3674a2017-09-18 17:41:52 -0700531type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700532 useModule, useFiles, useDefaultLibs, invalidVersion bool
533
Colin Cross6cef4812019-10-17 14:23:50 -0700534 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
535 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100536
537 // The default system modules to use. Will be an empty string if no system
538 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700539 systemModules string
540
Colin Cross6cef4812019-10-17 14:23:50 -0700541 // The modules that will be added ot the classpath when targeting 1.9 or higher
542 java9Classpath []string
543
Colin Crossa97c5d32018-03-28 14:58:31 -0700544 frameworkResModule string
545
Colin Cross86a60ae2018-05-29 14:44:55 -0700546 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700547 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100548
549 noStandardLibs, noFrameworksLibs bool
550}
551
552func (s sdkDep) hasStandardLibs() bool {
553 return !s.noStandardLibs
554}
555
556func (s sdkDep) hasFrameworkLibs() bool {
557 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700558}
559
Colin Crossa4f08812018-10-02 22:03:40 -0700560type jniLib struct {
Jaewoong Jung87a33e72020-03-26 14:01:48 -0700561 name string
562 path android.Path
563 target android.Target
564 coverageFile android.OptionalPath
Colin Crossa4f08812018-10-02 22:03:40 -0700565}
566
Colin Cross0ea8ba82019-06-06 14:33:29 -0700567func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800568 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
569}
570
Colin Cross0ea8ba82019-06-06 14:33:29 -0700571func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800572 return j.shouldInstrument(ctx) &&
573 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
574 ctx.Config().UnbundledBuild())
575}
576
Jiyong Park6a927c42020-01-21 02:03:43 +0900577func (j *Module) sdkVersion() sdkSpec {
578 return sdkSpecFrom(String(j.deviceProperties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -0700579}
580
Paul Duffine25c6442019-10-11 13:50:28 +0100581func (j *Module) systemModules() string {
582 return proptools.String(j.deviceProperties.System_modules)
583}
584
Jiyong Park6a927c42020-01-21 02:03:43 +0900585func (j *Module) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -0700586 if j.deviceProperties.Min_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900587 return sdkSpecFrom(*j.deviceProperties.Min_sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700588 }
589 return j.sdkVersion()
590}
591
Jiyong Park6a927c42020-01-21 02:03:43 +0900592func (j *Module) targetSdkVersion() sdkSpec {
Dan Willemsen419290a2018-10-31 15:28:47 -0700593 if j.deviceProperties.Target_sdk_version != nil {
Jiyong Park6a927c42020-01-21 02:03:43 +0900594 return sdkSpecFrom(*j.deviceProperties.Target_sdk_version)
Dan Willemsen419290a2018-10-31 15:28:47 -0700595 }
596 return j.sdkVersion()
597}
598
Jiyong Parkb02bb402019-12-03 00:43:57 +0900599func (j *Module) AvailableFor(what string) bool {
600 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
601 // Exception: for hostdex: true libraries, the platform variant is created
602 // even if it's not marked as available to platform. In that case, the platform
603 // variant is used only for the hostdex and not installed to the device.
604 return true
605 }
606 return j.ApexModuleBase.AvailableFor(what)
607}
608
Colin Crossbe1da472017-07-07 15:59:46 -0700609func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700610 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100611 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700612 if sdkDep.useDefaultLibs {
613 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
614 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
615 if sdkDep.hasFrameworkLibs() {
616 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700617 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700618 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700619 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100620 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700621 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700622 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
623 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
624 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
625 }
Colin Cross2fe66872015-03-30 17:20:39 -0700626 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700627
Nan Zhangb2b33de2018-02-23 11:18:47 -0800628 if ctx.ModuleName() == "android_stubs_current" ||
629 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700630 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700631 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800632 }
Colin Cross2fe66872015-03-30 17:20:39 -0700633 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700634
Inseob Kimac1e9862019-12-09 18:15:47 +0900635 syspropPublicStubs := syspropPublicStubs(ctx.Config())
636
637 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
638 // and redirects dependency to public stub depending on the link type.
639 rewriteSyspropLibs := func(libs []string, prop string) []string {
640 // make a copy
641 ret := android.CopyOf(libs)
642
643 for idx, lib := range libs {
644 stub, ok := syspropPublicStubs[lib]
645
646 if !ok {
647 continue
648 }
649
650 linkType, _ := j.getLinkType(ctx.ModuleName())
Inseob Kimc5239512020-01-14 15:36:21 +0900651 // only platform modules can use internal props
652 if linkType != javaPlatform {
Inseob Kimac1e9862019-12-09 18:15:47 +0900653 ret[idx] = stub
Inseob Kimac1e9862019-12-09 18:15:47 +0900654 }
655 }
656
657 return ret
658 }
659
660 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
661 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700662
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700663 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000664 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800665
Colin Crossfe17f6f2019-03-28 19:30:56 -0700666 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700667 if j.hasSrcExt(".proto") {
668 protoDeps(ctx, &j.protoProperties)
669 }
Colin Cross93e85952017-08-15 13:34:18 -0700670
671 if j.hasSrcExt(".kt") {
672 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
673 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700674 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
675 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800676 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800677 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
678 }
Colin Cross93e85952017-08-15 13:34:18 -0700679 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800680
Ulya Trafimovich38dfa0f2020-01-07 16:37:02 +0000681 // Framework libraries need special handling in static coverage builds: they should not have
682 // static dependency on jacoco, otherwise there would be multiple conflicting definitions of
683 // the same jacoco classes coming from different bootclasspath jars.
684 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
685 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
686 j.properties.Instrument = true
687 }
688 } else if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700689 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800690 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700691}
692
693func hasSrcExt(srcs []string, ext string) bool {
694 for _, src := range srcs {
695 if filepath.Ext(src) == ext {
696 return true
697 }
698 }
699
700 return false
701}
702
703func (j *Module) hasSrcExt(ext string) bool {
704 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700705}
706
Colin Cross46c9b8b2017-06-22 16:51:17 -0700707func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700708 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700709
Colin Crossebe1a512017-11-14 13:12:14 -0800710 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
711 aidlIncludes = append(aidlIncludes,
712 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
713 aidlIncludes = append(aidlIncludes,
714 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700715
Colin Cross3047fa22019-04-18 10:56:44 -0700716 var flags []string
717 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700718
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700719 if aidlPreprocess.Valid() {
720 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700721 deps = append(deps, aidlPreprocess.Path())
722 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700723 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700724 }
725
Colin Cross3047fa22019-04-18 10:56:44 -0700726 if len(j.exportAidlIncludeDirs) > 0 {
727 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
728 }
729
730 if len(aidlIncludes) > 0 {
731 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
732 }
733
Colin Cross635c3b02016-05-18 15:37:25 -0700734 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800735 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700736 flags = append(flags, "-I"+src.String())
737 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700738
Martijn Coeneneab15642018-03-09 09:29:59 +0100739 if Bool(j.deviceProperties.Aidl.Generate_traces) {
740 flags = append(flags, "-t")
741 }
742
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100743 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
744 flags = append(flags, "--transaction_names")
745 }
746
Colin Cross3047fa22019-04-18 10:56:44 -0700747 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700748}
749
Colin Cross32f676a2017-09-06 13:41:06 -0700750type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800751 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700752 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800753 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700754 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800755 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700756 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700757 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700758 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700759 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800760 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700761 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700762 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700763 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700764 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800765 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800766
767 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700768}
Colin Cross2fe66872015-03-30 17:20:39 -0700769
Colin Cross54250902017-12-05 09:28:08 -0800770func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
771 for _, f := range dep.Srcs() {
772 if f.Ext() != ".jar" {
773 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
774 ctx.OtherModuleName(dep.(blueprint.Module)))
775 }
776 }
777}
778
Jiyong Park2d492942018-03-05 17:44:10 +0900779type linkType int
780
781const (
Jiyong Park50146e92020-01-30 18:00:15 +0900782 // TODO(jiyong) rename these for better readability. Make the allowed
783 // and disallowed link types explicit
Jiyong Park2d492942018-03-05 17:44:10 +0900784 javaCore linkType = iota
785 javaSdk
786 javaSystem
Jiyong Park50146e92020-01-30 18:00:15 +0900787 javaModule
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900788 javaSystemServer
Jiyong Park2d492942018-03-05 17:44:10 +0900789 javaPlatform
790)
791
Jeongik Cha75b83b02019-11-01 15:28:00 +0900792type linkTypeContext interface {
793 android.Module
794 getLinkType(name string) (ret linkType, stubs bool)
795}
796
797func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700798 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700799 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900800 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
801 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100802 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900803 return javaCore, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900804 case ver.kind == sdkCore:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900805 return javaCore, false
806 case name == "android_system_stubs_current":
807 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900808 case ver.kind == sdkSystem:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900809 return javaSystem, false
810 case name == "android_test_stubs_current":
811 return javaSystem, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900812 case ver.kind == sdkTest:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900813 return javaPlatform, false
814 case name == "android_stubs_current":
815 return javaSdk, true
Jiyong Park6a927c42020-01-21 02:03:43 +0900816 case ver.kind == sdkPublic:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900817 return javaSdk, false
Jiyong Park50146e92020-01-30 18:00:15 +0900818 case name == "android_module_lib_stubs_current":
819 return javaModule, true
820 case ver.kind == sdkModule:
821 return javaModule, false
Anton Hanssonba6ab2e2020-03-19 15:23:38 +0000822 case name == "android_system_server_stubs_current":
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900823 return javaSystemServer, true
824 case ver.kind == sdkSystemServer:
825 return javaSystemServer, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900826 case ver.kind == sdkPrivate || ver.kind == sdkNone || ver.kind == sdkCorePlatform:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900827 return javaPlatform, false
Jiyong Park6a927c42020-01-21 02:03:43 +0900828 case !ver.valid():
829 panic(fmt.Errorf("sdk_version is invalid. got %q", ver.raw))
Colin Crossf19b9bb2018-03-26 14:42:44 -0700830 default:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900831 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900832 }
833}
834
Jeongik Cha75b83b02019-11-01 15:28:00 +0900835func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700836 if ctx.Host() {
837 return
838 }
839
Jeongik Cha75b83b02019-11-01 15:28:00 +0900840 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900841 if stubs {
842 return
843 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900844 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900845 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."
846
847 switch myLinkType {
848 case javaCore:
849 if otherLinkType != javaCore {
850 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 +0900851 ctx.OtherModuleName(to))
852 }
Jiyong Park2d492942018-03-05 17:44:10 +0900853 break
854 case javaSdk:
855 if otherLinkType != javaCore && otherLinkType != javaSdk {
856 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
857 ctx.OtherModuleName(to))
858 }
859 break
860 case javaSystem:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900861 if otherLinkType == javaPlatform || otherLinkType == javaModule || otherLinkType == javaSystemServer {
Jiyong Park2d492942018-03-05 17:44:10 +0900862 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
863 ctx.OtherModuleName(to))
864 }
865 break
Jiyong Park50146e92020-01-30 18:00:15 +0900866 case javaModule:
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900867 if otherLinkType == javaPlatform || otherLinkType == javaSystemServer {
Jiyong Park50146e92020-01-30 18:00:15 +0900868 ctx.ModuleErrorf("compiles against module API, but dependency %q is compiling against private API."+commonMessage,
869 ctx.OtherModuleName(to))
870 }
871 break
Jiyong Parkaae9bd12020-02-12 04:36:43 +0900872 case javaSystemServer:
873 if otherLinkType == javaPlatform {
874 ctx.ModuleErrorf("compiles against system server API, but dependency %q is compiling against private API."+commonMessage,
875 ctx.OtherModuleName(to))
876 }
877 break
Jiyong Park2d492942018-03-05 17:44:10 +0900878 case javaPlatform:
879 // no restriction on link-type
880 break
Jiyong Park750e5572018-01-31 00:20:13 +0900881 }
882}
883
Colin Cross32f676a2017-09-06 13:41:06 -0700884func (j *Module) collectDeps(ctx android.ModuleContext) deps {
885 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700886
Colin Cross300f0382018-03-06 13:11:51 -0800887 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700888 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800889 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700890 ctx.AddMissingDependencies(sdkDep.bootclasspath)
891 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800892 } else if sdkDep.useFiles {
893 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700894 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700895 deps.aidlPreprocess = sdkDep.aidl
896 } else {
897 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800898 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700899 }
900
Colin Crossd11fcda2017-10-23 17:59:01 -0700901 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700902 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700903 tag := ctx.OtherModuleDependencyTag(module)
904
Colin Crossa4f08812018-10-02 22:03:40 -0700905 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700906 // Handled by AndroidApp.collectAppDeps
907 return
908 }
909 if tag == certificateTag {
910 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700911 return
912 }
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100913
Colin Cross54250902017-12-05 09:28:08 -0800914 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800915 case SdkLibraryDependency:
916 switch tag {
917 case libTag:
918 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
919 // names of sdk libs that are directly depended are exported
920 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700921 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800922 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
923 }
Colin Cross54250902017-12-05 09:28:08 -0800924 case Dependency:
925 switch tag {
926 case bootClasspathTag:
927 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700928 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800929 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900930 // sdk lib names from dependencies are re-exported
931 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700932 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000933 pluginJars, pluginClasses := dep.ExportedPlugins()
934 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700935 case java9LibTag:
936 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800937 case staticLibTag:
938 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
939 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
940 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700941 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900942 // sdk lib names from dependencies are re-exported
943 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700944 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000945 pluginJars, pluginClasses := dep.ExportedPlugins()
946 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800947 case pluginTag:
948 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800949 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000950 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
951 } else {
952 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800953 }
954 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
955 } else {
956 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
957 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000958 case exportedPluginTag:
959 if plugin, ok := dep.(*Plugin); ok {
960 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
961 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
962 }
963 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
964 if plugin.pluginProperties.Processor_class != nil {
965 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
966 }
967 } else {
968 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
969 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800970 case frameworkApkTag:
971 if ctx.ModuleName() == "android_stubs_current" ||
972 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700973 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800974 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
975 // resource files out of there for aapt.
976 //
977 // Normally the package rule runs aapt, which includes the resource,
978 // but we're not running that in our package rule so just copy in the
979 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700980 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800981 }
Colin Cross54250902017-12-05 09:28:08 -0800982 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700983 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800984 case kotlinAnnotationsTag:
985 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800986 }
987
Colin Cross54250902017-12-05 09:28:08 -0800988 case android.SourceFileProducer:
989 switch tag {
990 case libTag:
991 checkProducesJars(ctx, dep)
992 deps.classpath = append(deps.classpath, dep.Srcs()...)
993 case staticLibTag:
994 checkProducesJars(ctx, dep)
995 deps.classpath = append(deps.classpath, dep.Srcs()...)
996 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
997 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800998 }
999 default:
Colin Crossec7a0422017-07-07 14:47:12 -07001000 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001001 case bootClasspathTag:
1002 // If a system modules dependency has been added to the bootclasspath
1003 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +00001004 sm := module.(SystemModulesProvider)
1005 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +01001006
Colin Cross1369cdb2017-09-29 17:58:17 -07001007 case systemModulesTag:
1008 if deps.systemModules != nil {
1009 panic("Found two system module dependencies")
1010 }
Paul Duffin83a2d962019-11-19 19:44:10 +00001011 sm := module.(SystemModulesProvider)
1012 outputDir, outputDeps := sm.OutputDirAndDeps()
1013 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001014 }
Colin Crossec7a0422017-07-07 14:47:12 -07001015 }
Colin Cross2fe66872015-03-30 17:20:39 -07001016 })
1017
Jiyong Park1be96912018-05-28 18:02:19 +09001018 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1019
Colin Cross32f676a2017-09-06 13:41:06 -07001020 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001021}
1022
Artur Satayev9cf46692019-11-26 18:08:34 +00001023func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1024 deps.processorPath = append(deps.processorPath, pluginJars...)
1025 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1026}
1027
Colin Cross1e743852019-10-28 11:37:20 -07001028func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001029 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001030 if err != nil {
1031 ctx.PropertyErrorf("sdk_version", "%s", err)
1032 }
Nan Zhang357466b2018-04-17 17:38:36 -07001033 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001034 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001035 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001036 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001037 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001038 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001039 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1040 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001041 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001042 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001043 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001044 }
Nan Zhang357466b2018-04-17 17:38:36 -07001045}
1046
Colin Cross1e743852019-10-28 11:37:20 -07001047type javaVersion int
1048
1049const (
1050 JAVA_VERSION_UNSUPPORTED = 0
1051 JAVA_VERSION_6 = 6
1052 JAVA_VERSION_7 = 7
1053 JAVA_VERSION_8 = 8
1054 JAVA_VERSION_9 = 9
1055)
1056
1057func (v javaVersion) String() string {
1058 switch v {
1059 case JAVA_VERSION_6:
1060 return "1.6"
1061 case JAVA_VERSION_7:
1062 return "1.7"
1063 case JAVA_VERSION_8:
1064 return "1.8"
1065 case JAVA_VERSION_9:
1066 return "1.9"
1067 default:
1068 return "unsupported"
1069 }
1070}
1071
1072// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1073func (v javaVersion) usesJavaModules() bool {
1074 return v >= 9
1075}
1076
1077func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001078 switch javaVersion {
1079 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001080 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001081 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001082 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001083 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001084 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001085 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001086 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001087 case "10", "11":
1088 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001089 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001090 default:
1091 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001092 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001093 }
1094}
1095
Nan Zhanged19fc32017-10-19 13:06:22 -07001096func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001097
Colin Crossf03c82b2015-04-13 13:53:40 -07001098 var flags javaBuilderFlags
1099
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001100 // javaVersion flag.
1101 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1102
Nan Zhanged19fc32017-10-19 13:06:22 -07001103 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001104 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001105 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001106 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001107 }
Colin Cross6510f912017-11-29 00:27:14 -08001108 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001109 // Override the -g flag passed globally to remove local variable debug info to reduce
1110 // disk and memory usage.
1111 javacFlags = append(javacFlags, "-g:source,lines")
1112 }
Colin Crossc228a702019-11-06 16:18:05 -08001113 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001114
Colin Cross66548102018-06-19 22:47:35 -07001115 if ctx.Config().RunErrorProne() {
1116 if config.ErrorProneClasspath == nil {
1117 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1118 }
1119
1120 errorProneFlags := []string{
1121 "-Xplugin:ErrorProne",
1122 "${config.ErrorProneChecks}",
1123 }
1124 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1125
1126 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1127 "'" + strings.Join(errorProneFlags, " ") + "'"
1128 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001129 }
1130
Nan Zhanged19fc32017-10-19 13:06:22 -07001131 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001132 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1133 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001134 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001135 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001136
Colin Crossbe9cdb82019-01-21 21:37:16 -08001137 flags.processor = strings.Join(deps.processorClasses, ",")
1138
Colin Cross1e743852019-10-28 11:37:20 -07001139 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1140 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001141 // Give host-side tools a version of OpenJDK's standard libraries
1142 // close to what they're targeting. As of Dec 2017, AOSP is only
1143 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1144 //
1145 // When building with OpenJDK 8, the following should have no
1146 // effect since those jars would be available by default.
1147 //
1148 // When building with OpenJDK 9 but targeting a version < 1.8,
1149 // putting them on the bootclasspath means that:
1150 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1151 // b) references to existing APIs are not reinterpreted in an
1152 // OpenJDK 9-specific way, eg. calls to subclasses of
1153 // java.nio.Buffer as in http://b/70862583
1154 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1155 flags.bootClasspath = append(flags.bootClasspath,
1156 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1157 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001158 if Bool(j.properties.Use_tools_jar) {
1159 flags.bootClasspath = append(flags.bootClasspath,
1160 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1161 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001162 }
1163
Colin Cross1e743852019-10-28 11:37:20 -07001164 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001165 // Manually specify build directory in case it is not under the repo root.
1166 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1167 // just adding a symlink under the root doesn't help.)
1168 patchPaths := ".:" + ctx.Config().BuildDir()
1169 classPath := flags.classpath.FormJavaClassPath("")
1170 if classPath != "" {
1171 patchPaths += ":" + classPath
1172 }
1173 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001174 }
1175
Nan Zhanged19fc32017-10-19 13:06:22 -07001176 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001177 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001178
Nan Zhanged19fc32017-10-19 13:06:22 -07001179 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001180 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001181
Colin Cross81440082018-08-15 20:21:55 -07001182 if len(javacFlags) > 0 {
1183 // optimization.
1184 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1185 flags.javacFlags = "$javacFlags"
1186 }
1187
Nan Zhanged19fc32017-10-19 13:06:22 -07001188 return flags
1189}
Colin Crossc0b06f12015-04-08 13:03:43 -07001190
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001191func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001192 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001193
1194 deps := j.collectDeps(ctx)
1195 flags := j.collectBuilderFlags(ctx, deps)
1196
Colin Cross1e743852019-10-28 11:37:20 -07001197 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001198 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1199 }
Colin Cross8a497952019-03-05 22:25:09 -08001200 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001201 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001202 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001203 }
1204
Colin Crossaf050172017-11-15 23:01:59 -08001205 srcFiles = j.genSources(ctx, srcFiles, flags)
1206
1207 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001208 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001209 if aaptSrcJar != nil {
1210 srcJars = append(srcJars, aaptSrcJar)
1211 }
Colin Crossb7a63242015-04-16 14:09:14 -07001212
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001213 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001214 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001215 }
1216
Colin Cross1ee23172017-10-18 14:44:18 -07001217 jarName := ctx.ModuleName() + ".jar"
1218
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001219 javaSrcFiles := srcFiles.FilterByExt(".java")
1220 var uniqueSrcFiles android.Paths
1221 set := make(map[string]bool)
1222 for _, v := range javaSrcFiles {
1223 if _, found := set[v.String()]; !found {
1224 set[v.String()] = true
1225 uniqueSrcFiles = append(uniqueSrcFiles, v)
1226 }
1227 }
1228
patricktu242faad2019-09-24 15:41:30 +08001229 // Collect .java files for AIDEGen
1230 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1231
Colin Cross55f63ea2018-08-27 12:37:09 -07001232 var kotlinJars android.Paths
1233
Colin Cross93e85952017-08-15 13:34:18 -07001234 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001235 // user defined kotlin flags.
1236 kotlincFlags := j.properties.Kotlincflags
1237 CheckKotlincFlags(ctx, kotlincFlags)
1238
Colin Cross93e85952017-08-15 13:34:18 -07001239 // If there are kotlin files, compile them first but pass all the kotlin and java files
1240 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1241 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001242 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001243 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001244 kotlincFlags = append(kotlincFlags, "-no-jdk")
1245 }
1246 if len(kotlincFlags) > 0 {
1247 // optimization.
1248 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1249 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001250 }
1251
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001252 var kotlinSrcFiles android.Paths
1253 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1254 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1255
patricktu242faad2019-09-24 15:41:30 +08001256 // Collect .kt files for AIDEGen
1257 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1258
Colin Crossafbb1732019-01-17 15:42:52 -08001259 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1260 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1261
1262 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1263 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1264
1265 if len(flags.processorPath) > 0 {
1266 // Use kapt for annotation processing
1267 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1268 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1269 srcJars = append(srcJars, kaptSrcJar)
1270 // Disable annotation processing in javac, it's already been handled by kapt
1271 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001272 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001273 }
Colin Cross93e85952017-08-15 13:34:18 -07001274
Colin Cross1ee23172017-10-18 14:44:18 -07001275 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001276 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001277 if ctx.Failed() {
1278 return
1279 }
1280
1281 // Make javac rule depend on the kotlinc rule
1282 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001283
Colin Cross93e85952017-08-15 13:34:18 -07001284 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001285 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001286 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001287 }
1288
Colin Cross55f63ea2018-08-27 12:37:09 -07001289 jars := append(android.Paths(nil), kotlinJars...)
1290
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001291 // Store the list of .java files that was passed to javac
1292 j.compiledJavaSrcs = uniqueSrcFiles
1293 j.compiledSrcJars = srcJars
1294
Nan Zhang61eaedb2017-11-02 13:28:15 -07001295 enable_sharding := false
Colin Crossf7d84012020-02-21 08:16:41 -08001296 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001297 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001298 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1299 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001300 // Formerly, there was a check here that prevented annotation processors
1301 // from being used when sharding was enabled, as some annotation processors
1302 // do not function correctly in sharded environments. It was removed to
1303 // allow for the use of annotation processors that do function correctly
1304 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001305 }
Colin Crossf7d84012020-02-21 08:16:41 -08001306 headerJarFileWithoutJarjar, j.headerJarFile =
1307 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001308 if ctx.Failed() {
1309 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001310 }
1311 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001312 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001313 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001314 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001315 // If error-prone is enabled, add an additional rule to compile the java files into
1316 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001317 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001318 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1319 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001320 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001321 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001322 extraJarDeps = append(extraJarDeps, errorprone)
1323 }
1324
Nan Zhang61eaedb2017-11-02 13:28:15 -07001325 if enable_sharding {
Colin Crossf7d84012020-02-21 08:16:41 -08001326 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001327 shardSize := int(*(j.properties.Javac_shard_size))
1328 var shardSrcs []android.Paths
1329 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001330 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001331 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001332 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1333 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001334 jars = append(jars, classes)
1335 }
1336 }
1337 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001338 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1339 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001340 jars = append(jars, classes)
1341 }
1342 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001343 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001344 jars = append(jars, classes)
1345 }
Colin Crossd6891432017-09-27 17:39:56 -07001346 if ctx.Failed() {
1347 return
1348 }
Colin Cross2fe66872015-03-30 17:20:39 -07001349 }
1350
Colin Cross0c4ce212019-05-03 15:28:19 -07001351 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1352
1353 var includeSrcJar android.WritablePath
1354 if Bool(j.properties.Include_srcs) {
1355 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1356 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1357 }
1358
Colin Crosscedd4762018-09-13 11:26:19 -07001359 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1360 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001361 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001362 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001363
1364 var resArgs []string
1365 var resDeps android.Paths
1366
1367 resArgs = append(resArgs, dirArgs...)
1368 resDeps = append(resDeps, dirDeps...)
1369
1370 resArgs = append(resArgs, fileArgs...)
1371 resDeps = append(resDeps, fileDeps...)
1372
Colin Cross988708c2019-05-06 14:04:11 -07001373 resArgs = append(resArgs, extraArgs...)
1374 resDeps = append(resDeps, extraDeps...)
1375
Colin Cross40a36712017-09-27 17:41:35 -07001376 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001377 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001378 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001379 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001380 if ctx.Failed() {
1381 return
1382 }
1383 }
1384
Colin Cross0c4ce212019-05-03 15:28:19 -07001385 var resourceJars android.Paths
1386 if j.resourceJar != nil {
1387 resourceJars = append(resourceJars, j.resourceJar)
1388 }
1389 if Bool(j.properties.Include_srcs) {
1390 resourceJars = append(resourceJars, includeSrcJar)
1391 }
1392 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001393
Colin Cross0c4ce212019-05-03 15:28:19 -07001394 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001395 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001396 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001397 false, nil, nil)
1398 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001399 } else if len(resourceJars) == 1 {
1400 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001401 }
1402
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001403 if len(deps.staticJars) > 0 {
1404 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001405 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001406
Colin Cross094054a2018-10-17 15:10:48 -07001407 manifest := j.overrideManifest
1408 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001409 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001410 }
Colin Cross635acc92017-09-12 22:50:46 -07001411
Colin Cross8a497952019-03-05 22:25:09 -08001412 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001413 if len(services) > 0 {
1414 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1415 var zipargs []string
1416 for _, file := range services {
1417 serviceFile := file.String()
1418 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1419 }
1420 ctx.Build(pctx, android.BuildParams{
1421 Rule: zip,
1422 Output: servicesJar,
1423 Implicits: services,
1424 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001425 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001426 },
1427 })
1428 jars = append(jars, servicesJar)
1429 }
1430
Colin Cross0a6e0072017-08-30 14:24:55 -07001431 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001432 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001433 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001434
1435 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001436 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1437 // Optimization: skip the combine step if there is nothing to do
1438 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1439 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1440 // any if len(jars) == 1.
1441 outputFile = moduleOutPath
1442 } else {
1443 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1444 ctx.Build(pctx, android.BuildParams{
1445 Rule: android.Cp,
1446 Input: jars[0],
1447 Output: combinedJar,
1448 })
1449 outputFile = combinedJar
1450 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001451 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001452 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001453 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001454 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001455 outputFile = combinedJar
1456 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001457
Colin Cross331a1212018-08-15 20:40:52 -07001458 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001459 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001460 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001461 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001462 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001463 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001464
1465 // jarjar resource jar if necessary
1466 if j.resourceJar != nil {
1467 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001468 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001469 j.resourceJar = resourceJarJarFile
1470 }
1471
Colin Cross0a6e0072017-08-30 14:24:55 -07001472 if ctx.Failed() {
1473 return
1474 }
1475 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001476
1477 // Check package restrictions if necessary.
1478 if len(j.properties.Permitted_packages) > 0 {
1479 // Check packages and copy to package-checked file.
1480 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1481 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1482 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1483
1484 if ctx.Failed() {
1485 return
1486 }
1487 }
1488
Nan Zhanged19fc32017-10-19 13:06:22 -07001489 j.implementationJarFile = outputFile
1490 if j.headerJarFile == nil {
1491 j.headerJarFile = j.implementationJarFile
1492 }
Colin Cross2fe66872015-03-30 17:20:39 -07001493
Jiyong Park93e57a02020-02-21 16:04:53 +09001494 // Force enable the instrumentation for java code that is built for APEXes ...
1495 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1496 // doesn't make sense)
1497 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1498 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park00cae1c2020-02-18 12:50:44 +00001499 j.properties.Instrument = true
1500 }
1501
Colin Cross3144dfc2018-01-03 15:06:47 -08001502 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001503 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1504 }
1505
Colin Cross331a1212018-08-15 20:40:52 -07001506 // merge implementation jar with resources if necessary
1507 implementationAndResourcesJar := outputFile
1508 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001509 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001510 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001511 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001512 false, nil, nil)
1513 implementationAndResourcesJar = combinedJar
1514 }
1515
1516 j.implementationAndResourcesJar = implementationAndResourcesJar
1517
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001518 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1519 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1520 if j.deviceProperties.Compile_dex == nil {
1521 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1522 }
1523 if j.deviceProperties.Hostdex == nil {
1524 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1525 }
1526 }
1527
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001528 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001529 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001530 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001531 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001532 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001533 if ctx.Failed() {
1534 return
1535 }
Colin Cross331a1212018-08-15 20:40:52 -07001536
Jiyong Park09cb6292019-07-15 15:29:23 +09001537 // Hidden API CSV generation and dex encoding
1538 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1539 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001540
Colin Cross331a1212018-08-15 20:40:52 -07001541 // merge dex jar with resources if necessary
1542 if j.resourceJar != nil {
1543 jars := android.Paths{dexOutputFile, j.resourceJar}
1544 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1545 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1546 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001547 if j.deviceProperties.UncompressDex {
1548 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1549 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1550 dexOutputFile = combinedAlignedJar
1551 } else {
1552 dexOutputFile = combinedJar
1553 }
Colin Cross331a1212018-08-15 20:40:52 -07001554 }
1555
1556 j.dexJarFile = dexOutputFile
1557
Colin Cross8faf8fc2019-01-16 15:15:52 -08001558 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001559 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1560
1561 j.maybeStrippedDexJarFile = dexOutputFile
1562
Colin Cross3063b782018-08-15 11:19:12 -07001563 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001564
1565 if ctx.Failed() {
1566 return
1567 }
Colin Cross331a1212018-08-15 20:40:52 -07001568 } else {
1569 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001570 }
Colin Cross331a1212018-08-15 20:40:52 -07001571
Colin Crossb7a63242015-04-16 14:09:14 -07001572 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001573
1574 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1575 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001576}
1577
Colin Cross3b706fd2019-09-05 16:44:18 -07001578func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1579 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1580
1581 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1582 if idx >= 0 {
1583 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1584 jarName += strconv.Itoa(idx)
1585 }
1586
1587 classes := android.PathForModuleOut(ctx, "javac", jarName)
1588 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1589
1590 if ctx.Config().EmitXrefRules() {
1591 extractionFile := android.PathForModuleOut(ctx, kzipName)
1592 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1593 j.kytheFiles = append(j.kytheFiles, extractionFile)
1594 }
1595
1596 return classes
1597}
1598
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001599// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1600// since some of these flags may be used internally.
1601func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1602 for _, flag := range flags {
1603 flag = strings.TrimSpace(flag)
1604
1605 if !strings.HasPrefix(flag, "-") {
1606 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1607 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1608 ctx.PropertyErrorf("kotlincflags",
1609 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1610 } else if inList(flag, config.KotlincIllegalFlags) {
1611 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1612 } else if flag == "-include-runtime" {
1613 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1614 } else {
1615 args := strings.Split(flag, " ")
1616 if args[0] == "-kotlin-home" {
1617 ctx.PropertyErrorf("kotlincflags",
1618 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1619 }
1620 }
1621 }
1622}
1623
Colin Cross8eadbf02017-10-24 17:46:00 -07001624func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf7d84012020-02-21 08:16:41 -08001625 deps deps, flags javaBuilderFlags, jarName string,
1626 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001627
1628 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001629 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001630 // Compile java sources into turbine.jar.
1631 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1632 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1633 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08001634 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001635 }
1636 jars = append(jars, turbineJar)
1637 }
1638
Colin Cross55f63ea2018-08-27 12:37:09 -07001639 jars = append(jars, extraJars...)
1640
Nan Zhanged19fc32017-10-19 13:06:22 -07001641 // Combine any static header libraries into classes-header.jar. If there is only
1642 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001643 jars = append(jars, deps.staticHeaderJars...)
1644
Colin Cross5c6ecc12017-10-23 18:12:27 -07001645 // we cannot skip the combine step for now if there is only one jar
1646 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1647 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001648 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001649 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001650 headerJar = combinedJar
Colin Crossf7d84012020-02-21 08:16:41 -08001651 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001652
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001653 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001654 // Transform classes.jar into classes-jarjar.jar
1655 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001656 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf7d84012020-02-21 08:16:41 -08001657 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001658 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08001659 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001660 }
1661 }
1662
Colin Crossf7d84012020-02-21 08:16:41 -08001663 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001664}
1665
Colin Crosscb933592017-11-22 13:49:43 -08001666func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001667 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001668
Colin Cross7a3139e2017-12-19 13:57:50 -08001669 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001670
Colin Cross84c38822018-01-03 15:59:46 -08001671 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001672 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1673
1674 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1675
1676 j.jacocoReportClassesFile = jacocoReportClassesFile
1677
1678 return instrumentedJar
1679}
1680
albaltai36ff7dc2018-12-25 14:35:23 +08001681var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001682
Nan Zhanged19fc32017-10-19 13:06:22 -07001683func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001684 if j.headerJarFile == nil {
1685 return nil
1686 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001687 return android.Paths{j.headerJarFile}
1688}
1689
1690func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001691 if j.implementationJarFile == nil {
1692 return nil
1693 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001694 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001695}
1696
Colin Crossf24a22a2019-01-31 14:12:44 -08001697func (j *Module) DexJar() android.Path {
1698 return j.dexJarFile
1699}
1700
Colin Cross331a1212018-08-15 20:40:52 -07001701func (j *Module) ResourceJars() android.Paths {
1702 if j.resourceJar == nil {
1703 return nil
1704 }
1705 return android.Paths{j.resourceJar}
1706}
1707
1708func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001709 if j.implementationAndResourcesJar == nil {
1710 return nil
1711 }
Colin Cross331a1212018-08-15 20:40:52 -07001712 return android.Paths{j.implementationAndResourcesJar}
1713}
1714
Colin Cross46c9b8b2017-06-22 16:51:17 -07001715func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001716 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001717 return j.exportAidlIncludeDirs
1718}
1719
Jiyong Park1be96912018-05-28 18:02:19 +09001720func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001721 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001722 return j.exportedSdkLibs
1723}
1724
Artur Satayev9cf46692019-11-26 18:08:34 +00001725func (j *Module) ExportedPlugins() (android.Paths, []string) {
1726 return j.exportedPluginJars, j.exportedPluginClasses
1727}
1728
Colin Cross0c4ce212019-05-03 15:28:19 -07001729func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1730 return j.srcJarArgs, j.srcJarDeps
1731}
1732
Colin Cross46c9b8b2017-06-22 16:51:17 -07001733var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001734
Colin Cross46c9b8b2017-06-22 16:51:17 -07001735func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001736 return j.logtagsSrcs
1737}
1738
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001739// Collect information for opening IDE project files in java/jdeps.go.
1740func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1741 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1742 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001743 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001744 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001745 if j.expandJarjarRules != nil {
1746 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001747 }
1748}
1749
1750func (j *Module) CompilerDeps() []string {
1751 jdeps := []string{}
1752 jdeps = append(jdeps, j.properties.Libs...)
1753 jdeps = append(jdeps, j.properties.Static_libs...)
1754 return jdeps
1755}
1756
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001757func (j *Module) hasCode(ctx android.ModuleContext) bool {
1758 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1759 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1760}
1761
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001762func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001763 // Dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Han5e9013b2020-03-10 06:23:13 +09001764 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
1765 return true
1766 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09001767 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001768}
1769
Jiyong Park0b238752019-10-29 11:23:10 +09001770func (j *Module) Stem() string {
1771 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1772}
1773
Jiyong Park618922e2020-01-08 13:35:43 +09001774func (j *Module) JacocoReportClassesFile() android.Path {
1775 return j.jacocoReportClassesFile
1776}
1777
Martin Stjernholm6d415272020-01-31 17:10:36 +00001778func (j *Module) IsInstallable() bool {
1779 return Bool(j.properties.Installable)
1780}
1781
Colin Cross2fe66872015-03-30 17:20:39 -07001782//
1783// Java libraries (.jar file)
1784//
1785
Anton Hansson78156ef2020-03-27 19:39:48 +00001786type LibraryProperties struct {
1787 Dist struct {
1788 // The tag of the output of this module that should be output.
1789 Tag *string `android:"arch_variant"`
1790 } `android:"arch_variant"`
1791}
1792
Colin Crossf506d872017-07-19 15:53:04 -07001793type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001794 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001795
Anton Hansson78156ef2020-03-27 19:39:48 +00001796 libraryProperties LibraryProperties
1797
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001798 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001799}
1800
Colin Cross42be7612019-02-21 18:12:14 -08001801func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001802 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1803 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1804 return true
1805 }
1806
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001807 // Store uncompressed (and do not strip) dex files from boot class path jars.
1808 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1809 return true
1810 }
1811
1812 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001813 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001814 return true
1815 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001816 if ctx.Config().UncompressPrivAppDex() &&
1817 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1818 return true
1819 }
1820
Colin Cross2fc72f62018-12-21 12:59:54 -08001821 return false
1822}
1823
Colin Crossf506d872017-07-19 15:53:04 -07001824func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev2db1c3f2020-04-08 19:09:30 +01001825 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001826 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001827 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001828 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001829 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001830 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001831
Jiyong Park7f7766d2019-07-25 22:02:35 +09001832 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1833 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001834 var extraInstallDeps android.Paths
1835 if j.InstallMixin != nil {
1836 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1837 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001838 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001839 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001840 }
Anton Hansson78156ef2020-03-27 19:39:48 +00001841
1842 // Verify Dist.Tag is set to a supported output
1843 if j.libraryProperties.Dist.Tag != nil {
1844 distFiles, err := j.OutputFiles(*j.libraryProperties.Dist.Tag)
1845 if err != nil {
1846 ctx.PropertyErrorf("dist.tag", "%s", err.Error())
1847 }
1848 j.distFile = distFiles[0]
1849 }
Colin Crossb7a63242015-04-16 14:09:14 -07001850}
1851
Colin Crossf506d872017-07-19 15:53:04 -07001852func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001853 j.deps(ctx)
1854}
1855
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001856const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001857 aidlIncludeDir = "aidl"
1858 javaDir = "java"
1859 jarFileSuffix = ".jar"
1860 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001861)
1862
Paul Duffina0dbf432019-12-05 11:25:53 +00001863// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +00001864func sdkSnapshotFilePathForJar(osPrefix, name string) string {
1865 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001866}
1867
Paul Duffina04c1072020-03-02 10:16:35 +00001868func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
1869 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001870}
1871
Paul Duffin13879572019-11-28 14:31:38 +00001872type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001873 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001874
1875 // Function to retrieve the appropriate output jar (implementation or header) from
1876 // the library.
1877 jarToExportGetter func(j *Library) android.Path
Paul Duffin13879572019-11-28 14:31:38 +00001878}
1879
1880func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1881 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1882}
1883
1884func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1885 _, ok := module.(*Library)
1886 return ok
1887}
1888
Paul Duffin3a4eb502020-03-19 16:11:18 +00001889func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1890 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00001891}
Paul Duffina0dbf432019-12-05 11:25:53 +00001892
Paul Duffin14eb4672020-03-02 11:33:02 +00001893func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +00001894 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +00001895}
1896
1897type librarySdkMemberProperties struct {
1898 android.SdkMemberPropertiesBase
1899
Paul Duffina551a1c2020-03-17 21:04:24 +00001900 JarToExport android.Path
1901 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +00001902}
1903
Paul Duffin3a4eb502020-03-19 16:11:18 +00001904func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +00001905 j := variant.(*Library)
1906
Paul Duffina551a1c2020-03-17 21:04:24 +00001907 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(j)
1908 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +00001909}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001910
Paul Duffin3a4eb502020-03-19 16:11:18 +00001911func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001912 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001913
Paul Duffina551a1c2020-03-17 21:04:24 +00001914 exportedJar := p.JarToExport
1915 if exportedJar != nil {
1916 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
Paul Duffin14eb4672020-03-02 11:33:02 +00001917 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
1918
Paul Duffina551a1c2020-03-17 21:04:24 +00001919 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
1920 }
1921
1922 aidlIncludeDirs := p.AidlIncludeDirs
1923 if len(aidlIncludeDirs) != 0 {
1924 sdkModuleContext := ctx.SdkModuleContext()
1925 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +00001926 // TODO(jiyong): copy parcelable declarations only
1927 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1928 for _, file := range aidlFiles {
1929 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1930 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001931 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001932
Paul Duffina551a1c2020-03-17 21:04:24 +00001933 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +00001934 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001935}
1936
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001937var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
1938 android.SdkMemberTypeBase{
1939 PropertyName: "java_header_libs",
1940 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001941 },
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001942 func(j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +00001943 headerJars := j.HeaderJars()
1944 if len(headerJars) != 1 {
1945 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1946 }
1947
1948 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001949 },
Paul Duffina0dbf432019-12-05 11:25:53 +00001950}
1951
Colin Cross1b16b0e2019-02-12 14:41:32 -08001952// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1953//
1954// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1955// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1956// as a `static_libs` dependency of another module.
1957//
1958// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1959// a device.
1960//
1961// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1962// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001963func LibraryFactory() android.Module {
1964 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001965
Colin Cross9ae1b922018-06-26 17:59:05 -07001966 module.AddProperties(
1967 &module.Module.properties,
1968 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001969 &module.Module.dexpreoptProperties,
Anton Hansson78156ef2020-03-27 19:39:48 +00001970 &module.Module.protoProperties,
1971 &module.libraryProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001972
Jiyong Park7f7766d2019-07-25 22:02:35 +09001973 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001974 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001975 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001976 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001977}
1978
Colin Cross1b16b0e2019-02-12 14:41:32 -08001979// java_library_static is an obsolete alias for java_library.
1980func LibraryStaticFactory() android.Module {
1981 return LibraryFactory()
1982}
1983
1984// java_library_host builds and links sources into a `.jar` file for the host.
1985//
1986// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1987// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001988func LibraryHostFactory() android.Module {
1989 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001990
Colin Cross6af17aa2017-09-20 12:59:05 -07001991 module.AddProperties(
1992 &module.Module.properties,
1993 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001994
Colin Cross9ae1b922018-06-26 17:59:05 -07001995 module.Module.properties.Installable = proptools.BoolPtr(true)
1996
Jiyong Park7f7766d2019-07-25 22:02:35 +09001997 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001998 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001999 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002000}
2001
2002//
Colin Crossb628ea52018-08-14 16:42:33 -07002003// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002004//
2005
2006type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002007 // list of compatibility suites (for example "cts", "vts") that the module should be
2008 // installed into.
2009 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002010
2011 // the name of the test configuration (for example "AndroidTest.xml") that should be
2012 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002013 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002014
Jack He33338892018-09-19 02:21:28 -07002015 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2016 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002017 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002018
Colin Crossd96ca352018-08-10 16:06:24 -07002019 // list of files or filegroup modules that provide data that should be installed alongside
2020 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002021 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002022
2023 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2024 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2025 // explicitly.
2026 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002027}
2028
Paul Duffin42df1442019-03-20 12:45:53 +00002029type testHelperLibraryProperties struct {
2030 // list of compatibility suites (for example "cts", "vts") that the module should be
2031 // installed into.
2032 Test_suites []string `android:"arch_variant"`
2033}
2034
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002035type prebuiltTestProperties struct {
2036 // list of compatibility suites (for example "cts", "vts") that the module should be
2037 // installed into.
2038 Test_suites []string `android:"arch_variant"`
2039
2040 // the name of the test configuration (for example "AndroidTest.xml") that should be
2041 // installed with the module.
2042 Test_config *string `android:"path,arch_variant"`
2043}
2044
Colin Cross05638fc2018-04-09 18:40:24 -07002045type Test struct {
2046 Library
2047
2048 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002049
2050 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002051 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002052}
2053
Paul Duffin42df1442019-03-20 12:45:53 +00002054type TestHelperLibrary struct {
2055 Library
2056
2057 testHelperLibraryProperties testHelperLibraryProperties
2058}
2059
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002060type JavaTestImport struct {
2061 Import
2062
2063 prebuiltTestProperties prebuiltTestProperties
2064
2065 testConfig android.Path
2066}
2067
Colin Cross303e21f2018-08-07 16:49:25 -07002068func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002069 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2070 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002071 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002072
2073 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002074}
2075
Paul Duffin42df1442019-03-20 12:45:53 +00002076func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2077 j.Library.GenerateAndroidBuildActions(ctx)
2078}
2079
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002080func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2081 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2082 j.prebuiltTestProperties.Test_suites, nil)
2083
2084 j.Import.GenerateAndroidBuildActions(ctx)
2085}
2086
2087type testSdkMemberType struct {
2088 android.SdkMemberTypeBase
2089}
2090
2091func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2092 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2093}
2094
2095func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2096 _, ok := module.(*Test)
2097 return ok
2098}
2099
Paul Duffin3a4eb502020-03-19 16:11:18 +00002100func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2101 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00002102}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002103
Paul Duffin14eb4672020-03-02 11:33:02 +00002104func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2105 return &testSdkMemberProperties{}
2106}
2107
2108type testSdkMemberProperties struct {
2109 android.SdkMemberPropertiesBase
2110
Paul Duffina551a1c2020-03-17 21:04:24 +00002111 JarToExport android.Path
2112 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00002113}
2114
Paul Duffin3a4eb502020-03-19 16:11:18 +00002115func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00002116 test := variant.(*Test)
2117
2118 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002119 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00002120 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002121 }
2122
Paul Duffina551a1c2020-03-17 21:04:24 +00002123 p.JarToExport = implementationJars[0]
2124 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00002125}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002126
Paul Duffin3a4eb502020-03-19 16:11:18 +00002127func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00002128 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00002129
Paul Duffina551a1c2020-03-17 21:04:24 +00002130 exportedJar := p.JarToExport
2131 if exportedJar != nil {
2132 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
2133 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002134
2135 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00002136 }
2137
2138 testConfig := p.TestConfig
2139 if testConfig != nil {
2140 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
2141 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002142 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
2143 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002144}
2145
Colin Cross1b16b0e2019-02-12 14:41:32 -08002146// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2147// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2148//
2149// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2150// compiled against the device bootclasspath.
2151//
2152// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2153// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002154func TestFactory() android.Module {
2155 module := &Test{}
2156
2157 module.AddProperties(
2158 &module.Module.properties,
2159 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002160 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002161 &module.Module.protoProperties,
2162 &module.testProperties)
2163
Colin Cross9ae1b922018-06-26 17:59:05 -07002164 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002165 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002166
Colin Cross05638fc2018-04-09 18:40:24 -07002167 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002168 return module
2169}
2170
Paul Duffin42df1442019-03-20 12:45:53 +00002171// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2172func TestHelperLibraryFactory() android.Module {
2173 module := &TestHelperLibrary{}
2174
2175 module.AddProperties(
2176 &module.Module.properties,
2177 &module.Module.deviceProperties,
2178 &module.Module.dexpreoptProperties,
2179 &module.Module.protoProperties,
2180 &module.testHelperLibraryProperties)
2181
Colin Cross9a4abed2019-04-24 13:19:28 -07002182 module.Module.properties.Installable = proptools.BoolPtr(true)
2183 module.Module.dexpreopter.isTest = true
2184
Paul Duffin42df1442019-03-20 12:45:53 +00002185 InitJavaModule(module, android.HostAndDeviceSupported)
2186 return module
2187}
2188
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002189// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2190// and makes sure that it is added to the appropriate test suite.
2191//
2192// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2193// compiled against an Android classpath.
2194//
2195// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2196// for host modules.
2197func JavaTestImportFactory() android.Module {
2198 module := &JavaTestImport{}
2199
2200 module.AddProperties(
2201 &module.Import.properties,
2202 &module.prebuiltTestProperties)
2203
2204 module.Import.properties.Installable = proptools.BoolPtr(true)
2205
2206 android.InitPrebuiltModule(module, &module.properties.Jars)
2207 android.InitApexModule(module)
2208 android.InitSdkAwareModule(module)
2209 InitJavaModule(module, android.HostAndDeviceSupported)
2210 return module
2211}
2212
Colin Cross1b16b0e2019-02-12 14:41:32 -08002213// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2214// allow running the test with `atest` or a `TEST_MAPPING` file.
2215//
2216// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2217// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002218func TestHostFactory() android.Module {
2219 module := &Test{}
2220
2221 module.AddProperties(
2222 &module.Module.properties,
2223 &module.Module.protoProperties,
2224 &module.testProperties)
2225
Colin Cross9ae1b922018-06-26 17:59:05 -07002226 module.Module.properties.Installable = proptools.BoolPtr(true)
2227
Colin Cross05638fc2018-04-09 18:40:24 -07002228 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002229 return module
2230}
2231
2232//
Colin Cross2fe66872015-03-30 17:20:39 -07002233// Java Binaries (.jar file plus wrapper script)
2234//
2235
Colin Crossf506d872017-07-19 15:53:04 -07002236type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002237 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002238 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002239
2240 // Name of the class containing main to be inserted into the manifest as Main-Class.
2241 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002242}
2243
Colin Crossf506d872017-07-19 15:53:04 -07002244type Binary struct {
2245 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002246
Colin Crossf506d872017-07-19 15:53:04 -07002247 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002248
Colin Cross6b4a32d2017-12-05 13:42:45 -08002249 isWrapperVariant bool
2250
Colin Crossc3315992017-12-08 19:12:36 -08002251 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002252 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002253}
2254
Alex Light24237172017-10-26 09:46:21 -07002255func (j *Binary) HostToolPath() android.OptionalPath {
2256 return android.OptionalPathForPath(j.binaryFile)
2257}
2258
Colin Crossf506d872017-07-19 15:53:04 -07002259func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002260 if ctx.Arch().ArchType == android.Common {
2261 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002262 if j.binaryProperties.Main_class != nil {
2263 if j.properties.Manifest != nil {
2264 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2265 }
2266 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2267 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2268 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2269 }
2270
Colin Cross6b4a32d2017-12-05 13:42:45 -08002271 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002272 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002273 // Handle the binary wrapper
2274 j.isWrapperVariant = true
2275
Colin Cross366938f2017-12-11 16:29:02 -08002276 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002277 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002278 } else {
2279 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2280 }
2281
2282 // Depend on the installed jar so that the wrapper doesn't get executed by
2283 // another build rule before the jar has been installed.
2284 jarFile := ctx.PrimaryModule().(*Binary).installFile
2285
2286 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2287 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002288 }
Colin Cross2fe66872015-03-30 17:20:39 -07002289}
2290
Colin Crossf506d872017-07-19 15:53:04 -07002291func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002292 if ctx.Arch().ArchType == android.Common {
2293 j.deps(ctx)
2294 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002295}
2296
Colin Cross1b16b0e2019-02-12 14:41:32 -08002297// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2298// as well.
2299//
2300// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2301// compiled against the device bootclasspath.
2302//
2303// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2304// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002305func BinaryFactory() android.Module {
2306 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002307
Colin Cross36242852017-06-23 15:06:31 -07002308 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002309 &module.Module.properties,
2310 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002311 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002312 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002313 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002314
Colin Cross9ae1b922018-06-26 17:59:05 -07002315 module.Module.properties.Installable = proptools.BoolPtr(true)
2316
Colin Cross6b4a32d2017-12-05 13:42:45 -08002317 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2318 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002319 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002320}
2321
Colin Cross1b16b0e2019-02-12 14:41:32 -08002322// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2323//
2324// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2325// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002326func BinaryHostFactory() android.Module {
2327 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002328
Colin Cross36242852017-06-23 15:06:31 -07002329 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002330 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002331 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002332 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002333
Colin Cross9ae1b922018-06-26 17:59:05 -07002334 module.Module.properties.Installable = proptools.BoolPtr(true)
2335
Colin Cross6b4a32d2017-12-05 13:42:45 -08002336 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2337 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002338 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002339}
2340
2341//
2342// Java prebuilts
2343//
2344
Colin Cross74d73e22017-08-02 11:05:49 -07002345type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00002346 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002347
Nan Zhangea568a42017-11-08 21:20:04 -08002348 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002349
2350 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002351
2352 // List of shared java libs that this module has dependencies to
2353 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002354
2355 // List of files to remove from the jar file(s)
2356 Exclude_files []string
2357
2358 // List of directories to remove from the jar file(s)
2359 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002360
2361 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002362 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002363
2364 // set the name of the output
2365 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09002366
2367 Aidl struct {
2368 // directories that should be added as include directories for any aidl sources of modules
2369 // that depend on this module, as well as to aidl for this module.
2370 Export_include_dirs []string
2371 }
Colin Cross74d73e22017-08-02 11:05:49 -07002372}
2373
2374type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002375 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002376 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002377 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002378 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002379 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002380
Colin Cross74d73e22017-08-02 11:05:49 -07002381 properties ImportProperties
2382
Colin Cross0a6e0072017-08-30 14:24:55 -07002383 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002384 exportedSdkLibs []string
Jiyong Park19604de2020-03-24 16:44:11 +09002385 exportAidlIncludeDirs android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -07002386}
2387
Jiyong Park6a927c42020-01-21 02:03:43 +09002388func (j *Import) sdkVersion() sdkSpec {
2389 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002390}
2391
Jiyong Park6a927c42020-01-21 02:03:43 +09002392func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002393 return j.sdkVersion()
2394}
2395
Colin Cross74d73e22017-08-02 11:05:49 -07002396func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002397 return &j.prebuilt
2398}
2399
Colin Cross74d73e22017-08-02 11:05:49 -07002400func (j *Import) PrebuiltSrcs() []string {
2401 return j.properties.Jars
2402}
2403
2404func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002405 return j.prebuilt.Name(j.ModuleBase.Name())
2406}
2407
Jiyong Park0b238752019-10-29 11:23:10 +09002408func (j *Import) Stem() string {
2409 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2410}
2411
Jiyong Park618922e2020-01-08 13:35:43 +09002412func (a *Import) JacocoReportClassesFile() android.Path {
2413 return nil
2414}
2415
Colin Cross74d73e22017-08-02 11:05:49 -07002416func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002417 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002418}
2419
Colin Cross74d73e22017-08-02 11:05:49 -07002420func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002421 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002422
Jiyong Park0b238752019-10-29 11:23:10 +09002423 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002424 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002425 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2426 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002427 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002428 inputFile := outputFile
2429 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2430 TransformJetifier(ctx, outputFile, inputFile)
2431 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002432 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002433
2434 ctx.VisitDirectDeps(func(module android.Module) {
2435 otherName := ctx.OtherModuleName(module)
2436 tag := ctx.OtherModuleDependencyTag(module)
2437
2438 switch dep := module.(type) {
2439 case Dependency:
2440 switch tag {
2441 case libTag, staticLibTag:
2442 // sdk lib names from dependencies are re-exported
2443 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2444 }
2445 case SdkLibraryDependency:
2446 switch tag {
2447 case libTag:
2448 // names of sdk libs that are directly depended are exported
2449 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2450 }
2451 }
2452 })
2453
2454 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002455 if Bool(j.properties.Installable) {
2456 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002457 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002458 }
Jiyong Park19604de2020-03-24 16:44:11 +09002459
2460 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Colin Cross2fe66872015-03-30 17:20:39 -07002461}
2462
Colin Cross74d73e22017-08-02 11:05:49 -07002463var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002464
Nan Zhanged19fc32017-10-19 13:06:22 -07002465func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002466 if j.combinedClasspathFile == nil {
2467 return nil
2468 }
Colin Cross37f6d792018-07-12 12:28:41 -07002469 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002470}
2471
2472func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002473 if j.combinedClasspathFile == nil {
2474 return nil
2475 }
Colin Cross37f6d792018-07-12 12:28:41 -07002476 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002477}
2478
Colin Cross331a1212018-08-15 20:40:52 -07002479func (j *Import) ResourceJars() android.Paths {
2480 return nil
2481}
2482
2483func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002484 if j.combinedClasspathFile == nil {
2485 return nil
2486 }
Colin Cross331a1212018-08-15 20:40:52 -07002487 return android.Paths{j.combinedClasspathFile}
2488}
2489
Colin Crossf24a22a2019-01-31 14:12:44 -08002490func (j *Import) DexJar() android.Path {
2491 return nil
2492}
2493
Colin Cross74d73e22017-08-02 11:05:49 -07002494func (j *Import) AidlIncludeDirs() android.Paths {
Jiyong Park19604de2020-03-24 16:44:11 +09002495 return j.exportAidlIncludeDirs
Colin Crossc0b06f12015-04-08 13:03:43 -07002496}
2497
Jiyong Park1be96912018-05-28 18:02:19 +09002498func (j *Import) ExportedSdkLibs() []string {
2499 return j.exportedSdkLibs
2500}
2501
Artur Satayev9cf46692019-11-26 18:08:34 +00002502func (j *Import) ExportedPlugins() (android.Paths, []string) {
2503 return nil, nil
2504}
2505
Colin Cross0c4ce212019-05-03 15:28:19 -07002506func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2507 return nil, nil
2508}
2509
Jiyong Park0f80c182020-01-31 02:49:53 +09002510func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09002511 // dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Han5e9013b2020-03-10 06:23:13 +09002512 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
2513 return true
2514 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002515 return false
Jiyong Park0f80c182020-01-31 02:49:53 +09002516}
2517
albaltai36ff7dc2018-12-25 14:35:23 +08002518// Add compile time check for interface implementation
2519var _ android.IDEInfo = (*Import)(nil)
2520var _ android.IDECustomizedModuleName = (*Import)(nil)
2521
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002522// Collect information for opening IDE project files in java/jdeps.go.
2523const (
2524 removedPrefix = "prebuilt_"
2525)
2526
2527func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2528 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2529}
2530
2531func (j *Import) IDECustomizedModuleName() string {
2532 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2533 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2534 // solution to get the Import name.
2535 name := j.Name()
2536 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002537 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002538 }
2539 return name
2540}
2541
Colin Cross74d73e22017-08-02 11:05:49 -07002542var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002543
Colin Cross1b16b0e2019-02-12 14:41:32 -08002544// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2545//
2546// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2547// compiled against an Android classpath.
2548//
2549// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2550// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002551func ImportFactory() android.Module {
2552 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002553
Colin Cross74d73e22017-08-02 11:05:49 -07002554 module.AddProperties(&module.properties)
2555
2556 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002557 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002558 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002559 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002560 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002561}
2562
Colin Cross1b16b0e2019-02-12 14:41:32 -08002563// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2564// module.
2565//
2566// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2567// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002568func ImportFactoryHost() android.Module {
2569 module := &Import{}
2570
2571 module.AddProperties(&module.properties)
2572
2573 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002574 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002575 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002576 return module
2577}
2578
Colin Cross42be7612019-02-21 18:12:14 -08002579// dex_import module
2580
2581type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002582 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002583
2584 // set the name of the output
2585 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002586}
2587
2588type DexImport struct {
2589 android.ModuleBase
2590 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002591 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002592 prebuilt android.Prebuilt
2593
2594 properties DexImportProperties
2595
2596 dexJarFile android.Path
2597 maybeStrippedDexJarFile android.Path
2598
2599 dexpreopter
2600}
2601
2602func (j *DexImport) Prebuilt() *android.Prebuilt {
2603 return &j.prebuilt
2604}
2605
2606func (j *DexImport) PrebuiltSrcs() []string {
2607 return j.properties.Jars
2608}
2609
2610func (j *DexImport) Name() string {
2611 return j.prebuilt.Name(j.ModuleBase.Name())
2612}
2613
Jiyong Park0b238752019-10-29 11:23:10 +09002614func (j *DexImport) Stem() string {
2615 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2616}
2617
Martin Stjernholm6d415272020-01-31 17:10:36 +00002618func (j *DexImport) IsInstallable() bool {
2619 return true
2620}
2621
Colin Cross42be7612019-02-21 18:12:14 -08002622func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2623 if len(j.properties.Jars) != 1 {
2624 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2625 }
2626
Jiyong Park0b238752019-10-29 11:23:10 +09002627 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002628 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2629
2630 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2631 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2632
2633 if j.dexpreopter.uncompressedDex {
2634 rule := android.NewRuleBuilder()
2635
2636 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2637 rule.Temporary(temporary)
2638
2639 // use zip2zip to uncompress classes*.dex files
2640 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002641 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002642 FlagWithInput("-i ", inputJar).
2643 FlagWithOutput("-o ", temporary).
2644 FlagWithArg("-0 ", "'classes*.dex'")
2645
2646 // use zipalign to align uncompressed classes*.dex files
2647 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002648 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002649 Flag("-f").
2650 Text("4").
2651 Input(temporary).
2652 Output(dexOutputFile)
2653
2654 rule.DeleteTemporaryFiles()
2655
2656 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2657 } else {
2658 ctx.Build(pctx, android.BuildParams{
2659 Rule: android.Cp,
2660 Input: inputJar,
2661 Output: dexOutputFile,
2662 })
2663 }
2664
2665 j.dexJarFile = dexOutputFile
2666
2667 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2668
2669 j.maybeStrippedDexJarFile = dexOutputFile
2670
2671 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2672 ctx.ModuleName()+".jar", dexOutputFile)
2673}
2674
2675func (j *DexImport) DexJar() android.Path {
2676 return j.dexJarFile
2677}
2678
2679// dex_import imports a `.jar` file containing classes.dex files.
2680//
2681// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2682// to the device.
2683func DexImportFactory() android.Module {
2684 module := &DexImport{}
2685
2686 module.AddProperties(&module.properties)
2687
2688 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002689 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002690 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002691 return module
2692}
2693
Colin Cross89536d42017-07-07 14:35:50 -07002694//
2695// Defaults
2696//
2697type Defaults struct {
2698 android.ModuleBase
2699 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002700 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002701}
2702
Colin Cross1b16b0e2019-02-12 14:41:32 -08002703// java_defaults provides a set of properties that can be inherited by other java or android modules.
2704//
2705// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2706// property in the defaults module that exists in the depending module will be prepended to the depending module's
2707// value for that property.
2708//
2709// Example:
2710//
2711// java_defaults {
2712// name: "example_defaults",
2713// srcs: ["common/**/*.java"],
2714// javacflags: ["-Xlint:all"],
2715// aaptflags: ["--auto-add-overlay"],
2716// }
2717//
2718// java_library {
2719// name: "example",
2720// defaults: ["example_defaults"],
2721// srcs: ["example/**/*.java"],
2722// }
2723//
2724// is functionally identical to:
2725//
2726// java_library {
2727// name: "example",
2728// srcs: [
2729// "common/**/*.java",
2730// "example/**/*.java",
2731// ],
2732// javacflags: ["-Xlint:all"],
2733// }
Colin Cross89536d42017-07-07 14:35:50 -07002734func defaultsFactory() android.Module {
2735 return DefaultsFactory()
2736}
2737
Paul Duffin47357662019-12-05 14:07:14 +00002738func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002739 module := &Defaults{}
2740
Colin Cross89536d42017-07-07 14:35:50 -07002741 module.AddProperties(
2742 &CompilerProperties{},
2743 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002744 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002745 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002746 &aaptProperties{},
2747 &androidLibraryProperties{},
2748 &appProperties{},
2749 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002750 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002751 &ImportProperties{},
2752 &AARImportProperties{},
2753 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002754 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002755 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002756 )
2757
2758 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002759 return module
2760}
Nan Zhangea568a42017-11-08 21:20:04 -08002761
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002762func kytheExtractJavaFactory() android.Singleton {
2763 return &kytheExtractJavaSingleton{}
2764}
2765
2766type kytheExtractJavaSingleton struct {
2767}
2768
2769func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2770 var xrefTargets android.Paths
2771 ctx.VisitAllModules(func(module android.Module) {
2772 if javaModule, ok := module.(xref); ok {
2773 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2774 }
2775 })
2776 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2777 if len(xrefTargets) > 0 {
2778 ctx.Build(pctx, android.BuildParams{
2779 Rule: blueprint.Phony,
2780 Output: android.PathForPhony(ctx, "xref_java"),
2781 Inputs: xrefTargets,
2782 })
2783 }
2784}
2785
Nan Zhangea568a42017-11-08 21:20:04 -08002786var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002787var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002788var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002789var inList = android.InList