blob: d8db5f8a41c63766eaa00a57660f8d0d70df21d6 [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 Duffin47357662019-12-05 14:07:14 +000037 android.RegisterModuleType("java_defaults", DefaultsFactory)
Colin Cross89536d42017-07-07 14:35:50 -070038
Colin Cross9ae1b922018-06-26 17:59:05 -070039 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080040 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070041 android.RegisterModuleType("java_library_host", LibraryHostFactory)
42 android.RegisterModuleType("java_binary", BinaryFactory)
43 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070044 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000045 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070046 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070047 android.RegisterModuleType("java_import", ImportFactory)
48 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080049 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
50 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080051 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070052
Colin Cross798bfce2016-10-12 14:28:16 -070053 android.RegisterSingletonType("logtags", LogtagsSingleton)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080054 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Paul Duffin255f18e2019-12-13 11:22:16 +000055
56 // Register sdk member types.
57 android.RegisterSdkMemberType(&headerLibrarySdkMemberType{
58 librarySdkMemberType{
59 android.SdkMemberTypeBase{
60 PropertyName: "java_header_libs",
61 },
62 },
63 })
64
65 android.RegisterSdkMemberType(&implLibrarySdkMemberType{
66 librarySdkMemberType{
67 android.SdkMemberTypeBase{
68 PropertyName: "java_libs",
69 },
70 },
71 })
Colin Cross463a90e2015-06-17 14:20:06 -070072}
73
Jeongik Cha2cc570d2019-10-29 15:44:45 +090074func (j *Module) checkSdkVersion(ctx android.ModuleContext) {
75 if j.SocSpecific() || j.DeviceSpecific() ||
76 (j.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) {
77 if sc, ok := ctx.Module().(sdkContext); ok {
78 if sc.sdkVersion() == "" {
79 ctx.PropertyErrorf("sdk_version",
80 "sdk_version must have a value when the module is located at vendor or product(only if PRODUCT_ENFORCE_PRODUCT_PARTITION_INTERFACE is set).")
81 }
82 }
83 }
84}
85
Jeongik Cha538c0d02019-07-11 15:54:27 +090086func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
87 if sc, ok := ctx.Module().(sdkContext); ok {
88 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
89 if usePlatformAPI != (sc.sdkVersion() == "") {
90 if usePlatformAPI {
91 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
92 } else {
93 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
94 }
95 }
96
97 }
98}
99
Colin Cross2fe66872015-03-30 17:20:39 -0700100// TODO:
101// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -0700102// Renderscript
103// Post-jar passes:
104// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -0700105// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -0700106// DroidDoc
107// Findbugs
108
Colin Cross89536d42017-07-07 14:35:50 -0700109type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700110 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
111 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -0800112 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700113
114 // list of source files that should not be used to build the Java module.
115 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700117
118 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -0700119 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700120
Colin Cross86a63ff2017-09-27 17:33:10 -0700121 // list of directories that should be excluded from java_resource_dirs
122 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700123
Colin Cross0f37af02017-09-27 17:42:05 -0700124 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -0800125 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700126
Colin Crosscedd4762018-09-13 11:26:19 -0700127 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -0800128 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700129
Colin Cross7d5136f2015-05-11 13:39:40 -0700130 // list of module-specific flags that will be used for javac compiles
131 Javacflags []string `android:"arch_variant"`
132
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200133 // list of module-specific flags that will be used for kotlinc compiles
134 Kotlincflags []string `android:"arch_variant"`
135
Colin Cross7d5136f2015-05-11 13:39:40 -0700136 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700137 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700138
139 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700140 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700141
142 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800143 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700144
Colin Cross540eff82017-06-22 17:01:52 -0700145 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800146 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700147
148 // If not blank, set the java version passed to javac as -source and -target
149 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700150
Colin Cross9ae1b922018-06-26 17:59:05 -0700151 // If set to true, allow this module to be dexed and installed on devices. Has no
152 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700153 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700154
Colin Cross0f37af02017-09-27 17:42:05 -0700155 // If set to true, include sources used to compile the module in to the final jar
156 Include_srcs *bool
157
Vladimir Marko0975ee02019-04-02 10:29:55 +0100158 // If not empty, classes are restricted to the specified packages and their sub-packages.
159 // This restriction is checked after applying jarjar rules and including static libs.
160 Permitted_packages []string
161
Colin Crossbe9cdb82019-01-21 21:37:16 -0800162 // List of modules to use as annotation processors
163 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700164
Artur Satayev9cf46692019-11-26 18:08:34 +0000165 // List of modules to export to libraries that directly depend on this library as annotation processors
166 Exported_plugins []string
167
Nan Zhang61eaedb2017-11-02 13:28:15 -0700168 // The number of Java source entries each Javac instance can process
169 Javac_shard_size *int64
170
Nan Zhang5f8cb422018-02-06 10:34:32 -0800171 // Add host jdk tools.jar to bootclasspath
172 Use_tools_jar *bool
173
Colin Cross1369cdb2017-09-29 17:58:17 -0700174 Openjdk9 struct {
Colin Cross6cef4812019-10-17 14:23:50 -0700175 // List of source files that should only be used when passing -source 1.9 or higher
Colin Cross27b922f2019-03-04 22:35:41 -0800176 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700177
Colin Cross6cef4812019-10-17 14:23:50 -0700178 // List of javac flags that should only be used when passing -source 1.9 or higher
Colin Cross1369cdb2017-09-29 17:58:17 -0700179 Javacflags []string
180 }
Colin Crosscb933592017-11-22 13:49:43 -0800181
Colin Cross81440082018-08-15 20:21:55 -0700182 // When compiling language level 9+ .java code in packages that are part of
183 // a system module, patch_module names the module that your sources and
184 // dependencies should be patched into. The Android runtime currently
185 // doesn't implement the JEP 261 module system so this option is only
186 // supported at compile time. It should only be needed to compile tests in
187 // packages that exist in libcore and which are inconvenient to move
188 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100189 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700190
Colin Crosscb933592017-11-22 13:49:43 -0800191 Jacoco struct {
192 // List of classes to include for instrumentation with jacoco to collect coverage
193 // information at runtime when building with coverage enabled. If unset defaults to all
194 // classes.
195 // Supports '*' as the last character of an entry in the list as a wildcard match.
196 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
197 // it matches classes in the package that have the class name as a prefix.
198 Include_filter []string
199
200 // List of classes to exclude from instrumentation with jacoco to collect coverage
201 // information at runtime when building with coverage enabled. Overrides classes selected
202 // by the include_filter property.
203 // Supports '*' as the last character of an entry in the list as a wildcard match.
204 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
205 // it matches classes in the package that have the class name as a prefix.
206 Exclude_filter []string
207 }
208
Andreas Gampef3e5b552018-01-22 21:27:21 -0800209 Errorprone struct {
210 // List of javac flags that should only be used when running errorprone.
211 Javacflags []string
212 }
213
Colin Cross0f2ee152017-12-14 15:22:43 -0800214 Proto struct {
215 // List of extra options that will be passed to the proto generator.
216 Output_params []string
217 }
218
Colin Crosscb933592017-11-22 13:49:43 -0800219 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800220
221 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800222 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700223}
224
Colin Cross89536d42017-07-07 14:35:50 -0700225type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700226 // list of module-specific flags that will be used for dex compiles
227 Dxflags []string `android:"arch_variant"`
228
Jeongik Cha538c0d02019-07-11 15:54:27 +0900229 // if not blank, set to the version of the sdk to compile against.
230 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800231 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700232
Colin Cross83bb3162018-06-25 15:48:06 -0700233 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
234 // Defaults to sdk_version if not set.
235 Min_sdk_version *string
236
Dan Willemsen419290a2018-10-31 15:28:47 -0700237 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
238 // Defaults to sdk_version if not set.
239 Target_sdk_version *string
240
Jeongik Cha356dac42019-08-19 14:09:52 +0900241 // Whether to compile against the platform APIs instead of an SDK.
242 // If true, then sdk_version must be empty. The value of this field
243 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700244 Platform_apis *bool
245
Colin Crossebe1a512017-11-14 13:12:14 -0800246 Aidl struct {
247 // Top level directories to pass to aidl tool
248 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700249
Colin Crossebe1a512017-11-14 13:12:14 -0800250 // Directories rooted at the Android.bp file to pass to aidl tool
251 Local_include_dirs []string
252
253 // directories that should be added as include directories for any aidl sources of modules
254 // that depend on this module, as well as to aidl for this module.
255 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100256
257 // whether to generate traces (for systrace) for this interface
258 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100259
260 // whether to generate Binder#GetTransaction name method.
261 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800262 }
Colin Cross92430102017-10-09 14:59:32 -0700263
264 // If true, export a copy of the module as a -hostdex module for host testing.
265 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700266
Colin Cross7f87f4f2019-04-24 13:41:45 -0700267 Target struct {
268 Hostdex struct {
269 // Additional required dependencies to add to -hostdex modules.
270 Required []string
271 }
272 }
273
David Brazdil17ef5632018-06-27 10:27:45 +0100274 // If set to true, compile dex regardless of installable. Defaults to false.
275 Compile_dex *bool
276
Colin Cross66dbc0b2017-12-28 12:23:20 -0800277 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700278 // If false, disable all optimization. Defaults to true for android_app and android_test
279 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800280 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700281 // True if the module containing this has it set by default.
282 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800283
284 // If true, optimize for size by removing unused code. Defaults to true for apps,
285 // false for libraries and tests.
286 Shrink *bool
287
288 // If true, optimize bytecode. Defaults to false.
289 Optimize *bool
290
291 // If true, obfuscate bytecode. Defaults to false.
292 Obfuscate *bool
293
294 // If true, do not use the flag files generated by aapt that automatically keep
295 // classes referenced by the app manifest. Defaults to false.
296 No_aapt_flags *bool
297
298 // Flags to pass to proguard.
299 Proguard_flags []string
300
301 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800302 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303 }
304
Paul Duffine25c6442019-10-11 13:50:28 +0100305 // When targeting 1.9 and above, override the modules to use with --system,
306 // otherwise provides defaults libraries to add to the bootclasspath.
Colin Cross1369cdb2017-09-29 17:58:17 -0700307 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700308
Jiyong Park4c4c0242019-10-21 14:53:15 +0900309 // set the name of the output
310 Stem *string
311
Colin Cross5a0dcd52018-10-05 14:20:06 -0700312 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800313 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700314}
315
Sasha Smundak2057f822019-04-16 17:16:58 -0700316func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
317 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
318}
319
Colin Cross46c9b8b2017-06-22 16:51:17 -0700320// Module contains the properties and members used by all java module types
321type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700322 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700323 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 android.ApexModuleBase
Jiyong Parkd1063c12019-07-17 20:08:41 +0900325 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -0700326
Colin Cross89536d42017-07-07 14:35:50 -0700327 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700328 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700329 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700330
Colin Cross331a1212018-08-15 20:40:52 -0700331 // jar file containing header classes including static library dependencies, suitable for
332 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700333 headerJarFile android.Path
334
Colin Cross331a1212018-08-15 20:40:52 -0700335 // jar file containing implementation classes including static library dependencies but no
336 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700337 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700338
Colin Cross331a1212018-08-15 20:40:52 -0700339 // jar file containing only resources including from static library dependencies
340 resourceJar android.Path
341
Colin Cross0c4ce212019-05-03 15:28:19 -0700342 // args and dependencies to package source files into a srcjar
343 srcJarArgs []string
344 srcJarDeps android.Paths
345
Colin Cross331a1212018-08-15 20:40:52 -0700346 // jar file containing implementation classes and resources including static library
347 // dependencies
348 implementationAndResourcesJar android.Path
349
350 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700351 dexJarFile android.Path
352
Colin Cross43f08db2018-11-12 10:13:39 -0800353 // output file that contains classes.dex if it should be in the output file
354 maybeStrippedDexJarFile android.Path
355
Colin Crosscb933592017-11-22 13:49:43 -0800356 // output file containing uninstrumented classes that will be instrumented by jacoco
357 jacocoReportClassesFile android.Path
358
Colin Cross66dbc0b2017-12-28 12:23:20 -0800359 // output file containing mapping of obfuscated names
360 proguardDictionary android.Path
361
Colin Cross331a1212018-08-15 20:40:52 -0700362 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700363 outputFile android.Path
364 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700365
Colin Cross635c3b02016-05-18 15:37:25 -0700366 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700367
Colin Cross635c3b02016-05-18 15:37:25 -0700368 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700369
Colin Cross2fe66872015-03-30 17:20:39 -0700370 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700371 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800372
373 // list of .java files and srcjars that was passed to javac
374 compiledJavaSrcs android.Paths
375 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800376
377 // list of extra progurad flag files
378 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900379
Colin Cross094054a2018-10-17 15:10:48 -0700380 // manifest file to use instead of properties.Manifest
381 overrideManifest android.OptionalPath
382
Artur Satayev9cf46692019-11-26 18:08:34 +0000383 // list of SDK lib names that this java module is exporting
Jiyong Park1be96912018-05-28 18:02:19 +0900384 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700385
Artur Satayev9cf46692019-11-26 18:08:34 +0000386 // list of plugins that this java module is exporting
387 exportedPluginJars android.Paths
388
389 // list of plugins that this java module is exporting
390 exportedPluginClasses []string
391
392 // list of source files, collected from srcFiles with unique java and all kt files,
patricktu242faad2019-09-24 15:41:30 +0800393 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700394 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800395
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800396 // expanded Jarjar_rules
397 expandJarjarRules android.Path
398
Vladimir Marko0975ee02019-04-02 10:29:55 +0100399 // list of additional targets for checkbuild
400 additionalCheckedModules android.Paths
401
Colin Cross988708c2019-05-06 14:04:11 -0700402 // Extra files generated by the module type to be added as java resources.
403 extraResources android.Paths
404
Colin Crossf24a22a2019-01-31 14:12:44 -0800405 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800406 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800407
408 // list of the xref extraction files
409 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700410}
411
Colin Cross41955e82019-05-29 14:40:35 -0700412func (j *Module) OutputFiles(tag string) (android.Paths, error) {
413 switch tag {
414 case "":
415 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700416 case ".jar":
417 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700418 case ".proguard_map":
419 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700420 default:
421 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
422 }
Colin Cross54250902017-12-05 09:28:08 -0800423}
424
Jiyong Park8fd61922018-11-08 02:50:25 +0900425func (j *Module) DexJarFile() android.Path {
426 return j.dexJarFile
427}
428
Colin Cross41955e82019-05-29 14:40:35 -0700429var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800430
Colin Crossf506d872017-07-19 15:53:04 -0700431type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700432 HeaderJars() android.Paths
433 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700434 ResourceJars() android.Paths
435 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800436 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700437 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900438 ExportedSdkLibs() []string
Artur Satayev9cf46692019-11-26 18:08:34 +0000439 ExportedPlugins() (android.Paths, []string)
Colin Cross0c4ce212019-05-03 15:28:19 -0700440 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700441 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700442}
443
Jiyong Parkc678ad32018-04-10 13:07:10 +0900444type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700445 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
446 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900447}
448
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800449type xref interface {
450 XrefJavaFiles() android.Paths
451}
452
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800453func (j *Module) XrefJavaFiles() android.Paths {
454 return j.kytheFiles
455}
456
Colin Cross89536d42017-07-07 14:35:50 -0700457func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
458 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
459 android.InitDefaultableModule(module)
460}
461
Colin Crossbe1da472017-07-07 15:59:46 -0700462type dependencyTag struct {
463 blueprint.BaseDependencyTag
464 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700465}
466
Colin Crossa4f08812018-10-02 22:03:40 -0700467type jniDependencyTag struct {
468 blueprint.BaseDependencyTag
469 target android.Target
470}
471
Jiyong Park8be103b2019-11-08 15:53:48 +0900472func IsJniDepTag(depTag blueprint.DependencyTag) bool {
473 _, ok := depTag.(*jniDependencyTag)
474 return ok
475}
476
Colin Crossbe1da472017-07-07 15:59:46 -0700477var (
Colin Cross4b964c02018-10-15 16:18:06 -0700478 staticLibTag = dependencyTag{name: "staticlib"}
479 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700480 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800481 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000482 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700483 bootClasspathTag = dependencyTag{name: "bootclasspath"}
484 systemModulesTag = dependencyTag{name: "system modules"}
485 frameworkResTag = dependencyTag{name: "framework-res"}
486 frameworkApkTag = dependencyTag{name: "framework-apk"}
487 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800488 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700489 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
490 certificateTag = dependencyTag{name: "certificate"}
491 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700492 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700493)
Colin Cross2fe66872015-03-30 17:20:39 -0700494
Colin Crossfc3674a2017-09-18 17:41:52 -0700495type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700496 useModule, useFiles, useDefaultLibs, invalidVersion bool
497
Colin Cross6cef4812019-10-17 14:23:50 -0700498 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
499 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100500
501 // The default system modules to use. Will be an empty string if no system
502 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700503 systemModules string
504
Colin Cross6cef4812019-10-17 14:23:50 -0700505 // The modules that will be added ot the classpath when targeting 1.9 or higher
506 java9Classpath []string
507
Colin Crossa97c5d32018-03-28 14:58:31 -0700508 frameworkResModule string
509
Colin Cross86a60ae2018-05-29 14:44:55 -0700510 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700511 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100512
513 noStandardLibs, noFrameworksLibs bool
514}
515
516func (s sdkDep) hasStandardLibs() bool {
517 return !s.noStandardLibs
518}
519
520func (s sdkDep) hasFrameworkLibs() bool {
521 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700522}
523
Colin Crossa4f08812018-10-02 22:03:40 -0700524type jniLib struct {
525 name string
526 path android.Path
527 target android.Target
528}
529
Colin Cross0ea8ba82019-06-06 14:33:29 -0700530func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800531 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
532}
533
Colin Cross0ea8ba82019-06-06 14:33:29 -0700534func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800535 return j.shouldInstrument(ctx) &&
536 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
537 ctx.Config().UnbundledBuild())
538}
539
Colin Cross83bb3162018-06-25 15:48:06 -0700540func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900541 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700542}
543
Paul Duffine25c6442019-10-11 13:50:28 +0100544func (j *Module) systemModules() string {
545 return proptools.String(j.deviceProperties.System_modules)
546}
547
Colin Cross83bb3162018-06-25 15:48:06 -0700548func (j *Module) minSdkVersion() string {
549 if j.deviceProperties.Min_sdk_version != nil {
550 return *j.deviceProperties.Min_sdk_version
551 }
552 return j.sdkVersion()
553}
554
Dan Willemsen419290a2018-10-31 15:28:47 -0700555func (j *Module) targetSdkVersion() string {
556 if j.deviceProperties.Target_sdk_version != nil {
557 return *j.deviceProperties.Target_sdk_version
558 }
559 return j.sdkVersion()
560}
561
Colin Crossbe1da472017-07-07 15:59:46 -0700562func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700563 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100564 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700565 if sdkDep.useDefaultLibs {
566 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
567 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
568 if sdkDep.hasFrameworkLibs() {
569 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700570 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700571 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700572 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100573 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700574 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700575 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
576 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
577 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
578 }
Colin Cross2fe66872015-03-30 17:20:39 -0700579 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700580
Nan Zhangb2b33de2018-02-23 11:18:47 -0800581 if ctx.ModuleName() == "android_stubs_current" ||
582 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700583 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700584 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800585 }
Colin Cross2fe66872015-03-30 17:20:39 -0700586 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700587
Colin Cross42d48b72018-08-29 14:10:52 -0700588 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
589 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700590
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700591 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000592 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800593
Colin Crossfe17f6f2019-03-28 19:30:56 -0700594 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700595 if j.hasSrcExt(".proto") {
596 protoDeps(ctx, &j.protoProperties)
597 }
Colin Cross93e85952017-08-15 13:34:18 -0700598
599 if j.hasSrcExt(".kt") {
600 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
601 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700602 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
603 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800604 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800605 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
606 }
Colin Cross93e85952017-08-15 13:34:18 -0700607 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800608
609 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700610 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800611 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700612}
613
614func hasSrcExt(srcs []string, ext string) bool {
615 for _, src := range srcs {
616 if filepath.Ext(src) == ext {
617 return true
618 }
619 }
620
621 return false
622}
623
624func (j *Module) hasSrcExt(ext string) bool {
625 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700626}
627
Colin Cross46c9b8b2017-06-22 16:51:17 -0700628func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700629 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700630
Colin Crossebe1a512017-11-14 13:12:14 -0800631 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
632 aidlIncludes = append(aidlIncludes,
633 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
634 aidlIncludes = append(aidlIncludes,
635 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700636
Colin Cross3047fa22019-04-18 10:56:44 -0700637 var flags []string
638 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700639
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700640 if aidlPreprocess.Valid() {
641 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700642 deps = append(deps, aidlPreprocess.Path())
643 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700644 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700645 }
646
Colin Cross3047fa22019-04-18 10:56:44 -0700647 if len(j.exportAidlIncludeDirs) > 0 {
648 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
649 }
650
651 if len(aidlIncludes) > 0 {
652 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
653 }
654
Colin Cross635c3b02016-05-18 15:37:25 -0700655 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800656 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700657 flags = append(flags, "-I"+src.String())
658 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700659
Martijn Coeneneab15642018-03-09 09:29:59 +0100660 if Bool(j.deviceProperties.Aidl.Generate_traces) {
661 flags = append(flags, "-t")
662 }
663
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100664 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
665 flags = append(flags, "--transaction_names")
666 }
667
Colin Cross3047fa22019-04-18 10:56:44 -0700668 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700669}
670
Colin Cross32f676a2017-09-06 13:41:06 -0700671type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800672 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700673 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800674 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700675 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800676 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700677 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700678 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700679 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700680 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800681 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700682 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700683 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700684 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700685 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800686 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800687
688 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700689}
Colin Cross2fe66872015-03-30 17:20:39 -0700690
Colin Cross54250902017-12-05 09:28:08 -0800691func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
692 for _, f := range dep.Srcs() {
693 if f.Ext() != ".jar" {
694 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
695 ctx.OtherModuleName(dep.(blueprint.Module)))
696 }
697 }
698}
699
Jiyong Park2d492942018-03-05 17:44:10 +0900700type linkType int
701
702const (
703 javaCore linkType = iota
704 javaSdk
705 javaSystem
706 javaPlatform
707)
708
Jeongik Cha75b83b02019-11-01 15:28:00 +0900709type linkTypeContext interface {
710 android.Module
711 getLinkType(name string) (ret linkType, stubs bool)
712}
713
714func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700715 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700716 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900717 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
718 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100719 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900720 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100721 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900722 return javaCore, false
723 case name == "android_system_stubs_current":
724 return javaSystem, true
725 case strings.HasPrefix(ver, "system_"):
726 return javaSystem, false
727 case name == "android_test_stubs_current":
728 return javaSystem, true
729 case strings.HasPrefix(ver, "test_"):
730 return javaPlatform, false
731 case name == "android_stubs_current":
732 return javaSdk, true
733 case ver == "current":
734 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100735 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900736 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700737 default:
738 if _, err := strconv.Atoi(ver); err != nil {
739 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
740 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900741 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900742 }
743}
744
Jeongik Cha75b83b02019-11-01 15:28:00 +0900745func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700746 if ctx.Host() {
747 return
748 }
749
Jeongik Cha75b83b02019-11-01 15:28:00 +0900750 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900751 if stubs {
752 return
753 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900754 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900755 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."
756
757 switch myLinkType {
758 case javaCore:
759 if otherLinkType != javaCore {
760 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 +0900761 ctx.OtherModuleName(to))
762 }
Jiyong Park2d492942018-03-05 17:44:10 +0900763 break
764 case javaSdk:
765 if otherLinkType != javaCore && otherLinkType != javaSdk {
766 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
767 ctx.OtherModuleName(to))
768 }
769 break
770 case javaSystem:
771 if otherLinkType == javaPlatform {
772 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
773 ctx.OtherModuleName(to))
774 }
775 break
776 case javaPlatform:
777 // no restriction on link-type
778 break
Jiyong Park750e5572018-01-31 00:20:13 +0900779 }
780}
781
Colin Cross32f676a2017-09-06 13:41:06 -0700782func (j *Module) collectDeps(ctx android.ModuleContext) deps {
783 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700784
Colin Cross300f0382018-03-06 13:11:51 -0800785 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700786 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800787 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700788 ctx.AddMissingDependencies(sdkDep.bootclasspath)
789 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800790 } else if sdkDep.useFiles {
791 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700792 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700793 deps.aidlPreprocess = sdkDep.aidl
794 } else {
795 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800796 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700797 }
798
Colin Crossd11fcda2017-10-23 17:59:01 -0700799 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700800 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700801 tag := ctx.OtherModuleDependencyTag(module)
802
Colin Crossa4f08812018-10-02 22:03:40 -0700803 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700804 // Handled by AndroidApp.collectAppDeps
805 return
806 }
807 if tag == certificateTag {
808 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700809 return
810 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900811 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900812 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900813 if to, ok := module.(linkTypeContext); ok {
814 switch tag {
815 case bootClasspathTag, libTag, staticLibTag:
816 checkLinkType(ctx, j, to, tag.(dependencyTag))
817 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700818 }
Jiyong Park750e5572018-01-31 00:20:13 +0900819 }
Colin Cross54250902017-12-05 09:28:08 -0800820 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800821 case SdkLibraryDependency:
822 switch tag {
823 case libTag:
824 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
825 // names of sdk libs that are directly depended are exported
826 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700827 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800828 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
829 }
Colin Cross54250902017-12-05 09:28:08 -0800830 case Dependency:
831 switch tag {
832 case bootClasspathTag:
833 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700834 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800835 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900836 // sdk lib names from dependencies are re-exported
837 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700838 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000839 pluginJars, pluginClasses := dep.ExportedPlugins()
840 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700841 case java9LibTag:
842 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800843 case staticLibTag:
844 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
845 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
846 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700847 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900848 // sdk lib names from dependencies are re-exported
849 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700850 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000851 pluginJars, pluginClasses := dep.ExportedPlugins()
852 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800853 case pluginTag:
854 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800855 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000856 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
857 } else {
858 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800859 }
860 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
861 } else {
862 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
863 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000864 case exportedPluginTag:
865 if plugin, ok := dep.(*Plugin); ok {
866 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
867 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
868 }
869 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
870 if plugin.pluginProperties.Processor_class != nil {
871 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
872 }
873 } else {
874 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
875 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800876 case frameworkApkTag:
877 if ctx.ModuleName() == "android_stubs_current" ||
878 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700879 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800880 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
881 // resource files out of there for aapt.
882 //
883 // Normally the package rule runs aapt, which includes the resource,
884 // but we're not running that in our package rule so just copy in the
885 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700886 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800887 }
Colin Cross54250902017-12-05 09:28:08 -0800888 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700889 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800890 case kotlinAnnotationsTag:
891 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800892 }
893
Colin Cross54250902017-12-05 09:28:08 -0800894 case android.SourceFileProducer:
895 switch tag {
896 case libTag:
897 checkProducesJars(ctx, dep)
898 deps.classpath = append(deps.classpath, dep.Srcs()...)
899 case staticLibTag:
900 checkProducesJars(ctx, dep)
901 deps.classpath = append(deps.classpath, dep.Srcs()...)
902 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
903 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800904 }
905 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700906 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100907 case bootClasspathTag:
908 // If a system modules dependency has been added to the bootclasspath
909 // then add its libs to the bootclasspath.
910 sm := module.(*SystemModules)
911 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
912
Colin Cross1369cdb2017-09-29 17:58:17 -0700913 case systemModulesTag:
914 if deps.systemModules != nil {
915 panic("Found two system module dependencies")
916 }
917 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000918 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700919 panic("Missing directory for system module dependency")
920 }
Colin Crossb77043e2019-07-16 13:57:13 -0700921 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700922 }
Colin Crossec7a0422017-07-07 14:47:12 -0700923 }
Colin Cross2fe66872015-03-30 17:20:39 -0700924 })
925
Jiyong Park1be96912018-05-28 18:02:19 +0900926 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
927
Colin Cross32f676a2017-09-06 13:41:06 -0700928 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700929}
930
Artur Satayev9cf46692019-11-26 18:08:34 +0000931func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
932 deps.processorPath = append(deps.processorPath, pluginJars...)
933 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
934}
935
Colin Cross1e743852019-10-28 11:37:20 -0700936func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800937 v := sdkContext.sdkVersion()
938 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100939 if ctx.Config().IsPdkBuild() &&
940 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700941 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800942 latestSdkVersion := 0
943 if len(sdkVersions) > 0 {
944 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
945 }
946 v = strconv.Itoa(latestSdkVersion)
947 }
948
949 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700950 if err != nil {
951 ctx.PropertyErrorf("sdk_version", "%s", err)
952 }
Nan Zhang357466b2018-04-17 17:38:36 -0700953 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700954 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700955 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -0700956 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100957 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -0700958 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -0700959 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
960 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -0700961 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -0700962 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700963 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700964 }
Nan Zhang357466b2018-04-17 17:38:36 -0700965}
966
Colin Cross1e743852019-10-28 11:37:20 -0700967type javaVersion int
968
969const (
970 JAVA_VERSION_UNSUPPORTED = 0
971 JAVA_VERSION_6 = 6
972 JAVA_VERSION_7 = 7
973 JAVA_VERSION_8 = 8
974 JAVA_VERSION_9 = 9
975)
976
977func (v javaVersion) String() string {
978 switch v {
979 case JAVA_VERSION_6:
980 return "1.6"
981 case JAVA_VERSION_7:
982 return "1.7"
983 case JAVA_VERSION_8:
984 return "1.8"
985 case JAVA_VERSION_9:
986 return "1.9"
987 default:
988 return "unsupported"
989 }
990}
991
992// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
993func (v javaVersion) usesJavaModules() bool {
994 return v >= 9
995}
996
997func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100998 switch javaVersion {
999 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001000 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001001 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001002 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001003 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001004 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001005 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001006 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001007 case "10", "11":
1008 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001009 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001010 default:
1011 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001012 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001013 }
1014}
1015
Nan Zhanged19fc32017-10-19 13:06:22 -07001016func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001017
Colin Crossf03c82b2015-04-13 13:53:40 -07001018 var flags javaBuilderFlags
1019
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001020 // javaVersion flag.
1021 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1022
Nan Zhanged19fc32017-10-19 13:06:22 -07001023 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001024 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001025 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001026 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001027 }
Colin Cross6510f912017-11-29 00:27:14 -08001028 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001029 // Override the -g flag passed globally to remove local variable debug info to reduce
1030 // disk and memory usage.
1031 javacFlags = append(javacFlags, "-g:source,lines")
1032 }
Colin Crossc228a702019-11-06 16:18:05 -08001033 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001034
Colin Cross66548102018-06-19 22:47:35 -07001035 if ctx.Config().RunErrorProne() {
1036 if config.ErrorProneClasspath == nil {
1037 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1038 }
1039
1040 errorProneFlags := []string{
1041 "-Xplugin:ErrorProne",
1042 "${config.ErrorProneChecks}",
1043 }
1044 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1045
1046 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1047 "'" + strings.Join(errorProneFlags, " ") + "'"
1048 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001049 }
1050
Nan Zhanged19fc32017-10-19 13:06:22 -07001051 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001052 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1053 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001054 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001055 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001056
Colin Crossbe9cdb82019-01-21 21:37:16 -08001057 flags.processor = strings.Join(deps.processorClasses, ",")
1058
Colin Cross1e743852019-10-28 11:37:20 -07001059 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1060 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001061 // Give host-side tools a version of OpenJDK's standard libraries
1062 // close to what they're targeting. As of Dec 2017, AOSP is only
1063 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1064 //
1065 // When building with OpenJDK 8, the following should have no
1066 // effect since those jars would be available by default.
1067 //
1068 // When building with OpenJDK 9 but targeting a version < 1.8,
1069 // putting them on the bootclasspath means that:
1070 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1071 // b) references to existing APIs are not reinterpreted in an
1072 // OpenJDK 9-specific way, eg. calls to subclasses of
1073 // java.nio.Buffer as in http://b/70862583
1074 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1075 flags.bootClasspath = append(flags.bootClasspath,
1076 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1077 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001078 if Bool(j.properties.Use_tools_jar) {
1079 flags.bootClasspath = append(flags.bootClasspath,
1080 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1081 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001082 }
1083
Colin Cross1e743852019-10-28 11:37:20 -07001084 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001085 // Manually specify build directory in case it is not under the repo root.
1086 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1087 // just adding a symlink under the root doesn't help.)
1088 patchPaths := ".:" + ctx.Config().BuildDir()
1089 classPath := flags.classpath.FormJavaClassPath("")
1090 if classPath != "" {
1091 patchPaths += ":" + classPath
1092 }
1093 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001094 }
1095
Nan Zhanged19fc32017-10-19 13:06:22 -07001096 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001097 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001098
Nan Zhanged19fc32017-10-19 13:06:22 -07001099 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001100 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001101
Colin Cross81440082018-08-15 20:21:55 -07001102 if len(javacFlags) > 0 {
1103 // optimization.
1104 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1105 flags.javacFlags = "$javacFlags"
1106 }
1107
Nan Zhanged19fc32017-10-19 13:06:22 -07001108 return flags
1109}
Colin Crossc0b06f12015-04-08 13:03:43 -07001110
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001111func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001112 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001113
1114 deps := j.collectDeps(ctx)
1115 flags := j.collectBuilderFlags(ctx, deps)
1116
Colin Cross1e743852019-10-28 11:37:20 -07001117 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001118 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1119 }
Colin Cross8a497952019-03-05 22:25:09 -08001120 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001121 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001122 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001123 }
1124
Colin Crossaf050172017-11-15 23:01:59 -08001125 srcFiles = j.genSources(ctx, srcFiles, flags)
1126
1127 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001128 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001129 if aaptSrcJar != nil {
1130 srcJars = append(srcJars, aaptSrcJar)
1131 }
Colin Crossb7a63242015-04-16 14:09:14 -07001132
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001133 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001134 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001135 }
1136
Colin Cross1ee23172017-10-18 14:44:18 -07001137 jarName := ctx.ModuleName() + ".jar"
1138
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001139 javaSrcFiles := srcFiles.FilterByExt(".java")
1140 var uniqueSrcFiles android.Paths
1141 set := make(map[string]bool)
1142 for _, v := range javaSrcFiles {
1143 if _, found := set[v.String()]; !found {
1144 set[v.String()] = true
1145 uniqueSrcFiles = append(uniqueSrcFiles, v)
1146 }
1147 }
1148
patricktu242faad2019-09-24 15:41:30 +08001149 // Collect .java files for AIDEGen
1150 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1151
Colin Cross55f63ea2018-08-27 12:37:09 -07001152 var kotlinJars android.Paths
1153
Colin Cross93e85952017-08-15 13:34:18 -07001154 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001155 // user defined kotlin flags.
1156 kotlincFlags := j.properties.Kotlincflags
1157 CheckKotlincFlags(ctx, kotlincFlags)
1158
Colin Cross93e85952017-08-15 13:34:18 -07001159 // If there are kotlin files, compile them first but pass all the kotlin and java files
1160 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1161 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001162 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001163 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001164 kotlincFlags = append(kotlincFlags, "-no-jdk")
1165 }
1166 if len(kotlincFlags) > 0 {
1167 // optimization.
1168 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1169 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001170 }
1171
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001172 var kotlinSrcFiles android.Paths
1173 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1174 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1175
patricktu242faad2019-09-24 15:41:30 +08001176 // Collect .kt files for AIDEGen
1177 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1178
Colin Crossafbb1732019-01-17 15:42:52 -08001179 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1180 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1181
1182 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1183 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1184
1185 if len(flags.processorPath) > 0 {
1186 // Use kapt for annotation processing
1187 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1188 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1189 srcJars = append(srcJars, kaptSrcJar)
1190 // Disable annotation processing in javac, it's already been handled by kapt
1191 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001192 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001193 }
Colin Cross93e85952017-08-15 13:34:18 -07001194
Colin Cross1ee23172017-10-18 14:44:18 -07001195 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001196 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001197 if ctx.Failed() {
1198 return
1199 }
1200
1201 // Make javac rule depend on the kotlinc rule
1202 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001203
Colin Cross93e85952017-08-15 13:34:18 -07001204 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001205 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001206 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001207 }
1208
Colin Cross55f63ea2018-08-27 12:37:09 -07001209 jars := append(android.Paths(nil), kotlinJars...)
1210
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001211 // Store the list of .java files that was passed to javac
1212 j.compiledJavaSrcs = uniqueSrcFiles
1213 j.compiledSrcJars = srcJars
1214
Nan Zhang61eaedb2017-11-02 13:28:15 -07001215 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001216 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001217 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1218 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001219 // Formerly, there was a check here that prevented annotation processors
1220 // from being used when sharding was enabled, as some annotation processors
1221 // do not function correctly in sharded environments. It was removed to
1222 // allow for the use of annotation processors that do function correctly
1223 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001224 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001225 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001226 if ctx.Failed() {
1227 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001228 }
1229 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001230 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001231 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001232 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001233 // If error-prone is enabled, add an additional rule to compile the java files into
1234 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001235 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001236 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1237 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001238 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001239 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001240 extraJarDeps = append(extraJarDeps, errorprone)
1241 }
1242
Nan Zhang61eaedb2017-11-02 13:28:15 -07001243 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001244 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001245 shardSize := int(*(j.properties.Javac_shard_size))
1246 var shardSrcs []android.Paths
1247 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001248 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001249 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001250 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1251 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001252 jars = append(jars, classes)
1253 }
1254 }
1255 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001256 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1257 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001258 jars = append(jars, classes)
1259 }
1260 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001261 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001262 jars = append(jars, classes)
1263 }
Colin Crossd6891432017-09-27 17:39:56 -07001264 if ctx.Failed() {
1265 return
1266 }
Colin Cross2fe66872015-03-30 17:20:39 -07001267 }
1268
Colin Cross0c4ce212019-05-03 15:28:19 -07001269 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1270
1271 var includeSrcJar android.WritablePath
1272 if Bool(j.properties.Include_srcs) {
1273 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1274 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1275 }
1276
Colin Crosscedd4762018-09-13 11:26:19 -07001277 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1278 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001279 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001280 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001281
1282 var resArgs []string
1283 var resDeps android.Paths
1284
1285 resArgs = append(resArgs, dirArgs...)
1286 resDeps = append(resDeps, dirDeps...)
1287
1288 resArgs = append(resArgs, fileArgs...)
1289 resDeps = append(resDeps, fileDeps...)
1290
Colin Cross988708c2019-05-06 14:04:11 -07001291 resArgs = append(resArgs, extraArgs...)
1292 resDeps = append(resDeps, extraDeps...)
1293
Colin Cross40a36712017-09-27 17:41:35 -07001294 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001295 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001296 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001297 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001298 if ctx.Failed() {
1299 return
1300 }
1301 }
1302
Colin Cross0c4ce212019-05-03 15:28:19 -07001303 var resourceJars android.Paths
1304 if j.resourceJar != nil {
1305 resourceJars = append(resourceJars, j.resourceJar)
1306 }
1307 if Bool(j.properties.Include_srcs) {
1308 resourceJars = append(resourceJars, includeSrcJar)
1309 }
1310 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001311
Colin Cross0c4ce212019-05-03 15:28:19 -07001312 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001313 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001314 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001315 false, nil, nil)
1316 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001317 } else if len(resourceJars) == 1 {
1318 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001319 }
1320
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001321 if len(deps.staticJars) > 0 {
1322 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001323 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001324
Colin Cross094054a2018-10-17 15:10:48 -07001325 manifest := j.overrideManifest
1326 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001327 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001328 }
Colin Cross635acc92017-09-12 22:50:46 -07001329
Colin Cross8a497952019-03-05 22:25:09 -08001330 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001331 if len(services) > 0 {
1332 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1333 var zipargs []string
1334 for _, file := range services {
1335 serviceFile := file.String()
1336 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1337 }
1338 ctx.Build(pctx, android.BuildParams{
1339 Rule: zip,
1340 Output: servicesJar,
1341 Implicits: services,
1342 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001343 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001344 },
1345 })
1346 jars = append(jars, servicesJar)
1347 }
1348
Colin Cross0a6e0072017-08-30 14:24:55 -07001349 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001350 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001351 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001352
1353 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001354 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1355 // Optimization: skip the combine step if there is nothing to do
1356 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1357 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1358 // any if len(jars) == 1.
1359 outputFile = moduleOutPath
1360 } else {
1361 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1362 ctx.Build(pctx, android.BuildParams{
1363 Rule: android.Cp,
1364 Input: jars[0],
1365 Output: combinedJar,
1366 })
1367 outputFile = combinedJar
1368 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001369 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001370 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001371 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001372 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001373 outputFile = combinedJar
1374 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001375
Colin Cross331a1212018-08-15 20:40:52 -07001376 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001377 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001378 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001379 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001380 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001381 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001382
1383 // jarjar resource jar if necessary
1384 if j.resourceJar != nil {
1385 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001386 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001387 j.resourceJar = resourceJarJarFile
1388 }
1389
Colin Cross0a6e0072017-08-30 14:24:55 -07001390 if ctx.Failed() {
1391 return
1392 }
1393 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001394
1395 // Check package restrictions if necessary.
1396 if len(j.properties.Permitted_packages) > 0 {
1397 // Check packages and copy to package-checked file.
1398 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1399 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1400 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1401
1402 if ctx.Failed() {
1403 return
1404 }
1405 }
1406
Nan Zhanged19fc32017-10-19 13:06:22 -07001407 j.implementationJarFile = outputFile
1408 if j.headerJarFile == nil {
1409 j.headerJarFile = j.implementationJarFile
1410 }
Colin Cross2fe66872015-03-30 17:20:39 -07001411
Colin Cross6510f912017-11-29 00:27:14 -08001412 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001413 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1414 j.properties.Instrument = true
1415 }
1416 }
1417
Colin Cross3144dfc2018-01-03 15:06:47 -08001418 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001419 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1420 }
1421
Colin Cross331a1212018-08-15 20:40:52 -07001422 // merge implementation jar with resources if necessary
1423 implementationAndResourcesJar := outputFile
1424 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001425 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001426 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001427 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001428 false, nil, nil)
1429 implementationAndResourcesJar = combinedJar
1430 }
1431
1432 j.implementationAndResourcesJar = implementationAndResourcesJar
1433
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001434 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001435 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001436 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001437 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001438 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001439 if ctx.Failed() {
1440 return
1441 }
Colin Cross331a1212018-08-15 20:40:52 -07001442
Jiyong Park09cb6292019-07-15 15:29:23 +09001443 // Hidden API CSV generation and dex encoding
1444 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1445 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001446
Colin Cross331a1212018-08-15 20:40:52 -07001447 // merge dex jar with resources if necessary
1448 if j.resourceJar != nil {
1449 jars := android.Paths{dexOutputFile, j.resourceJar}
1450 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1451 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1452 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001453 if j.deviceProperties.UncompressDex {
1454 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1455 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1456 dexOutputFile = combinedAlignedJar
1457 } else {
1458 dexOutputFile = combinedJar
1459 }
Colin Cross331a1212018-08-15 20:40:52 -07001460 }
1461
1462 j.dexJarFile = dexOutputFile
1463
Colin Cross8faf8fc2019-01-16 15:15:52 -08001464 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001465 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1466
1467 j.maybeStrippedDexJarFile = dexOutputFile
1468
Colin Cross3063b782018-08-15 11:19:12 -07001469 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001470
1471 if ctx.Failed() {
1472 return
1473 }
Colin Cross331a1212018-08-15 20:40:52 -07001474 } else {
1475 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001476 }
Colin Cross331a1212018-08-15 20:40:52 -07001477
Colin Crossb7a63242015-04-16 14:09:14 -07001478 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001479
1480 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1481 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001482}
1483
Colin Cross3b706fd2019-09-05 16:44:18 -07001484func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1485 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1486
1487 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1488 if idx >= 0 {
1489 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1490 jarName += strconv.Itoa(idx)
1491 }
1492
1493 classes := android.PathForModuleOut(ctx, "javac", jarName)
1494 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1495
1496 if ctx.Config().EmitXrefRules() {
1497 extractionFile := android.PathForModuleOut(ctx, kzipName)
1498 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1499 j.kytheFiles = append(j.kytheFiles, extractionFile)
1500 }
1501
1502 return classes
1503}
1504
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001505// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1506// since some of these flags may be used internally.
1507func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1508 for _, flag := range flags {
1509 flag = strings.TrimSpace(flag)
1510
1511 if !strings.HasPrefix(flag, "-") {
1512 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1513 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1514 ctx.PropertyErrorf("kotlincflags",
1515 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1516 } else if inList(flag, config.KotlincIllegalFlags) {
1517 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1518 } else if flag == "-include-runtime" {
1519 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1520 } else {
1521 args := strings.Split(flag, " ")
1522 if args[0] == "-kotlin-home" {
1523 ctx.PropertyErrorf("kotlincflags",
1524 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1525 }
1526 }
1527 }
1528}
1529
Colin Cross8eadbf02017-10-24 17:46:00 -07001530func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001531 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001532
1533 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001534 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001535 // Compile java sources into turbine.jar.
1536 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1537 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1538 if ctx.Failed() {
1539 return nil
1540 }
1541 jars = append(jars, turbineJar)
1542 }
1543
Colin Cross55f63ea2018-08-27 12:37:09 -07001544 jars = append(jars, extraJars...)
1545
Nan Zhanged19fc32017-10-19 13:06:22 -07001546 // Combine any static header libraries into classes-header.jar. If there is only
1547 // one input jar this step will be skipped.
1548 var headerJar android.Path
1549 jars = append(jars, deps.staticHeaderJars...)
1550
Colin Cross5c6ecc12017-10-23 18:12:27 -07001551 // we cannot skip the combine step for now if there is only one jar
1552 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1553 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001554 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001555 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001556 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001557
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001558 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001559 // Transform classes.jar into classes-jarjar.jar
1560 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001561 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001562 headerJar = jarjarFile
1563 if ctx.Failed() {
1564 return nil
1565 }
1566 }
1567
1568 return headerJar
1569}
1570
Colin Crosscb933592017-11-22 13:49:43 -08001571func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001572 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001573
Colin Cross7a3139e2017-12-19 13:57:50 -08001574 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001575
Colin Cross84c38822018-01-03 15:59:46 -08001576 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001577 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1578
1579 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1580
1581 j.jacocoReportClassesFile = jacocoReportClassesFile
1582
1583 return instrumentedJar
1584}
1585
albaltai36ff7dc2018-12-25 14:35:23 +08001586var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001587
Nan Zhanged19fc32017-10-19 13:06:22 -07001588func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001589 if j.headerJarFile == nil {
1590 return nil
1591 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001592 return android.Paths{j.headerJarFile}
1593}
1594
1595func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001596 if j.implementationJarFile == nil {
1597 return nil
1598 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001599 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001600}
1601
Colin Crossf24a22a2019-01-31 14:12:44 -08001602func (j *Module) DexJar() android.Path {
1603 return j.dexJarFile
1604}
1605
Colin Cross331a1212018-08-15 20:40:52 -07001606func (j *Module) ResourceJars() android.Paths {
1607 if j.resourceJar == nil {
1608 return nil
1609 }
1610 return android.Paths{j.resourceJar}
1611}
1612
1613func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001614 if j.implementationAndResourcesJar == nil {
1615 return nil
1616 }
Colin Cross331a1212018-08-15 20:40:52 -07001617 return android.Paths{j.implementationAndResourcesJar}
1618}
1619
Colin Cross46c9b8b2017-06-22 16:51:17 -07001620func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001621 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001622 return j.exportAidlIncludeDirs
1623}
1624
Jiyong Park1be96912018-05-28 18:02:19 +09001625func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001626 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001627 return j.exportedSdkLibs
1628}
1629
Artur Satayev9cf46692019-11-26 18:08:34 +00001630func (j *Module) ExportedPlugins() (android.Paths, []string) {
1631 return j.exportedPluginJars, j.exportedPluginClasses
1632}
1633
Colin Cross0c4ce212019-05-03 15:28:19 -07001634func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1635 return j.srcJarArgs, j.srcJarDeps
1636}
1637
Colin Cross46c9b8b2017-06-22 16:51:17 -07001638var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001639
Colin Cross46c9b8b2017-06-22 16:51:17 -07001640func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001641 return j.logtagsSrcs
1642}
1643
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001644// Collect information for opening IDE project files in java/jdeps.go.
1645func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1646 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1647 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001648 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001649 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001650 if j.expandJarjarRules != nil {
1651 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001652 }
1653}
1654
1655func (j *Module) CompilerDeps() []string {
1656 jdeps := []string{}
1657 jdeps = append(jdeps, j.properties.Libs...)
1658 jdeps = append(jdeps, j.properties.Static_libs...)
1659 return jdeps
1660}
1661
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001662func (j *Module) hasCode(ctx android.ModuleContext) bool {
1663 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1664 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1665}
1666
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001667func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1668 depTag := ctx.OtherModuleDependencyTag(dep)
1669 // dependencies other than the static linkage are all considered crossing APEX boundary
1670 return depTag == staticLibTag
1671}
1672
Jiyong Park0b238752019-10-29 11:23:10 +09001673func (j *Module) Stem() string {
1674 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1675}
1676
Colin Cross2fe66872015-03-30 17:20:39 -07001677//
1678// Java libraries (.jar file)
1679//
1680
Colin Crossf506d872017-07-19 15:53:04 -07001681type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001682 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001683
1684 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001685}
1686
Colin Cross42be7612019-02-21 18:12:14 -08001687func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001688 // Store uncompressed (and do not strip) dex files from boot class path jars.
1689 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1690 return true
1691 }
1692
1693 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001694 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001695 return true
1696 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001697 if ctx.Config().UncompressPrivAppDex() &&
1698 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1699 return true
1700 }
1701
Colin Cross2fc72f62018-12-21 12:59:54 -08001702 return false
1703}
1704
Colin Crossf506d872017-07-19 15:53:04 -07001705func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001706 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001707 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001708 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001709 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001710 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001711 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001712 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001713
Jiyong Park7f7766d2019-07-25 22:02:35 +09001714 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1715 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001716 var extraInstallDeps android.Paths
1717 if j.InstallMixin != nil {
1718 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1719 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001720 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001721 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001722 }
Colin Crossb7a63242015-04-16 14:09:14 -07001723}
1724
Colin Crossf506d872017-07-19 15:53:04 -07001725func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001726 j.deps(ctx)
1727}
1728
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001729const (
Paul Duffina0dbf432019-12-05 11:25:53 +00001730 aidlIncludeDir = "aidl"
1731 javaDir = "java"
1732 jarFileSuffix = ".jar"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001733)
1734
Paul Duffina0dbf432019-12-05 11:25:53 +00001735// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
1736func (j *Library) sdkSnapshotFilePathForJar() string {
1737 return filepath.Join(javaDir, j.Name()+jarFileSuffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001738}
1739
Paul Duffin13879572019-11-28 14:31:38 +00001740type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001741 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001742}
1743
1744func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1745 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1746}
1747
1748func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1749 _, ok := module.(*Library)
1750 return ok
1751}
1752
Paul Duffina0dbf432019-12-05 11:25:53 +00001753func (mt *librarySdkMemberType) buildSnapshot(
1754 sdkModuleContext android.ModuleContext,
1755 builder android.SnapshotBuilder,
1756 member android.SdkMember,
1757 jarToExportGetter func(j *Library) android.Path) {
1758
Paul Duffin13879572019-11-28 14:31:38 +00001759 variants := member.Variants()
1760 if len(variants) != 1 {
1761 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1762 for _, variant := range variants {
1763 sdkModuleContext.ModuleErrorf(" %q", variant)
1764 }
1765 }
1766 variant := variants[0]
1767 j := variant.(*Library)
1768
Paul Duffina0dbf432019-12-05 11:25:53 +00001769 exportedJar := jarToExportGetter(j)
1770 snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
1771 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001772
1773 for _, dir := range j.AidlIncludeDirs() {
1774 // TODO(jiyong): copy parcelable declarations only
1775 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1776 for _, file := range aidlFiles {
1777 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1778 }
1779 }
1780
Paul Duffin9d8d6092019-12-05 18:19:29 +00001781 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001782 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001783}
1784
Paul Duffina0dbf432019-12-05 11:25:53 +00001785type headerLibrarySdkMemberType struct {
1786 librarySdkMemberType
1787}
1788
1789func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1790 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1791 headerJars := j.HeaderJars()
1792 if len(headerJars) != 1 {
1793 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1794 }
1795
1796 return headerJars[0]
1797 })
1798}
1799
Paul Duffina0dbf432019-12-05 11:25:53 +00001800type implLibrarySdkMemberType struct {
1801 librarySdkMemberType
1802}
1803
1804func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1805 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1806 implementationJars := j.ImplementationJars()
1807 if len(implementationJars) != 1 {
1808 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1809 }
1810
1811 return implementationJars[0]
1812 })
1813}
1814
Colin Cross1b16b0e2019-02-12 14:41:32 -08001815// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1816//
1817// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1818// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1819// as a `static_libs` dependency of another module.
1820//
1821// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1822// a device.
1823//
1824// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1825// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001826func LibraryFactory() android.Module {
1827 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001828
Colin Cross9ae1b922018-06-26 17:59:05 -07001829 module.AddProperties(
1830 &module.Module.properties,
1831 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001832 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001833 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001834
Jiyong Park7f7766d2019-07-25 22:02:35 +09001835 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001836 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001837 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001838 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001839}
1840
Colin Cross1b16b0e2019-02-12 14:41:32 -08001841// java_library_static is an obsolete alias for java_library.
1842func LibraryStaticFactory() android.Module {
1843 return LibraryFactory()
1844}
1845
1846// java_library_host builds and links sources into a `.jar` file for the host.
1847//
1848// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1849// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001850func LibraryHostFactory() android.Module {
1851 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001852
Colin Cross6af17aa2017-09-20 12:59:05 -07001853 module.AddProperties(
1854 &module.Module.properties,
1855 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001856
Colin Cross9ae1b922018-06-26 17:59:05 -07001857 module.Module.properties.Installable = proptools.BoolPtr(true)
1858
Jiyong Park7f7766d2019-07-25 22:02:35 +09001859 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001860 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001861 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001862}
1863
1864//
Colin Crossb628ea52018-08-14 16:42:33 -07001865// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001866//
1867
1868type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001869 // list of compatibility suites (for example "cts", "vts") that the module should be
1870 // installed into.
1871 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001872
1873 // the name of the test configuration (for example "AndroidTest.xml") that should be
1874 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001875 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001876
Jack He33338892018-09-19 02:21:28 -07001877 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1878 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001879 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001880
Colin Crossd96ca352018-08-10 16:06:24 -07001881 // list of files or filegroup modules that provide data that should be installed alongside
1882 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001883 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001884
1885 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1886 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1887 // explicitly.
1888 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001889}
1890
Paul Duffin42df1442019-03-20 12:45:53 +00001891type testHelperLibraryProperties struct {
1892 // list of compatibility suites (for example "cts", "vts") that the module should be
1893 // installed into.
1894 Test_suites []string `android:"arch_variant"`
1895}
1896
Colin Cross05638fc2018-04-09 18:40:24 -07001897type Test struct {
1898 Library
1899
1900 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001901
1902 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001903 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001904}
1905
Paul Duffin42df1442019-03-20 12:45:53 +00001906type TestHelperLibrary struct {
1907 Library
1908
1909 testHelperLibraryProperties testHelperLibraryProperties
1910}
1911
Colin Cross303e21f2018-08-07 16:49:25 -07001912func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001913 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1914 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001915 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001916
1917 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001918}
1919
Paul Duffin42df1442019-03-20 12:45:53 +00001920func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1921 j.Library.GenerateAndroidBuildActions(ctx)
1922}
1923
Colin Cross1b16b0e2019-02-12 14:41:32 -08001924// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1925// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1926//
1927// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1928// compiled against the device bootclasspath.
1929//
1930// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1931// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001932func TestFactory() android.Module {
1933 module := &Test{}
1934
1935 module.AddProperties(
1936 &module.Module.properties,
1937 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001938 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001939 &module.Module.protoProperties,
1940 &module.testProperties)
1941
Colin Cross9ae1b922018-06-26 17:59:05 -07001942 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001943 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001944
Colin Cross05638fc2018-04-09 18:40:24 -07001945 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001946 return module
1947}
1948
Paul Duffin42df1442019-03-20 12:45:53 +00001949// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1950func TestHelperLibraryFactory() android.Module {
1951 module := &TestHelperLibrary{}
1952
1953 module.AddProperties(
1954 &module.Module.properties,
1955 &module.Module.deviceProperties,
1956 &module.Module.dexpreoptProperties,
1957 &module.Module.protoProperties,
1958 &module.testHelperLibraryProperties)
1959
Colin Cross9a4abed2019-04-24 13:19:28 -07001960 module.Module.properties.Installable = proptools.BoolPtr(true)
1961 module.Module.dexpreopter.isTest = true
1962
Paul Duffin42df1442019-03-20 12:45:53 +00001963 InitJavaModule(module, android.HostAndDeviceSupported)
1964 return module
1965}
1966
Colin Cross1b16b0e2019-02-12 14:41:32 -08001967// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1968// allow running the test with `atest` or a `TEST_MAPPING` file.
1969//
1970// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1971// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001972func TestHostFactory() android.Module {
1973 module := &Test{}
1974
1975 module.AddProperties(
1976 &module.Module.properties,
1977 &module.Module.protoProperties,
1978 &module.testProperties)
1979
Colin Cross9ae1b922018-06-26 17:59:05 -07001980 module.Module.properties.Installable = proptools.BoolPtr(true)
1981
Colin Cross05638fc2018-04-09 18:40:24 -07001982 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001983 return module
1984}
1985
1986//
Colin Cross2fe66872015-03-30 17:20:39 -07001987// Java Binaries (.jar file plus wrapper script)
1988//
1989
Colin Crossf506d872017-07-19 15:53:04 -07001990type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001991 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001992 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001993
1994 // Name of the class containing main to be inserted into the manifest as Main-Class.
1995 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001996}
1997
Colin Crossf506d872017-07-19 15:53:04 -07001998type Binary struct {
1999 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002000
Colin Crossf506d872017-07-19 15:53:04 -07002001 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002002
Colin Cross6b4a32d2017-12-05 13:42:45 -08002003 isWrapperVariant bool
2004
Colin Crossc3315992017-12-08 19:12:36 -08002005 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002006 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002007}
2008
Alex Light24237172017-10-26 09:46:21 -07002009func (j *Binary) HostToolPath() android.OptionalPath {
2010 return android.OptionalPathForPath(j.binaryFile)
2011}
2012
Colin Crossf506d872017-07-19 15:53:04 -07002013func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002014 if ctx.Arch().ArchType == android.Common {
2015 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002016 if j.binaryProperties.Main_class != nil {
2017 if j.properties.Manifest != nil {
2018 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2019 }
2020 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2021 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2022 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2023 }
2024
Colin Cross6b4a32d2017-12-05 13:42:45 -08002025 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002026 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002027 // Handle the binary wrapper
2028 j.isWrapperVariant = true
2029
Colin Cross366938f2017-12-11 16:29:02 -08002030 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002031 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002032 } else {
2033 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2034 }
2035
2036 // Depend on the installed jar so that the wrapper doesn't get executed by
2037 // another build rule before the jar has been installed.
2038 jarFile := ctx.PrimaryModule().(*Binary).installFile
2039
2040 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2041 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002042 }
Colin Cross2fe66872015-03-30 17:20:39 -07002043}
2044
Colin Crossf506d872017-07-19 15:53:04 -07002045func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002046 if ctx.Arch().ArchType == android.Common {
2047 j.deps(ctx)
2048 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002049}
2050
Colin Cross1b16b0e2019-02-12 14:41:32 -08002051// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2052// as well.
2053//
2054// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2055// compiled against the device bootclasspath.
2056//
2057// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2058// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002059func BinaryFactory() android.Module {
2060 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002061
Colin Cross36242852017-06-23 15:06:31 -07002062 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002063 &module.Module.properties,
2064 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002065 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002066 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002067 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002068
Colin Cross9ae1b922018-06-26 17:59:05 -07002069 module.Module.properties.Installable = proptools.BoolPtr(true)
2070
Colin Cross6b4a32d2017-12-05 13:42:45 -08002071 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2072 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002073 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002074}
2075
Colin Cross1b16b0e2019-02-12 14:41:32 -08002076// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2077//
2078// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2079// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002080func BinaryHostFactory() android.Module {
2081 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002082
Colin Cross36242852017-06-23 15:06:31 -07002083 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002084 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002085 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002086 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002087
Colin Cross9ae1b922018-06-26 17:59:05 -07002088 module.Module.properties.Installable = proptools.BoolPtr(true)
2089
Colin Cross6b4a32d2017-12-05 13:42:45 -08002090 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2091 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002092 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002093}
2094
2095//
2096// Java prebuilts
2097//
2098
Colin Cross74d73e22017-08-02 11:05:49 -07002099type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002100 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002101
Nan Zhangea568a42017-11-08 21:20:04 -08002102 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002103
2104 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002105
2106 // List of shared java libs that this module has dependencies to
2107 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002108
2109 // List of files to remove from the jar file(s)
2110 Exclude_files []string
2111
2112 // List of directories to remove from the jar file(s)
2113 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002114
2115 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002116 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002117
2118 // set the name of the output
2119 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002120}
2121
2122type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002123 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002124 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002125 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002126 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002127 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002128
Colin Cross74d73e22017-08-02 11:05:49 -07002129 properties ImportProperties
2130
Colin Cross0a6e0072017-08-30 14:24:55 -07002131 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002132 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002133}
2134
Colin Cross83bb3162018-06-25 15:48:06 -07002135func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002136 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002137}
2138
2139func (j *Import) minSdkVersion() string {
2140 return j.sdkVersion()
2141}
2142
Colin Cross74d73e22017-08-02 11:05:49 -07002143func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002144 return &j.prebuilt
2145}
2146
Colin Cross74d73e22017-08-02 11:05:49 -07002147func (j *Import) PrebuiltSrcs() []string {
2148 return j.properties.Jars
2149}
2150
2151func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002152 return j.prebuilt.Name(j.ModuleBase.Name())
2153}
2154
Jiyong Park0b238752019-10-29 11:23:10 +09002155func (j *Import) Stem() string {
2156 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2157}
2158
Colin Cross74d73e22017-08-02 11:05:49 -07002159func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002160 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002161}
2162
Colin Cross74d73e22017-08-02 11:05:49 -07002163func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002164 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002165
Jiyong Park0b238752019-10-29 11:23:10 +09002166 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002167 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002168 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2169 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002170 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002171 inputFile := outputFile
2172 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2173 TransformJetifier(ctx, outputFile, inputFile)
2174 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002175 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002176
2177 ctx.VisitDirectDeps(func(module android.Module) {
2178 otherName := ctx.OtherModuleName(module)
2179 tag := ctx.OtherModuleDependencyTag(module)
2180
2181 switch dep := module.(type) {
2182 case Dependency:
2183 switch tag {
2184 case libTag, staticLibTag:
2185 // sdk lib names from dependencies are re-exported
2186 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2187 }
2188 case SdkLibraryDependency:
2189 switch tag {
2190 case libTag:
2191 // names of sdk libs that are directly depended are exported
2192 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2193 }
2194 }
2195 })
2196
2197 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002198 if Bool(j.properties.Installable) {
2199 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002200 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002201 }
Colin Cross2fe66872015-03-30 17:20:39 -07002202}
2203
Colin Cross74d73e22017-08-02 11:05:49 -07002204var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002205
Nan Zhanged19fc32017-10-19 13:06:22 -07002206func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002207 if j.combinedClasspathFile == nil {
2208 return nil
2209 }
Colin Cross37f6d792018-07-12 12:28:41 -07002210 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002211}
2212
2213func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002214 if j.combinedClasspathFile == nil {
2215 return nil
2216 }
Colin Cross37f6d792018-07-12 12:28:41 -07002217 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002218}
2219
Colin Cross331a1212018-08-15 20:40:52 -07002220func (j *Import) ResourceJars() android.Paths {
2221 return nil
2222}
2223
2224func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002225 if j.combinedClasspathFile == nil {
2226 return nil
2227 }
Colin Cross331a1212018-08-15 20:40:52 -07002228 return android.Paths{j.combinedClasspathFile}
2229}
2230
Colin Crossf24a22a2019-01-31 14:12:44 -08002231func (j *Import) DexJar() android.Path {
2232 return nil
2233}
2234
Colin Cross74d73e22017-08-02 11:05:49 -07002235func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002236 return nil
2237}
2238
Jiyong Park1be96912018-05-28 18:02:19 +09002239func (j *Import) ExportedSdkLibs() []string {
2240 return j.exportedSdkLibs
2241}
2242
Artur Satayev9cf46692019-11-26 18:08:34 +00002243func (j *Import) ExportedPlugins() (android.Paths, []string) {
2244 return nil, nil
2245}
2246
Colin Cross0c4ce212019-05-03 15:28:19 -07002247func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2248 return nil, nil
2249}
2250
albaltai36ff7dc2018-12-25 14:35:23 +08002251// Add compile time check for interface implementation
2252var _ android.IDEInfo = (*Import)(nil)
2253var _ android.IDECustomizedModuleName = (*Import)(nil)
2254
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002255// Collect information for opening IDE project files in java/jdeps.go.
2256const (
2257 removedPrefix = "prebuilt_"
2258)
2259
2260func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2261 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2262}
2263
2264func (j *Import) IDECustomizedModuleName() string {
2265 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2266 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2267 // solution to get the Import name.
2268 name := j.Name()
2269 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002270 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002271 }
2272 return name
2273}
2274
Colin Cross74d73e22017-08-02 11:05:49 -07002275var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002276
Colin Cross1b16b0e2019-02-12 14:41:32 -08002277// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2278//
2279// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2280// compiled against an Android classpath.
2281//
2282// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2283// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002284func ImportFactory() android.Module {
2285 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002286
Colin Cross74d73e22017-08-02 11:05:49 -07002287 module.AddProperties(&module.properties)
2288
2289 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002290 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002291 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002292 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002293 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002294}
2295
Colin Cross1b16b0e2019-02-12 14:41:32 -08002296// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2297// module.
2298//
2299// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2300// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002301func ImportFactoryHost() android.Module {
2302 module := &Import{}
2303
2304 module.AddProperties(&module.properties)
2305
2306 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002307 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002308 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002309 return module
2310}
2311
Colin Cross42be7612019-02-21 18:12:14 -08002312// dex_import module
2313
2314type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002315 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002316
2317 // set the name of the output
2318 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002319}
2320
2321type DexImport struct {
2322 android.ModuleBase
2323 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002324 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002325 prebuilt android.Prebuilt
2326
2327 properties DexImportProperties
2328
2329 dexJarFile android.Path
2330 maybeStrippedDexJarFile android.Path
2331
2332 dexpreopter
2333}
2334
2335func (j *DexImport) Prebuilt() *android.Prebuilt {
2336 return &j.prebuilt
2337}
2338
2339func (j *DexImport) PrebuiltSrcs() []string {
2340 return j.properties.Jars
2341}
2342
2343func (j *DexImport) Name() string {
2344 return j.prebuilt.Name(j.ModuleBase.Name())
2345}
2346
Jiyong Park0b238752019-10-29 11:23:10 +09002347func (j *DexImport) Stem() string {
2348 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2349}
2350
Colin Cross42be7612019-02-21 18:12:14 -08002351func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2352 if len(j.properties.Jars) != 1 {
2353 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2354 }
2355
Jiyong Park0b238752019-10-29 11:23:10 +09002356 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002357 j.dexpreopter.isInstallable = true
2358 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2359
2360 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2361 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2362
2363 if j.dexpreopter.uncompressedDex {
2364 rule := android.NewRuleBuilder()
2365
2366 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2367 rule.Temporary(temporary)
2368
2369 // use zip2zip to uncompress classes*.dex files
2370 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002371 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002372 FlagWithInput("-i ", inputJar).
2373 FlagWithOutput("-o ", temporary).
2374 FlagWithArg("-0 ", "'classes*.dex'")
2375
2376 // use zipalign to align uncompressed classes*.dex files
2377 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002378 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002379 Flag("-f").
2380 Text("4").
2381 Input(temporary).
2382 Output(dexOutputFile)
2383
2384 rule.DeleteTemporaryFiles()
2385
2386 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2387 } else {
2388 ctx.Build(pctx, android.BuildParams{
2389 Rule: android.Cp,
2390 Input: inputJar,
2391 Output: dexOutputFile,
2392 })
2393 }
2394
2395 j.dexJarFile = dexOutputFile
2396
2397 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2398
2399 j.maybeStrippedDexJarFile = dexOutputFile
2400
2401 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2402 ctx.ModuleName()+".jar", dexOutputFile)
2403}
2404
2405func (j *DexImport) DexJar() android.Path {
2406 return j.dexJarFile
2407}
2408
2409// dex_import imports a `.jar` file containing classes.dex files.
2410//
2411// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2412// to the device.
2413func DexImportFactory() android.Module {
2414 module := &DexImport{}
2415
2416 module.AddProperties(&module.properties)
2417
2418 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002419 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002420 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002421 return module
2422}
2423
Colin Cross89536d42017-07-07 14:35:50 -07002424//
2425// Defaults
2426//
2427type Defaults struct {
2428 android.ModuleBase
2429 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002430 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002431}
2432
Colin Cross1b16b0e2019-02-12 14:41:32 -08002433// java_defaults provides a set of properties that can be inherited by other java or android modules.
2434//
2435// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2436// property in the defaults module that exists in the depending module will be prepended to the depending module's
2437// value for that property.
2438//
2439// Example:
2440//
2441// java_defaults {
2442// name: "example_defaults",
2443// srcs: ["common/**/*.java"],
2444// javacflags: ["-Xlint:all"],
2445// aaptflags: ["--auto-add-overlay"],
2446// }
2447//
2448// java_library {
2449// name: "example",
2450// defaults: ["example_defaults"],
2451// srcs: ["example/**/*.java"],
2452// }
2453//
2454// is functionally identical to:
2455//
2456// java_library {
2457// name: "example",
2458// srcs: [
2459// "common/**/*.java",
2460// "example/**/*.java",
2461// ],
2462// javacflags: ["-Xlint:all"],
2463// }
Colin Cross89536d42017-07-07 14:35:50 -07002464func defaultsFactory() android.Module {
2465 return DefaultsFactory()
2466}
2467
Paul Duffin47357662019-12-05 14:07:14 +00002468func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002469 module := &Defaults{}
2470
Colin Cross89536d42017-07-07 14:35:50 -07002471 module.AddProperties(
2472 &CompilerProperties{},
2473 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002474 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002475 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002476 &aaptProperties{},
2477 &androidLibraryProperties{},
2478 &appProperties{},
2479 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002480 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002481 &ImportProperties{},
2482 &AARImportProperties{},
2483 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002484 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002485 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002486 )
2487
2488 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002489 return module
2490}
Nan Zhangea568a42017-11-08 21:20:04 -08002491
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002492func kytheExtractJavaFactory() android.Singleton {
2493 return &kytheExtractJavaSingleton{}
2494}
2495
2496type kytheExtractJavaSingleton struct {
2497}
2498
2499func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2500 var xrefTargets android.Paths
2501 ctx.VisitAllModules(func(module android.Module) {
2502 if javaModule, ok := module.(xref); ok {
2503 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2504 }
2505 })
2506 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2507 if len(xrefTargets) > 0 {
2508 ctx.Build(pctx, android.BuildParams{
2509 Rule: blueprint.Phony,
2510 Output: android.PathForPhony(ctx, "xref_java"),
2511 Inputs: xrefTargets,
2512 })
2513 }
2514}
2515
Nan Zhangea568a42017-11-08 21:20:04 -08002516var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002517var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002518var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002519var inList = android.InList