blob: 59bfaf7587f30fe25dfb80e473d56071ac08bbfe [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
Jiyong Parkb02bb402019-12-03 00:43:57 +0900562func (j *Module) AvailableFor(what string) bool {
563 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
564 // Exception: for hostdex: true libraries, the platform variant is created
565 // even if it's not marked as available to platform. In that case, the platform
566 // variant is used only for the hostdex and not installed to the device.
567 return true
568 }
569 return j.ApexModuleBase.AvailableFor(what)
570}
571
Colin Crossbe1da472017-07-07 15:59:46 -0700572func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700573 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100574 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700575 if sdkDep.useDefaultLibs {
576 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
577 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
578 if sdkDep.hasFrameworkLibs() {
579 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700580 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700581 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700582 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100583 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700584 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700585 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
586 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
587 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
588 }
Colin Cross2fe66872015-03-30 17:20:39 -0700589 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700590
Nan Zhangb2b33de2018-02-23 11:18:47 -0800591 if ctx.ModuleName() == "android_stubs_current" ||
592 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700593 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700594 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800595 }
Colin Cross2fe66872015-03-30 17:20:39 -0700596 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700597
Colin Cross42d48b72018-08-29 14:10:52 -0700598 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
599 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700600
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700601 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000602 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800603
Colin Crossfe17f6f2019-03-28 19:30:56 -0700604 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700605 if j.hasSrcExt(".proto") {
606 protoDeps(ctx, &j.protoProperties)
607 }
Colin Cross93e85952017-08-15 13:34:18 -0700608
609 if j.hasSrcExt(".kt") {
610 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
611 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700612 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
613 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800614 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800615 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
616 }
Colin Cross93e85952017-08-15 13:34:18 -0700617 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800618
619 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700620 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800621 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700622}
623
624func hasSrcExt(srcs []string, ext string) bool {
625 for _, src := range srcs {
626 if filepath.Ext(src) == ext {
627 return true
628 }
629 }
630
631 return false
632}
633
634func (j *Module) hasSrcExt(ext string) bool {
635 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700636}
637
Colin Cross46c9b8b2017-06-22 16:51:17 -0700638func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700639 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700640
Colin Crossebe1a512017-11-14 13:12:14 -0800641 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
642 aidlIncludes = append(aidlIncludes,
643 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
644 aidlIncludes = append(aidlIncludes,
645 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700646
Colin Cross3047fa22019-04-18 10:56:44 -0700647 var flags []string
648 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700649
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700650 if aidlPreprocess.Valid() {
651 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700652 deps = append(deps, aidlPreprocess.Path())
653 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700654 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700655 }
656
Colin Cross3047fa22019-04-18 10:56:44 -0700657 if len(j.exportAidlIncludeDirs) > 0 {
658 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
659 }
660
661 if len(aidlIncludes) > 0 {
662 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
663 }
664
Colin Cross635c3b02016-05-18 15:37:25 -0700665 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800666 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700667 flags = append(flags, "-I"+src.String())
668 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700669
Martijn Coeneneab15642018-03-09 09:29:59 +0100670 if Bool(j.deviceProperties.Aidl.Generate_traces) {
671 flags = append(flags, "-t")
672 }
673
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100674 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
675 flags = append(flags, "--transaction_names")
676 }
677
Colin Cross3047fa22019-04-18 10:56:44 -0700678 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700679}
680
Colin Cross32f676a2017-09-06 13:41:06 -0700681type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800682 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700683 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800684 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700685 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800686 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700687 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700688 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700689 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700690 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800691 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700692 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700693 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700694 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700695 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800696 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800697
698 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700699}
Colin Cross2fe66872015-03-30 17:20:39 -0700700
Colin Cross54250902017-12-05 09:28:08 -0800701func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
702 for _, f := range dep.Srcs() {
703 if f.Ext() != ".jar" {
704 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
705 ctx.OtherModuleName(dep.(blueprint.Module)))
706 }
707 }
708}
709
Jiyong Park2d492942018-03-05 17:44:10 +0900710type linkType int
711
712const (
713 javaCore linkType = iota
714 javaSdk
715 javaSystem
716 javaPlatform
717)
718
Jeongik Cha75b83b02019-11-01 15:28:00 +0900719type linkTypeContext interface {
720 android.Module
721 getLinkType(name string) (ret linkType, stubs bool)
722}
723
724func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700725 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700726 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900727 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
728 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100729 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900730 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100731 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900732 return javaCore, false
733 case name == "android_system_stubs_current":
734 return javaSystem, true
735 case strings.HasPrefix(ver, "system_"):
736 return javaSystem, false
737 case name == "android_test_stubs_current":
738 return javaSystem, true
739 case strings.HasPrefix(ver, "test_"):
740 return javaPlatform, false
741 case name == "android_stubs_current":
742 return javaSdk, true
743 case ver == "current":
744 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100745 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900746 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700747 default:
748 if _, err := strconv.Atoi(ver); err != nil {
749 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
750 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900751 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900752 }
753}
754
Jeongik Cha75b83b02019-11-01 15:28:00 +0900755func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700756 if ctx.Host() {
757 return
758 }
759
Jeongik Cha75b83b02019-11-01 15:28:00 +0900760 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900761 if stubs {
762 return
763 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900764 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900765 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."
766
767 switch myLinkType {
768 case javaCore:
769 if otherLinkType != javaCore {
770 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 +0900771 ctx.OtherModuleName(to))
772 }
Jiyong Park2d492942018-03-05 17:44:10 +0900773 break
774 case javaSdk:
775 if otherLinkType != javaCore && otherLinkType != javaSdk {
776 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
777 ctx.OtherModuleName(to))
778 }
779 break
780 case javaSystem:
781 if otherLinkType == javaPlatform {
782 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
783 ctx.OtherModuleName(to))
784 }
785 break
786 case javaPlatform:
787 // no restriction on link-type
788 break
Jiyong Park750e5572018-01-31 00:20:13 +0900789 }
790}
791
Colin Cross32f676a2017-09-06 13:41:06 -0700792func (j *Module) collectDeps(ctx android.ModuleContext) deps {
793 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700794
Colin Cross300f0382018-03-06 13:11:51 -0800795 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700796 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800797 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700798 ctx.AddMissingDependencies(sdkDep.bootclasspath)
799 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800800 } else if sdkDep.useFiles {
801 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700802 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700803 deps.aidlPreprocess = sdkDep.aidl
804 } else {
805 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800806 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700807 }
808
Colin Crossd11fcda2017-10-23 17:59:01 -0700809 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700810 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700811 tag := ctx.OtherModuleDependencyTag(module)
812
Colin Crossa4f08812018-10-02 22:03:40 -0700813 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700814 // Handled by AndroidApp.collectAppDeps
815 return
816 }
817 if tag == certificateTag {
818 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700819 return
820 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900821 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900822 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900823 if to, ok := module.(linkTypeContext); ok {
824 switch tag {
825 case bootClasspathTag, libTag, staticLibTag:
826 checkLinkType(ctx, j, to, tag.(dependencyTag))
827 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700828 }
Jiyong Park750e5572018-01-31 00:20:13 +0900829 }
Colin Cross54250902017-12-05 09:28:08 -0800830 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800831 case SdkLibraryDependency:
832 switch tag {
833 case libTag:
834 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
835 // names of sdk libs that are directly depended are exported
836 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700837 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800838 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
839 }
Colin Cross54250902017-12-05 09:28:08 -0800840 case Dependency:
841 switch tag {
842 case bootClasspathTag:
843 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700844 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800845 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900846 // sdk lib names from dependencies are re-exported
847 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700848 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000849 pluginJars, pluginClasses := dep.ExportedPlugins()
850 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700851 case java9LibTag:
852 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800853 case staticLibTag:
854 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
855 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
856 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700857 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900858 // sdk lib names from dependencies are re-exported
859 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700860 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000861 pluginJars, pluginClasses := dep.ExportedPlugins()
862 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800863 case pluginTag:
864 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800865 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000866 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
867 } else {
868 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800869 }
870 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
871 } else {
872 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
873 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000874 case exportedPluginTag:
875 if plugin, ok := dep.(*Plugin); ok {
876 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
877 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
878 }
879 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
880 if plugin.pluginProperties.Processor_class != nil {
881 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
882 }
883 } else {
884 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
885 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800886 case frameworkApkTag:
887 if ctx.ModuleName() == "android_stubs_current" ||
888 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700889 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800890 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
891 // resource files out of there for aapt.
892 //
893 // Normally the package rule runs aapt, which includes the resource,
894 // but we're not running that in our package rule so just copy in the
895 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700896 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800897 }
Colin Cross54250902017-12-05 09:28:08 -0800898 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700899 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800900 case kotlinAnnotationsTag:
901 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800902 }
903
Colin Cross54250902017-12-05 09:28:08 -0800904 case android.SourceFileProducer:
905 switch tag {
906 case libTag:
907 checkProducesJars(ctx, dep)
908 deps.classpath = append(deps.classpath, dep.Srcs()...)
909 case staticLibTag:
910 checkProducesJars(ctx, dep)
911 deps.classpath = append(deps.classpath, dep.Srcs()...)
912 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
913 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800914 }
915 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700916 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100917 case bootClasspathTag:
918 // If a system modules dependency has been added to the bootclasspath
919 // then add its libs to the bootclasspath.
920 sm := module.(*SystemModules)
921 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
922
Colin Cross1369cdb2017-09-29 17:58:17 -0700923 case systemModulesTag:
924 if deps.systemModules != nil {
925 panic("Found two system module dependencies")
926 }
927 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000928 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700929 panic("Missing directory for system module dependency")
930 }
Colin Crossb77043e2019-07-16 13:57:13 -0700931 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700932 }
Colin Crossec7a0422017-07-07 14:47:12 -0700933 }
Colin Cross2fe66872015-03-30 17:20:39 -0700934 })
935
Jiyong Park1be96912018-05-28 18:02:19 +0900936 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
937
Colin Cross32f676a2017-09-06 13:41:06 -0700938 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700939}
940
Artur Satayev9cf46692019-11-26 18:08:34 +0000941func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
942 deps.processorPath = append(deps.processorPath, pluginJars...)
943 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
944}
945
Colin Cross1e743852019-10-28 11:37:20 -0700946func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800947 v := sdkContext.sdkVersion()
948 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100949 if ctx.Config().IsPdkBuild() &&
950 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700951 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800952 latestSdkVersion := 0
953 if len(sdkVersions) > 0 {
954 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
955 }
956 v = strconv.Itoa(latestSdkVersion)
957 }
958
959 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700960 if err != nil {
961 ctx.PropertyErrorf("sdk_version", "%s", err)
962 }
Nan Zhang357466b2018-04-17 17:38:36 -0700963 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700964 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700965 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -0700966 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100967 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -0700968 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -0700969 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
970 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -0700971 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -0700972 } else {
Colin Cross1e743852019-10-28 11:37:20 -0700973 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -0700974 }
Nan Zhang357466b2018-04-17 17:38:36 -0700975}
976
Colin Cross1e743852019-10-28 11:37:20 -0700977type javaVersion int
978
979const (
980 JAVA_VERSION_UNSUPPORTED = 0
981 JAVA_VERSION_6 = 6
982 JAVA_VERSION_7 = 7
983 JAVA_VERSION_8 = 8
984 JAVA_VERSION_9 = 9
985)
986
987func (v javaVersion) String() string {
988 switch v {
989 case JAVA_VERSION_6:
990 return "1.6"
991 case JAVA_VERSION_7:
992 return "1.7"
993 case JAVA_VERSION_8:
994 return "1.8"
995 case JAVA_VERSION_9:
996 return "1.9"
997 default:
998 return "unsupported"
999 }
1000}
1001
1002// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1003func (v javaVersion) usesJavaModules() bool {
1004 return v >= 9
1005}
1006
1007func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001008 switch javaVersion {
1009 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001010 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001011 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001012 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001013 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001014 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001015 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001016 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001017 case "10", "11":
1018 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001019 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001020 default:
1021 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001022 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001023 }
1024}
1025
Nan Zhanged19fc32017-10-19 13:06:22 -07001026func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001027
Colin Crossf03c82b2015-04-13 13:53:40 -07001028 var flags javaBuilderFlags
1029
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001030 // javaVersion flag.
1031 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1032
Nan Zhanged19fc32017-10-19 13:06:22 -07001033 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001034 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001035 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001036 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001037 }
Colin Cross6510f912017-11-29 00:27:14 -08001038 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001039 // Override the -g flag passed globally to remove local variable debug info to reduce
1040 // disk and memory usage.
1041 javacFlags = append(javacFlags, "-g:source,lines")
1042 }
Colin Crossc228a702019-11-06 16:18:05 -08001043 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001044
Colin Cross66548102018-06-19 22:47:35 -07001045 if ctx.Config().RunErrorProne() {
1046 if config.ErrorProneClasspath == nil {
1047 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1048 }
1049
1050 errorProneFlags := []string{
1051 "-Xplugin:ErrorProne",
1052 "${config.ErrorProneChecks}",
1053 }
1054 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1055
1056 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1057 "'" + strings.Join(errorProneFlags, " ") + "'"
1058 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001059 }
1060
Nan Zhanged19fc32017-10-19 13:06:22 -07001061 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001062 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1063 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001064 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001065 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001066
Colin Crossbe9cdb82019-01-21 21:37:16 -08001067 flags.processor = strings.Join(deps.processorClasses, ",")
1068
Colin Cross1e743852019-10-28 11:37:20 -07001069 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1070 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001071 // Give host-side tools a version of OpenJDK's standard libraries
1072 // close to what they're targeting. As of Dec 2017, AOSP is only
1073 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1074 //
1075 // When building with OpenJDK 8, the following should have no
1076 // effect since those jars would be available by default.
1077 //
1078 // When building with OpenJDK 9 but targeting a version < 1.8,
1079 // putting them on the bootclasspath means that:
1080 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1081 // b) references to existing APIs are not reinterpreted in an
1082 // OpenJDK 9-specific way, eg. calls to subclasses of
1083 // java.nio.Buffer as in http://b/70862583
1084 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1085 flags.bootClasspath = append(flags.bootClasspath,
1086 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1087 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001088 if Bool(j.properties.Use_tools_jar) {
1089 flags.bootClasspath = append(flags.bootClasspath,
1090 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1091 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001092 }
1093
Colin Cross1e743852019-10-28 11:37:20 -07001094 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001095 // Manually specify build directory in case it is not under the repo root.
1096 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1097 // just adding a symlink under the root doesn't help.)
1098 patchPaths := ".:" + ctx.Config().BuildDir()
1099 classPath := flags.classpath.FormJavaClassPath("")
1100 if classPath != "" {
1101 patchPaths += ":" + classPath
1102 }
1103 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001104 }
1105
Nan Zhanged19fc32017-10-19 13:06:22 -07001106 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001107 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001108
Nan Zhanged19fc32017-10-19 13:06:22 -07001109 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001110 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001111
Colin Cross81440082018-08-15 20:21:55 -07001112 if len(javacFlags) > 0 {
1113 // optimization.
1114 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1115 flags.javacFlags = "$javacFlags"
1116 }
1117
Nan Zhanged19fc32017-10-19 13:06:22 -07001118 return flags
1119}
Colin Crossc0b06f12015-04-08 13:03:43 -07001120
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001121func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001122 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001123
1124 deps := j.collectDeps(ctx)
1125 flags := j.collectBuilderFlags(ctx, deps)
1126
Colin Cross1e743852019-10-28 11:37:20 -07001127 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001128 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1129 }
Colin Cross8a497952019-03-05 22:25:09 -08001130 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001131 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001132 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001133 }
1134
Colin Crossaf050172017-11-15 23:01:59 -08001135 srcFiles = j.genSources(ctx, srcFiles, flags)
1136
1137 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001138 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001139 if aaptSrcJar != nil {
1140 srcJars = append(srcJars, aaptSrcJar)
1141 }
Colin Crossb7a63242015-04-16 14:09:14 -07001142
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001143 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001144 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001145 }
1146
Colin Cross1ee23172017-10-18 14:44:18 -07001147 jarName := ctx.ModuleName() + ".jar"
1148
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001149 javaSrcFiles := srcFiles.FilterByExt(".java")
1150 var uniqueSrcFiles android.Paths
1151 set := make(map[string]bool)
1152 for _, v := range javaSrcFiles {
1153 if _, found := set[v.String()]; !found {
1154 set[v.String()] = true
1155 uniqueSrcFiles = append(uniqueSrcFiles, v)
1156 }
1157 }
1158
patricktu242faad2019-09-24 15:41:30 +08001159 // Collect .java files for AIDEGen
1160 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1161
Colin Cross55f63ea2018-08-27 12:37:09 -07001162 var kotlinJars android.Paths
1163
Colin Cross93e85952017-08-15 13:34:18 -07001164 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001165 // user defined kotlin flags.
1166 kotlincFlags := j.properties.Kotlincflags
1167 CheckKotlincFlags(ctx, kotlincFlags)
1168
Colin Cross93e85952017-08-15 13:34:18 -07001169 // If there are kotlin files, compile them first but pass all the kotlin and java files
1170 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1171 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001172 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001173 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001174 kotlincFlags = append(kotlincFlags, "-no-jdk")
1175 }
1176 if len(kotlincFlags) > 0 {
1177 // optimization.
1178 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1179 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001180 }
1181
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001182 var kotlinSrcFiles android.Paths
1183 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1184 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1185
patricktu242faad2019-09-24 15:41:30 +08001186 // Collect .kt files for AIDEGen
1187 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1188
Colin Crossafbb1732019-01-17 15:42:52 -08001189 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1190 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1191
1192 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1193 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1194
1195 if len(flags.processorPath) > 0 {
1196 // Use kapt for annotation processing
1197 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1198 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1199 srcJars = append(srcJars, kaptSrcJar)
1200 // Disable annotation processing in javac, it's already been handled by kapt
1201 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001202 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001203 }
Colin Cross93e85952017-08-15 13:34:18 -07001204
Colin Cross1ee23172017-10-18 14:44:18 -07001205 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001206 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001207 if ctx.Failed() {
1208 return
1209 }
1210
1211 // Make javac rule depend on the kotlinc rule
1212 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001213
Colin Cross93e85952017-08-15 13:34:18 -07001214 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001215 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001216 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001217 }
1218
Colin Cross55f63ea2018-08-27 12:37:09 -07001219 jars := append(android.Paths(nil), kotlinJars...)
1220
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001221 // Store the list of .java files that was passed to javac
1222 j.compiledJavaSrcs = uniqueSrcFiles
1223 j.compiledSrcJars = srcJars
1224
Nan Zhang61eaedb2017-11-02 13:28:15 -07001225 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001226 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001227 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1228 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001229 // Formerly, there was a check here that prevented annotation processors
1230 // from being used when sharding was enabled, as some annotation processors
1231 // do not function correctly in sharded environments. It was removed to
1232 // allow for the use of annotation processors that do function correctly
1233 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001234 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001235 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001236 if ctx.Failed() {
1237 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001238 }
1239 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001240 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001241 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001242 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001243 // If error-prone is enabled, add an additional rule to compile the java files into
1244 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001245 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001246 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1247 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001248 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001249 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001250 extraJarDeps = append(extraJarDeps, errorprone)
1251 }
1252
Nan Zhang61eaedb2017-11-02 13:28:15 -07001253 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001254 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001255 shardSize := int(*(j.properties.Javac_shard_size))
1256 var shardSrcs []android.Paths
1257 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001258 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001259 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001260 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1261 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001262 jars = append(jars, classes)
1263 }
1264 }
1265 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001266 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1267 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001268 jars = append(jars, classes)
1269 }
1270 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001271 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001272 jars = append(jars, classes)
1273 }
Colin Crossd6891432017-09-27 17:39:56 -07001274 if ctx.Failed() {
1275 return
1276 }
Colin Cross2fe66872015-03-30 17:20:39 -07001277 }
1278
Colin Cross0c4ce212019-05-03 15:28:19 -07001279 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1280
1281 var includeSrcJar android.WritablePath
1282 if Bool(j.properties.Include_srcs) {
1283 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1284 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1285 }
1286
Colin Crosscedd4762018-09-13 11:26:19 -07001287 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1288 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001289 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001290 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001291
1292 var resArgs []string
1293 var resDeps android.Paths
1294
1295 resArgs = append(resArgs, dirArgs...)
1296 resDeps = append(resDeps, dirDeps...)
1297
1298 resArgs = append(resArgs, fileArgs...)
1299 resDeps = append(resDeps, fileDeps...)
1300
Colin Cross988708c2019-05-06 14:04:11 -07001301 resArgs = append(resArgs, extraArgs...)
1302 resDeps = append(resDeps, extraDeps...)
1303
Colin Cross40a36712017-09-27 17:41:35 -07001304 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001305 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001306 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001307 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001308 if ctx.Failed() {
1309 return
1310 }
1311 }
1312
Colin Cross0c4ce212019-05-03 15:28:19 -07001313 var resourceJars android.Paths
1314 if j.resourceJar != nil {
1315 resourceJars = append(resourceJars, j.resourceJar)
1316 }
1317 if Bool(j.properties.Include_srcs) {
1318 resourceJars = append(resourceJars, includeSrcJar)
1319 }
1320 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001321
Colin Cross0c4ce212019-05-03 15:28:19 -07001322 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001323 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001324 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001325 false, nil, nil)
1326 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001327 } else if len(resourceJars) == 1 {
1328 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001329 }
1330
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001331 if len(deps.staticJars) > 0 {
1332 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001333 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001334
Colin Cross094054a2018-10-17 15:10:48 -07001335 manifest := j.overrideManifest
1336 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001337 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001338 }
Colin Cross635acc92017-09-12 22:50:46 -07001339
Colin Cross8a497952019-03-05 22:25:09 -08001340 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001341 if len(services) > 0 {
1342 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1343 var zipargs []string
1344 for _, file := range services {
1345 serviceFile := file.String()
1346 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1347 }
1348 ctx.Build(pctx, android.BuildParams{
1349 Rule: zip,
1350 Output: servicesJar,
1351 Implicits: services,
1352 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001353 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001354 },
1355 })
1356 jars = append(jars, servicesJar)
1357 }
1358
Colin Cross0a6e0072017-08-30 14:24:55 -07001359 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001360 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001361 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001362
1363 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001364 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1365 // Optimization: skip the combine step if there is nothing to do
1366 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1367 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1368 // any if len(jars) == 1.
1369 outputFile = moduleOutPath
1370 } else {
1371 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1372 ctx.Build(pctx, android.BuildParams{
1373 Rule: android.Cp,
1374 Input: jars[0],
1375 Output: combinedJar,
1376 })
1377 outputFile = combinedJar
1378 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001379 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001380 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001381 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001382 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001383 outputFile = combinedJar
1384 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001385
Colin Cross331a1212018-08-15 20:40:52 -07001386 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001387 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001388 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001389 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001390 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001391 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001392
1393 // jarjar resource jar if necessary
1394 if j.resourceJar != nil {
1395 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001396 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001397 j.resourceJar = resourceJarJarFile
1398 }
1399
Colin Cross0a6e0072017-08-30 14:24:55 -07001400 if ctx.Failed() {
1401 return
1402 }
1403 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001404
1405 // Check package restrictions if necessary.
1406 if len(j.properties.Permitted_packages) > 0 {
1407 // Check packages and copy to package-checked file.
1408 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1409 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1410 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1411
1412 if ctx.Failed() {
1413 return
1414 }
1415 }
1416
Nan Zhanged19fc32017-10-19 13:06:22 -07001417 j.implementationJarFile = outputFile
1418 if j.headerJarFile == nil {
1419 j.headerJarFile = j.implementationJarFile
1420 }
Colin Cross2fe66872015-03-30 17:20:39 -07001421
Colin Cross6510f912017-11-29 00:27:14 -08001422 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001423 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1424 j.properties.Instrument = true
1425 }
1426 }
1427
Colin Cross3144dfc2018-01-03 15:06:47 -08001428 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001429 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1430 }
1431
Colin Cross331a1212018-08-15 20:40:52 -07001432 // merge implementation jar with resources if necessary
1433 implementationAndResourcesJar := outputFile
1434 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001435 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001436 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001437 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001438 false, nil, nil)
1439 implementationAndResourcesJar = combinedJar
1440 }
1441
1442 j.implementationAndResourcesJar = implementationAndResourcesJar
1443
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001444 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001445 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001446 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001447 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001448 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001449 if ctx.Failed() {
1450 return
1451 }
Colin Cross331a1212018-08-15 20:40:52 -07001452
Jiyong Park09cb6292019-07-15 15:29:23 +09001453 // Hidden API CSV generation and dex encoding
1454 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1455 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001456
Colin Cross331a1212018-08-15 20:40:52 -07001457 // merge dex jar with resources if necessary
1458 if j.resourceJar != nil {
1459 jars := android.Paths{dexOutputFile, j.resourceJar}
1460 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1461 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1462 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001463 if j.deviceProperties.UncompressDex {
1464 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1465 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1466 dexOutputFile = combinedAlignedJar
1467 } else {
1468 dexOutputFile = combinedJar
1469 }
Colin Cross331a1212018-08-15 20:40:52 -07001470 }
1471
1472 j.dexJarFile = dexOutputFile
1473
Colin Cross8faf8fc2019-01-16 15:15:52 -08001474 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001475 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1476
1477 j.maybeStrippedDexJarFile = dexOutputFile
1478
Colin Cross3063b782018-08-15 11:19:12 -07001479 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001480
1481 if ctx.Failed() {
1482 return
1483 }
Colin Cross331a1212018-08-15 20:40:52 -07001484 } else {
1485 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001486 }
Colin Cross331a1212018-08-15 20:40:52 -07001487
Colin Crossb7a63242015-04-16 14:09:14 -07001488 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001489
1490 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1491 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001492}
1493
Colin Cross3b706fd2019-09-05 16:44:18 -07001494func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1495 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1496
1497 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1498 if idx >= 0 {
1499 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1500 jarName += strconv.Itoa(idx)
1501 }
1502
1503 classes := android.PathForModuleOut(ctx, "javac", jarName)
1504 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1505
1506 if ctx.Config().EmitXrefRules() {
1507 extractionFile := android.PathForModuleOut(ctx, kzipName)
1508 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1509 j.kytheFiles = append(j.kytheFiles, extractionFile)
1510 }
1511
1512 return classes
1513}
1514
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001515// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1516// since some of these flags may be used internally.
1517func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1518 for _, flag := range flags {
1519 flag = strings.TrimSpace(flag)
1520
1521 if !strings.HasPrefix(flag, "-") {
1522 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1523 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1524 ctx.PropertyErrorf("kotlincflags",
1525 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1526 } else if inList(flag, config.KotlincIllegalFlags) {
1527 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1528 } else if flag == "-include-runtime" {
1529 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1530 } else {
1531 args := strings.Split(flag, " ")
1532 if args[0] == "-kotlin-home" {
1533 ctx.PropertyErrorf("kotlincflags",
1534 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1535 }
1536 }
1537 }
1538}
1539
Colin Cross8eadbf02017-10-24 17:46:00 -07001540func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001541 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001542
1543 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001544 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001545 // Compile java sources into turbine.jar.
1546 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1547 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1548 if ctx.Failed() {
1549 return nil
1550 }
1551 jars = append(jars, turbineJar)
1552 }
1553
Colin Cross55f63ea2018-08-27 12:37:09 -07001554 jars = append(jars, extraJars...)
1555
Nan Zhanged19fc32017-10-19 13:06:22 -07001556 // Combine any static header libraries into classes-header.jar. If there is only
1557 // one input jar this step will be skipped.
1558 var headerJar android.Path
1559 jars = append(jars, deps.staticHeaderJars...)
1560
Colin Cross5c6ecc12017-10-23 18:12:27 -07001561 // we cannot skip the combine step for now if there is only one jar
1562 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1563 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001564 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001565 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001566 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001567
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001568 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001569 // Transform classes.jar into classes-jarjar.jar
1570 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001571 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001572 headerJar = jarjarFile
1573 if ctx.Failed() {
1574 return nil
1575 }
1576 }
1577
1578 return headerJar
1579}
1580
Colin Crosscb933592017-11-22 13:49:43 -08001581func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001582 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001583
Colin Cross7a3139e2017-12-19 13:57:50 -08001584 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001585
Colin Cross84c38822018-01-03 15:59:46 -08001586 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001587 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1588
1589 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1590
1591 j.jacocoReportClassesFile = jacocoReportClassesFile
1592
1593 return instrumentedJar
1594}
1595
albaltai36ff7dc2018-12-25 14:35:23 +08001596var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001597
Nan Zhanged19fc32017-10-19 13:06:22 -07001598func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001599 if j.headerJarFile == nil {
1600 return nil
1601 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001602 return android.Paths{j.headerJarFile}
1603}
1604
1605func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001606 if j.implementationJarFile == nil {
1607 return nil
1608 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001609 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001610}
1611
Colin Crossf24a22a2019-01-31 14:12:44 -08001612func (j *Module) DexJar() android.Path {
1613 return j.dexJarFile
1614}
1615
Colin Cross331a1212018-08-15 20:40:52 -07001616func (j *Module) ResourceJars() android.Paths {
1617 if j.resourceJar == nil {
1618 return nil
1619 }
1620 return android.Paths{j.resourceJar}
1621}
1622
1623func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001624 if j.implementationAndResourcesJar == nil {
1625 return nil
1626 }
Colin Cross331a1212018-08-15 20:40:52 -07001627 return android.Paths{j.implementationAndResourcesJar}
1628}
1629
Colin Cross46c9b8b2017-06-22 16:51:17 -07001630func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001631 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001632 return j.exportAidlIncludeDirs
1633}
1634
Jiyong Park1be96912018-05-28 18:02:19 +09001635func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001636 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001637 return j.exportedSdkLibs
1638}
1639
Artur Satayev9cf46692019-11-26 18:08:34 +00001640func (j *Module) ExportedPlugins() (android.Paths, []string) {
1641 return j.exportedPluginJars, j.exportedPluginClasses
1642}
1643
Colin Cross0c4ce212019-05-03 15:28:19 -07001644func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1645 return j.srcJarArgs, j.srcJarDeps
1646}
1647
Colin Cross46c9b8b2017-06-22 16:51:17 -07001648var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001649
Colin Cross46c9b8b2017-06-22 16:51:17 -07001650func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001651 return j.logtagsSrcs
1652}
1653
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001654// Collect information for opening IDE project files in java/jdeps.go.
1655func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1656 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1657 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001658 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001659 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001660 if j.expandJarjarRules != nil {
1661 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001662 }
1663}
1664
1665func (j *Module) CompilerDeps() []string {
1666 jdeps := []string{}
1667 jdeps = append(jdeps, j.properties.Libs...)
1668 jdeps = append(jdeps, j.properties.Static_libs...)
1669 return jdeps
1670}
1671
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001672func (j *Module) hasCode(ctx android.ModuleContext) bool {
1673 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1674 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1675}
1676
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001677func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1678 depTag := ctx.OtherModuleDependencyTag(dep)
1679 // dependencies other than the static linkage are all considered crossing APEX boundary
1680 return depTag == staticLibTag
1681}
1682
Jiyong Park0b238752019-10-29 11:23:10 +09001683func (j *Module) Stem() string {
1684 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1685}
1686
Colin Cross2fe66872015-03-30 17:20:39 -07001687//
1688// Java libraries (.jar file)
1689//
1690
Colin Crossf506d872017-07-19 15:53:04 -07001691type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001692 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001693
1694 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001695}
1696
Colin Cross42be7612019-02-21 18:12:14 -08001697func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001698 // Store uncompressed (and do not strip) dex files from boot class path jars.
1699 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1700 return true
1701 }
1702
1703 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001704 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001705 return true
1706 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001707 if ctx.Config().UncompressPrivAppDex() &&
1708 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1709 return true
1710 }
1711
Colin Cross2fc72f62018-12-21 12:59:54 -08001712 return false
1713}
1714
Colin Crossf506d872017-07-19 15:53:04 -07001715func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001716 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001717 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001718 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001719 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001720 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001721 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001722 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001723
Jiyong Park7f7766d2019-07-25 22:02:35 +09001724 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1725 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001726 var extraInstallDeps android.Paths
1727 if j.InstallMixin != nil {
1728 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1729 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001730 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001731 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001732 }
Colin Crossb7a63242015-04-16 14:09:14 -07001733}
1734
Colin Crossf506d872017-07-19 15:53:04 -07001735func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001736 j.deps(ctx)
1737}
1738
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001739const (
Paul Duffina0dbf432019-12-05 11:25:53 +00001740 aidlIncludeDir = "aidl"
1741 javaDir = "java"
1742 jarFileSuffix = ".jar"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001743)
1744
Paul Duffina0dbf432019-12-05 11:25:53 +00001745// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
1746func (j *Library) sdkSnapshotFilePathForJar() string {
1747 return filepath.Join(javaDir, j.Name()+jarFileSuffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001748}
1749
Paul Duffin13879572019-11-28 14:31:38 +00001750type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001751 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001752}
1753
1754func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1755 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1756}
1757
1758func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1759 _, ok := module.(*Library)
1760 return ok
1761}
1762
Paul Duffina0dbf432019-12-05 11:25:53 +00001763func (mt *librarySdkMemberType) buildSnapshot(
1764 sdkModuleContext android.ModuleContext,
1765 builder android.SnapshotBuilder,
1766 member android.SdkMember,
1767 jarToExportGetter func(j *Library) android.Path) {
1768
Paul Duffin13879572019-11-28 14:31:38 +00001769 variants := member.Variants()
1770 if len(variants) != 1 {
1771 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1772 for _, variant := range variants {
1773 sdkModuleContext.ModuleErrorf(" %q", variant)
1774 }
1775 }
1776 variant := variants[0]
1777 j := variant.(*Library)
1778
Paul Duffina0dbf432019-12-05 11:25:53 +00001779 exportedJar := jarToExportGetter(j)
1780 snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
1781 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001782
1783 for _, dir := range j.AidlIncludeDirs() {
1784 // TODO(jiyong): copy parcelable declarations only
1785 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1786 for _, file := range aidlFiles {
1787 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1788 }
1789 }
1790
Paul Duffin9d8d6092019-12-05 18:19:29 +00001791 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001792 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001793}
1794
Paul Duffina0dbf432019-12-05 11:25:53 +00001795type headerLibrarySdkMemberType struct {
1796 librarySdkMemberType
1797}
1798
1799func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1800 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1801 headerJars := j.HeaderJars()
1802 if len(headerJars) != 1 {
1803 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1804 }
1805
1806 return headerJars[0]
1807 })
1808}
1809
Paul Duffina0dbf432019-12-05 11:25:53 +00001810type implLibrarySdkMemberType struct {
1811 librarySdkMemberType
1812}
1813
1814func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1815 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1816 implementationJars := j.ImplementationJars()
1817 if len(implementationJars) != 1 {
1818 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1819 }
1820
1821 return implementationJars[0]
1822 })
1823}
1824
Colin Cross1b16b0e2019-02-12 14:41:32 -08001825// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1826//
1827// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1828// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1829// as a `static_libs` dependency of another module.
1830//
1831// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1832// a device.
1833//
1834// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1835// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001836func LibraryFactory() android.Module {
1837 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001838
Colin Cross9ae1b922018-06-26 17:59:05 -07001839 module.AddProperties(
1840 &module.Module.properties,
1841 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001842 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001843 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001844
Jiyong Park7f7766d2019-07-25 22:02:35 +09001845 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001846 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001847 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001848 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001849}
1850
Colin Cross1b16b0e2019-02-12 14:41:32 -08001851// java_library_static is an obsolete alias for java_library.
1852func LibraryStaticFactory() android.Module {
1853 return LibraryFactory()
1854}
1855
1856// java_library_host builds and links sources into a `.jar` file for the host.
1857//
1858// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1859// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001860func LibraryHostFactory() android.Module {
1861 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001862
Colin Cross6af17aa2017-09-20 12:59:05 -07001863 module.AddProperties(
1864 &module.Module.properties,
1865 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001866
Colin Cross9ae1b922018-06-26 17:59:05 -07001867 module.Module.properties.Installable = proptools.BoolPtr(true)
1868
Jiyong Park7f7766d2019-07-25 22:02:35 +09001869 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001870 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001871 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001872}
1873
1874//
Colin Crossb628ea52018-08-14 16:42:33 -07001875// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001876//
1877
1878type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001879 // list of compatibility suites (for example "cts", "vts") that the module should be
1880 // installed into.
1881 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001882
1883 // the name of the test configuration (for example "AndroidTest.xml") that should be
1884 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001885 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001886
Jack He33338892018-09-19 02:21:28 -07001887 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1888 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001889 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001890
Colin Crossd96ca352018-08-10 16:06:24 -07001891 // list of files or filegroup modules that provide data that should be installed alongside
1892 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001893 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001894
1895 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1896 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1897 // explicitly.
1898 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001899}
1900
Paul Duffin42df1442019-03-20 12:45:53 +00001901type testHelperLibraryProperties struct {
1902 // list of compatibility suites (for example "cts", "vts") that the module should be
1903 // installed into.
1904 Test_suites []string `android:"arch_variant"`
1905}
1906
Colin Cross05638fc2018-04-09 18:40:24 -07001907type Test struct {
1908 Library
1909
1910 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001911
1912 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001913 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001914}
1915
Paul Duffin42df1442019-03-20 12:45:53 +00001916type TestHelperLibrary struct {
1917 Library
1918
1919 testHelperLibraryProperties testHelperLibraryProperties
1920}
1921
Colin Cross303e21f2018-08-07 16:49:25 -07001922func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001923 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1924 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001925 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001926
1927 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001928}
1929
Paul Duffin42df1442019-03-20 12:45:53 +00001930func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1931 j.Library.GenerateAndroidBuildActions(ctx)
1932}
1933
Colin Cross1b16b0e2019-02-12 14:41:32 -08001934// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1935// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1936//
1937// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1938// compiled against the device bootclasspath.
1939//
1940// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1941// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001942func TestFactory() android.Module {
1943 module := &Test{}
1944
1945 module.AddProperties(
1946 &module.Module.properties,
1947 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001948 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001949 &module.Module.protoProperties,
1950 &module.testProperties)
1951
Colin Cross9ae1b922018-06-26 17:59:05 -07001952 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001953 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001954
Colin Cross05638fc2018-04-09 18:40:24 -07001955 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001956 return module
1957}
1958
Paul Duffin42df1442019-03-20 12:45:53 +00001959// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1960func TestHelperLibraryFactory() android.Module {
1961 module := &TestHelperLibrary{}
1962
1963 module.AddProperties(
1964 &module.Module.properties,
1965 &module.Module.deviceProperties,
1966 &module.Module.dexpreoptProperties,
1967 &module.Module.protoProperties,
1968 &module.testHelperLibraryProperties)
1969
Colin Cross9a4abed2019-04-24 13:19:28 -07001970 module.Module.properties.Installable = proptools.BoolPtr(true)
1971 module.Module.dexpreopter.isTest = true
1972
Paul Duffin42df1442019-03-20 12:45:53 +00001973 InitJavaModule(module, android.HostAndDeviceSupported)
1974 return module
1975}
1976
Colin Cross1b16b0e2019-02-12 14:41:32 -08001977// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1978// allow running the test with `atest` or a `TEST_MAPPING` file.
1979//
1980// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1981// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001982func TestHostFactory() android.Module {
1983 module := &Test{}
1984
1985 module.AddProperties(
1986 &module.Module.properties,
1987 &module.Module.protoProperties,
1988 &module.testProperties)
1989
Colin Cross9ae1b922018-06-26 17:59:05 -07001990 module.Module.properties.Installable = proptools.BoolPtr(true)
1991
Colin Cross05638fc2018-04-09 18:40:24 -07001992 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001993 return module
1994}
1995
1996//
Colin Cross2fe66872015-03-30 17:20:39 -07001997// Java Binaries (.jar file plus wrapper script)
1998//
1999
Colin Crossf506d872017-07-19 15:53:04 -07002000type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002001 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002002 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002003
2004 // Name of the class containing main to be inserted into the manifest as Main-Class.
2005 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002006}
2007
Colin Crossf506d872017-07-19 15:53:04 -07002008type Binary struct {
2009 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002010
Colin Crossf506d872017-07-19 15:53:04 -07002011 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002012
Colin Cross6b4a32d2017-12-05 13:42:45 -08002013 isWrapperVariant bool
2014
Colin Crossc3315992017-12-08 19:12:36 -08002015 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002016 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002017}
2018
Alex Light24237172017-10-26 09:46:21 -07002019func (j *Binary) HostToolPath() android.OptionalPath {
2020 return android.OptionalPathForPath(j.binaryFile)
2021}
2022
Colin Crossf506d872017-07-19 15:53:04 -07002023func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002024 if ctx.Arch().ArchType == android.Common {
2025 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002026 if j.binaryProperties.Main_class != nil {
2027 if j.properties.Manifest != nil {
2028 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2029 }
2030 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2031 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2032 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2033 }
2034
Colin Cross6b4a32d2017-12-05 13:42:45 -08002035 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002036 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002037 // Handle the binary wrapper
2038 j.isWrapperVariant = true
2039
Colin Cross366938f2017-12-11 16:29:02 -08002040 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002041 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002042 } else {
2043 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2044 }
2045
2046 // Depend on the installed jar so that the wrapper doesn't get executed by
2047 // another build rule before the jar has been installed.
2048 jarFile := ctx.PrimaryModule().(*Binary).installFile
2049
2050 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2051 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002052 }
Colin Cross2fe66872015-03-30 17:20:39 -07002053}
2054
Colin Crossf506d872017-07-19 15:53:04 -07002055func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002056 if ctx.Arch().ArchType == android.Common {
2057 j.deps(ctx)
2058 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002059}
2060
Colin Cross1b16b0e2019-02-12 14:41:32 -08002061// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2062// as well.
2063//
2064// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2065// compiled against the device bootclasspath.
2066//
2067// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2068// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002069func BinaryFactory() android.Module {
2070 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002071
Colin Cross36242852017-06-23 15:06:31 -07002072 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002073 &module.Module.properties,
2074 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002075 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002076 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002077 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002078
Colin Cross9ae1b922018-06-26 17:59:05 -07002079 module.Module.properties.Installable = proptools.BoolPtr(true)
2080
Colin Cross6b4a32d2017-12-05 13:42:45 -08002081 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2082 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002083 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002084}
2085
Colin Cross1b16b0e2019-02-12 14:41:32 -08002086// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2087//
2088// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2089// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002090func BinaryHostFactory() android.Module {
2091 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002092
Colin Cross36242852017-06-23 15:06:31 -07002093 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002094 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002095 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002096 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002097
Colin Cross9ae1b922018-06-26 17:59:05 -07002098 module.Module.properties.Installable = proptools.BoolPtr(true)
2099
Colin Cross6b4a32d2017-12-05 13:42:45 -08002100 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2101 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002102 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002103}
2104
2105//
2106// Java prebuilts
2107//
2108
Colin Cross74d73e22017-08-02 11:05:49 -07002109type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002110 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002111
Nan Zhangea568a42017-11-08 21:20:04 -08002112 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002113
2114 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002115
2116 // List of shared java libs that this module has dependencies to
2117 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002118
2119 // List of files to remove from the jar file(s)
2120 Exclude_files []string
2121
2122 // List of directories to remove from the jar file(s)
2123 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002124
2125 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002126 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002127
2128 // set the name of the output
2129 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002130}
2131
2132type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002133 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002134 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002135 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002136 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002137 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002138
Colin Cross74d73e22017-08-02 11:05:49 -07002139 properties ImportProperties
2140
Colin Cross0a6e0072017-08-30 14:24:55 -07002141 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002142 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002143}
2144
Colin Cross83bb3162018-06-25 15:48:06 -07002145func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002146 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002147}
2148
2149func (j *Import) minSdkVersion() string {
2150 return j.sdkVersion()
2151}
2152
Colin Cross74d73e22017-08-02 11:05:49 -07002153func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002154 return &j.prebuilt
2155}
2156
Colin Cross74d73e22017-08-02 11:05:49 -07002157func (j *Import) PrebuiltSrcs() []string {
2158 return j.properties.Jars
2159}
2160
2161func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002162 return j.prebuilt.Name(j.ModuleBase.Name())
2163}
2164
Jiyong Park0b238752019-10-29 11:23:10 +09002165func (j *Import) Stem() string {
2166 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2167}
2168
Colin Cross74d73e22017-08-02 11:05:49 -07002169func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002170 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002171}
2172
Colin Cross74d73e22017-08-02 11:05:49 -07002173func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002174 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002175
Jiyong Park0b238752019-10-29 11:23:10 +09002176 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002177 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002178 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2179 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002180 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002181 inputFile := outputFile
2182 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2183 TransformJetifier(ctx, outputFile, inputFile)
2184 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002185 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002186
2187 ctx.VisitDirectDeps(func(module android.Module) {
2188 otherName := ctx.OtherModuleName(module)
2189 tag := ctx.OtherModuleDependencyTag(module)
2190
2191 switch dep := module.(type) {
2192 case Dependency:
2193 switch tag {
2194 case libTag, staticLibTag:
2195 // sdk lib names from dependencies are re-exported
2196 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2197 }
2198 case SdkLibraryDependency:
2199 switch tag {
2200 case libTag:
2201 // names of sdk libs that are directly depended are exported
2202 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2203 }
2204 }
2205 })
2206
2207 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002208 if Bool(j.properties.Installable) {
2209 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002210 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002211 }
Colin Cross2fe66872015-03-30 17:20:39 -07002212}
2213
Colin Cross74d73e22017-08-02 11:05:49 -07002214var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002215
Nan Zhanged19fc32017-10-19 13:06:22 -07002216func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002217 if j.combinedClasspathFile == nil {
2218 return nil
2219 }
Colin Cross37f6d792018-07-12 12:28:41 -07002220 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002221}
2222
2223func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002224 if j.combinedClasspathFile == nil {
2225 return nil
2226 }
Colin Cross37f6d792018-07-12 12:28:41 -07002227 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002228}
2229
Colin Cross331a1212018-08-15 20:40:52 -07002230func (j *Import) ResourceJars() android.Paths {
2231 return nil
2232}
2233
2234func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002235 if j.combinedClasspathFile == nil {
2236 return nil
2237 }
Colin Cross331a1212018-08-15 20:40:52 -07002238 return android.Paths{j.combinedClasspathFile}
2239}
2240
Colin Crossf24a22a2019-01-31 14:12:44 -08002241func (j *Import) DexJar() android.Path {
2242 return nil
2243}
2244
Colin Cross74d73e22017-08-02 11:05:49 -07002245func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002246 return nil
2247}
2248
Jiyong Park1be96912018-05-28 18:02:19 +09002249func (j *Import) ExportedSdkLibs() []string {
2250 return j.exportedSdkLibs
2251}
2252
Artur Satayev9cf46692019-11-26 18:08:34 +00002253func (j *Import) ExportedPlugins() (android.Paths, []string) {
2254 return nil, nil
2255}
2256
Colin Cross0c4ce212019-05-03 15:28:19 -07002257func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2258 return nil, nil
2259}
2260
albaltai36ff7dc2018-12-25 14:35:23 +08002261// Add compile time check for interface implementation
2262var _ android.IDEInfo = (*Import)(nil)
2263var _ android.IDECustomizedModuleName = (*Import)(nil)
2264
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002265// Collect information for opening IDE project files in java/jdeps.go.
2266const (
2267 removedPrefix = "prebuilt_"
2268)
2269
2270func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2271 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2272}
2273
2274func (j *Import) IDECustomizedModuleName() string {
2275 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2276 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2277 // solution to get the Import name.
2278 name := j.Name()
2279 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002280 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002281 }
2282 return name
2283}
2284
Colin Cross74d73e22017-08-02 11:05:49 -07002285var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002286
Colin Cross1b16b0e2019-02-12 14:41:32 -08002287// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2288//
2289// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2290// compiled against an Android classpath.
2291//
2292// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2293// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002294func ImportFactory() android.Module {
2295 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002296
Colin Cross74d73e22017-08-02 11:05:49 -07002297 module.AddProperties(&module.properties)
2298
2299 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002300 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002301 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002302 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002303 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002304}
2305
Colin Cross1b16b0e2019-02-12 14:41:32 -08002306// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2307// module.
2308//
2309// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2310// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002311func ImportFactoryHost() android.Module {
2312 module := &Import{}
2313
2314 module.AddProperties(&module.properties)
2315
2316 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002317 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002318 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002319 return module
2320}
2321
Colin Cross42be7612019-02-21 18:12:14 -08002322// dex_import module
2323
2324type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002325 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002326
2327 // set the name of the output
2328 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002329}
2330
2331type DexImport struct {
2332 android.ModuleBase
2333 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002334 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002335 prebuilt android.Prebuilt
2336
2337 properties DexImportProperties
2338
2339 dexJarFile android.Path
2340 maybeStrippedDexJarFile android.Path
2341
2342 dexpreopter
2343}
2344
2345func (j *DexImport) Prebuilt() *android.Prebuilt {
2346 return &j.prebuilt
2347}
2348
2349func (j *DexImport) PrebuiltSrcs() []string {
2350 return j.properties.Jars
2351}
2352
2353func (j *DexImport) Name() string {
2354 return j.prebuilt.Name(j.ModuleBase.Name())
2355}
2356
Jiyong Park0b238752019-10-29 11:23:10 +09002357func (j *DexImport) Stem() string {
2358 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2359}
2360
Colin Cross42be7612019-02-21 18:12:14 -08002361func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2362 if len(j.properties.Jars) != 1 {
2363 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2364 }
2365
Jiyong Park0b238752019-10-29 11:23:10 +09002366 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002367 j.dexpreopter.isInstallable = true
2368 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2369
2370 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2371 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2372
2373 if j.dexpreopter.uncompressedDex {
2374 rule := android.NewRuleBuilder()
2375
2376 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2377 rule.Temporary(temporary)
2378
2379 // use zip2zip to uncompress classes*.dex files
2380 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002381 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002382 FlagWithInput("-i ", inputJar).
2383 FlagWithOutput("-o ", temporary).
2384 FlagWithArg("-0 ", "'classes*.dex'")
2385
2386 // use zipalign to align uncompressed classes*.dex files
2387 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002388 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002389 Flag("-f").
2390 Text("4").
2391 Input(temporary).
2392 Output(dexOutputFile)
2393
2394 rule.DeleteTemporaryFiles()
2395
2396 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2397 } else {
2398 ctx.Build(pctx, android.BuildParams{
2399 Rule: android.Cp,
2400 Input: inputJar,
2401 Output: dexOutputFile,
2402 })
2403 }
2404
2405 j.dexJarFile = dexOutputFile
2406
2407 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2408
2409 j.maybeStrippedDexJarFile = dexOutputFile
2410
2411 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2412 ctx.ModuleName()+".jar", dexOutputFile)
2413}
2414
2415func (j *DexImport) DexJar() android.Path {
2416 return j.dexJarFile
2417}
2418
2419// dex_import imports a `.jar` file containing classes.dex files.
2420//
2421// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2422// to the device.
2423func DexImportFactory() android.Module {
2424 module := &DexImport{}
2425
2426 module.AddProperties(&module.properties)
2427
2428 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002429 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002430 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002431 return module
2432}
2433
Colin Cross89536d42017-07-07 14:35:50 -07002434//
2435// Defaults
2436//
2437type Defaults struct {
2438 android.ModuleBase
2439 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002440 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002441}
2442
Colin Cross1b16b0e2019-02-12 14:41:32 -08002443// java_defaults provides a set of properties that can be inherited by other java or android modules.
2444//
2445// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2446// property in the defaults module that exists in the depending module will be prepended to the depending module's
2447// value for that property.
2448//
2449// Example:
2450//
2451// java_defaults {
2452// name: "example_defaults",
2453// srcs: ["common/**/*.java"],
2454// javacflags: ["-Xlint:all"],
2455// aaptflags: ["--auto-add-overlay"],
2456// }
2457//
2458// java_library {
2459// name: "example",
2460// defaults: ["example_defaults"],
2461// srcs: ["example/**/*.java"],
2462// }
2463//
2464// is functionally identical to:
2465//
2466// java_library {
2467// name: "example",
2468// srcs: [
2469// "common/**/*.java",
2470// "example/**/*.java",
2471// ],
2472// javacflags: ["-Xlint:all"],
2473// }
Colin Cross89536d42017-07-07 14:35:50 -07002474func defaultsFactory() android.Module {
2475 return DefaultsFactory()
2476}
2477
Paul Duffin47357662019-12-05 14:07:14 +00002478func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002479 module := &Defaults{}
2480
Colin Cross89536d42017-07-07 14:35:50 -07002481 module.AddProperties(
2482 &CompilerProperties{},
2483 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002484 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002485 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002486 &aaptProperties{},
2487 &androidLibraryProperties{},
2488 &appProperties{},
2489 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002490 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002491 &ImportProperties{},
2492 &AARImportProperties{},
2493 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002494 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002495 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002496 )
2497
2498 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002499 return module
2500}
Nan Zhangea568a42017-11-08 21:20:04 -08002501
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002502func kytheExtractJavaFactory() android.Singleton {
2503 return &kytheExtractJavaSingleton{}
2504}
2505
2506type kytheExtractJavaSingleton struct {
2507}
2508
2509func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2510 var xrefTargets android.Paths
2511 ctx.VisitAllModules(func(module android.Module) {
2512 if javaModule, ok := module.(xref); ok {
2513 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2514 }
2515 })
2516 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2517 if len(xrefTargets) > 0 {
2518 ctx.Build(pctx, android.BuildParams{
2519 Rule: blueprint.Phony,
2520 Output: android.PathForPhony(ctx, "xref_java"),
2521 Inputs: xrefTargets,
2522 })
2523 }
2524}
2525
Nan Zhangea568a42017-11-08 21:20:04 -08002526var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002527var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002528var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002529var inList = android.InList