blob: e3fa69d2648037b7da5190f2838c9fb9ae97f576 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Paul Duffinf9b1da02019-12-18 19:51:55 +000037 RegisterJavaBuildComponents(android.InitRegistrationContext)
Paul Duffin255f18e2019-12-13 11:22:16 +000038
39 // Register sdk member types.
Paul Duffin7b81f5e2020-01-13 21:03:22 +000040 android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType)
Paul Duffin255f18e2019-12-13 11:22:16 +000041
Paul Duffin2b9e3d32020-02-28 14:39:53 +000042 android.RegisterSdkMemberType(&librarySdkMemberType{
43 android.SdkMemberTypeBase{
44 PropertyName: "java_libs",
45 },
46 func(j *Library) android.Path {
47 implementationJars := j.ImplementationJars()
48 if len(implementationJars) != 1 {
49 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
50 }
51
52 return implementationJars[0]
Paul Duffin255f18e2019-12-13 11:22:16 +000053 },
54 })
Paul Duffin1b82e6a2019-12-03 18:06:47 +000055
56 android.RegisterSdkMemberType(&testSdkMemberType{
57 SdkMemberTypeBase: android.SdkMemberTypeBase{
58 PropertyName: "java_tests",
59 },
60 })
Colin Cross463a90e2015-06-17 14:20:06 -070061}
62
Paul Duffinf9b1da02019-12-18 19:51:55 +000063func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
64 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
65
66 ctx.RegisterModuleType("java_library", LibraryFactory)
67 ctx.RegisterModuleType("java_library_static", LibraryStaticFactory)
68 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
69 ctx.RegisterModuleType("java_binary", BinaryFactory)
70 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
71 ctx.RegisterModuleType("java_test", TestFactory)
72 ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
73 ctx.RegisterModuleType("java_test_host", TestHostFactory)
Paul Duffin1b82e6a2019-12-03 18:06:47 +000074 ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
Paul Duffinf9b1da02019-12-18 19:51:55 +000075 ctx.RegisterModuleType("java_import", ImportFactory)
76 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
77 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
78 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
79 ctx.RegisterModuleType("dex_import", DexImportFactory)
80
Martin Stjernholm6d415272020-01-31 17:10:36 +000081 ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
82 ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel()
83 })
Martin Stjernholmd90676f2020-01-11 00:37:30 +000084
Paul Duffinf9b1da02019-12-18 19:51:55 +000085 ctx.RegisterSingletonType("logtags", LogtagsSingleton)
86 ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
87}
88
Artur Satayeve5ac15a2020-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 Satayeve5ac15a2020-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 Fan688de9a2020-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 Hansson1e65f942020-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 Jung37ca4a12020-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 Hanssonbbd78552020-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 Satayeve5ac15a2020-04-08 19:09:30 +0100913
Colin Cross54250902017-12-05 09:28:08 -0800914 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800915 case SdkLibraryDependency:
916 switch tag {
917 case libTag:
918 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
919 // names of sdk libs that are directly depended are exported
920 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700921 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800922 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
923 }
Colin Cross54250902017-12-05 09:28:08 -0800924 case Dependency:
925 switch tag {
926 case bootClasspathTag:
927 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700928 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800929 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900930 // sdk lib names from dependencies are re-exported
931 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700932 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000933 pluginJars, pluginClasses := dep.ExportedPlugins()
934 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700935 case java9LibTag:
936 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800937 case staticLibTag:
938 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
939 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
940 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700941 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900942 // sdk lib names from dependencies are re-exported
943 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700944 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000945 pluginJars, pluginClasses := dep.ExportedPlugins()
946 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800947 case pluginTag:
948 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800949 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000950 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
951 } else {
952 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800953 }
954 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
955 } else {
956 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
957 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000958 case exportedPluginTag:
959 if plugin, ok := dep.(*Plugin); ok {
960 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
961 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
962 }
963 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
964 if plugin.pluginProperties.Processor_class != nil {
965 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
966 }
967 } else {
968 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
969 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800970 case frameworkApkTag:
971 if ctx.ModuleName() == "android_stubs_current" ||
972 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700973 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800974 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
975 // resource files out of there for aapt.
976 //
977 // Normally the package rule runs aapt, which includes the resource,
978 // but we're not running that in our package rule so just copy in the
979 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700980 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800981 }
Colin Cross54250902017-12-05 09:28:08 -0800982 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700983 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800984 case kotlinAnnotationsTag:
985 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800986 }
987
Colin Cross54250902017-12-05 09:28:08 -0800988 case android.SourceFileProducer:
989 switch tag {
990 case libTag:
991 checkProducesJars(ctx, dep)
992 deps.classpath = append(deps.classpath, dep.Srcs()...)
993 case staticLibTag:
994 checkProducesJars(ctx, dep)
995 deps.classpath = append(deps.classpath, dep.Srcs()...)
996 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
997 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800998 }
999 default:
Colin Crossec7a0422017-07-07 14:47:12 -07001000 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +01001001 case bootClasspathTag:
1002 // If a system modules dependency has been added to the bootclasspath
1003 // then add its libs to the bootclasspath.
Paul Duffin83a2d962019-11-19 19:44:10 +00001004 sm := module.(SystemModulesProvider)
1005 deps.bootClasspath = append(deps.bootClasspath, sm.HeaderJars()...)
Paul Duffin68289b02019-09-20 13:50:52 +01001006
Colin Cross1369cdb2017-09-29 17:58:17 -07001007 case systemModulesTag:
1008 if deps.systemModules != nil {
1009 panic("Found two system module dependencies")
1010 }
Paul Duffin83a2d962019-11-19 19:44:10 +00001011 sm := module.(SystemModulesProvider)
1012 outputDir, outputDeps := sm.OutputDirAndDeps()
1013 deps.systemModules = &systemModules{outputDir, outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -07001014 }
Colin Crossec7a0422017-07-07 14:47:12 -07001015 }
Colin Cross2fe66872015-03-30 17:20:39 -07001016 })
1017
Jiyong Park1be96912018-05-28 18:02:19 +09001018 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
1019
Colin Cross32f676a2017-09-06 13:41:06 -07001020 return deps
Colin Cross2fe66872015-03-30 17:20:39 -07001021}
1022
Artur Satayev9cf46692019-11-26 18:08:34 +00001023func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
1024 deps.processorPath = append(deps.processorPath, pluginJars...)
1025 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
1026}
1027
Colin Cross1e743852019-10-28 11:37:20 -07001028func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Jiyong Park6a927c42020-01-21 02:03:43 +09001029 sdk, err := sdkContext.sdkVersion().effectiveVersion(ctx)
Colin Cross83bb3162018-06-25 15:48:06 -07001030 if err != nil {
1031 ctx.PropertyErrorf("sdk_version", "%s", err)
1032 }
Nan Zhang357466b2018-04-17 17:38:36 -07001033 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -07001034 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -07001035 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -07001036 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +01001037 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -07001038 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -07001039 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
1040 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -07001041 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -07001042 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001043 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001044 }
Nan Zhang357466b2018-04-17 17:38:36 -07001045}
1046
Colin Cross1e743852019-10-28 11:37:20 -07001047type javaVersion int
1048
1049const (
1050 JAVA_VERSION_UNSUPPORTED = 0
1051 JAVA_VERSION_6 = 6
1052 JAVA_VERSION_7 = 7
1053 JAVA_VERSION_8 = 8
1054 JAVA_VERSION_9 = 9
1055)
1056
1057func (v javaVersion) String() string {
1058 switch v {
1059 case JAVA_VERSION_6:
1060 return "1.6"
1061 case JAVA_VERSION_7:
1062 return "1.7"
1063 case JAVA_VERSION_8:
1064 return "1.8"
1065 case JAVA_VERSION_9:
1066 return "1.9"
1067 default:
1068 return "unsupported"
1069 }
1070}
1071
1072// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1073func (v javaVersion) usesJavaModules() bool {
1074 return v >= 9
1075}
1076
1077func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001078 switch javaVersion {
1079 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001080 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001081 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001082 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001083 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001084 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001085 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001086 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001087 case "10", "11":
1088 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001089 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001090 default:
1091 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001092 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001093 }
1094}
1095
Nan Zhanged19fc32017-10-19 13:06:22 -07001096func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001097
Colin Crossf03c82b2015-04-13 13:53:40 -07001098 var flags javaBuilderFlags
1099
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001100 // javaVersion flag.
1101 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1102
Nan Zhanged19fc32017-10-19 13:06:22 -07001103 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001104 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001105 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001106 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001107 }
Colin Cross6510f912017-11-29 00:27:14 -08001108 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001109 // Override the -g flag passed globally to remove local variable debug info to reduce
1110 // disk and memory usage.
1111 javacFlags = append(javacFlags, "-g:source,lines")
1112 }
Colin Crossc228a702019-11-06 16:18:05 -08001113 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001114
Colin Cross66548102018-06-19 22:47:35 -07001115 if ctx.Config().RunErrorProne() {
1116 if config.ErrorProneClasspath == nil {
1117 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1118 }
1119
1120 errorProneFlags := []string{
1121 "-Xplugin:ErrorProne",
1122 "${config.ErrorProneChecks}",
1123 }
1124 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1125
1126 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1127 "'" + strings.Join(errorProneFlags, " ") + "'"
1128 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001129 }
1130
Nan Zhanged19fc32017-10-19 13:06:22 -07001131 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001132 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1133 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001134 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001135 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001136
Colin Crossbe9cdb82019-01-21 21:37:16 -08001137 flags.processor = strings.Join(deps.processorClasses, ",")
1138
Colin Cross1e743852019-10-28 11:37:20 -07001139 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1140 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001141 // Give host-side tools a version of OpenJDK's standard libraries
1142 // close to what they're targeting. As of Dec 2017, AOSP is only
1143 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1144 //
1145 // When building with OpenJDK 8, the following should have no
1146 // effect since those jars would be available by default.
1147 //
1148 // When building with OpenJDK 9 but targeting a version < 1.8,
1149 // putting them on the bootclasspath means that:
1150 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1151 // b) references to existing APIs are not reinterpreted in an
1152 // OpenJDK 9-specific way, eg. calls to subclasses of
1153 // java.nio.Buffer as in http://b/70862583
1154 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1155 flags.bootClasspath = append(flags.bootClasspath,
1156 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1157 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001158 if Bool(j.properties.Use_tools_jar) {
1159 flags.bootClasspath = append(flags.bootClasspath,
1160 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1161 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001162 }
1163
Colin Cross1e743852019-10-28 11:37:20 -07001164 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001165 // Manually specify build directory in case it is not under the repo root.
1166 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1167 // just adding a symlink under the root doesn't help.)
1168 patchPaths := ".:" + ctx.Config().BuildDir()
1169 classPath := flags.classpath.FormJavaClassPath("")
1170 if classPath != "" {
1171 patchPaths += ":" + classPath
1172 }
1173 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001174 }
1175
Nan Zhanged19fc32017-10-19 13:06:22 -07001176 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001177 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001178
Nan Zhanged19fc32017-10-19 13:06:22 -07001179 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001180 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001181
Colin Cross81440082018-08-15 20:21:55 -07001182 if len(javacFlags) > 0 {
1183 // optimization.
1184 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1185 flags.javacFlags = "$javacFlags"
1186 }
1187
Nan Zhanged19fc32017-10-19 13:06:22 -07001188 return flags
1189}
Colin Crossc0b06f12015-04-08 13:03:43 -07001190
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001191func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001192 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001193
1194 deps := j.collectDeps(ctx)
1195 flags := j.collectBuilderFlags(ctx, deps)
1196
Colin Cross1e743852019-10-28 11:37:20 -07001197 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001198 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1199 }
Colin Cross8a497952019-03-05 22:25:09 -08001200 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001201 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001202 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001203 }
1204
Colin Crossaf050172017-11-15 23:01:59 -08001205 srcFiles = j.genSources(ctx, srcFiles, flags)
1206
1207 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001208 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001209 if aaptSrcJar != nil {
1210 srcJars = append(srcJars, aaptSrcJar)
1211 }
Colin Crossb7a63242015-04-16 14:09:14 -07001212
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001213 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001214 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001215 }
1216
Colin Cross1ee23172017-10-18 14:44:18 -07001217 jarName := ctx.ModuleName() + ".jar"
1218
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001219 javaSrcFiles := srcFiles.FilterByExt(".java")
1220 var uniqueSrcFiles android.Paths
1221 set := make(map[string]bool)
1222 for _, v := range javaSrcFiles {
1223 if _, found := set[v.String()]; !found {
1224 set[v.String()] = true
1225 uniqueSrcFiles = append(uniqueSrcFiles, v)
1226 }
1227 }
1228
patricktu242faad2019-09-24 15:41:30 +08001229 // Collect .java files for AIDEGen
1230 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1231
Colin Cross55f63ea2018-08-27 12:37:09 -07001232 var kotlinJars android.Paths
1233
Colin Cross93e85952017-08-15 13:34:18 -07001234 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001235 // user defined kotlin flags.
1236 kotlincFlags := j.properties.Kotlincflags
1237 CheckKotlincFlags(ctx, kotlincFlags)
1238
Colin Cross93e85952017-08-15 13:34:18 -07001239 // If there are kotlin files, compile them first but pass all the kotlin and java files
1240 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1241 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001242 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001243 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001244 kotlincFlags = append(kotlincFlags, "-no-jdk")
1245 }
1246 if len(kotlincFlags) > 0 {
1247 // optimization.
1248 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1249 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001250 }
1251
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001252 var kotlinSrcFiles android.Paths
1253 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1254 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1255
patricktu242faad2019-09-24 15:41:30 +08001256 // Collect .kt files for AIDEGen
1257 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1258
Colin Crossafbb1732019-01-17 15:42:52 -08001259 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1260 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1261
1262 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1263 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1264
1265 if len(flags.processorPath) > 0 {
1266 // Use kapt for annotation processing
1267 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1268 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1269 srcJars = append(srcJars, kaptSrcJar)
1270 // Disable annotation processing in javac, it's already been handled by kapt
1271 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001272 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001273 }
Colin Cross93e85952017-08-15 13:34:18 -07001274
Colin Cross1ee23172017-10-18 14:44:18 -07001275 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001276 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001277 if ctx.Failed() {
1278 return
1279 }
1280
1281 // Make javac rule depend on the kotlinc rule
1282 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001283
Colin Cross93e85952017-08-15 13:34:18 -07001284 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001285 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001286 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001287 }
1288
Colin Cross55f63ea2018-08-27 12:37:09 -07001289 jars := append(android.Paths(nil), kotlinJars...)
1290
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001291 // Store the list of .java files that was passed to javac
1292 j.compiledJavaSrcs = uniqueSrcFiles
1293 j.compiledSrcJars = srcJars
1294
Nan Zhang61eaedb2017-11-02 13:28:15 -07001295 enable_sharding := false
Colin Crossf5a66282020-02-21 08:16:41 -08001296 var headerJarFileWithoutJarjar android.Path
Colin Crossbe9cdb82019-01-21 21:37:16 -08001297 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001298 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1299 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001300 // Formerly, there was a check here that prevented annotation processors
1301 // from being used when sharding was enabled, as some annotation processors
1302 // do not function correctly in sharded environments. It was removed to
1303 // allow for the use of annotation processors that do function correctly
1304 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001305 }
Colin Crossf5a66282020-02-21 08:16:41 -08001306 headerJarFileWithoutJarjar, j.headerJarFile =
1307 j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001308 if ctx.Failed() {
1309 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001310 }
1311 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001312 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001313 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001314 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001315 // If error-prone is enabled, add an additional rule to compile the java files into
1316 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001317 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001318 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1319 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001320 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001321 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001322 extraJarDeps = append(extraJarDeps, errorprone)
1323 }
1324
Nan Zhang61eaedb2017-11-02 13:28:15 -07001325 if enable_sharding {
Colin Crossf5a66282020-02-21 08:16:41 -08001326 flags.classpath = append(flags.classpath, headerJarFileWithoutJarjar)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001327 shardSize := int(*(j.properties.Javac_shard_size))
1328 var shardSrcs []android.Paths
1329 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001330 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001331 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001332 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1333 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001334 jars = append(jars, classes)
1335 }
1336 }
1337 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001338 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1339 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001340 jars = append(jars, classes)
1341 }
1342 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001343 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001344 jars = append(jars, classes)
1345 }
Colin Crossd6891432017-09-27 17:39:56 -07001346 if ctx.Failed() {
1347 return
1348 }
Colin Cross2fe66872015-03-30 17:20:39 -07001349 }
1350
Colin Cross0c4ce212019-05-03 15:28:19 -07001351 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1352
1353 var includeSrcJar android.WritablePath
1354 if Bool(j.properties.Include_srcs) {
1355 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1356 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1357 }
1358
Colin Crosscedd4762018-09-13 11:26:19 -07001359 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1360 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001361 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001362 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001363
1364 var resArgs []string
1365 var resDeps android.Paths
1366
1367 resArgs = append(resArgs, dirArgs...)
1368 resDeps = append(resDeps, dirDeps...)
1369
1370 resArgs = append(resArgs, fileArgs...)
1371 resDeps = append(resDeps, fileDeps...)
1372
Colin Cross988708c2019-05-06 14:04:11 -07001373 resArgs = append(resArgs, extraArgs...)
1374 resDeps = append(resDeps, extraDeps...)
1375
Colin Cross40a36712017-09-27 17:41:35 -07001376 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001377 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001378 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001379 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001380 if ctx.Failed() {
1381 return
1382 }
1383 }
1384
Colin Cross0c4ce212019-05-03 15:28:19 -07001385 var resourceJars android.Paths
1386 if j.resourceJar != nil {
1387 resourceJars = append(resourceJars, j.resourceJar)
1388 }
1389 if Bool(j.properties.Include_srcs) {
1390 resourceJars = append(resourceJars, includeSrcJar)
1391 }
1392 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001393
Colin Cross0c4ce212019-05-03 15:28:19 -07001394 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001395 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001396 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001397 false, nil, nil)
1398 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001399 } else if len(resourceJars) == 1 {
1400 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001401 }
1402
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001403 if len(deps.staticJars) > 0 {
1404 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001405 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001406
Colin Cross094054a2018-10-17 15:10:48 -07001407 manifest := j.overrideManifest
1408 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001409 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001410 }
Colin Cross635acc92017-09-12 22:50:46 -07001411
Colin Cross8a497952019-03-05 22:25:09 -08001412 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001413 if len(services) > 0 {
1414 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1415 var zipargs []string
1416 for _, file := range services {
1417 serviceFile := file.String()
1418 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1419 }
1420 ctx.Build(pctx, android.BuildParams{
1421 Rule: zip,
1422 Output: servicesJar,
1423 Implicits: services,
1424 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001425 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001426 },
1427 })
1428 jars = append(jars, servicesJar)
1429 }
1430
Colin Cross0a6e0072017-08-30 14:24:55 -07001431 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001432 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001433 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001434
1435 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001436 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1437 // Optimization: skip the combine step if there is nothing to do
1438 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1439 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1440 // any if len(jars) == 1.
1441 outputFile = moduleOutPath
1442 } else {
1443 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1444 ctx.Build(pctx, android.BuildParams{
1445 Rule: android.Cp,
1446 Input: jars[0],
1447 Output: combinedJar,
1448 })
1449 outputFile = combinedJar
1450 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001451 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001452 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001453 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001454 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001455 outputFile = combinedJar
1456 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001457
Colin Cross331a1212018-08-15 20:40:52 -07001458 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001459 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001460 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001461 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001462 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001463 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001464
1465 // jarjar resource jar if necessary
1466 if j.resourceJar != nil {
1467 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001468 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001469 j.resourceJar = resourceJarJarFile
1470 }
1471
Colin Cross0a6e0072017-08-30 14:24:55 -07001472 if ctx.Failed() {
1473 return
1474 }
1475 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001476
1477 // Check package restrictions if necessary.
1478 if len(j.properties.Permitted_packages) > 0 {
1479 // Check packages and copy to package-checked file.
1480 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1481 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1482 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1483
1484 if ctx.Failed() {
1485 return
1486 }
1487 }
1488
Nan Zhanged19fc32017-10-19 13:06:22 -07001489 j.implementationJarFile = outputFile
1490 if j.headerJarFile == nil {
1491 j.headerJarFile = j.implementationJarFile
1492 }
Colin Cross2fe66872015-03-30 17:20:39 -07001493
Jiyong Park28826602020-02-21 16:04:53 +09001494 // Force enable the instrumentation for java code that is built for APEXes ...
1495 // except for the jacocoagent itself (because instrumenting jacocoagent using jacocoagent
1496 // doesn't make sense)
1497 isJacocoAgent := ctx.ModuleName() == "jacocoagent"
1498 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !isJacocoAgent && !j.IsForPlatform() {
Jiyong Park81aaa0c2020-02-18 12:50:44 +00001499 j.properties.Instrument = true
1500 }
1501
Colin Cross3144dfc2018-01-03 15:06:47 -08001502 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001503 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1504 }
1505
Colin Cross331a1212018-08-15 20:40:52 -07001506 // merge implementation jar with resources if necessary
1507 implementationAndResourcesJar := outputFile
1508 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001509 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001510 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001511 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001512 false, nil, nil)
1513 implementationAndResourcesJar = combinedJar
1514 }
1515
1516 j.implementationAndResourcesJar = implementationAndResourcesJar
1517
Jiyong Park6b21c7d2020-02-11 09:16:01 +09001518 // Enable dex compilation for the APEX variants, unless it is disabled explicitly
1519 if android.DirectlyInAnyApex(ctx, ctx.ModuleName()) && !j.IsForPlatform() {
1520 if j.deviceProperties.Compile_dex == nil {
1521 j.deviceProperties.Compile_dex = proptools.BoolPtr(true)
1522 }
1523 if j.deviceProperties.Hostdex == nil {
1524 j.deviceProperties.Hostdex = proptools.BoolPtr(true)
1525 }
1526 }
1527
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001528 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001529 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001530 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001531 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001532 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001533 if ctx.Failed() {
1534 return
1535 }
Colin Cross331a1212018-08-15 20:40:52 -07001536
Jiyong Park09cb6292019-07-15 15:29:23 +09001537 // Hidden API CSV generation and dex encoding
1538 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1539 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001540
Colin Cross331a1212018-08-15 20:40:52 -07001541 // merge dex jar with resources if necessary
1542 if j.resourceJar != nil {
1543 jars := android.Paths{dexOutputFile, j.resourceJar}
1544 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1545 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1546 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001547 if j.deviceProperties.UncompressDex {
1548 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1549 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1550 dexOutputFile = combinedAlignedJar
1551 } else {
1552 dexOutputFile = combinedJar
1553 }
Colin Cross331a1212018-08-15 20:40:52 -07001554 }
1555
1556 j.dexJarFile = dexOutputFile
1557
Colin Cross8faf8fc2019-01-16 15:15:52 -08001558 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001559 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1560
1561 j.maybeStrippedDexJarFile = dexOutputFile
1562
Colin Cross3063b782018-08-15 11:19:12 -07001563 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001564
1565 if ctx.Failed() {
1566 return
1567 }
Colin Cross331a1212018-08-15 20:40:52 -07001568 } else {
1569 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001570 }
Colin Cross331a1212018-08-15 20:40:52 -07001571
Colin Crossb7a63242015-04-16 14:09:14 -07001572 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001573
1574 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1575 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001576}
1577
Colin Cross3b706fd2019-09-05 16:44:18 -07001578func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1579 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1580
1581 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1582 if idx >= 0 {
1583 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1584 jarName += strconv.Itoa(idx)
1585 }
1586
1587 classes := android.PathForModuleOut(ctx, "javac", jarName)
1588 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1589
1590 if ctx.Config().EmitXrefRules() {
1591 extractionFile := android.PathForModuleOut(ctx, kzipName)
1592 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1593 j.kytheFiles = append(j.kytheFiles, extractionFile)
1594 }
1595
1596 return classes
1597}
1598
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001599// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1600// since some of these flags may be used internally.
1601func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1602 for _, flag := range flags {
1603 flag = strings.TrimSpace(flag)
1604
1605 if !strings.HasPrefix(flag, "-") {
1606 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1607 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1608 ctx.PropertyErrorf("kotlincflags",
1609 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1610 } else if inList(flag, config.KotlincIllegalFlags) {
1611 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1612 } else if flag == "-include-runtime" {
1613 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1614 } else {
1615 args := strings.Split(flag, " ")
1616 if args[0] == "-kotlin-home" {
1617 ctx.PropertyErrorf("kotlincflags",
1618 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1619 }
1620 }
1621 }
1622}
1623
Colin Cross8eadbf02017-10-24 17:46:00 -07001624func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Crossf5a66282020-02-21 08:16:41 -08001625 deps deps, flags javaBuilderFlags, jarName string,
1626 extraJars android.Paths) (headerJar, jarjarHeaderJar android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001627
1628 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001629 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001630 // Compile java sources into turbine.jar.
1631 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1632 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1633 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001634 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001635 }
1636 jars = append(jars, turbineJar)
1637 }
1638
Colin Cross55f63ea2018-08-27 12:37:09 -07001639 jars = append(jars, extraJars...)
1640
Nan Zhanged19fc32017-10-19 13:06:22 -07001641 // Combine any static header libraries into classes-header.jar. If there is only
1642 // one input jar this step will be skipped.
Nan Zhanged19fc32017-10-19 13:06:22 -07001643 jars = append(jars, deps.staticHeaderJars...)
1644
Colin Cross5c6ecc12017-10-23 18:12:27 -07001645 // we cannot skip the combine step for now if there is only one jar
1646 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1647 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001648 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001649 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001650 headerJar = combinedJar
Colin Crossf5a66282020-02-21 08:16:41 -08001651 jarjarHeaderJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001652
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001653 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001654 // Transform classes.jar into classes-jarjar.jar
1655 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001656 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Colin Crossf5a66282020-02-21 08:16:41 -08001657 jarjarHeaderJar = jarjarFile
Nan Zhanged19fc32017-10-19 13:06:22 -07001658 if ctx.Failed() {
Colin Crossf5a66282020-02-21 08:16:41 -08001659 return nil, nil
Nan Zhanged19fc32017-10-19 13:06:22 -07001660 }
1661 }
1662
Colin Crossf5a66282020-02-21 08:16:41 -08001663 return headerJar, jarjarHeaderJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001664}
1665
Colin Crosscb933592017-11-22 13:49:43 -08001666func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001667 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001668
Colin Cross7a3139e2017-12-19 13:57:50 -08001669 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001670
Colin Cross84c38822018-01-03 15:59:46 -08001671 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001672 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1673
1674 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1675
1676 j.jacocoReportClassesFile = jacocoReportClassesFile
1677
1678 return instrumentedJar
1679}
1680
albaltai36ff7dc2018-12-25 14:35:23 +08001681var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001682
Nan Zhanged19fc32017-10-19 13:06:22 -07001683func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001684 if j.headerJarFile == nil {
1685 return nil
1686 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001687 return android.Paths{j.headerJarFile}
1688}
1689
1690func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001691 if j.implementationJarFile == nil {
1692 return nil
1693 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001694 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001695}
1696
Colin Crossf24a22a2019-01-31 14:12:44 -08001697func (j *Module) DexJar() android.Path {
1698 return j.dexJarFile
1699}
1700
Colin Cross331a1212018-08-15 20:40:52 -07001701func (j *Module) ResourceJars() android.Paths {
1702 if j.resourceJar == nil {
1703 return nil
1704 }
1705 return android.Paths{j.resourceJar}
1706}
1707
1708func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001709 if j.implementationAndResourcesJar == nil {
1710 return nil
1711 }
Colin Cross331a1212018-08-15 20:40:52 -07001712 return android.Paths{j.implementationAndResourcesJar}
1713}
1714
Colin Cross46c9b8b2017-06-22 16:51:17 -07001715func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001716 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001717 return j.exportAidlIncludeDirs
1718}
1719
Jiyong Park1be96912018-05-28 18:02:19 +09001720func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001721 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001722 return j.exportedSdkLibs
1723}
1724
Artur Satayev9cf46692019-11-26 18:08:34 +00001725func (j *Module) ExportedPlugins() (android.Paths, []string) {
1726 return j.exportedPluginJars, j.exportedPluginClasses
1727}
1728
Colin Cross0c4ce212019-05-03 15:28:19 -07001729func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1730 return j.srcJarArgs, j.srcJarDeps
1731}
1732
Colin Cross46c9b8b2017-06-22 16:51:17 -07001733var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001734
Colin Cross46c9b8b2017-06-22 16:51:17 -07001735func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001736 return j.logtagsSrcs
1737}
1738
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001739// Collect information for opening IDE project files in java/jdeps.go.
1740func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1741 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1742 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001743 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001744 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001745 if j.expandJarjarRules != nil {
1746 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001747 }
1748}
1749
1750func (j *Module) CompilerDeps() []string {
1751 jdeps := []string{}
1752 jdeps = append(jdeps, j.properties.Libs...)
1753 jdeps = append(jdeps, j.properties.Static_libs...)
1754 return jdeps
1755}
1756
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001757func (j *Module) hasCode(ctx android.ModuleContext) bool {
1758 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1759 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1760}
1761
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001762func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09001763 // Dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001764 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
1765 return true
1766 }
Jiyong Park0f80c182020-01-31 02:49:53 +09001767 // Also, a dependency to an sdk member is also considered as such. This is required because
1768 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09001769 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
1770 return true
1771 }
1772 return false
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001773}
1774
Jiyong Park0b238752019-10-29 11:23:10 +09001775func (j *Module) Stem() string {
1776 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1777}
1778
Jiyong Park618922e2020-01-08 13:35:43 +09001779func (j *Module) JacocoReportClassesFile() android.Path {
1780 return j.jacocoReportClassesFile
1781}
1782
Martin Stjernholm6d415272020-01-31 17:10:36 +00001783func (j *Module) IsInstallable() bool {
1784 return Bool(j.properties.Installable)
1785}
1786
Colin Cross2fe66872015-03-30 17:20:39 -07001787//
1788// Java libraries (.jar file)
1789//
1790
Anton Hansson1e65f942020-03-27 19:39:48 +00001791type LibraryProperties struct {
1792 Dist struct {
1793 // The tag of the output of this module that should be output.
1794 Tag *string `android:"arch_variant"`
1795 } `android:"arch_variant"`
1796}
1797
Colin Crossf506d872017-07-19 15:53:04 -07001798type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001799 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001800
Anton Hansson1e65f942020-03-27 19:39:48 +00001801 libraryProperties LibraryProperties
1802
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001803 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001804}
1805
Colin Cross42be7612019-02-21 18:12:14 -08001806func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Ulya Trafimovichf491dde2020-01-24 12:19:45 +00001807 // Store uncompressed (and aligned) any dex files from jars in APEXes.
1808 if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() {
1809 return true
1810 }
1811
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001812 // Store uncompressed (and do not strip) dex files from boot class path jars.
1813 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1814 return true
1815 }
1816
1817 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001818 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001819 return true
1820 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001821 if ctx.Config().UncompressPrivAppDex() &&
1822 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1823 return true
1824 }
1825
Colin Cross2fc72f62018-12-21 12:59:54 -08001826 return false
1827}
1828
Colin Crossf506d872017-07-19 15:53:04 -07001829func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Artur Satayeve5ac15a2020-04-08 19:09:30 +01001830 j.checkSdkVersions(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001831 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001832 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross42be7612019-02-21 18:12:14 -08001833 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001834 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001835 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001836
Jiyong Park7f7766d2019-07-25 22:02:35 +09001837 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1838 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001839 var extraInstallDeps android.Paths
1840 if j.InstallMixin != nil {
1841 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1842 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001843 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001844 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001845 }
Anton Hansson1e65f942020-03-27 19:39:48 +00001846
1847 // Verify Dist.Tag is set to a supported output
1848 if j.libraryProperties.Dist.Tag != nil {
1849 distFiles, err := j.OutputFiles(*j.libraryProperties.Dist.Tag)
1850 if err != nil {
1851 ctx.PropertyErrorf("dist.tag", "%s", err.Error())
1852 }
1853 j.distFile = distFiles[0]
1854 }
Colin Crossb7a63242015-04-16 14:09:14 -07001855}
1856
Colin Crossf506d872017-07-19 15:53:04 -07001857func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001858 j.deps(ctx)
1859}
1860
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001861const (
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001862 aidlIncludeDir = "aidl"
1863 javaDir = "java"
1864 jarFileSuffix = ".jar"
1865 testConfigSuffix = "-AndroidTest.xml"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001866)
1867
Paul Duffina0dbf432019-12-05 11:25:53 +00001868// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
Paul Duffin9b358d72020-03-02 10:16:35 +00001869func sdkSnapshotFilePathForJar(osPrefix, name string) string {
1870 return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00001871}
1872
Paul Duffin9b358d72020-03-02 10:16:35 +00001873func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string {
1874 return filepath.Join(javaDir, osPrefix, name+suffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001875}
1876
Paul Duffin13879572019-11-28 14:31:38 +00001877type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001878 android.SdkMemberTypeBase
Paul Duffin2b9e3d32020-02-28 14:39:53 +00001879
1880 // Function to retrieve the appropriate output jar (implementation or header) from
1881 // the library.
1882 jarToExportGetter func(j *Library) android.Path
Paul Duffin13879572019-11-28 14:31:38 +00001883}
1884
1885func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1886 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1887}
1888
1889func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1890 _, ok := module.(*Library)
1891 return ok
1892}
1893
Paul Duffind76209b2020-03-02 11:33:02 +00001894func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
1895 return builder.AddPrebuiltModule(member, "java_import")
1896}
Paul Duffina0dbf432019-12-05 11:25:53 +00001897
Paul Duffind76209b2020-03-02 11:33:02 +00001898func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
1899 return &librarySdkMemberProperties{memberType: mt}
1900}
1901
1902type librarySdkMemberProperties struct {
1903 android.SdkMemberPropertiesBase
1904
1905 memberType *librarySdkMemberType
1906
1907 library *Library
1908 jarToExport android.Path
1909}
1910
1911func (p *librarySdkMemberProperties) PopulateFromVariant(variant android.SdkAware) {
Paul Duffin13879572019-11-28 14:31:38 +00001912 j := variant.(*Library)
1913
Paul Duffind76209b2020-03-02 11:33:02 +00001914 p.library = j
1915 p.jarToExport = p.memberType.jarToExportGetter(j)
1916}
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001917
Paul Duffind76209b2020-03-02 11:33:02 +00001918func (p *librarySdkMemberProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
1919 if p.jarToExport != nil {
1920 exportedJar := p.jarToExport
Paul Duffin9b358d72020-03-02 10:16:35 +00001921 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), p.library.Name())
Paul Duffind76209b2020-03-02 11:33:02 +00001922 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
1923
1924 for _, dir := range p.library.AidlIncludeDirs() {
1925 // TODO(jiyong): copy parcelable declarations only
1926 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1927 for _, file := range aidlFiles {
1928 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1929 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001930 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001931
Paul Duffind76209b2020-03-02 11:33:02 +00001932 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
1933 }
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001934}
1935
Paul Duffin2b9e3d32020-02-28 14:39:53 +00001936var javaHeaderLibsSdkMemberType android.SdkMemberType = &librarySdkMemberType{
1937 android.SdkMemberTypeBase{
1938 PropertyName: "java_header_libs",
1939 SupportsSdk: true,
Paul Duffin7b81f5e2020-01-13 21:03:22 +00001940 },
Paul Duffin2b9e3d32020-02-28 14:39:53 +00001941 func(j *Library) android.Path {
Paul Duffina0dbf432019-12-05 11:25:53 +00001942 headerJars := j.HeaderJars()
1943 if len(headerJars) != 1 {
1944 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1945 }
1946
1947 return headerJars[0]
Paul Duffin2b9e3d32020-02-28 14:39:53 +00001948 },
Paul Duffina0dbf432019-12-05 11:25:53 +00001949}
1950
Colin Cross1b16b0e2019-02-12 14:41:32 -08001951// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1952//
1953// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1954// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1955// as a `static_libs` dependency of another module.
1956//
1957// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1958// a device.
1959//
1960// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1961// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001962func LibraryFactory() android.Module {
1963 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001964
Colin Cross9ae1b922018-06-26 17:59:05 -07001965 module.AddProperties(
1966 &module.Module.properties,
1967 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001968 &module.Module.dexpreoptProperties,
Anton Hansson1e65f942020-03-27 19:39:48 +00001969 &module.Module.protoProperties,
1970 &module.libraryProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001971
Jiyong Park7f7766d2019-07-25 22:02:35 +09001972 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001973 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001974 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001975 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001976}
1977
Colin Cross1b16b0e2019-02-12 14:41:32 -08001978// java_library_static is an obsolete alias for java_library.
1979func LibraryStaticFactory() android.Module {
1980 return LibraryFactory()
1981}
1982
1983// java_library_host builds and links sources into a `.jar` file for the host.
1984//
1985// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1986// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001987func LibraryHostFactory() android.Module {
1988 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001989
Colin Cross6af17aa2017-09-20 12:59:05 -07001990 module.AddProperties(
1991 &module.Module.properties,
1992 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001993
Colin Cross9ae1b922018-06-26 17:59:05 -07001994 module.Module.properties.Installable = proptools.BoolPtr(true)
1995
Jiyong Park7f7766d2019-07-25 22:02:35 +09001996 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001997 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001998 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001999}
2000
2001//
Colin Crossb628ea52018-08-14 16:42:33 -07002002// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07002003//
2004
2005type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07002006 // list of compatibility suites (for example "cts", "vts") that the module should be
2007 // installed into.
2008 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07002009
2010 // the name of the test configuration (for example "AndroidTest.xml") that should be
2011 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002012 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07002013
Jack He33338892018-09-19 02:21:28 -07002014 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
2015 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08002016 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07002017
Colin Crossd96ca352018-08-10 16:06:24 -07002018 // list of files or filegroup modules that provide data that should be installed alongside
2019 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08002020 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07002021
2022 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
2023 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
2024 // explicitly.
2025 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07002026}
2027
Paul Duffin42df1442019-03-20 12:45:53 +00002028type testHelperLibraryProperties struct {
2029 // list of compatibility suites (for example "cts", "vts") that the module should be
2030 // installed into.
2031 Test_suites []string `android:"arch_variant"`
2032}
2033
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002034type prebuiltTestProperties struct {
2035 // list of compatibility suites (for example "cts", "vts") that the module should be
2036 // installed into.
2037 Test_suites []string `android:"arch_variant"`
2038
2039 // the name of the test configuration (for example "AndroidTest.xml") that should be
2040 // installed with the module.
2041 Test_config *string `android:"path,arch_variant"`
2042}
2043
Colin Cross05638fc2018-04-09 18:40:24 -07002044type Test struct {
2045 Library
2046
2047 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07002048
2049 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07002050 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07002051}
2052
Paul Duffin42df1442019-03-20 12:45:53 +00002053type TestHelperLibrary struct {
2054 Library
2055
2056 testHelperLibraryProperties testHelperLibraryProperties
2057}
2058
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002059type JavaTestImport struct {
2060 Import
2061
2062 prebuiltTestProperties prebuiltTestProperties
2063
2064 testConfig android.Path
2065}
2066
Colin Cross303e21f2018-08-07 16:49:25 -07002067func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07002068 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
2069 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08002070 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07002071
2072 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07002073}
2074
Paul Duffin42df1442019-03-20 12:45:53 +00002075func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2076 j.Library.GenerateAndroidBuildActions(ctx)
2077}
2078
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002079func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2080 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
2081 j.prebuiltTestProperties.Test_suites, nil)
2082
2083 j.Import.GenerateAndroidBuildActions(ctx)
2084}
2085
2086type testSdkMemberType struct {
2087 android.SdkMemberTypeBase
2088}
2089
2090func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
2091 mctx.AddVariationDependencies(nil, dependencyTag, names...)
2092}
2093
2094func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
2095 _, ok := module.(*Test)
2096 return ok
2097}
2098
Paul Duffind76209b2020-03-02 11:33:02 +00002099func (mt *testSdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
2100 return builder.AddPrebuiltModule(member, "java_test_import")
2101}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002102
Paul Duffind76209b2020-03-02 11:33:02 +00002103func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
2104 return &testSdkMemberProperties{}
2105}
2106
2107type testSdkMemberProperties struct {
2108 android.SdkMemberPropertiesBase
2109
2110 test *Test
2111 jarToExport android.Path
2112}
2113
2114func (p *testSdkMemberProperties) PopulateFromVariant(variant android.SdkAware) {
2115 test := variant.(*Test)
2116
2117 implementationJars := test.ImplementationJars()
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002118 if len(implementationJars) != 1 {
Paul Duffind76209b2020-03-02 11:33:02 +00002119 panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name()))
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002120 }
2121
Paul Duffind76209b2020-03-02 11:33:02 +00002122 p.test = test
2123 p.jarToExport = implementationJars[0]
2124}
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002125
Paul Duffind76209b2020-03-02 11:33:02 +00002126func (p *testSdkMemberProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
2127 if p.jarToExport != nil {
Paul Duffin9b358d72020-03-02 10:16:35 +00002128 snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(p.OsPrefix(), p.test.Name())
Paul Duffind76209b2020-03-02 11:33:02 +00002129 builder.CopyToSnapshot(p.jarToExport, snapshotRelativeJavaLibPath)
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002130
Paul Duffin9b358d72020-03-02 10:16:35 +00002131 snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), p.test.Name(), testConfigSuffix)
Paul Duffind76209b2020-03-02 11:33:02 +00002132 builder.CopyToSnapshot(p.test.testConfig, snapshotRelativeTestConfigPath)
2133
2134 propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
2135 propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath)
2136 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002137}
2138
Colin Cross1b16b0e2019-02-12 14:41:32 -08002139// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
2140// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
2141//
2142// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
2143// compiled against the device bootclasspath.
2144//
2145// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2146// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002147func TestFactory() android.Module {
2148 module := &Test{}
2149
2150 module.AddProperties(
2151 &module.Module.properties,
2152 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002153 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07002154 &module.Module.protoProperties,
2155 &module.testProperties)
2156
Colin Cross9ae1b922018-06-26 17:59:05 -07002157 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08002158 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07002159
Colin Cross05638fc2018-04-09 18:40:24 -07002160 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002161 return module
2162}
2163
Paul Duffin42df1442019-03-20 12:45:53 +00002164// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
2165func TestHelperLibraryFactory() android.Module {
2166 module := &TestHelperLibrary{}
2167
2168 module.AddProperties(
2169 &module.Module.properties,
2170 &module.Module.deviceProperties,
2171 &module.Module.dexpreoptProperties,
2172 &module.Module.protoProperties,
2173 &module.testHelperLibraryProperties)
2174
Colin Cross9a4abed2019-04-24 13:19:28 -07002175 module.Module.properties.Installable = proptools.BoolPtr(true)
2176 module.Module.dexpreopter.isTest = true
2177
Paul Duffin42df1442019-03-20 12:45:53 +00002178 InitJavaModule(module, android.HostAndDeviceSupported)
2179 return module
2180}
2181
Paul Duffin1b82e6a2019-12-03 18:06:47 +00002182// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
2183// and makes sure that it is added to the appropriate test suite.
2184//
2185// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
2186// compiled against an Android classpath.
2187//
2188// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2189// for host modules.
2190func JavaTestImportFactory() android.Module {
2191 module := &JavaTestImport{}
2192
2193 module.AddProperties(
2194 &module.Import.properties,
2195 &module.prebuiltTestProperties)
2196
2197 module.Import.properties.Installable = proptools.BoolPtr(true)
2198
2199 android.InitPrebuiltModule(module, &module.properties.Jars)
2200 android.InitApexModule(module)
2201 android.InitSdkAwareModule(module)
2202 InitJavaModule(module, android.HostAndDeviceSupported)
2203 return module
2204}
2205
Colin Cross1b16b0e2019-02-12 14:41:32 -08002206// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2207// allow running the test with `atest` or a `TEST_MAPPING` file.
2208//
2209// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2210// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002211func TestHostFactory() android.Module {
2212 module := &Test{}
2213
2214 module.AddProperties(
2215 &module.Module.properties,
2216 &module.Module.protoProperties,
2217 &module.testProperties)
2218
Colin Cross9ae1b922018-06-26 17:59:05 -07002219 module.Module.properties.Installable = proptools.BoolPtr(true)
2220
Colin Cross05638fc2018-04-09 18:40:24 -07002221 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002222 return module
2223}
2224
2225//
Colin Cross2fe66872015-03-30 17:20:39 -07002226// Java Binaries (.jar file plus wrapper script)
2227//
2228
Colin Crossf506d872017-07-19 15:53:04 -07002229type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002230 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002231 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002232
2233 // Name of the class containing main to be inserted into the manifest as Main-Class.
2234 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002235}
2236
Colin Crossf506d872017-07-19 15:53:04 -07002237type Binary struct {
2238 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002239
Colin Crossf506d872017-07-19 15:53:04 -07002240 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002241
Colin Cross6b4a32d2017-12-05 13:42:45 -08002242 isWrapperVariant bool
2243
Colin Crossc3315992017-12-08 19:12:36 -08002244 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002245 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002246}
2247
Alex Light24237172017-10-26 09:46:21 -07002248func (j *Binary) HostToolPath() android.OptionalPath {
2249 return android.OptionalPathForPath(j.binaryFile)
2250}
2251
Colin Crossf506d872017-07-19 15:53:04 -07002252func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002253 if ctx.Arch().ArchType == android.Common {
2254 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002255 if j.binaryProperties.Main_class != nil {
2256 if j.properties.Manifest != nil {
2257 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2258 }
2259 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2260 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2261 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2262 }
2263
Colin Cross6b4a32d2017-12-05 13:42:45 -08002264 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002265 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002266 // Handle the binary wrapper
2267 j.isWrapperVariant = true
2268
Colin Cross366938f2017-12-11 16:29:02 -08002269 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002270 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002271 } else {
2272 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2273 }
2274
2275 // Depend on the installed jar so that the wrapper doesn't get executed by
2276 // another build rule before the jar has been installed.
2277 jarFile := ctx.PrimaryModule().(*Binary).installFile
2278
2279 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2280 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002281 }
Colin Cross2fe66872015-03-30 17:20:39 -07002282}
2283
Colin Crossf506d872017-07-19 15:53:04 -07002284func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002285 if ctx.Arch().ArchType == android.Common {
2286 j.deps(ctx)
2287 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002288}
2289
Colin Cross1b16b0e2019-02-12 14:41:32 -08002290// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2291// as well.
2292//
2293// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2294// compiled against the device bootclasspath.
2295//
2296// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2297// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002298func BinaryFactory() android.Module {
2299 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002300
Colin Cross36242852017-06-23 15:06:31 -07002301 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002302 &module.Module.properties,
2303 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002304 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002305 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002306 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002307
Colin Cross9ae1b922018-06-26 17:59:05 -07002308 module.Module.properties.Installable = proptools.BoolPtr(true)
2309
Colin Cross6b4a32d2017-12-05 13:42:45 -08002310 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2311 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002312 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002313}
2314
Colin Cross1b16b0e2019-02-12 14:41:32 -08002315// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2316//
2317// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2318// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002319func BinaryHostFactory() android.Module {
2320 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002321
Colin Cross36242852017-06-23 15:06:31 -07002322 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002323 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002324 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002325 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002326
Colin Cross9ae1b922018-06-26 17:59:05 -07002327 module.Module.properties.Installable = proptools.BoolPtr(true)
2328
Colin Cross6b4a32d2017-12-05 13:42:45 -08002329 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2330 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002331 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002332}
2333
2334//
2335// Java prebuilts
2336//
2337
Colin Cross74d73e22017-08-02 11:05:49 -07002338type ImportProperties struct {
Paul Duffin9b358d72020-03-02 10:16:35 +00002339 Jars []string `android:"path,arch_variant"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002340
Nan Zhangea568a42017-11-08 21:20:04 -08002341 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002342
2343 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002344
2345 // List of shared java libs that this module has dependencies to
2346 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002347
2348 // List of files to remove from the jar file(s)
2349 Exclude_files []string
2350
2351 // List of directories to remove from the jar file(s)
2352 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002353
2354 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002355 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002356
2357 // set the name of the output
2358 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002359}
2360
2361type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002362 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002363 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002364 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002365 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002366 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002367
Colin Cross74d73e22017-08-02 11:05:49 -07002368 properties ImportProperties
2369
Colin Cross0a6e0072017-08-30 14:24:55 -07002370 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002371 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002372}
2373
Jiyong Park6a927c42020-01-21 02:03:43 +09002374func (j *Import) sdkVersion() sdkSpec {
2375 return sdkSpecFrom(String(j.properties.Sdk_version))
Colin Cross83bb3162018-06-25 15:48:06 -07002376}
2377
Jiyong Park6a927c42020-01-21 02:03:43 +09002378func (j *Import) minSdkVersion() sdkSpec {
Colin Cross83bb3162018-06-25 15:48:06 -07002379 return j.sdkVersion()
2380}
2381
Colin Cross74d73e22017-08-02 11:05:49 -07002382func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002383 return &j.prebuilt
2384}
2385
Colin Cross74d73e22017-08-02 11:05:49 -07002386func (j *Import) PrebuiltSrcs() []string {
2387 return j.properties.Jars
2388}
2389
2390func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002391 return j.prebuilt.Name(j.ModuleBase.Name())
2392}
2393
Jiyong Park0b238752019-10-29 11:23:10 +09002394func (j *Import) Stem() string {
2395 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2396}
2397
Jiyong Park618922e2020-01-08 13:35:43 +09002398func (a *Import) JacocoReportClassesFile() android.Path {
2399 return nil
2400}
2401
Colin Cross74d73e22017-08-02 11:05:49 -07002402func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002403 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002404}
2405
Colin Cross74d73e22017-08-02 11:05:49 -07002406func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002407 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002408
Jiyong Park0b238752019-10-29 11:23:10 +09002409 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002410 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002411 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2412 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002413 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002414 inputFile := outputFile
2415 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2416 TransformJetifier(ctx, outputFile, inputFile)
2417 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002418 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002419
2420 ctx.VisitDirectDeps(func(module android.Module) {
2421 otherName := ctx.OtherModuleName(module)
2422 tag := ctx.OtherModuleDependencyTag(module)
2423
2424 switch dep := module.(type) {
2425 case Dependency:
2426 switch tag {
2427 case libTag, staticLibTag:
2428 // sdk lib names from dependencies are re-exported
2429 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2430 }
2431 case SdkLibraryDependency:
2432 switch tag {
2433 case libTag:
2434 // names of sdk libs that are directly depended are exported
2435 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2436 }
2437 }
2438 })
2439
2440 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002441 if Bool(j.properties.Installable) {
2442 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002443 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002444 }
Colin Cross2fe66872015-03-30 17:20:39 -07002445}
2446
Colin Cross74d73e22017-08-02 11:05:49 -07002447var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002448
Nan Zhanged19fc32017-10-19 13:06:22 -07002449func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002450 if j.combinedClasspathFile == nil {
2451 return nil
2452 }
Colin Cross37f6d792018-07-12 12:28:41 -07002453 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002454}
2455
2456func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002457 if j.combinedClasspathFile == nil {
2458 return nil
2459 }
Colin Cross37f6d792018-07-12 12:28:41 -07002460 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002461}
2462
Colin Cross331a1212018-08-15 20:40:52 -07002463func (j *Import) ResourceJars() android.Paths {
2464 return nil
2465}
2466
2467func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002468 if j.combinedClasspathFile == nil {
2469 return nil
2470 }
Colin Cross331a1212018-08-15 20:40:52 -07002471 return android.Paths{j.combinedClasspathFile}
2472}
2473
Colin Crossf24a22a2019-01-31 14:12:44 -08002474func (j *Import) DexJar() android.Path {
2475 return nil
2476}
2477
Colin Cross74d73e22017-08-02 11:05:49 -07002478func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002479 return nil
2480}
2481
Jiyong Park1be96912018-05-28 18:02:19 +09002482func (j *Import) ExportedSdkLibs() []string {
2483 return j.exportedSdkLibs
2484}
2485
Artur Satayev9cf46692019-11-26 18:08:34 +00002486func (j *Import) ExportedPlugins() (android.Paths, []string) {
2487 return nil, nil
2488}
2489
Colin Cross0c4ce212019-05-03 15:28:19 -07002490func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2491 return nil, nil
2492}
2493
Jiyong Park0f80c182020-01-31 02:49:53 +09002494func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
Jiyong Park0f80c182020-01-31 02:49:53 +09002495 // dependencies other than the static linkage are all considered crossing APEX boundary
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002496 if staticLibTag == ctx.OtherModuleDependencyTag(dep) {
2497 return true
2498 }
Jiyong Park0f80c182020-01-31 02:49:53 +09002499 // Also, a dependency to an sdk member is also considered as such. This is required because
2500 // sdk members should be mutated into APEXes. Refer to sdk.sdkDepsReplaceMutator.
Jooyung Hanb8fa86a2020-03-10 06:23:13 +09002501 if sa, ok := dep.(android.SdkAware); ok && sa.IsInAnySdk() {
2502 return true
2503 }
2504 return false
Jiyong Park0f80c182020-01-31 02:49:53 +09002505}
2506
albaltai36ff7dc2018-12-25 14:35:23 +08002507// Add compile time check for interface implementation
2508var _ android.IDEInfo = (*Import)(nil)
2509var _ android.IDECustomizedModuleName = (*Import)(nil)
2510
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002511// Collect information for opening IDE project files in java/jdeps.go.
2512const (
2513 removedPrefix = "prebuilt_"
2514)
2515
2516func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2517 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2518}
2519
2520func (j *Import) IDECustomizedModuleName() string {
2521 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2522 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2523 // solution to get the Import name.
2524 name := j.Name()
2525 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002526 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002527 }
2528 return name
2529}
2530
Colin Cross74d73e22017-08-02 11:05:49 -07002531var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002532
Colin Cross1b16b0e2019-02-12 14:41:32 -08002533// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2534//
2535// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2536// compiled against an Android classpath.
2537//
2538// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2539// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002540func ImportFactory() android.Module {
2541 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002542
Colin Cross74d73e22017-08-02 11:05:49 -07002543 module.AddProperties(&module.properties)
2544
2545 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002546 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002547 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002548 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002549 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002550}
2551
Colin Cross1b16b0e2019-02-12 14:41:32 -08002552// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2553// module.
2554//
2555// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2556// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002557func ImportFactoryHost() android.Module {
2558 module := &Import{}
2559
2560 module.AddProperties(&module.properties)
2561
2562 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002563 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002564 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002565 return module
2566}
2567
Colin Cross42be7612019-02-21 18:12:14 -08002568// dex_import module
2569
2570type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002571 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002572
2573 // set the name of the output
2574 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002575}
2576
2577type DexImport struct {
2578 android.ModuleBase
2579 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002580 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002581 prebuilt android.Prebuilt
2582
2583 properties DexImportProperties
2584
2585 dexJarFile android.Path
2586 maybeStrippedDexJarFile android.Path
2587
2588 dexpreopter
2589}
2590
2591func (j *DexImport) Prebuilt() *android.Prebuilt {
2592 return &j.prebuilt
2593}
2594
2595func (j *DexImport) PrebuiltSrcs() []string {
2596 return j.properties.Jars
2597}
2598
2599func (j *DexImport) Name() string {
2600 return j.prebuilt.Name(j.ModuleBase.Name())
2601}
2602
Jiyong Park0b238752019-10-29 11:23:10 +09002603func (j *DexImport) Stem() string {
2604 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2605}
2606
Martin Stjernholm6d415272020-01-31 17:10:36 +00002607func (j *DexImport) IsInstallable() bool {
2608 return true
2609}
2610
Colin Cross42be7612019-02-21 18:12:14 -08002611func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2612 if len(j.properties.Jars) != 1 {
2613 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2614 }
2615
Jiyong Park0b238752019-10-29 11:23:10 +09002616 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002617 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2618
2619 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2620 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2621
2622 if j.dexpreopter.uncompressedDex {
2623 rule := android.NewRuleBuilder()
2624
2625 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2626 rule.Temporary(temporary)
2627
2628 // use zip2zip to uncompress classes*.dex files
2629 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002630 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002631 FlagWithInput("-i ", inputJar).
2632 FlagWithOutput("-o ", temporary).
2633 FlagWithArg("-0 ", "'classes*.dex'")
2634
2635 // use zipalign to align uncompressed classes*.dex files
2636 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002637 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002638 Flag("-f").
2639 Text("4").
2640 Input(temporary).
2641 Output(dexOutputFile)
2642
2643 rule.DeleteTemporaryFiles()
2644
2645 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2646 } else {
2647 ctx.Build(pctx, android.BuildParams{
2648 Rule: android.Cp,
2649 Input: inputJar,
2650 Output: dexOutputFile,
2651 })
2652 }
2653
2654 j.dexJarFile = dexOutputFile
2655
2656 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2657
2658 j.maybeStrippedDexJarFile = dexOutputFile
2659
2660 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2661 ctx.ModuleName()+".jar", dexOutputFile)
2662}
2663
2664func (j *DexImport) DexJar() android.Path {
2665 return j.dexJarFile
2666}
2667
2668// dex_import imports a `.jar` file containing classes.dex files.
2669//
2670// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2671// to the device.
2672func DexImportFactory() android.Module {
2673 module := &DexImport{}
2674
2675 module.AddProperties(&module.properties)
2676
2677 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002678 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002679 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002680 return module
2681}
2682
Colin Cross89536d42017-07-07 14:35:50 -07002683//
2684// Defaults
2685//
2686type Defaults struct {
2687 android.ModuleBase
2688 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002689 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002690}
2691
Colin Cross1b16b0e2019-02-12 14:41:32 -08002692// java_defaults provides a set of properties that can be inherited by other java or android modules.
2693//
2694// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2695// property in the defaults module that exists in the depending module will be prepended to the depending module's
2696// value for that property.
2697//
2698// Example:
2699//
2700// java_defaults {
2701// name: "example_defaults",
2702// srcs: ["common/**/*.java"],
2703// javacflags: ["-Xlint:all"],
2704// aaptflags: ["--auto-add-overlay"],
2705// }
2706//
2707// java_library {
2708// name: "example",
2709// defaults: ["example_defaults"],
2710// srcs: ["example/**/*.java"],
2711// }
2712//
2713// is functionally identical to:
2714//
2715// java_library {
2716// name: "example",
2717// srcs: [
2718// "common/**/*.java",
2719// "example/**/*.java",
2720// ],
2721// javacflags: ["-Xlint:all"],
2722// }
Colin Cross89536d42017-07-07 14:35:50 -07002723func defaultsFactory() android.Module {
2724 return DefaultsFactory()
2725}
2726
Paul Duffin47357662019-12-05 14:07:14 +00002727func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002728 module := &Defaults{}
2729
Colin Cross89536d42017-07-07 14:35:50 -07002730 module.AddProperties(
2731 &CompilerProperties{},
2732 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002733 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002734 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002735 &aaptProperties{},
2736 &androidLibraryProperties{},
2737 &appProperties{},
2738 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002739 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002740 &ImportProperties{},
2741 &AARImportProperties{},
2742 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002743 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002744 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002745 )
2746
2747 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002748 return module
2749}
Nan Zhangea568a42017-11-08 21:20:04 -08002750
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002751func kytheExtractJavaFactory() android.Singleton {
2752 return &kytheExtractJavaSingleton{}
2753}
2754
2755type kytheExtractJavaSingleton struct {
2756}
2757
2758func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2759 var xrefTargets android.Paths
2760 ctx.VisitAllModules(func(module android.Module) {
2761 if javaModule, ok := module.(xref); ok {
2762 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2763 }
2764 })
2765 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2766 if len(xrefTargets) > 0 {
2767 ctx.Build(pctx, android.BuildParams{
2768 Rule: blueprint.Phony,
2769 Output: android.PathForPhony(ctx, "xref_java"),
2770 Inputs: xrefTargets,
2771 })
2772 }
2773}
2774
Nan Zhangea568a42017-11-08 21:20:04 -08002775var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002776var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002777var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002778var inList = android.InList