blob: 38f485e88752531765a976c84e4bdc558596289c [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 Cross5a116862020-04-22 11:44:34 -07001137 flags.processors = append(flags.processors, deps.processorClasses...)
1138 flags.processors = android.FirstUniqueStrings(flags.processors)
Colin Crossbe9cdb82019-01-21 21:37:16 -08001139
Colin Cross1e743852019-10-28 11:37:20 -07001140 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1141 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001142 // Give host-side tools a version of OpenJDK's standard libraries
1143 // close to what they're targeting. As of Dec 2017, AOSP is only
1144 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1145 //
1146 // When building with OpenJDK 8, the following should have no
1147 // effect since those jars would be available by default.
1148 //
1149 // When building with OpenJDK 9 but targeting a version < 1.8,
1150 // putting them on the bootclasspath means that:
1151 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1152 // b) references to existing APIs are not reinterpreted in an
1153 // OpenJDK 9-specific way, eg. calls to subclasses of
1154 // java.nio.Buffer as in http://b/70862583
1155 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1156 flags.bootClasspath = append(flags.bootClasspath,
1157 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1158 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001159 if Bool(j.properties.Use_tools_jar) {
1160 flags.bootClasspath = append(flags.bootClasspath,
1161 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1162 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001163 }
1164
Colin Cross1e743852019-10-28 11:37:20 -07001165 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001166 // Manually specify build directory in case it is not under the repo root.
1167 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1168 // just adding a symlink under the root doesn't help.)
1169 patchPaths := ".:" + ctx.Config().BuildDir()
1170 classPath := flags.classpath.FormJavaClassPath("")
1171 if classPath != "" {
1172 patchPaths += ":" + classPath
1173 }
1174 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001175 }
1176
Nan Zhanged19fc32017-10-19 13:06:22 -07001177 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001178 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001179
Nan Zhanged19fc32017-10-19 13:06:22 -07001180 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001181 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001182
Colin Cross81440082018-08-15 20:21:55 -07001183 if len(javacFlags) > 0 {
1184 // optimization.
1185 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1186 flags.javacFlags = "$javacFlags"
1187 }
1188
Nan Zhanged19fc32017-10-19 13:06:22 -07001189 return flags
1190}
Colin Crossc0b06f12015-04-08 13:03:43 -07001191
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001192func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001193 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001194
1195 deps := j.collectDeps(ctx)
1196 flags := j.collectBuilderFlags(ctx, deps)
1197
Colin Cross1e743852019-10-28 11:37:20 -07001198 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001199 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1200 }
Colin Cross8a497952019-03-05 22:25:09 -08001201 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001202 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001203 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001204 }
1205
Colin Crossaf050172017-11-15 23:01:59 -08001206 srcFiles = j.genSources(ctx, srcFiles, flags)
1207
1208 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001209 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001210 if aaptSrcJar != nil {
1211 srcJars = append(srcJars, aaptSrcJar)
1212 }
Colin Crossb7a63242015-04-16 14:09:14 -07001213
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001214 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001215 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001216 }
1217
Colin Cross1ee23172017-10-18 14:44:18 -07001218 jarName := ctx.ModuleName() + ".jar"
1219
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001220 javaSrcFiles := srcFiles.FilterByExt(".java")
1221 var uniqueSrcFiles android.Paths
1222 set := make(map[string]bool)
1223 for _, v := range javaSrcFiles {
1224 if _, found := set[v.String()]; !found {
1225 set[v.String()] = true
1226 uniqueSrcFiles = append(uniqueSrcFiles, v)
1227 }
1228 }
1229
patricktu242faad2019-09-24 15:41:30 +08001230 // Collect .java files for AIDEGen
1231 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1232
Colin Cross55f63ea2018-08-27 12:37:09 -07001233 var kotlinJars android.Paths
1234
Colin Cross93e85952017-08-15 13:34:18 -07001235 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001236 // user defined kotlin flags.
1237 kotlincFlags := j.properties.Kotlincflags
1238 CheckKotlincFlags(ctx, kotlincFlags)
1239
Colin Cross93e85952017-08-15 13:34:18 -07001240 // If there are kotlin files, compile them first but pass all the kotlin and java files
1241 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1242 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001243 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001244 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001245 kotlincFlags = append(kotlincFlags, "-no-jdk")
1246 }
1247 if len(kotlincFlags) > 0 {
1248 // optimization.
1249 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1250 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001251 }
1252
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001253 var kotlinSrcFiles android.Paths
1254 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1255 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1256
patricktu242faad2019-09-24 15:41:30 +08001257 // Collect .kt files for AIDEGen
1258 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1259
Colin Crossafbb1732019-01-17 15:42:52 -08001260 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1261 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1262
1263 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1264 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1265
1266 if len(flags.processorPath) > 0 {
1267 // Use kapt for annotation processing
1268 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1269 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1270 srcJars = append(srcJars, kaptSrcJar)
1271 // Disable annotation processing in javac, it's already been handled by kapt
1272 flags.processorPath = nil
Colin Cross5a116862020-04-22 11:44:34 -07001273 flags.processors = nil
Colin Crossafbb1732019-01-17 15:42:52 -08001274 }
Colin Cross93e85952017-08-15 13:34:18 -07001275
Colin Cross1ee23172017-10-18 14:44:18 -07001276 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001277 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001278 if ctx.Failed() {
1279 return
1280 }
1281
1282 // Make javac rule depend on the kotlinc rule
1283 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001284
Colin Cross93e85952017-08-15 13:34:18 -07001285 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001286 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001287 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001288 }
1289
Colin Cross55f63ea2018-08-27 12:37:09 -07001290 jars := append(android.Paths(nil), kotlinJars...)
1291
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001292 // Store the list of .java files that was passed to javac
1293 j.compiledJavaSrcs = uniqueSrcFiles
1294 j.compiledSrcJars = srcJars
1295
Nan Zhang61eaedb2017-11-02 13:28:15 -07001296 enable_sharding := false
Colin Crossf7d84012020-02-21 08:16:41 -08001297 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001298 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001299 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1300 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001301 // Formerly, there was a check here that prevented annotation processors
1302 // from being used when sharding was enabled, as some annotation processors
1303 // do not function correctly in sharded environments. It was removed to
1304 // allow for the use of annotation processors that do function correctly
1305 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001306 }
Colin Crossf7d84012020-02-21 08:16:41 -08001307 headerJarFileWithoutJarjar, j.headerJarFile =
1308 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001309 if ctx.Failed() {
1310 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001311 }
1312 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001313 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001314 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001315 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001316 // If error-prone is enabled, add an additional rule to compile the java files into
1317 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001318 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001319 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1320 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001321 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001322 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001323 extraJarDeps = append(extraJarDeps, errorprone)
1324 }
1325
Nan Zhang61eaedb2017-11-02 13:28:15 -07001326 if enable_sharding {
Colin Crossf7d84012020-02-21 08:16:41 -08001327 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001328 shardSize := int(*(j.properties.Javac_shard_size))
1329 var shardSrcs []android.Paths
1330 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001331 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001332 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001333 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1334 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001335 jars = append(jars, classes)
1336 }
1337 }
1338 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001339 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1340 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001341 jars = append(jars, classes)
1342 }
1343 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001344 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001345 jars = append(jars, classes)
1346 }
Colin Crossd6891432017-09-27 17:39:56 -07001347 if ctx.Failed() {
1348 return
1349 }
Colin Cross2fe66872015-03-30 17:20:39 -07001350 }
1351
Colin Cross0c4ce212019-05-03 15:28:19 -07001352 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1353
1354 var includeSrcJar android.WritablePath
1355 if Bool(j.properties.Include_srcs) {
1356 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1357 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1358 }
1359
Colin Crosscedd4762018-09-13 11:26:19 -07001360 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1361 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001362 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001363 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001364
1365 var resArgs []string
1366 var resDeps android.Paths
1367
1368 resArgs = append(resArgs, dirArgs...)
1369 resDeps = append(resDeps, dirDeps...)
1370
1371 resArgs = append(resArgs, fileArgs...)
1372 resDeps = append(resDeps, fileDeps...)
1373
Colin Cross988708c2019-05-06 14:04:11 -07001374 resArgs = append(resArgs, extraArgs...)
1375 resDeps = append(resDeps, extraDeps...)
1376
Colin Cross40a36712017-09-27 17:41:35 -07001377 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001378 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001379 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001380 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001381 if ctx.Failed() {
1382 return
1383 }
1384 }
1385
Colin Cross0c4ce212019-05-03 15:28:19 -07001386 var resourceJars android.Paths
1387 if j.resourceJar != nil {
1388 resourceJars = append(resourceJars, j.resourceJar)
1389 }
1390 if Bool(j.properties.Include_srcs) {
1391 resourceJars = append(resourceJars, includeSrcJar)
1392 }
1393 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001394
Colin Cross0c4ce212019-05-03 15:28:19 -07001395 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001396 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001397 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001398 false, nil, nil)
1399 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001400 } else if len(resourceJars) == 1 {
1401 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001402 }
1403
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001404 if len(deps.staticJars) > 0 {
1405 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001406 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001407
Colin Cross094054a2018-10-17 15:10:48 -07001408 manifest := j.overrideManifest
1409 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001410 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001411 }
Colin Cross635acc92017-09-12 22:50:46 -07001412
Colin Cross8a497952019-03-05 22:25:09 -08001413 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001414 if len(services) > 0 {
1415 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1416 var zipargs []string
1417 for _, file := range services {
1418 serviceFile := file.String()
1419 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1420 }
1421 ctx.Build(pctx, android.BuildParams{
1422 Rule: zip,
1423 Output: servicesJar,
1424 Implicits: services,
1425 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001426 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001427 },
1428 })
1429 jars = append(jars, servicesJar)
1430 }
1431
Colin Cross0a6e0072017-08-30 14:24:55 -07001432 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001433 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001434 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001435
1436 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001437 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1438 // Optimization: skip the combine step if there is nothing to do
1439 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1440 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1441 // any if len(jars) == 1.
1442 outputFile = moduleOutPath
1443 } else {
1444 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1445 ctx.Build(pctx, android.BuildParams{
1446 Rule: android.Cp,
1447 Input: jars[0],
1448 Output: combinedJar,
1449 })
1450 outputFile = combinedJar
1451 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001452 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001453 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001454 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001455 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001456 outputFile = combinedJar
1457 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001458
Colin Cross331a1212018-08-15 20:40:52 -07001459 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001460 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001461 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001462 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001463 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001464 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001465
1466 // jarjar resource jar if necessary
1467 if j.resourceJar != nil {
1468 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001469 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001470 j.resourceJar = resourceJarJarFile
1471 }
1472
Colin Cross0a6e0072017-08-30 14:24:55 -07001473 if ctx.Failed() {
1474 return
1475 }
1476 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001477
1478 // Check package restrictions if necessary.
1479 if len(j.properties.Permitted_packages) > 0 {
1480 // Check packages and copy to package-checked file.
1481 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1482 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1483 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1484
1485 if ctx.Failed() {
1486 return
1487 }
1488 }
1489
Nan Zhanged19fc32017-10-19 13:06:22 -07001490 j.implementationJarFile = outputFile
1491 if j.headerJarFile == nil {
1492 j.headerJarFile = j.implementationJarFile
1493 }
Colin Cross2fe66872015-03-30 17:20:39 -07001494
Jiyong Park93e57a02020-02-21 16:04:53 +09001495 // Force enable the instrumentation for java code that is built for APEXes ...
1496 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1497 // doesn't make sense)
1498 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1499 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park00cae1c2020-02-18 12:50:44 +00001500 j.properties.Instrument = true
1501 }
1502
Colin Cross3144dfc2018-01-03 15:06:47 -08001503 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001504 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1505 }
1506
Colin Cross331a1212018-08-15 20:40:52 -07001507 // merge implementation jar with resources if necessary
1508 implementationAndResourcesJar := outputFile
1509 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001510 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001511 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001512 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001513 false, nil, nil)
1514 implementationAndResourcesJar = combinedJar
1515 }
1516
1517 j.implementationAndResourcesJar = implementationAndResourcesJar
1518
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001519 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1520 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1521 if j.deviceProperties.Compile_dex == nil {
1522 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1523 }
1524 if j.deviceProperties.Hostdex == nil {
1525 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1526 }
1527 }
1528
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001529 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001530 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001531 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001532 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001533 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001534 if ctx.Failed() {
1535 return
1536 }
Colin Cross331a1212018-08-15 20:40:52 -07001537
Jiyong Park09cb6292019-07-15 15:29:23 +09001538 // Hidden API CSV generation and dex encoding
1539 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1540 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001541
Colin Cross331a1212018-08-15 20:40:52 -07001542 // merge dex jar with resources if necessary
1543 if j.resourceJar != nil {
1544 jars := android.Paths{dexOutputFile, j.resourceJar}
1545 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1546 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1547 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001548 if j.deviceProperties.UncompressDex {
1549 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1550 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1551 dexOutputFile = combinedAlignedJar
1552 } else {
1553 dexOutputFile = combinedJar
1554 }
Colin Cross331a1212018-08-15 20:40:52 -07001555 }
1556
1557 j.dexJarFile = dexOutputFile
1558
Colin Cross8faf8fc2019-01-16 15:15:52 -08001559 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001560 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1561
1562 j.maybeStrippedDexJarFile = dexOutputFile
1563
Colin Cross3063b782018-08-15 11:19:12 -07001564 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001565
1566 if ctx.Failed() {
1567 return
1568 }
Colin Cross331a1212018-08-15 20:40:52 -07001569 } else {
1570 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001571 }
Colin Cross331a1212018-08-15 20:40:52 -07001572
Colin Crossb7a63242015-04-16 14:09:14 -07001573 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001574
1575 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1576 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001577}
1578
Colin Cross3b706fd2019-09-05 16:44:18 -07001579func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1580 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1581
1582 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1583 if idx >= 0 {
1584 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1585 jarName += strconv.Itoa(idx)
1586 }
1587
1588 classes := android.PathForModuleOut(ctx, "javac", jarName)
1589 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1590
1591 if ctx.Config().EmitXrefRules() {
1592 extractionFile := android.PathForModuleOut(ctx, kzipName)
1593 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1594 j.kytheFiles = append(j.kytheFiles, extractionFile)
1595 }
1596
1597 return classes
1598}
1599
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001600// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1601// since some of these flags may be used internally.
1602func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1603 for _, flag := range flags {
1604 flag = strings.TrimSpace(flag)
1605
1606 if !strings.HasPrefix(flag, "-") {
1607 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1608 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1609 ctx.PropertyErrorf("kotlincflags",
1610 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1611 } else if inList(flag, config.KotlincIllegalFlags) {
1612 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1613 } else if flag == "-include-runtime" {
1614 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1615 } else {
1616 args := strings.Split(flag, " ")
1617 if args[0] == "-kotlin-home" {
1618 ctx.PropertyErrorf("kotlincflags",
1619 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1620 }
1621 }
1622 }
1623}
1624
Colin Cross8eadbf02017-10-24 17:46:00 -07001625func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf7d84012020-02-21 08:16:41 -08001626 deps deps, flags javaBuilderFlags, jarName string,
1627 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001628
1629 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001630 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001631 // Compile java sources into turbine.jar.
1632 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1633 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1634 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08001635 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001636 }
1637 jars = append(jars, turbineJar)
1638 }
1639
Colin Cross55f63ea2018-08-27 12:37:09 -07001640 jars = append(jars, extraJars...)
1641
Nan Zhanged19fc32017-10-19 13:06:22 -07001642 // Combine any static header libraries into classes-header.jar. If there is only
1643 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001644 jars = append(jars, deps.staticHeaderJars...)
1645
Colin Cross5c6ecc12017-10-23 18:12:27 -07001646 // we cannot skip the combine step for now if there is only one jar
1647 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1648 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001649 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001650 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001651 headerJar = combinedJar
Colin Crossf7d84012020-02-21 08:16:41 -08001652 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001653
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001654 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001655 // Transform classes.jar into classes-jarjar.jar
1656 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001657 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf7d84012020-02-21 08:16:41 -08001658 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001659 if ctx.Failed() {
Colin Crossf7d84012020-02-21 08:16:41 -08001660 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001661 }
1662 }
1663
Colin Crossf7d84012020-02-21 08:16:41 -08001664 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001665}
1666
Colin Crosscb933592017-11-22 13:49:43 -08001667func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001668 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001669
Colin Cross7a3139e2017-12-19 13:57:50 -08001670 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001671
Colin Cross84c38822018-01-03 15:59:46 -08001672 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001673 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1674
1675 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1676
1677 j.jacocoReportClassesFile = jacocoReportClassesFile
1678
1679 return instrumentedJar
1680}
1681
albaltai36ff7dc2018-12-25 14:35:23 +08001682var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001683
Nan Zhanged19fc32017-10-19 13:06:22 -07001684func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001685 if j.headerJarFile == nil {
1686 return nil
1687 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001688 return android.Paths{j.headerJarFile}
1689}
1690
1691func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001692 if j.implementationJarFile == nil {
1693 return nil
1694 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001695 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001696}
1697
Colin Crossf24a22a2019-01-31 14:12:44 -08001698func (j *Module) DexJar() android.Path {
1699 return j.dexJarFile
1700}
1701
Colin Cross331a1212018-08-15 20:40:52 -07001702func (j *Module) ResourceJars() android.Paths {
1703 if j.resourceJar == nil {
1704 return nil
1705 }
1706 return android.Paths{j.resourceJar}
1707}
1708
1709func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001710 if j.implementationAndResourcesJar == nil {
1711 return nil
1712 }
Colin Cross331a1212018-08-15 20:40:52 -07001713 return android.Paths{j.implementationAndResourcesJar}
1714}
1715
Colin Cross46c9b8b2017-06-22 16:51:17 -07001716func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001717 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001718 return j.exportAidlIncludeDirs
1719}
1720
Jiyong Park1be96912018-05-28 18:02:19 +09001721func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001722 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001723 return j.exportedSdkLibs
1724}
1725
Artur Satayev9cf46692019-11-26 18:08:34 +00001726func (j *Module) ExportedPlugins() (android.Paths, []string) {
1727 return j.exportedPluginJars, j.exportedPluginClasses
1728}
1729
Colin Cross0c4ce212019-05-03 15:28:19 -07001730func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1731 return j.srcJarArgs, j.srcJarDeps
1732}
1733
Colin Cross46c9b8b2017-06-22 16:51:17 -07001734var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001735
Colin Cross46c9b8b2017-06-22 16:51:17 -07001736func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001737 return j.logtagsSrcs
1738}
1739
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001740// Collect information for opening IDE project files in java/jdeps.go.
1741func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1742 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1743 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001744 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001745 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001746 if j.expandJarjarRules != nil {
1747 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001748 }
1749}
1750
1751func (j *Module) CompilerDeps() []string {
1752 jdeps := []string{}
1753 jdeps = append(jdeps, j.properties.Libs...)
1754 jdeps = append(jdeps, j.properties.Static_libs...)
1755 return jdeps
1756}
1757
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001758func (j *Module) hasCode(ctx android.ModuleContext) bool {
1759 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1760 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1761}
1762
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001763func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001764 // Dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Han5e9013b2020-03-10 06:23:13 +09001765 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
1766 return true
1767 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09001768 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001769}
1770
Jiyong Park0b238752019-10-29 11:23:10 +09001771func (j *Module) Stem() string {
1772 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1773}
1774
Jiyong Park618922e2020-01-08 13:35:43 +09001775func (j *Module) JacocoReportClassesFile() android.Path {
1776 return j.jacocoReportClassesFile
1777}
1778
Martin Stjernholm6d415272020-01-31 17:10:36 +00001779func (j *Module) IsInstallable() bool {
1780 return Bool(j.properties.Installable)
1781}
1782
Colin Cross2fe66872015-03-30 17:20:39 -07001783//
1784// Java libraries (.jar file)
1785//
1786
Anton Hansson78156ef2020-03-27 19:39:48 +00001787type LibraryProperties struct {
1788 Dist struct {
1789 // The tag of the output of this module that should be output.
1790 Tag *string `android:"arch_variant"`
1791 } `android:"arch_variant"`
1792}
1793
Colin Crossf506d872017-07-19 15:53:04 -07001794type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001795 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001796
Anton Hansson78156ef2020-03-27 19:39:48 +00001797 libraryProperties LibraryProperties
1798
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001799 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001800}
1801
Colin Cross42be7612019-02-21 18:12:14 -08001802func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001803 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1804 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1805 return true
1806 }
1807
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001808 // Store uncompressed (and do not strip) dex files from boot class path jars.
1809 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1810 return true
1811 }
1812
1813 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001814 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001815 return true
1816 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001817 if ctx.Config().UncompressPrivAppDex() &&
1818 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1819 return true
1820 }
1821
Colin Cross2fc72f62018-12-21 12:59:54 -08001822 return false
1823}
1824
Colin Crossf506d872017-07-19 15:53:04 -07001825func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayev2db1c3f2020-04-08 19:09:30 +01001826 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001827 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001828 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001829 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001830 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001831 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001832
Jiyong Park7f7766d2019-07-25 22:02:35 +09001833 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1834 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001835 var extraInstallDeps android.Paths
1836 if j.InstallMixin != nil {
1837 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1838 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001839 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001840 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001841 }
Anton Hansson78156ef2020-03-27 19:39:48 +00001842
1843 // Verify Dist.Tag is set to a supported output
1844 if j.libraryProperties.Dist.Tag != nil {
1845 distFiles, err := j.OutputFiles(*j.libraryProperties.Dist.Tag)
1846 if err != nil {
1847 ctx.PropertyErrorf("dist.tag", "%s", err.Error())
1848 }
1849 j.distFile = distFiles[0]
1850 }
Colin Crossb7a63242015-04-16 14:09:14 -07001851}
1852
Colin Crossf506d872017-07-19 15:53:04 -07001853func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001854 j.deps(ctx)
1855}
1856
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001857const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001858 aidlIncludeDir = "aidl"
1859 javaDir = "java"
1860 jarFileSuffix = ".jar"
1861 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001862)
1863
Paul Duffina0dbf432019-12-05 11:25:53 +00001864// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffina04c1072020-03-02 10:16:35 +00001865func sdkSnapshotFilePathForJar(osPrefix, name string) string {
1866 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001867}
1868
Paul Duffina04c1072020-03-02 10:16:35 +00001869func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
1870 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001871}
1872
Paul Duffin13879572019-11-28 14:31:38 +00001873type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001874 android.SdkMemberTypeBase
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001875
1876 // Function to retrieve the appropriate output jar (implementation or header) from
1877 // the library.
1878 jarToExportGetter func(j *Library) android.Path
Paul Duffin13879572019-11-28 14:31:38 +00001879}
1880
1881func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1882 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1883}
1884
1885func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1886 _, ok := module.(*Library)
1887 return ok
1888}
1889
Paul Duffin3a4eb502020-03-19 16:11:18 +00001890func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
1891 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00001892}
Paul Duffina0dbf432019-12-05 11:25:53 +00001893
Paul Duffin14eb4672020-03-02 11:33:02 +00001894func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
Paul Duffina551a1c2020-03-17 21:04:24 +00001895 return &librarySdkMemberProperties{}
Paul Duffin14eb4672020-03-02 11:33:02 +00001896}
1897
1898type librarySdkMemberProperties struct {
1899 android.SdkMemberPropertiesBase
1900
Paul Duffina551a1c2020-03-17 21:04:24 +00001901 JarToExport android.Path
1902 AidlIncludeDirs android.Paths
Paul Duffin14eb4672020-03-02 11:33:02 +00001903}
1904
Paul Duffin3a4eb502020-03-19 16:11:18 +00001905func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin13879572019-11-28 14:31:38 +00001906 j := variant.(*Library)
1907
Paul Duffina551a1c2020-03-17 21:04:24 +00001908 p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(j)
1909 p.AidlIncludeDirs = j.AidlIncludeDirs()
Paul Duffin14eb4672020-03-02 11:33:02 +00001910}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001911
Paul Duffin3a4eb502020-03-19 16:11:18 +00001912func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00001913 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00001914
Paul Duffina551a1c2020-03-17 21:04:24 +00001915 exportedJar := p.JarToExport
1916 if exportedJar != nil {
1917 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
Paul Duffin14eb4672020-03-02 11:33:02 +00001918 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
1919
Paul Duffina551a1c2020-03-17 21:04:24 +00001920 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
1921 }
1922
1923 aidlIncludeDirs := p.AidlIncludeDirs
1924 if len(aidlIncludeDirs) != 0 {
1925 sdkModuleContext := ctx.SdkModuleContext()
1926 for _, dir := range aidlIncludeDirs {
Paul Duffin14eb4672020-03-02 11:33:02 +00001927 // TODO(jiyong): copy parcelable declarations only
1928 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1929 for _, file := range aidlFiles {
1930 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1931 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001932 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001933
Paul Duffina551a1c2020-03-17 21:04:24 +00001934 // TODO(b/151933053) - add aidl include dirs property
Paul Duffin14eb4672020-03-02 11:33:02 +00001935 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001936}
1937
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001938var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
1939 android.SdkMemberTypeBase{
1940 PropertyName: "java_header_libs",
1941 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001942 },
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001943 func(j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +00001944 headerJars := j.HeaderJars()
1945 if len(headerJars) != 1 {
1946 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1947 }
1948
1949 return headerJars[0]
Paul Duffinf5c0a9c2020-02-28 14:39:53 +00001950 },
Paul Duffina0dbf432019-12-05 11:25:53 +00001951}
1952
Colin Cross1b16b0e2019-02-12 14:41:32 -08001953// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1954//
1955// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1956// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1957// as a `static_libs` dependency of another module.
1958//
1959// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1960// a device.
1961//
1962// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1963// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001964func LibraryFactory() android.Module {
1965 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001966
Colin Cross9ae1b922018-06-26 17:59:05 -07001967 module.AddProperties(
1968 &module.Module.properties,
1969 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001970 &module.Module.dexpreoptProperties,
Anton Hansson78156ef2020-03-27 19:39:48 +00001971 &module.Module.protoProperties,
1972 &module.libraryProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001973
Jiyong Park7f7766d2019-07-25 22:02:35 +09001974 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001975 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001976 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001977 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001978}
1979
Colin Cross1b16b0e2019-02-12 14:41:32 -08001980// java_library_static is an obsolete alias for java_library.
1981func LibraryStaticFactory() android.Module {
1982 return LibraryFactory()
1983}
1984
1985// java_library_host builds and links sources into a `.jar` file for the host.
1986//
1987// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1988// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001989func LibraryHostFactory() android.Module {
1990 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001991
Colin Cross6af17aa2017-09-20 12:59:05 -07001992 module.AddProperties(
1993 &module.Module.properties,
1994 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001995
Colin Cross9ae1b922018-06-26 17:59:05 -07001996 module.Module.properties.Installable = proptools.BoolPtr(true)
1997
Jiyong Park7f7766d2019-07-25 22:02:35 +09001998 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001999 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07002000 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002001}
2002
2003//
Colin Crossb628ea52018-08-14 16:42:33 -07002004// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002005//
2006
2007type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002008 // list of compatibility suites (for example "cts", "vts") that the module should be
2009 // installed into.
2010 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002011
2012 // the name of the test configuration (for example "AndroidTest.xml") that should be
2013 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002014 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002015
Jack He33338892018-09-19 02:21:28 -07002016 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2017 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002018 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002019
Colin Crossd96ca352018-08-10 16:06:24 -07002020 // list of files or filegroup modules that provide data that should be installed alongside
2021 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002022 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002023
2024 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2025 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2026 // explicitly.
2027 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002028}
2029
Paul Duffin42df1442019-03-20 12:45:53 +00002030type testHelperLibraryProperties struct {
2031 // list of compatibility suites (for example "cts", "vts") that the module should be
2032 // installed into.
2033 Test_suites []string `android:"arch_variant"`
2034}
2035
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002036type prebuiltTestProperties struct {
2037 // list of compatibility suites (for example "cts", "vts") that the module should be
2038 // installed into.
2039 Test_suites []string `android:"arch_variant"`
2040
2041 // the name of the test configuration (for example "AndroidTest.xml") that should be
2042 // installed with the module.
2043 Test_config *string `android:"path,arch_variant"`
2044}
2045
Colin Cross05638fc2018-04-09 18:40:24 -07002046type Test struct {
2047 Library
2048
2049 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002050
2051 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002052 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002053}
2054
Paul Duffin42df1442019-03-20 12:45:53 +00002055type TestHelperLibrary struct {
2056 Library
2057
2058 testHelperLibraryProperties testHelperLibraryProperties
2059}
2060
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002061type JavaTestImport struct {
2062 Import
2063
2064 prebuiltTestProperties prebuiltTestProperties
2065
2066 testConfig android.Path
2067}
2068
Colin Cross303e21f2018-08-07 16:49:25 -07002069func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002070 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2071 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002072 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002073
2074 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002075}
2076
Paul Duffin42df1442019-03-20 12:45:53 +00002077func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2078 j.Library.GenerateAndroidBuildActions(ctx)
2079}
2080
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002081func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2082 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2083 j.prebuiltTestProperties.Test_suites, nil)
2084
2085 j.Import.GenerateAndroidBuildActions(ctx)
2086}
2087
2088type testSdkMemberType struct {
2089 android.SdkMemberTypeBase
2090}
2091
2092func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2093 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2094}
2095
2096func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2097 _, ok := module.(*Test)
2098 return ok
2099}
2100
Paul Duffin3a4eb502020-03-19 16:11:18 +00002101func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
2102 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import")
Paul Duffin14eb4672020-03-02 11:33:02 +00002103}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002104
Paul Duffin14eb4672020-03-02 11:33:02 +00002105func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2106 return &testSdkMemberProperties{}
2107}
2108
2109type testSdkMemberProperties struct {
2110 android.SdkMemberPropertiesBase
2111
Paul Duffina551a1c2020-03-17 21:04:24 +00002112 JarToExport android.Path
2113 TestConfig android.Path
Paul Duffin14eb4672020-03-02 11:33:02 +00002114}
2115
Paul Duffin3a4eb502020-03-19 16:11:18 +00002116func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin14eb4672020-03-02 11:33:02 +00002117 test := variant.(*Test)
2118
2119 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002120 if len(implementationJars) != 1 {
Paul Duffin14eb4672020-03-02 11:33:02 +00002121 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002122 }
2123
Paul Duffina551a1c2020-03-17 21:04:24 +00002124 p.JarToExport = implementationJars[0]
2125 p.TestConfig = test.testConfig
Paul Duffin14eb4672020-03-02 11:33:02 +00002126}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002127
Paul Duffin3a4eb502020-03-19 16:11:18 +00002128func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffina551a1c2020-03-17 21:04:24 +00002129 builder := ctx.SnapshotBuilder()
Paul Duffin3a4eb502020-03-19 16:11:18 +00002130
Paul Duffina551a1c2020-03-17 21:04:24 +00002131 exportedJar := p.JarToExport
2132 if exportedJar != nil {
2133 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), ctx.Name())
2134 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002135
2136 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffina551a1c2020-03-17 21:04:24 +00002137 }
2138
2139 testConfig := p.TestConfig
2140 if testConfig != nil {
2141 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix)
2142 builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath)
Paul Duffin14eb4672020-03-02 11:33:02 +00002143 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
2144 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002145}
2146
Colin Cross1b16b0e2019-02-12 14:41:32 -08002147// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2148// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2149//
2150// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2151// compiled against the device bootclasspath.
2152//
2153// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2154// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002155func TestFactory() android.Module {
2156 module := &Test{}
2157
2158 module.AddProperties(
2159 &module.Module.properties,
2160 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002161 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002162 &module.Module.protoProperties,
2163 &module.testProperties)
2164
Colin Cross9ae1b922018-06-26 17:59:05 -07002165 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002166 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002167
Colin Cross05638fc2018-04-09 18:40:24 -07002168 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002169 return module
2170}
2171
Paul Duffin42df1442019-03-20 12:45:53 +00002172// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2173func TestHelperLibraryFactory() android.Module {
2174 module := &TestHelperLibrary{}
2175
2176 module.AddProperties(
2177 &module.Module.properties,
2178 &module.Module.deviceProperties,
2179 &module.Module.dexpreoptProperties,
2180 &module.Module.protoProperties,
2181 &module.testHelperLibraryProperties)
2182
Colin Cross9a4abed2019-04-24 13:19:28 -07002183 module.Module.properties.Installable = proptools.BoolPtr(true)
2184 module.Module.dexpreopter.isTest = true
2185
Paul Duffin42df1442019-03-20 12:45:53 +00002186 InitJavaModule(module, android.HostAndDeviceSupported)
2187 return module
2188}
2189
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002190// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2191// and makes sure that it is added to the appropriate test suite.
2192//
2193// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2194// compiled against an Android classpath.
2195//
2196// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2197// for host modules.
2198func JavaTestImportFactory() android.Module {
2199 module := &JavaTestImport{}
2200
2201 module.AddProperties(
2202 &module.Import.properties,
2203 &module.prebuiltTestProperties)
2204
2205 module.Import.properties.Installable = proptools.BoolPtr(true)
2206
2207 android.InitPrebuiltModule(module, &module.properties.Jars)
2208 android.InitApexModule(module)
2209 android.InitSdkAwareModule(module)
2210 InitJavaModule(module, android.HostAndDeviceSupported)
2211 return module
2212}
2213
Colin Cross1b16b0e2019-02-12 14:41:32 -08002214// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2215// allow running the test with `atest` or a `TEST_MAPPING` file.
2216//
2217// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2218// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002219func TestHostFactory() android.Module {
2220 module := &Test{}
2221
2222 module.AddProperties(
2223 &module.Module.properties,
2224 &module.Module.protoProperties,
2225 &module.testProperties)
2226
Colin Cross9ae1b922018-06-26 17:59:05 -07002227 module.Module.properties.Installable = proptools.BoolPtr(true)
2228
Colin Cross05638fc2018-04-09 18:40:24 -07002229 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002230 return module
2231}
2232
2233//
Colin Cross2fe66872015-03-30 17:20:39 -07002234// Java Binaries (.jar file plus wrapper script)
2235//
2236
Colin Crossf506d872017-07-19 15:53:04 -07002237type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002238 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002239 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002240
2241 // Name of the class containing main to be inserted into the manifest as Main-Class.
2242 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002243}
2244
Colin Crossf506d872017-07-19 15:53:04 -07002245type Binary struct {
2246 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002247
Colin Crossf506d872017-07-19 15:53:04 -07002248 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002249
Colin Cross6b4a32d2017-12-05 13:42:45 -08002250 isWrapperVariant bool
2251
Colin Crossc3315992017-12-08 19:12:36 -08002252 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002253 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002254}
2255
Alex Light24237172017-10-26 09:46:21 -07002256func (j *Binary) HostToolPath() android.OptionalPath {
2257 return android.OptionalPathForPath(j.binaryFile)
2258}
2259
Colin Crossf506d872017-07-19 15:53:04 -07002260func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002261 if ctx.Arch().ArchType == android.Common {
2262 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002263 if j.binaryProperties.Main_class != nil {
2264 if j.properties.Manifest != nil {
2265 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2266 }
2267 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2268 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2269 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2270 }
2271
Colin Cross6b4a32d2017-12-05 13:42:45 -08002272 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002273 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002274 // Handle the binary wrapper
2275 j.isWrapperVariant = true
2276
Colin Cross366938f2017-12-11 16:29:02 -08002277 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002278 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002279 } else {
2280 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2281 }
2282
2283 // Depend on the installed jar so that the wrapper doesn't get executed by
2284 // another build rule before the jar has been installed.
2285 jarFile := ctx.PrimaryModule().(*Binary).installFile
2286
2287 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2288 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002289 }
Colin Cross2fe66872015-03-30 17:20:39 -07002290}
2291
Colin Crossf506d872017-07-19 15:53:04 -07002292func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002293 if ctx.Arch().ArchType == android.Common {
2294 j.deps(ctx)
2295 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002296}
2297
Colin Cross1b16b0e2019-02-12 14:41:32 -08002298// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2299// as well.
2300//
2301// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2302// compiled against the device bootclasspath.
2303//
2304// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2305// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002306func BinaryFactory() android.Module {
2307 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002308
Colin Cross36242852017-06-23 15:06:31 -07002309 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002310 &module.Module.properties,
2311 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002312 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002313 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002314 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002315
Colin Cross9ae1b922018-06-26 17:59:05 -07002316 module.Module.properties.Installable = proptools.BoolPtr(true)
2317
Colin Cross6b4a32d2017-12-05 13:42:45 -08002318 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2319 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002320 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002321}
2322
Colin Cross1b16b0e2019-02-12 14:41:32 -08002323// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2324//
2325// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2326// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002327func BinaryHostFactory() android.Module {
2328 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002329
Colin Cross36242852017-06-23 15:06:31 -07002330 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002331 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002332 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002333 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002334
Colin Cross9ae1b922018-06-26 17:59:05 -07002335 module.Module.properties.Installable = proptools.BoolPtr(true)
2336
Colin Cross6b4a32d2017-12-05 13:42:45 -08002337 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2338 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002339 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002340}
2341
2342//
2343// Java prebuilts
2344//
2345
Colin Cross74d73e22017-08-02 11:05:49 -07002346type ImportProperties struct {
Paul Duffina04c1072020-03-02 10:16:35 +00002347 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002348
Nan Zhangea568a42017-11-08 21:20:04 -08002349 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002350
2351 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002352
2353 // List of shared java libs that this module has dependencies to
2354 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002355
2356 // List of files to remove from the jar file(s)
2357 Exclude_files []string
2358
2359 // List of directories to remove from the jar file(s)
2360 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002361
2362 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002363 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002364
2365 // set the name of the output
2366 Stem *string
Jiyong Park19604de2020-03-24 16:44:11 +09002367
2368 Aidl struct {
2369 // directories that should be added as include directories for any aidl sources of modules
2370 // that depend on this module, as well as to aidl for this module.
2371 Export_include_dirs []string
2372 }
Colin Cross74d73e22017-08-02 11:05:49 -07002373}
2374
2375type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002376 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002377 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002378 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002379 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002380 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002381
Colin Cross74d73e22017-08-02 11:05:49 -07002382 properties ImportProperties
2383
Colin Cross0a6e0072017-08-30 14:24:55 -07002384 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002385 exportedSdkLibs []string
Jiyong Park19604de2020-03-24 16:44:11 +09002386 exportAidlIncludeDirs android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -07002387}
2388
Jiyong Park6a927c42020-01-21 02:03:43 +09002389func (j *Import) sdkVersion() sdkSpec {
2390 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002391}
2392
Jiyong Park6a927c42020-01-21 02:03:43 +09002393func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002394 return j.sdkVersion()
2395}
2396
Colin Cross74d73e22017-08-02 11:05:49 -07002397func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002398 return &j.prebuilt
2399}
2400
Colin Cross74d73e22017-08-02 11:05:49 -07002401func (j *Import) PrebuiltSrcs() []string {
2402 return j.properties.Jars
2403}
2404
2405func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002406 return j.prebuilt.Name(j.ModuleBase.Name())
2407}
2408
Jiyong Park0b238752019-10-29 11:23:10 +09002409func (j *Import) Stem() string {
2410 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2411}
2412
Jiyong Park618922e2020-01-08 13:35:43 +09002413func (a *Import) JacocoReportClassesFile() android.Path {
2414 return nil
2415}
2416
Colin Cross74d73e22017-08-02 11:05:49 -07002417func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002418 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002419}
2420
Colin Cross74d73e22017-08-02 11:05:49 -07002421func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002422 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002423
Jiyong Park0b238752019-10-29 11:23:10 +09002424 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002425 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002426 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2427 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002428 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002429 inputFile := outputFile
2430 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2431 TransformJetifier(ctx, outputFile, inputFile)
2432 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002433 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002434
2435 ctx.VisitDirectDeps(func(module android.Module) {
2436 otherName := ctx.OtherModuleName(module)
2437 tag := ctx.OtherModuleDependencyTag(module)
2438
2439 switch dep := module.(type) {
2440 case Dependency:
2441 switch tag {
2442 case libTag, staticLibTag:
2443 // sdk lib names from dependencies are re-exported
2444 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2445 }
2446 case SdkLibraryDependency:
2447 switch tag {
2448 case libTag:
2449 // names of sdk libs that are directly depended are exported
2450 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2451 }
2452 }
2453 })
2454
2455 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002456 if Bool(j.properties.Installable) {
2457 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002458 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002459 }
Jiyong Park19604de2020-03-24 16:44:11 +09002460
2461 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs)
Colin Cross2fe66872015-03-30 17:20:39 -07002462}
2463
Colin Cross74d73e22017-08-02 11:05:49 -07002464var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002465
Nan Zhanged19fc32017-10-19 13:06:22 -07002466func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002467 if j.combinedClasspathFile == nil {
2468 return nil
2469 }
Colin Cross37f6d792018-07-12 12:28:41 -07002470 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002471}
2472
2473func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002474 if j.combinedClasspathFile == nil {
2475 return nil
2476 }
Colin Cross37f6d792018-07-12 12:28:41 -07002477 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002478}
2479
Colin Cross331a1212018-08-15 20:40:52 -07002480func (j *Import) ResourceJars() android.Paths {
2481 return nil
2482}
2483
2484func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002485 if j.combinedClasspathFile == nil {
2486 return nil
2487 }
Colin Cross331a1212018-08-15 20:40:52 -07002488 return android.Paths{j.combinedClasspathFile}
2489}
2490
Colin Crossf24a22a2019-01-31 14:12:44 -08002491func (j *Import) DexJar() android.Path {
2492 return nil
2493}
2494
Colin Cross74d73e22017-08-02 11:05:49 -07002495func (j *Import) AidlIncludeDirs() android.Paths {
Jiyong Park19604de2020-03-24 16:44:11 +09002496 return j.exportAidlIncludeDirs
Colin Crossc0b06f12015-04-08 13:03:43 -07002497}
2498
Jiyong Park1be96912018-05-28 18:02:19 +09002499func (j *Import) ExportedSdkLibs() []string {
2500 return j.exportedSdkLibs
2501}
2502
Artur Satayev9cf46692019-11-26 18:08:34 +00002503func (j *Import) ExportedPlugins() (android.Paths, []string) {
2504 return nil, nil
2505}
2506
Colin Cross0c4ce212019-05-03 15:28:19 -07002507func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2508 return nil, nil
2509}
2510
Jiyong Park0f80c182020-01-31 02:49:53 +09002511func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09002512 // dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Han5e9013b2020-03-10 06:23:13 +09002513 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
2514 return true
2515 }
Jooyung Han5e9013b2020-03-10 06:23:13 +09002516 return false
Jiyong Park0f80c182020-01-31 02:49:53 +09002517}
2518
albaltai36ff7dc2018-12-25 14:35:23 +08002519// Add compile time check for interface implementation
2520var _ android.IDEInfo = (*Import)(nil)
2521var _ android.IDECustomizedModuleName = (*Import)(nil)
2522
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002523// Collect information for opening IDE project files in java/jdeps.go.
2524const (
2525 removedPrefix = "prebuilt_"
2526)
2527
2528func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2529 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2530}
2531
2532func (j *Import) IDECustomizedModuleName() string {
2533 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2534 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2535 // solution to get the Import name.
2536 name := j.Name()
2537 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002538 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002539 }
2540 return name
2541}
2542
Colin Cross74d73e22017-08-02 11:05:49 -07002543var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002544
Colin Cross1b16b0e2019-02-12 14:41:32 -08002545// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2546//
2547// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2548// compiled against an Android classpath.
2549//
2550// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2551// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002552func ImportFactory() android.Module {
2553 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002554
Colin Cross74d73e22017-08-02 11:05:49 -07002555 module.AddProperties(&module.properties)
2556
2557 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002558 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002559 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002560 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002561 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002562}
2563
Colin Cross1b16b0e2019-02-12 14:41:32 -08002564// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2565// module.
2566//
2567// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2568// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002569func ImportFactoryHost() android.Module {
2570 module := &Import{}
2571
2572 module.AddProperties(&module.properties)
2573
2574 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002575 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002576 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002577 return module
2578}
2579
Colin Cross42be7612019-02-21 18:12:14 -08002580// dex_import module
2581
2582type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002583 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002584
2585 // set the name of the output
2586 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002587}
2588
2589type DexImport struct {
2590 android.ModuleBase
2591 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002592 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002593 prebuilt android.Prebuilt
2594
2595 properties DexImportProperties
2596
2597 dexJarFile android.Path
2598 maybeStrippedDexJarFile android.Path
2599
2600 dexpreopter
2601}
2602
2603func (j *DexImport) Prebuilt() *android.Prebuilt {
2604 return &j.prebuilt
2605}
2606
2607func (j *DexImport) PrebuiltSrcs() []string {
2608 return j.properties.Jars
2609}
2610
2611func (j *DexImport) Name() string {
2612 return j.prebuilt.Name(j.ModuleBase.Name())
2613}
2614
Jiyong Park0b238752019-10-29 11:23:10 +09002615func (j *DexImport) Stem() string {
2616 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2617}
2618
Martin Stjernholm6d415272020-01-31 17:10:36 +00002619func (j *DexImport) IsInstallable() bool {
2620 return true
2621}
2622
Colin Cross42be7612019-02-21 18:12:14 -08002623func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2624 if len(j.properties.Jars) != 1 {
2625 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2626 }
2627
Jiyong Park0b238752019-10-29 11:23:10 +09002628 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002629 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2630
2631 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2632 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2633
2634 if j.dexpreopter.uncompressedDex {
2635 rule := android.NewRuleBuilder()
2636
2637 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2638 rule.Temporary(temporary)
2639
2640 // use zip2zip to uncompress classes*.dex files
2641 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002642 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002643 FlagWithInput("-i ", inputJar).
2644 FlagWithOutput("-o ", temporary).
2645 FlagWithArg("-0 ", "'classes*.dex'")
2646
2647 // use zipalign to align uncompressed classes*.dex files
2648 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002649 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002650 Flag("-f").
2651 Text("4").
2652 Input(temporary).
2653 Output(dexOutputFile)
2654
2655 rule.DeleteTemporaryFiles()
2656
2657 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2658 } else {
2659 ctx.Build(pctx, android.BuildParams{
2660 Rule: android.Cp,
2661 Input: inputJar,
2662 Output: dexOutputFile,
2663 })
2664 }
2665
2666 j.dexJarFile = dexOutputFile
2667
2668 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2669
2670 j.maybeStrippedDexJarFile = dexOutputFile
2671
2672 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2673 ctx.ModuleName()+".jar", dexOutputFile)
2674}
2675
2676func (j *DexImport) DexJar() android.Path {
2677 return j.dexJarFile
2678}
2679
2680// dex_import imports a `.jar` file containing classes.dex files.
2681//
2682// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2683// to the device.
2684func DexImportFactory() android.Module {
2685 module := &DexImport{}
2686
2687 module.AddProperties(&module.properties)
2688
2689 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002690 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002691 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002692 return module
2693}
2694
Colin Cross89536d42017-07-07 14:35:50 -07002695//
2696// Defaults
2697//
2698type Defaults struct {
2699 android.ModuleBase
2700 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002701 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002702}
2703
Colin Cross1b16b0e2019-02-12 14:41:32 -08002704// java_defaults provides a set of properties that can be inherited by other java or android modules.
2705//
2706// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2707// property in the defaults module that exists in the depending module will be prepended to the depending module's
2708// value for that property.
2709//
2710// Example:
2711//
2712// java_defaults {
2713// name: "example_defaults",
2714// srcs: ["common/**/*.java"],
2715// javacflags: ["-Xlint:all"],
2716// aaptflags: ["--auto-add-overlay"],
2717// }
2718//
2719// java_library {
2720// name: "example",
2721// defaults: ["example_defaults"],
2722// srcs: ["example/**/*.java"],
2723// }
2724//
2725// is functionally identical to:
2726//
2727// java_library {
2728// name: "example",
2729// srcs: [
2730// "common/**/*.java",
2731// "example/**/*.java",
2732// ],
2733// javacflags: ["-Xlint:all"],
2734// }
Colin Cross89536d42017-07-07 14:35:50 -07002735func defaultsFactory() android.Module {
2736 return DefaultsFactory()
2737}
2738
Paul Duffin47357662019-12-05 14:07:14 +00002739func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002740 module := &Defaults{}
2741
Colin Cross89536d42017-07-07 14:35:50 -07002742 module.AddProperties(
2743 &CompilerProperties{},
2744 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002745 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002746 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002747 &aaptProperties{},
2748 &androidLibraryProperties{},
2749 &appProperties{},
2750 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002751 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002752 &ImportProperties{},
2753 &AARImportProperties{},
2754 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002755 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002756 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002757 )
2758
2759 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002760 return module
2761}
Nan Zhangea568a42017-11-08 21:20:04 -08002762
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002763func kytheExtractJavaFactory() android.Singleton {
2764 return &kytheExtractJavaSingleton{}
2765}
2766
2767type kytheExtractJavaSingleton struct {
2768}
2769
2770func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2771 var xrefTargets android.Paths
2772 ctx.VisitAllModules(func(module android.Module) {
2773 if javaModule, ok := module.(xref); ok {
2774 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2775 }
2776 })
2777 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2778 if len(xrefTargets) > 0 {
2779 ctx.Build(pctx, android.BuildParams{
2780 Rule: blueprint.Phony,
2781 Output: android.PathForPhony(ctx, "xref_java"),
2782 Inputs: xrefTargets,
2783 })
2784 }
2785}
2786
Nan Zhangea568a42017-11-08 21:20:04 -08002787var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002788var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002789var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002790var inList = android.InList