blob: 052e06f6f5a3b3c1e27188c954a869141c656342 [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
Colin Crossa4f08812018-10-02 22:03:40 -0700469}
470
Jiyong Park8be103b2019-11-08 15:53:48 +0900471func IsJniDepTag(depTag blueprint.DependencyTag) bool {
472 _, ok := depTag.(*jniDependencyTag)
473 return ok
474}
475
Colin Crossbe1da472017-07-07 15:59:46 -0700476var (
Colin Cross4b964c02018-10-15 16:18:06 -0700477 staticLibTag = dependencyTag{name: "staticlib"}
478 libTag = dependencyTag{name: "javalib"}
Colin Cross6cef4812019-10-17 14:23:50 -0700479 java9LibTag = dependencyTag{name: "java9lib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800480 pluginTag = dependencyTag{name: "plugin"}
Artur Satayev9cf46692019-11-26 18:08:34 +0000481 exportedPluginTag = dependencyTag{name: "exported-plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700482 bootClasspathTag = dependencyTag{name: "bootclasspath"}
483 systemModulesTag = dependencyTag{name: "system modules"}
484 frameworkResTag = dependencyTag{name: "framework-res"}
485 frameworkApkTag = dependencyTag{name: "framework-apk"}
486 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800487 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700488 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
489 certificateTag = dependencyTag{name: "certificate"}
490 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700491 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700492)
Colin Cross2fe66872015-03-30 17:20:39 -0700493
Colin Crossfc3674a2017-09-18 17:41:52 -0700494type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700495 useModule, useFiles, useDefaultLibs, invalidVersion bool
496
Colin Cross6cef4812019-10-17 14:23:50 -0700497 // The modules that will be added to the bootclasspath when targeting 1.8 or lower
498 bootclasspath []string
Paul Duffine25c6442019-10-11 13:50:28 +0100499
500 // The default system modules to use. Will be an empty string if no system
501 // modules are to be used.
Colin Cross1369cdb2017-09-29 17:58:17 -0700502 systemModules string
503
Colin Cross6cef4812019-10-17 14:23:50 -0700504 // The modules that will be added ot the classpath when targeting 1.9 or higher
505 java9Classpath []string
506
Colin Crossa97c5d32018-03-28 14:58:31 -0700507 frameworkResModule string
508
Colin Cross86a60ae2018-05-29 14:44:55 -0700509 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700510 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100511
512 noStandardLibs, noFrameworksLibs bool
513}
514
515func (s sdkDep) hasStandardLibs() bool {
516 return !s.noStandardLibs
517}
518
519func (s sdkDep) hasFrameworkLibs() bool {
520 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700521}
522
Colin Crossa4f08812018-10-02 22:03:40 -0700523type jniLib struct {
524 name string
525 path android.Path
526 target android.Target
527}
528
Colin Cross0ea8ba82019-06-06 14:33:29 -0700529func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800530 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
531}
532
Colin Cross0ea8ba82019-06-06 14:33:29 -0700533func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800534 return j.shouldInstrument(ctx) &&
535 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
536 ctx.Config().UnbundledBuild())
537}
538
Colin Cross83bb3162018-06-25 15:48:06 -0700539func (j *Module) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900540 return String(j.deviceProperties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -0700541}
542
Paul Duffine25c6442019-10-11 13:50:28 +0100543func (j *Module) systemModules() string {
544 return proptools.String(j.deviceProperties.System_modules)
545}
546
Colin Cross83bb3162018-06-25 15:48:06 -0700547func (j *Module) minSdkVersion() string {
548 if j.deviceProperties.Min_sdk_version != nil {
549 return *j.deviceProperties.Min_sdk_version
550 }
551 return j.sdkVersion()
552}
553
Dan Willemsen419290a2018-10-31 15:28:47 -0700554func (j *Module) targetSdkVersion() string {
555 if j.deviceProperties.Target_sdk_version != nil {
556 return *j.deviceProperties.Target_sdk_version
557 }
558 return j.sdkVersion()
559}
560
Jiyong Parkb02bb402019-12-03 00:43:57 +0900561func (j *Module) AvailableFor(what string) bool {
562 if what == android.AvailableToPlatform && Bool(j.deviceProperties.Hostdex) {
563 // Exception: for hostdex: true libraries, the platform variant is created
564 // even if it's not marked as available to platform. In that case, the platform
565 // variant is used only for the hostdex and not installed to the device.
566 return true
567 }
568 return j.ApexModuleBase.AvailableFor(what)
569}
570
Colin Crossbe1da472017-07-07 15:59:46 -0700571func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700572 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100573 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross6d8d8c62019-10-28 15:10:03 -0700574 if sdkDep.useDefaultLibs {
575 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
576 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
577 if sdkDep.hasFrameworkLibs() {
578 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossbe1da472017-07-07 15:59:46 -0700579 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700580 } else if sdkDep.useModule {
Colin Cross6cef4812019-10-17 14:23:50 -0700581 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...)
Paul Duffine25c6442019-10-11 13:50:28 +0100582 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross6cef4812019-10-17 14:23:50 -0700583 ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...)
Colin Cross6d8d8c62019-10-28 15:10:03 -0700584 if j.deviceProperties.EffectiveOptimizeEnabled() && sdkDep.hasStandardLibs() {
585 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
586 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
587 }
Colin Cross2fe66872015-03-30 17:20:39 -0700588 }
Colin Cross6d8d8c62019-10-28 15:10:03 -0700589
Nan Zhangb2b33de2018-02-23 11:18:47 -0800590 if ctx.ModuleName() == "android_stubs_current" ||
591 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700592 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700593 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800594 }
Colin Cross2fe66872015-03-30 17:20:39 -0700595 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700596
Inseob Kimac1e9862019-12-09 18:15:47 +0900597 syspropPublicStubs := syspropPublicStubs(ctx.Config())
598
599 // rewriteSyspropLibs validates if a java module can link against platform's sysprop_library,
600 // and redirects dependency to public stub depending on the link type.
601 rewriteSyspropLibs := func(libs []string, prop string) []string {
602 // make a copy
603 ret := android.CopyOf(libs)
604
605 for idx, lib := range libs {
606 stub, ok := syspropPublicStubs[lib]
607
608 if !ok {
609 continue
610 }
611
612 linkType, _ := j.getLinkType(ctx.ModuleName())
613 if linkType == javaSystem {
614 ret[idx] = stub
615 } else if linkType != javaPlatform {
616 ctx.PropertyErrorf("sdk_version",
617 "can't link against sysprop_library %q from a module using public or core API",
618 lib)
619 }
620 }
621
622 return ret
623 }
624
625 ctx.AddVariationDependencies(nil, libTag, rewriteSyspropLibs(j.properties.Libs, "libs")...)
626 ctx.AddVariationDependencies(nil, staticLibTag, rewriteSyspropLibs(j.properties.Static_libs, "static_libs")...)
Colin Crossa4f08812018-10-02 22:03:40 -0700627
Colin Cross0f7d2ef2019-10-16 11:03:10 -0700628 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), pluginTag, j.properties.Plugins...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000629 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), exportedPluginTag, j.properties.Exported_plugins...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800630
Colin Crossfe17f6f2019-03-28 19:30:56 -0700631 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700632 if j.hasSrcExt(".proto") {
633 protoDeps(ctx, &j.protoProperties)
634 }
Colin Cross93e85952017-08-15 13:34:18 -0700635
636 if j.hasSrcExt(".kt") {
637 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
638 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700639 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
640 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800641 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800642 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
643 }
Colin Cross93e85952017-08-15 13:34:18 -0700644 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800645
646 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700647 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800648 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700649}
650
651func hasSrcExt(srcs []string, ext string) bool {
652 for _, src := range srcs {
653 if filepath.Ext(src) == ext {
654 return true
655 }
656 }
657
658 return false
659}
660
661func (j *Module) hasSrcExt(ext string) bool {
662 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700663}
664
Colin Cross46c9b8b2017-06-22 16:51:17 -0700665func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700666 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700667
Colin Crossebe1a512017-11-14 13:12:14 -0800668 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
669 aidlIncludes = append(aidlIncludes,
670 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
671 aidlIncludes = append(aidlIncludes,
672 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700673
Colin Cross3047fa22019-04-18 10:56:44 -0700674 var flags []string
675 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700676
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700677 if aidlPreprocess.Valid() {
678 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700679 deps = append(deps, aidlPreprocess.Path())
680 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700681 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700682 }
683
Colin Cross3047fa22019-04-18 10:56:44 -0700684 if len(j.exportAidlIncludeDirs) > 0 {
685 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
686 }
687
688 if len(aidlIncludes) > 0 {
689 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
690 }
691
Colin Cross635c3b02016-05-18 15:37:25 -0700692 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800693 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700694 flags = append(flags, "-I"+src.String())
695 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700696
Martijn Coeneneab15642018-03-09 09:29:59 +0100697 if Bool(j.deviceProperties.Aidl.Generate_traces) {
698 flags = append(flags, "-t")
699 }
700
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100701 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
702 flags = append(flags, "--transaction_names")
703 }
704
Colin Cross3047fa22019-04-18 10:56:44 -0700705 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700706}
707
Colin Cross32f676a2017-09-06 13:41:06 -0700708type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800709 classpath classpath
Colin Cross6cef4812019-10-17 14:23:50 -0700710 java9Classpath classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800711 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700712 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800713 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700714 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700715 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700716 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700717 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800718 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700719 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700720 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700721 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700722 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800723 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800724
725 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700726}
Colin Cross2fe66872015-03-30 17:20:39 -0700727
Colin Cross54250902017-12-05 09:28:08 -0800728func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
729 for _, f := range dep.Srcs() {
730 if f.Ext() != ".jar" {
731 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
732 ctx.OtherModuleName(dep.(blueprint.Module)))
733 }
734 }
735}
736
Jiyong Park2d492942018-03-05 17:44:10 +0900737type linkType int
738
739const (
740 javaCore linkType = iota
741 javaSdk
742 javaSystem
743 javaPlatform
744)
745
Jeongik Cha75b83b02019-11-01 15:28:00 +0900746type linkTypeContext interface {
747 android.Module
748 getLinkType(name string) (ret linkType, stubs bool)
749}
750
751func (m *Module) getLinkType(name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700752 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700753 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900754 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
755 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100756 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900757 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100758 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900759 return javaCore, false
760 case name == "android_system_stubs_current":
761 return javaSystem, true
762 case strings.HasPrefix(ver, "system_"):
763 return javaSystem, false
764 case name == "android_test_stubs_current":
765 return javaSystem, true
766 case strings.HasPrefix(ver, "test_"):
767 return javaPlatform, false
768 case name == "android_stubs_current":
769 return javaSdk, true
770 case ver == "current":
771 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100772 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900773 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700774 default:
775 if _, err := strconv.Atoi(ver); err != nil {
776 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
777 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900778 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900779 }
780}
781
Jeongik Cha75b83b02019-11-01 15:28:00 +0900782func checkLinkType(ctx android.ModuleContext, from *Module, to linkTypeContext, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700783 if ctx.Host() {
784 return
785 }
786
Jeongik Cha75b83b02019-11-01 15:28:00 +0900787 myLinkType, stubs := from.getLinkType(ctx.ModuleName())
Jiyong Park46f78fb2018-10-20 16:33:17 +0900788 if stubs {
789 return
790 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900791 otherLinkType, _ := to.getLinkType(ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900792 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."
793
794 switch myLinkType {
795 case javaCore:
796 if otherLinkType != javaCore {
797 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 +0900798 ctx.OtherModuleName(to))
799 }
Jiyong Park2d492942018-03-05 17:44:10 +0900800 break
801 case javaSdk:
802 if otherLinkType != javaCore && otherLinkType != javaSdk {
803 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
804 ctx.OtherModuleName(to))
805 }
806 break
807 case javaSystem:
808 if otherLinkType == javaPlatform {
809 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
810 ctx.OtherModuleName(to))
811 }
812 break
813 case javaPlatform:
814 // no restriction on link-type
815 break
Jiyong Park750e5572018-01-31 00:20:13 +0900816 }
817}
818
Colin Cross32f676a2017-09-06 13:41:06 -0700819func (j *Module) collectDeps(ctx android.ModuleContext) deps {
820 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700821
Colin Cross300f0382018-03-06 13:11:51 -0800822 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700823 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800824 if sdkDep.invalidVersion {
Colin Cross6cef4812019-10-17 14:23:50 -0700825 ctx.AddMissingDependencies(sdkDep.bootclasspath)
826 ctx.AddMissingDependencies(sdkDep.java9Classpath)
Colin Cross300f0382018-03-06 13:11:51 -0800827 } else if sdkDep.useFiles {
828 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700829 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700830 deps.aidlPreprocess = sdkDep.aidl
831 } else {
832 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800833 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700834 }
835
Colin Crossd11fcda2017-10-23 17:59:01 -0700836 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700837 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700838 tag := ctx.OtherModuleDependencyTag(module)
839
Colin Crossa4f08812018-10-02 22:03:40 -0700840 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700841 // Handled by AndroidApp.collectAppDeps
842 return
843 }
844 if tag == certificateTag {
845 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700846 return
847 }
Jeongik Cha75b83b02019-11-01 15:28:00 +0900848 switch module.(type) {
Jeongik Chae403e9e2019-12-07 00:16:24 +0900849 case *Library, *AndroidLibrary:
Jeongik Cha75b83b02019-11-01 15:28:00 +0900850 if to, ok := module.(linkTypeContext); ok {
851 switch tag {
852 case bootClasspathTag, libTag, staticLibTag:
853 checkLinkType(ctx, j, to, tag.(dependencyTag))
854 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700855 }
Jiyong Park750e5572018-01-31 00:20:13 +0900856 }
Colin Cross54250902017-12-05 09:28:08 -0800857 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800858 case SdkLibraryDependency:
859 switch tag {
860 case libTag:
861 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
862 // names of sdk libs that are directly depended are exported
863 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700864 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800865 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
866 }
Colin Cross54250902017-12-05 09:28:08 -0800867 case Dependency:
868 switch tag {
869 case bootClasspathTag:
870 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700871 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800872 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900873 // sdk lib names from dependencies are re-exported
874 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700875 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000876 pluginJars, pluginClasses := dep.ExportedPlugins()
877 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Cross6cef4812019-10-17 14:23:50 -0700878 case java9LibTag:
879 deps.java9Classpath = append(deps.java9Classpath, dep.HeaderJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800880 case staticLibTag:
881 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
882 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
883 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700884 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900885 // sdk lib names from dependencies are re-exported
886 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700887 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Artur Satayev9cf46692019-11-26 18:08:34 +0000888 pluginJars, pluginClasses := dep.ExportedPlugins()
889 addPlugins(&deps, pluginJars, pluginClasses...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800890 case pluginTag:
891 if plugin, ok := dep.(*Plugin); ok {
Colin Crossbe9cdb82019-01-21 21:37:16 -0800892 if plugin.pluginProperties.Processor_class != nil {
Artur Satayev9cf46692019-11-26 18:08:34 +0000893 addPlugins(&deps, plugin.ImplementationAndResourcesJars(), *plugin.pluginProperties.Processor_class)
894 } else {
895 addPlugins(&deps, plugin.ImplementationAndResourcesJars())
Colin Crossbe9cdb82019-01-21 21:37:16 -0800896 }
897 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
898 } else {
899 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
900 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000901 case exportedPluginTag:
902 if plugin, ok := dep.(*Plugin); ok {
903 if plugin.pluginProperties.Generates_api != nil && *plugin.pluginProperties.Generates_api {
904 ctx.PropertyErrorf("exported_plugins", "Cannot export plugins with generates_api = true, found %v", otherName)
905 }
906 j.exportedPluginJars = append(j.exportedPluginJars, plugin.ImplementationAndResourcesJars()...)
907 if plugin.pluginProperties.Processor_class != nil {
908 j.exportedPluginClasses = append(j.exportedPluginClasses, *plugin.pluginProperties.Processor_class)
909 }
910 } else {
911 ctx.PropertyErrorf("exported_plugins", "%q is not a java_plugin module", otherName)
912 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800913 case frameworkApkTag:
914 if ctx.ModuleName() == "android_stubs_current" ||
915 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700916 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800917 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
918 // resource files out of there for aapt.
919 //
920 // Normally the package rule runs aapt, which includes the resource,
921 // but we're not running that in our package rule so just copy in the
922 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700923 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800924 }
Colin Cross54250902017-12-05 09:28:08 -0800925 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700926 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800927 case kotlinAnnotationsTag:
928 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800929 }
930
Colin Cross54250902017-12-05 09:28:08 -0800931 case android.SourceFileProducer:
932 switch tag {
933 case libTag:
934 checkProducesJars(ctx, dep)
935 deps.classpath = append(deps.classpath, dep.Srcs()...)
936 case staticLibTag:
937 checkProducesJars(ctx, dep)
938 deps.classpath = append(deps.classpath, dep.Srcs()...)
939 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
940 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800941 }
942 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700943 switch tag {
Paul Duffin68289b02019-09-20 13:50:52 +0100944 case bootClasspathTag:
945 // If a system modules dependency has been added to the bootclasspath
946 // then add its libs to the bootclasspath.
947 sm := module.(*SystemModules)
948 deps.bootClasspath = append(deps.bootClasspath, sm.headerJars...)
949
Colin Cross1369cdb2017-09-29 17:58:17 -0700950 case systemModulesTag:
951 if deps.systemModules != nil {
952 panic("Found two system module dependencies")
953 }
954 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000955 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700956 panic("Missing directory for system module dependency")
957 }
Colin Crossb77043e2019-07-16 13:57:13 -0700958 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700959 }
Colin Crossec7a0422017-07-07 14:47:12 -0700960 }
Colin Cross2fe66872015-03-30 17:20:39 -0700961 })
962
Jiyong Park1be96912018-05-28 18:02:19 +0900963 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
964
Colin Cross32f676a2017-09-06 13:41:06 -0700965 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700966}
967
Artur Satayev9cf46692019-11-26 18:08:34 +0000968func addPlugins(deps *deps, pluginJars android.Paths, pluginClasses ...string) {
969 deps.processorPath = append(deps.processorPath, pluginJars...)
970 deps.processorClasses = append(deps.processorClasses, pluginClasses...)
971}
972
Colin Cross1e743852019-10-28 11:37:20 -0700973func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) javaVersion {
Colin Cross98fd5742019-01-09 23:04:25 -0800974 v := sdkContext.sdkVersion()
975 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100976 if ctx.Config().IsPdkBuild() &&
977 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700978 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800979 latestSdkVersion := 0
980 if len(sdkVersions) > 0 {
981 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
982 }
983 v = strconv.Itoa(latestSdkVersion)
984 }
985
986 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700987 if err != nil {
988 ctx.PropertyErrorf("sdk_version", "%s", err)
989 }
Nan Zhang357466b2018-04-17 17:38:36 -0700990 if javaVersion != "" {
Colin Cross1e743852019-10-28 11:37:20 -0700991 return normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700992 } else if ctx.Device() && sdk <= 23 {
Colin Cross1e743852019-10-28 11:37:20 -0700993 return JAVA_VERSION_7
Pete Gillina1c9e9d2019-10-17 14:52:07 +0100994 } else if ctx.Device() && sdk <= 29 {
Colin Cross1e743852019-10-28 11:37:20 -0700995 return JAVA_VERSION_8
Colin Cross6cef4812019-10-17 14:23:50 -0700996 } else if ctx.Device() && ctx.Config().UnbundledBuildUsePrebuiltSdks() {
997 // TODO(b/142896162): once we have prebuilt system modules we can use 1.9 for unbundled builds
Colin Cross1e743852019-10-28 11:37:20 -0700998 return JAVA_VERSION_8
Nan Zhang357466b2018-04-17 17:38:36 -0700999 } else {
Colin Cross1e743852019-10-28 11:37:20 -07001000 return JAVA_VERSION_9
Nan Zhang357466b2018-04-17 17:38:36 -07001001 }
Nan Zhang357466b2018-04-17 17:38:36 -07001002}
1003
Colin Cross1e743852019-10-28 11:37:20 -07001004type javaVersion int
1005
1006const (
1007 JAVA_VERSION_UNSUPPORTED = 0
1008 JAVA_VERSION_6 = 6
1009 JAVA_VERSION_7 = 7
1010 JAVA_VERSION_8 = 8
1011 JAVA_VERSION_9 = 9
1012)
1013
1014func (v javaVersion) String() string {
1015 switch v {
1016 case JAVA_VERSION_6:
1017 return "1.6"
1018 case JAVA_VERSION_7:
1019 return "1.7"
1020 case JAVA_VERSION_8:
1021 return "1.8"
1022 case JAVA_VERSION_9:
1023 return "1.9"
1024 default:
1025 return "unsupported"
1026 }
1027}
1028
1029// Returns true if javac targeting this version uses system modules instead of a bootclasspath.
1030func (v javaVersion) usesJavaModules() bool {
1031 return v >= 9
1032}
1033
1034func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion {
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001035 switch javaVersion {
1036 case "1.6", "6":
Colin Cross1e743852019-10-28 11:37:20 -07001037 return JAVA_VERSION_6
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001038 case "1.7", "7":
Colin Cross1e743852019-10-28 11:37:20 -07001039 return JAVA_VERSION_7
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001040 case "1.8", "8":
Colin Cross1e743852019-10-28 11:37:20 -07001041 return JAVA_VERSION_8
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001042 case "1.9", "9":
Colin Cross1e743852019-10-28 11:37:20 -07001043 return JAVA_VERSION_9
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001044 case "10", "11":
1045 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
Colin Cross1e743852019-10-28 11:37:20 -07001046 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001047 default:
1048 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
Colin Cross1e743852019-10-28 11:37:20 -07001049 return JAVA_VERSION_UNSUPPORTED
Pete Gillin4e8b48a2019-07-12 13:16:17 +01001050 }
1051}
1052
Nan Zhanged19fc32017-10-19 13:06:22 -07001053func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -07001054
Colin Crossf03c82b2015-04-13 13:53:40 -07001055 var flags javaBuilderFlags
1056
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001057 // javaVersion flag.
1058 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
1059
Nan Zhanged19fc32017-10-19 13:06:22 -07001060 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -07001061 javacFlags := j.properties.Javacflags
Colin Cross1e743852019-10-28 11:37:20 -07001062 if flags.javaVersion.usesJavaModules() {
Colin Cross1369cdb2017-09-29 17:58:17 -07001063 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -07001064 }
Colin Cross6510f912017-11-29 00:27:14 -08001065 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -07001066 // Override the -g flag passed globally to remove local variable debug info to reduce
1067 // disk and memory usage.
1068 javacFlags = append(javacFlags, "-g:source,lines")
1069 }
Colin Crossc228a702019-11-06 16:18:05 -08001070 javacFlags = append(javacFlags, "-Xlint:-dep-ann")
Colin Cross64162712017-08-08 13:17:59 -07001071
Colin Cross66548102018-06-19 22:47:35 -07001072 if ctx.Config().RunErrorProne() {
1073 if config.ErrorProneClasspath == nil {
1074 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
1075 }
1076
1077 errorProneFlags := []string{
1078 "-Xplugin:ErrorProne",
1079 "${config.ErrorProneChecks}",
1080 }
1081 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
1082
1083 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
1084 "'" + strings.Join(errorProneFlags, " ") + "'"
1085 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -08001086 }
1087
Nan Zhanged19fc32017-10-19 13:06:22 -07001088 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -08001089 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
1090 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6cef4812019-10-17 14:23:50 -07001091 flags.java9Classpath = append(flags.java9Classpath, deps.java9Classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -07001092 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -08001093
Colin Crossbe9cdb82019-01-21 21:37:16 -08001094 flags.processor = strings.Join(deps.processorClasses, ",")
1095
Colin Cross1e743852019-10-28 11:37:20 -07001096 if len(flags.bootClasspath) == 0 && ctx.Host() && !flags.javaVersion.usesJavaModules() &&
1097 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() {
Colin Cross7fdd2b72018-01-02 18:14:25 -08001098 // Give host-side tools a version of OpenJDK's standard libraries
1099 // close to what they're targeting. As of Dec 2017, AOSP is only
1100 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1101 //
1102 // When building with OpenJDK 8, the following should have no
1103 // effect since those jars would be available by default.
1104 //
1105 // When building with OpenJDK 9 but targeting a version < 1.8,
1106 // putting them on the bootclasspath means that:
1107 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1108 // b) references to existing APIs are not reinterpreted in an
1109 // OpenJDK 9-specific way, eg. calls to subclasses of
1110 // java.nio.Buffer as in http://b/70862583
1111 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1112 flags.bootClasspath = append(flags.bootClasspath,
1113 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1114 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001115 if Bool(j.properties.Use_tools_jar) {
1116 flags.bootClasspath = append(flags.bootClasspath,
1117 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1118 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001119 }
1120
Colin Cross1e743852019-10-28 11:37:20 -07001121 if j.properties.Patch_module != nil && flags.javaVersion.usesJavaModules() {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001122 // Manually specify build directory in case it is not under the repo root.
1123 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1124 // just adding a symlink under the root doesn't help.)
1125 patchPaths := ".:" + ctx.Config().BuildDir()
1126 classPath := flags.classpath.FormJavaClassPath("")
1127 if classPath != "" {
1128 patchPaths += ":" + classPath
1129 }
1130 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001131 }
1132
Nan Zhanged19fc32017-10-19 13:06:22 -07001133 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001134 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001135
Nan Zhanged19fc32017-10-19 13:06:22 -07001136 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001137 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001138
Colin Cross81440082018-08-15 20:21:55 -07001139 if len(javacFlags) > 0 {
1140 // optimization.
1141 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1142 flags.javacFlags = "$javacFlags"
1143 }
1144
Nan Zhanged19fc32017-10-19 13:06:22 -07001145 return flags
1146}
Colin Crossc0b06f12015-04-08 13:03:43 -07001147
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001148func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001149 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001150
1151 deps := j.collectDeps(ctx)
1152 flags := j.collectBuilderFlags(ctx, deps)
1153
Colin Cross1e743852019-10-28 11:37:20 -07001154 if flags.javaVersion.usesJavaModules() {
Nan Zhanged19fc32017-10-19 13:06:22 -07001155 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1156 }
Colin Cross8a497952019-03-05 22:25:09 -08001157 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001158 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001159 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001160 }
1161
Colin Crossaf050172017-11-15 23:01:59 -08001162 srcFiles = j.genSources(ctx, srcFiles, flags)
1163
1164 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001165 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001166 if aaptSrcJar != nil {
1167 srcJars = append(srcJars, aaptSrcJar)
1168 }
Colin Crossb7a63242015-04-16 14:09:14 -07001169
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001170 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001171 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001172 }
1173
Colin Cross1ee23172017-10-18 14:44:18 -07001174 jarName := ctx.ModuleName() + ".jar"
1175
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001176 javaSrcFiles := srcFiles.FilterByExt(".java")
1177 var uniqueSrcFiles android.Paths
1178 set := make(map[string]bool)
1179 for _, v := range javaSrcFiles {
1180 if _, found := set[v.String()]; !found {
1181 set[v.String()] = true
1182 uniqueSrcFiles = append(uniqueSrcFiles, v)
1183 }
1184 }
1185
patricktu242faad2019-09-24 15:41:30 +08001186 // Collect .java files for AIDEGen
1187 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1188
Colin Cross55f63ea2018-08-27 12:37:09 -07001189 var kotlinJars android.Paths
1190
Colin Cross93e85952017-08-15 13:34:18 -07001191 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001192 // user defined kotlin flags.
1193 kotlincFlags := j.properties.Kotlincflags
1194 CheckKotlincFlags(ctx, kotlincFlags)
1195
Colin Cross93e85952017-08-15 13:34:18 -07001196 // If there are kotlin files, compile them first but pass all the kotlin and java files
1197 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1198 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001199 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001200 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001201 kotlincFlags = append(kotlincFlags, "-no-jdk")
1202 }
1203 if len(kotlincFlags) > 0 {
1204 // optimization.
1205 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1206 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001207 }
1208
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001209 var kotlinSrcFiles android.Paths
1210 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1211 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1212
patricktu242faad2019-09-24 15:41:30 +08001213 // Collect .kt files for AIDEGen
1214 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1215
Colin Crossafbb1732019-01-17 15:42:52 -08001216 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1217 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1218
1219 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1220 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1221
1222 if len(flags.processorPath) > 0 {
1223 // Use kapt for annotation processing
1224 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1225 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1226 srcJars = append(srcJars, kaptSrcJar)
1227 // Disable annotation processing in javac, it's already been handled by kapt
1228 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001229 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001230 }
Colin Cross93e85952017-08-15 13:34:18 -07001231
Colin Cross1ee23172017-10-18 14:44:18 -07001232 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001233 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001234 if ctx.Failed() {
1235 return
1236 }
1237
1238 // Make javac rule depend on the kotlinc rule
1239 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001240
Colin Cross93e85952017-08-15 13:34:18 -07001241 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001242 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001243 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001244 }
1245
Colin Cross55f63ea2018-08-27 12:37:09 -07001246 jars := append(android.Paths(nil), kotlinJars...)
1247
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001248 // Store the list of .java files that was passed to javac
1249 j.compiledJavaSrcs = uniqueSrcFiles
1250 j.compiledSrcJars = srcJars
1251
Nan Zhang61eaedb2017-11-02 13:28:15 -07001252 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001253 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001254 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1255 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001256 // Formerly, there was a check here that prevented annotation processors
1257 // from being used when sharding was enabled, as some annotation processors
1258 // do not function correctly in sharded environments. It was removed to
1259 // allow for the use of annotation processors that do function correctly
1260 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001261 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001262 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001263 if ctx.Failed() {
1264 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001265 }
1266 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001267 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001268 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001269 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001270 // If error-prone is enabled, add an additional rule to compile the java files into
1271 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001272 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001273 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1274 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001275 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001276 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001277 extraJarDeps = append(extraJarDeps, errorprone)
1278 }
1279
Nan Zhang61eaedb2017-11-02 13:28:15 -07001280 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001281 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001282 shardSize := int(*(j.properties.Javac_shard_size))
1283 var shardSrcs []android.Paths
1284 if len(uniqueSrcFiles) > 0 {
Colin Cross0a2f7192019-09-23 14:33:09 -07001285 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001286 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001287 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1288 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001289 jars = append(jars, classes)
1290 }
1291 }
1292 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001293 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1294 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001295 jars = append(jars, classes)
1296 }
1297 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001298 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001299 jars = append(jars, classes)
1300 }
Colin Crossd6891432017-09-27 17:39:56 -07001301 if ctx.Failed() {
1302 return
1303 }
Colin Cross2fe66872015-03-30 17:20:39 -07001304 }
1305
Colin Cross0c4ce212019-05-03 15:28:19 -07001306 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1307
1308 var includeSrcJar android.WritablePath
1309 if Bool(j.properties.Include_srcs) {
1310 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1311 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1312 }
1313
Colin Crosscedd4762018-09-13 11:26:19 -07001314 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1315 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001316 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001317 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001318
1319 var resArgs []string
1320 var resDeps android.Paths
1321
1322 resArgs = append(resArgs, dirArgs...)
1323 resDeps = append(resDeps, dirDeps...)
1324
1325 resArgs = append(resArgs, fileArgs...)
1326 resDeps = append(resDeps, fileDeps...)
1327
Colin Cross988708c2019-05-06 14:04:11 -07001328 resArgs = append(resArgs, extraArgs...)
1329 resDeps = append(resDeps, extraDeps...)
1330
Colin Cross40a36712017-09-27 17:41:35 -07001331 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001332 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001333 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001334 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001335 if ctx.Failed() {
1336 return
1337 }
1338 }
1339
Colin Cross0c4ce212019-05-03 15:28:19 -07001340 var resourceJars android.Paths
1341 if j.resourceJar != nil {
1342 resourceJars = append(resourceJars, j.resourceJar)
1343 }
1344 if Bool(j.properties.Include_srcs) {
1345 resourceJars = append(resourceJars, includeSrcJar)
1346 }
1347 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001348
Colin Cross0c4ce212019-05-03 15:28:19 -07001349 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001350 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001351 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001352 false, nil, nil)
1353 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001354 } else if len(resourceJars) == 1 {
1355 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001356 }
1357
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001358 if len(deps.staticJars) > 0 {
1359 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001360 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001361
Colin Cross094054a2018-10-17 15:10:48 -07001362 manifest := j.overrideManifest
1363 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001364 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001365 }
Colin Cross635acc92017-09-12 22:50:46 -07001366
Colin Cross8a497952019-03-05 22:25:09 -08001367 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001368 if len(services) > 0 {
1369 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1370 var zipargs []string
1371 for _, file := range services {
1372 serviceFile := file.String()
1373 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1374 }
1375 ctx.Build(pctx, android.BuildParams{
1376 Rule: zip,
1377 Output: servicesJar,
1378 Implicits: services,
1379 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001380 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001381 },
1382 })
1383 jars = append(jars, servicesJar)
1384 }
1385
Colin Cross0a6e0072017-08-30 14:24:55 -07001386 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001387 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001388 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001389
1390 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001391 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1392 // Optimization: skip the combine step if there is nothing to do
1393 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1394 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1395 // any if len(jars) == 1.
1396 outputFile = moduleOutPath
1397 } else {
1398 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1399 ctx.Build(pctx, android.BuildParams{
1400 Rule: android.Cp,
1401 Input: jars[0],
1402 Output: combinedJar,
1403 })
1404 outputFile = combinedJar
1405 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001406 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001407 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001408 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001409 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001410 outputFile = combinedJar
1411 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001412
Colin Cross331a1212018-08-15 20:40:52 -07001413 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001414 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001415 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001416 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001417 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001418 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001419
1420 // jarjar resource jar if necessary
1421 if j.resourceJar != nil {
1422 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001423 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001424 j.resourceJar = resourceJarJarFile
1425 }
1426
Colin Cross0a6e0072017-08-30 14:24:55 -07001427 if ctx.Failed() {
1428 return
1429 }
1430 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001431
1432 // Check package restrictions if necessary.
1433 if len(j.properties.Permitted_packages) > 0 {
1434 // Check packages and copy to package-checked file.
1435 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1436 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1437 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1438
1439 if ctx.Failed() {
1440 return
1441 }
1442 }
1443
Nan Zhanged19fc32017-10-19 13:06:22 -07001444 j.implementationJarFile = outputFile
1445 if j.headerJarFile == nil {
1446 j.headerJarFile = j.implementationJarFile
1447 }
Colin Cross2fe66872015-03-30 17:20:39 -07001448
Colin Cross6510f912017-11-29 00:27:14 -08001449 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001450 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1451 j.properties.Instrument = true
1452 }
1453 }
1454
Colin Cross3144dfc2018-01-03 15:06:47 -08001455 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001456 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1457 }
1458
Colin Cross331a1212018-08-15 20:40:52 -07001459 // merge implementation jar with resources if necessary
1460 implementationAndResourcesJar := outputFile
1461 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001462 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001463 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001464 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001465 false, nil, nil)
1466 implementationAndResourcesJar = combinedJar
1467 }
1468
1469 j.implementationAndResourcesJar = implementationAndResourcesJar
1470
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001471 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001472 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001473 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001474 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001475 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001476 if ctx.Failed() {
1477 return
1478 }
Colin Cross331a1212018-08-15 20:40:52 -07001479
Jiyong Park09cb6292019-07-15 15:29:23 +09001480 // Hidden API CSV generation and dex encoding
1481 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1482 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001483
Colin Cross331a1212018-08-15 20:40:52 -07001484 // merge dex jar with resources if necessary
1485 if j.resourceJar != nil {
1486 jars := android.Paths{dexOutputFile, j.resourceJar}
1487 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1488 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1489 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001490 if j.deviceProperties.UncompressDex {
1491 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1492 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1493 dexOutputFile = combinedAlignedJar
1494 } else {
1495 dexOutputFile = combinedJar
1496 }
Colin Cross331a1212018-08-15 20:40:52 -07001497 }
1498
1499 j.dexJarFile = dexOutputFile
1500
Colin Cross8faf8fc2019-01-16 15:15:52 -08001501 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001502 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1503
1504 j.maybeStrippedDexJarFile = dexOutputFile
1505
Colin Cross3063b782018-08-15 11:19:12 -07001506 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001507
1508 if ctx.Failed() {
1509 return
1510 }
Colin Cross331a1212018-08-15 20:40:52 -07001511 } else {
1512 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001513 }
Colin Cross331a1212018-08-15 20:40:52 -07001514
Colin Crossb7a63242015-04-16 14:09:14 -07001515 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001516
1517 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1518 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001519}
1520
Colin Cross3b706fd2019-09-05 16:44:18 -07001521func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1522 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1523
1524 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1525 if idx >= 0 {
1526 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1527 jarName += strconv.Itoa(idx)
1528 }
1529
1530 classes := android.PathForModuleOut(ctx, "javac", jarName)
1531 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1532
1533 if ctx.Config().EmitXrefRules() {
1534 extractionFile := android.PathForModuleOut(ctx, kzipName)
1535 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1536 j.kytheFiles = append(j.kytheFiles, extractionFile)
1537 }
1538
1539 return classes
1540}
1541
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001542// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1543// since some of these flags may be used internally.
1544func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1545 for _, flag := range flags {
1546 flag = strings.TrimSpace(flag)
1547
1548 if !strings.HasPrefix(flag, "-") {
1549 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1550 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1551 ctx.PropertyErrorf("kotlincflags",
1552 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1553 } else if inList(flag, config.KotlincIllegalFlags) {
1554 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1555 } else if flag == "-include-runtime" {
1556 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1557 } else {
1558 args := strings.Split(flag, " ")
1559 if args[0] == "-kotlin-home" {
1560 ctx.PropertyErrorf("kotlincflags",
1561 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1562 }
1563 }
1564 }
1565}
1566
Colin Cross8eadbf02017-10-24 17:46:00 -07001567func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001568 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001569
1570 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001571 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001572 // Compile java sources into turbine.jar.
1573 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1574 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1575 if ctx.Failed() {
1576 return nil
1577 }
1578 jars = append(jars, turbineJar)
1579 }
1580
Colin Cross55f63ea2018-08-27 12:37:09 -07001581 jars = append(jars, extraJars...)
1582
Nan Zhanged19fc32017-10-19 13:06:22 -07001583 // Combine any static header libraries into classes-header.jar. If there is only
1584 // one input jar this step will be skipped.
1585 var headerJar android.Path
1586 jars = append(jars, deps.staticHeaderJars...)
1587
Colin Cross5c6ecc12017-10-23 18:12:27 -07001588 // we cannot skip the combine step for now if there is only one jar
1589 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1590 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001591 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001592 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001593 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001594
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001595 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001596 // Transform classes.jar into classes-jarjar.jar
1597 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001598 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001599 headerJar = jarjarFile
1600 if ctx.Failed() {
1601 return nil
1602 }
1603 }
1604
1605 return headerJar
1606}
1607
Colin Crosscb933592017-11-22 13:49:43 -08001608func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001609 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001610
Colin Cross7a3139e2017-12-19 13:57:50 -08001611 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001612
Colin Cross84c38822018-01-03 15:59:46 -08001613 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001614 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1615
1616 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1617
1618 j.jacocoReportClassesFile = jacocoReportClassesFile
1619
1620 return instrumentedJar
1621}
1622
albaltai36ff7dc2018-12-25 14:35:23 +08001623var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001624
Nan Zhanged19fc32017-10-19 13:06:22 -07001625func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001626 if j.headerJarFile == nil {
1627 return nil
1628 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001629 return android.Paths{j.headerJarFile}
1630}
1631
1632func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001633 if j.implementationJarFile == nil {
1634 return nil
1635 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001636 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001637}
1638
Colin Crossf24a22a2019-01-31 14:12:44 -08001639func (j *Module) DexJar() android.Path {
1640 return j.dexJarFile
1641}
1642
Colin Cross331a1212018-08-15 20:40:52 -07001643func (j *Module) ResourceJars() android.Paths {
1644 if j.resourceJar == nil {
1645 return nil
1646 }
1647 return android.Paths{j.resourceJar}
1648}
1649
1650func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001651 if j.implementationAndResourcesJar == nil {
1652 return nil
1653 }
Colin Cross331a1212018-08-15 20:40:52 -07001654 return android.Paths{j.implementationAndResourcesJar}
1655}
1656
Colin Cross46c9b8b2017-06-22 16:51:17 -07001657func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001658 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001659 return j.exportAidlIncludeDirs
1660}
1661
Jiyong Park1be96912018-05-28 18:02:19 +09001662func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001663 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001664 return j.exportedSdkLibs
1665}
1666
Artur Satayev9cf46692019-11-26 18:08:34 +00001667func (j *Module) ExportedPlugins() (android.Paths, []string) {
1668 return j.exportedPluginJars, j.exportedPluginClasses
1669}
1670
Colin Cross0c4ce212019-05-03 15:28:19 -07001671func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1672 return j.srcJarArgs, j.srcJarDeps
1673}
1674
Colin Cross46c9b8b2017-06-22 16:51:17 -07001675var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001676
Colin Cross46c9b8b2017-06-22 16:51:17 -07001677func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001678 return j.logtagsSrcs
1679}
1680
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001681// Collect information for opening IDE project files in java/jdeps.go.
1682func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1683 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1684 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001685 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001686 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001687 if j.expandJarjarRules != nil {
1688 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001689 }
1690}
1691
1692func (j *Module) CompilerDeps() []string {
1693 jdeps := []string{}
1694 jdeps = append(jdeps, j.properties.Libs...)
1695 jdeps = append(jdeps, j.properties.Static_libs...)
1696 return jdeps
1697}
1698
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001699func (j *Module) hasCode(ctx android.ModuleContext) bool {
1700 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1701 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1702}
1703
Jiyong Parka7bc8ad2019-10-15 15:20:07 +09001704func (j *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1705 depTag := ctx.OtherModuleDependencyTag(dep)
1706 // dependencies other than the static linkage are all considered crossing APEX boundary
1707 return depTag == staticLibTag
1708}
1709
Jiyong Park0b238752019-10-29 11:23:10 +09001710func (j *Module) Stem() string {
1711 return proptools.StringDefault(j.deviceProperties.Stem, j.Name())
1712}
1713
Colin Cross2fe66872015-03-30 17:20:39 -07001714//
1715// Java libraries (.jar file)
1716//
1717
Colin Crossf506d872017-07-19 15:53:04 -07001718type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001719 Module
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001720
1721 InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -07001722}
1723
Colin Cross42be7612019-02-21 18:12:14 -08001724func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001725 // Store uncompressed (and do not strip) dex files from boot class path jars.
1726 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1727 return true
1728 }
1729
1730 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001731 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001732 return true
1733 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001734 if ctx.Config().UncompressPrivAppDex() &&
1735 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1736 return true
1737 }
1738
Colin Cross2fc72f62018-12-21 12:59:54 -08001739 return false
1740}
1741
Colin Crossf506d872017-07-19 15:53:04 -07001742func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001743 j.checkSdkVersion(ctx)
Jiyong Park0b238752019-10-29 11:23:10 +09001744 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross43f08db2018-11-12 10:13:39 -08001745 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001746 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001747 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001748 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001749 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001750
Jiyong Park7f7766d2019-07-25 22:02:35 +09001751 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1752 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001753 var extraInstallDeps android.Paths
1754 if j.InstallMixin != nil {
1755 extraInstallDeps = j.InstallMixin(ctx, j.outputFile)
1756 }
Colin Cross2c429dc2017-08-31 16:45:16 -07001757 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Colin Crossf0f2e2c2019-10-15 16:36:40 -07001758 ctx.ModuleName()+".jar", j.outputFile, extraInstallDeps...)
Colin Cross2c429dc2017-08-31 16:45:16 -07001759 }
Colin Crossb7a63242015-04-16 14:09:14 -07001760}
1761
Colin Crossf506d872017-07-19 15:53:04 -07001762func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001763 j.deps(ctx)
1764}
1765
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001766const (
Paul Duffina0dbf432019-12-05 11:25:53 +00001767 aidlIncludeDir = "aidl"
1768 javaDir = "java"
1769 jarFileSuffix = ".jar"
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001770)
1771
Paul Duffina0dbf432019-12-05 11:25:53 +00001772// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
1773func (j *Library) sdkSnapshotFilePathForJar() string {
1774 return filepath.Join(javaDir, j.Name()+jarFileSuffix)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001775}
1776
Paul Duffin13879572019-11-28 14:31:38 +00001777type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +00001778 android.SdkMemberTypeBase
Paul Duffin13879572019-11-28 14:31:38 +00001779}
1780
1781func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
1782 mctx.AddVariationDependencies(nil, dependencyTag, names...)
1783}
1784
1785func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
1786 _, ok := module.(*Library)
1787 return ok
1788}
1789
Paul Duffina0dbf432019-12-05 11:25:53 +00001790func (mt *librarySdkMemberType) buildSnapshot(
1791 sdkModuleContext android.ModuleContext,
1792 builder android.SnapshotBuilder,
1793 member android.SdkMember,
1794 jarToExportGetter func(j *Library) android.Path) {
1795
Paul Duffin13879572019-11-28 14:31:38 +00001796 variants := member.Variants()
1797 if len(variants) != 1 {
1798 sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
1799 for _, variant := range variants {
1800 sdkModuleContext.ModuleErrorf(" %q", variant)
1801 }
1802 }
1803 variant := variants[0]
1804 j := variant.(*Library)
1805
Paul Duffina0dbf432019-12-05 11:25:53 +00001806 exportedJar := jarToExportGetter(j)
1807 snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
1808 builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001809
1810 for _, dir := range j.AidlIncludeDirs() {
1811 // TODO(jiyong): copy parcelable declarations only
1812 aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil)
1813 for _, file := range aidlFiles {
1814 builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file))
1815 }
1816 }
1817
Paul Duffin9d8d6092019-12-05 18:19:29 +00001818 module := builder.AddPrebuiltModule(member, "java_import")
Paul Duffinb645ec82019-11-27 17:43:54 +00001819 module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
Paul Duffin0e0cf1d2019-11-12 19:39:25 +00001820}
1821
Paul Duffina0dbf432019-12-05 11:25:53 +00001822type headerLibrarySdkMemberType struct {
1823 librarySdkMemberType
1824}
1825
1826func (mt *headerLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1827 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1828 headerJars := j.HeaderJars()
1829 if len(headerJars) != 1 {
1830 panic(fmt.Errorf("there must be only one header jar from %q", j.Name()))
1831 }
1832
1833 return headerJars[0]
1834 })
1835}
1836
Paul Duffina0dbf432019-12-05 11:25:53 +00001837type implLibrarySdkMemberType struct {
1838 librarySdkMemberType
1839}
1840
1841func (mt *implLibrarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
1842 mt.librarySdkMemberType.buildSnapshot(sdkModuleContext, builder, member, func(j *Library) android.Path {
1843 implementationJars := j.ImplementationJars()
1844 if len(implementationJars) != 1 {
1845 panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
1846 }
1847
1848 return implementationJars[0]
1849 })
1850}
1851
Colin Cross1b16b0e2019-02-12 14:41:32 -08001852// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1853//
1854// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1855// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1856// as a `static_libs` dependency of another module.
1857//
1858// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1859// a device.
1860//
1861// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1862// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001863func LibraryFactory() android.Module {
1864 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001865
Colin Cross9ae1b922018-06-26 17:59:05 -07001866 module.AddProperties(
1867 &module.Module.properties,
1868 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001869 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001870 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001871
Jiyong Park7f7766d2019-07-25 22:02:35 +09001872 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09001873 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001874 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross9ae1b922018-06-26 17:59:05 -07001875 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001876}
1877
Colin Cross1b16b0e2019-02-12 14:41:32 -08001878// java_library_static is an obsolete alias for java_library.
1879func LibraryStaticFactory() android.Module {
1880 return LibraryFactory()
1881}
1882
1883// java_library_host builds and links sources into a `.jar` file for the host.
1884//
1885// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1886// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001887func LibraryHostFactory() android.Module {
1888 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001889
Colin Cross6af17aa2017-09-20 12:59:05 -07001890 module.AddProperties(
1891 &module.Module.properties,
1892 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001893
Colin Cross9ae1b922018-06-26 17:59:05 -07001894 module.Module.properties.Installable = proptools.BoolPtr(true)
1895
Jiyong Park7f7766d2019-07-25 22:02:35 +09001896 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09001897 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001898 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001899}
1900
1901//
Colin Crossb628ea52018-08-14 16:42:33 -07001902// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001903//
1904
1905type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001906 // list of compatibility suites (for example "cts", "vts") that the module should be
1907 // installed into.
1908 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001909
1910 // the name of the test configuration (for example "AndroidTest.xml") that should be
1911 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001912 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001913
Jack He33338892018-09-19 02:21:28 -07001914 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1915 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001916 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001917
Colin Crossd96ca352018-08-10 16:06:24 -07001918 // list of files or filegroup modules that provide data that should be installed alongside
1919 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001920 Data []string `android:"path"`
Dan Shi6ffaaa82019-09-26 11:41:36 -07001921
1922 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
1923 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
1924 // explicitly.
1925 Auto_gen_config *bool
Colin Cross05638fc2018-04-09 18:40:24 -07001926}
1927
Paul Duffin42df1442019-03-20 12:45:53 +00001928type testHelperLibraryProperties struct {
1929 // list of compatibility suites (for example "cts", "vts") that the module should be
1930 // installed into.
1931 Test_suites []string `android:"arch_variant"`
1932}
1933
Colin Cross05638fc2018-04-09 18:40:24 -07001934type Test struct {
1935 Library
1936
1937 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001938
1939 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001940 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001941}
1942
Paul Duffin42df1442019-03-20 12:45:53 +00001943type TestHelperLibrary struct {
1944 Library
1945
1946 testHelperLibraryProperties testHelperLibraryProperties
1947}
1948
Colin Cross303e21f2018-08-07 16:49:25 -07001949func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Shi6ffaaa82019-09-26 11:41:36 -07001950 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
1951 j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
Colin Cross8a497952019-03-05 22:25:09 -08001952 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001953
1954 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001955}
1956
Paul Duffin42df1442019-03-20 12:45:53 +00001957func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1958 j.Library.GenerateAndroidBuildActions(ctx)
1959}
1960
Colin Cross1b16b0e2019-02-12 14:41:32 -08001961// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1962// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1963//
1964// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1965// compiled against the device bootclasspath.
1966//
1967// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1968// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001969func TestFactory() android.Module {
1970 module := &Test{}
1971
1972 module.AddProperties(
1973 &module.Module.properties,
1974 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001975 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001976 &module.Module.protoProperties,
1977 &module.testProperties)
1978
Colin Cross9ae1b922018-06-26 17:59:05 -07001979 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001980 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001981
Colin Cross05638fc2018-04-09 18:40:24 -07001982 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001983 return module
1984}
1985
Paul Duffin42df1442019-03-20 12:45:53 +00001986// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1987func TestHelperLibraryFactory() android.Module {
1988 module := &TestHelperLibrary{}
1989
1990 module.AddProperties(
1991 &module.Module.properties,
1992 &module.Module.deviceProperties,
1993 &module.Module.dexpreoptProperties,
1994 &module.Module.protoProperties,
1995 &module.testHelperLibraryProperties)
1996
Colin Cross9a4abed2019-04-24 13:19:28 -07001997 module.Module.properties.Installable = proptools.BoolPtr(true)
1998 module.Module.dexpreopter.isTest = true
1999
Paul Duffin42df1442019-03-20 12:45:53 +00002000 InitJavaModule(module, android.HostAndDeviceSupported)
2001 return module
2002}
2003
Colin Cross1b16b0e2019-02-12 14:41:32 -08002004// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
2005// allow running the test with `atest` or a `TEST_MAPPING` file.
2006//
2007// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
2008// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07002009func TestHostFactory() android.Module {
2010 module := &Test{}
2011
2012 module.AddProperties(
2013 &module.Module.properties,
2014 &module.Module.protoProperties,
2015 &module.testProperties)
2016
Colin Cross9ae1b922018-06-26 17:59:05 -07002017 module.Module.properties.Installable = proptools.BoolPtr(true)
2018
Colin Cross05638fc2018-04-09 18:40:24 -07002019 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07002020 return module
2021}
2022
2023//
Colin Cross2fe66872015-03-30 17:20:39 -07002024// Java Binaries (.jar file plus wrapper script)
2025//
2026
Colin Crossf506d872017-07-19 15:53:04 -07002027type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07002028 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08002029 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07002030
2031 // Name of the class containing main to be inserted into the manifest as Main-Class.
2032 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07002033}
2034
Colin Crossf506d872017-07-19 15:53:04 -07002035type Binary struct {
2036 Library
Colin Cross2fe66872015-03-30 17:20:39 -07002037
Colin Crossf506d872017-07-19 15:53:04 -07002038 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07002039
Colin Cross6b4a32d2017-12-05 13:42:45 -08002040 isWrapperVariant bool
2041
Colin Crossc3315992017-12-08 19:12:36 -08002042 wrapperFile android.Path
Colin Cross70dda7e2019-10-01 22:05:35 -07002043 binaryFile android.InstallPath
Colin Cross2fe66872015-03-30 17:20:39 -07002044}
2045
Alex Light24237172017-10-26 09:46:21 -07002046func (j *Binary) HostToolPath() android.OptionalPath {
2047 return android.OptionalPathForPath(j.binaryFile)
2048}
2049
Colin Crossf506d872017-07-19 15:53:04 -07002050func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002051 if ctx.Arch().ArchType == android.Common {
2052 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07002053 if j.binaryProperties.Main_class != nil {
2054 if j.properties.Manifest != nil {
2055 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
2056 }
2057 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
2058 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
2059 j.overrideManifest = android.OptionalPathForPath(manifestFile)
2060 }
2061
Colin Cross6b4a32d2017-12-05 13:42:45 -08002062 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07002063 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002064 // Handle the binary wrapper
2065 j.isWrapperVariant = true
2066
Colin Cross366938f2017-12-11 16:29:02 -08002067 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08002068 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08002069 } else {
2070 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
2071 }
2072
2073 // Depend on the installed jar so that the wrapper doesn't get executed by
2074 // another build rule before the jar has been installed.
2075 jarFile := ctx.PrimaryModule().(*Binary).installFile
2076
2077 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
2078 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07002079 }
Colin Cross2fe66872015-03-30 17:20:39 -07002080}
2081
Colin Crossf506d872017-07-19 15:53:04 -07002082func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08002083 if ctx.Arch().ArchType == android.Common {
2084 j.deps(ctx)
2085 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07002086}
2087
Colin Cross1b16b0e2019-02-12 14:41:32 -08002088// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
2089// as well.
2090//
2091// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
2092// compiled against the device bootclasspath.
2093//
2094// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
2095// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002096func BinaryFactory() android.Module {
2097 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002098
Colin Cross36242852017-06-23 15:06:31 -07002099 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002100 &module.Module.properties,
2101 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08002102 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002103 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002104 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002105
Colin Cross9ae1b922018-06-26 17:59:05 -07002106 module.Module.properties.Installable = proptools.BoolPtr(true)
2107
Colin Cross6b4a32d2017-12-05 13:42:45 -08002108 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
2109 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002110 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002111}
2112
Colin Cross1b16b0e2019-02-12 14:41:32 -08002113// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
2114//
2115// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
2116// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07002117func BinaryHostFactory() android.Module {
2118 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07002119
Colin Cross36242852017-06-23 15:06:31 -07002120 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07002121 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07002122 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07002123 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07002124
Colin Cross9ae1b922018-06-26 17:59:05 -07002125 module.Module.properties.Installable = proptools.BoolPtr(true)
2126
Colin Cross6b4a32d2017-12-05 13:42:45 -08002127 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
2128 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002129 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002130}
2131
2132//
2133// Java prebuilts
2134//
2135
Colin Cross74d73e22017-08-02 11:05:49 -07002136type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08002137 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07002138
Nan Zhangea568a42017-11-08 21:20:04 -08002139 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07002140
2141 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09002142
2143 // List of shared java libs that this module has dependencies to
2144 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07002145
2146 // List of files to remove from the jar file(s)
2147 Exclude_files []string
2148
2149 // List of directories to remove from the jar file(s)
2150 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07002151
2152 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07002153 Jetifier *bool
Jiyong Park4c4c0242019-10-21 14:53:15 +09002154
2155 // set the name of the output
2156 Stem *string
Colin Cross74d73e22017-08-02 11:05:49 -07002157}
2158
2159type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002160 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07002161 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002162 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07002163 prebuilt android.Prebuilt
Jiyong Parkd1063c12019-07-17 20:08:41 +09002164 android.SdkBase
Colin Cross2fe66872015-03-30 17:20:39 -07002165
Colin Cross74d73e22017-08-02 11:05:49 -07002166 properties ImportProperties
2167
Colin Cross0a6e0072017-08-30 14:24:55 -07002168 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09002169 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07002170}
2171
Colin Cross83bb3162018-06-25 15:48:06 -07002172func (j *Import) sdkVersion() string {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09002173 return String(j.properties.Sdk_version)
Colin Cross83bb3162018-06-25 15:48:06 -07002174}
2175
2176func (j *Import) minSdkVersion() string {
2177 return j.sdkVersion()
2178}
2179
Colin Cross74d73e22017-08-02 11:05:49 -07002180func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07002181 return &j.prebuilt
2182}
2183
Colin Cross74d73e22017-08-02 11:05:49 -07002184func (j *Import) PrebuiltSrcs() []string {
2185 return j.properties.Jars
2186}
2187
2188func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07002189 return j.prebuilt.Name(j.ModuleBase.Name())
2190}
2191
Jiyong Park0b238752019-10-29 11:23:10 +09002192func (j *Import) Stem() string {
2193 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2194}
2195
Colin Cross74d73e22017-08-02 11:05:49 -07002196func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07002197 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07002198}
2199
Colin Cross74d73e22017-08-02 11:05:49 -07002200func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08002201 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07002202
Jiyong Park0b238752019-10-29 11:23:10 +09002203 jarName := j.Stem() + ".jar"
Nan Zhang4c819fb2018-08-27 18:31:46 -07002204 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07002205 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
2206 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07002207 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07002208 inputFile := outputFile
2209 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
2210 TransformJetifier(ctx, outputFile, inputFile)
2211 }
Colin Crosse9a275b2017-10-16 17:09:48 -07002212 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09002213
2214 ctx.VisitDirectDeps(func(module android.Module) {
2215 otherName := ctx.OtherModuleName(module)
2216 tag := ctx.OtherModuleDependencyTag(module)
2217
2218 switch dep := module.(type) {
2219 case Dependency:
2220 switch tag {
2221 case libTag, staticLibTag:
2222 // sdk lib names from dependencies are re-exported
2223 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
2224 }
2225 case SdkLibraryDependency:
2226 switch tag {
2227 case libTag:
2228 // names of sdk libs that are directly depended are exported
2229 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
2230 }
2231 }
2232 })
2233
2234 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002235 if Bool(j.properties.Installable) {
2236 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
Jiyong Park4c4c0242019-10-21 14:53:15 +09002237 jarName, outputFile)
Nan Zhang4973ecf2018-08-10 13:42:12 -07002238 }
Colin Cross2fe66872015-03-30 17:20:39 -07002239}
2240
Colin Cross74d73e22017-08-02 11:05:49 -07002241var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002242
Nan Zhanged19fc32017-10-19 13:06:22 -07002243func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002244 if j.combinedClasspathFile == nil {
2245 return nil
2246 }
Colin Cross37f6d792018-07-12 12:28:41 -07002247 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002248}
2249
2250func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002251 if j.combinedClasspathFile == nil {
2252 return nil
2253 }
Colin Cross37f6d792018-07-12 12:28:41 -07002254 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002255}
2256
Colin Cross331a1212018-08-15 20:40:52 -07002257func (j *Import) ResourceJars() android.Paths {
2258 return nil
2259}
2260
2261func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002262 if j.combinedClasspathFile == nil {
2263 return nil
2264 }
Colin Cross331a1212018-08-15 20:40:52 -07002265 return android.Paths{j.combinedClasspathFile}
2266}
2267
Colin Crossf24a22a2019-01-31 14:12:44 -08002268func (j *Import) DexJar() android.Path {
2269 return nil
2270}
2271
Colin Cross74d73e22017-08-02 11:05:49 -07002272func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002273 return nil
2274}
2275
Jiyong Park1be96912018-05-28 18:02:19 +09002276func (j *Import) ExportedSdkLibs() []string {
2277 return j.exportedSdkLibs
2278}
2279
Artur Satayev9cf46692019-11-26 18:08:34 +00002280func (j *Import) ExportedPlugins() (android.Paths, []string) {
2281 return nil, nil
2282}
2283
Colin Cross0c4ce212019-05-03 15:28:19 -07002284func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2285 return nil, nil
2286}
2287
albaltai36ff7dc2018-12-25 14:35:23 +08002288// Add compile time check for interface implementation
2289var _ android.IDEInfo = (*Import)(nil)
2290var _ android.IDECustomizedModuleName = (*Import)(nil)
2291
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002292// Collect information for opening IDE project files in java/jdeps.go.
2293const (
2294 removedPrefix = "prebuilt_"
2295)
2296
2297func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2298 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2299}
2300
2301func (j *Import) IDECustomizedModuleName() string {
2302 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2303 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2304 // solution to get the Import name.
2305 name := j.Name()
2306 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002307 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002308 }
2309 return name
2310}
2311
Colin Cross74d73e22017-08-02 11:05:49 -07002312var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002313
Colin Cross1b16b0e2019-02-12 14:41:32 -08002314// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2315//
2316// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2317// compiled against an Android classpath.
2318//
2319// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2320// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002321func ImportFactory() android.Module {
2322 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002323
Colin Cross74d73e22017-08-02 11:05:49 -07002324 module.AddProperties(&module.properties)
2325
2326 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002327 android.InitApexModule(module)
Jiyong Parkd1063c12019-07-17 20:08:41 +09002328 android.InitSdkAwareModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002329 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002330 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002331}
2332
Colin Cross1b16b0e2019-02-12 14:41:32 -08002333// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2334// module.
2335//
2336// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2337// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002338func ImportFactoryHost() android.Module {
2339 module := &Import{}
2340
2341 module.AddProperties(&module.properties)
2342
2343 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002344 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002345 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002346 return module
2347}
2348
Colin Cross42be7612019-02-21 18:12:14 -08002349// dex_import module
2350
2351type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002352 Jars []string `android:"path"`
Jiyong Park4c4c0242019-10-21 14:53:15 +09002353
2354 // set the name of the output
2355 Stem *string
Colin Cross42be7612019-02-21 18:12:14 -08002356}
2357
2358type DexImport struct {
2359 android.ModuleBase
2360 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002361 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002362 prebuilt android.Prebuilt
2363
2364 properties DexImportProperties
2365
2366 dexJarFile android.Path
2367 maybeStrippedDexJarFile android.Path
2368
2369 dexpreopter
2370}
2371
2372func (j *DexImport) Prebuilt() *android.Prebuilt {
2373 return &j.prebuilt
2374}
2375
2376func (j *DexImport) PrebuiltSrcs() []string {
2377 return j.properties.Jars
2378}
2379
2380func (j *DexImport) Name() string {
2381 return j.prebuilt.Name(j.ModuleBase.Name())
2382}
2383
Jiyong Park0b238752019-10-29 11:23:10 +09002384func (j *DexImport) Stem() string {
2385 return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name())
2386}
2387
Colin Cross42be7612019-02-21 18:12:14 -08002388func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2389 if len(j.properties.Jars) != 1 {
2390 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2391 }
2392
Jiyong Park0b238752019-10-29 11:23:10 +09002393 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")
Colin Cross42be7612019-02-21 18:12:14 -08002394 j.dexpreopter.isInstallable = true
2395 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2396
2397 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2398 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2399
2400 if j.dexpreopter.uncompressedDex {
2401 rule := android.NewRuleBuilder()
2402
2403 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2404 rule.Temporary(temporary)
2405
2406 // use zip2zip to uncompress classes*.dex files
2407 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002408 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002409 FlagWithInput("-i ", inputJar).
2410 FlagWithOutput("-o ", temporary).
2411 FlagWithArg("-0 ", "'classes*.dex'")
2412
2413 // use zipalign to align uncompressed classes*.dex files
2414 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002415 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002416 Flag("-f").
2417 Text("4").
2418 Input(temporary).
2419 Output(dexOutputFile)
2420
2421 rule.DeleteTemporaryFiles()
2422
2423 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2424 } else {
2425 ctx.Build(pctx, android.BuildParams{
2426 Rule: android.Cp,
2427 Input: inputJar,
2428 Output: dexOutputFile,
2429 })
2430 }
2431
2432 j.dexJarFile = dexOutputFile
2433
2434 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2435
2436 j.maybeStrippedDexJarFile = dexOutputFile
2437
2438 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2439 ctx.ModuleName()+".jar", dexOutputFile)
2440}
2441
2442func (j *DexImport) DexJar() android.Path {
2443 return j.dexJarFile
2444}
2445
2446// dex_import imports a `.jar` file containing classes.dex files.
2447//
2448// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2449// to the device.
2450func DexImportFactory() android.Module {
2451 module := &DexImport{}
2452
2453 module.AddProperties(&module.properties)
2454
2455 android.InitPrebuiltModule(module, &module.properties.Jars)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002456 android.InitApexModule(module)
Jooyung Han18020ea2019-11-13 10:50:48 +09002457 InitJavaModule(module, android.DeviceSupported)
Colin Cross42be7612019-02-21 18:12:14 -08002458 return module
2459}
2460
Colin Cross89536d42017-07-07 14:35:50 -07002461//
2462// Defaults
2463//
2464type Defaults struct {
2465 android.ModuleBase
2466 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002467 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002468}
2469
Colin Cross1b16b0e2019-02-12 14:41:32 -08002470// java_defaults provides a set of properties that can be inherited by other java or android modules.
2471//
2472// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2473// property in the defaults module that exists in the depending module will be prepended to the depending module's
2474// value for that property.
2475//
2476// Example:
2477//
2478// java_defaults {
2479// name: "example_defaults",
2480// srcs: ["common/**/*.java"],
2481// javacflags: ["-Xlint:all"],
2482// aaptflags: ["--auto-add-overlay"],
2483// }
2484//
2485// java_library {
2486// name: "example",
2487// defaults: ["example_defaults"],
2488// srcs: ["example/**/*.java"],
2489// }
2490//
2491// is functionally identical to:
2492//
2493// java_library {
2494// name: "example",
2495// srcs: [
2496// "common/**/*.java",
2497// "example/**/*.java",
2498// ],
2499// javacflags: ["-Xlint:all"],
2500// }
Colin Cross89536d42017-07-07 14:35:50 -07002501func defaultsFactory() android.Module {
2502 return DefaultsFactory()
2503}
2504
Paul Duffin47357662019-12-05 14:07:14 +00002505func DefaultsFactory() android.Module {
Colin Cross89536d42017-07-07 14:35:50 -07002506 module := &Defaults{}
2507
Colin Cross89536d42017-07-07 14:35:50 -07002508 module.AddProperties(
2509 &CompilerProperties{},
2510 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002511 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002512 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002513 &aaptProperties{},
2514 &androidLibraryProperties{},
2515 &appProperties{},
2516 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002517 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002518 &ImportProperties{},
2519 &AARImportProperties{},
2520 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002521 &DexImportProperties{},
Jooyung Han18020ea2019-11-13 10:50:48 +09002522 &android.ApexProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002523 )
2524
2525 android.InitDefaultsModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002526 return module
2527}
Nan Zhangea568a42017-11-08 21:20:04 -08002528
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002529func kytheExtractJavaFactory() android.Singleton {
2530 return &kytheExtractJavaSingleton{}
2531}
2532
2533type kytheExtractJavaSingleton struct {
2534}
2535
2536func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2537 var xrefTargets android.Paths
2538 ctx.VisitAllModules(func(module android.Module) {
2539 if javaModule, ok := module.(xref); ok {
2540 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2541 }
2542 })
2543 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2544 if len(xrefTargets) > 0 {
2545 ctx.Build(pctx, android.BuildParams{
2546 Rule: blueprint.Phony,
2547 Output: android.PathForPhony(ctx, "xref_java"),
2548 Inputs: xrefTargets,
2549 })
2550 }
2551}
2552
Nan Zhangea568a42017-11-08 21:20:04 -08002553var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002554var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002555var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002556var inList = android.InList